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) {
338 /* Calling convention qualifier on the pointer? */
339 if (IsQualCConv (T)) {
340 /* Pull the convention off of the pointer */
341 Q = T[0].C & T_QUAL_CCONV;
342 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)) {
347 if ((T[1].C & T_QUAL_CCONV) == Q) {
348 Warning ("Pointer duplicates function's calling convention");
350 Error ("Function's and pointer's calling conventions are different");
353 if (Q == T_QUAL_FASTCALL && IsVariadicFunc (T + 1)) {
354 Error ("Variadic-function pointers cannot be __fastcall__");
356 /* Move the qualifier from the pointer to the function. */
361 Error ("Not pointer to a function; can't use a calling convention");
365 /* Apply the default far and near qualifiers if none are given */
366 Q = (T[0].C & T_QUAL_ADDRSIZE);
367 if (Q == T_QUAL_NONE) {
368 /* No address size qualifiers specified */
369 if (IsTypeFunc (T+1)) {
370 /* Pointer to function. Use the qualifier from the function,
371 ** or the default if the function doesn't have one.
373 Q = (T[1].C & T_QUAL_ADDRSIZE);
374 if (Q == T_QUAL_NONE) {
375 Q = CodeAddrSizeQualifier ();
378 Q = DataAddrSizeQualifier ();
382 /* We have address size qualifiers. If followed by a function,
383 ** apply them to the function also.
385 if (IsTypeFunc (T+1)) {
386 TypeCode FQ = (T[1].C & T_QUAL_ADDRSIZE);
387 if (FQ == T_QUAL_NONE) {
389 } else if (FQ != Q) {
390 Error ("Address size qualifier mismatch");
391 T[1].C = (T[1].C & ~T_QUAL_ADDRSIZE) | Q;
396 } else if (IsTypeFunc (T)) {
398 /* Apply the default far and near qualifiers if none are given */
399 if ((T[0].C & T_QUAL_ADDRSIZE) == 0) {
400 T[0].C |= CodeAddrSizeQualifier ();
410 static void ParseStorageClass (DeclSpec* D, unsigned DefStorage)
411 /* Parse a storage class */
413 /* Assume we're using an explicit storage class */
414 D->Flags &= ~DS_DEF_STORAGE;
416 /* Check the storage class given */
417 switch (CurTok.Tok) {
420 D->StorageClass = SC_EXTERN | SC_STATIC;
425 D->StorageClass = SC_STATIC;
430 D->StorageClass = SC_REGISTER | SC_STATIC;
435 D->StorageClass = SC_AUTO;
440 D->StorageClass = SC_TYPEDEF;
445 /* No storage class given, use default */
446 D->Flags |= DS_DEF_STORAGE;
447 D->StorageClass = DefStorage;
454 static void ParseEnumDecl (void)
455 /* Process an enum declaration . */
460 /* Accept forward definitions */
461 if (CurTok.Tok != TOK_LCURLY) {
465 /* Skip the opening curly brace */
468 /* Read the enum tags */
470 while (CurTok.Tok != TOK_RCURLY) {
472 /* We expect an identifier */
473 if (CurTok.Tok != TOK_IDENT) {
474 Error ("Identifier expected");
478 /* Remember the identifier and skip it */
479 strcpy (Ident, CurTok.Ident);
482 /* Check for an assigned value */
483 if (CurTok.Tok == TOK_ASSIGN) {
486 ConstAbsIntExpr (hie1, &Expr);
490 /* Add an entry to the symbol table */
491 AddConstSym (Ident, type_int, SC_ENUM, EnumVal++);
493 /* Check for end of definition */
494 if (CurTok.Tok != TOK_COMMA)
503 static int ParseFieldWidth (Declaration* Decl)
504 /* Parse an optional field width. Returns -1 if no field width is specified,
505 ** otherwise the width of the field.
510 if (CurTok.Tok != TOK_COLON) {
511 /* No bit-field declaration */
517 ConstAbsIntExpr (hie1, &Expr);
519 Error ("Negative width in bit-field");
522 if (Expr.IVal > (int) INT_BITS) {
523 Error ("Width of bit-field exceeds its type");
526 if (Expr.IVal == 0 && Decl->Ident[0] != '\0') {
527 Error ("Zero width for named bit-field");
530 if (!IsTypeInt (Decl->Type)) {
531 /* Only integer types may be used for bit-fields */
532 Error ("Bit-field has invalid type");
536 /* Return the field width */
537 return (int) Expr.IVal;
542 static SymEntry* StructOrUnionForwardDecl (const char* Name, unsigned Type)
543 /* Handle a struct or union forward decl */
545 /* Try to find a struct/union with the given name. If there is none,
546 ** insert a forward declaration into the current lexical level.
548 SymEntry* Entry = FindTagSym (Name);
550 Entry = AddStructSym (Name, Type, 0, 0);
551 } else if ((Entry->Flags & SC_TYPEMASK) != Type) {
552 /* Already defined, but no struct */
553 Error ("Symbol `%s' is already different kind", Name);
560 static unsigned CopyAnonStructFields (const Declaration* Decl, int Offs)
561 /* Copy fields from an anon union/struct into the current lexical level. The
562 ** function returns the size of the embedded struct/union.
565 /* Get the pointer to the symbol table entry of the anon struct */
566 SymEntry* Entry = GetSymEntry (Decl->Type);
568 /* Get the size of the anon struct */
569 unsigned Size = Entry->V.S.Size;
571 /* Get the symbol table containing the fields. If it is empty, there has
572 ** been an error before, so bail out.
574 SymTable* Tab = Entry->V.S.SymTab;
576 /* Incomplete definition - has been flagged before */
580 /* Get a pointer to the list of symbols. Then walk the list adding copies
581 ** of the embedded struct to the current level.
583 Entry = Tab->SymHead;
586 /* Enter a copy of this symbol adjusting the offset. We will just
587 ** reuse the type string here.
589 AddLocalSym (Entry->Name, Entry->Type, SC_STRUCTFIELD, Offs + Entry->V.Offs);
591 /* Currently, there can not be any attributes, but if there will be
592 ** some in the future, we want to know this.
594 CHECK (Entry->Attr == 0);
597 Entry = Entry->NextSym;
600 /* Return the size of the embedded struct */
606 static SymEntry* ParseUnionDecl (const char* Name)
607 /* Parse a union declaration. */
612 int FieldWidth; /* Width in bits, -1 if not a bit-field */
616 if (CurTok.Tok != TOK_LCURLY) {
617 /* Just a forward declaration. */
618 return StructOrUnionForwardDecl (Name, SC_UNION);
621 /* Add a forward declaration for the struct in the current lexical level */
622 AddStructSym (Name, SC_UNION, 0, 0);
624 /* Skip the curly brace */
627 /* Enter a new lexical level for the struct */
630 /* Parse union fields */
632 while (CurTok.Tok != TOK_RCURLY) {
634 /* Get the type of the entry */
636 InitDeclSpec (&Spec);
637 ParseTypeSpec (&Spec, -1, T_QUAL_NONE);
639 /* Read fields with this type */
644 /* Get type and name of the struct field */
645 ParseDecl (&Spec, &Decl, DM_ACCEPT_IDENT);
647 /* Check for a bit-field declaration */
648 FieldWidth = ParseFieldWidth (&Decl);
650 /* Ignore zero sized bit fields in a union */
651 if (FieldWidth == 0) {
655 /* Check for fields without a name */
656 if (Decl.Ident[0] == '\0') {
657 /* In cc65 mode, we allow anonymous structs/unions within
660 if (IS_Get (&Standard) >= STD_CC65 && IsClassStruct (Decl.Type)) {
661 /* This is an anonymous struct or union. Copy the fields
662 ** into the current level.
664 CopyAnonStructFields (&Decl, 0);
667 /* A non bit-field without a name is legal but useless */
668 Warning ("Declaration does not declare anything");
674 FieldSize = CheckedSizeOf (Decl.Type);
675 if (FieldSize > UnionSize) {
676 UnionSize = FieldSize;
679 /* Add a field entry to the table. */
680 if (FieldWidth > 0) {
681 AddBitField (Decl.Ident, 0, 0, FieldWidth);
683 AddLocalSym (Decl.Ident, Decl.Type, SC_STRUCTFIELD, 0);
686 NextMember: if (CurTok.Tok != TOK_COMMA) {
694 /* Skip the closing brace */
697 /* Remember the symbol table and leave the struct level */
698 FieldTab = GetSymTab ();
701 /* Make a real entry from the forward decl and return it */
702 return AddStructSym (Name, SC_UNION, UnionSize, FieldTab);
707 static SymEntry* ParseStructDecl (const char* Name)
708 /* Parse a struct declaration. */
713 int BitOffs; /* Bit offset for bit-fields */
714 int FieldWidth; /* Width in bits, -1 if not a bit-field */
718 if (CurTok.Tok != TOK_LCURLY) {
719 /* Just a forward declaration. */
720 return StructOrUnionForwardDecl (Name, SC_STRUCT);
723 /* Add a forward declaration for the struct in the current lexical level */
724 AddStructSym (Name, SC_STRUCT, 0, 0);
726 /* Skip the curly brace */
729 /* Enter a new lexical level for the struct */
732 /* Parse struct fields */
736 while (CurTok.Tok != TOK_RCURLY) {
738 /* Get the type of the entry */
740 InitDeclSpec (&Spec);
741 ParseTypeSpec (&Spec, -1, T_QUAL_NONE);
743 /* Read fields with this type */
749 /* If we had a flexible array member before, no other fields can
752 if (FlexibleMember) {
753 Error ("Flexible array member must be last field");
754 FlexibleMember = 0; /* Avoid further errors */
757 /* Get type and name of the struct field */
758 ParseDecl (&Spec, &Decl, DM_ACCEPT_IDENT);
760 /* Check for a bit-field declaration */
761 FieldWidth = ParseFieldWidth (&Decl);
763 /* If this is not a bit field, or the bit field is too large for
764 ** the remainder of the current member, or we have a bit field
765 ** with width zero, align the struct to the next member by adding
766 ** a member with an anonymous name.
769 if (FieldWidth <= 0 || (BitOffs + FieldWidth) > (int) INT_BITS) {
771 /* We need an anonymous name */
772 AnonName (Ident, "bit-field");
774 /* Add an anonymous bit-field that aligns to the next
777 AddBitField (Ident, StructSize, BitOffs, INT_BITS - BitOffs);
780 StructSize += SIZEOF_INT;
785 /* Apart from the above, a bit field with width 0 is not processed
788 if (FieldWidth == 0) {
792 /* Check if this field is a flexible array member, and
793 ** calculate the size of the field.
795 if (IsTypeArray (Decl.Type) && GetElementCount (Decl.Type) == UNSPECIFIED) {
796 /* Array with unspecified size */
797 if (StructSize == 0) {
798 Error ("Flexible array member cannot be first struct field");
801 /* Assume zero for size calculations */
802 SetElementCount (Decl.Type, FLEXIBLE);
805 /* Check for fields without names */
806 if (Decl.Ident[0] == '\0') {
807 if (FieldWidth < 0) {
808 /* In cc65 mode, we allow anonymous structs/unions within
811 if (IS_Get (&Standard) >= STD_CC65 && IsClassStruct (Decl.Type)) {
813 /* This is an anonymous struct or union. Copy the
814 ** fields into the current level.
816 StructSize += CopyAnonStructFields (&Decl, StructSize);
819 /* A non bit-field without a name is legal but useless */
820 Warning ("Declaration does not declare anything");
824 /* A bit-field without a name will get an anonymous one */
825 AnonName (Decl.Ident, "bit-field");
829 /* Add a field entry to the table */
830 if (FieldWidth > 0) {
831 /* Add full byte from the bit offset to the variable offset.
832 ** This simplifies handling he bit-field as a char type
835 unsigned Offs = StructSize + (BitOffs / CHAR_BITS);
836 AddBitField (Decl.Ident, Offs, BitOffs % CHAR_BITS, FieldWidth);
837 BitOffs += FieldWidth;
838 CHECK (BitOffs <= (int) INT_BITS);
839 if (BitOffs == INT_BITS) {
840 StructSize += SIZEOF_INT;
844 AddLocalSym (Decl.Ident, Decl.Type, SC_STRUCTFIELD, StructSize);
845 if (!FlexibleMember) {
846 StructSize += CheckedSizeOf (Decl.Type);
850 NextMember: if (CurTok.Tok != TOK_COMMA) {
858 /* If we have bits from bit-fields left, add them to the size. */
860 StructSize += ((BitOffs + CHAR_BITS - 1) / CHAR_BITS);
863 /* Skip the closing brace */
866 /* Remember the symbol table and leave the struct level */
867 FieldTab = GetSymTab ();
870 /* Make a real entry from the forward decl and return it */
871 return AddStructSym (Name, SC_STRUCT, StructSize, FieldTab);
876 static void ParseTypeSpec (DeclSpec* D, long Default, TypeCode Qualifiers)
877 /* Parse a type specifier */
882 /* Assume we have an explicit type */
883 D->Flags &= ~DS_DEF_TYPE;
885 /* Read type qualifiers if we have any */
886 Qualifiers |= OptionalQualifiers (T_QUAL_CONST | T_QUAL_VOLATILE);
888 /* Look at the data type */
889 switch (CurTok.Tok) {
893 D->Type[0].C = T_VOID;
894 D->Type[1].C = T_END;
899 D->Type[0].C = GetDefaultChar();
900 D->Type[1].C = T_END;
905 if (CurTok.Tok == TOK_UNSIGNED) {
908 D->Type[0].C = T_ULONG;
909 D->Type[1].C = T_END;
913 D->Type[0].C = T_LONG;
914 D->Type[1].C = T_END;
920 if (CurTok.Tok == TOK_UNSIGNED) {
923 D->Type[0].C = T_USHORT;
924 D->Type[1].C = T_END;
928 D->Type[0].C = T_SHORT;
929 D->Type[1].C = T_END;
935 D->Type[0].C = T_INT;
936 D->Type[1].C = T_END;
941 switch (CurTok.Tok) {
945 D->Type[0].C = T_SCHAR;
946 D->Type[1].C = T_END;
952 D->Type[0].C = T_SHORT;
953 D->Type[1].C = T_END;
959 D->Type[0].C = T_LONG;
960 D->Type[1].C = T_END;
968 D->Type[0].C = T_INT;
969 D->Type[1].C = T_END;
976 switch (CurTok.Tok) {
980 D->Type[0].C = T_UCHAR;
981 D->Type[1].C = T_END;
987 D->Type[0].C = T_USHORT;
988 D->Type[1].C = T_END;
994 D->Type[0].C = T_ULONG;
995 D->Type[1].C = T_END;
1003 D->Type[0].C = T_UINT;
1004 D->Type[1].C = T_END;
1011 D->Type[0].C = T_FLOAT;
1012 D->Type[1].C = T_END;
1017 D->Type[0].C = T_DOUBLE;
1018 D->Type[1].C = T_END;
1024 if (CurTok.Tok == TOK_IDENT) {
1025 strcpy (Ident, CurTok.Ident);
1028 AnonName (Ident, "union");
1030 /* Remember we have an extra type decl */
1031 D->Flags |= DS_EXTRA_TYPE;
1032 /* Declare the union in the current scope */
1033 Entry = ParseUnionDecl (Ident);
1034 /* Encode the union entry into the type */
1035 D->Type[0].C = T_UNION;
1036 SetSymEntry (D->Type, Entry);
1037 D->Type[1].C = T_END;
1043 if (CurTok.Tok == TOK_IDENT) {
1044 strcpy (Ident, CurTok.Ident);
1047 AnonName (Ident, "struct");
1049 /* Remember we have an extra type decl */
1050 D->Flags |= DS_EXTRA_TYPE;
1051 /* Declare the struct in the current scope */
1052 Entry = ParseStructDecl (Ident);
1053 /* Encode the struct entry into the type */
1054 D->Type[0].C = T_STRUCT;
1055 SetSymEntry (D->Type, Entry);
1056 D->Type[1].C = T_END;
1061 if (CurTok.Tok != TOK_LCURLY) {
1063 if (CurTok.Tok == TOK_IDENT) {
1064 /* Find an entry with this name */
1065 Entry = FindTagSym (CurTok.Ident);
1067 if (SymIsLocal (Entry) && (Entry->Flags & SC_ENUM) == 0) {
1068 Error ("Symbol `%s' is already different kind", Entry->Name);
1071 /* Insert entry into table ### */
1073 /* Skip the identifier */
1076 Error ("Identifier expected");
1079 /* Remember we have an extra type decl */
1080 D->Flags |= DS_EXTRA_TYPE;
1081 /* Parse the enum decl */
1083 D->Type[0].C = T_INT;
1084 D->Type[1].C = T_END;
1088 Entry = FindSym (CurTok.Ident);
1089 if (Entry && SymIsTypeDef (Entry)) {
1090 /* It's a typedef */
1092 TypeCopy (D->Type, Entry->Type);
1099 Error ("Type expected");
1100 D->Type[0].C = T_INT;
1101 D->Type[1].C = T_END;
1103 D->Flags |= DS_DEF_TYPE;
1104 D->Type[0].C = (TypeCode) Default;
1105 D->Type[1].C = T_END;
1110 /* There may also be qualifiers *after* the initial type */
1111 D->Type[0].C |= (Qualifiers | OptionalQualifiers (T_QUAL_CONST | T_QUAL_VOLATILE));
1116 static Type* ParamTypeCvt (Type* T)
1117 /* If T is an array, convert it to a pointer else do nothing. Return the
1121 if (IsTypeArray (T)) {
1129 static void ParseOldStyleParamList (FuncDesc* F)
1130 /* Parse an old style (K&R) parameter list */
1132 /* Some fix point tokens that are used for error recovery */
1133 static const token_t TokenList[] = { TOK_COMMA, TOK_RPAREN, TOK_SEMI };
1136 while (CurTok.Tok != TOK_RPAREN) {
1138 /* List of identifiers expected */
1139 if (CurTok.Tok == TOK_IDENT) {
1141 /* Create a symbol table entry with type int */
1142 AddLocalSym (CurTok.Ident, type_int, SC_AUTO | SC_PARAM | SC_DEF | SC_DEFTYPE, 0);
1144 /* Count arguments */
1147 /* Skip the identifier */
1151 /* Not a parameter name */
1152 Error ("Identifier expected");
1154 /* Try some smart error recovery */
1155 SkipTokens (TokenList, sizeof(TokenList) / sizeof(TokenList[0]));
1158 /* Check for more parameters */
1159 if (CurTok.Tok == TOK_COMMA) {
1166 /* Skip right paren. We must explicitly check for one here, since some of
1167 ** the breaks above bail out without checking.
1171 /* An optional list of type specifications follows */
1172 while (CurTok.Tok != TOK_LCURLY) {
1176 /* Read the declaration specifier */
1177 ParseDeclSpec (&Spec, SC_AUTO, T_INT);
1179 /* We accept only auto and register as storage class specifiers, but
1180 ** we ignore all this, since we use auto anyway.
1182 if ((Spec.StorageClass & SC_AUTO) == 0 &&
1183 (Spec.StorageClass & SC_REGISTER) == 0) {
1184 Error ("Illegal storage class");
1187 /* Parse a comma separated variable list */
1192 /* Read the parameter */
1193 ParseDecl (&Spec, &Decl, DM_NEED_IDENT);
1194 if (Decl.Ident[0] != '\0') {
1196 /* We have a name given. Search for the symbol */
1197 SymEntry* Sym = FindLocalSym (Decl.Ident);
1199 /* Check if we already changed the type for this
1202 if (Sym->Flags & SC_DEFTYPE) {
1203 /* Found it, change the default type to the one given */
1204 ChangeSymType (Sym, ParamTypeCvt (Decl.Type));
1205 /* Reset the "default type" flag */
1206 Sym->Flags &= ~SC_DEFTYPE;
1208 /* Type has already been changed */
1209 Error ("Redefinition for parameter `%s'", Sym->Name);
1212 Error ("Unknown identifier: `%s'", Decl.Ident);
1216 if (CurTok.Tok == TOK_COMMA) {
1224 /* Variable list must be semicolon terminated */
1231 static void ParseAnsiParamList (FuncDesc* F)
1232 /* Parse a new style (ANSI) parameter list */
1235 while (CurTok.Tok != TOK_RPAREN) {
1241 /* Allow an ellipsis as last parameter */
1242 if (CurTok.Tok == TOK_ELLIPSIS) {
1244 F->Flags |= FD_VARIADIC;
1248 /* Read the declaration specifier */
1249 ParseDeclSpec (&Spec, SC_AUTO, T_INT);
1251 /* We accept only auto and register as storage class specifiers */
1252 if ((Spec.StorageClass & SC_AUTO) == SC_AUTO) {
1253 Spec.StorageClass = SC_AUTO | SC_PARAM | SC_DEF;
1254 } else if ((Spec.StorageClass & SC_REGISTER) == SC_REGISTER) {
1255 Spec.StorageClass = SC_REGISTER | SC_STATIC | SC_PARAM | SC_DEF;
1257 Error ("Illegal storage class");
1258 Spec.StorageClass = SC_AUTO | SC_PARAM | SC_DEF;
1261 /* Allow parameters without a name, but remember if we had some to
1262 ** eventually print an error message later.
1264 ParseDecl (&Spec, &Decl, DM_ACCEPT_IDENT);
1265 if (Decl.Ident[0] == '\0') {
1267 /* Unnamed symbol. Generate a name that is not user accessible,
1268 ** then handle the symbol normal.
1270 AnonName (Decl.Ident, "param");
1271 F->Flags |= FD_UNNAMED_PARAMS;
1273 /* Clear defined bit on nonames */
1274 Decl.StorageClass &= ~SC_DEF;
1277 /* Parse attributes for this parameter */
1278 ParseAttribute (&Decl);
1280 /* Create a symbol table entry */
1281 Sym = AddLocalSym (Decl.Ident, ParamTypeCvt (Decl.Type), Decl.StorageClass, 0);
1283 /* Add attributes if we have any */
1284 SymUseAttr (Sym, &Decl);
1286 /* If the parameter is a struct or union, emit a warning */
1287 if (IsClassStruct (Decl.Type)) {
1288 if (IS_Get (&WarnStructParam)) {
1289 Warning ("Passing struct by value for parameter `%s'", Decl.Ident);
1293 /* Count arguments */
1296 /* Check for more parameters */
1297 if (CurTok.Tok == TOK_COMMA) {
1304 /* Skip right paren. We must explicitly check for one here, since some of
1305 ** the breaks above bail out without checking.
1312 static FuncDesc* ParseFuncDecl (void)
1313 /* Parse the argument list of a function. */
1318 /* Create a new function descriptor */
1319 FuncDesc* F = NewFuncDesc ();
1321 /* Enter a new lexical level */
1322 EnterFunctionLevel ();
1324 /* Check for several special parameter lists */
1325 if (CurTok.Tok == TOK_RPAREN) {
1326 /* Parameter list is empty */
1327 F->Flags |= (FD_EMPTY | FD_VARIADIC);
1328 } else if (CurTok.Tok == TOK_VOID && NextTok.Tok == TOK_RPAREN) {
1329 /* Parameter list declared as void */
1331 F->Flags |= FD_VOID_PARAM;
1332 } else if (CurTok.Tok == TOK_IDENT &&
1333 (NextTok.Tok == TOK_COMMA || NextTok.Tok == TOK_RPAREN)) {
1334 /* If the identifier is a typedef, we have a new style parameter list,
1335 ** if it's some other identifier, it's an old style parameter list.
1337 Sym = FindSym (CurTok.Ident);
1338 if (Sym == 0 || !SymIsTypeDef (Sym)) {
1339 /* Old style (K&R) function. */
1340 F->Flags |= FD_OLDSTYLE;
1345 if ((F->Flags & FD_OLDSTYLE) == 0) {
1347 /* New style function */
1348 ParseAnsiParamList (F);
1351 /* Old style function */
1352 ParseOldStyleParamList (F);
1355 /* Remember the last function parameter. We need it later for several
1356 ** purposes, for example when passing stuff to fastcall functions. Since
1357 ** more symbols are added to the table, it is easier if we remember it
1358 ** now, since it is currently the last entry in the symbol table.
1360 F->LastParam = GetSymTab()->SymTail;
1362 /* Assign offsets. If the function has a variable parameter list,
1363 ** there's one additional byte (the arg size).
1365 Offs = (F->Flags & FD_VARIADIC)? 1 : 0;
1368 unsigned Size = CheckedSizeOf (Sym->Type);
1369 if (SymIsRegVar (Sym)) {
1370 Sym->V.R.SaveOffs = Offs;
1375 F->ParamSize += Size;
1379 /* Leave the lexical level remembering the symbol tables */
1380 RememberFunctionLevel (F);
1382 /* Return the function descriptor */
1388 static void Declarator (const DeclSpec* Spec, Declaration* D, declmode_t Mode)
1389 /* Recursively process declarators. Build a type array in reverse order. */
1391 /* Read optional function or pointer qualifiers. They modify the
1392 ** identifier or token to the right. For convenience, we allow a calling
1393 ** convention also for pointers here. If it's a pointer-to-function, the
1394 ** qualifier later will be transfered to the function itself. If it's a
1395 ** pointer to something else, it will be flagged as an error.
1397 TypeCode Qualifiers = OptionalQualifiers (T_QUAL_ADDRSIZE | T_QUAL_CCONV);
1399 /* Pointer to something */
1400 if (CurTok.Tok == TOK_STAR) {
1405 /* Allow const, restrict, and volatile qualifiers */
1406 Qualifiers |= OptionalQualifiers (T_QUAL_CONST | T_QUAL_VOLATILE | T_QUAL_RESTRICT);
1408 /* Parse the type that the pointer points to */
1409 Declarator (Spec, D, Mode);
1412 AddTypeToDeclaration (D, T_PTR | Qualifiers);
1416 if (CurTok.Tok == TOK_LPAREN) {
1418 Declarator (Spec, D, Mode);
1421 /* Things depend on Mode now:
1422 ** - Mode == DM_NEED_IDENT means:
1423 ** we *must* have a type and a variable identifer.
1424 ** - Mode == DM_NO_IDENT means:
1425 ** we must have a type but no variable identifer
1426 ** (if there is one, it's not read).
1427 ** - Mode == DM_ACCEPT_IDENT means:
1428 ** we *may* have an identifier. If there is an identifier,
1429 ** it is read, but it is no error, if there is none.
1431 if (Mode == DM_NO_IDENT) {
1433 } else if (CurTok.Tok == TOK_IDENT) {
1434 strcpy (D->Ident, CurTok.Ident);
1437 if (Mode == DM_NEED_IDENT) {
1438 Error ("Identifier expected");
1444 while (CurTok.Tok == TOK_LBRACK || CurTok.Tok == TOK_LPAREN) {
1445 if (CurTok.Tok == TOK_LPAREN) {
1447 /* Function declaration */
1450 /* Skip the opening paren */
1453 /* Parse the function declaration */
1454 F = ParseFuncDecl ();
1456 /* We cannot specify fastcall for variadic functions */
1457 if ((F->Flags & FD_VARIADIC) && (Qualifiers & T_QUAL_FASTCALL)) {
1458 Error ("Variadic functions cannot be __fastcall__");
1459 Qualifiers &= ~T_QUAL_FASTCALL;
1462 /* Add the function type. Be sure to bounds check the type buffer */
1463 NeedTypeSpace (D, 1);
1464 D->Type[D->Index].C = T_FUNC | Qualifiers;
1465 D->Type[D->Index].A.P = F;
1468 /* Qualifiers now used */
1469 Qualifiers = T_QUAL_NONE;
1472 /* Array declaration. */
1473 long Size = UNSPECIFIED;
1475 /* We cannot have any qualifiers for an array */
1476 if (Qualifiers != T_QUAL_NONE) {
1477 Error ("Invalid qualifiers for array");
1478 Qualifiers = T_QUAL_NONE;
1481 /* Skip the left bracket */
1484 /* Read the size if it is given */
1485 if (CurTok.Tok != TOK_RBRACK) {
1487 ConstAbsIntExpr (hie1, &Expr);
1488 if (Expr.IVal <= 0) {
1489 if (D->Ident[0] != '\0') {
1490 Error ("Size of array `%s' is invalid", D->Ident);
1492 Error ("Size of array is invalid");
1499 /* Skip the right bracket */
1502 /* Add the array type with the size to the type */
1503 NeedTypeSpace (D, 1);
1504 D->Type[D->Index].C = T_ARRAY;
1505 D->Type[D->Index].A.L = Size;
1510 /* If we have remaining qualifiers, flag them as invalid */
1511 if (Qualifiers & T_QUAL_NEAR) {
1512 Error ("Invalid `__near__' qualifier");
1514 if (Qualifiers & T_QUAL_FAR) {
1515 Error ("Invalid `__far__' qualifier");
1517 if (Qualifiers & T_QUAL_FASTCALL) {
1518 Error ("Invalid `__fastcall__' qualifier");
1520 if (Qualifiers & T_QUAL_CDECL) {
1521 Error ("Invalid `__cdecl__' qualifier");
1527 /*****************************************************************************/
1529 /*****************************************************************************/
1533 Type* ParseType (Type* T)
1534 /* Parse a complete type specification */
1539 /* Get a type without a default */
1540 InitDeclSpec (&Spec);
1541 ParseTypeSpec (&Spec, -1, T_QUAL_NONE);
1543 /* Parse additional declarators */
1544 ParseDecl (&Spec, &Decl, DM_NO_IDENT);
1546 /* Copy the type to the target buffer */
1547 TypeCopy (T, Decl.Type);
1549 /* Return a pointer to the target buffer */
1555 void ParseDecl (const DeclSpec* Spec, Declaration* D, declmode_t Mode)
1556 /* Parse a variable, type or function declaration */
1558 /* Initialize the Declaration struct */
1559 InitDeclaration (D);
1561 /* Get additional declarators and the identifier */
1562 Declarator (Spec, D, Mode);
1564 /* Add the base type. */
1565 NeedTypeSpace (D, TypeLen (Spec->Type) + 1); /* Bounds check */
1566 TypeCopy (D->Type + D->Index, Spec->Type);
1568 /* Use the storage class from the declspec */
1569 D->StorageClass = Spec->StorageClass;
1571 /* Do several fixes on qualifiers */
1572 FixQualifiers (D->Type);
1574 /* If we have a function, add a special storage class */
1575 if (IsTypeFunc (D->Type)) {
1576 D->StorageClass |= SC_FUNC;
1579 /* Parse attributes for this declaration */
1582 /* Check several things for function or function pointer types */
1583 if (IsTypeFunc (D->Type) || IsTypeFuncPtr (D->Type)) {
1585 /* A function. Check the return type */
1586 Type* RetType = GetFuncReturn (D->Type);
1588 /* Functions may not return functions or arrays */
1589 if (IsTypeFunc (RetType)) {
1590 Error ("Functions are not allowed to return functions");
1591 } else if (IsTypeArray (RetType)) {
1592 Error ("Functions are not allowed to return arrays");
1595 /* The return type must not be qualified */
1596 if (GetQualifier (RetType) != T_QUAL_NONE && RetType[1].C == T_END) {
1598 if (GetType (RetType) == T_TYPE_VOID) {
1599 /* A qualified void type is always an error */
1600 Error ("function definition has qualified void return type");
1602 /* For others, qualifiers are ignored */
1603 Warning ("type qualifiers ignored on function return type");
1604 RetType[0].C = UnqualifiedType (RetType[0].C);
1608 /* Warn about an implicit int return in the function */
1609 if ((Spec->Flags & DS_DEF_TYPE) != 0 &&
1610 RetType[0].C == T_INT && RetType[1].C == T_END) {
1611 /* Function has an implicit int return. Output a warning if we don't
1612 ** have the C89 standard enabled explicitly.
1614 if (IS_Get (&Standard) >= STD_C99) {
1615 Warning ("Implicit `int' return type is an obsolete feature");
1617 GetFuncDesc (D->Type)->Flags |= FD_OLDSTYLE_INTRET;
1622 /* For anthing that is not a function or typedef, check for an implicit
1625 if ((D->StorageClass & SC_FUNC) != SC_FUNC &&
1626 (D->StorageClass & SC_TYPEMASK) != SC_TYPEDEF) {
1627 /* If the standard was not set explicitly to C89, print a warning
1628 ** for variables with implicit int type.
1630 if ((Spec->Flags & DS_DEF_TYPE) != 0 && IS_Get (&Standard) >= STD_C99) {
1631 Warning ("Implicit `int' is an obsolete feature");
1635 /* Check the size of the generated type */
1636 if (!IsTypeFunc (D->Type) && !IsTypeVoid (D->Type)) {
1637 unsigned Size = SizeOf (D->Type);
1638 if (Size >= 0x10000) {
1639 if (D->Ident[0] != '\0') {
1640 Error ("Size of `%s' is invalid (0x%06X)", D->Ident, Size);
1642 Error ("Invalid size in declaration (0x%06X)", Size);
1651 void ParseDeclSpec (DeclSpec* D, unsigned DefStorage, long DefType)
1652 /* Parse a declaration specification */
1654 TypeCode Qualifiers;
1656 /* Initialize the DeclSpec struct */
1659 /* There may be qualifiers *before* the storage class specifier */
1660 Qualifiers = OptionalQualifiers (T_QUAL_CONST | T_QUAL_VOLATILE);
1662 /* Now get the storage class specifier for this declaration */
1663 ParseStorageClass (D, DefStorage);
1665 /* Parse the type specifiers passing any initial type qualifiers */
1666 ParseTypeSpec (D, DefType, Qualifiers);
1671 void CheckEmptyDecl (const DeclSpec* D)
1672 /* Called after an empty type declaration (that is, a type declaration without
1673 ** a variable). Checks if the declaration does really make sense and issues a
1677 if ((D->Flags & DS_EXTRA_TYPE) == 0) {
1678 Warning ("Useless declaration");
1684 static void SkipInitializer (unsigned BracesExpected)
1685 /* Skip the remainder of an initializer in case of errors. Try to be somewhat
1686 ** smart so we don't have too many following errors.
1689 while (CurTok.Tok != TOK_CEOF && CurTok.Tok != TOK_SEMI && BracesExpected > 0) {
1690 switch (CurTok.Tok) {
1691 case TOK_RCURLY: --BracesExpected; break;
1692 case TOK_LCURLY: ++BracesExpected; break;
1701 static unsigned OpeningCurlyBraces (unsigned BracesNeeded)
1702 /* Accept any number of opening curly braces around an initialization, skip
1703 ** them and return the number. If the number of curly braces is less than
1704 ** BracesNeeded, issue a warning.
1707 unsigned BraceCount = 0;
1708 while (CurTok.Tok == TOK_LCURLY) {
1712 if (BraceCount < BracesNeeded) {
1713 Error ("`{' expected");
1720 static void ClosingCurlyBraces (unsigned BracesExpected)
1721 /* Accept and skip the given number of closing curly braces together with
1722 ** an optional comma. Output an error messages, if the input does not contain
1723 ** the expected number of braces.
1726 while (BracesExpected) {
1727 if (CurTok.Tok == TOK_RCURLY) {
1729 } else if (CurTok.Tok == TOK_COMMA && NextTok.Tok == TOK_RCURLY) {
1733 Error ("`}' expected");
1742 static void DefineData (ExprDesc* Expr)
1743 /* Output a data definition for the given expression */
1745 switch (ED_GetLoc (Expr)) {
1748 /* Absolute: numeric address or const */
1749 g_defdata (TypeOf (Expr->Type) | CF_CONST, Expr->IVal, 0);
1753 /* Global variable */
1754 g_defdata (CF_EXTERNAL, Expr->Name, Expr->IVal);
1759 /* Static variable or literal in the literal pool */
1760 g_defdata (CF_STATIC, Expr->Name, Expr->IVal);
1763 case E_LOC_REGISTER:
1764 /* Register variable. Taking the address is usually not
1767 if (IS_Get (&AllowRegVarAddr) == 0) {
1768 Error ("Cannot take the address of a register variable");
1770 g_defdata (CF_REGVAR, Expr->Name, Expr->IVal);
1776 Error ("Non constant initializer");
1780 Internal ("Unknown constant type: 0x%04X", ED_GetLoc (Expr));
1786 static void OutputBitFieldData (StructInitData* SI)
1787 /* Output bit field data */
1789 /* Ignore if we have no data */
1790 if (SI->ValBits > 0) {
1792 /* Output the data */
1793 g_defdata (CF_INT | CF_UNSIGNED | CF_CONST, SI->BitVal, 0);
1795 /* Clear the data from SI and account for the size */
1798 SI->Offs += SIZEOF_INT;
1804 static void ParseScalarInitInternal (Type* T, ExprDesc* ED)
1805 /* Parse initializaton for scalar data types. This function will not output the
1806 ** data but return it in ED.
1809 /* Optional opening brace */
1810 unsigned BraceCount = OpeningCurlyBraces (0);
1812 /* We warn if an initializer for a scalar contains braces, because this is
1813 ** quite unusual and often a sign for some problem in the input.
1815 if (BraceCount > 0) {
1816 Warning ("Braces around scalar initializer");
1819 /* Get the expression and convert it to the target type */
1820 ConstExpr (hie1, ED);
1821 TypeConversion (ED, T);
1823 /* Close eventually opening braces */
1824 ClosingCurlyBraces (BraceCount);
1829 static unsigned ParseScalarInit (Type* T)
1830 /* Parse initializaton for scalar data types. Return the number of data bytes. */
1834 /* Parse initialization */
1835 ParseScalarInitInternal (T, &ED);
1837 /* Output the data */
1846 static unsigned ParsePointerInit (Type* T)
1847 /* Parse initializaton for pointer data types. Return the number of data bytes. */
1849 /* Optional opening brace */
1850 unsigned BraceCount = OpeningCurlyBraces (0);
1854 ConstExpr (hie1, &ED);
1855 TypeConversion (&ED, T);
1857 /* Output the data */
1860 /* Close eventually opening braces */
1861 ClosingCurlyBraces (BraceCount);
1869 static unsigned ParseArrayInit (Type* T, int AllowFlexibleMembers)
1870 /* Parse initializaton for arrays. Return the number of data bytes. */
1874 /* Get the array data */
1875 Type* ElementType = GetElementType (T);
1876 unsigned ElementSize = CheckedSizeOf (ElementType);
1877 long ElementCount = GetElementCount (T);
1879 /* Special handling for a character array initialized by a literal */
1880 if (IsTypeChar (ElementType) &&
1881 (CurTok.Tok == TOK_SCONST || CurTok.Tok == TOK_WCSCONST ||
1882 (CurTok.Tok == TOK_LCURLY &&
1883 (NextTok.Tok == TOK_SCONST || NextTok.Tok == TOK_WCSCONST)))) {
1885 /* Char array initialized by string constant */
1888 /* If we initializer is enclosed in brackets, remember this fact and
1889 ** skip the opening bracket.
1891 NeedParen = (CurTok.Tok == TOK_LCURLY);
1896 /* Translate into target charset */
1897 TranslateLiteral (CurTok.SVal);
1899 /* If the array is one too small for the string literal, omit the
1902 Count = GetLiteralSize (CurTok.SVal);
1903 if (ElementCount != UNSPECIFIED &&
1904 ElementCount != FLEXIBLE &&
1905 Count == ElementCount + 1) {
1906 /* Omit the trailing zero */
1910 /* Output the data */
1911 g_defbytes (GetLiteralStr (CurTok.SVal), Count);
1913 /* Skip the string */
1916 /* If the initializer was enclosed in curly braces, we need a closing
1928 /* Initialize the array members */
1930 while (CurTok.Tok != TOK_RCURLY) {
1931 /* Flexible array members may not be initialized within
1932 ** an array (because the size of each element may differ
1935 ParseInitInternal (ElementType, 0);
1937 if (CurTok.Tok != TOK_COMMA)
1942 /* Closing curly braces */
1946 if (ElementCount == UNSPECIFIED) {
1947 /* Number of elements determined by initializer */
1948 SetElementCount (T, Count);
1949 ElementCount = Count;
1950 } else if (ElementCount == FLEXIBLE && AllowFlexibleMembers) {
1951 /* In non ANSI mode, allow initialization of flexible array
1954 ElementCount = Count;
1955 } else if (Count < ElementCount) {
1956 g_zerobytes ((ElementCount - Count) * ElementSize);
1957 } else if (Count > ElementCount) {
1958 Error ("Too many initializers");
1960 return ElementCount * ElementSize;
1965 static unsigned ParseStructInit (Type* T, int AllowFlexibleMembers)
1966 /* Parse initialization of a struct or union. Return the number of data bytes. */
1973 /* Consume the opening curly brace */
1976 /* Get a pointer to the struct entry from the type */
1977 Entry = GetSymEntry (T);
1979 /* Get the size of the struct from the symbol table entry */
1980 SI.Size = Entry->V.S.Size;
1982 /* Check if this struct definition has a field table. If it doesn't, it
1983 ** is an incomplete definition.
1985 Tab = Entry->V.S.SymTab;
1987 Error ("Cannot initialize variables with incomplete type");
1988 /* Try error recovery */
1989 SkipInitializer (1);
1990 /* Nothing initialized */
1994 /* Get a pointer to the list of symbols */
1995 Entry = Tab->SymHead;
1997 /* Initialize fields */
2001 while (CurTok.Tok != TOK_RCURLY) {
2005 Error ("Too many initializers");
2006 SkipInitializer (1);
2010 /* Parse initialization of one field. Bit-fields need a special
2013 if (SymIsBitField (Entry)) {
2019 /* Calculate the bitmask from the bit-field data */
2020 unsigned Mask = (1U << Entry->V.B.BitWidth) - 1U;
2023 CHECK (Entry->V.B.Offs * CHAR_BITS + Entry->V.B.BitOffs ==
2024 SI.Offs * CHAR_BITS + SI.ValBits);
2026 /* This may be an anonymous bit-field, in which case it doesn't
2027 ** have an initializer.
2029 if (IsAnonName (Entry->Name)) {
2030 /* Account for the data and output it if we have a full word */
2031 SI.ValBits += Entry->V.B.BitWidth;
2032 CHECK (SI.ValBits <= INT_BITS);
2033 if (SI.ValBits == INT_BITS) {
2034 OutputBitFieldData (&SI);
2038 /* Read the data, check for a constant integer, do a range
2041 ParseScalarInitInternal (type_uint, &ED);
2042 if (!ED_IsConstAbsInt (&ED)) {
2043 Error ("Constant initializer expected");
2044 ED_MakeConstAbsInt (&ED, 1);
2046 if (ED.IVal > (long) Mask) {
2047 Warning ("Truncating value in bit-field initializer");
2048 ED.IVal &= (long) Mask;
2050 Val = (unsigned) ED.IVal;
2053 /* Add the value to the currently stored bit-field value */
2054 Shift = (Entry->V.B.Offs - SI.Offs) * CHAR_BITS + Entry->V.B.BitOffs;
2055 SI.BitVal |= (Val << Shift);
2057 /* Account for the data and output it if we have a full word */
2058 SI.ValBits += Entry->V.B.BitWidth;
2059 CHECK (SI.ValBits <= INT_BITS);
2060 if (SI.ValBits == INT_BITS) {
2061 OutputBitFieldData (&SI);
2066 /* Standard member. We should never have stuff from a
2069 CHECK (SI.ValBits == 0);
2071 /* Flexible array members may only be initialized if they are
2072 ** the last field (or part of the last struct field).
2074 SI.Offs += ParseInitInternal (Entry->Type, AllowFlexibleMembers && Entry->NextSym == 0);
2077 /* More initializers? */
2078 if (CurTok.Tok != TOK_COMMA) {
2082 /* Skip the comma */
2086 /* Next member. For unions, only the first one can be initialized */
2087 if (IsTypeUnion (T)) {
2092 Entry = Entry->NextSym;
2096 /* Consume the closing curly brace */
2099 /* If we have data from a bit-field left, output it now */
2100 OutputBitFieldData (&SI);
2102 /* If there are struct fields left, reserve additional storage */
2103 if (SI.Offs < SI.Size) {
2104 g_zerobytes (SI.Size - SI.Offs);
2108 /* Return the actual number of bytes initialized. This number may be
2109 ** larger than sizeof (Struct) if flexible array members are present and
2110 ** were initialized (possible in non ANSI mode).
2117 static unsigned ParseVoidInit (void)
2118 /* Parse an initialization of a void variable (special cc65 extension).
2119 ** Return the number of bytes initialized.
2128 /* Allow an arbitrary list of values */
2131 ConstExpr (hie1, &Expr);
2132 switch (UnqualifiedType (Expr.Type[0].C)) {
2136 if (ED_IsConstAbsInt (&Expr)) {
2137 /* Make it byte sized */
2141 Size += SIZEOF_CHAR;
2150 if (ED_IsConstAbsInt (&Expr)) {
2151 /* Make it word sized */
2152 Expr.IVal &= 0xFFFF;
2160 if (ED_IsConstAbsInt (&Expr)) {
2161 /* Make it dword sized */
2162 Expr.IVal &= 0xFFFFFFFF;
2165 Size += SIZEOF_LONG;
2169 Error ("Illegal type in initialization");
2174 if (CurTok.Tok != TOK_COMMA) {
2179 } while (CurTok.Tok != TOK_RCURLY);
2184 /* Return the number of bytes initialized */
2190 static unsigned ParseInitInternal (Type* T, int AllowFlexibleMembers)
2191 /* Parse initialization of variables. Return the number of data bytes. */
2193 switch (UnqualifiedType (T->C)) {
2205 return ParseScalarInit (T);
2208 return ParsePointerInit (T);
2211 return ParseArrayInit (T, AllowFlexibleMembers);
2215 return ParseStructInit (T, AllowFlexibleMembers);
2218 if (IS_Get (&Standard) == STD_CC65) {
2219 /* Special cc65 extension in non ANSI mode */
2220 return ParseVoidInit ();
2225 Error ("Illegal type");
2233 unsigned ParseInit (Type* T)
2234 /* Parse initialization of variables. Return the number of data bytes. */
2236 /* Parse the initialization. Flexible array members can only be initialized
2239 unsigned Size = ParseInitInternal (T, IS_Get (&Standard) == STD_CC65);
2241 /* The initialization may not generate code on global level, because code
2242 ** outside function scope will never get executed.
2244 if (HaveGlobalCode ()) {
2245 Error ("Non constant initializers");
2246 RemoveGlobalCode ();
2249 /* Return the size needed for the initialization */