2
0

symtable.pas 77 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270
  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. {Looks for macro s (must be given in upper case) in the macrosymbolstack, }
  179. {and returns it if found. Returns nil otherwise.}
  180. function search_macro(const s : string):tsym;
  181. { Additionally to searching for a macro, also checks whether it's still }
  182. { actually defined (could be disable using "undef") }
  183. function defined_macro(const s : string):boolean;
  184. {*** Object Helpers ***}
  185. function search_default_property(pd : tobjectdef) : tpropertysym;
  186. {*** Macro Helpers ***}
  187. {If called initially, the following procedures manipulate macros in }
  188. {initialmacrotable, otherwise they manipulate system macros local to a module.}
  189. {Name can be given in any case (it will be converted to upper case).}
  190. procedure def_system_macro(const name : string);
  191. procedure set_system_macro(const name, value : string);
  192. procedure set_system_compvar(const name, value : string);
  193. procedure undef_system_macro(const name : string);
  194. {*** symtable stack ***}
  195. { $ifdef DEBUG
  196. procedure test_symtablestack;
  197. procedure list_symtablestack;
  198. $endif DEBUG}
  199. {$ifdef UNITALIASES}
  200. type
  201. punit_alias = ^tunit_alias;
  202. tunit_alias = object(TNamedIndexItem)
  203. newname : pshortstring;
  204. constructor init(const n:string);
  205. destructor done;virtual;
  206. end;
  207. var
  208. unitaliases : pdictionary;
  209. procedure addunitalias(const n:string);
  210. function getunitalias(const n:string):string;
  211. {$endif UNITALIASES}
  212. {*** Init / Done ***}
  213. procedure IniTSymtable;
  214. procedure DoneSymtable;
  215. const
  216. overloaded_names : array [NOTOKEN..last_overloaded] of string[16] =
  217. ('error',
  218. 'plus','minus','star','slash','equal',
  219. 'greater','lower','greater_or_equal',
  220. 'lower_or_equal',
  221. 'sym_diff','starstar',
  222. 'as','is','in','or',
  223. 'and','div','mod','not','shl','shr','xor',
  224. 'assign');
  225. implementation
  226. uses
  227. { global }
  228. verbose,globals,
  229. { target }
  230. systems,
  231. { symtable }
  232. symutil,defcmp,defutil,
  233. { module }
  234. fmodule,
  235. { codegen }
  236. procinfo
  237. ;
  238. var
  239. dupnr : longint; { unique number for duplicate symbols }
  240. {*****************************************************************************
  241. TStoredSymtable
  242. *****************************************************************************}
  243. procedure tstoredsymtable.insert(sym:TSymEntry;checkdup:boolean=true);
  244. begin
  245. inherited insert(sym,checkdup);
  246. end;
  247. procedure tstoredsymtable.delete(sym:TSymEntry);
  248. begin
  249. inherited delete(sym);
  250. end;
  251. procedure tstoredsymtable.ppuload(ppufile:tcompilerppufile);
  252. begin
  253. { load definitions }
  254. loaddefs(ppufile);
  255. { load symbols }
  256. loadsyms(ppufile);
  257. end;
  258. procedure tstoredsymtable.ppuwrite(ppufile:tcompilerppufile);
  259. begin
  260. { write definitions }
  261. writedefs(ppufile);
  262. { write symbols }
  263. writesyms(ppufile);
  264. end;
  265. procedure tstoredsymtable.loaddefs(ppufile:tcompilerppufile);
  266. var
  267. def : tdef;
  268. b : byte;
  269. begin
  270. { load start of definition section, which holds the amount of defs }
  271. if ppufile.readentry<>ibstartdefs then
  272. Message(unit_f_ppu_read_error);
  273. { read definitions }
  274. repeat
  275. b:=ppufile.readentry;
  276. case b of
  277. ibpointerdef : def:=tpointerdef.ppuload(ppufile);
  278. ibarraydef : def:=tarraydef.ppuload(ppufile);
  279. iborddef : def:=torddef.ppuload(ppufile);
  280. ibfloatdef : def:=tfloatdef.ppuload(ppufile);
  281. ibprocdef : def:=tprocdef.ppuload(ppufile);
  282. ibshortstringdef : def:=tstringdef.loadshort(ppufile);
  283. iblongstringdef : def:=tstringdef.loadlong(ppufile);
  284. ibansistringdef : def:=tstringdef.loadansi(ppufile);
  285. ibwidestringdef : def:=tstringdef.loadwide(ppufile);
  286. ibunicodestringdef : def:=tstringdef.loadunicode(ppufile);
  287. ibrecorddef : def:=trecorddef.ppuload(ppufile);
  288. ibobjectdef : def:=tobjectdef.ppuload(ppufile);
  289. ibenumdef : def:=tenumdef.ppuload(ppufile);
  290. ibsetdef : def:=tsetdef.ppuload(ppufile);
  291. ibprocvardef : def:=tprocvardef.ppuload(ppufile);
  292. ibfiledef : def:=tfiledef.ppuload(ppufile);
  293. ibclassrefdef : def:=tclassrefdef.ppuload(ppufile);
  294. ibformaldef : def:=tformaldef.ppuload(ppufile);
  295. ibvariantdef : def:=tvariantdef.ppuload(ppufile);
  296. ibundefineddef : def:=tundefineddef.ppuload(ppufile);
  297. ibenddefs : break;
  298. ibend : Message(unit_f_ppu_read_error);
  299. else
  300. Message1(unit_f_ppu_invalid_entry,tostr(b));
  301. end;
  302. InsertDef(def);
  303. until false;
  304. end;
  305. procedure tstoredsymtable.loadsyms(ppufile:tcompilerppufile);
  306. var
  307. b : byte;
  308. sym : tsym;
  309. begin
  310. { load start of definition section, which holds the amount of defs }
  311. if ppufile.readentry<>ibstartsyms then
  312. Message(unit_f_ppu_read_error);
  313. { now read the symbols }
  314. repeat
  315. b:=ppufile.readentry;
  316. case b of
  317. ibtypesym : sym:=ttypesym.ppuload(ppufile);
  318. ibprocsym : sym:=tprocsym.ppuload(ppufile);
  319. ibconstsym : sym:=tconstsym.ppuload(ppufile);
  320. ibstaticvarsym : sym:=tstaticvarsym.ppuload(ppufile);
  321. iblocalvarsym : sym:=tlocalvarsym.ppuload(ppufile);
  322. ibparavarsym : sym:=tparavarsym.ppuload(ppufile);
  323. ibfieldvarsym : sym:=tfieldvarsym.ppuload(ppufile);
  324. ibabsolutevarsym : sym:=tabsolutevarsym.ppuload(ppufile);
  325. ibenumsym : sym:=tenumsym.ppuload(ppufile);
  326. ibpropertysym : sym:=tpropertysym.ppuload(ppufile);
  327. ibunitsym : sym:=tunitsym.ppuload(ppufile);
  328. iblabelsym : sym:=tlabelsym.ppuload(ppufile);
  329. ibsyssym : sym:=tsyssym.ppuload(ppufile);
  330. ibmacrosym : sym:=tmacro.ppuload(ppufile);
  331. ibendsyms : break;
  332. ibend : Message(unit_f_ppu_read_error);
  333. else
  334. Message1(unit_f_ppu_invalid_entry,tostr(b));
  335. end;
  336. Insert(sym,false);
  337. until false;
  338. end;
  339. procedure tstoredsymtable.writedefs(ppufile:tcompilerppufile);
  340. var
  341. i : longint;
  342. def : tstoreddef;
  343. begin
  344. { each definition get a number, write then the amount of defs to the
  345. ibstartdef entry }
  346. ppufile.putlongint(DefList.count);
  347. ppufile.writeentry(ibstartdefs);
  348. { now write the definition }
  349. for i:=0 to DefList.Count-1 do
  350. begin
  351. def:=tstoreddef(DefList[i]);
  352. def.ppuwrite(ppufile);
  353. end;
  354. { write end of definitions }
  355. ppufile.writeentry(ibenddefs);
  356. end;
  357. procedure tstoredsymtable.writesyms(ppufile:tcompilerppufile);
  358. var
  359. i : longint;
  360. sym : Tstoredsym;
  361. begin
  362. { each definition get a number, write then the amount of syms and the
  363. datasize to the ibsymdef entry }
  364. ppufile.putlongint(SymList.count);
  365. ppufile.writeentry(ibstartsyms);
  366. { foreach is used to write all symbols }
  367. for i:=0 to SymList.Count-1 do
  368. begin
  369. sym:=tstoredsym(SymList[i]);
  370. sym.ppuwrite(ppufile);
  371. end;
  372. { end of symbols }
  373. ppufile.writeentry(ibendsyms);
  374. end;
  375. procedure tstoredsymtable.buildderef;
  376. var
  377. i : longint;
  378. def : tstoreddef;
  379. sym : tstoredsym;
  380. begin
  381. { interface definitions }
  382. for i:=0 to DefList.Count-1 do
  383. begin
  384. def:=tstoreddef(DefList[i]);
  385. def.buildderef;
  386. end;
  387. { interface symbols }
  388. for i:=0 to SymList.Count-1 do
  389. begin
  390. sym:=tstoredsym(SymList[i]);
  391. sym.buildderef;
  392. end;
  393. end;
  394. procedure tstoredsymtable.buildderefimpl;
  395. var
  396. i : longint;
  397. def : tstoreddef;
  398. begin
  399. { implementation definitions }
  400. for i:=0 to DefList.Count-1 do
  401. begin
  402. def:=tstoreddef(DefList[i]);
  403. def.buildderefimpl;
  404. end;
  405. end;
  406. procedure tstoredsymtable.deref;
  407. var
  408. i : longint;
  409. def : tstoreddef;
  410. sym : tstoredsym;
  411. begin
  412. { first deref the interface ttype symbols. This is needs
  413. to be done before the interface defs are derefed, because
  414. the interface defs can contain references to the type symbols
  415. which then already need to contain a resolved typedef field (PFV) }
  416. for i:=0 to SymList.Count-1 do
  417. begin
  418. sym:=tstoredsym(SymList[i]);
  419. if sym.typ=typesym then
  420. sym.deref;
  421. end;
  422. { interface definitions }
  423. for i:=0 to DefList.Count-1 do
  424. begin
  425. def:=tstoreddef(DefList[i]);
  426. def.deref;
  427. end;
  428. { interface symbols }
  429. for i:=0 to SymList.Count-1 do
  430. begin
  431. sym:=tstoredsym(SymList[i]);
  432. if sym.typ<>typesym then
  433. sym.deref;
  434. end;
  435. end;
  436. procedure tstoredsymtable.derefimpl;
  437. var
  438. i : longint;
  439. def : tstoreddef;
  440. begin
  441. { implementation definitions }
  442. for i:=0 to DefList.Count-1 do
  443. begin
  444. def:=tstoreddef(DefList[i]);
  445. def.derefimpl;
  446. end;
  447. end;
  448. function tstoredsymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  449. var
  450. hsym : tsym;
  451. begin
  452. hsym:=tsym(FindWithHash(hashedid));
  453. if assigned(hsym) then
  454. DuplicateSym(hashedid,sym,hsym);
  455. result:=assigned(hsym);
  456. end;
  457. {**************************************
  458. Callbacks
  459. **************************************}
  460. procedure TStoredSymtable.check_forward(sym:TObject;arg:pointer);
  461. begin
  462. if tsym(sym).typ=procsym then
  463. tprocsym(sym).check_forward
  464. { check also object method table }
  465. { we needn't to test the def list }
  466. { because each object has to have a type sym,
  467. only test objects declarations, not type renamings }
  468. else
  469. if (tsym(sym).typ=typesym) and
  470. assigned(ttypesym(sym).typedef) and
  471. (ttypesym(sym).typedef.typesym=ttypesym(sym)) and
  472. (ttypesym(sym).typedef.typ=objectdef) then
  473. tobjectdef(ttypesym(sym).typedef).check_forwards;
  474. end;
  475. procedure TStoredSymtable.labeldefined(sym:TObject;arg:pointer);
  476. begin
  477. if (tsym(sym).typ=labelsym) and
  478. not(tlabelsym(sym).defined) then
  479. begin
  480. if tlabelsym(sym).used then
  481. Message1(sym_e_label_used_and_not_defined,tlabelsym(sym).realname)
  482. else
  483. Message1(sym_w_label_not_defined,tlabelsym(sym).realname);
  484. end;
  485. end;
  486. procedure TStoredSymtable.varsymbolused(sym:TObject;arg:pointer);
  487. begin
  488. if (tsym(sym).typ in [staticvarsym,localvarsym,paravarsym,fieldvarsym]) and
  489. ((tsym(sym).owner.symtabletype in
  490. [parasymtable,localsymtable,ObjectSymtable,staticsymtable])) then
  491. begin
  492. { unused symbol should be reported only if no }
  493. { error is reported }
  494. { if the symbol is in a register it is used }
  495. { also don't count the value parameters which have local copies }
  496. { also don't claim for high param of open parameters (PM) }
  497. if (Errorcount<>0) or
  498. ([vo_is_hidden_para,vo_is_funcret] * tabstractvarsym(sym).varoptions = [vo_is_hidden_para]) then
  499. exit;
  500. if (tstoredsym(sym).refs=0) then
  501. begin
  502. if (vo_is_funcret in tabstractvarsym(sym).varoptions) then
  503. begin
  504. { don't warn about the result of constructors }
  505. if ((tsym(sym).owner.symtabletype<>localsymtable) or
  506. (tprocdef(tsym(sym).owner.defowner).proctypeoption<>potype_constructor)) and
  507. not(cs_opt_nodedfa in current_settings.optimizerswitches) then
  508. MessagePos(tsym(sym).fileinfo,sym_w_function_result_not_set)
  509. end
  510. else if (tsym(sym).owner.symtabletype=parasymtable) then
  511. MessagePos1(tsym(sym).fileinfo,sym_h_para_identifier_not_used,tsym(sym).prettyname)
  512. else if (tsym(sym).owner.symtabletype=ObjectSymtable) then
  513. MessagePos2(tsym(sym).fileinfo,sym_n_private_identifier_not_used,tsym(sym).owner.realname^,tsym(sym).prettyname)
  514. else
  515. MessagePos1(tsym(sym).fileinfo,sym_n_local_identifier_not_used,tsym(sym).prettyname);
  516. end
  517. else if tabstractvarsym(sym).varstate in [vs_written,vs_initialised] then
  518. begin
  519. if (tsym(sym).owner.symtabletype=parasymtable) then
  520. begin
  521. if not(tabstractvarsym(sym).varspez in [vs_var,vs_out]) and
  522. not(vo_is_funcret in tabstractvarsym(sym).varoptions) then
  523. MessagePos1(tsym(sym).fileinfo,sym_h_para_identifier_only_set,tsym(sym).prettyname)
  524. end
  525. else if (tsym(sym).owner.symtabletype=ObjectSymtable) then
  526. MessagePos2(tsym(sym).fileinfo,sym_n_private_identifier_only_set,tsym(sym).owner.realname^,tsym(sym).prettyname)
  527. else if tabstractvarsym(sym).varoptions*[vo_is_funcret,vo_is_public,vo_is_external]=[] then
  528. MessagePos1(tsym(sym).fileinfo,sym_n_local_identifier_only_set,tsym(sym).prettyname);
  529. end
  530. else if (tabstractvarsym(sym).varstate = vs_read_not_warned) and
  531. ([vo_is_public,vo_is_external] * tabstractvarsym(sym).varoptions = []) then
  532. MessagePos1(tsym(sym).fileinfo,sym_w_identifier_only_read,tsym(sym).prettyname)
  533. end
  534. else if ((tsym(sym).owner.symtabletype in
  535. [ObjectSymtable,parasymtable,localsymtable,staticsymtable])) then
  536. begin
  537. if (Errorcount<>0) or
  538. (sp_internal in tsym(sym).symoptions) then
  539. exit;
  540. { do not claim for inherited private fields !! }
  541. if (tsym(sym).refs=0) and (tsym(sym).owner.symtabletype=ObjectSymtable) then
  542. MessagePos2(tsym(sym).fileinfo,sym_n_private_method_not_used,tsym(sym).owner.realname^,tsym(sym).prettyname)
  543. { units references are problematic }
  544. else
  545. begin
  546. if (tsym(sym).refs=0) and
  547. not(tsym(sym).typ in [enumsym,unitsym]) and
  548. not(is_funcret_sym(tsym(sym))) and
  549. { don't complain about compiler generated syms for specializations, see also #13405 }
  550. not((tsym(sym).typ=typesym) and (df_specialization in ttypesym(sym).typedef.defoptions) and
  551. (pos('$',ttypesym(sym).Realname)<>0)) and
  552. (
  553. (tsym(sym).typ<>procsym) or
  554. ((tsym(sym).owner.symtabletype=staticsymtable) and
  555. not current_module.is_unit)
  556. ) and
  557. { don't complain about alias for hidden _cmd parameter to
  558. obj-c methods }
  559. not((tsym(sym).typ in [localvarsym,paravarsym,absolutevarsym]) and
  560. (vo_is_msgsel in tabstractvarsym(sym).varoptions)) then
  561. MessagePos2(tsym(sym).fileinfo,sym_h_local_symbol_not_used,SymTypeName[tsym(sym).typ],tsym(sym).prettyname);
  562. end;
  563. end;
  564. end;
  565. procedure TStoredSymtable.TestPrivate(sym:TObject;arg:pointer);
  566. begin
  567. if tsym(sym).visibility=vis_private then
  568. varsymbolused(sym,arg);
  569. end;
  570. procedure TStoredSymtable.objectprivatesymbolused(sym:TObject;arg:pointer);
  571. begin
  572. {
  573. Don't test simple object aliases PM
  574. }
  575. if (tsym(sym).typ=typesym) and
  576. (ttypesym(sym).typedef.typ=objectdef) and
  577. (ttypesym(sym).typedef.typesym=tsym(sym)) then
  578. tobjectdef(ttypesym(sym).typedef).symtable.SymList.ForEachCall(@TestPrivate,nil);
  579. end;
  580. procedure tstoredsymtable.testfordefaultproperty(sym:TObject;arg:pointer);
  581. begin
  582. if (tsym(sym).typ=propertysym) and
  583. (ppo_defaultproperty in tpropertysym(sym).propoptions) then
  584. ppointer(arg)^:=sym;
  585. end;
  586. {***********************************************
  587. Process all entries
  588. ***********************************************}
  589. procedure Tstoredsymtable.reset_all_defs;
  590. var
  591. i : longint;
  592. def : tstoreddef;
  593. begin
  594. for i:=0 to DefList.Count-1 do
  595. begin
  596. def:=tstoreddef(DefList[i]);
  597. def.reset;
  598. end;
  599. end;
  600. { checks, if all procsyms and methods are defined }
  601. procedure tstoredsymtable.check_forwards;
  602. begin
  603. SymList.ForEachCall(@check_forward,nil);
  604. end;
  605. procedure tstoredsymtable.checklabels;
  606. begin
  607. SymList.ForEachCall(@labeldefined,nil);
  608. end;
  609. procedure tstoredsymtable.allsymbolsused;
  610. begin
  611. SymList.ForEachCall(@varsymbolused,nil);
  612. end;
  613. procedure tstoredsymtable.allprivatesused;
  614. begin
  615. SymList.ForEachCall(@objectprivatesymbolused,nil);
  616. end;
  617. procedure TStoredSymtable._needs_init_final(sym:TObject;arg:pointer);
  618. begin
  619. if b_needs_init_final then
  620. exit;
  621. case tsym(sym).typ of
  622. fieldvarsym,
  623. staticvarsym,
  624. localvarsym,
  625. paravarsym :
  626. begin
  627. if not(is_class(tabstractvarsym(sym).vardef)) and
  628. tstoreddef(tabstractvarsym(sym).vardef).needs_inittable then
  629. b_needs_init_final:=true;
  630. end;
  631. end;
  632. end;
  633. { returns true, if p contains data which needs init/final code }
  634. function tstoredsymtable.needs_init_final : boolean;
  635. begin
  636. b_needs_init_final:=false;
  637. SymList.ForEachCall(@_needs_init_final,nil);
  638. needs_init_final:=b_needs_init_final;
  639. end;
  640. {****************************************************************************
  641. TAbstractRecordSymtable
  642. ****************************************************************************}
  643. constructor tabstractrecordsymtable.create(const n:string;usealign:shortint);
  644. begin
  645. inherited create(n);
  646. _datasize:=0;
  647. databitsize:=0;
  648. recordalignment:=1;
  649. usefieldalignment:=usealign;
  650. padalignment:=1;
  651. { recordalign C_alignment means C record packing, that starts
  652. with an alignment of 1 }
  653. case usealign of
  654. C_alignment,
  655. bit_alignment:
  656. fieldalignment:=1
  657. else
  658. fieldalignment:=usealign;
  659. end;
  660. end;
  661. procedure tabstractrecordsymtable.ppuload(ppufile:tcompilerppufile);
  662. begin
  663. inherited ppuload(ppufile);
  664. end;
  665. procedure tabstractrecordsymtable.ppuwrite(ppufile:tcompilerppufile);
  666. var
  667. oldtyp : byte;
  668. begin
  669. oldtyp:=ppufile.entrytyp;
  670. ppufile.entrytyp:=subentryid;
  671. inherited ppuwrite(ppufile);
  672. ppufile.entrytyp:=oldtyp;
  673. end;
  674. function field2recordalignment(fieldoffs, fieldalign: aint): aint;
  675. begin
  676. { optimal alignment of the record when declaring a variable of this }
  677. { type is independent of the packrecords setting }
  678. if (fieldoffs mod fieldalign) = 0 then
  679. result:=fieldalign
  680. else if (fieldalign >= 16) and
  681. ((fieldoffs mod 16) = 0) and
  682. ((fieldalign mod 16) = 0) then
  683. result:=16
  684. else if (fieldalign >= 8) and
  685. ((fieldoffs mod 8) = 0) and
  686. ((fieldalign mod 8) = 0) then
  687. result:=8
  688. else if (fieldalign >= 4) and
  689. ((fieldoffs mod 4) = 0) and
  690. ((fieldalign mod 4) = 0) then
  691. result:=4
  692. else if (fieldalign >= 2) and
  693. ((fieldoffs mod 2) = 0) and
  694. ((fieldalign mod 2) = 0) then
  695. result:=2
  696. else
  697. result:=1;
  698. end;
  699. procedure tabstractrecordsymtable.alignrecord(fieldoffset:aint;varalign:shortint);
  700. var
  701. varalignrecord: shortint;
  702. begin
  703. if (usefieldalignment=C_alignment) then
  704. varalignrecord:=used_align(varalign,current_settings.alignment.recordalignmin,current_settings.alignment.maxCrecordalign)
  705. else
  706. varalignrecord:=field2recordalignment(fieldoffset,varalign);
  707. recordalignment:=max(recordalignment,varalignrecord);
  708. end;
  709. procedure tabstractrecordsymtable.addfield(sym:tfieldvarsym;vis:tvisibility);
  710. var
  711. l : aint;
  712. varalignfield,
  713. varalign : shortint;
  714. vardef : tdef;
  715. begin
  716. if (sym.owner<>self) then
  717. internalerror(200602031);
  718. if sym.fieldoffset<>-1 then
  719. internalerror(200602032);
  720. { set visibility for the symbol }
  721. sym.visibility:=vis;
  722. { this symbol can't be loaded to a register }
  723. sym.varregable:=vr_none;
  724. { Calculate field offset }
  725. l:=sym.getsize;
  726. vardef:=sym.vardef;
  727. varalign:=vardef.alignment;
  728. if (usefieldalignment=bit_alignment) then
  729. begin
  730. { bitpacking only happens for ordinals, the rest is aligned at }
  731. { 1 byte (compatible with GPC/GCC) }
  732. if is_ordinal(vardef) then
  733. begin
  734. sym.fieldoffset:=databitsize;
  735. l:=sym.getpackedbitsize;
  736. end
  737. else
  738. begin
  739. databitsize:=_datasize*8;
  740. sym.fieldoffset:=databitsize;
  741. if (l>high(aint) div 8) then
  742. Message(sym_e_segment_too_large);
  743. l:=l*8;
  744. end;
  745. if varalign=0 then
  746. varalign:=size_2_align(l);
  747. recordalignment:=max(recordalignment,field2recordalignment(databitsize mod 8,varalign));
  748. { bit packed records are limited to high(aint) bits }
  749. { instead of bytes to avoid double precision }
  750. { arithmetic in offset calculations }
  751. if int64(l)>high(aint)-sym.fieldoffset then
  752. begin
  753. Message(sym_e_segment_too_large);
  754. _datasize:=high(aint);
  755. databitsize:=high(aint);
  756. end
  757. else
  758. begin
  759. databitsize:=sym.fieldoffset+l;
  760. _datasize:=(databitsize+7) div 8;
  761. end;
  762. { rest is not applicable }
  763. exit;
  764. end;
  765. { Calc the alignment size for C style records }
  766. if (usefieldalignment=C_alignment) then
  767. begin
  768. if (varalign>4) and
  769. ((varalign mod 4)<>0) and
  770. (vardef.typ=arraydef) then
  771. Message1(sym_w_wrong_C_pack,vardef.typename);
  772. if varalign=0 then
  773. varalign:=l;
  774. if (fieldalignment<current_settings.alignment.maxCrecordalign) then
  775. begin
  776. if (varalign>16) and (fieldalignment<32) then
  777. fieldalignment:=32
  778. else if (varalign>12) and (fieldalignment<16) then
  779. fieldalignment:=16
  780. { 12 is needed for long double }
  781. else if (varalign>8) and (fieldalignment<12) then
  782. fieldalignment:=12
  783. else if (varalign>4) and (fieldalignment<8) then
  784. fieldalignment:=8
  785. else if (varalign>2) and (fieldalignment<4) then
  786. fieldalignment:=4
  787. else if (varalign>1) and (fieldalignment<2) then
  788. fieldalignment:=2;
  789. end;
  790. fieldalignment:=min(fieldalignment,current_settings.alignment.maxCrecordalign);
  791. end;
  792. if varalign=0 then
  793. varalign:=size_2_align(l);
  794. varalignfield:=used_align(varalign,current_settings.alignment.recordalignmin,fieldalignment);
  795. sym.fieldoffset:=align(_datasize,varalignfield);
  796. if l>high(aint)-sym.fieldoffset then
  797. begin
  798. Message(sym_e_segment_too_large);
  799. _datasize:=high(aint);
  800. end
  801. else
  802. _datasize:=sym.fieldoffset+l;
  803. { Calc alignment needed for this record }
  804. alignrecord(sym.fieldoffset,varalign);
  805. end;
  806. procedure tabstractrecordsymtable.addalignmentpadding;
  807. begin
  808. { make the record size aligned correctly so it can be
  809. used as elements in an array. For C records we
  810. use the fieldalignment, because that is updated with the
  811. used alignment. }
  812. if (padalignment = 1) then
  813. case usefieldalignment of
  814. C_alignment:
  815. padalignment:=fieldalignment;
  816. { bitpacked }
  817. bit_alignment:
  818. padalignment:=1;
  819. { default/no packrecords specified }
  820. 0:
  821. padalignment:=recordalignment
  822. { specific packrecords setting -> use as upper limit }
  823. else
  824. padalignment:=min(recordalignment,usefieldalignment);
  825. end;
  826. _datasize:=align(_datasize,padalignment);
  827. end;
  828. procedure tabstractrecordsymtable.insertdef(def:TDefEntry);
  829. begin
  830. { Enums must also be available outside the record scope,
  831. insert in the owner of this symtable }
  832. if def.typ=enumdef then
  833. defowner.owner.insertdef(def)
  834. else
  835. inherited insertdef(def);
  836. end;
  837. function tabstractrecordsymtable.is_packed: boolean;
  838. begin
  839. result:=usefieldalignment=bit_alignment;
  840. end;
  841. procedure tabstractrecordsymtable.setdatasize(val: aint);
  842. begin
  843. _datasize:=val;
  844. if (usefieldalignment=bit_alignment) then
  845. { can overflow in non bitpacked records }
  846. databitsize:=val*8;
  847. end;
  848. {****************************************************************************
  849. TRecordSymtable
  850. ****************************************************************************}
  851. constructor trecordsymtable.create(usealign:shortint);
  852. begin
  853. inherited create('',usealign);
  854. symtabletype:=recordsymtable;
  855. end;
  856. { this procedure is reserved for inserting case variant into
  857. a record symtable }
  858. { the offset is the location of the start of the variant
  859. and datasize and dataalignment corresponds to
  860. the complete size (see code in pdecl unit) PM }
  861. procedure trecordsymtable.insertunionst(unionst : trecordsymtable;offset : longint);
  862. var
  863. sym : tsym;
  864. def : tdef;
  865. i : integer;
  866. varalignrecord,varalign,
  867. storesize,storealign : aint;
  868. bitsize: aint;
  869. begin
  870. storesize:=_datasize;
  871. storealign:=fieldalignment;
  872. _datasize:=offset;
  873. if (usefieldalignment=bit_alignment) then
  874. databitsize:=offset*8;
  875. { We move the ownership of the defs and symbols to the new recordsymtable.
  876. The old unionsymtable keeps the references, but doesn't own the
  877. objects anymore }
  878. unionst.DefList.OwnsObjects:=false;
  879. unionst.SymList.OwnsObjects:=false;
  880. { copy symbols }
  881. for i:=0 to unionst.SymList.Count-1 do
  882. begin
  883. sym:=TSym(unionst.SymList[i]);
  884. if sym.typ<>fieldvarsym then
  885. internalerror(200601272);
  886. { add to this record symtable }
  887. // unionst.SymList.List.List^[i].Data:=nil;
  888. sym.ChangeOwner(self);
  889. varalign:=tfieldvarsym(sym).vardef.alignment;
  890. if varalign=0 then
  891. varalign:=size_2_align(tfieldvarsym(sym).getsize);
  892. { retrieve size }
  893. if (usefieldalignment=bit_alignment) then
  894. begin
  895. { bit packed records are limited to high(aint) bits }
  896. { instead of bytes to avoid double precision }
  897. { arithmetic in offset calculations }
  898. if is_ordinal(tfieldvarsym(sym).vardef) then
  899. bitsize:=tfieldvarsym(sym).getpackedbitsize
  900. else
  901. begin
  902. bitsize:=tfieldvarsym(sym).getsize;
  903. if (bitsize>high(aint) div 8) then
  904. Message(sym_e_segment_too_large);
  905. bitsize:=bitsize*8;
  906. end;
  907. if bitsize>high(aint)-databitsize then
  908. begin
  909. Message(sym_e_segment_too_large);
  910. _datasize:=high(aint);
  911. databitsize:=high(aint);
  912. end
  913. else
  914. begin
  915. databitsize:=tfieldvarsym(sym).fieldoffset+offset*8;
  916. _datasize:=(databitsize+7) div 8;
  917. end;
  918. tfieldvarsym(sym).fieldoffset:=databitsize;
  919. varalignrecord:=field2recordalignment(tfieldvarsym(sym).fieldoffset div 8,varalign);
  920. end
  921. else
  922. begin
  923. if tfieldvarsym(sym).getsize>high(aint)-_datasize then
  924. begin
  925. Message(sym_e_segment_too_large);
  926. _datasize:=high(aint);
  927. end
  928. else
  929. _datasize:=tfieldvarsym(sym).fieldoffset+offset;
  930. { update address }
  931. tfieldvarsym(sym).fieldoffset:=_datasize;
  932. varalignrecord:=field2recordalignment(tfieldvarsym(sym).fieldoffset,varalign);
  933. end;
  934. { update alignment of this record }
  935. if (usefieldalignment<>C_alignment) then
  936. recordalignment:=max(recordalignment,varalignrecord);
  937. end;
  938. { update alignment for C records }
  939. if (usefieldalignment=C_alignment) then
  940. recordalignment:=max(recordalignment,unionst.recordalignment);
  941. { Register defs in the new record symtable }
  942. for i:=0 to unionst.DefList.Count-1 do
  943. begin
  944. def:=TDef(unionst.DefList[i]);
  945. def.ChangeOwner(self);
  946. end;
  947. _datasize:=storesize;
  948. fieldalignment:=storealign;
  949. end;
  950. {****************************************************************************
  951. TObjectSymtable
  952. ****************************************************************************}
  953. constructor tObjectSymtable.create(adefowner:tdef;const n:string;usealign:shortint);
  954. begin
  955. inherited create(n,usealign);
  956. symtabletype:=ObjectSymtable;
  957. defowner:=adefowner;
  958. end;
  959. function tObjectSymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  960. var
  961. hsym : tsym;
  962. begin
  963. result:=false;
  964. if not assigned(defowner) then
  965. internalerror(200602061);
  966. { procsym and propertysym have special code
  967. to override values in inherited classes. For other
  968. symbols check for duplicates }
  969. if not(sym.typ in [procsym,propertysym]) then
  970. begin
  971. { but private ids can be reused }
  972. hsym:=search_class_member(tobjectdef(defowner),hashedid.id);
  973. if assigned(hsym) and
  974. (
  975. (
  976. not(m_delphi in current_settings.modeswitches) and
  977. is_visible_for_object(hsym,tobjectdef(defowner))
  978. ) or
  979. (
  980. { In Delphi, you can repeat members of a parent class. You can't }
  981. { do this for objects however, and you (obviouly) can't }
  982. { declare two fields with the same name in a single class }
  983. (m_delphi in current_settings.modeswitches) and
  984. (
  985. is_object(tdef(defowner)) or
  986. (hsym.owner = self)
  987. )
  988. )
  989. ) then
  990. begin
  991. DuplicateSym(hashedid,sym,hsym);
  992. result:=true;
  993. end;
  994. end
  995. else
  996. begin
  997. if not(m_duplicate_names in current_settings.modeswitches) then
  998. result:=inherited checkduplicate(hashedid,sym);
  999. end;
  1000. end;
  1001. {****************************************************************************
  1002. TAbstractLocalSymtable
  1003. ****************************************************************************}
  1004. procedure tabstractlocalsymtable.ppuwrite(ppufile:tcompilerppufile);
  1005. var
  1006. oldtyp : byte;
  1007. begin
  1008. oldtyp:=ppufile.entrytyp;
  1009. ppufile.entrytyp:=subentryid;
  1010. { write definitions }
  1011. writedefs(ppufile);
  1012. { write symbols }
  1013. writesyms(ppufile);
  1014. ppufile.entrytyp:=oldtyp;
  1015. end;
  1016. function tabstractlocalsymtable.count_locals:longint;
  1017. var
  1018. i : longint;
  1019. sym : tsym;
  1020. begin
  1021. result:=0;
  1022. for i:=0 to SymList.Count-1 do
  1023. begin
  1024. sym:=tsym(SymList[i]);
  1025. { Count only varsyms, but ignore the funcretsym }
  1026. if (tsym(sym).typ in [localvarsym,paravarsym]) and
  1027. (tsym(sym)<>current_procinfo.procdef.funcretsym) and
  1028. (not(vo_is_parentfp in tabstractvarsym(sym).varoptions) or
  1029. (tstoredsym(sym).refs>0)) then
  1030. inc(result);
  1031. end;
  1032. end;
  1033. {****************************************************************************
  1034. TLocalSymtable
  1035. ****************************************************************************}
  1036. constructor tlocalsymtable.create(adefowner:tdef;level:byte);
  1037. begin
  1038. inherited create('');
  1039. defowner:=adefowner;
  1040. symtabletype:=localsymtable;
  1041. symtablelevel:=level;
  1042. end;
  1043. function tlocalsymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  1044. var
  1045. hsym : tsym;
  1046. begin
  1047. if not assigned(defowner) or
  1048. (defowner.typ<>procdef) then
  1049. internalerror(200602042);
  1050. result:=false;
  1051. hsym:=tsym(FindWithHash(hashedid));
  1052. if assigned(hsym) then
  1053. begin
  1054. { a local and the function can have the same
  1055. name in TP and Delphi, but RESULT not }
  1056. if (m_duplicate_names in current_settings.modeswitches) and
  1057. (hsym.typ in [absolutevarsym,localvarsym]) and
  1058. (vo_is_funcret in tabstractvarsym(hsym).varoptions) and
  1059. not((m_result in current_settings.modeswitches) and
  1060. (vo_is_result in tabstractvarsym(hsym).varoptions)) then
  1061. HideSym(hsym)
  1062. else
  1063. DuplicateSym(hashedid,sym,hsym);
  1064. result:=true;
  1065. exit;
  1066. end;
  1067. { check also parasymtable, this needs to be done here becuase
  1068. of the special situation with the funcret sym that needs to be
  1069. hidden for tp and delphi modes }
  1070. hsym:=tsym(tabstractprocdef(defowner).parast.FindWithHash(hashedid));
  1071. if assigned(hsym) then
  1072. begin
  1073. { a local and the function can have the same
  1074. name in TP and Delphi, but RESULT not }
  1075. if (m_duplicate_names in current_settings.modeswitches) and
  1076. (sym.typ in [absolutevarsym,localvarsym]) and
  1077. (vo_is_funcret in tabstractvarsym(sym).varoptions) and
  1078. not((m_result in current_settings.modeswitches) and
  1079. (vo_is_result in tabstractvarsym(sym).varoptions)) then
  1080. Hidesym(sym)
  1081. else
  1082. DuplicateSym(hashedid,sym,hsym);
  1083. result:=true;
  1084. exit;
  1085. end;
  1086. { check ObjectSymtable, skip this for funcret sym because
  1087. that will always be positive because it has the same name
  1088. as the procsym }
  1089. if not is_funcret_sym(sym) and
  1090. (defowner.typ=procdef) and
  1091. assigned(tprocdef(defowner)._class) and
  1092. (tprocdef(defowner).owner.defowner=tprocdef(defowner)._class) and
  1093. (
  1094. not(m_delphi in current_settings.modeswitches) or
  1095. is_object(tprocdef(defowner)._class)
  1096. ) then
  1097. result:=tprocdef(defowner)._class.symtable.checkduplicate(hashedid,sym);
  1098. end;
  1099. {****************************************************************************
  1100. TParaSymtable
  1101. ****************************************************************************}
  1102. constructor tparasymtable.create(adefowner:tdef;level:byte);
  1103. begin
  1104. inherited create('');
  1105. defowner:=adefowner;
  1106. symtabletype:=parasymtable;
  1107. symtablelevel:=level;
  1108. end;
  1109. function tparasymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  1110. begin
  1111. result:=inherited checkduplicate(hashedid,sym);
  1112. if result then
  1113. exit;
  1114. if not(m_duplicate_names in current_settings.modeswitches) and
  1115. (defowner.typ=procdef) and
  1116. assigned(tprocdef(defowner)._class) and
  1117. (tprocdef(defowner).owner.defowner=tprocdef(defowner)._class) and
  1118. (
  1119. not(m_delphi in current_settings.modeswitches) or
  1120. is_object(tprocdef(defowner)._class)
  1121. ) then
  1122. result:=tprocdef(defowner)._class.symtable.checkduplicate(hashedid,sym);
  1123. end;
  1124. {****************************************************************************
  1125. TAbstractUniTSymtable
  1126. ****************************************************************************}
  1127. constructor tabstractuniTSymtable.create(const n : string;id:word);
  1128. begin
  1129. inherited create(n);
  1130. moduleid:=id;
  1131. end;
  1132. function tabstractuniTSymtable.iscurrentunit:boolean;
  1133. begin
  1134. result:=assigned(current_module) and
  1135. (
  1136. (current_module.globalsymtable=self) or
  1137. (current_module.localsymtable=self)
  1138. );
  1139. end;
  1140. {****************************************************************************
  1141. TStaticSymtable
  1142. ****************************************************************************}
  1143. constructor tstaticsymtable.create(const n : string;id:word);
  1144. begin
  1145. inherited create(n,id);
  1146. symtabletype:=staticsymtable;
  1147. symtablelevel:=main_program_level;
  1148. end;
  1149. procedure tstaticsymtable.ppuload(ppufile:tcompilerppufile);
  1150. begin
  1151. inherited ppuload(ppufile);
  1152. { now we can deref the syms and defs }
  1153. deref;
  1154. end;
  1155. procedure tstaticsymtable.ppuwrite(ppufile:tcompilerppufile);
  1156. begin
  1157. inherited ppuwrite(ppufile);
  1158. end;
  1159. function tstaticsymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  1160. var
  1161. hsym : tsym;
  1162. begin
  1163. result:=false;
  1164. hsym:=tsym(FindWithHash(hashedid));
  1165. if assigned(hsym) then
  1166. begin
  1167. { Delphi (contrary to TP) you can have a symbol with the same name as the
  1168. unit, the unit can then not be accessed anymore using
  1169. <unit>.<id>, so we can hide the symbol }
  1170. if (m_delphi in current_settings.modeswitches) and
  1171. (hsym.typ=symconst.unitsym) then
  1172. HideSym(hsym)
  1173. else
  1174. DuplicateSym(hashedid,sym,hsym);
  1175. result:=true;
  1176. exit;
  1177. end;
  1178. if (current_module.localsymtable=self) and
  1179. assigned(current_module.globalsymtable) then
  1180. result:=tglobalsymtable(current_module.globalsymtable).checkduplicate(hashedid,sym);
  1181. end;
  1182. {****************************************************************************
  1183. TGlobalSymtable
  1184. ****************************************************************************}
  1185. constructor tglobalsymtable.create(const n : string;id:word);
  1186. begin
  1187. inherited create(n,id);
  1188. symtabletype:=globalsymtable;
  1189. symtablelevel:=main_program_level;
  1190. end;
  1191. procedure tglobalsymtable.ppuload(ppufile:tcompilerppufile);
  1192. begin
  1193. inherited ppuload(ppufile);
  1194. { now we can deref the syms and defs }
  1195. deref;
  1196. end;
  1197. procedure tglobalsymtable.ppuwrite(ppufile:tcompilerppufile);
  1198. begin
  1199. { write the symtable entries }
  1200. inherited ppuwrite(ppufile);
  1201. end;
  1202. function tglobalsymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  1203. var
  1204. hsym : tsym;
  1205. begin
  1206. result:=false;
  1207. hsym:=tsym(FindWithHash(hashedid));
  1208. if assigned(hsym) then
  1209. begin
  1210. { Delphi (contrary to TP) you can have a symbol with the same name as the
  1211. unit, the unit can then not be accessed anymore using
  1212. <unit>.<id>, so we can hide the symbol }
  1213. if (m_delphi in current_settings.modeswitches) and
  1214. (hsym.typ=symconst.unitsym) then
  1215. HideSym(hsym)
  1216. else
  1217. DuplicateSym(hashedid,sym,hsym);
  1218. result:=true;
  1219. exit;
  1220. end;
  1221. end;
  1222. {****************************************************************************
  1223. TWITHSYMTABLE
  1224. ****************************************************************************}
  1225. constructor twithsymtable.create(aowner:tdef;ASymList:TFPHashObjectList;refnode:tobject{tnode});
  1226. begin
  1227. inherited create('');
  1228. symtabletype:=withsymtable;
  1229. withrefnode:=refnode;
  1230. { Replace SymList with the passed symlist }
  1231. SymList.free;
  1232. SymList:=ASymList;
  1233. defowner:=aowner;
  1234. end;
  1235. destructor twithsymtable.destroy;
  1236. begin
  1237. withrefnode.free;
  1238. { Disable SymList because we don't Own it }
  1239. SymList:=nil;
  1240. inherited destroy;
  1241. end;
  1242. procedure twithsymtable.clear;
  1243. begin
  1244. { remove no entry from a withsymtable as it is only a pointer to the
  1245. recorddef or objectdef symtable }
  1246. end;
  1247. procedure twithsymtable.insertdef(def:TDefEntry);
  1248. begin
  1249. { Definitions can't be registered in the withsymtable
  1250. because the withsymtable is removed after the with block.
  1251. We can't easily solve it here because the next symtable in the
  1252. stack is not known. }
  1253. internalerror(200602046);
  1254. end;
  1255. {****************************************************************************
  1256. TSTT_ExceptionSymtable
  1257. ****************************************************************************}
  1258. constructor tstt_excepTSymtable.create;
  1259. begin
  1260. inherited create('');
  1261. symtabletype:=stt_excepTSymtable;
  1262. end;
  1263. {****************************************************************************
  1264. TMacroSymtable
  1265. ****************************************************************************}
  1266. constructor tmacrosymtable.create(exported: boolean);
  1267. begin
  1268. inherited create('');
  1269. if exported then
  1270. symtabletype:=exportedmacrosymtable
  1271. else
  1272. symtabletype:=localmacrosymtable;
  1273. symtablelevel:=main_program_level;
  1274. end;
  1275. {*****************************************************************************
  1276. Helper Routines
  1277. *****************************************************************************}
  1278. function FullTypeName(def,otherdef:tdef):string;
  1279. var
  1280. s1,s2 : string;
  1281. begin
  1282. s1:=def.typename;
  1283. { When the names are the same try to include the unit name }
  1284. if assigned(otherdef) and
  1285. (def.owner.symtabletype in [globalsymtable,staticsymtable]) then
  1286. begin
  1287. s2:=otherdef.typename;
  1288. if upper(s1)=upper(s2) then
  1289. s1:=def.owner.realname^+'.'+s1;
  1290. end;
  1291. FullTypeName:=s1;
  1292. end;
  1293. procedure incompatibletypes(def1,def2:tdef);
  1294. begin
  1295. { When there is an errordef there is already an error message show }
  1296. if (def2.typ=errordef) or
  1297. (def1.typ=errordef) then
  1298. exit;
  1299. CGMessage2(type_e_incompatible_types,FullTypeName(def1,def2),FullTypeName(def2,def1));
  1300. end;
  1301. procedure hidesym(sym:TSymEntry);
  1302. begin
  1303. sym.realname:='$hidden'+sym.realname;
  1304. tsym(sym).visibility:=vis_hidden;
  1305. end;
  1306. procedure duplicatesym(var hashedid:THashedIDString;dupsym,origsym:TSymEntry);
  1307. var
  1308. st : TSymtable;
  1309. begin
  1310. Message1(sym_e_duplicate_id,tsym(origsym).realname);
  1311. { Write hint where the original symbol was found }
  1312. st:=finduniTSymtable(origsym.owner);
  1313. with tsym(origsym).fileinfo do
  1314. begin
  1315. if assigned(st) and
  1316. (st.symtabletype=globalsymtable) and
  1317. st.iscurrentunit then
  1318. Message2(sym_h_duplicate_id_where,current_module.sourcefiles.get_file_name(fileindex),tostr(line))
  1319. else if assigned(st.name) then
  1320. Message2(sym_h_duplicate_id_where,'unit '+st.name^,tostr(line));
  1321. end;
  1322. { Rename duplicate sym to an unreachable name, but it can be
  1323. inserted in the symtable without errors }
  1324. inc(dupnr);
  1325. hashedid.id:='dup'+tostr(dupnr)+hashedid.id;
  1326. if assigned(dupsym) then
  1327. include(tsym(dupsym).symoptions,sp_implicitrename);
  1328. end;
  1329. {*****************************************************************************
  1330. Search
  1331. *****************************************************************************}
  1332. procedure addsymref(sym:tsym);
  1333. begin
  1334. { symbol uses count }
  1335. sym.IncRefCount;
  1336. { unit uses count }
  1337. if assigned(current_module) and
  1338. (sym.owner.symtabletype=globalsymtable) then
  1339. begin
  1340. if tglobalsymtable(sym.owner).moduleid>=current_module.unitmapsize then
  1341. internalerror(200501152);
  1342. inc(current_module.unitmap[tglobalsymtable(sym.owner).moduleid].refs);
  1343. end;
  1344. end;
  1345. function is_visible_for_object(symst:tsymtable;symvisibility:tvisibility;contextobjdef:tobjectdef):boolean;
  1346. var
  1347. symownerdef : tobjectdef;
  1348. begin
  1349. result:=false;
  1350. { Get objdectdef owner of the symtable for the is_related checks }
  1351. if not assigned(symst) or
  1352. (symst.symtabletype<>objectsymtable) then
  1353. internalerror(200810285);
  1354. symownerdef:=tobjectdef(symst.defowner);
  1355. case symvisibility of
  1356. vis_private :
  1357. begin
  1358. { private symbols are allowed when we are in the same
  1359. module as they are defined }
  1360. result:=(symownerdef.owner.symtabletype in [globalsymtable,staticsymtable]) and
  1361. (symownerdef.owner.iscurrentunit);
  1362. end;
  1363. vis_strictprivate :
  1364. begin
  1365. result:=assigned(current_objectdef) and
  1366. (current_objectdef=symownerdef);
  1367. end;
  1368. vis_strictprotected :
  1369. begin
  1370. result:=assigned(current_objectdef) and
  1371. current_objectdef.is_related(symownerdef);
  1372. end;
  1373. vis_protected :
  1374. begin
  1375. { protected symbols are visible in the module that defines them and
  1376. also visible to related objects. The related object must be defined
  1377. in the current module }
  1378. result:=(
  1379. (
  1380. (symownerdef.owner.symtabletype in [globalsymtable,staticsymtable]) and
  1381. (symownerdef.owner.iscurrentunit)
  1382. ) or
  1383. (
  1384. assigned(contextobjdef) and
  1385. (contextobjdef.owner.symtabletype in [globalsymtable,staticsymtable]) and
  1386. (contextobjdef.owner.iscurrentunit) and
  1387. contextobjdef.is_related(symownerdef)
  1388. )
  1389. );
  1390. end;
  1391. vis_public,
  1392. vis_published :
  1393. result:=true;
  1394. end;
  1395. end;
  1396. function is_visible_for_object(pd:tprocdef;contextobjdef:tobjectdef):boolean;
  1397. begin
  1398. result:=is_visible_for_object(pd.owner,pd.visibility,contextobjdef);
  1399. end;
  1400. function is_visible_for_object(sym:tsym;contextobjdef:tobjectdef):boolean;
  1401. var
  1402. i : longint;
  1403. pd : tprocdef;
  1404. begin
  1405. if sym.typ=procsym then
  1406. begin
  1407. { A procsym is visible, when there is at least one of the procdefs visible }
  1408. result:=false;
  1409. for i:=0 to tprocsym(sym).ProcdefList.Count-1 do
  1410. begin
  1411. pd:=tprocdef(tprocsym(sym).ProcdefList[i]);
  1412. if (pd.owner=sym.owner) and
  1413. is_visible_for_object(pd,contextobjdef) then
  1414. begin
  1415. result:=true;
  1416. exit;
  1417. end;
  1418. end;
  1419. end
  1420. else
  1421. result:=is_visible_for_object(sym.owner,sym.visibility,contextobjdef);
  1422. end;
  1423. function searchsym(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  1424. var
  1425. hashedid : THashedIDString;
  1426. contextobjdef : tobjectdef;
  1427. stackitem : psymtablestackitem;
  1428. begin
  1429. result:=false;
  1430. hashedid.id:=s;
  1431. stackitem:=symtablestack.stack;
  1432. while assigned(stackitem) do
  1433. begin
  1434. srsymtable:=stackitem^.symtable;
  1435. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  1436. if assigned(srsym) then
  1437. begin
  1438. { use the class from withsymtable only when it is
  1439. defined in this unit }
  1440. if (srsymtable.symtabletype=withsymtable) and
  1441. assigned(srsymtable.defowner) and
  1442. (srsymtable.defowner.typ=objectdef) and
  1443. (srsymtable.defowner.owner.symtabletype in [globalsymtable,staticsymtable]) and
  1444. (srsymtable.defowner.owner.iscurrentunit) then
  1445. contextobjdef:=tobjectdef(srsymtable.defowner)
  1446. else
  1447. contextobjdef:=current_objectdef;
  1448. if (srsym.owner.symtabletype<>objectsymtable) or
  1449. is_visible_for_object(srsym,contextobjdef) then
  1450. begin
  1451. { we need to know if a procedure references symbols
  1452. in the static symtable, because then it can't be
  1453. inlined from outside this unit }
  1454. if assigned(current_procinfo) and
  1455. (srsym.owner.symtabletype=staticsymtable) then
  1456. include(current_procinfo.flags,pi_uses_static_symtable);
  1457. addsymref(srsym);
  1458. result:=true;
  1459. exit;
  1460. end;
  1461. end;
  1462. stackitem:=stackitem^.next;
  1463. end;
  1464. srsym:=nil;
  1465. srsymtable:=nil;
  1466. end;
  1467. function searchsym_type(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  1468. var
  1469. hashedid : THashedIDString;
  1470. stackitem : psymtablestackitem;
  1471. begin
  1472. result:=false;
  1473. hashedid.id:=s;
  1474. stackitem:=symtablestack.stack;
  1475. while assigned(stackitem) do
  1476. begin
  1477. {
  1478. It is not possible to have type symbols in:
  1479. records
  1480. objects
  1481. parameters
  1482. Exception are generic definitions and specializations
  1483. that have the parameterized types inserted in the symtable.
  1484. }
  1485. srsymtable:=stackitem^.symtable;
  1486. if not(srsymtable.symtabletype in [recordsymtable,ObjectSymtable,parasymtable]) or
  1487. (assigned(srsymtable.defowner) and
  1488. (
  1489. (df_generic in tdef(srsymtable.defowner).defoptions) or
  1490. (df_specialization in tdef(srsymtable.defowner).defoptions))
  1491. ) then
  1492. begin
  1493. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  1494. if assigned(srsym) and
  1495. not(srsym.typ in [fieldvarsym,paravarsym]) and
  1496. (
  1497. (srsym.owner.symtabletype<>objectsymtable) or
  1498. is_visible_for_object(srsym,current_objectdef)
  1499. ) then
  1500. begin
  1501. { we need to know if a procedure references symbols
  1502. in the static symtable, because then it can't be
  1503. inlined from outside this unit }
  1504. if assigned(current_procinfo) and
  1505. (srsym.owner.symtabletype=staticsymtable) then
  1506. include(current_procinfo.flags,pi_uses_static_symtable);
  1507. addsymref(srsym);
  1508. result:=true;
  1509. exit;
  1510. end;
  1511. end;
  1512. stackitem:=stackitem^.next;
  1513. end;
  1514. result:=false;
  1515. srsym:=nil;
  1516. srsymtable:=nil;
  1517. end;
  1518. function searchsym_in_module(pm:pointer;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  1519. var
  1520. pmod : tmodule;
  1521. begin
  1522. pmod:=tmodule(pm);
  1523. result:=false;
  1524. if assigned(pmod.globalsymtable) then
  1525. begin
  1526. srsym:=tsym(pmod.globalsymtable.Find(s));
  1527. if assigned(srsym) then
  1528. begin
  1529. srsymtable:=pmod.globalsymtable;
  1530. addsymref(srsym);
  1531. result:=true;
  1532. exit;
  1533. end;
  1534. end;
  1535. { If the module is the current unit we also need
  1536. to search the local symtable }
  1537. if (pmod=current_module) and
  1538. assigned(pmod.localsymtable) then
  1539. begin
  1540. srsym:=tsym(pmod.localsymtable.Find(s));
  1541. if assigned(srsym) then
  1542. begin
  1543. srsymtable:=pmod.localsymtable;
  1544. addsymref(srsym);
  1545. result:=true;
  1546. exit;
  1547. end;
  1548. end;
  1549. srsym:=nil;
  1550. srsymtable:=nil;
  1551. end;
  1552. function searchsym_in_named_module(const unitname, symname: TIDString; out srsym: tsym; out srsymtable: tsymtable): boolean;
  1553. var
  1554. stackitem : psymtablestackitem;
  1555. begin
  1556. result:=false;
  1557. stackitem:=symtablestack.stack;
  1558. while assigned(stackitem) do
  1559. begin
  1560. srsymtable:=stackitem^.symtable;
  1561. if (srsymtable.symtabletype=globalsymtable) and
  1562. (srsymtable.name^=unitname) then
  1563. begin
  1564. srsym:=tsym(srsymtable.find(symname));
  1565. if not assigned(srsym) then
  1566. break;
  1567. result:=true;
  1568. exit;
  1569. end;
  1570. stackitem:=stackitem^.next;
  1571. end;
  1572. { If the module is the current unit we also need
  1573. to search the local symtable }
  1574. if (current_module.globalsymtable=srsymtable) and
  1575. assigned(current_module.localsymtable) then
  1576. begin
  1577. srsymtable:=current_module.localsymtable;
  1578. srsym:=tsym(srsymtable.find(symname));
  1579. if assigned(srsym) then
  1580. begin
  1581. result:=true;
  1582. exit;
  1583. end;
  1584. end;
  1585. end;
  1586. function searchsym_in_class(classh,contextclassh:tobjectdef;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  1587. var
  1588. hashedid : THashedIDString;
  1589. begin
  1590. { The contextclassh is used for visibility. The classh must be equal to
  1591. or be a parent of contextclassh. E.g. for inherited searches the classh is the
  1592. parent. }
  1593. if assigned(classh) and
  1594. not contextclassh.is_related(classh) then
  1595. internalerror(200811161);
  1596. result:=false;
  1597. hashedid.id:=s;
  1598. while assigned(classh) do
  1599. begin
  1600. srsymtable:=classh.symtable;
  1601. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  1602. if assigned(srsym) and
  1603. is_visible_for_object(srsym,contextclassh) then
  1604. begin
  1605. addsymref(srsym);
  1606. result:=true;
  1607. exit;
  1608. end;
  1609. classh:=classh.childof;
  1610. end;
  1611. srsym:=nil;
  1612. srsymtable:=nil;
  1613. end;
  1614. function searchsym_in_class_by_msgint(classh:tobjectdef;msgid:longint;out srdef : tdef;out srsym:tsym;out srsymtable:TSymtable):boolean;
  1615. var
  1616. def : tdef;
  1617. i : longint;
  1618. begin
  1619. result:=false;
  1620. def:=nil;
  1621. while assigned(classh) do
  1622. begin
  1623. for i:=0 to classh.symtable.DefList.Count-1 do
  1624. begin
  1625. def:=tstoreddef(classh.symtable.DefList[i]);
  1626. { Find also all hidden private methods to
  1627. be compatible with delphi, see tw6203 (PFV) }
  1628. if (def.typ=procdef) and
  1629. (po_msgint in tprocdef(def).procoptions) and
  1630. (tprocdef(def).messageinf.i=msgid) then
  1631. begin
  1632. srdef:=def;
  1633. srsym:=tprocdef(def).procsym;
  1634. srsymtable:=classh.symtable;
  1635. addsymref(srsym);
  1636. result:=true;
  1637. exit;
  1638. end;
  1639. end;
  1640. classh:=classh.childof;
  1641. end;
  1642. srdef:=nil;
  1643. srsym:=nil;
  1644. srsymtable:=nil;
  1645. end;
  1646. function searchsym_in_class_by_msgstr(classh:tobjectdef;const s:string;out srsym:tsym;out srsymtable:TSymtable):boolean;
  1647. var
  1648. def : tdef;
  1649. i : longint;
  1650. begin
  1651. result:=false;
  1652. def:=nil;
  1653. while assigned(classh) do
  1654. begin
  1655. for i:=0 to classh.symtable.DefList.Count-1 do
  1656. begin
  1657. def:=tstoreddef(classh.symtable.DefList[i]);
  1658. { Find also all hidden private methods to
  1659. be compatible with delphi, see tw6203 (PFV) }
  1660. if (def.typ=procdef) and
  1661. (po_msgstr in tprocdef(def).procoptions) and
  1662. (tprocdef(def).messageinf.str^=s) then
  1663. begin
  1664. srsym:=tprocdef(def).procsym;
  1665. srsymtable:=classh.symtable;
  1666. addsymref(srsym);
  1667. result:=true;
  1668. exit;
  1669. end;
  1670. end;
  1671. classh:=classh.childof;
  1672. end;
  1673. srsym:=nil;
  1674. srsymtable:=nil;
  1675. end;
  1676. function search_assignment_operator(from_def,to_def:Tdef):Tprocdef;
  1677. var
  1678. sym : Tprocsym;
  1679. hashedid : THashedIDString;
  1680. curreq,
  1681. besteq : tequaltype;
  1682. currpd,
  1683. bestpd : tprocdef;
  1684. stackitem : psymtablestackitem;
  1685. begin
  1686. hashedid.id:='assign';
  1687. besteq:=te_incompatible;
  1688. bestpd:=nil;
  1689. stackitem:=symtablestack.stack;
  1690. while assigned(stackitem) do
  1691. begin
  1692. sym:=Tprocsym(stackitem^.symtable.FindWithHash(hashedid));
  1693. if sym<>nil then
  1694. begin
  1695. if sym.typ<>procsym then
  1696. internalerror(200402031);
  1697. { if the source type is an alias then this is only the second choice,
  1698. if you mess with this code, check tw4093 }
  1699. currpd:=sym.find_procdef_assignment_operator(from_def,to_def,curreq);
  1700. if curreq>besteq then
  1701. begin
  1702. besteq:=curreq;
  1703. bestpd:=currpd;
  1704. if (besteq=te_exact) then
  1705. break;
  1706. end;
  1707. end;
  1708. stackitem:=stackitem^.next;
  1709. end;
  1710. result:=bestpd;
  1711. end;
  1712. function search_system_type(const s: TIDString): ttypesym;
  1713. var
  1714. sym : tsym;
  1715. begin
  1716. sym:=tsym(systemunit.Find(s));
  1717. if not assigned(sym) or
  1718. (sym.typ<>typesym) then
  1719. cgmessage1(cg_f_unknown_system_type,s);
  1720. result:=ttypesym(sym);
  1721. end;
  1722. function search_named_unit_globaltype(const unitname, typename: TIDString): ttypesym;
  1723. var
  1724. contextobjdef : tobjectdef;
  1725. stackitem : psymtablestackitem;
  1726. srsymtable: tsymtable;
  1727. sym: tsym;
  1728. begin
  1729. if searchsym_in_named_module(unitname,typename,sym,srsymtable) and
  1730. (sym.typ=typesym) then
  1731. begin
  1732. result:=ttypesym(sym);
  1733. exit;
  1734. end
  1735. else
  1736. begin
  1737. cgmessage2(cg_f_unknown_type_in_unit,typename,unitname);
  1738. result:=nil;
  1739. end;
  1740. end;
  1741. function search_class_member(pd : tobjectdef;const s : string):tsym;
  1742. { searches n in symtable of pd and all anchestors }
  1743. var
  1744. hashedid : THashedIDString;
  1745. srsym : tsym;
  1746. begin
  1747. hashedid.id:=s;
  1748. while assigned(pd) do
  1749. begin
  1750. srsym:=tsym(pd.symtable.FindWithHash(hashedid));
  1751. if assigned(srsym) then
  1752. begin
  1753. search_class_member:=srsym;
  1754. exit;
  1755. end;
  1756. pd:=pd.childof;
  1757. end;
  1758. search_class_member:=nil;
  1759. end;
  1760. function search_macro(const s : string):tsym;
  1761. var
  1762. stackitem : psymtablestackitem;
  1763. hashedid : THashedIDString;
  1764. srsym : tsym;
  1765. begin
  1766. hashedid.id:=s;
  1767. { First search the localmacrosymtable before searching the
  1768. global macrosymtables from the units }
  1769. if assigned(current_module) then
  1770. begin
  1771. srsym:=tsym(current_module.localmacrosymtable.FindWithHash(hashedid));
  1772. if assigned(srsym) then
  1773. begin
  1774. result:= srsym;
  1775. exit;
  1776. end;
  1777. end;
  1778. stackitem:=macrosymtablestack.stack;
  1779. while assigned(stackitem) do
  1780. begin
  1781. srsym:=tsym(stackitem^.symtable.FindWithHash(hashedid));
  1782. if assigned(srsym) then
  1783. begin
  1784. result:= srsym;
  1785. exit;
  1786. end;
  1787. stackitem:=stackitem^.next;
  1788. end;
  1789. result:= nil;
  1790. end;
  1791. function defined_macro(const s : string):boolean;
  1792. var
  1793. mac: tmacro;
  1794. begin
  1795. mac:=tmacro(search_macro(s));
  1796. if assigned(mac) then
  1797. begin
  1798. mac.is_used:=true;
  1799. defined_macro:=mac.defined;
  1800. end
  1801. else
  1802. defined_macro:=false;
  1803. end;
  1804. {****************************************************************************
  1805. Object Helpers
  1806. ****************************************************************************}
  1807. function search_default_property(pd : tobjectdef) : tpropertysym;
  1808. { returns the default property of a class, searches also anchestors }
  1809. var
  1810. _defaultprop : tpropertysym;
  1811. begin
  1812. _defaultprop:=nil;
  1813. while assigned(pd) do
  1814. begin
  1815. pd.symtable.SymList.ForEachCall(@tstoredsymtable(pd.symtable).testfordefaultproperty,@_defaultprop);
  1816. if assigned(_defaultprop) then
  1817. break;
  1818. pd:=pd.childof;
  1819. end;
  1820. search_default_property:=_defaultprop;
  1821. end;
  1822. {****************************************************************************
  1823. Macro Helpers
  1824. ****************************************************************************}
  1825. procedure def_system_macro(const name : string);
  1826. var
  1827. mac : tmacro;
  1828. s: string;
  1829. begin
  1830. if name = '' then
  1831. internalerror(2004121202);
  1832. s:= upper(name);
  1833. mac:=tmacro(search_macro(s));
  1834. if not assigned(mac) then
  1835. begin
  1836. mac:=tmacro.create(s);
  1837. if assigned(current_module) then
  1838. current_module.localmacrosymtable.insert(mac)
  1839. else
  1840. initialmacrosymtable.insert(mac);
  1841. end;
  1842. if not mac.defined then
  1843. Message1(parser_c_macro_defined,mac.name);
  1844. mac.defined:=true;
  1845. end;
  1846. procedure set_system_macro(const name, value : string);
  1847. var
  1848. mac : tmacro;
  1849. s: string;
  1850. begin
  1851. if name = '' then
  1852. internalerror(2004121203);
  1853. s:= upper(name);
  1854. mac:=tmacro(search_macro(s));
  1855. if not assigned(mac) then
  1856. begin
  1857. mac:=tmacro.create(s);
  1858. if assigned(current_module) then
  1859. current_module.localmacrosymtable.insert(mac)
  1860. else
  1861. initialmacrosymtable.insert(mac);
  1862. end
  1863. else
  1864. begin
  1865. mac.is_compiler_var:=false;
  1866. if assigned(mac.buftext) then
  1867. freemem(mac.buftext,mac.buflen);
  1868. end;
  1869. Message2(parser_c_macro_set_to,mac.name,value);
  1870. mac.buflen:=length(value);
  1871. getmem(mac.buftext,mac.buflen);
  1872. move(value[1],mac.buftext^,mac.buflen);
  1873. mac.defined:=true;
  1874. end;
  1875. procedure set_system_compvar(const name, value : string);
  1876. var
  1877. mac : tmacro;
  1878. s: string;
  1879. begin
  1880. if name = '' then
  1881. internalerror(2004121204);
  1882. s:= upper(name);
  1883. mac:=tmacro(search_macro(s));
  1884. if not assigned(mac) then
  1885. begin
  1886. mac:=tmacro.create(s);
  1887. mac.is_compiler_var:=true;
  1888. if assigned(current_module) then
  1889. current_module.localmacrosymtable.insert(mac)
  1890. else
  1891. initialmacrosymtable.insert(mac);
  1892. end
  1893. else
  1894. begin
  1895. mac.is_compiler_var:=true;
  1896. if assigned(mac.buftext) then
  1897. freemem(mac.buftext,mac.buflen);
  1898. end;
  1899. Message2(parser_c_macro_set_to,mac.name,value);
  1900. mac.buflen:=length(value);
  1901. getmem(mac.buftext,mac.buflen);
  1902. move(value[1],mac.buftext^,mac.buflen);
  1903. mac.defined:=true;
  1904. end;
  1905. procedure undef_system_macro(const name : string);
  1906. var
  1907. mac : tmacro;
  1908. s: string;
  1909. begin
  1910. if name = '' then
  1911. internalerror(2004121205);
  1912. s:= upper(name);
  1913. mac:=tmacro(search_macro(s));
  1914. if not assigned(mac) then
  1915. {If not found, then it's already undefined.}
  1916. else
  1917. begin
  1918. if mac.defined then
  1919. Message1(parser_c_macro_undefined,mac.name);
  1920. mac.defined:=false;
  1921. mac.is_compiler_var:=false;
  1922. { delete old definition }
  1923. if assigned(mac.buftext) then
  1924. begin
  1925. freemem(mac.buftext,mac.buflen);
  1926. mac.buftext:=nil;
  1927. end;
  1928. end;
  1929. end;
  1930. {$ifdef UNITALIASES}
  1931. {****************************************************************************
  1932. TUNIT_ALIAS
  1933. ****************************************************************************}
  1934. constructor tunit_alias.create(const n:string);
  1935. var
  1936. i : longint;
  1937. begin
  1938. i:=pos('=',n);
  1939. if i=0 then
  1940. fail;
  1941. inherited createname(Copy(n,1,i-1));
  1942. newname:=stringdup(Copy(n,i+1,255));
  1943. end;
  1944. destructor tunit_alias.destroy;
  1945. begin
  1946. stringdispose(newname);
  1947. inherited destroy;
  1948. end;
  1949. procedure addunitalias(const n:string);
  1950. begin
  1951. unitaliases^.insert(tunit_alias,init(Upper(n))));
  1952. end;
  1953. function getunitalias(const n:string):string;
  1954. var
  1955. p : punit_alias;
  1956. begin
  1957. p:=punit_alias(unitaliases^.Find(Upper(n)));
  1958. if assigned(p) then
  1959. getunitalias:=punit_alias(p).newname^
  1960. else
  1961. getunitalias:=n;
  1962. end;
  1963. {$endif UNITALIASES}
  1964. {****************************************************************************
  1965. Init/Done Symtable
  1966. ****************************************************************************}
  1967. procedure InitSymtable;
  1968. begin
  1969. { Reset symbolstack }
  1970. symtablestack:=nil;
  1971. systemunit:=nil;
  1972. { create error syms and def }
  1973. generrorsym:=terrorsym.create;
  1974. generrordef:=terrordef.create;
  1975. { macros }
  1976. initialmacrosymtable:=tmacrosymtable.create(false);
  1977. macrosymtablestack:=TSymtablestack.create;
  1978. macrosymtablestack.push(initialmacrosymtable);
  1979. {$ifdef UNITALIASES}
  1980. { unit aliases }
  1981. unitaliases:=TFPHashObjectList.create;
  1982. {$endif}
  1983. { set some global vars to nil, might be important for the ide }
  1984. class_tobject:=nil;
  1985. interface_iunknown:=nil;
  1986. rec_tguid:=nil;
  1987. dupnr:=0;
  1988. end;
  1989. procedure DoneSymtable;
  1990. begin
  1991. generrorsym.owner:=nil;
  1992. generrorsym.free;
  1993. generrordef.owner:=nil;
  1994. generrordef.free;
  1995. initialmacrosymtable.free;
  1996. macrosymtablestack.free;
  1997. {$ifdef UNITALIASES}
  1998. unitaliases.free;
  1999. {$endif}
  2000. end;
  2001. end.