2001-03-26 21:57:07 +00:00
|
|
|
/*****************************************************************************/
|
|
|
|
/* */
|
|
|
|
/* declare.c */
|
|
|
|
/* */
|
|
|
|
/* Parse variable and function declarations */
|
|
|
|
/* */
|
|
|
|
/* */
|
|
|
|
/* */
|
2008-08-01 21:40:07 +00:00
|
|
|
/* (C) 1998-2008 Ullrich von Bassewitz */
|
|
|
|
/* Roemerstrasse 52 */
|
2003-02-01 12:39:12 +00:00
|
|
|
/* D-70794 Filderstadt */
|
2003-02-03 22:14:20 +00:00
|
|
|
/* EMail: uz@cc65.org */
|
2001-03-26 21:57:07 +00:00
|
|
|
/* */
|
|
|
|
/* */
|
|
|
|
/* This software is provided 'as-is', without any expressed or implied */
|
|
|
|
/* warranty. In no event will the authors be held liable for any damages */
|
|
|
|
/* arising from the use of this software. */
|
|
|
|
/* */
|
|
|
|
/* Permission is granted to anyone to use this software for any purpose, */
|
|
|
|
/* including commercial applications, and to alter it and redistribute it */
|
|
|
|
/* freely, subject to the following restrictions: */
|
|
|
|
/* */
|
|
|
|
/* 1. The origin of this software must not be misrepresented; you must not */
|
|
|
|
/* claim that you wrote the original software. If you use this software */
|
|
|
|
/* in a product, an acknowledgment in the product documentation would be */
|
|
|
|
/* appreciated but is not required. */
|
|
|
|
/* 2. Altered source versions must be plainly marked as such, and must not */
|
|
|
|
/* be misrepresented as being the original software. */
|
|
|
|
/* 3. This notice may not be removed or altered from any source */
|
|
|
|
/* distribution. */
|
|
|
|
/* */
|
|
|
|
/*****************************************************************************/
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
2000-08-01 18:45:08 +00:00
|
|
|
/* common */
|
2003-11-29 07:17:31 +00:00
|
|
|
#include "addrsize.h"
|
|
|
|
#include "mmodel.h"
|
2000-08-01 18:45:08 +00:00
|
|
|
#include "xmalloc.h"
|
2000-06-14 21:01:37 +00:00
|
|
|
|
2000-08-01 18:45:08 +00:00
|
|
|
/* cc65 */
|
2000-05-28 13:40:48 +00:00
|
|
|
#include "anonname.h"
|
|
|
|
#include "codegen.h"
|
|
|
|
#include "datatype.h"
|
2003-08-11 20:18:30 +00:00
|
|
|
#include "declare.h"
|
2000-09-14 19:26:13 +00:00
|
|
|
#include "declattr.h"
|
2000-05-28 13:40:48 +00:00
|
|
|
#include "error.h"
|
|
|
|
#include "expr.h"
|
|
|
|
#include "funcdesc.h"
|
|
|
|
#include "function.h"
|
|
|
|
#include "global.h"
|
|
|
|
#include "litpool.h"
|
|
|
|
#include "pragma.h"
|
|
|
|
#include "scanner.h"
|
2004-06-21 22:22:11 +00:00
|
|
|
#include "standard.h"
|
2000-05-28 13:40:48 +00:00
|
|
|
#include "symtab.h"
|
2003-08-11 20:18:30 +00:00
|
|
|
#include "typeconv.h"
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
/* Forwards */
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
2006-02-19 12:29:37 +00:00
|
|
|
static void ParseTypeSpec (DeclSpec* D, long Default, TypeCode Qualifiers);
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Parse a type specificier */
|
|
|
|
|
2006-02-19 12:29:37 +00:00
|
|
|
static unsigned ParseInitInternal (Type* T, int AllowFlexibleMembers);
|
2003-02-05 22:02:48 +00:00
|
|
|
/* Parse initialization of variables. Return the number of data bytes. */
|
|
|
|
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
/* internal functions */
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
2006-02-19 12:29:37 +00:00
|
|
|
static TypeCode OptionalQualifiers (TypeCode Q)
|
2000-08-01 18:45:08 +00:00
|
|
|
/* Read type qualifiers if we have any */
|
2000-05-28 13:40:48 +00:00
|
|
|
{
|
2004-05-26 16:15:28 +00:00
|
|
|
while (TokIsTypeQual (&CurTok)) {
|
2000-08-01 18:45:08 +00:00
|
|
|
|
2001-05-22 09:32:24 +00:00
|
|
|
switch (CurTok.Tok) {
|
2000-08-01 18:45:08 +00:00
|
|
|
|
|
|
|
case TOK_CONST:
|
2002-11-25 15:05:15 +00:00
|
|
|
if (Q & T_QUAL_CONST) {
|
2000-11-02 21:22:33 +00:00
|
|
|
Error ("Duplicate qualifier: `const'");
|
2001-01-05 19:24:47 +00:00
|
|
|
}
|
2000-08-01 18:45:08 +00:00
|
|
|
Q |= T_QUAL_CONST;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TOK_VOLATILE:
|
|
|
|
if (Q & T_QUAL_VOLATILE) {
|
2000-11-02 21:22:33 +00:00
|
|
|
Error ("Duplicate qualifier: `volatile'");
|
2000-08-01 18:45:08 +00:00
|
|
|
}
|
|
|
|
Q |= T_QUAL_VOLATILE;
|
|
|
|
break;
|
|
|
|
|
2006-02-17 20:19:35 +00:00
|
|
|
case TOK_RESTRICT:
|
|
|
|
if (Q & T_QUAL_RESTRICT) {
|
|
|
|
Error ("Duplicate qualifier: `restrict'");
|
|
|
|
}
|
|
|
|
Q |= T_QUAL_RESTRICT;
|
|
|
|
break;
|
|
|
|
|
2000-08-01 18:45:08 +00:00
|
|
|
default:
|
2004-05-26 16:15:28 +00:00
|
|
|
Internal ("Unexpected type qualifier token: %d", CurTok.Tok);
|
2000-08-01 18:45:08 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Skip the token */
|
|
|
|
NextToken ();
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
2000-08-01 18:45:08 +00:00
|
|
|
|
|
|
|
/* Return the qualifiers read */
|
|
|
|
return Q;
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2007-03-18 18:26:00 +00:00
|
|
|
static void OptionalInt (void)
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Eat an optional "int" token */
|
|
|
|
{
|
2001-05-22 09:32:24 +00:00
|
|
|
if (CurTok.Tok == TOK_INT) {
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Skip it */
|
2000-06-12 18:31:40 +00:00
|
|
|
NextToken ();
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2007-03-18 18:26:00 +00:00
|
|
|
static void OptionalSigned (void)
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Eat an optional "signed" token */
|
|
|
|
{
|
2001-05-22 09:32:24 +00:00
|
|
|
if (CurTok.Tok == TOK_SIGNED) {
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Skip it */
|
2000-06-12 18:31:40 +00:00
|
|
|
NextToken ();
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void InitDeclSpec (DeclSpec* D)
|
|
|
|
/* Initialize the DeclSpec struct for use */
|
|
|
|
{
|
2004-05-02 09:57:05 +00:00
|
|
|
D->StorageClass = 0;
|
2006-02-19 12:29:37 +00:00
|
|
|
D->Type[0].C = T_END;
|
2004-05-02 09:57:05 +00:00
|
|
|
D->Flags = 0;
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void InitDeclaration (Declaration* D)
|
|
|
|
/* Initialize the Declaration struct for use */
|
|
|
|
{
|
2006-02-19 12:29:37 +00:00
|
|
|
D->Ident[0] = '\0';
|
|
|
|
D->Type[0].C = T_END;
|
|
|
|
D->Index = 0;
|
2004-05-02 09:57:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void NeedTypeSpace (Declaration* D, unsigned Count)
|
|
|
|
/* Check if there is enough space for Count type specifiers within D */
|
|
|
|
{
|
|
|
|
if (D->Index + Count >= MAXTYPELEN) {
|
|
|
|
/* We must call Fatal() here, since calling Error() will try to
|
|
|
|
* continue, and the declaration type is not correctly terminated
|
|
|
|
* in case we come here.
|
|
|
|
*/
|
|
|
|
Fatal ("Too many type specifiers");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2006-02-19 12:29:37 +00:00
|
|
|
static void AddTypeToDeclaration (Declaration* D, TypeCode T)
|
2004-05-02 09:57:05 +00:00
|
|
|
/* Add a type specifier to the type of a declaration */
|
|
|
|
{
|
|
|
|
NeedTypeSpace (D, 1);
|
2006-02-19 12:29:37 +00:00
|
|
|
D->Type[D->Index++].C = T;
|
2004-05-02 09:57:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2006-02-19 12:29:37 +00:00
|
|
|
static void AddFuncTypeToDeclaration (Declaration* D, FuncDesc* F)
|
|
|
|
/* Add a function type plus function descriptor to the type of a declaration */
|
2004-05-02 09:57:05 +00:00
|
|
|
{
|
2006-02-19 12:29:37 +00:00
|
|
|
NeedTypeSpace (D, 1);
|
|
|
|
D->Type[D->Index].C = T_FUNC;
|
|
|
|
SetFuncDesc (D->Type + D->Index, F);
|
|
|
|
++D->Index;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void AddArrayToDeclaration (Declaration* D, long Size)
|
|
|
|
/* Add an array type plus size to the type of a declaration */
|
|
|
|
{
|
|
|
|
NeedTypeSpace (D, 1);
|
|
|
|
D->Type[D->Index].C = T_ARRAY;
|
|
|
|
D->Type[D->Index].A.L = Size;
|
|
|
|
++D->Index;
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
2004-06-10 11:02:05 +00:00
|
|
|
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2007-03-18 18:26:00 +00:00
|
|
|
static void FixArrayQualifiers (Type* T)
|
|
|
|
/* Using typedefs, it is possible to generate declarations that have
|
|
|
|
* type qualifiers attached to an array, not the element type. Go and
|
|
|
|
* fix these here.
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
TypeCode Q = T_QUAL_NONE;
|
|
|
|
while (T->C != T_END) {
|
|
|
|
if (IsTypeArray (T)) {
|
|
|
|
/* Extract any type qualifiers */
|
|
|
|
Q |= T->C & T_MASK_QUAL;
|
|
|
|
T->C = UnqualifiedType (T->C);
|
|
|
|
} else {
|
|
|
|
/* Add extracted type qualifiers here */
|
|
|
|
T->C |= Q;
|
|
|
|
Q = T_QUAL_NONE;
|
|
|
|
}
|
|
|
|
++T;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Q must be empty now */
|
|
|
|
CHECK (Q == T_QUAL_NONE);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-05-28 13:40:48 +00:00
|
|
|
static void ParseStorageClass (DeclSpec* D, unsigned DefStorage)
|
|
|
|
/* Parse a storage class */
|
|
|
|
{
|
|
|
|
/* Assume we're using an explicit storage class */
|
|
|
|
D->Flags &= ~DS_DEF_STORAGE;
|
|
|
|
|
|
|
|
/* Check the storage class given */
|
2001-05-22 09:32:24 +00:00
|
|
|
switch (CurTok.Tok) {
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2000-06-12 18:31:40 +00:00
|
|
|
case TOK_EXTERN:
|
2000-05-28 13:40:48 +00:00
|
|
|
D->StorageClass = SC_EXTERN | SC_STATIC;
|
2000-06-12 18:31:40 +00:00
|
|
|
NextToken ();
|
2000-05-28 13:40:48 +00:00
|
|
|
break;
|
|
|
|
|
2000-06-12 18:31:40 +00:00
|
|
|
case TOK_STATIC:
|
2000-05-28 13:40:48 +00:00
|
|
|
D->StorageClass = SC_STATIC;
|
2000-06-12 18:31:40 +00:00
|
|
|
NextToken ();
|
2000-05-28 13:40:48 +00:00
|
|
|
break;
|
|
|
|
|
2000-06-12 18:31:40 +00:00
|
|
|
case TOK_REGISTER:
|
2000-05-28 13:40:48 +00:00
|
|
|
D->StorageClass = SC_REGISTER | SC_STATIC;
|
2000-06-12 18:31:40 +00:00
|
|
|
NextToken ();
|
2000-05-28 13:40:48 +00:00
|
|
|
break;
|
|
|
|
|
2000-06-12 18:31:40 +00:00
|
|
|
case TOK_AUTO:
|
2000-05-28 13:40:48 +00:00
|
|
|
D->StorageClass = SC_AUTO;
|
2000-06-12 18:31:40 +00:00
|
|
|
NextToken ();
|
2000-05-28 13:40:48 +00:00
|
|
|
break;
|
|
|
|
|
2000-06-12 18:31:40 +00:00
|
|
|
case TOK_TYPEDEF:
|
2000-05-28 13:40:48 +00:00
|
|
|
D->StorageClass = SC_TYPEDEF;
|
2000-06-12 18:31:40 +00:00
|
|
|
NextToken ();
|
2000-05-28 13:40:48 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
/* No storage class given, use default */
|
|
|
|
D->Flags |= DS_DEF_STORAGE;
|
|
|
|
D->StorageClass = DefStorage;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void ParseEnumDecl (void)
|
|
|
|
/* Process an enum declaration . */
|
|
|
|
{
|
|
|
|
int EnumVal;
|
|
|
|
ident Ident;
|
|
|
|
|
|
|
|
/* Accept forward definitions */
|
2001-05-22 09:32:24 +00:00
|
|
|
if (CurTok.Tok != TOK_LCURLY) {
|
2000-05-28 13:40:48 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Skip the opening curly brace */
|
2000-06-12 18:31:40 +00:00
|
|
|
NextToken ();
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/* Read the enum tags */
|
|
|
|
EnumVal = 0;
|
2001-05-22 09:32:24 +00:00
|
|
|
while (CurTok.Tok != TOK_RCURLY) {
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/* We expect an identifier */
|
2001-05-22 09:32:24 +00:00
|
|
|
if (CurTok.Tok != TOK_IDENT) {
|
2000-11-02 21:22:33 +00:00
|
|
|
Error ("Identifier expected");
|
2008-07-31 18:31:15 +00:00
|
|
|
continue;
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Remember the identifier and skip it */
|
|
|
|
strcpy (Ident, CurTok.Ident);
|
2000-06-12 18:31:40 +00:00
|
|
|
NextToken ();
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/* Check for an assigned value */
|
2001-05-22 09:32:24 +00:00
|
|
|
if (CurTok.Tok == TOK_ASSIGN) {
|
2004-06-06 14:48:59 +00:00
|
|
|
ExprDesc Expr;
|
2000-06-12 18:31:40 +00:00
|
|
|
NextToken ();
|
2004-06-06 14:48:59 +00:00
|
|
|
ConstAbsIntExpr (hie1, &Expr);
|
|
|
|
EnumVal = Expr.IVal;
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Add an entry to the symbol table */
|
2001-03-23 21:27:48 +00:00
|
|
|
AddConstSym (Ident, type_int, SC_ENUM, EnumVal++);
|
2003-02-03 22:14:20 +00:00
|
|
|
|
2006-02-17 19:42:40 +00:00
|
|
|
/* Check for end of definition */
|
2001-05-22 09:32:24 +00:00
|
|
|
if (CurTok.Tok != TOK_COMMA)
|
2000-05-28 13:40:48 +00:00
|
|
|
break;
|
2000-06-12 18:31:40 +00:00
|
|
|
NextToken ();
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
2004-06-10 11:02:05 +00:00
|
|
|
ConsumeRCurly ();
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2006-02-19 12:29:37 +00:00
|
|
|
static SymEntry* ParseStructDecl (const char* Name, TypeCode StructType)
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Parse a struct/union declaration. */
|
|
|
|
{
|
|
|
|
|
2003-02-03 22:14:20 +00:00
|
|
|
unsigned StructSize;
|
|
|
|
unsigned FieldSize;
|
|
|
|
unsigned Offs;
|
|
|
|
int FlexibleMember;
|
2000-05-28 13:40:48 +00:00
|
|
|
SymTable* FieldTab;
|
|
|
|
SymEntry* Entry;
|
|
|
|
|
|
|
|
|
2001-05-22 09:32:24 +00:00
|
|
|
if (CurTok.Tok != TOK_LCURLY) {
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Just a forward declaration. Try to find a struct with the given
|
|
|
|
* name. If there is none, insert a forward declaration into the
|
|
|
|
* current lexical level.
|
|
|
|
*/
|
2000-06-12 18:31:40 +00:00
|
|
|
Entry = FindTagSym (Name);
|
|
|
|
if (Entry == 0) {
|
2000-05-28 13:40:48 +00:00
|
|
|
Entry = AddStructSym (Name, 0, 0);
|
2000-06-12 18:31:40 +00:00
|
|
|
} else if (SymIsLocal (Entry) && (Entry->Flags & SC_STRUCT) == 0) {
|
|
|
|
/* Already defined in the level but no struct */
|
2000-11-02 21:22:33 +00:00
|
|
|
Error ("Symbol `%s' is already different kind", Name);
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
2000-06-12 18:31:40 +00:00
|
|
|
return Entry;
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Add a forward declaration for the struct in the current lexical level */
|
|
|
|
Entry = AddStructSym (Name, 0, 0);
|
|
|
|
|
|
|
|
/* Skip the curly brace */
|
2000-06-12 18:31:40 +00:00
|
|
|
NextToken ();
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/* Enter a new lexical level for the struct */
|
|
|
|
EnterStructLevel ();
|
|
|
|
|
|
|
|
/* Parse struct fields */
|
2003-02-03 22:14:20 +00:00
|
|
|
FlexibleMember = 0;
|
|
|
|
StructSize = 0;
|
2001-05-22 09:32:24 +00:00
|
|
|
while (CurTok.Tok != TOK_RCURLY) {
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/* Get the type of the entry */
|
|
|
|
DeclSpec Spec;
|
|
|
|
InitDeclSpec (&Spec);
|
2006-02-17 19:42:40 +00:00
|
|
|
ParseTypeSpec (&Spec, -1, T_QUAL_NONE);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/* Read fields with this type */
|
|
|
|
while (1) {
|
|
|
|
|
|
|
|
Declaration Decl;
|
2003-02-03 22:14:20 +00:00
|
|
|
|
|
|
|
/* If we had a flexible array member before, no other fields can
|
|
|
|
* follow.
|
|
|
|
*/
|
|
|
|
if (FlexibleMember) {
|
|
|
|
Error ("Flexible array member must be last field");
|
|
|
|
FlexibleMember = 0; /* Avoid further errors */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get type and name of the struct field */
|
2000-05-28 13:40:48 +00:00
|
|
|
ParseDecl (&Spec, &Decl, 0);
|
|
|
|
|
2002-11-24 23:31:54 +00:00
|
|
|
/* Get the offset of this field */
|
2003-02-01 12:39:12 +00:00
|
|
|
Offs = (StructType == T_STRUCT)? StructSize : 0;
|
2002-11-24 23:31:54 +00:00
|
|
|
|
2003-02-03 22:14:20 +00:00
|
|
|
/* Calculate the sizes, handle flexible array members */
|
|
|
|
if (StructType == T_STRUCT) {
|
|
|
|
|
|
|
|
/* It's a struct. Check if this field is a flexible array
|
|
|
|
* member, and calculate the size of the field.
|
|
|
|
*/
|
|
|
|
if (IsTypeArray (Decl.Type) && GetElementCount (Decl.Type) == UNSPECIFIED) {
|
|
|
|
/* Array with unspecified size */
|
|
|
|
if (StructSize == 0) {
|
|
|
|
Error ("Flexible array member cannot be first struct field");
|
|
|
|
}
|
|
|
|
FlexibleMember = 1;
|
|
|
|
/* Assume zero for size calculations */
|
2006-02-19 12:29:37 +00:00
|
|
|
SetElementCount (Decl.Type, FLEXIBLE);
|
2003-02-03 22:14:20 +00:00
|
|
|
} else {
|
|
|
|
StructSize += CheckedSizeOf (Decl.Type);
|
|
|
|
}
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
} else {
|
2003-02-03 22:14:20 +00:00
|
|
|
|
2003-02-01 12:39:12 +00:00
|
|
|
/* It's a union */
|
2003-02-03 22:14:20 +00:00
|
|
|
FieldSize = CheckedSizeOf (Decl.Type);
|
2003-02-01 12:39:12 +00:00
|
|
|
if (FieldSize > StructSize) {
|
|
|
|
StructSize = FieldSize;
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
2006-02-17 19:42:40 +00:00
|
|
|
}
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2003-02-03 22:14:20 +00:00
|
|
|
/* Add a field entry to the table */
|
|
|
|
AddLocalSym (Decl.Ident, Decl.Type, SC_STRUCTFIELD, Offs);
|
|
|
|
|
|
|
|
if (CurTok.Tok != TOK_COMMA) {
|
2000-05-28 13:40:48 +00:00
|
|
|
break;
|
2003-02-03 22:14:20 +00:00
|
|
|
}
|
2000-06-12 18:31:40 +00:00
|
|
|
NextToken ();
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
2006-02-17 19:42:40 +00:00
|
|
|
ConsumeSemi ();
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Skip the closing brace */
|
2000-06-12 18:31:40 +00:00
|
|
|
NextToken ();
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/* Remember the symbol table and leave the struct level */
|
|
|
|
FieldTab = GetSymTab ();
|
|
|
|
LeaveStructLevel ();
|
|
|
|
|
|
|
|
/* Make a real entry from the forward decl and return it */
|
2003-02-01 12:39:12 +00:00
|
|
|
return AddStructSym (Name, StructSize, FieldTab);
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2006-02-19 12:29:37 +00:00
|
|
|
static void ParseTypeSpec (DeclSpec* D, long Default, TypeCode Qualifiers)
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Parse a type specificier */
|
|
|
|
{
|
2000-08-01 18:45:08 +00:00
|
|
|
ident Ident;
|
2000-06-12 18:31:40 +00:00
|
|
|
SymEntry* Entry;
|
2006-02-19 12:29:37 +00:00
|
|
|
TypeCode StructType;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2000-07-27 21:08:52 +00:00
|
|
|
/* Assume we have an explicit type */
|
2000-05-28 13:40:48 +00:00
|
|
|
D->Flags &= ~DS_DEF_TYPE;
|
|
|
|
|
2000-08-01 18:45:08 +00:00
|
|
|
/* Read type qualifiers if we have any */
|
2006-02-17 19:42:40 +00:00
|
|
|
Qualifiers = OptionalQualifiers (Qualifiers);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/* Look at the data type */
|
2001-05-22 09:32:24 +00:00
|
|
|
switch (CurTok.Tok) {
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2000-06-12 18:31:40 +00:00
|
|
|
case TOK_VOID:
|
|
|
|
NextToken ();
|
2006-02-19 12:29:37 +00:00
|
|
|
D->Type[0].C = T_VOID;
|
|
|
|
D->Type[1].C = T_END;
|
2000-05-28 13:40:48 +00:00
|
|
|
break;
|
|
|
|
|
2000-06-12 18:31:40 +00:00
|
|
|
case TOK_CHAR:
|
|
|
|
NextToken ();
|
2006-02-19 12:29:37 +00:00
|
|
|
D->Type[0].C = GetDefaultChar();
|
|
|
|
D->Type[1].C = T_END;
|
2000-05-28 13:40:48 +00:00
|
|
|
break;
|
|
|
|
|
2000-06-12 18:31:40 +00:00
|
|
|
case TOK_LONG:
|
|
|
|
NextToken ();
|
2001-05-22 09:32:24 +00:00
|
|
|
if (CurTok.Tok == TOK_UNSIGNED) {
|
2000-08-01 18:45:08 +00:00
|
|
|
NextToken ();
|
2007-03-18 18:26:00 +00:00
|
|
|
OptionalInt ();
|
2006-02-19 12:29:37 +00:00
|
|
|
D->Type[0].C = T_ULONG;
|
|
|
|
D->Type[1].C = T_END;
|
2000-05-28 13:40:48 +00:00
|
|
|
} else {
|
2007-03-18 18:26:00 +00:00
|
|
|
OptionalSigned ();
|
|
|
|
OptionalInt ();
|
2006-02-19 12:29:37 +00:00
|
|
|
D->Type[0].C = T_LONG;
|
|
|
|
D->Type[1].C = T_END;
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2000-06-12 18:31:40 +00:00
|
|
|
case TOK_SHORT:
|
|
|
|
NextToken ();
|
2001-05-22 09:32:24 +00:00
|
|
|
if (CurTok.Tok == TOK_UNSIGNED) {
|
2000-08-01 18:45:08 +00:00
|
|
|
NextToken ();
|
2007-03-18 18:26:00 +00:00
|
|
|
OptionalInt ();
|
2006-02-19 12:29:37 +00:00
|
|
|
D->Type[0].C = T_USHORT;
|
|
|
|
D->Type[1].C = T_END;
|
2000-05-28 13:40:48 +00:00
|
|
|
} else {
|
2007-03-18 18:26:00 +00:00
|
|
|
OptionalSigned ();
|
|
|
|
OptionalInt ();
|
2006-02-19 12:29:37 +00:00
|
|
|
D->Type[0].C = T_SHORT;
|
|
|
|
D->Type[1].C = T_END;
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
2008-07-31 18:31:15 +00:00
|
|
|
break;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2000-06-12 18:31:40 +00:00
|
|
|
case TOK_INT:
|
|
|
|
NextToken ();
|
2006-02-19 12:29:37 +00:00
|
|
|
D->Type[0].C = T_INT;
|
|
|
|
D->Type[1].C = T_END;
|
2000-05-28 13:40:48 +00:00
|
|
|
break;
|
|
|
|
|
2000-06-12 18:31:40 +00:00
|
|
|
case TOK_SIGNED:
|
|
|
|
NextToken ();
|
2001-05-22 09:32:24 +00:00
|
|
|
switch (CurTok.Tok) {
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2000-06-12 18:31:40 +00:00
|
|
|
case TOK_CHAR:
|
|
|
|
NextToken ();
|
2006-02-19 12:29:37 +00:00
|
|
|
D->Type[0].C = T_SCHAR;
|
|
|
|
D->Type[1].C = T_END;
|
2000-05-28 13:40:48 +00:00
|
|
|
break;
|
|
|
|
|
2000-06-12 18:31:40 +00:00
|
|
|
case TOK_SHORT:
|
|
|
|
NextToken ();
|
2007-03-18 18:26:00 +00:00
|
|
|
OptionalInt ();
|
2006-02-19 12:29:37 +00:00
|
|
|
D->Type[0].C = T_SHORT;
|
|
|
|
D->Type[1].C = T_END;
|
2000-05-28 13:40:48 +00:00
|
|
|
break;
|
|
|
|
|
2000-06-12 18:31:40 +00:00
|
|
|
case TOK_LONG:
|
|
|
|
NextToken ();
|
2008-07-31 18:31:15 +00:00
|
|
|
OptionalInt ();
|
2006-02-19 12:29:37 +00:00
|
|
|
D->Type[0].C = T_LONG;
|
|
|
|
D->Type[1].C = T_END;
|
2002-06-12 12:12:04 +00:00
|
|
|
break;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2000-06-12 18:31:40 +00:00
|
|
|
case TOK_INT:
|
|
|
|
NextToken ();
|
2000-05-28 13:40:48 +00:00
|
|
|
/* FALL THROUGH */
|
|
|
|
|
2003-10-10 11:39:30 +00:00
|
|
|
default:
|
2006-02-19 12:29:37 +00:00
|
|
|
D->Type[0].C = T_INT;
|
|
|
|
D->Type[1].C = T_END;
|
2000-05-28 13:40:48 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2000-06-12 18:31:40 +00:00
|
|
|
case TOK_UNSIGNED:
|
|
|
|
NextToken ();
|
2001-05-22 09:32:24 +00:00
|
|
|
switch (CurTok.Tok) {
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2000-06-12 18:31:40 +00:00
|
|
|
case TOK_CHAR:
|
|
|
|
NextToken ();
|
2006-02-19 12:29:37 +00:00
|
|
|
D->Type[0].C = T_UCHAR;
|
|
|
|
D->Type[1].C = T_END;
|
2000-06-12 18:31:40 +00:00
|
|
|
break;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2000-06-12 18:31:40 +00:00
|
|
|
case TOK_SHORT:
|
|
|
|
NextToken ();
|
2007-03-18 18:26:00 +00:00
|
|
|
OptionalInt ();
|
2006-02-19 12:29:37 +00:00
|
|
|
D->Type[0].C = T_USHORT;
|
|
|
|
D->Type[1].C = T_END;
|
2000-05-28 13:40:48 +00:00
|
|
|
break;
|
|
|
|
|
2000-06-12 18:31:40 +00:00
|
|
|
case TOK_LONG:
|
|
|
|
NextToken ();
|
2007-03-18 18:26:00 +00:00
|
|
|
OptionalInt ();
|
2006-02-19 12:29:37 +00:00
|
|
|
D->Type[0].C = T_ULONG;
|
|
|
|
D->Type[1].C = T_END;
|
2000-05-28 13:40:48 +00:00
|
|
|
break;
|
|
|
|
|
2002-06-12 12:12:04 +00:00
|
|
|
case TOK_INT:
|
2000-06-12 18:31:40 +00:00
|
|
|
NextToken ();
|
2000-05-28 13:40:48 +00:00
|
|
|
/* FALL THROUGH */
|
|
|
|
|
|
|
|
default:
|
2006-02-19 12:29:37 +00:00
|
|
|
D->Type[0].C = T_UINT;
|
|
|
|
D->Type[1].C = T_END;
|
2000-05-28 13:40:48 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2003-10-10 11:39:30 +00:00
|
|
|
case TOK_FLOAT:
|
|
|
|
NextToken ();
|
2006-02-19 12:29:37 +00:00
|
|
|
D->Type[0].C = T_FLOAT;
|
|
|
|
D->Type[1].C = T_END;
|
2003-10-10 11:39:30 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TOK_DOUBLE:
|
|
|
|
NextToken ();
|
2006-02-19 12:29:37 +00:00
|
|
|
D->Type[0].C = T_DOUBLE;
|
|
|
|
D->Type[1].C = T_END;
|
2003-10-10 11:39:30 +00:00
|
|
|
break;
|
|
|
|
|
2000-06-12 18:31:40 +00:00
|
|
|
case TOK_STRUCT:
|
|
|
|
case TOK_UNION:
|
2001-05-22 09:32:24 +00:00
|
|
|
StructType = (CurTok.Tok == TOK_STRUCT)? T_STRUCT : T_UNION;
|
2000-06-12 18:31:40 +00:00
|
|
|
NextToken ();
|
|
|
|
/* */
|
2001-05-22 09:32:24 +00:00
|
|
|
if (CurTok.Tok == TOK_IDENT) {
|
2000-05-28 13:40:48 +00:00
|
|
|
strcpy (Ident, CurTok.Ident);
|
2000-06-12 18:31:40 +00:00
|
|
|
NextToken ();
|
2000-05-28 13:40:48 +00:00
|
|
|
} else {
|
|
|
|
AnonName (Ident, (StructType == T_STRUCT)? "struct" : "union");
|
|
|
|
}
|
2000-06-12 18:31:40 +00:00
|
|
|
/* Remember we have an extra type decl */
|
|
|
|
D->Flags |= DS_EXTRA_TYPE;
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Declare the struct in the current scope */
|
|
|
|
Entry = ParseStructDecl (Ident, StructType);
|
|
|
|
/* Encode the struct entry into the type */
|
2008-07-31 18:31:15 +00:00
|
|
|
D->Type[0].C = StructType;
|
2006-02-19 12:29:37 +00:00
|
|
|
SetSymEntry (D->Type, Entry);
|
|
|
|
D->Type[1].C = T_END;
|
2000-05-28 13:40:48 +00:00
|
|
|
break;
|
|
|
|
|
2000-06-12 18:31:40 +00:00
|
|
|
case TOK_ENUM:
|
|
|
|
NextToken ();
|
2006-02-17 19:42:40 +00:00
|
|
|
if (CurTok.Tok != TOK_LCURLY) {
|
2000-06-12 18:31:40 +00:00
|
|
|
/* Named enum */
|
2001-05-22 09:32:24 +00:00
|
|
|
if (CurTok.Tok == TOK_IDENT) {
|
2000-06-12 18:31:40 +00:00
|
|
|
/* Find an entry with this name */
|
2006-02-17 19:42:40 +00:00
|
|
|
Entry = FindTagSym (CurTok.Ident);
|
2000-06-12 18:31:40 +00:00
|
|
|
if (Entry) {
|
|
|
|
if (SymIsLocal (Entry) && (Entry->Flags & SC_ENUM) == 0) {
|
2000-11-02 21:22:33 +00:00
|
|
|
Error ("Symbol `%s' is already different kind", Entry->Name);
|
2000-06-12 18:31:40 +00:00
|
|
|
}
|
2002-06-12 12:12:04 +00:00
|
|
|
} else {
|
2000-06-12 18:31:40 +00:00
|
|
|
/* Insert entry into table ### */
|
|
|
|
}
|
|
|
|
/* Skip the identifier */
|
|
|
|
NextToken ();
|
2006-02-17 19:42:40 +00:00
|
|
|
} else {
|
2000-11-02 21:22:33 +00:00
|
|
|
Error ("Identifier expected");
|
2006-02-17 19:42:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Remember we have an extra type decl */
|
|
|
|
D->Flags |= DS_EXTRA_TYPE;
|
|
|
|
/* Parse the enum decl */
|
2000-05-28 13:40:48 +00:00
|
|
|
ParseEnumDecl ();
|
2006-02-19 12:29:37 +00:00
|
|
|
D->Type[0].C = T_INT;
|
|
|
|
D->Type[1].C = T_END;
|
2000-05-28 13:40:48 +00:00
|
|
|
break;
|
|
|
|
|
2000-06-12 18:31:40 +00:00
|
|
|
case TOK_IDENT:
|
2006-02-17 19:42:40 +00:00
|
|
|
Entry = FindSym (CurTok.Ident);
|
|
|
|
if (Entry && SymIsTypeDef (Entry)) {
|
2000-05-28 13:40:48 +00:00
|
|
|
/* It's a typedef */
|
2000-06-12 18:31:40 +00:00
|
|
|
NextToken ();
|
2006-02-17 19:42:40 +00:00
|
|
|
TypeCpy (D->Type, Entry->Type);
|
2000-05-28 13:40:48 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* FALL THROUGH */
|
|
|
|
|
|
|
|
default:
|
|
|
|
if (Default < 0) {
|
2000-11-02 21:22:33 +00:00
|
|
|
Error ("Type expected");
|
2006-02-19 12:29:37 +00:00
|
|
|
D->Type[0].C = T_INT;
|
|
|
|
D->Type[1].C = T_END;
|
2000-05-28 13:40:48 +00:00
|
|
|
} else {
|
2006-02-19 12:29:37 +00:00
|
|
|
D->Flags |= DS_DEF_TYPE;
|
|
|
|
D->Type[0].C = (TypeCode) Default;
|
|
|
|
D->Type[1].C = T_END;
|
2006-02-17 19:42:40 +00:00
|
|
|
}
|
|
|
|
break;
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
2000-08-01 18:45:08 +00:00
|
|
|
|
|
|
|
/* There may also be qualifiers *after* the initial type */
|
2006-02-19 12:29:37 +00:00
|
|
|
D->Type[0].C |= OptionalQualifiers (Qualifiers);
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2006-02-19 12:29:37 +00:00
|
|
|
static Type* ParamTypeCvt (Type* T)
|
2000-06-12 18:31:40 +00:00
|
|
|
/* If T is an array, convert it to a pointer else do nothing. Return the
|
|
|
|
* resulting type.
|
|
|
|
*/
|
2000-05-28 13:40:48 +00:00
|
|
|
{
|
2000-08-01 18:45:08 +00:00
|
|
|
if (IsTypeArray (T)) {
|
2006-02-19 12:29:37 +00:00
|
|
|
T->C = T_PTR;
|
2000-06-12 18:31:40 +00:00
|
|
|
}
|
|
|
|
return T;
|
|
|
|
}
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2000-06-12 18:31:40 +00:00
|
|
|
static void ParseOldStyleParamList (FuncDesc* F)
|
|
|
|
/* Parse an old style (K&R) parameter list */
|
|
|
|
{
|
|
|
|
/* Parse params */
|
2001-05-22 09:32:24 +00:00
|
|
|
while (CurTok.Tok != TOK_RPAREN) {
|
2000-06-12 18:31:40 +00:00
|
|
|
|
|
|
|
/* List of identifiers expected */
|
2001-05-22 09:32:24 +00:00
|
|
|
if (CurTok.Tok != TOK_IDENT) {
|
2000-11-02 21:22:33 +00:00
|
|
|
Error ("Identifier expected");
|
2000-06-12 18:31:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Create a symbol table entry with type int */
|
2008-08-01 21:40:07 +00:00
|
|
|
AddLocalSym (CurTok.Ident, type_int, SC_AUTO | SC_PARAM | SC_DEF | SC_DEFTYPE, 0);
|
2000-06-12 18:31:40 +00:00
|
|
|
|
|
|
|
/* Count arguments */
|
|
|
|
++F->ParamCount;
|
|
|
|
|
|
|
|
/* Skip the identifier */
|
|
|
|
NextToken ();
|
|
|
|
|
|
|
|
/* Check for more parameters */
|
2001-05-22 09:32:24 +00:00
|
|
|
if (CurTok.Tok == TOK_COMMA) {
|
2000-06-12 18:31:40 +00:00
|
|
|
NextToken ();
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Skip right paren. We must explicitly check for one here, since some of
|
|
|
|
* the breaks above bail out without checking.
|
|
|
|
*/
|
|
|
|
ConsumeRParen ();
|
|
|
|
|
|
|
|
/* An optional list of type specifications follows */
|
2001-05-22 09:32:24 +00:00
|
|
|
while (CurTok.Tok != TOK_LCURLY) {
|
2000-06-12 18:31:40 +00:00
|
|
|
|
2000-11-02 21:22:33 +00:00
|
|
|
DeclSpec Spec;
|
2000-06-12 18:31:40 +00:00
|
|
|
|
|
|
|
/* Read the declaration specifier */
|
|
|
|
ParseDeclSpec (&Spec, SC_AUTO, T_INT);
|
|
|
|
|
|
|
|
/* We accept only auto and register as storage class specifiers, but
|
|
|
|
* we ignore all this, since we use auto anyway.
|
|
|
|
*/
|
|
|
|
if ((Spec.StorageClass & SC_AUTO) == 0 &&
|
2006-02-17 19:42:40 +00:00
|
|
|
(Spec.StorageClass & SC_REGISTER) == 0) {
|
2000-11-02 21:22:33 +00:00
|
|
|
Error ("Illegal storage class");
|
2000-06-12 18:31:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Parse a comma separated variable list */
|
|
|
|
while (1) {
|
|
|
|
|
2008-08-01 21:40:07 +00:00
|
|
|
Declaration Decl;
|
2000-06-12 18:31:40 +00:00
|
|
|
|
|
|
|
/* Read the parameter */
|
|
|
|
ParseDecl (&Spec, &Decl, DM_NEED_IDENT);
|
|
|
|
if (Decl.Ident[0] != '\0') {
|
|
|
|
|
2008-07-31 18:31:15 +00:00
|
|
|
/* We have a name given. Search for the symbol */
|
2000-06-12 18:31:40 +00:00
|
|
|
SymEntry* Sym = FindLocalSym (Decl.Ident);
|
|
|
|
if (Sym) {
|
2008-08-01 21:40:07 +00:00
|
|
|
/* Check if we already changed the type for this
|
|
|
|
* parameter
|
|
|
|
*/
|
|
|
|
if (Sym->Flags & SC_DEFTYPE) {
|
|
|
|
/* Found it, change the default type to the one given */
|
|
|
|
ChangeSymType (Sym, ParamTypeCvt (Decl.Type));
|
|
|
|
/* Reset the "default type" flag */
|
|
|
|
Sym->Flags &= ~SC_DEFTYPE;
|
|
|
|
} else {
|
|
|
|
/* Type has already been changed */
|
|
|
|
Error ("Redefinition for parameter `%s'", Sym->Name);
|
|
|
|
}
|
2000-06-12 18:31:40 +00:00
|
|
|
} else {
|
2000-11-02 21:22:33 +00:00
|
|
|
Error ("Unknown identifier: `%s'", Decl.Ident);
|
2000-06-12 18:31:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-05-22 09:32:24 +00:00
|
|
|
if (CurTok.Tok == TOK_COMMA) {
|
2002-06-12 12:12:04 +00:00
|
|
|
NextToken ();
|
2000-06-12 18:31:40 +00:00
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Variable list must be semicolon terminated */
|
|
|
|
ConsumeSemi ();
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
2000-06-12 18:31:40 +00:00
|
|
|
}
|
|
|
|
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2000-06-12 18:31:40 +00:00
|
|
|
|
|
|
|
static void ParseAnsiParamList (FuncDesc* F)
|
|
|
|
/* Parse a new style (ANSI) parameter list */
|
|
|
|
{
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Parse params */
|
2001-05-22 09:32:24 +00:00
|
|
|
while (CurTok.Tok != TOK_RPAREN) {
|
2000-06-12 18:31:40 +00:00
|
|
|
|
|
|
|
DeclSpec Spec;
|
|
|
|
Declaration Decl;
|
2000-09-14 19:26:13 +00:00
|
|
|
DeclAttr Attr;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/* Allow an ellipsis as last parameter */
|
2001-05-22 09:32:24 +00:00
|
|
|
if (CurTok.Tok == TOK_ELLIPSIS) {
|
2006-02-17 19:42:40 +00:00
|
|
|
NextToken ();
|
2001-03-24 13:00:16 +00:00
|
|
|
F->Flags |= FD_VARIADIC;
|
2000-05-28 13:40:48 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read the declaration specifier */
|
|
|
|
ParseDeclSpec (&Spec, SC_AUTO, T_INT);
|
|
|
|
|
2002-11-25 15:05:15 +00:00
|
|
|
/* We accept only auto and register as storage class specifiers */
|
|
|
|
if ((Spec.StorageClass & SC_AUTO) == SC_AUTO) {
|
|
|
|
Spec.StorageClass = SC_AUTO | SC_PARAM | SC_DEF;
|
|
|
|
} else if ((Spec.StorageClass & SC_REGISTER) == SC_REGISTER) {
|
|
|
|
Spec.StorageClass = SC_REGISTER | SC_STATIC | SC_PARAM | SC_DEF;
|
|
|
|
} else {
|
2000-11-02 21:22:33 +00:00
|
|
|
Error ("Illegal storage class");
|
2002-11-25 15:05:15 +00:00
|
|
|
Spec.StorageClass = SC_AUTO | SC_PARAM | SC_DEF;
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Allow parameters without a name, but remember if we had some to
|
|
|
|
* eventually print an error message later.
|
|
|
|
*/
|
|
|
|
ParseDecl (&Spec, &Decl, DM_ACCEPT_IDENT);
|
|
|
|
if (Decl.Ident[0] == '\0') {
|
|
|
|
|
|
|
|
/* Unnamed symbol. Generate a name that is not user accessible,
|
|
|
|
* then handle the symbol normal.
|
|
|
|
*/
|
2000-06-12 18:31:40 +00:00
|
|
|
AnonName (Decl.Ident, "param");
|
|
|
|
F->Flags |= FD_UNNAMED_PARAMS;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2000-06-12 18:31:40 +00:00
|
|
|
/* Clear defined bit on nonames */
|
|
|
|
Spec.StorageClass &= ~SC_DEF;
|
|
|
|
}
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2000-10-10 20:40:36 +00:00
|
|
|
/* Parse an attribute ### */
|
2000-09-14 19:26:13 +00:00
|
|
|
ParseAttribute (&Decl, &Attr);
|
|
|
|
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Create a symbol table entry */
|
2000-06-12 18:31:40 +00:00
|
|
|
AddLocalSym (Decl.Ident, ParamTypeCvt (Decl.Type), Spec.StorageClass, 0);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/* Count arguments */
|
|
|
|
++F->ParamCount;
|
|
|
|
|
|
|
|
/* Check for more parameters */
|
2001-05-22 09:32:24 +00:00
|
|
|
if (CurTok.Tok == TOK_COMMA) {
|
2000-06-12 18:31:40 +00:00
|
|
|
NextToken ();
|
2000-05-28 13:40:48 +00:00
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Skip right paren. We must explicitly check for one here, since some of
|
|
|
|
* the breaks above bail out without checking.
|
|
|
|
*/
|
|
|
|
ConsumeRParen ();
|
|
|
|
|
2000-06-12 18:31:40 +00:00
|
|
|
/* Check if this is a function definition */
|
2001-05-22 09:32:24 +00:00
|
|
|
if (CurTok.Tok == TOK_LCURLY) {
|
2004-06-21 22:22:11 +00:00
|
|
|
/* Print an error if we have unnamed parameters and cc65 extensions
|
|
|
|
* are disabled.
|
2000-06-12 18:31:40 +00:00
|
|
|
*/
|
2004-06-21 22:22:11 +00:00
|
|
|
if (IS_Get (&Standard) != STD_CC65 &&
|
|
|
|
(F->Flags & FD_UNNAMED_PARAMS) != 0) {
|
2000-11-02 21:22:33 +00:00
|
|
|
Error ("Parameter name omitted");
|
2000-06-12 18:31:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2008-07-31 18:31:15 +00:00
|
|
|
static FuncDesc* ParseFuncDecl (void)
|
2000-06-12 18:31:40 +00:00
|
|
|
/* Parse the argument list of a function. */
|
|
|
|
{
|
|
|
|
unsigned Offs;
|
|
|
|
SymEntry* Sym;
|
|
|
|
|
|
|
|
/* Create a new function descriptor */
|
|
|
|
FuncDesc* F = NewFuncDesc ();
|
|
|
|
|
|
|
|
/* Enter a new lexical level */
|
|
|
|
EnterFunctionLevel ();
|
|
|
|
|
|
|
|
/* Check for several special parameter lists */
|
2001-05-22 09:32:24 +00:00
|
|
|
if (CurTok.Tok == TOK_RPAREN) {
|
2000-06-12 18:31:40 +00:00
|
|
|
/* Parameter list is empty */
|
2001-03-24 13:00:16 +00:00
|
|
|
F->Flags |= (FD_EMPTY | FD_VARIADIC);
|
2001-05-22 09:32:24 +00:00
|
|
|
} else if (CurTok.Tok == TOK_VOID && NextTok.Tok == TOK_RPAREN) {
|
2000-06-12 18:31:40 +00:00
|
|
|
/* Parameter list declared as void */
|
|
|
|
NextToken ();
|
|
|
|
F->Flags |= FD_VOID_PARAM;
|
2001-05-22 09:32:24 +00:00
|
|
|
} else if (CurTok.Tok == TOK_IDENT &&
|
2002-12-26 16:14:57 +00:00
|
|
|
(NextTok.Tok == TOK_COMMA || NextTok.Tok == TOK_RPAREN)) {
|
|
|
|
/* If the identifier is a typedef, we have a new style parameter list,
|
|
|
|
* if it's some other identifier, it's an old style parameter list.
|
|
|
|
*/
|
|
|
|
Sym = FindSym (CurTok.Ident);
|
|
|
|
if (Sym == 0 || !SymIsTypeDef (Sym)) {
|
2005-01-09 21:16:32 +00:00
|
|
|
/* Old style (K&R) function. */
|
|
|
|
F->Flags |= FD_OLDSTYLE;
|
2002-12-26 16:14:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-06-12 18:31:40 +00:00
|
|
|
/* Parse params */
|
|
|
|
if ((F->Flags & FD_OLDSTYLE) == 0) {
|
|
|
|
/* New style function */
|
|
|
|
ParseAnsiParamList (F);
|
|
|
|
} else {
|
|
|
|
/* Old style function */
|
|
|
|
ParseOldStyleParamList (F);
|
|
|
|
}
|
|
|
|
|
2003-08-10 17:05:18 +00:00
|
|
|
/* Remember the last function parameter. We need it later for several
|
|
|
|
* purposes, for example when passing stuff to fastcall functions. Since
|
|
|
|
* more symbols are added to the table, it is easier if we remember it
|
|
|
|
* now, since it is currently the last entry in the symbol table.
|
|
|
|
*/
|
|
|
|
F->LastParam = GetSymTab()->SymTail;
|
|
|
|
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Assign offsets. If the function has a variable parameter list,
|
|
|
|
* there's one additional byte (the arg size).
|
|
|
|
*/
|
2001-03-24 13:00:16 +00:00
|
|
|
Offs = (F->Flags & FD_VARIADIC)? 1 : 0;
|
2003-08-10 17:05:18 +00:00
|
|
|
Sym = F->LastParam;
|
2000-05-28 13:40:48 +00:00
|
|
|
while (Sym) {
|
2002-03-10 20:44:58 +00:00
|
|
|
unsigned Size = CheckedSizeOf (Sym->Type);
|
2002-11-25 15:05:15 +00:00
|
|
|
if (SymIsRegVar (Sym)) {
|
|
|
|
Sym->V.R.SaveOffs = Offs;
|
|
|
|
} else {
|
|
|
|
Sym->V.Offs = Offs;
|
|
|
|
}
|
2000-06-12 18:31:40 +00:00
|
|
|
Offs += Size;
|
|
|
|
F->ParamSize += Size;
|
|
|
|
Sym = Sym->PrevSym;
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
2003-11-29 07:17:31 +00:00
|
|
|
/* Add the default address size for the function */
|
|
|
|
if (CodeAddrSize == ADDR_SIZE_FAR) {
|
|
|
|
F->Flags |= FD_FAR;
|
|
|
|
} else {
|
|
|
|
F->Flags |= FD_NEAR;
|
|
|
|
}
|
|
|
|
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Leave the lexical level remembering the symbol tables */
|
|
|
|
RememberFunctionLevel (F);
|
|
|
|
|
|
|
|
/* Return the function descriptor */
|
|
|
|
return F;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2003-11-28 22:12:14 +00:00
|
|
|
static unsigned FunctionModifierFlags (void)
|
|
|
|
/* Parse __fastcall__, __near__ and __far__ and return the matching FD_ flags */
|
|
|
|
{
|
|
|
|
/* Read the flags */
|
|
|
|
unsigned Flags = FD_NONE;
|
|
|
|
while (CurTok.Tok == TOK_FASTCALL || CurTok.Tok == TOK_NEAR || CurTok.Tok == TOK_FAR) {
|
|
|
|
|
|
|
|
/* Get the flag bit for the next token */
|
|
|
|
unsigned F = FD_NONE;
|
|
|
|
switch (CurTok.Tok) {
|
2005-01-09 18:03:55 +00:00
|
|
|
case TOK_FASTCALL: F = FD_FASTCALL; break;
|
|
|
|
case TOK_NEAR: F = FD_NEAR; break;
|
|
|
|
case TOK_FAR: F = FD_FAR; break;
|
2003-11-28 22:12:14 +00:00
|
|
|
default: Internal ("Unexpected token: %d", CurTok.Tok);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Remember the flag for this modifier */
|
|
|
|
if (Flags & F) {
|
|
|
|
Error ("Duplicate modifier");
|
|
|
|
}
|
|
|
|
Flags |= F;
|
|
|
|
|
|
|
|
/* Skip the token */
|
|
|
|
NextToken ();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Sanity check */
|
|
|
|
if ((Flags & (FD_NEAR | FD_FAR)) == (FD_NEAR | FD_FAR)) {
|
|
|
|
Error ("Cannot specify both, `__near__' and `__far__' modifiers");
|
|
|
|
Flags &= ~(FD_NEAR | FD_FAR);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the flags read */
|
|
|
|
return Flags;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2006-02-19 12:29:37 +00:00
|
|
|
static void ApplyFunctionModifiers (Type* T, unsigned Flags)
|
2003-11-28 22:12:14 +00:00
|
|
|
/* Apply a set of function modifier flags to a function */
|
|
|
|
{
|
|
|
|
/* Get the function descriptor */
|
|
|
|
FuncDesc* F = GetFuncDesc (T);
|
|
|
|
|
|
|
|
/* Special check for __fastcall__ */
|
|
|
|
if ((Flags & FD_FASTCALL) != 0 && IsVariadicFunc (T)) {
|
|
|
|
Error ("Cannot apply `__fastcall__' to functions with "
|
|
|
|
"variable parameter list");
|
|
|
|
Flags &= ~FD_FASTCALL;
|
|
|
|
}
|
|
|
|
|
2003-11-29 07:17:31 +00:00
|
|
|
/* Remove the default function address size modifiers */
|
|
|
|
F->Flags &= ~(FD_NEAR | FD_FAR);
|
|
|
|
|
|
|
|
/* Add the new modifers */
|
2003-11-28 22:12:14 +00:00
|
|
|
F->Flags |= Flags;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2002-06-12 12:12:04 +00:00
|
|
|
static void Decl (const DeclSpec* Spec, Declaration* D, unsigned Mode)
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Recursively process declarators. Build a type array in reverse order. */
|
|
|
|
{
|
2003-11-28 22:12:14 +00:00
|
|
|
/* Pointer to something */
|
2001-05-22 09:32:24 +00:00
|
|
|
if (CurTok.Tok == TOK_STAR) {
|
2003-11-28 22:12:14 +00:00
|
|
|
|
2006-02-19 12:29:37 +00:00
|
|
|
TypeCode C;
|
2003-11-28 22:12:14 +00:00
|
|
|
|
|
|
|
/* Skip the star */
|
2000-06-12 18:31:40 +00:00
|
|
|
NextToken ();
|
2003-11-28 22:12:14 +00:00
|
|
|
|
2002-06-12 12:12:04 +00:00
|
|
|
/* Allow optional const or volatile qualifiers */
|
2006-02-19 12:29:37 +00:00
|
|
|
C = T_PTR | OptionalQualifiers (T_QUAL_NONE);
|
2003-11-28 22:12:14 +00:00
|
|
|
|
|
|
|
/* Parse the type, the pointer points to */
|
2002-06-12 12:12:04 +00:00
|
|
|
Decl (Spec, D, Mode);
|
2003-11-28 22:12:14 +00:00
|
|
|
|
2004-05-02 09:57:05 +00:00
|
|
|
/* Add the type */
|
2006-02-19 12:29:37 +00:00
|
|
|
AddTypeToDeclaration (D, C);
|
2000-05-28 13:40:48 +00:00
|
|
|
return;
|
2003-11-28 22:12:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Function modifiers */
|
|
|
|
if (CurTok.Tok == TOK_FASTCALL || CurTok.Tok == TOK_NEAR || CurTok.Tok == TOK_FAR) {
|
|
|
|
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Remember the current type pointer */
|
2006-02-19 12:29:37 +00:00
|
|
|
Type* T = D->Type + D->Index;
|
2003-11-28 22:12:14 +00:00
|
|
|
|
2008-07-31 18:31:15 +00:00
|
|
|
/* Read the flags */
|
|
|
|
unsigned Flags = FunctionModifierFlags ();
|
2003-11-28 22:12:14 +00:00
|
|
|
|
2008-07-31 18:31:15 +00:00
|
|
|
/* Parse the function */
|
|
|
|
Decl (Spec, D, Mode);
|
2003-11-28 22:12:14 +00:00
|
|
|
|
2008-07-31 18:31:15 +00:00
|
|
|
/* Check that we have a function */
|
|
|
|
if (!IsTypeFunc (T) && !IsTypeFuncPtr (T)) {
|
|
|
|
Error ("Function modifier applied to non function");
|
|
|
|
} else {
|
2003-11-28 22:12:14 +00:00
|
|
|
ApplyFunctionModifiers (T, Flags);
|
|
|
|
}
|
|
|
|
|
2008-07-31 18:31:15 +00:00
|
|
|
/* Done */
|
|
|
|
return;
|
2003-11-28 22:12:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (CurTok.Tok == TOK_LPAREN) {
|
|
|
|
NextToken ();
|
|
|
|
Decl (Spec, D, Mode);
|
|
|
|
ConsumeRParen ();
|
2000-05-28 13:40:48 +00:00
|
|
|
} else {
|
2000-06-12 18:31:40 +00:00
|
|
|
/* Things depend on Mode now:
|
2000-05-28 13:40:48 +00:00
|
|
|
* - Mode == DM_NEED_IDENT means:
|
2000-06-12 18:31:40 +00:00
|
|
|
* we *must* have a type and a variable identifer.
|
|
|
|
* - Mode == DM_NO_IDENT means:
|
2002-06-12 12:12:04 +00:00
|
|
|
* we must have a type but no variable identifer
|
2000-06-12 18:31:40 +00:00
|
|
|
* (if there is one, it's not read).
|
|
|
|
* - Mode == DM_ACCEPT_IDENT means:
|
2002-06-12 12:12:04 +00:00
|
|
|
* we *may* have an identifier. If there is an identifier,
|
|
|
|
* it is read, but it is no error, if there is none.
|
2000-06-12 18:31:40 +00:00
|
|
|
*/
|
|
|
|
if (Mode == DM_NO_IDENT) {
|
|
|
|
D->Ident[0] = '\0';
|
2001-05-22 09:32:24 +00:00
|
|
|
} else if (CurTok.Tok == TOK_IDENT) {
|
2000-05-28 13:40:48 +00:00
|
|
|
strcpy (D->Ident, CurTok.Ident);
|
2000-06-12 18:31:40 +00:00
|
|
|
NextToken ();
|
|
|
|
} else {
|
|
|
|
if (Mode == DM_NEED_IDENT) {
|
2000-11-02 21:22:33 +00:00
|
|
|
Error ("Identifier expected");
|
2000-06-12 18:31:40 +00:00
|
|
|
}
|
|
|
|
D->Ident[0] = '\0';
|
|
|
|
}
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
2001-05-22 09:32:24 +00:00
|
|
|
while (CurTok.Tok == TOK_LBRACK || CurTok.Tok == TOK_LPAREN) {
|
|
|
|
if (CurTok.Tok == TOK_LPAREN) {
|
2004-05-02 09:57:05 +00:00
|
|
|
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Function declaration */
|
2008-07-31 18:31:15 +00:00
|
|
|
FuncDesc* F;
|
2000-06-12 18:31:40 +00:00
|
|
|
NextToken ();
|
2004-05-02 09:57:05 +00:00
|
|
|
|
2008-07-31 18:31:15 +00:00
|
|
|
/* Parse the function declaration */
|
|
|
|
F = ParseFuncDecl ();
|
2004-05-02 09:57:05 +00:00
|
|
|
|
2008-07-31 18:31:15 +00:00
|
|
|
/* Add the function type. Be sure to bounds check the type buffer */
|
|
|
|
AddFuncTypeToDeclaration (D, F);
|
2000-05-28 13:40:48 +00:00
|
|
|
} else {
|
2008-07-31 18:31:15 +00:00
|
|
|
/* Array declaration */
|
2003-02-03 22:14:20 +00:00
|
|
|
long Size = UNSPECIFIED;
|
2000-06-12 18:31:40 +00:00
|
|
|
NextToken ();
|
2008-07-31 18:31:15 +00:00
|
|
|
/* Read the size if it is given */
|
2001-05-22 09:32:24 +00:00
|
|
|
if (CurTok.Tok != TOK_RBRACK) {
|
2004-06-06 14:48:59 +00:00
|
|
|
ExprDesc Expr;
|
|
|
|
ConstAbsIntExpr (hie1, &Expr);
|
|
|
|
if (Expr.IVal <= 0) {
|
2003-02-01 12:39:12 +00:00
|
|
|
if (D->Ident[0] != '\0') {
|
2003-02-03 22:14:20 +00:00
|
|
|
Error ("Size of array `%s' is invalid", D->Ident);
|
2003-02-01 12:39:12 +00:00
|
|
|
} else {
|
2003-02-03 22:14:20 +00:00
|
|
|
Error ("Size of array is invalid");
|
2003-02-01 12:39:12 +00:00
|
|
|
}
|
2004-06-06 14:48:59 +00:00
|
|
|
Expr.IVal = 1;
|
2003-02-01 12:39:12 +00:00
|
|
|
}
|
2004-06-06 14:48:59 +00:00
|
|
|
Size = Expr.IVal;
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
ConsumeRBrack ();
|
2004-05-02 09:57:05 +00:00
|
|
|
|
2008-07-31 18:31:15 +00:00
|
|
|
/* Add the array type with the size */
|
|
|
|
AddArrayToDeclaration (D, Size);
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
/* code */
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
2006-02-19 12:29:37 +00:00
|
|
|
Type* ParseType (Type* T)
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Parse a complete type specification */
|
|
|
|
{
|
|
|
|
DeclSpec Spec;
|
|
|
|
Declaration Decl;
|
|
|
|
|
|
|
|
/* Get a type without a default */
|
|
|
|
InitDeclSpec (&Spec);
|
2006-02-17 19:42:40 +00:00
|
|
|
ParseTypeSpec (&Spec, -1, T_QUAL_NONE);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/* Parse additional declarators */
|
|
|
|
ParseDecl (&Spec, &Decl, DM_NO_IDENT);
|
|
|
|
|
|
|
|
/* Copy the type to the target buffer */
|
2006-02-19 12:29:37 +00:00
|
|
|
TypeCpy (T, Decl.Type);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/* Return a pointer to the target buffer */
|
2006-02-19 12:29:37 +00:00
|
|
|
return T;
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void ParseDecl (const DeclSpec* Spec, Declaration* D, unsigned Mode)
|
|
|
|
/* Parse a variable, type or function declaration */
|
|
|
|
{
|
|
|
|
/* Initialize the Declaration struct */
|
|
|
|
InitDeclaration (D);
|
|
|
|
|
|
|
|
/* Get additional declarators and the identifier */
|
2002-06-12 12:12:04 +00:00
|
|
|
Decl (Spec, D, Mode);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/* Add the base type. */
|
2004-05-02 09:57:05 +00:00
|
|
|
NeedTypeSpace (D, TypeLen (Spec->Type) + 1); /* Bounds check */
|
|
|
|
TypeCpy (D->Type + D->Index, Spec->Type);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2007-03-18 18:26:00 +00:00
|
|
|
/* Fix any type qualifiers attached to an array type */
|
|
|
|
FixArrayQualifiers (D->Type);
|
|
|
|
|
2008-07-31 18:31:15 +00:00
|
|
|
/* Check several things for function or function pointer types */
|
|
|
|
if (IsTypeFunc (D->Type) || IsTypeFuncPtr (D->Type)) {
|
|
|
|
|
|
|
|
/* A function. Check the return type */
|
|
|
|
Type* RetType = GetFuncReturn (D->Type);
|
|
|
|
|
|
|
|
/* Functions may not return functions or arrays */
|
|
|
|
if (IsTypeFunc (RetType)) {
|
|
|
|
Error ("Functions are not allowed to return functions");
|
|
|
|
} else if (IsTypeArray (RetType)) {
|
|
|
|
Error ("Functions are not allowed to return arrays");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The return type must not be qualified */
|
|
|
|
if (GetQualifier (RetType) != T_QUAL_NONE && RetType[1].C == T_END) {
|
|
|
|
|
|
|
|
if (GetType (RetType) == T_TYPE_VOID) {
|
|
|
|
/* A qualified void type is always an error */
|
|
|
|
Error ("function definition has qualified void return type");
|
|
|
|
} else {
|
|
|
|
/* For others, qualifiers are ignored */
|
|
|
|
Warning ("type qualifiers ignored on function return type");
|
|
|
|
RetType[0].C = UnqualifiedType (RetType[0].C);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Warn about an implicit int return in the function */
|
|
|
|
if ((Spec->Flags & DS_DEF_TYPE) != 0 &&
|
|
|
|
RetType[0].C == T_INT && RetType[1].C == T_END) {
|
|
|
|
/* Function has an implicit int return. Output a warning if we don't
|
|
|
|
* have the C89 standard enabled explicitly.
|
|
|
|
*/
|
|
|
|
if (IS_Get (&Standard) >= STD_C99) {
|
|
|
|
Warning ("Implicit `int' return type is an obsolete feature");
|
|
|
|
}
|
|
|
|
GetFuncDesc (D->Type)->Flags |= FD_OLDSTYLE_INTRET;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Check the size of the generated type */
|
2004-06-10 11:02:05 +00:00
|
|
|
if (!IsTypeFunc (D->Type) && !IsTypeVoid (D->Type)) {
|
2008-07-31 18:31:15 +00:00
|
|
|
unsigned Size = SizeOf (D->Type);
|
|
|
|
if (Size >= 0x10000) {
|
|
|
|
if (D->Ident[0] != '\0') {
|
|
|
|
Error ("Size of `%s' is invalid (0x%06X)", D->Ident, Size);
|
|
|
|
} else {
|
|
|
|
Error ("Invalid size in declaration (0x%06X)", Size);
|
|
|
|
}
|
|
|
|
}
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
2008-07-31 18:31:15 +00:00
|
|
|
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2006-02-19 15:53:11 +00:00
|
|
|
void ParseDeclSpec (DeclSpec* D, unsigned DefStorage, long DefType)
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Parse a declaration specification */
|
|
|
|
{
|
2006-02-19 12:29:37 +00:00
|
|
|
TypeCode Qualifiers;
|
2006-02-17 19:42:40 +00:00
|
|
|
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Initialize the DeclSpec struct */
|
|
|
|
InitDeclSpec (D);
|
|
|
|
|
2006-02-17 19:42:40 +00:00
|
|
|
/* There may be qualifiers *before* the storage class specifier */
|
|
|
|
Qualifiers = OptionalQualifiers (T_QUAL_NONE);
|
|
|
|
|
|
|
|
/* Now get the storage class specifier for this declaration */
|
2000-05-28 13:40:48 +00:00
|
|
|
ParseStorageClass (D, DefStorage);
|
|
|
|
|
2006-02-17 19:42:40 +00:00
|
|
|
/* Parse the type specifiers passing any initial type qualifiers */
|
|
|
|
ParseTypeSpec (D, DefType, Qualifiers);
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-06-12 18:31:40 +00:00
|
|
|
void CheckEmptyDecl (const DeclSpec* D)
|
|
|
|
/* Called after an empty type declaration (that is, a type declaration without
|
|
|
|
* a variable). Checks if the declaration does really make sense and issues a
|
|
|
|
* warning if not.
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
if ((D->Flags & DS_EXTRA_TYPE) == 0) {
|
2000-10-15 19:52:01 +00:00
|
|
|
Warning ("Useless declaration");
|
2000-06-12 18:31:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2003-03-16 14:31:48 +00:00
|
|
|
static void SkipInitializer (unsigned BracesExpected)
|
|
|
|
/* Skip the remainder of an initializer in case of errors. Try to be somewhat
|
|
|
|
* smart so we don't have too many following errors.
|
2003-02-03 22:14:20 +00:00
|
|
|
*/
|
2000-05-28 13:40:48 +00:00
|
|
|
{
|
2003-03-16 14:31:48 +00:00
|
|
|
while (CurTok.Tok != TOK_CEOF && CurTok.Tok != TOK_SEMI && BracesExpected > 0) {
|
|
|
|
switch (CurTok.Tok) {
|
|
|
|
case TOK_RCURLY: --BracesExpected; break;
|
|
|
|
case TOK_LCURLY: ++BracesExpected; break;
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
NextToken ();
|
|
|
|
}
|
|
|
|
}
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2003-02-03 22:14:20 +00:00
|
|
|
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2003-03-16 14:31:48 +00:00
|
|
|
static unsigned OpeningCurlyBraces (unsigned BracesNeeded)
|
|
|
|
/* Accept any number of opening curly braces around an initialization, skip
|
|
|
|
* them and return the number. If the number of curly braces is less than
|
|
|
|
* BracesNeeded, issue a warning.
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
unsigned BraceCount = 0;
|
|
|
|
while (CurTok.Tok == TOK_LCURLY) {
|
|
|
|
++BraceCount;
|
|
|
|
NextToken ();
|
|
|
|
}
|
|
|
|
if (BraceCount < BracesNeeded) {
|
|
|
|
Error ("`{' expected");
|
|
|
|
}
|
|
|
|
return BraceCount;
|
|
|
|
}
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2003-03-16 14:31:48 +00:00
|
|
|
static void ClosingCurlyBraces (unsigned BracesExpected)
|
|
|
|
/* Accept and skip the given number of closing curly braces together with
|
|
|
|
* an optional comma. Output an error messages, if the input does not contain
|
|
|
|
* the expected number of braces.
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
while (BracesExpected) {
|
|
|
|
if (CurTok.Tok == TOK_RCURLY) {
|
|
|
|
NextToken ();
|
|
|
|
} else if (CurTok.Tok == TOK_COMMA && NextTok.Tok == TOK_RCURLY) {
|
|
|
|
NextToken ();
|
|
|
|
NextToken ();
|
|
|
|
} else {
|
|
|
|
Error ("`}' expected");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
--BracesExpected;
|
|
|
|
}
|
|
|
|
}
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2004-09-23 17:34:03 +00:00
|
|
|
static void DefineData (ExprDesc* Expr)
|
|
|
|
/* Output a data definition for the given expression */
|
|
|
|
{
|
|
|
|
switch (ED_GetLoc (Expr)) {
|
|
|
|
|
|
|
|
case E_LOC_ABS:
|
|
|
|
/* Absolute: numeric address or const */
|
|
|
|
g_defdata (TypeOf (Expr->Type) | CF_CONST, Expr->IVal, 0);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case E_LOC_GLOBAL:
|
|
|
|
/* Global variable */
|
|
|
|
g_defdata (CF_EXTERNAL, Expr->Name, Expr->IVal);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case E_LOC_STATIC:
|
|
|
|
case E_LOC_LITERAL:
|
|
|
|
/* Static variable or literal in the literal pool */
|
|
|
|
g_defdata (CF_STATIC, Expr->Name, Expr->IVal);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case E_LOC_REGISTER:
|
|
|
|
/* Register variable. Taking the address is usually not
|
|
|
|
* allowed.
|
|
|
|
*/
|
|
|
|
if (IS_Get (&AllowRegVarAddr) == 0) {
|
|
|
|
Error ("Cannot take the address of a register variable");
|
|
|
|
}
|
|
|
|
g_defdata (CF_REGVAR, Expr->Name, Expr->IVal);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case E_LOC_STACK:
|
|
|
|
case E_LOC_PRIMARY:
|
|
|
|
case E_LOC_EXPR:
|
|
|
|
Error ("Non constant initializer");
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
Internal ("Unknown constant type: 0x%04X", ED_GetLoc (Expr));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2006-02-19 12:29:37 +00:00
|
|
|
static unsigned ParseScalarInit (Type* T)
|
2003-03-16 15:18:10 +00:00
|
|
|
/* Parse initializaton for scalar data types. Return the number of data bytes. */
|
2003-03-16 14:31:48 +00:00
|
|
|
{
|
|
|
|
ExprDesc ED;
|
|
|
|
|
2003-03-16 15:18:10 +00:00
|
|
|
/* Optional opening brace */
|
2003-08-11 20:18:30 +00:00
|
|
|
unsigned BraceCount = OpeningCurlyBraces (0);
|
2003-03-16 15:18:10 +00:00
|
|
|
|
|
|
|
/* We warn if an initializer for a scalar contains braces, because this is
|
|
|
|
* quite unusual and often a sign for some problem in the input.
|
|
|
|
*/
|
|
|
|
if (BraceCount > 0) {
|
|
|
|
Warning ("Braces around scalar initializer");
|
|
|
|
}
|
|
|
|
|
2003-08-11 20:18:30 +00:00
|
|
|
/* Get the expression and convert it to the target type */
|
2004-05-31 15:27:33 +00:00
|
|
|
ConstExpr (hie1, &ED);
|
2004-05-25 20:59:38 +00:00
|
|
|
TypeConversion (&ED, T);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2003-03-16 14:31:48 +00:00
|
|
|
/* Output the data */
|
|
|
|
DefineData (&ED);
|
2003-02-03 22:14:20 +00:00
|
|
|
|
2003-03-16 14:31:48 +00:00
|
|
|
/* Close eventually opening braces */
|
|
|
|
ClosingCurlyBraces (BraceCount);
|
|
|
|
|
|
|
|
/* Done */
|
2003-08-11 20:18:30 +00:00
|
|
|
return SizeOf (T);
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2006-02-19 12:29:37 +00:00
|
|
|
static unsigned ParsePointerInit (Type* T)
|
2003-03-16 15:18:10 +00:00
|
|
|
/* Parse initializaton for pointer data types. Return the number of data bytes. */
|
|
|
|
{
|
|
|
|
/* Optional opening brace */
|
|
|
|
unsigned BraceCount = OpeningCurlyBraces (0);
|
|
|
|
|
|
|
|
/* Expression */
|
|
|
|
ExprDesc ED;
|
2004-05-31 15:27:33 +00:00
|
|
|
ConstExpr (hie1, &ED);
|
2004-05-25 20:59:38 +00:00
|
|
|
TypeConversion (&ED, T);
|
2003-03-16 15:18:10 +00:00
|
|
|
|
|
|
|
/* Output the data */
|
|
|
|
DefineData (&ED);
|
|
|
|
|
|
|
|
/* Close eventually opening braces */
|
|
|
|
ClosingCurlyBraces (BraceCount);
|
|
|
|
|
|
|
|
/* Done */
|
|
|
|
return SIZEOF_PTR;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2006-02-19 12:29:37 +00:00
|
|
|
static unsigned ParseArrayInit (Type* T, int AllowFlexibleMembers)
|
2003-03-16 14:31:48 +00:00
|
|
|
/* Parse initializaton for arrays. Return the number of data bytes. */
|
|
|
|
{
|
|
|
|
int Count;
|
|
|
|
|
|
|
|
/* Get the array data */
|
2006-02-19 12:29:37 +00:00
|
|
|
Type* ElementType = GetElementType (T);
|
2003-03-16 14:31:48 +00:00
|
|
|
unsigned ElementSize = CheckedSizeOf (ElementType);
|
|
|
|
long ElementCount = GetElementCount (T);
|
|
|
|
|
|
|
|
/* Special handling for a character array initialized by a literal */
|
2004-09-23 17:34:03 +00:00
|
|
|
if (IsTypeChar (ElementType) &&
|
|
|
|
(CurTok.Tok == TOK_SCONST ||
|
|
|
|
(CurTok.Tok == TOK_LCURLY && NextTok.Tok == TOK_SCONST))) {
|
2003-03-16 14:31:48 +00:00
|
|
|
|
|
|
|
/* Char array initialized by string constant */
|
2004-09-23 17:34:03 +00:00
|
|
|
int NeedParen;
|
|
|
|
const char* Str;
|
|
|
|
|
|
|
|
/* If we initializer is enclosed in brackets, remember this fact and
|
|
|
|
* skip the opening bracket.
|
|
|
|
*/
|
|
|
|
NeedParen = (CurTok.Tok == TOK_LCURLY);
|
|
|
|
if (NeedParen) {
|
|
|
|
NextToken ();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the initializer string and its size */
|
|
|
|
Str = GetLiteral (CurTok.IVal);
|
2003-03-16 14:31:48 +00:00
|
|
|
Count = GetLiteralPoolOffs () - CurTok.IVal;
|
|
|
|
|
|
|
|
/* Translate into target charset */
|
|
|
|
TranslateLiteralPool (CurTok.IVal);
|
2003-07-19 09:12:38 +00:00
|
|
|
|
|
|
|
/* If the array is one too small for the string literal, omit the
|
|
|
|
* trailing zero.
|
|
|
|
*/
|
|
|
|
if (ElementCount != UNSPECIFIED &&
|
|
|
|
ElementCount != FLEXIBLE &&
|
|
|
|
Count == ElementCount + 1) {
|
|
|
|
/* Omit the trailing zero */
|
|
|
|
--Count;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Output the data */
|
2003-03-16 14:31:48 +00:00
|
|
|
g_defbytes (Str, Count);
|
|
|
|
|
|
|
|
/* Remove string from pool */
|
|
|
|
ResetLiteralPoolOffs (CurTok.IVal);
|
|
|
|
NextToken ();
|
|
|
|
|
2004-09-23 17:34:03 +00:00
|
|
|
/* If the initializer was enclosed in curly braces, we need a closing
|
|
|
|
* one.
|
|
|
|
*/
|
|
|
|
if (NeedParen) {
|
|
|
|
ConsumeRCurly ();
|
|
|
|
}
|
|
|
|
|
2003-03-16 14:31:48 +00:00
|
|
|
} else {
|
|
|
|
|
2003-03-16 14:47:42 +00:00
|
|
|
/* Curly brace */
|
|
|
|
ConsumeLCurly ();
|
2003-03-16 14:31:48 +00:00
|
|
|
|
|
|
|
/* Initialize the array members */
|
|
|
|
Count = 0;
|
|
|
|
while (CurTok.Tok != TOK_RCURLY) {
|
|
|
|
/* Flexible array members may not be initialized within
|
|
|
|
* an array (because the size of each element may differ
|
|
|
|
* otherwise).
|
|
|
|
*/
|
|
|
|
ParseInitInternal (ElementType, 0);
|
|
|
|
++Count;
|
|
|
|
if (CurTok.Tok != TOK_COMMA)
|
|
|
|
break;
|
|
|
|
NextToken ();
|
|
|
|
}
|
|
|
|
|
2003-03-16 14:47:42 +00:00
|
|
|
/* Closing curly braces */
|
|
|
|
ConsumeRCurly ();
|
2003-03-16 14:31:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ElementCount == UNSPECIFIED) {
|
|
|
|
/* Number of elements determined by initializer */
|
2006-02-19 12:29:37 +00:00
|
|
|
SetElementCount (T, Count);
|
2003-03-16 14:31:48 +00:00
|
|
|
ElementCount = Count;
|
|
|
|
} else if (ElementCount == FLEXIBLE && AllowFlexibleMembers) {
|
|
|
|
/* In non ANSI mode, allow initialization of flexible array
|
|
|
|
* members.
|
|
|
|
*/
|
|
|
|
ElementCount = Count;
|
|
|
|
} else if (Count < ElementCount) {
|
|
|
|
g_zerobytes ((ElementCount - Count) * ElementSize);
|
|
|
|
} else if (Count > ElementCount) {
|
|
|
|
Error ("Too many initializers");
|
|
|
|
}
|
|
|
|
return ElementCount * ElementSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2006-02-19 12:29:37 +00:00
|
|
|
static unsigned ParseStructInit (Type* T, int AllowFlexibleMembers)
|
2003-02-03 22:14:20 +00:00
|
|
|
/* Parse initialization of a struct or union. Return the number of data bytes. */
|
2000-05-28 13:40:48 +00:00
|
|
|
{
|
|
|
|
SymEntry* Entry;
|
|
|
|
SymTable* Tab;
|
2003-02-03 22:14:20 +00:00
|
|
|
unsigned StructSize;
|
|
|
|
unsigned Size;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2003-03-06 12:12:25 +00:00
|
|
|
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Consume the opening curly brace */
|
2003-03-16 14:47:42 +00:00
|
|
|
ConsumeLCurly ();
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/* Get a pointer to the struct entry from the type */
|
2006-02-19 12:29:37 +00:00
|
|
|
Entry = GetSymEntry (T);
|
2003-02-03 22:14:20 +00:00
|
|
|
|
|
|
|
/* Get the size of the struct from the symbol table entry */
|
|
|
|
StructSize = Entry->V.S.Size;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/* Check if this struct definition has a field table. If it doesn't, it
|
|
|
|
* is an incomplete definition.
|
|
|
|
*/
|
|
|
|
Tab = Entry->V.S.SymTab;
|
|
|
|
if (Tab == 0) {
|
2000-11-02 21:22:33 +00:00
|
|
|
Error ("Cannot initialize variables with incomplete type");
|
2003-03-06 12:12:25 +00:00
|
|
|
/* Try error recovery */
|
2003-03-16 14:47:42 +00:00
|
|
|
SkipInitializer (1);
|
2003-03-06 12:12:25 +00:00
|
|
|
/* Nothing initialized */
|
2003-02-03 22:14:20 +00:00
|
|
|
return 0;
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Get a pointer to the list of symbols */
|
|
|
|
Entry = Tab->SymHead;
|
2003-02-03 22:14:20 +00:00
|
|
|
|
|
|
|
/* Initialize fields */
|
|
|
|
Size = 0;
|
2001-05-22 09:32:24 +00:00
|
|
|
while (CurTok.Tok != TOK_RCURLY) {
|
2003-02-03 22:14:20 +00:00
|
|
|
if (Entry == 0) {
|
|
|
|
Error ("Too many initializers");
|
2003-03-16 14:47:42 +00:00
|
|
|
SkipInitializer (1);
|
2003-02-03 22:14:20 +00:00
|
|
|
return Size;
|
|
|
|
}
|
2003-02-05 22:02:48 +00:00
|
|
|
/* Parse initialization of one field. Flexible array members may
|
|
|
|
* only be initialized if they are the last field (or part of the
|
|
|
|
* last struct field).
|
|
|
|
*/
|
|
|
|
Size += ParseInitInternal (Entry->Type, AllowFlexibleMembers && Entry->NextSym == 0);
|
2003-02-03 22:14:20 +00:00
|
|
|
Entry = Entry->NextSym;
|
|
|
|
if (CurTok.Tok != TOK_COMMA)
|
|
|
|
break;
|
|
|
|
NextToken ();
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Consume the closing curly brace */
|
2003-03-16 14:47:42 +00:00
|
|
|
ConsumeRCurly ();
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/* If there are struct fields left, reserve additional storage */
|
2003-02-03 22:14:20 +00:00
|
|
|
if (Size < StructSize) {
|
|
|
|
g_zerobytes (StructSize - Size);
|
2003-02-05 22:02:48 +00:00
|
|
|
Size = StructSize;
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
2003-02-03 22:14:20 +00:00
|
|
|
|
2003-02-05 22:02:48 +00:00
|
|
|
/* Return the actual number of bytes initialized. This number may be
|
|
|
|
* larger than StructSize if flexible array members are present and were
|
|
|
|
* initialized (possible in non ANSI mode).
|
|
|
|
*/
|
|
|
|
return Size;
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2003-03-16 14:31:48 +00:00
|
|
|
static unsigned ParseVoidInit (void)
|
|
|
|
/* Parse an initialization of a void variable (special cc65 extension).
|
|
|
|
* Return the number of bytes initialized.
|
|
|
|
*/
|
|
|
|
{
|
2004-05-31 15:27:33 +00:00
|
|
|
ExprDesc Expr;
|
2003-03-16 14:31:48 +00:00
|
|
|
unsigned Size;
|
|
|
|
|
|
|
|
/* Opening brace */
|
2003-03-16 14:47:42 +00:00
|
|
|
ConsumeLCurly ();
|
2003-03-16 14:31:48 +00:00
|
|
|
|
|
|
|
/* Allow an arbitrary list of values */
|
|
|
|
Size = 0;
|
|
|
|
do {
|
2004-05-31 15:27:33 +00:00
|
|
|
ConstExpr (hie1, &Expr);
|
2006-02-19 12:29:37 +00:00
|
|
|
switch (UnqualifiedType (Expr.Type[0].C)) {
|
2003-03-16 14:31:48 +00:00
|
|
|
|
|
|
|
case T_SCHAR:
|
|
|
|
case T_UCHAR:
|
2004-05-31 15:27:33 +00:00
|
|
|
if (ED_IsConstAbsInt (&Expr)) {
|
2003-03-16 14:31:48 +00:00
|
|
|
/* Make it byte sized */
|
2004-06-06 14:48:59 +00:00
|
|
|
Expr.IVal &= 0xFF;
|
2003-03-16 14:31:48 +00:00
|
|
|
}
|
2004-05-31 15:27:33 +00:00
|
|
|
DefineData (&Expr);
|
2003-03-16 14:31:48 +00:00
|
|
|
Size += SIZEOF_CHAR;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case T_SHORT:
|
|
|
|
case T_USHORT:
|
|
|
|
case T_INT:
|
|
|
|
case T_UINT:
|
|
|
|
case T_PTR:
|
|
|
|
case T_ARRAY:
|
2004-05-31 15:27:33 +00:00
|
|
|
if (ED_IsConstAbsInt (&Expr)) {
|
2003-03-16 14:31:48 +00:00
|
|
|
/* Make it word sized */
|
2004-06-06 14:48:59 +00:00
|
|
|
Expr.IVal &= 0xFFFF;
|
2003-03-16 14:31:48 +00:00
|
|
|
}
|
2004-05-31 15:27:33 +00:00
|
|
|
DefineData (&Expr);
|
2003-03-16 14:31:48 +00:00
|
|
|
Size += SIZEOF_INT;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case T_LONG:
|
|
|
|
case T_ULONG:
|
2004-05-31 15:27:33 +00:00
|
|
|
if (ED_IsConstAbsInt (&Expr)) {
|
|
|
|
/* Make it dword sized */
|
2004-06-06 14:48:59 +00:00
|
|
|
Expr.IVal &= 0xFFFFFFFF;
|
2004-05-31 15:27:33 +00:00
|
|
|
}
|
|
|
|
DefineData (&Expr);
|
2003-03-16 14:31:48 +00:00
|
|
|
Size += SIZEOF_LONG;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
Error ("Illegal type in initialization");
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (CurTok.Tok != TOK_COMMA) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
NextToken ();
|
|
|
|
|
|
|
|
} while (CurTok.Tok != TOK_RCURLY);
|
|
|
|
|
|
|
|
/* Closing brace */
|
2003-03-16 14:47:42 +00:00
|
|
|
ConsumeRCurly ();
|
2003-03-16 14:31:48 +00:00
|
|
|
|
|
|
|
/* Return the number of bytes initialized */
|
|
|
|
return Size;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2006-02-19 12:29:37 +00:00
|
|
|
static unsigned ParseInitInternal (Type* T, int AllowFlexibleMembers)
|
2003-02-03 22:14:20 +00:00
|
|
|
/* Parse initialization of variables. Return the number of data bytes. */
|
2000-05-28 13:40:48 +00:00
|
|
|
{
|
2006-02-19 12:29:37 +00:00
|
|
|
switch (UnqualifiedType (T->C)) {
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2000-08-01 15:04:35 +00:00
|
|
|
case T_SCHAR:
|
2000-05-28 13:40:48 +00:00
|
|
|
case T_UCHAR:
|
|
|
|
case T_SHORT:
|
|
|
|
case T_USHORT:
|
|
|
|
case T_INT:
|
|
|
|
case T_UINT:
|
|
|
|
case T_LONG:
|
|
|
|
case T_ULONG:
|
2003-03-16 15:18:10 +00:00
|
|
|
return ParseScalarInit (T);
|
|
|
|
|
|
|
|
case T_PTR:
|
|
|
|
return ParsePointerInit (T);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
case T_ARRAY:
|
2003-03-16 14:31:48 +00:00
|
|
|
return ParseArrayInit (T, AllowFlexibleMembers);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
case T_STRUCT:
|
|
|
|
case T_UNION:
|
2003-02-05 22:02:48 +00:00
|
|
|
return ParseStructInit (T, AllowFlexibleMembers);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
case T_VOID:
|
2004-06-21 22:22:11 +00:00
|
|
|
if (IS_Get (&Standard) == STD_CC65) {
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Special cc65 extension in non ANSI mode */
|
2003-02-03 22:14:20 +00:00
|
|
|
return ParseVoidInit ();
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
|
|
|
|
default:
|
2000-11-02 21:22:33 +00:00
|
|
|
Error ("Illegal type");
|
2003-02-03 22:14:20 +00:00
|
|
|
return SIZEOF_CHAR;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2003-03-16 15:18:10 +00:00
|
|
|
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
|
2006-02-19 12:29:37 +00:00
|
|
|
unsigned ParseInit (Type* T)
|
2003-02-05 22:02:48 +00:00
|
|
|
/* Parse initialization of variables. Return the number of data bytes. */
|
|
|
|
{
|
2004-06-21 22:22:11 +00:00
|
|
|
/* Parse the initialization. Flexible array members can only be initialized
|
|
|
|
* in cc65 mode.
|
|
|
|
*/
|
|
|
|
unsigned Size = ParseInitInternal (T, IS_Get (&Standard) == STD_CC65);
|
2003-08-11 20:18:30 +00:00
|
|
|
|
|
|
|
/* The initialization may not generate code on global level, because code
|
|
|
|
* outside function scope will never get executed.
|
|
|
|
*/
|
|
|
|
if (HaveGlobalCode ()) {
|
|
|
|
Error ("Non constant initializers");
|
|
|
|
RemoveGlobalCode ();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the size needed for the initialization */
|
|
|
|
return Size;
|
2003-02-05 22:02:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-02-03 22:14:20 +00:00
|
|
|
|