symtable.pas 87 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508
  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. (
  1397. assigned(current_objectdef) and
  1398. (
  1399. (current_objectdef=symownerdef) or
  1400. (current_objectdef.owner.moduleid=symownerdef.owner.moduleid)
  1401. )
  1402. ) or
  1403. (
  1404. not assigned(current_objectdef) and
  1405. (symownerdef.owner.moduleid=current_module.moduleid)
  1406. )
  1407. );
  1408. end;
  1409. vis_strictprivate :
  1410. begin
  1411. result:=assigned(current_objectdef) and
  1412. (current_objectdef=symownerdef);
  1413. end;
  1414. vis_strictprotected :
  1415. begin
  1416. result:=assigned(current_objectdef) and
  1417. current_objectdef.is_related(symownerdef);
  1418. end;
  1419. vis_protected :
  1420. begin
  1421. { protected symbols are visible in the module that defines them and
  1422. also visible to related objects. The related object must be defined
  1423. in the current module }
  1424. result:=(
  1425. (
  1426. (symownerdef.owner.symtabletype in [globalsymtable,staticsymtable]) and
  1427. (symownerdef.owner.iscurrentunit)
  1428. ) or
  1429. (
  1430. assigned(contextobjdef) and
  1431. (contextobjdef.owner.symtabletype in [globalsymtable,staticsymtable]) and
  1432. (contextobjdef.owner.iscurrentunit) and
  1433. contextobjdef.is_related(symownerdef)
  1434. ) or
  1435. ( // the case of specialize inside the generic declaration
  1436. (symownerdef.owner.symtabletype = objectsymtable) and
  1437. (
  1438. assigned(current_objectdef) and
  1439. (
  1440. (current_objectdef=symownerdef) or
  1441. (current_objectdef.owner.moduleid=symownerdef.owner.moduleid)
  1442. )
  1443. ) or
  1444. (
  1445. not assigned(current_objectdef) and
  1446. (symownerdef.owner.moduleid=current_module.moduleid)
  1447. )
  1448. )
  1449. );
  1450. end;
  1451. vis_public,
  1452. vis_published :
  1453. result:=true;
  1454. end;
  1455. end;
  1456. function is_visible_for_object(pd:tprocdef;contextobjdef:tobjectdef):boolean;
  1457. begin
  1458. result:=is_visible_for_object(pd.owner,pd.visibility,contextobjdef);
  1459. end;
  1460. function is_visible_for_object(sym:tsym;contextobjdef:tobjectdef):boolean;
  1461. var
  1462. i : longint;
  1463. pd : tprocdef;
  1464. begin
  1465. if sym.typ=procsym then
  1466. begin
  1467. { A procsym is visible, when there is at least one of the procdefs visible }
  1468. result:=false;
  1469. for i:=0 to tprocsym(sym).ProcdefList.Count-1 do
  1470. begin
  1471. pd:=tprocdef(tprocsym(sym).ProcdefList[i]);
  1472. if (pd.owner=sym.owner) and
  1473. is_visible_for_object(pd,contextobjdef) then
  1474. begin
  1475. result:=true;
  1476. exit;
  1477. end;
  1478. end;
  1479. end
  1480. else
  1481. result:=is_visible_for_object(sym.owner,sym.visibility,contextobjdef);
  1482. end;
  1483. function searchsym(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  1484. var
  1485. hashedid : THashedIDString;
  1486. contextobjdef : tobjectdef;
  1487. stackitem : psymtablestackitem;
  1488. begin
  1489. result:=false;
  1490. hashedid.id:=s;
  1491. stackitem:=symtablestack.stack;
  1492. while assigned(stackitem) do
  1493. begin
  1494. srsymtable:=stackitem^.symtable;
  1495. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  1496. if assigned(srsym) then
  1497. begin
  1498. { use the class from withsymtable only when it is
  1499. defined in this unit }
  1500. if (srsymtable.symtabletype=withsymtable) and
  1501. assigned(srsymtable.defowner) and
  1502. (srsymtable.defowner.typ=objectdef) and
  1503. (srsymtable.defowner.owner.symtabletype in [globalsymtable,staticsymtable]) and
  1504. (srsymtable.defowner.owner.iscurrentunit) then
  1505. contextobjdef:=tobjectdef(srsymtable.defowner)
  1506. else
  1507. contextobjdef:=current_objectdef;
  1508. if (srsym.owner.symtabletype<>objectsymtable) or
  1509. is_visible_for_object(srsym,contextobjdef) then
  1510. begin
  1511. { we need to know if a procedure references symbols
  1512. in the static symtable, because then it can't be
  1513. inlined from outside this unit }
  1514. if assigned(current_procinfo) and
  1515. (srsym.owner.symtabletype=staticsymtable) then
  1516. include(current_procinfo.flags,pi_uses_static_symtable);
  1517. addsymref(srsym);
  1518. result:=true;
  1519. exit;
  1520. end;
  1521. end;
  1522. { also search for class helpers }
  1523. if (srsymtable.symtabletype=objectsymtable) and
  1524. is_objcclass(tdef(srsymtable.defowner)) then
  1525. begin
  1526. if search_class_helper(tobjectdef(srsymtable.defowner),s,srsym,srsymtable) then
  1527. begin
  1528. result:=true;
  1529. exit;
  1530. end;
  1531. end;
  1532. stackitem:=stackitem^.next;
  1533. end;
  1534. srsym:=nil;
  1535. srsymtable:=nil;
  1536. end;
  1537. function searchsym_type(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  1538. var
  1539. hashedid : THashedIDString;
  1540. stackitem : psymtablestackitem;
  1541. begin
  1542. result:=false;
  1543. hashedid.id:=s;
  1544. stackitem:=symtablestack.stack;
  1545. while assigned(stackitem) do
  1546. begin
  1547. {
  1548. It is not possible to have type symbols in:
  1549. records
  1550. objects
  1551. parameters
  1552. Exception are classes, generic definitions and specializations
  1553. that have the parameterized types inserted in the symtable.
  1554. }
  1555. srsymtable:=stackitem^.symtable;
  1556. if not(srsymtable.symtabletype in [recordsymtable,ObjectSymtable,parasymtable]) or
  1557. (assigned(srsymtable.defowner) and
  1558. (
  1559. (df_generic in tdef(srsymtable.defowner).defoptions) or
  1560. (df_specialization in tdef(srsymtable.defowner).defoptions) or
  1561. is_class(tdef(srsymtable.defowner)))
  1562. ) then
  1563. begin
  1564. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  1565. if assigned(srsym) and
  1566. not(srsym.typ in [fieldvarsym,paravarsym]) and
  1567. (
  1568. (srsym.owner.symtabletype<>objectsymtable) or
  1569. (is_visible_for_object(srsym,current_objectdef) and
  1570. (srsym.typ=typesym))
  1571. ) then
  1572. begin
  1573. { we need to know if a procedure references symbols
  1574. in the static symtable, because then it can't be
  1575. inlined from outside this unit }
  1576. if assigned(current_procinfo) and
  1577. (srsym.owner.symtabletype=staticsymtable) then
  1578. include(current_procinfo.flags,pi_uses_static_symtable);
  1579. addsymref(srsym);
  1580. result:=true;
  1581. exit;
  1582. end;
  1583. end;
  1584. stackitem:=stackitem^.next;
  1585. end;
  1586. result:=false;
  1587. srsym:=nil;
  1588. srsymtable:=nil;
  1589. end;
  1590. function searchsym_in_module(pm:pointer;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  1591. var
  1592. pmod : tmodule;
  1593. begin
  1594. pmod:=tmodule(pm);
  1595. result:=false;
  1596. if assigned(pmod.globalsymtable) then
  1597. begin
  1598. srsym:=tsym(pmod.globalsymtable.Find(s));
  1599. if assigned(srsym) then
  1600. begin
  1601. srsymtable:=pmod.globalsymtable;
  1602. addsymref(srsym);
  1603. result:=true;
  1604. exit;
  1605. end;
  1606. end;
  1607. { If the module is the current unit we also need
  1608. to search the local symtable }
  1609. if (pmod=current_module) and
  1610. assigned(pmod.localsymtable) then
  1611. begin
  1612. srsym:=tsym(pmod.localsymtable.Find(s));
  1613. if assigned(srsym) then
  1614. begin
  1615. srsymtable:=pmod.localsymtable;
  1616. addsymref(srsym);
  1617. result:=true;
  1618. exit;
  1619. end;
  1620. end;
  1621. srsym:=nil;
  1622. srsymtable:=nil;
  1623. end;
  1624. function searchsym_in_named_module(const unitname, symname: TIDString; out srsym: tsym; out srsymtable: tsymtable): boolean;
  1625. var
  1626. stackitem : psymtablestackitem;
  1627. begin
  1628. result:=false;
  1629. stackitem:=symtablestack.stack;
  1630. while assigned(stackitem) do
  1631. begin
  1632. srsymtable:=stackitem^.symtable;
  1633. if (srsymtable.symtabletype=globalsymtable) and
  1634. (srsymtable.name^=unitname) then
  1635. begin
  1636. srsym:=tsym(srsymtable.find(symname));
  1637. if not assigned(srsym) then
  1638. break;
  1639. result:=true;
  1640. exit;
  1641. end;
  1642. stackitem:=stackitem^.next;
  1643. end;
  1644. { If the module is the current unit we also need
  1645. to search the local symtable }
  1646. if (current_module.globalsymtable=srsymtable) and
  1647. assigned(current_module.localsymtable) then
  1648. begin
  1649. srsymtable:=current_module.localsymtable;
  1650. srsym:=tsym(srsymtable.find(symname));
  1651. if assigned(srsym) then
  1652. begin
  1653. result:=true;
  1654. exit;
  1655. end;
  1656. end;
  1657. end;
  1658. function searchsym_in_class(classh,contextclassh:tobjectdef;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  1659. var
  1660. hashedid : THashedIDString;
  1661. orgclass : tobjectdef;
  1662. begin
  1663. orgclass:=classh;
  1664. { The contextclassh is used for visibility. The classh must be equal to
  1665. or be a parent of contextclassh. E.g. for inherited searches the classh is the
  1666. parent. }
  1667. if assigned(classh) and
  1668. not contextclassh.is_related(classh) then
  1669. internalerror(200811161);
  1670. result:=false;
  1671. hashedid.id:=s;
  1672. while assigned(classh) do
  1673. begin
  1674. srsymtable:=classh.symtable;
  1675. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  1676. if assigned(srsym) and
  1677. is_visible_for_object(srsym,contextclassh) then
  1678. begin
  1679. addsymref(srsym);
  1680. result:=true;
  1681. exit;
  1682. end;
  1683. classh:=classh.childof;
  1684. end;
  1685. if is_objcclass(orgclass) then
  1686. result:=search_class_helper(orgclass,s,srsym,srsymtable)
  1687. else
  1688. begin
  1689. srsym:=nil;
  1690. srsymtable:=nil;
  1691. end;
  1692. end;
  1693. function searchsym_in_class_by_msgint(classh:tobjectdef;msgid:longint;out srdef : tdef;out srsym:tsym;out srsymtable:TSymtable):boolean;
  1694. var
  1695. def : tdef;
  1696. i : longint;
  1697. begin
  1698. result:=false;
  1699. def:=nil;
  1700. while assigned(classh) do
  1701. begin
  1702. for i:=0 to classh.symtable.DefList.Count-1 do
  1703. begin
  1704. def:=tstoreddef(classh.symtable.DefList[i]);
  1705. { Find also all hidden private methods to
  1706. be compatible with delphi, see tw6203 (PFV) }
  1707. if (def.typ=procdef) and
  1708. (po_msgint in tprocdef(def).procoptions) and
  1709. (tprocdef(def).messageinf.i=msgid) then
  1710. begin
  1711. srdef:=def;
  1712. srsym:=tprocdef(def).procsym;
  1713. srsymtable:=classh.symtable;
  1714. addsymref(srsym);
  1715. result:=true;
  1716. exit;
  1717. end;
  1718. end;
  1719. classh:=classh.childof;
  1720. end;
  1721. srdef:=nil;
  1722. srsym:=nil;
  1723. srsymtable:=nil;
  1724. end;
  1725. function searchsym_in_class_by_msgstr(classh:tobjectdef;const s:string;out srsym:tsym;out srsymtable:TSymtable):boolean;
  1726. var
  1727. def : tdef;
  1728. i : longint;
  1729. begin
  1730. result:=false;
  1731. def:=nil;
  1732. while assigned(classh) do
  1733. begin
  1734. for i:=0 to classh.symtable.DefList.Count-1 do
  1735. begin
  1736. def:=tstoreddef(classh.symtable.DefList[i]);
  1737. { Find also all hidden private methods to
  1738. be compatible with delphi, see tw6203 (PFV) }
  1739. if (def.typ=procdef) and
  1740. (po_msgstr in tprocdef(def).procoptions) and
  1741. (tprocdef(def).messageinf.str^=s) then
  1742. begin
  1743. srsym:=tprocdef(def).procsym;
  1744. srsymtable:=classh.symtable;
  1745. addsymref(srsym);
  1746. result:=true;
  1747. exit;
  1748. end;
  1749. end;
  1750. classh:=classh.childof;
  1751. end;
  1752. srsym:=nil;
  1753. srsymtable:=nil;
  1754. end;
  1755. function search_assignment_operator(from_def,to_def:Tdef):Tprocdef;
  1756. var
  1757. sym : Tprocsym;
  1758. hashedid : THashedIDString;
  1759. curreq,
  1760. besteq : tequaltype;
  1761. currpd,
  1762. bestpd : tprocdef;
  1763. stackitem : psymtablestackitem;
  1764. begin
  1765. hashedid.id:='assign';
  1766. besteq:=te_incompatible;
  1767. bestpd:=nil;
  1768. stackitem:=symtablestack.stack;
  1769. while assigned(stackitem) do
  1770. begin
  1771. sym:=Tprocsym(stackitem^.symtable.FindWithHash(hashedid));
  1772. if sym<>nil then
  1773. begin
  1774. if sym.typ<>procsym then
  1775. internalerror(200402031);
  1776. { if the source type is an alias then this is only the second choice,
  1777. if you mess with this code, check tw4093 }
  1778. currpd:=sym.find_procdef_assignment_operator(from_def,to_def,curreq);
  1779. if curreq>besteq then
  1780. begin
  1781. besteq:=curreq;
  1782. bestpd:=currpd;
  1783. if (besteq=te_exact) then
  1784. break;
  1785. end;
  1786. end;
  1787. stackitem:=stackitem^.next;
  1788. end;
  1789. result:=bestpd;
  1790. end;
  1791. function search_enumerator_operator(type_def:Tdef): Tprocdef;
  1792. var
  1793. sym : Tprocsym;
  1794. hashedid : THashedIDString;
  1795. curreq,
  1796. besteq : tequaltype;
  1797. currpd,
  1798. bestpd : tprocdef;
  1799. stackitem : psymtablestackitem;
  1800. begin
  1801. hashedid.id:='enumerator';
  1802. besteq:=te_incompatible;
  1803. bestpd:=nil;
  1804. stackitem:=symtablestack.stack;
  1805. while assigned(stackitem) do
  1806. begin
  1807. sym:=Tprocsym(stackitem^.symtable.FindWithHash(hashedid));
  1808. if sym<>nil then
  1809. begin
  1810. if sym.typ<>procsym then
  1811. internalerror(200910241);
  1812. { if the source type is an alias then this is only the second choice,
  1813. if you mess with this code, check tw4093 }
  1814. currpd:=sym.find_procdef_enumerator_operator(type_def,curreq);
  1815. if curreq>besteq then
  1816. begin
  1817. besteq:=curreq;
  1818. bestpd:=currpd;
  1819. if (besteq=te_exact) then
  1820. break;
  1821. end;
  1822. end;
  1823. stackitem:=stackitem^.next;
  1824. end;
  1825. result:=bestpd;
  1826. end;
  1827. function search_system_type(const s: TIDString): ttypesym;
  1828. var
  1829. sym : tsym;
  1830. begin
  1831. sym:=tsym(systemunit.Find(s));
  1832. if not assigned(sym) or
  1833. (sym.typ<>typesym) then
  1834. cgmessage1(cg_f_unknown_system_type,s);
  1835. result:=ttypesym(sym);
  1836. end;
  1837. function search_named_unit_globaltype(const unitname, typename: TIDString): ttypesym;
  1838. var
  1839. srsymtable: tsymtable;
  1840. sym: tsym;
  1841. begin
  1842. if searchsym_in_named_module(unitname,typename,sym,srsymtable) and
  1843. (sym.typ=typesym) then
  1844. begin
  1845. result:=ttypesym(sym);
  1846. exit;
  1847. end
  1848. else
  1849. begin
  1850. cgmessage2(cg_f_unknown_type_in_unit,typename,unitname);
  1851. result:=nil;
  1852. end;
  1853. end;
  1854. function search_class_helper(pd : tobjectdef;const s : string; out srsym: tsym; out srsymtable: tsymtable):boolean;
  1855. var
  1856. hashedid : THashedIDString;
  1857. stackitem : psymtablestackitem;
  1858. i : longint;
  1859. defowner : tobjectdef;
  1860. begin
  1861. hashedid.id:=class_helper_prefix+s;
  1862. stackitem:=symtablestack.stack;
  1863. while assigned(stackitem) do
  1864. begin
  1865. srsymtable:=stackitem^.symtable;
  1866. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  1867. if assigned(srsym) then
  1868. begin
  1869. if not(srsymtable.symtabletype in [globalsymtable,staticsymtable]) or
  1870. not(srsym.owner.symtabletype in [globalsymtable,staticsymtable]) or
  1871. (srsym.typ<>procsym) then
  1872. internalerror(2009111505);
  1873. { check whether this procsym includes a helper for this particular class }
  1874. for i:=0 to tprocsym(srsym).procdeflist.count-1 do
  1875. begin
  1876. { does pd inherit from (or is the same as) the class
  1877. that this method's category extended?
  1878. Warning: this list contains both category and objcclass methods
  1879. (for id.randommethod), so only check category methods here
  1880. }
  1881. defowner:=tobjectdef(tprocdef(tprocsym(srsym).procdeflist[i]).owner.defowner);
  1882. if (oo_is_classhelper in defowner.objectoptions) and
  1883. pd.is_related(defowner.childof) then
  1884. begin
  1885. { we need to know if a procedure references symbols
  1886. in the static symtable, because then it can't be
  1887. inlined from outside this unit }
  1888. if assigned(current_procinfo) and
  1889. (srsym.owner.symtabletype=staticsymtable) then
  1890. include(current_procinfo.flags,pi_uses_static_symtable);
  1891. { no need to keep looking. There might be other
  1892. categories that extend this, a parent or child
  1893. class with a method with the same name (either
  1894. overriding this one, or overridden by this one),
  1895. but that doesn't matter as far as the basic
  1896. procsym is concerned.
  1897. }
  1898. srsym:=tprocdef(tprocsym(srsym).procdeflist[i]).procsym;
  1899. srsymtable:=srsym.owner;
  1900. addsymref(srsym);
  1901. result:=true;
  1902. exit;
  1903. end;
  1904. end;
  1905. end;
  1906. stackitem:=stackitem^.next;
  1907. end;
  1908. srsym:=nil;
  1909. srsymtable:=nil;
  1910. result:=false;
  1911. end;
  1912. function search_objc_method(const s : string; out srsym: tsym; out srsymtable: tsymtable):boolean;
  1913. var
  1914. hashedid : THashedIDString;
  1915. stackitem : psymtablestackitem;
  1916. i : longint;
  1917. begin
  1918. hashedid.id:=class_helper_prefix+s;
  1919. stackitem:=symtablestack.stack;
  1920. while assigned(stackitem) do
  1921. begin
  1922. srsymtable:=stackitem^.symtable;
  1923. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  1924. if assigned(srsym) then
  1925. begin
  1926. if not(srsymtable.symtabletype in [globalsymtable,staticsymtable]) or
  1927. not(srsym.owner.symtabletype in [globalsymtable,staticsymtable]) or
  1928. (srsym.typ<>procsym) then
  1929. internalerror(2009112005);
  1930. { check whether this procsym includes a helper for this particular class }
  1931. for i:=0 to tprocsym(srsym).procdeflist.count-1 do
  1932. begin
  1933. { we need to know if a procedure references symbols
  1934. in the static symtable, because then it can't be
  1935. inlined from outside this unit }
  1936. if assigned(current_procinfo) and
  1937. (srsym.owner.symtabletype=staticsymtable) then
  1938. include(current_procinfo.flags,pi_uses_static_symtable);
  1939. { no need to keep looking. There might be other
  1940. methods with the same name, but that doesn't matter
  1941. as far as the basic procsym is concerned.
  1942. }
  1943. srsym:=tprocdef(tprocsym(srsym).procdeflist[i]).procsym;
  1944. { We need the symtable in which the classhelper-like sym
  1945. is located, not the objectdef. The reason is that the
  1946. callnode will climb the symtablestack until it encounters
  1947. this symtable to start looking for overloads (and it won't
  1948. find the objectsymtable in which this method sym is
  1949. located
  1950. srsymtable:=srsym.owner;
  1951. }
  1952. addsymref(srsym);
  1953. result:=true;
  1954. exit;
  1955. end;
  1956. end;
  1957. stackitem:=stackitem^.next;
  1958. end;
  1959. srsym:=nil;
  1960. srsymtable:=nil;
  1961. result:=false;
  1962. end;
  1963. function search_class_member(pd : tobjectdef;const s : string):tsym;
  1964. { searches n in symtable of pd and all anchestors }
  1965. var
  1966. hashedid : THashedIDString;
  1967. srsym : tsym;
  1968. orgpd : tobjectdef;
  1969. srsymtable : tsymtable;
  1970. begin
  1971. hashedid.id:=s;
  1972. orgpd:=pd;
  1973. while assigned(pd) do
  1974. begin
  1975. srsym:=tsym(pd.symtable.FindWithHash(hashedid));
  1976. if assigned(srsym) then
  1977. begin
  1978. search_class_member:=srsym;
  1979. exit;
  1980. end;
  1981. pd:=pd.childof;
  1982. end;
  1983. { not found, now look for class helpers }
  1984. if is_objcclass(pd) then
  1985. search_class_helper(orgpd,s,result,srsymtable)
  1986. else
  1987. result:=nil;
  1988. end;
  1989. function search_macro(const s : string):tsym;
  1990. var
  1991. stackitem : psymtablestackitem;
  1992. hashedid : THashedIDString;
  1993. srsym : tsym;
  1994. begin
  1995. hashedid.id:=s;
  1996. { First search the localmacrosymtable before searching the
  1997. global macrosymtables from the units }
  1998. if assigned(current_module) then
  1999. begin
  2000. srsym:=tsym(current_module.localmacrosymtable.FindWithHash(hashedid));
  2001. if assigned(srsym) then
  2002. begin
  2003. result:= srsym;
  2004. exit;
  2005. end;
  2006. end;
  2007. stackitem:=macrosymtablestack.stack;
  2008. while assigned(stackitem) do
  2009. begin
  2010. srsym:=tsym(stackitem^.symtable.FindWithHash(hashedid));
  2011. if assigned(srsym) then
  2012. begin
  2013. result:= srsym;
  2014. exit;
  2015. end;
  2016. stackitem:=stackitem^.next;
  2017. end;
  2018. result:= nil;
  2019. end;
  2020. function defined_macro(const s : string):boolean;
  2021. var
  2022. mac: tmacro;
  2023. begin
  2024. mac:=tmacro(search_macro(s));
  2025. if assigned(mac) then
  2026. begin
  2027. mac.is_used:=true;
  2028. defined_macro:=mac.defined;
  2029. end
  2030. else
  2031. defined_macro:=false;
  2032. end;
  2033. {****************************************************************************
  2034. Object Helpers
  2035. ****************************************************************************}
  2036. function search_default_property(pd : tobjectdef) : tpropertysym;
  2037. { returns the default property of a class, searches also anchestors }
  2038. var
  2039. _defaultprop : tpropertysym;
  2040. begin
  2041. _defaultprop:=nil;
  2042. while assigned(pd) do
  2043. begin
  2044. pd.symtable.SymList.ForEachCall(@tstoredsymtable(pd.symtable).testfordefaultproperty,@_defaultprop);
  2045. if assigned(_defaultprop) then
  2046. break;
  2047. pd:=pd.childof;
  2048. end;
  2049. search_default_property:=_defaultprop;
  2050. end;
  2051. {****************************************************************************
  2052. Macro Helpers
  2053. ****************************************************************************}
  2054. procedure def_system_macro(const name : string);
  2055. var
  2056. mac : tmacro;
  2057. s: string;
  2058. begin
  2059. if name = '' then
  2060. internalerror(2004121202);
  2061. s:= upper(name);
  2062. mac:=tmacro(search_macro(s));
  2063. if not assigned(mac) then
  2064. begin
  2065. mac:=tmacro.create(s);
  2066. if assigned(current_module) then
  2067. current_module.localmacrosymtable.insert(mac)
  2068. else
  2069. initialmacrosymtable.insert(mac);
  2070. end;
  2071. if not mac.defined then
  2072. Message1(parser_c_macro_defined,mac.name);
  2073. mac.defined:=true;
  2074. end;
  2075. procedure set_system_macro(const name, value : string);
  2076. var
  2077. mac : tmacro;
  2078. s: string;
  2079. begin
  2080. if name = '' then
  2081. internalerror(2004121203);
  2082. s:= upper(name);
  2083. mac:=tmacro(search_macro(s));
  2084. if not assigned(mac) then
  2085. begin
  2086. mac:=tmacro.create(s);
  2087. if assigned(current_module) then
  2088. current_module.localmacrosymtable.insert(mac)
  2089. else
  2090. initialmacrosymtable.insert(mac);
  2091. end
  2092. else
  2093. begin
  2094. mac.is_compiler_var:=false;
  2095. if assigned(mac.buftext) then
  2096. freemem(mac.buftext,mac.buflen);
  2097. end;
  2098. Message2(parser_c_macro_set_to,mac.name,value);
  2099. mac.buflen:=length(value);
  2100. getmem(mac.buftext,mac.buflen);
  2101. move(value[1],mac.buftext^,mac.buflen);
  2102. mac.defined:=true;
  2103. end;
  2104. procedure set_system_compvar(const name, value : string);
  2105. var
  2106. mac : tmacro;
  2107. s: string;
  2108. begin
  2109. if name = '' then
  2110. internalerror(2004121204);
  2111. s:= upper(name);
  2112. mac:=tmacro(search_macro(s));
  2113. if not assigned(mac) then
  2114. begin
  2115. mac:=tmacro.create(s);
  2116. mac.is_compiler_var:=true;
  2117. if assigned(current_module) then
  2118. current_module.localmacrosymtable.insert(mac)
  2119. else
  2120. initialmacrosymtable.insert(mac);
  2121. end
  2122. else
  2123. begin
  2124. mac.is_compiler_var:=true;
  2125. if assigned(mac.buftext) then
  2126. freemem(mac.buftext,mac.buflen);
  2127. end;
  2128. Message2(parser_c_macro_set_to,mac.name,value);
  2129. mac.buflen:=length(value);
  2130. getmem(mac.buftext,mac.buflen);
  2131. move(value[1],mac.buftext^,mac.buflen);
  2132. mac.defined:=true;
  2133. end;
  2134. procedure undef_system_macro(const name : string);
  2135. var
  2136. mac : tmacro;
  2137. s: string;
  2138. begin
  2139. if name = '' then
  2140. internalerror(2004121205);
  2141. s:= upper(name);
  2142. mac:=tmacro(search_macro(s));
  2143. if not assigned(mac) then
  2144. {If not found, then it's already undefined.}
  2145. else
  2146. begin
  2147. if mac.defined then
  2148. Message1(parser_c_macro_undefined,mac.name);
  2149. mac.defined:=false;
  2150. mac.is_compiler_var:=false;
  2151. { delete old definition }
  2152. if assigned(mac.buftext) then
  2153. begin
  2154. freemem(mac.buftext,mac.buflen);
  2155. mac.buftext:=nil;
  2156. end;
  2157. end;
  2158. end;
  2159. {$ifdef UNITALIASES}
  2160. {****************************************************************************
  2161. TUNIT_ALIAS
  2162. ****************************************************************************}
  2163. constructor tunit_alias.create(const n:string);
  2164. var
  2165. i : longint;
  2166. begin
  2167. i:=pos('=',n);
  2168. if i=0 then
  2169. fail;
  2170. inherited createname(Copy(n,1,i-1));
  2171. newname:=stringdup(Copy(n,i+1,255));
  2172. end;
  2173. destructor tunit_alias.destroy;
  2174. begin
  2175. stringdispose(newname);
  2176. inherited destroy;
  2177. end;
  2178. procedure addunitalias(const n:string);
  2179. begin
  2180. unitaliases^.insert(tunit_alias,init(Upper(n))));
  2181. end;
  2182. function getunitalias(const n:string):string;
  2183. var
  2184. p : punit_alias;
  2185. begin
  2186. p:=punit_alias(unitaliases^.Find(Upper(n)));
  2187. if assigned(p) then
  2188. getunitalias:=punit_alias(p).newname^
  2189. else
  2190. getunitalias:=n;
  2191. end;
  2192. {$endif UNITALIASES}
  2193. {****************************************************************************
  2194. Init/Done Symtable
  2195. ****************************************************************************}
  2196. procedure InitSymtable;
  2197. begin
  2198. { Reset symbolstack }
  2199. symtablestack:=nil;
  2200. systemunit:=nil;
  2201. { create error syms and def }
  2202. generrorsym:=terrorsym.create;
  2203. generrordef:=terrordef.create;
  2204. { macros }
  2205. initialmacrosymtable:=tmacrosymtable.create(false);
  2206. macrosymtablestack:=TSymtablestack.create;
  2207. macrosymtablestack.push(initialmacrosymtable);
  2208. {$ifdef UNITALIASES}
  2209. { unit aliases }
  2210. unitaliases:=TFPHashObjectList.create;
  2211. {$endif}
  2212. { set some global vars to nil, might be important for the ide }
  2213. class_tobject:=nil;
  2214. interface_iunknown:=nil;
  2215. rec_tguid:=nil;
  2216. dupnr:=0;
  2217. end;
  2218. procedure DoneSymtable;
  2219. begin
  2220. generrorsym.owner:=nil;
  2221. generrorsym.free;
  2222. generrordef.owner:=nil;
  2223. generrordef.free;
  2224. initialmacrosymtable.free;
  2225. macrosymtablestack.free;
  2226. {$ifdef UNITALIASES}
  2227. unitaliases.free;
  2228. {$endif}
  2229. end;
  2230. end.