symtable.pas 75 KB

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