From 0df45fe2f2db73eb0e09985381462bb4f91d7566 Mon Sep 17 00:00:00 2001 From: acqn Date: Sun, 2 Aug 2020 21:08:50 +0800 Subject: [PATCH] Utility for getting ESU tag type symbols. --- src/cc65/datatype.c | 8 ++++---- src/cc65/datatype.h | 8 ++++---- src/cc65/declare.c | 11 ++++++----- src/cc65/symentry.c | 32 ++++++++++++++++++++++++++++++++ src/cc65/symentry.h | 10 ++++++++++ src/cc65/symtab.c | 2 +- src/cc65/typecmp.c | 4 ++-- 7 files changed, 59 insertions(+), 16 deletions(-) diff --git a/src/cc65/datatype.c b/src/cc65/datatype.c index 444902e60..5ac2ec4b4 100644 --- a/src/cc65/datatype.c +++ b/src/cc65/datatype.c @@ -895,8 +895,8 @@ Type* GetBaseElementType (Type* T) -SymEntry* GetSymEntry (const Type* T) -/* Return a SymEntry pointer from a type */ +SymEntry* GetESUSymEntry (const Type* T) +/* Return a SymEntry pointer from an enum/struct/union type */ { /* Only enums, structs or unions have a SymEntry attribute */ CHECK (IsClassStruct (T) || IsTypeEnum (T)); @@ -907,8 +907,8 @@ SymEntry* GetSymEntry (const Type* T) -void SetSymEntry (Type* T, SymEntry* S) -/* Set the SymEntry pointer for a type */ +void SetESUSymEntry (Type* T, SymEntry* S) +/* Set the SymEntry pointer for an enum/struct/union type */ { /* Only enums, structs or unions have a SymEntry attribute */ CHECK (IsClassStruct (T) || IsTypeEnum (T)); diff --git a/src/cc65/datatype.h b/src/cc65/datatype.h index 33c66d65d..4839be5d2 100644 --- a/src/cc65/datatype.h +++ b/src/cc65/datatype.h @@ -747,11 +747,11 @@ Type* GetBaseElementType (Type* T); ** the element type that is not an array. */ -struct SymEntry* GetSymEntry (const Type* T) attribute ((const)); -/* Return a SymEntry pointer from a type */ +struct SymEntry* GetESUSymEntry (const Type* T) attribute ((const)); +/* Return a SymEntry pointer from an enum/struct/union type */ -void SetSymEntry (Type* T, struct SymEntry* S); -/* Set the SymEntry pointer for a type */ +void SetESUSymEntry (Type* T, struct SymEntry* S); +/* Set the SymEntry pointer for an enum/struct/union type */ Type* IntPromotion (Type* T); /* Apply the integer promotions to T and return the result. The returned type diff --git a/src/cc65/declare.c b/src/cc65/declare.c index a525e1f86..5ecfeb04f 100644 --- a/src/cc65/declare.c +++ b/src/cc65/declare.c @@ -766,6 +766,7 @@ static unsigned PadWithBitField (unsigned StructSize, unsigned BitOffs) } + static unsigned AliasAnonStructFields (const Declaration* Decl, SymEntry* Anon) /* Create alias fields from an anon union/struct in the current lexical level. ** The function returns the count of created aliases. @@ -775,7 +776,7 @@ static unsigned AliasAnonStructFields (const Declaration* Decl, SymEntry* Anon) SymEntry* Alias; /* Get the pointer to the symbol table entry of the anon struct */ - SymEntry* Entry = GetSymEntry (Decl->Type); + SymEntry* Entry = GetESUSymEntry (Decl->Type); /* Get the symbol table containing the fields. If it is empty, there has ** been an error before, so bail out. @@ -1267,7 +1268,7 @@ static void ParseTypeSpec (DeclSpec* D, long Default, TypeCode Qualifiers) Entry = ParseUnionDecl (Ident); /* Encode the union entry into the type */ D->Type[0].C = T_UNION; - SetSymEntry (D->Type, Entry); + SetESUSymEntry (D->Type, Entry); D->Type[1].C = T_END; break; @@ -1286,7 +1287,7 @@ static void ParseTypeSpec (DeclSpec* D, long Default, TypeCode Qualifiers) Entry = ParseStructDecl (Ident); /* Encode the struct entry into the type */ D->Type[0].C = T_STRUCT; - SetSymEntry (D->Type, Entry); + SetESUSymEntry (D->Type, Entry); D->Type[1].C = T_END; break; @@ -1308,7 +1309,7 @@ static void ParseTypeSpec (DeclSpec* D, long Default, TypeCode Qualifiers) /* Parse the enum decl */ Entry = ParseEnumDecl (Ident); D->Type[0].C |= T_ENUM; - SetSymEntry (D->Type, Entry); + SetESUSymEntry (D->Type, Entry); D->Type[1].C = T_END; break; @@ -2258,7 +2259,7 @@ static unsigned ParseStructInit (Type* T, int* Braces, int AllowFlexibleMembers) } /* Get a pointer to the struct entry from the type */ - Entry = GetSymEntry (T); + Entry = GetESUSymEntry (T); /* Get the size of the struct from the symbol table entry */ SI.Size = Entry->V.S.Size; diff --git a/src/cc65/symentry.c b/src/cc65/symentry.c index c8d4f9e2a..6486ab764 100644 --- a/src/cc65/symentry.c +++ b/src/cc65/symentry.c @@ -278,6 +278,38 @@ void CvtRegVarToAuto (SymEntry* Sym) +SymEntry* GetSymType (const Type* T) +/* Get the symbol entry of the enum/struct/union type +** Return 0 if it is not an enum/struct/union. +*/ +{ + if ((IsClassStruct (T) || IsTypeEnum (T))) { + return T->A.P; + } + return 0; +} + + + +const char* GetSymTypeName (const Type* T) +/* Return a name string of the type or the symbol name if it is an ESU type. +** Note: This may use a static buffer that could be overwritten by other calls. +*/ +{ + static char TypeName [IDENTSIZE + 16]; + SymEntry* Sym; + + Sym = GetSymType (T); + if (Sym == 0) { + return GetBasicTypeName (T); + } + sprintf (TypeName, "%s %s", GetBasicTypeName (T), Sym->Name ? Sym->Name : ""); + + return TypeName; +} + + + void ChangeSymType (SymEntry* Entry, Type* T) /* Change the type of the given symbol */ { diff --git a/src/cc65/symentry.h b/src/cc65/symentry.h index e59bf2a35..1d8af3f50 100644 --- a/src/cc65/symentry.h +++ b/src/cc65/symentry.h @@ -304,6 +304,16 @@ void SymSetAsmName (SymEntry* Sym); void CvtRegVarToAuto (SymEntry* Sym); /* Convert a register variable to an auto variable */ +SymEntry* GetSymType (const Type* T); +/* Get the symbol entry of the enum/struct/union type +** Return 0 if it is not an enum/struct/union. +*/ + +const char* GetSymTypeName (const Type* T); +/* Return a name string of the type or the symbol name if it is an ESU type. +** Note: This may use a static buffer that could be overwritten by other calls. +*/ + void ChangeSymType (SymEntry* Entry, Type* T); /* Change the type of the given symbol */ diff --git a/src/cc65/symtab.c b/src/cc65/symtab.c index f7a7862cf..735cb854f 100644 --- a/src/cc65/symtab.c +++ b/src/cc65/symtab.c @@ -509,7 +509,7 @@ SymEntry FindStructField (const Type* T, const char* Name) if (IsClassStruct (T)) { /* Get a pointer to the struct/union type */ - const SymEntry* Struct = GetSymEntry (T); + const SymEntry* Struct = GetESUSymEntry (T); CHECK (Struct != 0); /* Now search in the struct/union symbol table. Beware: The table may diff --git a/src/cc65/typecmp.c b/src/cc65/typecmp.c index 66dd9039b..0eddf2988 100644 --- a/src/cc65/typecmp.c +++ b/src/cc65/typecmp.c @@ -368,8 +368,8 @@ static void DoCompare (const Type* lhs, const Type* rhs, typecmp_t* Result) ** pointer to the struct definition from the type, and compare ** the fields. */ - Sym1 = GetSymEntry (lhs); - Sym2 = GetSymEntry (rhs); + Sym1 = GetESUSymEntry (lhs); + Sym2 = GetESUSymEntry (rhs); /* If one symbol has a name, the names must be identical */ if (!HasAnonName (Sym1) || !HasAnonName (Sym2)) {