symtable.pas 79 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308
  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 loaddefs(ppufile:tcompilerppufile);
  46. procedure loadsyms(ppufile:tcompilerppufile);
  47. procedure writedefs(ppufile:tcompilerppufile);
  48. procedure writesyms(ppufile:tcompilerppufile);
  49. public
  50. procedure insert(sym:TSymEntry;checkdup:boolean=true);override;
  51. procedure delete(sym:TSymEntry);override;
  52. { load/write }
  53. procedure ppuload(ppufile:tcompilerppufile);virtual;
  54. procedure ppuwrite(ppufile:tcompilerppufile);virtual;
  55. procedure buildderef;virtual;
  56. procedure buildderefimpl;virtual;
  57. procedure deref;virtual;
  58. procedure derefimpl;virtual;
  59. function checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;override;
  60. procedure reset_all_defs;virtual;
  61. procedure allsymbolsused;
  62. procedure allprivatesused;
  63. procedure check_forwards;
  64. procedure checklabels;
  65. function needs_init_final : boolean;
  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;vis:tvisibility);
  79. procedure addalignmentpadding;
  80. procedure insertdef(def:TDefEntry);override;
  81. function is_packed: boolean;
  82. protected
  83. _datasize : aint;
  84. { size in bits of the data in case of bitpacked record. Only important during construction, }
  85. { no need to save in/restore from ppu file. datasize is always (databitsize+7) div 8. }
  86. databitsize : aint;
  87. procedure setdatasize(val: aint);
  88. public
  89. property datasize : aint read _datasize write setdatasize;
  90. end;
  91. trecordsymtable = class(tabstractrecordsymtable)
  92. public
  93. constructor create(usealign:shortint);
  94. procedure insertunionst(unionst : trecordsymtable;offset : longint);
  95. end;
  96. tObjectSymtable = class(tabstractrecordsymtable)
  97. public
  98. constructor create(adefowner:tdef;const n:string;usealign:shortint);
  99. function checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;override;
  100. end;
  101. { tabstractlocalsymtable }
  102. tabstractlocalsymtable = class(tstoredsymtable)
  103. public
  104. procedure ppuwrite(ppufile:tcompilerppufile);override;
  105. function count_locals:longint;
  106. end;
  107. tlocalsymtable = class(tabstractlocalsymtable)
  108. public
  109. constructor create(adefowner:tdef;level:byte);
  110. function checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;override;
  111. end;
  112. tparasymtable = class(tabstractlocalsymtable)
  113. public
  114. constructor create(adefowner:tdef;level:byte);
  115. function checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;override;
  116. end;
  117. tabstractuniTSymtable = class(tstoredsymtable)
  118. public
  119. constructor create(const n : string;id:word);
  120. function iscurrentunit:boolean;override;
  121. end;
  122. tglobalsymtable = class(tabstractuniTSymtable)
  123. public
  124. unittypecount : word;
  125. constructor create(const n : string;id:word);
  126. procedure ppuload(ppufile:tcompilerppufile);override;
  127. procedure ppuwrite(ppufile:tcompilerppufile);override;
  128. function checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;override;
  129. end;
  130. tstaticsymtable = class(tabstractuniTSymtable)
  131. public
  132. constructor create(const n : string;id:word);
  133. procedure ppuload(ppufile:tcompilerppufile);override;
  134. procedure ppuwrite(ppufile:tcompilerppufile);override;
  135. function checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;override;
  136. end;
  137. twithsymtable = class(TSymtable)
  138. withrefnode : tobject; { tnode }
  139. constructor create(aowner:tdef;ASymList:TFPHashObjectList;refnode:tobject{tnode});
  140. destructor destroy;override;
  141. procedure clear;override;
  142. procedure insertdef(def:TDefEntry);override;
  143. end;
  144. tstt_excepTSymtable = class(TSymtable)
  145. public
  146. constructor create;
  147. end;
  148. tmacrosymtable = class(tstoredsymtable)
  149. public
  150. constructor create(exported: boolean);
  151. end;
  152. var
  153. systemunit : tglobalsymtable; { pointer to the system unit }
  154. {****************************************************************************
  155. Functions
  156. ****************************************************************************}
  157. {*** Misc ***}
  158. function FullTypeName(def,otherdef:tdef):string;
  159. procedure incompatibletypes(def1,def2:tdef);
  160. procedure hidesym(sym:TSymEntry);
  161. procedure duplicatesym(var hashedid:THashedIDString;dupsym,origsym:TSymEntry);
  162. {*** Search ***}
  163. procedure addsymref(sym:tsym);
  164. function is_visible_for_object(symst:tsymtable;symvisibility:tvisibility;contextobjdef:tobjectdef):boolean;
  165. function is_visible_for_object(pd:tprocdef;contextobjdef:tobjectdef):boolean;
  166. function is_visible_for_object(sym:tsym;contextobjdef:tobjectdef):boolean;
  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_named_module(const unitname, symname: TIDString; out srsym: tsym; out srsymtable: tsymtable): boolean;
  171. function searchsym_in_class(classh,contextclassh:tobjectdef;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  172. function searchsym_in_class_by_msgint(classh:tobjectdef;msgid:longint;out srdef : tdef;out srsym:tsym;out srsymtable:TSymtable):boolean;
  173. function searchsym_in_class_by_msgstr(classh:tobjectdef;const s:string;out srsym:tsym;out srsymtable:TSymtable):boolean;
  174. function search_system_type(const s: TIDString): ttypesym;
  175. function search_named_unit_globaltype(const unitname, typename: TIDString): ttypesym;
  176. function search_class_member(pd : tobjectdef;const s : string):tsym;
  177. function search_assignment_operator(from_def,to_def:Tdef):Tprocdef;
  178. function search_enumerator_operator(type_def:Tdef):Tprocdef;
  179. {Looks for macro s (must be given in upper case) in the macrosymbolstack, }
  180. {and returns it if found. Returns nil otherwise.}
  181. function search_macro(const s : string):tsym;
  182. { Additionally to searching for a macro, also checks whether it's still }
  183. { actually defined (could be disable using "undef") }
  184. function defined_macro(const s : string):boolean;
  185. {*** Object Helpers ***}
  186. function search_default_property(pd : tobjectdef) : tpropertysym;
  187. {*** Macro Helpers ***}
  188. {If called initially, the following procedures manipulate macros in }
  189. {initialmacrotable, otherwise they manipulate system macros local to a module.}
  190. {Name can be given in any case (it will be converted to upper case).}
  191. procedure def_system_macro(const name : string);
  192. procedure set_system_macro(const name, value : string);
  193. procedure set_system_compvar(const name, value : string);
  194. procedure undef_system_macro(const name : string);
  195. {*** symtable stack ***}
  196. { $ifdef DEBUG
  197. procedure test_symtablestack;
  198. procedure list_symtablestack;
  199. $endif DEBUG}
  200. {$ifdef UNITALIASES}
  201. type
  202. punit_alias = ^tunit_alias;
  203. tunit_alias = object(TNamedIndexItem)
  204. newname : pshortstring;
  205. constructor init(const n:string);
  206. destructor done;virtual;
  207. end;
  208. var
  209. unitaliases : pdictionary;
  210. procedure addunitalias(const n:string);
  211. function getunitalias(const n:string):string;
  212. {$endif UNITALIASES}
  213. {*** Init / Done ***}
  214. procedure IniTSymtable;
  215. procedure DoneSymtable;
  216. const
  217. overloaded_names : array [NOTOKEN..last_overloaded] of string[16] =
  218. ('error',
  219. 'plus','minus','star','slash','equal',
  220. 'greater','lower','greater_or_equal',
  221. 'lower_or_equal',
  222. 'sym_diff','starstar',
  223. 'as','is','in','or',
  224. 'and','div','mod','not','shl','shr','xor',
  225. 'assign','enumerator');
  226. implementation
  227. uses
  228. { global }
  229. verbose,globals,
  230. { target }
  231. systems,
  232. { symtable }
  233. symutil,defcmp,defutil,
  234. { module }
  235. fmodule,
  236. { codegen }
  237. procinfo
  238. ;
  239. var
  240. dupnr : longint; { unique number for duplicate symbols }
  241. {*****************************************************************************
  242. TStoredSymtable
  243. *****************************************************************************}
  244. procedure tstoredsymtable.insert(sym:TSymEntry;checkdup:boolean=true);
  245. begin
  246. inherited insert(sym,checkdup);
  247. end;
  248. procedure tstoredsymtable.delete(sym:TSymEntry);
  249. begin
  250. inherited delete(sym);
  251. end;
  252. procedure tstoredsymtable.ppuload(ppufile:tcompilerppufile);
  253. begin
  254. { load definitions }
  255. loaddefs(ppufile);
  256. { load symbols }
  257. loadsyms(ppufile);
  258. end;
  259. procedure tstoredsymtable.ppuwrite(ppufile:tcompilerppufile);
  260. begin
  261. { write definitions }
  262. writedefs(ppufile);
  263. { write symbols }
  264. writesyms(ppufile);
  265. end;
  266. procedure tstoredsymtable.loaddefs(ppufile:tcompilerppufile);
  267. var
  268. def : tdef;
  269. b : byte;
  270. begin
  271. { load start of definition section, which holds the amount of defs }
  272. if ppufile.readentry<>ibstartdefs then
  273. Message(unit_f_ppu_read_error);
  274. { read definitions }
  275. repeat
  276. b:=ppufile.readentry;
  277. case b of
  278. ibpointerdef : def:=tpointerdef.ppuload(ppufile);
  279. ibarraydef : def:=tarraydef.ppuload(ppufile);
  280. iborddef : def:=torddef.ppuload(ppufile);
  281. ibfloatdef : def:=tfloatdef.ppuload(ppufile);
  282. ibprocdef : def:=tprocdef.ppuload(ppufile);
  283. ibshortstringdef : def:=tstringdef.loadshort(ppufile);
  284. iblongstringdef : def:=tstringdef.loadlong(ppufile);
  285. ibansistringdef : def:=tstringdef.loadansi(ppufile);
  286. ibwidestringdef : def:=tstringdef.loadwide(ppufile);
  287. ibunicodestringdef : def:=tstringdef.loadunicode(ppufile);
  288. ibrecorddef : def:=trecorddef.ppuload(ppufile);
  289. ibobjectdef : def:=tobjectdef.ppuload(ppufile);
  290. ibenumdef : def:=tenumdef.ppuload(ppufile);
  291. ibsetdef : def:=tsetdef.ppuload(ppufile);
  292. ibprocvardef : def:=tprocvardef.ppuload(ppufile);
  293. ibfiledef : def:=tfiledef.ppuload(ppufile);
  294. ibclassrefdef : def:=tclassrefdef.ppuload(ppufile);
  295. ibformaldef : def:=tformaldef.ppuload(ppufile);
  296. ibvariantdef : def:=tvariantdef.ppuload(ppufile);
  297. ibundefineddef : def:=tundefineddef.ppuload(ppufile);
  298. ibenddefs : break;
  299. ibend : Message(unit_f_ppu_read_error);
  300. else
  301. Message1(unit_f_ppu_invalid_entry,tostr(b));
  302. end;
  303. InsertDef(def);
  304. until false;
  305. end;
  306. procedure tstoredsymtable.loadsyms(ppufile:tcompilerppufile);
  307. var
  308. b : byte;
  309. sym : tsym;
  310. begin
  311. { load start of definition section, which holds the amount of defs }
  312. if ppufile.readentry<>ibstartsyms then
  313. Message(unit_f_ppu_read_error);
  314. { now read the symbols }
  315. repeat
  316. b:=ppufile.readentry;
  317. case b of
  318. ibtypesym : sym:=ttypesym.ppuload(ppufile);
  319. ibprocsym : sym:=tprocsym.ppuload(ppufile);
  320. ibconstsym : sym:=tconstsym.ppuload(ppufile);
  321. ibstaticvarsym : sym:=tstaticvarsym.ppuload(ppufile);
  322. iblocalvarsym : sym:=tlocalvarsym.ppuload(ppufile);
  323. ibparavarsym : sym:=tparavarsym.ppuload(ppufile);
  324. ibfieldvarsym : sym:=tfieldvarsym.ppuload(ppufile);
  325. ibabsolutevarsym : sym:=tabsolutevarsym.ppuload(ppufile);
  326. ibenumsym : sym:=tenumsym.ppuload(ppufile);
  327. ibpropertysym : sym:=tpropertysym.ppuload(ppufile);
  328. ibunitsym : sym:=tunitsym.ppuload(ppufile);
  329. iblabelsym : sym:=tlabelsym.ppuload(ppufile);
  330. ibsyssym : sym:=tsyssym.ppuload(ppufile);
  331. ibmacrosym : sym:=tmacro.ppuload(ppufile);
  332. ibendsyms : break;
  333. ibend : Message(unit_f_ppu_read_error);
  334. else
  335. Message1(unit_f_ppu_invalid_entry,tostr(b));
  336. end;
  337. Insert(sym,false);
  338. until false;
  339. end;
  340. procedure tstoredsymtable.writedefs(ppufile:tcompilerppufile);
  341. var
  342. i : longint;
  343. def : tstoreddef;
  344. begin
  345. { each definition get a number, write then the amount of defs to the
  346. ibstartdef entry }
  347. ppufile.putlongint(DefList.count);
  348. ppufile.writeentry(ibstartdefs);
  349. { now write the definition }
  350. for i:=0 to DefList.Count-1 do
  351. begin
  352. def:=tstoreddef(DefList[i]);
  353. def.ppuwrite(ppufile);
  354. end;
  355. { write end of definitions }
  356. ppufile.writeentry(ibenddefs);
  357. end;
  358. procedure tstoredsymtable.writesyms(ppufile:tcompilerppufile);
  359. var
  360. i : longint;
  361. sym : Tstoredsym;
  362. begin
  363. { each definition get a number, write then the amount of syms and the
  364. datasize to the ibsymdef entry }
  365. ppufile.putlongint(SymList.count);
  366. ppufile.writeentry(ibstartsyms);
  367. { foreach is used to write all symbols }
  368. for i:=0 to SymList.Count-1 do
  369. begin
  370. sym:=tstoredsym(SymList[i]);
  371. sym.ppuwrite(ppufile);
  372. end;
  373. { end of symbols }
  374. ppufile.writeentry(ibendsyms);
  375. end;
  376. procedure tstoredsymtable.buildderef;
  377. var
  378. i : longint;
  379. def : tstoreddef;
  380. sym : tstoredsym;
  381. begin
  382. { interface definitions }
  383. for i:=0 to DefList.Count-1 do
  384. begin
  385. def:=tstoreddef(DefList[i]);
  386. def.buildderef;
  387. end;
  388. { interface symbols }
  389. for i:=0 to SymList.Count-1 do
  390. begin
  391. sym:=tstoredsym(SymList[i]);
  392. sym.buildderef;
  393. end;
  394. end;
  395. procedure tstoredsymtable.buildderefimpl;
  396. var
  397. i : longint;
  398. def : tstoreddef;
  399. begin
  400. { implementation definitions }
  401. for i:=0 to DefList.Count-1 do
  402. begin
  403. def:=tstoreddef(DefList[i]);
  404. def.buildderefimpl;
  405. end;
  406. end;
  407. procedure tstoredsymtable.deref;
  408. var
  409. i : longint;
  410. def : tstoreddef;
  411. sym : tstoredsym;
  412. begin
  413. { first deref the interface ttype symbols. This is needs
  414. to be done before the interface defs are derefed, because
  415. the interface defs can contain references to the type symbols
  416. which then already need to contain a resolved typedef field (PFV) }
  417. for i:=0 to SymList.Count-1 do
  418. begin
  419. sym:=tstoredsym(SymList[i]);
  420. if sym.typ=typesym then
  421. sym.deref;
  422. end;
  423. { interface definitions }
  424. for i:=0 to DefList.Count-1 do
  425. begin
  426. def:=tstoreddef(DefList[i]);
  427. def.deref;
  428. end;
  429. { interface symbols }
  430. for i:=0 to SymList.Count-1 do
  431. begin
  432. sym:=tstoredsym(SymList[i]);
  433. if sym.typ<>typesym then
  434. sym.deref;
  435. end;
  436. end;
  437. procedure tstoredsymtable.derefimpl;
  438. var
  439. i : longint;
  440. def : tstoreddef;
  441. begin
  442. { implementation definitions }
  443. for i:=0 to DefList.Count-1 do
  444. begin
  445. def:=tstoreddef(DefList[i]);
  446. def.derefimpl;
  447. end;
  448. end;
  449. function tstoredsymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  450. var
  451. hsym : tsym;
  452. begin
  453. hsym:=tsym(FindWithHash(hashedid));
  454. if assigned(hsym) then
  455. DuplicateSym(hashedid,sym,hsym);
  456. result:=assigned(hsym);
  457. end;
  458. {**************************************
  459. Callbacks
  460. **************************************}
  461. procedure TStoredSymtable.check_forward(sym:TObject;arg:pointer);
  462. begin
  463. if tsym(sym).typ=procsym then
  464. tprocsym(sym).check_forward
  465. { check also object method table }
  466. { we needn't to test the def list }
  467. { because each object has to have a type sym,
  468. only test objects declarations, not type renamings }
  469. else
  470. if (tsym(sym).typ=typesym) and
  471. assigned(ttypesym(sym).typedef) and
  472. (ttypesym(sym).typedef.typesym=ttypesym(sym)) and
  473. (ttypesym(sym).typedef.typ=objectdef) then
  474. tobjectdef(ttypesym(sym).typedef).check_forwards;
  475. end;
  476. procedure TStoredSymtable.labeldefined(sym:TObject;arg:pointer);
  477. begin
  478. if (tsym(sym).typ=labelsym) and
  479. not(tlabelsym(sym).defined) then
  480. begin
  481. if tlabelsym(sym).used then
  482. Message1(sym_e_label_used_and_not_defined,tlabelsym(sym).realname)
  483. else
  484. Message1(sym_w_label_not_defined,tlabelsym(sym).realname);
  485. end;
  486. end;
  487. procedure TStoredSymtable.varsymbolused(sym:TObject;arg:pointer);
  488. begin
  489. if (tsym(sym).typ in [staticvarsym,localvarsym,paravarsym,fieldvarsym]) and
  490. ((tsym(sym).owner.symtabletype in
  491. [parasymtable,localsymtable,ObjectSymtable,staticsymtable])) then
  492. begin
  493. { unused symbol should be reported only if no }
  494. { error is reported }
  495. { if the symbol is in a register it is used }
  496. { also don't count the value parameters which have local copies }
  497. { also don't claim for high param of open parameters (PM) }
  498. if (Errorcount<>0) or
  499. ([vo_is_hidden_para,vo_is_funcret] * tabstractvarsym(sym).varoptions = [vo_is_hidden_para]) then
  500. exit;
  501. if (tstoredsym(sym).refs=0) then
  502. begin
  503. if (vo_is_funcret in tabstractvarsym(sym).varoptions) then
  504. begin
  505. { don't warn about the result of constructors }
  506. if ((tsym(sym).owner.symtabletype<>localsymtable) or
  507. (tprocdef(tsym(sym).owner.defowner).proctypeoption<>potype_constructor)) and
  508. not(cs_opt_nodedfa in current_settings.optimizerswitches) then
  509. MessagePos(tsym(sym).fileinfo,sym_w_function_result_not_set)
  510. end
  511. else if (tsym(sym).owner.symtabletype=parasymtable) then
  512. MessagePos1(tsym(sym).fileinfo,sym_h_para_identifier_not_used,tsym(sym).prettyname)
  513. else if (tsym(sym).owner.symtabletype=ObjectSymtable) then
  514. MessagePos2(tsym(sym).fileinfo,sym_n_private_identifier_not_used,tsym(sym).owner.realname^,tsym(sym).prettyname)
  515. else
  516. MessagePos1(tsym(sym).fileinfo,sym_n_local_identifier_not_used,tsym(sym).prettyname);
  517. end
  518. else if tabstractvarsym(sym).varstate in [vs_written,vs_initialised] then
  519. begin
  520. if (tsym(sym).owner.symtabletype=parasymtable) then
  521. begin
  522. if not(tabstractvarsym(sym).varspez in [vs_var,vs_out]) and
  523. not(vo_is_funcret in tabstractvarsym(sym).varoptions) then
  524. MessagePos1(tsym(sym).fileinfo,sym_h_para_identifier_only_set,tsym(sym).prettyname)
  525. end
  526. else if (tsym(sym).owner.symtabletype=ObjectSymtable) then
  527. MessagePos2(tsym(sym).fileinfo,sym_n_private_identifier_only_set,tsym(sym).owner.realname^,tsym(sym).prettyname)
  528. else if tabstractvarsym(sym).varoptions*[vo_is_funcret,vo_is_public,vo_is_external]=[] then
  529. MessagePos1(tsym(sym).fileinfo,sym_n_local_identifier_only_set,tsym(sym).prettyname);
  530. end
  531. else if (tabstractvarsym(sym).varstate = vs_read_not_warned) and
  532. ([vo_is_public,vo_is_external] * tabstractvarsym(sym).varoptions = []) then
  533. MessagePos1(tsym(sym).fileinfo,sym_w_identifier_only_read,tsym(sym).prettyname)
  534. end
  535. else if ((tsym(sym).owner.symtabletype in
  536. [ObjectSymtable,parasymtable,localsymtable,staticsymtable])) then
  537. begin
  538. if (Errorcount<>0) or
  539. (sp_internal in tsym(sym).symoptions) then
  540. exit;
  541. { do not claim for inherited private fields !! }
  542. if (tsym(sym).refs=0) and (tsym(sym).owner.symtabletype=ObjectSymtable) then
  543. MessagePos2(tsym(sym).fileinfo,sym_n_private_method_not_used,tsym(sym).owner.realname^,tsym(sym).prettyname)
  544. { units references are problematic }
  545. else
  546. begin
  547. if (tsym(sym).refs=0) and
  548. not(tsym(sym).typ in [enumsym,unitsym]) and
  549. not(is_funcret_sym(tsym(sym))) and
  550. { don't complain about compiler generated syms for specializations, see also #13405 }
  551. not((tsym(sym).typ=typesym) and (df_specialization in ttypesym(sym).typedef.defoptions) and
  552. (pos('$',ttypesym(sym).Realname)<>0)) and
  553. (
  554. (tsym(sym).typ<>procsym) or
  555. ((tsym(sym).owner.symtabletype=staticsymtable) and
  556. not current_module.is_unit)
  557. ) and
  558. { don't complain about alias for hidden _cmd parameter to
  559. obj-c methods }
  560. not((tsym(sym).typ in [localvarsym,paravarsym,absolutevarsym]) and
  561. (vo_is_msgsel in tabstractvarsym(sym).varoptions)) then
  562. MessagePos2(tsym(sym).fileinfo,sym_h_local_symbol_not_used,SymTypeName[tsym(sym).typ],tsym(sym).prettyname);
  563. end;
  564. end;
  565. end;
  566. procedure TStoredSymtable.TestPrivate(sym:TObject;arg:pointer);
  567. begin
  568. if tsym(sym).visibility=vis_private then
  569. varsymbolused(sym,arg);
  570. end;
  571. procedure TStoredSymtable.objectprivatesymbolused(sym:TObject;arg:pointer);
  572. begin
  573. {
  574. Don't test simple object aliases PM
  575. }
  576. if (tsym(sym).typ=typesym) and
  577. (ttypesym(sym).typedef.typ=objectdef) and
  578. (ttypesym(sym).typedef.typesym=tsym(sym)) then
  579. tobjectdef(ttypesym(sym).typedef).symtable.SymList.ForEachCall(@TestPrivate,nil);
  580. end;
  581. procedure tstoredsymtable.testfordefaultproperty(sym:TObject;arg:pointer);
  582. begin
  583. if (tsym(sym).typ=propertysym) and
  584. (ppo_defaultproperty in tpropertysym(sym).propoptions) then
  585. ppointer(arg)^:=sym;
  586. end;
  587. {***********************************************
  588. Process all entries
  589. ***********************************************}
  590. procedure Tstoredsymtable.reset_all_defs;
  591. var
  592. i : longint;
  593. def : tstoreddef;
  594. begin
  595. for i:=0 to DefList.Count-1 do
  596. begin
  597. def:=tstoreddef(DefList[i]);
  598. def.reset;
  599. end;
  600. end;
  601. { checks, if all procsyms and methods are defined }
  602. procedure tstoredsymtable.check_forwards;
  603. begin
  604. SymList.ForEachCall(@check_forward,nil);
  605. end;
  606. procedure tstoredsymtable.checklabels;
  607. begin
  608. SymList.ForEachCall(@labeldefined,nil);
  609. end;
  610. procedure tstoredsymtable.allsymbolsused;
  611. begin
  612. SymList.ForEachCall(@varsymbolused,nil);
  613. end;
  614. procedure tstoredsymtable.allprivatesused;
  615. begin
  616. SymList.ForEachCall(@objectprivatesymbolused,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. (
  977. not(m_delphi in current_settings.modeswitches) and
  978. is_visible_for_object(hsym,tobjectdef(defowner))
  979. ) or
  980. (
  981. { In Delphi, you can repeat members of a parent class. You can't }
  982. { do this for objects however, and you (obviouly) can't }
  983. { declare two fields with the same name in a single class }
  984. (m_delphi in current_settings.modeswitches) and
  985. (
  986. is_object(tdef(defowner)) or
  987. (hsym.owner = self)
  988. )
  989. )
  990. ) then
  991. begin
  992. DuplicateSym(hashedid,sym,hsym);
  993. result:=true;
  994. end;
  995. end
  996. else
  997. begin
  998. if not(m_duplicate_names in current_settings.modeswitches) then
  999. result:=inherited checkduplicate(hashedid,sym);
  1000. end;
  1001. end;
  1002. {****************************************************************************
  1003. TAbstractLocalSymtable
  1004. ****************************************************************************}
  1005. procedure tabstractlocalsymtable.ppuwrite(ppufile:tcompilerppufile);
  1006. var
  1007. oldtyp : byte;
  1008. begin
  1009. oldtyp:=ppufile.entrytyp;
  1010. ppufile.entrytyp:=subentryid;
  1011. { write definitions }
  1012. writedefs(ppufile);
  1013. { write symbols }
  1014. writesyms(ppufile);
  1015. ppufile.entrytyp:=oldtyp;
  1016. end;
  1017. function tabstractlocalsymtable.count_locals:longint;
  1018. var
  1019. i : longint;
  1020. sym : tsym;
  1021. begin
  1022. result:=0;
  1023. for i:=0 to SymList.Count-1 do
  1024. begin
  1025. sym:=tsym(SymList[i]);
  1026. { Count only varsyms, but ignore the funcretsym }
  1027. if (tsym(sym).typ in [localvarsym,paravarsym]) and
  1028. (tsym(sym)<>current_procinfo.procdef.funcretsym) and
  1029. (not(vo_is_parentfp in tabstractvarsym(sym).varoptions) or
  1030. (tstoredsym(sym).refs>0)) then
  1031. inc(result);
  1032. end;
  1033. end;
  1034. {****************************************************************************
  1035. TLocalSymtable
  1036. ****************************************************************************}
  1037. constructor tlocalsymtable.create(adefowner:tdef;level:byte);
  1038. begin
  1039. inherited create('');
  1040. defowner:=adefowner;
  1041. symtabletype:=localsymtable;
  1042. symtablelevel:=level;
  1043. end;
  1044. function tlocalsymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  1045. var
  1046. hsym : tsym;
  1047. begin
  1048. if not assigned(defowner) or
  1049. (defowner.typ<>procdef) then
  1050. internalerror(200602042);
  1051. result:=false;
  1052. hsym:=tsym(FindWithHash(hashedid));
  1053. if assigned(hsym) then
  1054. begin
  1055. { a local and the function can have the same
  1056. name in TP and Delphi, but RESULT not }
  1057. if (m_duplicate_names in current_settings.modeswitches) and
  1058. (hsym.typ in [absolutevarsym,localvarsym]) and
  1059. (vo_is_funcret in tabstractvarsym(hsym).varoptions) and
  1060. not((m_result in current_settings.modeswitches) and
  1061. (vo_is_result in tabstractvarsym(hsym).varoptions)) then
  1062. HideSym(hsym)
  1063. else
  1064. DuplicateSym(hashedid,sym,hsym);
  1065. result:=true;
  1066. exit;
  1067. end;
  1068. { check also parasymtable, this needs to be done here becuase
  1069. of the special situation with the funcret sym that needs to be
  1070. hidden for tp and delphi modes }
  1071. hsym:=tsym(tabstractprocdef(defowner).parast.FindWithHash(hashedid));
  1072. if assigned(hsym) then
  1073. begin
  1074. { a local and the function can have the same
  1075. name in TP and Delphi, but RESULT not }
  1076. if (m_duplicate_names in current_settings.modeswitches) and
  1077. (sym.typ in [absolutevarsym,localvarsym]) and
  1078. (vo_is_funcret in tabstractvarsym(sym).varoptions) and
  1079. not((m_result in current_settings.modeswitches) and
  1080. (vo_is_result in tabstractvarsym(sym).varoptions)) then
  1081. Hidesym(sym)
  1082. else
  1083. DuplicateSym(hashedid,sym,hsym);
  1084. result:=true;
  1085. exit;
  1086. end;
  1087. { check ObjectSymtable, skip this for funcret sym because
  1088. that will always be positive because it has the same name
  1089. as the procsym }
  1090. if not is_funcret_sym(sym) and
  1091. (defowner.typ=procdef) and
  1092. assigned(tprocdef(defowner)._class) and
  1093. (tprocdef(defowner).owner.defowner=tprocdef(defowner)._class) and
  1094. (
  1095. not(m_delphi in current_settings.modeswitches) or
  1096. is_object(tprocdef(defowner)._class)
  1097. ) then
  1098. result:=tprocdef(defowner)._class.symtable.checkduplicate(hashedid,sym);
  1099. end;
  1100. {****************************************************************************
  1101. TParaSymtable
  1102. ****************************************************************************}
  1103. constructor tparasymtable.create(adefowner:tdef;level:byte);
  1104. begin
  1105. inherited create('');
  1106. defowner:=adefowner;
  1107. symtabletype:=parasymtable;
  1108. symtablelevel:=level;
  1109. end;
  1110. function tparasymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  1111. begin
  1112. result:=inherited checkduplicate(hashedid,sym);
  1113. if result then
  1114. exit;
  1115. if not(m_duplicate_names in current_settings.modeswitches) and
  1116. (defowner.typ=procdef) and
  1117. assigned(tprocdef(defowner)._class) and
  1118. (tprocdef(defowner).owner.defowner=tprocdef(defowner)._class) and
  1119. (
  1120. not(m_delphi in current_settings.modeswitches) or
  1121. is_object(tprocdef(defowner)._class)
  1122. ) then
  1123. result:=tprocdef(defowner)._class.symtable.checkduplicate(hashedid,sym);
  1124. end;
  1125. {****************************************************************************
  1126. TAbstractUniTSymtable
  1127. ****************************************************************************}
  1128. constructor tabstractuniTSymtable.create(const n : string;id:word);
  1129. begin
  1130. inherited create(n);
  1131. moduleid:=id;
  1132. end;
  1133. function tabstractuniTSymtable.iscurrentunit:boolean;
  1134. begin
  1135. result:=assigned(current_module) and
  1136. (
  1137. (current_module.globalsymtable=self) or
  1138. (current_module.localsymtable=self)
  1139. );
  1140. end;
  1141. {****************************************************************************
  1142. TStaticSymtable
  1143. ****************************************************************************}
  1144. constructor tstaticsymtable.create(const n : string;id:word);
  1145. begin
  1146. inherited create(n,id);
  1147. symtabletype:=staticsymtable;
  1148. symtablelevel:=main_program_level;
  1149. end;
  1150. procedure tstaticsymtable.ppuload(ppufile:tcompilerppufile);
  1151. begin
  1152. inherited ppuload(ppufile);
  1153. { now we can deref the syms and defs }
  1154. deref;
  1155. end;
  1156. procedure tstaticsymtable.ppuwrite(ppufile:tcompilerppufile);
  1157. begin
  1158. inherited ppuwrite(ppufile);
  1159. end;
  1160. function tstaticsymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  1161. var
  1162. hsym : tsym;
  1163. begin
  1164. result:=false;
  1165. hsym:=tsym(FindWithHash(hashedid));
  1166. if assigned(hsym) then
  1167. begin
  1168. { Delphi (contrary to TP) you can have a symbol with the same name as the
  1169. unit, the unit can then not be accessed anymore using
  1170. <unit>.<id>, so we can hide the symbol }
  1171. if (m_delphi in current_settings.modeswitches) and
  1172. (hsym.typ=symconst.unitsym) then
  1173. HideSym(hsym)
  1174. else
  1175. DuplicateSym(hashedid,sym,hsym);
  1176. result:=true;
  1177. exit;
  1178. end;
  1179. if (current_module.localsymtable=self) and
  1180. assigned(current_module.globalsymtable) then
  1181. result:=tglobalsymtable(current_module.globalsymtable).checkduplicate(hashedid,sym);
  1182. end;
  1183. {****************************************************************************
  1184. TGlobalSymtable
  1185. ****************************************************************************}
  1186. constructor tglobalsymtable.create(const n : string;id:word);
  1187. begin
  1188. inherited create(n,id);
  1189. symtabletype:=globalsymtable;
  1190. symtablelevel:=main_program_level;
  1191. end;
  1192. procedure tglobalsymtable.ppuload(ppufile:tcompilerppufile);
  1193. begin
  1194. inherited ppuload(ppufile);
  1195. { now we can deref the syms and defs }
  1196. deref;
  1197. end;
  1198. procedure tglobalsymtable.ppuwrite(ppufile:tcompilerppufile);
  1199. begin
  1200. { write the symtable entries }
  1201. inherited ppuwrite(ppufile);
  1202. end;
  1203. function tglobalsymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  1204. var
  1205. hsym : tsym;
  1206. begin
  1207. result:=false;
  1208. hsym:=tsym(FindWithHash(hashedid));
  1209. if assigned(hsym) then
  1210. begin
  1211. { Delphi (contrary to TP) you can have a symbol with the same name as the
  1212. unit, the unit can then not be accessed anymore using
  1213. <unit>.<id>, so we can hide the symbol }
  1214. if (m_delphi in current_settings.modeswitches) and
  1215. (hsym.typ=symconst.unitsym) then
  1216. HideSym(hsym)
  1217. else
  1218. DuplicateSym(hashedid,sym,hsym);
  1219. result:=true;
  1220. exit;
  1221. end;
  1222. end;
  1223. {****************************************************************************
  1224. TWITHSYMTABLE
  1225. ****************************************************************************}
  1226. constructor twithsymtable.create(aowner:tdef;ASymList:TFPHashObjectList;refnode:tobject{tnode});
  1227. begin
  1228. inherited create('');
  1229. symtabletype:=withsymtable;
  1230. withrefnode:=refnode;
  1231. { Replace SymList with the passed symlist }
  1232. SymList.free;
  1233. SymList:=ASymList;
  1234. defowner:=aowner;
  1235. end;
  1236. destructor twithsymtable.destroy;
  1237. begin
  1238. withrefnode.free;
  1239. { Disable SymList because we don't Own it }
  1240. SymList:=nil;
  1241. inherited destroy;
  1242. end;
  1243. procedure twithsymtable.clear;
  1244. begin
  1245. { remove no entry from a withsymtable as it is only a pointer to the
  1246. recorddef or objectdef symtable }
  1247. end;
  1248. procedure twithsymtable.insertdef(def:TDefEntry);
  1249. begin
  1250. { Definitions can't be registered in the withsymtable
  1251. because the withsymtable is removed after the with block.
  1252. We can't easily solve it here because the next symtable in the
  1253. stack is not known. }
  1254. internalerror(200602046);
  1255. end;
  1256. {****************************************************************************
  1257. TSTT_ExceptionSymtable
  1258. ****************************************************************************}
  1259. constructor tstt_excepTSymtable.create;
  1260. begin
  1261. inherited create('');
  1262. symtabletype:=stt_excepTSymtable;
  1263. end;
  1264. {****************************************************************************
  1265. TMacroSymtable
  1266. ****************************************************************************}
  1267. constructor tmacrosymtable.create(exported: boolean);
  1268. begin
  1269. inherited create('');
  1270. if exported then
  1271. symtabletype:=exportedmacrosymtable
  1272. else
  1273. symtabletype:=localmacrosymtable;
  1274. symtablelevel:=main_program_level;
  1275. end;
  1276. {*****************************************************************************
  1277. Helper Routines
  1278. *****************************************************************************}
  1279. function FullTypeName(def,otherdef:tdef):string;
  1280. var
  1281. s1,s2 : string;
  1282. begin
  1283. s1:=def.typename;
  1284. { When the names are the same try to include the unit name }
  1285. if assigned(otherdef) and
  1286. (def.owner.symtabletype in [globalsymtable,staticsymtable]) then
  1287. begin
  1288. s2:=otherdef.typename;
  1289. if upper(s1)=upper(s2) then
  1290. s1:=def.owner.realname^+'.'+s1;
  1291. end;
  1292. FullTypeName:=s1;
  1293. end;
  1294. procedure incompatibletypes(def1,def2:tdef);
  1295. begin
  1296. { When there is an errordef there is already an error message show }
  1297. if (def2.typ=errordef) or
  1298. (def1.typ=errordef) then
  1299. exit;
  1300. CGMessage2(type_e_incompatible_types,FullTypeName(def1,def2),FullTypeName(def2,def1));
  1301. end;
  1302. procedure hidesym(sym:TSymEntry);
  1303. begin
  1304. sym.realname:='$hidden'+sym.realname;
  1305. tsym(sym).visibility:=vis_hidden;
  1306. end;
  1307. procedure duplicatesym(var hashedid:THashedIDString;dupsym,origsym:TSymEntry);
  1308. var
  1309. st : TSymtable;
  1310. begin
  1311. Message1(sym_e_duplicate_id,tsym(origsym).realname);
  1312. { Write hint where the original symbol was found }
  1313. st:=finduniTSymtable(origsym.owner);
  1314. with tsym(origsym).fileinfo do
  1315. begin
  1316. if assigned(st) and
  1317. (st.symtabletype=globalsymtable) and
  1318. st.iscurrentunit then
  1319. Message2(sym_h_duplicate_id_where,current_module.sourcefiles.get_file_name(fileindex),tostr(line))
  1320. else if assigned(st.name) then
  1321. Message2(sym_h_duplicate_id_where,'unit '+st.name^,tostr(line));
  1322. end;
  1323. { Rename duplicate sym to an unreachable name, but it can be
  1324. inserted in the symtable without errors }
  1325. inc(dupnr);
  1326. hashedid.id:='dup'+tostr(dupnr)+hashedid.id;
  1327. if assigned(dupsym) then
  1328. include(tsym(dupsym).symoptions,sp_implicitrename);
  1329. end;
  1330. {*****************************************************************************
  1331. Search
  1332. *****************************************************************************}
  1333. procedure addsymref(sym:tsym);
  1334. begin
  1335. { symbol uses count }
  1336. sym.IncRefCount;
  1337. { unit uses count }
  1338. if assigned(current_module) and
  1339. (sym.owner.symtabletype=globalsymtable) then
  1340. begin
  1341. if tglobalsymtable(sym.owner).moduleid>=current_module.unitmapsize then
  1342. internalerror(200501152);
  1343. inc(current_module.unitmap[tglobalsymtable(sym.owner).moduleid].refs);
  1344. end;
  1345. end;
  1346. function is_visible_for_object(symst:tsymtable;symvisibility:tvisibility;contextobjdef:tobjectdef):boolean;
  1347. var
  1348. symownerdef : tobjectdef;
  1349. begin
  1350. result:=false;
  1351. { Get objdectdef owner of the symtable for the is_related checks }
  1352. if not assigned(symst) or
  1353. (symst.symtabletype<>objectsymtable) then
  1354. internalerror(200810285);
  1355. symownerdef:=tobjectdef(symst.defowner);
  1356. case symvisibility of
  1357. vis_private :
  1358. begin
  1359. { private symbols are allowed when we are in the same
  1360. module as they are defined }
  1361. result:=(symownerdef.owner.symtabletype in [globalsymtable,staticsymtable]) and
  1362. (symownerdef.owner.iscurrentunit);
  1363. end;
  1364. vis_strictprivate :
  1365. begin
  1366. result:=assigned(current_objectdef) and
  1367. (current_objectdef=symownerdef);
  1368. end;
  1369. vis_strictprotected :
  1370. begin
  1371. result:=assigned(current_objectdef) and
  1372. current_objectdef.is_related(symownerdef);
  1373. end;
  1374. vis_protected :
  1375. begin
  1376. { protected symbols are visible in the module that defines them and
  1377. also visible to related objects. The related object must be defined
  1378. in the current module }
  1379. result:=(
  1380. (
  1381. (symownerdef.owner.symtabletype in [globalsymtable,staticsymtable]) and
  1382. (symownerdef.owner.iscurrentunit)
  1383. ) or
  1384. (
  1385. assigned(contextobjdef) and
  1386. (contextobjdef.owner.symtabletype in [globalsymtable,staticsymtable]) and
  1387. (contextobjdef.owner.iscurrentunit) and
  1388. contextobjdef.is_related(symownerdef)
  1389. )
  1390. );
  1391. end;
  1392. vis_public,
  1393. vis_published :
  1394. result:=true;
  1395. end;
  1396. end;
  1397. function is_visible_for_object(pd:tprocdef;contextobjdef:tobjectdef):boolean;
  1398. begin
  1399. result:=is_visible_for_object(pd.owner,pd.visibility,contextobjdef);
  1400. end;
  1401. function is_visible_for_object(sym:tsym;contextobjdef:tobjectdef):boolean;
  1402. var
  1403. i : longint;
  1404. pd : tprocdef;
  1405. begin
  1406. if sym.typ=procsym then
  1407. begin
  1408. { A procsym is visible, when there is at least one of the procdefs visible }
  1409. result:=false;
  1410. for i:=0 to tprocsym(sym).ProcdefList.Count-1 do
  1411. begin
  1412. pd:=tprocdef(tprocsym(sym).ProcdefList[i]);
  1413. if (pd.owner=sym.owner) and
  1414. is_visible_for_object(pd,contextobjdef) then
  1415. begin
  1416. result:=true;
  1417. exit;
  1418. end;
  1419. end;
  1420. end
  1421. else
  1422. result:=is_visible_for_object(sym.owner,sym.visibility,contextobjdef);
  1423. end;
  1424. function searchsym(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  1425. var
  1426. hashedid : THashedIDString;
  1427. contextobjdef : tobjectdef;
  1428. stackitem : psymtablestackitem;
  1429. begin
  1430. result:=false;
  1431. hashedid.id:=s;
  1432. stackitem:=symtablestack.stack;
  1433. while assigned(stackitem) do
  1434. begin
  1435. srsymtable:=stackitem^.symtable;
  1436. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  1437. if assigned(srsym) then
  1438. begin
  1439. { use the class from withsymtable only when it is
  1440. defined in this unit }
  1441. if (srsymtable.symtabletype=withsymtable) and
  1442. assigned(srsymtable.defowner) and
  1443. (srsymtable.defowner.typ=objectdef) and
  1444. (srsymtable.defowner.owner.symtabletype in [globalsymtable,staticsymtable]) and
  1445. (srsymtable.defowner.owner.iscurrentunit) then
  1446. contextobjdef:=tobjectdef(srsymtable.defowner)
  1447. else
  1448. contextobjdef:=current_objectdef;
  1449. if (srsym.owner.symtabletype<>objectsymtable) or
  1450. is_visible_for_object(srsym,contextobjdef) then
  1451. begin
  1452. { we need to know if a procedure references symbols
  1453. in the static symtable, because then it can't be
  1454. inlined from outside this unit }
  1455. if assigned(current_procinfo) and
  1456. (srsym.owner.symtabletype=staticsymtable) then
  1457. include(current_procinfo.flags,pi_uses_static_symtable);
  1458. addsymref(srsym);
  1459. result:=true;
  1460. exit;
  1461. end;
  1462. end;
  1463. stackitem:=stackitem^.next;
  1464. end;
  1465. srsym:=nil;
  1466. srsymtable:=nil;
  1467. end;
  1468. function searchsym_type(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  1469. var
  1470. hashedid : THashedIDString;
  1471. stackitem : psymtablestackitem;
  1472. begin
  1473. result:=false;
  1474. hashedid.id:=s;
  1475. stackitem:=symtablestack.stack;
  1476. while assigned(stackitem) do
  1477. begin
  1478. {
  1479. It is not possible to have type symbols in:
  1480. records
  1481. objects
  1482. parameters
  1483. Exception are generic definitions and specializations
  1484. that have the parameterized types inserted in the symtable.
  1485. }
  1486. srsymtable:=stackitem^.symtable;
  1487. if not(srsymtable.symtabletype in [recordsymtable,ObjectSymtable,parasymtable]) or
  1488. (assigned(srsymtable.defowner) and
  1489. (
  1490. (df_generic in tdef(srsymtable.defowner).defoptions) or
  1491. (df_specialization in tdef(srsymtable.defowner).defoptions))
  1492. ) then
  1493. begin
  1494. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  1495. if assigned(srsym) and
  1496. not(srsym.typ in [fieldvarsym,paravarsym]) and
  1497. (
  1498. (srsym.owner.symtabletype<>objectsymtable) or
  1499. is_visible_for_object(srsym,current_objectdef)
  1500. ) then
  1501. begin
  1502. { we need to know if a procedure references symbols
  1503. in the static symtable, because then it can't be
  1504. inlined from outside this unit }
  1505. if assigned(current_procinfo) and
  1506. (srsym.owner.symtabletype=staticsymtable) then
  1507. include(current_procinfo.flags,pi_uses_static_symtable);
  1508. addsymref(srsym);
  1509. result:=true;
  1510. exit;
  1511. end;
  1512. end;
  1513. stackitem:=stackitem^.next;
  1514. end;
  1515. result:=false;
  1516. srsym:=nil;
  1517. srsymtable:=nil;
  1518. end;
  1519. function searchsym_in_module(pm:pointer;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  1520. var
  1521. pmod : tmodule;
  1522. begin
  1523. pmod:=tmodule(pm);
  1524. result:=false;
  1525. if assigned(pmod.globalsymtable) then
  1526. begin
  1527. srsym:=tsym(pmod.globalsymtable.Find(s));
  1528. if assigned(srsym) then
  1529. begin
  1530. srsymtable:=pmod.globalsymtable;
  1531. addsymref(srsym);
  1532. result:=true;
  1533. exit;
  1534. end;
  1535. end;
  1536. { If the module is the current unit we also need
  1537. to search the local symtable }
  1538. if (pmod=current_module) and
  1539. assigned(pmod.localsymtable) then
  1540. begin
  1541. srsym:=tsym(pmod.localsymtable.Find(s));
  1542. if assigned(srsym) then
  1543. begin
  1544. srsymtable:=pmod.localsymtable;
  1545. addsymref(srsym);
  1546. result:=true;
  1547. exit;
  1548. end;
  1549. end;
  1550. srsym:=nil;
  1551. srsymtable:=nil;
  1552. end;
  1553. function searchsym_in_named_module(const unitname, symname: TIDString; out srsym: tsym; out srsymtable: tsymtable): boolean;
  1554. var
  1555. stackitem : psymtablestackitem;
  1556. begin
  1557. result:=false;
  1558. stackitem:=symtablestack.stack;
  1559. while assigned(stackitem) do
  1560. begin
  1561. srsymtable:=stackitem^.symtable;
  1562. if (srsymtable.symtabletype=globalsymtable) and
  1563. (srsymtable.name^=unitname) then
  1564. begin
  1565. srsym:=tsym(srsymtable.find(symname));
  1566. if not assigned(srsym) then
  1567. break;
  1568. result:=true;
  1569. exit;
  1570. end;
  1571. stackitem:=stackitem^.next;
  1572. end;
  1573. { If the module is the current unit we also need
  1574. to search the local symtable }
  1575. if (current_module.globalsymtable=srsymtable) and
  1576. assigned(current_module.localsymtable) then
  1577. begin
  1578. srsymtable:=current_module.localsymtable;
  1579. srsym:=tsym(srsymtable.find(symname));
  1580. if assigned(srsym) then
  1581. begin
  1582. result:=true;
  1583. exit;
  1584. end;
  1585. end;
  1586. end;
  1587. function searchsym_in_class(classh,contextclassh:tobjectdef;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  1588. var
  1589. hashedid : THashedIDString;
  1590. begin
  1591. { The contextclassh is used for visibility. The classh must be equal to
  1592. or be a parent of contextclassh. E.g. for inherited searches the classh is the
  1593. parent. }
  1594. if assigned(classh) and
  1595. not contextclassh.is_related(classh) then
  1596. internalerror(200811161);
  1597. result:=false;
  1598. hashedid.id:=s;
  1599. while assigned(classh) do
  1600. begin
  1601. srsymtable:=classh.symtable;
  1602. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  1603. if assigned(srsym) and
  1604. is_visible_for_object(srsym,contextclassh) then
  1605. begin
  1606. addsymref(srsym);
  1607. result:=true;
  1608. exit;
  1609. end;
  1610. classh:=classh.childof;
  1611. end;
  1612. srsym:=nil;
  1613. srsymtable:=nil;
  1614. end;
  1615. function searchsym_in_class_by_msgint(classh:tobjectdef;msgid:longint;out srdef : tdef;out srsym:tsym;out srsymtable:TSymtable):boolean;
  1616. var
  1617. def : tdef;
  1618. i : longint;
  1619. begin
  1620. result:=false;
  1621. def:=nil;
  1622. while assigned(classh) do
  1623. begin
  1624. for i:=0 to classh.symtable.DefList.Count-1 do
  1625. begin
  1626. def:=tstoreddef(classh.symtable.DefList[i]);
  1627. { Find also all hidden private methods to
  1628. be compatible with delphi, see tw6203 (PFV) }
  1629. if (def.typ=procdef) and
  1630. (po_msgint in tprocdef(def).procoptions) and
  1631. (tprocdef(def).messageinf.i=msgid) then
  1632. begin
  1633. srdef:=def;
  1634. srsym:=tprocdef(def).procsym;
  1635. srsymtable:=classh.symtable;
  1636. addsymref(srsym);
  1637. result:=true;
  1638. exit;
  1639. end;
  1640. end;
  1641. classh:=classh.childof;
  1642. end;
  1643. srdef:=nil;
  1644. srsym:=nil;
  1645. srsymtable:=nil;
  1646. end;
  1647. function searchsym_in_class_by_msgstr(classh:tobjectdef;const s:string;out srsym:tsym;out srsymtable:TSymtable):boolean;
  1648. var
  1649. def : tdef;
  1650. i : longint;
  1651. begin
  1652. result:=false;
  1653. def:=nil;
  1654. while assigned(classh) do
  1655. begin
  1656. for i:=0 to classh.symtable.DefList.Count-1 do
  1657. begin
  1658. def:=tstoreddef(classh.symtable.DefList[i]);
  1659. { Find also all hidden private methods to
  1660. be compatible with delphi, see tw6203 (PFV) }
  1661. if (def.typ=procdef) and
  1662. (po_msgstr in tprocdef(def).procoptions) and
  1663. (tprocdef(def).messageinf.str^=s) then
  1664. begin
  1665. srsym:=tprocdef(def).procsym;
  1666. srsymtable:=classh.symtable;
  1667. addsymref(srsym);
  1668. result:=true;
  1669. exit;
  1670. end;
  1671. end;
  1672. classh:=classh.childof;
  1673. end;
  1674. srsym:=nil;
  1675. srsymtable:=nil;
  1676. end;
  1677. function search_assignment_operator(from_def,to_def:Tdef):Tprocdef;
  1678. var
  1679. sym : Tprocsym;
  1680. hashedid : THashedIDString;
  1681. curreq,
  1682. besteq : tequaltype;
  1683. currpd,
  1684. bestpd : tprocdef;
  1685. stackitem : psymtablestackitem;
  1686. begin
  1687. hashedid.id:='assign';
  1688. besteq:=te_incompatible;
  1689. bestpd:=nil;
  1690. stackitem:=symtablestack.stack;
  1691. while assigned(stackitem) do
  1692. begin
  1693. sym:=Tprocsym(stackitem^.symtable.FindWithHash(hashedid));
  1694. if sym<>nil then
  1695. begin
  1696. if sym.typ<>procsym then
  1697. internalerror(200402031);
  1698. { if the source type is an alias then this is only the second choice,
  1699. if you mess with this code, check tw4093 }
  1700. currpd:=sym.find_procdef_assignment_operator(from_def,to_def,curreq);
  1701. if curreq>besteq then
  1702. begin
  1703. besteq:=curreq;
  1704. bestpd:=currpd;
  1705. if (besteq=te_exact) then
  1706. break;
  1707. end;
  1708. end;
  1709. stackitem:=stackitem^.next;
  1710. end;
  1711. result:=bestpd;
  1712. end;
  1713. function search_enumerator_operator(type_def:Tdef): Tprocdef;
  1714. var
  1715. sym : Tprocsym;
  1716. hashedid : THashedIDString;
  1717. curreq,
  1718. besteq : tequaltype;
  1719. currpd,
  1720. bestpd : tprocdef;
  1721. stackitem : psymtablestackitem;
  1722. begin
  1723. hashedid.id:='enumerator';
  1724. besteq:=te_incompatible;
  1725. bestpd:=nil;
  1726. stackitem:=symtablestack.stack;
  1727. while assigned(stackitem) do
  1728. begin
  1729. sym:=Tprocsym(stackitem^.symtable.FindWithHash(hashedid));
  1730. if sym<>nil then
  1731. begin
  1732. if sym.typ<>procsym then
  1733. internalerror(200910241);
  1734. { if the source type is an alias then this is only the second choice,
  1735. if you mess with this code, check tw4093 }
  1736. currpd:=sym.find_procdef_enumerator_operator(type_def,curreq);
  1737. if curreq>besteq then
  1738. begin
  1739. besteq:=curreq;
  1740. bestpd:=currpd;
  1741. if (besteq=te_exact) then
  1742. break;
  1743. end;
  1744. end;
  1745. stackitem:=stackitem^.next;
  1746. end;
  1747. result:=bestpd;
  1748. end;
  1749. function search_system_type(const s: TIDString): ttypesym;
  1750. var
  1751. sym : tsym;
  1752. begin
  1753. sym:=tsym(systemunit.Find(s));
  1754. if not assigned(sym) or
  1755. (sym.typ<>typesym) then
  1756. cgmessage1(cg_f_unknown_system_type,s);
  1757. result:=ttypesym(sym);
  1758. end;
  1759. function search_named_unit_globaltype(const unitname, typename: TIDString): ttypesym;
  1760. var
  1761. contextobjdef : tobjectdef;
  1762. stackitem : psymtablestackitem;
  1763. srsymtable: tsymtable;
  1764. sym: tsym;
  1765. begin
  1766. if searchsym_in_named_module(unitname,typename,sym,srsymtable) and
  1767. (sym.typ=typesym) then
  1768. begin
  1769. result:=ttypesym(sym);
  1770. exit;
  1771. end
  1772. else
  1773. begin
  1774. cgmessage2(cg_f_unknown_type_in_unit,typename,unitname);
  1775. result:=nil;
  1776. end;
  1777. end;
  1778. function search_class_member(pd : tobjectdef;const s : string):tsym;
  1779. { searches n in symtable of pd and all anchestors }
  1780. var
  1781. hashedid : THashedIDString;
  1782. srsym : tsym;
  1783. begin
  1784. hashedid.id:=s;
  1785. while assigned(pd) do
  1786. begin
  1787. srsym:=tsym(pd.symtable.FindWithHash(hashedid));
  1788. if assigned(srsym) then
  1789. begin
  1790. search_class_member:=srsym;
  1791. exit;
  1792. end;
  1793. pd:=pd.childof;
  1794. end;
  1795. search_class_member:=nil;
  1796. end;
  1797. function search_macro(const s : string):tsym;
  1798. var
  1799. stackitem : psymtablestackitem;
  1800. hashedid : THashedIDString;
  1801. srsym : tsym;
  1802. begin
  1803. hashedid.id:=s;
  1804. { First search the localmacrosymtable before searching the
  1805. global macrosymtables from the units }
  1806. if assigned(current_module) then
  1807. begin
  1808. srsym:=tsym(current_module.localmacrosymtable.FindWithHash(hashedid));
  1809. if assigned(srsym) then
  1810. begin
  1811. result:= srsym;
  1812. exit;
  1813. end;
  1814. end;
  1815. stackitem:=macrosymtablestack.stack;
  1816. while assigned(stackitem) do
  1817. begin
  1818. srsym:=tsym(stackitem^.symtable.FindWithHash(hashedid));
  1819. if assigned(srsym) then
  1820. begin
  1821. result:= srsym;
  1822. exit;
  1823. end;
  1824. stackitem:=stackitem^.next;
  1825. end;
  1826. result:= nil;
  1827. end;
  1828. function defined_macro(const s : string):boolean;
  1829. var
  1830. mac: tmacro;
  1831. begin
  1832. mac:=tmacro(search_macro(s));
  1833. if assigned(mac) then
  1834. begin
  1835. mac.is_used:=true;
  1836. defined_macro:=mac.defined;
  1837. end
  1838. else
  1839. defined_macro:=false;
  1840. end;
  1841. {****************************************************************************
  1842. Object Helpers
  1843. ****************************************************************************}
  1844. function search_default_property(pd : tobjectdef) : tpropertysym;
  1845. { returns the default property of a class, searches also anchestors }
  1846. var
  1847. _defaultprop : tpropertysym;
  1848. begin
  1849. _defaultprop:=nil;
  1850. while assigned(pd) do
  1851. begin
  1852. pd.symtable.SymList.ForEachCall(@tstoredsymtable(pd.symtable).testfordefaultproperty,@_defaultprop);
  1853. if assigned(_defaultprop) then
  1854. break;
  1855. pd:=pd.childof;
  1856. end;
  1857. search_default_property:=_defaultprop;
  1858. end;
  1859. {****************************************************************************
  1860. Macro Helpers
  1861. ****************************************************************************}
  1862. procedure def_system_macro(const name : string);
  1863. var
  1864. mac : tmacro;
  1865. s: string;
  1866. begin
  1867. if name = '' then
  1868. internalerror(2004121202);
  1869. s:= upper(name);
  1870. mac:=tmacro(search_macro(s));
  1871. if not assigned(mac) then
  1872. begin
  1873. mac:=tmacro.create(s);
  1874. if assigned(current_module) then
  1875. current_module.localmacrosymtable.insert(mac)
  1876. else
  1877. initialmacrosymtable.insert(mac);
  1878. end;
  1879. if not mac.defined then
  1880. Message1(parser_c_macro_defined,mac.name);
  1881. mac.defined:=true;
  1882. end;
  1883. procedure set_system_macro(const name, value : string);
  1884. var
  1885. mac : tmacro;
  1886. s: string;
  1887. begin
  1888. if name = '' then
  1889. internalerror(2004121203);
  1890. s:= upper(name);
  1891. mac:=tmacro(search_macro(s));
  1892. if not assigned(mac) then
  1893. begin
  1894. mac:=tmacro.create(s);
  1895. if assigned(current_module) then
  1896. current_module.localmacrosymtable.insert(mac)
  1897. else
  1898. initialmacrosymtable.insert(mac);
  1899. end
  1900. else
  1901. begin
  1902. mac.is_compiler_var:=false;
  1903. if assigned(mac.buftext) then
  1904. freemem(mac.buftext,mac.buflen);
  1905. end;
  1906. Message2(parser_c_macro_set_to,mac.name,value);
  1907. mac.buflen:=length(value);
  1908. getmem(mac.buftext,mac.buflen);
  1909. move(value[1],mac.buftext^,mac.buflen);
  1910. mac.defined:=true;
  1911. end;
  1912. procedure set_system_compvar(const name, value : string);
  1913. var
  1914. mac : tmacro;
  1915. s: string;
  1916. begin
  1917. if name = '' then
  1918. internalerror(2004121204);
  1919. s:= upper(name);
  1920. mac:=tmacro(search_macro(s));
  1921. if not assigned(mac) then
  1922. begin
  1923. mac:=tmacro.create(s);
  1924. mac.is_compiler_var:=true;
  1925. if assigned(current_module) then
  1926. current_module.localmacrosymtable.insert(mac)
  1927. else
  1928. initialmacrosymtable.insert(mac);
  1929. end
  1930. else
  1931. begin
  1932. mac.is_compiler_var:=true;
  1933. if assigned(mac.buftext) then
  1934. freemem(mac.buftext,mac.buflen);
  1935. end;
  1936. Message2(parser_c_macro_set_to,mac.name,value);
  1937. mac.buflen:=length(value);
  1938. getmem(mac.buftext,mac.buflen);
  1939. move(value[1],mac.buftext^,mac.buflen);
  1940. mac.defined:=true;
  1941. end;
  1942. procedure undef_system_macro(const name : string);
  1943. var
  1944. mac : tmacro;
  1945. s: string;
  1946. begin
  1947. if name = '' then
  1948. internalerror(2004121205);
  1949. s:= upper(name);
  1950. mac:=tmacro(search_macro(s));
  1951. if not assigned(mac) then
  1952. {If not found, then it's already undefined.}
  1953. else
  1954. begin
  1955. if mac.defined then
  1956. Message1(parser_c_macro_undefined,mac.name);
  1957. mac.defined:=false;
  1958. mac.is_compiler_var:=false;
  1959. { delete old definition }
  1960. if assigned(mac.buftext) then
  1961. begin
  1962. freemem(mac.buftext,mac.buflen);
  1963. mac.buftext:=nil;
  1964. end;
  1965. end;
  1966. end;
  1967. {$ifdef UNITALIASES}
  1968. {****************************************************************************
  1969. TUNIT_ALIAS
  1970. ****************************************************************************}
  1971. constructor tunit_alias.create(const n:string);
  1972. var
  1973. i : longint;
  1974. begin
  1975. i:=pos('=',n);
  1976. if i=0 then
  1977. fail;
  1978. inherited createname(Copy(n,1,i-1));
  1979. newname:=stringdup(Copy(n,i+1,255));
  1980. end;
  1981. destructor tunit_alias.destroy;
  1982. begin
  1983. stringdispose(newname);
  1984. inherited destroy;
  1985. end;
  1986. procedure addunitalias(const n:string);
  1987. begin
  1988. unitaliases^.insert(tunit_alias,init(Upper(n))));
  1989. end;
  1990. function getunitalias(const n:string):string;
  1991. var
  1992. p : punit_alias;
  1993. begin
  1994. p:=punit_alias(unitaliases^.Find(Upper(n)));
  1995. if assigned(p) then
  1996. getunitalias:=punit_alias(p).newname^
  1997. else
  1998. getunitalias:=n;
  1999. end;
  2000. {$endif UNITALIASES}
  2001. {****************************************************************************
  2002. Init/Done Symtable
  2003. ****************************************************************************}
  2004. procedure InitSymtable;
  2005. begin
  2006. { Reset symbolstack }
  2007. symtablestack:=nil;
  2008. systemunit:=nil;
  2009. { create error syms and def }
  2010. generrorsym:=terrorsym.create;
  2011. generrordef:=terrordef.create;
  2012. { macros }
  2013. initialmacrosymtable:=tmacrosymtable.create(false);
  2014. macrosymtablestack:=TSymtablestack.create;
  2015. macrosymtablestack.push(initialmacrosymtable);
  2016. {$ifdef UNITALIASES}
  2017. { unit aliases }
  2018. unitaliases:=TFPHashObjectList.create;
  2019. {$endif}
  2020. { set some global vars to nil, might be important for the ide }
  2021. class_tobject:=nil;
  2022. interface_iunknown:=nil;
  2023. rec_tguid:=nil;
  2024. dupnr:=0;
  2025. end;
  2026. procedure DoneSymtable;
  2027. begin
  2028. generrorsym.owner:=nil;
  2029. generrorsym.free;
  2030. generrordef.owner:=nil;
  2031. generrordef.free;
  2032. initialmacrosymtable.free;
  2033. macrosymtablestack.free;
  2034. {$ifdef UNITALIASES}
  2035. unitaliases.free;
  2036. {$endif}
  2037. end;
  2038. end.