symtable.pas 71 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090
  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) and
  1026. { delphi allows local typed consts. having the same name as class members, probably
  1027. a delphi bug, but some delphi code depends on it }
  1028. not((m_duplicate_names in current_settings.modeswitches) and (sym.typ=staticvarsym)) then
  1029. result:=tprocdef(defowner)._class.symtable.checkduplicate(hashedid,sym);
  1030. end;
  1031. {****************************************************************************
  1032. TParaSymtable
  1033. ****************************************************************************}
  1034. constructor tparasymtable.create(adefowner:tdef;level:byte);
  1035. begin
  1036. inherited create('');
  1037. defowner:=adefowner;
  1038. symtabletype:=parasymtable;
  1039. symtablelevel:=level;
  1040. end;
  1041. function tparasymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  1042. begin
  1043. result:=inherited checkduplicate(hashedid,sym);
  1044. if result then
  1045. exit;
  1046. if not(m_duplicate_names in current_settings.modeswitches) and
  1047. (defowner.typ=procdef) and
  1048. assigned(tprocdef(defowner)._class) and
  1049. (tprocdef(defowner).owner.defowner=tprocdef(defowner)._class) then
  1050. result:=tprocdef(defowner)._class.symtable.checkduplicate(hashedid,sym);
  1051. end;
  1052. {****************************************************************************
  1053. TAbstractUniTSymtable
  1054. ****************************************************************************}
  1055. constructor tabstractuniTSymtable.create(const n : string;id:word);
  1056. begin
  1057. inherited create(n);
  1058. moduleid:=id;
  1059. end;
  1060. function tabstractuniTSymtable.iscurrentunit:boolean;
  1061. begin
  1062. result:=assigned(current_module) and
  1063. (
  1064. (current_module.globalsymtable=self) or
  1065. (current_module.localsymtable=self)
  1066. );
  1067. end;
  1068. {****************************************************************************
  1069. TStaticSymtable
  1070. ****************************************************************************}
  1071. constructor tstaticsymtable.create(const n : string;id:word);
  1072. begin
  1073. inherited create(n,id);
  1074. symtabletype:=staticsymtable;
  1075. symtablelevel:=main_program_level;
  1076. end;
  1077. procedure tstaticsymtable.ppuload(ppufile:tcompilerppufile);
  1078. begin
  1079. inherited ppuload(ppufile);
  1080. { now we can deref the syms and defs }
  1081. deref;
  1082. end;
  1083. procedure tstaticsymtable.ppuwrite(ppufile:tcompilerppufile);
  1084. begin
  1085. inherited ppuwrite(ppufile);
  1086. end;
  1087. function tstaticsymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  1088. var
  1089. hsym : tsym;
  1090. begin
  1091. result:=false;
  1092. hsym:=tsym(FindWithHash(hashedid));
  1093. if assigned(hsym) then
  1094. begin
  1095. { Delphi you can have a symbol with the same name as the
  1096. unit, the unit can then not be accessed anymore using
  1097. <unit>.<id>, so we can hide the symbol }
  1098. if (m_duplicate_names in current_settings.modeswitches) and
  1099. (hsym.typ=symconst.unitsym) then
  1100. HideSym(hsym)
  1101. else
  1102. DuplicateSym(hashedid,sym,hsym);
  1103. result:=true;
  1104. exit;
  1105. end;
  1106. if (current_module.localsymtable=self) and
  1107. assigned(current_module.globalsymtable) then
  1108. result:=tglobalsymtable(current_module.globalsymtable).checkduplicate(hashedid,sym);
  1109. end;
  1110. {****************************************************************************
  1111. TGlobalSymtable
  1112. ****************************************************************************}
  1113. constructor tglobalsymtable.create(const n : string;id:word);
  1114. begin
  1115. inherited create(n,id);
  1116. symtabletype:=globalsymtable;
  1117. symtablelevel:=main_program_level;
  1118. end;
  1119. procedure tglobalsymtable.ppuload(ppufile:tcompilerppufile);
  1120. begin
  1121. inherited ppuload(ppufile);
  1122. { now we can deref the syms and defs }
  1123. deref;
  1124. end;
  1125. procedure tglobalsymtable.ppuwrite(ppufile:tcompilerppufile);
  1126. begin
  1127. { write the symtable entries }
  1128. inherited ppuwrite(ppufile);
  1129. end;
  1130. function tglobalsymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  1131. var
  1132. hsym : tsym;
  1133. begin
  1134. result:=false;
  1135. hsym:=tsym(FindWithHash(hashedid));
  1136. if assigned(hsym) then
  1137. begin
  1138. { Delphi you can have a symbol with the same name as the
  1139. unit, the unit can then not be accessed anymore using
  1140. <unit>.<id>, so we can hide the symbol }
  1141. if (m_duplicate_names in current_settings.modeswitches) and
  1142. (hsym.typ=symconst.unitsym) then
  1143. HideSym(hsym)
  1144. else
  1145. DuplicateSym(hashedid,sym,hsym);
  1146. result:=true;
  1147. exit;
  1148. end;
  1149. end;
  1150. {****************************************************************************
  1151. TWITHSYMTABLE
  1152. ****************************************************************************}
  1153. constructor twithsymtable.create(aowner:tdef;ASymList:TFPHashObjectList;refnode:tobject{tnode});
  1154. begin
  1155. inherited create('');
  1156. symtabletype:=withsymtable;
  1157. withrefnode:=refnode;
  1158. { Replace SymList with the passed symlist }
  1159. SymList.free;
  1160. SymList:=ASymList;
  1161. defowner:=aowner;
  1162. end;
  1163. destructor twithsymtable.destroy;
  1164. begin
  1165. withrefnode.free;
  1166. { Disable SymList because we don't Own it }
  1167. SymList:=nil;
  1168. inherited destroy;
  1169. end;
  1170. procedure twithsymtable.clear;
  1171. begin
  1172. { remove no entry from a withsymtable as it is only a pointer to the
  1173. recorddef or objectdef symtable }
  1174. end;
  1175. procedure twithsymtable.insertdef(def:TDefEntry);
  1176. begin
  1177. { Definitions can't be registered in the withsymtable
  1178. because the withsymtable is removed after the with block.
  1179. We can't easily solve it here because the next symtable in the
  1180. stack is not known. }
  1181. internalerror(200602046);
  1182. end;
  1183. {****************************************************************************
  1184. TSTT_ExceptionSymtable
  1185. ****************************************************************************}
  1186. constructor tstt_excepTSymtable.create;
  1187. begin
  1188. inherited create('');
  1189. symtabletype:=stt_excepTSymtable;
  1190. end;
  1191. {****************************************************************************
  1192. TMacroSymtable
  1193. ****************************************************************************}
  1194. constructor tmacrosymtable.create(exported: boolean);
  1195. begin
  1196. inherited create('');
  1197. if exported then
  1198. symtabletype:=exportedmacrosymtable
  1199. else
  1200. symtabletype:=localmacrosymtable;
  1201. symtablelevel:=main_program_level;
  1202. end;
  1203. {*****************************************************************************
  1204. Helper Routines
  1205. *****************************************************************************}
  1206. function FullTypeName(def,otherdef:tdef):string;
  1207. var
  1208. s1,s2 : string;
  1209. begin
  1210. s1:=def.typename;
  1211. { When the names are the same try to include the unit name }
  1212. if assigned(otherdef) and
  1213. (def.owner.symtabletype in [globalsymtable,staticsymtable]) then
  1214. begin
  1215. s2:=otherdef.typename;
  1216. if upper(s1)=upper(s2) then
  1217. s1:=def.owner.realname^+'.'+s1;
  1218. end;
  1219. FullTypeName:=s1;
  1220. end;
  1221. procedure incompatibletypes(def1,def2:tdef);
  1222. begin
  1223. { When there is an errordef there is already an error message show }
  1224. if (def2.typ=errordef) or
  1225. (def1.typ=errordef) then
  1226. exit;
  1227. CGMessage2(type_e_incompatible_types,FullTypeName(def1,def2),FullTypeName(def2,def1));
  1228. end;
  1229. procedure hidesym(sym:TSymEntry);
  1230. begin
  1231. sym.realname:='$hidden'+sym.realname;
  1232. include(tsym(sym).symoptions,sp_hidden);
  1233. end;
  1234. procedure duplicatesym(var hashedid:THashedIDString;dupsym,origsym:TSymEntry);
  1235. var
  1236. st : TSymtable;
  1237. begin
  1238. Message1(sym_e_duplicate_id,tsym(origsym).realname);
  1239. { Write hint where the original symbol was found }
  1240. st:=finduniTSymtable(origsym.owner);
  1241. with tsym(origsym).fileinfo do
  1242. begin
  1243. if assigned(st) and
  1244. (st.symtabletype=globalsymtable) and
  1245. st.iscurrentunit then
  1246. Message2(sym_h_duplicate_id_where,current_module.sourcefiles.get_file_name(fileindex),tostr(line))
  1247. else if assigned(st.name) then
  1248. Message2(sym_h_duplicate_id_where,'unit '+st.name^,tostr(line));
  1249. end;
  1250. { Rename duplicate sym to an unreachable name, but it can be
  1251. inserted in the symtable without errors }
  1252. inc(dupnr);
  1253. hashedid.id:='dup'+tostr(dupnr)+hashedid.id;
  1254. if assigned(dupsym) then
  1255. include(tsym(dupsym).symoptions,sp_implicitrename);
  1256. end;
  1257. {*****************************************************************************
  1258. Search
  1259. *****************************************************************************}
  1260. procedure addsymref(sym:tsym);
  1261. begin
  1262. { symbol uses count }
  1263. sym.IncRefCount;
  1264. { unit uses count }
  1265. if assigned(current_module) and
  1266. (sym.owner.symtabletype=globalsymtable) then
  1267. begin
  1268. if tglobalsymtable(sym.owner).moduleid>=current_module.unitmapsize then
  1269. internalerror(200501152);
  1270. inc(current_module.unitmap[tglobalsymtable(sym.owner).moduleid].refs);
  1271. end;
  1272. end;
  1273. function searchsym(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  1274. var
  1275. hashedid : THashedIDString;
  1276. topclass : tobjectdef;
  1277. context : tobjectdef;
  1278. stackitem : psymtablestackitem;
  1279. begin
  1280. result:=false;
  1281. hashedid.id:=s;
  1282. stackitem:=symtablestack.stack;
  1283. while assigned(stackitem) do
  1284. begin
  1285. srsymtable:=stackitem^.symtable;
  1286. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  1287. if assigned(srsym) then
  1288. begin
  1289. topclass:=nil;
  1290. { use the class from withsymtable only when it is
  1291. defined in this unit }
  1292. if (srsymtable.symtabletype=withsymtable) and
  1293. assigned(srsymtable.defowner) and
  1294. (srsymtable.defowner.typ=objectdef) and
  1295. (srsymtable.defowner.owner.symtabletype in [globalsymtable,staticsymtable]) and
  1296. (srsymtable.defowner.owner.iscurrentunit) then
  1297. topclass:=tobjectdef(srsymtable.defowner)
  1298. else
  1299. begin
  1300. if assigned(current_procinfo) then
  1301. topclass:=current_procinfo.procdef._class;
  1302. end;
  1303. if assigned(current_procinfo) then
  1304. context:=current_procinfo.procdef._class
  1305. else
  1306. context:=nil;
  1307. if tsym(srsym).is_visible_for_object(topclass,context) then
  1308. begin
  1309. { we need to know if a procedure references symbols
  1310. in the static symtable, because then it can't be
  1311. inlined from outside this unit }
  1312. if assigned(current_procinfo) and
  1313. (srsym.owner.symtabletype=staticsymtable) then
  1314. include(current_procinfo.flags,pi_uses_static_symtable);
  1315. addsymref(srsym);
  1316. result:=true;
  1317. exit;
  1318. end;
  1319. end;
  1320. stackitem:=stackitem^.next;
  1321. end;
  1322. srsym:=nil;
  1323. srsymtable:=nil;
  1324. end;
  1325. function searchsym_type(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  1326. var
  1327. hashedid : THashedIDString;
  1328. stackitem : psymtablestackitem;
  1329. begin
  1330. result:=false;
  1331. hashedid.id:=s;
  1332. stackitem:=symtablestack.stack;
  1333. while assigned(stackitem) do
  1334. begin
  1335. {
  1336. It is not possible to have type symbols in:
  1337. records
  1338. objects
  1339. parameters
  1340. Exception are generic definitions and specializations
  1341. that have the parameterized types inserted in the symtable.
  1342. }
  1343. srsymtable:=stackitem^.symtable;
  1344. if not(srsymtable.symtabletype in [recordsymtable,ObjectSymtable,parasymtable]) or
  1345. (assigned(srsymtable.defowner) and
  1346. (
  1347. (df_generic in tdef(srsymtable.defowner).defoptions) or
  1348. (df_specialization in tdef(srsymtable.defowner).defoptions))
  1349. ) then
  1350. begin
  1351. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  1352. if assigned(srsym) and
  1353. not(srsym.typ in [fieldvarsym,paravarsym]) and
  1354. (not assigned(current_procinfo) or
  1355. tsym(srsym).is_visible_for_object(current_procinfo.procdef._class,current_procinfo.procdef._class)) then
  1356. begin
  1357. { we need to know if a procedure references symbols
  1358. in the static symtable, because then it can't be
  1359. inlined from outside this unit }
  1360. if assigned(current_procinfo) and
  1361. (srsym.owner.symtabletype=staticsymtable) then
  1362. include(current_procinfo.flags,pi_uses_static_symtable);
  1363. addsymref(srsym);
  1364. result:=true;
  1365. exit;
  1366. end;
  1367. end;
  1368. stackitem:=stackitem^.next;
  1369. end;
  1370. result:=false;
  1371. srsym:=nil;
  1372. srsymtable:=nil;
  1373. end;
  1374. function searchsym_in_module(pm:pointer;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  1375. var
  1376. pmod : tmodule;
  1377. begin
  1378. pmod:=tmodule(pm);
  1379. result:=false;
  1380. if assigned(pmod.globalsymtable) then
  1381. begin
  1382. srsym:=tsym(pmod.globalsymtable.Find(s));
  1383. if assigned(srsym) then
  1384. begin
  1385. srsymtable:=pmod.globalsymtable;
  1386. addsymref(srsym);
  1387. result:=true;
  1388. exit;
  1389. end;
  1390. end;
  1391. { If the module is the current unit we also need
  1392. to search the local symtable }
  1393. if (pmod=current_module) and
  1394. assigned(pmod.localsymtable) then
  1395. begin
  1396. srsym:=tsym(pmod.localsymtable.Find(s));
  1397. if assigned(srsym) then
  1398. begin
  1399. srsymtable:=pmod.localsymtable;
  1400. addsymref(srsym);
  1401. result:=true;
  1402. exit;
  1403. end;
  1404. end;
  1405. srsym:=nil;
  1406. srsymtable:=nil;
  1407. end;
  1408. function searchsym_in_class(classh,contextclassh:tobjectdef;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  1409. var
  1410. hashedid : THashedIDString;
  1411. currentclassh : tobjectdef;
  1412. begin
  1413. result:=false;
  1414. hashedid.id:=s;
  1415. if assigned(current_procinfo.procdef) then
  1416. currentclassh:=current_procinfo.procdef._class
  1417. else
  1418. currentclassh:=nil;
  1419. while assigned(classh) do
  1420. begin
  1421. srsymtable:=classh.symtable;
  1422. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  1423. if assigned(srsym) and
  1424. tsym(srsym).is_visible_for_object(contextclassh,currentclassh) then
  1425. begin
  1426. addsymref(srsym);
  1427. result:=true;
  1428. exit;
  1429. end;
  1430. classh:=classh.childof;
  1431. end;
  1432. srsym:=nil;
  1433. srsymtable:=nil;
  1434. end;
  1435. function searchsym_in_class_by_msgint(classh:tobjectdef;msgid:longint;out srdef : tdef;out srsym:tsym;out srsymtable:TSymtable):boolean;
  1436. var
  1437. def : tdef;
  1438. i : longint;
  1439. begin
  1440. result:=false;
  1441. def:=nil;
  1442. while assigned(classh) do
  1443. begin
  1444. for i:=0 to classh.symtable.DefList.Count-1 do
  1445. begin
  1446. def:=tstoreddef(classh.symtable.DefList[i]);
  1447. { Find also all hidden private methods to
  1448. be compatible with delphi, see tw6203 (PFV) }
  1449. if (def.typ=procdef) and
  1450. (po_msgint in tprocdef(def).procoptions) and
  1451. (tprocdef(def).messageinf.i=msgid) then
  1452. begin
  1453. srdef:=def;
  1454. srsym:=tprocdef(def).procsym;
  1455. srsymtable:=classh.symtable;
  1456. addsymref(srsym);
  1457. result:=true;
  1458. exit;
  1459. end;
  1460. end;
  1461. classh:=classh.childof;
  1462. end;
  1463. srdef:=nil;
  1464. srsym:=nil;
  1465. srsymtable:=nil;
  1466. end;
  1467. function searchsym_in_class_by_msgstr(classh:tobjectdef;const s:string;out srsym:tsym;out srsymtable:TSymtable):boolean;
  1468. var
  1469. def : tdef;
  1470. i : longint;
  1471. begin
  1472. result:=false;
  1473. def:=nil;
  1474. while assigned(classh) do
  1475. begin
  1476. for i:=0 to classh.symtable.DefList.Count-1 do
  1477. begin
  1478. def:=tstoreddef(classh.symtable.DefList[i]);
  1479. { Find also all hidden private methods to
  1480. be compatible with delphi, see tw6203 (PFV) }
  1481. if (def.typ=procdef) and
  1482. (po_msgstr in tprocdef(def).procoptions) and
  1483. (tprocdef(def).messageinf.str^=s) then
  1484. begin
  1485. srsym:=tprocdef(def).procsym;
  1486. srsymtable:=classh.symtable;
  1487. addsymref(srsym);
  1488. result:=true;
  1489. exit;
  1490. end;
  1491. end;
  1492. classh:=classh.childof;
  1493. end;
  1494. srsym:=nil;
  1495. srsymtable:=nil;
  1496. end;
  1497. function search_assignment_operator(from_def,to_def:Tdef):Tprocdef;
  1498. var
  1499. sym : Tprocsym;
  1500. hashedid : THashedIDString;
  1501. curreq,
  1502. besteq : tequaltype;
  1503. currpd,
  1504. bestpd : tprocdef;
  1505. stackitem : psymtablestackitem;
  1506. begin
  1507. hashedid.id:='assign';
  1508. besteq:=te_incompatible;
  1509. bestpd:=nil;
  1510. stackitem:=symtablestack.stack;
  1511. while assigned(stackitem) do
  1512. begin
  1513. sym:=Tprocsym(stackitem^.symtable.FindWithHash(hashedid));
  1514. if sym<>nil then
  1515. begin
  1516. if sym.typ<>procsym then
  1517. internalerror(200402031);
  1518. { if the source type is an alias then this is only the second choice,
  1519. if you mess with this code, check tw4093 }
  1520. currpd:=sym.find_procdef_assignment_operator(from_def,to_def,curreq);
  1521. if curreq>besteq then
  1522. begin
  1523. besteq:=curreq;
  1524. bestpd:=currpd;
  1525. if (besteq=te_exact) then
  1526. break;
  1527. end;
  1528. end;
  1529. stackitem:=stackitem^.next;
  1530. end;
  1531. result:=bestpd;
  1532. end;
  1533. function search_system_type(const s: TIDString): ttypesym;
  1534. var
  1535. sym : tsym;
  1536. begin
  1537. sym:=tsym(systemunit.Find(s));
  1538. if not assigned(sym) or
  1539. (sym.typ<>typesym) then
  1540. cgmessage1(cg_f_unknown_system_type,s);
  1541. result:=ttypesym(sym);
  1542. end;
  1543. function search_class_member(pd : tobjectdef;const s : string):tsym;
  1544. { searches n in symtable of pd and all anchestors }
  1545. var
  1546. hashedid : THashedIDString;
  1547. srsym : tsym;
  1548. begin
  1549. hashedid.id:=s;
  1550. while assigned(pd) do
  1551. begin
  1552. srsym:=tsym(pd.symtable.FindWithHash(hashedid));
  1553. if assigned(srsym) then
  1554. begin
  1555. search_class_member:=srsym;
  1556. exit;
  1557. end;
  1558. pd:=pd.childof;
  1559. end;
  1560. search_class_member:=nil;
  1561. end;
  1562. function search_macro(const s : string):tsym;
  1563. var
  1564. stackitem : psymtablestackitem;
  1565. hashedid : THashedIDString;
  1566. srsym : tsym;
  1567. begin
  1568. hashedid.id:=s;
  1569. { First search the localmacrosymtable before searching the
  1570. global macrosymtables from the units }
  1571. if assigned(current_module) then
  1572. begin
  1573. srsym:=tsym(current_module.localmacrosymtable.FindWithHash(hashedid));
  1574. if assigned(srsym) then
  1575. begin
  1576. result:= srsym;
  1577. exit;
  1578. end;
  1579. end;
  1580. stackitem:=macrosymtablestack.stack;
  1581. while assigned(stackitem) do
  1582. begin
  1583. srsym:=tsym(stackitem^.symtable.FindWithHash(hashedid));
  1584. if assigned(srsym) then
  1585. begin
  1586. result:= srsym;
  1587. exit;
  1588. end;
  1589. stackitem:=stackitem^.next;
  1590. end;
  1591. result:= nil;
  1592. end;
  1593. {****************************************************************************
  1594. Object Helpers
  1595. ****************************************************************************}
  1596. procedure search_class_overloads(aprocsym : tprocsym);
  1597. { searches n in symtable of pd and all anchestors }
  1598. var
  1599. hashedid : THashedIDString;
  1600. srsym : tprocsym;
  1601. objdef : tobjectdef;
  1602. begin
  1603. if aprocsym.overloadchecked then
  1604. exit;
  1605. aprocsym.overloadchecked:=true;
  1606. if (aprocsym.owner.symtabletype<>ObjectSymtable) then
  1607. internalerror(200111021);
  1608. objdef:=tobjectdef(aprocsym.owner.defowner);
  1609. { we start in the parent }
  1610. if not assigned(objdef.childof) then
  1611. exit;
  1612. objdef:=objdef.childof;
  1613. hashedid.id:=aprocsym.name;
  1614. while assigned(objdef) do
  1615. begin
  1616. srsym:=tprocsym(objdef.symtable.FindWithHash(hashedid));
  1617. if assigned(srsym) then
  1618. begin
  1619. if (srsym.typ<>procsym) then
  1620. internalerror(200111022);
  1621. if srsym.is_visible_for_object(tobjectdef(aprocsym.owner.defowner),tobjectdef(aprocsym.owner.defowner)) then
  1622. begin
  1623. srsym.add_para_match_to(Aprocsym,[cpo_ignorehidden,cpo_allowdefaults]);
  1624. { we can stop if the overloads were already added
  1625. for the found symbol }
  1626. if srsym.overloadchecked then
  1627. break;
  1628. end;
  1629. end;
  1630. { next parent }
  1631. objdef:=objdef.childof;
  1632. end;
  1633. end;
  1634. procedure tstoredsymtable.testfordefaultproperty(sym:TObject;arg:pointer);
  1635. begin
  1636. if (tsym(sym).typ=propertysym) and
  1637. (ppo_defaultproperty in tpropertysym(sym).propoptions) then
  1638. ppointer(arg)^:=sym;
  1639. end;
  1640. function search_default_property(pd : tobjectdef) : tpropertysym;
  1641. { returns the default property of a class, searches also anchestors }
  1642. var
  1643. _defaultprop : tpropertysym;
  1644. begin
  1645. _defaultprop:=nil;
  1646. while assigned(pd) do
  1647. begin
  1648. pd.symtable.SymList.ForEachCall(@tstoredsymtable(pd.symtable).testfordefaultproperty,@_defaultprop);
  1649. if assigned(_defaultprop) then
  1650. break;
  1651. pd:=pd.childof;
  1652. end;
  1653. search_default_property:=_defaultprop;
  1654. end;
  1655. {****************************************************************************
  1656. Macro Helpers
  1657. ****************************************************************************}
  1658. procedure def_system_macro(const name : string);
  1659. var
  1660. mac : tmacro;
  1661. s: string;
  1662. begin
  1663. if name = '' then
  1664. internalerror(2004121202);
  1665. s:= upper(name);
  1666. mac:=tmacro(search_macro(s));
  1667. if not assigned(mac) then
  1668. begin
  1669. mac:=tmacro.create(s);
  1670. if assigned(current_module) then
  1671. current_module.localmacrosymtable.insert(mac)
  1672. else
  1673. initialmacrosymtable.insert(mac);
  1674. end;
  1675. if not mac.defined then
  1676. Message1(parser_c_macro_defined,mac.name);
  1677. mac.defined:=true;
  1678. end;
  1679. procedure set_system_macro(const name, value : string);
  1680. var
  1681. mac : tmacro;
  1682. s: string;
  1683. begin
  1684. if name = '' then
  1685. internalerror(2004121203);
  1686. s:= upper(name);
  1687. mac:=tmacro(search_macro(s));
  1688. if not assigned(mac) then
  1689. begin
  1690. mac:=tmacro.create(s);
  1691. if assigned(current_module) then
  1692. current_module.localmacrosymtable.insert(mac)
  1693. else
  1694. initialmacrosymtable.insert(mac);
  1695. end
  1696. else
  1697. begin
  1698. mac.is_compiler_var:=false;
  1699. if assigned(mac.buftext) then
  1700. freemem(mac.buftext,mac.buflen);
  1701. end;
  1702. Message2(parser_c_macro_set_to,mac.name,value);
  1703. mac.buflen:=length(value);
  1704. getmem(mac.buftext,mac.buflen);
  1705. move(value[1],mac.buftext^,mac.buflen);
  1706. mac.defined:=true;
  1707. end;
  1708. procedure set_system_compvar(const name, value : string);
  1709. var
  1710. mac : tmacro;
  1711. s: string;
  1712. begin
  1713. if name = '' then
  1714. internalerror(2004121204);
  1715. s:= upper(name);
  1716. mac:=tmacro(search_macro(s));
  1717. if not assigned(mac) then
  1718. begin
  1719. mac:=tmacro.create(s);
  1720. mac.is_compiler_var:=true;
  1721. if assigned(current_module) then
  1722. current_module.localmacrosymtable.insert(mac)
  1723. else
  1724. initialmacrosymtable.insert(mac);
  1725. end
  1726. else
  1727. begin
  1728. mac.is_compiler_var:=true;
  1729. if assigned(mac.buftext) then
  1730. freemem(mac.buftext,mac.buflen);
  1731. end;
  1732. Message2(parser_c_macro_set_to,mac.name,value);
  1733. mac.buflen:=length(value);
  1734. getmem(mac.buftext,mac.buflen);
  1735. move(value[1],mac.buftext^,mac.buflen);
  1736. mac.defined:=true;
  1737. end;
  1738. procedure undef_system_macro(const name : string);
  1739. var
  1740. mac : tmacro;
  1741. s: string;
  1742. begin
  1743. if name = '' then
  1744. internalerror(2004121205);
  1745. s:= upper(name);
  1746. mac:=tmacro(search_macro(s));
  1747. if not assigned(mac) then
  1748. {If not found, then it's already undefined.}
  1749. else
  1750. begin
  1751. if mac.defined then
  1752. Message1(parser_c_macro_undefined,mac.name);
  1753. mac.defined:=false;
  1754. mac.is_compiler_var:=false;
  1755. { delete old definition }
  1756. if assigned(mac.buftext) then
  1757. begin
  1758. freemem(mac.buftext,mac.buflen);
  1759. mac.buftext:=nil;
  1760. end;
  1761. end;
  1762. end;
  1763. {$ifdef UNITALIASES}
  1764. {****************************************************************************
  1765. TUNIT_ALIAS
  1766. ****************************************************************************}
  1767. constructor tunit_alias.create(const n:string);
  1768. var
  1769. i : longint;
  1770. begin
  1771. i:=pos('=',n);
  1772. if i=0 then
  1773. fail;
  1774. inherited createname(Copy(n,1,i-1));
  1775. newname:=stringdup(Copy(n,i+1,255));
  1776. end;
  1777. destructor tunit_alias.destroy;
  1778. begin
  1779. stringdispose(newname);
  1780. inherited destroy;
  1781. end;
  1782. procedure addunitalias(const n:string);
  1783. begin
  1784. unitaliases^.insert(tunit_alias,init(Upper(n))));
  1785. end;
  1786. function getunitalias(const n:string):string;
  1787. var
  1788. p : punit_alias;
  1789. begin
  1790. p:=punit_alias(unitaliases^.Find(Upper(n)));
  1791. if assigned(p) then
  1792. getunitalias:=punit_alias(p).newname^
  1793. else
  1794. getunitalias:=n;
  1795. end;
  1796. {$endif UNITALIASES}
  1797. {****************************************************************************
  1798. Init/Done Symtable
  1799. ****************************************************************************}
  1800. procedure IniTSymtable;
  1801. begin
  1802. { Reset symbolstack }
  1803. symtablestack:=nil;
  1804. systemunit:=nil;
  1805. { create error syms and def }
  1806. generrorsym:=terrorsym.create;
  1807. generrordef:=terrordef.create;
  1808. { macros }
  1809. initialmacrosymtable:=tmacrosymtable.create(false);
  1810. macrosymtablestack:=TSymtablestack.create;
  1811. macrosymtablestack.push(initialmacrosymtable);
  1812. {$ifdef UNITALIASES}
  1813. { unit aliases }
  1814. unitaliases:=TFPHashObjectList.create;
  1815. {$endif}
  1816. { set some global vars to nil, might be important for the ide }
  1817. class_tobject:=nil;
  1818. interface_iunknown:=nil;
  1819. rec_tguid:=nil;
  1820. dupnr:=0;
  1821. end;
  1822. procedure DoneSymtable;
  1823. begin
  1824. generrorsym.owner:=nil;
  1825. generrorsym.free;
  1826. generrordef.owner:=nil;
  1827. generrordef.free;
  1828. initialmacrosymtable.free;
  1829. macrosymtablestack.free;
  1830. {$ifdef UNITALIASES}
  1831. unitaliases.free;
  1832. {$endif}
  1833. end;
  1834. end.