symtable.pas 71 KB

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