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 /*****************************************************************************/
61 #include "wrappedcall.h"
66 /*****************************************************************************/
68 /*****************************************************************************/
72 typedef struct StructInitData StructInitData;
73 struct StructInitData {
74 unsigned Size; /* Size of struct */
75 unsigned Offs; /* Current offset in struct */
76 unsigned BitVal; /* Summed up bit-field value */
77 unsigned ValBits; /* Valid bits in Val */
82 /*****************************************************************************/
84 /*****************************************************************************/
88 static void ParseTypeSpec (DeclSpec* D, long Default, TypeCode Qualifiers);
89 /* Parse a type specifier */
91 static unsigned ParseInitInternal (Type* T, int AllowFlexibleMembers);
92 /* Parse initialization of variables. Return the number of data bytes. */
96 /*****************************************************************************/
97 /* Internal functions */
98 /*****************************************************************************/
102 static void DuplicateQualifier (const char* Name)
103 /* Print an error message */
105 Warning ("Duplicate qualifier: `%s'", Name);
110 static TypeCode OptionalQualifiers (TypeCode Allowed)
111 /* Read type qualifiers if we have any. Allowed specifies the allowed
115 /* We start without any qualifiers */
116 TypeCode Q = T_QUAL_NONE;
118 /* Check for more qualifiers */
121 switch (CurTok.Tok) {
124 if (Allowed & T_QUAL_CONST) {
125 if (Q & T_QUAL_CONST) {
126 DuplicateQualifier ("const");
135 if (Allowed & T_QUAL_VOLATILE) {
136 if (Q & T_QUAL_VOLATILE) {
137 DuplicateQualifier ("volatile");
139 Q |= T_QUAL_VOLATILE;
146 if (Allowed & T_QUAL_RESTRICT) {
147 if (Q & T_QUAL_RESTRICT) {
148 DuplicateQualifier ("restrict");
150 Q |= T_QUAL_RESTRICT;
157 if (Allowed & T_QUAL_NEAR) {
158 if (Q & T_QUAL_NEAR) {
159 DuplicateQualifier ("near");
168 if (Allowed & T_QUAL_FAR) {
169 if (Q & T_QUAL_FAR) {
170 DuplicateQualifier ("far");
179 if (Allowed & T_QUAL_FASTCALL) {
180 if (Q & T_QUAL_FASTCALL) {
181 DuplicateQualifier ("fastcall");
183 Q |= T_QUAL_FASTCALL;
190 if (Allowed & T_QUAL_CDECL) {
191 if (Q & T_QUAL_CDECL) {
192 DuplicateQualifier ("cdecl");
210 /* We cannot have more than one address size far qualifier */
211 switch (Q & T_QUAL_ADDRSIZE) {
219 Error ("Cannot specify more than one address size qualifier");
220 Q &= ~T_QUAL_ADDRSIZE;
223 /* We cannot have more than one calling convention specifier */
224 switch (Q & T_QUAL_CCONV) {
227 case T_QUAL_FASTCALL:
232 Error ("Cannot specify more than one calling convention qualifier");
236 /* Return the qualifiers read */
242 static void OptionalInt (void)
243 /* Eat an optional "int" token */
245 if (CurTok.Tok == TOK_INT) {
253 static void OptionalSigned (void)
254 /* Eat an optional "signed" token */
256 if (CurTok.Tok == TOK_SIGNED) {
264 static void InitDeclSpec (DeclSpec* D)
265 /* Initialize the DeclSpec struct for use */
268 D->Type[0].C = T_END;
274 static void InitDeclaration (Declaration* D)
275 /* Initialize the Declaration struct for use */
278 D->Type[0].C = T_END;
285 static void NeedTypeSpace (Declaration* D, unsigned Count)
286 /* Check if there is enough space for Count type specifiers within D */
288 if (D->Index + Count >= MAXTYPELEN) {
289 /* We must call Fatal() here, since calling Error() will try to
290 ** continue, and the declaration type is not correctly terminated
291 ** in case we come here.
293 Fatal ("Too many type specifiers");
299 static void AddTypeToDeclaration (Declaration* D, TypeCode T)
300 /* Add a type specifier to the type of a declaration */
302 NeedTypeSpace (D, 1);
303 D->Type[D->Index++].C = T;
308 static void FixQualifiers (Type* DataType)
309 /* Apply several fixes to qualifiers */
314 /* Using typedefs, it is possible to generate declarations that have
315 ** type qualifiers attached to an array, not the element type. Go and
320 while (T->C != T_END) {
321 if (IsTypeArray (T)) {
322 /* Extract any type qualifiers */
323 Q |= GetQualifier (T);
324 T->C = UnqualifiedType (T->C);
326 /* Add extracted type qualifiers here */
332 /* Q must be empty now */
333 CHECK (Q == T_QUAL_NONE);
335 /* Do some fixes on pointers and functions. */
337 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;
345 /* Pointer to a function which doesn't have an explicit convention? */
346 if (IsTypeFunc (T + 1)) {
347 if (IsQualCConv (T + 1)) {
348 if ((T[1].C & T_QUAL_CCONV) == Q) {
349 Warning ("Pointer duplicates function's calling convention");
351 Error ("Function's and pointer's calling conventions are different");
354 if (Q == T_QUAL_FASTCALL && IsVariadicFunc (T + 1)) {
355 Error ("Variadic-function pointers cannot be __fastcall__");
357 /* Move the qualifier from the pointer to the function. */
362 Error ("Not pointer to a function; can't use a calling convention");
366 /* Apply the default far and near qualifiers if none are given */
367 Q = (T[0].C & T_QUAL_ADDRSIZE);
368 if (Q == T_QUAL_NONE) {
369 /* No address size qualifiers specified */
370 if (IsTypeFunc (T+1)) {
371 /* Pointer to function. Use the qualifier from the function,
372 ** or the default if the function doesn't have one.
374 Q = (T[1].C & T_QUAL_ADDRSIZE);
375 if (Q == T_QUAL_NONE) {
376 Q = CodeAddrSizeQualifier ();
379 Q = DataAddrSizeQualifier ();
383 /* We have address size qualifiers. If followed by a function,
384 ** apply them to the function also.
386 if (IsTypeFunc (T+1)) {
387 TypeCode FQ = (T[1].C & T_QUAL_ADDRSIZE);
388 if (FQ == T_QUAL_NONE) {
390 } else if (FQ != Q) {
391 Error ("Address size qualifier mismatch");
392 T[1].C = (T[1].C & ~T_QUAL_ADDRSIZE) | Q;
397 } else if (IsTypeFunc (T)) {
399 /* Apply the default far and near qualifiers if none are given */
400 if ((T[0].C & T_QUAL_ADDRSIZE) == 0) {
401 T[0].C |= CodeAddrSizeQualifier ();
411 static void ParseStorageClass (DeclSpec* D, unsigned DefStorage)
412 /* Parse a storage class */
414 /* Assume we're using an explicit storage class */
415 D->Flags &= ~DS_DEF_STORAGE;
417 /* Check the storage class given */
418 switch (CurTok.Tok) {
421 D->StorageClass = SC_EXTERN | SC_STATIC;
426 D->StorageClass = SC_STATIC;
431 D->StorageClass = SC_REGISTER | SC_STATIC;
436 D->StorageClass = SC_AUTO;
441 D->StorageClass = SC_TYPEDEF;
446 /* No storage class given, use default */
447 D->Flags |= DS_DEF_STORAGE;
448 D->StorageClass = DefStorage;
455 static void ParseEnumDecl (void)
456 /* Process an enum declaration . */
461 /* Accept forward definitions */
462 if (CurTok.Tok != TOK_LCURLY) {
466 /* Skip the opening curly brace */
469 /* Read the enum tags */
471 while (CurTok.Tok != TOK_RCURLY) {
473 /* We expect an identifier */
474 if (CurTok.Tok != TOK_IDENT) {
475 Error ("Identifier expected");
479 /* Remember the identifier and skip it */
480 strcpy (Ident, CurTok.Ident);
483 /* Check for an assigned value */
484 if (CurTok.Tok == TOK_ASSIGN) {
487 ConstAbsIntExpr (hie1, &Expr);
491 /* Add an entry to the symbol table */
492 AddConstSym (Ident, type_int, SC_ENUM, EnumVal++);
494 /* Check for end of definition */
495 if (CurTok.Tok != TOK_COMMA)
504 static int ParseFieldWidth (Declaration* Decl)
505 /* Parse an optional field width. Returns -1 if no field width is specified,
506 ** otherwise the width of the field.
511 if (CurTok.Tok != TOK_COLON) {
512 /* No bit-field declaration */
518 ConstAbsIntExpr (hie1, &Expr);
520 Error ("Negative width in bit-field");
523 if (Expr.IVal > (int) INT_BITS) {
524 Error ("Width of bit-field exceeds its type");
527 if (Expr.IVal == 0 && Decl->Ident[0] != '\0') {
528 Error ("Zero width for named bit-field");
531 if (!IsTypeInt (Decl->Type)) {
532 /* Only integer types may be used for bit-fields */
533 Error ("Bit-field has invalid type");
537 /* Return the field width */
538 return (int) Expr.IVal;
543 static SymEntry* StructOrUnionForwardDecl (const char* Name, unsigned Type)
544 /* Handle a struct or union forward decl */
546 /* Try to find a struct/union with the given name. If there is none,
547 ** insert a forward declaration into the current lexical level.
549 SymEntry* Entry = FindTagSym (Name);
551 Entry = AddStructSym (Name, Type, 0, 0);
552 } else if ((Entry->Flags & SC_TYPEMASK) != Type) {
553 /* Already defined, but no struct */
554 Error ("Symbol `%s' is already different kind", Name);
561 static unsigned CopyAnonStructFields (const Declaration* Decl, int Offs)
562 /* Copy fields from an anon union/struct into the current lexical level. The
563 ** function returns the size of the embedded struct/union.
566 /* Get the pointer to the symbol table entry of the anon struct */
567 SymEntry* Entry = GetSymEntry (Decl->Type);
569 /* Get the size of the anon struct */
570 unsigned Size = Entry->V.S.Size;
572 /* Get the symbol table containing the fields. If it is empty, there has
573 ** been an error before, so bail out.
575 SymTable* Tab = Entry->V.S.SymTab;
577 /* Incomplete definition - has been flagged before */
581 /* Get a pointer to the list of symbols. Then walk the list adding copies
582 ** of the embedded struct to the current level.
584 Entry = Tab->SymHead;
587 /* Enter a copy of this symbol adjusting the offset. We will just
588 ** reuse the type string here.
590 AddLocalSym (Entry->Name, Entry->Type, SC_STRUCTFIELD, Offs + Entry->V.Offs);
592 /* Currently, there can not be any attributes, but if there will be
593 ** some in the future, we want to know this.
595 CHECK (Entry->Attr == 0);
598 Entry = Entry->NextSym;
601 /* Return the size of the embedded struct */
607 static SymEntry* ParseUnionDecl (const char* Name)
608 /* Parse a union declaration. */
613 int FieldWidth; /* Width in bits, -1 if not a bit-field */
617 if (CurTok.Tok != TOK_LCURLY) {
618 /* Just a forward declaration. */
619 return StructOrUnionForwardDecl (Name, SC_UNION);
622 /* Add a forward declaration for the struct in the current lexical level */
623 AddStructSym (Name, SC_UNION, 0, 0);
625 /* Skip the curly brace */
628 /* Enter a new lexical level for the struct */
631 /* Parse union fields */
633 while (CurTok.Tok != TOK_RCURLY) {
635 /* Get the type of the entry */
637 InitDeclSpec (&Spec);
638 ParseTypeSpec (&Spec, -1, T_QUAL_NONE);
640 /* Read fields with this type */
645 /* Get type and name of the struct field */
646 ParseDecl (&Spec, &Decl, DM_ACCEPT_IDENT);
648 /* Check for a bit-field declaration */
649 FieldWidth = ParseFieldWidth (&Decl);
651 /* Ignore zero sized bit fields in a union */
652 if (FieldWidth == 0) {
656 /* Check for fields without a name */
657 if (Decl.Ident[0] == '\0') {
658 /* In cc65 mode, we allow anonymous structs/unions within
661 if (IS_Get (&Standard) >= STD_CC65 && IsClassStruct (Decl.Type)) {
662 /* This is an anonymous struct or union. Copy the fields
663 ** into the current level.
665 CopyAnonStructFields (&Decl, 0);
668 /* A non bit-field without a name is legal but useless */
669 Warning ("Declaration does not declare anything");
675 FieldSize = CheckedSizeOf (Decl.Type);
676 if (FieldSize > UnionSize) {
677 UnionSize = FieldSize;
680 /* Add a field entry to the table. */
681 if (FieldWidth > 0) {
682 AddBitField (Decl.Ident, 0, 0, FieldWidth);
684 AddLocalSym (Decl.Ident, Decl.Type, SC_STRUCTFIELD, 0);
687 NextMember: if (CurTok.Tok != TOK_COMMA) {
695 /* Skip the closing brace */
698 /* Remember the symbol table and leave the struct level */
699 FieldTab = GetSymTab ();
702 /* Make a real entry from the forward decl and return it */
703 return AddStructSym (Name, SC_UNION, UnionSize, FieldTab);
708 static SymEntry* ParseStructDecl (const char* Name)
709 /* Parse a struct declaration. */
714 int BitOffs; /* Bit offset for bit-fields */
715 int FieldWidth; /* Width in bits, -1 if not a bit-field */
719 if (CurTok.Tok != TOK_LCURLY) {
720 /* Just a forward declaration. */
721 return StructOrUnionForwardDecl (Name, SC_STRUCT);
724 /* Add a forward declaration for the struct in the current lexical level */
725 AddStructSym (Name, SC_STRUCT, 0, 0);
727 /* Skip the curly brace */
730 /* Enter a new lexical level for the struct */
733 /* Parse struct fields */
737 while (CurTok.Tok != TOK_RCURLY) {
739 /* Get the type of the entry */
741 InitDeclSpec (&Spec);
742 ParseTypeSpec (&Spec, -1, T_QUAL_NONE);
744 /* Read fields with this type */
750 /* If we had a flexible array member before, no other fields can
753 if (FlexibleMember) {
754 Error ("Flexible array member must be last field");
755 FlexibleMember = 0; /* Avoid further errors */
758 /* Get type and name of the struct field */
759 ParseDecl (&Spec, &Decl, DM_ACCEPT_IDENT);
761 /* Check for a bit-field declaration */
762 FieldWidth = ParseFieldWidth (&Decl);
764 /* If this is not a bit field, or the bit field is too large for
765 ** the remainder of the current member, or we have a bit field
766 ** with width zero, align the struct to the next member by adding
767 ** a member with an anonymous name.
770 if (FieldWidth <= 0 || (BitOffs + FieldWidth) > (int) INT_BITS) {
772 /* We need an anonymous name */
773 AnonName (Ident, "bit-field");
775 /* Add an anonymous bit-field that aligns to the next
778 AddBitField (Ident, StructSize, BitOffs, INT_BITS - BitOffs);
781 StructSize += SIZEOF_INT;
786 /* Apart from the above, a bit field with width 0 is not processed
789 if (FieldWidth == 0) {
793 /* Check if this field is a flexible array member, and
794 ** calculate the size of the field.
796 if (IsTypeArray (Decl.Type) && GetElementCount (Decl.Type) == UNSPECIFIED) {
797 /* Array with unspecified size */
798 if (StructSize == 0) {
799 Error ("Flexible array member cannot be first struct field");
802 /* Assume zero for size calculations */
803 SetElementCount (Decl.Type, FLEXIBLE);
806 /* Check for fields without names */
807 if (Decl.Ident[0] == '\0') {
808 if (FieldWidth < 0) {
809 /* In cc65 mode, we allow anonymous structs/unions within
812 if (IS_Get (&Standard) >= STD_CC65 && IsClassStruct (Decl.Type)) {
814 /* This is an anonymous struct or union. Copy the
815 ** fields into the current level.
817 StructSize += CopyAnonStructFields (&Decl, StructSize);
820 /* A non bit-field without a name is legal but useless */
821 Warning ("Declaration does not declare anything");
825 /* A bit-field without a name will get an anonymous one */
826 AnonName (Decl.Ident, "bit-field");
830 /* Add a field entry to the table */
831 if (FieldWidth > 0) {
832 /* Add full byte from the bit offset to the variable offset.
833 ** This simplifies handling he bit-field as a char type
836 unsigned Offs = StructSize + (BitOffs / CHAR_BITS);
837 AddBitField (Decl.Ident, Offs, BitOffs % CHAR_BITS, FieldWidth);
838 BitOffs += FieldWidth;
839 CHECK (BitOffs <= (int) INT_BITS);
840 if (BitOffs == INT_BITS) {
841 StructSize += SIZEOF_INT;
845 AddLocalSym (Decl.Ident, Decl.Type, SC_STRUCTFIELD, StructSize);
846 if (!FlexibleMember) {
847 StructSize += CheckedSizeOf (Decl.Type);
851 NextMember: if (CurTok.Tok != TOK_COMMA) {
859 /* If we have bits from bit-fields left, add them to the size. */
861 StructSize += ((BitOffs + CHAR_BITS - 1) / CHAR_BITS);
864 /* Skip the closing brace */
867 /* Remember the symbol table and leave the struct level */
868 FieldTab = GetSymTab ();
871 /* Make a real entry from the forward decl and return it */
872 return AddStructSym (Name, SC_STRUCT, StructSize, FieldTab);
877 static void ParseTypeSpec (DeclSpec* D, long Default, TypeCode Qualifiers)
878 /* Parse a type specifier */
883 /* Assume we have an explicit type */
884 D->Flags &= ~DS_DEF_TYPE;
886 /* Read type qualifiers if we have any */
887 Qualifiers |= OptionalQualifiers (T_QUAL_CONST | T_QUAL_VOLATILE);
889 /* Look at the data type */
890 switch (CurTok.Tok) {
894 D->Type[0].C = T_VOID;
896 D->Type[1].C = T_END;
901 D->Type[0].C = GetDefaultChar();
902 D->Type[1].C = T_END;
907 if (CurTok.Tok == TOK_UNSIGNED) {
910 D->Type[0].C = T_ULONG;
911 D->Type[1].C = T_END;
915 D->Type[0].C = T_LONG;
916 D->Type[1].C = T_END;
922 if (CurTok.Tok == TOK_UNSIGNED) {
925 D->Type[0].C = T_USHORT;
926 D->Type[1].C = T_END;
930 D->Type[0].C = T_SHORT;
931 D->Type[1].C = T_END;
937 D->Type[0].C = T_INT;
938 D->Type[1].C = T_END;
943 switch (CurTok.Tok) {
947 D->Type[0].C = T_SCHAR;
948 D->Type[1].C = T_END;
954 D->Type[0].C = T_SHORT;
955 D->Type[1].C = T_END;
961 D->Type[0].C = T_LONG;
962 D->Type[1].C = T_END;
970 D->Type[0].C = T_INT;
971 D->Type[1].C = T_END;
978 switch (CurTok.Tok) {
982 D->Type[0].C = T_UCHAR;
983 D->Type[1].C = T_END;
989 D->Type[0].C = T_USHORT;
990 D->Type[1].C = T_END;
996 D->Type[0].C = T_ULONG;
997 D->Type[1].C = T_END;
1005 D->Type[0].C = T_UINT;
1006 D->Type[1].C = T_END;
1013 D->Type[0].C = T_FLOAT;
1014 D->Type[1].C = T_END;
1019 D->Type[0].C = T_DOUBLE;
1020 D->Type[1].C = T_END;
1026 if (CurTok.Tok == TOK_IDENT) {
1027 strcpy (Ident, CurTok.Ident);
1030 AnonName (Ident, "union");
1032 /* Remember we have an extra type decl */
1033 D->Flags |= DS_EXTRA_TYPE;
1034 /* Declare the union in the current scope */
1035 Entry = ParseUnionDecl (Ident);
1036 /* Encode the union entry into the type */
1037 D->Type[0].C = T_UNION;
1038 SetSymEntry (D->Type, Entry);
1039 D->Type[1].C = T_END;
1045 if (CurTok.Tok == TOK_IDENT) {
1046 strcpy (Ident, CurTok.Ident);
1049 AnonName (Ident, "struct");
1051 /* Remember we have an extra type decl */
1052 D->Flags |= DS_EXTRA_TYPE;
1053 /* Declare the struct in the current scope */
1054 Entry = ParseStructDecl (Ident);
1055 /* Encode the struct entry into the type */
1056 D->Type[0].C = T_STRUCT;
1057 SetSymEntry (D->Type, Entry);
1058 D->Type[1].C = T_END;
1063 if (CurTok.Tok != TOK_LCURLY) {
1065 if (CurTok.Tok == TOK_IDENT) {
1066 /* Find an entry with this name */
1067 Entry = FindTagSym (CurTok.Ident);
1069 if (SymIsLocal (Entry) && (Entry->Flags & SC_ENUM) == 0) {
1070 Error ("Symbol `%s' is already different kind", Entry->Name);
1073 /* Insert entry into table ### */
1075 /* Skip the identifier */
1078 Error ("Identifier expected");
1081 /* Remember we have an extra type decl */
1082 D->Flags |= DS_EXTRA_TYPE;
1083 /* Parse the enum decl */
1085 D->Type[0].C = T_INT;
1086 D->Type[1].C = T_END;
1090 Entry = FindSym (CurTok.Ident);
1091 if (Entry && SymIsTypeDef (Entry)) {
1092 /* It's a typedef */
1094 TypeCopy (D->Type, Entry->Type);
1101 Error ("Type expected");
1102 D->Type[0].C = T_INT;
1103 D->Type[1].C = T_END;
1105 D->Flags |= DS_DEF_TYPE;
1106 D->Type[0].C = (TypeCode) Default;
1107 D->Type[1].C = T_END;
1112 /* There may also be qualifiers *after* the initial type */
1113 D->Type[0].C |= (Qualifiers | OptionalQualifiers (T_QUAL_CONST | T_QUAL_VOLATILE));
1118 static Type* ParamTypeCvt (Type* T)
1119 /* If T is an array, convert it to a pointer else do nothing. Return the
1123 if (IsTypeArray (T)) {
1131 static void ParseOldStyleParamList (FuncDesc* F)
1132 /* Parse an old style (K&R) parameter list */
1134 /* Some fix point tokens that are used for error recovery */
1135 static const token_t TokenList[] = { TOK_COMMA, TOK_RPAREN, TOK_SEMI };
1138 while (CurTok.Tok != TOK_RPAREN) {
1140 /* List of identifiers expected */
1141 if (CurTok.Tok == TOK_IDENT) {
1143 /* Create a symbol table entry with type int */
1144 AddLocalSym (CurTok.Ident, type_int, SC_AUTO | SC_PARAM | SC_DEF | SC_DEFTYPE, 0);
1146 /* Count arguments */
1149 /* Skip the identifier */
1153 /* Not a parameter name */
1154 Error ("Identifier expected");
1156 /* Try some smart error recovery */
1157 SkipTokens (TokenList, sizeof(TokenList) / sizeof(TokenList[0]));
1160 /* Check for more parameters */
1161 if (CurTok.Tok == TOK_COMMA) {
1168 /* Skip right paren. We must explicitly check for one here, since some of
1169 ** the breaks above bail out without checking.
1173 /* An optional list of type specifications follows */
1174 while (CurTok.Tok != TOK_LCURLY) {
1178 /* Read the declaration specifier */
1179 ParseDeclSpec (&Spec, SC_AUTO, T_INT);
1181 /* We accept only auto and register as storage class specifiers, but
1182 ** we ignore all this, since we use auto anyway.
1184 if ((Spec.StorageClass & SC_AUTO) == 0 &&
1185 (Spec.StorageClass & SC_REGISTER) == 0) {
1186 Error ("Illegal storage class");
1189 /* Parse a comma separated variable list */
1194 /* Read the parameter */
1195 ParseDecl (&Spec, &Decl, DM_NEED_IDENT);
1196 if (Decl.Ident[0] != '\0') {
1198 /* We have a name given. Search for the symbol */
1199 SymEntry* Sym = FindLocalSym (Decl.Ident);
1201 /* Check if we already changed the type for this
1204 if (Sym->Flags & SC_DEFTYPE) {
1205 /* Found it, change the default type to the one given */
1206 ChangeSymType (Sym, ParamTypeCvt (Decl.Type));
1207 /* Reset the "default type" flag */
1208 Sym->Flags &= ~SC_DEFTYPE;
1210 /* Type has already been changed */
1211 Error ("Redefinition for parameter `%s'", Sym->Name);
1214 Error ("Unknown identifier: `%s'", Decl.Ident);
1218 if (CurTok.Tok == TOK_COMMA) {
1226 /* Variable list must be semicolon terminated */
1233 static void ParseAnsiParamList (FuncDesc* F)
1234 /* Parse a new style (ANSI) parameter list */
1237 while (CurTok.Tok != TOK_RPAREN) {
1243 /* Allow an ellipsis as last parameter */
1244 if (CurTok.Tok == TOK_ELLIPSIS) {
1246 F->Flags |= FD_VARIADIC;
1250 /* Read the declaration specifier */
1251 ParseDeclSpec (&Spec, SC_AUTO, T_INT);
1253 /* We accept only auto and register as storage class specifiers */
1254 if ((Spec.StorageClass & SC_AUTO) == SC_AUTO) {
1255 Spec.StorageClass = SC_AUTO | SC_PARAM | SC_DEF;
1256 } else if ((Spec.StorageClass & SC_REGISTER) == SC_REGISTER) {
1257 Spec.StorageClass = SC_REGISTER | SC_STATIC | SC_PARAM | SC_DEF;
1259 Error ("Illegal storage class");
1260 Spec.StorageClass = SC_AUTO | SC_PARAM | SC_DEF;
1263 /* Allow parameters without a name, but remember if we had some to
1264 ** eventually print an error message later.
1266 ParseDecl (&Spec, &Decl, DM_ACCEPT_IDENT);
1267 if (Decl.Ident[0] == '\0') {
1269 /* Unnamed symbol. Generate a name that is not user accessible,
1270 ** then handle the symbol normal.
1272 AnonName (Decl.Ident, "param");
1273 F->Flags |= FD_UNNAMED_PARAMS;
1275 /* Clear defined bit on nonames */
1276 Decl.StorageClass &= ~SC_DEF;
1279 /* Parse attributes for this parameter */
1280 ParseAttribute (&Decl);
1282 /* Create a symbol table entry */
1283 Sym = AddLocalSym (Decl.Ident, ParamTypeCvt (Decl.Type), Decl.StorageClass, 0);
1285 /* Add attributes if we have any */
1286 SymUseAttr (Sym, &Decl);
1288 /* If the parameter is a struct or union, emit a warning */
1289 if (IsClassStruct (Decl.Type)) {
1290 if (IS_Get (&WarnStructParam)) {
1291 Warning ("Passing struct by value for parameter `%s'", Decl.Ident);
1295 /* Count arguments */
1298 /* Check for more parameters */
1299 if (CurTok.Tok == TOK_COMMA) {
1306 /* Skip right paren. We must explicitly check for one here, since some of
1307 ** the breaks above bail out without checking.
1314 static FuncDesc* ParseFuncDecl (void)
1315 /* Parse the argument list of a function. */
1319 SymEntry* WrappedCall;
1320 unsigned char WrappedCallData;
1322 /* Create a new function descriptor */
1323 FuncDesc* F = NewFuncDesc ();
1325 /* Enter a new lexical level */
1326 EnterFunctionLevel ();
1328 /* Check for several special parameter lists */
1329 if (CurTok.Tok == TOK_RPAREN) {
1330 /* Parameter list is empty */
1331 F->Flags |= (FD_EMPTY | FD_VARIADIC);
1332 } else if (CurTok.Tok == TOK_VOID && NextTok.Tok == TOK_RPAREN) {
1333 /* Parameter list declared as void */
1335 F->Flags |= FD_VOID_PARAM;
1336 } else if (CurTok.Tok == TOK_IDENT &&
1337 (NextTok.Tok == TOK_COMMA || NextTok.Tok == TOK_RPAREN)) {
1338 /* If the identifier is a typedef, we have a new style parameter list,
1339 ** if it's some other identifier, it's an old style parameter list.
1341 Sym = FindSym (CurTok.Ident);
1342 if (Sym == 0 || !SymIsTypeDef (Sym)) {
1343 /* Old style (K&R) function. */
1344 F->Flags |= FD_OLDSTYLE;
1349 if ((F->Flags & FD_OLDSTYLE) == 0) {
1351 /* New style function */
1352 ParseAnsiParamList (F);
1355 /* Old style function */
1356 ParseOldStyleParamList (F);
1359 /* Remember the last function parameter. We need it later for several
1360 ** purposes, for example when passing stuff to fastcall functions. Since
1361 ** more symbols are added to the table, it is easier if we remember it
1362 ** now, since it is currently the last entry in the symbol table.
1364 F->LastParam = GetSymTab()->SymTail;
1366 /* Assign offsets. If the function has a variable parameter list,
1367 ** there's one additional byte (the arg size).
1369 Offs = (F->Flags & FD_VARIADIC)? 1 : 0;
1372 unsigned Size = CheckedSizeOf (Sym->Type);
1373 if (SymIsRegVar (Sym)) {
1374 Sym->V.R.SaveOffs = Offs;
1379 F->ParamSize += Size;
1383 /* Leave the lexical level remembering the symbol tables */
1384 RememberFunctionLevel (F);
1386 /* Did we have a WrappedCall for this function? */
1387 GetWrappedCall((void **) &WrappedCall, &WrappedCallData);
1389 F->WrappedCall = WrappedCall;
1390 F->WrappedCallData = WrappedCallData;
1393 /* Return the function descriptor */
1399 static void Declarator (const DeclSpec* Spec, Declaration* D, declmode_t Mode)
1400 /* Recursively process declarators. Build a type array in reverse order. */
1402 /* Read optional function or pointer qualifiers. They modify the
1403 ** identifier or token to the right. For convenience, we allow a calling
1404 ** convention also for pointers here. If it's a pointer-to-function, the
1405 ** qualifier later will be transfered to the function itself. If it's a
1406 ** pointer to something else, it will be flagged as an error.
1408 TypeCode Qualifiers = OptionalQualifiers (T_QUAL_ADDRSIZE | T_QUAL_CCONV);
1410 /* Pointer to something */
1411 if (CurTok.Tok == TOK_STAR) {
1416 /* Allow const, restrict, and volatile qualifiers */
1417 Qualifiers |= OptionalQualifiers (T_QUAL_CONST | T_QUAL_VOLATILE | T_QUAL_RESTRICT);
1419 /* Parse the type that the pointer points to */
1420 Declarator (Spec, D, Mode);
1423 AddTypeToDeclaration (D, T_PTR | Qualifiers);
1427 if (CurTok.Tok == TOK_LPAREN) {
1429 Declarator (Spec, D, Mode);
1432 /* Things depend on Mode now:
1433 ** - Mode == DM_NEED_IDENT means:
1434 ** we *must* have a type and a variable identifer.
1435 ** - Mode == DM_NO_IDENT means:
1436 ** we must have a type but no variable identifer
1437 ** (if there is one, it's not read).
1438 ** - Mode == DM_ACCEPT_IDENT means:
1439 ** we *may* have an identifier. If there is an identifier,
1440 ** it is read, but it is no error, if there is none.
1442 if (Mode == DM_NO_IDENT) {
1444 } else if (CurTok.Tok == TOK_IDENT) {
1445 strcpy (D->Ident, CurTok.Ident);
1448 if (Mode == DM_NEED_IDENT) {
1449 Error ("Identifier expected");
1455 while (CurTok.Tok == TOK_LBRACK || CurTok.Tok == TOK_LPAREN) {
1456 if (CurTok.Tok == TOK_LPAREN) {
1458 /* Function declaration */
1460 SymEntry* PrevEntry;
1462 /* Skip the opening paren */
1465 /* Parse the function declaration */
1466 F = ParseFuncDecl ();
1468 /* We cannot specify fastcall for variadic functions */
1469 if ((F->Flags & FD_VARIADIC) && (Qualifiers & T_QUAL_FASTCALL)) {
1470 Error ("Variadic functions cannot be __fastcall__");
1471 Qualifiers &= ~T_QUAL_FASTCALL;
1474 /* Was there a previous entry? If so, copy WrappedCall info from it */
1475 PrevEntry = FindGlobalSym (D->Ident);
1476 if (PrevEntry && PrevEntry->Flags & SC_FUNC) {
1477 FuncDesc* D = PrevEntry->V.F.Func;
1478 if (D->WrappedCall && !F->WrappedCall) {
1479 F->WrappedCall = D->WrappedCall;
1480 F->WrappedCallData = D->WrappedCallData;
1484 /* Add the function type. Be sure to bounds check the type buffer */
1485 NeedTypeSpace (D, 1);
1486 D->Type[D->Index].C = T_FUNC | Qualifiers;
1487 D->Type[D->Index].A.P = F;
1490 /* Qualifiers now used */
1491 Qualifiers = T_QUAL_NONE;
1494 /* Array declaration. */
1495 long Size = UNSPECIFIED;
1497 /* We cannot have any qualifiers for an array */
1498 if (Qualifiers != T_QUAL_NONE) {
1499 Error ("Invalid qualifiers for array");
1500 Qualifiers = T_QUAL_NONE;
1503 /* Skip the left bracket */
1506 /* Read the size if it is given */
1507 if (CurTok.Tok != TOK_RBRACK) {
1509 ConstAbsIntExpr (hie1, &Expr);
1510 if (Expr.IVal <= 0) {
1511 if (D->Ident[0] != '\0') {
1512 Error ("Size of array `%s' is invalid", D->Ident);
1514 Error ("Size of array is invalid");
1521 /* Skip the right bracket */
1524 /* Add the array type with the size to the type */
1525 NeedTypeSpace (D, 1);
1526 D->Type[D->Index].C = T_ARRAY;
1527 D->Type[D->Index].A.L = Size;
1532 /* If we have remaining qualifiers, flag them as invalid */
1533 if (Qualifiers & T_QUAL_NEAR) {
1534 Error ("Invalid `__near__' qualifier");
1536 if (Qualifiers & T_QUAL_FAR) {
1537 Error ("Invalid `__far__' qualifier");
1539 if (Qualifiers & T_QUAL_FASTCALL) {
1540 Error ("Invalid `__fastcall__' qualifier");
1542 if (Qualifiers & T_QUAL_CDECL) {
1543 Error ("Invalid `__cdecl__' qualifier");
1549 /*****************************************************************************/
1551 /*****************************************************************************/
1555 Type* ParseType (Type* T)
1556 /* Parse a complete type specification */
1561 /* Get a type without a default */
1562 InitDeclSpec (&Spec);
1563 ParseTypeSpec (&Spec, -1, T_QUAL_NONE);
1565 /* Parse additional declarators */
1566 ParseDecl (&Spec, &Decl, DM_NO_IDENT);
1568 /* Copy the type to the target buffer */
1569 TypeCopy (T, Decl.Type);
1571 /* Return a pointer to the target buffer */
1577 void ParseDecl (const DeclSpec* Spec, Declaration* D, declmode_t Mode)
1578 /* Parse a variable, type or function declaration */
1580 /* Initialize the Declaration struct */
1581 InitDeclaration (D);
1583 /* Get additional declarators and the identifier */
1584 Declarator (Spec, D, Mode);
1586 /* Add the base type. */
1587 NeedTypeSpace (D, TypeLen (Spec->Type) + 1); /* Bounds check */
1588 TypeCopy (D->Type + D->Index, Spec->Type);
1590 /* Use the storage class from the declspec */
1591 D->StorageClass = Spec->StorageClass;
1593 /* Do several fixes on qualifiers */
1594 FixQualifiers (D->Type);
1596 /* If we have a function, add a special storage class */
1597 if (IsTypeFunc (D->Type)) {
1598 D->StorageClass |= SC_FUNC;
1601 /* Parse attributes for this declaration */
1604 /* Check several things for function or function pointer types */
1605 if (IsTypeFunc (D->Type) || IsTypeFuncPtr (D->Type)) {
1607 /* A function. Check the return type */
1608 Type* RetType = GetFuncReturn (D->Type);
1610 /* Functions may not return functions or arrays */
1611 if (IsTypeFunc (RetType)) {
1612 Error ("Functions are not allowed to return functions");
1613 } else if (IsTypeArray (RetType)) {
1614 Error ("Functions are not allowed to return arrays");
1617 /* The return type must not be qualified */
1618 if (GetQualifier (RetType) != T_QUAL_NONE && RetType[1].C == T_END) {
1620 if (GetType (RetType) == T_TYPE_VOID) {
1621 /* A qualified void type is always an error */
1622 Error ("function definition has qualified void return type");
1624 /* For others, qualifiers are ignored */
1625 Warning ("type qualifiers ignored on function return type");
1626 RetType[0].C = UnqualifiedType (RetType[0].C);
1630 /* Warn about an implicit int return in the function */
1631 if ((Spec->Flags & DS_DEF_TYPE) != 0 &&
1632 RetType[0].C == T_INT && RetType[1].C == T_END) {
1633 /* Function has an implicit int return. Output a warning if we don't
1634 ** have the C89 standard enabled explicitly.
1636 if (IS_Get (&Standard) >= STD_C99) {
1637 Warning ("Implicit `int' return type is an obsolete feature");
1639 GetFuncDesc (D->Type)->Flags |= FD_OLDSTYLE_INTRET;
1644 /* For anthing that is not a function or typedef, check for an implicit
1647 if ((D->StorageClass & SC_FUNC) != SC_FUNC &&
1648 (D->StorageClass & SC_TYPEMASK) != SC_TYPEDEF) {
1649 /* If the standard was not set explicitly to C89, print a warning
1650 ** for variables with implicit int type.
1652 if ((Spec->Flags & DS_DEF_TYPE) != 0 && IS_Get (&Standard) >= STD_C99) {
1653 Warning ("Implicit `int' is an obsolete feature");
1657 /* Check the size of the generated type */
1658 if (!IsTypeFunc (D->Type) && !IsTypeVoid (D->Type)) {
1659 unsigned Size = SizeOf (D->Type);
1660 if (Size >= 0x10000) {
1661 if (D->Ident[0] != '\0') {
1662 Error ("Size of `%s' is invalid (0x%06X)", D->Ident, Size);
1664 Error ("Invalid size in declaration (0x%06X)", Size);
1673 void ParseDeclSpec (DeclSpec* D, unsigned DefStorage, long DefType)
1674 /* Parse a declaration specification */
1676 TypeCode Qualifiers;
1678 /* Initialize the DeclSpec struct */
1681 /* There may be qualifiers *before* the storage class specifier */
1682 Qualifiers = OptionalQualifiers (T_QUAL_CONST | T_QUAL_VOLATILE);
1684 /* Now get the storage class specifier for this declaration */
1685 ParseStorageClass (D, DefStorage);
1687 /* Parse the type specifiers passing any initial type qualifiers */
1688 ParseTypeSpec (D, DefType, Qualifiers);
1693 void CheckEmptyDecl (const DeclSpec* D)
1694 /* Called after an empty type declaration (that is, a type declaration without
1695 ** a variable). Checks if the declaration does really make sense and issues a
1699 if ((D->Flags & DS_EXTRA_TYPE) == 0) {
1700 Warning ("Useless declaration");
1706 static void SkipInitializer (unsigned BracesExpected)
1707 /* Skip the remainder of an initializer in case of errors. Try to be somewhat
1708 ** smart so we don't have too many following errors.
1711 while (CurTok.Tok != TOK_CEOF && CurTok.Tok != TOK_SEMI && BracesExpected > 0) {
1712 switch (CurTok.Tok) {
1713 case TOK_RCURLY: --BracesExpected; break;
1714 case TOK_LCURLY: ++BracesExpected; break;
1723 static unsigned OpeningCurlyBraces (unsigned BracesNeeded)
1724 /* Accept any number of opening curly braces around an initialization, skip
1725 ** them and return the number. If the number of curly braces is less than
1726 ** BracesNeeded, issue a warning.
1729 unsigned BraceCount = 0;
1730 while (CurTok.Tok == TOK_LCURLY) {
1734 if (BraceCount < BracesNeeded) {
1735 Error ("`{' expected");
1742 static void ClosingCurlyBraces (unsigned BracesExpected)
1743 /* Accept and skip the given number of closing curly braces together with
1744 ** an optional comma. Output an error messages, if the input does not contain
1745 ** the expected number of braces.
1748 while (BracesExpected) {
1749 if (CurTok.Tok == TOK_RCURLY) {
1751 } else if (CurTok.Tok == TOK_COMMA && NextTok.Tok == TOK_RCURLY) {
1755 Error ("`}' expected");
1764 static void DefineData (ExprDesc* Expr)
1765 /* Output a data definition for the given expression */
1767 switch (ED_GetLoc (Expr)) {
1770 /* Absolute: numeric address or const */
1771 g_defdata (TypeOf (Expr->Type) | CF_CONST, Expr->IVal, 0);
1775 /* Global variable */
1776 g_defdata (CF_EXTERNAL, Expr->Name, Expr->IVal);
1781 /* Static variable or literal in the literal pool */
1782 g_defdata (CF_STATIC, Expr->Name, Expr->IVal);
1785 case E_LOC_REGISTER:
1786 /* Register variable. Taking the address is usually not
1789 if (IS_Get (&AllowRegVarAddr) == 0) {
1790 Error ("Cannot take the address of a register variable");
1792 g_defdata (CF_REGVAR, Expr->Name, Expr->IVal);
1798 Error ("Non constant initializer");
1802 Internal ("Unknown constant type: 0x%04X", ED_GetLoc (Expr));
1808 static void OutputBitFieldData (StructInitData* SI)
1809 /* Output bit field data */
1811 /* Ignore if we have no data */
1812 if (SI->ValBits > 0) {
1814 /* Output the data */
1815 g_defdata (CF_INT | CF_UNSIGNED | CF_CONST, SI->BitVal, 0);
1817 /* Clear the data from SI and account for the size */
1820 SI->Offs += SIZEOF_INT;
1826 static void ParseScalarInitInternal (Type* T, ExprDesc* ED)
1827 /* Parse initializaton for scalar data types. This function will not output the
1828 ** data but return it in ED.
1831 /* Optional opening brace */
1832 unsigned BraceCount = OpeningCurlyBraces (0);
1834 /* We warn if an initializer for a scalar contains braces, because this is
1835 ** quite unusual and often a sign for some problem in the input.
1837 if (BraceCount > 0) {
1838 Warning ("Braces around scalar initializer");
1841 /* Get the expression and convert it to the target type */
1842 ConstExpr (hie1, ED);
1843 TypeConversion (ED, T);
1845 /* Close eventually opening braces */
1846 ClosingCurlyBraces (BraceCount);
1851 static unsigned ParseScalarInit (Type* T)
1852 /* Parse initializaton for scalar data types. Return the number of data bytes. */
1856 /* Parse initialization */
1857 ParseScalarInitInternal (T, &ED);
1859 /* Output the data */
1868 static unsigned ParsePointerInit (Type* T)
1869 /* Parse initializaton for pointer data types. Return the number of data bytes. */
1871 /* Optional opening brace */
1872 unsigned BraceCount = OpeningCurlyBraces (0);
1876 ConstExpr (hie1, &ED);
1877 TypeConversion (&ED, T);
1879 /* Output the data */
1882 /* Close eventually opening braces */
1883 ClosingCurlyBraces (BraceCount);
1891 static unsigned ParseArrayInit (Type* T, int AllowFlexibleMembers)
1892 /* Parse initializaton for arrays. Return the number of data bytes. */
1896 /* Get the array data */
1897 Type* ElementType = GetElementType (T);
1898 unsigned ElementSize = CheckedSizeOf (ElementType);
1899 long ElementCount = GetElementCount (T);
1901 /* Special handling for a character array initialized by a literal */
1902 if (IsTypeChar (ElementType) &&
1903 (CurTok.Tok == TOK_SCONST || CurTok.Tok == TOK_WCSCONST ||
1904 (CurTok.Tok == TOK_LCURLY &&
1905 (NextTok.Tok == TOK_SCONST || NextTok.Tok == TOK_WCSCONST)))) {
1907 /* Char array initialized by string constant */
1910 /* If we initializer is enclosed in brackets, remember this fact and
1911 ** skip the opening bracket.
1913 NeedParen = (CurTok.Tok == TOK_LCURLY);
1918 /* Translate into target charset */
1919 TranslateLiteral (CurTok.SVal);
1921 /* If the array is one too small for the string literal, omit the
1924 Count = GetLiteralSize (CurTok.SVal);
1925 if (ElementCount != UNSPECIFIED &&
1926 ElementCount != FLEXIBLE &&
1927 Count == ElementCount + 1) {
1928 /* Omit the trailing zero */
1932 /* Output the data */
1933 g_defbytes (GetLiteralStr (CurTok.SVal), Count);
1935 /* Skip the string */
1938 /* If the initializer was enclosed in curly braces, we need a closing
1950 /* Initialize the array members */
1952 while (CurTok.Tok != TOK_RCURLY) {
1953 /* Flexible array members may not be initialized within
1954 ** an array (because the size of each element may differ
1957 ParseInitInternal (ElementType, 0);
1959 if (CurTok.Tok != TOK_COMMA)
1964 /* Closing curly braces */
1968 if (ElementCount == UNSPECIFIED) {
1969 /* Number of elements determined by initializer */
1970 SetElementCount (T, Count);
1971 ElementCount = Count;
1972 } else if (ElementCount == FLEXIBLE && AllowFlexibleMembers) {
1973 /* In non ANSI mode, allow initialization of flexible array
1976 ElementCount = Count;
1977 } else if (Count < ElementCount) {
1978 g_zerobytes ((ElementCount - Count) * ElementSize);
1979 } else if (Count > ElementCount) {
1980 Error ("Too many initializers");
1982 return ElementCount * ElementSize;
1987 static unsigned ParseStructInit (Type* T, int AllowFlexibleMembers)
1988 /* Parse initialization of a struct or union. Return the number of data bytes. */
1995 /* Consume the opening curly brace */
1998 /* Get a pointer to the struct entry from the type */
1999 Entry = GetSymEntry (T);
2001 /* Get the size of the struct from the symbol table entry */
2002 SI.Size = Entry->V.S.Size;
2004 /* Check if this struct definition has a field table. If it doesn't, it
2005 ** is an incomplete definition.
2007 Tab = Entry->V.S.SymTab;
2009 Error ("Cannot initialize variables with incomplete type");
2010 /* Try error recovery */
2011 SkipInitializer (1);
2012 /* Nothing initialized */
2016 /* Get a pointer to the list of symbols */
2017 Entry = Tab->SymHead;
2019 /* Initialize fields */
2023 while (CurTok.Tok != TOK_RCURLY) {
2027 Error ("Too many initializers");
2028 SkipInitializer (1);
2032 /* Parse initialization of one field. Bit-fields need a special
2035 if (SymIsBitField (Entry)) {
2041 /* Calculate the bitmask from the bit-field data */
2042 unsigned Mask = (1U << Entry->V.B.BitWidth) - 1U;
2045 CHECK (Entry->V.B.Offs * CHAR_BITS + Entry->V.B.BitOffs ==
2046 SI.Offs * CHAR_BITS + SI.ValBits);
2048 /* This may be an anonymous bit-field, in which case it doesn't
2049 ** have an initializer.
2051 if (IsAnonName (Entry->Name)) {
2052 /* Account for the data and output it if we have a full word */
2053 SI.ValBits += Entry->V.B.BitWidth;
2054 CHECK (SI.ValBits <= INT_BITS);
2055 if (SI.ValBits == INT_BITS) {
2056 OutputBitFieldData (&SI);
2060 /* Read the data, check for a constant integer, do a range
2063 ParseScalarInitInternal (type_uint, &ED);
2064 if (!ED_IsConstAbsInt (&ED)) {
2065 Error ("Constant initializer expected");
2066 ED_MakeConstAbsInt (&ED, 1);
2068 if (ED.IVal > (long) Mask) {
2069 Warning ("Truncating value in bit-field initializer");
2070 ED.IVal &= (long) Mask;
2072 Val = (unsigned) ED.IVal;
2075 /* Add the value to the currently stored bit-field value */
2076 Shift = (Entry->V.B.Offs - SI.Offs) * CHAR_BITS + Entry->V.B.BitOffs;
2077 SI.BitVal |= (Val << Shift);
2079 /* Account for the data and output it if we have a full word */
2080 SI.ValBits += Entry->V.B.BitWidth;
2081 CHECK (SI.ValBits <= INT_BITS);
2082 if (SI.ValBits == INT_BITS) {
2083 OutputBitFieldData (&SI);
2088 /* Standard member. We should never have stuff from a
2091 CHECK (SI.ValBits == 0);
2093 /* Flexible array members may only be initialized if they are
2094 ** the last field (or part of the last struct field).
2096 SI.Offs += ParseInitInternal (Entry->Type, AllowFlexibleMembers && Entry->NextSym == 0);
2099 /* More initializers? */
2100 if (CurTok.Tok != TOK_COMMA) {
2104 /* Skip the comma */
2108 /* Next member. For unions, only the first one can be initialized */
2109 if (IsTypeUnion (T)) {
2114 Entry = Entry->NextSym;
2118 /* Consume the closing curly brace */
2121 /* If we have data from a bit-field left, output it now */
2122 OutputBitFieldData (&SI);
2124 /* If there are struct fields left, reserve additional storage */
2125 if (SI.Offs < SI.Size) {
2126 g_zerobytes (SI.Size - SI.Offs);
2130 /* Return the actual number of bytes initialized. This number may be
2131 ** larger than sizeof (Struct) if flexible array members are present and
2132 ** were initialized (possible in non ANSI mode).
2139 static unsigned ParseVoidInit (Type* T)
2140 /* Parse an initialization of a void variable (special cc65 extension).
2141 ** Return the number of bytes initialized.
2150 /* Allow an arbitrary list of values */
2153 ConstExpr (hie1, &Expr);
2154 switch (UnqualifiedType (Expr.Type[0].C)) {
2158 if (ED_IsConstAbsInt (&Expr)) {
2159 /* Make it byte sized */
2163 Size += SIZEOF_CHAR;
2172 if (ED_IsConstAbsInt (&Expr)) {
2173 /* Make it word sized */
2174 Expr.IVal &= 0xFFFF;
2182 if (ED_IsConstAbsInt (&Expr)) {
2183 /* Make it dword sized */
2184 Expr.IVal &= 0xFFFFFFFF;
2187 Size += SIZEOF_LONG;
2191 Error ("Illegal type in initialization");
2196 if (CurTok.Tok != TOK_COMMA) {
2201 } while (CurTok.Tok != TOK_RCURLY);
2206 /* Number of bytes determined by initializer */
2209 /* Return the number of bytes initialized */
2215 static unsigned ParseInitInternal (Type* T, int AllowFlexibleMembers)
2216 /* Parse initialization of variables. Return the number of data bytes. */
2218 switch (UnqualifiedType (T->C)) {
2230 return ParseScalarInit (T);
2233 return ParsePointerInit (T);
2236 return ParseArrayInit (T, AllowFlexibleMembers);
2240 return ParseStructInit (T, AllowFlexibleMembers);
2243 if (IS_Get (&Standard) == STD_CC65) {
2244 /* Special cc65 extension in non-ANSI mode */
2245 return ParseVoidInit (T);
2250 Error ("Illegal type");
2258 unsigned ParseInit (Type* T)
2259 /* Parse initialization of variables. Return the number of data bytes. */
2261 /* Parse the initialization. Flexible array members can only be initialized
2264 unsigned Size = ParseInitInternal (T, IS_Get (&Standard) == STD_CC65);
2266 /* The initialization may not generate code on global level, because code
2267 ** outside function scope will never get executed.
2269 if (HaveGlobalCode ()) {
2270 Error ("Non constant initializers");
2271 RemoveGlobalCode ();
2274 /* Return the size needed for the initialization */