symtable.pas 70 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086
  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. {*** Object Helpers ***}
  178. procedure search_class_overloads(aprocsym : tprocsym);
  179. function search_default_property(pd : tobjectdef) : tpropertysym;
  180. {*** Macro Helpers ***}
  181. {If called initially, the following procedures manipulate macros in }
  182. {initialmacrotable, otherwise they manipulate system macros local to a module.}
  183. {Name can be given in any case (it will be converted to upper case).}
  184. procedure def_system_macro(const name : string);
  185. procedure set_system_macro(const name, value : string);
  186. procedure set_system_compvar(const name, value : string);
  187. procedure undef_system_macro(const name : string);
  188. {*** symtable stack ***}
  189. { $ifdef DEBUG
  190. procedure test_symtablestack;
  191. procedure list_symtablestack;
  192. $endif DEBUG}
  193. {$ifdef UNITALIASES}
  194. type
  195. punit_alias = ^tunit_alias;
  196. tunit_alias = object(TNamedIndexItem)
  197. newname : pshortstring;
  198. constructor init(const n:string);
  199. destructor done;virtual;
  200. end;
  201. var
  202. unitaliases : pdictionary;
  203. procedure addunitalias(const n:string);
  204. function getunitalias(const n:string):string;
  205. {$endif UNITALIASES}
  206. {*** Init / Done ***}
  207. procedure IniTSymtable;
  208. procedure DoneSymtable;
  209. const
  210. overloaded_names : array [NOTOKEN..last_overloaded] of string[16] =
  211. ('error',
  212. 'plus','minus','star','slash','equal',
  213. 'greater','lower','greater_or_equal',
  214. 'lower_or_equal',
  215. 'sym_diff','starstar',
  216. 'as','is','in','or',
  217. 'and','div','mod','not','shl','shr','xor',
  218. 'assign');
  219. implementation
  220. uses
  221. { global }
  222. verbose,globals,
  223. { target }
  224. systems,
  225. { symtable }
  226. symutil,defcmp,defutil,
  227. { module }
  228. fmodule,
  229. { codegen }
  230. procinfo
  231. ;
  232. var
  233. dupnr : longint; { unique number for duplicate symbols }
  234. {*****************************************************************************
  235. TStoredSymtable
  236. *****************************************************************************}
  237. procedure tstoredsymtable.ppuload(ppufile:tcompilerppufile);
  238. begin
  239. { load definitions }
  240. loaddefs(ppufile);
  241. { load symbols }
  242. loadsyms(ppufile);
  243. end;
  244. procedure tstoredsymtable.ppuwrite(ppufile:tcompilerppufile);
  245. begin
  246. { write definitions }
  247. writedefs(ppufile);
  248. { write symbols }
  249. writesyms(ppufile);
  250. end;
  251. procedure tstoredsymtable.loaddefs(ppufile:tcompilerppufile);
  252. var
  253. def : tdef;
  254. b : byte;
  255. begin
  256. { load start of definition section, which holds the amount of defs }
  257. if ppufile.readentry<>ibstartdefs then
  258. Message(unit_f_ppu_read_error);
  259. { read definitions }
  260. repeat
  261. b:=ppufile.readentry;
  262. case b of
  263. ibpointerdef : def:=tpointerdef.ppuload(ppufile);
  264. ibarraydef : def:=tarraydef.ppuload(ppufile);
  265. iborddef : def:=torddef.ppuload(ppufile);
  266. ibfloatdef : def:=tfloatdef.ppuload(ppufile);
  267. ibprocdef : def:=tprocdef.ppuload(ppufile);
  268. ibshortstringdef : def:=tstringdef.loadshort(ppufile);
  269. iblongstringdef : def:=tstringdef.loadlong(ppufile);
  270. ibansistringdef : def:=tstringdef.loadansi(ppufile);
  271. ibwidestringdef : def:=tstringdef.loadwide(ppufile);
  272. ibrecorddef : def:=trecorddef.ppuload(ppufile);
  273. ibobjectdef : def:=tobjectdef.ppuload(ppufile);
  274. ibenumdef : def:=tenumdef.ppuload(ppufile);
  275. ibsetdef : def:=tsetdef.ppuload(ppufile);
  276. ibprocvardef : def:=tprocvardef.ppuload(ppufile);
  277. ibfiledef : def:=tfiledef.ppuload(ppufile);
  278. ibclassrefdef : def:=tclassrefdef.ppuload(ppufile);
  279. ibformaldef : def:=tformaldef.ppuload(ppufile);
  280. ibvariantdef : def:=tvariantdef.ppuload(ppufile);
  281. ibundefineddef : def:=tundefineddef.ppuload(ppufile);
  282. ibenddefs : break;
  283. ibend : Message(unit_f_ppu_read_error);
  284. else
  285. Message1(unit_f_ppu_invalid_entry,tostr(b));
  286. end;
  287. InsertDef(def);
  288. until false;
  289. end;
  290. procedure tstoredsymtable.loadsyms(ppufile:tcompilerppufile);
  291. var
  292. b : byte;
  293. sym : tsym;
  294. begin
  295. { load start of definition section, which holds the amount of defs }
  296. if ppufile.readentry<>ibstartsyms then
  297. Message(unit_f_ppu_read_error);
  298. { now read the symbols }
  299. repeat
  300. b:=ppufile.readentry;
  301. case b of
  302. ibtypesym : sym:=ttypesym.ppuload(ppufile);
  303. ibprocsym : sym:=tprocsym.ppuload(ppufile);
  304. ibconstsym : sym:=tconstsym.ppuload(ppufile);
  305. ibglobalvarsym : sym:=tglobalvarsym.ppuload(ppufile);
  306. iblocalvarsym : sym:=tlocalvarsym.ppuload(ppufile);
  307. ibparavarsym : sym:=tparavarsym.ppuload(ppufile);
  308. ibfieldvarsym : sym:=tfieldvarsym.ppuload(ppufile);
  309. ibabsolutevarsym : sym:=tabsolutevarsym.ppuload(ppufile);
  310. ibenumsym : sym:=tenumsym.ppuload(ppufile);
  311. ibtypedconstsym : sym:=ttypedconstsym.ppuload(ppufile);
  312. ibpropertysym : sym:=tpropertysym.ppuload(ppufile);
  313. ibunitsym : sym:=tunitsym.ppuload(ppufile);
  314. iblabelsym : sym:=tlabelsym.ppuload(ppufile);
  315. ibsyssym : sym:=tsyssym.ppuload(ppufile);
  316. ibmacrosym : sym:=tmacro.ppuload(ppufile);
  317. ibendsyms : break;
  318. ibend : Message(unit_f_ppu_read_error);
  319. else
  320. Message1(unit_f_ppu_invalid_entry,tostr(b));
  321. end;
  322. Insert(sym,false);
  323. until false;
  324. end;
  325. procedure tstoredsymtable.writedefs(ppufile:tcompilerppufile);
  326. var
  327. i : longint;
  328. def : tstoreddef;
  329. begin
  330. { each definition get a number, write then the amount of defs to the
  331. ibstartdef entry }
  332. ppufile.putlongint(DefList.count);
  333. ppufile.writeentry(ibstartdefs);
  334. { now write the definition }
  335. for i:=0 to DefList.Count-1 do
  336. begin
  337. def:=tstoreddef(DefList[i]);
  338. 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),hashedid.id);
  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. function tabstractlocalsymtable.count_locals:longint;
  951. var
  952. i : longint;
  953. sym : tsym;
  954. begin
  955. result:=0;
  956. for i:=0 to SymList.Count-1 do
  957. begin
  958. sym:=tsym(SymList[i]);
  959. { Count only varsyms, but ignore the funcretsym }
  960. if (tsym(sym).typ in [localvarsym,paravarsym]) and
  961. (tsym(sym)<>current_procinfo.procdef.funcretsym) and
  962. (not(vo_is_parentfp in tabstractvarsym(sym).varoptions) or
  963. (tstoredsym(sym).refs>0)) then
  964. inc(result);
  965. end;
  966. end;
  967. {****************************************************************************
  968. TLocalSymtable
  969. ****************************************************************************}
  970. constructor tlocalsymtable.create(adefowner:tdef;level:byte);
  971. begin
  972. inherited create('');
  973. defowner:=adefowner;
  974. symtabletype:=localsymtable;
  975. symtablelevel:=level;
  976. end;
  977. function tlocalsymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  978. var
  979. hsym : tsym;
  980. begin
  981. if not assigned(defowner) or
  982. (defowner.typ<>procdef) then
  983. internalerror(200602042);
  984. result:=false;
  985. hsym:=tsym(FindWithHash(hashedid));
  986. if assigned(hsym) then
  987. begin
  988. { a local and the function can have the same
  989. name in TP and Delphi, but RESULT not }
  990. if (m_duplicate_names in current_settings.modeswitches) and
  991. (hsym.typ in [absolutevarsym,localvarsym]) and
  992. (vo_is_funcret in tabstractvarsym(hsym).varoptions) and
  993. not((m_result in current_settings.modeswitches) and
  994. (vo_is_result in tabstractvarsym(hsym).varoptions)) then
  995. HideSym(hsym)
  996. else
  997. DuplicateSym(hashedid,sym,hsym);
  998. result:=true;
  999. exit;
  1000. end;
  1001. { check also parasymtable, this needs to be done here becuase
  1002. of the special situation with the funcret sym that needs to be
  1003. hidden for tp and delphi modes }
  1004. hsym:=tsym(tabstractprocdef(defowner).parast.FindWithHash(hashedid));
  1005. if assigned(hsym) then
  1006. begin
  1007. { a local and the function can have the same
  1008. name in TP and Delphi, but RESULT not }
  1009. if (m_duplicate_names in current_settings.modeswitches) and
  1010. (sym.typ in [absolutevarsym,localvarsym]) and
  1011. (vo_is_funcret in tabstractvarsym(sym).varoptions) and
  1012. not((m_result in current_settings.modeswitches) and
  1013. (vo_is_result in tabstractvarsym(sym).varoptions)) then
  1014. Hidesym(sym)
  1015. else
  1016. DuplicateSym(hashedid,sym,hsym);
  1017. result:=true;
  1018. exit;
  1019. end;
  1020. { check ObjectSymtable, skip this for funcret sym because
  1021. that will always be positive because it has the same name
  1022. as the procsym }
  1023. if not(m_duplicate_names in current_settings.modeswitches) and
  1024. not is_funcret_sym(sym) and
  1025. (defowner.typ=procdef) and
  1026. assigned(tprocdef(defowner)._class) and
  1027. (tprocdef(defowner).owner.defowner=tprocdef(defowner)._class) then
  1028. result:=tprocdef(defowner)._class.symtable.checkduplicate(hashedid,sym);
  1029. end;
  1030. {****************************************************************************
  1031. TParaSymtable
  1032. ****************************************************************************}
  1033. constructor tparasymtable.create(adefowner:tdef;level:byte);
  1034. begin
  1035. inherited create('');
  1036. defowner:=adefowner;
  1037. symtabletype:=parasymtable;
  1038. symtablelevel:=level;
  1039. end;
  1040. function tparasymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  1041. begin
  1042. result:=inherited checkduplicate(hashedid,sym);
  1043. if result then
  1044. exit;
  1045. if not(m_duplicate_names in current_settings.modeswitches) and
  1046. (defowner.typ=procdef) and
  1047. assigned(tprocdef(defowner)._class) and
  1048. (tprocdef(defowner).owner.defowner=tprocdef(defowner)._class) then
  1049. result:=tprocdef(defowner)._class.symtable.checkduplicate(hashedid,sym);
  1050. end;
  1051. {****************************************************************************
  1052. TAbstractUniTSymtable
  1053. ****************************************************************************}
  1054. constructor tabstractuniTSymtable.create(const n : string;id:word);
  1055. begin
  1056. inherited create(n);
  1057. moduleid:=id;
  1058. end;
  1059. function tabstractuniTSymtable.iscurrentunit:boolean;
  1060. begin
  1061. result:=assigned(current_module) and
  1062. (
  1063. (current_module.globalsymtable=self) or
  1064. (current_module.localsymtable=self)
  1065. );
  1066. end;
  1067. {****************************************************************************
  1068. TStaticSymtable
  1069. ****************************************************************************}
  1070. constructor tstaticsymtable.create(const n : string;id:word);
  1071. begin
  1072. inherited create(n,id);
  1073. symtabletype:=staticsymtable;
  1074. symtablelevel:=main_program_level;
  1075. end;
  1076. procedure tstaticsymtable.ppuload(ppufile:tcompilerppufile);
  1077. begin
  1078. inherited ppuload(ppufile);
  1079. { now we can deref the syms and defs }
  1080. deref;
  1081. end;
  1082. procedure tstaticsymtable.ppuwrite(ppufile:tcompilerppufile);
  1083. begin
  1084. inherited ppuwrite(ppufile);
  1085. end;
  1086. function tstaticsymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  1087. var
  1088. hsym : tsym;
  1089. begin
  1090. result:=false;
  1091. hsym:=tsym(FindWithHash(hashedid));
  1092. if assigned(hsym) then
  1093. begin
  1094. { Delphi you can have a symbol with the same name as the
  1095. unit, the unit can then not be accessed anymore using
  1096. <unit>.<id>, so we can hide the symbol }
  1097. if (m_duplicate_names in current_settings.modeswitches) and
  1098. (hsym.typ=symconst.unitsym) then
  1099. HideSym(hsym)
  1100. else
  1101. DuplicateSym(hashedid,sym,hsym);
  1102. result:=true;
  1103. exit;
  1104. end;
  1105. if (current_module.localsymtable=self) and
  1106. assigned(current_module.globalsymtable) then
  1107. result:=tglobalsymtable(current_module.globalsymtable).checkduplicate(hashedid,sym);
  1108. end;
  1109. {****************************************************************************
  1110. TGlobalSymtable
  1111. ****************************************************************************}
  1112. constructor tglobalsymtable.create(const n : string;id:word);
  1113. begin
  1114. inherited create(n,id);
  1115. symtabletype:=globalsymtable;
  1116. symtablelevel:=main_program_level;
  1117. end;
  1118. procedure tglobalsymtable.ppuload(ppufile:tcompilerppufile);
  1119. begin
  1120. inherited ppuload(ppufile);
  1121. { now we can deref the syms and defs }
  1122. deref;
  1123. end;
  1124. procedure tglobalsymtable.ppuwrite(ppufile:tcompilerppufile);
  1125. begin
  1126. { write the symtable entries }
  1127. inherited ppuwrite(ppufile);
  1128. end;
  1129. function tglobalsymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  1130. var
  1131. hsym : tsym;
  1132. begin
  1133. result:=false;
  1134. hsym:=tsym(FindWithHash(hashedid));
  1135. if assigned(hsym) then
  1136. begin
  1137. { Delphi you can have a symbol with the same name as the
  1138. unit, the unit can then not be accessed anymore using
  1139. <unit>.<id>, so we can hide the symbol }
  1140. if (m_duplicate_names in current_settings.modeswitches) and
  1141. (hsym.typ=symconst.unitsym) then
  1142. HideSym(hsym)
  1143. else
  1144. DuplicateSym(hashedid,sym,hsym);
  1145. result:=true;
  1146. exit;
  1147. end;
  1148. end;
  1149. {****************************************************************************
  1150. TWITHSYMTABLE
  1151. ****************************************************************************}
  1152. constructor twithsymtable.create(aowner:tdef;ASymList:TFPHashObjectList;refnode:tobject{tnode});
  1153. begin
  1154. inherited create('');
  1155. symtabletype:=withsymtable;
  1156. withrefnode:=refnode;
  1157. { Replace SymList with the passed symlist }
  1158. SymList.free;
  1159. SymList:=ASymList;
  1160. defowner:=aowner;
  1161. end;
  1162. destructor twithsymtable.destroy;
  1163. begin
  1164. withrefnode.free;
  1165. { Disable SymList because we don't Own it }
  1166. SymList:=nil;
  1167. inherited destroy;
  1168. end;
  1169. procedure twithsymtable.clear;
  1170. begin
  1171. { remove no entry from a withsymtable as it is only a pointer to the
  1172. recorddef or objectdef symtable }
  1173. end;
  1174. procedure twithsymtable.insertdef(def:TDefEntry);
  1175. begin
  1176. { Definitions can't be registered in the withsymtable
  1177. because the withsymtable is removed after the with block.
  1178. We can't easily solve it here because the next symtable in the
  1179. stack is not known. }
  1180. internalerror(200602046);
  1181. end;
  1182. {****************************************************************************
  1183. TSTT_ExceptionSymtable
  1184. ****************************************************************************}
  1185. constructor tstt_excepTSymtable.create;
  1186. begin
  1187. inherited create('');
  1188. symtabletype:=stt_excepTSymtable;
  1189. end;
  1190. {****************************************************************************
  1191. TMacroSymtable
  1192. ****************************************************************************}
  1193. constructor tmacrosymtable.create(exported: boolean);
  1194. begin
  1195. inherited create('');
  1196. if exported then
  1197. symtabletype:=exportedmacrosymtable
  1198. else
  1199. symtabletype:=localmacrosymtable;
  1200. symtablelevel:=main_program_level;
  1201. end;
  1202. {*****************************************************************************
  1203. Helper Routines
  1204. *****************************************************************************}
  1205. function FullTypeName(def,otherdef:tdef):string;
  1206. var
  1207. s1,s2 : string;
  1208. begin
  1209. s1:=def.typename;
  1210. { When the names are the same try to include the unit name }
  1211. if assigned(otherdef) and
  1212. (def.owner.symtabletype in [globalsymtable,staticsymtable]) then
  1213. begin
  1214. s2:=otherdef.typename;
  1215. if upper(s1)=upper(s2) then
  1216. s1:=def.owner.realname^+'.'+s1;
  1217. end;
  1218. FullTypeName:=s1;
  1219. end;
  1220. procedure incompatibletypes(def1,def2:tdef);
  1221. begin
  1222. { When there is an errordef there is already an error message show }
  1223. if (def2.typ=errordef) or
  1224. (def1.typ=errordef) then
  1225. exit;
  1226. CGMessage2(type_e_incompatible_types,FullTypeName(def1,def2),FullTypeName(def2,def1));
  1227. end;
  1228. procedure hidesym(sym:TSymEntry);
  1229. begin
  1230. sym.realname:='$hidden'+sym.realname;
  1231. include(tsym(sym).symoptions,sp_hidden);
  1232. end;
  1233. procedure duplicatesym(var hashedid:THashedIDString;dupsym,origsym:TSymEntry);
  1234. var
  1235. st : TSymtable;
  1236. begin
  1237. Message1(sym_e_duplicate_id,tsym(origsym).realname);
  1238. { Write hint where the original symbol was found }
  1239. st:=finduniTSymtable(origsym.owner);
  1240. with tsym(origsym).fileinfo do
  1241. begin
  1242. if assigned(st) and
  1243. (st.symtabletype=globalsymtable) and
  1244. st.iscurrentunit then
  1245. Message2(sym_h_duplicate_id_where,current_module.sourcefiles.get_file_name(fileindex),tostr(line))
  1246. else if assigned(st.name) then
  1247. Message2(sym_h_duplicate_id_where,'unit '+st.name^,tostr(line));
  1248. end;
  1249. { Rename duplicate sym to an unreachable name, but it can be
  1250. inserted in the symtable without errors }
  1251. inc(dupnr);
  1252. hashedid.id:='dup'+tostr(dupnr)+hashedid.id;
  1253. if assigned(dupsym) then
  1254. include(tsym(dupsym).symoptions,sp_implicitrename);
  1255. end;
  1256. {*****************************************************************************
  1257. Search
  1258. *****************************************************************************}
  1259. procedure addsymref(sym:tsym);
  1260. begin
  1261. { symbol uses count }
  1262. inc(sym.refs);
  1263. { unit uses count }
  1264. if assigned(current_module) and
  1265. (sym.owner.symtabletype=globalsymtable) then
  1266. begin
  1267. if tglobalsymtable(sym.owner).moduleid>=current_module.unitmapsize then
  1268. internalerror(200501152);
  1269. inc(current_module.unitmap[tglobalsymtable(sym.owner).moduleid].refs);
  1270. end;
  1271. end;
  1272. function searchsym(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  1273. var
  1274. hashedid : THashedIDString;
  1275. topclass : tobjectdef;
  1276. context : tobjectdef;
  1277. stackitem : psymtablestackitem;
  1278. begin
  1279. result:=false;
  1280. hashedid.id:=s;
  1281. stackitem:=symtablestack.stack;
  1282. while assigned(stackitem) do
  1283. begin
  1284. srsymtable:=stackitem^.symtable;
  1285. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  1286. if assigned(srsym) then
  1287. begin
  1288. topclass:=nil;
  1289. { use the class from withsymtable only when it is
  1290. defined in this unit }
  1291. if (srsymtable.symtabletype=withsymtable) and
  1292. assigned(srsymtable.defowner) and
  1293. (srsymtable.defowner.typ=objectdef) and
  1294. (srsymtable.defowner.owner.symtabletype in [globalsymtable,staticsymtable]) and
  1295. (srsymtable.defowner.owner.iscurrentunit) then
  1296. topclass:=tobjectdef(srsymtable.defowner)
  1297. else
  1298. begin
  1299. if assigned(current_procinfo) then
  1300. topclass:=current_procinfo.procdef._class;
  1301. end;
  1302. if assigned(current_procinfo) then
  1303. context:=current_procinfo.procdef._class
  1304. else
  1305. context:=nil;
  1306. if tsym(srsym).is_visible_for_object(topclass,context) then
  1307. begin
  1308. { we need to know if a procedure references symbols
  1309. in the static symtable, because then it can't be
  1310. inlined from outside this unit }
  1311. if assigned(current_procinfo) and
  1312. (srsym.owner.symtabletype=staticsymtable) then
  1313. include(current_procinfo.flags,pi_uses_static_symtable);
  1314. addsymref(srsym);
  1315. result:=true;
  1316. exit;
  1317. end;
  1318. end;
  1319. stackitem:=stackitem^.next;
  1320. end;
  1321. srsym:=nil;
  1322. srsymtable:=nil;
  1323. end;
  1324. function searchsym_type(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  1325. var
  1326. hashedid : THashedIDString;
  1327. stackitem : psymtablestackitem;
  1328. begin
  1329. result:=false;
  1330. hashedid.id:=s;
  1331. stackitem:=symtablestack.stack;
  1332. while assigned(stackitem) do
  1333. begin
  1334. {
  1335. It is not possible to have type symbols in:
  1336. records
  1337. objects
  1338. parameters
  1339. Exception are generic definitions and specializations
  1340. that have the parameterized types inserted in the symtable.
  1341. }
  1342. srsymtable:=stackitem^.symtable;
  1343. if not(srsymtable.symtabletype in [recordsymtable,ObjectSymtable,parasymtable]) or
  1344. (assigned(srsymtable.defowner) and
  1345. (
  1346. (df_generic in tdef(srsymtable.defowner).defoptions) or
  1347. (df_specialization in tdef(srsymtable.defowner).defoptions))
  1348. ) then
  1349. begin
  1350. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  1351. if assigned(srsym) and
  1352. not(srsym.typ in [fieldvarsym,paravarsym]) and
  1353. (not assigned(current_procinfo) or
  1354. tsym(srsym).is_visible_for_object(current_procinfo.procdef._class,current_procinfo.procdef._class)) then
  1355. begin
  1356. { we need to know if a procedure references symbols
  1357. in the static symtable, because then it can't be
  1358. inlined from outside this unit }
  1359. if assigned(current_procinfo) and
  1360. (srsym.owner.symtabletype=staticsymtable) then
  1361. include(current_procinfo.flags,pi_uses_static_symtable);
  1362. addsymref(srsym);
  1363. result:=true;
  1364. exit;
  1365. end;
  1366. end;
  1367. stackitem:=stackitem^.next;
  1368. end;
  1369. result:=false;
  1370. srsym:=nil;
  1371. srsymtable:=nil;
  1372. end;
  1373. function searchsym_in_module(pm:pointer;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  1374. var
  1375. pmod : tmodule;
  1376. begin
  1377. pmod:=tmodule(pm);
  1378. result:=false;
  1379. if assigned(pmod.globalsymtable) then
  1380. begin
  1381. srsym:=tsym(pmod.globalsymtable.Find(s));
  1382. if assigned(srsym) then
  1383. begin
  1384. srsymtable:=pmod.globalsymtable;
  1385. addsymref(srsym);
  1386. result:=true;
  1387. exit;
  1388. end;
  1389. end;
  1390. { If the module is the current unit we also need
  1391. to search the local symtable }
  1392. if (pmod=current_module) and
  1393. assigned(pmod.localsymtable) then
  1394. begin
  1395. srsym:=tsym(pmod.localsymtable.Find(s));
  1396. if assigned(srsym) then
  1397. begin
  1398. srsymtable:=pmod.localsymtable;
  1399. addsymref(srsym);
  1400. result:=true;
  1401. exit;
  1402. end;
  1403. end;
  1404. srsym:=nil;
  1405. srsymtable:=nil;
  1406. end;
  1407. function searchsym_in_class(classh,contextclassh:tobjectdef;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  1408. var
  1409. hashedid : THashedIDString;
  1410. currentclassh : tobjectdef;
  1411. begin
  1412. result:=false;
  1413. hashedid.id:=s;
  1414. if assigned(current_procinfo.procdef) then
  1415. currentclassh:=current_procinfo.procdef._class
  1416. else
  1417. currentclassh:=nil;
  1418. while assigned(classh) do
  1419. begin
  1420. srsymtable:=classh.symtable;
  1421. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  1422. if assigned(srsym) and
  1423. tsym(srsym).is_visible_for_object(contextclassh,currentclassh) then
  1424. begin
  1425. result:=true;
  1426. exit;
  1427. end;
  1428. classh:=classh.childof;
  1429. end;
  1430. srsym:=nil;
  1431. srsymtable:=nil;
  1432. end;
  1433. function searchsym_in_class_by_msgint(classh:tobjectdef;msgid:longint;out srdef : tdef;out srsym:tsym;out srsymtable:TSymtable):boolean;
  1434. var
  1435. def : tdef;
  1436. i : longint;
  1437. begin
  1438. result:=false;
  1439. def:=nil;
  1440. while assigned(classh) do
  1441. begin
  1442. for i:=0 to classh.symtable.DefList.Count-1 do
  1443. begin
  1444. def:=tstoreddef(classh.symtable.DefList[i]);
  1445. { Find also all hidden private methods to
  1446. be compatible with delphi, see tw6203 (PFV) }
  1447. if (def.typ=procdef) and
  1448. (po_msgint in tprocdef(def).procoptions) and
  1449. (tprocdef(def).messageinf.i=msgid) then
  1450. begin
  1451. srdef:=def;
  1452. srsym:=tprocdef(def).procsym;
  1453. srsymtable:=classh.symtable;
  1454. result:=true;
  1455. exit;
  1456. end;
  1457. end;
  1458. classh:=classh.childof;
  1459. end;
  1460. srsym:=nil;
  1461. srsymtable:=nil;
  1462. end;
  1463. function searchsym_in_class_by_msgstr(classh:tobjectdef;const s:string;out srsym:tsym;out srsymtable:TSymtable):boolean;
  1464. var
  1465. def : tdef;
  1466. i : longint;
  1467. begin
  1468. result:=false;
  1469. def:=nil;
  1470. while assigned(classh) do
  1471. begin
  1472. for i:=0 to classh.symtable.DefList.Count-1 do
  1473. begin
  1474. def:=tstoreddef(classh.symtable.DefList[i]);
  1475. { Find also all hidden private methods to
  1476. be compatible with delphi, see tw6203 (PFV) }
  1477. if (def.typ=procdef) and
  1478. (po_msgstr in tprocdef(def).procoptions) and
  1479. (tprocdef(def).messageinf.str^=s) then
  1480. begin
  1481. srsym:=tprocdef(def).procsym;
  1482. srsymtable:=classh.symtable;
  1483. result:=true;
  1484. exit;
  1485. end;
  1486. end;
  1487. classh:=classh.childof;
  1488. end;
  1489. srsym:=nil;
  1490. srsymtable:=nil;
  1491. end;
  1492. function search_assignment_operator(from_def,to_def:Tdef):Tprocdef;
  1493. var
  1494. sym : Tprocsym;
  1495. hashedid : THashedIDString;
  1496. curreq,
  1497. besteq : tequaltype;
  1498. currpd,
  1499. bestpd : tprocdef;
  1500. stackitem : psymtablestackitem;
  1501. begin
  1502. hashedid.id:='assign';
  1503. besteq:=te_incompatible;
  1504. bestpd:=nil;
  1505. stackitem:=symtablestack.stack;
  1506. while assigned(stackitem) do
  1507. begin
  1508. sym:=Tprocsym(stackitem^.symtable.FindWithHash(hashedid));
  1509. if sym<>nil then
  1510. begin
  1511. if sym.typ<>procsym then
  1512. internalerror(200402031);
  1513. { if the source type is an alias then this is only the second choice,
  1514. if you mess with this code, check tw4093 }
  1515. currpd:=sym.find_procdef_assignment_operator(from_def,to_def,curreq);
  1516. if curreq>besteq then
  1517. begin
  1518. besteq:=curreq;
  1519. bestpd:=currpd;
  1520. if (besteq=te_exact) then
  1521. break;
  1522. end;
  1523. end;
  1524. stackitem:=stackitem^.next;
  1525. end;
  1526. result:=bestpd;
  1527. end;
  1528. function search_system_type(const s: TIDString): ttypesym;
  1529. var
  1530. sym : tsym;
  1531. begin
  1532. sym:=tsym(systemunit.Find(s));
  1533. if not assigned(sym) or
  1534. (sym.typ<>typesym) then
  1535. internalerror(200501251);
  1536. result:=ttypesym(sym);
  1537. end;
  1538. function search_class_member(pd : tobjectdef;const s : string):tsym;
  1539. { searches n in symtable of pd and all anchestors }
  1540. var
  1541. hashedid : THashedIDString;
  1542. srsym : tsym;
  1543. begin
  1544. hashedid.id:=s;
  1545. while assigned(pd) do
  1546. begin
  1547. srsym:=tsym(pd.symtable.FindWithHash(hashedid));
  1548. if assigned(srsym) then
  1549. begin
  1550. search_class_member:=srsym;
  1551. exit;
  1552. end;
  1553. pd:=pd.childof;
  1554. end;
  1555. search_class_member:=nil;
  1556. end;
  1557. function search_macro(const s : string):tsym;
  1558. var
  1559. stackitem : psymtablestackitem;
  1560. hashedid : THashedIDString;
  1561. srsym : tsym;
  1562. begin
  1563. hashedid.id:=s;
  1564. { First search the localmacrosymtable before searching the
  1565. global macrosymtables from the units }
  1566. if assigned(current_module) then
  1567. begin
  1568. srsym:=tsym(current_module.localmacrosymtable.FindWithHash(hashedid));
  1569. if assigned(srsym) then
  1570. begin
  1571. result:= srsym;
  1572. exit;
  1573. end;
  1574. end;
  1575. stackitem:=macrosymtablestack.stack;
  1576. while assigned(stackitem) do
  1577. begin
  1578. srsym:=tsym(stackitem^.symtable.FindWithHash(hashedid));
  1579. if assigned(srsym) then
  1580. begin
  1581. result:= srsym;
  1582. exit;
  1583. end;
  1584. stackitem:=stackitem^.next;
  1585. end;
  1586. result:= nil;
  1587. end;
  1588. {****************************************************************************
  1589. Object Helpers
  1590. ****************************************************************************}
  1591. procedure search_class_overloads(aprocsym : tprocsym);
  1592. { searches n in symtable of pd and all anchestors }
  1593. var
  1594. hashedid : THashedIDString;
  1595. srsym : tprocsym;
  1596. objdef : tobjectdef;
  1597. begin
  1598. if aprocsym.overloadchecked then
  1599. exit;
  1600. aprocsym.overloadchecked:=true;
  1601. if (aprocsym.owner.symtabletype<>ObjectSymtable) then
  1602. internalerror(200111021);
  1603. objdef:=tobjectdef(aprocsym.owner.defowner);
  1604. { we start in the parent }
  1605. if not assigned(objdef.childof) then
  1606. exit;
  1607. objdef:=objdef.childof;
  1608. hashedid.id:=aprocsym.name;
  1609. while assigned(objdef) do
  1610. begin
  1611. srsym:=tprocsym(objdef.symtable.FindWithHash(hashedid));
  1612. if assigned(srsym) then
  1613. begin
  1614. if (srsym.typ<>procsym) then
  1615. internalerror(200111022);
  1616. if srsym.is_visible_for_object(tobjectdef(aprocsym.owner.defowner),tobjectdef(aprocsym.owner.defowner)) then
  1617. begin
  1618. srsym.add_para_match_to(Aprocsym,[cpo_ignorehidden,cpo_allowdefaults]);
  1619. { we can stop if the overloads were already added
  1620. for the found symbol }
  1621. if srsym.overloadchecked then
  1622. break;
  1623. end;
  1624. end;
  1625. { next parent }
  1626. objdef:=objdef.childof;
  1627. end;
  1628. end;
  1629. procedure tstoredsymtable.testfordefaultproperty(sym:TObject;arg:pointer);
  1630. begin
  1631. if (tsym(sym).typ=propertysym) and
  1632. (ppo_defaultproperty in tpropertysym(sym).propoptions) then
  1633. ppointer(arg)^:=sym;
  1634. end;
  1635. function search_default_property(pd : tobjectdef) : tpropertysym;
  1636. { returns the default property of a class, searches also anchestors }
  1637. var
  1638. _defaultprop : tpropertysym;
  1639. begin
  1640. _defaultprop:=nil;
  1641. while assigned(pd) do
  1642. begin
  1643. pd.symtable.SymList.ForEachCall(@tstoredsymtable(pd.symtable).testfordefaultproperty,@_defaultprop);
  1644. if assigned(_defaultprop) then
  1645. break;
  1646. pd:=pd.childof;
  1647. end;
  1648. search_default_property:=_defaultprop;
  1649. end;
  1650. {****************************************************************************
  1651. Macro Helpers
  1652. ****************************************************************************}
  1653. procedure def_system_macro(const name : string);
  1654. var
  1655. mac : tmacro;
  1656. s: string;
  1657. begin
  1658. if name = '' then
  1659. internalerror(2004121201);
  1660. s:= upper(name);
  1661. mac:=tmacro(search_macro(s));
  1662. if not assigned(mac) then
  1663. begin
  1664. mac:=tmacro.create(s);
  1665. if assigned(current_module) then
  1666. current_module.localmacrosymtable.insert(mac)
  1667. else
  1668. initialmacrosymtable.insert(mac);
  1669. end;
  1670. if not mac.defined then
  1671. Message1(parser_c_macro_defined,mac.name);
  1672. mac.defined:=true;
  1673. end;
  1674. procedure set_system_macro(const name, value : string);
  1675. var
  1676. mac : tmacro;
  1677. s: string;
  1678. begin
  1679. if name = '' then
  1680. internalerror(2004121201);
  1681. s:= upper(name);
  1682. mac:=tmacro(search_macro(s));
  1683. if not assigned(mac) then
  1684. begin
  1685. mac:=tmacro.create(s);
  1686. if assigned(current_module) then
  1687. current_module.localmacrosymtable.insert(mac)
  1688. else
  1689. initialmacrosymtable.insert(mac);
  1690. end
  1691. else
  1692. begin
  1693. mac.is_compiler_var:=false;
  1694. if assigned(mac.buftext) then
  1695. freemem(mac.buftext,mac.buflen);
  1696. end;
  1697. Message2(parser_c_macro_set_to,mac.name,value);
  1698. mac.buflen:=length(value);
  1699. getmem(mac.buftext,mac.buflen);
  1700. move(value[1],mac.buftext^,mac.buflen);
  1701. mac.defined:=true;
  1702. end;
  1703. procedure set_system_compvar(const name, value : string);
  1704. var
  1705. mac : tmacro;
  1706. s: string;
  1707. begin
  1708. if name = '' then
  1709. internalerror(2004121201);
  1710. s:= upper(name);
  1711. mac:=tmacro(search_macro(s));
  1712. if not assigned(mac) then
  1713. begin
  1714. mac:=tmacro.create(s);
  1715. mac.is_compiler_var:=true;
  1716. if assigned(current_module) then
  1717. current_module.localmacrosymtable.insert(mac)
  1718. else
  1719. initialmacrosymtable.insert(mac);
  1720. end
  1721. else
  1722. begin
  1723. mac.is_compiler_var:=true;
  1724. if assigned(mac.buftext) then
  1725. freemem(mac.buftext,mac.buflen);
  1726. end;
  1727. Message2(parser_c_macro_set_to,mac.name,value);
  1728. mac.buflen:=length(value);
  1729. getmem(mac.buftext,mac.buflen);
  1730. move(value[1],mac.buftext^,mac.buflen);
  1731. mac.defined:=true;
  1732. end;
  1733. procedure undef_system_macro(const name : string);
  1734. var
  1735. mac : tmacro;
  1736. s: string;
  1737. begin
  1738. if name = '' then
  1739. internalerror(2004121201);
  1740. s:= upper(name);
  1741. mac:=tmacro(search_macro(s));
  1742. if not assigned(mac) then
  1743. {If not found, then it's already undefined.}
  1744. else
  1745. begin
  1746. if mac.defined then
  1747. Message1(parser_c_macro_undefined,mac.name);
  1748. mac.defined:=false;
  1749. mac.is_compiler_var:=false;
  1750. { delete old definition }
  1751. if assigned(mac.buftext) then
  1752. begin
  1753. freemem(mac.buftext,mac.buflen);
  1754. mac.buftext:=nil;
  1755. end;
  1756. end;
  1757. end;
  1758. {$ifdef UNITALIASES}
  1759. {****************************************************************************
  1760. TUNIT_ALIAS
  1761. ****************************************************************************}
  1762. constructor tunit_alias.create(const n:string);
  1763. var
  1764. i : longint;
  1765. begin
  1766. i:=pos('=',n);
  1767. if i=0 then
  1768. fail;
  1769. inherited createname(Copy(n,1,i-1));
  1770. newname:=stringdup(Copy(n,i+1,255));
  1771. end;
  1772. destructor tunit_alias.destroy;
  1773. begin
  1774. stringdispose(newname);
  1775. inherited destroy;
  1776. end;
  1777. procedure addunitalias(const n:string);
  1778. begin
  1779. unitaliases^.insert(tunit_alias,init(Upper(n))));
  1780. end;
  1781. function getunitalias(const n:string):string;
  1782. var
  1783. p : punit_alias;
  1784. begin
  1785. p:=punit_alias(unitaliases^.Find(Upper(n)));
  1786. if assigned(p) then
  1787. getunitalias:=punit_alias(p).newname^
  1788. else
  1789. getunitalias:=n;
  1790. end;
  1791. {$endif UNITALIASES}
  1792. {****************************************************************************
  1793. Init/Done Symtable
  1794. ****************************************************************************}
  1795. procedure IniTSymtable;
  1796. begin
  1797. { Reset symbolstack }
  1798. symtablestack:=nil;
  1799. systemunit:=nil;
  1800. { create error syms and def }
  1801. generrorsym:=terrorsym.create;
  1802. generrordef:=terrordef.create;
  1803. { macros }
  1804. initialmacrosymtable:=tmacrosymtable.create(false);
  1805. macrosymtablestack:=TSymtablestack.create;
  1806. macrosymtablestack.push(initialmacrosymtable);
  1807. {$ifdef UNITALIASES}
  1808. { unit aliases }
  1809. unitaliases:=TFPHashObjectList.create;
  1810. {$endif}
  1811. { set some global vars to nil, might be important for the ide }
  1812. class_tobject:=nil;
  1813. interface_iunknown:=nil;
  1814. rec_tguid:=nil;
  1815. dupnr:=0;
  1816. end;
  1817. procedure DoneSymtable;
  1818. begin
  1819. generrorsym.owner:=nil;
  1820. generrorsym.free;
  1821. generrordef.owner:=nil;
  1822. generrordef.free;
  1823. initialmacrosymtable.free;
  1824. macrosymtablestack.free;
  1825. {$ifdef UNITALIASES}
  1826. unitaliases.free;
  1827. {$endif}
  1828. end;
  1829. end.