From d171b3dac96e836a6b952b14951d0479c31ab280 Mon Sep 17 00:00:00 2001 From: cuz Date: Tue, 1 Aug 2000 19:05:24 +0000 Subject: [PATCH] Check for const in function parameters (first level only). Place local static const data into the RODATA segment. git-svn-id: svn://svn.cc65.org/cc65/trunk@253 b7a2c559-68d2-44c3-8de9-860c34a00d81 --- src/cc65/error.c | 3 ++- src/cc65/error.h | 1 + src/cc65/expr.c | 10 ++++++++++ src/cc65/locals.c | 26 +++++++++++++++----------- 4 files changed, 28 insertions(+), 12 deletions(-) diff --git a/src/cc65/error.c b/src/cc65/error.c index 0821189e1..4a8a0ecb7 100644 --- a/src/cc65/error.c +++ b/src/cc65/error.c @@ -152,7 +152,8 @@ static char* ErrMsg [ERR_COUNT-1] = { "Variable has unknown size", "Unknown identifier: `%s'", "Duplicate qualifier: `%s'", - "Assignment to const", + "Assignment discards `const' qualifier", + "Passing argument %u discards `const' qualifier", }; diff --git a/src/cc65/error.h b/src/cc65/error.h index 623dae4ff..4220d86a5 100644 --- a/src/cc65/error.h +++ b/src/cc65/error.h @@ -148,6 +148,7 @@ enum Errors { ERR_UNKNOWN_IDENT, ERR_DUPLICATE_QUALIFIER, ERR_CONST_ASSIGN, + ERR_CONST_PARAM, ERR_COUNT /* Error count */ }; diff --git a/src/cc65/expr.c b/src/cc65/expr.c index 7717c780f..434813220 100644 --- a/src/cc65/expr.c +++ b/src/cc65/expr.c @@ -580,6 +580,16 @@ static void callfunction (struct expent* lval) * convert the actual argument to the type needed. */ if (!Ellipsis) { + /* If the left side is not const and the right is const, print + * an error. Note: This is an incomplete check, since other parts + * of the type string may have a const qualifier, but it catches + * some errors and is cheap here. We will redo it the right way + * as soon as the parser is rewritten. #### + */ + if (!IsConst (Param->Type) && IsConst (lval2.e_tptr)) { + Error (ERR_CONST_PARAM, ParamCount); + } + /* Promote the argument if needed */ assignadjust (Param->Type, &lval2); /* If we have a prototype, chars may be pushed as chars */ diff --git a/src/cc65/locals.c b/src/cc65/locals.c index 25fb3262f..3a6e6a5ab 100644 --- a/src/cc65/locals.c +++ b/src/cc65/locals.c @@ -191,7 +191,7 @@ static void ParseOneDecl (const DeclSpec* Spec) /* Setup the type flags for the assignment */ flags = Size == 1? CF_FORCECHAR : CF_NONE; - /* Get the expression into the primary */ + /* Get the expression into the primary */ if (evalexpr (flags, hie1, &lval) == 0) { /* Constant expression. Adjust the types */ assignadjust (Decl.Type, &lval); @@ -268,20 +268,24 @@ static void ParseOneDecl (const DeclSpec* Spec) /* Static data */ if (curtok == TOK_ASSIGN) { - /* Initialization ahead, switch to data segment */ - g_usedata (); + /* Initialization ahead, switch to data segment */ + if (IsConst (Decl.Type)) { + g_userodata (); + } else { + g_usedata (); + } - /* Define the variable label */ - SymData = GetLabel (); - g_defloclabel (SymData); + /* Define the variable label */ + SymData = GetLabel (); + g_defloclabel (SymData); - /* Skip the '=' */ - NextToken (); + /* Skip the '=' */ + NextToken (); - /* Allow initialization of static vars */ - ParseInit (Decl.Type); + /* Allow initialization of static vars */ + ParseInit (Decl.Type); - /* Mark the variable as referenced */ + /* Mark the variable as referenced */ SC |= SC_REF; } else { -- 2.39.5