symtable.pas 82 KB

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