2
0

symtable.pas 71 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087
  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,aasmdata
  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(sym:TObject;arg:pointer);
  40. procedure check_forward(sym:TObject;arg:pointer);
  41. procedure labeldefined(sym:TObject;arg:pointer);
  42. procedure varsymbolused(sym:TObject;arg:pointer);
  43. procedure TestPrivate(sym:TObject;arg:pointer);
  44. procedure objectprivatesymbolused(sym:TObject;arg:pointer);
  45. procedure unchain_overloads(sym:TObject;arg:pointer);
  46. procedure loaddefs(ppufile:tcompilerppufile);
  47. procedure loadsyms(ppufile:tcompilerppufile);
  48. procedure writedefs(ppufile:tcompilerppufile);
  49. procedure writesyms(ppufile:tcompilerppufile);
  50. public
  51. { load/write }
  52. procedure ppuload(ppufile:tcompilerppufile);virtual;
  53. procedure ppuwrite(ppufile:tcompilerppufile);virtual;
  54. procedure buildderef;virtual;
  55. procedure buildderefimpl;virtual;
  56. procedure deref;virtual;
  57. procedure derefimpl;virtual;
  58. function checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;override;
  59. procedure reset_all_defs;virtual;
  60. procedure allsymbolsused;
  61. procedure allprivatesused;
  62. procedure check_forwards;
  63. procedure checklabels;
  64. function needs_init_final : boolean;
  65. procedure unchain_overloaded;
  66. procedure testfordefaultproperty(sym:TObject;arg:pointer);
  67. end;
  68. tabstractrecordsymtable = class(tstoredsymtable)
  69. public
  70. usefieldalignment, { alignment to use for fields (PACKRECORDS value), C_alignment is C style }
  71. recordalignment, { alignment required when inserting this record }
  72. fieldalignment, { alignment current alignment used when fields are inserted }
  73. padalignment : shortint; { size to a multiple of which the symtable has to be rounded up }
  74. constructor create(const n:string;usealign:shortint);
  75. procedure ppuload(ppufile:tcompilerppufile);override;
  76. procedure ppuwrite(ppufile:tcompilerppufile);override;
  77. procedure addfield(sym:tfieldvarsym);
  78. procedure insertfield(sym:tfieldvarsym);
  79. procedure addalignmentpadding;
  80. procedure insertdef(def:TDefEntry);override;
  81. function is_packed: boolean;
  82. protected
  83. procedure setdatasize(val: aint);
  84. _datasize : aint;
  85. { size in bits of the data in case of bitpacked record. Only important during construction, }
  86. { no need to save in/restore from ppu file. datasize is always (databitsize+7) div 8. }
  87. databitsize : aint;
  88. { bitpacked? -> all fieldvarsym offsets are in bits instead of bytes }
  89. public
  90. property datasize : aint read _datasize write setdatasize;
  91. end;
  92. trecordsymtable = class(tabstractrecordsymtable)
  93. public
  94. constructor create(usealign:shortint);
  95. procedure insertunionst(unionst : trecordsymtable;offset : longint);
  96. end;
  97. tObjectSymtable = class(tabstractrecordsymtable)
  98. public
  99. constructor create(adefowner:tdef;const n:string;usealign:shortint);
  100. function checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;override;
  101. end;
  102. { tabstractlocalsymtable }
  103. tabstractlocalsymtable = class(tstoredsymtable)
  104. public
  105. procedure ppuwrite(ppufile:tcompilerppufile);override;
  106. function count_locals:longint;
  107. end;
  108. tlocalsymtable = class(tabstractlocalsymtable)
  109. public
  110. constructor create(adefowner:tdef;level:byte);
  111. function checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;override;
  112. end;
  113. tparasymtable = class(tabstractlocalsymtable)
  114. public
  115. constructor create(adefowner:tdef;level:byte);
  116. function checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;override;
  117. end;
  118. tabstractuniTSymtable = class(tstoredsymtable)
  119. public
  120. constructor create(const n : string;id:word);
  121. function iscurrentunit:boolean;override;
  122. end;
  123. tglobalsymtable = class(tabstractuniTSymtable)
  124. public
  125. unittypecount : word;
  126. constructor create(const n : string;id:word);
  127. procedure ppuload(ppufile:tcompilerppufile);override;
  128. procedure ppuwrite(ppufile:tcompilerppufile);override;
  129. function checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;override;
  130. end;
  131. tstaticsymtable = class(tabstractuniTSymtable)
  132. public
  133. constructor create(const n : string;id:word);
  134. procedure ppuload(ppufile:tcompilerppufile);override;
  135. procedure ppuwrite(ppufile:tcompilerppufile);override;
  136. function checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;override;
  137. end;
  138. twithsymtable = class(TSymtable)
  139. withrefnode : tobject; { tnode }
  140. constructor create(aowner:tdef;ASymList:TFPHashObjectList;refnode:tobject{tnode});
  141. destructor destroy;override;
  142. procedure clear;override;
  143. procedure insertdef(def:TDefEntry);override;
  144. end;
  145. tstt_excepTSymtable = class(TSymtable)
  146. public
  147. constructor create;
  148. end;
  149. tmacrosymtable = class(tstoredsymtable)
  150. public
  151. constructor create(exported: boolean);
  152. end;
  153. var
  154. systemunit : tglobalsymtable; { pointer to the system unit }
  155. {****************************************************************************
  156. Functions
  157. ****************************************************************************}
  158. {*** Misc ***}
  159. function FullTypeName(def,otherdef:tdef):string;
  160. procedure incompatibletypes(def1,def2:tdef);
  161. procedure hidesym(sym:TSymEntry);
  162. procedure duplicatesym(var hashedid:THashedIDString;dupsym,origsym:TSymEntry);
  163. {*** Search ***}
  164. procedure addsymref(sym:tsym);
  165. function searchsym(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  166. function searchsym_type(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  167. function searchsym_in_module(pm:pointer;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  168. function searchsym_in_class(classh,contextclassh:tobjectdef;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  169. function searchsym_in_class_by_msgint(classh:tobjectdef;msgid:longint;out srdef : tdef;out srsym:tsym;out srsymtable:TSymtable):boolean;
  170. function searchsym_in_class_by_msgstr(classh:tobjectdef;const s:string;out srsym:tsym;out srsymtable:TSymtable):boolean;
  171. function search_system_type(const s: TIDString): ttypesym;
  172. function search_class_member(pd : tobjectdef;const s : string):tsym;
  173. function search_assignment_operator(from_def,to_def:Tdef):Tprocdef;
  174. {Looks for macro s (must be given in upper case) in the macrosymbolstack, }
  175. {and returns it if found. Returns nil otherwise.}
  176. function search_macro(const s : string):tsym;
  177. {*** Object Helpers ***}
  178. procedure search_class_overloads(aprocsym : tprocsym);
  179. function search_default_property(pd : tobjectdef) : tpropertysym;
  180. {*** Macro Helpers ***}
  181. {If called initially, the following procedures manipulate macros in }
  182. {initialmacrotable, otherwise they manipulate system macros local to a module.}
  183. {Name can be given in any case (it will be converted to upper case).}
  184. procedure def_system_macro(const name : string);
  185. procedure set_system_macro(const name, value : string);
  186. procedure set_system_compvar(const name, value : string);
  187. procedure undef_system_macro(const name : string);
  188. {*** symtable stack ***}
  189. { $ifdef DEBUG
  190. procedure test_symtablestack;
  191. procedure list_symtablestack;
  192. $endif DEBUG}
  193. {$ifdef UNITALIASES}
  194. type
  195. punit_alias = ^tunit_alias;
  196. tunit_alias = object(TNamedIndexItem)
  197. newname : pshortstring;
  198. constructor init(const n:string);
  199. destructor done;virtual;
  200. end;
  201. var
  202. unitaliases : pdictionary;
  203. procedure addunitalias(const n:string);
  204. function getunitalias(const n:string):string;
  205. {$endif UNITALIASES}
  206. {*** Init / Done ***}
  207. procedure IniTSymtable;
  208. procedure DoneSymtable;
  209. const
  210. overloaded_names : array [NOTOKEN..last_overloaded] of string[16] =
  211. ('error',
  212. 'plus','minus','star','slash','equal',
  213. 'greater','lower','greater_or_equal',
  214. 'lower_or_equal',
  215. 'sym_diff','starstar',
  216. 'as','is','in','or',
  217. 'and','div','mod','not','shl','shr','xor',
  218. 'assign');
  219. implementation
  220. uses
  221. { global }
  222. verbose,globals,
  223. { target }
  224. systems,
  225. { symtable }
  226. symutil,defcmp,defutil,
  227. { module }
  228. fmodule,
  229. { codegen }
  230. procinfo
  231. ;
  232. var
  233. dupnr : longint; { unique number for duplicate symbols }
  234. {*****************************************************************************
  235. TStoredSymtable
  236. *****************************************************************************}
  237. procedure tstoredsymtable.ppuload(ppufile:tcompilerppufile);
  238. begin
  239. { load definitions }
  240. loaddefs(ppufile);
  241. { load symbols }
  242. loadsyms(ppufile);
  243. end;
  244. procedure tstoredsymtable.ppuwrite(ppufile:tcompilerppufile);
  245. begin
  246. { write definitions }
  247. writedefs(ppufile);
  248. { write symbols }
  249. writesyms(ppufile);
  250. end;
  251. procedure tstoredsymtable.loaddefs(ppufile:tcompilerppufile);
  252. var
  253. def : tdef;
  254. b : byte;
  255. begin
  256. { load start of definition section, which holds the amount of defs }
  257. if ppufile.readentry<>ibstartdefs then
  258. Message(unit_f_ppu_read_error);
  259. { read definitions }
  260. repeat
  261. b:=ppufile.readentry;
  262. case b of
  263. ibpointerdef : def:=tpointerdef.ppuload(ppufile);
  264. ibarraydef : def:=tarraydef.ppuload(ppufile);
  265. iborddef : def:=torddef.ppuload(ppufile);
  266. ibfloatdef : def:=tfloatdef.ppuload(ppufile);
  267. ibprocdef : def:=tprocdef.ppuload(ppufile);
  268. ibshortstringdef : def:=tstringdef.loadshort(ppufile);
  269. iblongstringdef : def:=tstringdef.loadlong(ppufile);
  270. ibansistringdef : def:=tstringdef.loadansi(ppufile);
  271. ibwidestringdef : def:=tstringdef.loadwide(ppufile);
  272. ibrecorddef : def:=trecorddef.ppuload(ppufile);
  273. ibobjectdef : def:=tobjectdef.ppuload(ppufile);
  274. ibenumdef : def:=tenumdef.ppuload(ppufile);
  275. ibsetdef : def:=tsetdef.ppuload(ppufile);
  276. ibprocvardef : def:=tprocvardef.ppuload(ppufile);
  277. ibfiledef : def:=tfiledef.ppuload(ppufile);
  278. ibclassrefdef : def:=tclassrefdef.ppuload(ppufile);
  279. ibformaldef : def:=tformaldef.ppuload(ppufile);
  280. ibvariantdef : def:=tvariantdef.ppuload(ppufile);
  281. ibundefineddef : def:=tundefineddef.ppuload(ppufile);
  282. ibenddefs : break;
  283. ibend : Message(unit_f_ppu_read_error);
  284. else
  285. Message1(unit_f_ppu_invalid_entry,tostr(b));
  286. end;
  287. InsertDef(def);
  288. until false;
  289. end;
  290. procedure tstoredsymtable.loadsyms(ppufile:tcompilerppufile);
  291. var
  292. b : byte;
  293. sym : tsym;
  294. begin
  295. { load start of definition section, which holds the amount of defs }
  296. if ppufile.readentry<>ibstartsyms then
  297. Message(unit_f_ppu_read_error);
  298. { now read the symbols }
  299. repeat
  300. b:=ppufile.readentry;
  301. case b of
  302. ibtypesym : sym:=ttypesym.ppuload(ppufile);
  303. ibprocsym : sym:=tprocsym.ppuload(ppufile);
  304. ibconstsym : sym:=tconstsym.ppuload(ppufile);
  305. ibstaticvarsym : sym:=tstaticvarsym.ppuload(ppufile);
  306. iblocalvarsym : sym:=tlocalvarsym.ppuload(ppufile);
  307. ibparavarsym : sym:=tparavarsym.ppuload(ppufile);
  308. ibfieldvarsym : sym:=tfieldvarsym.ppuload(ppufile);
  309. ibabsolutevarsym : sym:=tabsolutevarsym.ppuload(ppufile);
  310. ibenumsym : sym:=tenumsym.ppuload(ppufile);
  311. ibpropertysym : sym:=tpropertysym.ppuload(ppufile);
  312. ibunitsym : sym:=tunitsym.ppuload(ppufile);
  313. iblabelsym : sym:=tlabelsym.ppuload(ppufile);
  314. ibsyssym : sym:=tsyssym.ppuload(ppufile);
  315. ibmacrosym : sym:=tmacro.ppuload(ppufile);
  316. ibendsyms : break;
  317. ibend : Message(unit_f_ppu_read_error);
  318. else
  319. Message1(unit_f_ppu_invalid_entry,tostr(b));
  320. end;
  321. Insert(sym,false);
  322. until false;
  323. end;
  324. procedure tstoredsymtable.writedefs(ppufile:tcompilerppufile);
  325. var
  326. i : longint;
  327. def : tstoreddef;
  328. begin
  329. { each definition get a number, write then the amount of defs to the
  330. ibstartdef entry }
  331. ppufile.putlongint(DefList.count);
  332. ppufile.writeentry(ibstartdefs);
  333. { now write the definition }
  334. for i:=0 to DefList.Count-1 do
  335. begin
  336. def:=tstoreddef(DefList[i]);
  337. def.ppuwrite(ppufile);
  338. end;
  339. { write end of definitions }
  340. ppufile.writeentry(ibenddefs);
  341. end;
  342. procedure tstoredsymtable.writesyms(ppufile:tcompilerppufile);
  343. var
  344. i : longint;
  345. sym : Tstoredsym;
  346. begin
  347. { each definition get a number, write then the amount of syms and the
  348. datasize to the ibsymdef entry }
  349. ppufile.putlongint(SymList.count);
  350. ppufile.writeentry(ibstartsyms);
  351. { foreach is used to write all symbols }
  352. for i:=0 to SymList.Count-1 do
  353. begin
  354. sym:=tstoredsym(SymList[i]);
  355. sym.ppuwrite(ppufile);
  356. end;
  357. { end of symbols }
  358. ppufile.writeentry(ibendsyms);
  359. end;
  360. procedure tstoredsymtable.buildderef;
  361. var
  362. i : longint;
  363. def : tstoreddef;
  364. sym : tstoredsym;
  365. begin
  366. { interface definitions }
  367. for i:=0 to DefList.Count-1 do
  368. begin
  369. def:=tstoreddef(DefList[i]);
  370. def.buildderef;
  371. end;
  372. { interface symbols }
  373. for i:=0 to SymList.Count-1 do
  374. begin
  375. sym:=tstoredsym(SymList[i]);
  376. sym.buildderef;
  377. end;
  378. end;
  379. procedure tstoredsymtable.buildderefimpl;
  380. var
  381. i : longint;
  382. def : tstoreddef;
  383. begin
  384. { implementation definitions }
  385. for i:=0 to DefList.Count-1 do
  386. begin
  387. def:=tstoreddef(DefList[i]);
  388. def.buildderefimpl;
  389. end;
  390. end;
  391. procedure tstoredsymtable.deref;
  392. var
  393. i : longint;
  394. def : tstoreddef;
  395. sym : tstoredsym;
  396. begin
  397. { first deref the interface ttype symbols. This is needs
  398. to be done before the interface defs are derefed, because
  399. the interface defs can contain references to the type symbols
  400. which then already need to contain a resolved typedef field (PFV) }
  401. for i:=0 to SymList.Count-1 do
  402. begin
  403. sym:=tstoredsym(SymList[i]);
  404. if sym.typ=typesym then
  405. sym.deref;
  406. end;
  407. { interface definitions }
  408. for i:=0 to DefList.Count-1 do
  409. begin
  410. def:=tstoreddef(DefList[i]);
  411. def.deref;
  412. end;
  413. { interface symbols }
  414. for i:=0 to SymList.Count-1 do
  415. begin
  416. sym:=tstoredsym(SymList[i]);
  417. if sym.typ<>typesym then
  418. sym.deref;
  419. end;
  420. end;
  421. procedure tstoredsymtable.derefimpl;
  422. var
  423. i : longint;
  424. def : tstoreddef;
  425. begin
  426. { implementation definitions }
  427. for i:=0 to DefList.Count-1 do
  428. begin
  429. def:=tstoreddef(DefList[i]);
  430. def.derefimpl;
  431. end;
  432. end;
  433. function tstoredsymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  434. var
  435. hsym : tsym;
  436. begin
  437. hsym:=tsym(FindWithHash(hashedid));
  438. if assigned(hsym) then
  439. DuplicateSym(hashedid,sym,hsym);
  440. result:=assigned(hsym);
  441. end;
  442. {**************************************
  443. Callbacks
  444. **************************************}
  445. procedure TStoredSymtable.check_forward(sym:TObject;arg:pointer);
  446. begin
  447. if tsym(sym).typ=procsym then
  448. tprocsym(sym).check_forward
  449. { check also object method table }
  450. { we needn't to test the def list }
  451. { because each object has to have a type sym,
  452. only test objects declarations, not type renamings }
  453. else
  454. if (tsym(sym).typ=typesym) and
  455. assigned(ttypesym(sym).typedef) and
  456. (ttypesym(sym).typedef.typesym=ttypesym(sym)) and
  457. (ttypesym(sym).typedef.typ=objectdef) then
  458. tobjectdef(ttypesym(sym).typedef).check_forwards;
  459. end;
  460. procedure TStoredSymtable.labeldefined(sym:TObject;arg:pointer);
  461. begin
  462. if (tsym(sym).typ=labelsym) and
  463. not(tlabelsym(sym).defined) then
  464. begin
  465. if tlabelsym(sym).used then
  466. Message1(sym_e_label_used_and_not_defined,tlabelsym(sym).realname)
  467. else
  468. Message1(sym_w_label_not_defined,tlabelsym(sym).realname);
  469. end;
  470. end;
  471. procedure TStoredSymtable.varsymbolused(sym:TObject;arg:pointer);
  472. begin
  473. if (tsym(sym).typ in [staticvarsym,localvarsym,paravarsym,fieldvarsym]) and
  474. ((tsym(sym).owner.symtabletype in
  475. [parasymtable,localsymtable,ObjectSymtable,staticsymtable])) then
  476. begin
  477. { unused symbol should be reported only if no }
  478. { error is reported }
  479. { if the symbol is in a register it is used }
  480. { also don't count the value parameters which have local copies }
  481. { also don't claim for high param of open parameters (PM) }
  482. if (Errorcount<>0) or
  483. ([vo_is_hidden_para,vo_is_funcret] * tabstractvarsym(sym).varoptions = [vo_is_hidden_para]) then
  484. exit;
  485. if (tstoredsym(sym).refs=0) then
  486. begin
  487. if (vo_is_funcret in tabstractvarsym(sym).varoptions) then
  488. begin
  489. { don't warn about the result of constructors }
  490. if (tsym(sym).owner.symtabletype<>localsymtable) or
  491. (tprocdef(tsym(sym).owner.defowner).proctypeoption<>potype_constructor) then
  492. MessagePos(tsym(sym).fileinfo,sym_w_function_result_not_set)
  493. end
  494. else if (tsym(sym).owner.symtabletype=parasymtable) then
  495. MessagePos1(tsym(sym).fileinfo,sym_h_para_identifier_not_used,tsym(sym).realname)
  496. else if (tsym(sym).owner.symtabletype=ObjectSymtable) then
  497. MessagePos2(tsym(sym).fileinfo,sym_n_private_identifier_not_used,tsym(sym).owner.realname^,tsym(sym).realname)
  498. else
  499. MessagePos1(tsym(sym).fileinfo,sym_n_local_identifier_not_used,tsym(sym).realname);
  500. end
  501. else if tabstractvarsym(sym).varstate in [vs_written,vs_initialised] then
  502. begin
  503. if (tsym(sym).owner.symtabletype=parasymtable) then
  504. begin
  505. if not(tabstractvarsym(sym).varspez in [vs_var,vs_out]) and
  506. not(vo_is_funcret in tabstractvarsym(sym).varoptions) then
  507. MessagePos1(tsym(sym).fileinfo,sym_h_para_identifier_only_set,tsym(sym).realname)
  508. end
  509. else if (tsym(sym).owner.symtabletype=ObjectSymtable) then
  510. MessagePos2(tsym(sym).fileinfo,sym_n_private_identifier_only_set,tsym(sym).owner.realname^,tsym(sym).realname)
  511. else if not(vo_is_public in tabstractvarsym(sym).varoptions) and
  512. not(vo_is_funcret in tabstractvarsym(sym).varoptions) then
  513. MessagePos1(tsym(sym).fileinfo,sym_n_local_identifier_only_set,tsym(sym).realname);
  514. end
  515. else if (tabstractvarsym(sym).varstate = vs_read_not_warned) and
  516. ([vo_is_public,vo_is_external] * tabstractvarsym(sym).varoptions = []) then
  517. MessagePos1(tsym(sym).fileinfo,sym_w_identifier_only_read,tsym(sym).realname)
  518. end
  519. else if ((tsym(sym).owner.symtabletype in
  520. [ObjectSymtable,parasymtable,localsymtable,staticsymtable])) then
  521. begin
  522. if (Errorcount<>0) or
  523. (sp_internal in tsym(sym).symoptions) then
  524. exit;
  525. { do not claim for inherited private fields !! }
  526. if (Tsym(sym).refs=0) and (tsym(sym).owner.symtabletype=ObjectSymtable) then
  527. MessagePos2(tsym(sym).fileinfo,sym_n_private_method_not_used,tsym(sym).owner.realname^,tsym(sym).realname)
  528. { units references are problematic }
  529. else
  530. begin
  531. if (Tsym(sym).refs=0) and
  532. not(tsym(sym).typ in [enumsym,unitsym]) and
  533. not(is_funcret_sym(tsym(sym))) and
  534. (
  535. (tsym(sym).typ<>procsym) or
  536. ((tsym(sym).owner.symtabletype=staticsymtable) and
  537. not current_module.is_unit)
  538. ) then
  539. MessagePos2(tsym(sym).fileinfo,sym_h_local_symbol_not_used,SymTypeName[tsym(sym).typ],tsym(sym).realname);
  540. end;
  541. end;
  542. end;
  543. procedure TStoredSymtable.TestPrivate(sym:TObject;arg:pointer);
  544. begin
  545. if sp_private in tsym(sym).symoptions then
  546. varsymbolused(sym,arg);
  547. end;
  548. procedure TStoredSymtable.objectprivatesymbolused(sym:TObject;arg:pointer);
  549. begin
  550. {
  551. Don't test simple object aliases PM
  552. }
  553. if (tsym(sym).typ=typesym) and
  554. (ttypesym(sym).typedef.typ=objectdef) and
  555. (ttypesym(sym).typedef.typesym=tsym(sym)) then
  556. tobjectdef(ttypesym(sym).typedef).symtable.SymList.ForEachCall(@TestPrivate,nil);
  557. end;
  558. procedure tstoredsymtable.unchain_overloads(sym:TObject;arg:pointer);
  559. begin
  560. if tsym(sym).typ=procsym then
  561. tprocsym(sym).unchain_overload;
  562. end;
  563. {***********************************************
  564. Process all entries
  565. ***********************************************}
  566. procedure Tstoredsymtable.reset_all_defs;
  567. var
  568. i : longint;
  569. def : tstoreddef;
  570. begin
  571. for i:=0 to DefList.Count-1 do
  572. begin
  573. def:=tstoreddef(DefList[i]);
  574. def.reset;
  575. end;
  576. end;
  577. { checks, if all procsyms and methods are defined }
  578. procedure tstoredsymtable.check_forwards;
  579. begin
  580. SymList.ForEachCall(@check_forward,nil);
  581. end;
  582. procedure tstoredsymtable.checklabels;
  583. begin
  584. SymList.ForEachCall(@labeldefined,nil);
  585. end;
  586. procedure tstoredsymtable.allsymbolsused;
  587. begin
  588. SymList.ForEachCall(@varsymbolused,nil);
  589. end;
  590. procedure tstoredsymtable.allprivatesused;
  591. begin
  592. SymList.ForEachCall(@objectprivatesymbolused,nil);
  593. end;
  594. procedure tstoredsymtable.unchain_overloaded;
  595. begin
  596. SymList.ForEachCall(@unchain_overloads,nil);
  597. end;
  598. procedure TStoredSymtable._needs_init_final(sym:TObject;arg:pointer);
  599. begin
  600. if b_needs_init_final then
  601. exit;
  602. case tsym(sym).typ of
  603. fieldvarsym,
  604. staticvarsym,
  605. localvarsym,
  606. paravarsym :
  607. begin
  608. if not(is_class(tabstractvarsym(sym).vardef)) and
  609. tstoreddef(tabstractvarsym(sym).vardef).needs_inittable then
  610. b_needs_init_final:=true;
  611. end;
  612. end;
  613. end;
  614. { returns true, if p contains data which needs init/final code }
  615. function tstoredsymtable.needs_init_final : boolean;
  616. begin
  617. b_needs_init_final:=false;
  618. SymList.ForEachCall(@_needs_init_final,nil);
  619. needs_init_final:=b_needs_init_final;
  620. end;
  621. {****************************************************************************
  622. TAbstractRecordSymtable
  623. ****************************************************************************}
  624. constructor tabstractrecordsymtable.create(const n:string;usealign:shortint);
  625. begin
  626. inherited create(n);
  627. _datasize:=0;
  628. databitsize:=0;
  629. recordalignment:=1;
  630. usefieldalignment:=usealign;
  631. padalignment:=1;
  632. { recordalign C_alignment means C record packing, that starts
  633. with an alignment of 1 }
  634. case usealign of
  635. C_alignment,
  636. bit_alignment:
  637. fieldalignment:=1
  638. else
  639. fieldalignment:=usealign;
  640. end;
  641. end;
  642. procedure tabstractrecordsymtable.ppuload(ppufile:tcompilerppufile);
  643. begin
  644. inherited ppuload(ppufile);
  645. end;
  646. procedure tabstractrecordsymtable.ppuwrite(ppufile:tcompilerppufile);
  647. var
  648. oldtyp : byte;
  649. begin
  650. oldtyp:=ppufile.entrytyp;
  651. ppufile.entrytyp:=subentryid;
  652. inherited ppuwrite(ppufile);
  653. ppufile.entrytyp:=oldtyp;
  654. end;
  655. procedure tabstractrecordsymtable.addfield(sym:tfieldvarsym);
  656. var
  657. l : aint;
  658. varalignrecord,
  659. varalignfield,
  660. varalign : shortint;
  661. vardef : tdef;
  662. begin
  663. if (sym.owner<>self) then
  664. internalerror(200602031);
  665. if sym.fieldoffset<>-1 then
  666. internalerror(200602032);
  667. { this symbol can't be loaded to a register }
  668. sym.varregable:=vr_none;
  669. { Calculate field offset }
  670. l:=sym.getsize;
  671. vardef:=sym.vardef;
  672. varalign:=vardef.alignment;
  673. if (usefieldalignment=bit_alignment) then
  674. begin
  675. { bitpacking only happens for ordinals, the rest is aligned at }
  676. { 1 byte (compatible with GPC/GCC) }
  677. if is_ordinal(vardef) then
  678. begin
  679. sym.fieldoffset:=databitsize;
  680. l:=sym.getpackedbitsize;
  681. end
  682. else
  683. begin
  684. databitsize:=_datasize*8;
  685. sym.fieldoffset:=databitsize;
  686. if (l>high(aint) div 8) then
  687. Message(sym_e_segment_too_large);
  688. l:=l*8;
  689. end;
  690. { bit packed records are limited to high(aint) bits }
  691. { instead of bytes to avoid double precision }
  692. { arithmetic in offset calculations }
  693. if int64(l)>high(aint)-sym.fieldoffset then
  694. begin
  695. Message(sym_e_segment_too_large);
  696. _datasize:=high(aint);
  697. databitsize:=high(aint);
  698. end
  699. else
  700. begin
  701. databitsize:=sym.fieldoffset+l;
  702. _datasize:=(databitsize+7) div 8;
  703. end;
  704. { rest is not applicable }
  705. exit;
  706. end;
  707. { Calc the alignment size for C style records }
  708. if (usefieldalignment=C_alignment) then
  709. begin
  710. if (varalign>4) and
  711. ((varalign mod 4)<>0) and
  712. (vardef.typ=arraydef) then
  713. Message1(sym_w_wrong_C_pack,vardef.typename);
  714. if varalign=0 then
  715. varalign:=l;
  716. if (fieldalignment<current_settings.alignment.maxCrecordalign) then
  717. begin
  718. if (varalign>16) and (fieldalignment<32) then
  719. fieldalignment:=32
  720. else if (varalign>12) and (fieldalignment<16) then
  721. fieldalignment:=16
  722. { 12 is needed for long double }
  723. else if (varalign>8) and (fieldalignment<12) then
  724. fieldalignment:=12
  725. else if (varalign>4) and (fieldalignment<8) then
  726. fieldalignment:=8
  727. else if (varalign>2) and (fieldalignment<4) then
  728. fieldalignment:=4
  729. else if (varalign>1) and (fieldalignment<2) then
  730. fieldalignment:=2;
  731. end;
  732. fieldalignment:=min(fieldalignment,current_settings.alignment.maxCrecordalign);
  733. end;
  734. if varalign=0 then
  735. varalign:=size_2_align(l);
  736. if (usefieldalignment<> bit_alignment) then
  737. varalignfield:=used_align(varalign,current_settings.alignment.recordalignmin,fieldalignment);
  738. sym.fieldoffset:=align(_datasize,varalignfield);
  739. if l>high(aint)-sym.fieldoffset then
  740. begin
  741. Message(sym_e_segment_too_large);
  742. _datasize:=high(aint);
  743. end
  744. else
  745. _datasize:=sym.fieldoffset+l;
  746. { Calc alignment needed for this record }
  747. if (usefieldalignment=C_alignment) then
  748. varalignrecord:=used_align(varalign,current_settings.alignment.recordalignmin,current_settings.alignment.maxCrecordalign)
  749. else
  750. if (usefieldalignment=0) then
  751. varalignrecord:=used_align(varalign,current_settings.alignment.recordalignmin,current_settings.alignment.recordalignmax)
  752. else
  753. begin
  754. { packrecords is set explicitly, ignore recordalignmax limit }
  755. varalignrecord:=used_align(varalign,current_settings.alignment.recordalignmin,usefieldalignment);
  756. end;
  757. recordalignment:=max(recordalignment,varalignrecord);
  758. end;
  759. procedure tabstractrecordsymtable.insertfield(sym:tfieldvarsym);
  760. begin
  761. insert(sym);
  762. addfield(sym);
  763. end;
  764. procedure tabstractrecordsymtable.addalignmentpadding;
  765. begin
  766. { make the record size aligned correctly so it can be
  767. used as elements in an array. For C records we
  768. use the fieldalignment, because that is updated with the
  769. used alignment. }
  770. if (padalignment = 1) then
  771. if usefieldalignment=C_alignment then
  772. padalignment:=fieldalignment
  773. else
  774. padalignment:=recordalignment;
  775. _datasize:=align(_datasize,padalignment);
  776. end;
  777. procedure tabstractrecordsymtable.insertdef(def:TDefEntry);
  778. begin
  779. { Enums must also be available outside the record scope,
  780. insert in the owner of this symtable }
  781. if def.typ=enumdef then
  782. defowner.owner.insertdef(def)
  783. else
  784. inherited insertdef(def);
  785. end;
  786. function tabstractrecordsymtable.is_packed: boolean;
  787. begin
  788. result:=usefieldalignment=bit_alignment;
  789. end;
  790. procedure tabstractrecordsymtable.setdatasize(val: aint);
  791. begin
  792. _datasize:=val;
  793. if (usefieldalignment=bit_alignment) then
  794. { can overflow in non bitpacked records }
  795. databitsize:=val*8;
  796. end;
  797. {****************************************************************************
  798. TRecordSymtable
  799. ****************************************************************************}
  800. constructor trecordsymtable.create(usealign:shortint);
  801. begin
  802. inherited create('',usealign);
  803. symtabletype:=recordsymtable;
  804. end;
  805. { this procedure is reserved for inserting case variant into
  806. a record symtable }
  807. { the offset is the location of the start of the variant
  808. and datasize and dataalignment corresponds to
  809. the complete size (see code in pdecl unit) PM }
  810. procedure trecordsymtable.insertunionst(unionst : trecordsymtable;offset : longint);
  811. var
  812. sym : tsym;
  813. def : tdef;
  814. i : integer;
  815. varalignrecord,varalign,
  816. storesize,storealign : aint;
  817. bitsize: aint;
  818. begin
  819. storesize:=_datasize;
  820. storealign:=fieldalignment;
  821. _datasize:=offset;
  822. if (usefieldalignment=bit_alignment) then
  823. databitsize:=offset*8;
  824. { We move the ownership of the defs and symbols to the new recordsymtable.
  825. The old unionsymtable keeps the references, but doesn't own the
  826. objects anymore }
  827. unionst.DefList.OwnsObjects:=false;
  828. unionst.SymList.OwnsObjects:=false;
  829. { copy symbols }
  830. for i:=0 to unionst.SymList.Count-1 do
  831. begin
  832. sym:=TSym(unionst.SymList[i]);
  833. if sym.typ<>fieldvarsym then
  834. internalerror(200601272);
  835. { add to this record symtable }
  836. // unionst.SymList.List.List^[i].Data:=nil;
  837. sym.ChangeOwner(self);
  838. { retrieve size }
  839. if (usefieldalignment=bit_alignment) then
  840. begin
  841. { bit packed records are limited to high(aint) bits }
  842. { instead of bytes to avoid double precision }
  843. { arithmetic in offset calculations }
  844. if is_ordinal(tfieldvarsym(sym).vardef) then
  845. bitsize:=tfieldvarsym(sym).getpackedbitsize
  846. else
  847. begin
  848. bitsize:=tfieldvarsym(sym).getsize;
  849. if (bitsize>high(aint) div 8) then
  850. Message(sym_e_segment_too_large);
  851. bitsize:=bitsize*8;
  852. end;
  853. if bitsize>high(aint)-databitsize then
  854. begin
  855. Message(sym_e_segment_too_large);
  856. _datasize:=high(aint);
  857. databitsize:=high(aint);
  858. end
  859. else
  860. begin
  861. databitsize:=tfieldvarsym(sym).fieldoffset+offset*8;
  862. _datasize:=(databitsize+7) div 8;
  863. end;
  864. tfieldvarsym(sym).fieldoffset:=databitsize;
  865. end
  866. else
  867. begin
  868. if tfieldvarsym(sym).getsize>high(aint)-_datasize then
  869. begin
  870. Message(sym_e_segment_too_large);
  871. _datasize:=high(aint);
  872. end
  873. else
  874. _datasize:=tfieldvarsym(sym).fieldoffset+offset;
  875. { update address }
  876. tfieldvarsym(sym).fieldoffset:=_datasize;
  877. { update alignment of this record }
  878. varalign:=tfieldvarsym(sym).vardef.alignment;
  879. if varalign=0 then
  880. varalign:=size_2_align(tfieldvarsym(sym).getsize);
  881. varalignrecord:=used_align(varalign,current_settings.alignment.recordalignmin,fieldalignment);
  882. recordalignment:=max(recordalignment,varalignrecord);
  883. end;
  884. end;
  885. { Register defs in the new record symtable }
  886. for i:=0 to unionst.DefList.Count-1 do
  887. begin
  888. def:=TDef(unionst.DefList[i]);
  889. def.ChangeOwner(self);
  890. end;
  891. _datasize:=storesize;
  892. fieldalignment:=storealign;
  893. end;
  894. {****************************************************************************
  895. TObjectSymtable
  896. ****************************************************************************}
  897. constructor tObjectSymtable.create(adefowner:tdef;const n:string;usealign:shortint);
  898. begin
  899. inherited create(n,usealign);
  900. symtabletype:=ObjectSymtable;
  901. defowner:=adefowner;
  902. end;
  903. function tObjectSymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  904. var
  905. hsym : tsym;
  906. begin
  907. result:=false;
  908. if not assigned(defowner) then
  909. internalerror(200602061);
  910. { check for duplicate field, parameter or local names
  911. also in inherited classes }
  912. if (sym.typ in [fieldvarsym,paravarsym,localvarsym]) and
  913. (
  914. not(m_delphi in current_settings.modeswitches) or
  915. is_object(tdef(defowner))
  916. ) then
  917. begin
  918. { but private ids can be reused }
  919. hsym:=search_class_member(tobjectdef(defowner),hashedid.id);
  920. if assigned(hsym) and
  921. tsym(hsym).is_visible_for_object(tobjectdef(defowner),tobjectdef(defowner)) then
  922. begin
  923. DuplicateSym(hashedid,sym,hsym);
  924. result:=true;
  925. end;
  926. end
  927. else
  928. begin
  929. if not(m_duplicate_names in current_settings.modeswitches) or
  930. not(sym.typ in [paravarsym,localvarsym]) then
  931. result:=inherited checkduplicate(hashedid,sym);
  932. end;
  933. end;
  934. {****************************************************************************
  935. TAbstractLocalSymtable
  936. ****************************************************************************}
  937. procedure tabstractlocalsymtable.ppuwrite(ppufile:tcompilerppufile);
  938. var
  939. oldtyp : byte;
  940. begin
  941. oldtyp:=ppufile.entrytyp;
  942. ppufile.entrytyp:=subentryid;
  943. { write definitions }
  944. writedefs(ppufile);
  945. { write symbols }
  946. writesyms(ppufile);
  947. ppufile.entrytyp:=oldtyp;
  948. end;
  949. function tabstractlocalsymtable.count_locals:longint;
  950. var
  951. i : longint;
  952. sym : tsym;
  953. begin
  954. result:=0;
  955. for i:=0 to SymList.Count-1 do
  956. begin
  957. sym:=tsym(SymList[i]);
  958. { Count only varsyms, but ignore the funcretsym }
  959. if (tsym(sym).typ in [localvarsym,paravarsym]) and
  960. (tsym(sym)<>current_procinfo.procdef.funcretsym) and
  961. (not(vo_is_parentfp in tabstractvarsym(sym).varoptions) or
  962. (tstoredsym(sym).refs>0)) then
  963. inc(result);
  964. end;
  965. end;
  966. {****************************************************************************
  967. TLocalSymtable
  968. ****************************************************************************}
  969. constructor tlocalsymtable.create(adefowner:tdef;level:byte);
  970. begin
  971. inherited create('');
  972. defowner:=adefowner;
  973. symtabletype:=localsymtable;
  974. symtablelevel:=level;
  975. end;
  976. function tlocalsymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  977. var
  978. hsym : tsym;
  979. begin
  980. if not assigned(defowner) or
  981. (defowner.typ<>procdef) then
  982. internalerror(200602042);
  983. result:=false;
  984. hsym:=tsym(FindWithHash(hashedid));
  985. if assigned(hsym) then
  986. begin
  987. { a local and the function can have the same
  988. name in TP and Delphi, but RESULT not }
  989. if (m_duplicate_names in current_settings.modeswitches) and
  990. (hsym.typ in [absolutevarsym,localvarsym]) and
  991. (vo_is_funcret in tabstractvarsym(hsym).varoptions) and
  992. not((m_result in current_settings.modeswitches) and
  993. (vo_is_result in tabstractvarsym(hsym).varoptions)) then
  994. HideSym(hsym)
  995. else
  996. DuplicateSym(hashedid,sym,hsym);
  997. result:=true;
  998. exit;
  999. end;
  1000. { check also parasymtable, this needs to be done here becuase
  1001. of the special situation with the funcret sym that needs to be
  1002. hidden for tp and delphi modes }
  1003. hsym:=tsym(tabstractprocdef(defowner).parast.FindWithHash(hashedid));
  1004. if assigned(hsym) then
  1005. begin
  1006. { a local and the function can have the same
  1007. name in TP and Delphi, but RESULT not }
  1008. if (m_duplicate_names in current_settings.modeswitches) and
  1009. (sym.typ in [absolutevarsym,localvarsym]) and
  1010. (vo_is_funcret in tabstractvarsym(sym).varoptions) and
  1011. not((m_result in current_settings.modeswitches) and
  1012. (vo_is_result in tabstractvarsym(sym).varoptions)) then
  1013. Hidesym(sym)
  1014. else
  1015. DuplicateSym(hashedid,sym,hsym);
  1016. result:=true;
  1017. exit;
  1018. end;
  1019. { check ObjectSymtable, skip this for funcret sym because
  1020. that will always be positive because it has the same name
  1021. as the procsym }
  1022. if not is_funcret_sym(sym) and
  1023. (defowner.typ=procdef) and
  1024. assigned(tprocdef(defowner)._class) and
  1025. (tprocdef(defowner).owner.defowner=tprocdef(defowner)._class) then
  1026. result:=tprocdef(defowner)._class.symtable.checkduplicate(hashedid,sym);
  1027. end;
  1028. {****************************************************************************
  1029. TParaSymtable
  1030. ****************************************************************************}
  1031. constructor tparasymtable.create(adefowner:tdef;level:byte);
  1032. begin
  1033. inherited create('');
  1034. defowner:=adefowner;
  1035. symtabletype:=parasymtable;
  1036. symtablelevel:=level;
  1037. end;
  1038. function tparasymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  1039. begin
  1040. result:=inherited checkduplicate(hashedid,sym);
  1041. if result then
  1042. exit;
  1043. if not(m_duplicate_names in current_settings.modeswitches) and
  1044. (defowner.typ=procdef) and
  1045. assigned(tprocdef(defowner)._class) and
  1046. (tprocdef(defowner).owner.defowner=tprocdef(defowner)._class) then
  1047. result:=tprocdef(defowner)._class.symtable.checkduplicate(hashedid,sym);
  1048. end;
  1049. {****************************************************************************
  1050. TAbstractUniTSymtable
  1051. ****************************************************************************}
  1052. constructor tabstractuniTSymtable.create(const n : string;id:word);
  1053. begin
  1054. inherited create(n);
  1055. moduleid:=id;
  1056. end;
  1057. function tabstractuniTSymtable.iscurrentunit:boolean;
  1058. begin
  1059. result:=assigned(current_module) and
  1060. (
  1061. (current_module.globalsymtable=self) or
  1062. (current_module.localsymtable=self)
  1063. );
  1064. end;
  1065. {****************************************************************************
  1066. TStaticSymtable
  1067. ****************************************************************************}
  1068. constructor tstaticsymtable.create(const n : string;id:word);
  1069. begin
  1070. inherited create(n,id);
  1071. symtabletype:=staticsymtable;
  1072. symtablelevel:=main_program_level;
  1073. end;
  1074. procedure tstaticsymtable.ppuload(ppufile:tcompilerppufile);
  1075. begin
  1076. inherited ppuload(ppufile);
  1077. { now we can deref the syms and defs }
  1078. deref;
  1079. end;
  1080. procedure tstaticsymtable.ppuwrite(ppufile:tcompilerppufile);
  1081. begin
  1082. inherited ppuwrite(ppufile);
  1083. end;
  1084. function tstaticsymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  1085. var
  1086. hsym : tsym;
  1087. begin
  1088. result:=false;
  1089. hsym:=tsym(FindWithHash(hashedid));
  1090. if assigned(hsym) then
  1091. begin
  1092. { Delphi you can have a symbol with the same name as the
  1093. unit, the unit can then not be accessed anymore using
  1094. <unit>.<id>, so we can hide the symbol }
  1095. if (m_duplicate_names in current_settings.modeswitches) and
  1096. (hsym.typ=symconst.unitsym) then
  1097. HideSym(hsym)
  1098. else
  1099. DuplicateSym(hashedid,sym,hsym);
  1100. result:=true;
  1101. exit;
  1102. end;
  1103. if (current_module.localsymtable=self) and
  1104. assigned(current_module.globalsymtable) then
  1105. result:=tglobalsymtable(current_module.globalsymtable).checkduplicate(hashedid,sym);
  1106. end;
  1107. {****************************************************************************
  1108. TGlobalSymtable
  1109. ****************************************************************************}
  1110. constructor tglobalsymtable.create(const n : string;id:word);
  1111. begin
  1112. inherited create(n,id);
  1113. symtabletype:=globalsymtable;
  1114. symtablelevel:=main_program_level;
  1115. end;
  1116. procedure tglobalsymtable.ppuload(ppufile:tcompilerppufile);
  1117. begin
  1118. inherited ppuload(ppufile);
  1119. { now we can deref the syms and defs }
  1120. deref;
  1121. end;
  1122. procedure tglobalsymtable.ppuwrite(ppufile:tcompilerppufile);
  1123. begin
  1124. { write the symtable entries }
  1125. inherited ppuwrite(ppufile);
  1126. end;
  1127. function tglobalsymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  1128. var
  1129. hsym : tsym;
  1130. begin
  1131. result:=false;
  1132. hsym:=tsym(FindWithHash(hashedid));
  1133. if assigned(hsym) then
  1134. begin
  1135. { Delphi you can have a symbol with the same name as the
  1136. unit, the unit can then not be accessed anymore using
  1137. <unit>.<id>, so we can hide the symbol }
  1138. if (m_duplicate_names in current_settings.modeswitches) and
  1139. (hsym.typ=symconst.unitsym) then
  1140. HideSym(hsym)
  1141. else
  1142. DuplicateSym(hashedid,sym,hsym);
  1143. result:=true;
  1144. exit;
  1145. end;
  1146. end;
  1147. {****************************************************************************
  1148. TWITHSYMTABLE
  1149. ****************************************************************************}
  1150. constructor twithsymtable.create(aowner:tdef;ASymList:TFPHashObjectList;refnode:tobject{tnode});
  1151. begin
  1152. inherited create('');
  1153. symtabletype:=withsymtable;
  1154. withrefnode:=refnode;
  1155. { Replace SymList with the passed symlist }
  1156. SymList.free;
  1157. SymList:=ASymList;
  1158. defowner:=aowner;
  1159. end;
  1160. destructor twithsymtable.destroy;
  1161. begin
  1162. withrefnode.free;
  1163. { Disable SymList because we don't Own it }
  1164. SymList:=nil;
  1165. inherited destroy;
  1166. end;
  1167. procedure twithsymtable.clear;
  1168. begin
  1169. { remove no entry from a withsymtable as it is only a pointer to the
  1170. recorddef or objectdef symtable }
  1171. end;
  1172. procedure twithsymtable.insertdef(def:TDefEntry);
  1173. begin
  1174. { Definitions can't be registered in the withsymtable
  1175. because the withsymtable is removed after the with block.
  1176. We can't easily solve it here because the next symtable in the
  1177. stack is not known. }
  1178. internalerror(200602046);
  1179. end;
  1180. {****************************************************************************
  1181. TSTT_ExceptionSymtable
  1182. ****************************************************************************}
  1183. constructor tstt_excepTSymtable.create;
  1184. begin
  1185. inherited create('');
  1186. symtabletype:=stt_excepTSymtable;
  1187. end;
  1188. {****************************************************************************
  1189. TMacroSymtable
  1190. ****************************************************************************}
  1191. constructor tmacrosymtable.create(exported: boolean);
  1192. begin
  1193. inherited create('');
  1194. if exported then
  1195. symtabletype:=exportedmacrosymtable
  1196. else
  1197. symtabletype:=localmacrosymtable;
  1198. symtablelevel:=main_program_level;
  1199. end;
  1200. {*****************************************************************************
  1201. Helper Routines
  1202. *****************************************************************************}
  1203. function FullTypeName(def,otherdef:tdef):string;
  1204. var
  1205. s1,s2 : string;
  1206. begin
  1207. s1:=def.typename;
  1208. { When the names are the same try to include the unit name }
  1209. if assigned(otherdef) and
  1210. (def.owner.symtabletype in [globalsymtable,staticsymtable]) then
  1211. begin
  1212. s2:=otherdef.typename;
  1213. if upper(s1)=upper(s2) then
  1214. s1:=def.owner.realname^+'.'+s1;
  1215. end;
  1216. FullTypeName:=s1;
  1217. end;
  1218. procedure incompatibletypes(def1,def2:tdef);
  1219. begin
  1220. { When there is an errordef there is already an error message show }
  1221. if (def2.typ=errordef) or
  1222. (def1.typ=errordef) then
  1223. exit;
  1224. CGMessage2(type_e_incompatible_types,FullTypeName(def1,def2),FullTypeName(def2,def1));
  1225. end;
  1226. procedure hidesym(sym:TSymEntry);
  1227. begin
  1228. sym.realname:='$hidden'+sym.realname;
  1229. include(tsym(sym).symoptions,sp_hidden);
  1230. end;
  1231. procedure duplicatesym(var hashedid:THashedIDString;dupsym,origsym:TSymEntry);
  1232. var
  1233. st : TSymtable;
  1234. begin
  1235. Message1(sym_e_duplicate_id,tsym(origsym).realname);
  1236. { Write hint where the original symbol was found }
  1237. st:=finduniTSymtable(origsym.owner);
  1238. with tsym(origsym).fileinfo do
  1239. begin
  1240. if assigned(st) and
  1241. (st.symtabletype=globalsymtable) and
  1242. st.iscurrentunit then
  1243. Message2(sym_h_duplicate_id_where,current_module.sourcefiles.get_file_name(fileindex),tostr(line))
  1244. else if assigned(st.name) then
  1245. Message2(sym_h_duplicate_id_where,'unit '+st.name^,tostr(line));
  1246. end;
  1247. { Rename duplicate sym to an unreachable name, but it can be
  1248. inserted in the symtable without errors }
  1249. inc(dupnr);
  1250. hashedid.id:='dup'+tostr(dupnr)+hashedid.id;
  1251. if assigned(dupsym) then
  1252. include(tsym(dupsym).symoptions,sp_implicitrename);
  1253. end;
  1254. {*****************************************************************************
  1255. Search
  1256. *****************************************************************************}
  1257. procedure addsymref(sym:tsym);
  1258. begin
  1259. { symbol uses count }
  1260. sym.IncRefCount;
  1261. { unit uses count }
  1262. if assigned(current_module) and
  1263. (sym.owner.symtabletype=globalsymtable) then
  1264. begin
  1265. if tglobalsymtable(sym.owner).moduleid>=current_module.unitmapsize then
  1266. internalerror(200501152);
  1267. inc(current_module.unitmap[tglobalsymtable(sym.owner).moduleid].refs);
  1268. end;
  1269. end;
  1270. function searchsym(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  1271. var
  1272. hashedid : THashedIDString;
  1273. topclass : tobjectdef;
  1274. context : tobjectdef;
  1275. stackitem : psymtablestackitem;
  1276. begin
  1277. result:=false;
  1278. hashedid.id:=s;
  1279. stackitem:=symtablestack.stack;
  1280. while assigned(stackitem) do
  1281. begin
  1282. srsymtable:=stackitem^.symtable;
  1283. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  1284. if assigned(srsym) then
  1285. begin
  1286. topclass:=nil;
  1287. { use the class from withsymtable only when it is
  1288. defined in this unit }
  1289. if (srsymtable.symtabletype=withsymtable) and
  1290. assigned(srsymtable.defowner) and
  1291. (srsymtable.defowner.typ=objectdef) and
  1292. (srsymtable.defowner.owner.symtabletype in [globalsymtable,staticsymtable]) and
  1293. (srsymtable.defowner.owner.iscurrentunit) then
  1294. topclass:=tobjectdef(srsymtable.defowner)
  1295. else
  1296. begin
  1297. if assigned(current_procinfo) then
  1298. topclass:=current_procinfo.procdef._class;
  1299. end;
  1300. if assigned(current_procinfo) then
  1301. context:=current_procinfo.procdef._class
  1302. else
  1303. context:=nil;
  1304. if tsym(srsym).is_visible_for_object(topclass,context) then
  1305. begin
  1306. { we need to know if a procedure references symbols
  1307. in the static symtable, because then it can't be
  1308. inlined from outside this unit }
  1309. if assigned(current_procinfo) and
  1310. (srsym.owner.symtabletype=staticsymtable) then
  1311. include(current_procinfo.flags,pi_uses_static_symtable);
  1312. addsymref(srsym);
  1313. result:=true;
  1314. exit;
  1315. end;
  1316. end;
  1317. stackitem:=stackitem^.next;
  1318. end;
  1319. srsym:=nil;
  1320. srsymtable:=nil;
  1321. end;
  1322. function searchsym_type(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  1323. var
  1324. hashedid : THashedIDString;
  1325. stackitem : psymtablestackitem;
  1326. begin
  1327. result:=false;
  1328. hashedid.id:=s;
  1329. stackitem:=symtablestack.stack;
  1330. while assigned(stackitem) do
  1331. begin
  1332. {
  1333. It is not possible to have type symbols in:
  1334. records
  1335. objects
  1336. parameters
  1337. Exception are generic definitions and specializations
  1338. that have the parameterized types inserted in the symtable.
  1339. }
  1340. srsymtable:=stackitem^.symtable;
  1341. if not(srsymtable.symtabletype in [recordsymtable,ObjectSymtable,parasymtable]) or
  1342. (assigned(srsymtable.defowner) and
  1343. (
  1344. (df_generic in tdef(srsymtable.defowner).defoptions) or
  1345. (df_specialization in tdef(srsymtable.defowner).defoptions))
  1346. ) then
  1347. begin
  1348. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  1349. if assigned(srsym) and
  1350. not(srsym.typ in [fieldvarsym,paravarsym]) and
  1351. (not assigned(current_procinfo) or
  1352. tsym(srsym).is_visible_for_object(current_procinfo.procdef._class,current_procinfo.procdef._class)) then
  1353. begin
  1354. { we need to know if a procedure references symbols
  1355. in the static symtable, because then it can't be
  1356. inlined from outside this unit }
  1357. if assigned(current_procinfo) and
  1358. (srsym.owner.symtabletype=staticsymtable) then
  1359. include(current_procinfo.flags,pi_uses_static_symtable);
  1360. addsymref(srsym);
  1361. result:=true;
  1362. exit;
  1363. end;
  1364. end;
  1365. stackitem:=stackitem^.next;
  1366. end;
  1367. result:=false;
  1368. srsym:=nil;
  1369. srsymtable:=nil;
  1370. end;
  1371. function searchsym_in_module(pm:pointer;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  1372. var
  1373. pmod : tmodule;
  1374. begin
  1375. pmod:=tmodule(pm);
  1376. result:=false;
  1377. if assigned(pmod.globalsymtable) then
  1378. begin
  1379. srsym:=tsym(pmod.globalsymtable.Find(s));
  1380. if assigned(srsym) then
  1381. begin
  1382. srsymtable:=pmod.globalsymtable;
  1383. addsymref(srsym);
  1384. result:=true;
  1385. exit;
  1386. end;
  1387. end;
  1388. { If the module is the current unit we also need
  1389. to search the local symtable }
  1390. if (pmod=current_module) and
  1391. assigned(pmod.localsymtable) then
  1392. begin
  1393. srsym:=tsym(pmod.localsymtable.Find(s));
  1394. if assigned(srsym) then
  1395. begin
  1396. srsymtable:=pmod.localsymtable;
  1397. addsymref(srsym);
  1398. result:=true;
  1399. exit;
  1400. end;
  1401. end;
  1402. srsym:=nil;
  1403. srsymtable:=nil;
  1404. end;
  1405. function searchsym_in_class(classh,contextclassh:tobjectdef;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  1406. var
  1407. hashedid : THashedIDString;
  1408. currentclassh : tobjectdef;
  1409. begin
  1410. result:=false;
  1411. hashedid.id:=s;
  1412. if assigned(current_procinfo.procdef) then
  1413. currentclassh:=current_procinfo.procdef._class
  1414. else
  1415. currentclassh:=nil;
  1416. while assigned(classh) do
  1417. begin
  1418. srsymtable:=classh.symtable;
  1419. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  1420. if assigned(srsym) and
  1421. tsym(srsym).is_visible_for_object(contextclassh,currentclassh) then
  1422. begin
  1423. addsymref(srsym);
  1424. result:=true;
  1425. exit;
  1426. end;
  1427. classh:=classh.childof;
  1428. end;
  1429. srsym:=nil;
  1430. srsymtable:=nil;
  1431. end;
  1432. function searchsym_in_class_by_msgint(classh:tobjectdef;msgid:longint;out srdef : tdef;out srsym:tsym;out srsymtable:TSymtable):boolean;
  1433. var
  1434. def : tdef;
  1435. i : longint;
  1436. begin
  1437. result:=false;
  1438. def:=nil;
  1439. while assigned(classh) do
  1440. begin
  1441. for i:=0 to classh.symtable.DefList.Count-1 do
  1442. begin
  1443. def:=tstoreddef(classh.symtable.DefList[i]);
  1444. { Find also all hidden private methods to
  1445. be compatible with delphi, see tw6203 (PFV) }
  1446. if (def.typ=procdef) and
  1447. (po_msgint in tprocdef(def).procoptions) and
  1448. (tprocdef(def).messageinf.i=msgid) then
  1449. begin
  1450. srdef:=def;
  1451. srsym:=tprocdef(def).procsym;
  1452. srsymtable:=classh.symtable;
  1453. addsymref(srsym);
  1454. result:=true;
  1455. exit;
  1456. end;
  1457. end;
  1458. classh:=classh.childof;
  1459. end;
  1460. srdef:=nil;
  1461. srsym:=nil;
  1462. srsymtable:=nil;
  1463. end;
  1464. function searchsym_in_class_by_msgstr(classh:tobjectdef;const s:string;out srsym:tsym;out srsymtable:TSymtable):boolean;
  1465. var
  1466. def : tdef;
  1467. i : longint;
  1468. begin
  1469. result:=false;
  1470. def:=nil;
  1471. while assigned(classh) do
  1472. begin
  1473. for i:=0 to classh.symtable.DefList.Count-1 do
  1474. begin
  1475. def:=tstoreddef(classh.symtable.DefList[i]);
  1476. { Find also all hidden private methods to
  1477. be compatible with delphi, see tw6203 (PFV) }
  1478. if (def.typ=procdef) and
  1479. (po_msgstr in tprocdef(def).procoptions) and
  1480. (tprocdef(def).messageinf.str^=s) then
  1481. begin
  1482. srsym:=tprocdef(def).procsym;
  1483. srsymtable:=classh.symtable;
  1484. addsymref(srsym);
  1485. result:=true;
  1486. exit;
  1487. end;
  1488. end;
  1489. classh:=classh.childof;
  1490. end;
  1491. srsym:=nil;
  1492. srsymtable:=nil;
  1493. end;
  1494. function search_assignment_operator(from_def,to_def:Tdef):Tprocdef;
  1495. var
  1496. sym : Tprocsym;
  1497. hashedid : THashedIDString;
  1498. curreq,
  1499. besteq : tequaltype;
  1500. currpd,
  1501. bestpd : tprocdef;
  1502. stackitem : psymtablestackitem;
  1503. begin
  1504. hashedid.id:='assign';
  1505. besteq:=te_incompatible;
  1506. bestpd:=nil;
  1507. stackitem:=symtablestack.stack;
  1508. while assigned(stackitem) do
  1509. begin
  1510. sym:=Tprocsym(stackitem^.symtable.FindWithHash(hashedid));
  1511. if sym<>nil then
  1512. begin
  1513. if sym.typ<>procsym then
  1514. internalerror(200402031);
  1515. { if the source type is an alias then this is only the second choice,
  1516. if you mess with this code, check tw4093 }
  1517. currpd:=sym.find_procdef_assignment_operator(from_def,to_def,curreq);
  1518. if curreq>besteq then
  1519. begin
  1520. besteq:=curreq;
  1521. bestpd:=currpd;
  1522. if (besteq=te_exact) then
  1523. break;
  1524. end;
  1525. end;
  1526. stackitem:=stackitem^.next;
  1527. end;
  1528. result:=bestpd;
  1529. end;
  1530. function search_system_type(const s: TIDString): ttypesym;
  1531. var
  1532. sym : tsym;
  1533. begin
  1534. sym:=tsym(systemunit.Find(s));
  1535. if not assigned(sym) or
  1536. (sym.typ<>typesym) then
  1537. cgmessage1(cg_f_unknown_system_type,s);
  1538. result:=ttypesym(sym);
  1539. end;
  1540. function search_class_member(pd : tobjectdef;const s : string):tsym;
  1541. { searches n in symtable of pd and all anchestors }
  1542. var
  1543. hashedid : THashedIDString;
  1544. srsym : tsym;
  1545. begin
  1546. hashedid.id:=s;
  1547. while assigned(pd) do
  1548. begin
  1549. srsym:=tsym(pd.symtable.FindWithHash(hashedid));
  1550. if assigned(srsym) then
  1551. begin
  1552. search_class_member:=srsym;
  1553. exit;
  1554. end;
  1555. pd:=pd.childof;
  1556. end;
  1557. search_class_member:=nil;
  1558. end;
  1559. function search_macro(const s : string):tsym;
  1560. var
  1561. stackitem : psymtablestackitem;
  1562. hashedid : THashedIDString;
  1563. srsym : tsym;
  1564. begin
  1565. hashedid.id:=s;
  1566. { First search the localmacrosymtable before searching the
  1567. global macrosymtables from the units }
  1568. if assigned(current_module) then
  1569. begin
  1570. srsym:=tsym(current_module.localmacrosymtable.FindWithHash(hashedid));
  1571. if assigned(srsym) then
  1572. begin
  1573. result:= srsym;
  1574. exit;
  1575. end;
  1576. end;
  1577. stackitem:=macrosymtablestack.stack;
  1578. while assigned(stackitem) do
  1579. begin
  1580. srsym:=tsym(stackitem^.symtable.FindWithHash(hashedid));
  1581. if assigned(srsym) then
  1582. begin
  1583. result:= srsym;
  1584. exit;
  1585. end;
  1586. stackitem:=stackitem^.next;
  1587. end;
  1588. result:= nil;
  1589. end;
  1590. {****************************************************************************
  1591. Object Helpers
  1592. ****************************************************************************}
  1593. procedure search_class_overloads(aprocsym : tprocsym);
  1594. { searches n in symtable of pd and all anchestors }
  1595. var
  1596. hashedid : THashedIDString;
  1597. srsym : tprocsym;
  1598. objdef : tobjectdef;
  1599. begin
  1600. if aprocsym.overloadchecked then
  1601. exit;
  1602. aprocsym.overloadchecked:=true;
  1603. if (aprocsym.owner.symtabletype<>ObjectSymtable) then
  1604. internalerror(200111021);
  1605. objdef:=tobjectdef(aprocsym.owner.defowner);
  1606. { we start in the parent }
  1607. if not assigned(objdef.childof) then
  1608. exit;
  1609. objdef:=objdef.childof;
  1610. hashedid.id:=aprocsym.name;
  1611. while assigned(objdef) do
  1612. begin
  1613. srsym:=tprocsym(objdef.symtable.FindWithHash(hashedid));
  1614. if assigned(srsym) then
  1615. begin
  1616. if (srsym.typ<>procsym) then
  1617. internalerror(200111022);
  1618. if srsym.is_visible_for_object(tobjectdef(aprocsym.owner.defowner),tobjectdef(aprocsym.owner.defowner)) then
  1619. begin
  1620. srsym.add_para_match_to(Aprocsym,[cpo_ignorehidden,cpo_allowdefaults]);
  1621. { we can stop if the overloads were already added
  1622. for the found symbol }
  1623. if srsym.overloadchecked then
  1624. break;
  1625. end;
  1626. end;
  1627. { next parent }
  1628. objdef:=objdef.childof;
  1629. end;
  1630. end;
  1631. procedure tstoredsymtable.testfordefaultproperty(sym:TObject;arg:pointer);
  1632. begin
  1633. if (tsym(sym).typ=propertysym) and
  1634. (ppo_defaultproperty in tpropertysym(sym).propoptions) then
  1635. ppointer(arg)^:=sym;
  1636. end;
  1637. function search_default_property(pd : tobjectdef) : tpropertysym;
  1638. { returns the default property of a class, searches also anchestors }
  1639. var
  1640. _defaultprop : tpropertysym;
  1641. begin
  1642. _defaultprop:=nil;
  1643. while assigned(pd) do
  1644. begin
  1645. pd.symtable.SymList.ForEachCall(@tstoredsymtable(pd.symtable).testfordefaultproperty,@_defaultprop);
  1646. if assigned(_defaultprop) then
  1647. break;
  1648. pd:=pd.childof;
  1649. end;
  1650. search_default_property:=_defaultprop;
  1651. end;
  1652. {****************************************************************************
  1653. Macro Helpers
  1654. ****************************************************************************}
  1655. procedure def_system_macro(const name : string);
  1656. var
  1657. mac : tmacro;
  1658. s: string;
  1659. begin
  1660. if name = '' then
  1661. internalerror(2004121202);
  1662. s:= upper(name);
  1663. mac:=tmacro(search_macro(s));
  1664. if not assigned(mac) then
  1665. begin
  1666. mac:=tmacro.create(s);
  1667. if assigned(current_module) then
  1668. current_module.localmacrosymtable.insert(mac)
  1669. else
  1670. initialmacrosymtable.insert(mac);
  1671. end;
  1672. if not mac.defined then
  1673. Message1(parser_c_macro_defined,mac.name);
  1674. mac.defined:=true;
  1675. end;
  1676. procedure set_system_macro(const name, value : string);
  1677. var
  1678. mac : tmacro;
  1679. s: string;
  1680. begin
  1681. if name = '' then
  1682. internalerror(2004121203);
  1683. s:= upper(name);
  1684. mac:=tmacro(search_macro(s));
  1685. if not assigned(mac) then
  1686. begin
  1687. mac:=tmacro.create(s);
  1688. if assigned(current_module) then
  1689. current_module.localmacrosymtable.insert(mac)
  1690. else
  1691. initialmacrosymtable.insert(mac);
  1692. end
  1693. else
  1694. begin
  1695. mac.is_compiler_var:=false;
  1696. if assigned(mac.buftext) then
  1697. freemem(mac.buftext,mac.buflen);
  1698. end;
  1699. Message2(parser_c_macro_set_to,mac.name,value);
  1700. mac.buflen:=length(value);
  1701. getmem(mac.buftext,mac.buflen);
  1702. move(value[1],mac.buftext^,mac.buflen);
  1703. mac.defined:=true;
  1704. end;
  1705. procedure set_system_compvar(const name, value : string);
  1706. var
  1707. mac : tmacro;
  1708. s: string;
  1709. begin
  1710. if name = '' then
  1711. internalerror(2004121204);
  1712. s:= upper(name);
  1713. mac:=tmacro(search_macro(s));
  1714. if not assigned(mac) then
  1715. begin
  1716. mac:=tmacro.create(s);
  1717. mac.is_compiler_var:=true;
  1718. if assigned(current_module) then
  1719. current_module.localmacrosymtable.insert(mac)
  1720. else
  1721. initialmacrosymtable.insert(mac);
  1722. end
  1723. else
  1724. begin
  1725. mac.is_compiler_var:=true;
  1726. if assigned(mac.buftext) then
  1727. freemem(mac.buftext,mac.buflen);
  1728. end;
  1729. Message2(parser_c_macro_set_to,mac.name,value);
  1730. mac.buflen:=length(value);
  1731. getmem(mac.buftext,mac.buflen);
  1732. move(value[1],mac.buftext^,mac.buflen);
  1733. mac.defined:=true;
  1734. end;
  1735. procedure undef_system_macro(const name : string);
  1736. var
  1737. mac : tmacro;
  1738. s: string;
  1739. begin
  1740. if name = '' then
  1741. internalerror(2004121205);
  1742. s:= upper(name);
  1743. mac:=tmacro(search_macro(s));
  1744. if not assigned(mac) then
  1745. {If not found, then it's already undefined.}
  1746. else
  1747. begin
  1748. if mac.defined then
  1749. Message1(parser_c_macro_undefined,mac.name);
  1750. mac.defined:=false;
  1751. mac.is_compiler_var:=false;
  1752. { delete old definition }
  1753. if assigned(mac.buftext) then
  1754. begin
  1755. freemem(mac.buftext,mac.buflen);
  1756. mac.buftext:=nil;
  1757. end;
  1758. end;
  1759. end;
  1760. {$ifdef UNITALIASES}
  1761. {****************************************************************************
  1762. TUNIT_ALIAS
  1763. ****************************************************************************}
  1764. constructor tunit_alias.create(const n:string);
  1765. var
  1766. i : longint;
  1767. begin
  1768. i:=pos('=',n);
  1769. if i=0 then
  1770. fail;
  1771. inherited createname(Copy(n,1,i-1));
  1772. newname:=stringdup(Copy(n,i+1,255));
  1773. end;
  1774. destructor tunit_alias.destroy;
  1775. begin
  1776. stringdispose(newname);
  1777. inherited destroy;
  1778. end;
  1779. procedure addunitalias(const n:string);
  1780. begin
  1781. unitaliases^.insert(tunit_alias,init(Upper(n))));
  1782. end;
  1783. function getunitalias(const n:string):string;
  1784. var
  1785. p : punit_alias;
  1786. begin
  1787. p:=punit_alias(unitaliases^.Find(Upper(n)));
  1788. if assigned(p) then
  1789. getunitalias:=punit_alias(p).newname^
  1790. else
  1791. getunitalias:=n;
  1792. end;
  1793. {$endif UNITALIASES}
  1794. {****************************************************************************
  1795. Init/Done Symtable
  1796. ****************************************************************************}
  1797. procedure IniTSymtable;
  1798. begin
  1799. { Reset symbolstack }
  1800. symtablestack:=nil;
  1801. systemunit:=nil;
  1802. { create error syms and def }
  1803. generrorsym:=terrorsym.create;
  1804. generrordef:=terrordef.create;
  1805. { macros }
  1806. initialmacrosymtable:=tmacrosymtable.create(false);
  1807. macrosymtablestack:=TSymtablestack.create;
  1808. macrosymtablestack.push(initialmacrosymtable);
  1809. {$ifdef UNITALIASES}
  1810. { unit aliases }
  1811. unitaliases:=TFPHashObjectList.create;
  1812. {$endif}
  1813. { set some global vars to nil, might be important for the ide }
  1814. class_tobject:=nil;
  1815. interface_iunknown:=nil;
  1816. rec_tguid:=nil;
  1817. dupnr:=0;
  1818. end;
  1819. procedure DoneSymtable;
  1820. begin
  1821. generrorsym.owner:=nil;
  1822. generrorsym.free;
  1823. generrordef.owner:=nil;
  1824. generrordef.free;
  1825. initialmacrosymtable.free;
  1826. macrosymtablestack.free;
  1827. {$ifdef UNITALIASES}
  1828. unitaliases.free;
  1829. {$endif}
  1830. end;
  1831. end.