symtable.pas 88 KB

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