#ifndef SYMBOL_HPP #define SYMBOL_HPP #include #include #include #include #include "gc_cpp.h" // Garbage collector #include "sym_custom.h" #include "errorhnd.h" //Forward declarations struct Cname; struct Ctype; struct gc_enum_id; //****************************************************** // Definition of abstract class SymbolTableEntry //****************************************************** class SymbolTableEntry : public gc_cleanup { protected : char * EntryName; int NestingLevel; public : SymbolTableEntry(char *name); virtual ~SymbolTableEntry(void); virtual EntityTypes WhatIs(void)=0; virtual int operator == (SymbolTableEntry& entry); virtual int operator == (char *name); void SetNestingLevel(int nl) { NestingLevel = nl; } int GetNestingLevel(void) { return NestingLevel; } char *GetName(void){ return EntryName; } void SetName(char *NewName) { if (EntryName != NULL) free(EntryName); EntryName = strdup(NewName); } virtual ostream& Print(ostream&)=0; friend ostream& operator<<(ostream&out, SymbolTableEntry &a) { return a.Print(out); //REMEMBER this is a virtual call } }; struct LessStrObj { bool operator()(const char*op1,const char*op2) const { if(strcmp(op1,op2)<0) return true; return false; } }; //****************************************************** // Definition of class Scope //****************************************************** //MapType is the data-structure used for locating a SymbolTableEntry //by it's name //ListType is the data-structure giving the original order //e.g. the order the user entered //of the SymbolTableEntries typedef map< char*,SymbolTableEntry*,LessStrObj > MapType; typedef list< SymbolTableEntry* > ListType; class Scope : public gc_cleanup { protected : char *ScopeName; Scope *EnclosingScope; MapType Entity; ListType ListEntity; ErrorHandlerClass *ErrorHandler; int NestingLevel; void dummy1(); //Used only for template instantiation... public : Scope(Scope *, char *, ErrorHandlerClass *, int); ~Scope(void); Scope *GetEnclosing(void) { return EnclosingScope; } int GetNestingLevel(void) { return NestingLevel; } MapType* GetEntity() { return &Entity; } ListType* GetListEntity() { return &ListEntity; } char *GetScopeName(void) { return ScopeName; } void SetScopeName(char *NewName) { if (ScopeName != NULL) free(ScopeName); ScopeName = strdup(NewName); if (ScopeName == NULL) ErrorHandler->Internal("Not enough memory"); } ReturnCodes Insert(SymbolTableEntry *); SymbolTableEntry *Lookup(char *, int = SEARCH_ALL_SCOPES); ostream& Print(ostream&); friend ostream& operator<<(ostream&out,Scope&sc) { return sc.Print(out); } }; //****************************************************** // Definition of class SymbolTableClass //****************************************************** class SymbolTableClass { protected: Scope *CurrentScope; ErrorHandlerClass *ErrorHandler; public: SymbolTableClass(ErrorHandlerClass * = NULL); ~SymbolTableClass(void); void SetErrorHandler(ErrorHandlerClass *eh) { ErrorHandler = eh; } void OpenScope(char *); void CloseScope(void); Scope* Up(); Scope *GetCurrentScope(void) { return CurrentScope; } char *GetScopeName(void) { return CurrentScope->GetScopeName(); } ReturnCodes Insert(SymbolTableEntry *); SymbolTableEntry* Lookup(char *, int = SEARCH_ALL_SCOPES); int GetNestingLevelOf(char *); ostream& Print(ostream&); friend ostream& operator<<(ostream&out,SymbolTableClass&s) { return s.Print(out); } }; //****************************************************** // Definition of class TypeEntry //****************************************************** class TypeEntry : public SymbolTableEntry { protected: Ctype* c_type; Cname* c_name; int size_in_bytes; //the "SIZEOF" int alignment; //the alignment public: TypeEntry(char *str,int size,int align); //for built-ins TypeEntry(Cname*,Ctype*); //for typedefs TypeEntry(char *,Ctype*); //for "structs" virtual ~TypeEntry(); virtual EntityTypes WhatIs(void) { return TypeEnt; } virtual Scope *GetScope(); virtual Ctype* GetCtype(); virtual Cname* GetCname(); virtual int GetSize() { return size_in_bytes; } virtual int GetAlignment() { return alignment; } virtual void SetAlignment(int al) { alignment=al; } virtual void SetSize(int s) { size_in_bytes=s; } virtual bool IsSimple(); ostream& Print(ostream&); }; //****************************************************** // Definition of class VariableEntry //****************************************************** class VariableEntry : public SymbolTableEntry { protected : Cname *c_name; Ctype *c_type; int alignment; //The "local" alignment, since each var //could have a user-defined alignment public : VariableEntry(Cname *, Ctype *); virtual ~VariableEntry(void); virtual EntityTypes WhatIs(void) { return VariableEnt; } virtual Cname* GetCname(); virtual Ctype* GetCtype(); virtual int GetAlignment() { return alignment; } virtual void SetAlignment(int al) { alignment=al; } virtual int GetSize(); ostream& Print(ostream&); }; //****************************************************** // Definition of class ParameterEntry //****************************************************** /* class ParameterEntry : public VariableEntry { public: ParameterEntry() : VariableEntry(NULL,NULL) {} }; */ //****************************************************** // Definition of class EnumEntry //****************************************************** class EnumEntry : public SymbolTableEntry { protected: gc_enum_id *enum_id; Ctype *c_type; public: EntityTypes WhatIs(void) { return EnumEnt; } ostream& Print(ostream&); EnumEntry(gc_enum_id *,Ctype *); gc_enum_id *GetEnumID() {return enum_id;} Ctype* GetCtype() {return c_type;} }; #endif