1 /*****************************************************************************/
5 /* Parse variable and function declarations */
9 /* (C) 1998-2015, Ullrich von Bassewitz */
10 /* Roemerstrasse 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 /*****************************************************************************/
65 /*****************************************************************************/
67 /*****************************************************************************/
71 typedef struct StructInitData StructInitData;
72 struct StructInitData {
73 unsigned Size; /* Size of struct */
74 unsigned Offs; /* Current offset in struct */
75 unsigned BitVal; /* Summed up bit-field value */
76 unsigned ValBits; /* Valid bits in Val */
81 /*****************************************************************************/
83 /*****************************************************************************/
87 static void ParseTypeSpec (DeclSpec* D, long Default, TypeCode Qualifiers);
88 /* Parse a type specifier */
90 static unsigned ParseInitInternal (Type* T, int AllowFlexibleMembers);
91 /* Parse initialization of variables. Return the number of data bytes. */
95 /*****************************************************************************/
96 /* Internal functions */
97 /*****************************************************************************/
101 static void DuplicateQualifier (const char* Name)
102 /* Print an error message */
104 Warning ("Duplicate qualifier: `%s'", Name);
109 static TypeCode OptionalQualifiers (TypeCode Allowed)
110 /* Read type qualifiers if we have any. Allowed specifies the allowed
114 /* We start without any qualifiers */
115 TypeCode Q = T_QUAL_NONE;
117 /* Check for more qualifiers */
120 switch (CurTok.Tok) {
123 if (Allowed & T_QUAL_CONST) {
124 if (Q & T_QUAL_CONST) {
125 DuplicateQualifier ("const");
134 if (Allowed & T_QUAL_VOLATILE) {
135 if (Q & T_QUAL_VOLATILE) {
136 DuplicateQualifier ("volatile");
138 Q |= T_QUAL_VOLATILE;
145 if (Allowed & T_QUAL_RESTRICT) {
146 if (Q & T_QUAL_RESTRICT) {
147 DuplicateQualifier ("restrict");
149 Q |= T_QUAL_RESTRICT;
156 if (Allowed & T_QUAL_NEAR) {
157 if (Q & T_QUAL_NEAR) {
158 DuplicateQualifier ("near");
167 if (Allowed & T_QUAL_FAR) {
168 if (Q & T_QUAL_FAR) {
169 DuplicateQualifier ("far");
178 if (Allowed & T_QUAL_FASTCALL) {
179 if (Q & T_QUAL_FASTCALL) {
180 DuplicateQualifier ("fastcall");
182 Q |= T_QUAL_FASTCALL;
189 if (Allowed & T_QUAL_CDECL) {
190 if (Q & T_QUAL_CDECL) {
191 DuplicateQualifier ("cdecl");
209 /* We cannot have more than one address size far qualifier */
210 switch (Q & T_QUAL_ADDRSIZE) {
218 Error ("Cannot specify more than one address size qualifier");
219 Q &= ~T_QUAL_ADDRSIZE;
222 /* We cannot have more than one calling convention specifier */
223 switch (Q & T_QUAL_CCONV) {
226 case T_QUAL_FASTCALL:
231 Error ("Cannot specify more than one calling convention qualifier");
235 /* Return the qualifiers read */
241 static void OptionalInt (void)
242 /* Eat an optional "int" token */
244 if (CurTok.Tok == TOK_INT) {
252 static void OptionalSigned (void)
253 /* Eat an optional "signed" token */
255 if (CurTok.Tok == TOK_SIGNED) {
263 static void InitDeclSpec (DeclSpec* D)
264 /* Initialize the DeclSpec struct for use */
267 D->Type[0].C = T_END;
273 static void InitDeclaration (Declaration* D)
274 /* Initialize the Declaration struct for use */
277 D->Type[0].C = T_END;
284 static void NeedTypeSpace (Declaration* D, unsigned Count)
285 /* Check if there is enough space for Count type specifiers within D */
287 if (D->Index + Count >= MAXTYPELEN) {
288 /* We must call Fatal() here, since calling Error() will try to
289 ** continue, and the declaration type is not correctly terminated
290 ** in case we come here.
292 Fatal ("Too many type specifiers");
298 static void AddTypeToDeclaration (Declaration* D, TypeCode T)
299 /* Add a type specifier to the type of a declaration */
301 NeedTypeSpace (D, 1);
302 D->Type[D->Index++].C = T;
307 static void FixQualifiers (Type* DataType)
308 /* Apply several fixes to qualifiers */
313 /* Using typedefs, it is possible to generate declarations that have
314 ** type qualifiers attached to an array, not the element type. Go and
319 while (T->C != T_END) {
320 if (IsTypeArray (T)) {
321 /* Extract any type qualifiers */
322 Q |= GetQualifier (T);
323 T->C = UnqualifiedType (T->C);
325 /* Add extracted type qualifiers here */
331 /* Q must be empty now */
332 CHECK (Q == T_QUAL_NONE);
334 /* Do some fixes on pointers and functions. */
336 while (T->C != T_END) {
339 /* Calling convention qualifier on the pointer? */
340 if (IsQualCConv (T)) {
341 /* Pull the convention off of the pointer */
342 Q = T[0].C & T_QUAL_CCONV;
343 T[0].C &= ~T_QUAL_CCONV;
344 /* Pointer to a function which doesn't have an explicit convention? */
345 if (IsTypeFunc (T + 1)) {
346 if (IsQualCConv (T + 1)) {
348 /* TODO: The end of Declarator() catches this error.
349 ** Try to make it let the error be caught here, instead.
351 Warning ("Pointer duplicates function's calling convention");
353 Error ("Mismatch between pointer's and function's calling conventions");
356 if (Q == T_QUAL_FASTCALL && IsVariadicFunc (T + 1)) {
357 Error ("Variadic-function pointers cannot be `__fastcall__'");
359 /* Move the qualifier from the pointer to the function. */
364 Error ("Not pointer to a function; can't use a calling convention");
368 /* Apply the default far and near qualifiers if none are given */
369 Q = (T[0].C & T_QUAL_ADDRSIZE);
370 if (Q == T_QUAL_NONE) {
371 /* No address size qualifiers specified */
372 if (IsTypeFunc (T+1)) {
373 /* Pointer to function. Use the qualifier from the function,
374 ** or the default if the function doesn't have one.
376 Q = (T[1].C & T_QUAL_ADDRSIZE);
377 if (Q == T_QUAL_NONE) {
378 Q = CodeAddrSizeQualifier ();
381 Q = DataAddrSizeQualifier ();
385 /* We have address size qualifiers. If followed by a function,
386 ** apply them to the function also.
388 if (IsTypeFunc (T+1)) {
389 TypeCode FQ = (T[1].C & T_QUAL_ADDRSIZE);
390 if (FQ == T_QUAL_NONE) {
392 } else if (FQ != Q) {
393 Error ("Address size qualifier mismatch");
394 T[1].C = (T[1].C & ~T_QUAL_ADDRSIZE) | Q;
399 } else if (IsTypeFunc (T)) {
401 /* Apply the default far and near qualifiers if none are given */
402 if ((T[0].C & T_QUAL_ADDRSIZE) == 0) {
403 T[0].C |= CodeAddrSizeQualifier ();
413 static void ParseStorageClass (DeclSpec* D, unsigned DefStorage)
414 /* Parse a storage class */
416 /* Assume we're using an explicit storage class */
417 D->Flags &= ~DS_DEF_STORAGE;
419 /* Check the storage class given */
420 switch (CurTok.Tok) {
423 D->StorageClass = SC_EXTERN | SC_STATIC;
428 D->StorageClass = SC_STATIC;
433 D->StorageClass = SC_REGISTER | SC_STATIC;
438 D->StorageClass = SC_AUTO;
443 D->StorageClass = SC_TYPEDEF;
448 /* No storage class given, use default */
449 D->Flags |= DS_DEF_STORAGE;
450 D->StorageClass = DefStorage;
457 static void ParseEnumDecl (void)
458 /* Process an enum declaration . */
463 /* Accept forward definitions */
464 if (CurTok.Tok != TOK_LCURLY) {
468 /* Skip the opening curly brace */
471 /* Read the enum tags */
473 while (CurTok.Tok != TOK_RCURLY) {
475 /* We expect an identifier */
476 if (CurTok.Tok != TOK_IDENT) {
477 Error ("Identifier expected");
481 /* Remember the identifier and skip it */
482 strcpy (Ident, CurTok.Ident);
485 /* Check for an assigned value */
486 if (CurTok.Tok == TOK_ASSIGN) {
489 ConstAbsIntExpr (hie1, &Expr);
493 /* Add an entry to the symbol table */
494 AddConstSym (Ident, type_int, SC_ENUM, EnumVal++);
496 /* Check for end of definition */
497 if (CurTok.Tok != TOK_COMMA)
506 static int ParseFieldWidth (Declaration* Decl)
507 /* Parse an optional field width. Returns -1 if no field width is specified,
508 ** otherwise the width of the field.
513 if (CurTok.Tok != TOK_COLON) {
514 /* No bit-field declaration */
520 ConstAbsIntExpr (hie1, &Expr);
522 Error ("Negative width in bit-field");
525 if (Expr.IVal > (int) INT_BITS) {
526 Error ("Width of bit-field exceeds its type");
529 if (Expr.IVal == 0 && Decl->Ident[0] != '\0') {
530 Error ("Zero width for named bit-field");
533 if (!IsTypeInt (Decl->Type)) {
534 /* Only integer types may be used for bit-fields */
535 Error ("Bit-field has invalid type");
539 /* Return the field width */
540 return (int) Expr.IVal;
545 static SymEntry* StructOrUnionForwardDecl (const char* Name, unsigned Type)
546 /* Handle a struct or union forward decl */
548 /* Try to find a struct/union with the given name. If there is none,
549 ** insert a forward declaration into the current lexical level.
551 SymEntry* Entry = FindTagSym (Name);
553 Entry = AddStructSym (Name, Type, 0, 0);
554 } else if ((Entry->Flags & SC_TYPEMASK) != Type) {
555 /* Already defined, but no struct */
556 Error ("Symbol `%s' is already different kind", Name);
563 static unsigned CopyAnonStructFields (const Declaration* Decl, int Offs)
564 /* Copy fields from an anon union/struct into the current lexical level. The
565 ** function returns the size of the embedded struct/union.
568 /* Get the pointer to the symbol table entry of the anon struct */
569 SymEntry* Entry = GetSymEntry (Decl->Type);
571 /* Get the size of the anon struct */
572 unsigned Size = Entry->V.S.Size;
574 /* Get the symbol table containing the fields. If it is empty, there has
575 ** been an error before, so bail out.
577 SymTable* Tab = Entry->V.S.SymTab;
579 /* Incomplete definition - has been flagged before */
583 /* Get a pointer to the list of symbols. Then walk the list adding copies
584 ** of the embedded struct to the current level.
586 Entry = Tab->SymHead;
589 /* Enter a copy of this symbol adjusting the offset. We will just
590 ** reuse the type string here.
592 AddLocalSym (Entry->Name, Entry->Type, SC_STRUCTFIELD, Offs + Entry->V.Offs);
594 /* Currently, there can not be any attributes, but if there will be
595 ** some in the future, we want to know this.
597 CHECK (Entry->Attr == 0);
600 Entry = Entry->NextSym;
603 /* Return the size of the embedded struct */
609 static SymEntry* ParseUnionDecl (const char* Name)
610 /* Parse a union declaration. */
615 int FieldWidth; /* Width in bits, -1 if not a bit-field */
619 if (CurTok.Tok != TOK_LCURLY) {
620 /* Just a forward declaration. */
621 return StructOrUnionForwardDecl (Name, SC_UNION);
624 /* Add a forward declaration for the struct in the current lexical level */
625 AddStructSym (Name, SC_UNION, 0, 0);
627 /* Skip the curly brace */
630 /* Enter a new lexical level for the struct */
633 /* Parse union fields */
635 while (CurTok.Tok != TOK_RCURLY) {
637 /* Get the type of the entry */
639 InitDeclSpec (&Spec);
640 ParseTypeSpec (&Spec, -1, T_QUAL_NONE);
642 /* Read fields with this type */
647 /* Get type and name of the struct field */
648 ParseDecl (&Spec, &Decl, DM_ACCEPT_IDENT);
650 /* Check for a bit-field declaration */
651 FieldWidth = ParseFieldWidth (&Decl);
653 /* Ignore zero sized bit fields in a union */
654 if (FieldWidth == 0) {
658 /* Check for fields without a name */
659 if (Decl.Ident[0] == '\0') {
660 /* In cc65 mode, we allow anonymous structs/unions within
663 if (IS_Get (&Standard) >= STD_CC65 && IsClassStruct (Decl.Type)) {
664 /* This is an anonymous struct or union. Copy the fields
665 ** into the current level.
667 CopyAnonStructFields (&Decl, 0);
670 /* A non bit-field without a name is legal but useless */
671 Warning ("Declaration does not declare anything");
677 FieldSize = CheckedSizeOf (Decl.Type);
678 if (FieldSize > UnionSize) {
679 UnionSize = FieldSize;
682 /* Add a field entry to the table. */
683 if (FieldWidth > 0) {
684 AddBitField (Decl.Ident, 0, 0, FieldWidth);
686 AddLocalSym (Decl.Ident, Decl.Type, SC_STRUCTFIELD, 0);
689 NextMember: if (CurTok.Tok != TOK_COMMA) {
697 /* Skip the closing brace */
700 /* Remember the symbol table and leave the struct level */
701 FieldTab = GetSymTab ();
704 /* Make a real entry from the forward decl and return it */
705 return AddStructSym (Name, SC_UNION, UnionSize, FieldTab);
710 static SymEntry* ParseStructDecl (const char* Name)
711 /* Parse a struct declaration. */
716 int BitOffs; /* Bit offset for bit-fields */
717 int FieldWidth; /* Width in bits, -1 if not a bit-field */
721 if (CurTok.Tok != TOK_LCURLY) {
722 /* Just a forward declaration. */
723 return StructOrUnionForwardDecl (Name, SC_STRUCT);
726 /* Add a forward declaration for the struct in the current lexical level */
727 AddStructSym (Name, SC_STRUCT, 0, 0);
729 /* Skip the curly brace */
732 /* Enter a new lexical level for the struct */
735 /* Parse struct fields */
739 while (CurTok.Tok != TOK_RCURLY) {
741 /* Get the type of the entry */
743 InitDeclSpec (&Spec);
744 ParseTypeSpec (&Spec, -1, T_QUAL_NONE);
746 /* Read fields with this type */
752 /* If we had a flexible array member before, no other fields can
755 if (FlexibleMember) {
756 Error ("Flexible array member must be last field");
757 FlexibleMember = 0; /* Avoid further errors */
760 /* Get type and name of the struct field */
761 ParseDecl (&Spec, &Decl, DM_ACCEPT_IDENT);
763 /* Check for a bit-field declaration */
764 FieldWidth = ParseFieldWidth (&Decl);
766 /* If this is not a bit field, or the bit field is too large for
767 ** the remainder of the current member, or we have a bit field
768 ** with width zero, align the struct to the next member by adding
769 ** a member with an anonymous name.
772 if (FieldWidth <= 0 || (BitOffs + FieldWidth) > (int) INT_BITS) {
774 /* We need an anonymous name */
775 AnonName (Ident, "bit-field");
777 /* Add an anonymous bit-field that aligns to the next
780 AddBitField (Ident, StructSize, BitOffs, INT_BITS - BitOffs);
783 StructSize += SIZEOF_INT;
788 /* Apart from the above, a bit field with width 0 is not processed
791 if (FieldWidth == 0) {
795 /* Check if this field is a flexible array member, and
796 ** calculate the size of the field.
798 if (IsTypeArray (Decl.Type) && GetElementCount (Decl.Type) == UNSPECIFIED) {
799 /* Array with unspecified size */
800 if (StructSize == 0) {
801 Error ("Flexible array member cannot be first struct field");
804 /* Assume zero for size calculations */
805 SetElementCount (Decl.Type, FLEXIBLE);
808 /* Check for fields without names */
809 if (Decl.Ident[0] == '\0') {
810 if (FieldWidth < 0) {
811 /* In cc65 mode, we allow anonymous structs/unions within
814 if (IS_Get (&Standard) >= STD_CC65 && IsClassStruct (Decl.Type)) {
816 /* This is an anonymous struct or union. Copy the
817 ** fields into the current level.
819 StructSize += CopyAnonStructFields (&Decl, StructSize);
822 /* A non bit-field without a name is legal but useless */
823 Warning ("Declaration does not declare anything");
827 /* A bit-field without a name will get an anonymous one */
828 AnonName (Decl.Ident, "bit-field");
832 /* Add a field entry to the table */
833 if (FieldWidth > 0) {
834 /* Add full byte from the bit offset to the variable offset.
835 ** This simplifies handling he bit-field as a char type
838 unsigned Offs = StructSize + (BitOffs / CHAR_BITS);
839 AddBitField (Decl.Ident, Offs, BitOffs % CHAR_BITS, FieldWidth);
840 BitOffs += FieldWidth;
841 CHECK (BitOffs <= (int) INT_BITS);
842 if (BitOffs == INT_BITS) {
843 StructSize += SIZEOF_INT;
847 AddLocalSym (Decl.Ident, Decl.Type, SC_STRUCTFIELD, StructSize);
848 if (!FlexibleMember) {
849 StructSize += CheckedSizeOf (Decl.Type);
853 NextMember: if (CurTok.Tok != TOK_COMMA) {
861 /* If we have bits from bit-fields left, add them to the size. */
863 StructSize += ((BitOffs + CHAR_BITS - 1) / CHAR_BITS);
866 /* Skip the closing brace */
869 /* Remember the symbol table and leave the struct level */
870 FieldTab = GetSymTab ();
873 /* Make a real entry from the forward decl and return it */
874 return AddStructSym (Name, SC_STRUCT, StructSize, FieldTab);
879 static void ParseTypeSpec (DeclSpec* D, long Default, TypeCode Qualifiers)
880 /* Parse a type specifier */
885 /* Assume we have an explicit type */
886 D->Flags &= ~DS_DEF_TYPE;
888 /* Read type qualifiers if we have any */
889 Qualifiers |= OptionalQualifiers (T_QUAL_CONST | T_QUAL_VOLATILE);
891 /* Look at the data type */
892 switch (CurTok.Tok) {
896 D->Type[0].C = T_VOID;
897 D->Type[1].C = T_END;
902 D->Type[0].C = GetDefaultChar();
903 D->Type[1].C = T_END;
908 if (CurTok.Tok == TOK_UNSIGNED) {
911 D->Type[0].C = T_ULONG;
912 D->Type[1].C = T_END;
916 D->Type[0].C = T_LONG;
917 D->Type[1].C = T_END;
923 if (CurTok.Tok == TOK_UNSIGNED) {
926 D->Type[0].C = T_USHORT;
927 D->Type[1].C = T_END;
931 D->Type[0].C = T_SHORT;
932 D->Type[1].C = T_END;
938 D->Type[0].C = T_INT;
939 D->Type[1].C = T_END;
944 switch (CurTok.Tok) {
948 D->Type[0].C = T_SCHAR;
949 D->Type[1].C = T_END;
955 D->Type[0].C = T_SHORT;
956 D->Type[1].C = T_END;
962 D->Type[0].C = T_LONG;
963 D->Type[1].C = T_END;
971 D->Type[0].C = T_INT;
972 D->Type[1].C = T_END;
979 switch (CurTok.Tok) {
983 D->Type[0].C = T_UCHAR;
984 D->Type[1].C = T_END;
990 D->Type[0].C = T_USHORT;
991 D->Type[1].C = T_END;
997 D->Type[0].C = T_ULONG;
998 D->Type[1].C = T_END;
1006 D->Type[0].C = T_UINT;
1007 D->Type[1].C = T_END;
1014 D->Type[0].C = T_FLOAT;
1015 D->Type[1].C = T_END;
1020 D->Type[0].C = T_DOUBLE;
1021 D->Type[1].C = T_END;
1027 if (CurTok.Tok == TOK_IDENT) {
1028 strcpy (Ident, CurTok.Ident);
1031 AnonName (Ident, "union");
1033 /* Remember we have an extra type decl */
1034 D->Flags |= DS_EXTRA_TYPE;
1035 /* Declare the union in the current scope */
1036 Entry = ParseUnionDecl (Ident);
1037 /* Encode the union entry into the type */
1038 D->Type[0].C = T_UNION;
1039 SetSymEntry (D->Type, Entry);
1040 D->Type[1].C = T_END;
1046 if (CurTok.Tok == TOK_IDENT) {
1047 strcpy (Ident, CurTok.Ident);
1050 AnonName (Ident, "struct");
1052 /* Remember we have an extra type decl */
1053 D->Flags |= DS_EXTRA_TYPE;
1054 /* Declare the struct in the current scope */
1055 Entry = ParseStructDecl (Ident);
1056 /* Encode the struct entry into the type */
1057 D->Type[0].C = T_STRUCT;
1058 SetSymEntry (D->Type, Entry);
1059 D->Type[1].C = T_END;
1064 if (CurTok.Tok != TOK_LCURLY) {
1066 if (CurTok.Tok == TOK_IDENT) {
1067 /* Find an entry with this name */
1068 Entry = FindTagSym (CurTok.Ident);
1070 if (SymIsLocal (Entry) && (Entry->Flags & SC_ENUM) == 0) {
1071 Error ("Symbol `%s' is already different kind", Entry->Name);
1074 /* Insert entry into table ### */
1076 /* Skip the identifier */
1079 Error ("Identifier expected");
1082 /* Remember we have an extra type decl */
1083 D->Flags |= DS_EXTRA_TYPE;
1084 /* Parse the enum decl */
1086 D->Type[0].C = T_INT;
1087 D->Type[1].C = T_END;
1091 Entry = FindSym (CurTok.Ident);
1092 if (Entry && SymIsTypeDef (Entry)) {
1093 /* It's a typedef */
1095 TypeCopy (D->Type, Entry->Type);
1102 Error ("Type expected");
1103 D->Type[0].C = T_INT;
1104 D->Type[1].C = T_END;
1106 D->Flags |= DS_DEF_TYPE;
1107 D->Type[0].C = (TypeCode) Default;
1108 D->Type[1].C = T_END;
1113 /* There may also be qualifiers *after* the initial type */
1114 D->Type[0].C |= (Qualifiers | OptionalQualifiers (T_QUAL_CONST | T_QUAL_VOLATILE));
1119 static Type* ParamTypeCvt (Type* T)
1120 /* If T is an array, convert it to a pointer else do nothing. Return the
1124 if (IsTypeArray (T)) {
1132 static void ParseOldStyleParamList (FuncDesc* F)
1133 /* Parse an old style (K&R) parameter list */
1135 /* Some fix point tokens that are used for error recovery */
1136 static const token_t TokenList[] = { TOK_COMMA, TOK_RPAREN, TOK_SEMI };
1139 while (CurTok.Tok != TOK_RPAREN) {
1141 /* List of identifiers expected */
1142 if (CurTok.Tok == TOK_IDENT) {
1144 /* Create a symbol table entry with type int */
1145 AddLocalSym (CurTok.Ident, type_int, SC_AUTO | SC_PARAM | SC_DEF | SC_DEFTYPE, 0);
1147 /* Count arguments */
1150 /* Skip the identifier */
1154 /* Not a parameter name */
1155 Error ("Identifier expected");
1157 /* Try some smart error recovery */
1158 SkipTokens (TokenList, sizeof(TokenList) / sizeof(TokenList[0]));
1161 /* Check for more parameters */
1162 if (CurTok.Tok == TOK_COMMA) {
1169 /* Skip right paren. We must explicitly check for one here, since some of
1170 ** the breaks above bail out without checking.
1174 /* An optional list of type specifications follows */
1175 while (CurTok.Tok != TOK_LCURLY) {
1179 /* Read the declaration specifier */
1180 ParseDeclSpec (&Spec, SC_AUTO, T_INT);
1182 /* We accept only auto and register as storage class specifiers, but
1183 ** we ignore all this, since we use auto anyway.
1185 if ((Spec.StorageClass & SC_AUTO) == 0 &&
1186 (Spec.StorageClass & SC_REGISTER) == 0) {
1187 Error ("Illegal storage class");
1190 /* Parse a comma separated variable list */
1195 /* Read the parameter */
1196 ParseDecl (&Spec, &Decl, DM_NEED_IDENT);
1197 if (Decl.Ident[0] != '\0') {
1199 /* We have a name given. Search for the symbol */
1200 SymEntry* Sym = FindLocalSym (Decl.Ident);
1202 /* Check if we already changed the type for this
1205 if (Sym->Flags & SC_DEFTYPE) {
1206 /* Found it, change the default type to the one given */
1207 ChangeSymType (Sym, ParamTypeCvt (Decl.Type));
1208 /* Reset the "default type" flag */
1209 Sym->Flags &= ~SC_DEFTYPE;
1211 /* Type has already been changed */
1212 Error ("Redefinition for parameter `%s'", Sym->Name);
1215 Error ("Unknown identifier: `%s'", Decl.Ident);
1219 if (CurTok.Tok == TOK_COMMA) {
1227 /* Variable list must be semicolon terminated */
1234 static void ParseAnsiParamList (FuncDesc* F)
1235 /* Parse a new style (ANSI) parameter list */
1238 while (CurTok.Tok != TOK_RPAREN) {
1244 /* Allow an ellipsis as last parameter */
1245 if (CurTok.Tok == TOK_ELLIPSIS) {
1247 F->Flags |= FD_VARIADIC;
1251 /* Read the declaration specifier */
1252 ParseDeclSpec (&Spec, SC_AUTO, T_INT);
1254 /* We accept only auto and register as storage class specifiers */
1255 if ((Spec.StorageClass & SC_AUTO) == SC_AUTO) {
1256 Spec.StorageClass = SC_AUTO | SC_PARAM | SC_DEF;
1257 } else if ((Spec.StorageClass & SC_REGISTER) == SC_REGISTER) {
1258 Spec.StorageClass = SC_REGISTER | SC_STATIC | SC_PARAM | SC_DEF;
1260 Error ("Illegal storage class");
1261 Spec.StorageClass = SC_AUTO | SC_PARAM | SC_DEF;
1264 /* Allow parameters without a name, but remember if we had some to
1265 ** eventually print an error message later.
1267 ParseDecl (&Spec, &Decl, DM_ACCEPT_IDENT);
1268 if (Decl.Ident[0] == '\0') {
1270 /* Unnamed symbol. Generate a name that is not user accessible,
1271 ** then handle the symbol normal.
1273 AnonName (Decl.Ident, "param");
1274 F->Flags |= FD_UNNAMED_PARAMS;
1276 /* Clear defined bit on nonames */
1277 Decl.StorageClass &= ~SC_DEF;
1280 /* Parse attributes for this parameter */
1281 ParseAttribute (&Decl);
1283 /* Create a symbol table entry */
1284 Sym = AddLocalSym (Decl.Ident, ParamTypeCvt (Decl.Type), Decl.StorageClass, 0);
1286 /* Add attributes if we have any */
1287 SymUseAttr (Sym, &Decl);
1289 /* If the parameter is a struct or union, emit a warning */
1290 if (IsClassStruct (Decl.Type)) {
1291 if (IS_Get (&WarnStructParam)) {
1292 Warning ("Passing struct by value for parameter `%s'", Decl.Ident);
1296 /* Count arguments */
1299 /* Check for more parameters */
1300 if (CurTok.Tok == TOK_COMMA) {
1307 /* Skip right paren. We must explicitly check for one here, since some of
1308 ** the breaks above bail out without checking.
1315 static FuncDesc* ParseFuncDecl (void)
1316 /* Parse the argument list of a function. */
1321 /* Create a new function descriptor */
1322 FuncDesc* F = NewFuncDesc ();
1324 /* Enter a new lexical level */
1325 EnterFunctionLevel ();
1327 /* Check for several special parameter lists */
1328 if (CurTok.Tok == TOK_RPAREN) {
1329 /* Parameter list is empty */
1330 F->Flags |= (FD_EMPTY | FD_VARIADIC);
1331 } else if (CurTok.Tok == TOK_VOID && NextTok.Tok == TOK_RPAREN) {
1332 /* Parameter list declared as void */
1334 F->Flags |= FD_VOID_PARAM;
1335 } else if (CurTok.Tok == TOK_IDENT &&
1336 (NextTok.Tok == TOK_COMMA || NextTok.Tok == TOK_RPAREN)) {
1337 /* If the identifier is a typedef, we have a new style parameter list,
1338 ** if it's some other identifier, it's an old style parameter list.
1340 Sym = FindSym (CurTok.Ident);
1341 if (Sym == 0 || !SymIsTypeDef (Sym)) {
1342 /* Old style (K&R) function. */
1343 F->Flags |= FD_OLDSTYLE;
1348 if ((F->Flags & FD_OLDSTYLE) == 0) {
1350 /* New style function */
1351 ParseAnsiParamList (F);
1354 /* Old style function */
1355 ParseOldStyleParamList (F);
1358 /* Remember the last function parameter. We need it later for several
1359 ** purposes, for example when passing stuff to fastcall functions. Since
1360 ** more symbols are added to the table, it is easier if we remember it
1361 ** now, since it is currently the last entry in the symbol table.
1363 F->LastParam = GetSymTab()->SymTail;
1365 /* Assign offsets. If the function has a variable parameter list,
1366 ** there's one additional byte (the arg size).
1368 Offs = (F->Flags & FD_VARIADIC)? 1 : 0;
1371 unsigned Size = CheckedSizeOf (Sym->Type);
1372 if (SymIsRegVar (Sym)) {
1373 Sym->V.R.SaveOffs = Offs;
1378 F->ParamSize += Size;
1382 /* Leave the lexical level remembering the symbol tables */
1383 RememberFunctionLevel (F);
1385 /* Return the function descriptor */
1391 static void Declarator (const DeclSpec* Spec, Declaration* D, declmode_t Mode)
1392 /* Recursively process declarators. Build a type array in reverse order. */
1394 /* Read optional function or pointer qualifiers. They modify the
1395 ** identifier or token to the right. For convenience, we allow a calling
1396 ** convention also for pointers here. If it's a pointer-to-function, the
1397 ** qualifier later will be transfered to the function itself. If it's a
1398 ** pointer to something else, it will be flagged as an error.
1400 TypeCode Qualifiers = OptionalQualifiers (T_QUAL_ADDRSIZE | T_QUAL_CCONV);
1402 /* Pointer to something */
1403 if (CurTok.Tok == TOK_STAR) {
1408 /* Allow const, restrict, and volatile qualifiers */
1409 Qualifiers |= OptionalQualifiers (T_QUAL_CONST | T_QUAL_VOLATILE | T_QUAL_RESTRICT);
1411 /* Parse the type that the pointer points to */
1412 Declarator (Spec, D, Mode);
1415 AddTypeToDeclaration (D, T_PTR | Qualifiers);
1419 if (CurTok.Tok == TOK_LPAREN) {
1421 Declarator (Spec, D, Mode);
1424 /* Things depend on Mode now:
1425 ** - Mode == DM_NEED_IDENT means:
1426 ** we *must* have a type and a variable identifer.
1427 ** - Mode == DM_NO_IDENT means:
1428 ** we must have a type but no variable identifer
1429 ** (if there is one, it's not read).
1430 ** - Mode == DM_ACCEPT_IDENT means:
1431 ** we *may* have an identifier. If there is an identifier,
1432 ** it is read, but it is no error, if there is none.
1434 if (Mode == DM_NO_IDENT) {
1436 } else if (CurTok.Tok == TOK_IDENT) {
1437 strcpy (D->Ident, CurTok.Ident);
1440 if (Mode == DM_NEED_IDENT) {
1441 Error ("Identifier expected");
1447 while (CurTok.Tok == TOK_LBRACK || CurTok.Tok == TOK_LPAREN) {
1448 if (CurTok.Tok == TOK_LPAREN) {
1450 /* Function declaration */
1453 /* Skip the opening paren */
1456 /* Parse the function declaration */
1457 F = ParseFuncDecl ();
1459 /* We cannot specify fastcall for variadic functions */
1460 if ((F->Flags & FD_VARIADIC) && (Qualifiers & T_QUAL_FASTCALL)) {
1461 Error ("Variadic functions cannot be `__fastcall__'");
1462 Qualifiers &= ~T_QUAL_FASTCALL;
1465 /* Add the function type. Be sure to bounds check the type buffer */
1466 NeedTypeSpace (D, 1);
1467 D->Type[D->Index].C = T_FUNC | Qualifiers;
1468 D->Type[D->Index].A.P = F;
1471 /* Qualifiers now used */
1472 Qualifiers = T_QUAL_NONE;
1475 /* Array declaration. */
1476 long Size = UNSPECIFIED;
1478 /* We cannot have any qualifiers for an array */
1479 if (Qualifiers != T_QUAL_NONE) {
1480 Error ("Invalid qualifiers for array");
1481 Qualifiers = T_QUAL_NONE;
1484 /* Skip the left bracket */
1487 /* Read the size if it is given */
1488 if (CurTok.Tok != TOK_RBRACK) {
1490 ConstAbsIntExpr (hie1, &Expr);
1491 if (Expr.IVal <= 0) {
1492 if (D->Ident[0] != '\0') {
1493 Error ("Size of array `%s' is invalid", D->Ident);
1495 Error ("Size of array is invalid");
1502 /* Skip the right bracket */
1505 /* Add the array type with the size to the type */
1506 NeedTypeSpace (D, 1);
1507 D->Type[D->Index].C = T_ARRAY;
1508 D->Type[D->Index].A.L = Size;
1513 /* If we have remaining qualifiers, flag them as invalid */
1514 if (Qualifiers & T_QUAL_NEAR) {
1515 Error ("Invalid `__near__' qualifier");
1517 if (Qualifiers & T_QUAL_FAR) {
1518 Error ("Invalid `__far__' qualifier");
1520 if (Qualifiers & T_QUAL_FASTCALL) {
1521 Error ("Invalid `__fastcall__' qualifier");
1523 if (Qualifiers & T_QUAL_CDECL) {
1524 Error ("Invalid `__cdecl__' qualifier");
1530 /*****************************************************************************/
1532 /*****************************************************************************/
1536 Type* ParseType (Type* T)
1537 /* Parse a complete type specification */
1542 /* Get a type without a default */
1543 InitDeclSpec (&Spec);
1544 ParseTypeSpec (&Spec, -1, T_QUAL_NONE);
1546 /* Parse additional declarators */
1547 ParseDecl (&Spec, &Decl, DM_NO_IDENT);
1549 /* Copy the type to the target buffer */
1550 TypeCopy (T, Decl.Type);
1552 /* Return a pointer to the target buffer */
1558 void ParseDecl (const DeclSpec* Spec, Declaration* D, declmode_t Mode)
1559 /* Parse a variable, type or function declaration */
1561 /* Initialize the Declaration struct */
1562 InitDeclaration (D);
1564 /* Get additional declarators and the identifier */
1565 Declarator (Spec, D, Mode);
1567 /* Add the base type. */
1568 NeedTypeSpace (D, TypeLen (Spec->Type) + 1); /* Bounds check */
1569 TypeCopy (D->Type + D->Index, Spec->Type);
1571 /* Use the storage class from the declspec */
1572 D->StorageClass = Spec->StorageClass;
1574 /* Do several fixes on qualifiers */
1575 FixQualifiers (D->Type);
1577 /* If we have a function, add a special storage class */
1578 if (IsTypeFunc (D->Type)) {
1579 D->StorageClass |= SC_FUNC;
1582 /* Parse attributes for this declaration */
1585 /* Check several things for function or function pointer types */
1586 if (IsTypeFunc (D->Type) || IsTypeFuncPtr (D->Type)) {
1588 /* A function. Check the return type */
1589 Type* RetType = GetFuncReturn (D->Type);
1591 /* Functions may not return functions or arrays */
1592 if (IsTypeFunc (RetType)) {
1593 Error ("Functions are not allowed to return functions");
1594 } else if (IsTypeArray (RetType)) {
1595 Error ("Functions are not allowed to return arrays");
1598 /* The return type must not be qualified */
1599 if (GetQualifier (RetType) != T_QUAL_NONE && RetType[1].C == T_END) {
1601 if (GetType (RetType) == T_TYPE_VOID) {
1602 /* A qualified void type is always an error */
1603 Error ("function definition has qualified void return type");
1605 /* For others, qualifiers are ignored */
1606 Warning ("type qualifiers ignored on function return type");
1607 RetType[0].C = UnqualifiedType (RetType[0].C);
1611 /* Warn about an implicit int return in the function */
1612 if ((Spec->Flags & DS_DEF_TYPE) != 0 &&
1613 RetType[0].C == T_INT && RetType[1].C == T_END) {
1614 /* Function has an implicit int return. Output a warning if we don't
1615 ** have the C89 standard enabled explicitly.
1617 if (IS_Get (&Standard) >= STD_C99) {
1618 Warning ("Implicit `int' return type is an obsolete feature");
1620 GetFuncDesc (D->Type)->Flags |= FD_OLDSTYLE_INTRET;
1625 /* For anthing that is not a function or typedef, check for an implicit
1628 if ((D->StorageClass & SC_FUNC) != SC_FUNC &&
1629 (D->StorageClass & SC_TYPEMASK) != SC_TYPEDEF) {
1630 /* If the standard was not set explicitly to C89, print a warning
1631 ** for variables with implicit int type.
1633 if ((Spec->Flags & DS_DEF_TYPE) != 0 && IS_Get (&Standard) >= STD_C99) {
1634 Warning ("Implicit `int' is an obsolete feature");
1638 /* Check the size of the generated type */
1639 if (!IsTypeFunc (D->Type) && !IsTypeVoid (D->Type)) {
1640 unsigned Size = SizeOf (D->Type);
1641 if (Size >= 0x10000) {
1642 if (D->Ident[0] != '\0') {
1643 Error ("Size of `%s' is invalid (0x%06X)", D->Ident, Size);
1645 Error ("Invalid size in declaration (0x%06X)", Size);
1654 void ParseDeclSpec (DeclSpec* D, unsigned DefStorage, long DefType)
1655 /* Parse a declaration specification */
1657 TypeCode Qualifiers;
1659 /* Initialize the DeclSpec struct */
1662 /* There may be qualifiers *before* the storage class specifier */
1663 Qualifiers = OptionalQualifiers (T_QUAL_CONST | T_QUAL_VOLATILE);
1665 /* Now get the storage class specifier for this declaration */
1666 ParseStorageClass (D, DefStorage);
1668 /* Parse the type specifiers passing any initial type qualifiers */
1669 ParseTypeSpec (D, DefType, Qualifiers);
1674 void CheckEmptyDecl (const DeclSpec* D)
1675 /* Called after an empty type declaration (that is, a type declaration without
1676 ** a variable). Checks if the declaration does really make sense and issues a
1680 if ((D->Flags & DS_EXTRA_TYPE) == 0) {
1681 Warning ("Useless declaration");
1687 static void SkipInitializer (unsigned BracesExpected)
1688 /* Skip the remainder of an initializer in case of errors. Try to be somewhat
1689 ** smart so we don't have too many following errors.
1692 while (CurTok.Tok != TOK_CEOF && CurTok.Tok != TOK_SEMI && BracesExpected > 0) {
1693 switch (CurTok.Tok) {
1694 case TOK_RCURLY: --BracesExpected; break;
1695 case TOK_LCURLY: ++BracesExpected; break;
1704 static unsigned OpeningCurlyBraces (unsigned BracesNeeded)
1705 /* Accept any number of opening curly braces around an initialization, skip
1706 ** them and return the number. If the number of curly braces is less than
1707 ** BracesNeeded, issue a warning.
1710 unsigned BraceCount = 0;
1711 while (CurTok.Tok == TOK_LCURLY) {
1715 if (BraceCount < BracesNeeded) {
1716 Error ("`{' expected");
1723 static void ClosingCurlyBraces (unsigned BracesExpected)
1724 /* Accept and skip the given number of closing curly braces together with
1725 ** an optional comma. Output an error messages, if the input does not contain
1726 ** the expected number of braces.
1729 while (BracesExpected) {
1730 if (CurTok.Tok == TOK_RCURLY) {
1732 } else if (CurTok.Tok == TOK_COMMA && NextTok.Tok == TOK_RCURLY) {
1736 Error ("`}' expected");
1745 static void DefineData (ExprDesc* Expr)
1746 /* Output a data definition for the given expression */
1748 switch (ED_GetLoc (Expr)) {
1751 /* Absolute: numeric address or const */
1752 g_defdata (TypeOf (Expr->Type) | CF_CONST, Expr->IVal, 0);
1756 /* Global variable */
1757 g_defdata (CF_EXTERNAL, Expr->Name, Expr->IVal);
1762 /* Static variable or literal in the literal pool */
1763 g_defdata (CF_STATIC, Expr->Name, Expr->IVal);
1766 case E_LOC_REGISTER:
1767 /* Register variable. Taking the address is usually not
1770 if (IS_Get (&AllowRegVarAddr) == 0) {
1771 Error ("Cannot take the address of a register variable");
1773 g_defdata (CF_REGVAR, Expr->Name, Expr->IVal);
1779 Error ("Non constant initializer");
1783 Internal ("Unknown constant type: 0x%04X", ED_GetLoc (Expr));
1789 static void OutputBitFieldData (StructInitData* SI)
1790 /* Output bit field data */
1792 /* Ignore if we have no data */
1793 if (SI->ValBits > 0) {
1795 /* Output the data */
1796 g_defdata (CF_INT | CF_UNSIGNED | CF_CONST, SI->BitVal, 0);
1798 /* Clear the data from SI and account for the size */
1801 SI->Offs += SIZEOF_INT;
1807 static void ParseScalarInitInternal (Type* T, ExprDesc* ED)
1808 /* Parse initializaton for scalar data types. This function will not output the
1809 ** data but return it in ED.
1812 /* Optional opening brace */
1813 unsigned BraceCount = OpeningCurlyBraces (0);
1815 /* We warn if an initializer for a scalar contains braces, because this is
1816 ** quite unusual and often a sign for some problem in the input.
1818 if (BraceCount > 0) {
1819 Warning ("Braces around scalar initializer");
1822 /* Get the expression and convert it to the target type */
1823 ConstExpr (hie1, ED);
1824 TypeConversion (ED, T);
1826 /* Close eventually opening braces */
1827 ClosingCurlyBraces (BraceCount);
1832 static unsigned ParseScalarInit (Type* T)
1833 /* Parse initializaton for scalar data types. Return the number of data bytes. */
1837 /* Parse initialization */
1838 ParseScalarInitInternal (T, &ED);
1840 /* Output the data */
1849 static unsigned ParsePointerInit (Type* T)
1850 /* Parse initializaton for pointer data types. Return the number of data bytes. */
1852 /* Optional opening brace */
1853 unsigned BraceCount = OpeningCurlyBraces (0);
1857 ConstExpr (hie1, &ED);
1858 TypeConversion (&ED, T);
1860 /* Output the data */
1863 /* Close eventually opening braces */
1864 ClosingCurlyBraces (BraceCount);
1872 static unsigned ParseArrayInit (Type* T, int AllowFlexibleMembers)
1873 /* Parse initializaton for arrays. Return the number of data bytes. */
1877 /* Get the array data */
1878 Type* ElementType = GetElementType (T);
1879 unsigned ElementSize = CheckedSizeOf (ElementType);
1880 long ElementCount = GetElementCount (T);
1882 /* Special handling for a character array initialized by a literal */
1883 if (IsTypeChar (ElementType) &&
1884 (CurTok.Tok == TOK_SCONST || CurTok.Tok == TOK_WCSCONST ||
1885 (CurTok.Tok == TOK_LCURLY &&
1886 (NextTok.Tok == TOK_SCONST || NextTok.Tok == TOK_WCSCONST)))) {
1888 /* Char array initialized by string constant */
1891 /* If we initializer is enclosed in brackets, remember this fact and
1892 ** skip the opening bracket.
1894 NeedParen = (CurTok.Tok == TOK_LCURLY);
1899 /* Translate into target charset */
1900 TranslateLiteral (CurTok.SVal);
1902 /* If the array is one too small for the string literal, omit the
1905 Count = GetLiteralSize (CurTok.SVal);
1906 if (ElementCount != UNSPECIFIED &&
1907 ElementCount != FLEXIBLE &&
1908 Count == ElementCount + 1) {
1909 /* Omit the trailing zero */
1913 /* Output the data */
1914 g_defbytes (GetLiteralStr (CurTok.SVal), Count);
1916 /* Skip the string */
1919 /* If the initializer was enclosed in curly braces, we need a closing
1931 /* Initialize the array members */
1933 while (CurTok.Tok != TOK_RCURLY) {
1934 /* Flexible array members may not be initialized within
1935 ** an array (because the size of each element may differ
1938 ParseInitInternal (ElementType, 0);
1940 if (CurTok.Tok != TOK_COMMA)
1945 /* Closing curly braces */
1949 if (ElementCount == UNSPECIFIED) {
1950 /* Number of elements determined by initializer */
1951 SetElementCount (T, Count);
1952 ElementCount = Count;
1953 } else if (ElementCount == FLEXIBLE && AllowFlexibleMembers) {
1954 /* In non ANSI mode, allow initialization of flexible array
1957 ElementCount = Count;
1958 } else if (Count < ElementCount) {
1959 g_zerobytes ((ElementCount - Count) * ElementSize);
1960 } else if (Count > ElementCount) {
1961 Error ("Too many initializers");
1963 return ElementCount * ElementSize;
1968 static unsigned ParseStructInit (Type* T, int AllowFlexibleMembers)
1969 /* Parse initialization of a struct or union. Return the number of data bytes. */
1976 /* Consume the opening curly brace */
1979 /* Get a pointer to the struct entry from the type */
1980 Entry = GetSymEntry (T);
1982 /* Get the size of the struct from the symbol table entry */
1983 SI.Size = Entry->V.S.Size;
1985 /* Check if this struct definition has a field table. If it doesn't, it
1986 ** is an incomplete definition.
1988 Tab = Entry->V.S.SymTab;
1990 Error ("Cannot initialize variables with incomplete type");
1991 /* Try error recovery */
1992 SkipInitializer (1);
1993 /* Nothing initialized */
1997 /* Get a pointer to the list of symbols */
1998 Entry = Tab->SymHead;
2000 /* Initialize fields */
2004 while (CurTok.Tok != TOK_RCURLY) {
2008 Error ("Too many initializers");
2009 SkipInitializer (1);
2013 /* Parse initialization of one field. Bit-fields need a special
2016 if (SymIsBitField (Entry)) {
2022 /* Calculate the bitmask from the bit-field data */
2023 unsigned Mask = (1U << Entry->V.B.BitWidth) - 1U;
2026 CHECK (Entry->V.B.Offs * CHAR_BITS + Entry->V.B.BitOffs ==
2027 SI.Offs * CHAR_BITS + SI.ValBits);
2029 /* This may be an anonymous bit-field, in which case it doesn't
2030 ** have an initializer.
2032 if (IsAnonName (Entry->Name)) {
2033 /* Account for the data and output it if we have a full word */
2034 SI.ValBits += Entry->V.B.BitWidth;
2035 CHECK (SI.ValBits <= INT_BITS);
2036 if (SI.ValBits == INT_BITS) {
2037 OutputBitFieldData (&SI);
2041 /* Read the data, check for a constant integer, do a range
2044 ParseScalarInitInternal (type_uint, &ED);
2045 if (!ED_IsConstAbsInt (&ED)) {
2046 Error ("Constant initializer expected");
2047 ED_MakeConstAbsInt (&ED, 1);
2049 if (ED.IVal > (long) Mask) {
2050 Warning ("Truncating value in bit-field initializer");
2051 ED.IVal &= (long) Mask;
2053 Val = (unsigned) ED.IVal;
2056 /* Add the value to the currently stored bit-field value */
2057 Shift = (Entry->V.B.Offs - SI.Offs) * CHAR_BITS + Entry->V.B.BitOffs;
2058 SI.BitVal |= (Val << Shift);
2060 /* Account for the data and output it if we have a full word */
2061 SI.ValBits += Entry->V.B.BitWidth;
2062 CHECK (SI.ValBits <= INT_BITS);
2063 if (SI.ValBits == INT_BITS) {
2064 OutputBitFieldData (&SI);
2069 /* Standard member. We should never have stuff from a
2072 CHECK (SI.ValBits == 0);
2074 /* Flexible array members may only be initialized if they are
2075 ** the last field (or part of the last struct field).
2077 SI.Offs += ParseInitInternal (Entry->Type, AllowFlexibleMembers && Entry->NextSym == 0);
2080 /* More initializers? */
2081 if (CurTok.Tok != TOK_COMMA) {
2085 /* Skip the comma */
2089 /* Next member. For unions, only the first one can be initialized */
2090 if (IsTypeUnion (T)) {
2095 Entry = Entry->NextSym;
2099 /* Consume the closing curly brace */
2102 /* If we have data from a bit-field left, output it now */
2103 OutputBitFieldData (&SI);
2105 /* If there are struct fields left, reserve additional storage */
2106 if (SI.Offs < SI.Size) {
2107 g_zerobytes (SI.Size - SI.Offs);
2111 /* Return the actual number of bytes initialized. This number may be
2112 ** larger than sizeof (Struct) if flexible array members are present and
2113 ** were initialized (possible in non ANSI mode).
2120 static unsigned ParseVoidInit (void)
2121 /* Parse an initialization of a void variable (special cc65 extension).
2122 ** Return the number of bytes initialized.
2131 /* Allow an arbitrary list of values */
2134 ConstExpr (hie1, &Expr);
2135 switch (UnqualifiedType (Expr.Type[0].C)) {
2139 if (ED_IsConstAbsInt (&Expr)) {
2140 /* Make it byte sized */
2144 Size += SIZEOF_CHAR;
2153 if (ED_IsConstAbsInt (&Expr)) {
2154 /* Make it word sized */
2155 Expr.IVal &= 0xFFFF;
2163 if (ED_IsConstAbsInt (&Expr)) {
2164 /* Make it dword sized */
2165 Expr.IVal &= 0xFFFFFFFF;
2168 Size += SIZEOF_LONG;
2172 Error ("Illegal type in initialization");
2177 if (CurTok.Tok != TOK_COMMA) {
2182 } while (CurTok.Tok != TOK_RCURLY);
2187 /* Return the number of bytes initialized */
2193 static unsigned ParseInitInternal (Type* T, int AllowFlexibleMembers)
2194 /* Parse initialization of variables. Return the number of data bytes. */
2196 switch (UnqualifiedType (T->C)) {
2208 return ParseScalarInit (T);
2211 return ParsePointerInit (T);
2214 return ParseArrayInit (T, AllowFlexibleMembers);
2218 return ParseStructInit (T, AllowFlexibleMembers);
2221 if (IS_Get (&Standard) == STD_CC65) {
2222 /* Special cc65 extension in non ANSI mode */
2223 return ParseVoidInit ();
2228 Error ("Illegal type");
2236 unsigned ParseInit (Type* T)
2237 /* Parse initialization of variables. Return the number of data bytes. */
2239 /* Parse the initialization. Flexible array members can only be initialized
2242 unsigned Size = ParseInitInternal (T, IS_Get (&Standard) == STD_CC65);
2244 /* The initialization may not generate code on global level, because code
2245 ** outside function scope will never get executed.
2247 if (HaveGlobalCode ()) {
2248 Error ("Non constant initializers");
2249 RemoveGlobalCode ();
2252 /* Return the size needed for the initialization */