2000-05-28 13:40:48 +00:00
|
|
|
/*
|
|
|
|
* declare.c
|
|
|
|
*
|
|
|
|
* Ullrich von Bassewitz, 20.06.1998
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
2000-08-01 18:45:08 +00:00
|
|
|
/* common */
|
|
|
|
#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"
|
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"
|
|
|
|
#include "symtab.h"
|
|
|
|
#include "declare.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
/* Forwards */
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void ParseTypeSpec (DeclSpec* D, int Default);
|
|
|
|
/* Parse a type specificier */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
/* internal functions */
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-08-01 18:45:08 +00:00
|
|
|
static type OptionalQualifiers (type Q)
|
|
|
|
/* Read type qualifiers if we have any */
|
2000-05-28 13:40:48 +00:00
|
|
|
{
|
2000-06-12 18:31:40 +00:00
|
|
|
while (curtok == TOK_CONST || curtok == TOK_VOLATILE) {
|
2000-08-01 18:45:08 +00:00
|
|
|
|
|
|
|
switch (curtok) {
|
|
|
|
|
|
|
|
case TOK_CONST:
|
|
|
|
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;
|
|
|
|
|
|
|
|
default:
|
|
|
|
/* Keep gcc silent */
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void optionalint (void)
|
|
|
|
/* Eat an optional "int" token */
|
|
|
|
{
|
2000-06-12 18:31:40 +00:00
|
|
|
if (curtok == 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void optionalsigned (void)
|
|
|
|
/* Eat an optional "signed" token */
|
|
|
|
{
|
2000-06-12 18:31:40 +00:00
|
|
|
if (curtok == 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 */
|
|
|
|
{
|
|
|
|
D->StorageClass = 0;
|
|
|
|
D->Type[0] = T_END;
|
|
|
|
D->Flags = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void InitDeclaration (Declaration* D)
|
|
|
|
/* Initialize the Declaration struct for use */
|
|
|
|
{
|
|
|
|
D->Ident[0] = '\0';
|
2000-08-01 18:45:08 +00:00
|
|
|
D->Type[0] = T_END;
|
2000-05-28 13:40:48 +00:00
|
|
|
D->T = D->Type;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 */
|
|
|
|
switch (curtok) {
|
|
|
|
|
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 */
|
2000-06-12 18:31:40 +00:00
|
|
|
if (curtok != 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;
|
2000-06-12 18:31:40 +00:00
|
|
|
while (curtok != TOK_RCURLY) {
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/* We expect an identifier */
|
2000-06-12 18:31:40 +00:00
|
|
|
if (curtok != TOK_IDENT) {
|
2000-11-02 21:22:33 +00:00
|
|
|
Error ("Identifier expected");
|
2000-05-28 13:40:48 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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 */
|
2000-06-12 18:31:40 +00:00
|
|
|
if (curtok == TOK_ASSIGN) {
|
2000-05-28 13:40:48 +00:00
|
|
|
struct expent lval;
|
2000-06-12 18:31:40 +00:00
|
|
|
NextToken ();
|
2000-05-28 13:40:48 +00:00
|
|
|
constexpr (&lval);
|
|
|
|
EnumVal = lval.e_const;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add an entry to the symbol table */
|
2001-03-23 21:27:48 +00:00
|
|
|
AddConstSym (Ident, type_int, SC_ENUM, EnumVal++);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/* Check for end of definition */
|
2000-06-12 18:31:40 +00:00
|
|
|
if (curtok != 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
|
|
|
}
|
|
|
|
ConsumeRCurly ();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static SymEntry* ParseStructDecl (const char* Name, type StructType)
|
|
|
|
/* Parse a struct/union declaration. */
|
|
|
|
{
|
|
|
|
|
|
|
|
unsigned Size;
|
|
|
|
unsigned Offs;
|
|
|
|
SymTable* FieldTab;
|
|
|
|
SymEntry* Entry;
|
|
|
|
|
|
|
|
|
2000-06-12 18:31:40 +00:00
|
|
|
if (curtok != 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 */
|
|
|
|
Size = 0;
|
2000-06-12 18:31:40 +00:00
|
|
|
while (curtok != TOK_RCURLY) {
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/* Get the type of the entry */
|
|
|
|
DeclSpec Spec;
|
|
|
|
InitDeclSpec (&Spec);
|
|
|
|
ParseTypeSpec (&Spec, -1);
|
|
|
|
|
|
|
|
/* Read fields with this type */
|
|
|
|
while (1) {
|
|
|
|
|
|
|
|
/* Get type and name of the struct field */
|
|
|
|
Declaration Decl;
|
|
|
|
ParseDecl (&Spec, &Decl, 0);
|
|
|
|
|
|
|
|
/* Add a field entry to the table */
|
|
|
|
AddLocalSym (Decl.Ident, Decl.Type, SC_SFLD, (StructType == T_STRUCT)? Size : 0);
|
|
|
|
|
|
|
|
/* Calculate offset of next field/size of the union */
|
|
|
|
Offs = SizeOf (Decl.Type);
|
|
|
|
if (StructType == T_STRUCT) {
|
|
|
|
Size += Offs;
|
|
|
|
} else {
|
|
|
|
if (Offs > Size) {
|
|
|
|
Size = Offs;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-06-12 18:31:40 +00:00
|
|
|
if (curtok != 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
|
|
|
}
|
|
|
|
ConsumeSemi ();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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 */
|
|
|
|
return AddStructSym (Name, Size, FieldTab);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void ParseTypeSpec (DeclSpec* D, int Default)
|
|
|
|
/* Parse a type specificier */
|
|
|
|
{
|
2000-08-01 18:45:08 +00:00
|
|
|
ident Ident;
|
2000-06-12 18:31:40 +00:00
|
|
|
SymEntry* Entry;
|
|
|
|
type StructType;
|
2000-08-01 18:45:08 +00:00
|
|
|
type Qualifiers; /* Type qualifiers */
|
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 */
|
|
|
|
Qualifiers = OptionalQualifiers (T_QUAL_NONE);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/* Look at the data type */
|
|
|
|
switch (curtok) {
|
|
|
|
|
2000-06-12 18:31:40 +00:00
|
|
|
case TOK_VOID:
|
|
|
|
NextToken ();
|
2000-05-28 13:40:48 +00:00
|
|
|
D->Type[0] = T_VOID;
|
|
|
|
D->Type[1] = T_END;
|
|
|
|
break;
|
|
|
|
|
2000-06-12 18:31:40 +00:00
|
|
|
case TOK_CHAR:
|
|
|
|
NextToken ();
|
2000-05-28 13:40:48 +00:00
|
|
|
D->Type[0] = GetDefaultChar();
|
|
|
|
D->Type[1] = T_END;
|
|
|
|
break;
|
|
|
|
|
2000-06-12 18:31:40 +00:00
|
|
|
case TOK_LONG:
|
|
|
|
NextToken ();
|
|
|
|
if (curtok == TOK_UNSIGNED) {
|
2000-08-01 18:45:08 +00:00
|
|
|
NextToken ();
|
|
|
|
optionalint ();
|
|
|
|
D->Type[0] = T_ULONG;
|
|
|
|
D->Type[1] = T_END;
|
2000-05-28 13:40:48 +00:00
|
|
|
} else {
|
2000-08-01 18:45:08 +00:00
|
|
|
optionalsigned ();
|
|
|
|
optionalint ();
|
|
|
|
D->Type[0] = T_LONG;
|
|
|
|
D->Type[1] = T_END;
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2000-06-12 18:31:40 +00:00
|
|
|
case TOK_SHORT:
|
|
|
|
NextToken ();
|
|
|
|
if (curtok == TOK_UNSIGNED) {
|
2000-08-01 18:45:08 +00:00
|
|
|
NextToken ();
|
|
|
|
optionalint ();
|
|
|
|
D->Type[0] = T_USHORT;
|
|
|
|
D->Type[1] = T_END;
|
2000-05-28 13:40:48 +00:00
|
|
|
} else {
|
2000-08-01 18:45:08 +00:00
|
|
|
optionalsigned ();
|
|
|
|
optionalint ();
|
|
|
|
D->Type[0] = T_SHORT;
|
2000-05-28 13:40:48 +00:00
|
|
|
D->Type[1] = T_END;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2000-06-12 18:31:40 +00:00
|
|
|
case TOK_INT:
|
|
|
|
NextToken ();
|
2000-05-28 13:40:48 +00:00
|
|
|
D->Type[0] = T_INT;
|
|
|
|
D->Type[1] = T_END;
|
|
|
|
break;
|
|
|
|
|
2000-06-12 18:31:40 +00:00
|
|
|
case TOK_SIGNED:
|
|
|
|
NextToken ();
|
2000-05-28 13:40:48 +00:00
|
|
|
switch (curtok) {
|
|
|
|
|
2000-06-12 18:31:40 +00:00
|
|
|
case TOK_CHAR:
|
|
|
|
NextToken ();
|
2000-10-10 19:35:32 +00:00
|
|
|
D->Type[0] = T_SCHAR;
|
2000-05-28 13:40:48 +00:00
|
|
|
D->Type[1] = T_END;
|
|
|
|
break;
|
|
|
|
|
2000-06-12 18:31:40 +00:00
|
|
|
case TOK_SHORT:
|
|
|
|
NextToken ();
|
2000-05-28 13:40:48 +00:00
|
|
|
optionalint ();
|
|
|
|
D->Type[0] = T_SHORT;
|
|
|
|
D->Type[1] = T_END;
|
|
|
|
break;
|
|
|
|
|
2000-06-12 18:31:40 +00:00
|
|
|
case TOK_LONG:
|
|
|
|
NextToken ();
|
2000-05-28 13:40:48 +00:00
|
|
|
optionalint ();
|
|
|
|
D->Type[0] = T_LONG;
|
|
|
|
D->Type[1] = T_END;
|
|
|
|
break;
|
|
|
|
|
2000-06-12 18:31:40 +00:00
|
|
|
case TOK_INT:
|
|
|
|
NextToken ();
|
2000-05-28 13:40:48 +00:00
|
|
|
/* FALL THROUGH */
|
|
|
|
|
|
|
|
default:
|
|
|
|
D->Type[0] = T_INT;
|
|
|
|
D->Type[1] = T_END;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2000-06-12 18:31:40 +00:00
|
|
|
case TOK_UNSIGNED:
|
|
|
|
NextToken ();
|
2000-05-28 13:40:48 +00:00
|
|
|
switch (curtok) {
|
|
|
|
|
2000-06-12 18:31:40 +00:00
|
|
|
case TOK_CHAR:
|
|
|
|
NextToken ();
|
2000-05-28 13:40:48 +00:00
|
|
|
D->Type[0] = T_UCHAR;
|
|
|
|
D->Type[1] = 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 ();
|
2000-05-28 13:40:48 +00:00
|
|
|
optionalint ();
|
|
|
|
D->Type[0] = T_USHORT;
|
|
|
|
D->Type[1] = T_END;
|
|
|
|
break;
|
|
|
|
|
2000-06-12 18:31:40 +00:00
|
|
|
case TOK_LONG:
|
|
|
|
NextToken ();
|
2000-05-28 13:40:48 +00:00
|
|
|
optionalint ();
|
|
|
|
D->Type[0] = T_ULONG;
|
|
|
|
D->Type[1] = T_END;
|
|
|
|
break;
|
|
|
|
|
2000-06-12 18:31:40 +00:00
|
|
|
case TOK_INT:
|
|
|
|
NextToken ();
|
2000-05-28 13:40:48 +00:00
|
|
|
/* FALL THROUGH */
|
|
|
|
|
|
|
|
default:
|
|
|
|
D->Type[0] = T_UINT;
|
|
|
|
D->Type[1] = T_END;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2000-06-12 18:31:40 +00:00
|
|
|
case TOK_STRUCT:
|
|
|
|
case TOK_UNION:
|
|
|
|
StructType = (curtok == TOK_STRUCT)? T_STRUCT : T_UNION;
|
|
|
|
NextToken ();
|
|
|
|
/* */
|
|
|
|
if (curtok == 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 */
|
|
|
|
D->Type[0] = StructType;
|
|
|
|
EncodePtr (D->Type+1, Entry);
|
|
|
|
D->Type[DECODE_SIZE+1] = T_END;
|
|
|
|
break;
|
|
|
|
|
2000-06-12 18:31:40 +00:00
|
|
|
case TOK_ENUM:
|
|
|
|
NextToken ();
|
|
|
|
if (curtok != TOK_LCURLY) {
|
|
|
|
/* Named enum */
|
|
|
|
if (curtok == TOK_IDENT) {
|
|
|
|
/* Find an entry with this name */
|
|
|
|
Entry = FindTagSym (CurTok.Ident);
|
|
|
|
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
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* Insert entry into table ### */
|
|
|
|
}
|
|
|
|
/* Skip the identifier */
|
|
|
|
NextToken ();
|
|
|
|
} else {
|
2000-11-02 21:22:33 +00:00
|
|
|
Error ("Identifier expected");
|
2000-06-12 18:31:40 +00:00
|
|
|
}
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
2000-06-12 18:31: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 ();
|
|
|
|
D->Type[0] = T_INT;
|
|
|
|
D->Type[1] = T_END;
|
|
|
|
break;
|
|
|
|
|
2000-06-12 18:31:40 +00:00
|
|
|
case TOK_IDENT:
|
2000-05-28 13:40:48 +00:00
|
|
|
Entry = FindSym (CurTok.Ident);
|
|
|
|
if (Entry && IsTypeDef (Entry)) {
|
|
|
|
/* It's a typedef */
|
2000-06-12 18:31:40 +00:00
|
|
|
NextToken ();
|
2000-05-28 13:40:48 +00:00
|
|
|
TypeCpy (D->Type, Entry->Type);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* FALL THROUGH */
|
|
|
|
|
|
|
|
default:
|
|
|
|
if (Default < 0) {
|
2000-11-02 21:22:33 +00:00
|
|
|
Error ("Type expected");
|
2000-05-28 13:40:48 +00:00
|
|
|
D->Type[0] = T_INT;
|
|
|
|
D->Type[1] = T_END;
|
|
|
|
} else {
|
|
|
|
D->Flags |= DS_DEF_TYPE;
|
|
|
|
D->Type[0] = (type) Default;
|
|
|
|
D->Type[1] = T_END;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2000-08-01 18:45:08 +00:00
|
|
|
|
|
|
|
/* There may also be qualifiers *after* the initial type */
|
|
|
|
D->Type[0] |= OptionalQualifiers (Qualifiers);
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-06-12 18:31:40 +00:00
|
|
|
static type* ParamTypeCvt (type* T)
|
|
|
|
/* 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)) {
|
2000-06-12 18:31:40 +00:00
|
|
|
T += DECODE_SIZE;
|
|
|
|
T[0] = T_PTR;
|
|
|
|
}
|
|
|
|
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 */
|
|
|
|
while (curtok != TOK_RPAREN) {
|
|
|
|
|
|
|
|
/* List of identifiers expected */
|
|
|
|
if (curtok != 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 */
|
|
|
|
AddLocalSym (CurTok.Ident, type_int, SC_AUTO | SC_PARAM | SC_DEF, 0);
|
|
|
|
|
|
|
|
/* Count arguments */
|
|
|
|
++F->ParamCount;
|
|
|
|
|
|
|
|
/* Skip the identifier */
|
|
|
|
NextToken ();
|
|
|
|
|
|
|
|
/* Check for more parameters */
|
|
|
|
if (curtok == TOK_COMMA) {
|
|
|
|
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 */
|
|
|
|
while (curtok != TOK_LCURLY) {
|
|
|
|
|
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 &&
|
|
|
|
(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) {
|
|
|
|
|
|
|
|
Declaration Decl;
|
|
|
|
|
|
|
|
/* Read the parameter */
|
|
|
|
ParseDecl (&Spec, &Decl, DM_NEED_IDENT);
|
|
|
|
if (Decl.Ident[0] != '\0') {
|
|
|
|
|
|
|
|
/* We have a name given. Search for the symbol */
|
|
|
|
SymEntry* Sym = FindLocalSym (Decl.Ident);
|
|
|
|
if (Sym) {
|
|
|
|
/* Found it, change the default type to the one given */
|
|
|
|
ChangeSymType (Sym, ParamTypeCvt (Decl.Type));
|
|
|
|
} else {
|
2000-11-02 21:22:33 +00:00
|
|
|
Error ("Unknown identifier: `%s'", Decl.Ident);
|
2000-06-12 18:31:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (curtok == TOK_COMMA) {
|
|
|
|
NextToken ();
|
|
|
|
} 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 */
|
2000-06-12 18:31:40 +00:00
|
|
|
while (curtok != TOK_RPAREN) {
|
|
|
|
|
|
|
|
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 */
|
2000-06-12 18:31:40 +00:00
|
|
|
if (curtok == TOK_ELLIPSIS) {
|
|
|
|
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);
|
|
|
|
|
|
|
|
/* We accept only auto and register as storage class specifiers, but
|
|
|
|
* we ignore all this and use auto.
|
|
|
|
*/
|
|
|
|
if ((Spec.StorageClass & SC_AUTO) == 0 &&
|
|
|
|
(Spec.StorageClass & SC_REGISTER) == 0) {
|
2000-11-02 21:22:33 +00:00
|
|
|
Error ("Illegal storage class");
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
Spec.StorageClass = SC_AUTO | SC_PARAM | SC_DEF;
|
|
|
|
|
|
|
|
/* 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 */
|
2000-06-12 18:31:40 +00:00
|
|
|
if (curtok == TOK_COMMA) {
|
|
|
|
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 */
|
|
|
|
if (curtok == TOK_LCURLY) {
|
|
|
|
/* Print an error if in strict ANSI mode and we have unnamed
|
|
|
|
* parameters.
|
|
|
|
*/
|
|
|
|
if (ANSI && (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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static FuncDesc* ParseFuncDecl (void)
|
|
|
|
/* 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 */
|
|
|
|
if (curtok == TOK_RPAREN) {
|
|
|
|
/* Parameter list is empty */
|
2001-03-24 13:00:16 +00:00
|
|
|
F->Flags |= (FD_EMPTY | FD_VARIADIC);
|
2000-06-12 18:31:40 +00:00
|
|
|
} else if (curtok == TOK_VOID && nxttok == TOK_RPAREN) {
|
|
|
|
/* Parameter list declared as void */
|
|
|
|
NextToken ();
|
|
|
|
F->Flags |= FD_VOID_PARAM;
|
|
|
|
} else if (curtok == TOK_IDENT && (nxttok == TOK_COMMA || nxttok == TOK_RPAREN)) {
|
|
|
|
/* Old style (K&R) function. Assume variable param list. */
|
2001-03-24 13:00:16 +00:00
|
|
|
F->Flags |= (FD_OLDSTYLE | FD_VARIADIC);
|
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);
|
|
|
|
}
|
|
|
|
|
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;
|
2000-05-28 13:40:48 +00:00
|
|
|
Sym = GetSymTab()->SymTail;
|
|
|
|
while (Sym) {
|
2000-06-12 18:31:40 +00:00
|
|
|
unsigned Size = SizeOf (Sym->Type);
|
2000-05-28 13:40:48 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/* Leave the lexical level remembering the symbol tables */
|
|
|
|
RememberFunctionLevel (F);
|
|
|
|
|
|
|
|
/* Return the function descriptor */
|
|
|
|
return F;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void Decl (Declaration* D, unsigned Mode)
|
|
|
|
/* Recursively process declarators. Build a type array in reverse order. */
|
|
|
|
{
|
2000-08-01 18:45:08 +00:00
|
|
|
|
2000-06-12 18:31:40 +00:00
|
|
|
if (curtok == TOK_STAR) {
|
2000-08-01 18:45:08 +00:00
|
|
|
type T = T_PTR;
|
2000-06-12 18:31:40 +00:00
|
|
|
NextToken ();
|
2000-08-01 18:45:08 +00:00
|
|
|
/* Allow optional const or volatile qualifiers */
|
|
|
|
T |= OptionalQualifiers (T_QUAL_NONE);
|
2000-05-28 13:40:48 +00:00
|
|
|
Decl (D, Mode);
|
2000-08-01 18:45:08 +00:00
|
|
|
*D->T++ = T;
|
2000-05-28 13:40:48 +00:00
|
|
|
return;
|
2000-06-12 18:31:40 +00:00
|
|
|
} else if (curtok == TOK_LPAREN) {
|
|
|
|
NextToken ();
|
2000-05-28 13:40:48 +00:00
|
|
|
Decl (D, Mode);
|
|
|
|
ConsumeRParen ();
|
2000-06-12 18:31:40 +00:00
|
|
|
} else if (curtok == TOK_FASTCALL) {
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Remember the current type pointer */
|
|
|
|
type* T = D->T;
|
|
|
|
/* Skip the fastcall token */
|
2000-06-12 18:31:40 +00:00
|
|
|
NextToken ();
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Parse the function */
|
|
|
|
Decl (D, Mode);
|
|
|
|
/* Set the fastcall flag */
|
2000-08-01 18:45:08 +00:00
|
|
|
if (!IsTypeFunc (T)) {
|
2000-11-02 21:22:33 +00:00
|
|
|
Error ("__fastcall__ modifier applied to non function");
|
2001-03-23 19:17:04 +00:00
|
|
|
} else if (IsVariadicFunc (T)) {
|
2001-03-22 21:02:01 +00:00
|
|
|
Error ("Cannot apply __fastcall__ to functions with variable parameter list");
|
2001-03-23 19:17:04 +00:00
|
|
|
} else {
|
|
|
|
FuncDesc* F = (FuncDesc*) DecodePtr (T+1);
|
2000-05-28 13:40:48 +00:00
|
|
|
F->Flags |= FD_FASTCALL;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
} 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:
|
|
|
|
* we must have a type but no variable identifer
|
|
|
|
* (if there is one, it's not read).
|
|
|
|
* - Mode == DM_ACCEPT_IDENT means:
|
|
|
|
* we *may* have an identifier. If there is an identifier,
|
|
|
|
* it is read, but it is no error, if there is none.
|
|
|
|
*/
|
|
|
|
if (Mode == DM_NO_IDENT) {
|
|
|
|
D->Ident[0] = '\0';
|
|
|
|
} else if (curtok == 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';
|
|
|
|
return;
|
|
|
|
}
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
2000-06-12 18:31:40 +00:00
|
|
|
while (curtok == TOK_LBRACK || curtok == TOK_LPAREN) {
|
|
|
|
if (curtok == TOK_LPAREN) {
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Function declaration */
|
|
|
|
FuncDesc* F;
|
2000-06-12 18:31:40 +00:00
|
|
|
NextToken ();
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Parse the function declaration */
|
|
|
|
F = ParseFuncDecl ();
|
|
|
|
*D->T++ = T_FUNC;
|
|
|
|
EncodePtr (D->T, F);
|
|
|
|
D->T += DECODE_SIZE;
|
|
|
|
} else {
|
|
|
|
/* Array declaration */
|
|
|
|
unsigned long Size = 0;
|
2000-06-12 18:31:40 +00:00
|
|
|
NextToken ();
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Read the size if it is given */
|
2000-06-12 18:31:40 +00:00
|
|
|
if (curtok != TOK_RBRACK) {
|
2000-05-28 13:40:48 +00:00
|
|
|
struct expent lval;
|
|
|
|
constexpr (&lval);
|
|
|
|
Size = lval.e_const;
|
|
|
|
}
|
|
|
|
ConsumeRBrack ();
|
|
|
|
*D->T++ = T_ARRAY;
|
|
|
|
Encode (D->T, Size);
|
|
|
|
D->T += DECODE_SIZE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
/* code */
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
type* ParseType (type* Type)
|
|
|
|
/* Parse a complete type specification */
|
|
|
|
{
|
|
|
|
DeclSpec Spec;
|
|
|
|
Declaration Decl;
|
|
|
|
|
|
|
|
/* Get a type without a default */
|
|
|
|
InitDeclSpec (&Spec);
|
|
|
|
ParseTypeSpec (&Spec, -1);
|
|
|
|
|
|
|
|
/* Parse additional declarators */
|
|
|
|
InitDeclaration (&Decl);
|
|
|
|
ParseDecl (&Spec, &Decl, DM_NO_IDENT);
|
|
|
|
|
|
|
|
/* Copy the type to the target buffer */
|
|
|
|
TypeCpy (Type, Decl.Type);
|
|
|
|
|
|
|
|
/* Return a pointer to the target buffer */
|
|
|
|
return Type;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 */
|
|
|
|
Decl (D, Mode);
|
|
|
|
|
|
|
|
/* Add the base type. */
|
|
|
|
TypeCpy (D->T, Spec->Type);
|
|
|
|
|
|
|
|
/* Check the size of the generated type */
|
2000-08-01 18:45:08 +00:00
|
|
|
if (!IsTypeFunc (D->Type) && !IsTypeVoid (D->Type) && SizeOf (D->Type) >= 0x10000) {
|
2000-11-02 21:22:33 +00:00
|
|
|
if (D->Ident[0] != '\0') {
|
|
|
|
Error ("Size of `%s' is invalid", D->Ident);
|
|
|
|
} else {
|
|
|
|
Error ("Invalid size");
|
|
|
|
}
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void ParseDeclSpec (DeclSpec* D, unsigned DefStorage, int DefType)
|
|
|
|
/* Parse a declaration specification */
|
|
|
|
{
|
|
|
|
/* Initialize the DeclSpec struct */
|
|
|
|
InitDeclSpec (D);
|
|
|
|
|
|
|
|
/* First, get the storage class specifier for this declaration */
|
|
|
|
ParseStorageClass (D, DefStorage);
|
|
|
|
|
|
|
|
/* Parse the type specifiers */
|
|
|
|
ParseTypeSpec (D, DefType);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-05-28 13:40:48 +00:00
|
|
|
static void ParseVoidInit (void)
|
|
|
|
/* Parse an initialization of a void variable (special cc65 extension) */
|
|
|
|
{
|
|
|
|
struct expent lval;
|
|
|
|
|
|
|
|
/* Allow an arbitrary list of values */
|
|
|
|
ConsumeLCurly ();
|
|
|
|
do {
|
|
|
|
constexpr (&lval);
|
|
|
|
switch (lval.e_tptr[0]) {
|
|
|
|
|
2000-08-01 15:04:35 +00:00
|
|
|
case T_SCHAR:
|
2000-05-28 13:40:48 +00:00
|
|
|
case T_UCHAR:
|
|
|
|
if ((lval.e_flags & E_MCTYPE) == E_TCONST) {
|
|
|
|
/* Make it byte sized */
|
|
|
|
lval.e_const &= 0xFF;
|
|
|
|
}
|
|
|
|
DefineData (&lval);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case T_SHORT:
|
|
|
|
case T_USHORT:
|
|
|
|
case T_INT:
|
|
|
|
case T_UINT:
|
|
|
|
case T_PTR:
|
|
|
|
case T_ARRAY:
|
|
|
|
if ((lval.e_flags & E_MCTYPE) == E_TCONST) {
|
2000-06-12 18:31:40 +00:00
|
|
|
/* Make it word sized */
|
2000-05-28 13:40:48 +00:00
|
|
|
lval.e_const &= 0xFFFF;
|
|
|
|
}
|
|
|
|
DefineData (&lval);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case T_LONG:
|
|
|
|
case T_ULONG:
|
|
|
|
DefineData (&lval);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2000-11-02 21:22:33 +00:00
|
|
|
Error ("Illegal type in initialization");
|
2000-05-28 13:40:48 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2000-06-12 18:31:40 +00:00
|
|
|
if (curtok != 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
|
|
|
|
2000-06-12 18:31:40 +00:00
|
|
|
} while (curtok != TOK_RCURLY);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
ConsumeRCurly ();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void ParseStructInit (type* Type)
|
|
|
|
/* Parse initialization of a struct or union */
|
|
|
|
{
|
|
|
|
SymEntry* Entry;
|
|
|
|
SymTable* Tab;
|
|
|
|
|
|
|
|
/* Consume the opening curly brace */
|
|
|
|
ConsumeLCurly ();
|
|
|
|
|
|
|
|
/* Get a pointer to the struct entry from the type */
|
|
|
|
Entry = (SymEntry*) Decode (Type + 1);
|
|
|
|
|
|
|
|
/* 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");
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Returning here will cause lots of errors, but recovery is difficult */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get a pointer to the list of symbols */
|
|
|
|
Entry = Tab->SymHead;
|
2000-06-12 18:31:40 +00:00
|
|
|
while (curtok != TOK_RCURLY) {
|
|
|
|
if (Entry == 0) {
|
2000-11-02 21:22:33 +00:00
|
|
|
Error ("Too many initializers");
|
2000-05-28 13:40:48 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
ParseInit (Entry->Type);
|
|
|
|
Entry = Entry->NextSym;
|
2000-06-12 18:31:40 +00:00
|
|
|
if (curtok != 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
|
|
|
}
|
|
|
|
|
|
|
|
/* Consume the closing curly brace */
|
|
|
|
ConsumeRCurly ();
|
|
|
|
|
|
|
|
/* If there are struct fields left, reserve additional storage */
|
|
|
|
while (Entry) {
|
|
|
|
g_zerobytes (SizeOf (Entry->Type));
|
|
|
|
Entry = Entry->NextSym;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-08-01 18:45:08 +00:00
|
|
|
void ParseInit (type* T)
|
|
|
|
/* Parse initialization of variables. */
|
2000-05-28 13:40:48 +00:00
|
|
|
{
|
|
|
|
int count;
|
|
|
|
struct expent lval;
|
|
|
|
type* t;
|
|
|
|
const char* str;
|
|
|
|
int sz;
|
|
|
|
|
2000-08-01 18:45:08 +00:00
|
|
|
switch (UnqualifiedType (*T)) {
|
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:
|
|
|
|
constexpr (&lval);
|
|
|
|
if ((lval.e_flags & E_MCTYPE) == E_TCONST) {
|
|
|
|
/* Make it byte sized */
|
|
|
|
lval.e_const &= 0xFF;
|
|
|
|
}
|
2000-08-01 18:45:08 +00:00
|
|
|
assignadjust (T, &lval);
|
2000-05-28 13:40:48 +00:00
|
|
|
DefineData (&lval);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case T_SHORT:
|
|
|
|
case T_USHORT:
|
|
|
|
case T_INT:
|
|
|
|
case T_UINT:
|
|
|
|
case T_PTR:
|
|
|
|
constexpr (&lval);
|
|
|
|
if ((lval.e_flags & E_MCTYPE) == E_TCONST) {
|
|
|
|
/* Make it word sized */
|
|
|
|
lval.e_const &= 0xFFFF;
|
|
|
|
}
|
2000-08-01 18:45:08 +00:00
|
|
|
assignadjust (T, &lval);
|
2000-05-28 13:40:48 +00:00
|
|
|
DefineData (&lval);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case T_LONG:
|
|
|
|
case T_ULONG:
|
|
|
|
constexpr (&lval);
|
|
|
|
if ((lval.e_flags & E_MCTYPE) == E_TCONST) {
|
|
|
|
/* Make it long sized */
|
|
|
|
lval.e_const &= 0xFFFFFFFF;
|
|
|
|
}
|
2000-08-01 18:45:08 +00:00
|
|
|
assignadjust (T, &lval);
|
2000-05-28 13:40:48 +00:00
|
|
|
DefineData (&lval);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case T_ARRAY:
|
2000-08-01 18:45:08 +00:00
|
|
|
sz = Decode (T + 1);
|
|
|
|
t = T + DECODE_SIZE + 1;
|
|
|
|
if (IsTypeChar(t) && curtok == TOK_SCONST) {
|
2000-05-28 13:40:48 +00:00
|
|
|
str = GetLiteral (curval);
|
|
|
|
count = strlen (str) + 1;
|
|
|
|
TranslateLiteralPool (curval); /* Translate into target charset */
|
|
|
|
g_defbytes (str, count);
|
|
|
|
ResetLiteralOffs (curval); /* Remove string from pool */
|
2000-06-12 18:31:40 +00:00
|
|
|
NextToken ();
|
2000-05-28 13:40:48 +00:00
|
|
|
} else {
|
|
|
|
ConsumeLCurly ();
|
|
|
|
count = 0;
|
2000-06-12 18:31:40 +00:00
|
|
|
while (curtok != TOK_RCURLY) {
|
2000-08-01 18:45:08 +00:00
|
|
|
ParseInit (T + DECODE_SIZE + 1);
|
2000-05-28 13:40:48 +00:00
|
|
|
++count;
|
2000-06-12 18:31:40 +00:00
|
|
|
if (curtok != 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
|
|
|
}
|
|
|
|
ConsumeRCurly ();
|
|
|
|
}
|
|
|
|
if (sz == 0) {
|
2000-08-01 18:45:08 +00:00
|
|
|
Encode (T + 1, count);
|
2000-05-28 13:40:48 +00:00
|
|
|
} else if (count < sz) {
|
2000-08-01 18:45:08 +00:00
|
|
|
g_zerobytes ((sz - count) * SizeOf (T + DECODE_SIZE + 1));
|
2000-05-28 13:40:48 +00:00
|
|
|
} else if (count > sz) {
|
2000-11-02 21:22:33 +00:00
|
|
|
Error ("Too many initializers");
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case T_STRUCT:
|
|
|
|
case T_UNION:
|
2000-08-01 18:45:08 +00:00
|
|
|
ParseStructInit (T);
|
2000-05-28 13:40:48 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case T_VOID:
|
|
|
|
if (!ANSI) {
|
|
|
|
/* Special cc65 extension in non ANSI mode */
|
|
|
|
ParseVoidInit ();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
|
|
|
|
default:
|
2000-11-02 21:22:33 +00:00
|
|
|
Error ("Illegal type");
|
2000-05-28 13:40:48 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|