From 6beb090193ee932ee67bed553f54d721c48c276d Mon Sep 17 00:00:00 2001 From: acqn Date: Mon, 3 Jan 2022 23:54:14 +0800 Subject: [PATCH] Fixed anonymous bit-fields declared with typedef'ed type names. --- src/cc65/declare.c | 2 +- src/cc65/symtab.c | 95 +++++++++++++++++++++++++++++++++++++--------- src/cc65/symtab.h | 23 ++++++++++- 3 files changed, 100 insertions(+), 20 deletions(-) diff --git a/src/cc65/declare.c b/src/cc65/declare.c index 8af67524f..fa4c52818 100644 --- a/src/cc65/declare.c +++ b/src/cc65/declare.c @@ -1462,7 +1462,7 @@ static void ParseTypeSpec (DeclSpec* D, long Default, TypeCode Qualifiers, case TOK_IDENT: /* This could be a label */ - if (NextTok.Tok != TOK_COLON) { + if (NextTok.Tok != TOK_COLON || GetLexicalLevel () == LEX_LEVEL_STRUCT) { Entry = FindSym (CurTok.Ident); if (Entry && SymIsTypeDef (Entry)) { /* It's a typedef */ diff --git a/src/cc65/symtab.c b/src/cc65/symtab.c index 3de0bb460..c1c5c4696 100644 --- a/src/cc65/symtab.c +++ b/src/cc65/symtab.c @@ -88,7 +88,8 @@ SymTable EmptySymTab = { #define SYMTAB_SIZE_LABEL 7U /* The current and root symbol tables */ -static unsigned LexicalLevel = 0; /* For safety checks */ +static unsigned LexLevelDepth = 0; /* For safety checks */ +static LexicalLevel* CurrentLex = 0; static SymTable* SymTab0 = 0; static SymTable* SymTab = 0; static SymTable* TagTab0 = 0; @@ -213,10 +214,46 @@ static void CheckSymTable (SymTable* Tab) +unsigned GetLexicalLevelDepth (void) +/* Return the current lexical level depth */ +{ + return LexLevelDepth; +} + + + unsigned GetLexicalLevel (void) /* Return the current lexical level */ { - return LexicalLevel; + if (CurrentLex != 0) { + return CurrentLex->CurrentLevel; + } + return LEX_LEVEL_NONE; +} + + + +void PushLexicalLevel (unsigned NewLevel) +/* Enter the specified lexical level */ +{ + LexicalLevel* L = xmalloc (sizeof (LexicalLevel)); + L->PrevLex = CurrentLex; + CurrentLex = L; + CurrentLex->CurrentLevel = NewLevel; + ++LexLevelDepth; +} + + + +void PopLexicalLevel (void) +/* Exit the current lexical level */ +{ + LexicalLevel* L; + PRECONDITION (CurrentLex != 0 && LexLevelDepth > 0); + L = CurrentLex; + CurrentLex = L->PrevLex; + xfree (L); + --LexLevelDepth; } @@ -225,7 +262,10 @@ void EnterGlobalLevel (void) /* Enter the program global lexical level */ { /* Safety */ - PRECONDITION (++LexicalLevel == LEX_LEVEL_GLOBAL); + PRECONDITION (GetLexicalLevel () == LEX_LEVEL_NONE); + + /* Enter global lexical level */ + PushLexicalLevel (LEX_LEVEL_GLOBAL); /* Create and assign the symbol table */ SymTab0 = SymTab = NewSymTable (SYMTAB_SIZE_GLOBAL); @@ -246,7 +286,7 @@ void LeaveGlobalLevel (void) /* Leave the program global lexical level */ { /* Safety */ - PRECONDITION (LexicalLevel-- == LEX_LEVEL_GLOBAL); + PRECONDITION (GetLexicalLevel () == LEX_LEVEL_GLOBAL); /* Check the tables */ CheckSymTable (SymTab0); @@ -260,6 +300,9 @@ void LeaveGlobalLevel (void) /* Don't delete the symbol and struct tables! */ SymTab = 0; TagTab = 0; + + /* Exit global lexical level */ + PopLexicalLevel (); } @@ -269,8 +312,8 @@ void EnterFunctionLevel (void) { SymTable* S; - /* New lexical level */ - ++LexicalLevel; + /* Enter function lexical level */ + PushLexicalLevel (LEX_LEVEL_FUNCTION); /* Get a new symbol table and make it current */ S = NewSymTable (SYMTAB_SIZE_FUNCTION); @@ -293,8 +336,11 @@ void EnterFunctionLevel (void) void RememberFunctionLevel (struct FuncDesc* F) /* Remember the symbol tables for the level and leave the level without checks */ { - /* Leave the lexical level */ - --LexicalLevel; + /* Safety */ + PRECONDITION (GetLexicalLevel () == LEX_LEVEL_FUNCTION); + + /* Leave function lexical level */ + PopLexicalLevel (); /* Remember the tables */ F->SymTab = SymTab; @@ -311,8 +357,8 @@ void RememberFunctionLevel (struct FuncDesc* F) void ReenterFunctionLevel (struct FuncDesc* F) /* Reenter the function lexical level using the existing tables from F */ { - /* New lexical level */ - ++LexicalLevel; + /* Enter function lexical level */ + PushLexicalLevel (LEX_LEVEL_FUNCTION); /* Make the tables current again */ F->SymTab->PrevTab = SymTab; @@ -330,8 +376,11 @@ void ReenterFunctionLevel (struct FuncDesc* F) void LeaveFunctionLevel (void) /* Leave function lexical level */ { - /* Leave the lexical level */ - --LexicalLevel; + /* Safety */ + PRECONDITION (GetLexicalLevel () == LEX_LEVEL_FUNCTION); + + /* Leave function lexical level */ + PopLexicalLevel (); /* Check the tables */ CheckSymTable (SymTab); @@ -355,8 +404,8 @@ void EnterBlockLevel (void) { SymTable* S; - /* New lexical level */ - ++LexicalLevel; + /* Enter block lexical level */ + PushLexicalLevel (LEX_LEVEL_BLOCK); /* Get a new symbol table and make it current */ S = NewSymTable (SYMTAB_SIZE_BLOCK); @@ -374,8 +423,11 @@ void EnterBlockLevel (void) void LeaveBlockLevel (void) /* Leave a nested block in a function */ { - /* Leave the lexical level */ - --LexicalLevel; + /* Safety */ + PRECONDITION (GetLexicalLevel () == LEX_LEVEL_BLOCK); + + /* Leave block lexical level */ + PopLexicalLevel (); /* Check the tables */ CheckSymTable (SymTab); @@ -392,6 +444,9 @@ void EnterStructLevel (void) { SymTable* S; + /* Enter struct lexical level */ + PushLexicalLevel (LEX_LEVEL_STRUCT); + /* Get a new symbol table and make it current. Note: Structs and enums ** nested in struct scope are NOT local to the struct but visible in the ** outside scope. So we will NOT create a new struct or enum table. @@ -406,6 +461,12 @@ void EnterStructLevel (void) void LeaveStructLevel (void) /* Leave a nested block for a struct definition */ { + /* Safety */ + PRECONDITION (GetLexicalLevel () == LEX_LEVEL_STRUCT); + + /* Leave struct lexical level */ + PopLexicalLevel (); + /* Don't delete the table */ FieldTab = FieldTab->PrevTab; } @@ -1398,7 +1459,7 @@ void EmitDebugInfo (void) /* For cosmetic reasons in the output file, we will insert two tabs ** on global level and just one on local level. */ - if (LexicalLevel == LEX_LEVEL_GLOBAL) { + if (GetLexicalLevel () == LEX_LEVEL_GLOBAL) { Head = "\t.dbg\t\tsym"; } else { Head = "\t.dbg\tsym"; diff --git a/src/cc65/symtab.h b/src/cc65/symtab.h index 469a4ba77..1df61a822 100644 --- a/src/cc65/symtab.h +++ b/src/cc65/symtab.h @@ -65,12 +65,22 @@ struct SymTable { /* An empty symbol table */ extern SymTable EmptySymTab; -/* Forwards */ -struct FuncDesc; +/* Lexical level linked list node type */ +typedef struct LexicalLevel LexicalLevel; +struct LexicalLevel { + LexicalLevel* PrevLex; + unsigned CurrentLevel; +}; /* Predefined lexical levels */ +#define LEX_LEVEL_NONE 0U #define LEX_LEVEL_GLOBAL 1U #define LEX_LEVEL_FUNCTION 2U +#define LEX_LEVEL_BLOCK 3U +#define LEX_LEVEL_STRUCT 4U + +/* Forwards */ +struct FuncDesc; @@ -80,9 +90,18 @@ struct FuncDesc; +unsigned GetLexicalLevelDepth (void); +/* Return the current lexical level depth */ + unsigned GetLexicalLevel (void); /* Return the current lexical level */ +void PushLexicalLevel (unsigned NewLevel); +/* Enter the specified lexical level */ + +void PopLexicalLevel (void); +/* Exit the current lexical level */ + void EnterGlobalLevel (void); /* Enter the program global lexical level */