symtable.pas 87 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505
  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. (sp_internal in tsym(sym).symoptions) 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. { do not claim for inherited private fields !! }
  542. if (tsym(sym).refs=0) and (tsym(sym).owner.symtabletype=ObjectSymtable) then
  543. MessagePos2(tsym(sym).fileinfo,sym_n_private_method_not_used,tsym(sym).owner.realname^,tsym(sym).prettyname)
  544. { units references are problematic }
  545. else
  546. begin
  547. if (tsym(sym).refs=0) and
  548. not(tsym(sym).typ in [enumsym,unitsym]) and
  549. not(is_funcret_sym(tsym(sym))) and
  550. { don't complain about compiler generated syms for specializations, see also #13405 }
  551. not((tsym(sym).typ=typesym) and (df_specialization in ttypesym(sym).typedef.defoptions) and
  552. (pos('$',ttypesym(sym).Realname)<>0)) and
  553. (
  554. (tsym(sym).typ<>procsym) or
  555. ((tsym(sym).owner.symtabletype=staticsymtable) and
  556. not current_module.is_unit)
  557. ) and
  558. { don't complain about alias for hidden _cmd parameter to
  559. obj-c methods }
  560. not((tsym(sym).typ in [localvarsym,paravarsym,absolutevarsym]) and
  561. (vo_is_msgsel in tabstractvarsym(sym).varoptions)) then
  562. MessagePos2(tsym(sym).fileinfo,sym_h_local_symbol_not_used,SymTypeName[tsym(sym).typ],tsym(sym).prettyname);
  563. end;
  564. end;
  565. end;
  566. procedure TStoredSymtable.TestPrivate(sym:TObject;arg:pointer);
  567. begin
  568. if tsym(sym).visibility=vis_private then
  569. varsymbolused(sym,arg);
  570. end;
  571. procedure TStoredSymtable.objectprivatesymbolused(sym:TObject;arg:pointer);
  572. begin
  573. {
  574. Don't test simple object aliases PM
  575. }
  576. if (tsym(sym).typ=typesym) and
  577. (ttypesym(sym).typedef.typ=objectdef) and
  578. (ttypesym(sym).typedef.typesym=tsym(sym)) then
  579. tobjectdef(ttypesym(sym).typedef).symtable.SymList.ForEachCall(@TestPrivate,nil);
  580. end;
  581. procedure tstoredsymtable.testfordefaultproperty(sym:TObject;arg:pointer);
  582. begin
  583. if (tsym(sym).typ=propertysym) and
  584. (ppo_defaultproperty in tpropertysym(sym).propoptions) then
  585. ppointer(arg)^:=sym;
  586. end;
  587. {***********************************************
  588. Process all entries
  589. ***********************************************}
  590. procedure Tstoredsymtable.reset_all_defs;
  591. var
  592. i : longint;
  593. def : tstoreddef;
  594. begin
  595. for i:=0 to DefList.Count-1 do
  596. begin
  597. def:=tstoreddef(DefList[i]);
  598. def.reset;
  599. end;
  600. end;
  601. { checks, if all procsyms and methods are defined }
  602. procedure tstoredsymtable.check_forwards;
  603. begin
  604. SymList.ForEachCall(@check_forward,nil);
  605. end;
  606. procedure tstoredsymtable.checklabels;
  607. begin
  608. SymList.ForEachCall(@labeldefined,nil);
  609. end;
  610. procedure tstoredsymtable.allsymbolsused;
  611. begin
  612. SymList.ForEachCall(@varsymbolused,nil);
  613. end;
  614. procedure tstoredsymtable.allprivatesused;
  615. begin
  616. SymList.ForEachCall(@objectprivatesymbolused,nil);
  617. end;
  618. procedure TStoredSymtable._needs_init_final(sym:TObject;arg:pointer);
  619. begin
  620. if b_needs_init_final then
  621. exit;
  622. case tsym(sym).typ of
  623. fieldvarsym,
  624. staticvarsym,
  625. localvarsym,
  626. paravarsym :
  627. begin
  628. if not(is_class(tabstractvarsym(sym).vardef)) and
  629. tstoreddef(tabstractvarsym(sym).vardef).needs_inittable then
  630. b_needs_init_final:=true;
  631. end;
  632. end;
  633. end;
  634. { returns true, if p contains data which needs init/final code }
  635. function tstoredsymtable.needs_init_final : boolean;
  636. begin
  637. b_needs_init_final:=false;
  638. SymList.ForEachCall(@_needs_init_final,nil);
  639. needs_init_final:=b_needs_init_final;
  640. end;
  641. {****************************************************************************
  642. TAbstractRecordSymtable
  643. ****************************************************************************}
  644. constructor tabstractrecordsymtable.create(const n:string;usealign:shortint);
  645. begin
  646. inherited create(n);
  647. _datasize:=0;
  648. databitsize:=0;
  649. recordalignment:=1;
  650. usefieldalignment:=usealign;
  651. padalignment:=1;
  652. { recordalign C_alignment means C record packing, that starts
  653. with an alignment of 1 }
  654. case usealign of
  655. C_alignment,
  656. bit_alignment:
  657. fieldalignment:=1;
  658. mac68k_alignment:
  659. fieldalignment:=2;
  660. else
  661. fieldalignment:=usealign;
  662. end;
  663. end;
  664. procedure tabstractrecordsymtable.ppuload(ppufile:tcompilerppufile);
  665. begin
  666. inherited ppuload(ppufile);
  667. end;
  668. procedure tabstractrecordsymtable.ppuwrite(ppufile:tcompilerppufile);
  669. var
  670. oldtyp : byte;
  671. begin
  672. oldtyp:=ppufile.entrytyp;
  673. ppufile.entrytyp:=subentryid;
  674. inherited ppuwrite(ppufile);
  675. ppufile.entrytyp:=oldtyp;
  676. end;
  677. function field2recordalignment(fieldoffs, fieldalign: aint): aint;
  678. begin
  679. { optimal alignment of the record when declaring a variable of this }
  680. { type is independent of the packrecords setting }
  681. if (fieldoffs mod fieldalign) = 0 then
  682. result:=fieldalign
  683. else if (fieldalign >= 16) and
  684. ((fieldoffs mod 16) = 0) and
  685. ((fieldalign mod 16) = 0) then
  686. result:=16
  687. else if (fieldalign >= 8) and
  688. ((fieldoffs mod 8) = 0) and
  689. ((fieldalign mod 8) = 0) then
  690. result:=8
  691. else if (fieldalign >= 4) and
  692. ((fieldoffs mod 4) = 0) and
  693. ((fieldalign mod 4) = 0) then
  694. result:=4
  695. else if (fieldalign >= 2) and
  696. ((fieldoffs mod 2) = 0) and
  697. ((fieldalign mod 2) = 0) then
  698. result:=2
  699. else
  700. result:=1;
  701. end;
  702. procedure tabstractrecordsymtable.alignrecord(fieldoffset:aint;varalign:shortint);
  703. var
  704. varalignrecord: shortint;
  705. begin
  706. case usefieldalignment of
  707. C_alignment:
  708. varalignrecord:=used_align(varalign,current_settings.alignment.recordalignmin,current_settings.alignment.maxCrecordalign);
  709. mac68k_alignment:
  710. varalignrecord:=2;
  711. else
  712. varalignrecord:=field2recordalignment(fieldoffset,varalign);
  713. end;
  714. recordalignment:=max(recordalignment,varalignrecord);
  715. end;
  716. procedure tabstractrecordsymtable.addfield(sym:tfieldvarsym;vis:tvisibility);
  717. var
  718. l : aint;
  719. varalignfield,
  720. varalign : shortint;
  721. vardef : tdef;
  722. begin
  723. if (sym.owner<>self) then
  724. internalerror(200602031);
  725. if sym.fieldoffset<>-1 then
  726. internalerror(200602032);
  727. { set visibility for the symbol }
  728. sym.visibility:=vis;
  729. { this symbol can't be loaded to a register }
  730. sym.varregable:=vr_none;
  731. { Calculate field offset }
  732. l:=sym.getsize;
  733. vardef:=sym.vardef;
  734. varalign:=vardef.alignment;
  735. case usefieldalignment of
  736. bit_alignment:
  737. begin
  738. { bitpacking only happens for ordinals, the rest is aligned at }
  739. { 1 byte (compatible with GPC/GCC) }
  740. if is_ordinal(vardef) then
  741. begin
  742. sym.fieldoffset:=databitsize;
  743. l:=sym.getpackedbitsize;
  744. end
  745. else
  746. begin
  747. databitsize:=_datasize*8;
  748. sym.fieldoffset:=databitsize;
  749. if (l>high(aint) div 8) then
  750. Message(sym_e_segment_too_large);
  751. l:=l*8;
  752. end;
  753. if varalign=0 then
  754. varalign:=size_2_align(l);
  755. recordalignment:=max(recordalignment,field2recordalignment(databitsize mod 8,varalign));
  756. { bit packed records are limited to high(aint) bits }
  757. { instead of bytes to avoid double precision }
  758. { arithmetic in offset calculations }
  759. if int64(l)>high(aint)-sym.fieldoffset then
  760. begin
  761. Message(sym_e_segment_too_large);
  762. _datasize:=high(aint);
  763. databitsize:=high(aint);
  764. end
  765. else
  766. begin
  767. databitsize:=sym.fieldoffset+l;
  768. _datasize:=(databitsize+7) div 8;
  769. end;
  770. { rest is not applicable }
  771. exit;
  772. end;
  773. { Calc the alignment size for C style records }
  774. C_alignment:
  775. begin
  776. if (varalign>4) and
  777. ((varalign mod 4)<>0) and
  778. (vardef.typ=arraydef) then
  779. Message1(sym_w_wrong_C_pack,vardef.typename);
  780. if varalign=0 then
  781. varalign:=l;
  782. if (fieldalignment<current_settings.alignment.maxCrecordalign) then
  783. begin
  784. if (varalign>16) and (fieldalignment<32) then
  785. fieldalignment:=32
  786. else if (varalign>12) and (fieldalignment<16) then
  787. fieldalignment:=16
  788. { 12 is needed for long double }
  789. else if (varalign>8) and (fieldalignment<12) then
  790. fieldalignment:=12
  791. else if (varalign>4) and (fieldalignment<8) then
  792. fieldalignment:=8
  793. else if (varalign>2) and (fieldalignment<4) then
  794. fieldalignment:=4
  795. else if (varalign>1) and (fieldalignment<2) then
  796. fieldalignment:=2;
  797. end;
  798. fieldalignment:=min(fieldalignment,current_settings.alignment.maxCrecordalign);
  799. end;
  800. mac68k_alignment:
  801. begin
  802. { mac68k alignment (C description):
  803. * char is aligned to 1 byte
  804. * everything else (except vector) is aligned to 2 bytes
  805. * vector is aligned to 16 bytes
  806. }
  807. if l>1 then
  808. fieldalignment:=2
  809. else
  810. fieldalignment:=1;
  811. varalign:=2;
  812. end;
  813. end;
  814. if varalign=0 then
  815. varalign:=size_2_align(l);
  816. varalignfield:=used_align(varalign,current_settings.alignment.recordalignmin,fieldalignment);
  817. sym.fieldoffset:=align(_datasize,varalignfield);
  818. if l>high(aint)-sym.fieldoffset then
  819. begin
  820. Message(sym_e_segment_too_large);
  821. _datasize:=high(aint);
  822. end
  823. else
  824. _datasize:=sym.fieldoffset+l;
  825. { Calc alignment needed for this record }
  826. alignrecord(sym.fieldoffset,varalign);
  827. end;
  828. procedure tabstractrecordsymtable.addalignmentpadding;
  829. begin
  830. { make the record size aligned correctly so it can be
  831. used as elements in an array. For C records we
  832. use the fieldalignment, because that is updated with the
  833. used alignment. }
  834. if (padalignment = 1) then
  835. case usefieldalignment of
  836. C_alignment:
  837. padalignment:=fieldalignment;
  838. { bitpacked }
  839. bit_alignment:
  840. padalignment:=1;
  841. { mac68k: always round to multiple of 2 }
  842. mac68k_alignment:
  843. padalignment:=2;
  844. { default/no packrecords specified }
  845. 0:
  846. padalignment:=recordalignment
  847. { specific packrecords setting -> use as upper limit }
  848. else
  849. padalignment:=min(recordalignment,usefieldalignment);
  850. end;
  851. _datasize:=align(_datasize,padalignment);
  852. end;
  853. procedure tabstractrecordsymtable.insertdef(def:TDefEntry);
  854. begin
  855. { Enums must also be available outside the record scope,
  856. insert in the owner of this symtable }
  857. if def.typ=enumdef then
  858. defowner.owner.insertdef(def)
  859. else
  860. inherited insertdef(def);
  861. end;
  862. function tabstractrecordsymtable.is_packed: boolean;
  863. begin
  864. result:=usefieldalignment=bit_alignment;
  865. end;
  866. procedure tabstractrecordsymtable.setdatasize(val: aint);
  867. begin
  868. _datasize:=val;
  869. if (usefieldalignment=bit_alignment) then
  870. { can overflow in non bitpacked records }
  871. databitsize:=val*8;
  872. end;
  873. {****************************************************************************
  874. TRecordSymtable
  875. ****************************************************************************}
  876. constructor trecordsymtable.create(usealign:shortint);
  877. begin
  878. inherited create('',usealign);
  879. symtabletype:=recordsymtable;
  880. end;
  881. { this procedure is reserved for inserting case variant into
  882. a record symtable }
  883. { the offset is the location of the start of the variant
  884. and datasize and dataalignment corresponds to
  885. the complete size (see code in pdecl unit) PM }
  886. procedure trecordsymtable.insertunionst(unionst : trecordsymtable;offset : longint);
  887. var
  888. sym : tsym;
  889. def : tdef;
  890. i : integer;
  891. varalignrecord,varalign,
  892. storesize,storealign : aint;
  893. bitsize: aint;
  894. begin
  895. storesize:=_datasize;
  896. storealign:=fieldalignment;
  897. _datasize:=offset;
  898. if (usefieldalignment=bit_alignment) then
  899. databitsize:=offset*8;
  900. { We move the ownership of the defs and symbols to the new recordsymtable.
  901. The old unionsymtable keeps the references, but doesn't own the
  902. objects anymore }
  903. unionst.DefList.OwnsObjects:=false;
  904. unionst.SymList.OwnsObjects:=false;
  905. { copy symbols }
  906. for i:=0 to unionst.SymList.Count-1 do
  907. begin
  908. sym:=TSym(unionst.SymList[i]);
  909. if sym.typ<>fieldvarsym then
  910. internalerror(200601272);
  911. { add to this record symtable }
  912. // unionst.SymList.List.List^[i].Data:=nil;
  913. sym.ChangeOwner(self);
  914. varalign:=tfieldvarsym(sym).vardef.alignment;
  915. if varalign=0 then
  916. varalign:=size_2_align(tfieldvarsym(sym).getsize);
  917. { retrieve size }
  918. if (usefieldalignment=bit_alignment) then
  919. begin
  920. { bit packed records are limited to high(aint) bits }
  921. { instead of bytes to avoid double precision }
  922. { arithmetic in offset calculations }
  923. if is_ordinal(tfieldvarsym(sym).vardef) then
  924. bitsize:=tfieldvarsym(sym).getpackedbitsize
  925. else
  926. begin
  927. bitsize:=tfieldvarsym(sym).getsize;
  928. if (bitsize>high(aint) div 8) then
  929. Message(sym_e_segment_too_large);
  930. bitsize:=bitsize*8;
  931. end;
  932. if bitsize>high(aint)-databitsize then
  933. begin
  934. Message(sym_e_segment_too_large);
  935. _datasize:=high(aint);
  936. databitsize:=high(aint);
  937. end
  938. else
  939. begin
  940. databitsize:=tfieldvarsym(sym).fieldoffset+offset*8;
  941. _datasize:=(databitsize+7) div 8;
  942. end;
  943. tfieldvarsym(sym).fieldoffset:=databitsize;
  944. varalignrecord:=field2recordalignment(tfieldvarsym(sym).fieldoffset div 8,varalign);
  945. end
  946. else
  947. begin
  948. if tfieldvarsym(sym).getsize>high(aint)-_datasize then
  949. begin
  950. Message(sym_e_segment_too_large);
  951. _datasize:=high(aint);
  952. end
  953. else
  954. _datasize:=tfieldvarsym(sym).fieldoffset+offset;
  955. { update address }
  956. tfieldvarsym(sym).fieldoffset:=_datasize;
  957. varalignrecord:=field2recordalignment(tfieldvarsym(sym).fieldoffset,varalign);
  958. end;
  959. { update alignment of this record }
  960. if (usefieldalignment<>C_alignment) and
  961. (usefieldalignment<>mac68k_alignment) then
  962. recordalignment:=max(recordalignment,varalignrecord);
  963. end;
  964. { update alignment for C records }
  965. if (usefieldalignment=C_alignment) and
  966. (usefieldalignment<>mac68k_alignment) then
  967. recordalignment:=max(recordalignment,unionst.recordalignment);
  968. { Register defs in the new record symtable }
  969. for i:=0 to unionst.DefList.Count-1 do
  970. begin
  971. def:=TDef(unionst.DefList[i]);
  972. def.ChangeOwner(self);
  973. end;
  974. _datasize:=storesize;
  975. fieldalignment:=storealign;
  976. end;
  977. {****************************************************************************
  978. TObjectSymtable
  979. ****************************************************************************}
  980. constructor tObjectSymtable.create(adefowner:tdef;const n:string;usealign:shortint);
  981. begin
  982. inherited create(n,usealign);
  983. symtabletype:=ObjectSymtable;
  984. defowner:=adefowner;
  985. end;
  986. function tObjectSymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  987. var
  988. hsym : tsym;
  989. begin
  990. result:=false;
  991. if not assigned(defowner) then
  992. internalerror(200602061);
  993. { procsym and propertysym have special code
  994. to override values in inherited classes. For other
  995. symbols check for duplicates }
  996. if not(sym.typ in [procsym,propertysym]) then
  997. begin
  998. { but private ids can be reused }
  999. hsym:=search_class_member(tobjectdef(defowner),hashedid.id);
  1000. if assigned(hsym) and
  1001. (
  1002. (
  1003. not(m_delphi in current_settings.modeswitches) and
  1004. is_visible_for_object(hsym,tobjectdef(defowner))
  1005. ) or
  1006. (
  1007. { In Delphi, you can repeat members of a parent class. You can't }
  1008. { do this for objects however, and you (obviouly) can't }
  1009. { declare two fields with the same name in a single class }
  1010. (m_delphi in current_settings.modeswitches) and
  1011. (
  1012. is_object(tdef(defowner)) or
  1013. (hsym.owner = self)
  1014. )
  1015. )
  1016. ) then
  1017. begin
  1018. DuplicateSym(hashedid,sym,hsym);
  1019. result:=true;
  1020. end;
  1021. end
  1022. else
  1023. begin
  1024. if not(m_duplicate_names in current_settings.modeswitches) then
  1025. result:=inherited checkduplicate(hashedid,sym);
  1026. end;
  1027. end;
  1028. {****************************************************************************
  1029. TAbstractLocalSymtable
  1030. ****************************************************************************}
  1031. procedure tabstractlocalsymtable.ppuwrite(ppufile:tcompilerppufile);
  1032. var
  1033. oldtyp : byte;
  1034. begin
  1035. oldtyp:=ppufile.entrytyp;
  1036. ppufile.entrytyp:=subentryid;
  1037. { write definitions }
  1038. writedefs(ppufile);
  1039. { write symbols }
  1040. writesyms(ppufile);
  1041. ppufile.entrytyp:=oldtyp;
  1042. end;
  1043. function tabstractlocalsymtable.count_locals:longint;
  1044. var
  1045. i : longint;
  1046. sym : tsym;
  1047. begin
  1048. result:=0;
  1049. for i:=0 to SymList.Count-1 do
  1050. begin
  1051. sym:=tsym(SymList[i]);
  1052. { Count only varsyms, but ignore the funcretsym }
  1053. if (tsym(sym).typ in [localvarsym,paravarsym]) and
  1054. (tsym(sym)<>current_procinfo.procdef.funcretsym) and
  1055. (not(vo_is_parentfp in tabstractvarsym(sym).varoptions) or
  1056. (tstoredsym(sym).refs>0)) then
  1057. inc(result);
  1058. end;
  1059. end;
  1060. {****************************************************************************
  1061. TLocalSymtable
  1062. ****************************************************************************}
  1063. constructor tlocalsymtable.create(adefowner:tdef;level:byte);
  1064. begin
  1065. inherited create('');
  1066. defowner:=adefowner;
  1067. symtabletype:=localsymtable;
  1068. symtablelevel:=level;
  1069. end;
  1070. function tlocalsymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  1071. var
  1072. hsym : tsym;
  1073. begin
  1074. if not assigned(defowner) or
  1075. (defowner.typ<>procdef) then
  1076. internalerror(200602042);
  1077. result:=false;
  1078. hsym:=tsym(FindWithHash(hashedid));
  1079. if assigned(hsym) then
  1080. begin
  1081. { a local and the function can have the same
  1082. name in TP and Delphi, but RESULT not }
  1083. if (m_duplicate_names in current_settings.modeswitches) and
  1084. (hsym.typ in [absolutevarsym,localvarsym]) and
  1085. (vo_is_funcret in tabstractvarsym(hsym).varoptions) and
  1086. not((m_result in current_settings.modeswitches) and
  1087. (vo_is_result in tabstractvarsym(hsym).varoptions)) then
  1088. HideSym(hsym)
  1089. else
  1090. DuplicateSym(hashedid,sym,hsym);
  1091. result:=true;
  1092. exit;
  1093. end;
  1094. { check also parasymtable, this needs to be done here becuase
  1095. of the special situation with the funcret sym that needs to be
  1096. hidden for tp and delphi modes }
  1097. hsym:=tsym(tabstractprocdef(defowner).parast.FindWithHash(hashedid));
  1098. if assigned(hsym) then
  1099. begin
  1100. { a local and the function can have the same
  1101. name in TP and Delphi, but RESULT not }
  1102. if (m_duplicate_names in current_settings.modeswitches) and
  1103. (sym.typ in [absolutevarsym,localvarsym]) and
  1104. (vo_is_funcret in tabstractvarsym(sym).varoptions) and
  1105. not((m_result in current_settings.modeswitches) and
  1106. (vo_is_result in tabstractvarsym(sym).varoptions)) then
  1107. Hidesym(sym)
  1108. else
  1109. DuplicateSym(hashedid,sym,hsym);
  1110. result:=true;
  1111. exit;
  1112. end;
  1113. { check ObjectSymtable, skip this for funcret sym because
  1114. that will always be positive because it has the same name
  1115. as the procsym }
  1116. if not is_funcret_sym(sym) and
  1117. (defowner.typ=procdef) and
  1118. assigned(tprocdef(defowner)._class) and
  1119. (tprocdef(defowner).owner.defowner=tprocdef(defowner)._class) and
  1120. (
  1121. not(m_delphi in current_settings.modeswitches) or
  1122. is_object(tprocdef(defowner)._class)
  1123. ) then
  1124. result:=tprocdef(defowner)._class.symtable.checkduplicate(hashedid,sym);
  1125. end;
  1126. {****************************************************************************
  1127. TParaSymtable
  1128. ****************************************************************************}
  1129. constructor tparasymtable.create(adefowner:tdef;level:byte);
  1130. begin
  1131. inherited create('');
  1132. defowner:=adefowner;
  1133. symtabletype:=parasymtable;
  1134. symtablelevel:=level;
  1135. end;
  1136. function tparasymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  1137. begin
  1138. result:=inherited checkduplicate(hashedid,sym);
  1139. if result then
  1140. exit;
  1141. if not(m_duplicate_names in current_settings.modeswitches) and
  1142. (defowner.typ=procdef) and
  1143. assigned(tprocdef(defowner)._class) and
  1144. (tprocdef(defowner).owner.defowner=tprocdef(defowner)._class) and
  1145. (
  1146. not(m_delphi in current_settings.modeswitches) or
  1147. is_object(tprocdef(defowner)._class)
  1148. ) then
  1149. result:=tprocdef(defowner)._class.symtable.checkduplicate(hashedid,sym);
  1150. end;
  1151. {****************************************************************************
  1152. TAbstractUniTSymtable
  1153. ****************************************************************************}
  1154. constructor tabstractuniTSymtable.create(const n : string;id:word);
  1155. begin
  1156. inherited create(n);
  1157. moduleid:=id;
  1158. end;
  1159. function tabstractuniTSymtable.iscurrentunit:boolean;
  1160. begin
  1161. result:=assigned(current_module) and
  1162. (
  1163. (current_module.globalsymtable=self) or
  1164. (current_module.localsymtable=self)
  1165. );
  1166. end;
  1167. {****************************************************************************
  1168. TStaticSymtable
  1169. ****************************************************************************}
  1170. constructor tstaticsymtable.create(const n : string;id:word);
  1171. begin
  1172. inherited create(n,id);
  1173. symtabletype:=staticsymtable;
  1174. symtablelevel:=main_program_level;
  1175. end;
  1176. procedure tstaticsymtable.ppuload(ppufile:tcompilerppufile);
  1177. begin
  1178. inherited ppuload(ppufile);
  1179. { now we can deref the syms and defs }
  1180. deref;
  1181. end;
  1182. procedure tstaticsymtable.ppuwrite(ppufile:tcompilerppufile);
  1183. begin
  1184. inherited ppuwrite(ppufile);
  1185. end;
  1186. function tstaticsymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  1187. var
  1188. hsym : tsym;
  1189. begin
  1190. result:=false;
  1191. hsym:=tsym(FindWithHash(hashedid));
  1192. if assigned(hsym) then
  1193. begin
  1194. { Delphi (contrary to TP) you can have a symbol with the same name as the
  1195. unit, the unit can then not be accessed anymore using
  1196. <unit>.<id>, so we can hide the symbol }
  1197. if (m_delphi in current_settings.modeswitches) and
  1198. (hsym.typ=symconst.unitsym) then
  1199. HideSym(hsym)
  1200. else
  1201. DuplicateSym(hashedid,sym,hsym);
  1202. result:=true;
  1203. exit;
  1204. end;
  1205. if (current_module.localsymtable=self) and
  1206. assigned(current_module.globalsymtable) then
  1207. result:=tglobalsymtable(current_module.globalsymtable).checkduplicate(hashedid,sym);
  1208. end;
  1209. {****************************************************************************
  1210. TGlobalSymtable
  1211. ****************************************************************************}
  1212. constructor tglobalsymtable.create(const n : string;id:word);
  1213. begin
  1214. inherited create(n,id);
  1215. symtabletype:=globalsymtable;
  1216. symtablelevel:=main_program_level;
  1217. end;
  1218. procedure tglobalsymtable.ppuload(ppufile:tcompilerppufile);
  1219. begin
  1220. inherited ppuload(ppufile);
  1221. { now we can deref the syms and defs }
  1222. deref;
  1223. end;
  1224. procedure tglobalsymtable.ppuwrite(ppufile:tcompilerppufile);
  1225. begin
  1226. { write the symtable entries }
  1227. inherited ppuwrite(ppufile);
  1228. end;
  1229. function tglobalsymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  1230. var
  1231. hsym : tsym;
  1232. begin
  1233. result:=false;
  1234. hsym:=tsym(FindWithHash(hashedid));
  1235. if assigned(hsym) then
  1236. begin
  1237. { Delphi (contrary to TP) you can have a symbol with the same name as the
  1238. unit, the unit can then not be accessed anymore using
  1239. <unit>.<id>, so we can hide the symbol }
  1240. if (m_delphi in current_settings.modeswitches) and
  1241. (hsym.typ=symconst.unitsym) then
  1242. HideSym(hsym)
  1243. else
  1244. DuplicateSym(hashedid,sym,hsym);
  1245. result:=true;
  1246. exit;
  1247. end;
  1248. end;
  1249. {****************************************************************************
  1250. TWITHSYMTABLE
  1251. ****************************************************************************}
  1252. constructor twithsymtable.create(aowner:tdef;ASymList:TFPHashObjectList;refnode:tobject{tnode});
  1253. begin
  1254. inherited create('');
  1255. symtabletype:=withsymtable;
  1256. withrefnode:=refnode;
  1257. { Replace SymList with the passed symlist }
  1258. SymList.free;
  1259. SymList:=ASymList;
  1260. defowner:=aowner;
  1261. end;
  1262. destructor twithsymtable.destroy;
  1263. begin
  1264. withrefnode.free;
  1265. { Disable SymList because we don't Own it }
  1266. SymList:=nil;
  1267. inherited destroy;
  1268. end;
  1269. procedure twithsymtable.clear;
  1270. begin
  1271. { remove no entry from a withsymtable as it is only a pointer to the
  1272. recorddef or objectdef symtable }
  1273. end;
  1274. procedure twithsymtable.insertdef(def:TDefEntry);
  1275. begin
  1276. { Definitions can't be registered in the withsymtable
  1277. because the withsymtable is removed after the with block.
  1278. We can't easily solve it here because the next symtable in the
  1279. stack is not known. }
  1280. internalerror(200602046);
  1281. end;
  1282. {****************************************************************************
  1283. TSTT_ExceptionSymtable
  1284. ****************************************************************************}
  1285. constructor tstt_excepTSymtable.create;
  1286. begin
  1287. inherited create('');
  1288. symtabletype:=stt_excepTSymtable;
  1289. end;
  1290. {****************************************************************************
  1291. TMacroSymtable
  1292. ****************************************************************************}
  1293. constructor tmacrosymtable.create(exported: boolean);
  1294. begin
  1295. inherited create('');
  1296. if exported then
  1297. symtabletype:=exportedmacrosymtable
  1298. else
  1299. symtabletype:=localmacrosymtable;
  1300. symtablelevel:=main_program_level;
  1301. end;
  1302. {*****************************************************************************
  1303. Helper Routines
  1304. *****************************************************************************}
  1305. function FullTypeName(def,otherdef:tdef):string;
  1306. var
  1307. s1,s2 : string;
  1308. begin
  1309. s1:=def.typename;
  1310. { When the names are the same try to include the unit name }
  1311. if assigned(otherdef) and
  1312. (def.owner.symtabletype in [globalsymtable,staticsymtable]) then
  1313. begin
  1314. s2:=otherdef.typename;
  1315. if upper(s1)=upper(s2) then
  1316. s1:=def.owner.realname^+'.'+s1;
  1317. end;
  1318. FullTypeName:=s1;
  1319. end;
  1320. procedure incompatibletypes(def1,def2:tdef);
  1321. begin
  1322. { When there is an errordef there is already an error message show }
  1323. if (def2.typ=errordef) or
  1324. (def1.typ=errordef) then
  1325. exit;
  1326. CGMessage2(type_e_incompatible_types,FullTypeName(def1,def2),FullTypeName(def2,def1));
  1327. end;
  1328. procedure hidesym(sym:TSymEntry);
  1329. begin
  1330. sym.realname:='$hidden'+sym.realname;
  1331. tsym(sym).visibility:=vis_hidden;
  1332. end;
  1333. procedure duplicatesym(var hashedid:THashedIDString;dupsym,origsym:TSymEntry);
  1334. var
  1335. st : TSymtable;
  1336. begin
  1337. Message1(sym_e_duplicate_id,tsym(origsym).realname);
  1338. { Write hint where the original symbol was found }
  1339. st:=finduniTSymtable(origsym.owner);
  1340. with tsym(origsym).fileinfo do
  1341. begin
  1342. if assigned(st) and
  1343. (st.symtabletype=globalsymtable) and
  1344. st.iscurrentunit then
  1345. Message2(sym_h_duplicate_id_where,current_module.sourcefiles.get_file_name(fileindex),tostr(line))
  1346. else if assigned(st.name) then
  1347. Message2(sym_h_duplicate_id_where,'unit '+st.name^,tostr(line));
  1348. end;
  1349. { Rename duplicate sym to an unreachable name, but it can be
  1350. inserted in the symtable without errors }
  1351. inc(dupnr);
  1352. hashedid.id:='dup'+tostr(dupnr)+hashedid.id;
  1353. if assigned(dupsym) then
  1354. include(tsym(dupsym).symoptions,sp_implicitrename);
  1355. end;
  1356. {*****************************************************************************
  1357. Search
  1358. *****************************************************************************}
  1359. procedure addsymref(sym:tsym);
  1360. begin
  1361. { symbol uses count }
  1362. sym.IncRefCount;
  1363. { unit uses count }
  1364. if assigned(current_module) and
  1365. (sym.owner.symtabletype=globalsymtable) then
  1366. begin
  1367. if tglobalsymtable(sym.owner).moduleid>=current_module.unitmapsize then
  1368. internalerror(200501152);
  1369. inc(current_module.unitmap[tglobalsymtable(sym.owner).moduleid].refs);
  1370. end;
  1371. end;
  1372. function is_visible_for_object(symst:tsymtable;symvisibility:tvisibility;contextobjdef:tobjectdef):boolean;
  1373. var
  1374. symownerdef : tobjectdef;
  1375. begin
  1376. result:=false;
  1377. { Get objdectdef owner of the symtable for the is_related checks }
  1378. if not assigned(symst) or
  1379. (symst.symtabletype<>objectsymtable) then
  1380. internalerror(200810285);
  1381. symownerdef:=tobjectdef(symst.defowner);
  1382. case symvisibility of
  1383. vis_private :
  1384. begin
  1385. { private symbols are allowed when we are in the same
  1386. module as they are defined }
  1387. result:=(
  1388. (symownerdef.owner.symtabletype in [globalsymtable,staticsymtable]) and
  1389. (symownerdef.owner.iscurrentunit)
  1390. ) or
  1391. ( // the case of specialize inside the generic declaration
  1392. (symownerdef.owner.symtabletype = objectsymtable) and
  1393. (
  1394. assigned(current_objectdef) and
  1395. (
  1396. (current_objectdef=symownerdef) or
  1397. (current_objectdef.owner.moduleid=symownerdef.owner.moduleid)
  1398. )
  1399. ) or
  1400. (
  1401. not assigned(current_objectdef) and
  1402. (symownerdef.owner.moduleid=current_module.moduleid)
  1403. )
  1404. );
  1405. end;
  1406. vis_strictprivate :
  1407. begin
  1408. result:=assigned(current_objectdef) and
  1409. (current_objectdef=symownerdef);
  1410. end;
  1411. vis_strictprotected :
  1412. begin
  1413. result:=assigned(current_objectdef) and
  1414. current_objectdef.is_related(symownerdef);
  1415. end;
  1416. vis_protected :
  1417. begin
  1418. { protected symbols are visible in the module that defines them and
  1419. also visible to related objects. The related object must be defined
  1420. in the current module }
  1421. result:=(
  1422. (
  1423. (symownerdef.owner.symtabletype in [globalsymtable,staticsymtable]) and
  1424. (symownerdef.owner.iscurrentunit)
  1425. ) or
  1426. (
  1427. assigned(contextobjdef) and
  1428. (contextobjdef.owner.symtabletype in [globalsymtable,staticsymtable]) and
  1429. (contextobjdef.owner.iscurrentunit) and
  1430. contextobjdef.is_related(symownerdef)
  1431. ) or
  1432. ( // the case of specialize inside the generic declaration
  1433. (symownerdef.owner.symtabletype = objectsymtable) and
  1434. (
  1435. assigned(current_objectdef) and
  1436. (
  1437. (current_objectdef=symownerdef) or
  1438. (current_objectdef.owner.moduleid=symownerdef.owner.moduleid)
  1439. )
  1440. ) or
  1441. (
  1442. not assigned(current_objectdef) and
  1443. (symownerdef.owner.moduleid=current_module.moduleid)
  1444. )
  1445. )
  1446. );
  1447. end;
  1448. vis_public,
  1449. vis_published :
  1450. result:=true;
  1451. end;
  1452. end;
  1453. function is_visible_for_object(pd:tprocdef;contextobjdef:tobjectdef):boolean;
  1454. begin
  1455. result:=is_visible_for_object(pd.owner,pd.visibility,contextobjdef);
  1456. end;
  1457. function is_visible_for_object(sym:tsym;contextobjdef:tobjectdef):boolean;
  1458. var
  1459. i : longint;
  1460. pd : tprocdef;
  1461. begin
  1462. if sym.typ=procsym then
  1463. begin
  1464. { A procsym is visible, when there is at least one of the procdefs visible }
  1465. result:=false;
  1466. for i:=0 to tprocsym(sym).ProcdefList.Count-1 do
  1467. begin
  1468. pd:=tprocdef(tprocsym(sym).ProcdefList[i]);
  1469. if (pd.owner=sym.owner) and
  1470. is_visible_for_object(pd,contextobjdef) then
  1471. begin
  1472. result:=true;
  1473. exit;
  1474. end;
  1475. end;
  1476. end
  1477. else
  1478. result:=is_visible_for_object(sym.owner,sym.visibility,contextobjdef);
  1479. end;
  1480. function searchsym(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  1481. var
  1482. hashedid : THashedIDString;
  1483. contextobjdef : tobjectdef;
  1484. stackitem : psymtablestackitem;
  1485. begin
  1486. result:=false;
  1487. hashedid.id:=s;
  1488. stackitem:=symtablestack.stack;
  1489. while assigned(stackitem) do
  1490. begin
  1491. srsymtable:=stackitem^.symtable;
  1492. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  1493. if assigned(srsym) then
  1494. begin
  1495. { use the class from withsymtable only when it is
  1496. defined in this unit }
  1497. if (srsymtable.symtabletype=withsymtable) and
  1498. assigned(srsymtable.defowner) and
  1499. (srsymtable.defowner.typ=objectdef) and
  1500. (srsymtable.defowner.owner.symtabletype in [globalsymtable,staticsymtable]) and
  1501. (srsymtable.defowner.owner.iscurrentunit) then
  1502. contextobjdef:=tobjectdef(srsymtable.defowner)
  1503. else
  1504. contextobjdef:=current_objectdef;
  1505. if (srsym.owner.symtabletype<>objectsymtable) or
  1506. is_visible_for_object(srsym,contextobjdef) then
  1507. begin
  1508. { we need to know if a procedure references symbols
  1509. in the static symtable, because then it can't be
  1510. inlined from outside this unit }
  1511. if assigned(current_procinfo) and
  1512. (srsym.owner.symtabletype=staticsymtable) then
  1513. include(current_procinfo.flags,pi_uses_static_symtable);
  1514. addsymref(srsym);
  1515. result:=true;
  1516. exit;
  1517. end;
  1518. end;
  1519. { also search for class helpers }
  1520. if (srsymtable.symtabletype=objectsymtable) and
  1521. is_objcclass(tdef(srsymtable.defowner)) then
  1522. begin
  1523. if search_class_helper(tobjectdef(srsymtable.defowner),s,srsym,srsymtable) then
  1524. begin
  1525. result:=true;
  1526. exit;
  1527. end;
  1528. end;
  1529. stackitem:=stackitem^.next;
  1530. end;
  1531. srsym:=nil;
  1532. srsymtable:=nil;
  1533. end;
  1534. function searchsym_type(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  1535. var
  1536. hashedid : THashedIDString;
  1537. stackitem : psymtablestackitem;
  1538. begin
  1539. result:=false;
  1540. hashedid.id:=s;
  1541. stackitem:=symtablestack.stack;
  1542. while assigned(stackitem) do
  1543. begin
  1544. {
  1545. It is not possible to have type symbols in:
  1546. records
  1547. objects
  1548. parameters
  1549. Exception are classes, generic definitions and specializations
  1550. that have the parameterized types inserted in the symtable.
  1551. }
  1552. srsymtable:=stackitem^.symtable;
  1553. if not(srsymtable.symtabletype in [recordsymtable,ObjectSymtable,parasymtable]) or
  1554. (assigned(srsymtable.defowner) and
  1555. (
  1556. (df_generic in tdef(srsymtable.defowner).defoptions) or
  1557. (df_specialization in tdef(srsymtable.defowner).defoptions) or
  1558. is_class(tdef(srsymtable.defowner)))
  1559. ) then
  1560. begin
  1561. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  1562. if assigned(srsym) and
  1563. not(srsym.typ in [fieldvarsym,paravarsym]) and
  1564. (
  1565. (srsym.owner.symtabletype<>objectsymtable) or
  1566. (is_visible_for_object(srsym,current_objectdef) and
  1567. (srsym.typ=typesym))
  1568. ) then
  1569. begin
  1570. { we need to know if a procedure references symbols
  1571. in the static symtable, because then it can't be
  1572. inlined from outside this unit }
  1573. if assigned(current_procinfo) and
  1574. (srsym.owner.symtabletype=staticsymtable) then
  1575. include(current_procinfo.flags,pi_uses_static_symtable);
  1576. addsymref(srsym);
  1577. result:=true;
  1578. exit;
  1579. end;
  1580. end;
  1581. stackitem:=stackitem^.next;
  1582. end;
  1583. result:=false;
  1584. srsym:=nil;
  1585. srsymtable:=nil;
  1586. end;
  1587. function searchsym_in_module(pm:pointer;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  1588. var
  1589. pmod : tmodule;
  1590. begin
  1591. pmod:=tmodule(pm);
  1592. result:=false;
  1593. if assigned(pmod.globalsymtable) then
  1594. begin
  1595. srsym:=tsym(pmod.globalsymtable.Find(s));
  1596. if assigned(srsym) then
  1597. begin
  1598. srsymtable:=pmod.globalsymtable;
  1599. addsymref(srsym);
  1600. result:=true;
  1601. exit;
  1602. end;
  1603. end;
  1604. { If the module is the current unit we also need
  1605. to search the local symtable }
  1606. if (pmod=current_module) and
  1607. assigned(pmod.localsymtable) then
  1608. begin
  1609. srsym:=tsym(pmod.localsymtable.Find(s));
  1610. if assigned(srsym) then
  1611. begin
  1612. srsymtable:=pmod.localsymtable;
  1613. addsymref(srsym);
  1614. result:=true;
  1615. exit;
  1616. end;
  1617. end;
  1618. srsym:=nil;
  1619. srsymtable:=nil;
  1620. end;
  1621. function searchsym_in_named_module(const unitname, symname: TIDString; out srsym: tsym; out srsymtable: tsymtable): boolean;
  1622. var
  1623. stackitem : psymtablestackitem;
  1624. begin
  1625. result:=false;
  1626. stackitem:=symtablestack.stack;
  1627. while assigned(stackitem) do
  1628. begin
  1629. srsymtable:=stackitem^.symtable;
  1630. if (srsymtable.symtabletype=globalsymtable) and
  1631. (srsymtable.name^=unitname) then
  1632. begin
  1633. srsym:=tsym(srsymtable.find(symname));
  1634. if not assigned(srsym) then
  1635. break;
  1636. result:=true;
  1637. exit;
  1638. end;
  1639. stackitem:=stackitem^.next;
  1640. end;
  1641. { If the module is the current unit we also need
  1642. to search the local symtable }
  1643. if (current_module.globalsymtable=srsymtable) and
  1644. assigned(current_module.localsymtable) then
  1645. begin
  1646. srsymtable:=current_module.localsymtable;
  1647. srsym:=tsym(srsymtable.find(symname));
  1648. if assigned(srsym) then
  1649. begin
  1650. result:=true;
  1651. exit;
  1652. end;
  1653. end;
  1654. end;
  1655. function searchsym_in_class(classh,contextclassh:tobjectdef;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  1656. var
  1657. hashedid : THashedIDString;
  1658. orgclass : tobjectdef;
  1659. begin
  1660. orgclass:=classh;
  1661. { The contextclassh is used for visibility. The classh must be equal to
  1662. or be a parent of contextclassh. E.g. for inherited searches the classh is the
  1663. parent. }
  1664. if assigned(classh) and
  1665. not contextclassh.is_related(classh) then
  1666. internalerror(200811161);
  1667. result:=false;
  1668. hashedid.id:=s;
  1669. while assigned(classh) do
  1670. begin
  1671. srsymtable:=classh.symtable;
  1672. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  1673. if assigned(srsym) and
  1674. is_visible_for_object(srsym,contextclassh) then
  1675. begin
  1676. addsymref(srsym);
  1677. result:=true;
  1678. exit;
  1679. end;
  1680. classh:=classh.childof;
  1681. end;
  1682. if is_objcclass(orgclass) then
  1683. result:=search_class_helper(orgclass,s,srsym,srsymtable)
  1684. else
  1685. begin
  1686. srsym:=nil;
  1687. srsymtable:=nil;
  1688. end;
  1689. end;
  1690. function searchsym_in_class_by_msgint(classh:tobjectdef;msgid:longint;out srdef : tdef;out srsym:tsym;out srsymtable:TSymtable):boolean;
  1691. var
  1692. def : tdef;
  1693. i : longint;
  1694. begin
  1695. result:=false;
  1696. def:=nil;
  1697. while assigned(classh) do
  1698. begin
  1699. for i:=0 to classh.symtable.DefList.Count-1 do
  1700. begin
  1701. def:=tstoreddef(classh.symtable.DefList[i]);
  1702. { Find also all hidden private methods to
  1703. be compatible with delphi, see tw6203 (PFV) }
  1704. if (def.typ=procdef) and
  1705. (po_msgint in tprocdef(def).procoptions) and
  1706. (tprocdef(def).messageinf.i=msgid) then
  1707. begin
  1708. srdef:=def;
  1709. srsym:=tprocdef(def).procsym;
  1710. srsymtable:=classh.symtable;
  1711. addsymref(srsym);
  1712. result:=true;
  1713. exit;
  1714. end;
  1715. end;
  1716. classh:=classh.childof;
  1717. end;
  1718. srdef:=nil;
  1719. srsym:=nil;
  1720. srsymtable:=nil;
  1721. end;
  1722. function searchsym_in_class_by_msgstr(classh:tobjectdef;const s:string;out srsym:tsym;out srsymtable:TSymtable):boolean;
  1723. var
  1724. def : tdef;
  1725. i : longint;
  1726. begin
  1727. result:=false;
  1728. def:=nil;
  1729. while assigned(classh) do
  1730. begin
  1731. for i:=0 to classh.symtable.DefList.Count-1 do
  1732. begin
  1733. def:=tstoreddef(classh.symtable.DefList[i]);
  1734. { Find also all hidden private methods to
  1735. be compatible with delphi, see tw6203 (PFV) }
  1736. if (def.typ=procdef) and
  1737. (po_msgstr in tprocdef(def).procoptions) and
  1738. (tprocdef(def).messageinf.str^=s) then
  1739. begin
  1740. srsym:=tprocdef(def).procsym;
  1741. srsymtable:=classh.symtable;
  1742. addsymref(srsym);
  1743. result:=true;
  1744. exit;
  1745. end;
  1746. end;
  1747. classh:=classh.childof;
  1748. end;
  1749. srsym:=nil;
  1750. srsymtable:=nil;
  1751. end;
  1752. function search_assignment_operator(from_def,to_def:Tdef):Tprocdef;
  1753. var
  1754. sym : Tprocsym;
  1755. hashedid : THashedIDString;
  1756. curreq,
  1757. besteq : tequaltype;
  1758. currpd,
  1759. bestpd : tprocdef;
  1760. stackitem : psymtablestackitem;
  1761. begin
  1762. hashedid.id:='assign';
  1763. besteq:=te_incompatible;
  1764. bestpd:=nil;
  1765. stackitem:=symtablestack.stack;
  1766. while assigned(stackitem) do
  1767. begin
  1768. sym:=Tprocsym(stackitem^.symtable.FindWithHash(hashedid));
  1769. if sym<>nil then
  1770. begin
  1771. if sym.typ<>procsym then
  1772. internalerror(200402031);
  1773. { if the source type is an alias then this is only the second choice,
  1774. if you mess with this code, check tw4093 }
  1775. currpd:=sym.find_procdef_assignment_operator(from_def,to_def,curreq);
  1776. if curreq>besteq then
  1777. begin
  1778. besteq:=curreq;
  1779. bestpd:=currpd;
  1780. if (besteq=te_exact) then
  1781. break;
  1782. end;
  1783. end;
  1784. stackitem:=stackitem^.next;
  1785. end;
  1786. result:=bestpd;
  1787. end;
  1788. function search_enumerator_operator(type_def:Tdef): Tprocdef;
  1789. var
  1790. sym : Tprocsym;
  1791. hashedid : THashedIDString;
  1792. curreq,
  1793. besteq : tequaltype;
  1794. currpd,
  1795. bestpd : tprocdef;
  1796. stackitem : psymtablestackitem;
  1797. begin
  1798. hashedid.id:='enumerator';
  1799. besteq:=te_incompatible;
  1800. bestpd:=nil;
  1801. stackitem:=symtablestack.stack;
  1802. while assigned(stackitem) do
  1803. begin
  1804. sym:=Tprocsym(stackitem^.symtable.FindWithHash(hashedid));
  1805. if sym<>nil then
  1806. begin
  1807. if sym.typ<>procsym then
  1808. internalerror(200910241);
  1809. { if the source type is an alias then this is only the second choice,
  1810. if you mess with this code, check tw4093 }
  1811. currpd:=sym.find_procdef_enumerator_operator(type_def,curreq);
  1812. if curreq>besteq then
  1813. begin
  1814. besteq:=curreq;
  1815. bestpd:=currpd;
  1816. if (besteq=te_exact) then
  1817. break;
  1818. end;
  1819. end;
  1820. stackitem:=stackitem^.next;
  1821. end;
  1822. result:=bestpd;
  1823. end;
  1824. function search_system_type(const s: TIDString): ttypesym;
  1825. var
  1826. sym : tsym;
  1827. begin
  1828. sym:=tsym(systemunit.Find(s));
  1829. if not assigned(sym) or
  1830. (sym.typ<>typesym) then
  1831. cgmessage1(cg_f_unknown_system_type,s);
  1832. result:=ttypesym(sym);
  1833. end;
  1834. function search_named_unit_globaltype(const unitname, typename: TIDString): ttypesym;
  1835. var
  1836. srsymtable: tsymtable;
  1837. sym: tsym;
  1838. begin
  1839. if searchsym_in_named_module(unitname,typename,sym,srsymtable) and
  1840. (sym.typ=typesym) then
  1841. begin
  1842. result:=ttypesym(sym);
  1843. exit;
  1844. end
  1845. else
  1846. begin
  1847. cgmessage2(cg_f_unknown_type_in_unit,typename,unitname);
  1848. result:=nil;
  1849. end;
  1850. end;
  1851. function search_class_helper(pd : tobjectdef;const s : string; out srsym: tsym; out srsymtable: tsymtable):boolean;
  1852. var
  1853. hashedid : THashedIDString;
  1854. stackitem : psymtablestackitem;
  1855. i : longint;
  1856. defowner : tobjectdef;
  1857. begin
  1858. hashedid.id:=class_helper_prefix+s;
  1859. stackitem:=symtablestack.stack;
  1860. while assigned(stackitem) do
  1861. begin
  1862. srsymtable:=stackitem^.symtable;
  1863. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  1864. if assigned(srsym) then
  1865. begin
  1866. if not(srsymtable.symtabletype in [globalsymtable,staticsymtable]) or
  1867. not(srsym.owner.symtabletype in [globalsymtable,staticsymtable]) or
  1868. (srsym.typ<>procsym) then
  1869. internalerror(2009111505);
  1870. { check whether this procsym includes a helper for this particular class }
  1871. for i:=0 to tprocsym(srsym).procdeflist.count-1 do
  1872. begin
  1873. { does pd inherit from (or is the same as) the class
  1874. that this method's category extended?
  1875. Warning: this list contains both category and objcclass methods
  1876. (for id.randommethod), so only check category methods here
  1877. }
  1878. defowner:=tobjectdef(tprocdef(tprocsym(srsym).procdeflist[i]).owner.defowner);
  1879. if (oo_is_classhelper in defowner.objectoptions) and
  1880. pd.is_related(defowner.childof) then
  1881. begin
  1882. { we need to know if a procedure references symbols
  1883. in the static symtable, because then it can't be
  1884. inlined from outside this unit }
  1885. if assigned(current_procinfo) and
  1886. (srsym.owner.symtabletype=staticsymtable) then
  1887. include(current_procinfo.flags,pi_uses_static_symtable);
  1888. { no need to keep looking. There might be other
  1889. categories that extend this, a parent or child
  1890. class with a method with the same name (either
  1891. overriding this one, or overridden by this one),
  1892. but that doesn't matter as far as the basic
  1893. procsym is concerned.
  1894. }
  1895. srsym:=tprocdef(tprocsym(srsym).procdeflist[i]).procsym;
  1896. srsymtable:=srsym.owner;
  1897. addsymref(srsym);
  1898. result:=true;
  1899. exit;
  1900. end;
  1901. end;
  1902. end;
  1903. stackitem:=stackitem^.next;
  1904. end;
  1905. srsym:=nil;
  1906. srsymtable:=nil;
  1907. result:=false;
  1908. end;
  1909. function search_objc_method(const s : string; out srsym: tsym; out srsymtable: tsymtable):boolean;
  1910. var
  1911. hashedid : THashedIDString;
  1912. stackitem : psymtablestackitem;
  1913. i : longint;
  1914. begin
  1915. hashedid.id:=class_helper_prefix+s;
  1916. stackitem:=symtablestack.stack;
  1917. while assigned(stackitem) do
  1918. begin
  1919. srsymtable:=stackitem^.symtable;
  1920. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  1921. if assigned(srsym) then
  1922. begin
  1923. if not(srsymtable.symtabletype in [globalsymtable,staticsymtable]) or
  1924. not(srsym.owner.symtabletype in [globalsymtable,staticsymtable]) or
  1925. (srsym.typ<>procsym) then
  1926. internalerror(2009112005);
  1927. { check whether this procsym includes a helper for this particular class }
  1928. for i:=0 to tprocsym(srsym).procdeflist.count-1 do
  1929. begin
  1930. { we need to know if a procedure references symbols
  1931. in the static symtable, because then it can't be
  1932. inlined from outside this unit }
  1933. if assigned(current_procinfo) and
  1934. (srsym.owner.symtabletype=staticsymtable) then
  1935. include(current_procinfo.flags,pi_uses_static_symtable);
  1936. { no need to keep looking. There might be other
  1937. methods with the same name, but that doesn't matter
  1938. as far as the basic procsym is concerned.
  1939. }
  1940. srsym:=tprocdef(tprocsym(srsym).procdeflist[i]).procsym;
  1941. { We need the symtable in which the classhelper-like sym
  1942. is located, not the objectdef. The reason is that the
  1943. callnode will climb the symtablestack until it encounters
  1944. this symtable to start looking for overloads (and it won't
  1945. find the objectsymtable in which this method sym is
  1946. located
  1947. srsymtable:=srsym.owner;
  1948. }
  1949. addsymref(srsym);
  1950. result:=true;
  1951. exit;
  1952. end;
  1953. end;
  1954. stackitem:=stackitem^.next;
  1955. end;
  1956. srsym:=nil;
  1957. srsymtable:=nil;
  1958. result:=false;
  1959. end;
  1960. function search_class_member(pd : tobjectdef;const s : string):tsym;
  1961. { searches n in symtable of pd and all anchestors }
  1962. var
  1963. hashedid : THashedIDString;
  1964. srsym : tsym;
  1965. orgpd : tobjectdef;
  1966. srsymtable : tsymtable;
  1967. begin
  1968. hashedid.id:=s;
  1969. orgpd:=pd;
  1970. while assigned(pd) do
  1971. begin
  1972. srsym:=tsym(pd.symtable.FindWithHash(hashedid));
  1973. if assigned(srsym) then
  1974. begin
  1975. search_class_member:=srsym;
  1976. exit;
  1977. end;
  1978. pd:=pd.childof;
  1979. end;
  1980. { not found, now look for class helpers }
  1981. if is_objcclass(pd) then
  1982. search_class_helper(orgpd,s,result,srsymtable)
  1983. else
  1984. result:=nil;
  1985. end;
  1986. function search_macro(const s : string):tsym;
  1987. var
  1988. stackitem : psymtablestackitem;
  1989. hashedid : THashedIDString;
  1990. srsym : tsym;
  1991. begin
  1992. hashedid.id:=s;
  1993. { First search the localmacrosymtable before searching the
  1994. global macrosymtables from the units }
  1995. if assigned(current_module) then
  1996. begin
  1997. srsym:=tsym(current_module.localmacrosymtable.FindWithHash(hashedid));
  1998. if assigned(srsym) then
  1999. begin
  2000. result:= srsym;
  2001. exit;
  2002. end;
  2003. end;
  2004. stackitem:=macrosymtablestack.stack;
  2005. while assigned(stackitem) do
  2006. begin
  2007. srsym:=tsym(stackitem^.symtable.FindWithHash(hashedid));
  2008. if assigned(srsym) then
  2009. begin
  2010. result:= srsym;
  2011. exit;
  2012. end;
  2013. stackitem:=stackitem^.next;
  2014. end;
  2015. result:= nil;
  2016. end;
  2017. function defined_macro(const s : string):boolean;
  2018. var
  2019. mac: tmacro;
  2020. begin
  2021. mac:=tmacro(search_macro(s));
  2022. if assigned(mac) then
  2023. begin
  2024. mac.is_used:=true;
  2025. defined_macro:=mac.defined;
  2026. end
  2027. else
  2028. defined_macro:=false;
  2029. end;
  2030. {****************************************************************************
  2031. Object Helpers
  2032. ****************************************************************************}
  2033. function search_default_property(pd : tobjectdef) : tpropertysym;
  2034. { returns the default property of a class, searches also anchestors }
  2035. var
  2036. _defaultprop : tpropertysym;
  2037. begin
  2038. _defaultprop:=nil;
  2039. while assigned(pd) do
  2040. begin
  2041. pd.symtable.SymList.ForEachCall(@tstoredsymtable(pd.symtable).testfordefaultproperty,@_defaultprop);
  2042. if assigned(_defaultprop) then
  2043. break;
  2044. pd:=pd.childof;
  2045. end;
  2046. search_default_property:=_defaultprop;
  2047. end;
  2048. {****************************************************************************
  2049. Macro Helpers
  2050. ****************************************************************************}
  2051. procedure def_system_macro(const name : string);
  2052. var
  2053. mac : tmacro;
  2054. s: string;
  2055. begin
  2056. if name = '' then
  2057. internalerror(2004121202);
  2058. s:= upper(name);
  2059. mac:=tmacro(search_macro(s));
  2060. if not assigned(mac) then
  2061. begin
  2062. mac:=tmacro.create(s);
  2063. if assigned(current_module) then
  2064. current_module.localmacrosymtable.insert(mac)
  2065. else
  2066. initialmacrosymtable.insert(mac);
  2067. end;
  2068. if not mac.defined then
  2069. Message1(parser_c_macro_defined,mac.name);
  2070. mac.defined:=true;
  2071. end;
  2072. procedure set_system_macro(const name, value : string);
  2073. var
  2074. mac : tmacro;
  2075. s: string;
  2076. begin
  2077. if name = '' then
  2078. internalerror(2004121203);
  2079. s:= upper(name);
  2080. mac:=tmacro(search_macro(s));
  2081. if not assigned(mac) then
  2082. begin
  2083. mac:=tmacro.create(s);
  2084. if assigned(current_module) then
  2085. current_module.localmacrosymtable.insert(mac)
  2086. else
  2087. initialmacrosymtable.insert(mac);
  2088. end
  2089. else
  2090. begin
  2091. mac.is_compiler_var:=false;
  2092. if assigned(mac.buftext) then
  2093. freemem(mac.buftext,mac.buflen);
  2094. end;
  2095. Message2(parser_c_macro_set_to,mac.name,value);
  2096. mac.buflen:=length(value);
  2097. getmem(mac.buftext,mac.buflen);
  2098. move(value[1],mac.buftext^,mac.buflen);
  2099. mac.defined:=true;
  2100. end;
  2101. procedure set_system_compvar(const name, value : string);
  2102. var
  2103. mac : tmacro;
  2104. s: string;
  2105. begin
  2106. if name = '' then
  2107. internalerror(2004121204);
  2108. s:= upper(name);
  2109. mac:=tmacro(search_macro(s));
  2110. if not assigned(mac) then
  2111. begin
  2112. mac:=tmacro.create(s);
  2113. mac.is_compiler_var:=true;
  2114. if assigned(current_module) then
  2115. current_module.localmacrosymtable.insert(mac)
  2116. else
  2117. initialmacrosymtable.insert(mac);
  2118. end
  2119. else
  2120. begin
  2121. mac.is_compiler_var:=true;
  2122. if assigned(mac.buftext) then
  2123. freemem(mac.buftext,mac.buflen);
  2124. end;
  2125. Message2(parser_c_macro_set_to,mac.name,value);
  2126. mac.buflen:=length(value);
  2127. getmem(mac.buftext,mac.buflen);
  2128. move(value[1],mac.buftext^,mac.buflen);
  2129. mac.defined:=true;
  2130. end;
  2131. procedure undef_system_macro(const name : string);
  2132. var
  2133. mac : tmacro;
  2134. s: string;
  2135. begin
  2136. if name = '' then
  2137. internalerror(2004121205);
  2138. s:= upper(name);
  2139. mac:=tmacro(search_macro(s));
  2140. if not assigned(mac) then
  2141. {If not found, then it's already undefined.}
  2142. else
  2143. begin
  2144. if mac.defined then
  2145. Message1(parser_c_macro_undefined,mac.name);
  2146. mac.defined:=false;
  2147. mac.is_compiler_var:=false;
  2148. { delete old definition }
  2149. if assigned(mac.buftext) then
  2150. begin
  2151. freemem(mac.buftext,mac.buflen);
  2152. mac.buftext:=nil;
  2153. end;
  2154. end;
  2155. end;
  2156. {$ifdef UNITALIASES}
  2157. {****************************************************************************
  2158. TUNIT_ALIAS
  2159. ****************************************************************************}
  2160. constructor tunit_alias.create(const n:string);
  2161. var
  2162. i : longint;
  2163. begin
  2164. i:=pos('=',n);
  2165. if i=0 then
  2166. fail;
  2167. inherited createname(Copy(n,1,i-1));
  2168. newname:=stringdup(Copy(n,i+1,255));
  2169. end;
  2170. destructor tunit_alias.destroy;
  2171. begin
  2172. stringdispose(newname);
  2173. inherited destroy;
  2174. end;
  2175. procedure addunitalias(const n:string);
  2176. begin
  2177. unitaliases^.insert(tunit_alias,init(Upper(n))));
  2178. end;
  2179. function getunitalias(const n:string):string;
  2180. var
  2181. p : punit_alias;
  2182. begin
  2183. p:=punit_alias(unitaliases^.Find(Upper(n)));
  2184. if assigned(p) then
  2185. getunitalias:=punit_alias(p).newname^
  2186. else
  2187. getunitalias:=n;
  2188. end;
  2189. {$endif UNITALIASES}
  2190. {****************************************************************************
  2191. Init/Done Symtable
  2192. ****************************************************************************}
  2193. procedure InitSymtable;
  2194. begin
  2195. { Reset symbolstack }
  2196. symtablestack:=nil;
  2197. systemunit:=nil;
  2198. { create error syms and def }
  2199. generrorsym:=terrorsym.create;
  2200. generrordef:=terrordef.create;
  2201. { macros }
  2202. initialmacrosymtable:=tmacrosymtable.create(false);
  2203. macrosymtablestack:=TSymtablestack.create;
  2204. macrosymtablestack.push(initialmacrosymtable);
  2205. {$ifdef UNITALIASES}
  2206. { unit aliases }
  2207. unitaliases:=TFPHashObjectList.create;
  2208. {$endif}
  2209. { set some global vars to nil, might be important for the ide }
  2210. class_tobject:=nil;
  2211. interface_iunknown:=nil;
  2212. rec_tguid:=nil;
  2213. dupnr:=0;
  2214. end;
  2215. procedure DoneSymtable;
  2216. begin
  2217. generrorsym.owner:=nil;
  2218. generrorsym.free;
  2219. generrordef.owner:=nil;
  2220. generrordef.free;
  2221. initialmacrosymtable.free;
  2222. macrosymtablestack.free;
  2223. {$ifdef UNITALIASES}
  2224. unitaliases.free;
  2225. {$endif}
  2226. end;
  2227. end.