symtable.pas 80 KB

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