符号表定义

  { types of the symtables } 符号表类型
  TSymtabletype = (
    abstractsymtable,      { not a real symtable             }抽象符号表
    globalsymtable,        { unit interface symtable         }单元接口符号表
    staticsymtable,        { unit implementation symtable    }单元实现符号表
    ObjectSymtable,        { object symtable                 }对象符号表
    recordsymtable,        { record symtable                 }记录符号表
    localsymtable,         { subroutine symtable             }子例程符号表
    parasymtable,          { arguments symtable              }参数符号表
    withsymtable,          { with operator symtable          }WITH符号表
    stt_excepTSymtable,    { try/except symtable             }try/except符号表
    exportedmacrosymtable, { }                    可导出宏符号表
    localmacrosymtable,    { }                    本地符号表
    enumsymtable,          { symtable for enum members       }枚举符号表
    arraysymtable          { used to store parameterised type 参数类型数组符号表
                             in array                        }
  );


符号表类
       TSymtable = class
       public
          name      : pshortstring;    符号表名
          realname  : pshortstring;    真实表名
          DefList   : TFPObjectList;    定义列表
          SymList   : TFPHashObjectList;    符号列表
          defowner  : TDefEntry; { for records and objects } 拥有者定义
          moduleid  : longint;        模块ID
          refcount  : smallint;        引用数
          currentvisibility : tvisibility;    当前可见性
          currentlyoptional : boolean;        符号表定义是可参数化
          tableoptions : tsymtableoptions;    符号表参数
          { level of symtable, used for nested procedures }
          symtablelevel : byte;        符号表级别,主要是针对嵌套过程
          symtabletype  : TSymtabletype;    符号表类型
          constructor Create(const s:string);    创建符号表
          destructor  destroy;override;        释放符号表
          procedure freeinstance;override;    释放符号表内存
          function  getcopy:TSymtable;        获取符号表拷贝
          procedure clear;virtual;        清空符号表
          function  checkduplicate(var s:THashedIDString;sym:TSymEntry):boolean;virtual;    检查是否有相同符号
          procedure insert(sym:TSymEntry;checkdup:boolean=true);virtual;    插入符号
          procedure Delete(sym:TSymEntry);virtual;    删除符号
          function  Find(const s:TIDString) : TSymEntry;    查找符号
          function  FindWithHash(const s:THashedIDString) : TSymEntry;virtual;    查找符号通过HASH值
          procedure insertdef(def:TDefEntry);virtual;    插入定义
          procedure deletedef(def:TDefEntry);    删除定义
          function  iscurrentunit:boolean;virtual;    是否在当前单元
          { includes the flag in this symtable and all parent symtables; if
            it's already set the flag is not set again }
          procedure includeoption(option:tsymtableoption); 包括符号参数,父符号对象同样处理
       end;


符号表堆栈
       TSymtablestack = class
         stack : psymtablestackitem;    符号表项目,单链表
         constructor create;    创建
         destructor destroy;override;    释放
         procedure clear;    清空
         procedure push(st:TSymtable); virtual;    压栈
         procedure pop(st:TSymtable); virtual;    出栈
         function  top:TSymtable;    栈顶
       end;



定义项
{************************************************
                 TDefEntry
************************************************}

      TDefEntry = class
         typ   : tdeftyp;    定义类型
         defid : longint;    定义ID
         owner : TSymtable;    拥有者符号表
      end;


符号项
{************************************************
                   TSymEntry
************************************************}

      { this object is the base for all symbol objects }
      TSymEntry = class(TFPHashObject)
      private
         FRealName : pshortstring;        名字
         function  GetRealname:shortstring;    
         procedure SetRealname(const ANewName:shortstring);
      public
         typ   : tsymtyp;    类型
         SymId : longint;    ID
         Owner : TSymtable;    拥有者符号表
         destructor destroy;override;    释放
         property RealName:shortstring read GetRealName write SetRealName;
      end;  






全局变量

       initialmacrosymtable: TSymtable;   { macros initially defined by the compiler or      
                                            given on the command line. Is common
                                            for all files compiled and do not change. }
    初始宏符号表,初始宏来自于FPC各个选项,如WIN32,CPU,FPU,FEATURE等等。在开始加载OPTION时初始化。由set_system_macro和def_system_macro两函数初始。

       macrosymtablestack,                宏符号表栈
       symtablestack        : TSymtablestack;        符号表栈
                                                
    宏符号表栈,每次loaddefaultunits都清空栈,然后macrosymtablestack.push(initialmacrosymtable);在loadunits里,将每个ppu模块的macrosymtablestack.push(pu.u.globalmacrosymtable);当然叠加编译时也会重置.
    符号表栈非常有用,在过程解析,单元解析,对象解析起到保证符号表一致作用。
    

查找宏
    function search_macro(const s : string):tsym;
      var
        stackitem  : psymtablestackitem;
        hashedid   : THashedIDString;
        srsym      : tsym;
      begin
        hashedid.id:=s;

        { First search the localmacrosymtable before searching the
          global macrosymtables from the units }
        if assigned(current_module) then
          begin
            srsym:=tsym(current_module.localmacrosymtable.FindWithHash(hashedid));    先到模块本地宏符号表里找
            if assigned(srsym) then
              begin
                result:= srsym;
                exit;
              end;
          end;

        stackitem:=macrosymtablestack.stack;            
        while assigned(stackitem) do
          begin
            srsym:=tsym(stackitem^.symtable.FindWithHash(hashedid));    模块本地宏符号表找不到,再到全局宏符号栈里查找
            if assigned(srsym) then
              begin
                result:= srsym;
                exit;
              end;
            stackitem:=stackitem^.next;
          end;
        result:= nil;
      end;   
    

    var
       systemunit     : tglobalsymtable; { pointer to the system unit }   
全局符号表,所有单元不管有没有引用system.pas都会有一份全局符号表。

以下是传说中的编译器内部函数:
    procedure create_intern_symbols;
      {
        all intern procedures for the system unit
      }
      begin
        systemunit.insert(tsyssym.create('Concat',in_concat_x));        连接
        systemunit.insert(tsyssym.create('Write',in_write_x));            写
        systemunit.insert(tsyssym.create('WriteLn',in_writeln_x));        写换行
        systemunit.insert(tsyssym.create('WriteStr',in_writestr_x));    写字符串
        systemunit.insert(tsyssym.create('Assigned',in_assigned_x));    是否存在
        systemunit.insert(tsyssym.create('Read',in_read_x));        读
        systemunit.insert(tsyssym.create('ReadLn',in_readln_x));    读换行
        systemunit.insert(tsyssym.create('ReadStr',in_readstr_x));    读字符串
        systemunit.insert(tsyssym.create('Ofs',in_ofs_x));        偏移
        systemunit.insert(tsyssym.create('SizeOf',in_sizeof_x));    大小
        systemunit.insert(tsyssym.create('BitSizeOf',in_bitsizeof_x));    位大小
        systemunit.insert(tsyssym.create('TypeOf',in_typeof_x));    是否某类型
        systemunit.insert(tsyssym.create('Low',in_low_x));        最小大小
        systemunit.insert(tsyssym.create('High',in_high_x));        最大大小
        systemunit.insert(tsyssym.create('Slice',in_slice_x));        指定数组一部分
        systemunit.insert(tsyssym.create('Seg',in_seg_x));        段
        systemunit.insert(tsyssym.create('Ord',in_ord_x));        序号
        systemunit.insert(tsyssym.create('Pred',in_pred_x));        前趋
        systemunit.insert(tsyssym.create('Succ',in_succ_x));        后继
        systemunit.insert(tsyssym.create('Exclude',in_exclude_x_y));    排除
        systemunit.insert(tsyssym.create('Include',in_include_x_y));    加入
        systemunit.insert(tsyssym.create('Pack',in_pack_x_y_z));    打包
        systemunit.insert(tsyssym.create('Unpack',in_unpack_x_y_z));    解包
        systemunit.insert(tsyssym.create('Break',in_break));        终止
        systemunit.insert(tsyssym.create('Exit',in_exit));        退出
        systemunit.insert(tsyssym.create('Continue',in_continue));    继续
        systemunit.insert(tsyssym.create('Leave',in_leave)); {macpas only}    离开
        systemunit.insert(tsyssym.create('Cycle',in_cycle)); {macpas only}    园
        systemunit.insert(tsyssym.create('Dec',in_dec_x));    减
        systemunit.insert(tsyssym.create('Inc',in_inc_x));    加
        systemunit.insert(tsyssym.create('Str',in_str_x_string));    字符串
        systemunit.insert(tsyssym.create('Assert',in_assert_x_y));    断言
        systemunit.insert(tsyssym.create('Val',in_val_x));    值
        systemunit.insert(tsyssym.create('Addr',in_addr_x));    地址
        systemunit.insert(tsyssym.create('TypeInfo',in_typeinfo_x));    类型信息
        systemunit.insert(tsyssym.create('SetLength',in_setlength_x));    设置长度
        systemunit.insert(tsyssym.create('Copy',in_copy_x));        拷贝
        systemunit.insert(tsyssym.create('Initialize',in_initialize_x));    初始化
        systemunit.insert(tsyssym.create('Finalize',in_finalize_x));    结束
        systemunit.insert(tsyssym.create('Length',in_length_x));    长度
        systemunit.insert(tsyssym.create('New',in_new_x));    分配内存
        systemunit.insert(tsyssym.create('Dispose',in_dispose_x));    释放内存
{$if defined(x86) or defined(arm)}
        systemunit.insert(tsyssym.create('Get_Frame',in_get_frame));    获取帧
{$endif defined(x86) or defined(arm)}
      end;



另外全局类型:

{$ifdef x86}
        if target_info.system<>system_x86_64_win64 then
          addtype('Comp',tfloatdef.create(s64comp));
{$endif x86}
        addtype('Currency',s64currencytype);
        addtype('Pointer',voidpointertype);
{$ifdef x86}
        addtype('FarPointer',voidfarpointertype);
{$endif x86}
        addtype('ShortString',cshortstringtype);
{$ifdef support_longstring}
        addtype('LongString',clongstringtype);
{$endif support_longstring}
        addtype('AnsiString',cansistringtype);
        addtype('WideString',cwidestringtype);
        addtype('UnicodeString',cunicodestringtype);

        addtype('OpenString',openshortstringtype);
        addtype('Boolean',pasbool8type);
        addtype('Boolean16',pasbool16type);
        addtype('Boolean32',pasbool32type);
        addtype('Boolean64',pasbool64type);
        addtype('ByteBool',bool8type);
        addtype('WordBool',bool16type);
        addtype('LongBool',bool32type);
        addtype('QWordBool',bool64type);
        addtype('Byte',u8inttype);
        addtype('ShortInt',s8inttype);
        addtype('Word',u16inttype);
        addtype('SmallInt',s16inttype);
        addtype('LongWord',u32inttype);
        addtype('LongInt',s32inttype);
        addtype('QWord',u64inttype);
        addtype('Int64',s64inttype);
        addtype('Char',cchartype);
        addtype('WideChar',cwidechartype);
        addtype('Text',tfiledef.createtext);
        addtype('TypedFile',tfiledef.createtyped(voidtype));
        addtype('Variant',cvarianttype);
        addtype('OleVariant',colevarianttype);
        { Internal types }
        addtype('$undefined',cundefinedtype);
        addtype('$formal',cformaltype);
        addtype('$typedformal',ctypedformaltype);
        addtype('$void',voidtype);
        addtype('$byte',u8inttype);
        addtype('$shortint',s8inttype);
        addtype('$word',u16inttype);
        addtype('$smallint',s16inttype);
        addtype('$ulong',u32inttype);
        addtype('$longint',s32inttype);
        addtype('$qword',u64inttype);
        addtype('$int64',s64inttype);
        addtype('$char',cchartype);
        addtype('$widechar',cwidechartype);
        addtype('$shortstring',cshortstringtype);
        addtype('$longstring',clongstringtype);
        addtype('$ansistring',cansistringtype);
        addtype('$widestring',cwidestringtype);
        addtype('$unicodestring',cunicodestringtype);
        addtype('$openshortstring',openshortstringtype);
        addtype('$boolean',pasbool8type);
        addtype('$boolean16',pasbool16type);
        addtype('$boolean32',pasbool32type);
        addtype('$boolean64',pasbool64type);
        addtype('$bytebool',bool8type);
        addtype('$wordbool',bool16type);
        addtype('$longbool',bool32type);
        addtype('$qwordbool',bool64type);
        addtype('$void_pointer',voidpointertype);
        addtype('$char_pointer',charpointertype);
        addtype('$widechar_pointer',widecharpointertype);
        addtype('$void_farpointer',voidfarpointertype);
        addtype('$openchararray',openchararraytype);
        addtype('$file',cfiletype);
        addtype('$variant',cvarianttype);
        addtype('$olevariant',cvarianttype);
        if init_settings.fputype<>fpu_none then    如果浮点处理器不为空则加入以下类型
          begin
            addtype('$s32real',s32floattype);
            addtype('$s64real',s64floattype);
            addtype('$s80real',s80floattype);
            addtype('$sc80real',sc80floattype);
          end;
        addtype('$s64currency',s64currencytype);

    systemunit.insert(ttypesym.create('$pvmt',pvmttype));
    addtype('$__vtbl_ptr_type',vmttype);
    addtype('$vtblarray',vmtarraytype);
    addtype('$methodpointer',methodpointertype);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值