symtable.pas 72 KB

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