From 40d01eb4cece210f6336d786ae0acb7f26c448c4 Mon Sep 17 00:00:00 2001 From: cuz Date: Thu, 23 Sep 2004 17:34:03 +0000 Subject: [PATCH] Fixed bug with braces in initializer lists git-svn-id: svn://svn.cc65.org/cc65/trunk@3193 b7a2c559-68d2-44c3-8de9-860c34a00d81 --- src/cc65/declare.c | 69 ++++++++++++++++++++++++++++++++++++++++++++-- src/cc65/expr.c | 38 ------------------------- src/cc65/expr.h | 3 -- 3 files changed, 67 insertions(+), 43 deletions(-) diff --git a/src/cc65/declare.c b/src/cc65/declare.c index d71938c6f..44b8ddf05 100644 --- a/src/cc65/declare.c +++ b/src/cc65/declare.c @@ -1211,6 +1211,50 @@ static void ClosingCurlyBraces (unsigned BracesExpected) +static void DefineData (ExprDesc* Expr) +/* Output a data definition for the given expression */ +{ + switch (ED_GetLoc (Expr)) { + + case E_LOC_ABS: + /* Absolute: numeric address or const */ + g_defdata (TypeOf (Expr->Type) | CF_CONST, Expr->IVal, 0); + break; + + case E_LOC_GLOBAL: + /* Global variable */ + g_defdata (CF_EXTERNAL, Expr->Name, Expr->IVal); + break; + + case E_LOC_STATIC: + case E_LOC_LITERAL: + /* Static variable or literal in the literal pool */ + g_defdata (CF_STATIC, Expr->Name, Expr->IVal); + break; + + case E_LOC_REGISTER: + /* Register variable. Taking the address is usually not + * allowed. + */ + if (IS_Get (&AllowRegVarAddr) == 0) { + Error ("Cannot take the address of a register variable"); + } + g_defdata (CF_REGVAR, Expr->Name, Expr->IVal); + break; + + case E_LOC_STACK: + case E_LOC_PRIMARY: + case E_LOC_EXPR: + Error ("Non constant initializer"); + break; + + default: + Internal ("Unknown constant type: 0x%04X", ED_GetLoc (Expr)); + } +} + + + static unsigned ParseScalarInit (type* T) /* Parse initializaton for scalar data types. Return the number of data bytes. */ { @@ -1276,10 +1320,24 @@ static unsigned ParseArrayInit (type* T, int AllowFlexibleMembers) long ElementCount = GetElementCount (T); /* Special handling for a character array initialized by a literal */ - if (IsTypeChar (ElementType) && CurTok.Tok == TOK_SCONST) { + if (IsTypeChar (ElementType) && + (CurTok.Tok == TOK_SCONST || + (CurTok.Tok == TOK_LCURLY && NextTok.Tok == TOK_SCONST))) { /* Char array initialized by string constant */ - const char* Str = GetLiteral (CurTok.IVal); + int NeedParen; + const char* Str; + + /* If we initializer is enclosed in brackets, remember this fact and + * skip the opening bracket. + */ + NeedParen = (CurTok.Tok == TOK_LCURLY); + if (NeedParen) { + NextToken (); + } + + /* Get the initializer string and its size */ + Str = GetLiteral (CurTok.IVal); Count = GetLiteralPoolOffs () - CurTok.IVal; /* Translate into target charset */ @@ -1302,6 +1360,13 @@ static unsigned ParseArrayInit (type* T, int AllowFlexibleMembers) ResetLiteralPoolOffs (CurTok.IVal); NextToken (); + /* If the initializer was enclosed in curly braces, we need a closing + * one. + */ + if (NeedParen) { + ConsumeRCurly (); + } + } else { /* Curly brace */ diff --git a/src/cc65/expr.c b/src/cc65/expr.c index b539ba138..b47040096 100644 --- a/src/cc65/expr.c +++ b/src/cc65/expr.c @@ -181,44 +181,6 @@ static unsigned typeadjust (ExprDesc* lhs, ExprDesc* rhs, int NoPush) -void DefineData (ExprDesc* Expr) -/* Output a data definition for the given expression */ -{ - switch (ED_GetLoc (Expr)) { - - case E_LOC_ABS: - /* Absolute: numeric address or const */ - g_defdata (TypeOf (Expr->Type) | CF_CONST, Expr->IVal, 0); - break; - - case E_LOC_GLOBAL: - /* Global variable */ - g_defdata (CF_EXTERNAL, Expr->Name, Expr->IVal); - break; - - case E_LOC_STATIC: - case E_LOC_LITERAL: - /* Static variable or literal in the literal pool */ - g_defdata (CF_STATIC, Expr->Name, Expr->IVal); - break; - - case E_LOC_REGISTER: - /* Register variable. Taking the address is usually not - * allowed. - */ - if (IS_Get (&AllowRegVarAddr) == 0) { - Error ("Cannot take the address of a register variable"); - } - g_defdata (CF_REGVAR, Expr->Name, Expr->IVal); - break; - - default: - Internal ("Unknown constant type: 0x%04X", ED_GetLoc (Expr)); - } -} - - - static int kcalc (token_t tok, long val1, long val2) /* Calculate an operation with left and right operand constant. */ { diff --git a/src/cc65/expr.h b/src/cc65/expr.h index e166dd5ee..990bed683 100644 --- a/src/cc65/expr.h +++ b/src/cc65/expr.h @@ -82,9 +82,6 @@ void hie1 (ExprDesc* lval); void hie0 (ExprDesc* Expr); /* Parse comma operator. */ -void DefineData (ExprDesc* lval); -/* Output a data definition for the given expression */ - /* End of expr.h */ -- 2.39.5