symtable.pas 72 KB

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