symtable.pas 73 KB

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