symtable.pas 76 KB

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