1 /*****************************************************************************/
5 /* Parse variable and function declarations */
9 /* (C) 1998-2003 Ullrich von Bassewitz */
10 /* Römerstrasse 52 */
11 /* D-70794 Filderstadt */
12 /* EMail: uz@musoftware.de */
15 /* This software is provided 'as-is', without any expressed or implied */
16 /* warranty. In no event will the authors be held liable for any damages */
17 /* arising from the use of this software. */
19 /* Permission is granted to anyone to use this software for any purpose, */
20 /* including commercial applications, and to alter it and redistribute it */
21 /* freely, subject to the following restrictions: */
23 /* 1. The origin of this software must not be misrepresented; you must not */
24 /* claim that you wrote the original software. If you use this software */
25 /* in a product, an acknowledgment in the product documentation would be */
26 /* appreciated but is not required. */
27 /* 2. Altered source versions must be plainly marked as such, and must not */
28 /* be misrepresented as being the original software. */
29 /* 3. This notice may not be removed or altered from any source */
32 /*****************************************************************************/
61 /*****************************************************************************/
63 /*****************************************************************************/
67 static void ParseTypeSpec (DeclSpec* D, int Default);
68 /* Parse a type specificier */
72 /*****************************************************************************/
73 /* internal functions */
74 /*****************************************************************************/
78 static type OptionalQualifiers (type Q)
79 /* Read type qualifiers if we have any */
81 while (CurTok.Tok == TOK_CONST || CurTok.Tok == TOK_VOLATILE) {
86 if (Q & T_QUAL_CONST) {
87 Error ("Duplicate qualifier: `const'");
93 if (Q & T_QUAL_VOLATILE) {
94 Error ("Duplicate qualifier: `volatile'");
100 /* Keep gcc silent */
109 /* Return the qualifiers read */
115 static void optionalint (void)
116 /* Eat an optional "int" token */
118 if (CurTok.Tok == TOK_INT) {
126 static void optionalsigned (void)
127 /* Eat an optional "signed" token */
129 if (CurTok.Tok == TOK_SIGNED) {
137 static void InitDeclSpec (DeclSpec* D)
138 /* Initialize the DeclSpec struct for use */
147 static void InitDeclaration (Declaration* D)
148 /* Initialize the Declaration struct for use */
157 static void ParseStorageClass (DeclSpec* D, unsigned DefStorage)
158 /* Parse a storage class */
160 /* Assume we're using an explicit storage class */
161 D->Flags &= ~DS_DEF_STORAGE;
163 /* Check the storage class given */
164 switch (CurTok.Tok) {
167 D->StorageClass = SC_EXTERN | SC_STATIC;
172 D->StorageClass = SC_STATIC;
177 D->StorageClass = SC_REGISTER | SC_STATIC;
182 D->StorageClass = SC_AUTO;
187 D->StorageClass = SC_TYPEDEF;
192 /* No storage class given, use default */
193 D->Flags |= DS_DEF_STORAGE;
194 D->StorageClass = DefStorage;
201 static void ParseEnumDecl (void)
202 /* Process an enum declaration . */
207 /* Accept forward definitions */
208 if (CurTok.Tok != TOK_LCURLY) {
212 /* Skip the opening curly brace */
215 /* Read the enum tags */
217 while (CurTok.Tok != TOK_RCURLY) {
219 /* We expect an identifier */
220 if (CurTok.Tok != TOK_IDENT) {
221 Error ("Identifier expected");
225 /* Remember the identifier and skip it */
226 strcpy (Ident, CurTok.Ident);
229 /* Check for an assigned value */
230 if (CurTok.Tok == TOK_ASSIGN) {
234 EnumVal = lval.ConstVal;
237 /* Add an entry to the symbol table */
238 AddConstSym (Ident, type_int, SC_ENUM, EnumVal++);
240 /* Check for end of definition */
241 if (CurTok.Tok != TOK_COMMA)
250 static SymEntry* ParseStructDecl (const char* Name, type StructType)
251 /* Parse a struct/union declaration. */
261 if (CurTok.Tok != TOK_LCURLY) {
262 /* Just a forward declaration. Try to find a struct with the given
263 * name. If there is none, insert a forward declaration into the
264 * current lexical level.
266 Entry = FindTagSym (Name);
268 Entry = AddStructSym (Name, 0, 0);
269 } else if (SymIsLocal (Entry) && (Entry->Flags & SC_STRUCT) == 0) {
270 /* Already defined in the level but no struct */
271 Error ("Symbol `%s' is already different kind", Name);
276 /* Add a forward declaration for the struct in the current lexical level */
277 Entry = AddStructSym (Name, 0, 0);
279 /* Skip the curly brace */
282 /* Enter a new lexical level for the struct */
285 /* Parse struct fields */
287 while (CurTok.Tok != TOK_RCURLY) {
289 /* Get the type of the entry */
291 InitDeclSpec (&Spec);
292 ParseTypeSpec (&Spec, -1);
294 /* Read fields with this type */
297 /* Get type and name of the struct field */
299 ParseDecl (&Spec, &Decl, 0);
301 /* Get the offset of this field */
302 Offs = (StructType == T_STRUCT)? StructSize : 0;
304 /* Add a field entry to the table */
305 AddLocalSym (Decl.Ident, Decl.Type, SC_STRUCTFIELD, Offs);
307 /* Calculate offset of next field/size of the union */
308 FieldSize = CheckedSizeOf (Decl.Type);
309 if (StructType == T_STRUCT) {
311 StructSize += FieldSize;
314 if (FieldSize > StructSize) {
315 StructSize = FieldSize;
319 if (CurTok.Tok != TOK_COMMA)
326 /* Skip the closing brace */
329 /* Remember the symbol table and leave the struct level */
330 FieldTab = GetSymTab ();
333 /* Make a real entry from the forward decl and return it */
334 return AddStructSym (Name, StructSize, FieldTab);
339 static void ParseTypeSpec (DeclSpec* D, int Default)
340 /* Parse a type specificier */
345 type Qualifiers; /* Type qualifiers */
347 /* Assume we have an explicit type */
348 D->Flags &= ~DS_DEF_TYPE;
350 /* Read type qualifiers if we have any */
351 Qualifiers = OptionalQualifiers (T_QUAL_NONE);
353 /* Look at the data type */
354 switch (CurTok.Tok) {
364 D->Type[0] = GetDefaultChar();
370 if (CurTok.Tok == TOK_UNSIGNED) {
373 D->Type[0] = T_ULONG;
385 if (CurTok.Tok == TOK_UNSIGNED) {
388 D->Type[0] = T_USHORT;
393 D->Type[0] = T_SHORT;
406 switch (CurTok.Tok) {
410 D->Type[0] = T_SCHAR;
417 D->Type[0] = T_SHORT;
441 switch (CurTok.Tok) {
445 D->Type[0] = T_UCHAR;
452 D->Type[0] = T_USHORT;
459 D->Type[0] = T_ULONG;
476 StructType = (CurTok.Tok == TOK_STRUCT)? T_STRUCT : T_UNION;
479 if (CurTok.Tok == TOK_IDENT) {
480 strcpy (Ident, CurTok.Ident);
483 AnonName (Ident, (StructType == T_STRUCT)? "struct" : "union");
485 /* Remember we have an extra type decl */
486 D->Flags |= DS_EXTRA_TYPE;
487 /* Declare the struct in the current scope */
488 Entry = ParseStructDecl (Ident, StructType);
489 /* Encode the struct entry into the type */
490 D->Type[0] = StructType;
491 EncodePtr (D->Type+1, Entry);
492 D->Type[DECODE_SIZE+1] = T_END;
497 if (CurTok.Tok != TOK_LCURLY) {
499 if (CurTok.Tok == TOK_IDENT) {
500 /* Find an entry with this name */
501 Entry = FindTagSym (CurTok.Ident);
503 if (SymIsLocal (Entry) && (Entry->Flags & SC_ENUM) == 0) {
504 Error ("Symbol `%s' is already different kind", Entry->Name);
507 /* Insert entry into table ### */
509 /* Skip the identifier */
512 Error ("Identifier expected");
515 /* Remember we have an extra type decl */
516 D->Flags |= DS_EXTRA_TYPE;
517 /* Parse the enum decl */
524 Entry = FindSym (CurTok.Ident);
525 if (Entry && SymIsTypeDef (Entry)) {
528 TypeCpy (D->Type, Entry->Type);
535 Error ("Type expected");
539 D->Flags |= DS_DEF_TYPE;
540 D->Type[0] = (type) Default;
546 /* There may also be qualifiers *after* the initial type */
547 D->Type[0] |= OptionalQualifiers (Qualifiers);
552 static type* ParamTypeCvt (type* T)
553 /* If T is an array, convert it to a pointer else do nothing. Return the
557 if (IsTypeArray (T)) {
566 static void ParseOldStyleParamList (FuncDesc* F)
567 /* Parse an old style (K&R) parameter list */
570 while (CurTok.Tok != TOK_RPAREN) {
572 /* List of identifiers expected */
573 if (CurTok.Tok != TOK_IDENT) {
574 Error ("Identifier expected");
577 /* Create a symbol table entry with type int */
578 AddLocalSym (CurTok.Ident, type_int, SC_AUTO | SC_PARAM | SC_DEF, 0);
580 /* Count arguments */
583 /* Skip the identifier */
586 /* Check for more parameters */
587 if (CurTok.Tok == TOK_COMMA) {
594 /* Skip right paren. We must explicitly check for one here, since some of
595 * the breaks above bail out without checking.
599 /* An optional list of type specifications follows */
600 while (CurTok.Tok != TOK_LCURLY) {
604 /* Read the declaration specifier */
605 ParseDeclSpec (&Spec, SC_AUTO, T_INT);
607 /* We accept only auto and register as storage class specifiers, but
608 * we ignore all this, since we use auto anyway.
610 if ((Spec.StorageClass & SC_AUTO) == 0 &&
611 (Spec.StorageClass & SC_REGISTER) == 0) {
612 Error ("Illegal storage class");
615 /* Parse a comma separated variable list */
620 /* Read the parameter */
621 ParseDecl (&Spec, &Decl, DM_NEED_IDENT);
622 if (Decl.Ident[0] != '\0') {
624 /* We have a name given. Search for the symbol */
625 SymEntry* Sym = FindLocalSym (Decl.Ident);
627 /* Found it, change the default type to the one given */
628 ChangeSymType (Sym, ParamTypeCvt (Decl.Type));
630 Error ("Unknown identifier: `%s'", Decl.Ident);
634 if (CurTok.Tok == TOK_COMMA) {
642 /* Variable list must be semicolon terminated */
649 static void ParseAnsiParamList (FuncDesc* F)
650 /* Parse a new style (ANSI) parameter list */
653 while (CurTok.Tok != TOK_RPAREN) {
659 /* Allow an ellipsis as last parameter */
660 if (CurTok.Tok == TOK_ELLIPSIS) {
662 F->Flags |= FD_VARIADIC;
666 /* Read the declaration specifier */
667 ParseDeclSpec (&Spec, SC_AUTO, T_INT);
669 /* We accept only auto and register as storage class specifiers */
670 if ((Spec.StorageClass & SC_AUTO) == SC_AUTO) {
671 Spec.StorageClass = SC_AUTO | SC_PARAM | SC_DEF;
672 } else if ((Spec.StorageClass & SC_REGISTER) == SC_REGISTER) {
673 Spec.StorageClass = SC_REGISTER | SC_STATIC | SC_PARAM | SC_DEF;
675 Error ("Illegal storage class");
676 Spec.StorageClass = SC_AUTO | SC_PARAM | SC_DEF;
679 /* Allow parameters without a name, but remember if we had some to
680 * eventually print an error message later.
682 ParseDecl (&Spec, &Decl, DM_ACCEPT_IDENT);
683 if (Decl.Ident[0] == '\0') {
685 /* Unnamed symbol. Generate a name that is not user accessible,
686 * then handle the symbol normal.
688 AnonName (Decl.Ident, "param");
689 F->Flags |= FD_UNNAMED_PARAMS;
691 /* Clear defined bit on nonames */
692 Spec.StorageClass &= ~SC_DEF;
695 /* Parse an attribute ### */
696 ParseAttribute (&Decl, &Attr);
698 /* Create a symbol table entry */
699 AddLocalSym (Decl.Ident, ParamTypeCvt (Decl.Type), Spec.StorageClass, 0);
701 /* Count arguments */
704 /* Check for more parameters */
705 if (CurTok.Tok == TOK_COMMA) {
712 /* Skip right paren. We must explicitly check for one here, since some of
713 * the breaks above bail out without checking.
717 /* Check if this is a function definition */
718 if (CurTok.Tok == TOK_LCURLY) {
719 /* Print an error if in strict ANSI mode and we have unnamed
722 if (ANSI && (F->Flags & FD_UNNAMED_PARAMS) != 0) {
723 Error ("Parameter name omitted");
730 static FuncDesc* ParseFuncDecl (const DeclSpec* Spec)
731 /* Parse the argument list of a function. */
736 /* Create a new function descriptor */
737 FuncDesc* F = NewFuncDesc ();
739 /* Enter a new lexical level */
740 EnterFunctionLevel ();
742 /* Check for several special parameter lists */
743 if (CurTok.Tok == TOK_RPAREN) {
744 /* Parameter list is empty */
745 F->Flags |= (FD_EMPTY | FD_VARIADIC);
746 } else if (CurTok.Tok == TOK_VOID && NextTok.Tok == TOK_RPAREN) {
747 /* Parameter list declared as void */
749 F->Flags |= FD_VOID_PARAM;
750 } else if (CurTok.Tok == TOK_IDENT &&
751 (NextTok.Tok == TOK_COMMA || NextTok.Tok == TOK_RPAREN)) {
752 /* If the identifier is a typedef, we have a new style parameter list,
753 * if it's some other identifier, it's an old style parameter list.
755 Sym = FindSym (CurTok.Ident);
756 if (Sym == 0 || !SymIsTypeDef (Sym)) {
757 /* Old style (K&R) function. Assume variable param list. */
758 F->Flags |= (FD_OLDSTYLE | FD_VARIADIC);
762 /* Check for an implicit int return in the function */
763 if ((Spec->Flags & DS_DEF_TYPE) != 0 &&
764 Spec->Type[0] == T_INT &&
765 Spec->Type[1] == T_END) {
766 /* Function has an implicit int return */
767 F->Flags |= FD_OLDSTYLE_INTRET;
771 if ((F->Flags & FD_OLDSTYLE) == 0) {
772 /* New style function */
773 ParseAnsiParamList (F);
775 /* Old style function */
776 ParseOldStyleParamList (F);
779 /* Assign offsets. If the function has a variable parameter list,
780 * there's one additional byte (the arg size).
782 Offs = (F->Flags & FD_VARIADIC)? 1 : 0;
783 Sym = GetSymTab()->SymTail;
785 unsigned Size = CheckedSizeOf (Sym->Type);
786 if (SymIsRegVar (Sym)) {
787 Sym->V.R.SaveOffs = Offs;
792 F->ParamSize += Size;
796 /* Leave the lexical level remembering the symbol tables */
797 RememberFunctionLevel (F);
799 /* Return the function descriptor */
805 static void Decl (const DeclSpec* Spec, Declaration* D, unsigned Mode)
806 /* Recursively process declarators. Build a type array in reverse order. */
809 if (CurTok.Tok == TOK_STAR) {
812 /* Allow optional const or volatile qualifiers */
813 T |= OptionalQualifiers (T_QUAL_NONE);
814 Decl (Spec, D, Mode);
817 } else if (CurTok.Tok == TOK_LPAREN) {
819 Decl (Spec, D, Mode);
821 } else if (CurTok.Tok == TOK_FASTCALL) {
822 /* Remember the current type pointer */
824 /* Skip the fastcall token */
826 /* Parse the function */
827 Decl (Spec, D, Mode);
828 /* Set the fastcall flag */
829 if (!IsTypeFunc (T) && !IsTypeFuncPtr (T)) {
830 Error ("__fastcall__ modifier applied to non function");
831 } else if (IsVariadicFunc (T)) {
832 Error ("Cannot apply __fastcall__ to functions with variable parameter list");
834 FuncDesc* F = GetFuncDesc (T);
835 F->Flags |= FD_FASTCALL;
839 /* Things depend on Mode now:
840 * - Mode == DM_NEED_IDENT means:
841 * we *must* have a type and a variable identifer.
842 * - Mode == DM_NO_IDENT means:
843 * we must have a type but no variable identifer
844 * (if there is one, it's not read).
845 * - Mode == DM_ACCEPT_IDENT means:
846 * we *may* have an identifier. If there is an identifier,
847 * it is read, but it is no error, if there is none.
849 if (Mode == DM_NO_IDENT) {
851 } else if (CurTok.Tok == TOK_IDENT) {
852 strcpy (D->Ident, CurTok.Ident);
855 if (Mode == DM_NEED_IDENT) {
856 Error ("Identifier expected");
862 while (CurTok.Tok == TOK_LBRACK || CurTok.Tok == TOK_LPAREN) {
863 if (CurTok.Tok == TOK_LPAREN) {
864 /* Function declaration */
867 /* Parse the function declaration */
868 F = ParseFuncDecl (Spec);
873 /* Array declaration */
874 unsigned long Size = 0;
876 /* Read the size if it is given */
877 if (CurTok.Tok != TOK_RBRACK) {
880 if (lval.ConstVal < 0) {
881 if (D->Ident[0] != '\0') {
882 Error ("Size of array `%s' is negative", D->Ident);
884 Error ("Size of array is negative");
888 Size = lval.ConstVal;
900 /*****************************************************************************/
902 /*****************************************************************************/
906 type* ParseType (type* Type)
907 /* Parse a complete type specification */
912 /* Get a type without a default */
913 InitDeclSpec (&Spec);
914 ParseTypeSpec (&Spec, -1);
916 /* Parse additional declarators */
917 InitDeclaration (&Decl);
918 ParseDecl (&Spec, &Decl, DM_NO_IDENT);
920 /* Copy the type to the target buffer */
921 TypeCpy (Type, Decl.Type);
923 /* Return a pointer to the target buffer */
929 void ParseDecl (const DeclSpec* Spec, Declaration* D, unsigned Mode)
930 /* Parse a variable, type or function declaration */
932 /* Initialize the Declaration struct */
935 /* Get additional declarators and the identifier */
936 Decl (Spec, D, Mode);
938 /* Add the base type. */
939 TypeCpy (D->T, Spec->Type);
941 /* Check the size of the generated type */
942 if (!IsTypeFunc (D->Type) && !IsTypeVoid (D->Type) && SizeOf (D->Type) >= 0x10000) {
943 if (D->Ident[0] != '\0') {
944 Error ("Size of `%s' is invalid", D->Ident);
946 Error ("Invalid size");
953 void ParseDeclSpec (DeclSpec* D, unsigned DefStorage, int DefType)
954 /* Parse a declaration specification */
956 /* Initialize the DeclSpec struct */
959 /* First, get the storage class specifier for this declaration */
960 ParseStorageClass (D, DefStorage);
962 /* Parse the type specifiers */
963 ParseTypeSpec (D, DefType);
968 void CheckEmptyDecl (const DeclSpec* D)
969 /* Called after an empty type declaration (that is, a type declaration without
970 * a variable). Checks if the declaration does really make sense and issues a
974 if ((D->Flags & DS_EXTRA_TYPE) == 0) {
975 Warning ("Useless declaration");
981 static void ParseVoidInit (void)
982 /* Parse an initialization of a void variable (special cc65 extension) */
986 /* Allow an arbitrary list of values */
990 switch (lval.Type[0]) {
994 if ((lval.Flags & E_MCTYPE) == E_TCONST) {
995 /* Make it byte sized */
996 lval.ConstVal &= 0xFF;
1007 if ((lval.Flags & E_MCTYPE) == E_TCONST) {
1008 /* Make it word sized */
1009 lval.ConstVal &= 0xFFFF;
1020 Error ("Illegal type in initialization");
1025 if (CurTok.Tok != TOK_COMMA) {
1030 } while (CurTok.Tok != TOK_RCURLY);
1037 static void ParseStructInit (type* Type)
1038 /* Parse initialization of a struct or union */
1043 /* Consume the opening curly brace */
1046 /* Get a pointer to the struct entry from the type */
1047 Entry = (SymEntry*) Decode (Type + 1);
1049 /* Check if this struct definition has a field table. If it doesn't, it
1050 * is an incomplete definition.
1052 Tab = Entry->V.S.SymTab;
1054 Error ("Cannot initialize variables with incomplete type");
1055 /* Returning here will cause lots of errors, but recovery is difficult */
1059 /* Get a pointer to the list of symbols */
1060 Entry = Tab->SymHead;
1061 while (CurTok.Tok != TOK_RCURLY) {
1063 Error ("Too many initializers");
1066 ParseInit (Entry->Type);
1067 Entry = Entry->NextSym;
1068 if (CurTok.Tok != TOK_COMMA)
1073 /* Consume the closing curly brace */
1076 /* If there are struct fields left, reserve additional storage */
1078 g_zerobytes (CheckedSizeOf (Entry->Type));
1079 Entry = Entry->NextSym;
1085 void ParseInit (type* T)
1086 /* Parse initialization of variables. */
1094 switch (UnqualifiedType (*T)) {
1099 if ((lval.Flags & E_MCTYPE) == E_TCONST) {
1100 /* Make it byte sized */
1101 lval.ConstVal &= 0xFF;
1103 assignadjust (T, &lval);
1113 if ((lval.Flags & E_MCTYPE) == E_TCONST) {
1114 /* Make it word sized */
1115 lval.ConstVal &= 0xFFFF;
1117 assignadjust (T, &lval);
1124 if ((lval.Flags & E_MCTYPE) == E_TCONST) {
1125 /* Make it long sized */
1126 lval.ConstVal &= 0xFFFFFFFF;
1128 assignadjust (T, &lval);
1133 Size = Decode (T + 1);
1134 t = T + DECODE_SIZE + 1;
1135 if (IsTypeChar(t) && CurTok.Tok == TOK_SCONST) {
1136 str = GetLiteral (CurTok.IVal);
1137 Count = strlen (str) + 1;
1138 TranslateLiteralPool (CurTok.IVal); /* Translate into target charset */
1139 g_defbytes (str, Count);
1140 ResetLiteralPoolOffs (CurTok.IVal); /* Remove string from pool */
1145 while (CurTok.Tok != TOK_RCURLY) {
1146 ParseInit (T + DECODE_SIZE + 1);
1148 if (CurTok.Tok != TOK_COMMA)
1155 Encode (T + 1, Count);
1156 } else if (Count < Size) {
1157 g_zerobytes ((Size - Count) * CheckedSizeOf (T + DECODE_SIZE + 1));
1158 } else if (Count > Size) {
1159 Error ("Too many initializers");
1165 ParseStructInit (T);
1170 /* Special cc65 extension in non ANSI mode */
1177 Error ("Illegal type");