1 /*****************************************************************************/
5 /* Parse variable and function declarations */
9 /* (C) 1998-2002 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 == 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. */
260 if (CurTok.Tok != 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 != 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 /* Get the offset of this field */
301 Offs = (StructType == T_STRUCT)? Size : 0;
303 /* Add a field entry to the table */
304 AddLocalSym (Decl.Ident, Decl.Type, SC_STRUCTFIELD, Offs);
306 /* Calculate offset of next field/size of the union */
307 Offs = CheckedSizeOf (Decl.Type);
308 if (StructType == T_STRUCT) {
316 if (CurTok.Tok != TOK_COMMA)
323 /* Skip the closing brace */
326 /* Remember the symbol table and leave the struct level */
327 FieldTab = GetSymTab ();
330 /* Make a real entry from the forward decl and return it */
331 return AddStructSym (Name, Size, FieldTab);
336 static void ParseTypeSpec (DeclSpec* D, int Default)
337 /* Parse a type specificier */
342 type Qualifiers; /* Type qualifiers */
344 /* Assume we have an explicit type */
345 D->Flags &= ~DS_DEF_TYPE;
347 /* Read type qualifiers if we have any */
348 Qualifiers = OptionalQualifiers (T_QUAL_NONE);
350 /* Look at the data type */
351 switch (CurTok.Tok) {
361 D->Type[0] = GetDefaultChar();
367 if (CurTok.Tok == TOK_UNSIGNED) {
370 D->Type[0] = T_ULONG;
382 if (CurTok.Tok == TOK_UNSIGNED) {
385 D->Type[0] = T_USHORT;
390 D->Type[0] = T_SHORT;
403 switch (CurTok.Tok) {
407 D->Type[0] = T_SCHAR;
414 D->Type[0] = T_SHORT;
438 switch (CurTok.Tok) {
442 D->Type[0] = T_UCHAR;
449 D->Type[0] = T_USHORT;
456 D->Type[0] = T_ULONG;
473 StructType = (CurTok.Tok == TOK_STRUCT)? T_STRUCT : T_UNION;
476 if (CurTok.Tok == TOK_IDENT) {
477 strcpy (Ident, CurTok.Ident);
480 AnonName (Ident, (StructType == T_STRUCT)? "struct" : "union");
482 /* Remember we have an extra type decl */
483 D->Flags |= DS_EXTRA_TYPE;
484 /* Declare the struct in the current scope */
485 Entry = ParseStructDecl (Ident, StructType);
486 /* Encode the struct entry into the type */
487 D->Type[0] = StructType;
488 EncodePtr (D->Type+1, Entry);
489 D->Type[DECODE_SIZE+1] = T_END;
494 if (CurTok.Tok != TOK_LCURLY) {
496 if (CurTok.Tok == TOK_IDENT) {
497 /* Find an entry with this name */
498 Entry = FindTagSym (CurTok.Ident);
500 if (SymIsLocal (Entry) && (Entry->Flags & SC_ENUM) == 0) {
501 Error ("Symbol `%s' is already different kind", Entry->Name);
504 /* Insert entry into table ### */
506 /* Skip the identifier */
509 Error ("Identifier expected");
512 /* Remember we have an extra type decl */
513 D->Flags |= DS_EXTRA_TYPE;
514 /* Parse the enum decl */
521 Entry = FindSym (CurTok.Ident);
522 if (Entry && SymIsTypeDef (Entry)) {
525 TypeCpy (D->Type, Entry->Type);
532 Error ("Type expected");
536 D->Flags |= DS_DEF_TYPE;
537 D->Type[0] = (type) Default;
543 /* There may also be qualifiers *after* the initial type */
544 D->Type[0] |= OptionalQualifiers (Qualifiers);
549 static type* ParamTypeCvt (type* T)
550 /* If T is an array, convert it to a pointer else do nothing. Return the
554 if (IsTypeArray (T)) {
563 static void ParseOldStyleParamList (FuncDesc* F)
564 /* Parse an old style (K&R) parameter list */
567 while (CurTok.Tok != TOK_RPAREN) {
569 /* List of identifiers expected */
570 if (CurTok.Tok != TOK_IDENT) {
571 Error ("Identifier expected");
574 /* Create a symbol table entry with type int */
575 AddLocalSym (CurTok.Ident, type_int, SC_AUTO | SC_PARAM | SC_DEF, 0);
577 /* Count arguments */
580 /* Skip the identifier */
583 /* Check for more parameters */
584 if (CurTok.Tok == TOK_COMMA) {
591 /* Skip right paren. We must explicitly check for one here, since some of
592 * the breaks above bail out without checking.
596 /* An optional list of type specifications follows */
597 while (CurTok.Tok != TOK_LCURLY) {
601 /* Read the declaration specifier */
602 ParseDeclSpec (&Spec, SC_AUTO, T_INT);
604 /* We accept only auto and register as storage class specifiers, but
605 * we ignore all this, since we use auto anyway.
607 if ((Spec.StorageClass & SC_AUTO) == 0 &&
608 (Spec.StorageClass & SC_REGISTER) == 0) {
609 Error ("Illegal storage class");
612 /* Parse a comma separated variable list */
617 /* Read the parameter */
618 ParseDecl (&Spec, &Decl, DM_NEED_IDENT);
619 if (Decl.Ident[0] != '\0') {
621 /* We have a name given. Search for the symbol */
622 SymEntry* Sym = FindLocalSym (Decl.Ident);
624 /* Found it, change the default type to the one given */
625 ChangeSymType (Sym, ParamTypeCvt (Decl.Type));
627 Error ("Unknown identifier: `%s'", Decl.Ident);
631 if (CurTok.Tok == TOK_COMMA) {
639 /* Variable list must be semicolon terminated */
646 static void ParseAnsiParamList (FuncDesc* F)
647 /* Parse a new style (ANSI) parameter list */
650 while (CurTok.Tok != TOK_RPAREN) {
656 /* Allow an ellipsis as last parameter */
657 if (CurTok.Tok == TOK_ELLIPSIS) {
659 F->Flags |= FD_VARIADIC;
663 /* Read the declaration specifier */
664 ParseDeclSpec (&Spec, SC_AUTO, T_INT);
666 /* We accept only auto and register as storage class specifiers, but
667 * we ignore all this and use auto.
669 if ((Spec.StorageClass & SC_AUTO) == 0 &&
670 (Spec.StorageClass & SC_REGISTER) == 0) {
671 Error ("Illegal storage class");
673 Spec.StorageClass = SC_AUTO | SC_PARAM | SC_DEF;
675 /* Allow parameters without a name, but remember if we had some to
676 * eventually print an error message later.
678 ParseDecl (&Spec, &Decl, DM_ACCEPT_IDENT);
679 if (Decl.Ident[0] == '\0') {
681 /* Unnamed symbol. Generate a name that is not user accessible,
682 * then handle the symbol normal.
684 AnonName (Decl.Ident, "param");
685 F->Flags |= FD_UNNAMED_PARAMS;
687 /* Clear defined bit on nonames */
688 Spec.StorageClass &= ~SC_DEF;
691 /* Parse an attribute ### */
692 ParseAttribute (&Decl, &Attr);
694 /* Create a symbol table entry */
695 AddLocalSym (Decl.Ident, ParamTypeCvt (Decl.Type), Spec.StorageClass, 0);
697 /* Count arguments */
700 /* Check for more parameters */
701 if (CurTok.Tok == TOK_COMMA) {
708 /* Skip right paren. We must explicitly check for one here, since some of
709 * the breaks above bail out without checking.
713 /* Check if this is a function definition */
714 if (CurTok.Tok == TOK_LCURLY) {
715 /* Print an error if in strict ANSI mode and we have unnamed
718 if (ANSI && (F->Flags & FD_UNNAMED_PARAMS) != 0) {
719 Error ("Parameter name omitted");
726 static FuncDesc* ParseFuncDecl (const DeclSpec* Spec)
727 /* Parse the argument list of a function. */
732 /* Create a new function descriptor */
733 FuncDesc* F = NewFuncDesc ();
735 /* Enter a new lexical level */
736 EnterFunctionLevel ();
738 /* Check for several special parameter lists */
739 if (CurTok.Tok == TOK_RPAREN) {
740 /* Parameter list is empty */
741 F->Flags |= (FD_EMPTY | FD_VARIADIC);
742 } else if (CurTok.Tok == TOK_VOID && NextTok.Tok == TOK_RPAREN) {
743 /* Parameter list declared as void */
745 F->Flags |= FD_VOID_PARAM;
746 } else if (CurTok.Tok == TOK_IDENT &&
747 (NextTok.Tok == TOK_COMMA || NextTok.Tok == TOK_RPAREN)) {
748 /* If the identifier is a typedef, we have a new style parameter list,
749 * if it's some other identifier, it's an old style parameter list.
751 Sym = FindSym (CurTok.Ident);
752 if (Sym == 0 || !SymIsTypeDef (Sym)) {
753 /* Old style (K&R) function. Assume variable param list. */
754 F->Flags |= (FD_OLDSTYLE | FD_VARIADIC);
756 /* Check for an implicit int return in the K&R function */
757 if ((Spec->Flags & DS_DEF_TYPE) != 0 &&
758 Spec->Type[0] == T_INT &&
759 Spec->Type[1] == T_END) {
760 /* Function has an implicit int return */
761 F->Flags |= FD_OLDSTYLE_INTRET;
767 if ((F->Flags & FD_OLDSTYLE) == 0) {
768 /* New style function */
769 ParseAnsiParamList (F);
771 /* Old style function */
772 ParseOldStyleParamList (F);
775 /* Assign offsets. If the function has a variable parameter list,
776 * there's one additional byte (the arg size).
778 Offs = (F->Flags & FD_VARIADIC)? 1 : 0;
779 Sym = GetSymTab()->SymTail;
781 unsigned Size = CheckedSizeOf (Sym->Type);
784 F->ParamSize += Size;
788 /* Leave the lexical level remembering the symbol tables */
789 RememberFunctionLevel (F);
791 /* Return the function descriptor */
797 static void Decl (const DeclSpec* Spec, Declaration* D, unsigned Mode)
798 /* Recursively process declarators. Build a type array in reverse order. */
801 if (CurTok.Tok == TOK_STAR) {
804 /* Allow optional const or volatile qualifiers */
805 T |= OptionalQualifiers (T_QUAL_NONE);
806 Decl (Spec, D, Mode);
809 } else if (CurTok.Tok == TOK_LPAREN) {
811 Decl (Spec, D, Mode);
813 } else if (CurTok.Tok == TOK_FASTCALL) {
814 /* Remember the current type pointer */
816 /* Skip the fastcall token */
818 /* Parse the function */
819 Decl (Spec, D, Mode);
820 /* Set the fastcall flag */
821 if (!IsTypeFunc (T) && !IsTypeFuncPtr (T)) {
822 Error ("__fastcall__ modifier applied to non function");
823 } else if (IsVariadicFunc (T)) {
824 Error ("Cannot apply __fastcall__ to functions with variable parameter list");
826 FuncDesc* F = GetFuncDesc (T);
827 F->Flags |= FD_FASTCALL;
831 /* Things depend on Mode now:
832 * - Mode == DM_NEED_IDENT means:
833 * we *must* have a type and a variable identifer.
834 * - Mode == DM_NO_IDENT means:
835 * we must have a type but no variable identifer
836 * (if there is one, it's not read).
837 * - Mode == DM_ACCEPT_IDENT means:
838 * we *may* have an identifier. If there is an identifier,
839 * it is read, but it is no error, if there is none.
841 if (Mode == DM_NO_IDENT) {
843 } else if (CurTok.Tok == TOK_IDENT) {
844 strcpy (D->Ident, CurTok.Ident);
847 if (Mode == DM_NEED_IDENT) {
848 Error ("Identifier expected");
854 while (CurTok.Tok == TOK_LBRACK || CurTok.Tok == TOK_LPAREN) {
855 if (CurTok.Tok == TOK_LPAREN) {
856 /* Function declaration */
859 /* Parse the function declaration */
860 F = ParseFuncDecl (Spec);
865 /* Array declaration */
866 unsigned long Size = 0;
868 /* Read the size if it is given */
869 if (CurTok.Tok != TOK_RBRACK) {
872 Size = lval.ConstVal;
884 /*****************************************************************************/
886 /*****************************************************************************/
890 type* ParseType (type* Type)
891 /* Parse a complete type specification */
896 /* Get a type without a default */
897 InitDeclSpec (&Spec);
898 ParseTypeSpec (&Spec, -1);
900 /* Parse additional declarators */
901 InitDeclaration (&Decl);
902 ParseDecl (&Spec, &Decl, DM_NO_IDENT);
904 /* Copy the type to the target buffer */
905 TypeCpy (Type, Decl.Type);
907 /* Return a pointer to the target buffer */
913 void ParseDecl (const DeclSpec* Spec, Declaration* D, unsigned Mode)
914 /* Parse a variable, type or function declaration */
916 /* Initialize the Declaration struct */
919 /* Get additional declarators and the identifier */
920 Decl (Spec, D, Mode);
922 /* Add the base type. */
923 TypeCpy (D->T, Spec->Type);
925 /* Check the size of the generated type */
926 if (!IsTypeFunc (D->Type) && !IsTypeVoid (D->Type) && SizeOf (D->Type) >= 0x10000) {
927 if (D->Ident[0] != '\0') {
928 Error ("Size of `%s' is invalid", D->Ident);
930 Error ("Invalid size");
937 void ParseDeclSpec (DeclSpec* D, unsigned DefStorage, int DefType)
938 /* Parse a declaration specification */
940 /* Initialize the DeclSpec struct */
943 /* First, get the storage class specifier for this declaration */
944 ParseStorageClass (D, DefStorage);
946 /* Parse the type specifiers */
947 ParseTypeSpec (D, DefType);
952 void CheckEmptyDecl (const DeclSpec* D)
953 /* Called after an empty type declaration (that is, a type declaration without
954 * a variable). Checks if the declaration does really make sense and issues a
958 if ((D->Flags & DS_EXTRA_TYPE) == 0) {
959 Warning ("Useless declaration");
965 static void ParseVoidInit (void)
966 /* Parse an initialization of a void variable (special cc65 extension) */
970 /* Allow an arbitrary list of values */
974 switch (lval.Type[0]) {
978 if ((lval.Flags & E_MCTYPE) == E_TCONST) {
979 /* Make it byte sized */
980 lval.ConstVal &= 0xFF;
991 if ((lval.Flags & E_MCTYPE) == E_TCONST) {
992 /* Make it word sized */
993 lval.ConstVal &= 0xFFFF;
1004 Error ("Illegal type in initialization");
1009 if (CurTok.Tok != TOK_COMMA) {
1014 } while (CurTok.Tok != TOK_RCURLY);
1021 static void ParseStructInit (type* Type)
1022 /* Parse initialization of a struct or union */
1027 /* Consume the opening curly brace */
1030 /* Get a pointer to the struct entry from the type */
1031 Entry = (SymEntry*) Decode (Type + 1);
1033 /* Check if this struct definition has a field table. If it doesn't, it
1034 * is an incomplete definition.
1036 Tab = Entry->V.S.SymTab;
1038 Error ("Cannot initialize variables with incomplete type");
1039 /* Returning here will cause lots of errors, but recovery is difficult */
1043 /* Get a pointer to the list of symbols */
1044 Entry = Tab->SymHead;
1045 while (CurTok.Tok != TOK_RCURLY) {
1047 Error ("Too many initializers");
1050 ParseInit (Entry->Type);
1051 Entry = Entry->NextSym;
1052 if (CurTok.Tok != TOK_COMMA)
1057 /* Consume the closing curly brace */
1060 /* If there are struct fields left, reserve additional storage */
1062 g_zerobytes (CheckedSizeOf (Entry->Type));
1063 Entry = Entry->NextSym;
1069 void ParseInit (type* T)
1070 /* Parse initialization of variables. */
1078 switch (UnqualifiedType (*T)) {
1083 if ((lval.Flags & E_MCTYPE) == E_TCONST) {
1084 /* Make it byte sized */
1085 lval.ConstVal &= 0xFF;
1087 assignadjust (T, &lval);
1097 if ((lval.Flags & E_MCTYPE) == E_TCONST) {
1098 /* Make it word sized */
1099 lval.ConstVal &= 0xFFFF;
1101 assignadjust (T, &lval);
1108 if ((lval.Flags & E_MCTYPE) == E_TCONST) {
1109 /* Make it long sized */
1110 lval.ConstVal &= 0xFFFFFFFF;
1112 assignadjust (T, &lval);
1117 Size = Decode (T + 1);
1118 t = T + DECODE_SIZE + 1;
1119 if (IsTypeChar(t) && CurTok.Tok == TOK_SCONST) {
1120 str = GetLiteral (CurTok.IVal);
1121 Count = strlen (str) + 1;
1122 TranslateLiteralPool (CurTok.IVal); /* Translate into target charset */
1123 g_defbytes (str, Count);
1124 ResetLiteralPoolOffs (CurTok.IVal); /* Remove string from pool */
1129 while (CurTok.Tok != TOK_RCURLY) {
1130 ParseInit (T + DECODE_SIZE + 1);
1132 if (CurTok.Tok != TOK_COMMA)
1139 Encode (T + 1, Count);
1140 } else if (Count < Size) {
1141 g_zerobytes ((Size - Count) * CheckedSizeOf (T + DECODE_SIZE + 1));
1142 } else if (Count > Size) {
1143 Error ("Too many initializers");
1149 ParseStructInit (T);
1154 /* Special cc65 extension in non ANSI mode */
1161 Error ("Illegal type");