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