symtable.pas 87 KB

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