symtable.pas 70 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078
  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. l:=l*8;
  687. end;
  688. { bit packed records are limited to high(aint) bits }
  689. { instead of bytes to avoid double precision }
  690. { arithmetic in offset calculations }
  691. if (int64(l)+sym.fieldoffset)>high(aint) then
  692. begin
  693. Message(sym_e_segment_too_large);
  694. _datasize:=high(aint);
  695. databitsize:=high(aint);
  696. end
  697. else
  698. begin
  699. databitsize:=sym.fieldoffset+l;
  700. _datasize:=(databitsize+7) div 8;
  701. end;
  702. { rest is not applicable }
  703. exit;
  704. end;
  705. { Calc the alignment size for C style records }
  706. if (usefieldalignment=C_alignment) then
  707. begin
  708. if (varalign>4) and
  709. ((varalign mod 4)<>0) and
  710. (vardef.typ=arraydef) then
  711. Message1(sym_w_wrong_C_pack,vardef.typename);
  712. if varalign=0 then
  713. varalign:=l;
  714. if (fieldalignment<current_settings.alignment.maxCrecordalign) then
  715. begin
  716. if (varalign>16) and (fieldalignment<32) then
  717. fieldalignment:=32
  718. else if (varalign>12) and (fieldalignment<16) then
  719. fieldalignment:=16
  720. { 12 is needed for long double }
  721. else if (varalign>8) and (fieldalignment<12) then
  722. fieldalignment:=12
  723. else if (varalign>4) and (fieldalignment<8) then
  724. fieldalignment:=8
  725. else if (varalign>2) and (fieldalignment<4) then
  726. fieldalignment:=4
  727. else if (varalign>1) and (fieldalignment<2) then
  728. fieldalignment:=2;
  729. end;
  730. fieldalignment:=min(fieldalignment,current_settings.alignment.maxCrecordalign);
  731. end;
  732. if varalign=0 then
  733. varalign:=size_2_align(l);
  734. if (usefieldalignment<> bit_alignment) then
  735. varalignfield:=used_align(varalign,current_settings.alignment.recordalignmin,fieldalignment);
  736. sym.fieldoffset:=align(_datasize,varalignfield);
  737. if (int64(l)+sym.fieldoffset)>high(aint) then
  738. begin
  739. Message(sym_e_segment_too_large);
  740. _datasize:=high(aint);
  741. end
  742. else
  743. _datasize:=sym.fieldoffset+l;
  744. { Calc alignment needed for this record }
  745. if (usefieldalignment=C_alignment) then
  746. varalignrecord:=used_align(varalign,current_settings.alignment.recordalignmin,current_settings.alignment.maxCrecordalign)
  747. else
  748. if (usefieldalignment=0) then
  749. varalignrecord:=used_align(varalign,current_settings.alignment.recordalignmin,current_settings.alignment.recordalignmax)
  750. else
  751. begin
  752. { packrecords is set explicitly, ignore recordalignmax limit }
  753. varalignrecord:=used_align(varalign,current_settings.alignment.recordalignmin,usefieldalignment);
  754. end;
  755. recordalignment:=max(recordalignment,varalignrecord);
  756. end;
  757. procedure tabstractrecordsymtable.insertfield(sym:tfieldvarsym);
  758. begin
  759. insert(sym);
  760. addfield(sym);
  761. end;
  762. procedure tabstractrecordsymtable.addalignmentpadding;
  763. begin
  764. { make the record size aligned correctly so it can be
  765. used as elements in an array. For C records we
  766. use the fieldalignment, because that is updated with the
  767. used alignment. }
  768. if (padalignment = 1) then
  769. if usefieldalignment=C_alignment then
  770. padalignment:=fieldalignment
  771. else
  772. padalignment:=recordalignment;
  773. _datasize:=align(_datasize,padalignment);
  774. end;
  775. procedure tabstractrecordsymtable.insertdef(def:TDefEntry);
  776. begin
  777. { Enums must also be available outside the record scope,
  778. insert in the owner of this symtable }
  779. if def.typ=enumdef then
  780. defowner.owner.insertdef(def)
  781. else
  782. inherited insertdef(def);
  783. end;
  784. function tabstractrecordsymtable.is_packed: boolean;
  785. begin
  786. result:=usefieldalignment=bit_alignment;
  787. end;
  788. procedure tabstractrecordsymtable.setdatasize(val: aint);
  789. begin
  790. _datasize:=val;
  791. if (usefieldalignment=bit_alignment) then
  792. { can overflow in non bitpacked records }
  793. databitsize:=val*8;
  794. end;
  795. {****************************************************************************
  796. TRecordSymtable
  797. ****************************************************************************}
  798. constructor trecordsymtable.create(usealign:shortint);
  799. begin
  800. inherited create('',usealign);
  801. symtabletype:=recordsymtable;
  802. end;
  803. { this procedure is reserved for inserting case variant into
  804. a record symtable }
  805. { the offset is the location of the start of the variant
  806. and datasize and dataalignment corresponds to
  807. the complete size (see code in pdecl unit) PM }
  808. procedure trecordsymtable.insertunionst(unionst : trecordsymtable;offset : longint);
  809. var
  810. sym : tsym;
  811. def : tdef;
  812. i,
  813. varalignrecord,varalign,
  814. storesize,storealign : longint;
  815. begin
  816. storesize:=_datasize;
  817. storealign:=fieldalignment;
  818. _datasize:=offset;
  819. if (usefieldalignment=bit_alignment) then
  820. databitsize:=offset*8;
  821. { We move the ownership of the defs and symbols to the new recordsymtable.
  822. The old unionsymtable keeps the references, but doesn't own the
  823. objects anymore }
  824. unionst.DefList.OwnsObjects:=false;
  825. unionst.SymList.OwnsObjects:=false;
  826. { copy symbols }
  827. for i:=0 to unionst.SymList.Count-1 do
  828. begin
  829. sym:=TSym(unionst.SymList[i]);
  830. if sym.typ<>fieldvarsym then
  831. internalerror(200601272);
  832. { add to this record symtable }
  833. // unionst.SymList.List.List^[i].Data:=nil;
  834. sym.ChangeOwner(self);
  835. { retrieve size }
  836. if (usefieldalignment=bit_alignment) then
  837. begin
  838. { bit packed records are limited to high(aint) bits }
  839. { instead of bytes to avoid double precision }
  840. { arithmetic in offset calculations }
  841. if databitsize>high(aint) then
  842. begin
  843. Message(sym_e_segment_too_large);
  844. _datasize:=high(aint);
  845. databitsize:=high(aint);
  846. end
  847. else
  848. begin
  849. databitsize:=tfieldvarsym(sym).fieldoffset+offset*8;
  850. _datasize:=(databitsize+7) div 8;
  851. end;
  852. tfieldvarsym(sym).fieldoffset:=databitsize;
  853. end
  854. else
  855. begin
  856. _datasize:=tfieldvarsym(sym).fieldoffset+offset;
  857. if _datasize>high(aint) then
  858. begin
  859. Message(sym_e_segment_too_large);
  860. _datasize:=high(aint);
  861. end;
  862. { update address }
  863. tfieldvarsym(sym).fieldoffset:=_datasize;
  864. { update alignment of this record }
  865. varalign:=tfieldvarsym(sym).vardef.alignment;
  866. if varalign=0 then
  867. varalign:=size_2_align(tfieldvarsym(sym).getsize);
  868. varalignrecord:=used_align(varalign,current_settings.alignment.recordalignmin,fieldalignment);
  869. recordalignment:=max(recordalignment,varalignrecord);
  870. end;
  871. end;
  872. { Register defs in the new record symtable }
  873. for i:=0 to unionst.DefList.Count-1 do
  874. begin
  875. def:=TDef(unionst.DefList[i]);
  876. def.ChangeOwner(self);
  877. end;
  878. _datasize:=storesize;
  879. fieldalignment:=storealign;
  880. end;
  881. {****************************************************************************
  882. TObjectSymtable
  883. ****************************************************************************}
  884. constructor tObjectSymtable.create(adefowner:tdef;const n:string;usealign:shortint);
  885. begin
  886. inherited create(n,usealign);
  887. symtabletype:=ObjectSymtable;
  888. defowner:=adefowner;
  889. end;
  890. function tObjectSymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  891. var
  892. hsym : tsym;
  893. begin
  894. result:=false;
  895. if not assigned(defowner) then
  896. internalerror(200602061);
  897. if (m_duplicate_names in current_settings.modeswitches) and
  898. (sym.typ in [paravarsym,localvarsym]) then
  899. exit;
  900. { check for duplicate field, parameter or local names
  901. also in inherited classes }
  902. if (sym.typ in [fieldvarsym,paravarsym,localvarsym]) and
  903. (
  904. not(m_delphi in current_settings.modeswitches) or
  905. is_object(tdef(defowner))
  906. ) then
  907. begin
  908. { but private ids can be reused }
  909. hsym:=search_class_member(tobjectdef(defowner),hashedid.id);
  910. if assigned(hsym) and
  911. tsym(hsym).is_visible_for_object(tobjectdef(defowner),tobjectdef(defowner)) then
  912. begin
  913. DuplicateSym(hashedid,sym,hsym);
  914. result:=true;
  915. end;
  916. end
  917. else
  918. begin
  919. result:=inherited checkduplicate(hashedid,sym);
  920. if result then
  921. exit;
  922. end;
  923. end;
  924. {****************************************************************************
  925. TAbstractLocalSymtable
  926. ****************************************************************************}
  927. procedure tabstractlocalsymtable.ppuwrite(ppufile:tcompilerppufile);
  928. var
  929. oldtyp : byte;
  930. begin
  931. oldtyp:=ppufile.entrytyp;
  932. ppufile.entrytyp:=subentryid;
  933. { write definitions }
  934. writedefs(ppufile);
  935. { write symbols }
  936. writesyms(ppufile);
  937. ppufile.entrytyp:=oldtyp;
  938. end;
  939. function tabstractlocalsymtable.count_locals:longint;
  940. var
  941. i : longint;
  942. sym : tsym;
  943. begin
  944. result:=0;
  945. for i:=0 to SymList.Count-1 do
  946. begin
  947. sym:=tsym(SymList[i]);
  948. { Count only varsyms, but ignore the funcretsym }
  949. if (tsym(sym).typ in [localvarsym,paravarsym]) and
  950. (tsym(sym)<>current_procinfo.procdef.funcretsym) and
  951. (not(vo_is_parentfp in tabstractvarsym(sym).varoptions) or
  952. (tstoredsym(sym).refs>0)) then
  953. inc(result);
  954. end;
  955. end;
  956. {****************************************************************************
  957. TLocalSymtable
  958. ****************************************************************************}
  959. constructor tlocalsymtable.create(adefowner:tdef;level:byte);
  960. begin
  961. inherited create('');
  962. defowner:=adefowner;
  963. symtabletype:=localsymtable;
  964. symtablelevel:=level;
  965. end;
  966. function tlocalsymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  967. var
  968. hsym : tsym;
  969. begin
  970. if not assigned(defowner) or
  971. (defowner.typ<>procdef) then
  972. internalerror(200602042);
  973. result:=false;
  974. hsym:=tsym(FindWithHash(hashedid));
  975. if assigned(hsym) then
  976. begin
  977. { a local and the function can have the same
  978. name in TP and Delphi, but RESULT not }
  979. if (m_duplicate_names in current_settings.modeswitches) and
  980. (hsym.typ in [absolutevarsym,localvarsym]) and
  981. (vo_is_funcret in tabstractvarsym(hsym).varoptions) and
  982. not((m_result in current_settings.modeswitches) and
  983. (vo_is_result in tabstractvarsym(hsym).varoptions)) then
  984. HideSym(hsym)
  985. else
  986. DuplicateSym(hashedid,sym,hsym);
  987. result:=true;
  988. exit;
  989. end;
  990. { check also parasymtable, this needs to be done here becuase
  991. of the special situation with the funcret sym that needs to be
  992. hidden for tp and delphi modes }
  993. hsym:=tsym(tabstractprocdef(defowner).parast.FindWithHash(hashedid));
  994. if assigned(hsym) then
  995. begin
  996. { a local and the function can have the same
  997. name in TP and Delphi, but RESULT not }
  998. if (m_duplicate_names in current_settings.modeswitches) and
  999. (sym.typ in [absolutevarsym,localvarsym]) and
  1000. (vo_is_funcret in tabstractvarsym(sym).varoptions) and
  1001. not((m_result in current_settings.modeswitches) and
  1002. (vo_is_result in tabstractvarsym(sym).varoptions)) then
  1003. Hidesym(sym)
  1004. else
  1005. DuplicateSym(hashedid,sym,hsym);
  1006. result:=true;
  1007. exit;
  1008. end;
  1009. { check ObjectSymtable, skip this for funcret sym because
  1010. that will always be positive because it has the same name
  1011. as the procsym }
  1012. if not(m_duplicate_names in current_settings.modeswitches) and
  1013. not is_funcret_sym(sym) and
  1014. (defowner.typ=procdef) and
  1015. assigned(tprocdef(defowner)._class) and
  1016. (tprocdef(defowner).owner.defowner=tprocdef(defowner)._class) then
  1017. result:=tprocdef(defowner)._class.symtable.checkduplicate(hashedid,sym);
  1018. end;
  1019. {****************************************************************************
  1020. TParaSymtable
  1021. ****************************************************************************}
  1022. constructor tparasymtable.create(adefowner:tdef;level:byte);
  1023. begin
  1024. inherited create('');
  1025. defowner:=adefowner;
  1026. symtabletype:=parasymtable;
  1027. symtablelevel:=level;
  1028. end;
  1029. function tparasymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  1030. begin
  1031. result:=inherited checkduplicate(hashedid,sym);
  1032. if result then
  1033. exit;
  1034. if not(m_duplicate_names in current_settings.modeswitches) and
  1035. (defowner.typ=procdef) and
  1036. assigned(tprocdef(defowner)._class) and
  1037. (tprocdef(defowner).owner.defowner=tprocdef(defowner)._class) then
  1038. result:=tprocdef(defowner)._class.symtable.checkduplicate(hashedid,sym);
  1039. end;
  1040. {****************************************************************************
  1041. TAbstractUniTSymtable
  1042. ****************************************************************************}
  1043. constructor tabstractuniTSymtable.create(const n : string;id:word);
  1044. begin
  1045. inherited create(n);
  1046. moduleid:=id;
  1047. end;
  1048. function tabstractuniTSymtable.iscurrentunit:boolean;
  1049. begin
  1050. result:=assigned(current_module) and
  1051. (
  1052. (current_module.globalsymtable=self) or
  1053. (current_module.localsymtable=self)
  1054. );
  1055. end;
  1056. {****************************************************************************
  1057. TStaticSymtable
  1058. ****************************************************************************}
  1059. constructor tstaticsymtable.create(const n : string;id:word);
  1060. begin
  1061. inherited create(n,id);
  1062. symtabletype:=staticsymtable;
  1063. symtablelevel:=main_program_level;
  1064. end;
  1065. procedure tstaticsymtable.ppuload(ppufile:tcompilerppufile);
  1066. begin
  1067. inherited ppuload(ppufile);
  1068. { now we can deref the syms and defs }
  1069. deref;
  1070. end;
  1071. procedure tstaticsymtable.ppuwrite(ppufile:tcompilerppufile);
  1072. begin
  1073. inherited ppuwrite(ppufile);
  1074. end;
  1075. function tstaticsymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  1076. var
  1077. hsym : tsym;
  1078. begin
  1079. result:=false;
  1080. hsym:=tsym(FindWithHash(hashedid));
  1081. if assigned(hsym) then
  1082. begin
  1083. { Delphi you can have a symbol with the same name as the
  1084. unit, the unit can then not be accessed anymore using
  1085. <unit>.<id>, so we can hide the symbol }
  1086. if (m_duplicate_names in current_settings.modeswitches) and
  1087. (hsym.typ=symconst.unitsym) then
  1088. HideSym(hsym)
  1089. else
  1090. DuplicateSym(hashedid,sym,hsym);
  1091. result:=true;
  1092. exit;
  1093. end;
  1094. if (current_module.localsymtable=self) and
  1095. assigned(current_module.globalsymtable) then
  1096. result:=tglobalsymtable(current_module.globalsymtable).checkduplicate(hashedid,sym);
  1097. end;
  1098. {****************************************************************************
  1099. TGlobalSymtable
  1100. ****************************************************************************}
  1101. constructor tglobalsymtable.create(const n : string;id:word);
  1102. begin
  1103. inherited create(n,id);
  1104. symtabletype:=globalsymtable;
  1105. symtablelevel:=main_program_level;
  1106. end;
  1107. procedure tglobalsymtable.ppuload(ppufile:tcompilerppufile);
  1108. begin
  1109. inherited ppuload(ppufile);
  1110. { now we can deref the syms and defs }
  1111. deref;
  1112. end;
  1113. procedure tglobalsymtable.ppuwrite(ppufile:tcompilerppufile);
  1114. begin
  1115. { write the symtable entries }
  1116. inherited ppuwrite(ppufile);
  1117. end;
  1118. function tglobalsymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  1119. var
  1120. hsym : tsym;
  1121. begin
  1122. result:=false;
  1123. hsym:=tsym(FindWithHash(hashedid));
  1124. if assigned(hsym) then
  1125. begin
  1126. { Delphi you can have a symbol with the same name as the
  1127. unit, the unit can then not be accessed anymore using
  1128. <unit>.<id>, so we can hide the symbol }
  1129. if (m_duplicate_names in current_settings.modeswitches) and
  1130. (hsym.typ=symconst.unitsym) then
  1131. HideSym(hsym)
  1132. else
  1133. DuplicateSym(hashedid,sym,hsym);
  1134. result:=true;
  1135. exit;
  1136. end;
  1137. end;
  1138. {****************************************************************************
  1139. TWITHSYMTABLE
  1140. ****************************************************************************}
  1141. constructor twithsymtable.create(aowner:tdef;ASymList:TFPHashObjectList;refnode:tobject{tnode});
  1142. begin
  1143. inherited create('');
  1144. symtabletype:=withsymtable;
  1145. withrefnode:=refnode;
  1146. { Replace SymList with the passed symlist }
  1147. SymList.free;
  1148. SymList:=ASymList;
  1149. defowner:=aowner;
  1150. end;
  1151. destructor twithsymtable.destroy;
  1152. begin
  1153. withrefnode.free;
  1154. { Disable SymList because we don't Own it }
  1155. SymList:=nil;
  1156. inherited destroy;
  1157. end;
  1158. procedure twithsymtable.clear;
  1159. begin
  1160. { remove no entry from a withsymtable as it is only a pointer to the
  1161. recorddef or objectdef symtable }
  1162. end;
  1163. procedure twithsymtable.insertdef(def:TDefEntry);
  1164. begin
  1165. { Definitions can't be registered in the withsymtable
  1166. because the withsymtable is removed after the with block.
  1167. We can't easily solve it here because the next symtable in the
  1168. stack is not known. }
  1169. internalerror(200602046);
  1170. end;
  1171. {****************************************************************************
  1172. TSTT_ExceptionSymtable
  1173. ****************************************************************************}
  1174. constructor tstt_excepTSymtable.create;
  1175. begin
  1176. inherited create('');
  1177. symtabletype:=stt_excepTSymtable;
  1178. end;
  1179. {****************************************************************************
  1180. TMacroSymtable
  1181. ****************************************************************************}
  1182. constructor tmacrosymtable.create(exported: boolean);
  1183. begin
  1184. inherited create('');
  1185. if exported then
  1186. symtabletype:=exportedmacrosymtable
  1187. else
  1188. symtabletype:=localmacrosymtable;
  1189. symtablelevel:=main_program_level;
  1190. end;
  1191. {*****************************************************************************
  1192. Helper Routines
  1193. *****************************************************************************}
  1194. function FullTypeName(def,otherdef:tdef):string;
  1195. var
  1196. s1,s2 : string;
  1197. begin
  1198. s1:=def.typename;
  1199. { When the names are the same try to include the unit name }
  1200. if assigned(otherdef) and
  1201. (def.owner.symtabletype in [globalsymtable,staticsymtable]) then
  1202. begin
  1203. s2:=otherdef.typename;
  1204. if upper(s1)=upper(s2) then
  1205. s1:=def.owner.realname^+'.'+s1;
  1206. end;
  1207. FullTypeName:=s1;
  1208. end;
  1209. procedure incompatibletypes(def1,def2:tdef);
  1210. begin
  1211. { When there is an errordef there is already an error message show }
  1212. if (def2.typ=errordef) or
  1213. (def1.typ=errordef) then
  1214. exit;
  1215. CGMessage2(type_e_incompatible_types,FullTypeName(def1,def2),FullTypeName(def2,def1));
  1216. end;
  1217. procedure hidesym(sym:TSymEntry);
  1218. begin
  1219. sym.realname:='$hidden'+sym.realname;
  1220. include(tsym(sym).symoptions,sp_hidden);
  1221. end;
  1222. procedure duplicatesym(var hashedid:THashedIDString;dupsym,origsym:TSymEntry);
  1223. var
  1224. st : TSymtable;
  1225. begin
  1226. Message1(sym_e_duplicate_id,tsym(origsym).realname);
  1227. { Write hint where the original symbol was found }
  1228. st:=finduniTSymtable(origsym.owner);
  1229. with tsym(origsym).fileinfo do
  1230. begin
  1231. if assigned(st) and
  1232. (st.symtabletype=globalsymtable) and
  1233. st.iscurrentunit then
  1234. Message2(sym_h_duplicate_id_where,current_module.sourcefiles.get_file_name(fileindex),tostr(line))
  1235. else if assigned(st.name) then
  1236. Message2(sym_h_duplicate_id_where,'unit '+st.name^,tostr(line));
  1237. end;
  1238. { Rename duplicate sym to an unreachable name, but it can be
  1239. inserted in the symtable without errors }
  1240. inc(dupnr);
  1241. hashedid.id:='dup'+tostr(dupnr)+hashedid.id;
  1242. if assigned(dupsym) then
  1243. include(tsym(dupsym).symoptions,sp_implicitrename);
  1244. end;
  1245. {*****************************************************************************
  1246. Search
  1247. *****************************************************************************}
  1248. procedure addsymref(sym:tsym);
  1249. begin
  1250. { symbol uses count }
  1251. inc(sym.refs);
  1252. { unit uses count }
  1253. if assigned(current_module) and
  1254. (sym.owner.symtabletype=globalsymtable) then
  1255. begin
  1256. if tglobalsymtable(sym.owner).moduleid>=current_module.unitmapsize then
  1257. internalerror(200501152);
  1258. inc(current_module.unitmap[tglobalsymtable(sym.owner).moduleid].refs);
  1259. end;
  1260. end;
  1261. function searchsym(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  1262. var
  1263. hashedid : THashedIDString;
  1264. topclass : tobjectdef;
  1265. context : tobjectdef;
  1266. stackitem : psymtablestackitem;
  1267. begin
  1268. result:=false;
  1269. hashedid.id:=s;
  1270. stackitem:=symtablestack.stack;
  1271. while assigned(stackitem) do
  1272. begin
  1273. srsymtable:=stackitem^.symtable;
  1274. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  1275. if assigned(srsym) then
  1276. begin
  1277. topclass:=nil;
  1278. { use the class from withsymtable only when it is
  1279. defined in this unit }
  1280. if (srsymtable.symtabletype=withsymtable) and
  1281. assigned(srsymtable.defowner) and
  1282. (srsymtable.defowner.typ=objectdef) and
  1283. (srsymtable.defowner.owner.symtabletype in [globalsymtable,staticsymtable]) and
  1284. (srsymtable.defowner.owner.iscurrentunit) then
  1285. topclass:=tobjectdef(srsymtable.defowner)
  1286. else
  1287. begin
  1288. if assigned(current_procinfo) then
  1289. topclass:=current_procinfo.procdef._class;
  1290. end;
  1291. if assigned(current_procinfo) then
  1292. context:=current_procinfo.procdef._class
  1293. else
  1294. context:=nil;
  1295. if tsym(srsym).is_visible_for_object(topclass,context) then
  1296. begin
  1297. { we need to know if a procedure references symbols
  1298. in the static symtable, because then it can't be
  1299. inlined from outside this unit }
  1300. if assigned(current_procinfo) and
  1301. (srsym.owner.symtabletype=staticsymtable) then
  1302. include(current_procinfo.flags,pi_uses_static_symtable);
  1303. addsymref(srsym);
  1304. result:=true;
  1305. exit;
  1306. end;
  1307. end;
  1308. stackitem:=stackitem^.next;
  1309. end;
  1310. srsym:=nil;
  1311. srsymtable:=nil;
  1312. end;
  1313. function searchsym_type(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  1314. var
  1315. hashedid : THashedIDString;
  1316. stackitem : psymtablestackitem;
  1317. begin
  1318. result:=false;
  1319. hashedid.id:=s;
  1320. stackitem:=symtablestack.stack;
  1321. while assigned(stackitem) do
  1322. begin
  1323. {
  1324. It is not possible to have type symbols in:
  1325. records
  1326. objects
  1327. parameters
  1328. Exception are generic definitions and specializations
  1329. that have the parameterized types inserted in the symtable.
  1330. }
  1331. srsymtable:=stackitem^.symtable;
  1332. if not(srsymtable.symtabletype in [recordsymtable,ObjectSymtable,parasymtable]) or
  1333. (assigned(srsymtable.defowner) and
  1334. (
  1335. (df_generic in tdef(srsymtable.defowner).defoptions) or
  1336. (df_specialization in tdef(srsymtable.defowner).defoptions))
  1337. ) then
  1338. begin
  1339. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  1340. if assigned(srsym) and
  1341. not(srsym.typ in [fieldvarsym,paravarsym]) and
  1342. (not assigned(current_procinfo) or
  1343. tsym(srsym).is_visible_for_object(current_procinfo.procdef._class,current_procinfo.procdef._class)) then
  1344. begin
  1345. { we need to know if a procedure references symbols
  1346. in the static symtable, because then it can't be
  1347. inlined from outside this unit }
  1348. if assigned(current_procinfo) and
  1349. (srsym.owner.symtabletype=staticsymtable) then
  1350. include(current_procinfo.flags,pi_uses_static_symtable);
  1351. addsymref(srsym);
  1352. result:=true;
  1353. exit;
  1354. end;
  1355. end;
  1356. stackitem:=stackitem^.next;
  1357. end;
  1358. result:=false;
  1359. srsym:=nil;
  1360. srsymtable:=nil;
  1361. end;
  1362. function searchsym_in_module(pm:pointer;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  1363. var
  1364. pmod : tmodule;
  1365. begin
  1366. pmod:=tmodule(pm);
  1367. result:=false;
  1368. if assigned(pmod.globalsymtable) then
  1369. begin
  1370. srsym:=tsym(pmod.globalsymtable.Find(s));
  1371. if assigned(srsym) then
  1372. begin
  1373. srsymtable:=pmod.globalsymtable;
  1374. addsymref(srsym);
  1375. result:=true;
  1376. exit;
  1377. end;
  1378. end;
  1379. { If the module is the current unit we also need
  1380. to search the local symtable }
  1381. if (pmod=current_module) and
  1382. assigned(pmod.localsymtable) then
  1383. begin
  1384. srsym:=tsym(pmod.localsymtable.Find(s));
  1385. if assigned(srsym) then
  1386. begin
  1387. srsymtable:=pmod.localsymtable;
  1388. addsymref(srsym);
  1389. result:=true;
  1390. exit;
  1391. end;
  1392. end;
  1393. srsym:=nil;
  1394. srsymtable:=nil;
  1395. end;
  1396. function searchsym_in_class(classh,contextclassh:tobjectdef;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  1397. var
  1398. hashedid : THashedIDString;
  1399. currentclassh : tobjectdef;
  1400. begin
  1401. result:=false;
  1402. hashedid.id:=s;
  1403. if assigned(current_procinfo.procdef) then
  1404. currentclassh:=current_procinfo.procdef._class
  1405. else
  1406. currentclassh:=nil;
  1407. while assigned(classh) do
  1408. begin
  1409. srsymtable:=classh.symtable;
  1410. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  1411. if assigned(srsym) and
  1412. tsym(srsym).is_visible_for_object(contextclassh,currentclassh) then
  1413. begin
  1414. addsymref(srsym);
  1415. result:=true;
  1416. exit;
  1417. end;
  1418. classh:=classh.childof;
  1419. end;
  1420. srsym:=nil;
  1421. srsymtable:=nil;
  1422. end;
  1423. function searchsym_in_class_by_msgint(classh:tobjectdef;msgid:longint;out srdef : tdef;out srsym:tsym;out srsymtable:TSymtable):boolean;
  1424. var
  1425. def : tdef;
  1426. i : longint;
  1427. begin
  1428. result:=false;
  1429. def:=nil;
  1430. while assigned(classh) do
  1431. begin
  1432. for i:=0 to classh.symtable.DefList.Count-1 do
  1433. begin
  1434. def:=tstoreddef(classh.symtable.DefList[i]);
  1435. { Find also all hidden private methods to
  1436. be compatible with delphi, see tw6203 (PFV) }
  1437. if (def.typ=procdef) and
  1438. (po_msgint in tprocdef(def).procoptions) and
  1439. (tprocdef(def).messageinf.i=msgid) then
  1440. begin
  1441. srdef:=def;
  1442. srsym:=tprocdef(def).procsym;
  1443. srsymtable:=classh.symtable;
  1444. addsymref(srsym);
  1445. result:=true;
  1446. exit;
  1447. end;
  1448. end;
  1449. classh:=classh.childof;
  1450. end;
  1451. srsym:=nil;
  1452. srsymtable:=nil;
  1453. end;
  1454. function searchsym_in_class_by_msgstr(classh:tobjectdef;const s:string;out srsym:tsym;out srsymtable:TSymtable):boolean;
  1455. var
  1456. def : tdef;
  1457. i : longint;
  1458. begin
  1459. result:=false;
  1460. def:=nil;
  1461. while assigned(classh) do
  1462. begin
  1463. for i:=0 to classh.symtable.DefList.Count-1 do
  1464. begin
  1465. def:=tstoreddef(classh.symtable.DefList[i]);
  1466. { Find also all hidden private methods to
  1467. be compatible with delphi, see tw6203 (PFV) }
  1468. if (def.typ=procdef) and
  1469. (po_msgstr in tprocdef(def).procoptions) and
  1470. (tprocdef(def).messageinf.str^=s) then
  1471. begin
  1472. srsym:=tprocdef(def).procsym;
  1473. srsymtable:=classh.symtable;
  1474. addsymref(srsym);
  1475. result:=true;
  1476. exit;
  1477. end;
  1478. end;
  1479. classh:=classh.childof;
  1480. end;
  1481. srsym:=nil;
  1482. srsymtable:=nil;
  1483. end;
  1484. function search_assignment_operator(from_def,to_def:Tdef):Tprocdef;
  1485. var
  1486. sym : Tprocsym;
  1487. hashedid : THashedIDString;
  1488. curreq,
  1489. besteq : tequaltype;
  1490. currpd,
  1491. bestpd : tprocdef;
  1492. stackitem : psymtablestackitem;
  1493. begin
  1494. hashedid.id:='assign';
  1495. besteq:=te_incompatible;
  1496. bestpd:=nil;
  1497. stackitem:=symtablestack.stack;
  1498. while assigned(stackitem) do
  1499. begin
  1500. sym:=Tprocsym(stackitem^.symtable.FindWithHash(hashedid));
  1501. if sym<>nil then
  1502. begin
  1503. if sym.typ<>procsym then
  1504. internalerror(200402031);
  1505. { if the source type is an alias then this is only the second choice,
  1506. if you mess with this code, check tw4093 }
  1507. currpd:=sym.find_procdef_assignment_operator(from_def,to_def,curreq);
  1508. if curreq>besteq then
  1509. begin
  1510. besteq:=curreq;
  1511. bestpd:=currpd;
  1512. if (besteq=te_exact) then
  1513. break;
  1514. end;
  1515. end;
  1516. stackitem:=stackitem^.next;
  1517. end;
  1518. result:=bestpd;
  1519. end;
  1520. function search_system_type(const s: TIDString): ttypesym;
  1521. var
  1522. sym : tsym;
  1523. begin
  1524. sym:=tsym(systemunit.Find(s));
  1525. if not assigned(sym) or
  1526. (sym.typ<>typesym) then
  1527. cgmessage1(cg_f_unknown_system_type,s);
  1528. result:=ttypesym(sym);
  1529. end;
  1530. function search_class_member(pd : tobjectdef;const s : string):tsym;
  1531. { searches n in symtable of pd and all anchestors }
  1532. var
  1533. hashedid : THashedIDString;
  1534. srsym : tsym;
  1535. begin
  1536. hashedid.id:=s;
  1537. while assigned(pd) do
  1538. begin
  1539. srsym:=tsym(pd.symtable.FindWithHash(hashedid));
  1540. if assigned(srsym) then
  1541. begin
  1542. search_class_member:=srsym;
  1543. exit;
  1544. end;
  1545. pd:=pd.childof;
  1546. end;
  1547. search_class_member:=nil;
  1548. end;
  1549. function search_macro(const s : string):tsym;
  1550. var
  1551. stackitem : psymtablestackitem;
  1552. hashedid : THashedIDString;
  1553. srsym : tsym;
  1554. begin
  1555. hashedid.id:=s;
  1556. { First search the localmacrosymtable before searching the
  1557. global macrosymtables from the units }
  1558. if assigned(current_module) then
  1559. begin
  1560. srsym:=tsym(current_module.localmacrosymtable.FindWithHash(hashedid));
  1561. if assigned(srsym) then
  1562. begin
  1563. result:= srsym;
  1564. exit;
  1565. end;
  1566. end;
  1567. stackitem:=macrosymtablestack.stack;
  1568. while assigned(stackitem) do
  1569. begin
  1570. srsym:=tsym(stackitem^.symtable.FindWithHash(hashedid));
  1571. if assigned(srsym) then
  1572. begin
  1573. result:= srsym;
  1574. exit;
  1575. end;
  1576. stackitem:=stackitem^.next;
  1577. end;
  1578. result:= nil;
  1579. end;
  1580. {****************************************************************************
  1581. Object Helpers
  1582. ****************************************************************************}
  1583. procedure search_class_overloads(aprocsym : tprocsym);
  1584. { searches n in symtable of pd and all anchestors }
  1585. var
  1586. hashedid : THashedIDString;
  1587. srsym : tprocsym;
  1588. objdef : tobjectdef;
  1589. begin
  1590. if aprocsym.overloadchecked then
  1591. exit;
  1592. aprocsym.overloadchecked:=true;
  1593. if (aprocsym.owner.symtabletype<>ObjectSymtable) then
  1594. internalerror(200111021);
  1595. objdef:=tobjectdef(aprocsym.owner.defowner);
  1596. { we start in the parent }
  1597. if not assigned(objdef.childof) then
  1598. exit;
  1599. objdef:=objdef.childof;
  1600. hashedid.id:=aprocsym.name;
  1601. while assigned(objdef) do
  1602. begin
  1603. srsym:=tprocsym(objdef.symtable.FindWithHash(hashedid));
  1604. if assigned(srsym) then
  1605. begin
  1606. if (srsym.typ<>procsym) then
  1607. internalerror(200111022);
  1608. if srsym.is_visible_for_object(tobjectdef(aprocsym.owner.defowner),tobjectdef(aprocsym.owner.defowner)) then
  1609. begin
  1610. srsym.add_para_match_to(Aprocsym,[cpo_ignorehidden,cpo_allowdefaults]);
  1611. { we can stop if the overloads were already added
  1612. for the found symbol }
  1613. if srsym.overloadchecked then
  1614. break;
  1615. end;
  1616. end;
  1617. { next parent }
  1618. objdef:=objdef.childof;
  1619. end;
  1620. end;
  1621. procedure tstoredsymtable.testfordefaultproperty(sym:TObject;arg:pointer);
  1622. begin
  1623. if (tsym(sym).typ=propertysym) and
  1624. (ppo_defaultproperty in tpropertysym(sym).propoptions) then
  1625. ppointer(arg)^:=sym;
  1626. end;
  1627. function search_default_property(pd : tobjectdef) : tpropertysym;
  1628. { returns the default property of a class, searches also anchestors }
  1629. var
  1630. _defaultprop : tpropertysym;
  1631. begin
  1632. _defaultprop:=nil;
  1633. while assigned(pd) do
  1634. begin
  1635. pd.symtable.SymList.ForEachCall(@tstoredsymtable(pd.symtable).testfordefaultproperty,@_defaultprop);
  1636. if assigned(_defaultprop) then
  1637. break;
  1638. pd:=pd.childof;
  1639. end;
  1640. search_default_property:=_defaultprop;
  1641. end;
  1642. {****************************************************************************
  1643. Macro Helpers
  1644. ****************************************************************************}
  1645. procedure def_system_macro(const name : string);
  1646. var
  1647. mac : tmacro;
  1648. s: string;
  1649. begin
  1650. if name = '' then
  1651. internalerror(2004121201);
  1652. s:= upper(name);
  1653. mac:=tmacro(search_macro(s));
  1654. if not assigned(mac) then
  1655. begin
  1656. mac:=tmacro.create(s);
  1657. if assigned(current_module) then
  1658. current_module.localmacrosymtable.insert(mac)
  1659. else
  1660. initialmacrosymtable.insert(mac);
  1661. end;
  1662. if not mac.defined then
  1663. Message1(parser_c_macro_defined,mac.name);
  1664. mac.defined:=true;
  1665. end;
  1666. procedure set_system_macro(const name, value : string);
  1667. var
  1668. mac : tmacro;
  1669. s: string;
  1670. begin
  1671. if name = '' then
  1672. internalerror(2004121201);
  1673. s:= upper(name);
  1674. mac:=tmacro(search_macro(s));
  1675. if not assigned(mac) then
  1676. begin
  1677. mac:=tmacro.create(s);
  1678. if assigned(current_module) then
  1679. current_module.localmacrosymtable.insert(mac)
  1680. else
  1681. initialmacrosymtable.insert(mac);
  1682. end
  1683. else
  1684. begin
  1685. mac.is_compiler_var:=false;
  1686. if assigned(mac.buftext) then
  1687. freemem(mac.buftext,mac.buflen);
  1688. end;
  1689. Message2(parser_c_macro_set_to,mac.name,value);
  1690. mac.buflen:=length(value);
  1691. getmem(mac.buftext,mac.buflen);
  1692. move(value[1],mac.buftext^,mac.buflen);
  1693. mac.defined:=true;
  1694. end;
  1695. procedure set_system_compvar(const name, value : string);
  1696. var
  1697. mac : tmacro;
  1698. s: string;
  1699. begin
  1700. if name = '' then
  1701. internalerror(2004121201);
  1702. s:= upper(name);
  1703. mac:=tmacro(search_macro(s));
  1704. if not assigned(mac) then
  1705. begin
  1706. mac:=tmacro.create(s);
  1707. mac.is_compiler_var:=true;
  1708. if assigned(current_module) then
  1709. current_module.localmacrosymtable.insert(mac)
  1710. else
  1711. initialmacrosymtable.insert(mac);
  1712. end
  1713. else
  1714. begin
  1715. mac.is_compiler_var:=true;
  1716. if assigned(mac.buftext) then
  1717. freemem(mac.buftext,mac.buflen);
  1718. end;
  1719. Message2(parser_c_macro_set_to,mac.name,value);
  1720. mac.buflen:=length(value);
  1721. getmem(mac.buftext,mac.buflen);
  1722. move(value[1],mac.buftext^,mac.buflen);
  1723. mac.defined:=true;
  1724. end;
  1725. procedure undef_system_macro(const name : string);
  1726. var
  1727. mac : tmacro;
  1728. s: string;
  1729. begin
  1730. if name = '' then
  1731. internalerror(2004121201);
  1732. s:= upper(name);
  1733. mac:=tmacro(search_macro(s));
  1734. if not assigned(mac) then
  1735. {If not found, then it's already undefined.}
  1736. else
  1737. begin
  1738. if mac.defined then
  1739. Message1(parser_c_macro_undefined,mac.name);
  1740. mac.defined:=false;
  1741. mac.is_compiler_var:=false;
  1742. { delete old definition }
  1743. if assigned(mac.buftext) then
  1744. begin
  1745. freemem(mac.buftext,mac.buflen);
  1746. mac.buftext:=nil;
  1747. end;
  1748. end;
  1749. end;
  1750. {$ifdef UNITALIASES}
  1751. {****************************************************************************
  1752. TUNIT_ALIAS
  1753. ****************************************************************************}
  1754. constructor tunit_alias.create(const n:string);
  1755. var
  1756. i : longint;
  1757. begin
  1758. i:=pos('=',n);
  1759. if i=0 then
  1760. fail;
  1761. inherited createname(Copy(n,1,i-1));
  1762. newname:=stringdup(Copy(n,i+1,255));
  1763. end;
  1764. destructor tunit_alias.destroy;
  1765. begin
  1766. stringdispose(newname);
  1767. inherited destroy;
  1768. end;
  1769. procedure addunitalias(const n:string);
  1770. begin
  1771. unitaliases^.insert(tunit_alias,init(Upper(n))));
  1772. end;
  1773. function getunitalias(const n:string):string;
  1774. var
  1775. p : punit_alias;
  1776. begin
  1777. p:=punit_alias(unitaliases^.Find(Upper(n)));
  1778. if assigned(p) then
  1779. getunitalias:=punit_alias(p).newname^
  1780. else
  1781. getunitalias:=n;
  1782. end;
  1783. {$endif UNITALIASES}
  1784. {****************************************************************************
  1785. Init/Done Symtable
  1786. ****************************************************************************}
  1787. procedure IniTSymtable;
  1788. begin
  1789. { Reset symbolstack }
  1790. symtablestack:=nil;
  1791. systemunit:=nil;
  1792. { create error syms and def }
  1793. generrorsym:=terrorsym.create;
  1794. generrordef:=terrordef.create;
  1795. { macros }
  1796. initialmacrosymtable:=tmacrosymtable.create(false);
  1797. macrosymtablestack:=TSymtablestack.create;
  1798. macrosymtablestack.push(initialmacrosymtable);
  1799. {$ifdef UNITALIASES}
  1800. { unit aliases }
  1801. unitaliases:=TFPHashObjectList.create;
  1802. {$endif}
  1803. { set some global vars to nil, might be important for the ide }
  1804. class_tobject:=nil;
  1805. interface_iunknown:=nil;
  1806. rec_tguid:=nil;
  1807. dupnr:=0;
  1808. end;
  1809. procedure DoneSymtable;
  1810. begin
  1811. generrorsym.owner:=nil;
  1812. generrorsym.free;
  1813. generrordef.owner:=nil;
  1814. generrordef.free;
  1815. initialmacrosymtable.free;
  1816. macrosymtablestack.free;
  1817. {$ifdef UNITALIASES}
  1818. unitaliases.free;
  1819. {$endif}
  1820. end;
  1821. end.