From 9004c4fccbe629fe32d7cf864fe57203ba7c0ed9 Mon Sep 17 00:00:00 2001 From: acqn Date: Sat, 15 Aug 2020 06:27:11 +0800 Subject: [PATCH] Made it easier to support 0-size structs in the future. --- src/cc65/compile.c | 10 ++++++---- src/cc65/datatype.c | 12 ++++++++++-- src/cc65/locals.c | 12 ++++++------ 3 files changed, 22 insertions(+), 12 deletions(-) diff --git a/src/cc65/compile.c b/src/cc65/compile.c index 85c9bd5a4..d1f78098d 100644 --- a/src/cc65/compile.c +++ b/src/cc65/compile.c @@ -218,14 +218,16 @@ static void Parse (void) ** void types in ISO modes. */ if (Size == 0) { - if (!IsTypeVoid (Decl.Type)) { + if (!IsEmptiableObjectType (Decl.Type)) { if (!IsTypeArray (Decl.Type)) { /* Size is unknown and not an array */ - Error ("Variable '%s' has unknown size", Decl.Ident); + Error ("Cannot initialize variable '%s' of unknown size", Decl.Ident); } } else if (IS_Get (&Standard) != STD_CC65) { /* We cannot declare variables of type void */ - Error ("Illegal type for variable '%s'", Decl.Ident); + Error ("Illegal type '%s' for variable '%s'", + GetFullTypeName (Decl.Type), + Decl.Ident); } } @@ -253,7 +255,7 @@ static void Parse (void) /* We cannot declare variables of type void */ Error ("Illegal type for variable '%s'", Decl.Ident); Entry->Flags &= ~(SC_STORAGE | SC_DEF); - } else if (Size == 0 && SymIsDef (Entry)) { + } else if (Size == 0 && SymIsDef (Entry) && !IsEmptiableObjectType (Decl.Type)) { /* Size is unknown. Is it an array? */ if (!IsTypeArray (Decl.Type)) { Error ("Variable '%s' has unknown size", Decl.Ident); diff --git a/src/cc65/datatype.c b/src/cc65/datatype.c index e43af238e..b69a44dd0 100644 --- a/src/cc65/datatype.c +++ b/src/cc65/datatype.c @@ -805,7 +805,11 @@ unsigned CheckedSizeOf (const Type* T) { unsigned Size = SizeOf (T); if (Size == 0) { - Error ("Size of type '%s' is unknown", GetFullTypeName (T)); + if (HasUnknownSize (T + 1)) { + Error ("Size of type '%s' is unknown", GetFullTypeName (T)); + } else { + Error ("Size of type '%s' is 0", GetFullTypeName (T)); + } Size = SIZEOF_CHAR; /* Don't return zero */ } return Size; @@ -821,7 +825,11 @@ unsigned CheckedPSizeOf (const Type* T) { unsigned Size = PSizeOf (T); if (Size == 0) { - Error ("Size of type '%s' is unknown", GetFullTypeName (T + 1)); + if (HasUnknownSize (T + 1)) { + Error ("Pointer to type '%s' of unknown size", GetFullTypeName (T + 1)); + } else { + Error ("Pointer to type '%s' of 0 size", GetFullTypeName (T + 1)); + } Size = SIZEOF_CHAR; /* Don't return zero */ } return Size; diff --git a/src/cc65/locals.c b/src/cc65/locals.c index 297994455..ad36bded0 100644 --- a/src/cc65/locals.c +++ b/src/cc65/locals.c @@ -174,8 +174,8 @@ static void ParseRegisterDecl (Declaration* Decl, int Reg) Sym->Flags |= SC_REF; } - /* Cannot allocate a variable of zero size */ - if (Size == 0) { + /* Cannot allocate a variable of unknown size */ + if (HasUnknownSize (Sym->Type)) { if (IsTypeArray (Decl->Type)) { Error ("Array '%s' has unknown size", Decl->Ident); } else { @@ -370,8 +370,8 @@ static void ParseAutoDecl (Declaration* Decl) } } - /* Cannot allocate a variable of zero size */ - if (Size == 0) { + /* Cannot allocate an incomplete variable */ + if (HasUnknownSize (Sym->Type)) { if (IsTypeArray (Decl->Type)) { Error ("Array '%s' has unknown size", Decl->Ident); } else { @@ -428,8 +428,8 @@ static void ParseStaticDecl (Declaration* Decl) } - /* Cannot allocate a variable of zero size */ - if (Size == 0) { + /* Cannot allocate an incomplete variable */ + if (HasUnknownSize (Sym->Type)) { if (IsTypeArray (Decl->Type)) { Error ("Array '%s' has unknown size", Decl->Ident); } else {