⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 declaration.h

📁 一个面向对像语言的编译器
💻 H
字号:
/*
 * File: declaration.h
 * -------------------
 * The Declaration class defines a simple object that is used to
 * track usage of identifiers (such as variables, function names,
 * etc.) within a program.
 *
 * Although technically a "declaration" refers to a statement that
 * establishes the identity of a name whereas "definition" means the full
 * description (i.e. the difference between just a function prototype and
 * a version with the actual body), we use the Declaration class for both.
 */

#ifndef _H_declaration
#define _H_declaration


class Type;
class Scope;
class Tac; 

  // This constant is set as the offset for a new Declaration.
  // If never changed, the offset will be this distinctive unaligned number.
  // Keep in mind when debugging if one of your variables ends up at
  // offset -555, it is probably due to forgetting to assign the offset
const int Unassigned = -555;

class Declaration {

  public:
         // These are the three kinds of Declarations we deal with
    typedef enum {Variable, Function, Class} KindOfDecl;
  private:
    KindOfDecl declCode;
    char *name;
    Type *type;
    int lineFound;

    Scope *scope;
    int offset, stackFrameSize;
	
	//declaration是否是地址
	bool bIsAddr;
	//函数是否定义了,true—函数已定义,false—函数还没有定义
	bool bFunIsDefined;

  public:
	  bool FunIsDefined();
	  void MakeFunDefined();
	  void ValidateAddr();

	  bool IsAddr();
        // public constant for shared declaration used in error situations
    static Declaration *errorDecl;
	
        // This constructor creates a new Declaration of the specified kind,
        // pairing name with type
    Declaration(KindOfDecl kind, const char *name, Type *t, int lineNum = -1);

        // Some trivial accessors
    const char *GetName()              { return name; }
    Type *GetType()                    { return type; }
    int GetLineFound()                 { return lineFound; }

    bool IsVarDecl()                   { return declCode == Variable; }
    bool IsClassDecl()                 { return declCode == Class; }
    bool IsFunctionDecl()              { return declCode == Function; }

    void SetScope(Scope *s)            { scope = s; }
    Scope *GetScope()                  { return scope; }
    bool IsGlobal() ;                  // true if scope is global
    bool IsClassField();               // true if scope is class


         // The offset field is used to track location of variables/methods
         // For a global variable, offset is from global pointer
         // For a local variable or parameter, offset is from frame pointer
         // For an instance variable, offset is from base of object
         // For a method, offset is position in class vTable
         // For global functions and class definitions, offset is not used
         // The Unassigned constant is used when the offset is as yet unset
    void SetOffset(int off)            { offset = off; }
    int GetOffset();                  
    
         // The function label applies only to function declarations
         // It is synthesized from the function name prefixed with
         // underscore (will include class name if method, too)
    const char *GetFunctionLabel();
    const char *Getx86FunctionLabel();
    
         // The stack frame size applies only to function declarations.
         // It is the total number of bytes used for local and temp vars
         // in the body of the function. (not including parameters, this is
         // just includes the part the callee is responsible for).  Our
         // Mips translator needs to know this size so it can correctly
         // adjust the stack pointer upon entry and exit of the function.
         // When generating TAC, you will need to count the local and
         // temporary variables and record the total size so you can
         // report the correct amount to adjust the stack pointer
    void SetStackFrameSize(int sz);
    int GetStackFrameSize();
    
};

 
#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -