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 void DuplicateQualifier (const char* Name)
86 /* Print an error message */
88 Warning ("Duplicate qualifier: `%s'", Name);
93 static TypeCode OptionalQualifiers (TypeCode Allowed)
94 /* Read type qualifiers if we have any. Allowed specifies the allowed
98 /* We start without any qualifiers */
99 TypeCode Q = T_QUAL_NONE;
101 /* Check for more qualifiers */
104 switch (CurTok.Tok) {
107 if (Allowed & T_QUAL_CONST) {
108 if (Q & T_QUAL_CONST) {
109 DuplicateQualifier ("const");
118 if (Allowed & T_QUAL_VOLATILE) {
119 if (Q & T_QUAL_VOLATILE) {
120 DuplicateQualifier ("volatile");
122 Q |= T_QUAL_VOLATILE;
129 if (Allowed & T_QUAL_RESTRICT) {
130 if (Q & T_QUAL_RESTRICT) {
131 DuplicateQualifier ("restrict");
133 Q |= T_QUAL_RESTRICT;
140 if (Allowed & T_QUAL_NEAR) {
141 if (Q & T_QUAL_NEAR) {
142 DuplicateQualifier ("near");
151 if (Allowed & T_QUAL_FAR) {
152 if (Q & T_QUAL_FAR) {
153 DuplicateQualifier ("far");
162 if (Allowed & T_QUAL_FASTCALL) {
163 if (Q & T_QUAL_FASTCALL) {
164 DuplicateQualifier ("fastcall");
166 Q |= T_QUAL_FASTCALL;
182 /* We cannot have more than one address size far qualifier */
183 switch (Q & T_QUAL_ADDRSIZE) {
191 Error ("Cannot specify more than one address size qualifier");
192 Q &= ~T_QUAL_ADDRSIZE;
195 /* Return the qualifiers read */
201 static void OptionalInt (void)
202 /* Eat an optional "int" token */
204 if (CurTok.Tok == TOK_INT) {
212 static void OptionalSigned (void)
213 /* Eat an optional "signed" token */
215 if (CurTok.Tok == TOK_SIGNED) {
223 static void InitDeclSpec (DeclSpec* D)
224 /* Initialize the DeclSpec struct for use */
227 D->Type[0].C = T_END;
233 static void InitDeclaration (Declaration* D)
234 /* Initialize the Declaration struct for use */
237 D->Type[0].C = T_END;
243 static void NeedTypeSpace (Declaration* D, unsigned Count)
244 /* Check if there is enough space for Count type specifiers within D */
246 if (D->Index + Count >= MAXTYPELEN) {
247 /* We must call Fatal() here, since calling Error() will try to
248 * continue, and the declaration type is not correctly terminated
249 * in case we come here.
251 Fatal ("Too many type specifiers");
257 static void AddTypeToDeclaration (Declaration* D, TypeCode T)
258 /* Add a type specifier to the type of a declaration */
260 NeedTypeSpace (D, 1);
261 D->Type[D->Index++].C = T;
266 static void FixQualifiers (Type* DataType)
267 /* Apply several fixes to qualifiers */
272 /* Using typedefs, it is possible to generate declarations that have
273 * type qualifiers attached to an array, not the element type. Go and
278 while (T->C != T_END) {
279 if (IsTypeArray (T)) {
280 /* Extract any type qualifiers */
281 Q |= T->C & T_MASK_QUAL;
282 T->C = UnqualifiedType (T->C);
284 /* Add extracted type qualifiers here */
290 /* Q must be empty now */
291 CHECK (Q == T_QUAL_NONE);
293 /* Do some fixes on pointers and functions. */
295 while (T->C != T_END) {
298 /* Fastcall qualifier on the pointer? */
299 if (IsQualFastcall (T)) {
300 /* Pointer to function which is not fastcall? */
301 if (IsTypeFunc (T+1) && !IsQualFastcall (T+1)) {
302 /* Move the fastcall qualifier from the pointer to
305 T[0].C &= ~T_QUAL_FASTCALL;
306 T[1].C |= T_QUAL_FASTCALL;
308 Error ("Invalid `_fastcall__' qualifier for pointer");
312 /* Apply the default far and near qualifiers if none are given */
313 Q = (T[0].C & T_QUAL_ADDRSIZE);
314 if (Q == T_QUAL_NONE) {
315 /* No address size qualifiers specified */
316 if (IsTypeFunc (T+1)) {
317 /* Pointer to function. Use the qualifier from the function
318 * or the default if the function don't has one.
320 Q = (T[1].C & T_QUAL_ADDRSIZE);
321 if (Q == T_QUAL_NONE) {
322 Q = CodeAddrSizeQualifier ();
325 Q = DataAddrSizeQualifier ();
329 /* We have address size qualifiers. If followed by a function,
330 * apply these also to the function.
332 if (IsTypeFunc (T+1)) {
333 TypeCode FQ = (T[1].C & T_QUAL_ADDRSIZE);
334 if (FQ == T_QUAL_NONE) {
336 } else if (FQ != Q) {
337 Error ("Address size qualifier mismatch");
338 T[1].C = (T[1].C & ~T_QUAL_ADDRSIZE) | Q;
343 } else if (IsTypeFunc (T)) {
345 /* Apply the default far and near qualifiers if none are given */
346 if ((T[0].C & T_QUAL_ADDRSIZE) == 0) {
347 T[0].C |= CodeAddrSizeQualifier ();
357 static void ParseStorageClass (DeclSpec* D, unsigned DefStorage)
358 /* Parse a storage class */
360 /* Assume we're using an explicit storage class */
361 D->Flags &= ~DS_DEF_STORAGE;
363 /* Check the storage class given */
364 switch (CurTok.Tok) {
367 D->StorageClass = SC_EXTERN | SC_STATIC;
372 D->StorageClass = SC_STATIC;
377 D->StorageClass = SC_REGISTER | SC_STATIC;
382 D->StorageClass = SC_AUTO;
387 D->StorageClass = SC_TYPEDEF;
392 /* No storage class given, use default */
393 D->Flags |= DS_DEF_STORAGE;
394 D->StorageClass = DefStorage;
401 static void ParseEnumDecl (void)
402 /* Process an enum declaration . */
407 /* Accept forward definitions */
408 if (CurTok.Tok != TOK_LCURLY) {
412 /* Skip the opening curly brace */
415 /* Read the enum tags */
417 while (CurTok.Tok != TOK_RCURLY) {
419 /* We expect an identifier */
420 if (CurTok.Tok != TOK_IDENT) {
421 Error ("Identifier expected");
425 /* Remember the identifier and skip it */
426 strcpy (Ident, CurTok.Ident);
429 /* Check for an assigned value */
430 if (CurTok.Tok == TOK_ASSIGN) {
433 ConstAbsIntExpr (hie1, &Expr);
437 /* Add an entry to the symbol table */
438 AddConstSym (Ident, type_int, SC_ENUM, EnumVal++);
440 /* Check for end of definition */
441 if (CurTok.Tok != TOK_COMMA)
450 static SymEntry* ParseStructDecl (const char* Name, TypeCode StructType)
451 /* Parse a struct/union declaration. */
462 if (CurTok.Tok != TOK_LCURLY) {
463 /* Just a forward declaration. Try to find a struct with the given
464 * name. If there is none, insert a forward declaration into the
465 * current lexical level.
467 Entry = FindTagSym (Name);
469 Entry = AddStructSym (Name, 0, 0);
470 } else if (SymIsLocal (Entry) && (Entry->Flags & SC_STRUCT) == 0) {
471 /* Already defined in the level but no struct */
472 Error ("Symbol `%s' is already different kind", Name);
477 /* Add a forward declaration for the struct in the current lexical level */
478 Entry = AddStructSym (Name, 0, 0);
480 /* Skip the curly brace */
483 /* Enter a new lexical level for the struct */
486 /* Parse struct fields */
489 while (CurTok.Tok != TOK_RCURLY) {
491 /* Get the type of the entry */
493 InitDeclSpec (&Spec);
494 ParseTypeSpec (&Spec, -1, T_QUAL_NONE);
496 /* Read fields with this type */
501 /* If we had a flexible array member before, no other fields can
504 if (FlexibleMember) {
505 Error ("Flexible array member must be last field");
506 FlexibleMember = 0; /* Avoid further errors */
509 /* Get type and name of the struct field */
510 ParseDecl (&Spec, &Decl, 0);
512 /* Get the offset of this field */
513 Offs = (StructType == T_STRUCT)? StructSize : 0;
515 /* Calculate the sizes, handle flexible array members */
516 if (StructType == T_STRUCT) {
518 /* It's a struct. Check if this field is a flexible array
519 * member, and calculate the size of the field.
521 if (IsTypeArray (Decl.Type) && GetElementCount (Decl.Type) == UNSPECIFIED) {
522 /* Array with unspecified size */
523 if (StructSize == 0) {
524 Error ("Flexible array member cannot be first struct field");
527 /* Assume zero for size calculations */
528 SetElementCount (Decl.Type, FLEXIBLE);
530 StructSize += CheckedSizeOf (Decl.Type);
536 FieldSize = CheckedSizeOf (Decl.Type);
537 if (FieldSize > StructSize) {
538 StructSize = FieldSize;
542 /* Add a field entry to the table */
543 AddLocalSym (Decl.Ident, Decl.Type, SC_STRUCTFIELD, Offs);
545 if (CurTok.Tok != TOK_COMMA) {
553 /* Skip the closing brace */
556 /* Remember the symbol table and leave the struct level */
557 FieldTab = GetSymTab ();
560 /* Make a real entry from the forward decl and return it */
561 return AddStructSym (Name, StructSize, FieldTab);
566 static void ParseTypeSpec (DeclSpec* D, long Default, TypeCode Qualifiers)
567 /* Parse a type specificier */
573 /* Assume we have an explicit type */
574 D->Flags &= ~DS_DEF_TYPE;
576 /* Read type qualifiers if we have any */
577 Qualifiers |= OptionalQualifiers (T_QUAL_CONST | T_QUAL_VOLATILE);
579 /* Look at the data type */
580 switch (CurTok.Tok) {
584 D->Type[0].C = T_VOID;
585 D->Type[1].C = T_END;
590 D->Type[0].C = GetDefaultChar();
591 D->Type[1].C = T_END;
596 if (CurTok.Tok == TOK_UNSIGNED) {
599 D->Type[0].C = T_ULONG;
600 D->Type[1].C = T_END;
604 D->Type[0].C = T_LONG;
605 D->Type[1].C = T_END;
611 if (CurTok.Tok == TOK_UNSIGNED) {
614 D->Type[0].C = T_USHORT;
615 D->Type[1].C = T_END;
619 D->Type[0].C = T_SHORT;
620 D->Type[1].C = T_END;
626 D->Type[0].C = T_INT;
627 D->Type[1].C = T_END;
632 switch (CurTok.Tok) {
636 D->Type[0].C = T_SCHAR;
637 D->Type[1].C = T_END;
643 D->Type[0].C = T_SHORT;
644 D->Type[1].C = T_END;
650 D->Type[0].C = T_LONG;
651 D->Type[1].C = T_END;
659 D->Type[0].C = T_INT;
660 D->Type[1].C = T_END;
667 switch (CurTok.Tok) {
671 D->Type[0].C = T_UCHAR;
672 D->Type[1].C = T_END;
678 D->Type[0].C = T_USHORT;
679 D->Type[1].C = T_END;
685 D->Type[0].C = T_ULONG;
686 D->Type[1].C = T_END;
694 D->Type[0].C = T_UINT;
695 D->Type[1].C = T_END;
702 D->Type[0].C = T_FLOAT;
703 D->Type[1].C = T_END;
708 D->Type[0].C = T_DOUBLE;
709 D->Type[1].C = T_END;
714 StructType = (CurTok.Tok == TOK_STRUCT)? T_STRUCT : T_UNION;
717 if (CurTok.Tok == TOK_IDENT) {
718 strcpy (Ident, CurTok.Ident);
721 AnonName (Ident, (StructType == T_STRUCT)? "struct" : "union");
723 /* Remember we have an extra type decl */
724 D->Flags |= DS_EXTRA_TYPE;
725 /* Declare the struct in the current scope */
726 Entry = ParseStructDecl (Ident, StructType);
727 /* Encode the struct entry into the type */
728 D->Type[0].C = StructType;
729 SetSymEntry (D->Type, Entry);
730 D->Type[1].C = T_END;
735 if (CurTok.Tok != TOK_LCURLY) {
737 if (CurTok.Tok == TOK_IDENT) {
738 /* Find an entry with this name */
739 Entry = FindTagSym (CurTok.Ident);
741 if (SymIsLocal (Entry) && (Entry->Flags & SC_ENUM) == 0) {
742 Error ("Symbol `%s' is already different kind", Entry->Name);
745 /* Insert entry into table ### */
747 /* Skip the identifier */
750 Error ("Identifier expected");
753 /* Remember we have an extra type decl */
754 D->Flags |= DS_EXTRA_TYPE;
755 /* Parse the enum decl */
757 D->Type[0].C = T_INT;
758 D->Type[1].C = T_END;
762 Entry = FindSym (CurTok.Ident);
763 if (Entry && SymIsTypeDef (Entry)) {
766 TypeCopy (D->Type, Entry->Type);
773 Error ("Type expected");
774 D->Type[0].C = T_INT;
775 D->Type[1].C = T_END;
777 D->Flags |= DS_DEF_TYPE;
778 D->Type[0].C = (TypeCode) Default;
779 D->Type[1].C = T_END;
784 /* There may also be qualifiers *after* the initial type */
785 D->Type[0].C |= (Qualifiers | OptionalQualifiers (T_QUAL_CONST | T_QUAL_VOLATILE));
790 static Type* ParamTypeCvt (Type* T)
791 /* If T is an array, convert it to a pointer else do nothing. Return the
795 if (IsTypeArray (T)) {
803 static void ParseOldStyleParamList (FuncDesc* F)
804 /* Parse an old style (K&R) parameter list */
807 while (CurTok.Tok != TOK_RPAREN) {
809 /* List of identifiers expected */
810 if (CurTok.Tok != TOK_IDENT) {
811 Error ("Identifier expected");
814 /* Create a symbol table entry with type int */
815 AddLocalSym (CurTok.Ident, type_int, SC_AUTO | SC_PARAM | SC_DEF | SC_DEFTYPE, 0);
817 /* Count arguments */
820 /* Skip the identifier */
823 /* Check for more parameters */
824 if (CurTok.Tok == TOK_COMMA) {
831 /* Skip right paren. We must explicitly check for one here, since some of
832 * the breaks above bail out without checking.
836 /* An optional list of type specifications follows */
837 while (CurTok.Tok != TOK_LCURLY) {
841 /* Read the declaration specifier */
842 ParseDeclSpec (&Spec, SC_AUTO, T_INT);
844 /* We accept only auto and register as storage class specifiers, but
845 * we ignore all this, since we use auto anyway.
847 if ((Spec.StorageClass & SC_AUTO) == 0 &&
848 (Spec.StorageClass & SC_REGISTER) == 0) {
849 Error ("Illegal storage class");
852 /* Parse a comma separated variable list */
857 /* Read the parameter */
858 ParseDecl (&Spec, &Decl, DM_NEED_IDENT);
859 if (Decl.Ident[0] != '\0') {
861 /* We have a name given. Search for the symbol */
862 SymEntry* Sym = FindLocalSym (Decl.Ident);
864 /* Check if we already changed the type for this
867 if (Sym->Flags & SC_DEFTYPE) {
868 /* Found it, change the default type to the one given */
869 ChangeSymType (Sym, ParamTypeCvt (Decl.Type));
870 /* Reset the "default type" flag */
871 Sym->Flags &= ~SC_DEFTYPE;
873 /* Type has already been changed */
874 Error ("Redefinition for parameter `%s'", Sym->Name);
877 Error ("Unknown identifier: `%s'", Decl.Ident);
881 if (CurTok.Tok == TOK_COMMA) {
889 /* Variable list must be semicolon terminated */
896 static void ParseAnsiParamList (FuncDesc* F)
897 /* Parse a new style (ANSI) parameter list */
900 while (CurTok.Tok != TOK_RPAREN) {
906 /* Allow an ellipsis as last parameter */
907 if (CurTok.Tok == TOK_ELLIPSIS) {
909 F->Flags |= FD_VARIADIC;
913 /* Read the declaration specifier */
914 ParseDeclSpec (&Spec, SC_AUTO, T_INT);
916 /* We accept only auto and register as storage class specifiers */
917 if ((Spec.StorageClass & SC_AUTO) == SC_AUTO) {
918 Spec.StorageClass = SC_AUTO | SC_PARAM | SC_DEF;
919 } else if ((Spec.StorageClass & SC_REGISTER) == SC_REGISTER) {
920 Spec.StorageClass = SC_REGISTER | SC_STATIC | SC_PARAM | SC_DEF;
922 Error ("Illegal storage class");
923 Spec.StorageClass = SC_AUTO | SC_PARAM | SC_DEF;
926 /* Allow parameters without a name, but remember if we had some to
927 * eventually print an error message later.
929 ParseDecl (&Spec, &Decl, DM_ACCEPT_IDENT);
930 if (Decl.Ident[0] == '\0') {
932 /* Unnamed symbol. Generate a name that is not user accessible,
933 * then handle the symbol normal.
935 AnonName (Decl.Ident, "param");
936 F->Flags |= FD_UNNAMED_PARAMS;
938 /* Clear defined bit on nonames */
939 Decl.StorageClass &= ~SC_DEF;
942 /* Parse an attribute ### */
943 ParseAttribute (&Decl, &Attr);
945 /* Create a symbol table entry */
946 AddLocalSym (Decl.Ident, ParamTypeCvt (Decl.Type), Decl.StorageClass, 0);
948 /* Count arguments */
951 /* Check for more parameters */
952 if (CurTok.Tok == TOK_COMMA) {
959 /* Skip right paren. We must explicitly check for one here, since some of
960 * the breaks above bail out without checking.
964 /* Check if this is a function definition */
965 if (CurTok.Tok == TOK_LCURLY) {
966 /* Print an error if we have unnamed parameters and cc65 extensions
969 if (IS_Get (&Standard) != STD_CC65 &&
970 (F->Flags & FD_UNNAMED_PARAMS) != 0) {
971 Error ("Parameter name omitted");
978 static FuncDesc* ParseFuncDecl (void)
979 /* Parse the argument list of a function. */
984 /* Create a new function descriptor */
985 FuncDesc* F = NewFuncDesc ();
987 /* Enter a new lexical level */
988 EnterFunctionLevel ();
990 /* Check for several special parameter lists */
991 if (CurTok.Tok == TOK_RPAREN) {
992 /* Parameter list is empty */
993 F->Flags |= (FD_EMPTY | FD_VARIADIC);
994 } else if (CurTok.Tok == TOK_VOID && NextTok.Tok == TOK_RPAREN) {
995 /* Parameter list declared as void */
997 F->Flags |= FD_VOID_PARAM;
998 } else if (CurTok.Tok == TOK_IDENT &&
999 (NextTok.Tok == TOK_COMMA || NextTok.Tok == TOK_RPAREN)) {
1000 /* If the identifier is a typedef, we have a new style parameter list,
1001 * if it's some other identifier, it's an old style parameter list.
1003 Sym = FindSym (CurTok.Ident);
1004 if (Sym == 0 || !SymIsTypeDef (Sym)) {
1005 /* Old style (K&R) function. */
1006 F->Flags |= FD_OLDSTYLE;
1011 if ((F->Flags & FD_OLDSTYLE) == 0) {
1012 /* New style function */
1013 ParseAnsiParamList (F);
1015 /* Old style function */
1016 ParseOldStyleParamList (F);
1019 /* Remember the last function parameter. We need it later for several
1020 * purposes, for example when passing stuff to fastcall functions. Since
1021 * more symbols are added to the table, it is easier if we remember it
1022 * now, since it is currently the last entry in the symbol table.
1024 F->LastParam = GetSymTab()->SymTail;
1026 /* Assign offsets. If the function has a variable parameter list,
1027 * there's one additional byte (the arg size).
1029 Offs = (F->Flags & FD_VARIADIC)? 1 : 0;
1032 unsigned Size = CheckedSizeOf (Sym->Type);
1033 if (SymIsRegVar (Sym)) {
1034 Sym->V.R.SaveOffs = Offs;
1039 F->ParamSize += Size;
1043 /* Leave the lexical level remembering the symbol tables */
1044 RememberFunctionLevel (F);
1046 /* Return the function descriptor */
1052 static void Declarator (const DeclSpec* Spec, Declaration* D, unsigned Mode)
1053 /* Recursively process declarators. Build a type array in reverse order. */
1055 /* Read optional function or pointer qualifiers. These modify the
1056 * identifier or token to the right. For convenience, we allow the fastcall
1057 * qualifier also for pointers here. If it is a pointer-to-function, the
1058 * qualifier will later be transfered to the function itself. If it's a
1059 * pointer to something else, it will be flagged as an error.
1061 TypeCode Qualifiers = OptionalQualifiers (T_QUAL_ADDRSIZE | T_QUAL_FASTCALL);
1063 /* Pointer to something */
1064 if (CurTok.Tok == TOK_STAR) {
1069 /* Allow const, restrict and volatile qualifiers */
1070 Qualifiers |= OptionalQualifiers (T_QUAL_CONST | T_QUAL_VOLATILE | T_QUAL_RESTRICT);
1072 /* Parse the type, the pointer points to */
1073 Declarator (Spec, D, Mode);
1076 AddTypeToDeclaration (D, T_PTR | Qualifiers);
1080 if (CurTok.Tok == TOK_LPAREN) {
1082 Declarator (Spec, D, Mode);
1085 /* Things depend on Mode now:
1086 * - Mode == DM_NEED_IDENT means:
1087 * we *must* have a type and a variable identifer.
1088 * - Mode == DM_NO_IDENT means:
1089 * we must have a type but no variable identifer
1090 * (if there is one, it's not read).
1091 * - Mode == DM_ACCEPT_IDENT means:
1092 * we *may* have an identifier. If there is an identifier,
1093 * it is read, but it is no error, if there is none.
1095 if (Mode == DM_NO_IDENT) {
1097 } else if (CurTok.Tok == TOK_IDENT) {
1098 strcpy (D->Ident, CurTok.Ident);
1101 if (Mode == DM_NEED_IDENT) {
1102 Error ("Identifier expected");
1108 while (CurTok.Tok == TOK_LBRACK || CurTok.Tok == TOK_LPAREN) {
1109 if (CurTok.Tok == TOK_LPAREN) {
1111 /* Function declaration */
1114 /* Skip the opening paren */
1117 /* Parse the function declaration */
1118 F = ParseFuncDecl ();
1120 /* We cannot specify fastcall for variadic functions */
1121 if ((F->Flags & FD_VARIADIC) && (Qualifiers & T_QUAL_FASTCALL)) {
1122 Error ("Variadic functions cannot be `__fastcall'");
1123 Qualifiers &= ~T_QUAL_FASTCALL;
1126 /* Add the function type. Be sure to bounds check the type buffer */
1127 NeedTypeSpace (D, 1);
1128 D->Type[D->Index].C = T_FUNC | Qualifiers;
1129 D->Type[D->Index].A.P = F;
1132 /* Qualifiers now used */
1133 Qualifiers = T_QUAL_NONE;
1136 /* Array declaration. */
1137 long Size = UNSPECIFIED;
1139 /* We cannot have any qualifiers for an array */
1140 if (Qualifiers != T_QUAL_NONE) {
1141 Error ("Invalid qualifiers for array");
1142 Qualifiers = T_QUAL_NONE;
1145 /* Skip the left bracket */
1148 /* Read the size if it is given */
1149 if (CurTok.Tok != TOK_RBRACK) {
1151 ConstAbsIntExpr (hie1, &Expr);
1152 if (Expr.IVal <= 0) {
1153 if (D->Ident[0] != '\0') {
1154 Error ("Size of array `%s' is invalid", D->Ident);
1156 Error ("Size of array is invalid");
1163 /* Skip the right bracket */
1166 /* Add the array type with the size to the type */
1167 NeedTypeSpace (D, 1);
1168 D->Type[D->Index].C = T_ARRAY;
1169 D->Type[D->Index].A.L = Size;
1174 /* If we have remaining qualifiers, flag them as invalid */
1175 if (Qualifiers & T_QUAL_NEAR) {
1176 Error ("Invalid `__near__' qualifier");
1178 if (Qualifiers & T_QUAL_FAR) {
1179 Error ("Invalid `__far__' qualifier");
1181 if (Qualifiers & T_QUAL_FASTCALL) {
1182 Error ("Invalid `__fastcall__' qualifier");
1188 /*****************************************************************************/
1190 /*****************************************************************************/
1194 Type* ParseType (Type* T)
1195 /* Parse a complete type specification */
1200 /* Get a type without a default */
1201 InitDeclSpec (&Spec);
1202 ParseTypeSpec (&Spec, -1, T_QUAL_NONE);
1204 /* Parse additional declarators */
1205 ParseDecl (&Spec, &Decl, DM_NO_IDENT);
1207 /* Copy the type to the target buffer */
1208 TypeCopy (T, Decl.Type);
1210 /* Return a pointer to the target buffer */
1216 void ParseDecl (const DeclSpec* Spec, Declaration* D, unsigned Mode)
1217 /* Parse a variable, type or function declaration */
1219 /* Initialize the Declaration struct */
1220 InitDeclaration (D);
1222 /* Get additional declarators and the identifier */
1223 Declarator (Spec, D, Mode);
1225 /* Add the base type. */
1226 NeedTypeSpace (D, TypeLen (Spec->Type) + 1); /* Bounds check */
1227 TypeCopy (D->Type + D->Index, Spec->Type);
1229 /* Use the storage class from the declspec */
1230 D->StorageClass = Spec->StorageClass;
1232 /* Do several fixes on qualifiers */
1233 FixQualifiers (D->Type);
1235 /* If we have a function, add a special storage class */
1236 if (IsTypeFunc (D->Type)) {
1237 D->StorageClass |= SC_FUNC;
1240 /* Check several things for function or function pointer types */
1241 if (IsTypeFunc (D->Type) || IsTypeFuncPtr (D->Type)) {
1243 /* A function. Check the return type */
1244 Type* RetType = GetFuncReturn (D->Type);
1246 /* Functions may not return functions or arrays */
1247 if (IsTypeFunc (RetType)) {
1248 Error ("Functions are not allowed to return functions");
1249 } else if (IsTypeArray (RetType)) {
1250 Error ("Functions are not allowed to return arrays");
1253 /* The return type must not be qualified */
1254 if (GetQualifier (RetType) != T_QUAL_NONE && RetType[1].C == T_END) {
1256 if (GetType (RetType) == T_TYPE_VOID) {
1257 /* A qualified void type is always an error */
1258 Error ("function definition has qualified void return type");
1260 /* For others, qualifiers are ignored */
1261 Warning ("type qualifiers ignored on function return type");
1262 RetType[0].C = UnqualifiedType (RetType[0].C);
1266 /* Warn about an implicit int return in the function */
1267 if ((Spec->Flags & DS_DEF_TYPE) != 0 &&
1268 RetType[0].C == T_INT && RetType[1].C == T_END) {
1269 /* Function has an implicit int return. Output a warning if we don't
1270 * have the C89 standard enabled explicitly.
1272 if (IS_Get (&Standard) >= STD_C99) {
1273 Warning ("Implicit `int' return type is an obsolete feature");
1275 GetFuncDesc (D->Type)->Flags |= FD_OLDSTYLE_INTRET;
1280 /* For anthing that is not a function or typedef, check for an implicit
1283 if ((D->StorageClass & SC_FUNC) != SC_FUNC &&
1284 (D->StorageClass & SC_TYPEDEF) != SC_TYPEDEF) {
1285 /* If the standard was not set explicitly to C89, print a warning
1286 * for variables with implicit int type.
1288 if ((Spec->Flags & DS_DEF_TYPE) != 0 && IS_Get (&Standard) >= STD_C99) {
1289 Warning ("Implicit `int' is an obsolete feature");
1293 /* Check the size of the generated type */
1294 if (!IsTypeFunc (D->Type) && !IsTypeVoid (D->Type)) {
1295 unsigned Size = SizeOf (D->Type);
1296 if (Size >= 0x10000) {
1297 if (D->Ident[0] != '\0') {
1298 Error ("Size of `%s' is invalid (0x%06X)", D->Ident, Size);
1300 Error ("Invalid size in declaration (0x%06X)", Size);
1309 void ParseDeclSpec (DeclSpec* D, unsigned DefStorage, long DefType)
1310 /* Parse a declaration specification */
1312 TypeCode Qualifiers;
1314 /* Initialize the DeclSpec struct */
1317 /* There may be qualifiers *before* the storage class specifier */
1318 Qualifiers = OptionalQualifiers (T_QUAL_CONST | T_QUAL_VOLATILE);
1320 /* Now get the storage class specifier for this declaration */
1321 ParseStorageClass (D, DefStorage);
1323 /* Parse the type specifiers passing any initial type qualifiers */
1324 ParseTypeSpec (D, DefType, Qualifiers);
1329 void CheckEmptyDecl (const DeclSpec* D)
1330 /* Called after an empty type declaration (that is, a type declaration without
1331 * a variable). Checks if the declaration does really make sense and issues a
1335 if ((D->Flags & DS_EXTRA_TYPE) == 0) {
1336 Warning ("Useless declaration");
1342 static void SkipInitializer (unsigned BracesExpected)
1343 /* Skip the remainder of an initializer in case of errors. Try to be somewhat
1344 * smart so we don't have too many following errors.
1347 while (CurTok.Tok != TOK_CEOF && CurTok.Tok != TOK_SEMI && BracesExpected > 0) {
1348 switch (CurTok.Tok) {
1349 case TOK_RCURLY: --BracesExpected; break;
1350 case TOK_LCURLY: ++BracesExpected; break;
1359 static unsigned OpeningCurlyBraces (unsigned BracesNeeded)
1360 /* Accept any number of opening curly braces around an initialization, skip
1361 * them and return the number. If the number of curly braces is less than
1362 * BracesNeeded, issue a warning.
1365 unsigned BraceCount = 0;
1366 while (CurTok.Tok == TOK_LCURLY) {
1370 if (BraceCount < BracesNeeded) {
1371 Error ("`{' expected");
1378 static void ClosingCurlyBraces (unsigned BracesExpected)
1379 /* Accept and skip the given number of closing curly braces together with
1380 * an optional comma. Output an error messages, if the input does not contain
1381 * the expected number of braces.
1384 while (BracesExpected) {
1385 if (CurTok.Tok == TOK_RCURLY) {
1387 } else if (CurTok.Tok == TOK_COMMA && NextTok.Tok == TOK_RCURLY) {
1391 Error ("`}' expected");
1400 static void DefineData (ExprDesc* Expr)
1401 /* Output a data definition for the given expression */
1403 switch (ED_GetLoc (Expr)) {
1406 /* Absolute: numeric address or const */
1407 g_defdata (TypeOf (Expr->Type) | CF_CONST, Expr->IVal, 0);
1411 /* Global variable */
1412 g_defdata (CF_EXTERNAL, Expr->Name, Expr->IVal);
1417 /* Static variable or literal in the literal pool */
1418 g_defdata (CF_STATIC, Expr->Name, Expr->IVal);
1421 case E_LOC_REGISTER:
1422 /* Register variable. Taking the address is usually not
1425 if (IS_Get (&AllowRegVarAddr) == 0) {
1426 Error ("Cannot take the address of a register variable");
1428 g_defdata (CF_REGVAR, Expr->Name, Expr->IVal);
1434 Error ("Non constant initializer");
1438 Internal ("Unknown constant type: 0x%04X", ED_GetLoc (Expr));
1444 static unsigned ParseScalarInit (Type* T)
1445 /* Parse initializaton for scalar data types. Return the number of data bytes. */
1449 /* Optional opening brace */
1450 unsigned BraceCount = OpeningCurlyBraces (0);
1452 /* We warn if an initializer for a scalar contains braces, because this is
1453 * quite unusual and often a sign for some problem in the input.
1455 if (BraceCount > 0) {
1456 Warning ("Braces around scalar initializer");
1459 /* Get the expression and convert it to the target type */
1460 ConstExpr (hie1, &ED);
1461 TypeConversion (&ED, T);
1463 /* Output the data */
1466 /* Close eventually opening braces */
1467 ClosingCurlyBraces (BraceCount);
1475 static unsigned ParsePointerInit (Type* T)
1476 /* Parse initializaton for pointer data types. Return the number of data bytes. */
1478 /* Optional opening brace */
1479 unsigned BraceCount = OpeningCurlyBraces (0);
1483 ConstExpr (hie1, &ED);
1484 TypeConversion (&ED, T);
1486 /* Output the data */
1489 /* Close eventually opening braces */
1490 ClosingCurlyBraces (BraceCount);
1498 static unsigned ParseArrayInit (Type* T, int AllowFlexibleMembers)
1499 /* Parse initializaton for arrays. Return the number of data bytes. */
1503 /* Get the array data */
1504 Type* ElementType = GetElementType (T);
1505 unsigned ElementSize = CheckedSizeOf (ElementType);
1506 long ElementCount = GetElementCount (T);
1508 /* Special handling for a character array initialized by a literal */
1509 if (IsTypeChar (ElementType) &&
1510 (CurTok.Tok == TOK_SCONST ||
1511 (CurTok.Tok == TOK_LCURLY && NextTok.Tok == TOK_SCONST))) {
1513 /* Char array initialized by string constant */
1517 /* If we initializer is enclosed in brackets, remember this fact and
1518 * skip the opening bracket.
1520 NeedParen = (CurTok.Tok == TOK_LCURLY);
1525 /* Get the initializer string and its size */
1526 Str = GetLiteral (CurTok.IVal);
1527 Count = GetLiteralPoolOffs () - CurTok.IVal;
1529 /* Translate into target charset */
1530 TranslateLiteralPool (CurTok.IVal);
1532 /* If the array is one too small for the string literal, omit the
1535 if (ElementCount != UNSPECIFIED &&
1536 ElementCount != FLEXIBLE &&
1537 Count == ElementCount + 1) {
1538 /* Omit the trailing zero */
1542 /* Output the data */
1543 g_defbytes (Str, Count);
1545 /* Remove string from pool */
1546 ResetLiteralPoolOffs (CurTok.IVal);
1549 /* If the initializer was enclosed in curly braces, we need a closing
1561 /* Initialize the array members */
1563 while (CurTok.Tok != TOK_RCURLY) {
1564 /* Flexible array members may not be initialized within
1565 * an array (because the size of each element may differ
1568 ParseInitInternal (ElementType, 0);
1570 if (CurTok.Tok != TOK_COMMA)
1575 /* Closing curly braces */
1579 if (ElementCount == UNSPECIFIED) {
1580 /* Number of elements determined by initializer */
1581 SetElementCount (T, Count);
1582 ElementCount = Count;
1583 } else if (ElementCount == FLEXIBLE && AllowFlexibleMembers) {
1584 /* In non ANSI mode, allow initialization of flexible array
1587 ElementCount = Count;
1588 } else if (Count < ElementCount) {
1589 g_zerobytes ((ElementCount - Count) * ElementSize);
1590 } else if (Count > ElementCount) {
1591 Error ("Too many initializers");
1593 return ElementCount * ElementSize;
1598 static unsigned ParseStructInit (Type* T, int AllowFlexibleMembers)
1599 /* Parse initialization of a struct or union. Return the number of data bytes. */
1603 unsigned StructSize;
1607 /* Consume the opening curly brace */
1610 /* Get a pointer to the struct entry from the type */
1611 Entry = GetSymEntry (T);
1613 /* Get the size of the struct from the symbol table entry */
1614 StructSize = Entry->V.S.Size;
1616 /* Check if this struct definition has a field table. If it doesn't, it
1617 * is an incomplete definition.
1619 Tab = Entry->V.S.SymTab;
1621 Error ("Cannot initialize variables with incomplete type");
1622 /* Try error recovery */
1623 SkipInitializer (1);
1624 /* Nothing initialized */
1628 /* Get a pointer to the list of symbols */
1629 Entry = Tab->SymHead;
1631 /* Initialize fields */
1633 while (CurTok.Tok != TOK_RCURLY) {
1635 Error ("Too many initializers");
1636 SkipInitializer (1);
1639 /* Parse initialization of one field. Flexible array members may
1640 * only be initialized if they are the last field (or part of the
1641 * last struct field).
1643 Size += ParseInitInternal (Entry->Type, AllowFlexibleMembers && Entry->NextSym == 0);
1644 Entry = Entry->NextSym;
1645 if (CurTok.Tok != TOK_COMMA)
1650 /* Consume the closing curly brace */
1653 /* If there are struct fields left, reserve additional storage */
1654 if (Size < StructSize) {
1655 g_zerobytes (StructSize - Size);
1659 /* Return the actual number of bytes initialized. This number may be
1660 * larger than StructSize if flexible array members are present and were
1661 * initialized (possible in non ANSI mode).
1668 static unsigned ParseVoidInit (void)
1669 /* Parse an initialization of a void variable (special cc65 extension).
1670 * Return the number of bytes initialized.
1679 /* Allow an arbitrary list of values */
1682 ConstExpr (hie1, &Expr);
1683 switch (UnqualifiedType (Expr.Type[0].C)) {
1687 if (ED_IsConstAbsInt (&Expr)) {
1688 /* Make it byte sized */
1692 Size += SIZEOF_CHAR;
1701 if (ED_IsConstAbsInt (&Expr)) {
1702 /* Make it word sized */
1703 Expr.IVal &= 0xFFFF;
1711 if (ED_IsConstAbsInt (&Expr)) {
1712 /* Make it dword sized */
1713 Expr.IVal &= 0xFFFFFFFF;
1716 Size += SIZEOF_LONG;
1720 Error ("Illegal type in initialization");
1725 if (CurTok.Tok != TOK_COMMA) {
1730 } while (CurTok.Tok != TOK_RCURLY);
1735 /* Return the number of bytes initialized */
1741 static unsigned ParseInitInternal (Type* T, int AllowFlexibleMembers)
1742 /* Parse initialization of variables. Return the number of data bytes. */
1744 switch (UnqualifiedType (T->C)) {
1756 return ParseScalarInit (T);
1759 return ParsePointerInit (T);
1762 return ParseArrayInit (T, AllowFlexibleMembers);
1766 return ParseStructInit (T, AllowFlexibleMembers);
1769 if (IS_Get (&Standard) == STD_CC65) {
1770 /* Special cc65 extension in non ANSI mode */
1771 return ParseVoidInit ();
1776 Error ("Illegal type");
1784 unsigned ParseInit (Type* T)
1785 /* Parse initialization of variables. Return the number of data bytes. */
1787 /* Parse the initialization. Flexible array members can only be initialized
1790 unsigned Size = ParseInitInternal (T, IS_Get (&Standard) == STD_CC65);
1792 /* The initialization may not generate code on global level, because code
1793 * outside function scope will never get executed.
1795 if (HaveGlobalCode ()) {
1796 Error ("Non constant initializers");
1797 RemoveGlobalCode ();
1800 /* Return the size needed for the initialization */