symtable.pas 81 KB

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