symtable.pas 89 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl, Pierre Muller
  3. This unit handles the symbol tables
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit symtable;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. { common }
  22. cutils,cclasses,
  23. { global }
  24. cpuinfo,globtype,tokens,
  25. { symtable }
  26. symconst,symbase,symtype,symdef,symsym,
  27. { ppu }
  28. ppu,
  29. { assembler }
  30. aasmtai,aasmdata
  31. ;
  32. {****************************************************************************
  33. Symtable types
  34. ****************************************************************************}
  35. type
  36. tstoredsymtable = class(TSymtable)
  37. private
  38. b_needs_init_final : boolean;
  39. procedure _needs_init_final(sym:TObject;arg:pointer);
  40. procedure check_forward(sym:TObject;arg:pointer);
  41. procedure labeldefined(sym:TObject;arg:pointer);
  42. procedure varsymbolused(sym:TObject;arg:pointer);
  43. procedure TestPrivate(sym:TObject;arg:pointer);
  44. procedure objectprivatesymbolused(sym:TObject;arg:pointer);
  45. procedure loaddefs(ppufile:tcompilerppufile);
  46. procedure loadsyms(ppufile:tcompilerppufile);
  47. procedure writedefs(ppufile:tcompilerppufile);
  48. procedure writesyms(ppufile:tcompilerppufile);
  49. public
  50. procedure insert(sym:TSymEntry;checkdup:boolean=true);override;
  51. procedure delete(sym:TSymEntry);override;
  52. { load/write }
  53. procedure ppuload(ppufile:tcompilerppufile);virtual;
  54. procedure ppuwrite(ppufile:tcompilerppufile);virtual;
  55. procedure buildderef;virtual;
  56. procedure buildderefimpl;virtual;
  57. procedure deref;virtual;
  58. procedure derefimpl;virtual;
  59. function checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;override;
  60. procedure reset_all_defs;virtual;
  61. procedure allsymbolsused;
  62. procedure allprivatesused;
  63. procedure check_forwards;
  64. procedure checklabels;
  65. function needs_init_final : boolean;
  66. procedure testfordefaultproperty(sym:TObject;arg:pointer);
  67. end;
  68. {$ifdef support_llvm}
  69. tllvmshadowsymtableentry = class
  70. constructor create(def: tdef; fieldoffset: aint);
  71. private
  72. ffieldoffset: aint;
  73. fdef: tdef;
  74. public
  75. property fieldoffset: aint read ffieldoffset;
  76. property def: tdef read fdef;
  77. end;
  78. tllvmshadowsymtable = class;
  79. {$endif support_llvm}
  80. tabstractrecordsymtable = class(tstoredsymtable)
  81. public
  82. usefieldalignment, { alignment to use for fields (PACKRECORDS value), C_alignment is C style }
  83. recordalignment, { alignment desired when inserting this record }
  84. fieldalignment, { alignment current alignment used when fields are inserted }
  85. padalignment : shortint; { size to a multiple of which the symtable has to be rounded up }
  86. {$ifdef support_llvm}
  87. llvmst: tllvmshadowsymtable;
  88. {$endif}
  89. constructor create(const n:string;usealign:shortint);
  90. destructor destroy; override;
  91. procedure ppuload(ppufile:tcompilerppufile);override;
  92. procedure ppuwrite(ppufile:tcompilerppufile);override;
  93. procedure alignrecord(fieldoffset:aint;varalign:shortint);
  94. procedure addfield(sym:tfieldvarsym;vis:tvisibility);
  95. procedure addalignmentpadding;
  96. procedure insertdef(def:TDefEntry);override;
  97. function is_packed: boolean;
  98. protected
  99. procedure setdatasize(val: aint);
  100. _datasize : aint;
  101. { size in bits of the data in case of bitpacked record. Only important during construction, }
  102. { no need to save in/restore from ppu file. datasize is always (databitsize+7) div 8. }
  103. databitsize : aint;
  104. { bitpacked? -> all fieldvarsym offsets are in bits instead of bytes }
  105. public
  106. property datasize : aint read _datasize write setdatasize;
  107. end;
  108. trecordsymtable = class(tabstractrecordsymtable)
  109. public
  110. constructor create(usealign:shortint);
  111. procedure insertunionst(unionst : trecordsymtable;offset : longint);
  112. end;
  113. tObjectSymtable = class(tabstractrecordsymtable)
  114. public
  115. constructor create(adefowner:tdef;const n:string;usealign:shortint);
  116. function checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;override;
  117. end;
  118. {$ifdef support_llvm}
  119. { llvm record definitions cannot contain variant/union parts, }
  120. { you have to flatten them first. the tllvmshadowsymtable }
  121. { contains a flattened version of a record/object symtable }
  122. tllvmshadowsymtable = class
  123. private
  124. equivst: tabstractrecordsymtable;
  125. curroffset: aint;
  126. public
  127. symdeflist: TFPObjectList;
  128. constructor create(st: tabstractrecordsymtable);
  129. destructor destroy; override;
  130. private
  131. // generate the table
  132. procedure generate;
  133. // helpers
  134. procedure appenddefoffset(vardef:tdef; fieldoffset: aint; derefclass: boolean);
  135. procedure findvariantstarts(variantstarts: tfplist);
  136. procedure addalignmentpadding(finalsize: aint);
  137. procedure buildmapping(variantstarts: tfplist);
  138. procedure buildtable(variantstarts: tfplist);
  139. end;
  140. {$endif support_llvm}
  141. { tabstractlocalsymtable }
  142. tabstractlocalsymtable = class(tstoredsymtable)
  143. public
  144. procedure ppuwrite(ppufile:tcompilerppufile);override;
  145. function count_locals:longint;
  146. end;
  147. tlocalsymtable = class(tabstractlocalsymtable)
  148. public
  149. constructor create(adefowner:tdef;level:byte);
  150. function checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;override;
  151. end;
  152. tparasymtable = class(tabstractlocalsymtable)
  153. public
  154. constructor create(adefowner:tdef;level:byte);
  155. function checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;override;
  156. end;
  157. tabstractuniTSymtable = class(tstoredsymtable)
  158. public
  159. constructor create(const n : string;id:word);
  160. function iscurrentunit:boolean;override;
  161. end;
  162. tglobalsymtable = class(tabstractuniTSymtable)
  163. public
  164. unittypecount : word;
  165. constructor create(const n : string;id:word);
  166. procedure ppuload(ppufile:tcompilerppufile);override;
  167. procedure ppuwrite(ppufile:tcompilerppufile);override;
  168. function checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;override;
  169. end;
  170. tstaticsymtable = class(tabstractuniTSymtable)
  171. public
  172. constructor create(const n : string;id:word);
  173. procedure ppuload(ppufile:tcompilerppufile);override;
  174. procedure ppuwrite(ppufile:tcompilerppufile);override;
  175. function checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;override;
  176. end;
  177. twithsymtable = class(TSymtable)
  178. withrefnode : tobject; { tnode }
  179. constructor create(aowner:tdef;ASymList:TFPHashObjectList;refnode:tobject{tnode});
  180. destructor destroy;override;
  181. procedure clear;override;
  182. procedure insertdef(def:TDefEntry);override;
  183. end;
  184. tstt_excepTSymtable = class(TSymtable)
  185. public
  186. constructor create;
  187. end;
  188. tmacrosymtable = class(tstoredsymtable)
  189. public
  190. constructor create(exported: boolean);
  191. end;
  192. var
  193. systemunit : tglobalsymtable; { pointer to the system unit }
  194. {****************************************************************************
  195. Functions
  196. ****************************************************************************}
  197. {*** Misc ***}
  198. function FullTypeName(def,otherdef:tdef):string;
  199. procedure incompatibletypes(def1,def2:tdef);
  200. procedure hidesym(sym:TSymEntry);
  201. procedure duplicatesym(var hashedid:THashedIDString;dupsym,origsym:TSymEntry);
  202. {*** Search ***}
  203. procedure addsymref(sym:tsym);
  204. function is_visible_for_object(symst:tsymtable;symvisibility:tvisibility;contextobjdef:tobjectdef):boolean;
  205. function is_visible_for_object(pd:tprocdef;contextobjdef:tobjectdef):boolean;
  206. function is_visible_for_object(sym:tsym;contextobjdef:tobjectdef):boolean;
  207. function searchsym(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  208. function searchsym_type(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  209. function searchsym_in_module(pm:pointer;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  210. function searchsym_in_class(classh,contextclassh:tobjectdef;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  211. function searchsym_in_class_by_msgint(classh:tobjectdef;msgid:longint;out srdef : tdef;out srsym:tsym;out srsymtable:TSymtable):boolean;
  212. function searchsym_in_class_by_msgstr(classh:tobjectdef;const s:string;out srsym:tsym;out srsymtable:TSymtable):boolean;
  213. function search_system_type(const s: TIDString): ttypesym;
  214. function search_class_member(pd : tobjectdef;const s : string):tsym;
  215. function search_assignment_operator(from_def,to_def:Tdef):Tprocdef;
  216. {Looks for macro s (must be given in upper case) in the macrosymbolstack, }
  217. {and returns it if found. Returns nil otherwise.}
  218. function search_macro(const s : string):tsym;
  219. { Additionally to searching for a macro, also checks whether it's still }
  220. { actually defined (could be disable using "undef") }
  221. function defined_macro(const s : string):boolean;
  222. {*** Object Helpers ***}
  223. function search_default_property(pd : tobjectdef) : tpropertysym;
  224. {*** Macro Helpers ***}
  225. {If called initially, the following procedures manipulate macros in }
  226. {initialmacrotable, otherwise they manipulate system macros local to a module.}
  227. {Name can be given in any case (it will be converted to upper case).}
  228. procedure def_system_macro(const name : string);
  229. procedure set_system_macro(const name, value : string);
  230. procedure set_system_compvar(const name, value : string);
  231. procedure undef_system_macro(const name : string);
  232. {*** symtable stack ***}
  233. { $ifdef DEBUG
  234. procedure test_symtablestack;
  235. procedure list_symtablestack;
  236. $endif DEBUG}
  237. {$ifdef UNITALIASES}
  238. type
  239. punit_alias = ^tunit_alias;
  240. tunit_alias = object(TNamedIndexItem)
  241. newname : pshortstring;
  242. constructor init(const n:string);
  243. destructor done;virtual;
  244. end;
  245. var
  246. unitaliases : pdictionary;
  247. procedure addunitalias(const n:string);
  248. function getunitalias(const n:string):string;
  249. {$endif UNITALIASES}
  250. {*** Init / Done ***}
  251. procedure IniTSymtable;
  252. procedure DoneSymtable;
  253. const
  254. overloaded_names : array [NOTOKEN..last_overloaded] of string[16] =
  255. ('error',
  256. 'plus','minus','star','slash','equal',
  257. 'greater','lower','greater_or_equal',
  258. 'lower_or_equal',
  259. 'sym_diff','starstar',
  260. 'as','is','in','or',
  261. 'and','div','mod','not','shl','shr','xor',
  262. 'assign');
  263. implementation
  264. uses
  265. { global }
  266. verbose,globals,
  267. { target }
  268. systems,
  269. { symtable }
  270. symutil,defcmp,defutil,
  271. { module }
  272. fmodule,
  273. { codegen }
  274. procinfo
  275. ;
  276. var
  277. dupnr : longint; { unique number for duplicate symbols }
  278. {*****************************************************************************
  279. TStoredSymtable
  280. *****************************************************************************}
  281. procedure tstoredsymtable.insert(sym:TSymEntry;checkdup:boolean=true);
  282. begin
  283. inherited insert(sym,checkdup);
  284. end;
  285. procedure tstoredsymtable.delete(sym:TSymEntry);
  286. begin
  287. inherited delete(sym);
  288. end;
  289. procedure tstoredsymtable.ppuload(ppufile:tcompilerppufile);
  290. begin
  291. { load definitions }
  292. loaddefs(ppufile);
  293. { load symbols }
  294. loadsyms(ppufile);
  295. end;
  296. procedure tstoredsymtable.ppuwrite(ppufile:tcompilerppufile);
  297. begin
  298. { write definitions }
  299. writedefs(ppufile);
  300. { write symbols }
  301. writesyms(ppufile);
  302. end;
  303. procedure tstoredsymtable.loaddefs(ppufile:tcompilerppufile);
  304. var
  305. def : tdef;
  306. b : byte;
  307. begin
  308. { load start of definition section, which holds the amount of defs }
  309. if ppufile.readentry<>ibstartdefs then
  310. Message(unit_f_ppu_read_error);
  311. { read definitions }
  312. repeat
  313. b:=ppufile.readentry;
  314. case b of
  315. ibpointerdef : def:=tpointerdef.ppuload(ppufile);
  316. ibarraydef : def:=tarraydef.ppuload(ppufile);
  317. iborddef : def:=torddef.ppuload(ppufile);
  318. ibfloatdef : def:=tfloatdef.ppuload(ppufile);
  319. ibprocdef : def:=tprocdef.ppuload(ppufile);
  320. ibshortstringdef : def:=tstringdef.loadshort(ppufile);
  321. iblongstringdef : def:=tstringdef.loadlong(ppufile);
  322. ibansistringdef : def:=tstringdef.loadansi(ppufile);
  323. ibwidestringdef : def:=tstringdef.loadwide(ppufile);
  324. ibunicodestringdef : def:=tstringdef.loadunicode(ppufile);
  325. ibrecorddef : def:=trecorddef.ppuload(ppufile);
  326. ibobjectdef : def:=tobjectdef.ppuload(ppufile);
  327. ibenumdef : def:=tenumdef.ppuload(ppufile);
  328. ibsetdef : def:=tsetdef.ppuload(ppufile);
  329. ibprocvardef : def:=tprocvardef.ppuload(ppufile);
  330. ibfiledef : def:=tfiledef.ppuload(ppufile);
  331. ibclassrefdef : def:=tclassrefdef.ppuload(ppufile);
  332. ibformaldef : def:=tformaldef.ppuload(ppufile);
  333. ibvariantdef : def:=tvariantdef.ppuload(ppufile);
  334. ibundefineddef : def:=tundefineddef.ppuload(ppufile);
  335. ibenddefs : break;
  336. ibend : Message(unit_f_ppu_read_error);
  337. else
  338. Message1(unit_f_ppu_invalid_entry,tostr(b));
  339. end;
  340. InsertDef(def);
  341. until false;
  342. end;
  343. procedure tstoredsymtable.loadsyms(ppufile:tcompilerppufile);
  344. var
  345. b : byte;
  346. sym : tsym;
  347. begin
  348. { load start of definition section, which holds the amount of defs }
  349. if ppufile.readentry<>ibstartsyms then
  350. Message(unit_f_ppu_read_error);
  351. { now read the symbols }
  352. repeat
  353. b:=ppufile.readentry;
  354. case b of
  355. ibtypesym : sym:=ttypesym.ppuload(ppufile);
  356. ibprocsym : sym:=tprocsym.ppuload(ppufile);
  357. ibconstsym : sym:=tconstsym.ppuload(ppufile);
  358. ibstaticvarsym : sym:=tstaticvarsym.ppuload(ppufile);
  359. iblocalvarsym : sym:=tlocalvarsym.ppuload(ppufile);
  360. ibparavarsym : sym:=tparavarsym.ppuload(ppufile);
  361. ibfieldvarsym : sym:=tfieldvarsym.ppuload(ppufile);
  362. ibabsolutevarsym : sym:=tabsolutevarsym.ppuload(ppufile);
  363. ibenumsym : sym:=tenumsym.ppuload(ppufile);
  364. ibpropertysym : sym:=tpropertysym.ppuload(ppufile);
  365. ibunitsym : sym:=tunitsym.ppuload(ppufile);
  366. iblabelsym : sym:=tlabelsym.ppuload(ppufile);
  367. ibsyssym : sym:=tsyssym.ppuload(ppufile);
  368. ibmacrosym : sym:=tmacro.ppuload(ppufile);
  369. ibendsyms : break;
  370. ibend : Message(unit_f_ppu_read_error);
  371. else
  372. Message1(unit_f_ppu_invalid_entry,tostr(b));
  373. end;
  374. Insert(sym,false);
  375. until false;
  376. end;
  377. procedure tstoredsymtable.writedefs(ppufile:tcompilerppufile);
  378. var
  379. i : longint;
  380. def : tstoreddef;
  381. begin
  382. { each definition get a number, write then the amount of defs to the
  383. ibstartdef entry }
  384. ppufile.putlongint(DefList.count);
  385. ppufile.writeentry(ibstartdefs);
  386. { now write the definition }
  387. for i:=0 to DefList.Count-1 do
  388. begin
  389. def:=tstoreddef(DefList[i]);
  390. def.ppuwrite(ppufile);
  391. end;
  392. { write end of definitions }
  393. ppufile.writeentry(ibenddefs);
  394. end;
  395. procedure tstoredsymtable.writesyms(ppufile:tcompilerppufile);
  396. var
  397. i : longint;
  398. sym : Tstoredsym;
  399. begin
  400. { each definition get a number, write then the amount of syms and the
  401. datasize to the ibsymdef entry }
  402. ppufile.putlongint(SymList.count);
  403. ppufile.writeentry(ibstartsyms);
  404. { foreach is used to write all symbols }
  405. for i:=0 to SymList.Count-1 do
  406. begin
  407. sym:=tstoredsym(SymList[i]);
  408. sym.ppuwrite(ppufile);
  409. end;
  410. { end of symbols }
  411. ppufile.writeentry(ibendsyms);
  412. end;
  413. procedure tstoredsymtable.buildderef;
  414. var
  415. i : longint;
  416. def : tstoreddef;
  417. sym : tstoredsym;
  418. begin
  419. { interface definitions }
  420. for i:=0 to DefList.Count-1 do
  421. begin
  422. def:=tstoreddef(DefList[i]);
  423. def.buildderef;
  424. end;
  425. { interface symbols }
  426. for i:=0 to SymList.Count-1 do
  427. begin
  428. sym:=tstoredsym(SymList[i]);
  429. sym.buildderef;
  430. end;
  431. end;
  432. procedure tstoredsymtable.buildderefimpl;
  433. var
  434. i : longint;
  435. def : tstoreddef;
  436. begin
  437. { implementation definitions }
  438. for i:=0 to DefList.Count-1 do
  439. begin
  440. def:=tstoreddef(DefList[i]);
  441. def.buildderefimpl;
  442. end;
  443. end;
  444. procedure tstoredsymtable.deref;
  445. var
  446. i : longint;
  447. def : tstoreddef;
  448. sym : tstoredsym;
  449. begin
  450. { first deref the interface ttype symbols. This is needs
  451. to be done before the interface defs are derefed, because
  452. the interface defs can contain references to the type symbols
  453. which then already need to contain a resolved typedef field (PFV) }
  454. for i:=0 to SymList.Count-1 do
  455. begin
  456. sym:=tstoredsym(SymList[i]);
  457. if sym.typ=typesym then
  458. sym.deref;
  459. end;
  460. { interface definitions }
  461. for i:=0 to DefList.Count-1 do
  462. begin
  463. def:=tstoreddef(DefList[i]);
  464. def.deref;
  465. end;
  466. { interface symbols }
  467. for i:=0 to SymList.Count-1 do
  468. begin
  469. sym:=tstoredsym(SymList[i]);
  470. if sym.typ<>typesym then
  471. sym.deref;
  472. end;
  473. end;
  474. procedure tstoredsymtable.derefimpl;
  475. var
  476. i : longint;
  477. def : tstoreddef;
  478. begin
  479. { implementation definitions }
  480. for i:=0 to DefList.Count-1 do
  481. begin
  482. def:=tstoreddef(DefList[i]);
  483. def.derefimpl;
  484. end;
  485. end;
  486. function tstoredsymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  487. var
  488. hsym : tsym;
  489. begin
  490. hsym:=tsym(FindWithHash(hashedid));
  491. if assigned(hsym) then
  492. DuplicateSym(hashedid,sym,hsym);
  493. result:=assigned(hsym);
  494. end;
  495. {**************************************
  496. Callbacks
  497. **************************************}
  498. procedure TStoredSymtable.check_forward(sym:TObject;arg:pointer);
  499. begin
  500. if tsym(sym).typ=procsym then
  501. tprocsym(sym).check_forward
  502. { check also object method table }
  503. { we needn't to test the def list }
  504. { because each object has to have a type sym,
  505. only test objects declarations, not type renamings }
  506. else
  507. if (tsym(sym).typ=typesym) and
  508. assigned(ttypesym(sym).typedef) and
  509. (ttypesym(sym).typedef.typesym=ttypesym(sym)) and
  510. (ttypesym(sym).typedef.typ=objectdef) then
  511. tobjectdef(ttypesym(sym).typedef).check_forwards;
  512. end;
  513. procedure TStoredSymtable.labeldefined(sym:TObject;arg:pointer);
  514. begin
  515. if (tsym(sym).typ=labelsym) and
  516. not(tlabelsym(sym).defined) then
  517. begin
  518. if tlabelsym(sym).used then
  519. Message1(sym_e_label_used_and_not_defined,tlabelsym(sym).realname)
  520. else
  521. Message1(sym_w_label_not_defined,tlabelsym(sym).realname);
  522. end;
  523. end;
  524. procedure TStoredSymtable.varsymbolused(sym:TObject;arg:pointer);
  525. begin
  526. if (tsym(sym).typ in [staticvarsym,localvarsym,paravarsym,fieldvarsym]) and
  527. ((tsym(sym).owner.symtabletype in
  528. [parasymtable,localsymtable,ObjectSymtable,staticsymtable])) then
  529. begin
  530. { unused symbol should be reported only if no }
  531. { error is reported }
  532. { if the symbol is in a register it is used }
  533. { also don't count the value parameters which have local copies }
  534. { also don't claim for high param of open parameters (PM) }
  535. if (Errorcount<>0) or
  536. ([vo_is_hidden_para,vo_is_funcret] * tabstractvarsym(sym).varoptions = [vo_is_hidden_para]) then
  537. exit;
  538. if (tstoredsym(sym).refs=0) then
  539. begin
  540. if (vo_is_funcret in tabstractvarsym(sym).varoptions) then
  541. begin
  542. { don't warn about the result of constructors }
  543. if ((tsym(sym).owner.symtabletype<>localsymtable) or
  544. (tprocdef(tsym(sym).owner.defowner).proctypeoption<>potype_constructor)) and
  545. not(cs_opt_nodedfa in current_settings.optimizerswitches) then
  546. MessagePos(tsym(sym).fileinfo,sym_w_function_result_not_set)
  547. end
  548. else if (tsym(sym).owner.symtabletype=parasymtable) then
  549. MessagePos1(tsym(sym).fileinfo,sym_h_para_identifier_not_used,tsym(sym).realname)
  550. else if (tsym(sym).owner.symtabletype=ObjectSymtable) then
  551. MessagePos2(tsym(sym).fileinfo,sym_n_private_identifier_not_used,tsym(sym).owner.realname^,tsym(sym).realname)
  552. else
  553. MessagePos1(tsym(sym).fileinfo,sym_n_local_identifier_not_used,tsym(sym).realname);
  554. end
  555. else if tabstractvarsym(sym).varstate in [vs_written,vs_initialised] then
  556. begin
  557. if (tsym(sym).owner.symtabletype=parasymtable) then
  558. begin
  559. if not(tabstractvarsym(sym).varspez in [vs_var,vs_out]) and
  560. not(vo_is_funcret in tabstractvarsym(sym).varoptions) then
  561. MessagePos1(tsym(sym).fileinfo,sym_h_para_identifier_only_set,tsym(sym).realname)
  562. end
  563. else if (tsym(sym).owner.symtabletype=ObjectSymtable) then
  564. MessagePos2(tsym(sym).fileinfo,sym_n_private_identifier_only_set,tsym(sym).owner.realname^,tsym(sym).realname)
  565. else if tabstractvarsym(sym).varoptions*[vo_is_funcret,vo_is_public,vo_is_external]=[] then
  566. MessagePos1(tsym(sym).fileinfo,sym_n_local_identifier_only_set,tsym(sym).realname);
  567. end
  568. else if (tabstractvarsym(sym).varstate = vs_read_not_warned) and
  569. ([vo_is_public,vo_is_external] * tabstractvarsym(sym).varoptions = []) then
  570. MessagePos1(tsym(sym).fileinfo,sym_w_identifier_only_read,tsym(sym).realname)
  571. end
  572. else if ((tsym(sym).owner.symtabletype in
  573. [ObjectSymtable,parasymtable,localsymtable,staticsymtable])) then
  574. begin
  575. if (Errorcount<>0) or
  576. (sp_internal in tsym(sym).symoptions) then
  577. exit;
  578. { do not claim for inherited private fields !! }
  579. if (Tsym(sym).refs=0) and (tsym(sym).owner.symtabletype=ObjectSymtable) then
  580. MessagePos2(tsym(sym).fileinfo,sym_n_private_method_not_used,tsym(sym).owner.realname^,tsym(sym).realname)
  581. { units references are problematic }
  582. else
  583. begin
  584. if (Tsym(sym).refs=0) and
  585. not(tsym(sym).typ in [enumsym,unitsym]) and
  586. not(is_funcret_sym(tsym(sym))) and
  587. (
  588. (tsym(sym).typ<>procsym) or
  589. ((tsym(sym).owner.symtabletype=staticsymtable) and
  590. not current_module.is_unit)
  591. ) then
  592. MessagePos2(tsym(sym).fileinfo,sym_h_local_symbol_not_used,SymTypeName[tsym(sym).typ],tsym(sym).realname);
  593. end;
  594. end;
  595. end;
  596. procedure TStoredSymtable.TestPrivate(sym:TObject;arg:pointer);
  597. begin
  598. if tsym(sym).visibility=vis_private then
  599. varsymbolused(sym,arg);
  600. end;
  601. procedure TStoredSymtable.objectprivatesymbolused(sym:TObject;arg:pointer);
  602. begin
  603. {
  604. Don't test simple object aliases PM
  605. }
  606. if (tsym(sym).typ=typesym) and
  607. (ttypesym(sym).typedef.typ=objectdef) and
  608. (ttypesym(sym).typedef.typesym=tsym(sym)) then
  609. tobjectdef(ttypesym(sym).typedef).symtable.SymList.ForEachCall(@TestPrivate,nil);
  610. end;
  611. procedure tstoredsymtable.testfordefaultproperty(sym:TObject;arg:pointer);
  612. begin
  613. if (tsym(sym).typ=propertysym) and
  614. (ppo_defaultproperty in tpropertysym(sym).propoptions) then
  615. ppointer(arg)^:=sym;
  616. end;
  617. {***********************************************
  618. Process all entries
  619. ***********************************************}
  620. procedure Tstoredsymtable.reset_all_defs;
  621. var
  622. i : longint;
  623. def : tstoreddef;
  624. begin
  625. for i:=0 to DefList.Count-1 do
  626. begin
  627. def:=tstoreddef(DefList[i]);
  628. def.reset;
  629. end;
  630. end;
  631. { checks, if all procsyms and methods are defined }
  632. procedure tstoredsymtable.check_forwards;
  633. begin
  634. SymList.ForEachCall(@check_forward,nil);
  635. end;
  636. procedure tstoredsymtable.checklabels;
  637. begin
  638. SymList.ForEachCall(@labeldefined,nil);
  639. end;
  640. procedure tstoredsymtable.allsymbolsused;
  641. begin
  642. SymList.ForEachCall(@varsymbolused,nil);
  643. end;
  644. procedure tstoredsymtable.allprivatesused;
  645. begin
  646. SymList.ForEachCall(@objectprivatesymbolused,nil);
  647. end;
  648. procedure TStoredSymtable._needs_init_final(sym:TObject;arg:pointer);
  649. begin
  650. if b_needs_init_final then
  651. exit;
  652. case tsym(sym).typ of
  653. fieldvarsym,
  654. staticvarsym,
  655. localvarsym,
  656. paravarsym :
  657. begin
  658. if not(is_class(tabstractvarsym(sym).vardef)) and
  659. tstoreddef(tabstractvarsym(sym).vardef).needs_inittable then
  660. b_needs_init_final:=true;
  661. end;
  662. end;
  663. end;
  664. { returns true, if p contains data which needs init/final code }
  665. function tstoredsymtable.needs_init_final : boolean;
  666. begin
  667. b_needs_init_final:=false;
  668. SymList.ForEachCall(@_needs_init_final,nil);
  669. needs_init_final:=b_needs_init_final;
  670. end;
  671. {****************************************************************************
  672. TAbstractRecordSymtable
  673. ****************************************************************************}
  674. constructor tabstractrecordsymtable.create(const n:string;usealign:shortint);
  675. begin
  676. inherited create(n);
  677. _datasize:=0;
  678. databitsize:=0;
  679. recordalignment:=1;
  680. usefieldalignment:=usealign;
  681. padalignment:=1;
  682. { recordalign C_alignment means C record packing, that starts
  683. with an alignment of 1 }
  684. case usealign of
  685. C_alignment,
  686. bit_alignment:
  687. fieldalignment:=1
  688. else
  689. fieldalignment:=usealign;
  690. end;
  691. end;
  692. destructor tabstractrecordsymtable.destroy;
  693. begin
  694. {$ifdef support_llvm}
  695. llvmst.free;
  696. {$endif suppor_llvm}
  697. inherited destroy;
  698. end;
  699. procedure tabstractrecordsymtable.ppuload(ppufile:tcompilerppufile);
  700. begin
  701. inherited ppuload(ppufile);
  702. end;
  703. procedure tabstractrecordsymtable.ppuwrite(ppufile:tcompilerppufile);
  704. var
  705. oldtyp : byte;
  706. begin
  707. oldtyp:=ppufile.entrytyp;
  708. ppufile.entrytyp:=subentryid;
  709. inherited ppuwrite(ppufile);
  710. ppufile.entrytyp:=oldtyp;
  711. end;
  712. function field2recordalignment(fieldoffs, fieldalign: aint): aint;
  713. begin
  714. { optimal alignment of the record when declaring a variable of this }
  715. { type is independent of the packrecords setting }
  716. if (fieldoffs mod fieldalign) = 0 then
  717. result:=fieldalign
  718. else if (fieldalign >= 16) and
  719. ((fieldoffs mod 16) = 0) and
  720. ((fieldalign mod 16) = 0) then
  721. result:=16
  722. else if (fieldalign >= 8) and
  723. ((fieldoffs mod 8) = 0) and
  724. ((fieldalign mod 8) = 0) then
  725. result:=8
  726. else if (fieldalign >= 4) and
  727. ((fieldoffs mod 4) = 0) and
  728. ((fieldalign mod 4) = 0) then
  729. result:=4
  730. else if (fieldalign >= 2) and
  731. ((fieldoffs mod 2) = 0) and
  732. ((fieldalign mod 2) = 0) then
  733. result:=2
  734. else
  735. result:=1;
  736. end;
  737. procedure tabstractrecordsymtable.alignrecord(fieldoffset:aint;varalign:shortint);
  738. var
  739. varalignrecord: shortint;
  740. begin
  741. if (usefieldalignment=C_alignment) then
  742. varalignrecord:=used_align(varalign,current_settings.alignment.recordalignmin,current_settings.alignment.maxCrecordalign)
  743. else
  744. varalignrecord:=field2recordalignment(fieldoffset,varalign);
  745. recordalignment:=max(recordalignment,varalignrecord);
  746. end;
  747. procedure tabstractrecordsymtable.addfield(sym:tfieldvarsym;vis:tvisibility);
  748. var
  749. l : aint;
  750. varalignfield,
  751. varalign : shortint;
  752. vardef : tdef;
  753. begin
  754. if (sym.owner<>self) then
  755. internalerror(200602031);
  756. if sym.fieldoffset<>-1 then
  757. internalerror(200602032);
  758. { set visibility for the symbol }
  759. sym.visibility:=vis;
  760. { this symbol can't be loaded to a register }
  761. sym.varregable:=vr_none;
  762. { Calculate field offset }
  763. l:=sym.getsize;
  764. vardef:=sym.vardef;
  765. varalign:=vardef.alignment;
  766. if (usefieldalignment=bit_alignment) then
  767. begin
  768. { bitpacking only happens for ordinals, the rest is aligned at }
  769. { 1 byte (compatible with GPC/GCC) }
  770. if is_ordinal(vardef) then
  771. begin
  772. sym.fieldoffset:=databitsize;
  773. l:=sym.getpackedbitsize;
  774. end
  775. else
  776. begin
  777. databitsize:=_datasize*8;
  778. sym.fieldoffset:=databitsize;
  779. if (l>high(aint) div 8) then
  780. Message(sym_e_segment_too_large);
  781. l:=l*8;
  782. end;
  783. if varalign=0 then
  784. varalign:=size_2_align(l);
  785. recordalignment:=max(recordalignment,field2recordalignment(databitsize mod 8,varalign));
  786. { bit packed records are limited to high(aint) bits }
  787. { instead of bytes to avoid double precision }
  788. { arithmetic in offset calculations }
  789. if int64(l)>high(aint)-sym.fieldoffset then
  790. begin
  791. Message(sym_e_segment_too_large);
  792. _datasize:=high(aint);
  793. databitsize:=high(aint);
  794. end
  795. else
  796. begin
  797. databitsize:=sym.fieldoffset+l;
  798. _datasize:=(databitsize+7) div 8;
  799. end;
  800. { rest is not applicable }
  801. exit;
  802. end;
  803. { Calc the alignment size for C style records }
  804. if (usefieldalignment=C_alignment) then
  805. begin
  806. if (varalign>4) and
  807. ((varalign mod 4)<>0) and
  808. (vardef.typ=arraydef) then
  809. Message1(sym_w_wrong_C_pack,vardef.typename);
  810. if varalign=0 then
  811. varalign:=l;
  812. if (fieldalignment<current_settings.alignment.maxCrecordalign) then
  813. begin
  814. if (varalign>16) and (fieldalignment<32) then
  815. fieldalignment:=32
  816. else if (varalign>12) and (fieldalignment<16) then
  817. fieldalignment:=16
  818. { 12 is needed for long double }
  819. else if (varalign>8) and (fieldalignment<12) then
  820. fieldalignment:=12
  821. else if (varalign>4) and (fieldalignment<8) then
  822. fieldalignment:=8
  823. else if (varalign>2) and (fieldalignment<4) then
  824. fieldalignment:=4
  825. else if (varalign>1) and (fieldalignment<2) then
  826. fieldalignment:=2;
  827. end;
  828. fieldalignment:=min(fieldalignment,current_settings.alignment.maxCrecordalign);
  829. end;
  830. if varalign=0 then
  831. varalign:=size_2_align(l);
  832. varalignfield:=used_align(varalign,current_settings.alignment.recordalignmin,fieldalignment);
  833. sym.fieldoffset:=align(_datasize,varalignfield);
  834. if l>high(aint)-sym.fieldoffset then
  835. begin
  836. Message(sym_e_segment_too_large);
  837. _datasize:=high(aint);
  838. end
  839. else
  840. _datasize:=sym.fieldoffset+l;
  841. { Calc alignment needed for this record }
  842. alignrecord(sym.fieldoffset,varalign);
  843. end;
  844. procedure tabstractrecordsymtable.addalignmentpadding;
  845. begin
  846. { make the record size aligned correctly so it can be
  847. used as elements in an array. For C records we
  848. use the fieldalignment, because that is updated with the
  849. used alignment. }
  850. if (padalignment = 1) then
  851. case usefieldalignment of
  852. C_alignment:
  853. padalignment:=fieldalignment;
  854. { bitpacked }
  855. bit_alignment:
  856. padalignment:=1;
  857. { default/no packrecords specified }
  858. 0:
  859. padalignment:=recordalignment
  860. { specific packrecords setting -> use as upper limit }
  861. else
  862. padalignment:=min(recordalignment,usefieldalignment);
  863. end;
  864. _datasize:=align(_datasize,padalignment);
  865. end;
  866. procedure tabstractrecordsymtable.insertdef(def:TDefEntry);
  867. begin
  868. { Enums must also be available outside the record scope,
  869. insert in the owner of this symtable }
  870. if def.typ=enumdef then
  871. defowner.owner.insertdef(def)
  872. else
  873. inherited insertdef(def);
  874. end;
  875. function tabstractrecordsymtable.is_packed: boolean;
  876. begin
  877. result:=usefieldalignment=bit_alignment;
  878. end;
  879. procedure tabstractrecordsymtable.setdatasize(val: aint);
  880. begin
  881. _datasize:=val;
  882. if (usefieldalignment=bit_alignment) then
  883. { can overflow in non bitpacked records }
  884. databitsize:=val*8;
  885. end;
  886. {****************************************************************************
  887. TRecordSymtable
  888. ****************************************************************************}
  889. constructor trecordsymtable.create(usealign:shortint);
  890. begin
  891. inherited create('',usealign);
  892. symtabletype:=recordsymtable;
  893. end;
  894. { this procedure is reserved for inserting case variant into
  895. a record symtable }
  896. { the offset is the location of the start of the variant
  897. and datasize and dataalignment corresponds to
  898. the complete size (see code in pdecl unit) PM }
  899. procedure trecordsymtable.insertunionst(unionst : trecordsymtable;offset : longint);
  900. var
  901. sym : tsym;
  902. def : tdef;
  903. i : integer;
  904. varalignrecord,varalign,
  905. storesize,storealign : aint;
  906. bitsize: aint;
  907. begin
  908. storesize:=_datasize;
  909. storealign:=fieldalignment;
  910. _datasize:=offset;
  911. if (usefieldalignment=bit_alignment) then
  912. databitsize:=offset*8;
  913. { We move the ownership of the defs and symbols to the new recordsymtable.
  914. The old unionsymtable keeps the references, but doesn't own the
  915. objects anymore }
  916. unionst.DefList.OwnsObjects:=false;
  917. unionst.SymList.OwnsObjects:=false;
  918. { copy symbols }
  919. for i:=0 to unionst.SymList.Count-1 do
  920. begin
  921. sym:=TSym(unionst.SymList[i]);
  922. if sym.typ<>fieldvarsym then
  923. internalerror(200601272);
  924. { add to this record symtable }
  925. // unionst.SymList.List.List^[i].Data:=nil;
  926. sym.ChangeOwner(self);
  927. varalign:=tfieldvarsym(sym).vardef.alignment;
  928. if varalign=0 then
  929. varalign:=size_2_align(tfieldvarsym(sym).getsize);
  930. { retrieve size }
  931. if (usefieldalignment=bit_alignment) then
  932. begin
  933. { bit packed records are limited to high(aint) bits }
  934. { instead of bytes to avoid double precision }
  935. { arithmetic in offset calculations }
  936. if is_ordinal(tfieldvarsym(sym).vardef) then
  937. bitsize:=tfieldvarsym(sym).getpackedbitsize
  938. else
  939. begin
  940. bitsize:=tfieldvarsym(sym).getsize;
  941. if (bitsize>high(aint) div 8) then
  942. Message(sym_e_segment_too_large);
  943. bitsize:=bitsize*8;
  944. end;
  945. if bitsize>high(aint)-databitsize then
  946. begin
  947. Message(sym_e_segment_too_large);
  948. _datasize:=high(aint);
  949. databitsize:=high(aint);
  950. end
  951. else
  952. begin
  953. databitsize:=tfieldvarsym(sym).fieldoffset+offset*8;
  954. _datasize:=(databitsize+7) div 8;
  955. end;
  956. tfieldvarsym(sym).fieldoffset:=databitsize;
  957. varalignrecord:=field2recordalignment(tfieldvarsym(sym).fieldoffset div 8,varalign);
  958. end
  959. else
  960. begin
  961. if tfieldvarsym(sym).getsize>high(aint)-_datasize then
  962. begin
  963. Message(sym_e_segment_too_large);
  964. _datasize:=high(aint);
  965. end
  966. else
  967. _datasize:=tfieldvarsym(sym).fieldoffset+offset;
  968. { update address }
  969. tfieldvarsym(sym).fieldoffset:=_datasize;
  970. varalignrecord:=field2recordalignment(tfieldvarsym(sym).fieldoffset,varalign);
  971. end;
  972. { update alignment of this record }
  973. if (usefieldalignment<>C_alignment) then
  974. recordalignment:=max(recordalignment,varalignrecord);
  975. end;
  976. { update alignment for C records }
  977. if (usefieldalignment=C_alignment) then
  978. recordalignment:=max(recordalignment,unionst.recordalignment);
  979. { Register defs in the new record symtable }
  980. for i:=0 to unionst.DefList.Count-1 do
  981. begin
  982. def:=TDef(unionst.DefList[i]);
  983. def.ChangeOwner(self);
  984. end;
  985. _datasize:=storesize;
  986. fieldalignment:=storealign;
  987. end;
  988. {****************************************************************************
  989. TObjectSymtable
  990. ****************************************************************************}
  991. constructor tObjectSymtable.create(adefowner:tdef;const n:string;usealign:shortint);
  992. begin
  993. inherited create(n,usealign);
  994. symtabletype:=ObjectSymtable;
  995. defowner:=adefowner;
  996. end;
  997. function tObjectSymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  998. var
  999. hsym : tsym;
  1000. begin
  1001. result:=false;
  1002. if not assigned(defowner) then
  1003. internalerror(200602061);
  1004. { procsym and propertysym have special code
  1005. to override values in inherited classes. For other
  1006. symbols check for duplicates }
  1007. if not(sym.typ in [procsym,propertysym]) then
  1008. begin
  1009. { but private ids can be reused }
  1010. hsym:=search_class_member(tobjectdef(defowner),hashedid.id);
  1011. if assigned(hsym) and
  1012. (
  1013. (
  1014. not(m_delphi in current_settings.modeswitches) and
  1015. is_visible_for_object(hsym,tobjectdef(defowner))
  1016. ) or
  1017. (
  1018. { In Delphi, you can repeat members of a parent class. You can't }
  1019. { do this for objects however, and you (obviouly) can't }
  1020. { declare two fields with the same name in a single class }
  1021. (m_delphi in current_settings.modeswitches) and
  1022. (
  1023. is_object(tdef(defowner)) or
  1024. (hsym.owner = self)
  1025. )
  1026. )
  1027. ) then
  1028. begin
  1029. DuplicateSym(hashedid,sym,hsym);
  1030. result:=true;
  1031. end;
  1032. end
  1033. else
  1034. begin
  1035. if not(m_duplicate_names in current_settings.modeswitches) then
  1036. result:=inherited checkduplicate(hashedid,sym);
  1037. end;
  1038. end;
  1039. {$ifdef support_llvm}
  1040. {****************************************************************************
  1041. tLlvmShadowSymtableEntry
  1042. ****************************************************************************}
  1043. constructor tllvmshadowsymtableentry.create(def: tdef; fieldoffset: aint);
  1044. begin
  1045. fdef:=def;
  1046. ffieldoffset:=fieldoffset;
  1047. end;
  1048. {****************************************************************************
  1049. TLlvmShadowSymtable
  1050. ****************************************************************************}
  1051. constructor tllvmshadowsymtable.create(st: tabstractrecordsymtable);
  1052. begin
  1053. equivst:=st;
  1054. curroffset:=0;
  1055. symdeflist:=tfpobjectlist.create(true);
  1056. generate;
  1057. end;
  1058. destructor tllvmshadowsymtable.destroy;
  1059. begin
  1060. symdeflist.free;
  1061. end;
  1062. procedure tllvmshadowsymtable.appenddefoffset(vardef:tdef; fieldoffset: aint; derefclass: boolean);
  1063. var
  1064. sizectr,
  1065. tmpsize: aint;
  1066. begin
  1067. case equivst.usefieldalignment of
  1068. C_alignment:
  1069. { default for llvm, don't add explicit padding }
  1070. symdeflist.add(tllvmshadowsymtableentry.create(vardef,fieldoffset));
  1071. bit_alignment:
  1072. begin
  1073. { curoffset: bit address after the previous field. }
  1074. { llvm has no special support for bitfields in records, }
  1075. { so we replace them with plain bytes. }
  1076. { as soon as a single bit of a byte is allocated, we }
  1077. { allocate the byte in the llvm shadow record }
  1078. if (fieldoffset>curroffset) then
  1079. curroffset:=align(curroffset,8);
  1080. { fields in bitpacked records always start either right }
  1081. { after the previous one, or at the next byte boundary. }
  1082. if (curroffset<>fieldoffset) then
  1083. internalerror(2008051002);
  1084. if is_ordinal(vardef) and
  1085. (vardef.packedbitsize mod 8 <> 0) then
  1086. begin
  1087. tmpsize:=vardef.packedbitsize;
  1088. sizectr:=tmpsize+7;
  1089. repeat
  1090. symdeflist.add(tllvmshadowsymtableentry.create(u8inttype,fieldoffset+(tmpsize+7)-sizectr));
  1091. dec(sizectr,8);
  1092. until (sizectr<=0);
  1093. inc(curroffset,tmpsize);
  1094. end
  1095. else
  1096. begin
  1097. symdeflist.add(tllvmshadowsymtableentry.create(vardef,fieldoffset));
  1098. if not(derefclass) then
  1099. inc(curroffset,vardef.size*8)
  1100. else
  1101. inc(curroffset,tobjectsymtable(tobjectdef(vardef).symtable).datasize*8);
  1102. end;
  1103. end
  1104. else
  1105. begin
  1106. { curoffset: address right after the previous field }
  1107. while (fieldoffset>curroffset) do
  1108. begin
  1109. symdeflist.add(tllvmshadowsymtableentry.create(s8inttype,curroffset));
  1110. inc(curroffset);
  1111. end;
  1112. symdeflist.add(tllvmshadowsymtableentry.create(vardef,fieldoffset));
  1113. if not(derefclass) then
  1114. inc(curroffset,vardef.size)
  1115. else
  1116. inc(curroffset,tobjectsymtable(tobjectdef(vardef).symtable).datasize);
  1117. end
  1118. end
  1119. end;
  1120. procedure tllvmshadowsymtable.addalignmentpadding(finalsize: aint);
  1121. begin
  1122. case equivst.usefieldalignment of
  1123. { already correct in this case }
  1124. bit_alignment,
  1125. { handled by llvm }
  1126. C_alignment:
  1127. ;
  1128. else
  1129. begin
  1130. { add padding fields }
  1131. while (finalsize>curroffset) do
  1132. begin
  1133. symdeflist.add(tllvmshadowsymtableentry.create(s8inttype,curroffset));
  1134. inc(curroffset);
  1135. end;
  1136. end;
  1137. end;
  1138. end;
  1139. procedure tllvmshadowsymtable.findvariantstarts(variantstarts: tfplist);
  1140. var
  1141. sym: tfieldvarsym;
  1142. lastoffset: aint;
  1143. newalignment: aint;
  1144. i, j: longint;
  1145. begin
  1146. i:=0;
  1147. while (i<equivst.symlist.count) do
  1148. begin
  1149. if (tsym(equivst.symlist[i]).typ<>fieldvarsym) then
  1150. begin
  1151. inc(i);
  1152. continue;
  1153. end;
  1154. sym:=tfieldvarsym(equivst.symlist[i]);
  1155. { a "better" algorithm might be to use the largest }
  1156. { variant in case of (bit)packing, since then }
  1157. { alignment doesn't matter }
  1158. if (vo_is_first_field in sym.varoptions) then
  1159. begin
  1160. { we assume that all fields are processed in order. }
  1161. if (variantstarts.count<>0) then
  1162. lastoffset:=tfieldvarsym(variantstarts[variantstarts.count-1]).fieldoffset
  1163. else
  1164. lastoffset:=-1;
  1165. { new variant at same level as last one: use if higher alignment }
  1166. if (lastoffset=sym.fieldoffset) then
  1167. begin
  1168. if (equivst.fieldalignment<>bit_alignment) then
  1169. newalignment:=used_align(sym.vardef.alignment,current_settings.alignment.recordalignmin,equivst.fieldalignment)
  1170. else
  1171. newalignment:=1;
  1172. if (newalignment>tfieldvarsym(variantstarts[variantstarts.count-1]).vardef.alignment) then
  1173. variantstarts[variantstarts.count-1]:=sym;
  1174. end
  1175. { variant at deeper level than last one -> add }
  1176. else if (lastoffset<sym.fieldoffset) then
  1177. variantstarts.add(sym)
  1178. else
  1179. begin
  1180. { a variant at a less deep level, so backtrack }
  1181. j:=variantstarts.count-2;
  1182. while (j>=0) do
  1183. begin
  1184. if (tfieldvarsym(variantstarts[j]).fieldoffset=sym.fieldoffset) then
  1185. break;
  1186. dec(j);
  1187. end;
  1188. if (j<0) then
  1189. internalerror(2008051003);
  1190. { new variant has higher alignment? }
  1191. if (equivst.fieldalignment<>bit_alignment) then
  1192. newalignment:=used_align(sym.vardef.alignment,current_settings.alignment.recordalignmin,equivst.fieldalignment)
  1193. else
  1194. newalignment:=1;
  1195. { yes, replace and remove previous nested variants }
  1196. if (newalignment>tfieldvarsym(variantstarts[j]).vardef.alignment) then
  1197. begin
  1198. variantstarts[j]:=sym;
  1199. variantstarts.count:=j+1;
  1200. end
  1201. { no, skip this variant }
  1202. else
  1203. begin
  1204. inc(i);
  1205. while (i<equivst.symlist.count) and
  1206. ((tsym(equivst.symlist[i]).typ<>fieldvarsym) or
  1207. (tfieldvarsym(equivst.symlist[i]).fieldoffset>sym.fieldoffset)) do
  1208. inc(i);
  1209. continue;
  1210. end;
  1211. end;
  1212. end;
  1213. inc(i);
  1214. end;
  1215. end;
  1216. procedure tllvmshadowsymtable.buildtable(variantstarts: tfplist);
  1217. var
  1218. lastvaroffsetprocessed: aint;
  1219. i, equivcount, varcount: longint;
  1220. begin
  1221. { if it's an object/class, the first entry is the parent (if there is one) }
  1222. if (equivst.symtabletype=objectsymtable) and
  1223. assigned(tobjectdef(equivst.defowner).childof) then
  1224. appenddefoffset(tobjectdef(equivst.defowner).childof,0,is_class_or_interface_or_dispinterface(tobjectdef(equivst.defowner).childof));
  1225. equivcount:=equivst.symlist.count;
  1226. varcount:=0;
  1227. i:=0;
  1228. lastvaroffsetprocessed:=-1;
  1229. while (i<equivcount) do
  1230. begin
  1231. if (tsym(equivst.symlist[i]).typ<>fieldvarsym) then
  1232. begin
  1233. inc(i);
  1234. continue;
  1235. end;
  1236. { start of a new variant? }
  1237. if (vo_is_first_field in tfieldvarsym(equivst.symlist[i]).varoptions) then
  1238. begin
  1239. { if we want to process the same variant offset twice, it means that we }
  1240. { got to the end and are trying to process the next variant part -> stop }
  1241. if (tfieldvarsym(equivst.symlist[i]).fieldoffset<=lastvaroffsetprocessed) then
  1242. break;
  1243. if (varcount>=variantstarts.count) then
  1244. internalerror(2008051005);
  1245. { new variant part -> use the one with the biggest alignment }
  1246. i:=equivst.symlist.indexof(tobject(variantstarts[varcount]));
  1247. lastvaroffsetprocessed:=tfieldvarsym(equivst.symlist[i]).fieldoffset;
  1248. inc(varcount);
  1249. if (i<0) then
  1250. internalerror(2008051004);
  1251. end;
  1252. appenddefoffset(tfieldvarsym(equivst.symlist[i]).vardef,tfieldvarsym(equivst.symlist[i]).fieldoffset,false);
  1253. inc(i);
  1254. end;
  1255. addalignmentpadding(equivst.datasize);
  1256. end;
  1257. procedure tllvmshadowsymtable.buildmapping(variantstarts: tfplist);
  1258. var
  1259. i, varcount: longint;
  1260. shadowindex: longint;
  1261. equivcount : longint;
  1262. begin
  1263. varcount:=0;
  1264. shadowindex:=0;
  1265. equivcount:=equivst.symlist.count;
  1266. i:=0;
  1267. while (i < equivcount) do
  1268. begin
  1269. if (tsym(equivst.symlist[i]).typ<>fieldvarsym) then
  1270. begin
  1271. inc(i);
  1272. continue;
  1273. end;
  1274. { start of a new variant? }
  1275. if (vo_is_first_field in tfieldvarsym(equivst.symlist[i]).varoptions) then
  1276. begin
  1277. { back up to a less deeply nested variant level? }
  1278. while (tfieldvarsym(equivst.symlist[i]).fieldoffset<tfieldvarsym(variantstarts[varcount]).fieldoffset) do
  1279. dec(varcount);
  1280. { it's possible that some variants are more deeply nested than the
  1281. one we recorded in the shadowsymtable (since we recorded the one
  1282. with the biggest alignment, not necessarily the biggest one in size
  1283. }
  1284. if (tfieldvarsym(equivst.symlist[i]).fieldoffset>tfieldvarsym(variantstarts[varcount]).fieldoffset) then
  1285. varcount:=variantstarts.count-1
  1286. else if (tfieldvarsym(equivst.symlist[i]).fieldoffset<>tfieldvarsym(variantstarts[varcount]).fieldoffset) then
  1287. internalerror(2008051006);
  1288. { reset the shadowindex to the start of this variant. }
  1289. { in case the llvmfieldnr is not (yet) set for this }
  1290. { field, shadowindex will simply be reset to zero and }
  1291. { we'll start searching from the start of the record }
  1292. shadowindex:=tfieldvarsym(variantstarts[varcount]).llvmfieldnr;
  1293. if (varcount<pred(variantstarts.count)) then
  1294. inc(varcount);
  1295. end;
  1296. { find the last shadowfield whose offset <= the current field's offset }
  1297. while (tllvmshadowsymtableentry(symdeflist[shadowindex]).fieldoffset<tfieldvarsym(equivst.symlist[i]).fieldoffset) and
  1298. (shadowindex<symdeflist.count-1) and
  1299. (tllvmshadowsymtableentry(symdeflist[shadowindex+1]).fieldoffset>=tfieldvarsym(equivst.symlist[i]).fieldoffset) do
  1300. inc(shadowindex);
  1301. { set the field number and potential offset from that field (in case }
  1302. { of overlapping variants) }
  1303. tfieldvarsym(equivst.symlist[i]).llvmfieldnr:=shadowindex;
  1304. tfieldvarsym(equivst.symlist[i]).offsetfromllvmfield:=
  1305. tfieldvarsym(equivst.symlist[i]).fieldoffset-tllvmshadowsymtableentry(symdeflist[shadowindex]).fieldoffset;
  1306. inc(i);
  1307. end;
  1308. end;
  1309. procedure tllvmshadowsymtable.generate;
  1310. var
  1311. variantstarts: tfplist;
  1312. begin
  1313. variantstarts:=tfplist.create;
  1314. { first go through the entire record and }
  1315. { store the fieldvarsyms of the variants }
  1316. { with the highest alignment }
  1317. findvariantstarts(variantstarts);
  1318. { now go through the regular fields and the selected variants, }
  1319. { and add them to the llvm shadow record symtable }
  1320. buildtable(variantstarts);
  1321. { finally map all original fields to the llvm definition }
  1322. buildmapping(variantstarts);
  1323. variantstarts.free;
  1324. end;
  1325. {$endif support_llvm}
  1326. {****************************************************************************
  1327. TAbstractLocalSymtable
  1328. ****************************************************************************}
  1329. procedure tabstractlocalsymtable.ppuwrite(ppufile:tcompilerppufile);
  1330. var
  1331. oldtyp : byte;
  1332. begin
  1333. oldtyp:=ppufile.entrytyp;
  1334. ppufile.entrytyp:=subentryid;
  1335. { write definitions }
  1336. writedefs(ppufile);
  1337. { write symbols }
  1338. writesyms(ppufile);
  1339. ppufile.entrytyp:=oldtyp;
  1340. end;
  1341. function tabstractlocalsymtable.count_locals:longint;
  1342. var
  1343. i : longint;
  1344. sym : tsym;
  1345. begin
  1346. result:=0;
  1347. for i:=0 to SymList.Count-1 do
  1348. begin
  1349. sym:=tsym(SymList[i]);
  1350. { Count only varsyms, but ignore the funcretsym }
  1351. if (tsym(sym).typ in [localvarsym,paravarsym]) and
  1352. (tsym(sym)<>current_procinfo.procdef.funcretsym) and
  1353. (not(vo_is_parentfp in tabstractvarsym(sym).varoptions) or
  1354. (tstoredsym(sym).refs>0)) then
  1355. inc(result);
  1356. end;
  1357. end;
  1358. {****************************************************************************
  1359. TLocalSymtable
  1360. ****************************************************************************}
  1361. constructor tlocalsymtable.create(adefowner:tdef;level:byte);
  1362. begin
  1363. inherited create('');
  1364. defowner:=adefowner;
  1365. symtabletype:=localsymtable;
  1366. symtablelevel:=level;
  1367. end;
  1368. function tlocalsymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  1369. var
  1370. hsym : tsym;
  1371. begin
  1372. if not assigned(defowner) or
  1373. (defowner.typ<>procdef) then
  1374. internalerror(200602042);
  1375. result:=false;
  1376. hsym:=tsym(FindWithHash(hashedid));
  1377. if assigned(hsym) then
  1378. begin
  1379. { a local and the function can have the same
  1380. name in TP and Delphi, but RESULT not }
  1381. if (m_duplicate_names in current_settings.modeswitches) and
  1382. (hsym.typ in [absolutevarsym,localvarsym]) and
  1383. (vo_is_funcret in tabstractvarsym(hsym).varoptions) and
  1384. not((m_result in current_settings.modeswitches) and
  1385. (vo_is_result in tabstractvarsym(hsym).varoptions)) then
  1386. HideSym(hsym)
  1387. else
  1388. DuplicateSym(hashedid,sym,hsym);
  1389. result:=true;
  1390. exit;
  1391. end;
  1392. { check also parasymtable, this needs to be done here becuase
  1393. of the special situation with the funcret sym that needs to be
  1394. hidden for tp and delphi modes }
  1395. hsym:=tsym(tabstractprocdef(defowner).parast.FindWithHash(hashedid));
  1396. if assigned(hsym) then
  1397. begin
  1398. { a local and the function can have the same
  1399. name in TP and Delphi, but RESULT not }
  1400. if (m_duplicate_names in current_settings.modeswitches) and
  1401. (sym.typ in [absolutevarsym,localvarsym]) and
  1402. (vo_is_funcret in tabstractvarsym(sym).varoptions) and
  1403. not((m_result in current_settings.modeswitches) and
  1404. (vo_is_result in tabstractvarsym(sym).varoptions)) then
  1405. Hidesym(sym)
  1406. else
  1407. DuplicateSym(hashedid,sym,hsym);
  1408. result:=true;
  1409. exit;
  1410. end;
  1411. { check ObjectSymtable, skip this for funcret sym because
  1412. that will always be positive because it has the same name
  1413. as the procsym }
  1414. if not is_funcret_sym(sym) and
  1415. (defowner.typ=procdef) and
  1416. assigned(tprocdef(defowner)._class) and
  1417. (tprocdef(defowner).owner.defowner=tprocdef(defowner)._class) and
  1418. (
  1419. not(m_delphi in current_settings.modeswitches) or
  1420. is_object(tprocdef(defowner)._class)
  1421. ) then
  1422. result:=tprocdef(defowner)._class.symtable.checkduplicate(hashedid,sym);
  1423. end;
  1424. {****************************************************************************
  1425. TParaSymtable
  1426. ****************************************************************************}
  1427. constructor tparasymtable.create(adefowner:tdef;level:byte);
  1428. begin
  1429. inherited create('');
  1430. defowner:=adefowner;
  1431. symtabletype:=parasymtable;
  1432. symtablelevel:=level;
  1433. end;
  1434. function tparasymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  1435. begin
  1436. result:=inherited checkduplicate(hashedid,sym);
  1437. if result then
  1438. exit;
  1439. if not(m_duplicate_names in current_settings.modeswitches) and
  1440. (defowner.typ=procdef) and
  1441. assigned(tprocdef(defowner)._class) and
  1442. (tprocdef(defowner).owner.defowner=tprocdef(defowner)._class) and
  1443. (
  1444. not(m_delphi in current_settings.modeswitches) or
  1445. is_object(tprocdef(defowner)._class)
  1446. ) then
  1447. result:=tprocdef(defowner)._class.symtable.checkduplicate(hashedid,sym);
  1448. end;
  1449. {****************************************************************************
  1450. TAbstractUniTSymtable
  1451. ****************************************************************************}
  1452. constructor tabstractuniTSymtable.create(const n : string;id:word);
  1453. begin
  1454. inherited create(n);
  1455. moduleid:=id;
  1456. end;
  1457. function tabstractuniTSymtable.iscurrentunit:boolean;
  1458. begin
  1459. result:=assigned(current_module) and
  1460. (
  1461. (current_module.globalsymtable=self) or
  1462. (current_module.localsymtable=self)
  1463. );
  1464. end;
  1465. {****************************************************************************
  1466. TStaticSymtable
  1467. ****************************************************************************}
  1468. constructor tstaticsymtable.create(const n : string;id:word);
  1469. begin
  1470. inherited create(n,id);
  1471. symtabletype:=staticsymtable;
  1472. symtablelevel:=main_program_level;
  1473. end;
  1474. procedure tstaticsymtable.ppuload(ppufile:tcompilerppufile);
  1475. begin
  1476. inherited ppuload(ppufile);
  1477. { now we can deref the syms and defs }
  1478. deref;
  1479. end;
  1480. procedure tstaticsymtable.ppuwrite(ppufile:tcompilerppufile);
  1481. begin
  1482. inherited ppuwrite(ppufile);
  1483. end;
  1484. function tstaticsymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  1485. var
  1486. hsym : tsym;
  1487. begin
  1488. result:=false;
  1489. hsym:=tsym(FindWithHash(hashedid));
  1490. if assigned(hsym) then
  1491. begin
  1492. { Delphi (contrary to TP) you can have a symbol with the same name as the
  1493. unit, the unit can then not be accessed anymore using
  1494. <unit>.<id>, so we can hide the symbol }
  1495. if (m_delphi in current_settings.modeswitches) and
  1496. (hsym.typ=symconst.unitsym) then
  1497. HideSym(hsym)
  1498. else
  1499. DuplicateSym(hashedid,sym,hsym);
  1500. result:=true;
  1501. exit;
  1502. end;
  1503. if (current_module.localsymtable=self) and
  1504. assigned(current_module.globalsymtable) then
  1505. result:=tglobalsymtable(current_module.globalsymtable).checkduplicate(hashedid,sym);
  1506. end;
  1507. {****************************************************************************
  1508. TGlobalSymtable
  1509. ****************************************************************************}
  1510. constructor tglobalsymtable.create(const n : string;id:word);
  1511. begin
  1512. inherited create(n,id);
  1513. symtabletype:=globalsymtable;
  1514. symtablelevel:=main_program_level;
  1515. end;
  1516. procedure tglobalsymtable.ppuload(ppufile:tcompilerppufile);
  1517. begin
  1518. inherited ppuload(ppufile);
  1519. { now we can deref the syms and defs }
  1520. deref;
  1521. end;
  1522. procedure tglobalsymtable.ppuwrite(ppufile:tcompilerppufile);
  1523. begin
  1524. { write the symtable entries }
  1525. inherited ppuwrite(ppufile);
  1526. end;
  1527. function tglobalsymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  1528. var
  1529. hsym : tsym;
  1530. begin
  1531. result:=false;
  1532. hsym:=tsym(FindWithHash(hashedid));
  1533. if assigned(hsym) then
  1534. begin
  1535. { Delphi (contrary to TP) you can have a symbol with the same name as the
  1536. unit, the unit can then not be accessed anymore using
  1537. <unit>.<id>, so we can hide the symbol }
  1538. if (m_delphi in current_settings.modeswitches) and
  1539. (hsym.typ=symconst.unitsym) then
  1540. HideSym(hsym)
  1541. else
  1542. DuplicateSym(hashedid,sym,hsym);
  1543. result:=true;
  1544. exit;
  1545. end;
  1546. end;
  1547. {****************************************************************************
  1548. TWITHSYMTABLE
  1549. ****************************************************************************}
  1550. constructor twithsymtable.create(aowner:tdef;ASymList:TFPHashObjectList;refnode:tobject{tnode});
  1551. begin
  1552. inherited create('');
  1553. symtabletype:=withsymtable;
  1554. withrefnode:=refnode;
  1555. { Replace SymList with the passed symlist }
  1556. SymList.free;
  1557. SymList:=ASymList;
  1558. defowner:=aowner;
  1559. end;
  1560. destructor twithsymtable.destroy;
  1561. begin
  1562. withrefnode.free;
  1563. { Disable SymList because we don't Own it }
  1564. SymList:=nil;
  1565. inherited destroy;
  1566. end;
  1567. procedure twithsymtable.clear;
  1568. begin
  1569. { remove no entry from a withsymtable as it is only a pointer to the
  1570. recorddef or objectdef symtable }
  1571. end;
  1572. procedure twithsymtable.insertdef(def:TDefEntry);
  1573. begin
  1574. { Definitions can't be registered in the withsymtable
  1575. because the withsymtable is removed after the with block.
  1576. We can't easily solve it here because the next symtable in the
  1577. stack is not known. }
  1578. internalerror(200602046);
  1579. end;
  1580. {****************************************************************************
  1581. TSTT_ExceptionSymtable
  1582. ****************************************************************************}
  1583. constructor tstt_excepTSymtable.create;
  1584. begin
  1585. inherited create('');
  1586. symtabletype:=stt_excepTSymtable;
  1587. end;
  1588. {****************************************************************************
  1589. TMacroSymtable
  1590. ****************************************************************************}
  1591. constructor tmacrosymtable.create(exported: boolean);
  1592. begin
  1593. inherited create('');
  1594. if exported then
  1595. symtabletype:=exportedmacrosymtable
  1596. else
  1597. symtabletype:=localmacrosymtable;
  1598. symtablelevel:=main_program_level;
  1599. end;
  1600. {*****************************************************************************
  1601. Helper Routines
  1602. *****************************************************************************}
  1603. function FullTypeName(def,otherdef:tdef):string;
  1604. var
  1605. s1,s2 : string;
  1606. begin
  1607. s1:=def.typename;
  1608. { When the names are the same try to include the unit name }
  1609. if assigned(otherdef) and
  1610. (def.owner.symtabletype in [globalsymtable,staticsymtable]) then
  1611. begin
  1612. s2:=otherdef.typename;
  1613. if upper(s1)=upper(s2) then
  1614. s1:=def.owner.realname^+'.'+s1;
  1615. end;
  1616. FullTypeName:=s1;
  1617. end;
  1618. procedure incompatibletypes(def1,def2:tdef);
  1619. begin
  1620. { When there is an errordef there is already an error message show }
  1621. if (def2.typ=errordef) or
  1622. (def1.typ=errordef) then
  1623. exit;
  1624. CGMessage2(type_e_incompatible_types,FullTypeName(def1,def2),FullTypeName(def2,def1));
  1625. end;
  1626. procedure hidesym(sym:TSymEntry);
  1627. begin
  1628. sym.realname:='$hidden'+sym.realname;
  1629. tsym(sym).visibility:=vis_hidden;
  1630. end;
  1631. procedure duplicatesym(var hashedid:THashedIDString;dupsym,origsym:TSymEntry);
  1632. var
  1633. st : TSymtable;
  1634. begin
  1635. Message1(sym_e_duplicate_id,tsym(origsym).realname);
  1636. { Write hint where the original symbol was found }
  1637. st:=finduniTSymtable(origsym.owner);
  1638. with tsym(origsym).fileinfo do
  1639. begin
  1640. if assigned(st) and
  1641. (st.symtabletype=globalsymtable) and
  1642. st.iscurrentunit then
  1643. Message2(sym_h_duplicate_id_where,current_module.sourcefiles.get_file_name(fileindex),tostr(line))
  1644. else if assigned(st.name) then
  1645. Message2(sym_h_duplicate_id_where,'unit '+st.name^,tostr(line));
  1646. end;
  1647. { Rename duplicate sym to an unreachable name, but it can be
  1648. inserted in the symtable without errors }
  1649. inc(dupnr);
  1650. hashedid.id:='dup'+tostr(dupnr)+hashedid.id;
  1651. if assigned(dupsym) then
  1652. include(tsym(dupsym).symoptions,sp_implicitrename);
  1653. end;
  1654. {*****************************************************************************
  1655. Search
  1656. *****************************************************************************}
  1657. procedure addsymref(sym:tsym);
  1658. begin
  1659. { symbol uses count }
  1660. sym.IncRefCount;
  1661. { unit uses count }
  1662. if assigned(current_module) and
  1663. (sym.owner.symtabletype=globalsymtable) then
  1664. begin
  1665. if tglobalsymtable(sym.owner).moduleid>=current_module.unitmapsize then
  1666. internalerror(200501152);
  1667. inc(current_module.unitmap[tglobalsymtable(sym.owner).moduleid].refs);
  1668. end;
  1669. end;
  1670. function is_visible_for_object(symst:tsymtable;symvisibility:tvisibility;contextobjdef:tobjectdef):boolean;
  1671. var
  1672. symownerdef : tobjectdef;
  1673. begin
  1674. result:=false;
  1675. { Get objdectdef owner of the symtable for the is_related checks }
  1676. if not assigned(symst) or
  1677. (symst.symtabletype<>objectsymtable) then
  1678. internalerror(200810285);
  1679. symownerdef:=tobjectdef(symst.defowner);
  1680. case symvisibility of
  1681. vis_private :
  1682. begin
  1683. { private symbols are allowed when we are in the same
  1684. module as they are defined }
  1685. result:=(symownerdef.owner.symtabletype in [globalsymtable,staticsymtable]) and
  1686. (symownerdef.owner.iscurrentunit);
  1687. end;
  1688. vis_strictprivate :
  1689. begin
  1690. result:=assigned(current_objectdef) and
  1691. (current_objectdef=symownerdef);
  1692. end;
  1693. vis_strictprotected :
  1694. begin
  1695. result:=assigned(current_objectdef) and
  1696. current_objectdef.is_related(symownerdef);
  1697. end;
  1698. vis_protected :
  1699. begin
  1700. { protected symbols are visible in the module that defines them and
  1701. also visible to related objects. The related object must be defined
  1702. in the current module }
  1703. result:=(
  1704. (
  1705. (symownerdef.owner.symtabletype in [globalsymtable,staticsymtable]) and
  1706. (symownerdef.owner.iscurrentunit)
  1707. ) or
  1708. (
  1709. assigned(contextobjdef) and
  1710. (contextobjdef.owner.symtabletype in [globalsymtable,staticsymtable]) and
  1711. (contextobjdef.owner.iscurrentunit) and
  1712. contextobjdef.is_related(symownerdef)
  1713. )
  1714. );
  1715. end;
  1716. vis_public,
  1717. vis_published :
  1718. result:=true;
  1719. end;
  1720. end;
  1721. function is_visible_for_object(pd:tprocdef;contextobjdef:tobjectdef):boolean;
  1722. begin
  1723. result:=is_visible_for_object(pd.owner,pd.visibility,contextobjdef);
  1724. end;
  1725. function is_visible_for_object(sym:tsym;contextobjdef:tobjectdef):boolean;
  1726. var
  1727. i : longint;
  1728. pd : tprocdef;
  1729. begin
  1730. if sym.typ=procsym then
  1731. begin
  1732. { A procsym is visible, when there is at least one of the procdefs visible }
  1733. result:=false;
  1734. for i:=0 to tprocsym(sym).ProcdefList.Count-1 do
  1735. begin
  1736. pd:=tprocdef(tprocsym(sym).ProcdefList[i]);
  1737. if (pd.owner=sym.owner) and
  1738. is_visible_for_object(pd,contextobjdef) then
  1739. begin
  1740. result:=true;
  1741. exit;
  1742. end;
  1743. end;
  1744. end
  1745. else
  1746. result:=is_visible_for_object(sym.owner,sym.visibility,contextobjdef);
  1747. end;
  1748. function searchsym(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  1749. var
  1750. hashedid : THashedIDString;
  1751. contextobjdef : tobjectdef;
  1752. stackitem : psymtablestackitem;
  1753. begin
  1754. result:=false;
  1755. hashedid.id:=s;
  1756. stackitem:=symtablestack.stack;
  1757. while assigned(stackitem) do
  1758. begin
  1759. srsymtable:=stackitem^.symtable;
  1760. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  1761. if assigned(srsym) then
  1762. begin
  1763. { use the class from withsymtable only when it is
  1764. defined in this unit }
  1765. if (srsymtable.symtabletype=withsymtable) and
  1766. assigned(srsymtable.defowner) and
  1767. (srsymtable.defowner.typ=objectdef) and
  1768. (srsymtable.defowner.owner.symtabletype in [globalsymtable,staticsymtable]) and
  1769. (srsymtable.defowner.owner.iscurrentunit) then
  1770. contextobjdef:=tobjectdef(srsymtable.defowner)
  1771. else
  1772. contextobjdef:=current_objectdef;
  1773. if (srsym.owner.symtabletype<>objectsymtable) or
  1774. is_visible_for_object(srsym,contextobjdef) then
  1775. begin
  1776. { we need to know if a procedure references symbols
  1777. in the static symtable, because then it can't be
  1778. inlined from outside this unit }
  1779. if assigned(current_procinfo) and
  1780. (srsym.owner.symtabletype=staticsymtable) then
  1781. include(current_procinfo.flags,pi_uses_static_symtable);
  1782. addsymref(srsym);
  1783. result:=true;
  1784. exit;
  1785. end;
  1786. end;
  1787. stackitem:=stackitem^.next;
  1788. end;
  1789. srsym:=nil;
  1790. srsymtable:=nil;
  1791. end;
  1792. function searchsym_type(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  1793. var
  1794. hashedid : THashedIDString;
  1795. stackitem : psymtablestackitem;
  1796. begin
  1797. result:=false;
  1798. hashedid.id:=s;
  1799. stackitem:=symtablestack.stack;
  1800. while assigned(stackitem) do
  1801. begin
  1802. {
  1803. It is not possible to have type symbols in:
  1804. records
  1805. objects
  1806. parameters
  1807. Exception are generic definitions and specializations
  1808. that have the parameterized types inserted in the symtable.
  1809. }
  1810. srsymtable:=stackitem^.symtable;
  1811. if not(srsymtable.symtabletype in [recordsymtable,ObjectSymtable,parasymtable]) or
  1812. (assigned(srsymtable.defowner) and
  1813. (
  1814. (df_generic in tdef(srsymtable.defowner).defoptions) or
  1815. (df_specialization in tdef(srsymtable.defowner).defoptions))
  1816. ) then
  1817. begin
  1818. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  1819. if assigned(srsym) and
  1820. not(srsym.typ in [fieldvarsym,paravarsym]) and
  1821. (
  1822. (srsym.owner.symtabletype<>objectsymtable) or
  1823. is_visible_for_object(srsym,current_objectdef)
  1824. ) then
  1825. begin
  1826. { we need to know if a procedure references symbols
  1827. in the static symtable, because then it can't be
  1828. inlined from outside this unit }
  1829. if assigned(current_procinfo) and
  1830. (srsym.owner.symtabletype=staticsymtable) then
  1831. include(current_procinfo.flags,pi_uses_static_symtable);
  1832. addsymref(srsym);
  1833. result:=true;
  1834. exit;
  1835. end;
  1836. end;
  1837. stackitem:=stackitem^.next;
  1838. end;
  1839. result:=false;
  1840. srsym:=nil;
  1841. srsymtable:=nil;
  1842. end;
  1843. function searchsym_in_module(pm:pointer;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  1844. var
  1845. pmod : tmodule;
  1846. begin
  1847. pmod:=tmodule(pm);
  1848. result:=false;
  1849. if assigned(pmod.globalsymtable) then
  1850. begin
  1851. srsym:=tsym(pmod.globalsymtable.Find(s));
  1852. if assigned(srsym) then
  1853. begin
  1854. srsymtable:=pmod.globalsymtable;
  1855. addsymref(srsym);
  1856. result:=true;
  1857. exit;
  1858. end;
  1859. end;
  1860. { If the module is the current unit we also need
  1861. to search the local symtable }
  1862. if (pmod=current_module) and
  1863. assigned(pmod.localsymtable) then
  1864. begin
  1865. srsym:=tsym(pmod.localsymtable.Find(s));
  1866. if assigned(srsym) then
  1867. begin
  1868. srsymtable:=pmod.localsymtable;
  1869. addsymref(srsym);
  1870. result:=true;
  1871. exit;
  1872. end;
  1873. end;
  1874. srsym:=nil;
  1875. srsymtable:=nil;
  1876. end;
  1877. function searchsym_in_class(classh,contextclassh:tobjectdef;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  1878. var
  1879. hashedid : THashedIDString;
  1880. begin
  1881. { The contextclassh is used for visibility. The classh must be equal to
  1882. or be a parent of contextclassh. E.g. for inherited searches the classh is the
  1883. parent. }
  1884. if assigned(classh) and
  1885. not contextclassh.is_related(classh) then
  1886. internalerror(200811161);
  1887. result:=false;
  1888. hashedid.id:=s;
  1889. while assigned(classh) do
  1890. begin
  1891. srsymtable:=classh.symtable;
  1892. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  1893. if assigned(srsym) and
  1894. is_visible_for_object(srsym,contextclassh) then
  1895. begin
  1896. addsymref(srsym);
  1897. result:=true;
  1898. exit;
  1899. end;
  1900. classh:=classh.childof;
  1901. end;
  1902. srsym:=nil;
  1903. srsymtable:=nil;
  1904. end;
  1905. function searchsym_in_class_by_msgint(classh:tobjectdef;msgid:longint;out srdef : tdef;out srsym:tsym;out srsymtable:TSymtable):boolean;
  1906. var
  1907. def : tdef;
  1908. i : longint;
  1909. begin
  1910. result:=false;
  1911. def:=nil;
  1912. while assigned(classh) do
  1913. begin
  1914. for i:=0 to classh.symtable.DefList.Count-1 do
  1915. begin
  1916. def:=tstoreddef(classh.symtable.DefList[i]);
  1917. { Find also all hidden private methods to
  1918. be compatible with delphi, see tw6203 (PFV) }
  1919. if (def.typ=procdef) and
  1920. (po_msgint in tprocdef(def).procoptions) and
  1921. (tprocdef(def).messageinf.i=msgid) then
  1922. begin
  1923. srdef:=def;
  1924. srsym:=tprocdef(def).procsym;
  1925. srsymtable:=classh.symtable;
  1926. addsymref(srsym);
  1927. result:=true;
  1928. exit;
  1929. end;
  1930. end;
  1931. classh:=classh.childof;
  1932. end;
  1933. srdef:=nil;
  1934. srsym:=nil;
  1935. srsymtable:=nil;
  1936. end;
  1937. function searchsym_in_class_by_msgstr(classh:tobjectdef;const s:string;out srsym:tsym;out srsymtable:TSymtable):boolean;
  1938. var
  1939. def : tdef;
  1940. i : longint;
  1941. begin
  1942. result:=false;
  1943. def:=nil;
  1944. while assigned(classh) do
  1945. begin
  1946. for i:=0 to classh.symtable.DefList.Count-1 do
  1947. begin
  1948. def:=tstoreddef(classh.symtable.DefList[i]);
  1949. { Find also all hidden private methods to
  1950. be compatible with delphi, see tw6203 (PFV) }
  1951. if (def.typ=procdef) and
  1952. (po_msgstr in tprocdef(def).procoptions) and
  1953. (tprocdef(def).messageinf.str^=s) then
  1954. begin
  1955. srsym:=tprocdef(def).procsym;
  1956. srsymtable:=classh.symtable;
  1957. addsymref(srsym);
  1958. result:=true;
  1959. exit;
  1960. end;
  1961. end;
  1962. classh:=classh.childof;
  1963. end;
  1964. srsym:=nil;
  1965. srsymtable:=nil;
  1966. end;
  1967. function search_assignment_operator(from_def,to_def:Tdef):Tprocdef;
  1968. var
  1969. sym : Tprocsym;
  1970. hashedid : THashedIDString;
  1971. curreq,
  1972. besteq : tequaltype;
  1973. currpd,
  1974. bestpd : tprocdef;
  1975. stackitem : psymtablestackitem;
  1976. begin
  1977. hashedid.id:='assign';
  1978. besteq:=te_incompatible;
  1979. bestpd:=nil;
  1980. stackitem:=symtablestack.stack;
  1981. while assigned(stackitem) do
  1982. begin
  1983. sym:=Tprocsym(stackitem^.symtable.FindWithHash(hashedid));
  1984. if sym<>nil then
  1985. begin
  1986. if sym.typ<>procsym then
  1987. internalerror(200402031);
  1988. { if the source type is an alias then this is only the second choice,
  1989. if you mess with this code, check tw4093 }
  1990. currpd:=sym.find_procdef_assignment_operator(from_def,to_def,curreq);
  1991. if curreq>besteq then
  1992. begin
  1993. besteq:=curreq;
  1994. bestpd:=currpd;
  1995. if (besteq=te_exact) then
  1996. break;
  1997. end;
  1998. end;
  1999. stackitem:=stackitem^.next;
  2000. end;
  2001. result:=bestpd;
  2002. end;
  2003. function search_system_type(const s: TIDString): ttypesym;
  2004. var
  2005. sym : tsym;
  2006. begin
  2007. sym:=tsym(systemunit.Find(s));
  2008. if not assigned(sym) or
  2009. (sym.typ<>typesym) then
  2010. cgmessage1(cg_f_unknown_system_type,s);
  2011. result:=ttypesym(sym);
  2012. end;
  2013. function search_class_member(pd : tobjectdef;const s : string):tsym;
  2014. { searches n in symtable of pd and all anchestors }
  2015. var
  2016. hashedid : THashedIDString;
  2017. srsym : tsym;
  2018. begin
  2019. hashedid.id:=s;
  2020. while assigned(pd) do
  2021. begin
  2022. srsym:=tsym(pd.symtable.FindWithHash(hashedid));
  2023. if assigned(srsym) then
  2024. begin
  2025. search_class_member:=srsym;
  2026. exit;
  2027. end;
  2028. pd:=pd.childof;
  2029. end;
  2030. search_class_member:=nil;
  2031. end;
  2032. function search_macro(const s : string):tsym;
  2033. var
  2034. stackitem : psymtablestackitem;
  2035. hashedid : THashedIDString;
  2036. srsym : tsym;
  2037. begin
  2038. hashedid.id:=s;
  2039. { First search the localmacrosymtable before searching the
  2040. global macrosymtables from the units }
  2041. if assigned(current_module) then
  2042. begin
  2043. srsym:=tsym(current_module.localmacrosymtable.FindWithHash(hashedid));
  2044. if assigned(srsym) then
  2045. begin
  2046. result:= srsym;
  2047. exit;
  2048. end;
  2049. end;
  2050. stackitem:=macrosymtablestack.stack;
  2051. while assigned(stackitem) do
  2052. begin
  2053. srsym:=tsym(stackitem^.symtable.FindWithHash(hashedid));
  2054. if assigned(srsym) then
  2055. begin
  2056. result:= srsym;
  2057. exit;
  2058. end;
  2059. stackitem:=stackitem^.next;
  2060. end;
  2061. result:= nil;
  2062. end;
  2063. function defined_macro(const s : string):boolean;
  2064. var
  2065. mac: tmacro;
  2066. begin
  2067. mac:=tmacro(search_macro(s));
  2068. if assigned(mac) then
  2069. begin
  2070. mac.is_used:=true;
  2071. defined_macro:=mac.defined;
  2072. end
  2073. else
  2074. defined_macro:=false;
  2075. end;
  2076. {****************************************************************************
  2077. Object Helpers
  2078. ****************************************************************************}
  2079. function search_default_property(pd : tobjectdef) : tpropertysym;
  2080. { returns the default property of a class, searches also anchestors }
  2081. var
  2082. _defaultprop : tpropertysym;
  2083. begin
  2084. _defaultprop:=nil;
  2085. while assigned(pd) do
  2086. begin
  2087. pd.symtable.SymList.ForEachCall(@tstoredsymtable(pd.symtable).testfordefaultproperty,@_defaultprop);
  2088. if assigned(_defaultprop) then
  2089. break;
  2090. pd:=pd.childof;
  2091. end;
  2092. search_default_property:=_defaultprop;
  2093. end;
  2094. {****************************************************************************
  2095. Macro Helpers
  2096. ****************************************************************************}
  2097. procedure def_system_macro(const name : string);
  2098. var
  2099. mac : tmacro;
  2100. s: string;
  2101. begin
  2102. if name = '' then
  2103. internalerror(2004121202);
  2104. s:= upper(name);
  2105. mac:=tmacro(search_macro(s));
  2106. if not assigned(mac) then
  2107. begin
  2108. mac:=tmacro.create(s);
  2109. if assigned(current_module) then
  2110. current_module.localmacrosymtable.insert(mac)
  2111. else
  2112. initialmacrosymtable.insert(mac);
  2113. end;
  2114. if not mac.defined then
  2115. Message1(parser_c_macro_defined,mac.name);
  2116. mac.defined:=true;
  2117. end;
  2118. procedure set_system_macro(const name, value : string);
  2119. var
  2120. mac : tmacro;
  2121. s: string;
  2122. begin
  2123. if name = '' then
  2124. internalerror(2004121203);
  2125. s:= upper(name);
  2126. mac:=tmacro(search_macro(s));
  2127. if not assigned(mac) then
  2128. begin
  2129. mac:=tmacro.create(s);
  2130. if assigned(current_module) then
  2131. current_module.localmacrosymtable.insert(mac)
  2132. else
  2133. initialmacrosymtable.insert(mac);
  2134. end
  2135. else
  2136. begin
  2137. mac.is_compiler_var:=false;
  2138. if assigned(mac.buftext) then
  2139. freemem(mac.buftext,mac.buflen);
  2140. end;
  2141. Message2(parser_c_macro_set_to,mac.name,value);
  2142. mac.buflen:=length(value);
  2143. getmem(mac.buftext,mac.buflen);
  2144. move(value[1],mac.buftext^,mac.buflen);
  2145. mac.defined:=true;
  2146. end;
  2147. procedure set_system_compvar(const name, value : string);
  2148. var
  2149. mac : tmacro;
  2150. s: string;
  2151. begin
  2152. if name = '' then
  2153. internalerror(2004121204);
  2154. s:= upper(name);
  2155. mac:=tmacro(search_macro(s));
  2156. if not assigned(mac) then
  2157. begin
  2158. mac:=tmacro.create(s);
  2159. mac.is_compiler_var:=true;
  2160. if assigned(current_module) then
  2161. current_module.localmacrosymtable.insert(mac)
  2162. else
  2163. initialmacrosymtable.insert(mac);
  2164. end
  2165. else
  2166. begin
  2167. mac.is_compiler_var:=true;
  2168. if assigned(mac.buftext) then
  2169. freemem(mac.buftext,mac.buflen);
  2170. end;
  2171. Message2(parser_c_macro_set_to,mac.name,value);
  2172. mac.buflen:=length(value);
  2173. getmem(mac.buftext,mac.buflen);
  2174. move(value[1],mac.buftext^,mac.buflen);
  2175. mac.defined:=true;
  2176. end;
  2177. procedure undef_system_macro(const name : string);
  2178. var
  2179. mac : tmacro;
  2180. s: string;
  2181. begin
  2182. if name = '' then
  2183. internalerror(2004121205);
  2184. s:= upper(name);
  2185. mac:=tmacro(search_macro(s));
  2186. if not assigned(mac) then
  2187. {If not found, then it's already undefined.}
  2188. else
  2189. begin
  2190. if mac.defined then
  2191. Message1(parser_c_macro_undefined,mac.name);
  2192. mac.defined:=false;
  2193. mac.is_compiler_var:=false;
  2194. { delete old definition }
  2195. if assigned(mac.buftext) then
  2196. begin
  2197. freemem(mac.buftext,mac.buflen);
  2198. mac.buftext:=nil;
  2199. end;
  2200. end;
  2201. end;
  2202. {$ifdef UNITALIASES}
  2203. {****************************************************************************
  2204. TUNIT_ALIAS
  2205. ****************************************************************************}
  2206. constructor tunit_alias.create(const n:string);
  2207. var
  2208. i : longint;
  2209. begin
  2210. i:=pos('=',n);
  2211. if i=0 then
  2212. fail;
  2213. inherited createname(Copy(n,1,i-1));
  2214. newname:=stringdup(Copy(n,i+1,255));
  2215. end;
  2216. destructor tunit_alias.destroy;
  2217. begin
  2218. stringdispose(newname);
  2219. inherited destroy;
  2220. end;
  2221. procedure addunitalias(const n:string);
  2222. begin
  2223. unitaliases^.insert(tunit_alias,init(Upper(n))));
  2224. end;
  2225. function getunitalias(const n:string):string;
  2226. var
  2227. p : punit_alias;
  2228. begin
  2229. p:=punit_alias(unitaliases^.Find(Upper(n)));
  2230. if assigned(p) then
  2231. getunitalias:=punit_alias(p).newname^
  2232. else
  2233. getunitalias:=n;
  2234. end;
  2235. {$endif UNITALIASES}
  2236. {****************************************************************************
  2237. Init/Done Symtable
  2238. ****************************************************************************}
  2239. procedure InitSymtable;
  2240. begin
  2241. { Reset symbolstack }
  2242. symtablestack:=nil;
  2243. systemunit:=nil;
  2244. { create error syms and def }
  2245. generrorsym:=terrorsym.create;
  2246. generrordef:=terrordef.create;
  2247. { macros }
  2248. initialmacrosymtable:=tmacrosymtable.create(false);
  2249. macrosymtablestack:=TSymtablestack.create;
  2250. macrosymtablestack.push(initialmacrosymtable);
  2251. {$ifdef UNITALIASES}
  2252. { unit aliases }
  2253. unitaliases:=TFPHashObjectList.create;
  2254. {$endif}
  2255. { set some global vars to nil, might be important for the ide }
  2256. class_tobject:=nil;
  2257. interface_iunknown:=nil;
  2258. rec_tguid:=nil;
  2259. dupnr:=0;
  2260. end;
  2261. procedure DoneSymtable;
  2262. begin
  2263. generrorsym.owner:=nil;
  2264. generrorsym.free;
  2265. generrordef.owner:=nil;
  2266. generrordef.free;
  2267. initialmacrosymtable.free;
  2268. macrosymtablestack.free;
  2269. {$ifdef UNITALIASES}
  2270. unitaliases.free;
  2271. {$endif}
  2272. end;
  2273. end.