123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346 |
- {
- Copyright (c) 1998-2002 by Florian Klaempfl, Pierre Muller
- This unit handles the symbol tables
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 2 of the License, or
- (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- ****************************************************************************
- }
- unit symbase;
- {$i fpcdefs.inc}
- interface
- uses
- { common }
- cutils,cclasses,
- { global }
- globtype,globals,
- { symtable }
- symconst
- ;
- {************************************************
- Some internal constants
- ************************************************}
- const
- hasharraysize = 256;
- indexgrowsize = 64;
- {************************************************
- Needed forward pointers
- ************************************************}
- type
- tsymtable = class;
- {************************************************
- TSymtableEntry
- ************************************************}
- tsymtableentry = class(TNamedIndexItem)
- owner : tsymtable;
- end;
- {************************************************
- TDefEntry
- ************************************************}
- tdefentry = class(tsymtableentry)
- deftype : tdeftype;
- end;
- {************************************************
- TSymEntry
- ************************************************}
- { this object is the base for all symbol objects }
- tsymentry = class(tsymtableentry)
- typ : tsymtyp;
- end;
- {************************************************
- TSymtable
- ************************************************}
- tsearchhasharray = array[0..hasharraysize-1] of tsymentry;
- psearchhasharray = ^tsearchhasharray;
- tsymtable = class
- private
- clearing : boolean;
- {$ifdef EXTDEBUG}
- procedure dumpsym(p : TNamedIndexItem;arg:pointer);
- {$endif EXTDEBUG}
- public
- name : pstring;
- realname : pstring;
- symindex,
- defindex : TIndexArray;
- symsearch : Tdictionary;
- defowner : tdefentry; { for records and objects }
- symtabletype : tsymtabletype;
- { level of symtable, used for nested procedures }
- symtablelevel : byte;
- moduleid : longint;
- refcount : integer;
- constructor Create(const s:string);
- destructor destroy;override;
- procedure freeinstance;override;
- function getcopy:tsymtable;
- procedure clear;virtual;
- function rename(const olds,news : stringid):tsymentry;
- procedure foreach(proc2call : tnamedindexcallback;arg:pointer);
- procedure foreach_static(proc2call : tnamedindexstaticcallback;arg:pointer);
- function checkduplicate(sym : tsymentry):boolean;virtual;
- procedure insert(sym : tsymentry);virtual;
- procedure delete(sym:tsymentry);
- procedure replace(oldsym,newsym:tsymentry);
- function search(const s : stringid) : tsymentry;
- function speedsearch(const s : stringid;speedvalue : cardinal) : tsymentry;virtual;
- procedure insertdef(def:tdefentry);virtual;
- procedure deletedef(def:tdefentry);virtual;
- function iscurrentunit:boolean;virtual;
- {$ifdef EXTDEBUG}
- procedure dump;
- {$endif EXTDEBUG}
- function getdefnr(l : longint) : tdefentry;
- function getsymnr(l : longint) : tsymentry;
- end;
- var
- aktrecordsymtable : tsymtable; { current record symtable }
- aktparasymtable : tsymtable; { current proc para symtable }
- aktlocalsymtable : tsymtable; { current proc local symtable }
- initialmacrosymtable: tsymtable; { macros initially defined by the compiler or
- given on the command line. Is common
- for all files compiled and do not change. }
- implementation
- uses
- verbose;
- {****************************************************************************
- TSYMTABLE
- ****************************************************************************}
- constructor tsymtable.Create(const s:string);
- begin
- if s<>'' then
- begin
- name:=stringdup(upper(s));
- realname:=stringdup(s);
- end
- else
- begin
- name:=nil;
- realname:=nil;
- end;
- symtabletype:=abstractsymtable;
- symtablelevel:=0;
- defowner:=nil;
- symindex:=tindexarray.create(indexgrowsize);
- defindex:=TIndexArray.create(indexgrowsize);
- symsearch:=tdictionary.create;
- symsearch.noclear:=true;
- refcount:=1;
- end;
- destructor tsymtable.destroy;
- begin
- { freeinstance decreases refcount }
- if refcount>1 then
- exit;
- { symsearch can already be disposed or set to nil for withsymtable }
- if assigned(symsearch) then
- begin
- symsearch.destroy;
- symsearch:=nil;
- end;
- clear;
- stringdispose(name);
- stringdispose(realname);
- end;
- procedure tsymtable.freeinstance;
- begin
- dec(refcount);
- if refcount=0 then
- inherited freeinstance;
- end;
- function tsymtable.getcopy:tsymtable;
- begin
- inc(refcount);
- result:=self;
- end;
- {$ifdef EXTDEBUG}
- procedure tsymtable.dumpsym(p : TNamedIndexItem;arg:pointer);
- begin
- writeln(p.name);
- end;
- procedure tsymtable.dump;
- begin
- if assigned(name) then
- writeln('Symtable ',name^)
- else
- writeln('Symtable <not named>');
- symsearch.foreach(@dumpsym,nil);
- end;
- {$endif EXTDEBUG}
- function tsymtable.iscurrentunit:boolean;
- begin
- result:=false;
- end;
- procedure tsymtable.foreach(proc2call : tnamedindexcallback;arg:pointer);
- begin
- symindex.foreach(proc2call,arg);
- end;
- procedure tsymtable.foreach_static(proc2call : tnamedindexstaticcallback;arg:pointer);
- begin
- symindex.foreach_static(proc2call,arg);
- end;
- {***********************************************
- Table Access
- ***********************************************}
- procedure tsymtable.clear;
- begin
- clearing:=true;
- symindex.clear;
- defindex.clear;
- clearing:=false;
- end;
- function tsymtable.checkduplicate(sym : tsymentry):boolean;
- begin
- result:=(speedsearch(sym.name,sym.speedvalue)<>nil);
- end;
- procedure tsymtable.insert(sym:tsymentry);
- begin
- checkduplicate(sym);
- sym.owner:=self;
- { insert in index and search hash }
- symindex.insert(sym);
- symsearch.insert(sym);
- end;
- procedure tsymtable.insertdef(def:tdefentry);
- begin
- def.owner:=self;
- defindex.insert(def);
- end;
- procedure tsymtable.deletedef(def:tdefentry);
- begin
- { if we are already clearing everything it will already
- be deleted }
- if clearing then
- exit;
- defindex.deleteindex(def);
- def.owner:=nil;
- end;
- procedure tsymtable.delete(sym:tsymentry);
- begin
- sym.owner:=nil;
- { remove from index and search hash }
- symsearch.delete(sym.name);
- symindex.delete(sym);
- end;
- procedure tsymtable.replace(oldsym,newsym:tsymentry);
- begin
- { Replace the entry in the dictionary, this checks
- the name }
- if not symsearch.replace(oldsym,newsym) then
- internalerror(200209061);
- { replace in index }
- symindex.replace(oldsym,newsym);
- { set owner of new symb }
- newsym.owner:=self;
- end;
- function tsymtable.search(const s : stringid) : tsymentry;
- begin
- search:=speedsearch(s,getspeedvalue(s));
- end;
- function tsymtable.speedsearch(const s : stringid;speedvalue : cardinal) : tsymentry;
- begin
- speedsearch:=tsymentry(symsearch.speedsearch(s,speedvalue));
- end;
- function tsymtable.rename(const olds,news : stringid):tsymentry;
- begin
- rename:=tsymentry(symsearch.rename(olds,news));
- end;
- function tsymtable.getsymnr(l : longint) : tsymentry;
- var
- hp : tsymentry;
- begin
- hp:=tsymentry(symindex.search(l));
- if hp=nil then
- internalerror(10999);
- getsymnr:=hp;
- end;
- function tsymtable.getdefnr(l : longint) : tdefentry;
- var
- hp : tdefentry;
- begin
- hp:=tdefentry(defindex.search(l));
- if hp=nil then
- internalerror(10998);
- getdefnr:=hp;
- end;
- end.
|