1 /*****************************************************************************/
5 /* Parse variable and function declarations */
9 /* (C) 1998-2003 Ullrich von Bassewitz */
10 /* Römerstrasse 52 */
11 /* D-70794 Filderstadt */
12 /* EMail: uz@cc65.org */
15 /* This software is provided 'as-is', without any expressed or implied */
16 /* warranty. In no event will the authors be held liable for any damages */
17 /* arising from the use of this software. */
19 /* Permission is granted to anyone to use this software for any purpose, */
20 /* including commercial applications, and to alter it and redistribute it */
21 /* freely, subject to the following restrictions: */
23 /* 1. The origin of this software must not be misrepresented; you must not */
24 /* claim that you wrote the original software. If you use this software */
25 /* in a product, an acknowledgment in the product documentation would be */
26 /* appreciated but is not required. */
27 /* 2. Altered source versions must be plainly marked as such, and must not */
28 /* be misrepresented as being the original software. */
29 /* 3. This notice may not be removed or altered from any source */
32 /*****************************************************************************/
62 /*****************************************************************************/
64 /*****************************************************************************/
68 static void ParseTypeSpec (DeclSpec* D, int Default);
69 /* Parse a type specificier */
71 static unsigned ParseInitInternal (type* T, int AllowFlexibleMembers);
72 /* Parse initialization of variables. Return the number of data bytes. */
76 /*****************************************************************************/
77 /* internal functions */
78 /*****************************************************************************/
82 static type OptionalQualifiers (type Q)
83 /* Read type qualifiers if we have any */
85 while (CurTok.Tok == TOK_CONST || CurTok.Tok == TOK_VOLATILE) {
90 if (Q & T_QUAL_CONST) {
91 Error ("Duplicate qualifier: `const'");
97 if (Q & T_QUAL_VOLATILE) {
98 Error ("Duplicate qualifier: `volatile'");
100 Q |= T_QUAL_VOLATILE;
104 /* Keep gcc silent */
113 /* Return the qualifiers read */
119 static void optionalint (void)
120 /* Eat an optional "int" token */
122 if (CurTok.Tok == TOK_INT) {
130 static void optionalsigned (void)
131 /* Eat an optional "signed" token */
133 if (CurTok.Tok == TOK_SIGNED) {
141 static void InitDeclSpec (DeclSpec* D)
142 /* Initialize the DeclSpec struct for use */
151 static void InitDeclaration (Declaration* D)
152 /* Initialize the Declaration struct for use */
161 static void ParseStorageClass (DeclSpec* D, unsigned DefStorage)
162 /* Parse a storage class */
164 /* Assume we're using an explicit storage class */
165 D->Flags &= ~DS_DEF_STORAGE;
167 /* Check the storage class given */
168 switch (CurTok.Tok) {
171 D->StorageClass = SC_EXTERN | SC_STATIC;
176 D->StorageClass = SC_STATIC;
181 D->StorageClass = SC_REGISTER | SC_STATIC;
186 D->StorageClass = SC_AUTO;
191 D->StorageClass = SC_TYPEDEF;
196 /* No storage class given, use default */
197 D->Flags |= DS_DEF_STORAGE;
198 D->StorageClass = DefStorage;
205 static void ParseEnumDecl (void)
206 /* Process an enum declaration . */
211 /* Accept forward definitions */
212 if (CurTok.Tok != TOK_LCURLY) {
216 /* Skip the opening curly brace */
219 /* Read the enum tags */
221 while (CurTok.Tok != TOK_RCURLY) {
223 /* We expect an identifier */
224 if (CurTok.Tok != TOK_IDENT) {
225 Error ("Identifier expected");
229 /* Remember the identifier and skip it */
230 strcpy (Ident, CurTok.Ident);
233 /* Check for an assigned value */
234 if (CurTok.Tok == TOK_ASSIGN) {
238 EnumVal = lval.ConstVal;
241 /* Add an entry to the symbol table */
242 AddConstSym (Ident, type_int, SC_ENUM, EnumVal++);
244 /* Check for end of definition */
245 if (CurTok.Tok != TOK_COMMA)
254 static SymEntry* ParseStructDecl (const char* Name, type StructType)
255 /* Parse a struct/union declaration. */
266 if (CurTok.Tok != TOK_LCURLY) {
267 /* Just a forward declaration. Try to find a struct with the given
268 * name. If there is none, insert a forward declaration into the
269 * current lexical level.
271 Entry = FindTagSym (Name);
273 Entry = AddStructSym (Name, 0, 0);
274 } else if (SymIsLocal (Entry) && (Entry->Flags & SC_STRUCT) == 0) {
275 /* Already defined in the level but no struct */
276 Error ("Symbol `%s' is already different kind", Name);
281 /* Add a forward declaration for the struct in the current lexical level */
282 Entry = AddStructSym (Name, 0, 0);
284 /* Skip the curly brace */
287 /* Enter a new lexical level for the struct */
290 /* Parse struct fields */
293 while (CurTok.Tok != TOK_RCURLY) {
295 /* Get the type of the entry */
297 InitDeclSpec (&Spec);
298 ParseTypeSpec (&Spec, -1);
300 /* Read fields with this type */
305 /* If we had a flexible array member before, no other fields can
308 if (FlexibleMember) {
309 Error ("Flexible array member must be last field");
310 FlexibleMember = 0; /* Avoid further errors */
313 /* Get type and name of the struct field */
314 ParseDecl (&Spec, &Decl, 0);
316 /* Get the offset of this field */
317 Offs = (StructType == T_STRUCT)? StructSize : 0;
319 /* Calculate the sizes, handle flexible array members */
320 if (StructType == T_STRUCT) {
322 /* It's a struct. Check if this field is a flexible array
323 * member, and calculate the size of the field.
325 if (IsTypeArray (Decl.Type) && GetElementCount (Decl.Type) == UNSPECIFIED) {
326 /* Array with unspecified size */
327 if (StructSize == 0) {
328 Error ("Flexible array member cannot be first struct field");
331 /* Assume zero for size calculations */
332 Encode (Decl.Type + 1, FLEXIBLE);
334 StructSize += CheckedSizeOf (Decl.Type);
340 FieldSize = CheckedSizeOf (Decl.Type);
341 if (FieldSize > StructSize) {
342 StructSize = FieldSize;
346 /* Add a field entry to the table */
347 AddLocalSym (Decl.Ident, Decl.Type, SC_STRUCTFIELD, Offs);
349 if (CurTok.Tok != TOK_COMMA) {
357 /* Skip the closing brace */
360 /* Remember the symbol table and leave the struct level */
361 FieldTab = GetSymTab ();
364 /* Make a real entry from the forward decl and return it */
365 return AddStructSym (Name, StructSize, FieldTab);
370 static void ParseTypeSpec (DeclSpec* D, int Default)
371 /* Parse a type specificier */
376 type Qualifiers; /* Type qualifiers */
378 /* Assume we have an explicit type */
379 D->Flags &= ~DS_DEF_TYPE;
381 /* Read type qualifiers if we have any */
382 Qualifiers = OptionalQualifiers (T_QUAL_NONE);
384 /* Look at the data type */
385 switch (CurTok.Tok) {
395 D->Type[0] = GetDefaultChar();
401 if (CurTok.Tok == TOK_UNSIGNED) {
404 D->Type[0] = T_ULONG;
416 if (CurTok.Tok == TOK_UNSIGNED) {
419 D->Type[0] = T_USHORT;
424 D->Type[0] = T_SHORT;
437 switch (CurTok.Tok) {
441 D->Type[0] = T_SCHAR;
448 D->Type[0] = T_SHORT;
472 switch (CurTok.Tok) {
476 D->Type[0] = T_UCHAR;
483 D->Type[0] = T_USHORT;
490 D->Type[0] = T_ULONG;
507 D->Type[0] = T_FLOAT;
513 D->Type[0] = T_DOUBLE;
519 StructType = (CurTok.Tok == TOK_STRUCT)? T_STRUCT : T_UNION;
522 if (CurTok.Tok == TOK_IDENT) {
523 strcpy (Ident, CurTok.Ident);
526 AnonName (Ident, (StructType == T_STRUCT)? "struct" : "union");
528 /* Remember we have an extra type decl */
529 D->Flags |= DS_EXTRA_TYPE;
530 /* Declare the struct in the current scope */
531 Entry = ParseStructDecl (Ident, StructType);
532 /* Encode the struct entry into the type */
533 D->Type[0] = StructType;
534 EncodePtr (D->Type+1, Entry);
535 D->Type[DECODE_SIZE+1] = T_END;
540 if (CurTok.Tok != TOK_LCURLY) {
542 if (CurTok.Tok == TOK_IDENT) {
543 /* Find an entry with this name */
544 Entry = FindTagSym (CurTok.Ident);
546 if (SymIsLocal (Entry) && (Entry->Flags & SC_ENUM) == 0) {
547 Error ("Symbol `%s' is already different kind", Entry->Name);
550 /* Insert entry into table ### */
552 /* Skip the identifier */
555 Error ("Identifier expected");
558 /* Remember we have an extra type decl */
559 D->Flags |= DS_EXTRA_TYPE;
560 /* Parse the enum decl */
567 Entry = FindSym (CurTok.Ident);
568 if (Entry && SymIsTypeDef (Entry)) {
571 TypeCpy (D->Type, Entry->Type);
578 Error ("Type expected");
582 D->Flags |= DS_DEF_TYPE;
583 D->Type[0] = (type) Default;
589 /* There may also be qualifiers *after* the initial type */
590 D->Type[0] |= OptionalQualifiers (Qualifiers);
595 static type* ParamTypeCvt (type* T)
596 /* If T is an array, convert it to a pointer else do nothing. Return the
600 if (IsTypeArray (T)) {
609 static void ParseOldStyleParamList (FuncDesc* F)
610 /* Parse an old style (K&R) parameter list */
613 while (CurTok.Tok != TOK_RPAREN) {
615 /* List of identifiers expected */
616 if (CurTok.Tok != TOK_IDENT) {
617 Error ("Identifier expected");
620 /* Create a symbol table entry with type int */
621 AddLocalSym (CurTok.Ident, type_int, SC_AUTO | SC_PARAM | SC_DEF, 0);
623 /* Count arguments */
626 /* Skip the identifier */
629 /* Check for more parameters */
630 if (CurTok.Tok == TOK_COMMA) {
637 /* Skip right paren. We must explicitly check for one here, since some of
638 * the breaks above bail out without checking.
642 /* An optional list of type specifications follows */
643 while (CurTok.Tok != TOK_LCURLY) {
647 /* Read the declaration specifier */
648 ParseDeclSpec (&Spec, SC_AUTO, T_INT);
650 /* We accept only auto and register as storage class specifiers, but
651 * we ignore all this, since we use auto anyway.
653 if ((Spec.StorageClass & SC_AUTO) == 0 &&
654 (Spec.StorageClass & SC_REGISTER) == 0) {
655 Error ("Illegal storage class");
658 /* Parse a comma separated variable list */
663 /* Read the parameter */
664 ParseDecl (&Spec, &Decl, DM_NEED_IDENT);
665 if (Decl.Ident[0] != '\0') {
667 /* We have a name given. Search for the symbol */
668 SymEntry* Sym = FindLocalSym (Decl.Ident);
670 /* Found it, change the default type to the one given */
671 ChangeSymType (Sym, ParamTypeCvt (Decl.Type));
673 Error ("Unknown identifier: `%s'", Decl.Ident);
677 if (CurTok.Tok == TOK_COMMA) {
685 /* Variable list must be semicolon terminated */
692 static void ParseAnsiParamList (FuncDesc* F)
693 /* Parse a new style (ANSI) parameter list */
696 while (CurTok.Tok != TOK_RPAREN) {
702 /* Allow an ellipsis as last parameter */
703 if (CurTok.Tok == TOK_ELLIPSIS) {
705 F->Flags |= FD_VARIADIC;
709 /* Read the declaration specifier */
710 ParseDeclSpec (&Spec, SC_AUTO, T_INT);
712 /* We accept only auto and register as storage class specifiers */
713 if ((Spec.StorageClass & SC_AUTO) == SC_AUTO) {
714 Spec.StorageClass = SC_AUTO | SC_PARAM | SC_DEF;
715 } else if ((Spec.StorageClass & SC_REGISTER) == SC_REGISTER) {
716 Spec.StorageClass = SC_REGISTER | SC_STATIC | SC_PARAM | SC_DEF;
718 Error ("Illegal storage class");
719 Spec.StorageClass = SC_AUTO | SC_PARAM | SC_DEF;
722 /* Allow parameters without a name, but remember if we had some to
723 * eventually print an error message later.
725 ParseDecl (&Spec, &Decl, DM_ACCEPT_IDENT);
726 if (Decl.Ident[0] == '\0') {
728 /* Unnamed symbol. Generate a name that is not user accessible,
729 * then handle the symbol normal.
731 AnonName (Decl.Ident, "param");
732 F->Flags |= FD_UNNAMED_PARAMS;
734 /* Clear defined bit on nonames */
735 Spec.StorageClass &= ~SC_DEF;
738 /* Parse an attribute ### */
739 ParseAttribute (&Decl, &Attr);
741 /* Create a symbol table entry */
742 AddLocalSym (Decl.Ident, ParamTypeCvt (Decl.Type), Spec.StorageClass, 0);
744 /* Count arguments */
747 /* Check for more parameters */
748 if (CurTok.Tok == TOK_COMMA) {
755 /* Skip right paren. We must explicitly check for one here, since some of
756 * the breaks above bail out without checking.
760 /* Check if this is a function definition */
761 if (CurTok.Tok == TOK_LCURLY) {
762 /* Print an error if in strict ANSI mode and we have unnamed
765 if (ANSI && (F->Flags & FD_UNNAMED_PARAMS) != 0) {
766 Error ("Parameter name omitted");
773 static FuncDesc* ParseFuncDecl (const DeclSpec* Spec)
774 /* Parse the argument list of a function. */
779 /* Create a new function descriptor */
780 FuncDesc* F = NewFuncDesc ();
782 /* Enter a new lexical level */
783 EnterFunctionLevel ();
785 /* Check for several special parameter lists */
786 if (CurTok.Tok == TOK_RPAREN) {
787 /* Parameter list is empty */
788 F->Flags |= (FD_EMPTY | FD_VARIADIC);
789 } else if (CurTok.Tok == TOK_VOID && NextTok.Tok == TOK_RPAREN) {
790 /* Parameter list declared as void */
792 F->Flags |= FD_VOID_PARAM;
793 } else if (CurTok.Tok == TOK_IDENT &&
794 (NextTok.Tok == TOK_COMMA || NextTok.Tok == TOK_RPAREN)) {
795 /* If the identifier is a typedef, we have a new style parameter list,
796 * if it's some other identifier, it's an old style parameter list.
798 Sym = FindSym (CurTok.Ident);
799 if (Sym == 0 || !SymIsTypeDef (Sym)) {
800 /* Old style (K&R) function. Assume variable param list. */
801 F->Flags |= (FD_OLDSTYLE | FD_VARIADIC);
805 /* Check for an implicit int return in the function */
806 if ((Spec->Flags & DS_DEF_TYPE) != 0 &&
807 Spec->Type[0] == T_INT &&
808 Spec->Type[1] == T_END) {
809 /* Function has an implicit int return */
810 F->Flags |= FD_OLDSTYLE_INTRET;
814 if ((F->Flags & FD_OLDSTYLE) == 0) {
815 /* New style function */
816 ParseAnsiParamList (F);
818 /* Old style function */
819 ParseOldStyleParamList (F);
822 /* Remember the last function parameter. We need it later for several
823 * purposes, for example when passing stuff to fastcall functions. Since
824 * more symbols are added to the table, it is easier if we remember it
825 * now, since it is currently the last entry in the symbol table.
827 F->LastParam = GetSymTab()->SymTail;
829 /* Assign offsets. If the function has a variable parameter list,
830 * there's one additional byte (the arg size).
832 Offs = (F->Flags & FD_VARIADIC)? 1 : 0;
835 unsigned Size = CheckedSizeOf (Sym->Type);
836 if (SymIsRegVar (Sym)) {
837 Sym->V.R.SaveOffs = Offs;
842 F->ParamSize += Size;
846 /* Leave the lexical level remembering the symbol tables */
847 RememberFunctionLevel (F);
849 /* Return the function descriptor */
855 static void Decl (const DeclSpec* Spec, Declaration* D, unsigned Mode)
856 /* Recursively process declarators. Build a type array in reverse order. */
859 if (CurTok.Tok == TOK_STAR) {
862 /* Allow optional const or volatile qualifiers */
863 T |= OptionalQualifiers (T_QUAL_NONE);
864 Decl (Spec, D, Mode);
867 } else if (CurTok.Tok == TOK_LPAREN) {
869 Decl (Spec, D, Mode);
871 } else if (CurTok.Tok == TOK_FASTCALL) {
872 /* Remember the current type pointer */
874 /* Skip the fastcall token */
876 /* Parse the function */
877 Decl (Spec, D, Mode);
878 /* Set the fastcall flag */
879 if (!IsTypeFunc (T) && !IsTypeFuncPtr (T)) {
880 Error ("__fastcall__ modifier applied to non function");
881 } else if (IsVariadicFunc (T)) {
882 Error ("Cannot apply __fastcall__ to functions with variable parameter list");
884 FuncDesc* F = GetFuncDesc (T);
885 F->Flags |= FD_FASTCALL;
889 /* Things depend on Mode now:
890 * - Mode == DM_NEED_IDENT means:
891 * we *must* have a type and a variable identifer.
892 * - Mode == DM_NO_IDENT means:
893 * we must have a type but no variable identifer
894 * (if there is one, it's not read).
895 * - Mode == DM_ACCEPT_IDENT means:
896 * we *may* have an identifier. If there is an identifier,
897 * it is read, but it is no error, if there is none.
899 if (Mode == DM_NO_IDENT) {
901 } else if (CurTok.Tok == TOK_IDENT) {
902 strcpy (D->Ident, CurTok.Ident);
905 if (Mode == DM_NEED_IDENT) {
906 Error ("Identifier expected");
912 while (CurTok.Tok == TOK_LBRACK || CurTok.Tok == TOK_LPAREN) {
913 if (CurTok.Tok == TOK_LPAREN) {
914 /* Function declaration */
917 /* Parse the function declaration */
918 F = ParseFuncDecl (Spec);
923 /* Array declaration */
924 long Size = UNSPECIFIED;
926 /* Read the size if it is given */
927 if (CurTok.Tok != TOK_RBRACK) {
930 if (lval.ConstVal <= 0) {
931 if (D->Ident[0] != '\0') {
932 Error ("Size of array `%s' is invalid", D->Ident);
934 Error ("Size of array is invalid");
938 Size = lval.ConstVal;
950 /*****************************************************************************/
952 /*****************************************************************************/
956 type* ParseType (type* Type)
957 /* Parse a complete type specification */
962 /* Get a type without a default */
963 InitDeclSpec (&Spec);
964 ParseTypeSpec (&Spec, -1);
966 /* Parse additional declarators */
967 InitDeclaration (&Decl);
968 ParseDecl (&Spec, &Decl, DM_NO_IDENT);
970 /* Copy the type to the target buffer */
971 TypeCpy (Type, Decl.Type);
973 /* Return a pointer to the target buffer */
979 void ParseDecl (const DeclSpec* Spec, Declaration* D, unsigned Mode)
980 /* Parse a variable, type or function declaration */
982 /* Initialize the Declaration struct */
985 /* Get additional declarators and the identifier */
986 Decl (Spec, D, Mode);
988 /* Add the base type. */
989 TypeCpy (D->T, Spec->Type);
991 /* Check the size of the generated type */
992 if (!IsTypeFunc (D->Type) && !IsTypeVoid (D->Type) && SizeOf (D->Type) >= 0x10000) {
993 if (D->Ident[0] != '\0') {
994 Error ("Size of `%s' is invalid", D->Ident);
996 Error ("Invalid size");
1003 void ParseDeclSpec (DeclSpec* D, unsigned DefStorage, int DefType)
1004 /* Parse a declaration specification */
1006 /* Initialize the DeclSpec struct */
1009 /* First, get the storage class specifier for this declaration */
1010 ParseStorageClass (D, DefStorage);
1012 /* Parse the type specifiers */
1013 ParseTypeSpec (D, DefType);
1018 void CheckEmptyDecl (const DeclSpec* D)
1019 /* Called after an empty type declaration (that is, a type declaration without
1020 * a variable). Checks if the declaration does really make sense and issues a
1024 if ((D->Flags & DS_EXTRA_TYPE) == 0) {
1025 Warning ("Useless declaration");
1031 static void SkipInitializer (unsigned BracesExpected)
1032 /* Skip the remainder of an initializer in case of errors. Try to be somewhat
1033 * smart so we don't have too many following errors.
1036 while (CurTok.Tok != TOK_CEOF && CurTok.Tok != TOK_SEMI && BracesExpected > 0) {
1037 switch (CurTok.Tok) {
1038 case TOK_RCURLY: --BracesExpected; break;
1039 case TOK_LCURLY: ++BracesExpected; break;
1048 static unsigned OpeningCurlyBraces (unsigned BracesNeeded)
1049 /* Accept any number of opening curly braces around an initialization, skip
1050 * them and return the number. If the number of curly braces is less than
1051 * BracesNeeded, issue a warning.
1054 unsigned BraceCount = 0;
1055 while (CurTok.Tok == TOK_LCURLY) {
1059 if (BraceCount < BracesNeeded) {
1060 Error ("`{' expected");
1067 static void ClosingCurlyBraces (unsigned BracesExpected)
1068 /* Accept and skip the given number of closing curly braces together with
1069 * an optional comma. Output an error messages, if the input does not contain
1070 * the expected number of braces.
1073 while (BracesExpected) {
1074 if (CurTok.Tok == TOK_RCURLY) {
1076 } else if (CurTok.Tok == TOK_COMMA && NextTok.Tok == TOK_RCURLY) {
1080 Error ("`}' expected");
1089 static unsigned ParseScalarInit (type* T)
1090 /* Parse initializaton for scalar data types. Return the number of data bytes. */
1094 /* Optional opening brace */
1095 unsigned BraceCount = OpeningCurlyBraces (0);
1097 /* We warn if an initializer for a scalar contains braces, because this is
1098 * quite unusual and often a sign for some problem in the input.
1100 if (BraceCount > 0) {
1101 Warning ("Braces around scalar initializer");
1104 /* Get the expression and convert it to the target type */
1106 TypeConversion (&ED, 0, T);
1108 /* Output the data */
1111 /* Close eventually opening braces */
1112 ClosingCurlyBraces (BraceCount);
1120 static unsigned ParsePointerInit (type* T)
1121 /* Parse initializaton for pointer data types. Return the number of data bytes. */
1123 /* Optional opening brace */
1124 unsigned BraceCount = OpeningCurlyBraces (0);
1129 if ((ED.Flags & E_MCTYPE) == E_TCONST) {
1130 /* Make the const value the correct size */
1131 ED.ConstVal &= 0xFFFF;
1133 TypeConversion (&ED, 0, T);
1135 /* Output the data */
1138 /* Close eventually opening braces */
1139 ClosingCurlyBraces (BraceCount);
1147 static unsigned ParseArrayInit (type* T, int AllowFlexibleMembers)
1148 /* Parse initializaton for arrays. Return the number of data bytes. */
1152 /* Get the array data */
1153 type* ElementType = GetElementType (T);
1154 unsigned ElementSize = CheckedSizeOf (ElementType);
1155 long ElementCount = GetElementCount (T);
1157 /* Special handling for a character array initialized by a literal */
1158 if (IsTypeChar (ElementType) && CurTok.Tok == TOK_SCONST) {
1160 /* Char array initialized by string constant */
1161 const char* Str = GetLiteral (CurTok.IVal);
1162 Count = GetLiteralPoolOffs () - CurTok.IVal;
1164 /* Translate into target charset */
1165 TranslateLiteralPool (CurTok.IVal);
1167 /* If the array is one too small for the string literal, omit the
1170 if (ElementCount != UNSPECIFIED &&
1171 ElementCount != FLEXIBLE &&
1172 Count == ElementCount + 1) {
1173 /* Omit the trailing zero */
1177 /* Output the data */
1178 g_defbytes (Str, Count);
1180 /* Remove string from pool */
1181 ResetLiteralPoolOffs (CurTok.IVal);
1189 /* Initialize the array members */
1191 while (CurTok.Tok != TOK_RCURLY) {
1192 /* Flexible array members may not be initialized within
1193 * an array (because the size of each element may differ
1196 ParseInitInternal (ElementType, 0);
1198 if (CurTok.Tok != TOK_COMMA)
1203 /* Closing curly braces */
1208 if (ElementCount == UNSPECIFIED) {
1209 /* Number of elements determined by initializer */
1210 Encode (T + 1, Count);
1211 ElementCount = Count;
1212 } else if (ElementCount == FLEXIBLE && AllowFlexibleMembers) {
1213 /* In non ANSI mode, allow initialization of flexible array
1216 ElementCount = Count;
1217 } else if (Count < ElementCount) {
1218 g_zerobytes ((ElementCount - Count) * ElementSize);
1219 } else if (Count > ElementCount) {
1220 Error ("Too many initializers");
1222 return ElementCount * ElementSize;
1227 static unsigned ParseStructInit (type* Type, int AllowFlexibleMembers)
1228 /* Parse initialization of a struct or union. Return the number of data bytes. */
1232 unsigned StructSize;
1236 /* Consume the opening curly brace */
1239 /* Get a pointer to the struct entry from the type */
1240 Entry = DecodePtr (Type + 1);
1242 /* Get the size of the struct from the symbol table entry */
1243 StructSize = Entry->V.S.Size;
1245 /* Check if this struct definition has a field table. If it doesn't, it
1246 * is an incomplete definition.
1248 Tab = Entry->V.S.SymTab;
1250 Error ("Cannot initialize variables with incomplete type");
1251 /* Try error recovery */
1252 SkipInitializer (1);
1253 /* Nothing initialized */
1257 /* Get a pointer to the list of symbols */
1258 Entry = Tab->SymHead;
1260 /* Initialize fields */
1262 while (CurTok.Tok != TOK_RCURLY) {
1264 Error ("Too many initializers");
1265 SkipInitializer (1);
1268 /* Parse initialization of one field. Flexible array members may
1269 * only be initialized if they are the last field (or part of the
1270 * last struct field).
1272 Size += ParseInitInternal (Entry->Type, AllowFlexibleMembers && Entry->NextSym == 0);
1273 Entry = Entry->NextSym;
1274 if (CurTok.Tok != TOK_COMMA)
1279 /* Consume the closing curly brace */
1282 /* If there are struct fields left, reserve additional storage */
1283 if (Size < StructSize) {
1284 g_zerobytes (StructSize - Size);
1288 /* Return the actual number of bytes initialized. This number may be
1289 * larger than StructSize if flexible array members are present and were
1290 * initialized (possible in non ANSI mode).
1297 static unsigned ParseVoidInit (void)
1298 /* Parse an initialization of a void variable (special cc65 extension).
1299 * Return the number of bytes initialized.
1308 /* Allow an arbitrary list of values */
1312 switch (UnqualifiedType (lval.Type[0])) {
1316 if ((lval.Flags & E_MCTYPE) == E_TCONST) {
1317 /* Make it byte sized */
1318 lval.ConstVal &= 0xFF;
1321 Size += SIZEOF_CHAR;
1330 if ((lval.Flags & E_MCTYPE) == E_TCONST) {
1331 /* Make it word sized */
1332 lval.ConstVal &= 0xFFFF;
1341 Size += SIZEOF_LONG;
1345 Error ("Illegal type in initialization");
1350 if (CurTok.Tok != TOK_COMMA) {
1355 } while (CurTok.Tok != TOK_RCURLY);
1360 /* Return the number of bytes initialized */
1366 static unsigned ParseInitInternal (type* T, int AllowFlexibleMembers)
1367 /* Parse initialization of variables. Return the number of data bytes. */
1369 switch (UnqualifiedType (*T)) {
1379 return ParseScalarInit (T);
1382 return ParsePointerInit (T);
1385 return ParseArrayInit (T, AllowFlexibleMembers);
1389 return ParseStructInit (T, AllowFlexibleMembers);
1393 /* Special cc65 extension in non ANSI mode */
1394 return ParseVoidInit ();
1399 Error ("Illegal type");
1407 unsigned ParseInit (type* T)
1408 /* Parse initialization of variables. Return the number of data bytes. */
1410 /* Parse the initialization */
1411 unsigned Size = ParseInitInternal (T, !ANSI);
1413 /* The initialization may not generate code on global level, because code
1414 * outside function scope will never get executed.
1416 if (HaveGlobalCode ()) {
1417 Error ("Non constant initializers");
1418 RemoveGlobalCode ();
1421 /* Return the size needed for the initialization */