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 FieldSize = CopyAnonStructFields (&Decl, 0);
666 if (FieldSize > UnionSize) {
667 UnionSize = FieldSize;
671 /* A non bit-field without a name is legal but useless */
672 Warning ("Declaration does not declare anything");
678 FieldSize = CheckedSizeOf (Decl.Type);
679 if (FieldSize > UnionSize) {
680 UnionSize = FieldSize;
683 /* Add a field entry to the table. */
684 if (FieldWidth > 0) {
685 AddBitField (Decl.Ident, 0, 0, FieldWidth);
687 AddLocalSym (Decl.Ident, Decl.Type, SC_STRUCTFIELD, 0);
690 NextMember: if (CurTok.Tok != TOK_COMMA) {
698 /* Skip the closing brace */
701 /* Remember the symbol table and leave the struct level */
702 FieldTab = GetSymTab ();
705 /* Make a real entry from the forward decl and return it */
706 return AddStructSym (Name, SC_UNION, UnionSize, FieldTab);
711 static SymEntry* ParseStructDecl (const char* Name)
712 /* Parse a struct declaration. */
717 int BitOffs; /* Bit offset for bit-fields */
718 int FieldWidth; /* Width in bits, -1 if not a bit-field */
722 if (CurTok.Tok != TOK_LCURLY) {
723 /* Just a forward declaration. */
724 return StructOrUnionForwardDecl (Name, SC_STRUCT);
727 /* Add a forward declaration for the struct in the current lexical level */
728 AddStructSym (Name, SC_STRUCT, 0, 0);
730 /* Skip the curly brace */
733 /* Enter a new lexical level for the struct */
736 /* Parse struct fields */
740 while (CurTok.Tok != TOK_RCURLY) {
742 /* Get the type of the entry */
744 InitDeclSpec (&Spec);
745 ParseTypeSpec (&Spec, -1, T_QUAL_NONE);
747 /* Read fields with this type */
753 /* If we had a flexible array member before, no other fields can
756 if (FlexibleMember) {
757 Error ("Flexible array member must be last field");
758 FlexibleMember = 0; /* Avoid further errors */
761 /* Get type and name of the struct field */
762 ParseDecl (&Spec, &Decl, DM_ACCEPT_IDENT);
764 /* Check for a bit-field declaration */
765 FieldWidth = ParseFieldWidth (&Decl);
767 /* If this is not a bit field, or the bit field is too large for
768 ** the remainder of the current member, or we have a bit field
769 ** with width zero, align the struct to the next member by adding
770 ** a member with an anonymous name.
773 if (FieldWidth <= 0 || (BitOffs + FieldWidth) > (int) INT_BITS) {
775 /* We need an anonymous name */
776 AnonName (Ident, "bit-field");
778 /* Add an anonymous bit-field that aligns to the next
781 AddBitField (Ident, StructSize, BitOffs, INT_BITS - BitOffs);
784 StructSize += SIZEOF_INT;
789 /* Apart from the above, a bit field with width 0 is not processed
792 if (FieldWidth == 0) {
796 /* Check if this field is a flexible array member, and
797 ** calculate the size of the field.
799 if (IsTypeArray (Decl.Type) && GetElementCount (Decl.Type) == UNSPECIFIED) {
800 /* Array with unspecified size */
801 if (StructSize == 0) {
802 Error ("Flexible array member cannot be first struct field");
805 /* Assume zero for size calculations */
806 SetElementCount (Decl.Type, FLEXIBLE);
809 /* Check for fields without names */
810 if (Decl.Ident[0] == '\0') {
811 if (FieldWidth < 0) {
812 /* In cc65 mode, we allow anonymous structs/unions within
815 if (IS_Get (&Standard) >= STD_CC65 && IsClassStruct (Decl.Type)) {
817 /* This is an anonymous struct or union. Copy the
818 ** fields into the current level.
820 StructSize += CopyAnonStructFields (&Decl, StructSize);
823 /* A non bit-field without a name is legal but useless */
824 Warning ("Declaration does not declare anything");
828 /* A bit-field without a name will get an anonymous one */
829 AnonName (Decl.Ident, "bit-field");
833 /* Add a field entry to the table */
834 if (FieldWidth > 0) {
835 /* Add full byte from the bit offset to the variable offset.
836 ** This simplifies handling he bit-field as a char type
839 unsigned Offs = StructSize + (BitOffs / CHAR_BITS);
840 AddBitField (Decl.Ident, Offs, BitOffs % CHAR_BITS, FieldWidth);
841 BitOffs += FieldWidth;
842 CHECK (BitOffs <= (int) INT_BITS);
843 if (BitOffs == INT_BITS) {
844 StructSize += SIZEOF_INT;
848 AddLocalSym (Decl.Ident, Decl.Type, SC_STRUCTFIELD, StructSize);
849 if (!FlexibleMember) {
850 StructSize += CheckedSizeOf (Decl.Type);
854 NextMember: if (CurTok.Tok != TOK_COMMA) {
862 /* If we have bits from bit-fields left, add them to the size. */
864 StructSize += ((BitOffs + CHAR_BITS - 1) / CHAR_BITS);
867 /* Skip the closing brace */
870 /* Remember the symbol table and leave the struct level */
871 FieldTab = GetSymTab ();
874 /* Make a real entry from the forward decl and return it */
875 return AddStructSym (Name, SC_STRUCT, StructSize, FieldTab);
880 static void ParseTypeSpec (DeclSpec* D, long Default, TypeCode Qualifiers)
881 /* Parse a type specifier */
886 /* Assume we have an explicit type */
887 D->Flags &= ~DS_DEF_TYPE;
889 /* Read type qualifiers if we have any */
890 Qualifiers |= OptionalQualifiers (T_QUAL_CONST | T_QUAL_VOLATILE);
892 /* Look at the data type */
893 switch (CurTok.Tok) {
897 D->Type[0].C = T_VOID;
899 D->Type[1].C = T_END;
904 D->Type[0].C = GetDefaultChar();
905 D->Type[1].C = T_END;
910 if (CurTok.Tok == TOK_UNSIGNED) {
913 D->Type[0].C = T_ULONG;
914 D->Type[1].C = T_END;
918 D->Type[0].C = T_LONG;
919 D->Type[1].C = T_END;
925 if (CurTok.Tok == TOK_UNSIGNED) {
928 D->Type[0].C = T_USHORT;
929 D->Type[1].C = T_END;
933 D->Type[0].C = T_SHORT;
934 D->Type[1].C = T_END;
940 D->Type[0].C = T_INT;
941 D->Type[1].C = T_END;
946 switch (CurTok.Tok) {
950 D->Type[0].C = T_SCHAR;
951 D->Type[1].C = T_END;
957 D->Type[0].C = T_SHORT;
958 D->Type[1].C = T_END;
964 D->Type[0].C = T_LONG;
965 D->Type[1].C = T_END;
973 D->Type[0].C = T_INT;
974 D->Type[1].C = T_END;
981 switch (CurTok.Tok) {
985 D->Type[0].C = T_UCHAR;
986 D->Type[1].C = T_END;
992 D->Type[0].C = T_USHORT;
993 D->Type[1].C = T_END;
999 D->Type[0].C = T_ULONG;
1000 D->Type[1].C = T_END;
1008 D->Type[0].C = T_UINT;
1009 D->Type[1].C = T_END;
1016 D->Type[0].C = T_FLOAT;
1017 D->Type[1].C = T_END;
1022 D->Type[0].C = T_DOUBLE;
1023 D->Type[1].C = T_END;
1029 if (CurTok.Tok == TOK_IDENT) {
1030 strcpy (Ident, CurTok.Ident);
1033 AnonName (Ident, "union");
1035 /* Remember we have an extra type decl */
1036 D->Flags |= DS_EXTRA_TYPE;
1037 /* Declare the union in the current scope */
1038 Entry = ParseUnionDecl (Ident);
1039 /* Encode the union entry into the type */
1040 D->Type[0].C = T_UNION;
1041 SetSymEntry (D->Type, Entry);
1042 D->Type[1].C = T_END;
1048 if (CurTok.Tok == TOK_IDENT) {
1049 strcpy (Ident, CurTok.Ident);
1052 AnonName (Ident, "struct");
1054 /* Remember we have an extra type decl */
1055 D->Flags |= DS_EXTRA_TYPE;
1056 /* Declare the struct in the current scope */
1057 Entry = ParseStructDecl (Ident);
1058 /* Encode the struct entry into the type */
1059 D->Type[0].C = T_STRUCT;
1060 SetSymEntry (D->Type, Entry);
1061 D->Type[1].C = T_END;
1066 if (CurTok.Tok != TOK_LCURLY) {
1068 if (CurTok.Tok == TOK_IDENT) {
1069 /* Find an entry with this name */
1070 Entry = FindTagSym (CurTok.Ident);
1072 if (SymIsLocal (Entry) && (Entry->Flags & SC_ENUM) == 0) {
1073 Error ("Symbol '%s' is already different kind", Entry->Name);
1076 /* Insert entry into table ### */
1078 /* Skip the identifier */
1081 Error ("Identifier expected");
1084 /* Remember we have an extra type decl */
1085 D->Flags |= DS_EXTRA_TYPE;
1086 /* Parse the enum decl */
1088 D->Type[0].C = T_INT;
1089 D->Type[1].C = T_END;
1093 Entry = FindSym (CurTok.Ident);
1094 if (Entry && SymIsTypeDef (Entry)) {
1095 /* It's a typedef */
1097 TypeCopy (D->Type, Entry->Type);
1104 Error ("Type expected");
1105 D->Type[0].C = T_INT;
1106 D->Type[1].C = T_END;
1108 D->Flags |= DS_DEF_TYPE;
1109 D->Type[0].C = (TypeCode) Default;
1110 D->Type[1].C = T_END;
1115 /* There may also be qualifiers *after* the initial type */
1116 D->Type[0].C |= (Qualifiers | OptionalQualifiers (T_QUAL_CONST | T_QUAL_VOLATILE));
1121 static Type* ParamTypeCvt (Type* T)
1122 /* If T is an array, convert it to a pointer else do nothing. Return the
1126 if (IsTypeArray (T)) {
1134 static void ParseOldStyleParamList (FuncDesc* F)
1135 /* Parse an old style (K&R) parameter list */
1137 /* Some fix point tokens that are used for error recovery */
1138 static const token_t TokenList[] = { TOK_COMMA, TOK_RPAREN, TOK_SEMI };
1141 while (CurTok.Tok != TOK_RPAREN) {
1143 /* List of identifiers expected */
1144 if (CurTok.Tok == TOK_IDENT) {
1146 /* Create a symbol table entry with type int */
1147 AddLocalSym (CurTok.Ident, type_int, SC_AUTO | SC_PARAM | SC_DEF | SC_DEFTYPE, 0);
1149 /* Count arguments */
1152 /* Skip the identifier */
1156 /* Not a parameter name */
1157 Error ("Identifier expected");
1159 /* Try some smart error recovery */
1160 SkipTokens (TokenList, sizeof(TokenList) / sizeof(TokenList[0]));
1163 /* Check for more parameters */
1164 if (CurTok.Tok == TOK_COMMA) {
1171 /* Skip right paren. We must explicitly check for one here, since some of
1172 ** the breaks above bail out without checking.
1176 /* An optional list of type specifications follows */
1177 while (CurTok.Tok != TOK_LCURLY) {
1181 /* Read the declaration specifier */
1182 ParseDeclSpec (&Spec, SC_AUTO, T_INT);
1184 /* We accept only auto and register as storage class specifiers, but
1185 ** we ignore all this, since we use auto anyway.
1187 if ((Spec.StorageClass & SC_AUTO) == 0 &&
1188 (Spec.StorageClass & SC_REGISTER) == 0) {
1189 Error ("Illegal storage class");
1192 /* Parse a comma separated variable list */
1197 /* Read the parameter */
1198 ParseDecl (&Spec, &Decl, DM_NEED_IDENT);
1199 if (Decl.Ident[0] != '\0') {
1201 /* We have a name given. Search for the symbol */
1202 SymEntry* Sym = FindLocalSym (Decl.Ident);
1204 /* Check if we already changed the type for this
1207 if (Sym->Flags & SC_DEFTYPE) {
1208 /* Found it, change the default type to the one given */
1209 ChangeSymType (Sym, ParamTypeCvt (Decl.Type));
1210 /* Reset the "default type" flag */
1211 Sym->Flags &= ~SC_DEFTYPE;
1213 /* Type has already been changed */
1214 Error ("Redefinition for parameter '%s'", Sym->Name);
1217 Error ("Unknown identifier: '%s'", Decl.Ident);
1221 if (CurTok.Tok == TOK_COMMA) {
1229 /* Variable list must be semicolon terminated */
1236 static void ParseAnsiParamList (FuncDesc* F)
1237 /* Parse a new style (ANSI) parameter list */
1240 while (CurTok.Tok != TOK_RPAREN) {
1246 /* Allow an ellipsis as last parameter */
1247 if (CurTok.Tok == TOK_ELLIPSIS) {
1249 F->Flags |= FD_VARIADIC;
1253 /* Read the declaration specifier */
1254 ParseDeclSpec (&Spec, SC_AUTO, T_INT);
1256 /* We accept only auto and register as storage class specifiers */
1257 if ((Spec.StorageClass & SC_AUTO) == SC_AUTO) {
1258 Spec.StorageClass = SC_AUTO | SC_PARAM | SC_DEF;
1259 } else if ((Spec.StorageClass & SC_REGISTER) == SC_REGISTER) {
1260 Spec.StorageClass = SC_REGISTER | SC_STATIC | SC_PARAM | SC_DEF;
1262 Error ("Illegal storage class");
1263 Spec.StorageClass = SC_AUTO | SC_PARAM | SC_DEF;
1266 /* Allow parameters without a name, but remember if we had some to
1267 ** eventually print an error message later.
1269 ParseDecl (&Spec, &Decl, DM_ACCEPT_IDENT);
1270 if (Decl.Ident[0] == '\0') {
1272 /* Unnamed symbol. Generate a name that is not user accessible,
1273 ** then handle the symbol normal.
1275 AnonName (Decl.Ident, "param");
1276 F->Flags |= FD_UNNAMED_PARAMS;
1278 /* Clear defined bit on nonames */
1279 Decl.StorageClass &= ~SC_DEF;
1282 /* Parse attributes for this parameter */
1283 ParseAttribute (&Decl);
1285 /* Create a symbol table entry */
1286 Sym = AddLocalSym (Decl.Ident, ParamTypeCvt (Decl.Type), Decl.StorageClass, 0);
1288 /* Add attributes if we have any */
1289 SymUseAttr (Sym, &Decl);
1291 /* If the parameter is a struct or union, emit a warning */
1292 if (IsClassStruct (Decl.Type)) {
1293 if (IS_Get (&WarnStructParam)) {
1294 Warning ("Passing struct by value for parameter '%s'", Decl.Ident);
1298 /* Count arguments */
1301 /* Check for more parameters */
1302 if (CurTok.Tok == TOK_COMMA) {
1309 /* Skip right paren. We must explicitly check for one here, since some of
1310 ** the breaks above bail out without checking.
1317 static FuncDesc* ParseFuncDecl (void)
1318 /* Parse the argument list of a function. */
1322 SymEntry* WrappedCall;
1323 unsigned char WrappedCallData;
1325 /* Create a new function descriptor */
1326 FuncDesc* F = NewFuncDesc ();
1328 /* Enter a new lexical level */
1329 EnterFunctionLevel ();
1331 /* Check for several special parameter lists */
1332 if (CurTok.Tok == TOK_RPAREN) {
1333 /* Parameter list is empty */
1334 F->Flags |= (FD_EMPTY | FD_VARIADIC);
1335 } else if (CurTok.Tok == TOK_VOID && NextTok.Tok == TOK_RPAREN) {
1336 /* Parameter list declared as void */
1338 F->Flags |= FD_VOID_PARAM;
1339 } else if (CurTok.Tok == TOK_IDENT &&
1340 (NextTok.Tok == TOK_COMMA || NextTok.Tok == TOK_RPAREN)) {
1341 /* If the identifier is a typedef, we have a new style parameter list,
1342 ** if it's some other identifier, it's an old style parameter list.
1344 Sym = FindSym (CurTok.Ident);
1345 if (Sym == 0 || !SymIsTypeDef (Sym)) {
1346 /* Old style (K&R) function. */
1347 F->Flags |= FD_OLDSTYLE;
1352 if ((F->Flags & FD_OLDSTYLE) == 0) {
1354 /* New style function */
1355 ParseAnsiParamList (F);
1358 /* Old style function */
1359 ParseOldStyleParamList (F);
1362 /* Remember the last function parameter. We need it later for several
1363 ** purposes, for example when passing stuff to fastcall functions. Since
1364 ** more symbols are added to the table, it is easier if we remember it
1365 ** now, since it is currently the last entry in the symbol table.
1367 F->LastParam = GetSymTab()->SymTail;
1369 /* Assign offsets. If the function has a variable parameter list,
1370 ** there's one additional byte (the arg size).
1372 Offs = (F->Flags & FD_VARIADIC)? 1 : 0;
1375 unsigned Size = CheckedSizeOf (Sym->Type);
1376 if (SymIsRegVar (Sym)) {
1377 Sym->V.R.SaveOffs = Offs;
1382 F->ParamSize += Size;
1386 /* Leave the lexical level remembering the symbol tables */
1387 RememberFunctionLevel (F);
1389 /* Did we have a WrappedCall for this function? */
1390 GetWrappedCall((void **) &WrappedCall, &WrappedCallData);
1392 F->WrappedCall = WrappedCall;
1393 F->WrappedCallData = WrappedCallData;
1396 /* Return the function descriptor */
1402 static void Declarator (const DeclSpec* Spec, Declaration* D, declmode_t Mode)
1403 /* Recursively process declarators. Build a type array in reverse order. */
1405 /* Read optional function or pointer qualifiers. They modify the
1406 ** identifier or token to the right. For convenience, we allow a calling
1407 ** convention also for pointers here. If it's a pointer-to-function, the
1408 ** qualifier later will be transfered to the function itself. If it's a
1409 ** pointer to something else, it will be flagged as an error.
1411 TypeCode Qualifiers = OptionalQualifiers (T_QUAL_ADDRSIZE | T_QUAL_CCONV);
1413 /* Pointer to something */
1414 if (CurTok.Tok == TOK_STAR) {
1419 /* Allow const, restrict, and volatile qualifiers */
1420 Qualifiers |= OptionalQualifiers (T_QUAL_CONST | T_QUAL_VOLATILE | T_QUAL_RESTRICT);
1422 /* Parse the type that the pointer points to */
1423 Declarator (Spec, D, Mode);
1426 AddTypeToDeclaration (D, T_PTR | Qualifiers);
1430 if (CurTok.Tok == TOK_LPAREN) {
1432 Declarator (Spec, D, Mode);
1435 /* Things depend on Mode now:
1436 ** - Mode == DM_NEED_IDENT means:
1437 ** we *must* have a type and a variable identifer.
1438 ** - Mode == DM_NO_IDENT means:
1439 ** we must have a type but no variable identifer
1440 ** (if there is one, it's not read).
1441 ** - Mode == DM_ACCEPT_IDENT means:
1442 ** we *may* have an identifier. If there is an identifier,
1443 ** it is read, but it is no error, if there is none.
1445 if (Mode == DM_NO_IDENT) {
1447 } else if (CurTok.Tok == TOK_IDENT) {
1448 strcpy (D->Ident, CurTok.Ident);
1451 if (Mode == DM_NEED_IDENT) {
1452 Error ("Identifier expected");
1458 while (CurTok.Tok == TOK_LBRACK || CurTok.Tok == TOK_LPAREN) {
1459 if (CurTok.Tok == TOK_LPAREN) {
1461 /* Function declaration */
1463 SymEntry* PrevEntry;
1465 /* Skip the opening paren */
1468 /* Parse the function declaration */
1469 F = ParseFuncDecl ();
1471 /* We cannot specify fastcall for variadic functions */
1472 if ((F->Flags & FD_VARIADIC) && (Qualifiers & T_QUAL_FASTCALL)) {
1473 Error ("Variadic functions cannot be __fastcall__");
1474 Qualifiers &= ~T_QUAL_FASTCALL;
1477 /* Was there a previous entry? If so, copy WrappedCall info from it */
1478 PrevEntry = FindGlobalSym (D->Ident);
1479 if (PrevEntry && PrevEntry->Flags & SC_FUNC) {
1480 FuncDesc* D = PrevEntry->V.F.Func;
1481 if (D->WrappedCall && !F->WrappedCall) {
1482 F->WrappedCall = D->WrappedCall;
1483 F->WrappedCallData = D->WrappedCallData;
1487 /* Add the function type. Be sure to bounds check the type buffer */
1488 NeedTypeSpace (D, 1);
1489 D->Type[D->Index].C = T_FUNC | Qualifiers;
1490 D->Type[D->Index].A.P = F;
1493 /* Qualifiers now used */
1494 Qualifiers = T_QUAL_NONE;
1497 /* Array declaration. */
1498 long Size = UNSPECIFIED;
1500 /* We cannot have any qualifiers for an array */
1501 if (Qualifiers != T_QUAL_NONE) {
1502 Error ("Invalid qualifiers for array");
1503 Qualifiers = T_QUAL_NONE;
1506 /* Skip the left bracket */
1509 /* Read the size if it is given */
1510 if (CurTok.Tok != TOK_RBRACK) {
1512 ConstAbsIntExpr (hie1, &Expr);
1513 if (Expr.IVal <= 0) {
1514 if (D->Ident[0] != '\0') {
1515 Error ("Size of array '%s' is invalid", D->Ident);
1517 Error ("Size of array is invalid");
1524 /* Skip the right bracket */
1527 /* Add the array type with the size to the type */
1528 NeedTypeSpace (D, 1);
1529 D->Type[D->Index].C = T_ARRAY;
1530 D->Type[D->Index].A.L = Size;
1535 /* If we have remaining qualifiers, flag them as invalid */
1536 if (Qualifiers & T_QUAL_NEAR) {
1537 Error ("Invalid '__near__' qualifier");
1539 if (Qualifiers & T_QUAL_FAR) {
1540 Error ("Invalid '__far__' qualifier");
1542 if (Qualifiers & T_QUAL_FASTCALL) {
1543 Error ("Invalid '__fastcall__' qualifier");
1545 if (Qualifiers & T_QUAL_CDECL) {
1546 Error ("Invalid '__cdecl__' qualifier");
1552 /*****************************************************************************/
1554 /*****************************************************************************/
1558 Type* ParseType (Type* T)
1559 /* Parse a complete type specification */
1564 /* Get a type without a default */
1565 InitDeclSpec (&Spec);
1566 ParseTypeSpec (&Spec, -1, T_QUAL_NONE);
1568 /* Parse additional declarators */
1569 ParseDecl (&Spec, &Decl, DM_NO_IDENT);
1571 /* Copy the type to the target buffer */
1572 TypeCopy (T, Decl.Type);
1574 /* Return a pointer to the target buffer */
1580 void ParseDecl (const DeclSpec* Spec, Declaration* D, declmode_t Mode)
1581 /* Parse a variable, type or function declaration */
1583 /* Initialize the Declaration struct */
1584 InitDeclaration (D);
1586 /* Get additional declarators and the identifier */
1587 Declarator (Spec, D, Mode);
1589 /* Add the base type. */
1590 NeedTypeSpace (D, TypeLen (Spec->Type) + 1); /* Bounds check */
1591 TypeCopy (D->Type + D->Index, Spec->Type);
1593 /* Use the storage class from the declspec */
1594 D->StorageClass = Spec->StorageClass;
1596 /* Do several fixes on qualifiers */
1597 FixQualifiers (D->Type);
1599 /* If we have a function, add a special storage class */
1600 if (IsTypeFunc (D->Type)) {
1601 D->StorageClass |= SC_FUNC;
1604 /* Parse attributes for this declaration */
1607 /* Check several things for function or function pointer types */
1608 if (IsTypeFunc (D->Type) || IsTypeFuncPtr (D->Type)) {
1610 /* A function. Check the return type */
1611 Type* RetType = GetFuncReturn (D->Type);
1613 /* Functions may not return functions or arrays */
1614 if (IsTypeFunc (RetType)) {
1615 Error ("Functions are not allowed to return functions");
1616 } else if (IsTypeArray (RetType)) {
1617 Error ("Functions are not allowed to return arrays");
1620 /* The return type must not be qualified */
1621 if (GetQualifier (RetType) != T_QUAL_NONE && RetType[1].C == T_END) {
1623 if (GetType (RetType) == T_TYPE_VOID) {
1624 /* A qualified void type is always an error */
1625 Error ("function definition has qualified void return type");
1627 /* For others, qualifiers are ignored */
1628 Warning ("type qualifiers ignored on function return type");
1629 RetType[0].C = UnqualifiedType (RetType[0].C);
1633 /* Warn about an implicit int return in the function */
1634 if ((Spec->Flags & DS_DEF_TYPE) != 0 &&
1635 RetType[0].C == T_INT && RetType[1].C == T_END) {
1636 /* Function has an implicit int return. Output a warning if we don't
1637 ** have the C89 standard enabled explicitly.
1639 if (IS_Get (&Standard) >= STD_C99) {
1640 Warning ("Implicit 'int' return type is an obsolete feature");
1642 GetFuncDesc (D->Type)->Flags |= FD_OLDSTYLE_INTRET;
1647 /* For anthing that is not a function or typedef, check for an implicit
1650 if ((D->StorageClass & SC_FUNC) != SC_FUNC &&
1651 (D->StorageClass & SC_TYPEMASK) != SC_TYPEDEF) {
1652 /* If the standard was not set explicitly to C89, print a warning
1653 ** for variables with implicit int type.
1655 if ((Spec->Flags & DS_DEF_TYPE) != 0 && IS_Get (&Standard) >= STD_C99) {
1656 Warning ("Implicit 'int' is an obsolete feature");
1660 /* Check the size of the generated type */
1661 if (!IsTypeFunc (D->Type) && !IsTypeVoid (D->Type)) {
1662 unsigned Size = SizeOf (D->Type);
1663 if (Size >= 0x10000) {
1664 if (D->Ident[0] != '\0') {
1665 Error ("Size of '%s' is invalid (0x%06X)", D->Ident, Size);
1667 Error ("Invalid size in declaration (0x%06X)", Size);
1676 void ParseDeclSpec (DeclSpec* D, unsigned DefStorage, long DefType)
1677 /* Parse a declaration specification */
1679 TypeCode Qualifiers;
1681 /* Initialize the DeclSpec struct */
1684 /* There may be qualifiers *before* the storage class specifier */
1685 Qualifiers = OptionalQualifiers (T_QUAL_CONST | T_QUAL_VOLATILE);
1687 /* Now get the storage class specifier for this declaration */
1688 ParseStorageClass (D, DefStorage);
1690 /* Parse the type specifiers passing any initial type qualifiers */
1691 ParseTypeSpec (D, DefType, Qualifiers);
1696 void CheckEmptyDecl (const DeclSpec* D)
1697 /* Called after an empty type declaration (that is, a type declaration without
1698 ** a variable). Checks if the declaration does really make sense and issues a
1702 if ((D->Flags & DS_EXTRA_TYPE) == 0) {
1703 Warning ("Useless declaration");
1709 static void SkipInitializer (unsigned BracesExpected)
1710 /* Skip the remainder of an initializer in case of errors. Try to be somewhat
1711 ** smart so we don't have too many following errors.
1714 while (CurTok.Tok != TOK_CEOF && CurTok.Tok != TOK_SEMI && BracesExpected > 0) {
1715 switch (CurTok.Tok) {
1716 case TOK_RCURLY: --BracesExpected; break;
1717 case TOK_LCURLY: ++BracesExpected; break;
1726 static unsigned OpeningCurlyBraces (unsigned BracesNeeded)
1727 /* Accept any number of opening curly braces around an initialization, skip
1728 ** them and return the number. If the number of curly braces is less than
1729 ** BracesNeeded, issue a warning.
1732 unsigned BraceCount = 0;
1733 while (CurTok.Tok == TOK_LCURLY) {
1737 if (BraceCount < BracesNeeded) {
1738 Error ("'{' expected");
1745 static void ClosingCurlyBraces (unsigned BracesExpected)
1746 /* Accept and skip the given number of closing curly braces together with
1747 ** an optional comma. Output an error messages, if the input does not contain
1748 ** the expected number of braces.
1751 while (BracesExpected) {
1752 if (CurTok.Tok == TOK_RCURLY) {
1754 } else if (CurTok.Tok == TOK_COMMA && NextTok.Tok == TOK_RCURLY) {
1758 Error ("'}' expected");
1767 static void DefineData (ExprDesc* Expr)
1768 /* Output a data definition for the given expression */
1770 switch (ED_GetLoc (Expr)) {
1773 /* Absolute: numeric address or const */
1774 g_defdata (TypeOf (Expr->Type) | CF_CONST, Expr->IVal, 0);
1778 /* Global variable */
1779 g_defdata (CF_EXTERNAL, Expr->Name, Expr->IVal);
1784 /* Static variable or literal in the literal pool */
1785 g_defdata (CF_STATIC, Expr->Name, Expr->IVal);
1788 case E_LOC_REGISTER:
1789 /* Register variable. Taking the address is usually not
1792 if (IS_Get (&AllowRegVarAddr) == 0) {
1793 Error ("Cannot take the address of a register variable");
1795 g_defdata (CF_REGVAR, Expr->Name, Expr->IVal);
1801 Error ("Non constant initializer");
1805 Internal ("Unknown constant type: 0x%04X", ED_GetLoc (Expr));
1811 static void OutputBitFieldData (StructInitData* SI)
1812 /* Output bit field data */
1814 /* Ignore if we have no data */
1815 if (SI->ValBits > 0) {
1817 /* Output the data */
1818 g_defdata (CF_INT | CF_UNSIGNED | CF_CONST, SI->BitVal, 0);
1820 /* Clear the data from SI and account for the size */
1823 SI->Offs += SIZEOF_INT;
1829 static void ParseScalarInitInternal (Type* T, ExprDesc* ED)
1830 /* Parse initializaton for scalar data types. This function will not output the
1831 ** data but return it in ED.
1834 /* Optional opening brace */
1835 unsigned BraceCount = OpeningCurlyBraces (0);
1837 /* We warn if an initializer for a scalar contains braces, because this is
1838 ** quite unusual and often a sign for some problem in the input.
1840 if (BraceCount > 0) {
1841 Warning ("Braces around scalar initializer");
1844 /* Get the expression and convert it to the target type */
1845 ConstExpr (hie1, ED);
1846 TypeConversion (ED, T);
1848 /* Close eventually opening braces */
1849 ClosingCurlyBraces (BraceCount);
1854 static unsigned ParseScalarInit (Type* T)
1855 /* Parse initializaton for scalar data types. Return the number of data bytes. */
1859 /* Parse initialization */
1860 ParseScalarInitInternal (T, &ED);
1862 /* Output the data */
1871 static unsigned ParsePointerInit (Type* T)
1872 /* Parse initializaton for pointer data types. Return the number of data bytes. */
1874 /* Optional opening brace */
1875 unsigned BraceCount = OpeningCurlyBraces (0);
1879 ConstExpr (hie1, &ED);
1880 TypeConversion (&ED, T);
1882 /* Output the data */
1885 /* Close eventually opening braces */
1886 ClosingCurlyBraces (BraceCount);
1894 static unsigned ParseArrayInit (Type* T, int AllowFlexibleMembers)
1895 /* Parse initializaton for arrays. Return the number of data bytes. */
1899 /* Get the array data */
1900 Type* ElementType = GetElementType (T);
1901 unsigned ElementSize = CheckedSizeOf (ElementType);
1902 long ElementCount = GetElementCount (T);
1904 /* Special handling for a character array initialized by a literal */
1905 if (IsTypeChar (ElementType) &&
1906 (CurTok.Tok == TOK_SCONST || CurTok.Tok == TOK_WCSCONST ||
1907 (CurTok.Tok == TOK_LCURLY &&
1908 (NextTok.Tok == TOK_SCONST || NextTok.Tok == TOK_WCSCONST)))) {
1910 /* Char array initialized by string constant */
1913 /* If we initializer is enclosed in brackets, remember this fact and
1914 ** skip the opening bracket.
1916 NeedParen = (CurTok.Tok == TOK_LCURLY);
1921 /* Translate into target charset */
1922 TranslateLiteral (CurTok.SVal);
1924 /* If the array is one too small for the string literal, omit the
1927 Count = GetLiteralSize (CurTok.SVal);
1928 if (ElementCount != UNSPECIFIED &&
1929 ElementCount != FLEXIBLE &&
1930 Count == ElementCount + 1) {
1931 /* Omit the trailing zero */
1935 /* Output the data */
1936 g_defbytes (GetLiteralStr (CurTok.SVal), Count);
1938 /* Skip the string */
1941 /* If the initializer was enclosed in curly braces, we need a closing
1953 /* Initialize the array members */
1955 while (CurTok.Tok != TOK_RCURLY) {
1956 /* Flexible array members may not be initialized within
1957 ** an array (because the size of each element may differ
1960 ParseInitInternal (ElementType, 0);
1962 if (CurTok.Tok != TOK_COMMA)
1967 /* Closing curly braces */
1971 if (ElementCount == UNSPECIFIED) {
1972 /* Number of elements determined by initializer */
1973 SetElementCount (T, Count);
1974 ElementCount = Count;
1975 } else if (ElementCount == FLEXIBLE && AllowFlexibleMembers) {
1976 /* In non ANSI mode, allow initialization of flexible array
1979 ElementCount = Count;
1980 } else if (Count < ElementCount) {
1981 g_zerobytes ((ElementCount - Count) * ElementSize);
1982 } else if (Count > ElementCount) {
1983 Error ("Too many initializers");
1985 return ElementCount * ElementSize;
1990 static unsigned ParseStructInit (Type* T, int AllowFlexibleMembers)
1991 /* Parse initialization of a struct or union. Return the number of data bytes. */
1998 /* Consume the opening curly brace */
2001 /* Get a pointer to the struct entry from the type */
2002 Entry = GetSymEntry (T);
2004 /* Get the size of the struct from the symbol table entry */
2005 SI.Size = Entry->V.S.Size;
2007 /* Check if this struct definition has a field table. If it doesn't, it
2008 ** is an incomplete definition.
2010 Tab = Entry->V.S.SymTab;
2012 Error ("Cannot initialize variables with incomplete type");
2013 /* Try error recovery */
2014 SkipInitializer (1);
2015 /* Nothing initialized */
2019 /* Get a pointer to the list of symbols */
2020 Entry = Tab->SymHead;
2022 /* Initialize fields */
2026 while (CurTok.Tok != TOK_RCURLY) {
2030 Error ("Too many initializers");
2031 SkipInitializer (1);
2035 /* Parse initialization of one field. Bit-fields need a special
2038 if (SymIsBitField (Entry)) {
2044 /* Calculate the bitmask from the bit-field data */
2045 unsigned Mask = (1U << Entry->V.B.BitWidth) - 1U;
2048 CHECK (Entry->V.B.Offs * CHAR_BITS + Entry->V.B.BitOffs ==
2049 SI.Offs * CHAR_BITS + SI.ValBits);
2051 /* This may be an anonymous bit-field, in which case it doesn't
2052 ** have an initializer.
2054 if (IsAnonName (Entry->Name)) {
2055 /* Account for the data and output it if we have a full word */
2056 SI.ValBits += Entry->V.B.BitWidth;
2057 CHECK (SI.ValBits <= INT_BITS);
2058 if (SI.ValBits == INT_BITS) {
2059 OutputBitFieldData (&SI);
2063 /* Read the data, check for a constant integer, do a range
2066 ParseScalarInitInternal (type_uint, &ED);
2067 if (!ED_IsConstAbsInt (&ED)) {
2068 Error ("Constant initializer expected");
2069 ED_MakeConstAbsInt (&ED, 1);
2071 if (ED.IVal > (long) Mask) {
2072 Warning ("Truncating value in bit-field initializer");
2073 ED.IVal &= (long) Mask;
2075 Val = (unsigned) ED.IVal;
2078 /* Add the value to the currently stored bit-field value */
2079 Shift = (Entry->V.B.Offs - SI.Offs) * CHAR_BITS + Entry->V.B.BitOffs;
2080 SI.BitVal |= (Val << Shift);
2082 /* Account for the data and output it if we have a full word */
2083 SI.ValBits += Entry->V.B.BitWidth;
2084 CHECK (SI.ValBits <= INT_BITS);
2085 if (SI.ValBits == INT_BITS) {
2086 OutputBitFieldData (&SI);
2091 /* Standard member. We should never have stuff from a
2094 CHECK (SI.ValBits == 0);
2096 /* Flexible array members may only be initialized if they are
2097 ** the last field (or part of the last struct field).
2099 SI.Offs += ParseInitInternal (Entry->Type, AllowFlexibleMembers && Entry->NextSym == 0);
2102 /* More initializers? */
2103 if (CurTok.Tok != TOK_COMMA) {
2107 /* Skip the comma */
2111 /* Next member. For unions, only the first one can be initialized */
2112 if (IsTypeUnion (T)) {
2117 Entry = Entry->NextSym;
2121 /* Consume the closing curly brace */
2124 /* If we have data from a bit-field left, output it now */
2125 OutputBitFieldData (&SI);
2127 /* If there are struct fields left, reserve additional storage */
2128 if (SI.Offs < SI.Size) {
2129 g_zerobytes (SI.Size - SI.Offs);
2133 /* Return the actual number of bytes initialized. This number may be
2134 ** larger than sizeof (Struct) if flexible array members are present and
2135 ** were initialized (possible in non ANSI mode).
2142 static unsigned ParseVoidInit (Type* T)
2143 /* Parse an initialization of a void variable (special cc65 extension).
2144 ** Return the number of bytes initialized.
2153 /* Allow an arbitrary list of values */
2156 ConstExpr (hie1, &Expr);
2157 switch (UnqualifiedType (Expr.Type[0].C)) {
2161 if (ED_IsConstAbsInt (&Expr)) {
2162 /* Make it byte sized */
2166 Size += SIZEOF_CHAR;
2175 if (ED_IsConstAbsInt (&Expr)) {
2176 /* Make it word sized */
2177 Expr.IVal &= 0xFFFF;
2185 if (ED_IsConstAbsInt (&Expr)) {
2186 /* Make it dword sized */
2187 Expr.IVal &= 0xFFFFFFFF;
2190 Size += SIZEOF_LONG;
2194 Error ("Illegal type in initialization");
2199 if (CurTok.Tok != TOK_COMMA) {
2204 } while (CurTok.Tok != TOK_RCURLY);
2209 /* Number of bytes determined by initializer */
2212 /* Return the number of bytes initialized */
2218 static unsigned ParseInitInternal (Type* T, int AllowFlexibleMembers)
2219 /* Parse initialization of variables. Return the number of data bytes. */
2221 switch (UnqualifiedType (T->C)) {
2233 return ParseScalarInit (T);
2236 return ParsePointerInit (T);
2239 return ParseArrayInit (T, AllowFlexibleMembers);
2243 return ParseStructInit (T, AllowFlexibleMembers);
2246 if (IS_Get (&Standard) == STD_CC65) {
2247 /* Special cc65 extension in non-ANSI mode */
2248 return ParseVoidInit (T);
2253 Error ("Illegal type");
2261 unsigned ParseInit (Type* T)
2262 /* Parse initialization of variables. Return the number of data bytes. */
2264 /* Parse the initialization. Flexible array members can only be initialized
2267 unsigned Size = ParseInitInternal (T, IS_Get (&Standard) == STD_CC65);
2269 /* The initialization may not generate code on global level, because code
2270 ** outside function scope will never get executed.
2272 if (HaveGlobalCode ()) {
2273 Error ("Non constant initializers");
2274 RemoveGlobalCode ();
2277 /* Return the size needed for the initialization */