symtable.pas 75 KB

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