symtable.pas 85 KB

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