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 */
667 if ((Spec.StorageClass & SC_AUTO) == SC_AUTO) {
668 Spec.StorageClass = SC_AUTO | SC_PARAM | SC_DEF;
669 } else if ((Spec.StorageClass & SC_REGISTER) == SC_REGISTER) {
670 Spec.StorageClass = SC_REGISTER | SC_STATIC | SC_PARAM | SC_DEF;
672 Error ("Illegal storage class");
673 Spec.StorageClass = SC_AUTO | SC_PARAM | SC_DEF;
676 /* Allow parameters without a name, but remember if we had some to
677 * eventually print an error message later.
679 ParseDecl (&Spec, &Decl, DM_ACCEPT_IDENT);
680 if (Decl.Ident[0] == '\0') {
682 /* Unnamed symbol. Generate a name that is not user accessible,
683 * then handle the symbol normal.
685 AnonName (Decl.Ident, "param");
686 F->Flags |= FD_UNNAMED_PARAMS;
688 /* Clear defined bit on nonames */
689 Spec.StorageClass &= ~SC_DEF;
692 /* Parse an attribute ### */
693 ParseAttribute (&Decl, &Attr);
695 /* Create a symbol table entry */
696 AddLocalSym (Decl.Ident, ParamTypeCvt (Decl.Type), Spec.StorageClass, 0);
698 /* Count arguments */
701 /* Check for more parameters */
702 if (CurTok.Tok == TOK_COMMA) {
709 /* Skip right paren. We must explicitly check for one here, since some of
710 * the breaks above bail out without checking.
714 /* Check if this is a function definition */
715 if (CurTok.Tok == TOK_LCURLY) {
716 /* Print an error if in strict ANSI mode and we have unnamed
719 if (ANSI && (F->Flags & FD_UNNAMED_PARAMS) != 0) {
720 Error ("Parameter name omitted");
727 static FuncDesc* ParseFuncDecl (const DeclSpec* Spec)
728 /* Parse the argument list of a function. */
733 /* Create a new function descriptor */
734 FuncDesc* F = NewFuncDesc ();
736 /* Enter a new lexical level */
737 EnterFunctionLevel ();
739 /* Check for several special parameter lists */
740 if (CurTok.Tok == TOK_RPAREN) {
741 /* Parameter list is empty */
742 F->Flags |= (FD_EMPTY | FD_VARIADIC);
743 } else if (CurTok.Tok == TOK_VOID && NextTok.Tok == TOK_RPAREN) {
744 /* Parameter list declared as void */
746 F->Flags |= FD_VOID_PARAM;
747 } else if (CurTok.Tok == TOK_IDENT &&
748 (NextTok.Tok == TOK_COMMA || NextTok.Tok == TOK_RPAREN)) {
749 /* If the identifier is a typedef, we have a new style parameter list,
750 * if it's some other identifier, it's an old style parameter list.
752 Sym = FindSym (CurTok.Ident);
753 if (Sym == 0 || !SymIsTypeDef (Sym)) {
754 /* Old style (K&R) function. Assume variable param list. */
755 F->Flags |= (FD_OLDSTYLE | FD_VARIADIC);
759 /* Check for an implicit int return in the function */
760 if ((Spec->Flags & DS_DEF_TYPE) != 0 &&
761 Spec->Type[0] == T_INT &&
762 Spec->Type[1] == T_END) {
763 /* Function has an implicit int return */
764 F->Flags |= FD_OLDSTYLE_INTRET;
768 if ((F->Flags & FD_OLDSTYLE) == 0) {
769 /* New style function */
770 ParseAnsiParamList (F);
772 /* Old style function */
773 ParseOldStyleParamList (F);
776 /* Assign offsets. If the function has a variable parameter list,
777 * there's one additional byte (the arg size).
779 Offs = (F->Flags & FD_VARIADIC)? 1 : 0;
780 Sym = GetSymTab()->SymTail;
782 unsigned Size = CheckedSizeOf (Sym->Type);
783 if (SymIsRegVar (Sym)) {
784 Sym->V.R.SaveOffs = Offs;
789 F->ParamSize += Size;
793 /* Leave the lexical level remembering the symbol tables */
794 RememberFunctionLevel (F);
796 /* Return the function descriptor */
802 static void Decl (const DeclSpec* Spec, Declaration* D, unsigned Mode)
803 /* Recursively process declarators. Build a type array in reverse order. */
806 if (CurTok.Tok == TOK_STAR) {
809 /* Allow optional const or volatile qualifiers */
810 T |= OptionalQualifiers (T_QUAL_NONE);
811 Decl (Spec, D, Mode);
814 } else if (CurTok.Tok == TOK_LPAREN) {
816 Decl (Spec, D, Mode);
818 } else if (CurTok.Tok == TOK_FASTCALL) {
819 /* Remember the current type pointer */
821 /* Skip the fastcall token */
823 /* Parse the function */
824 Decl (Spec, D, Mode);
825 /* Set the fastcall flag */
826 if (!IsTypeFunc (T) && !IsTypeFuncPtr (T)) {
827 Error ("__fastcall__ modifier applied to non function");
828 } else if (IsVariadicFunc (T)) {
829 Error ("Cannot apply __fastcall__ to functions with variable parameter list");
831 FuncDesc* F = GetFuncDesc (T);
832 F->Flags |= FD_FASTCALL;
836 /* Things depend on Mode now:
837 * - Mode == DM_NEED_IDENT means:
838 * we *must* have a type and a variable identifer.
839 * - Mode == DM_NO_IDENT means:
840 * we must have a type but no variable identifer
841 * (if there is one, it's not read).
842 * - Mode == DM_ACCEPT_IDENT means:
843 * we *may* have an identifier. If there is an identifier,
844 * it is read, but it is no error, if there is none.
846 if (Mode == DM_NO_IDENT) {
848 } else if (CurTok.Tok == TOK_IDENT) {
849 strcpy (D->Ident, CurTok.Ident);
852 if (Mode == DM_NEED_IDENT) {
853 Error ("Identifier expected");
859 while (CurTok.Tok == TOK_LBRACK || CurTok.Tok == TOK_LPAREN) {
860 if (CurTok.Tok == TOK_LPAREN) {
861 /* Function declaration */
864 /* Parse the function declaration */
865 F = ParseFuncDecl (Spec);
870 /* Array declaration */
871 unsigned long Size = 0;
873 /* Read the size if it is given */
874 if (CurTok.Tok != TOK_RBRACK) {
877 Size = lval.ConstVal;
889 /*****************************************************************************/
891 /*****************************************************************************/
895 type* ParseType (type* Type)
896 /* Parse a complete type specification */
901 /* Get a type without a default */
902 InitDeclSpec (&Spec);
903 ParseTypeSpec (&Spec, -1);
905 /* Parse additional declarators */
906 InitDeclaration (&Decl);
907 ParseDecl (&Spec, &Decl, DM_NO_IDENT);
909 /* Copy the type to the target buffer */
910 TypeCpy (Type, Decl.Type);
912 /* Return a pointer to the target buffer */
918 void ParseDecl (const DeclSpec* Spec, Declaration* D, unsigned Mode)
919 /* Parse a variable, type or function declaration */
921 /* Initialize the Declaration struct */
924 /* Get additional declarators and the identifier */
925 Decl (Spec, D, Mode);
927 /* Add the base type. */
928 TypeCpy (D->T, Spec->Type);
930 /* Check the size of the generated type */
931 if (!IsTypeFunc (D->Type) && !IsTypeVoid (D->Type) && SizeOf (D->Type) >= 0x10000) {
932 if (D->Ident[0] != '\0') {
933 Error ("Size of `%s' is invalid", D->Ident);
935 Error ("Invalid size");
942 void ParseDeclSpec (DeclSpec* D, unsigned DefStorage, int DefType)
943 /* Parse a declaration specification */
945 /* Initialize the DeclSpec struct */
948 /* First, get the storage class specifier for this declaration */
949 ParseStorageClass (D, DefStorage);
951 /* Parse the type specifiers */
952 ParseTypeSpec (D, DefType);
957 void CheckEmptyDecl (const DeclSpec* D)
958 /* Called after an empty type declaration (that is, a type declaration without
959 * a variable). Checks if the declaration does really make sense and issues a
963 if ((D->Flags & DS_EXTRA_TYPE) == 0) {
964 Warning ("Useless declaration");
970 static void ParseVoidInit (void)
971 /* Parse an initialization of a void variable (special cc65 extension) */
975 /* Allow an arbitrary list of values */
979 switch (lval.Type[0]) {
983 if ((lval.Flags & E_MCTYPE) == E_TCONST) {
984 /* Make it byte sized */
985 lval.ConstVal &= 0xFF;
996 if ((lval.Flags & E_MCTYPE) == E_TCONST) {
997 /* Make it word sized */
998 lval.ConstVal &= 0xFFFF;
1009 Error ("Illegal type in initialization");
1014 if (CurTok.Tok != TOK_COMMA) {
1019 } while (CurTok.Tok != TOK_RCURLY);
1026 static void ParseStructInit (type* Type)
1027 /* Parse initialization of a struct or union */
1032 /* Consume the opening curly brace */
1035 /* Get a pointer to the struct entry from the type */
1036 Entry = (SymEntry*) Decode (Type + 1);
1038 /* Check if this struct definition has a field table. If it doesn't, it
1039 * is an incomplete definition.
1041 Tab = Entry->V.S.SymTab;
1043 Error ("Cannot initialize variables with incomplete type");
1044 /* Returning here will cause lots of errors, but recovery is difficult */
1048 /* Get a pointer to the list of symbols */
1049 Entry = Tab->SymHead;
1050 while (CurTok.Tok != TOK_RCURLY) {
1052 Error ("Too many initializers");
1055 ParseInit (Entry->Type);
1056 Entry = Entry->NextSym;
1057 if (CurTok.Tok != TOK_COMMA)
1062 /* Consume the closing curly brace */
1065 /* If there are struct fields left, reserve additional storage */
1067 g_zerobytes (CheckedSizeOf (Entry->Type));
1068 Entry = Entry->NextSym;
1074 void ParseInit (type* T)
1075 /* Parse initialization of variables. */
1083 switch (UnqualifiedType (*T)) {
1088 if ((lval.Flags & E_MCTYPE) == E_TCONST) {
1089 /* Make it byte sized */
1090 lval.ConstVal &= 0xFF;
1092 assignadjust (T, &lval);
1102 if ((lval.Flags & E_MCTYPE) == E_TCONST) {
1103 /* Make it word sized */
1104 lval.ConstVal &= 0xFFFF;
1106 assignadjust (T, &lval);
1113 if ((lval.Flags & E_MCTYPE) == E_TCONST) {
1114 /* Make it long sized */
1115 lval.ConstVal &= 0xFFFFFFFF;
1117 assignadjust (T, &lval);
1122 Size = Decode (T + 1);
1123 t = T + DECODE_SIZE + 1;
1124 if (IsTypeChar(t) && CurTok.Tok == TOK_SCONST) {
1125 str = GetLiteral (CurTok.IVal);
1126 Count = strlen (str) + 1;
1127 TranslateLiteralPool (CurTok.IVal); /* Translate into target charset */
1128 g_defbytes (str, Count);
1129 ResetLiteralPoolOffs (CurTok.IVal); /* Remove string from pool */
1134 while (CurTok.Tok != TOK_RCURLY) {
1135 ParseInit (T + DECODE_SIZE + 1);
1137 if (CurTok.Tok != TOK_COMMA)
1144 Encode (T + 1, Count);
1145 } else if (Count < Size) {
1146 g_zerobytes ((Size - Count) * CheckedSizeOf (T + DECODE_SIZE + 1));
1147 } else if (Count > Size) {
1148 Error ("Too many initializers");
1154 ParseStructInit (T);
1159 /* Special cc65 extension in non ANSI mode */
1166 Error ("Illegal type");