symtable.pas 88 KB

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