symtable.pas 83 KB

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