symtable.pas 90 KB

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