1 /*****************************************************************************/
5 /* Parse variable and function declarations */
9 /* (C) 1998-2001 Ullrich von Bassewitz */
11 /* D-70597 Stuttgart */
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_CONST || curtok == 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_INT) {
126 static void optionalsigned (void)
127 /* Eat an optional "signed" token */
129 if (curtok == 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 */
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_LCURLY) {
212 /* Skip the opening curly brace */
215 /* Read the enum tags */
217 while (curtok != TOK_RCURLY) {
219 /* We expect an identifier */
220 if (curtok != 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_ASSIGN) {
234 EnumVal = lval.e_const;
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_COMMA)
250 static SymEntry* ParseStructDecl (const char* Name, type StructType)
251 /* Parse a struct/union declaration. */
260 if (curtok != TOK_LCURLY) {
261 /* Just a forward declaration. Try to find a struct with the given
262 * name. If there is none, insert a forward declaration into the
263 * current lexical level.
265 Entry = FindTagSym (Name);
267 Entry = AddStructSym (Name, 0, 0);
268 } else if (SymIsLocal (Entry) && (Entry->Flags & SC_STRUCT) == 0) {
269 /* Already defined in the level but no struct */
270 Error ("Symbol `%s' is already different kind", Name);
275 /* Add a forward declaration for the struct in the current lexical level */
276 Entry = AddStructSym (Name, 0, 0);
278 /* Skip the curly brace */
281 /* Enter a new lexical level for the struct */
284 /* Parse struct fields */
286 while (curtok != TOK_RCURLY) {
288 /* Get the type of the entry */
290 InitDeclSpec (&Spec);
291 ParseTypeSpec (&Spec, -1);
293 /* Read fields with this type */
296 /* Get type and name of the struct field */
298 ParseDecl (&Spec, &Decl, 0);
300 /* Add a field entry to the table */
301 AddLocalSym (Decl.Ident, Decl.Type, SC_SFLD, (StructType == T_STRUCT)? Size : 0);
303 /* Calculate offset of next field/size of the union */
304 Offs = SizeOf (Decl.Type);
305 if (StructType == T_STRUCT) {
313 if (curtok != TOK_COMMA)
320 /* Skip the closing brace */
323 /* Remember the symbol table and leave the struct level */
324 FieldTab = GetSymTab ();
327 /* Make a real entry from the forward decl and return it */
328 return AddStructSym (Name, Size, FieldTab);
333 static void ParseTypeSpec (DeclSpec* D, int Default)
334 /* Parse a type specificier */
339 type Qualifiers; /* Type qualifiers */
341 /* Assume we have an explicit type */
342 D->Flags &= ~DS_DEF_TYPE;
344 /* Read type qualifiers if we have any */
345 Qualifiers = OptionalQualifiers (T_QUAL_NONE);
347 /* Look at the data type */
358 D->Type[0] = GetDefaultChar();
364 if (curtok == TOK_UNSIGNED) {
367 D->Type[0] = T_ULONG;
379 if (curtok == TOK_UNSIGNED) {
382 D->Type[0] = T_USHORT;
387 D->Type[0] = T_SHORT;
404 D->Type[0] = T_SCHAR;
411 D->Type[0] = T_SHORT;
439 D->Type[0] = T_UCHAR;
446 D->Type[0] = T_USHORT;
453 D->Type[0] = T_ULONG;
470 StructType = (curtok == TOK_STRUCT)? T_STRUCT : T_UNION;
473 if (curtok == TOK_IDENT) {
474 strcpy (Ident, CurTok.Ident);
477 AnonName (Ident, (StructType == T_STRUCT)? "struct" : "union");
479 /* Remember we have an extra type decl */
480 D->Flags |= DS_EXTRA_TYPE;
481 /* Declare the struct in the current scope */
482 Entry = ParseStructDecl (Ident, StructType);
483 /* Encode the struct entry into the type */
484 D->Type[0] = StructType;
485 EncodePtr (D->Type+1, Entry);
486 D->Type[DECODE_SIZE+1] = T_END;
491 if (curtok != TOK_LCURLY) {
493 if (curtok == TOK_IDENT) {
494 /* Find an entry with this name */
495 Entry = FindTagSym (CurTok.Ident);
497 if (SymIsLocal (Entry) && (Entry->Flags & SC_ENUM) == 0) {
498 Error ("Symbol `%s' is already different kind", Entry->Name);
501 /* Insert entry into table ### */
503 /* Skip the identifier */
506 Error ("Identifier expected");
509 /* Remember we have an extra type decl */
510 D->Flags |= DS_EXTRA_TYPE;
511 /* Parse the enum decl */
518 Entry = FindSym (CurTok.Ident);
519 if (Entry && IsTypeDef (Entry)) {
522 TypeCpy (D->Type, Entry->Type);
529 Error ("Type expected");
533 D->Flags |= DS_DEF_TYPE;
534 D->Type[0] = (type) Default;
540 /* There may also be qualifiers *after* the initial type */
541 D->Type[0] |= OptionalQualifiers (Qualifiers);
546 static type* ParamTypeCvt (type* T)
547 /* If T is an array, convert it to a pointer else do nothing. Return the
551 if (IsTypeArray (T)) {
560 static void ParseOldStyleParamList (FuncDesc* F)
561 /* Parse an old style (K&R) parameter list */
564 while (curtok != TOK_RPAREN) {
566 /* List of identifiers expected */
567 if (curtok != TOK_IDENT) {
568 Error ("Identifier expected");
571 /* Create a symbol table entry with type int */
572 AddLocalSym (CurTok.Ident, type_int, SC_AUTO | SC_PARAM | SC_DEF, 0);
574 /* Count arguments */
577 /* Skip the identifier */
580 /* Check for more parameters */
581 if (curtok == TOK_COMMA) {
588 /* Skip right paren. We must explicitly check for one here, since some of
589 * the breaks above bail out without checking.
593 /* An optional list of type specifications follows */
594 while (curtok != TOK_LCURLY) {
598 /* Read the declaration specifier */
599 ParseDeclSpec (&Spec, SC_AUTO, T_INT);
601 /* We accept only auto and register as storage class specifiers, but
602 * we ignore all this, since we use auto anyway.
604 if ((Spec.StorageClass & SC_AUTO) == 0 &&
605 (Spec.StorageClass & SC_REGISTER) == 0) {
606 Error ("Illegal storage class");
609 /* Parse a comma separated variable list */
614 /* Read the parameter */
615 ParseDecl (&Spec, &Decl, DM_NEED_IDENT);
616 if (Decl.Ident[0] != '\0') {
618 /* We have a name given. Search for the symbol */
619 SymEntry* Sym = FindLocalSym (Decl.Ident);
621 /* Found it, change the default type to the one given */
622 ChangeSymType (Sym, ParamTypeCvt (Decl.Type));
624 Error ("Unknown identifier: `%s'", Decl.Ident);
628 if (curtok == TOK_COMMA) {
636 /* Variable list must be semicolon terminated */
643 static void ParseAnsiParamList (FuncDesc* F)
644 /* Parse a new style (ANSI) parameter list */
647 while (curtok != TOK_RPAREN) {
653 /* Allow an ellipsis as last parameter */
654 if (curtok == TOK_ELLIPSIS) {
656 F->Flags |= FD_VARIADIC;
660 /* Read the declaration specifier */
661 ParseDeclSpec (&Spec, SC_AUTO, T_INT);
663 /* We accept only auto and register as storage class specifiers, but
664 * we ignore all this and use auto.
666 if ((Spec.StorageClass & SC_AUTO) == 0 &&
667 (Spec.StorageClass & SC_REGISTER) == 0) {
668 Error ("Illegal storage class");
670 Spec.StorageClass = SC_AUTO | SC_PARAM | SC_DEF;
672 /* Allow parameters without a name, but remember if we had some to
673 * eventually print an error message later.
675 ParseDecl (&Spec, &Decl, DM_ACCEPT_IDENT);
676 if (Decl.Ident[0] == '\0') {
678 /* Unnamed symbol. Generate a name that is not user accessible,
679 * then handle the symbol normal.
681 AnonName (Decl.Ident, "param");
682 F->Flags |= FD_UNNAMED_PARAMS;
684 /* Clear defined bit on nonames */
685 Spec.StorageClass &= ~SC_DEF;
688 /* Parse an attribute ### */
689 ParseAttribute (&Decl, &Attr);
691 /* Create a symbol table entry */
692 AddLocalSym (Decl.Ident, ParamTypeCvt (Decl.Type), Spec.StorageClass, 0);
694 /* Count arguments */
697 /* Check for more parameters */
698 if (curtok == TOK_COMMA) {
705 /* Skip right paren. We must explicitly check for one here, since some of
706 * the breaks above bail out without checking.
710 /* Check if this is a function definition */
711 if (curtok == TOK_LCURLY) {
712 /* Print an error if in strict ANSI mode and we have unnamed
715 if (ANSI && (F->Flags & FD_UNNAMED_PARAMS) != 0) {
716 Error ("Parameter name omitted");
723 static FuncDesc* ParseFuncDecl (void)
724 /* Parse the argument list of a function. */
729 /* Create a new function descriptor */
730 FuncDesc* F = NewFuncDesc ();
732 /* Enter a new lexical level */
733 EnterFunctionLevel ();
735 /* Check for several special parameter lists */
736 if (curtok == TOK_RPAREN) {
737 /* Parameter list is empty */
738 F->Flags |= (FD_EMPTY | FD_VARIADIC);
739 } else if (curtok == TOK_VOID && nxttok == TOK_RPAREN) {
740 /* Parameter list declared as void */
742 F->Flags |= FD_VOID_PARAM;
743 } else if (curtok == TOK_IDENT && (nxttok == TOK_COMMA || nxttok == TOK_RPAREN)) {
744 /* Old style (K&R) function. Assume variable param list. */
745 F->Flags |= (FD_OLDSTYLE | FD_VARIADIC);
749 if ((F->Flags & FD_OLDSTYLE) == 0) {
750 /* New style function */
751 ParseAnsiParamList (F);
753 /* Old style function */
754 ParseOldStyleParamList (F);
757 /* Assign offsets. If the function has a variable parameter list,
758 * there's one additional byte (the arg size).
760 Offs = (F->Flags & FD_VARIADIC)? 1 : 0;
761 Sym = GetSymTab()->SymTail;
763 unsigned Size = SizeOf (Sym->Type);
766 F->ParamSize += Size;
770 /* Leave the lexical level remembering the symbol tables */
771 RememberFunctionLevel (F);
773 /* Return the function descriptor */
779 static void Decl (Declaration* D, unsigned Mode)
780 /* Recursively process declarators. Build a type array in reverse order. */
783 if (curtok == TOK_STAR) {
786 /* Allow optional const or volatile qualifiers */
787 T |= OptionalQualifiers (T_QUAL_NONE);
791 } else if (curtok == TOK_LPAREN) {
795 } else if (curtok == TOK_FASTCALL) {
796 /* Remember the current type pointer */
798 /* Skip the fastcall token */
800 /* Parse the function */
802 /* Set the fastcall flag */
803 if (!IsTypeFunc (T)) {
804 Error ("__fastcall__ modifier applied to non function");
805 } else if (IsVariadicFunc (T)) {
806 Error ("Cannot apply __fastcall__ to functions with variable parameter list");
808 FuncDesc* F = (FuncDesc*) DecodePtr (T+1);
809 F->Flags |= FD_FASTCALL;
813 /* Things depend on Mode now:
814 * - Mode == DM_NEED_IDENT means:
815 * we *must* have a type and a variable identifer.
816 * - Mode == DM_NO_IDENT means:
817 * we must have a type but no variable identifer
818 * (if there is one, it's not read).
819 * - Mode == DM_ACCEPT_IDENT means:
820 * we *may* have an identifier. If there is an identifier,
821 * it is read, but it is no error, if there is none.
823 if (Mode == DM_NO_IDENT) {
825 } else if (curtok == TOK_IDENT) {
826 strcpy (D->Ident, CurTok.Ident);
829 if (Mode == DM_NEED_IDENT) {
830 Error ("Identifier expected");
837 while (curtok == TOK_LBRACK || curtok == TOK_LPAREN) {
838 if (curtok == TOK_LPAREN) {
839 /* Function declaration */
842 /* Parse the function declaration */
843 F = ParseFuncDecl ();
848 /* Array declaration */
849 unsigned long Size = 0;
851 /* Read the size if it is given */
852 if (curtok != TOK_RBRACK) {
867 /*****************************************************************************/
869 /*****************************************************************************/
873 type* ParseType (type* Type)
874 /* Parse a complete type specification */
879 /* Get a type without a default */
880 InitDeclSpec (&Spec);
881 ParseTypeSpec (&Spec, -1);
883 /* Parse additional declarators */
884 InitDeclaration (&Decl);
885 ParseDecl (&Spec, &Decl, DM_NO_IDENT);
887 /* Copy the type to the target buffer */
888 TypeCpy (Type, Decl.Type);
890 /* Return a pointer to the target buffer */
896 void ParseDecl (const DeclSpec* Spec, Declaration* D, unsigned Mode)
897 /* Parse a variable, type or function declaration */
899 /* Initialize the Declaration struct */
902 /* Get additional declarators and the identifier */
905 /* Add the base type. */
906 TypeCpy (D->T, Spec->Type);
908 /* Check the size of the generated type */
909 if (!IsTypeFunc (D->Type) && !IsTypeVoid (D->Type) && SizeOf (D->Type) >= 0x10000) {
910 if (D->Ident[0] != '\0') {
911 Error ("Size of `%s' is invalid", D->Ident);
913 Error ("Invalid size");
920 void ParseDeclSpec (DeclSpec* D, unsigned DefStorage, int DefType)
921 /* Parse a declaration specification */
923 /* Initialize the DeclSpec struct */
926 /* First, get the storage class specifier for this declaration */
927 ParseStorageClass (D, DefStorage);
929 /* Parse the type specifiers */
930 ParseTypeSpec (D, DefType);
935 void CheckEmptyDecl (const DeclSpec* D)
936 /* Called after an empty type declaration (that is, a type declaration without
937 * a variable). Checks if the declaration does really make sense and issues a
941 if ((D->Flags & DS_EXTRA_TYPE) == 0) {
942 Warning ("Useless declaration");
948 static void ParseVoidInit (void)
949 /* Parse an initialization of a void variable (special cc65 extension) */
953 /* Allow an arbitrary list of values */
957 switch (lval.e_tptr[0]) {
961 if ((lval.e_flags & E_MCTYPE) == E_TCONST) {
962 /* Make it byte sized */
963 lval.e_const &= 0xFF;
974 if ((lval.e_flags & E_MCTYPE) == E_TCONST) {
975 /* Make it word sized */
976 lval.e_const &= 0xFFFF;
987 Error ("Illegal type in initialization");
992 if (curtok != TOK_COMMA) {
997 } while (curtok != TOK_RCURLY);
1004 static void ParseStructInit (type* Type)
1005 /* Parse initialization of a struct or union */
1010 /* Consume the opening curly brace */
1013 /* Get a pointer to the struct entry from the type */
1014 Entry = (SymEntry*) Decode (Type + 1);
1016 /* Check if this struct definition has a field table. If it doesn't, it
1017 * is an incomplete definition.
1019 Tab = Entry->V.S.SymTab;
1021 Error ("Cannot initialize variables with incomplete type");
1022 /* Returning here will cause lots of errors, but recovery is difficult */
1026 /* Get a pointer to the list of symbols */
1027 Entry = Tab->SymHead;
1028 while (curtok != TOK_RCURLY) {
1030 Error ("Too many initializers");
1033 ParseInit (Entry->Type);
1034 Entry = Entry->NextSym;
1035 if (curtok != TOK_COMMA)
1040 /* Consume the closing curly brace */
1043 /* If there are struct fields left, reserve additional storage */
1045 g_zerobytes (SizeOf (Entry->Type));
1046 Entry = Entry->NextSym;
1052 void ParseInit (type* T)
1053 /* Parse initialization of variables. */
1061 switch (UnqualifiedType (*T)) {
1066 if ((lval.e_flags & E_MCTYPE) == E_TCONST) {
1067 /* Make it byte sized */
1068 lval.e_const &= 0xFF;
1070 assignadjust (T, &lval);
1080 if ((lval.e_flags & E_MCTYPE) == E_TCONST) {
1081 /* Make it word sized */
1082 lval.e_const &= 0xFFFF;
1084 assignadjust (T, &lval);
1091 if ((lval.e_flags & E_MCTYPE) == E_TCONST) {
1092 /* Make it long sized */
1093 lval.e_const &= 0xFFFFFFFF;
1095 assignadjust (T, &lval);
1100 Size = Decode (T + 1);
1101 t = T + DECODE_SIZE + 1;
1102 if (IsTypeChar(t) && curtok == TOK_SCONST) {
1103 str = GetLiteral (curval);
1104 Count = strlen (str) + 1;
1105 TranslateLiteralPool (curval); /* Translate into target charset */
1106 g_defbytes (str, Count);
1107 ResetLiteralPoolOffs (curval); /* Remove string from pool */
1112 while (curtok != TOK_RCURLY) {
1113 ParseInit (T + DECODE_SIZE + 1);
1115 if (curtok != TOK_COMMA)
1122 Encode (T + 1, Count);
1123 } else if (Count < Size) {
1124 g_zerobytes ((Size - Count) * SizeOf (T + DECODE_SIZE + 1));
1125 } else if (Count > Size) {
1126 Error ("Too many initializers");
1132 ParseStructInit (T);
1137 /* Special cc65 extension in non ANSI mode */
1144 Error ("Illegal type");