symtable.pas 86 KB

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