#ifndef _SEMANT #define _SEMANT /* * Contains Semant, VarInfo, FieldInfo, FormalInfo, LocalInfo, and ThisInfo * classes. * created by CMB 8/04 * * MOD 10/3/06 by CMB * changed VarInfo* << operator to take pointer to const object * and fixed code therein * * See documentation below for details of the various classes. * */ #include using namespace std; class Program; class Class; // forward decl of SymTab template type template class SymTab; typedef SymTab ClassTable; // defined in espresso.y extern Program *ast; // defined in espresso.y extern StringTab stringtab; // defined in testsemant.cc extern bool semantDebug; /* * Semant class * * encapsulates vars needed for semantic analysis * and algs used. * The constuctor passes in the ast for the program to analyze * and the name of the file being compiled. * analyzeProg semantically analyzes the ast * */ class Semant { public: Semant(Program *ast, const string &filename); void analyzeProg(); ClassTable *getClassTable() { return classSymTab; } private: Program *ast; string fileName; ClassTable *classSymTab; void addBuiltIns(); }; /* * VarInfo class hierarchy * * For the info to associate with a variable name in a symbol table. * VarInfo is an Abstract Base Class, so you can not instantiate it. * * Create the object specific to the kind of variable being declared. * E.g., for a local variable declaration do * * new LocalInfo(type) * * Using these classes for the info about a var, rather than the bare * bones Symbol* or the AST for the declaration, makes it easy to * later write codegen code specific to each kind of variable and * still allows one symbol table for all kinds of variables. * * Hence, using this hierarchy, when we do code generation we won't * need to recreate the symbol tables or modify our code that created * the symbol tables. * * We will be adding member functions and data members to these * classes when we write the code generator. * */ class VarInfo { // ABC public: VarInfo(Symbol *_type) : type(_type) { } Symbol *getType() { return type; } virtual ostream & dump(ostream &o) const = 0; protected: Symbol *type; ostream & defaultDump(ostream &o) const { o << type; return o; } }; class FormalInfo : public VarInfo { public: FormalInfo(Symbol *_type): VarInfo(_type) { } virtual ostream & dump(ostream &o) const; }; class FieldInfo : public VarInfo { public: FieldInfo(Symbol *_type): VarInfo(_type) { } virtual ostream & dump(ostream &o) const; }; class LocalInfo : public VarInfo { public: LocalInfo(Symbol *_type): VarInfo(_type) { } virtual ostream & dump(ostream &o) const; }; // for the special variable "this" class ThisInfo : public VarInfo { public: ThisInfo(Symbol *_type): VarInfo(_type) { } virtual ostream & dump(ostream &o) const; }; // needed to dump out symbol table ostream & operator<<(ostream&o, const VarInfo *varInfoPtr); ostream & operator<<(ostream&o, const VarInfo &varInfo); #endif