symtable.pas 77 KB

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