symtable.pas 72 KB

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