From fd679d92d0054a26f10dbcbd53e90cce5690bbb4 Mon Sep 17 00:00:00 2001 From: uz Date: Sat, 2 Feb 2013 22:31:26 +0000 Subject: [PATCH] Fixed a problem: When a struct or unit was declared with a tag name, it was possible to use the opposite type with the tag name. That is "struct a" after declaring "union a" and vice versa. git-svn-id: svn://svn.cc65.org/cc65/trunk@5980 b7a2c559-68d2-44c3-8de9-860c34a00d81 --- src/cc65/compile.c | 4 ++-- src/cc65/declare.c | 28 ++++++++++++++-------------- src/cc65/locals.c | 6 +++--- src/cc65/symentry.c | 7 ++++--- src/cc65/symentry.h | 10 ++++++---- src/cc65/symtab.c | 15 ++++++++++----- src/cc65/symtab.h | 4 ++-- 7 files changed, 41 insertions(+), 33 deletions(-) diff --git a/src/cc65/compile.c b/src/cc65/compile.c index f619d5b59..c87d32729 100644 --- a/src/cc65/compile.c +++ b/src/cc65/compile.c @@ -6,7 +6,7 @@ /* */ /* */ /* */ -/* (C) 2000-2012, Ullrich von Bassewitz */ +/* (C) 2000-2013, Ullrich von Bassewitz */ /* Roemerstrasse 52 */ /* D-70794 Filderstadt */ /* EMail: uz@cc65.org */ @@ -150,7 +150,7 @@ static void Parse (void) * This means that "extern int i;" will not get storage allocated. */ if ((Decl.StorageClass & SC_FUNC) != SC_FUNC && - (Decl.StorageClass & SC_TYPEDEF) != SC_TYPEDEF && + (Decl.StorageClass & SC_TYPEMASK) != SC_TYPEDEF && ((Spec.Flags & DS_DEF_STORAGE) != 0 || (Decl.StorageClass & (SC_EXTERN|SC_STATIC)) == SC_STATIC || ((Decl.StorageClass & SC_EXTERN) != 0 && diff --git a/src/cc65/declare.c b/src/cc65/declare.c index 800000105..5f4a0f10f 100644 --- a/src/cc65/declare.c +++ b/src/cc65/declare.c @@ -6,7 +6,7 @@ /* */ /* */ /* */ -/* (C) 1998-2010, Ullrich von Bassewitz */ +/* (C) 1998-2013, Ullrich von Bassewitz */ /* Roemerstrasse 52 */ /* D-70794 Filderstadt */ /* EMail: uz@cc65.org */ @@ -527,17 +527,17 @@ static int ParseFieldWidth (Declaration* Decl) -static SymEntry* StructOrUnionForwardDecl (const char* Name) +static SymEntry* StructOrUnionForwardDecl (const char* Name, unsigned Type) /* Handle a struct or union forward decl */ { - /* Try to find a struct with the given name. If there is none, + /* Try to find a struct/union with the given name. If there is none, * insert a forward declaration into the current lexical level. */ SymEntry* Entry = FindTagSym (Name); if (Entry == 0) { - Entry = AddStructSym (Name, 0, 0); - } else if (SymIsLocal (Entry) && (Entry->Flags & SC_STRUCT) != SC_STRUCT) { - /* Already defined in the level, but no struct */ + Entry = AddStructSym (Name, Type, 0, 0); + } else if ((Entry->Flags & SC_TYPEMASK) != Type) { + /* Already defined, but no struct */ Error ("Symbol `%s' is already different kind", Name); } return Entry; @@ -574,7 +574,7 @@ static unsigned CopyAnonStructFields (const Declaration* Decl, int Offs) /* Enter a copy of this symbol adjusting the offset. We will just * reuse the type string here. */ - AddLocalSym (Entry->Name, Entry->Type,SC_STRUCTFIELD, Offs + Entry->V.Offs); + AddLocalSym (Entry->Name, Entry->Type, SC_STRUCTFIELD, Offs + Entry->V.Offs); /* Currently, there can not be any attributes, but if there will be * some in the future, we want to know this. @@ -604,11 +604,11 @@ static SymEntry* ParseUnionDecl (const char* Name) if (CurTok.Tok != TOK_LCURLY) { /* Just a forward declaration. */ - return StructOrUnionForwardDecl (Name); + return StructOrUnionForwardDecl (Name, SC_UNION); } /* Add a forward declaration for the struct in the current lexical level */ - Entry = AddStructSym (Name, 0, 0); + Entry = AddStructSym (Name, SC_UNION, 0, 0); /* Skip the curly brace */ NextToken (); @@ -688,7 +688,7 @@ NextMember: if (CurTok.Tok != TOK_COMMA) { LeaveStructLevel (); /* Make a real entry from the forward decl and return it */ - return AddStructSym (Name, UnionSize, FieldTab); + return AddStructSym (Name, SC_UNION, UnionSize, FieldTab); } @@ -707,11 +707,11 @@ static SymEntry* ParseStructDecl (const char* Name) if (CurTok.Tok != TOK_LCURLY) { /* Just a forward declaration. */ - return StructOrUnionForwardDecl (Name); + return StructOrUnionForwardDecl (Name, SC_STRUCT); } /* Add a forward declaration for the struct in the current lexical level */ - Entry = AddStructSym (Name, 0, 0); + Entry = AddStructSym (Name, SC_STRUCT, 0, 0); /* Skip the curly brace */ NextToken (); @@ -858,7 +858,7 @@ NextMember: if (CurTok.Tok != TOK_COMMA) { LeaveStructLevel (); /* Make a real entry from the forward decl and return it */ - return AddStructSym (Name, StructSize, FieldTab); + return AddStructSym (Name, SC_STRUCT, StructSize, FieldTab); } @@ -1613,7 +1613,7 @@ void ParseDecl (const DeclSpec* Spec, Declaration* D, declmode_t Mode) * int declaration. */ if ((D->StorageClass & SC_FUNC) != SC_FUNC && - (D->StorageClass & SC_TYPEDEF) != SC_TYPEDEF) { + (D->StorageClass & SC_TYPEMASK) != SC_TYPEDEF) { /* If the standard was not set explicitly to C89, print a warning * for variables with implicit int type. */ diff --git a/src/cc65/locals.c b/src/cc65/locals.c index ef7420475..20b6b511e 100644 --- a/src/cc65/locals.c +++ b/src/cc65/locals.c @@ -6,7 +6,7 @@ /* */ /* */ /* */ -/* (C) 2000-2009, Ullrich von Bassewitz */ +/* (C) 2000-2013, Ullrich von Bassewitz */ /* Roemerstrasse 52 */ /* D-70794 Filderstadt */ /* EMail: uz@cc65.org */ @@ -176,7 +176,7 @@ static void ParseRegisterDecl (Declaration* Decl, int Reg) -static void ParseAutoDecl (Declaration* Decl) +static void ParseAutoDecl (Declaration* Decl) /* Parse the declaration of an auto variable. */ { unsigned Flags; @@ -440,7 +440,7 @@ static void ParseOneDecl (const DeclSpec* Spec) /* Handle anything that needs storage (no functions, no typdefs) */ if ((Decl.StorageClass & SC_FUNC) != SC_FUNC && - (Decl.StorageClass & SC_TYPEDEF) != SC_TYPEDEF) { + (Decl.StorageClass & SC_TYPEMASK) != SC_TYPEDEF) { /* If we have a register variable, try to allocate a register and * convert the declaration to "auto" if this is not possible. diff --git a/src/cc65/symentry.c b/src/cc65/symentry.c index 1644c962f..67654a9f0 100644 --- a/src/cc65/symentry.c +++ b/src/cc65/symentry.c @@ -6,7 +6,7 @@ /* */ /* */ /* */ -/* (C) 2000-2009, Ullrich von Bassewitz */ +/* (C) 2000-2013, Ullrich von Bassewitz */ /* Roemerstrasse 52 */ /* D-70794 Filderstadt */ /* EMail: uz@cc65.org */ @@ -100,6 +100,7 @@ void DumpSymEntry (FILE* F, const SymEntry* E) { "SC_TYPEDEF", SC_TYPEDEF }, { "SC_BITFIELD", SC_BITFIELD }, { "SC_STRUCTFIELD", SC_STRUCTFIELD }, + { "SC_UNION", SC_UNION }, { "SC_STRUCT", SC_STRUCT }, { "SC_AUTO", SC_AUTO }, { "SC_REGISTER", SC_REGISTER }, @@ -125,7 +126,7 @@ void DumpSymEntry (FILE* F, const SymEntry* E) /* Print the assembler name if we have one */ if (E->AsmName) { fprintf (F, " AsmName: %s\n", E->AsmName); - } + } /* Print the flags */ SymFlags = E->Flags; @@ -216,7 +217,7 @@ void SymSetAsmName (SymEntry* Sym) /* Cannot be used to change the name */ PRECONDITION (Sym->AsmName == 0); - + /* The assembler name starts with an underline */ Len = strlen (Sym->Name); Sym->AsmName = xmalloc (Len + 2); diff --git a/src/cc65/symentry.h b/src/cc65/symentry.h index 08cac8d80..54a7d32ad 100644 --- a/src/cc65/symentry.h +++ b/src/cc65/symentry.h @@ -6,7 +6,7 @@ /* */ /* */ /* */ -/* (C) 2000-2009, Ullrich von Bassewitz */ +/* (C) 2000-2013, Ullrich von Bassewitz */ /* Roemerstrasse 52 */ /* D-70794 Filderstadt */ /* EMail: uz@cc65.org */ @@ -87,10 +87,12 @@ struct LiteralPool; #define SC_REF 0x2000U /* Symbol is referenced */ #define SC_TYPE 0x4000U /* This is a type, struct, typedef, etc. */ -#define SC_STRUCT 0x4001U /* Struct or union */ -#define SC_STRUCTFIELD 0x4002U /* Struct or union field */ +#define SC_STRUCT 0x4001U /* Struct */ +#define SC_UNION 0x4002U /* Union */ +#define SC_STRUCTFIELD 0x4003U /* Struct or union field */ #define SC_BITFIELD 0x4004U /* A bit-field inside a struct or union */ -#define SC_TYPEDEF 0x4008U /* A typedef */ +#define SC_TYPEDEF 0x4005U /* A typedef */ +#define SC_TYPEMASK 0x400FU /* Mask for above types */ #define SC_ZEROPAGE 0x8000U /* Symbol marked as zeropage */ diff --git a/src/cc65/symtab.c b/src/cc65/symtab.c index 23b19695e..d7a4b92dc 100644 --- a/src/cc65/symtab.c +++ b/src/cc65/symtab.c @@ -6,7 +6,7 @@ /* */ /* */ /* */ -/* (C) 2000-2011, Ullrich von Bassewitz */ +/* (C) 2000-2013, Ullrich von Bassewitz */ /* Roemerstrasse 52 */ /* D-70794 Filderstadt */ /* EMail: uz@cc65.org */ @@ -543,15 +543,20 @@ static void AddSymEntry (SymTable* T, SymEntry* S) -SymEntry* AddStructSym (const char* Name, unsigned Size, SymTable* Tab) +SymEntry* AddStructSym (const char* Name, unsigned Type, unsigned Size, SymTable* Tab) /* Add a struct/union entry and return it */ { + SymEntry* Entry; + + /* Type must be struct or union */ + PRECONDITION (Type == SC_STRUCT || Type == SC_UNION); + /* Do we have an entry with this name already? */ - SymEntry* Entry = FindSymInTable (TagTab, Name, HashStr (Name)); + Entry = FindSymInTable (TagTab, Name, HashStr (Name)); if (Entry) { /* We do have an entry. This may be a forward, so check it. */ - if ((Entry->Flags & SC_STRUCT) == 0) { + if ((Entry->Flags & SC_TYPEMASK) != Type) { /* Existing symbol is not a struct */ Error ("Symbol `%s' is already different kind", Name); } else if (Size > 0 && Entry->V.S.Size > 0) { @@ -568,7 +573,7 @@ SymEntry* AddStructSym (const char* Name, unsigned Size, SymTable* Tab) } else { /* Create a new entry */ - Entry = NewSymEntry (Name, SC_STRUCT); + Entry = NewSymEntry (Name, Type); /* Set the struct data */ Entry->V.S.SymTab = Tab; diff --git a/src/cc65/symtab.h b/src/cc65/symtab.h index 3311e8b73..66216313c 100644 --- a/src/cc65/symtab.h +++ b/src/cc65/symtab.h @@ -6,7 +6,7 @@ /* */ /* */ /* */ -/* (C) 2000-2009, Ullrich von Bassewitz */ +/* (C) 2000-2013, Ullrich von Bassewitz */ /* Roemerstrasse 52 */ /* D-70794 Filderstadt */ /* EMail: uz@cc65.org */ @@ -144,7 +144,7 @@ SymEntry* FindStructField (const Type* TypeArray, const char* Name); -SymEntry* AddStructSym (const char* Name, unsigned Size, SymTable* Tab); +SymEntry* AddStructSym (const char* Name, unsigned Type, unsigned Size, SymTable* Tab); /* Add a struct/union entry and return it */ SymEntry* AddBitField (const char* Name, unsigned Offs, unsigned BitOffs, unsigned Width); -- 2.39.5