symtable.pas 71 KB

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