symtable.pas 70 KB

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