symtable.pas 87 KB

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