symtable.pas 74 KB

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