scope.h

来自「一个面向对像语言的编译器」· C头文件 代码 · 共 77 行

H
77
字号
/*
 * File: scope.h
 * -------------
 * The Scope class will be used to manage scopes, sort of
 * table used to map identifier names to Declaration objects.
 * The ScopeStack class keeps a stack of Scope objects to handle the
 * nested scopes.
 */

#ifndef _H_scope
#define _H_scope

#include "hashtable.h"
/*#include "declaration.h"
#include "assert.h"
#define Assert assert*/
#define MaxNest 32

class Declaration;
class Type;
class ScopeStack ;



class Scope { 
  public:
   typedef enum {Global, Class, Function, Local} KindOfScope;

  private:
 
    KindOfScope scopeCode;
    Hashtable *table;
	Type *classType;
  public:
	  Declaration * NextDecla();
	  Declaration * FirstDecla();
    Scope(KindOfScope s);       // for local/global
    Scope(Declaration *fn);     // for fn
    Scope(Type *classType);     // for class

    bool IsGlobalScope()         { return scopeCode == Global; }
    bool IsLocalScope()          { return scopeCode == Local; }
    bool IsClassScope()          { return scopeCode == Class; }
    bool IsFunctionScope()       { return scopeCode == Function; }

    Type *GetClassType();        // only for class scopes
    void CopyFieldsFromParent(Scope *parent); // only for class scopes

    Declaration *Lookup(const char *identifier);
    bool Declare(Declaration *decl);

};
 
class ScopeStack {
  private:
    
    Scope *stack[MaxNest];
    int top;

  public:
	  Scope * GetClassScope();

    ScopeStack();

    typedef enum { Shallow, Deep } TypeOfLookup;

    Declaration *Lookup(const char *identifier, TypeOfLookup lk = Deep);
    bool Declare(Declaration *decl); 

    void PushScope(Scope *scope);
    void PopScope();

    Scope *GetCurrentScope();
};

#endif

⌨️ 快捷键说明

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