symtable.pas 88 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517
  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. { add to this record symtable }
  924. // unionst.SymList.List.List^[i].Data:=nil;
  925. sym.ChangeOwner(self);
  926. varalign:=tfieldvarsym(sym).vardef.alignment;
  927. if varalign=0 then
  928. varalign:=size_2_align(tfieldvarsym(sym).getsize);
  929. { retrieve size }
  930. if (usefieldalignment=bit_alignment) then
  931. begin
  932. { bit packed records are limited to high(aint) bits }
  933. { instead of bytes to avoid double precision }
  934. { arithmetic in offset calculations }
  935. if is_ordinal(tfieldvarsym(sym).vardef) then
  936. bitsize:=tfieldvarsym(sym).getpackedbitsize
  937. else
  938. begin
  939. bitsize:=tfieldvarsym(sym).getsize;
  940. if (bitsize>high(aint) div 8) then
  941. Message(sym_e_segment_too_large);
  942. bitsize:=bitsize*8;
  943. end;
  944. if bitsize>high(aint)-databitsize then
  945. begin
  946. Message(sym_e_segment_too_large);
  947. _datasize:=high(aint);
  948. databitsize:=high(aint);
  949. end
  950. else
  951. begin
  952. databitsize:=tfieldvarsym(sym).fieldoffset+offset*8;
  953. _datasize:=(databitsize+7) div 8;
  954. end;
  955. tfieldvarsym(sym).fieldoffset:=databitsize;
  956. varalignrecord:=field2recordalignment(tfieldvarsym(sym).fieldoffset div 8,varalign);
  957. end
  958. else
  959. begin
  960. if tfieldvarsym(sym).getsize>high(aint)-_datasize then
  961. begin
  962. Message(sym_e_segment_too_large);
  963. _datasize:=high(aint);
  964. end
  965. else
  966. _datasize:=tfieldvarsym(sym).fieldoffset+offset;
  967. { update address }
  968. tfieldvarsym(sym).fieldoffset:=_datasize;
  969. varalignrecord:=field2recordalignment(tfieldvarsym(sym).fieldoffset,varalign);
  970. end;
  971. { update alignment of this record }
  972. if (usefieldalignment<>C_alignment) and
  973. (usefieldalignment<>mac68k_alignment) then
  974. recordalignment:=max(recordalignment,varalignrecord);
  975. end;
  976. { update alignment for C records }
  977. if (usefieldalignment=C_alignment) and
  978. (usefieldalignment<>mac68k_alignment) then
  979. recordalignment:=max(recordalignment,unionst.recordalignment);
  980. { Register defs in the new record symtable }
  981. for i:=0 to unionst.DefList.Count-1 do
  982. begin
  983. def:=TDef(unionst.DefList[i]);
  984. def.ChangeOwner(self);
  985. end;
  986. _datasize:=storesize;
  987. fieldalignment:=storealign;
  988. end;
  989. {****************************************************************************
  990. TObjectSymtable
  991. ****************************************************************************}
  992. constructor tObjectSymtable.create(adefowner:tdef;const n:string;usealign:shortint);
  993. begin
  994. inherited create(n,usealign);
  995. symtabletype:=ObjectSymtable;
  996. defowner:=adefowner;
  997. end;
  998. function tObjectSymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  999. var
  1000. hsym : tsym;
  1001. begin
  1002. result:=false;
  1003. if not assigned(defowner) then
  1004. internalerror(200602061);
  1005. { procsym and propertysym have special code
  1006. to override values in inherited classes. For other
  1007. symbols check for duplicates }
  1008. if not(sym.typ in [procsym,propertysym]) then
  1009. begin
  1010. { but private ids can be reused }
  1011. hsym:=search_class_member(tobjectdef(defowner),hashedid.id);
  1012. if assigned(hsym) and
  1013. (
  1014. (
  1015. not(m_delphi in current_settings.modeswitches) and
  1016. is_visible_for_object(hsym,tobjectdef(defowner))
  1017. ) or
  1018. (
  1019. { In Delphi, you can repeat members of a parent class. You can't }
  1020. { do this for objects however, and you (obviouly) can't }
  1021. { declare two fields with the same name in a single class }
  1022. (m_delphi in current_settings.modeswitches) and
  1023. (
  1024. is_object(tdef(defowner)) or
  1025. (hsym.owner = self)
  1026. )
  1027. )
  1028. ) then
  1029. begin
  1030. DuplicateSym(hashedid,sym,hsym);
  1031. result:=true;
  1032. end;
  1033. end
  1034. else
  1035. begin
  1036. if not(m_duplicate_names in current_settings.modeswitches) then
  1037. result:=inherited checkduplicate(hashedid,sym);
  1038. end;
  1039. end;
  1040. {****************************************************************************
  1041. TAbstractLocalSymtable
  1042. ****************************************************************************}
  1043. procedure tabstractlocalsymtable.ppuwrite(ppufile:tcompilerppufile);
  1044. var
  1045. oldtyp : byte;
  1046. begin
  1047. oldtyp:=ppufile.entrytyp;
  1048. ppufile.entrytyp:=subentryid;
  1049. { write definitions }
  1050. writedefs(ppufile);
  1051. { write symbols }
  1052. writesyms(ppufile);
  1053. ppufile.entrytyp:=oldtyp;
  1054. end;
  1055. function tabstractlocalsymtable.count_locals:longint;
  1056. var
  1057. i : longint;
  1058. sym : tsym;
  1059. begin
  1060. result:=0;
  1061. for i:=0 to SymList.Count-1 do
  1062. begin
  1063. sym:=tsym(SymList[i]);
  1064. { Count only varsyms, but ignore the funcretsym }
  1065. if (tsym(sym).typ in [localvarsym,paravarsym]) and
  1066. (tsym(sym)<>current_procinfo.procdef.funcretsym) and
  1067. (not(vo_is_parentfp in tabstractvarsym(sym).varoptions) or
  1068. (tstoredsym(sym).refs>0)) then
  1069. inc(result);
  1070. end;
  1071. end;
  1072. {****************************************************************************
  1073. TLocalSymtable
  1074. ****************************************************************************}
  1075. constructor tlocalsymtable.create(adefowner:tdef;level:byte);
  1076. begin
  1077. inherited create('');
  1078. defowner:=adefowner;
  1079. symtabletype:=localsymtable;
  1080. symtablelevel:=level;
  1081. end;
  1082. function tlocalsymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  1083. var
  1084. hsym : tsym;
  1085. begin
  1086. if not assigned(defowner) or
  1087. (defowner.typ<>procdef) then
  1088. internalerror(200602042);
  1089. result:=false;
  1090. hsym:=tsym(FindWithHash(hashedid));
  1091. if assigned(hsym) then
  1092. begin
  1093. { a local and the function can have the same
  1094. name in TP and Delphi, but RESULT not }
  1095. if (m_duplicate_names in current_settings.modeswitches) and
  1096. (hsym.typ in [absolutevarsym,localvarsym]) and
  1097. (vo_is_funcret in tabstractvarsym(hsym).varoptions) and
  1098. not((m_result in current_settings.modeswitches) and
  1099. (vo_is_result in tabstractvarsym(hsym).varoptions)) then
  1100. HideSym(hsym)
  1101. else
  1102. DuplicateSym(hashedid,sym,hsym);
  1103. result:=true;
  1104. exit;
  1105. end;
  1106. { check also parasymtable, this needs to be done here becuase
  1107. of the special situation with the funcret sym that needs to be
  1108. hidden for tp and delphi modes }
  1109. hsym:=tsym(tabstractprocdef(defowner).parast.FindWithHash(hashedid));
  1110. if assigned(hsym) then
  1111. begin
  1112. { a local and the function can have the same
  1113. name in TP and Delphi, but RESULT not }
  1114. if (m_duplicate_names in current_settings.modeswitches) and
  1115. (sym.typ in [absolutevarsym,localvarsym]) and
  1116. (vo_is_funcret in tabstractvarsym(sym).varoptions) and
  1117. not((m_result in current_settings.modeswitches) and
  1118. (vo_is_result in tabstractvarsym(sym).varoptions)) then
  1119. Hidesym(sym)
  1120. else
  1121. DuplicateSym(hashedid,sym,hsym);
  1122. result:=true;
  1123. exit;
  1124. end;
  1125. { check ObjectSymtable, skip this for funcret sym because
  1126. that will always be positive because it has the same name
  1127. as the procsym }
  1128. if not is_funcret_sym(sym) and
  1129. (defowner.typ=procdef) and
  1130. assigned(tprocdef(defowner)._class) and
  1131. (tprocdef(defowner).owner.defowner=tprocdef(defowner)._class) and
  1132. (
  1133. not(m_delphi in current_settings.modeswitches) or
  1134. is_object(tprocdef(defowner)._class)
  1135. ) then
  1136. result:=tprocdef(defowner)._class.symtable.checkduplicate(hashedid,sym);
  1137. end;
  1138. {****************************************************************************
  1139. TParaSymtable
  1140. ****************************************************************************}
  1141. constructor tparasymtable.create(adefowner:tdef;level:byte);
  1142. begin
  1143. inherited create('');
  1144. defowner:=adefowner;
  1145. symtabletype:=parasymtable;
  1146. symtablelevel:=level;
  1147. end;
  1148. function tparasymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  1149. begin
  1150. result:=inherited checkduplicate(hashedid,sym);
  1151. if result then
  1152. exit;
  1153. if not(m_duplicate_names in current_settings.modeswitches) and
  1154. (defowner.typ=procdef) and
  1155. assigned(tprocdef(defowner)._class) and
  1156. (tprocdef(defowner).owner.defowner=tprocdef(defowner)._class) and
  1157. (
  1158. not(m_delphi in current_settings.modeswitches) or
  1159. is_object(tprocdef(defowner)._class)
  1160. ) then
  1161. result:=tprocdef(defowner)._class.symtable.checkduplicate(hashedid,sym);
  1162. end;
  1163. {****************************************************************************
  1164. TAbstractUniTSymtable
  1165. ****************************************************************************}
  1166. constructor tabstractuniTSymtable.create(const n : string;id:word);
  1167. begin
  1168. inherited create(n);
  1169. moduleid:=id;
  1170. end;
  1171. function tabstractuniTSymtable.iscurrentunit:boolean;
  1172. begin
  1173. result:=assigned(current_module) and
  1174. (
  1175. (current_module.globalsymtable=self) or
  1176. (current_module.localsymtable=self)
  1177. );
  1178. end;
  1179. {****************************************************************************
  1180. TStaticSymtable
  1181. ****************************************************************************}
  1182. constructor tstaticsymtable.create(const n : string;id:word);
  1183. begin
  1184. inherited create(n,id);
  1185. symtabletype:=staticsymtable;
  1186. symtablelevel:=main_program_level;
  1187. end;
  1188. procedure tstaticsymtable.ppuload(ppufile:tcompilerppufile);
  1189. begin
  1190. inherited ppuload(ppufile);
  1191. { now we can deref the syms and defs }
  1192. deref;
  1193. end;
  1194. procedure tstaticsymtable.ppuwrite(ppufile:tcompilerppufile);
  1195. begin
  1196. inherited ppuwrite(ppufile);
  1197. end;
  1198. function tstaticsymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  1199. var
  1200. hsym : tsym;
  1201. begin
  1202. result:=false;
  1203. hsym:=tsym(FindWithHash(hashedid));
  1204. if assigned(hsym) then
  1205. begin
  1206. { Delphi (contrary to TP) you can have a symbol with the same name as the
  1207. unit, the unit can then not be accessed anymore using
  1208. <unit>.<id>, so we can hide the symbol }
  1209. if (m_delphi in current_settings.modeswitches) and
  1210. (hsym.typ=symconst.unitsym) then
  1211. HideSym(hsym)
  1212. else
  1213. DuplicateSym(hashedid,sym,hsym);
  1214. result:=true;
  1215. exit;
  1216. end;
  1217. if (current_module.localsymtable=self) and
  1218. assigned(current_module.globalsymtable) then
  1219. result:=tglobalsymtable(current_module.globalsymtable).checkduplicate(hashedid,sym);
  1220. end;
  1221. {****************************************************************************
  1222. TGlobalSymtable
  1223. ****************************************************************************}
  1224. constructor tglobalsymtable.create(const n : string;id:word);
  1225. begin
  1226. inherited create(n,id);
  1227. symtabletype:=globalsymtable;
  1228. symtablelevel:=main_program_level;
  1229. end;
  1230. procedure tglobalsymtable.ppuload(ppufile:tcompilerppufile);
  1231. begin
  1232. inherited ppuload(ppufile);
  1233. { now we can deref the syms and defs }
  1234. deref;
  1235. end;
  1236. procedure tglobalsymtable.ppuwrite(ppufile:tcompilerppufile);
  1237. begin
  1238. { write the symtable entries }
  1239. inherited ppuwrite(ppufile);
  1240. end;
  1241. function tglobalsymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  1242. var
  1243. hsym : tsym;
  1244. begin
  1245. result:=false;
  1246. hsym:=tsym(FindWithHash(hashedid));
  1247. if assigned(hsym) then
  1248. begin
  1249. { Delphi (contrary to TP) you can have a symbol with the same name as the
  1250. unit, the unit can then not be accessed anymore using
  1251. <unit>.<id>, so we can hide the symbol }
  1252. if (m_delphi in current_settings.modeswitches) and
  1253. (hsym.typ=symconst.unitsym) then
  1254. HideSym(hsym)
  1255. else
  1256. DuplicateSym(hashedid,sym,hsym);
  1257. result:=true;
  1258. exit;
  1259. end;
  1260. end;
  1261. {****************************************************************************
  1262. TWITHSYMTABLE
  1263. ****************************************************************************}
  1264. constructor twithsymtable.create(aowner:tdef;ASymList:TFPHashObjectList;refnode:tobject{tnode});
  1265. begin
  1266. inherited create('');
  1267. symtabletype:=withsymtable;
  1268. withrefnode:=refnode;
  1269. { Replace SymList with the passed symlist }
  1270. SymList.free;
  1271. SymList:=ASymList;
  1272. defowner:=aowner;
  1273. end;
  1274. destructor twithsymtable.destroy;
  1275. begin
  1276. withrefnode.free;
  1277. { Disable SymList because we don't Own it }
  1278. SymList:=nil;
  1279. inherited destroy;
  1280. end;
  1281. procedure twithsymtable.clear;
  1282. begin
  1283. { remove no entry from a withsymtable as it is only a pointer to the
  1284. recorddef or objectdef symtable }
  1285. end;
  1286. procedure twithsymtable.insertdef(def:TDefEntry);
  1287. begin
  1288. { Definitions can't be registered in the withsymtable
  1289. because the withsymtable is removed after the with block.
  1290. We can't easily solve it here because the next symtable in the
  1291. stack is not known. }
  1292. internalerror(200602046);
  1293. end;
  1294. {****************************************************************************
  1295. TSTT_ExceptionSymtable
  1296. ****************************************************************************}
  1297. constructor tstt_excepTSymtable.create;
  1298. begin
  1299. inherited create('');
  1300. symtabletype:=stt_excepTSymtable;
  1301. end;
  1302. {****************************************************************************
  1303. TMacroSymtable
  1304. ****************************************************************************}
  1305. constructor tmacrosymtable.create(exported: boolean);
  1306. begin
  1307. inherited create('');
  1308. if exported then
  1309. symtabletype:=exportedmacrosymtable
  1310. else
  1311. symtabletype:=localmacrosymtable;
  1312. symtablelevel:=main_program_level;
  1313. end;
  1314. {*****************************************************************************
  1315. Helper Routines
  1316. *****************************************************************************}
  1317. function FullTypeName(def,otherdef:tdef):string;
  1318. var
  1319. s1,s2 : string;
  1320. begin
  1321. s1:=def.typename;
  1322. { When the names are the same try to include the unit name }
  1323. if assigned(otherdef) and
  1324. (def.owner.symtabletype in [globalsymtable,staticsymtable]) then
  1325. begin
  1326. s2:=otherdef.typename;
  1327. if upper(s1)=upper(s2) then
  1328. s1:=def.owner.realname^+'.'+s1;
  1329. end;
  1330. FullTypeName:=s1;
  1331. end;
  1332. procedure incompatibletypes(def1,def2:tdef);
  1333. begin
  1334. { When there is an errordef there is already an error message show }
  1335. if (def2.typ=errordef) or
  1336. (def1.typ=errordef) then
  1337. exit;
  1338. CGMessage2(type_e_incompatible_types,FullTypeName(def1,def2),FullTypeName(def2,def1));
  1339. end;
  1340. procedure hidesym(sym:TSymEntry);
  1341. begin
  1342. sym.realname:='$hidden'+sym.realname;
  1343. tsym(sym).visibility:=vis_hidden;
  1344. end;
  1345. procedure duplicatesym(var hashedid:THashedIDString;dupsym,origsym:TSymEntry);
  1346. var
  1347. st : TSymtable;
  1348. begin
  1349. Message1(sym_e_duplicate_id,tsym(origsym).realname);
  1350. { Write hint where the original symbol was found }
  1351. st:=finduniTSymtable(origsym.owner);
  1352. with tsym(origsym).fileinfo do
  1353. begin
  1354. if assigned(st) and
  1355. (st.symtabletype=globalsymtable) and
  1356. st.iscurrentunit then
  1357. Message2(sym_h_duplicate_id_where,current_module.sourcefiles.get_file_name(fileindex),tostr(line))
  1358. else if assigned(st.name) then
  1359. Message2(sym_h_duplicate_id_where,'unit '+st.name^,tostr(line));
  1360. end;
  1361. { Rename duplicate sym to an unreachable name, but it can be
  1362. inserted in the symtable without errors }
  1363. inc(dupnr);
  1364. hashedid.id:='dup'+tostr(dupnr)+hashedid.id;
  1365. if assigned(dupsym) then
  1366. include(tsym(dupsym).symoptions,sp_implicitrename);
  1367. end;
  1368. {*****************************************************************************
  1369. Search
  1370. *****************************************************************************}
  1371. procedure addsymref(sym:tsym);
  1372. begin
  1373. { symbol uses count }
  1374. sym.IncRefCount;
  1375. { unit uses count }
  1376. if assigned(current_module) and
  1377. (sym.owner.symtabletype=globalsymtable) then
  1378. begin
  1379. if tglobalsymtable(sym.owner).moduleid>=current_module.unitmapsize then
  1380. internalerror(200501152);
  1381. inc(current_module.unitmap[tglobalsymtable(sym.owner).moduleid].refs);
  1382. end;
  1383. end;
  1384. function is_visible_for_object(symst:tsymtable;symvisibility:tvisibility;contextobjdef:tobjectdef):boolean;
  1385. var
  1386. symownerdef : tobjectdef;
  1387. begin
  1388. result:=false;
  1389. { Get objdectdef owner of the symtable for the is_related checks }
  1390. if not assigned(symst) or
  1391. (symst.symtabletype<>objectsymtable) then
  1392. internalerror(200810285);
  1393. symownerdef:=tobjectdef(symst.defowner);
  1394. case symvisibility of
  1395. vis_private :
  1396. begin
  1397. { private symbols are allowed when we are in the same
  1398. module as they are defined }
  1399. result:=(
  1400. (symownerdef.owner.symtabletype in [globalsymtable,staticsymtable]) and
  1401. (symownerdef.owner.iscurrentunit)
  1402. ) or
  1403. ( // the case of specialize inside the generic declaration
  1404. (symownerdef.owner.symtabletype = objectsymtable) and
  1405. (
  1406. assigned(current_objectdef) and
  1407. (
  1408. (current_objectdef=symownerdef) or
  1409. (current_objectdef.owner.moduleid=symownerdef.owner.moduleid)
  1410. )
  1411. ) or
  1412. (
  1413. not assigned(current_objectdef) and
  1414. (symownerdef.owner.moduleid=current_module.moduleid)
  1415. )
  1416. );
  1417. end;
  1418. vis_strictprivate :
  1419. begin
  1420. result:=assigned(current_objectdef) and
  1421. (current_objectdef=symownerdef);
  1422. end;
  1423. vis_strictprotected :
  1424. begin
  1425. result:=assigned(current_objectdef) and
  1426. current_objectdef.is_related(symownerdef);
  1427. end;
  1428. vis_protected :
  1429. begin
  1430. { protected symbols are visible in the module that defines them and
  1431. also visible to related objects. The related object must be defined
  1432. in the current module }
  1433. result:=(
  1434. (
  1435. (symownerdef.owner.symtabletype in [globalsymtable,staticsymtable]) and
  1436. (symownerdef.owner.iscurrentunit)
  1437. ) or
  1438. (
  1439. assigned(contextobjdef) and
  1440. (contextobjdef.owner.symtabletype in [globalsymtable,staticsymtable]) and
  1441. (contextobjdef.owner.iscurrentunit) and
  1442. contextobjdef.is_related(symownerdef)
  1443. ) or
  1444. ( // the case of specialize inside the generic declaration
  1445. (symownerdef.owner.symtabletype = objectsymtable) and
  1446. (
  1447. assigned(current_objectdef) and
  1448. (
  1449. (current_objectdef=symownerdef) or
  1450. (current_objectdef.owner.moduleid=symownerdef.owner.moduleid)
  1451. )
  1452. ) or
  1453. (
  1454. not assigned(current_objectdef) and
  1455. (symownerdef.owner.moduleid=current_module.moduleid)
  1456. )
  1457. )
  1458. );
  1459. end;
  1460. vis_public,
  1461. vis_published :
  1462. result:=true;
  1463. end;
  1464. end;
  1465. function is_visible_for_object(pd:tprocdef;contextobjdef:tobjectdef):boolean;
  1466. begin
  1467. result:=is_visible_for_object(pd.owner,pd.visibility,contextobjdef);
  1468. end;
  1469. function is_visible_for_object(sym:tsym;contextobjdef:tobjectdef):boolean;
  1470. var
  1471. i : longint;
  1472. pd : tprocdef;
  1473. begin
  1474. if sym.typ=procsym then
  1475. begin
  1476. { A procsym is visible, when there is at least one of the procdefs visible }
  1477. result:=false;
  1478. for i:=0 to tprocsym(sym).ProcdefList.Count-1 do
  1479. begin
  1480. pd:=tprocdef(tprocsym(sym).ProcdefList[i]);
  1481. if (pd.owner=sym.owner) and
  1482. is_visible_for_object(pd,contextobjdef) then
  1483. begin
  1484. result:=true;
  1485. exit;
  1486. end;
  1487. end;
  1488. end
  1489. else
  1490. result:=is_visible_for_object(sym.owner,sym.visibility,contextobjdef);
  1491. end;
  1492. function searchsym(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  1493. var
  1494. hashedid : THashedIDString;
  1495. contextobjdef : tobjectdef;
  1496. stackitem : psymtablestackitem;
  1497. begin
  1498. result:=false;
  1499. hashedid.id:=s;
  1500. stackitem:=symtablestack.stack;
  1501. while assigned(stackitem) do
  1502. begin
  1503. srsymtable:=stackitem^.symtable;
  1504. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  1505. if assigned(srsym) then
  1506. begin
  1507. { use the class from withsymtable only when it is
  1508. defined in this unit }
  1509. if (srsymtable.symtabletype=withsymtable) and
  1510. assigned(srsymtable.defowner) and
  1511. (srsymtable.defowner.typ=objectdef) and
  1512. (srsymtable.defowner.owner.symtabletype in [globalsymtable,staticsymtable]) and
  1513. (srsymtable.defowner.owner.iscurrentunit) then
  1514. contextobjdef:=tobjectdef(srsymtable.defowner)
  1515. else
  1516. contextobjdef:=current_objectdef;
  1517. if (srsym.owner.symtabletype<>objectsymtable) or
  1518. is_visible_for_object(srsym,contextobjdef) then
  1519. begin
  1520. { we need to know if a procedure references symbols
  1521. in the static symtable, because then it can't be
  1522. inlined from outside this unit }
  1523. if assigned(current_procinfo) and
  1524. (srsym.owner.symtabletype=staticsymtable) then
  1525. include(current_procinfo.flags,pi_uses_static_symtable);
  1526. addsymref(srsym);
  1527. result:=true;
  1528. exit;
  1529. end;
  1530. end;
  1531. { also search for class helpers }
  1532. if (srsymtable.symtabletype=objectsymtable) and
  1533. is_objcclass(tdef(srsymtable.defowner)) then
  1534. begin
  1535. if search_class_helper(tobjectdef(srsymtable.defowner),s,srsym,srsymtable) then
  1536. begin
  1537. result:=true;
  1538. exit;
  1539. end;
  1540. end;
  1541. stackitem:=stackitem^.next;
  1542. end;
  1543. srsym:=nil;
  1544. srsymtable:=nil;
  1545. end;
  1546. function searchsym_type(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  1547. var
  1548. hashedid : THashedIDString;
  1549. stackitem : psymtablestackitem;
  1550. begin
  1551. result:=false;
  1552. hashedid.id:=s;
  1553. stackitem:=symtablestack.stack;
  1554. while assigned(stackitem) do
  1555. begin
  1556. {
  1557. It is not possible to have type symbols in:
  1558. records
  1559. objects
  1560. parameters
  1561. Exception are classes, generic definitions and specializations
  1562. that have the parameterized types inserted in the symtable.
  1563. }
  1564. srsymtable:=stackitem^.symtable;
  1565. if not(srsymtable.symtabletype in [recordsymtable,ObjectSymtable,parasymtable]) or
  1566. (assigned(srsymtable.defowner) and
  1567. (
  1568. (df_generic in tdef(srsymtable.defowner).defoptions) or
  1569. (df_specialization in tdef(srsymtable.defowner).defoptions) or
  1570. is_class(tdef(srsymtable.defowner)))
  1571. ) then
  1572. begin
  1573. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  1574. if assigned(srsym) and
  1575. not(srsym.typ in [fieldvarsym,paravarsym]) and
  1576. (
  1577. (srsym.owner.symtabletype<>objectsymtable) or
  1578. (is_visible_for_object(srsym,current_objectdef) and
  1579. (srsym.typ=typesym))
  1580. ) then
  1581. begin
  1582. { we need to know if a procedure references symbols
  1583. in the static symtable, because then it can't be
  1584. inlined from outside this unit }
  1585. if assigned(current_procinfo) and
  1586. (srsym.owner.symtabletype=staticsymtable) then
  1587. include(current_procinfo.flags,pi_uses_static_symtable);
  1588. addsymref(srsym);
  1589. result:=true;
  1590. exit;
  1591. end;
  1592. end;
  1593. stackitem:=stackitem^.next;
  1594. end;
  1595. result:=false;
  1596. srsym:=nil;
  1597. srsymtable:=nil;
  1598. end;
  1599. function searchsym_in_module(pm:pointer;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  1600. var
  1601. pmod : tmodule;
  1602. begin
  1603. pmod:=tmodule(pm);
  1604. result:=false;
  1605. if assigned(pmod.globalsymtable) then
  1606. begin
  1607. srsym:=tsym(pmod.globalsymtable.Find(s));
  1608. if assigned(srsym) then
  1609. begin
  1610. srsymtable:=pmod.globalsymtable;
  1611. addsymref(srsym);
  1612. result:=true;
  1613. exit;
  1614. end;
  1615. end;
  1616. { If the module is the current unit we also need
  1617. to search the local symtable }
  1618. if (pmod=current_module) and
  1619. assigned(pmod.localsymtable) then
  1620. begin
  1621. srsym:=tsym(pmod.localsymtable.Find(s));
  1622. if assigned(srsym) then
  1623. begin
  1624. srsymtable:=pmod.localsymtable;
  1625. addsymref(srsym);
  1626. result:=true;
  1627. exit;
  1628. end;
  1629. end;
  1630. srsym:=nil;
  1631. srsymtable:=nil;
  1632. end;
  1633. function searchsym_in_named_module(const unitname, symname: TIDString; out srsym: tsym; out srsymtable: tsymtable): boolean;
  1634. var
  1635. stackitem : psymtablestackitem;
  1636. begin
  1637. result:=false;
  1638. stackitem:=symtablestack.stack;
  1639. while assigned(stackitem) do
  1640. begin
  1641. srsymtable:=stackitem^.symtable;
  1642. if (srsymtable.symtabletype=globalsymtable) and
  1643. (srsymtable.name^=unitname) then
  1644. begin
  1645. srsym:=tsym(srsymtable.find(symname));
  1646. if not assigned(srsym) then
  1647. break;
  1648. result:=true;
  1649. exit;
  1650. end;
  1651. stackitem:=stackitem^.next;
  1652. end;
  1653. { If the module is the current unit we also need
  1654. to search the local symtable }
  1655. if (current_module.globalsymtable=srsymtable) and
  1656. assigned(current_module.localsymtable) then
  1657. begin
  1658. srsymtable:=current_module.localsymtable;
  1659. srsym:=tsym(srsymtable.find(symname));
  1660. if assigned(srsym) then
  1661. begin
  1662. result:=true;
  1663. exit;
  1664. end;
  1665. end;
  1666. end;
  1667. function searchsym_in_class(classh,contextclassh:tobjectdef;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  1668. var
  1669. hashedid : THashedIDString;
  1670. orgclass : tobjectdef;
  1671. begin
  1672. orgclass:=classh;
  1673. { The contextclassh is used for visibility. The classh must be equal to
  1674. or be a parent of contextclassh. E.g. for inherited searches the classh is the
  1675. parent. }
  1676. if assigned(classh) and
  1677. not contextclassh.is_related(classh) then
  1678. internalerror(200811161);
  1679. result:=false;
  1680. hashedid.id:=s;
  1681. while assigned(classh) do
  1682. begin
  1683. srsymtable:=classh.symtable;
  1684. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  1685. if assigned(srsym) and
  1686. is_visible_for_object(srsym,contextclassh) then
  1687. begin
  1688. addsymref(srsym);
  1689. result:=true;
  1690. exit;
  1691. end;
  1692. classh:=classh.childof;
  1693. end;
  1694. if is_objcclass(orgclass) then
  1695. result:=search_class_helper(orgclass,s,srsym,srsymtable)
  1696. else
  1697. begin
  1698. srsym:=nil;
  1699. srsymtable:=nil;
  1700. end;
  1701. end;
  1702. function searchsym_in_class_by_msgint(classh:tobjectdef;msgid:longint;out srdef : tdef;out srsym:tsym;out srsymtable:TSymtable):boolean;
  1703. var
  1704. def : tdef;
  1705. i : longint;
  1706. begin
  1707. result:=false;
  1708. def:=nil;
  1709. while assigned(classh) do
  1710. begin
  1711. for i:=0 to classh.symtable.DefList.Count-1 do
  1712. begin
  1713. def:=tstoreddef(classh.symtable.DefList[i]);
  1714. { Find also all hidden private methods to
  1715. be compatible with delphi, see tw6203 (PFV) }
  1716. if (def.typ=procdef) and
  1717. (po_msgint in tprocdef(def).procoptions) and
  1718. (tprocdef(def).messageinf.i=msgid) then
  1719. begin
  1720. srdef:=def;
  1721. srsym:=tprocdef(def).procsym;
  1722. srsymtable:=classh.symtable;
  1723. addsymref(srsym);
  1724. result:=true;
  1725. exit;
  1726. end;
  1727. end;
  1728. classh:=classh.childof;
  1729. end;
  1730. srdef:=nil;
  1731. srsym:=nil;
  1732. srsymtable:=nil;
  1733. end;
  1734. function searchsym_in_class_by_msgstr(classh:tobjectdef;const s:string;out srsym:tsym;out srsymtable:TSymtable):boolean;
  1735. var
  1736. def : tdef;
  1737. i : longint;
  1738. begin
  1739. result:=false;
  1740. def:=nil;
  1741. while assigned(classh) do
  1742. begin
  1743. for i:=0 to classh.symtable.DefList.Count-1 do
  1744. begin
  1745. def:=tstoreddef(classh.symtable.DefList[i]);
  1746. { Find also all hidden private methods to
  1747. be compatible with delphi, see tw6203 (PFV) }
  1748. if (def.typ=procdef) and
  1749. (po_msgstr in tprocdef(def).procoptions) and
  1750. (tprocdef(def).messageinf.str^=s) then
  1751. begin
  1752. srsym:=tprocdef(def).procsym;
  1753. srsymtable:=classh.symtable;
  1754. addsymref(srsym);
  1755. result:=true;
  1756. exit;
  1757. end;
  1758. end;
  1759. classh:=classh.childof;
  1760. end;
  1761. srsym:=nil;
  1762. srsymtable:=nil;
  1763. end;
  1764. function search_assignment_operator(from_def,to_def:Tdef):Tprocdef;
  1765. var
  1766. sym : Tprocsym;
  1767. hashedid : THashedIDString;
  1768. curreq,
  1769. besteq : tequaltype;
  1770. currpd,
  1771. bestpd : tprocdef;
  1772. stackitem : psymtablestackitem;
  1773. begin
  1774. hashedid.id:='assign';
  1775. besteq:=te_incompatible;
  1776. bestpd:=nil;
  1777. stackitem:=symtablestack.stack;
  1778. while assigned(stackitem) do
  1779. begin
  1780. sym:=Tprocsym(stackitem^.symtable.FindWithHash(hashedid));
  1781. if sym<>nil then
  1782. begin
  1783. if sym.typ<>procsym then
  1784. internalerror(200402031);
  1785. { if the source type is an alias then this is only the second choice,
  1786. if you mess with this code, check tw4093 }
  1787. currpd:=sym.find_procdef_assignment_operator(from_def,to_def,curreq);
  1788. if curreq>besteq then
  1789. begin
  1790. besteq:=curreq;
  1791. bestpd:=currpd;
  1792. if (besteq=te_exact) then
  1793. break;
  1794. end;
  1795. end;
  1796. stackitem:=stackitem^.next;
  1797. end;
  1798. result:=bestpd;
  1799. end;
  1800. function search_enumerator_operator(type_def:Tdef): Tprocdef;
  1801. var
  1802. sym : Tprocsym;
  1803. hashedid : THashedIDString;
  1804. curreq,
  1805. besteq : tequaltype;
  1806. currpd,
  1807. bestpd : tprocdef;
  1808. stackitem : psymtablestackitem;
  1809. begin
  1810. hashedid.id:='enumerator';
  1811. besteq:=te_incompatible;
  1812. bestpd:=nil;
  1813. stackitem:=symtablestack.stack;
  1814. while assigned(stackitem) do
  1815. begin
  1816. sym:=Tprocsym(stackitem^.symtable.FindWithHash(hashedid));
  1817. if sym<>nil then
  1818. begin
  1819. if sym.typ<>procsym then
  1820. internalerror(200910241);
  1821. { if the source type is an alias then this is only the second choice,
  1822. if you mess with this code, check tw4093 }
  1823. currpd:=sym.find_procdef_enumerator_operator(type_def,curreq);
  1824. if curreq>besteq then
  1825. begin
  1826. besteq:=curreq;
  1827. bestpd:=currpd;
  1828. if (besteq=te_exact) then
  1829. break;
  1830. end;
  1831. end;
  1832. stackitem:=stackitem^.next;
  1833. end;
  1834. result:=bestpd;
  1835. end;
  1836. function search_system_type(const s: TIDString): ttypesym;
  1837. var
  1838. sym : tsym;
  1839. begin
  1840. sym:=tsym(systemunit.Find(s));
  1841. if not assigned(sym) or
  1842. (sym.typ<>typesym) then
  1843. cgmessage1(cg_f_unknown_system_type,s);
  1844. result:=ttypesym(sym);
  1845. end;
  1846. function search_named_unit_globaltype(const unitname, typename: TIDString): ttypesym;
  1847. var
  1848. srsymtable: tsymtable;
  1849. sym: tsym;
  1850. begin
  1851. if searchsym_in_named_module(unitname,typename,sym,srsymtable) and
  1852. (sym.typ=typesym) then
  1853. begin
  1854. result:=ttypesym(sym);
  1855. exit;
  1856. end
  1857. else
  1858. begin
  1859. cgmessage2(cg_f_unknown_type_in_unit,typename,unitname);
  1860. result:=nil;
  1861. end;
  1862. end;
  1863. function search_class_helper(pd : tobjectdef;const s : string; out srsym: tsym; out srsymtable: tsymtable):boolean;
  1864. var
  1865. hashedid : THashedIDString;
  1866. stackitem : psymtablestackitem;
  1867. i : longint;
  1868. defowner : tobjectdef;
  1869. begin
  1870. hashedid.id:=class_helper_prefix+s;
  1871. stackitem:=symtablestack.stack;
  1872. while assigned(stackitem) do
  1873. begin
  1874. srsymtable:=stackitem^.symtable;
  1875. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  1876. if assigned(srsym) then
  1877. begin
  1878. if not(srsymtable.symtabletype in [globalsymtable,staticsymtable]) or
  1879. not(srsym.owner.symtabletype in [globalsymtable,staticsymtable]) or
  1880. (srsym.typ<>procsym) then
  1881. internalerror(2009111505);
  1882. { check whether this procsym includes a helper for this particular class }
  1883. for i:=0 to tprocsym(srsym).procdeflist.count-1 do
  1884. begin
  1885. { does pd inherit from (or is the same as) the class
  1886. that this method's category extended?
  1887. Warning: this list contains both category and objcclass methods
  1888. (for id.randommethod), so only check category methods here
  1889. }
  1890. defowner:=tobjectdef(tprocdef(tprocsym(srsym).procdeflist[i]).owner.defowner);
  1891. if (oo_is_classhelper in defowner.objectoptions) and
  1892. pd.is_related(defowner.childof) then
  1893. begin
  1894. { we need to know if a procedure references symbols
  1895. in the static symtable, because then it can't be
  1896. inlined from outside this unit }
  1897. if assigned(current_procinfo) and
  1898. (srsym.owner.symtabletype=staticsymtable) then
  1899. include(current_procinfo.flags,pi_uses_static_symtable);
  1900. { no need to keep looking. There might be other
  1901. categories that extend this, a parent or child
  1902. class with a method with the same name (either
  1903. overriding this one, or overridden by this one),
  1904. but that doesn't matter as far as the basic
  1905. procsym is concerned.
  1906. }
  1907. srsym:=tprocdef(tprocsym(srsym).procdeflist[i]).procsym;
  1908. srsymtable:=srsym.owner;
  1909. addsymref(srsym);
  1910. result:=true;
  1911. exit;
  1912. end;
  1913. end;
  1914. end;
  1915. stackitem:=stackitem^.next;
  1916. end;
  1917. srsym:=nil;
  1918. srsymtable:=nil;
  1919. result:=false;
  1920. end;
  1921. function search_objc_method(const s : string; out srsym: tsym; out srsymtable: tsymtable):boolean;
  1922. var
  1923. hashedid : THashedIDString;
  1924. stackitem : psymtablestackitem;
  1925. i : longint;
  1926. begin
  1927. hashedid.id:=class_helper_prefix+s;
  1928. stackitem:=symtablestack.stack;
  1929. while assigned(stackitem) do
  1930. begin
  1931. srsymtable:=stackitem^.symtable;
  1932. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  1933. if assigned(srsym) then
  1934. begin
  1935. if not(srsymtable.symtabletype in [globalsymtable,staticsymtable]) or
  1936. not(srsym.owner.symtabletype in [globalsymtable,staticsymtable]) or
  1937. (srsym.typ<>procsym) then
  1938. internalerror(2009112005);
  1939. { check whether this procsym includes a helper for this particular class }
  1940. for i:=0 to tprocsym(srsym).procdeflist.count-1 do
  1941. begin
  1942. { we need to know if a procedure references symbols
  1943. in the static symtable, because then it can't be
  1944. inlined from outside this unit }
  1945. if assigned(current_procinfo) and
  1946. (srsym.owner.symtabletype=staticsymtable) then
  1947. include(current_procinfo.flags,pi_uses_static_symtable);
  1948. { no need to keep looking. There might be other
  1949. methods with the same name, but that doesn't matter
  1950. as far as the basic procsym is concerned.
  1951. }
  1952. srsym:=tprocdef(tprocsym(srsym).procdeflist[i]).procsym;
  1953. { We need the symtable in which the classhelper-like sym
  1954. is located, not the objectdef. The reason is that the
  1955. callnode will climb the symtablestack until it encounters
  1956. this symtable to start looking for overloads (and it won't
  1957. find the objectsymtable in which this method sym is
  1958. located
  1959. srsymtable:=srsym.owner;
  1960. }
  1961. addsymref(srsym);
  1962. result:=true;
  1963. exit;
  1964. end;
  1965. end;
  1966. stackitem:=stackitem^.next;
  1967. end;
  1968. srsym:=nil;
  1969. srsymtable:=nil;
  1970. result:=false;
  1971. end;
  1972. function search_class_member(pd : tobjectdef;const s : string):tsym;
  1973. { searches n in symtable of pd and all anchestors }
  1974. var
  1975. hashedid : THashedIDString;
  1976. srsym : tsym;
  1977. orgpd : tobjectdef;
  1978. srsymtable : tsymtable;
  1979. begin
  1980. hashedid.id:=s;
  1981. orgpd:=pd;
  1982. while assigned(pd) do
  1983. begin
  1984. srsym:=tsym(pd.symtable.FindWithHash(hashedid));
  1985. if assigned(srsym) then
  1986. begin
  1987. search_class_member:=srsym;
  1988. exit;
  1989. end;
  1990. pd:=pd.childof;
  1991. end;
  1992. { not found, now look for class helpers }
  1993. if is_objcclass(pd) then
  1994. search_class_helper(orgpd,s,result,srsymtable)
  1995. else
  1996. result:=nil;
  1997. end;
  1998. function search_macro(const s : string):tsym;
  1999. var
  2000. stackitem : psymtablestackitem;
  2001. hashedid : THashedIDString;
  2002. srsym : tsym;
  2003. begin
  2004. hashedid.id:=s;
  2005. { First search the localmacrosymtable before searching the
  2006. global macrosymtables from the units }
  2007. if assigned(current_module) then
  2008. begin
  2009. srsym:=tsym(current_module.localmacrosymtable.FindWithHash(hashedid));
  2010. if assigned(srsym) then
  2011. begin
  2012. result:= srsym;
  2013. exit;
  2014. end;
  2015. end;
  2016. stackitem:=macrosymtablestack.stack;
  2017. while assigned(stackitem) do
  2018. begin
  2019. srsym:=tsym(stackitem^.symtable.FindWithHash(hashedid));
  2020. if assigned(srsym) then
  2021. begin
  2022. result:= srsym;
  2023. exit;
  2024. end;
  2025. stackitem:=stackitem^.next;
  2026. end;
  2027. result:= nil;
  2028. end;
  2029. function defined_macro(const s : string):boolean;
  2030. var
  2031. mac: tmacro;
  2032. begin
  2033. mac:=tmacro(search_macro(s));
  2034. if assigned(mac) then
  2035. begin
  2036. mac.is_used:=true;
  2037. defined_macro:=mac.defined;
  2038. end
  2039. else
  2040. defined_macro:=false;
  2041. end;
  2042. {****************************************************************************
  2043. Object Helpers
  2044. ****************************************************************************}
  2045. function search_default_property(pd : tobjectdef) : tpropertysym;
  2046. { returns the default property of a class, searches also anchestors }
  2047. var
  2048. _defaultprop : tpropertysym;
  2049. begin
  2050. _defaultprop:=nil;
  2051. while assigned(pd) do
  2052. begin
  2053. pd.symtable.SymList.ForEachCall(@tstoredsymtable(pd.symtable).testfordefaultproperty,@_defaultprop);
  2054. if assigned(_defaultprop) then
  2055. break;
  2056. pd:=pd.childof;
  2057. end;
  2058. search_default_property:=_defaultprop;
  2059. end;
  2060. {****************************************************************************
  2061. Macro Helpers
  2062. ****************************************************************************}
  2063. procedure def_system_macro(const name : string);
  2064. var
  2065. mac : tmacro;
  2066. s: string;
  2067. begin
  2068. if name = '' then
  2069. internalerror(2004121202);
  2070. s:= upper(name);
  2071. mac:=tmacro(search_macro(s));
  2072. if not assigned(mac) then
  2073. begin
  2074. mac:=tmacro.create(s);
  2075. if assigned(current_module) then
  2076. current_module.localmacrosymtable.insert(mac)
  2077. else
  2078. initialmacrosymtable.insert(mac);
  2079. end;
  2080. if not mac.defined then
  2081. Message1(parser_c_macro_defined,mac.name);
  2082. mac.defined:=true;
  2083. end;
  2084. procedure set_system_macro(const name, value : string);
  2085. var
  2086. mac : tmacro;
  2087. s: string;
  2088. begin
  2089. if name = '' then
  2090. internalerror(2004121203);
  2091. s:= upper(name);
  2092. mac:=tmacro(search_macro(s));
  2093. if not assigned(mac) then
  2094. begin
  2095. mac:=tmacro.create(s);
  2096. if assigned(current_module) then
  2097. current_module.localmacrosymtable.insert(mac)
  2098. else
  2099. initialmacrosymtable.insert(mac);
  2100. end
  2101. else
  2102. begin
  2103. mac.is_compiler_var:=false;
  2104. if assigned(mac.buftext) then
  2105. freemem(mac.buftext,mac.buflen);
  2106. end;
  2107. Message2(parser_c_macro_set_to,mac.name,value);
  2108. mac.buflen:=length(value);
  2109. getmem(mac.buftext,mac.buflen);
  2110. move(value[1],mac.buftext^,mac.buflen);
  2111. mac.defined:=true;
  2112. end;
  2113. procedure set_system_compvar(const name, value : string);
  2114. var
  2115. mac : tmacro;
  2116. s: string;
  2117. begin
  2118. if name = '' then
  2119. internalerror(2004121204);
  2120. s:= upper(name);
  2121. mac:=tmacro(search_macro(s));
  2122. if not assigned(mac) then
  2123. begin
  2124. mac:=tmacro.create(s);
  2125. mac.is_compiler_var:=true;
  2126. if assigned(current_module) then
  2127. current_module.localmacrosymtable.insert(mac)
  2128. else
  2129. initialmacrosymtable.insert(mac);
  2130. end
  2131. else
  2132. begin
  2133. mac.is_compiler_var:=true;
  2134. if assigned(mac.buftext) then
  2135. freemem(mac.buftext,mac.buflen);
  2136. end;
  2137. Message2(parser_c_macro_set_to,mac.name,value);
  2138. mac.buflen:=length(value);
  2139. getmem(mac.buftext,mac.buflen);
  2140. move(value[1],mac.buftext^,mac.buflen);
  2141. mac.defined:=true;
  2142. end;
  2143. procedure undef_system_macro(const name : string);
  2144. var
  2145. mac : tmacro;
  2146. s: string;
  2147. begin
  2148. if name = '' then
  2149. internalerror(2004121205);
  2150. s:= upper(name);
  2151. mac:=tmacro(search_macro(s));
  2152. if not assigned(mac) then
  2153. {If not found, then it's already undefined.}
  2154. else
  2155. begin
  2156. if mac.defined then
  2157. Message1(parser_c_macro_undefined,mac.name);
  2158. mac.defined:=false;
  2159. mac.is_compiler_var:=false;
  2160. { delete old definition }
  2161. if assigned(mac.buftext) then
  2162. begin
  2163. freemem(mac.buftext,mac.buflen);
  2164. mac.buftext:=nil;
  2165. end;
  2166. end;
  2167. end;
  2168. {$ifdef UNITALIASES}
  2169. {****************************************************************************
  2170. TUNIT_ALIAS
  2171. ****************************************************************************}
  2172. constructor tunit_alias.create(const n:string);
  2173. var
  2174. i : longint;
  2175. begin
  2176. i:=pos('=',n);
  2177. if i=0 then
  2178. fail;
  2179. inherited createname(Copy(n,1,i-1));
  2180. newname:=stringdup(Copy(n,i+1,255));
  2181. end;
  2182. destructor tunit_alias.destroy;
  2183. begin
  2184. stringdispose(newname);
  2185. inherited destroy;
  2186. end;
  2187. procedure addunitalias(const n:string);
  2188. begin
  2189. unitaliases^.insert(tunit_alias,init(Upper(n))));
  2190. end;
  2191. function getunitalias(const n:string):string;
  2192. var
  2193. p : punit_alias;
  2194. begin
  2195. p:=punit_alias(unitaliases^.Find(Upper(n)));
  2196. if assigned(p) then
  2197. getunitalias:=punit_alias(p).newname^
  2198. else
  2199. getunitalias:=n;
  2200. end;
  2201. {$endif UNITALIASES}
  2202. {****************************************************************************
  2203. Init/Done Symtable
  2204. ****************************************************************************}
  2205. procedure InitSymtable;
  2206. begin
  2207. { Reset symbolstack }
  2208. symtablestack:=nil;
  2209. systemunit:=nil;
  2210. { create error syms and def }
  2211. generrorsym:=terrorsym.create;
  2212. generrordef:=terrordef.create;
  2213. { macros }
  2214. initialmacrosymtable:=tmacrosymtable.create(false);
  2215. macrosymtablestack:=TSymtablestack.create;
  2216. macrosymtablestack.push(initialmacrosymtable);
  2217. {$ifdef UNITALIASES}
  2218. { unit aliases }
  2219. unitaliases:=TFPHashObjectList.create;
  2220. {$endif}
  2221. { set some global vars to nil, might be important for the ide }
  2222. class_tobject:=nil;
  2223. interface_iunknown:=nil;
  2224. rec_tguid:=nil;
  2225. dupnr:=0;
  2226. end;
  2227. procedure DoneSymtable;
  2228. begin
  2229. generrorsym.owner:=nil;
  2230. generrorsym.free;
  2231. generrordef.owner:=nil;
  2232. generrordef.free;
  2233. initialmacrosymtable.free;
  2234. macrosymtablestack.free;
  2235. {$ifdef UNITALIASES}
  2236. unitaliases.free;
  2237. {$endif}
  2238. end;
  2239. end.