symtable.pas 81 KB

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