symtable.pas 84 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543
  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,symppu,
  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 unitsymbolused(p : TNamedIndexItem;arg:pointer);
  44. procedure varsymbolused(p : TNamedIndexItem;arg:pointer);
  45. procedure TestPrivate(p : TNamedIndexItem;arg:pointer);
  46. procedure objectprivatesymbolused(p : TNamedIndexItem;arg:pointer);
  47. {$ifdef GDB}
  48. private
  49. procedure concatstab(p : TNamedIndexItem;arg:pointer);
  50. procedure resetstab(p : TNamedIndexItem;arg:pointer);
  51. procedure concattypestab(p : TNamedIndexItem;arg:pointer);
  52. {$endif}
  53. procedure unchain_overloads(p : TNamedIndexItem;arg:pointer);
  54. procedure loaddefs(ppufile:tcompilerppufile);
  55. procedure loadsyms(ppufile:tcompilerppufile);
  56. procedure writedefs(ppufile:tcompilerppufile);
  57. procedure writesyms(ppufile:tcompilerppufile);
  58. public
  59. { load/write }
  60. procedure ppuload(ppufile:tcompilerppufile);virtual;
  61. procedure ppuwrite(ppufile:tcompilerppufile);virtual;
  62. procedure load_references(ppufile:tcompilerppufile;locals:boolean);virtual;
  63. procedure write_references(ppufile:tcompilerppufile;locals:boolean);virtual;
  64. procedure deref;virtual;
  65. procedure derefimpl;virtual;
  66. procedure insert(sym : tsymentry);override;
  67. function speedsearch(const s : stringid;speedvalue : cardinal) : tsymentry;override;
  68. procedure allsymbolsused;
  69. procedure allprivatesused;
  70. procedure allunitsused;
  71. procedure check_forwards;
  72. procedure checklabels;
  73. function needs_init_final : boolean;
  74. procedure unchain_overloaded;
  75. procedure chainoperators;
  76. {$ifdef GDB}
  77. procedure concatstabto(asmlist : taasmoutput);virtual;
  78. function getnewtypecount : word; override;
  79. {$endif GDB}
  80. procedure testfordefaultproperty(p : TNamedIndexItem;arg:pointer);
  81. end;
  82. tabstractrecordsymtable = class(tstoredsymtable)
  83. public
  84. datasize : longint;
  85. dataalignment : byte;
  86. constructor create(const n:string);
  87. procedure ppuload(ppufile:tcompilerppufile);override;
  88. procedure ppuwrite(ppufile:tcompilerppufile);override;
  89. procedure load_references(ppufile:tcompilerppufile;locals:boolean);override;
  90. procedure write_references(ppufile:tcompilerppufile;locals:boolean);override;
  91. procedure insertfield(sym:tvarsym;addsym:boolean);
  92. end;
  93. trecordsymtable = class(tabstractrecordsymtable)
  94. public
  95. constructor create;
  96. procedure insert_in(tsymt : trecordsymtable;offset : longint);
  97. end;
  98. tobjectsymtable = class(tabstractrecordsymtable)
  99. public
  100. constructor create(const n:string);
  101. procedure insert(sym : tsymentry);override;
  102. end;
  103. tabstractlocalsymtable = class(tstoredsymtable)
  104. public
  105. procedure ppuwrite(ppufile:tcompilerppufile);override;
  106. end;
  107. tlocalsymtable = class(tabstractlocalsymtable)
  108. public
  109. constructor create(level:byte);
  110. procedure insert(sym : tsymentry);override;
  111. end;
  112. tparasymtable = class(tabstractlocalsymtable)
  113. public
  114. constructor create(level:byte);
  115. procedure insert(sym : tsymentry);override;
  116. end;
  117. tabstractunitsymtable = class(tstoredsymtable)
  118. public
  119. {$ifdef GDB}
  120. dbx_count : longint;
  121. prev_dbx_counter : plongint;
  122. dbx_count_ok : boolean;
  123. is_stab_written : boolean;
  124. {$endif GDB}
  125. constructor create(const n : string);
  126. {$ifdef GDB}
  127. procedure concattypestabto(asmlist : taasmoutput);
  128. {$endif GDB}
  129. end;
  130. tglobalsymtable = class(tabstractunitsymtable)
  131. public
  132. unitsym : tunitsym;
  133. unittypecount : word;
  134. constructor create(const n : string);
  135. destructor destroy;override;
  136. procedure ppuload(ppufile:tcompilerppufile);override;
  137. procedure ppuwrite(ppufile:tcompilerppufile);override;
  138. procedure load_references(ppufile:tcompilerppufile;locals:boolean);override;
  139. procedure write_references(ppufile:tcompilerppufile;locals:boolean);override;
  140. procedure insert(sym : tsymentry);override;
  141. {$ifdef GDB}
  142. function getnewtypecount : word; override;
  143. {$endif}
  144. end;
  145. tstaticsymtable = class(tabstractunitsymtable)
  146. public
  147. constructor create(const n : string);
  148. procedure ppuload(ppufile:tcompilerppufile);override;
  149. procedure ppuwrite(ppufile:tcompilerppufile);override;
  150. procedure load_references(ppufile:tcompilerppufile;locals:boolean);override;
  151. procedure write_references(ppufile:tcompilerppufile;locals:boolean);override;
  152. procedure insert(sym : tsymentry);override;
  153. end;
  154. twithsymtable = class(tsymtable)
  155. withrefnode : pointer; { tnode }
  156. constructor create(aowner:tdef;asymsearch:TDictionary;refnode:pointer{tnode});
  157. destructor destroy;override;
  158. procedure clear;override;
  159. end;
  160. tstt_exceptsymtable = class(tsymtable)
  161. public
  162. constructor create;
  163. end;
  164. var
  165. constsymtable : tsymtable; { symtable were the constants can be inserted }
  166. systemunit : tglobalsymtable; { pointer to the system unit }
  167. {****************************************************************************
  168. Functions
  169. ****************************************************************************}
  170. {*** Misc ***}
  171. procedure globaldef(const s : string;var t:ttype);
  172. function findunitsymtable(st:tsymtable):tsymtable;
  173. procedure duplicatesym(sym:tsym);
  174. {*** Search ***}
  175. function searchsym(const s : stringid;var srsym:tsym;var srsymtable:tsymtable):boolean;
  176. function searchsym_type(const s : stringid;var srsym:tsym;var srsymtable:tsymtable):boolean;
  177. function searchsymonlyin(p : tsymtable;const s : stringid):tsym;
  178. function searchsym_in_class(classh:tobjectdef;const s : stringid):tsym;
  179. function searchsym_in_class_by_msgint(classh:tobjectdef;i:longint):tsym;
  180. function searchsym_in_class_by_msgstr(classh:tobjectdef;const s:string):tsym;
  181. function searchsystype(const s: stringid; var srsym: ttypesym): boolean;
  182. function searchsysvar(const s: stringid; var srsym: tvarsym; var symowner: tsymtable): boolean;
  183. function search_class_member(pd : tobjectdef;const s : string):tsym;
  184. {*** Object Helpers ***}
  185. function search_default_property(pd : tobjectdef) : tpropertysym;
  186. {*** symtable stack ***}
  187. procedure RestoreUnitSyms;
  188. {$ifdef DEBUG}
  189. procedure test_symtablestack;
  190. procedure list_symtablestack;
  191. {$endif DEBUG}
  192. {$ifdef UNITALIASES}
  193. type
  194. punit_alias = ^tunit_alias;
  195. tunit_alias = object(TNamedIndexItem)
  196. newname : pstring;
  197. constructor init(const n:string);
  198. destructor done;virtual;
  199. end;
  200. var
  201. unitaliases : pdictionary;
  202. procedure addunitalias(const n:string);
  203. function getunitalias(const n:string):string;
  204. {$endif UNITALIASES}
  205. {*** Init / Done ***}
  206. procedure InitSymtable;
  207. procedure DoneSymtable;
  208. type
  209. toverloaded_operators = array[NOTOKEN..last_overloaded] of tprocsym;
  210. var
  211. overloaded_operators : toverloaded_operators;
  212. { unequal is not equal}
  213. const
  214. overloaded_names : array [NOTOKEN..last_overloaded] of string[16] =
  215. ('error',
  216. 'plus','minus','star','slash','equal',
  217. 'greater','lower','greater_or_equal',
  218. 'lower_or_equal',
  219. 'sym_diff','starstar',
  220. 'as','is','in','or',
  221. 'and','div','mod','not','shl','shr','xor',
  222. 'assign');
  223. implementation
  224. uses
  225. { global }
  226. verbose,globals,
  227. { target }
  228. systems,
  229. { symtable }
  230. symutil,
  231. { module }
  232. fmodule,
  233. {$ifdef GDB}
  234. gdb,
  235. {$endif GDB}
  236. { codegen }
  237. procinfo
  238. ;
  239. {*****************************************************************************
  240. TStoredSymtable
  241. *****************************************************************************}
  242. procedure tstoredsymtable.ppuload(ppufile:tcompilerppufile);
  243. begin
  244. { load definitions }
  245. loaddefs(ppufile);
  246. { load symbols }
  247. loadsyms(ppufile);
  248. end;
  249. procedure tstoredsymtable.ppuwrite(ppufile:tcompilerppufile);
  250. begin
  251. { write definitions }
  252. writedefs(ppufile);
  253. { write symbols }
  254. writesyms(ppufile);
  255. end;
  256. procedure tstoredsymtable.loaddefs(ppufile:tcompilerppufile);
  257. var
  258. hp : tdef;
  259. b : byte;
  260. begin
  261. { load start of definition section, which holds the amount of defs }
  262. if ppufile.readentry<>ibstartdefs then
  263. Message(unit_f_ppu_read_error);
  264. ppufile.getlongint;
  265. { read definitions }
  266. repeat
  267. b:=ppufile.readentry;
  268. case b of
  269. ibpointerdef : hp:=tpointerdef.ppuload(ppufile);
  270. ibarraydef : hp:=tarraydef.ppuload(ppufile);
  271. iborddef : hp:=torddef.ppuload(ppufile);
  272. ibfloatdef : hp:=tfloatdef.ppuload(ppufile);
  273. ibprocdef : hp:=tprocdef.ppuload(ppufile);
  274. ibshortstringdef : hp:=tstringdef.loadshort(ppufile);
  275. iblongstringdef : hp:=tstringdef.loadlong(ppufile);
  276. ibansistringdef : hp:=tstringdef.loadansi(ppufile);
  277. ibwidestringdef : hp:=tstringdef.loadwide(ppufile);
  278. ibrecorddef : hp:=trecorddef.ppuload(ppufile);
  279. ibobjectdef : hp:=tobjectdef.ppuload(ppufile);
  280. ibenumdef : hp:=tenumdef.ppuload(ppufile);
  281. ibsetdef : hp:=tsetdef.ppuload(ppufile);
  282. ibprocvardef : hp:=tprocvardef.ppuload(ppufile);
  283. ibfiledef : hp:=tfiledef.ppuload(ppufile);
  284. ibclassrefdef : hp:=tclassrefdef.ppuload(ppufile);
  285. ibformaldef : hp:=tformaldef.ppuload(ppufile);
  286. ibvariantdef : hp:=tvariantdef.ppuload(ppufile);
  287. ibenddefs : break;
  288. ibend : Message(unit_f_ppu_read_error);
  289. else
  290. Message1(unit_f_ppu_invalid_entry,tostr(b));
  291. end;
  292. hp.owner:=self;
  293. defindex.insert(hp);
  294. until false;
  295. end;
  296. procedure tstoredsymtable.loadsyms(ppufile:tcompilerppufile);
  297. var
  298. b : byte;
  299. sym : tsym;
  300. begin
  301. { load start of definition section, which holds the amount of defs }
  302. if ppufile.readentry<>ibstartsyms then
  303. Message(unit_f_ppu_read_error);
  304. { skip amount of symbols, not used currently }
  305. ppufile.getlongint;
  306. { now read the symbols }
  307. repeat
  308. b:=ppufile.readentry;
  309. case b of
  310. ibtypesym : sym:=ttypesym.ppuload(ppufile);
  311. ibprocsym : sym:=tprocsym.ppuload(ppufile);
  312. ibconstsym : sym:=tconstsym.ppuload(ppufile);
  313. ibvarsym : sym:=tvarsym.ppuload(ppufile);
  314. ibabsolutesym : sym:=tabsolutesym.ppuload(ppufile);
  315. ibenumsym : sym:=tenumsym.ppuload(ppufile);
  316. ibtypedconstsym : sym:=ttypedconstsym.ppuload(ppufile);
  317. ibpropertysym : sym:=tpropertysym.ppuload(ppufile);
  318. ibunitsym : sym:=tunitsym.ppuload(ppufile);
  319. iblabelsym : sym:=tlabelsym.ppuload(ppufile);
  320. ibsyssym : sym:=tsyssym.ppuload(ppufile);
  321. ibrttisym : sym:=trttisym.ppuload(ppufile);
  322. ibendsyms : break;
  323. ibend : Message(unit_f_ppu_read_error);
  324. else
  325. Message1(unit_f_ppu_invalid_entry,tostr(b));
  326. end;
  327. sym.owner:=self;
  328. symindex.insert(sym);
  329. symsearch.insert(sym);
  330. until false;
  331. end;
  332. procedure tstoredsymtable.writedefs(ppufile:tcompilerppufile);
  333. var
  334. pd : tstoreddef;
  335. begin
  336. { each definition get a number, write then the amount of defs to the
  337. ibstartdef entry }
  338. ppufile.putlongint(defindex.count);
  339. ppufile.writeentry(ibstartdefs);
  340. { now write the definition }
  341. pd:=tstoreddef(defindex.first);
  342. while assigned(pd) do
  343. begin
  344. pd.ppuwrite(ppufile);
  345. pd:=tstoreddef(pd.indexnext);
  346. end;
  347. { write end of definitions }
  348. ppufile.writeentry(ibenddefs);
  349. end;
  350. procedure tstoredsymtable.writesyms(ppufile:tcompilerppufile);
  351. var
  352. pd : tstoredsym;
  353. begin
  354. { each definition get a number, write then the amount of syms and the
  355. datasize to the ibsymdef entry }
  356. ppufile.putlongint(symindex.count);
  357. ppufile.writeentry(ibstartsyms);
  358. { foreach is used to write all symbols }
  359. pd:=tstoredsym(symindex.first);
  360. while assigned(pd) do
  361. begin
  362. pd.ppuwrite(ppufile);
  363. pd:=tstoredsym(pd.indexnext);
  364. end;
  365. { end of symbols }
  366. ppufile.writeentry(ibendsyms);
  367. end;
  368. procedure tstoredsymtable.load_references(ppufile:tcompilerppufile;locals:boolean);
  369. var
  370. b : byte;
  371. d : tderef;
  372. sym : tstoredsym;
  373. prdef : tstoreddef;
  374. begin
  375. b:=ppufile.readentry;
  376. if b <> ibbeginsymtablebrowser then
  377. Message1(unit_f_ppu_invalid_entry,tostr(b));
  378. repeat
  379. b:=ppufile.readentry;
  380. case b of
  381. ibsymref :
  382. begin
  383. ppufile.getderef(d);
  384. sym:=tstoredsym(d.resolve);
  385. if assigned(sym) then
  386. sym.load_references(ppufile,locals);
  387. end;
  388. ibdefref :
  389. begin
  390. ppufile.getderef(d);
  391. prdef:=tstoreddef(d.resolve);
  392. if assigned(prdef) then
  393. begin
  394. if prdef.deftype<>procdef then
  395. Message(unit_f_ppu_read_error);
  396. tprocdef(prdef).load_references(ppufile,locals);
  397. end;
  398. end;
  399. ibendsymtablebrowser :
  400. break;
  401. else
  402. Message1(unit_f_ppu_invalid_entry,tostr(b));
  403. end;
  404. until false;
  405. end;
  406. procedure tstoredsymtable.write_references(ppufile:tcompilerppufile;locals:boolean);
  407. var
  408. pd : tstoredsym;
  409. begin
  410. ppufile.writeentry(ibbeginsymtablebrowser);
  411. { write all symbols }
  412. pd:=tstoredsym(symindex.first);
  413. while assigned(pd) do
  414. begin
  415. pd.write_references(ppufile,locals);
  416. pd:=tstoredsym(pd.indexnext);
  417. end;
  418. ppufile.writeentry(ibendsymtablebrowser);
  419. end;
  420. procedure tstoredsymtable.deref;
  421. var
  422. hp : tdef;
  423. hs : tsym;
  424. begin
  425. { first deref the interface ttype symbols. This is needs
  426. to be done before the interface defs are derefed, because
  427. the interface defs can contain references to the type symbols
  428. which then already need to contain a resolved restype field (PFV) }
  429. hs:=tsym(symindex.first);
  430. while assigned(hs) do
  431. begin
  432. if hs.typ=typesym then
  433. hs.deref;
  434. hs:=tsym(hs.indexnext);
  435. end;
  436. { deref the interface definitions }
  437. hp:=tdef(defindex.first);
  438. while assigned(hp) do
  439. begin
  440. hp.deref;
  441. hp:=tdef(hp.indexnext);
  442. end;
  443. { deref the interface symbols }
  444. hs:=tsym(symindex.first);
  445. while assigned(hs) do
  446. begin
  447. if hs.typ<>typesym then
  448. hs.deref;
  449. hs:=tsym(hs.indexnext);
  450. end;
  451. end;
  452. procedure tstoredsymtable.derefimpl;
  453. var
  454. hp : tdef;
  455. begin
  456. { deref the implementation part of definitions }
  457. hp:=tdef(defindex.first);
  458. while assigned(hp) do
  459. begin
  460. hp.derefimpl;
  461. hp:=tdef(hp.indexnext);
  462. end;
  463. end;
  464. procedure tstoredsymtable.insert(sym:tsymentry);
  465. var
  466. hsym : tsym;
  467. begin
  468. { set owner and sym indexnb }
  469. sym.owner:=self;
  470. { check the current symtable }
  471. hsym:=tsym(search(sym.name));
  472. if assigned(hsym) then
  473. begin
  474. { in TP and Delphi you can have a local with the
  475. same name as the function, the function is then hidden for
  476. the user. (Under delphi it can still be accessed using result),
  477. but don't allow hiding of RESULT }
  478. if (m_duplicate_names in aktmodeswitches) and
  479. (sym.typ in [varsym,absolutesym]) and
  480. (vo_is_funcret in tvarsym(sym).varoptions) and
  481. not((m_result in aktmodeswitches) and
  482. (vo_is_result in tvarsym(sym).varoptions)) then
  483. sym.name:='hidden'+sym.name
  484. else
  485. begin
  486. DuplicateSym(hsym);
  487. exit;
  488. end;
  489. end;
  490. { register definition of typesym }
  491. if (sym.typ = typesym) and
  492. assigned(ttypesym(sym).restype.def) then
  493. begin
  494. if not(assigned(ttypesym(sym).restype.def.owner)) and
  495. (ttypesym(sym).restype.def.deftype<>errordef) then
  496. registerdef(ttypesym(sym).restype.def);
  497. {$ifdef GDB}
  498. if (cs_debuginfo in aktmoduleswitches) and assigned(debuglist) and
  499. (symtabletype in [globalsymtable,staticsymtable]) then
  500. begin
  501. ttypesym(sym).isusedinstab := true;
  502. {sym.concatstabto(debuglist);}
  503. end;
  504. {$endif GDB}
  505. end;
  506. { insert in index and search hash }
  507. symindex.insert(sym);
  508. symsearch.insert(sym);
  509. end;
  510. function tstoredsymtable.speedsearch(const s : stringid;speedvalue : cardinal) : tsymentry;
  511. var
  512. hp : tstoredsym;
  513. newref : tref;
  514. begin
  515. hp:=tstoredsym(inherited speedsearch(s,speedvalue));
  516. if assigned(hp) then
  517. begin
  518. { reject non static members in static procedures }
  519. if (symtabletype=objectsymtable) and
  520. not(sp_static in hp.symoptions) and
  521. allow_only_static then
  522. Message(sym_e_only_static_in_static);
  523. { unit uses count }
  524. if (unitid<>0) and
  525. (symtabletype = globalsymtable) and
  526. assigned(tglobalsymtable(self).unitsym) then
  527. inc(tglobalsymtable(self).unitsym.refs);
  528. {$ifdef GDB}
  529. { if it is a type, we need the stabs of this type
  530. this might be the cause of the class debug problems
  531. as TCHILDCLASS.Create did not generate appropriate
  532. stabs debug info if TCHILDCLASS wasn't used anywhere else PM }
  533. if (cs_debuginfo in aktmoduleswitches) and
  534. (hp.typ=typesym) and
  535. make_ref then
  536. begin
  537. if assigned(ttypesym(hp).restype.def) then
  538. tstoreddef(ttypesym(hp).restype.def).numberstring
  539. else
  540. ttypesym(hp).isusedinstab:=true;
  541. end;
  542. {$endif GDB}
  543. { unitsym are only loaded for browsing PM }
  544. { this was buggy anyway because we could use }
  545. { unitsyms from other units in _USES !! }
  546. {if (symtabletype=unitsymtable) and (hp.typ=unitsym) and
  547. assigned(current_module) and (current_module.globalsymtable<>.load) then
  548. hp:=nil;}
  549. if make_ref and (cs_browser in aktmoduleswitches) then
  550. begin
  551. newref:=tref.create(hp.lastref,@akttokenpos);
  552. { for symbols that are in tables without browser info or syssyms }
  553. if hp.refcount=0 then
  554. begin
  555. hp.defref:=newref;
  556. hp.lastref:=newref;
  557. end
  558. else
  559. if resolving_forward and assigned(hp.defref) then
  560. { put it as second reference }
  561. begin
  562. newref.nextref:=hp.defref.nextref;
  563. hp.defref.nextref:=newref;
  564. hp.lastref.nextref:=nil;
  565. end
  566. else
  567. hp.lastref:=newref;
  568. inc(hp.refcount);
  569. end;
  570. if make_ref then
  571. inc(hp.refs);
  572. end; { value was not found }
  573. speedsearch:=hp;
  574. end;
  575. {**************************************
  576. Callbacks
  577. **************************************}
  578. procedure TStoredSymtable.check_forward(sym : TNamedIndexItem;arg:pointer);
  579. begin
  580. if tsym(sym).typ=procsym then
  581. tprocsym(sym).check_forward
  582. { check also object method table }
  583. { we needn't to test the def list }
  584. { because each object has to have a type sym }
  585. else
  586. if (tsym(sym).typ=typesym) and
  587. assigned(ttypesym(sym).restype.def) and
  588. (ttypesym(sym).restype.def.deftype=objectdef) then
  589. tobjectdef(ttypesym(sym).restype.def).check_forwards;
  590. end;
  591. procedure TStoredSymtable.labeldefined(p : TNamedIndexItem;arg:pointer);
  592. begin
  593. if (tsym(p).typ=labelsym) and
  594. not(tlabelsym(p).defined) then
  595. begin
  596. if tlabelsym(p).used then
  597. Message1(sym_e_label_used_and_not_defined,tlabelsym(p).realname)
  598. else
  599. Message1(sym_w_label_not_defined,tlabelsym(p).realname);
  600. end;
  601. end;
  602. procedure TStoredSymtable.unitsymbolused(p : TNamedIndexItem;arg:pointer);
  603. begin
  604. if (tsym(p).typ=unitsym) and
  605. (tunitsym(p).refs=0) and
  606. { do not claim for unit name itself !! }
  607. assigned(tunitsym(p).unitsymtable) and
  608. (tunitsym(p).unitsymtable.symtabletype=globalsymtable) then
  609. MessagePos2(tsym(p).fileinfo,sym_n_unit_not_used,p.name,current_module.modulename^);
  610. end;
  611. procedure TStoredSymtable.varsymbolused(p : TNamedIndexItem;arg:pointer);
  612. begin
  613. if (tsym(p).typ=varsym) and
  614. ((tsym(p).owner.symtabletype in
  615. [parasymtable,localsymtable,objectsymtable,staticsymtable])) then
  616. begin
  617. { unused symbol should be reported only if no }
  618. { error is reported }
  619. { if the symbol is in a register it is used }
  620. { also don't count the value parameters which have local copies }
  621. { also don't claim for high param of open parameters (PM) }
  622. if (Errorcount<>0) or
  623. (vo_is_self in tvarsym(p).varoptions) or
  624. (vo_is_vmt in tvarsym(p).varoptions) or
  625. (vo_is_high_value in tvarsym(p).varoptions) or
  626. (copy(p.name,1,6)='hidden') then
  627. exit;
  628. if (tvarsym(p).refs=0) then
  629. begin
  630. if (vo_is_funcret in tvarsym(p).varoptions) then
  631. begin
  632. { don't warn about the result of constructors }
  633. if (tsym(p).owner.symtabletype<>localsymtable) or
  634. (tprocdef(tsym(p).owner.defowner).proctypeoption<>potype_constructor) then
  635. MessagePos(tsym(p).fileinfo,sym_w_function_result_not_set)
  636. end
  637. else if (tsym(p).owner.symtabletype=parasymtable) then
  638. MessagePos1(tsym(p).fileinfo,sym_h_para_identifier_not_used,tsym(p).realname)
  639. else if (tsym(p).owner.symtabletype=objectsymtable) then
  640. MessagePos2(tsym(p).fileinfo,sym_n_private_identifier_not_used,tsym(p).owner.realname^,tsym(p).realname)
  641. else
  642. MessagePos1(tsym(p).fileinfo,sym_n_local_identifier_not_used,tsym(p).realname);
  643. end
  644. else if tvarsym(p).varstate=vs_assigned then
  645. begin
  646. if (tsym(p).owner.symtabletype=parasymtable) then
  647. begin
  648. if not(tvarsym(p).varspez in [vs_var,vs_out]) and
  649. not(vo_is_funcret in tvarsym(p).varoptions) then
  650. MessagePos1(tsym(p).fileinfo,sym_h_para_identifier_only_set,tsym(p).realname)
  651. end
  652. else if (tsym(p).owner.symtabletype=objectsymtable) then
  653. MessagePos2(tsym(p).fileinfo,sym_n_private_identifier_only_set,tsym(p).owner.realname^,tsym(p).realname)
  654. else if not(vo_is_exported in tvarsym(p).varoptions) and
  655. not(vo_is_funcret in tvarsym(p).varoptions) then
  656. MessagePos1(tsym(p).fileinfo,sym_n_local_identifier_only_set,tsym(p).realname);
  657. end;
  658. end
  659. else if ((tsym(p).owner.symtabletype in
  660. [objectsymtable,parasymtable,localsymtable,staticsymtable])) then
  661. begin
  662. if (Errorcount<>0) or
  663. (copy(p.name,1,3)='def') then
  664. exit;
  665. { do not claim for inherited private fields !! }
  666. if (tstoredsym(p).refs=0) and (tsym(p).owner.symtabletype=objectsymtable) then
  667. MessagePos2(tsym(p).fileinfo,sym_n_private_method_not_used,tsym(p).owner.realname^,tsym(p).realname)
  668. { units references are problematic }
  669. else
  670. begin
  671. if (tstoredsym(p).refs=0) and
  672. not(tsym(p).typ in [enumsym,unitsym]) and
  673. not(is_funcret_sym(tsym(p))) and
  674. (
  675. (tsym(p).typ<>procsym) or
  676. {$ifdef GDB}
  677. not (tprocsym(p).is_global) or
  678. {$endif GDB}
  679. { all program functions are declared global
  680. but unused should still be signaled PM }
  681. ((tsym(p).owner.symtabletype=staticsymtable) and
  682. not current_module.is_unit)
  683. ) then
  684. MessagePos2(tsym(p).fileinfo,sym_h_local_symbol_not_used,SymTypeName[tsym(p).typ],tsym(p).realname);
  685. end;
  686. end;
  687. end;
  688. procedure TStoredSymtable.TestPrivate(p : TNamedIndexItem;arg:pointer);
  689. begin
  690. if sp_private in tsym(p).symoptions then
  691. varsymbolused(p,arg);
  692. end;
  693. procedure TStoredSymtable.objectprivatesymbolused(p : TNamedIndexItem;arg:pointer);
  694. begin
  695. {
  696. Don't test simple object aliases PM
  697. }
  698. if (tsym(p).typ=typesym) and
  699. (ttypesym(p).restype.def.deftype=objectdef) and
  700. (ttypesym(p).restype.def.typesym=tsym(p)) then
  701. tobjectdef(ttypesym(p).restype.def).symtable.foreach({$ifdef FPCPROCVAR}@{$endif}TestPrivate,nil);
  702. end;
  703. procedure tstoredsymtable.unchain_overloads(p : TNamedIndexItem;arg:pointer);
  704. begin
  705. if tsym(p).typ=procsym then
  706. tprocsym(p).unchain_overload;
  707. end;
  708. {$ifdef GDB}
  709. procedure TStoredSymtable.concatstab(p : TNamedIndexItem;arg:pointer);
  710. begin
  711. if tsym(p).typ <> procsym then
  712. tstoredsym(p).concatstabto(TAAsmOutput(arg));
  713. end;
  714. procedure TStoredSymtable.resetstab(p : TNamedIndexItem;arg:pointer);
  715. begin
  716. if tsym(p).typ <> procsym then
  717. tstoredsym(p).isstabwritten:=false;
  718. end;
  719. procedure TStoredSymtable.concattypestab(p : TNamedIndexItem;arg:pointer);
  720. begin
  721. if tsym(p).typ = typesym then
  722. begin
  723. tstoredsym(p).isstabwritten:=false;
  724. tstoredsym(p).concatstabto(TAAsmOutput(arg));
  725. end;
  726. end;
  727. function tstoredsymtable.getnewtypecount : word;
  728. begin
  729. getnewtypecount:=pglobaltypecount^;
  730. inc(pglobaltypecount^);
  731. end;
  732. {$endif GDB}
  733. procedure tstoredsymtable.chainoperators;
  734. var
  735. t : ttoken;
  736. srsym : tsym;
  737. srsymtable,
  738. storesymtablestack : tsymtable;
  739. begin
  740. storesymtablestack:=symtablestack;
  741. symtablestack:=self;
  742. make_ref:=false;
  743. for t:=first_overloaded to last_overloaded do
  744. begin
  745. overloaded_operators[t]:=nil;
  746. { each operator has a unique lowercased internal name PM }
  747. while assigned(symtablestack) do
  748. begin
  749. searchsym(overloaded_names[t],srsym,srsymtable);
  750. if not assigned(srsym) then
  751. begin
  752. if (t=_STARSTAR) then
  753. begin
  754. symtablestack:=systemunit;
  755. searchsym('POWER',srsym,srsymtable);
  756. end;
  757. end;
  758. if assigned(srsym) then
  759. begin
  760. if (srsym.typ<>procsym) then
  761. internalerror(12344321);
  762. { remove all previous chains }
  763. tprocsym(srsym).unchain_overload;
  764. { use this procsym as start ? }
  765. if not assigned(overloaded_operators[t]) then
  766. overloaded_operators[t]:=tprocsym(srsym)
  767. else
  768. { already got a procsym, only add defs defined in the
  769. unit of the current procsym }
  770. Tprocsym(srsym).concat_procdefs_to(overloaded_operators[t]);
  771. symtablestack:=srsym.owner.next;
  772. end
  773. else
  774. begin
  775. symtablestack:=nil;
  776. end;
  777. { search for same procsym in other units }
  778. end;
  779. symtablestack:=self;
  780. end;
  781. make_ref:=true;
  782. symtablestack:=storesymtablestack;
  783. end;
  784. {***********************************************
  785. Process all entries
  786. ***********************************************}
  787. { checks, if all procsyms and methods are defined }
  788. procedure tstoredsymtable.check_forwards;
  789. begin
  790. foreach({$ifdef FPCPROCVAR}@{$endif}check_forward,nil);
  791. end;
  792. procedure tstoredsymtable.checklabels;
  793. begin
  794. foreach({$ifdef FPCPROCVAR}@{$endif}labeldefined,nil);
  795. end;
  796. procedure tstoredsymtable.allunitsused;
  797. begin
  798. foreach({$ifdef FPCPROCVAR}@{$endif}unitsymbolused,nil);
  799. end;
  800. procedure tstoredsymtable.allsymbolsused;
  801. begin
  802. foreach({$ifdef FPCPROCVAR}@{$endif}varsymbolused,nil);
  803. end;
  804. procedure tstoredsymtable.allprivatesused;
  805. begin
  806. foreach({$ifdef FPCPROCVAR}@{$endif}objectprivatesymbolused,nil);
  807. end;
  808. procedure tstoredsymtable.unchain_overloaded;
  809. begin
  810. foreach({$ifdef FPCPROCVAR}@{$endif}unchain_overloads,nil);
  811. end;
  812. {$ifdef GDB}
  813. procedure tstoredsymtable.concatstabto(asmlist : taasmoutput);
  814. begin
  815. if symtabletype in [inlineparasymtable,inlinelocalsymtable] then
  816. foreach({$ifdef FPCPROCVAR}@{$endif}resetstab,nil);
  817. foreach({$ifdef FPCPROCVAR}@{$endif}concatstab,asmlist);
  818. end;
  819. {$endif}
  820. procedure TStoredSymtable._needs_init_final(p : tnamedindexitem;arg:pointer);
  821. begin
  822. if b_needs_init_final then
  823. exit;
  824. case tsym(p).typ of
  825. varsym :
  826. begin
  827. if not(is_class(tvarsym(p).vartype.def)) and
  828. tstoreddef(tvarsym(p).vartype.def).needs_inittable then
  829. b_needs_init_final:=true;
  830. end;
  831. typedconstsym :
  832. begin
  833. if ttypedconstsym(p).is_writable and
  834. tstoreddef(ttypedconstsym(p).typedconsttype.def).needs_inittable then
  835. b_needs_init_final:=true;
  836. end;
  837. end;
  838. end;
  839. { returns true, if p contains data which needs init/final code }
  840. function tstoredsymtable.needs_init_final : boolean;
  841. begin
  842. b_needs_init_final:=false;
  843. foreach({$ifdef FPCPROCVAR}@{$endif}_needs_init_final,nil);
  844. needs_init_final:=b_needs_init_final;
  845. end;
  846. {****************************************************************************
  847. TAbstractRecordSymtable
  848. ****************************************************************************}
  849. constructor tabstractrecordsymtable.create(const n:string);
  850. begin
  851. inherited create(n);
  852. datasize:=0;
  853. dataalignment:=1;
  854. end;
  855. procedure tabstractrecordsymtable.ppuload(ppufile:tcompilerppufile);
  856. var
  857. storesymtable : tsymtable;
  858. begin
  859. storesymtable:=aktrecordsymtable;
  860. aktrecordsymtable:=self;
  861. inherited ppuload(ppufile);
  862. aktrecordsymtable:=storesymtable;
  863. end;
  864. procedure tabstractrecordsymtable.ppuwrite(ppufile:tcompilerppufile);
  865. var
  866. oldtyp : byte;
  867. storesymtable : tsymtable;
  868. begin
  869. storesymtable:=aktrecordsymtable;
  870. aktrecordsymtable:=self;
  871. oldtyp:=ppufile.entrytyp;
  872. ppufile.entrytyp:=subentryid;
  873. inherited ppuwrite(ppufile);
  874. ppufile.entrytyp:=oldtyp;
  875. aktrecordsymtable:=storesymtable;
  876. end;
  877. procedure tabstractrecordsymtable.load_references(ppufile:tcompilerppufile;locals:boolean);
  878. var
  879. storesymtable : tsymtable;
  880. begin
  881. storesymtable:=aktrecordsymtable;
  882. aktrecordsymtable:=self;
  883. inherited load_references(ppufile,locals);
  884. aktrecordsymtable:=storesymtable;
  885. end;
  886. procedure tabstractrecordsymtable.write_references(ppufile:tcompilerppufile;locals:boolean);
  887. var
  888. storesymtable : tsymtable;
  889. begin
  890. storesymtable:=aktrecordsymtable;
  891. aktrecordsymtable:=self;
  892. inherited write_references(ppufile,locals);
  893. aktrecordsymtable:=storesymtable;
  894. end;
  895. procedure tabstractrecordsymtable.insertfield(sym : tvarsym;addsym:boolean);
  896. var
  897. l,varalign : longint;
  898. vardef : tdef;
  899. begin
  900. if addsym then
  901. insert(sym);
  902. { Calculate field offset }
  903. l:=tvarsym(sym).getvaluesize;
  904. vardef:=tvarsym(sym).vartype.def;
  905. { this symbol can't be loaded to a register }
  906. exclude(tvarsym(sym).varoptions,vo_regable);
  907. exclude(tvarsym(sym).varoptions,vo_fpuregable);
  908. { get the alignment size }
  909. if (aktalignment.recordalignmax=-1) then
  910. begin
  911. varalign:=vardef.alignment;
  912. if (varalign>4) and
  913. ((varalign mod 4)<>0) and
  914. (vardef.deftype=arraydef) then
  915. Message1(sym_w_wrong_C_pack,vardef.typename);
  916. if varalign=0 then
  917. varalign:=l;
  918. if (dataalignment<aktalignment.maxCrecordalign) then
  919. begin
  920. if (varalign>16) and (dataalignment<32) then
  921. dataalignment:=32
  922. else if (varalign>12) and (dataalignment<16) then
  923. dataalignment:=16
  924. { 12 is needed for long double }
  925. else if (varalign>8) and (dataalignment<12) then
  926. dataalignment:=12
  927. else if (varalign>4) and (dataalignment<8) then
  928. dataalignment:=8
  929. else if (varalign>2) and (dataalignment<4) then
  930. dataalignment:=4
  931. else if (varalign>1) and (dataalignment<2) then
  932. dataalignment:=2;
  933. end;
  934. dataalignment:=min(dataalignment,aktalignment.maxCrecordalign);
  935. end
  936. else
  937. varalign:=vardef.alignment;
  938. if varalign=0 then
  939. varalign:=size_2_align(l);
  940. varalign:=used_align(varalign,aktalignment.recordalignmin,dataalignment);
  941. tvarsym(sym).fieldoffset:=align(datasize,varalign);
  942. datasize:=tvarsym(sym).fieldoffset+l;
  943. end;
  944. {****************************************************************************
  945. TRecordSymtable
  946. ****************************************************************************}
  947. constructor trecordsymtable.create;
  948. begin
  949. inherited create('');
  950. symtabletype:=recordsymtable;
  951. end;
  952. { this procedure is reserved for inserting case variant into
  953. a record symtable }
  954. { the offset is the location of the start of the variant
  955. and datasize and dataalignment corresponds to
  956. the complete size (see code in pdecl unit) PM }
  957. procedure trecordsymtable.insert_in(tsymt : trecordsymtable;offset : longint);
  958. var
  959. ps,nps : tvarsym;
  960. pd,npd : tdef;
  961. storesize,storealign : longint;
  962. begin
  963. storesize:=tsymt.datasize;
  964. storealign:=tsymt.dataalignment;
  965. tsymt.datasize:=offset;
  966. ps:=tvarsym(symindex.first);
  967. while assigned(ps) do
  968. begin
  969. nps:=tvarsym(ps.indexnext);
  970. { remove from current symtable }
  971. symindex.deleteindex(ps);
  972. ps.left:=nil;
  973. ps.right:=nil;
  974. { add to symt }
  975. ps.owner:=tsymt;
  976. tsymt.datasize:=ps.fieldoffset+offset;
  977. tsymt.symindex.insert(ps);
  978. tsymt.symsearch.insert(ps);
  979. { update address }
  980. ps.fieldoffset:=tsymt.datasize;
  981. { next }
  982. ps:=nps;
  983. end;
  984. pd:=tdef(defindex.first);
  985. while assigned(pd) do
  986. begin
  987. npd:=tdef(pd.indexnext);
  988. defindex.deleteindex(pd);
  989. pd.left:=nil;
  990. pd.right:=nil;
  991. tsymt.registerdef(pd);
  992. pd:=npd;
  993. end;
  994. tsymt.datasize:=storesize;
  995. tsymt.dataalignment:=storealign;
  996. end;
  997. {****************************************************************************
  998. TObjectSymtable
  999. ****************************************************************************}
  1000. constructor tobjectsymtable.create(const n:string);
  1001. begin
  1002. inherited create(n);
  1003. symtabletype:=objectsymtable;
  1004. end;
  1005. procedure tobjectsymtable.insert(sym:tsymentry);
  1006. var
  1007. hsym : tsym;
  1008. begin
  1009. { check for duplicate field id in inherited classes }
  1010. if (sym.typ=varsym) and
  1011. assigned(defowner) and
  1012. (
  1013. not(m_delphi in aktmodeswitches) or
  1014. is_object(tdef(defowner))
  1015. ) then
  1016. begin
  1017. { but private ids can be reused }
  1018. hsym:=search_class_member(tobjectdef(defowner),sym.name);
  1019. if assigned(hsym) and
  1020. tstoredsym(hsym).is_visible_for_object(tobjectdef(defowner)) then
  1021. begin
  1022. DuplicateSym(hsym);
  1023. exit;
  1024. end;
  1025. end;
  1026. inherited insert(sym);
  1027. end;
  1028. {****************************************************************************
  1029. TAbstractLocalSymtable
  1030. ****************************************************************************}
  1031. procedure tabstractlocalsymtable.ppuwrite(ppufile:tcompilerppufile);
  1032. var
  1033. oldtyp : byte;
  1034. begin
  1035. oldtyp:=ppufile.entrytyp;
  1036. ppufile.entrytyp:=subentryid;
  1037. { write definitions }
  1038. writedefs(ppufile);
  1039. { write symbols }
  1040. writesyms(ppufile);
  1041. ppufile.entrytyp:=oldtyp;
  1042. end;
  1043. {****************************************************************************
  1044. TLocalSymtable
  1045. ****************************************************************************}
  1046. constructor tlocalsymtable.create(level:byte);
  1047. begin
  1048. inherited create('');
  1049. symtabletype:=localsymtable;
  1050. symtablelevel:=level;
  1051. end;
  1052. procedure tlocalsymtable.insert(sym:tsymentry);
  1053. var
  1054. hsym : tsym;
  1055. begin
  1056. { need to hide function result? }
  1057. hsym:=tsym(search(sym.name));
  1058. if assigned(hsym) then
  1059. begin
  1060. { a local and the function can have the same
  1061. name in TP and Delphi, but RESULT not }
  1062. if (m_duplicate_names in aktmodeswitches) and
  1063. (hsym.typ in [absolutesym,varsym]) and
  1064. (vo_is_funcret in tvarsym(hsym).varoptions) and
  1065. not((m_result in aktmodeswitches) and
  1066. (vo_is_result in tvarsym(hsym).varoptions)) then
  1067. hsym.owner.rename(hsym.name,'hidden'+hsym.name)
  1068. else
  1069. begin
  1070. DuplicateSym(hsym);
  1071. exit;
  1072. end;
  1073. end;
  1074. if assigned(next) and
  1075. (next.symtabletype=parasymtable) then
  1076. begin
  1077. { check para symtable }
  1078. hsym:=tsym(next.search(sym.name));
  1079. if assigned(hsym) then
  1080. begin
  1081. { a local and the function can have the same
  1082. name in TP and Delphi, but RESULT not }
  1083. if (m_duplicate_names in aktmodeswitches) and
  1084. (sym.typ in [absolutesym,varsym]) and
  1085. (vo_is_funcret in tvarsym(sym).varoptions) and
  1086. not((m_result in aktmodeswitches) and
  1087. (vo_is_result in tvarsym(sym).varoptions)) then
  1088. sym.name:='hidden'+sym.name
  1089. else
  1090. begin
  1091. DuplicateSym(hsym);
  1092. exit;
  1093. end;
  1094. end;
  1095. { check for duplicate id in local symtable of methods }
  1096. if assigned(next.next) and
  1097. { funcretsym is allowed !! }
  1098. (not is_funcret_sym(sym)) and
  1099. (next.next.symtabletype=objectsymtable) then
  1100. begin
  1101. hsym:=search_class_member(tobjectdef(next.next.defowner),sym.name);
  1102. if assigned(hsym) and
  1103. { private ids can be reused }
  1104. (not(sp_private in hsym.symoptions) or
  1105. (hsym.owner.defowner.owner.symtabletype<>globalsymtable)) then
  1106. begin
  1107. { delphi allows to reuse the names in a class, but not
  1108. in object (tp7 compatible) }
  1109. if not((m_delphi in aktmodeswitches) and
  1110. is_class(tdef(next.next.defowner))) then
  1111. begin
  1112. DuplicateSym(hsym);
  1113. exit;
  1114. end;
  1115. end;
  1116. end;
  1117. end;
  1118. inherited insert(sym);
  1119. end;
  1120. {****************************************************************************
  1121. TParaSymtable
  1122. ****************************************************************************}
  1123. constructor tparasymtable.create(level:byte);
  1124. begin
  1125. inherited create('');
  1126. symtabletype:=parasymtable;
  1127. symtablelevel:=level;
  1128. end;
  1129. procedure tparasymtable.insert(sym:tsymentry);
  1130. var
  1131. hsym : tsym;
  1132. begin
  1133. { check for duplicate id in para symtable of methods }
  1134. if assigned(next) and
  1135. (next.symtabletype=objectsymtable) and
  1136. { funcretsym is allowed }
  1137. (not is_funcret_sym(sym)) then
  1138. begin
  1139. hsym:=search_class_member(tobjectdef(next.defowner),sym.name);
  1140. { private ids can be reused }
  1141. if assigned(hsym) and
  1142. tstoredsym(hsym).is_visible_for_object(tobjectdef(next.defowner)) then
  1143. begin
  1144. { delphi allows to reuse the names in a class, but not
  1145. in object (tp7 compatible) }
  1146. if not((m_delphi in aktmodeswitches) and
  1147. is_class_or_interface(tobjectdef(next.defowner))) then
  1148. begin
  1149. DuplicateSym(hsym);
  1150. exit;
  1151. end;
  1152. end;
  1153. end;
  1154. inherited insert(sym);
  1155. end;
  1156. {****************************************************************************
  1157. TAbstractUnitSymtable
  1158. ****************************************************************************}
  1159. constructor tabstractunitsymtable.create(const n : string);
  1160. begin
  1161. inherited create(n);
  1162. symsearch.usehash;
  1163. {$ifdef GDB}
  1164. { reset GDB things }
  1165. prev_dbx_counter := dbx_counter;
  1166. dbx_counter := nil;
  1167. is_stab_written:=false;
  1168. dbx_count := -1;
  1169. {$endif GDB}
  1170. end;
  1171. {$ifdef GDB}
  1172. procedure tabstractunitsymtable.concattypestabto(asmlist : taasmoutput);
  1173. var prev_dbx_count : plongint;
  1174. begin
  1175. if is_stab_written then
  1176. exit;
  1177. if not assigned(name) then
  1178. name := stringdup('Main_program');
  1179. if (symtabletype = globalsymtable) and
  1180. (current_module.globalsymtable<>self) then
  1181. begin
  1182. unitid:=current_module.unitcount;
  1183. inc(current_module.unitcount);
  1184. end;
  1185. asmList.concat(tai_comment.Create(strpnew('Begin unit '+name^+' has index '+tostr(unitid))));
  1186. if cs_gdb_dbx in aktglobalswitches then
  1187. begin
  1188. if dbx_count_ok then
  1189. begin
  1190. asmList.concat(tai_comment.Create(strpnew('"repeated" unit '+name^
  1191. +' has index '+tostr(unitid)+' dbx count = '+tostr(dbx_count))));
  1192. asmList.concat(Tai_stabs.Create(strpnew('"'+name^+'",'
  1193. +tostr(N_EXCL)+',0,0,'+tostr(dbx_count))));
  1194. exit;
  1195. end
  1196. else if (current_module.globalsymtable<>self) then
  1197. begin
  1198. prev_dbx_count := dbx_counter;
  1199. dbx_counter := nil;
  1200. do_count_dbx:=false;
  1201. if (symtabletype = globalsymtable) and (unitid<>0) then
  1202. asmList.concat(Tai_stabs.Create(strpnew('"'+name^+'",'+tostr(N_BINCL)+',0,0,0')));
  1203. dbx_counter := @dbx_count;
  1204. dbx_count:=0;
  1205. do_count_dbx:=assigned(dbx_counter);
  1206. end;
  1207. end;
  1208. foreach({$ifdef FPCPROCVAR}@{$endif}concattypestab,asmlist);
  1209. if cs_gdb_dbx in aktglobalswitches then
  1210. begin
  1211. if (current_module.globalsymtable<>self) then
  1212. begin
  1213. dbx_counter := prev_dbx_count;
  1214. do_count_dbx:=false;
  1215. asmList.concat(tai_comment.Create(strpnew('End unit '+name^
  1216. +' has index '+tostr(unitid))));
  1217. asmList.concat(Tai_stabs.Create(strpnew('"'+name^+'",'
  1218. +tostr(N_EINCL)+',0,0,0')));
  1219. do_count_dbx:=assigned(dbx_counter);
  1220. dbx_count_ok := {true}false;
  1221. end;
  1222. end;
  1223. is_stab_written:=true;
  1224. end;
  1225. {$endif GDB}
  1226. {****************************************************************************
  1227. TStaticSymtable
  1228. ****************************************************************************}
  1229. constructor tstaticsymtable.create(const n : string);
  1230. begin
  1231. inherited create(n);
  1232. symtabletype:=staticsymtable;
  1233. symtablelevel:=main_program_level;
  1234. end;
  1235. procedure tstaticsymtable.ppuload(ppufile:tcompilerppufile);
  1236. begin
  1237. aktstaticsymtable:=self;
  1238. next:=symtablestack;
  1239. symtablestack:=self;
  1240. inherited ppuload(ppufile);
  1241. { now we can deref the syms and defs }
  1242. deref;
  1243. { restore symtablestack }
  1244. symtablestack:=next;
  1245. end;
  1246. procedure tstaticsymtable.ppuwrite(ppufile:tcompilerppufile);
  1247. begin
  1248. aktstaticsymtable:=self;
  1249. inherited ppuwrite(ppufile);
  1250. end;
  1251. procedure tstaticsymtable.load_references(ppufile:tcompilerppufile;locals:boolean);
  1252. begin
  1253. aktstaticsymtable:=self;
  1254. inherited load_references(ppufile,locals);
  1255. end;
  1256. procedure tstaticsymtable.write_references(ppufile:tcompilerppufile;locals:boolean);
  1257. begin
  1258. aktstaticsymtable:=self;
  1259. inherited write_references(ppufile,locals);
  1260. end;
  1261. procedure tstaticsymtable.insert(sym:tsymentry);
  1262. var
  1263. hsym : tsym;
  1264. begin
  1265. { also check the global symtable }
  1266. if assigned(next) and
  1267. (next.unitid=0) then
  1268. begin
  1269. hsym:=tsym(next.search(sym.name));
  1270. if assigned(hsym) then
  1271. begin
  1272. { Delphi you can have a symbol with the same name as the
  1273. unit, the unit can then not be accessed anymore using
  1274. <unit>.<id>, so we can hide the symbol }
  1275. if (m_duplicate_names in aktmodeswitches) and
  1276. (hsym.typ=symconst.unitsym) then
  1277. hsym.owner.rename(hsym.name,'hidden'+hsym.name)
  1278. else
  1279. begin
  1280. DuplicateSym(hsym);
  1281. exit;
  1282. end;
  1283. end;
  1284. end;
  1285. inherited insert(sym);
  1286. end;
  1287. {****************************************************************************
  1288. TGlobalSymtable
  1289. ****************************************************************************}
  1290. constructor tglobalsymtable.create(const n : string);
  1291. begin
  1292. inherited create(n);
  1293. symtabletype:=globalsymtable;
  1294. symtablelevel:=main_program_level;
  1295. unitid:=0;
  1296. unitsym:=nil;
  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. destructor tglobalsymtable.destroy;
  1316. var
  1317. pus : tunitsym;
  1318. begin
  1319. pus:=unitsym;
  1320. while assigned(pus) do
  1321. begin
  1322. unitsym:=pus.prevsym;
  1323. pus.prevsym:=nil;
  1324. pus.unitsymtable:=nil;
  1325. pus:=unitsym;
  1326. end;
  1327. inherited destroy;
  1328. end;
  1329. procedure tglobalsymtable.ppuload(ppufile:tcompilerppufile);
  1330. {$ifdef GDB}
  1331. var
  1332. b : byte;
  1333. {$endif GDB}
  1334. begin
  1335. {$ifdef GDB}
  1336. if cs_gdb_dbx in aktglobalswitches then
  1337. begin
  1338. UnitTypeCount:=1;
  1339. PglobalTypeCount:=@UnitTypeCount;
  1340. end;
  1341. {$endif GDB}
  1342. aktglobalsymtable:=self;
  1343. next:=symtablestack;
  1344. symtablestack:=self;
  1345. inherited ppuload(ppufile);
  1346. { now we can deref the syms and defs }
  1347. deref;
  1348. { restore symtablestack }
  1349. symtablestack:=next;
  1350. { read dbx count }
  1351. {$ifdef GDB}
  1352. if (current_module.flags and uf_has_dbx)<>0 then
  1353. begin
  1354. b:=ppufile.readentry;
  1355. if b<>ibdbxcount then
  1356. Message(unit_f_ppu_dbx_count_problem)
  1357. else
  1358. dbx_count:=ppufile.getlongint;
  1359. {$IfDef EXTDEBUG}
  1360. writeln('Read dbx_count ',dbx_count,' in unit ',name^,'.ppu');
  1361. {$ENDIF EXTDEBUG}
  1362. { we can't use dbx_vcount, because we don't know
  1363. if the object file will be loaded before or afeter PM }
  1364. dbx_count_ok := {true}false;
  1365. end
  1366. else
  1367. begin
  1368. dbx_count:=-1;
  1369. dbx_count_ok:=false;
  1370. end;
  1371. {$endif GDB}
  1372. end;
  1373. procedure tglobalsymtable.ppuwrite(ppufile:tcompilerppufile);
  1374. begin
  1375. aktglobalsymtable:=self;
  1376. { write the symtable entries }
  1377. inherited ppuwrite(ppufile);
  1378. { write dbx count }
  1379. {$ifdef GDB}
  1380. if cs_gdb_dbx in aktglobalswitches then
  1381. begin
  1382. {$IfDef EXTDEBUG}
  1383. writeln('Writing dbx_count ',dbx_count,' in unit ',name^,'.ppu');
  1384. {$ENDIF EXTDEBUG}
  1385. ppufile.do_crc:=false;
  1386. ppufile.putlongint(dbx_count);
  1387. ppufile.writeentry(ibdbxcount);
  1388. ppufile.do_crc:=true;
  1389. end;
  1390. {$endif GDB}
  1391. end;
  1392. procedure tglobalsymtable.load_references(ppufile:tcompilerppufile;locals:boolean);
  1393. begin
  1394. aktglobalsymtable:=self;
  1395. inherited load_references(ppufile,locals);
  1396. end;
  1397. procedure tglobalsymtable.write_references(ppufile:tcompilerppufile;locals:boolean);
  1398. begin
  1399. aktglobalsymtable:=self;
  1400. inherited write_references(ppufile,locals);
  1401. end;
  1402. procedure tglobalsymtable.insert(sym:tsymentry);
  1403. var
  1404. hsym : tsym;
  1405. begin
  1406. { also check the global symtable }
  1407. if assigned(next) and
  1408. (next.unitid=0) then
  1409. begin
  1410. hsym:=tsym(next.search(sym.name));
  1411. if assigned(hsym) then
  1412. begin
  1413. { Delphi you can have a symbol with the same name as the
  1414. unit, the unit can then not be accessed anymore using
  1415. <unit>.<id>, so we can hide the symbol }
  1416. if (m_duplicate_names in aktmodeswitches) and
  1417. (hsym.typ=symconst.unitsym) then
  1418. hsym.owner.rename(hsym.name,'hidden'+hsym.name)
  1419. else
  1420. begin
  1421. DuplicateSym(hsym);
  1422. exit;
  1423. end;
  1424. end;
  1425. end;
  1426. hsym:=tsym(search(sym.name));
  1427. if assigned(hsym) then
  1428. begin
  1429. { Delphi you can have a symbol with the same name as the
  1430. unit, the unit can then not be accessed anymore using
  1431. <unit>.<id>, so we can hide the symbol }
  1432. if (m_duplicate_names in aktmodeswitches) and
  1433. (hsym.typ=symconst.unitsym) then
  1434. hsym.owner.rename(hsym.name,'hidden'+hsym.name)
  1435. else
  1436. begin
  1437. DuplicateSym(hsym);
  1438. exit;
  1439. end;
  1440. end;
  1441. inherited insert(sym);
  1442. end;
  1443. {$ifdef GDB}
  1444. function tglobalsymtable.getnewtypecount : word;
  1445. begin
  1446. if not (cs_gdb_dbx in aktglobalswitches) then
  1447. getnewtypecount:=inherited getnewtypecount
  1448. else
  1449. begin
  1450. getnewtypecount:=unittypecount;
  1451. inc(unittypecount);
  1452. end;
  1453. end;
  1454. {$endif}
  1455. {****************************************************************************
  1456. TWITHSYMTABLE
  1457. ****************************************************************************}
  1458. constructor twithsymtable.create(aowner:tdef;asymsearch:TDictionary;refnode:pointer{tnode});
  1459. begin
  1460. inherited create('');
  1461. symtabletype:=withsymtable;
  1462. withrefnode:=refnode;
  1463. { we don't need the symsearch }
  1464. symsearch.free;
  1465. { set the defaults }
  1466. symsearch:=asymsearch;
  1467. defowner:=aowner;
  1468. end;
  1469. destructor twithsymtable.destroy;
  1470. begin
  1471. symsearch:=nil;
  1472. inherited destroy;
  1473. end;
  1474. procedure twithsymtable.clear;
  1475. begin
  1476. { remove no entry from a withsymtable as it is only a pointer to the
  1477. recorddef or objectdef symtable }
  1478. end;
  1479. {****************************************************************************
  1480. TSTT_ExceptionSymtable
  1481. ****************************************************************************}
  1482. constructor tstt_exceptsymtable.create;
  1483. begin
  1484. inherited create('');
  1485. symtabletype:=stt_exceptsymtable;
  1486. end;
  1487. {*****************************************************************************
  1488. Helper Routines
  1489. *****************************************************************************}
  1490. function findunitsymtable(st:tsymtable):tsymtable;
  1491. begin
  1492. findunitsymtable:=nil;
  1493. repeat
  1494. if not assigned(st) then
  1495. internalerror(5566561);
  1496. case st.symtabletype of
  1497. localsymtable,
  1498. parasymtable,
  1499. staticsymtable :
  1500. exit;
  1501. globalsymtable :
  1502. begin
  1503. findunitsymtable:=st;
  1504. exit;
  1505. end;
  1506. objectsymtable :
  1507. st:=st.defowner.owner;
  1508. recordsymtable :
  1509. begin
  1510. { don't continue when the current
  1511. symtable is used for variant records }
  1512. if trecorddef(st.defowner).isunion then
  1513. begin
  1514. findunitsymtable:=nil;
  1515. exit;
  1516. end
  1517. else
  1518. st:=st.defowner.owner;
  1519. end;
  1520. else
  1521. internalerror(5566562);
  1522. end;
  1523. until false;
  1524. end;
  1525. procedure duplicatesym(sym:tsym);
  1526. var
  1527. st : tsymtable;
  1528. begin
  1529. Message1(sym_e_duplicate_id,sym.realname);
  1530. st:=findunitsymtable(sym.owner);
  1531. with sym.fileinfo do
  1532. begin
  1533. if assigned(st) and (st.unitid<>0) then
  1534. Message2(sym_h_duplicate_id_where,'unit '+st.name^,tostr(line))
  1535. else
  1536. Message2(sym_h_duplicate_id_where,current_module.sourcefiles.get_file_name(fileindex),tostr(line));
  1537. end;
  1538. end;
  1539. {*****************************************************************************
  1540. Search
  1541. *****************************************************************************}
  1542. function searchsym(const s : stringid;var srsym:tsym;var srsymtable:tsymtable):boolean;
  1543. var
  1544. speedvalue : cardinal;
  1545. begin
  1546. speedvalue:=getspeedvalue(s);
  1547. srsymtable:=symtablestack;
  1548. while assigned(srsymtable) do
  1549. begin
  1550. srsym:=tsym(srsymtable.speedsearch(s,speedvalue));
  1551. if assigned(srsym) and
  1552. (not assigned(current_procinfo) or
  1553. tstoredsym(srsym).is_visible_for_proc(current_procinfo.procdef)) then
  1554. begin
  1555. searchsym:=true;
  1556. exit;
  1557. end
  1558. else
  1559. srsymtable:=srsymtable.next;
  1560. end;
  1561. searchsym:=false;
  1562. end;
  1563. function searchsym_type(const s : stringid;var srsym:tsym;var srsymtable:tsymtable):boolean;
  1564. var
  1565. speedvalue : cardinal;
  1566. begin
  1567. speedvalue:=getspeedvalue(s);
  1568. srsymtable:=symtablestack;
  1569. while assigned(srsymtable) do
  1570. begin
  1571. {
  1572. It is not possible to have type defintions in:
  1573. records
  1574. objects
  1575. parameters
  1576. }
  1577. if not(srsymtable.symtabletype in [recordsymtable,objectsymtable,parasymtable]) then
  1578. begin
  1579. srsym:=tsym(srsymtable.speedsearch(s,speedvalue));
  1580. if assigned(srsym) and
  1581. (not assigned(current_procinfo) or
  1582. tstoredsym(srsym).is_visible_for_proc(current_procinfo.procdef)) then
  1583. begin
  1584. result:=true;
  1585. exit;
  1586. end
  1587. end;
  1588. srsymtable:=srsymtable.next;
  1589. end;
  1590. result:=false;
  1591. end;
  1592. function searchsymonlyin(p : tsymtable;const s : stringid):tsym;
  1593. var
  1594. srsym : tsym;
  1595. begin
  1596. { the caller have to take care if srsym=nil }
  1597. if assigned(p) then
  1598. begin
  1599. srsym:=tsym(p.search(s));
  1600. if assigned(srsym) then
  1601. begin
  1602. searchsymonlyin:=srsym;
  1603. exit;
  1604. end;
  1605. { also check in the local symtbale if it exists }
  1606. if (p=tsymtable(current_module.globalsymtable)) then
  1607. begin
  1608. srsym:=tsym(current_module.localsymtable.search(s));
  1609. if assigned(srsym) then
  1610. begin
  1611. searchsymonlyin:=srsym;
  1612. exit;
  1613. end;
  1614. end
  1615. end;
  1616. searchsymonlyin:=nil;
  1617. end;
  1618. function searchsym_in_class(classh:tobjectdef;const s : stringid):tsym;
  1619. var
  1620. speedvalue : cardinal;
  1621. topclassh : tobjectdef;
  1622. sym : tsym;
  1623. begin
  1624. speedvalue:=getspeedvalue(s);
  1625. { when the class passed is defined in this unit we
  1626. need to use the scope of that class. This is a trick
  1627. that can be used to access protected members in other
  1628. units. At least kylix supports it this way (PFV) }
  1629. if assigned(classh) and
  1630. (classh.owner.symtabletype in [globalsymtable,staticsymtable]) and
  1631. (classh.owner.unitid=0) then
  1632. topclassh:=classh
  1633. else
  1634. topclassh:=nil;
  1635. sym:=nil;
  1636. while assigned(classh) do
  1637. begin
  1638. sym:=tsym(classh.symtable.speedsearch(s,speedvalue));
  1639. if assigned(sym) then
  1640. begin
  1641. if assigned(topclassh) then
  1642. begin
  1643. if tstoredsym(sym).is_visible_for_object(topclassh) then
  1644. break;
  1645. end
  1646. else
  1647. begin
  1648. if (not assigned(current_procinfo) or
  1649. tstoredsym(sym).is_visible_for_proc(current_procinfo.procdef)) then
  1650. break;
  1651. end;
  1652. end;
  1653. classh:=classh.childof;
  1654. end;
  1655. searchsym_in_class:=sym;
  1656. end;
  1657. function searchsym_in_class_by_msgint(classh:tobjectdef;i:longint):tsym;
  1658. var
  1659. topclassh : tobjectdef;
  1660. def : tdef;
  1661. sym : tsym;
  1662. begin
  1663. { when the class passed is defined in this unit we
  1664. need to use the scope of that class. This is a trick
  1665. that can be used to access protected members in other
  1666. units. At least kylix supports it this way (PFV) }
  1667. if assigned(classh) and
  1668. (classh.owner.symtabletype in [globalsymtable,staticsymtable]) and
  1669. (classh.owner.unitid=0) then
  1670. topclassh:=classh
  1671. else
  1672. begin
  1673. if assigned(current_procinfo) then
  1674. topclassh:=current_procinfo.procdef._class
  1675. else
  1676. topclassh:=nil;
  1677. end;
  1678. sym:=nil;
  1679. def:=nil;
  1680. while assigned(classh) do
  1681. begin
  1682. def:=tdef(classh.symtable.defindex.first);
  1683. while assigned(def) do
  1684. begin
  1685. if (def.deftype=procdef) and
  1686. (po_msgint in tprocdef(def).procoptions) and
  1687. (tprocdef(def).messageinf.i=i) then
  1688. begin
  1689. sym:=tprocdef(def).procsym;
  1690. if assigned(topclassh) then
  1691. begin
  1692. if tprocdef(def).is_visible_for_object(topclassh) then
  1693. break;
  1694. end
  1695. else
  1696. break;
  1697. end;
  1698. def:=tdef(def.indexnext);
  1699. end;
  1700. if assigned(sym) then
  1701. break;
  1702. classh:=classh.childof;
  1703. end;
  1704. searchsym_in_class_by_msgint:=sym;
  1705. end;
  1706. function searchsym_in_class_by_msgstr(classh:tobjectdef;const s:string):tsym;
  1707. var
  1708. topclassh : tobjectdef;
  1709. def : tdef;
  1710. sym : tsym;
  1711. begin
  1712. { when the class passed is defined in this unit we
  1713. need to use the scope of that class. This is a trick
  1714. that can be used to access protected members in other
  1715. units. At least kylix supports it this way (PFV) }
  1716. if assigned(classh) and
  1717. (classh.owner.symtabletype in [globalsymtable,staticsymtable]) and
  1718. (classh.owner.unitid=0) then
  1719. topclassh:=classh
  1720. else
  1721. begin
  1722. if assigned(current_procinfo) then
  1723. topclassh:=current_procinfo.procdef._class
  1724. else
  1725. topclassh:=nil;
  1726. end;
  1727. sym:=nil;
  1728. def:=nil;
  1729. while assigned(classh) do
  1730. begin
  1731. def:=tdef(classh.symtable.defindex.first);
  1732. while assigned(def) do
  1733. begin
  1734. if (def.deftype=procdef) and
  1735. (po_msgstr in tprocdef(def).procoptions) and
  1736. (tprocdef(def).messageinf.str=s) then
  1737. begin
  1738. sym:=tprocdef(def).procsym;
  1739. if assigned(topclassh) then
  1740. begin
  1741. if tprocdef(def).is_visible_for_object(topclassh) then
  1742. break;
  1743. end
  1744. else
  1745. break;
  1746. end;
  1747. def:=tdef(def.indexnext);
  1748. end;
  1749. if assigned(sym) then
  1750. break;
  1751. classh:=classh.childof;
  1752. end;
  1753. searchsym_in_class_by_msgstr:=sym;
  1754. end;
  1755. function searchsystype(const s: stringid; var srsym: ttypesym): boolean;
  1756. var
  1757. symowner: tsymtable;
  1758. begin
  1759. if not(cs_compilesystem in aktmoduleswitches) then
  1760. srsym := ttypesym(searchsymonlyin(systemunit,s))
  1761. else
  1762. searchsym(s,tsym(srsym),symowner);
  1763. searchsystype :=
  1764. assigned(srsym) and
  1765. (srsym.typ = typesym);
  1766. end;
  1767. function searchsysvar(const s: stringid; var srsym: tvarsym; var symowner: tsymtable): boolean;
  1768. begin
  1769. if not(cs_compilesystem in aktmoduleswitches) then
  1770. begin
  1771. srsym := tvarsym(searchsymonlyin(systemunit,s));
  1772. symowner := systemunit;
  1773. end
  1774. else
  1775. searchsym(s,tsym(srsym),symowner);
  1776. searchsysvar :=
  1777. assigned(srsym) and
  1778. (srsym.typ = varsym);
  1779. end;
  1780. function search_class_member(pd : tobjectdef;const s : string):tsym;
  1781. { searches n in symtable of pd and all anchestors }
  1782. var
  1783. speedvalue : cardinal;
  1784. srsym : tsym;
  1785. begin
  1786. speedvalue:=getspeedvalue(s);
  1787. while assigned(pd) do
  1788. begin
  1789. srsym:=tsym(pd.symtable.speedsearch(s,speedvalue));
  1790. if assigned(srsym) then
  1791. begin
  1792. search_class_member:=srsym;
  1793. exit;
  1794. end;
  1795. pd:=pd.childof;
  1796. end;
  1797. search_class_member:=nil;
  1798. end;
  1799. {*****************************************************************************
  1800. Definition Helpers
  1801. *****************************************************************************}
  1802. procedure globaldef(const s : string;var t:ttype);
  1803. var st : string;
  1804. symt : tsymtable;
  1805. srsym : tsym;
  1806. srsymtable : tsymtable;
  1807. begin
  1808. srsym := nil;
  1809. if pos('.',s) > 0 then
  1810. begin
  1811. st := copy(s,1,pos('.',s)-1);
  1812. searchsym(st,srsym,srsymtable);
  1813. st := copy(s,pos('.',s)+1,255);
  1814. if assigned(srsym) then
  1815. begin
  1816. if srsym.typ = unitsym then
  1817. begin
  1818. symt := tunitsym(srsym).unitsymtable;
  1819. srsym := tsym(symt.search(st));
  1820. end else srsym := nil;
  1821. end;
  1822. end else st := s;
  1823. if srsym = nil then
  1824. searchsym(st,srsym,srsymtable);
  1825. if srsym = nil then
  1826. srsym:=searchsymonlyin(systemunit,st);
  1827. if (not assigned(srsym)) or
  1828. (srsym.typ<>typesym) then
  1829. begin
  1830. Message(type_e_type_id_expected);
  1831. t:=generrortype;
  1832. exit;
  1833. end;
  1834. t := ttypesym(srsym).restype;
  1835. end;
  1836. {****************************************************************************
  1837. Object Helpers
  1838. ****************************************************************************}
  1839. var
  1840. _defaultprop : tpropertysym;
  1841. procedure tstoredsymtable.testfordefaultproperty(p : TNamedIndexItem;arg:pointer);
  1842. begin
  1843. if (tsym(p).typ=propertysym) and
  1844. (ppo_defaultproperty in tpropertysym(p).propoptions) then
  1845. _defaultprop:=tpropertysym(p);
  1846. end;
  1847. function search_default_property(pd : tobjectdef) : tpropertysym;
  1848. { returns the default property of a class, searches also anchestors }
  1849. begin
  1850. _defaultprop:=nil;
  1851. while assigned(pd) do
  1852. begin
  1853. pd.symtable.foreach({$ifdef FPCPROCVAR}@{$endif}tstoredsymtable(pd.symtable).testfordefaultproperty,nil);
  1854. if assigned(_defaultprop) then
  1855. break;
  1856. pd:=pd.childof;
  1857. end;
  1858. search_default_property:=_defaultprop;
  1859. end;
  1860. {$ifdef UNITALIASES}
  1861. {****************************************************************************
  1862. TUNIT_ALIAS
  1863. ****************************************************************************}
  1864. constructor tunit_alias.create(const n:string);
  1865. var
  1866. i : longint;
  1867. begin
  1868. i:=pos('=',n);
  1869. if i=0 then
  1870. fail;
  1871. inherited createname(Copy(n,1,i-1));
  1872. newname:=stringdup(Copy(n,i+1,255));
  1873. end;
  1874. destructor tunit_alias.destroy;
  1875. begin
  1876. stringdispose(newname);
  1877. inherited destroy;
  1878. end;
  1879. procedure addunitalias(const n:string);
  1880. begin
  1881. unitaliases^.insert(tunit_alias,init(Upper(n))));
  1882. end;
  1883. function getunitalias(const n:string):string;
  1884. var
  1885. p : punit_alias;
  1886. begin
  1887. p:=punit_alias(unitaliases^.search(Upper(n)));
  1888. if assigned(p) then
  1889. getunitalias:=punit_alias(p).newname^
  1890. else
  1891. getunitalias:=n;
  1892. end;
  1893. {$endif UNITALIASES}
  1894. {****************************************************************************
  1895. Symtable Stack
  1896. ****************************************************************************}
  1897. procedure RestoreUnitSyms;
  1898. var
  1899. p : tsymtable;
  1900. begin
  1901. p:=symtablestack;
  1902. while assigned(p) do
  1903. begin
  1904. if (p.symtabletype=globalsymtable) and
  1905. assigned(tglobalsymtable(p).unitsym) and
  1906. ((tglobalsymtable(p).unitsym.owner=current_module.globalsymtable) or
  1907. (tglobalsymtable(p).unitsym.owner=current_module.localsymtable)) then
  1908. tglobalsymtable(p).unitsym.restoreunitsym;
  1909. p:=p.next;
  1910. end;
  1911. end;
  1912. {$ifdef DEBUG}
  1913. procedure test_symtablestack;
  1914. var
  1915. p : tsymtable;
  1916. i : longint;
  1917. begin
  1918. p:=symtablestack;
  1919. i:=0;
  1920. while assigned(p) do
  1921. begin
  1922. inc(i);
  1923. p:=p.next;
  1924. if i>500 then
  1925. Message(sym_f_internal_error_in_symtablestack);
  1926. end;
  1927. end;
  1928. procedure list_symtablestack;
  1929. var
  1930. p : tsymtable;
  1931. i : longint;
  1932. begin
  1933. p:=symtablestack;
  1934. i:=0;
  1935. while assigned(p) do
  1936. begin
  1937. inc(i);
  1938. writeln(i,' ',p.name^);
  1939. p:=p.next;
  1940. if i>500 then
  1941. Message(sym_f_internal_error_in_symtablestack);
  1942. end;
  1943. end;
  1944. {$endif DEBUG}
  1945. {****************************************************************************
  1946. Init/Done Symtable
  1947. ****************************************************************************}
  1948. procedure InitSymtable;
  1949. var
  1950. token : ttoken;
  1951. begin
  1952. { Reset symbolstack }
  1953. registerdef:=false;
  1954. symtablestack:=nil;
  1955. systemunit:=nil;
  1956. {$ifdef GDB}
  1957. firstglobaldef:=nil;
  1958. lastglobaldef:=nil;
  1959. globaltypecount:=1;
  1960. pglobaltypecount:=@globaltypecount;
  1961. {$endif GDB}
  1962. { defs for internal use }
  1963. voidprocdef:=tprocdef.create(unknown_level);
  1964. { create error syms and def }
  1965. generrorsym:=terrorsym.create;
  1966. generrortype.setdef(terrordef.create);
  1967. {$ifdef UNITALIASES}
  1968. { unit aliases }
  1969. unitaliases:=tdictionary.create;
  1970. {$endif}
  1971. for token:=first_overloaded to last_overloaded do
  1972. overloaded_operators[token]:=nil;
  1973. end;
  1974. procedure DoneSymtable;
  1975. begin
  1976. voidprocdef.free;
  1977. generrorsym.free;
  1978. generrortype.def.free;
  1979. {$ifdef UNITALIASES}
  1980. unitaliases.free;
  1981. {$endif}
  1982. end;
  1983. end.
  1984. {
  1985. $Log$
  1986. Revision 1.112 2003-10-02 21:13:46 peter
  1987. * protected visibility fixes
  1988. Revision 1.111 2003/10/01 19:05:33 peter
  1989. * searchsym_type to search for type definitions. It ignores
  1990. records,objects and parameters
  1991. Revision 1.110 2003/09/23 17:56:06 peter
  1992. * locals and paras are allocated in the code generation
  1993. * tvarsym.localloc contains the location of para/local when
  1994. generating code for the current procedure
  1995. Revision 1.109 2003/08/23 22:31:08 peter
  1996. * unchain operators before adding to overloaded list
  1997. Revision 1.108 2003/06/25 18:31:23 peter
  1998. * sym,def resolving partly rewritten to support also parent objects
  1999. not directly available through the uses clause
  2000. Revision 1.107 2003/06/13 21:19:31 peter
  2001. * current_procdef removed, use current_procinfo.procdef instead
  2002. Revision 1.106 2003/06/09 18:26:27 peter
  2003. * para can be the same as function name in delphi
  2004. Revision 1.105 2003/06/08 11:40:00 peter
  2005. * check parast when inserting in localst
  2006. Revision 1.104 2003/06/07 20:26:32 peter
  2007. * re-resolving added instead of reloading from ppu
  2008. * tderef object added to store deref info for resolving
  2009. Revision 1.103 2003/05/25 11:34:17 peter
  2010. * methodpointer self pushing fixed
  2011. Revision 1.102 2003/05/23 14:27:35 peter
  2012. * remove some unit dependencies
  2013. * current_procinfo changes to store more info
  2014. Revision 1.101 2003/05/16 14:32:58 peter
  2015. * fix dup check for hiding the result varsym in localst, the result
  2016. sym was already in the localst when adding the locals
  2017. Revision 1.100 2003/05/15 18:58:53 peter
  2018. * removed selfpointer_offset, vmtpointer_offset
  2019. * tvarsym.adjusted_address
  2020. * address in localsymtable is now in the real direction
  2021. * removed some obsolete globals
  2022. Revision 1.99 2003/05/13 15:17:13 peter
  2023. * fix crash with hiding function result. The function result is now
  2024. inserted as last so the symbol that we are going to insert is the
  2025. result and needs to be renamed instead of the already existing
  2026. symbol
  2027. Revision 1.98 2003/05/11 14:45:12 peter
  2028. * tloadnode does not support objectsymtable,withsymtable anymore
  2029. * withnode cleanup
  2030. * direct with rewritten to use temprefnode
  2031. Revision 1.97 2003/04/27 11:21:34 peter
  2032. * aktprocdef renamed to current_procinfo.procdef
  2033. * procinfo renamed to current_procinfo
  2034. * procinfo will now be stored in current_module so it can be
  2035. cleaned up properly
  2036. * gen_main_procsym changed to create_main_proc and release_main_proc
  2037. to also generate a tprocinfo structure
  2038. * fixed unit implicit initfinal
  2039. Revision 1.96 2003/04/27 07:29:51 peter
  2040. * current_procinfo.procdef cleanup, current_procdef is now always nil when parsing
  2041. a new procdef declaration
  2042. * aktprocsym removed
  2043. * lexlevel removed, use symtable.symtablelevel instead
  2044. * implicit init/final code uses the normal genentry/genexit
  2045. * funcret state checking updated for new funcret handling
  2046. Revision 1.95 2003/04/26 00:33:07 peter
  2047. * vo_is_result flag added for the special RESULT symbol
  2048. Revision 1.94 2003/04/25 20:59:35 peter
  2049. * removed funcretn,funcretsym, function result is now in varsym
  2050. and aliases for result and function name are added using absolutesym
  2051. * vs_hidden parameter for funcret passed in parameter
  2052. * vs_hidden fixes
  2053. * writenode changed to printnode and released from extdebug
  2054. * -vp option added to generate a tree.log with the nodetree
  2055. * nicer printnode for statements, callnode
  2056. Revision 1.93 2003/04/16 07:53:11 jonas
  2057. * calculation of parameter and resultlocation offsets now depends on
  2058. tg.direction instead of if(n)def powerpc
  2059. Revision 1.92 2003/04/05 21:09:32 jonas
  2060. * several ppc/generic result offset related fixes. The "normal" result
  2061. offset seems now to be calculated correctly and a lot of duplicate
  2062. calculations have been removed. Nested functions accessing the parent's
  2063. function result don't work at all though :(
  2064. Revision 1.91 2003/03/17 18:56:49 peter
  2065. * ignore hints for default parameter values
  2066. Revision 1.90 2003/03/17 16:54:41 peter
  2067. * support DefaultHandler and anonymous inheritance fixed
  2068. for message methods
  2069. Revision 1.89 2002/12/29 14:57:50 peter
  2070. * unit loading changed to first register units and load them
  2071. afterwards. This is needed to support uses xxx in yyy correctly
  2072. * unit dependency check fixed
  2073. Revision 1.88 2002/12/27 18:07:45 peter
  2074. * fix crashes when searching symbols
  2075. Revision 1.87 2002/12/25 01:26:56 peter
  2076. * duplicate procsym-unitsym fix
  2077. Revision 1.86 2002/12/21 13:07:34 peter
  2078. * type redefine fix for tb0437
  2079. Revision 1.85 2002/12/07 14:27:10 carl
  2080. * 3% memory optimization
  2081. * changed some types
  2082. + added type checking with different size for call node and for
  2083. parameters
  2084. Revision 1.84 2002/12/06 17:51:11 peter
  2085. * merged cdecl and array fixes
  2086. Revision 1.83 2002/11/30 11:12:48 carl
  2087. + checking for symbols used with hint directives is done mostly in pexpr
  2088. only now
  2089. Revision 1.82 2002/11/29 22:31:20 carl
  2090. + unimplemented hint directive added
  2091. * hint directive parsing implemented
  2092. * warning on these directives
  2093. Revision 1.81 2002/11/27 20:04:09 peter
  2094. * tvarsym.get_push_size replaced by paramanager.push_size
  2095. Revision 1.80 2002/11/22 22:45:49 carl
  2096. + small optimization for speed
  2097. Revision 1.79 2002/11/19 16:26:33 pierre
  2098. * correct a stabs generation problem that lead to use errordef in stabs
  2099. Revision 1.78 2002/11/18 17:32:00 peter
  2100. * pass proccalloption to ret_in_xxx and push_xxx functions
  2101. Revision 1.77 2002/11/15 01:58:54 peter
  2102. * merged changes from 1.0.7 up to 04-11
  2103. - -V option for generating bug report tracing
  2104. - more tracing for option parsing
  2105. - errors for cdecl and high()
  2106. - win32 import stabs
  2107. - win32 records<=8 are returned in eax:edx (turned off by default)
  2108. - heaptrc update
  2109. - more info for temp management in .s file with EXTDEBUG
  2110. Revision 1.76 2002/11/09 15:29:28 carl
  2111. + bss / constant alignment fixes
  2112. * avoid incrementing address/datasize in local symtable for const's
  2113. Revision 1.75 2002/10/14 19:44:43 peter
  2114. * threadvars need 4 bytes extra for storing the threadvar index
  2115. Revision 1.74 2002/10/06 19:41:31 peter
  2116. * Add finalization of typed consts
  2117. * Finalization of globals in the main program
  2118. Revision 1.73 2002/10/05 12:43:29 carl
  2119. * fixes for Delphi 6 compilation
  2120. (warning : Some features do not work under Delphi)
  2121. Revision 1.72 2002/09/09 19:41:46 peter
  2122. * real fix internalerror for dup ids in union sym
  2123. Revision 1.71 2002/09/09 17:34:16 peter
  2124. * tdicationary.replace added to replace and item in a dictionary. This
  2125. is only allowed for the same name
  2126. * varsyms are inserted in symtable before the types are parsed. This
  2127. fixes the long standing "var longint : longint" bug
  2128. - consume_idlist and idstringlist removed. The loops are inserted
  2129. at the callers place and uses the symtable for duplicate id checking
  2130. Revision 1.70 2002/09/05 19:29:45 peter
  2131. * memdebug enhancements
  2132. Revision 1.69 2002/08/25 19:25:21 peter
  2133. * sym.insert_in_data removed
  2134. * symtable.insertvardata/insertconstdata added
  2135. * removed insert_in_data call from symtable.insert, it needs to be
  2136. called separatly. This allows to deref the address calculation
  2137. * procedures now calculate the parast addresses after the procedure
  2138. directives are parsed. This fixes the cdecl parast problem
  2139. * push_addr_param has an extra argument that specifies if cdecl is used
  2140. or not
  2141. Revision 1.68 2002/08/18 20:06:27 peter
  2142. * inlining is now also allowed in interface
  2143. * renamed write/load to ppuwrite/ppuload
  2144. * tnode storing in ppu
  2145. * nld,ncon,nbas are already updated for storing in ppu
  2146. Revision 1.67 2002/08/17 09:23:43 florian
  2147. * first part of procinfo rewrite
  2148. Revision 1.66 2002/08/11 13:24:15 peter
  2149. * saving of asmsymbols in ppu supported
  2150. * asmsymbollist global is removed and moved into a new class
  2151. tasmlibrarydata that will hold the info of a .a file which
  2152. corresponds with a single module. Added librarydata to tmodule
  2153. to keep the library info stored for the module. In the future the
  2154. objectfiles will also be stored to the tasmlibrarydata class
  2155. * all getlabel/newasmsymbol and friends are moved to the new class
  2156. Revision 1.65 2002/07/23 09:51:27 daniel
  2157. * Tried to make Tprocsym.defs protected. I didn't succeed but the cleanups
  2158. are worth comitting.
  2159. Revision 1.64 2002/07/16 15:34:21 florian
  2160. * exit is now a syssym instead of a keyword
  2161. Revision 1.63 2002/07/15 19:44:53 florian
  2162. * fixed crash with default parameters and stdcall calling convention
  2163. Revision 1.62 2002/07/01 18:46:28 peter
  2164. * internal linker
  2165. * reorganized aasm layer
  2166. Revision 1.61 2002/05/18 13:34:19 peter
  2167. * readded missing revisions
  2168. Revision 1.60 2002/05/16 19:46:45 carl
  2169. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  2170. + try to fix temp allocation (still in ifdef)
  2171. + generic constructor calls
  2172. + start of tassembler / tmodulebase class cleanup
  2173. Revision 1.58 2002/05/12 16:53:15 peter
  2174. * moved entry and exitcode to ncgutil and cgobj
  2175. * foreach gets extra argument for passing local data to the
  2176. iterator function
  2177. * -CR checks also class typecasts at runtime by changing them
  2178. into as
  2179. * fixed compiler to cycle with the -CR option
  2180. * fixed stabs with elf writer, finally the global variables can
  2181. be watched
  2182. * removed a lot of routines from cga unit and replaced them by
  2183. calls to cgobj
  2184. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  2185. u32bit then the other is typecasted also to u32bit without giving
  2186. a rangecheck warning/error.
  2187. * fixed pascal calling method with reversing also the high tree in
  2188. the parast, detected by tcalcst3 test
  2189. Revision 1.57 2002/04/04 19:06:05 peter
  2190. * removed unused units
  2191. * use tlocation.size in cg.a_*loc*() routines
  2192. Revision 1.56 2002/03/04 19:10:11 peter
  2193. * removed compiler warnings
  2194. Revision 1.55 2002/02/03 09:30:07 peter
  2195. * more fixes for protected handling
  2196. Revision 1.54 2002/01/29 21:30:25 peter
  2197. * allow also dup id in delphi mode in interfaces
  2198. Revision 1.53 2002/01/29 19:46:00 peter
  2199. * fixed recordsymtable.insert_in() for inserting variant record fields
  2200. to not used symtable.insert() because that also updates alignmentinfo
  2201. which was already set
  2202. Revision 1.52 2002/01/24 18:25:50 peter
  2203. * implicit result variable generation for assembler routines
  2204. * removed m_tp modeswitch, use m_tp7 or not(m_fpc) instead
  2205. }