symtable.pas 70 KB

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