1 /*****************************************************************************/
5 /* Parse variable and function declarations */
9 /* (C) 1998-2008 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 static void ParseTypeSpec (DeclSpec* D, long Default, TypeCode Qualifiers);
72 /* Parse a type specificier */
74 static unsigned ParseInitInternal (Type* T, int AllowFlexibleMembers);
75 /* Parse initialization of variables. Return the number of data bytes. */
79 /*****************************************************************************/
80 /* internal functions */
81 /*****************************************************************************/
85 static TypeCode OptionalQualifiers (TypeCode Q)
86 /* Read type qualifiers if we have any */
88 while (TokIsTypeQual (&CurTok)) {
93 if (Q & T_QUAL_CONST) {
94 Error ("Duplicate qualifier: `const'");
100 if (Q & T_QUAL_VOLATILE) {
101 Error ("Duplicate qualifier: `volatile'");
103 Q |= T_QUAL_VOLATILE;
107 if (Q & T_QUAL_RESTRICT) {
108 Error ("Duplicate qualifier: `restrict'");
110 Q |= T_QUAL_RESTRICT;
114 Internal ("Unexpected type qualifier token: %d", CurTok.Tok);
122 /* Return the qualifiers read */
128 static void OptionalInt (void)
129 /* Eat an optional "int" token */
131 if (CurTok.Tok == TOK_INT) {
139 static void OptionalSigned (void)
140 /* Eat an optional "signed" token */
142 if (CurTok.Tok == TOK_SIGNED) {
150 static void InitDeclSpec (DeclSpec* D)
151 /* Initialize the DeclSpec struct for use */
154 D->Type[0].C = T_END;
160 static void InitDeclaration (Declaration* D)
161 /* Initialize the Declaration struct for use */
164 D->Type[0].C = T_END;
170 static void NeedTypeSpace (Declaration* D, unsigned Count)
171 /* Check if there is enough space for Count type specifiers within D */
173 if (D->Index + Count >= MAXTYPELEN) {
174 /* We must call Fatal() here, since calling Error() will try to
175 * continue, and the declaration type is not correctly terminated
176 * in case we come here.
178 Fatal ("Too many type specifiers");
184 static void AddTypeToDeclaration (Declaration* D, TypeCode T)
185 /* Add a type specifier to the type of a declaration */
187 NeedTypeSpace (D, 1);
188 D->Type[D->Index++].C = T;
193 static void AddFuncTypeToDeclaration (Declaration* D, FuncDesc* F)
194 /* Add a function type plus function descriptor to the type of a declaration */
196 NeedTypeSpace (D, 1);
197 D->Type[D->Index].C = T_FUNC;
198 SetFuncDesc (D->Type + D->Index, F);
204 static void AddArrayToDeclaration (Declaration* D, long Size)
205 /* Add an array type plus size to the type of a declaration */
207 NeedTypeSpace (D, 1);
208 D->Type[D->Index].C = T_ARRAY;
209 D->Type[D->Index].A.L = Size;
215 static void FixArrayQualifiers (Type* T)
216 /* Using typedefs, it is possible to generate declarations that have
217 * type qualifiers attached to an array, not the element type. Go and
221 TypeCode Q = T_QUAL_NONE;
222 while (T->C != T_END) {
223 if (IsTypeArray (T)) {
224 /* Extract any type qualifiers */
225 Q |= T->C & T_MASK_QUAL;
226 T->C = UnqualifiedType (T->C);
228 /* Add extracted type qualifiers here */
235 /* Q must be empty now */
236 CHECK (Q == T_QUAL_NONE);
241 static void ParseStorageClass (DeclSpec* D, unsigned DefStorage)
242 /* Parse a storage class */
244 /* Assume we're using an explicit storage class */
245 D->Flags &= ~DS_DEF_STORAGE;
247 /* Check the storage class given */
248 switch (CurTok.Tok) {
251 D->StorageClass = SC_EXTERN | SC_STATIC;
256 D->StorageClass = SC_STATIC;
261 D->StorageClass = SC_REGISTER | SC_STATIC;
266 D->StorageClass = SC_AUTO;
271 D->StorageClass = SC_TYPEDEF;
276 /* No storage class given, use default */
277 D->Flags |= DS_DEF_STORAGE;
278 D->StorageClass = DefStorage;
285 static void ParseEnumDecl (void)
286 /* Process an enum declaration . */
291 /* Accept forward definitions */
292 if (CurTok.Tok != TOK_LCURLY) {
296 /* Skip the opening curly brace */
299 /* Read the enum tags */
301 while (CurTok.Tok != TOK_RCURLY) {
303 /* We expect an identifier */
304 if (CurTok.Tok != TOK_IDENT) {
305 Error ("Identifier expected");
309 /* Remember the identifier and skip it */
310 strcpy (Ident, CurTok.Ident);
313 /* Check for an assigned value */
314 if (CurTok.Tok == TOK_ASSIGN) {
317 ConstAbsIntExpr (hie1, &Expr);
321 /* Add an entry to the symbol table */
322 AddConstSym (Ident, type_int, SC_ENUM, EnumVal++);
324 /* Check for end of definition */
325 if (CurTok.Tok != TOK_COMMA)
334 static SymEntry* ParseStructDecl (const char* Name, TypeCode StructType)
335 /* Parse a struct/union declaration. */
346 if (CurTok.Tok != TOK_LCURLY) {
347 /* Just a forward declaration. Try to find a struct with the given
348 * name. If there is none, insert a forward declaration into the
349 * current lexical level.
351 Entry = FindTagSym (Name);
353 Entry = AddStructSym (Name, 0, 0);
354 } else if (SymIsLocal (Entry) && (Entry->Flags & SC_STRUCT) == 0) {
355 /* Already defined in the level but no struct */
356 Error ("Symbol `%s' is already different kind", Name);
361 /* Add a forward declaration for the struct in the current lexical level */
362 Entry = AddStructSym (Name, 0, 0);
364 /* Skip the curly brace */
367 /* Enter a new lexical level for the struct */
370 /* Parse struct fields */
373 while (CurTok.Tok != TOK_RCURLY) {
375 /* Get the type of the entry */
377 InitDeclSpec (&Spec);
378 ParseTypeSpec (&Spec, -1, T_QUAL_NONE);
380 /* Read fields with this type */
385 /* If we had a flexible array member before, no other fields can
388 if (FlexibleMember) {
389 Error ("Flexible array member must be last field");
390 FlexibleMember = 0; /* Avoid further errors */
393 /* Get type and name of the struct field */
394 ParseDecl (&Spec, &Decl, 0);
396 /* Get the offset of this field */
397 Offs = (StructType == T_STRUCT)? StructSize : 0;
399 /* Calculate the sizes, handle flexible array members */
400 if (StructType == T_STRUCT) {
402 /* It's a struct. Check if this field is a flexible array
403 * member, and calculate the size of the field.
405 if (IsTypeArray (Decl.Type) && GetElementCount (Decl.Type) == UNSPECIFIED) {
406 /* Array with unspecified size */
407 if (StructSize == 0) {
408 Error ("Flexible array member cannot be first struct field");
411 /* Assume zero for size calculations */
412 SetElementCount (Decl.Type, FLEXIBLE);
414 StructSize += CheckedSizeOf (Decl.Type);
420 FieldSize = CheckedSizeOf (Decl.Type);
421 if (FieldSize > StructSize) {
422 StructSize = FieldSize;
426 /* Add a field entry to the table */
427 AddLocalSym (Decl.Ident, Decl.Type, SC_STRUCTFIELD, Offs);
429 if (CurTok.Tok != TOK_COMMA) {
437 /* Skip the closing brace */
440 /* Remember the symbol table and leave the struct level */
441 FieldTab = GetSymTab ();
444 /* Make a real entry from the forward decl and return it */
445 return AddStructSym (Name, StructSize, FieldTab);
450 static void ParseTypeSpec (DeclSpec* D, long Default, TypeCode Qualifiers)
451 /* Parse a type specificier */
457 /* Assume we have an explicit type */
458 D->Flags &= ~DS_DEF_TYPE;
460 /* Read type qualifiers if we have any */
461 Qualifiers = OptionalQualifiers (Qualifiers);
463 /* Look at the data type */
464 switch (CurTok.Tok) {
468 D->Type[0].C = T_VOID;
469 D->Type[1].C = T_END;
474 D->Type[0].C = GetDefaultChar();
475 D->Type[1].C = T_END;
480 if (CurTok.Tok == TOK_UNSIGNED) {
483 D->Type[0].C = T_ULONG;
484 D->Type[1].C = T_END;
488 D->Type[0].C = T_LONG;
489 D->Type[1].C = T_END;
495 if (CurTok.Tok == TOK_UNSIGNED) {
498 D->Type[0].C = T_USHORT;
499 D->Type[1].C = T_END;
503 D->Type[0].C = T_SHORT;
504 D->Type[1].C = T_END;
510 D->Type[0].C = T_INT;
511 D->Type[1].C = T_END;
516 switch (CurTok.Tok) {
520 D->Type[0].C = T_SCHAR;
521 D->Type[1].C = T_END;
527 D->Type[0].C = T_SHORT;
528 D->Type[1].C = T_END;
534 D->Type[0].C = T_LONG;
535 D->Type[1].C = T_END;
543 D->Type[0].C = T_INT;
544 D->Type[1].C = T_END;
551 switch (CurTok.Tok) {
555 D->Type[0].C = T_UCHAR;
556 D->Type[1].C = T_END;
562 D->Type[0].C = T_USHORT;
563 D->Type[1].C = T_END;
569 D->Type[0].C = T_ULONG;
570 D->Type[1].C = T_END;
578 D->Type[0].C = T_UINT;
579 D->Type[1].C = T_END;
586 D->Type[0].C = T_FLOAT;
587 D->Type[1].C = T_END;
592 D->Type[0].C = T_DOUBLE;
593 D->Type[1].C = T_END;
598 StructType = (CurTok.Tok == TOK_STRUCT)? T_STRUCT : T_UNION;
601 if (CurTok.Tok == TOK_IDENT) {
602 strcpy (Ident, CurTok.Ident);
605 AnonName (Ident, (StructType == T_STRUCT)? "struct" : "union");
607 /* Remember we have an extra type decl */
608 D->Flags |= DS_EXTRA_TYPE;
609 /* Declare the struct in the current scope */
610 Entry = ParseStructDecl (Ident, StructType);
611 /* Encode the struct entry into the type */
612 D->Type[0].C = StructType;
613 SetSymEntry (D->Type, Entry);
614 D->Type[1].C = T_END;
619 if (CurTok.Tok != TOK_LCURLY) {
621 if (CurTok.Tok == TOK_IDENT) {
622 /* Find an entry with this name */
623 Entry = FindTagSym (CurTok.Ident);
625 if (SymIsLocal (Entry) && (Entry->Flags & SC_ENUM) == 0) {
626 Error ("Symbol `%s' is already different kind", Entry->Name);
629 /* Insert entry into table ### */
631 /* Skip the identifier */
634 Error ("Identifier expected");
637 /* Remember we have an extra type decl */
638 D->Flags |= DS_EXTRA_TYPE;
639 /* Parse the enum decl */
641 D->Type[0].C = T_INT;
642 D->Type[1].C = T_END;
646 Entry = FindSym (CurTok.Ident);
647 if (Entry && SymIsTypeDef (Entry)) {
650 TypeCpy (D->Type, Entry->Type);
657 Error ("Type expected");
658 D->Type[0].C = T_INT;
659 D->Type[1].C = T_END;
661 D->Flags |= DS_DEF_TYPE;
662 D->Type[0].C = (TypeCode) Default;
663 D->Type[1].C = T_END;
668 /* There may also be qualifiers *after* the initial type */
669 D->Type[0].C |= OptionalQualifiers (Qualifiers);
674 static Type* ParamTypeCvt (Type* T)
675 /* If T is an array, convert it to a pointer else do nothing. Return the
679 if (IsTypeArray (T)) {
687 static void ParseOldStyleParamList (FuncDesc* F)
688 /* Parse an old style (K&R) parameter list */
691 while (CurTok.Tok != TOK_RPAREN) {
693 /* List of identifiers expected */
694 if (CurTok.Tok != TOK_IDENT) {
695 Error ("Identifier expected");
698 /* Create a symbol table entry with type int */
699 AddLocalSym (CurTok.Ident, type_int, SC_AUTO | SC_PARAM | SC_DEF | SC_DEFTYPE, 0);
701 /* Count arguments */
704 /* Skip the identifier */
707 /* Check for more parameters */
708 if (CurTok.Tok == TOK_COMMA) {
715 /* Skip right paren. We must explicitly check for one here, since some of
716 * the breaks above bail out without checking.
720 /* An optional list of type specifications follows */
721 while (CurTok.Tok != TOK_LCURLY) {
725 /* Read the declaration specifier */
726 ParseDeclSpec (&Spec, SC_AUTO, T_INT);
728 /* We accept only auto and register as storage class specifiers, but
729 * we ignore all this, since we use auto anyway.
731 if ((Spec.StorageClass & SC_AUTO) == 0 &&
732 (Spec.StorageClass & SC_REGISTER) == 0) {
733 Error ("Illegal storage class");
736 /* Parse a comma separated variable list */
741 /* Read the parameter */
742 ParseDecl (&Spec, &Decl, DM_NEED_IDENT);
743 if (Decl.Ident[0] != '\0') {
745 /* We have a name given. Search for the symbol */
746 SymEntry* Sym = FindLocalSym (Decl.Ident);
748 /* Check if we already changed the type for this
751 if (Sym->Flags & SC_DEFTYPE) {
752 /* Found it, change the default type to the one given */
753 ChangeSymType (Sym, ParamTypeCvt (Decl.Type));
754 /* Reset the "default type" flag */
755 Sym->Flags &= ~SC_DEFTYPE;
757 /* Type has already been changed */
758 Error ("Redefinition for parameter `%s'", Sym->Name);
761 Error ("Unknown identifier: `%s'", Decl.Ident);
765 if (CurTok.Tok == TOK_COMMA) {
773 /* Variable list must be semicolon terminated */
780 static void ParseAnsiParamList (FuncDesc* F)
781 /* Parse a new style (ANSI) parameter list */
784 while (CurTok.Tok != TOK_RPAREN) {
790 /* Allow an ellipsis as last parameter */
791 if (CurTok.Tok == TOK_ELLIPSIS) {
793 F->Flags |= FD_VARIADIC;
797 /* Read the declaration specifier */
798 ParseDeclSpec (&Spec, SC_AUTO, T_INT);
800 /* We accept only auto and register as storage class specifiers */
801 if ((Spec.StorageClass & SC_AUTO) == SC_AUTO) {
802 Spec.StorageClass = SC_AUTO | SC_PARAM | SC_DEF;
803 } else if ((Spec.StorageClass & SC_REGISTER) == SC_REGISTER) {
804 Spec.StorageClass = SC_REGISTER | SC_STATIC | SC_PARAM | SC_DEF;
806 Error ("Illegal storage class");
807 Spec.StorageClass = SC_AUTO | SC_PARAM | SC_DEF;
810 /* Allow parameters without a name, but remember if we had some to
811 * eventually print an error message later.
813 ParseDecl (&Spec, &Decl, DM_ACCEPT_IDENT);
814 if (Decl.Ident[0] == '\0') {
816 /* Unnamed symbol. Generate a name that is not user accessible,
817 * then handle the symbol normal.
819 AnonName (Decl.Ident, "param");
820 F->Flags |= FD_UNNAMED_PARAMS;
822 /* Clear defined bit on nonames */
823 Decl.StorageClass &= ~SC_DEF;
826 /* Parse an attribute ### */
827 ParseAttribute (&Decl, &Attr);
829 /* Create a symbol table entry */
830 AddLocalSym (Decl.Ident, ParamTypeCvt (Decl.Type), Decl.StorageClass, 0);
832 /* Count arguments */
835 /* Check for more parameters */
836 if (CurTok.Tok == TOK_COMMA) {
843 /* Skip right paren. We must explicitly check for one here, since some of
844 * the breaks above bail out without checking.
848 /* Check if this is a function definition */
849 if (CurTok.Tok == TOK_LCURLY) {
850 /* Print an error if we have unnamed parameters and cc65 extensions
853 if (IS_Get (&Standard) != STD_CC65 &&
854 (F->Flags & FD_UNNAMED_PARAMS) != 0) {
855 Error ("Parameter name omitted");
862 static FuncDesc* ParseFuncDecl (void)
863 /* Parse the argument list of a function. */
868 /* Create a new function descriptor */
869 FuncDesc* F = NewFuncDesc ();
871 /* Enter a new lexical level */
872 EnterFunctionLevel ();
874 /* Check for several special parameter lists */
875 if (CurTok.Tok == TOK_RPAREN) {
876 /* Parameter list is empty */
877 F->Flags |= (FD_EMPTY | FD_VARIADIC);
878 } else if (CurTok.Tok == TOK_VOID && NextTok.Tok == TOK_RPAREN) {
879 /* Parameter list declared as void */
881 F->Flags |= FD_VOID_PARAM;
882 } else if (CurTok.Tok == TOK_IDENT &&
883 (NextTok.Tok == TOK_COMMA || NextTok.Tok == TOK_RPAREN)) {
884 /* If the identifier is a typedef, we have a new style parameter list,
885 * if it's some other identifier, it's an old style parameter list.
887 Sym = FindSym (CurTok.Ident);
888 if (Sym == 0 || !SymIsTypeDef (Sym)) {
889 /* Old style (K&R) function. */
890 F->Flags |= FD_OLDSTYLE;
895 if ((F->Flags & FD_OLDSTYLE) == 0) {
896 /* New style function */
897 ParseAnsiParamList (F);
899 /* Old style function */
900 ParseOldStyleParamList (F);
903 /* Remember the last function parameter. We need it later for several
904 * purposes, for example when passing stuff to fastcall functions. Since
905 * more symbols are added to the table, it is easier if we remember it
906 * now, since it is currently the last entry in the symbol table.
908 F->LastParam = GetSymTab()->SymTail;
910 /* Assign offsets. If the function has a variable parameter list,
911 * there's one additional byte (the arg size).
913 Offs = (F->Flags & FD_VARIADIC)? 1 : 0;
916 unsigned Size = CheckedSizeOf (Sym->Type);
917 if (SymIsRegVar (Sym)) {
918 Sym->V.R.SaveOffs = Offs;
923 F->ParamSize += Size;
927 /* Add the default address size for the function */
928 if (CodeAddrSize == ADDR_SIZE_FAR) {
934 /* Leave the lexical level remembering the symbol tables */
935 RememberFunctionLevel (F);
937 /* Return the function descriptor */
943 static unsigned FunctionModifierFlags (void)
944 /* Parse __fastcall__, __near__ and __far__ and return the matching FD_ flags */
947 unsigned Flags = FD_NONE;
948 while (CurTok.Tok == TOK_FASTCALL || CurTok.Tok == TOK_NEAR || CurTok.Tok == TOK_FAR) {
950 /* Get the flag bit for the next token */
951 unsigned F = FD_NONE;
952 switch (CurTok.Tok) {
953 case TOK_FASTCALL: F = FD_FASTCALL; break;
954 case TOK_NEAR: F = FD_NEAR; break;
955 case TOK_FAR: F = FD_FAR; break;
956 default: Internal ("Unexpected token: %d", CurTok.Tok);
959 /* Remember the flag for this modifier */
961 Error ("Duplicate modifier");
970 if ((Flags & (FD_NEAR | FD_FAR)) == (FD_NEAR | FD_FAR)) {
971 Error ("Cannot specify both, `__near__' and `__far__' modifiers");
972 Flags &= ~(FD_NEAR | FD_FAR);
975 /* Return the flags read */
981 static void ApplyFunctionModifiers (Type* T, unsigned Flags)
982 /* Apply a set of function modifier flags to a function */
984 /* Get the function descriptor */
985 FuncDesc* F = GetFuncDesc (T);
987 /* Special check for __fastcall__ */
988 if ((Flags & FD_FASTCALL) != 0 && IsVariadicFunc (T)) {
989 Error ("Cannot apply `__fastcall__' to functions with "
990 "variable parameter list");
991 Flags &= ~FD_FASTCALL;
994 /* Remove the default function address size modifiers */
995 F->Flags &= ~(FD_NEAR | FD_FAR);
997 /* Add the new modifers */
1003 static void Decl (const DeclSpec* Spec, Declaration* D, unsigned Mode)
1004 /* Recursively process declarators. Build a type array in reverse order. */
1006 /* Pointer to something */
1007 if (CurTok.Tok == TOK_STAR) {
1014 /* Allow optional const or volatile qualifiers */
1015 C = T_PTR | OptionalQualifiers (T_QUAL_NONE);
1017 /* Parse the type, the pointer points to */
1018 Decl (Spec, D, Mode);
1021 AddTypeToDeclaration (D, C);
1025 /* Function modifiers */
1026 if (CurTok.Tok == TOK_FASTCALL || CurTok.Tok == TOK_NEAR || CurTok.Tok == TOK_FAR) {
1028 /* Remember the current type pointer */
1029 Type* T = D->Type + D->Index;
1031 /* Read the flags */
1032 unsigned Flags = FunctionModifierFlags ();
1034 /* Parse the function */
1035 Decl (Spec, D, Mode);
1037 /* Check that we have a function */
1038 if (!IsTypeFunc (T) && !IsTypeFuncPtr (T)) {
1039 Error ("Function modifier applied to non function");
1041 ApplyFunctionModifiers (T, Flags);
1048 if (CurTok.Tok == TOK_LPAREN) {
1050 Decl (Spec, D, Mode);
1053 /* Things depend on Mode now:
1054 * - Mode == DM_NEED_IDENT means:
1055 * we *must* have a type and a variable identifer.
1056 * - Mode == DM_NO_IDENT means:
1057 * we must have a type but no variable identifer
1058 * (if there is one, it's not read).
1059 * - Mode == DM_ACCEPT_IDENT means:
1060 * we *may* have an identifier. If there is an identifier,
1061 * it is read, but it is no error, if there is none.
1063 if (Mode == DM_NO_IDENT) {
1065 } else if (CurTok.Tok == TOK_IDENT) {
1066 strcpy (D->Ident, CurTok.Ident);
1069 if (Mode == DM_NEED_IDENT) {
1070 Error ("Identifier expected");
1076 while (CurTok.Tok == TOK_LBRACK || CurTok.Tok == TOK_LPAREN) {
1077 if (CurTok.Tok == TOK_LPAREN) {
1079 /* Function declaration */
1083 /* Parse the function declaration */
1084 F = ParseFuncDecl ();
1086 /* Add the function type. Be sure to bounds check the type buffer */
1087 AddFuncTypeToDeclaration (D, F);
1089 /* Array declaration */
1090 long Size = UNSPECIFIED;
1092 /* Read the size if it is given */
1093 if (CurTok.Tok != TOK_RBRACK) {
1095 ConstAbsIntExpr (hie1, &Expr);
1096 if (Expr.IVal <= 0) {
1097 if (D->Ident[0] != '\0') {
1098 Error ("Size of array `%s' is invalid", D->Ident);
1100 Error ("Size of array is invalid");
1108 /* Add the array type with the size */
1109 AddArrayToDeclaration (D, Size);
1116 /*****************************************************************************/
1118 /*****************************************************************************/
1122 Type* ParseType (Type* T)
1123 /* Parse a complete type specification */
1128 /* Get a type without a default */
1129 InitDeclSpec (&Spec);
1130 ParseTypeSpec (&Spec, -1, T_QUAL_NONE);
1132 /* Parse additional declarators */
1133 ParseDecl (&Spec, &Decl, DM_NO_IDENT);
1135 /* Copy the type to the target buffer */
1136 TypeCpy (T, Decl.Type);
1138 /* Return a pointer to the target buffer */
1144 void ParseDecl (const DeclSpec* Spec, Declaration* D, unsigned Mode)
1145 /* Parse a variable, type or function declaration */
1147 /* Initialize the Declaration struct */
1148 InitDeclaration (D);
1150 /* Get additional declarators and the identifier */
1151 Decl (Spec, D, Mode);
1153 /* Add the base type. */
1154 NeedTypeSpace (D, TypeLen (Spec->Type) + 1); /* Bounds check */
1155 TypeCpy (D->Type + D->Index, Spec->Type);
1157 /* Use the storage class from the declspec */
1158 D->StorageClass = Spec->StorageClass;
1160 /* Fix any type qualifiers attached to an array type */
1161 FixArrayQualifiers (D->Type);
1163 /* If we have a function, add a special storage class */
1164 if (IsTypeFunc (D->Type)) {
1165 D->StorageClass |= SC_FUNC;
1168 /* Check several things for function or function pointer types */
1169 if (IsTypeFunc (D->Type) || IsTypeFuncPtr (D->Type)) {
1171 /* A function. Check the return type */
1172 Type* RetType = GetFuncReturn (D->Type);
1174 /* Functions may not return functions or arrays */
1175 if (IsTypeFunc (RetType)) {
1176 Error ("Functions are not allowed to return functions");
1177 } else if (IsTypeArray (RetType)) {
1178 Error ("Functions are not allowed to return arrays");
1181 /* The return type must not be qualified */
1182 if (GetQualifier (RetType) != T_QUAL_NONE && RetType[1].C == T_END) {
1184 if (GetType (RetType) == T_TYPE_VOID) {
1185 /* A qualified void type is always an error */
1186 Error ("function definition has qualified void return type");
1188 /* For others, qualifiers are ignored */
1189 Warning ("type qualifiers ignored on function return type");
1190 RetType[0].C = UnqualifiedType (RetType[0].C);
1194 /* Warn about an implicit int return in the function */
1195 if ((Spec->Flags & DS_DEF_TYPE) != 0 &&
1196 RetType[0].C == T_INT && RetType[1].C == T_END) {
1197 /* Function has an implicit int return. Output a warning if we don't
1198 * have the C89 standard enabled explicitly.
1200 if (IS_Get (&Standard) >= STD_C99) {
1201 Warning ("Implicit `int' return type is an obsolete feature");
1203 GetFuncDesc (D->Type)->Flags |= FD_OLDSTYLE_INTRET;
1208 /* For anthing that is not a function or typedef, check for an implicit
1211 if ((D->StorageClass & SC_FUNC) != SC_FUNC &&
1212 (D->StorageClass & SC_TYPEDEF) != SC_TYPEDEF) {
1213 /* If the standard was not set explicitly to C89, print a warning
1214 * for variables with implicit int type.
1216 if ((Spec->Flags & DS_DEF_TYPE) != 0 && IS_Get (&Standard) >= STD_C99) {
1217 Warning ("Implicit `int' is an obsolete feature");
1221 /* Check the size of the generated type */
1222 if (!IsTypeFunc (D->Type) && !IsTypeVoid (D->Type)) {
1223 unsigned Size = SizeOf (D->Type);
1224 if (Size >= 0x10000) {
1225 if (D->Ident[0] != '\0') {
1226 Error ("Size of `%s' is invalid (0x%06X)", D->Ident, Size);
1228 Error ("Invalid size in declaration (0x%06X)", Size);
1237 void ParseDeclSpec (DeclSpec* D, unsigned DefStorage, long DefType)
1238 /* Parse a declaration specification */
1240 TypeCode Qualifiers;
1242 /* Initialize the DeclSpec struct */
1245 /* There may be qualifiers *before* the storage class specifier */
1246 Qualifiers = OptionalQualifiers (T_QUAL_NONE);
1248 /* Now get the storage class specifier for this declaration */
1249 ParseStorageClass (D, DefStorage);
1251 /* Parse the type specifiers passing any initial type qualifiers */
1252 ParseTypeSpec (D, DefType, Qualifiers);
1257 void CheckEmptyDecl (const DeclSpec* D)
1258 /* Called after an empty type declaration (that is, a type declaration without
1259 * a variable). Checks if the declaration does really make sense and issues a
1263 if ((D->Flags & DS_EXTRA_TYPE) == 0) {
1264 Warning ("Useless declaration");
1270 static void SkipInitializer (unsigned BracesExpected)
1271 /* Skip the remainder of an initializer in case of errors. Try to be somewhat
1272 * smart so we don't have too many following errors.
1275 while (CurTok.Tok != TOK_CEOF && CurTok.Tok != TOK_SEMI && BracesExpected > 0) {
1276 switch (CurTok.Tok) {
1277 case TOK_RCURLY: --BracesExpected; break;
1278 case TOK_LCURLY: ++BracesExpected; break;
1287 static unsigned OpeningCurlyBraces (unsigned BracesNeeded)
1288 /* Accept any number of opening curly braces around an initialization, skip
1289 * them and return the number. If the number of curly braces is less than
1290 * BracesNeeded, issue a warning.
1293 unsigned BraceCount = 0;
1294 while (CurTok.Tok == TOK_LCURLY) {
1298 if (BraceCount < BracesNeeded) {
1299 Error ("`{' expected");
1306 static void ClosingCurlyBraces (unsigned BracesExpected)
1307 /* Accept and skip the given number of closing curly braces together with
1308 * an optional comma. Output an error messages, if the input does not contain
1309 * the expected number of braces.
1312 while (BracesExpected) {
1313 if (CurTok.Tok == TOK_RCURLY) {
1315 } else if (CurTok.Tok == TOK_COMMA && NextTok.Tok == TOK_RCURLY) {
1319 Error ("`}' expected");
1328 static void DefineData (ExprDesc* Expr)
1329 /* Output a data definition for the given expression */
1331 switch (ED_GetLoc (Expr)) {
1334 /* Absolute: numeric address or const */
1335 g_defdata (TypeOf (Expr->Type) | CF_CONST, Expr->IVal, 0);
1339 /* Global variable */
1340 g_defdata (CF_EXTERNAL, Expr->Name, Expr->IVal);
1345 /* Static variable or literal in the literal pool */
1346 g_defdata (CF_STATIC, Expr->Name, Expr->IVal);
1349 case E_LOC_REGISTER:
1350 /* Register variable. Taking the address is usually not
1353 if (IS_Get (&AllowRegVarAddr) == 0) {
1354 Error ("Cannot take the address of a register variable");
1356 g_defdata (CF_REGVAR, Expr->Name, Expr->IVal);
1362 Error ("Non constant initializer");
1366 Internal ("Unknown constant type: 0x%04X", ED_GetLoc (Expr));
1372 static unsigned ParseScalarInit (Type* T)
1373 /* Parse initializaton for scalar data types. Return the number of data bytes. */
1377 /* Optional opening brace */
1378 unsigned BraceCount = OpeningCurlyBraces (0);
1380 /* We warn if an initializer for a scalar contains braces, because this is
1381 * quite unusual and often a sign for some problem in the input.
1383 if (BraceCount > 0) {
1384 Warning ("Braces around scalar initializer");
1387 /* Get the expression and convert it to the target type */
1388 ConstExpr (hie1, &ED);
1389 TypeConversion (&ED, T);
1391 /* Output the data */
1394 /* Close eventually opening braces */
1395 ClosingCurlyBraces (BraceCount);
1403 static unsigned ParsePointerInit (Type* T)
1404 /* Parse initializaton for pointer data types. Return the number of data bytes. */
1406 /* Optional opening brace */
1407 unsigned BraceCount = OpeningCurlyBraces (0);
1411 ConstExpr (hie1, &ED);
1412 TypeConversion (&ED, T);
1414 /* Output the data */
1417 /* Close eventually opening braces */
1418 ClosingCurlyBraces (BraceCount);
1426 static unsigned ParseArrayInit (Type* T, int AllowFlexibleMembers)
1427 /* Parse initializaton for arrays. Return the number of data bytes. */
1431 /* Get the array data */
1432 Type* ElementType = GetElementType (T);
1433 unsigned ElementSize = CheckedSizeOf (ElementType);
1434 long ElementCount = GetElementCount (T);
1436 /* Special handling for a character array initialized by a literal */
1437 if (IsTypeChar (ElementType) &&
1438 (CurTok.Tok == TOK_SCONST ||
1439 (CurTok.Tok == TOK_LCURLY && NextTok.Tok == TOK_SCONST))) {
1441 /* Char array initialized by string constant */
1445 /* If we initializer is enclosed in brackets, remember this fact and
1446 * skip the opening bracket.
1448 NeedParen = (CurTok.Tok == TOK_LCURLY);
1453 /* Get the initializer string and its size */
1454 Str = GetLiteral (CurTok.IVal);
1455 Count = GetLiteralPoolOffs () - CurTok.IVal;
1457 /* Translate into target charset */
1458 TranslateLiteralPool (CurTok.IVal);
1460 /* If the array is one too small for the string literal, omit the
1463 if (ElementCount != UNSPECIFIED &&
1464 ElementCount != FLEXIBLE &&
1465 Count == ElementCount + 1) {
1466 /* Omit the trailing zero */
1470 /* Output the data */
1471 g_defbytes (Str, Count);
1473 /* Remove string from pool */
1474 ResetLiteralPoolOffs (CurTok.IVal);
1477 /* If the initializer was enclosed in curly braces, we need a closing
1489 /* Initialize the array members */
1491 while (CurTok.Tok != TOK_RCURLY) {
1492 /* Flexible array members may not be initialized within
1493 * an array (because the size of each element may differ
1496 ParseInitInternal (ElementType, 0);
1498 if (CurTok.Tok != TOK_COMMA)
1503 /* Closing curly braces */
1507 if (ElementCount == UNSPECIFIED) {
1508 /* Number of elements determined by initializer */
1509 SetElementCount (T, Count);
1510 ElementCount = Count;
1511 } else if (ElementCount == FLEXIBLE && AllowFlexibleMembers) {
1512 /* In non ANSI mode, allow initialization of flexible array
1515 ElementCount = Count;
1516 } else if (Count < ElementCount) {
1517 g_zerobytes ((ElementCount - Count) * ElementSize);
1518 } else if (Count > ElementCount) {
1519 Error ("Too many initializers");
1521 return ElementCount * ElementSize;
1526 static unsigned ParseStructInit (Type* T, int AllowFlexibleMembers)
1527 /* Parse initialization of a struct or union. Return the number of data bytes. */
1531 unsigned StructSize;
1535 /* Consume the opening curly brace */
1538 /* Get a pointer to the struct entry from the type */
1539 Entry = GetSymEntry (T);
1541 /* Get the size of the struct from the symbol table entry */
1542 StructSize = Entry->V.S.Size;
1544 /* Check if this struct definition has a field table. If it doesn't, it
1545 * is an incomplete definition.
1547 Tab = Entry->V.S.SymTab;
1549 Error ("Cannot initialize variables with incomplete type");
1550 /* Try error recovery */
1551 SkipInitializer (1);
1552 /* Nothing initialized */
1556 /* Get a pointer to the list of symbols */
1557 Entry = Tab->SymHead;
1559 /* Initialize fields */
1561 while (CurTok.Tok != TOK_RCURLY) {
1563 Error ("Too many initializers");
1564 SkipInitializer (1);
1567 /* Parse initialization of one field. Flexible array members may
1568 * only be initialized if they are the last field (or part of the
1569 * last struct field).
1571 Size += ParseInitInternal (Entry->Type, AllowFlexibleMembers && Entry->NextSym == 0);
1572 Entry = Entry->NextSym;
1573 if (CurTok.Tok != TOK_COMMA)
1578 /* Consume the closing curly brace */
1581 /* If there are struct fields left, reserve additional storage */
1582 if (Size < StructSize) {
1583 g_zerobytes (StructSize - Size);
1587 /* Return the actual number of bytes initialized. This number may be
1588 * larger than StructSize if flexible array members are present and were
1589 * initialized (possible in non ANSI mode).
1596 static unsigned ParseVoidInit (void)
1597 /* Parse an initialization of a void variable (special cc65 extension).
1598 * Return the number of bytes initialized.
1607 /* Allow an arbitrary list of values */
1610 ConstExpr (hie1, &Expr);
1611 switch (UnqualifiedType (Expr.Type[0].C)) {
1615 if (ED_IsConstAbsInt (&Expr)) {
1616 /* Make it byte sized */
1620 Size += SIZEOF_CHAR;
1629 if (ED_IsConstAbsInt (&Expr)) {
1630 /* Make it word sized */
1631 Expr.IVal &= 0xFFFF;
1639 if (ED_IsConstAbsInt (&Expr)) {
1640 /* Make it dword sized */
1641 Expr.IVal &= 0xFFFFFFFF;
1644 Size += SIZEOF_LONG;
1648 Error ("Illegal type in initialization");
1653 if (CurTok.Tok != TOK_COMMA) {
1658 } while (CurTok.Tok != TOK_RCURLY);
1663 /* Return the number of bytes initialized */
1669 static unsigned ParseInitInternal (Type* T, int AllowFlexibleMembers)
1670 /* Parse initialization of variables. Return the number of data bytes. */
1672 switch (UnqualifiedType (T->C)) {
1684 return ParseScalarInit (T);
1687 return ParsePointerInit (T);
1690 return ParseArrayInit (T, AllowFlexibleMembers);
1694 return ParseStructInit (T, AllowFlexibleMembers);
1697 if (IS_Get (&Standard) == STD_CC65) {
1698 /* Special cc65 extension in non ANSI mode */
1699 return ParseVoidInit ();
1704 Error ("Illegal type");
1712 unsigned ParseInit (Type* T)
1713 /* Parse initialization of variables. Return the number of data bytes. */
1715 /* Parse the initialization. Flexible array members can only be initialized
1718 unsigned Size = ParseInitInternal (T, IS_Get (&Standard) == STD_CC65);
1720 /* The initialization may not generate code on global level, because code
1721 * outside function scope will never get executed.
1723 if (HaveGlobalCode ()) {
1724 Error ("Non constant initializers");
1725 RemoveGlobalCode ();
1728 /* Return the size needed for the initialization */