symtable.pas 76 KB

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