symtable.pas 90 KB

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