symtable.pas 77 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266
  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. ) then
  557. MessagePos2(tsym(sym).fileinfo,sym_h_local_symbol_not_used,SymTypeName[tsym(sym).typ],tsym(sym).prettyname);
  558. end;
  559. end;
  560. end;
  561. procedure TStoredSymtable.TestPrivate(sym:TObject;arg:pointer);
  562. begin
  563. if tsym(sym).visibility=vis_private then
  564. varsymbolused(sym,arg);
  565. end;
  566. procedure TStoredSymtable.objectprivatesymbolused(sym:TObject;arg:pointer);
  567. begin
  568. {
  569. Don't test simple object aliases PM
  570. }
  571. if (tsym(sym).typ=typesym) and
  572. (ttypesym(sym).typedef.typ=objectdef) and
  573. (ttypesym(sym).typedef.typesym=tsym(sym)) then
  574. tobjectdef(ttypesym(sym).typedef).symtable.SymList.ForEachCall(@TestPrivate,nil);
  575. end;
  576. procedure tstoredsymtable.testfordefaultproperty(sym:TObject;arg:pointer);
  577. begin
  578. if (tsym(sym).typ=propertysym) and
  579. (ppo_defaultproperty in tpropertysym(sym).propoptions) then
  580. ppointer(arg)^:=sym;
  581. end;
  582. {***********************************************
  583. Process all entries
  584. ***********************************************}
  585. procedure Tstoredsymtable.reset_all_defs;
  586. var
  587. i : longint;
  588. def : tstoreddef;
  589. begin
  590. for i:=0 to DefList.Count-1 do
  591. begin
  592. def:=tstoreddef(DefList[i]);
  593. def.reset;
  594. end;
  595. end;
  596. { checks, if all procsyms and methods are defined }
  597. procedure tstoredsymtable.check_forwards;
  598. begin
  599. SymList.ForEachCall(@check_forward,nil);
  600. end;
  601. procedure tstoredsymtable.checklabels;
  602. begin
  603. SymList.ForEachCall(@labeldefined,nil);
  604. end;
  605. procedure tstoredsymtable.allsymbolsused;
  606. begin
  607. SymList.ForEachCall(@varsymbolused,nil);
  608. end;
  609. procedure tstoredsymtable.allprivatesused;
  610. begin
  611. SymList.ForEachCall(@objectprivatesymbolused,nil);
  612. end;
  613. procedure TStoredSymtable._needs_init_final(sym:TObject;arg:pointer);
  614. begin
  615. if b_needs_init_final then
  616. exit;
  617. case tsym(sym).typ of
  618. fieldvarsym,
  619. staticvarsym,
  620. localvarsym,
  621. paravarsym :
  622. begin
  623. if not(is_class(tabstractvarsym(sym).vardef)) and
  624. tstoreddef(tabstractvarsym(sym).vardef).needs_inittable then
  625. b_needs_init_final:=true;
  626. end;
  627. end;
  628. end;
  629. { returns true, if p contains data which needs init/final code }
  630. function tstoredsymtable.needs_init_final : boolean;
  631. begin
  632. b_needs_init_final:=false;
  633. SymList.ForEachCall(@_needs_init_final,nil);
  634. needs_init_final:=b_needs_init_final;
  635. end;
  636. {****************************************************************************
  637. TAbstractRecordSymtable
  638. ****************************************************************************}
  639. constructor tabstractrecordsymtable.create(const n:string;usealign:shortint);
  640. begin
  641. inherited create(n);
  642. _datasize:=0;
  643. databitsize:=0;
  644. recordalignment:=1;
  645. usefieldalignment:=usealign;
  646. padalignment:=1;
  647. { recordalign C_alignment means C record packing, that starts
  648. with an alignment of 1 }
  649. case usealign of
  650. C_alignment,
  651. bit_alignment:
  652. fieldalignment:=1
  653. else
  654. fieldalignment:=usealign;
  655. end;
  656. end;
  657. procedure tabstractrecordsymtable.ppuload(ppufile:tcompilerppufile);
  658. begin
  659. inherited ppuload(ppufile);
  660. end;
  661. procedure tabstractrecordsymtable.ppuwrite(ppufile:tcompilerppufile);
  662. var
  663. oldtyp : byte;
  664. begin
  665. oldtyp:=ppufile.entrytyp;
  666. ppufile.entrytyp:=subentryid;
  667. inherited ppuwrite(ppufile);
  668. ppufile.entrytyp:=oldtyp;
  669. end;
  670. function field2recordalignment(fieldoffs, fieldalign: aint): aint;
  671. begin
  672. { optimal alignment of the record when declaring a variable of this }
  673. { type is independent of the packrecords setting }
  674. if (fieldoffs mod fieldalign) = 0 then
  675. result:=fieldalign
  676. else if (fieldalign >= 16) and
  677. ((fieldoffs mod 16) = 0) and
  678. ((fieldalign mod 16) = 0) then
  679. result:=16
  680. else if (fieldalign >= 8) and
  681. ((fieldoffs mod 8) = 0) and
  682. ((fieldalign mod 8) = 0) then
  683. result:=8
  684. else if (fieldalign >= 4) and
  685. ((fieldoffs mod 4) = 0) and
  686. ((fieldalign mod 4) = 0) then
  687. result:=4
  688. else if (fieldalign >= 2) and
  689. ((fieldoffs mod 2) = 0) and
  690. ((fieldalign mod 2) = 0) then
  691. result:=2
  692. else
  693. result:=1;
  694. end;
  695. procedure tabstractrecordsymtable.alignrecord(fieldoffset:aint;varalign:shortint);
  696. var
  697. varalignrecord: shortint;
  698. begin
  699. if (usefieldalignment=C_alignment) then
  700. varalignrecord:=used_align(varalign,current_settings.alignment.recordalignmin,current_settings.alignment.maxCrecordalign)
  701. else
  702. varalignrecord:=field2recordalignment(fieldoffset,varalign);
  703. recordalignment:=max(recordalignment,varalignrecord);
  704. end;
  705. procedure tabstractrecordsymtable.addfield(sym:tfieldvarsym;vis:tvisibility);
  706. var
  707. l : aint;
  708. varalignfield,
  709. varalign : shortint;
  710. vardef : tdef;
  711. begin
  712. if (sym.owner<>self) then
  713. internalerror(200602031);
  714. if sym.fieldoffset<>-1 then
  715. internalerror(200602032);
  716. { set visibility for the symbol }
  717. sym.visibility:=vis;
  718. { this symbol can't be loaded to a register }
  719. sym.varregable:=vr_none;
  720. { Calculate field offset }
  721. l:=sym.getsize;
  722. vardef:=sym.vardef;
  723. varalign:=vardef.alignment;
  724. if (usefieldalignment=bit_alignment) then
  725. begin
  726. { bitpacking only happens for ordinals, the rest is aligned at }
  727. { 1 byte (compatible with GPC/GCC) }
  728. if is_ordinal(vardef) then
  729. begin
  730. sym.fieldoffset:=databitsize;
  731. l:=sym.getpackedbitsize;
  732. end
  733. else
  734. begin
  735. databitsize:=_datasize*8;
  736. sym.fieldoffset:=databitsize;
  737. if (l>high(aint) div 8) then
  738. Message(sym_e_segment_too_large);
  739. l:=l*8;
  740. end;
  741. if varalign=0 then
  742. varalign:=size_2_align(l);
  743. recordalignment:=max(recordalignment,field2recordalignment(databitsize mod 8,varalign));
  744. { bit packed records are limited to high(aint) bits }
  745. { instead of bytes to avoid double precision }
  746. { arithmetic in offset calculations }
  747. if int64(l)>high(aint)-sym.fieldoffset then
  748. begin
  749. Message(sym_e_segment_too_large);
  750. _datasize:=high(aint);
  751. databitsize:=high(aint);
  752. end
  753. else
  754. begin
  755. databitsize:=sym.fieldoffset+l;
  756. _datasize:=(databitsize+7) div 8;
  757. end;
  758. { rest is not applicable }
  759. exit;
  760. end;
  761. { Calc the alignment size for C style records }
  762. if (usefieldalignment=C_alignment) then
  763. begin
  764. if (varalign>4) and
  765. ((varalign mod 4)<>0) and
  766. (vardef.typ=arraydef) then
  767. Message1(sym_w_wrong_C_pack,vardef.typename);
  768. if varalign=0 then
  769. varalign:=l;
  770. if (fieldalignment<current_settings.alignment.maxCrecordalign) then
  771. begin
  772. if (varalign>16) and (fieldalignment<32) then
  773. fieldalignment:=32
  774. else if (varalign>12) and (fieldalignment<16) then
  775. fieldalignment:=16
  776. { 12 is needed for long double }
  777. else if (varalign>8) and (fieldalignment<12) then
  778. fieldalignment:=12
  779. else if (varalign>4) and (fieldalignment<8) then
  780. fieldalignment:=8
  781. else if (varalign>2) and (fieldalignment<4) then
  782. fieldalignment:=4
  783. else if (varalign>1) and (fieldalignment<2) then
  784. fieldalignment:=2;
  785. end;
  786. fieldalignment:=min(fieldalignment,current_settings.alignment.maxCrecordalign);
  787. end;
  788. if varalign=0 then
  789. varalign:=size_2_align(l);
  790. varalignfield:=used_align(varalign,current_settings.alignment.recordalignmin,fieldalignment);
  791. sym.fieldoffset:=align(_datasize,varalignfield);
  792. if l>high(aint)-sym.fieldoffset then
  793. begin
  794. Message(sym_e_segment_too_large);
  795. _datasize:=high(aint);
  796. end
  797. else
  798. _datasize:=sym.fieldoffset+l;
  799. { Calc alignment needed for this record }
  800. alignrecord(sym.fieldoffset,varalign);
  801. end;
  802. procedure tabstractrecordsymtable.addalignmentpadding;
  803. begin
  804. { make the record size aligned correctly so it can be
  805. used as elements in an array. For C records we
  806. use the fieldalignment, because that is updated with the
  807. used alignment. }
  808. if (padalignment = 1) then
  809. case usefieldalignment of
  810. C_alignment:
  811. padalignment:=fieldalignment;
  812. { bitpacked }
  813. bit_alignment:
  814. padalignment:=1;
  815. { default/no packrecords specified }
  816. 0:
  817. padalignment:=recordalignment
  818. { specific packrecords setting -> use as upper limit }
  819. else
  820. padalignment:=min(recordalignment,usefieldalignment);
  821. end;
  822. _datasize:=align(_datasize,padalignment);
  823. end;
  824. procedure tabstractrecordsymtable.insertdef(def:TDefEntry);
  825. begin
  826. { Enums must also be available outside the record scope,
  827. insert in the owner of this symtable }
  828. if def.typ=enumdef then
  829. defowner.owner.insertdef(def)
  830. else
  831. inherited insertdef(def);
  832. end;
  833. function tabstractrecordsymtable.is_packed: boolean;
  834. begin
  835. result:=usefieldalignment=bit_alignment;
  836. end;
  837. procedure tabstractrecordsymtable.setdatasize(val: aint);
  838. begin
  839. _datasize:=val;
  840. if (usefieldalignment=bit_alignment) then
  841. { can overflow in non bitpacked records }
  842. databitsize:=val*8;
  843. end;
  844. {****************************************************************************
  845. TRecordSymtable
  846. ****************************************************************************}
  847. constructor trecordsymtable.create(usealign:shortint);
  848. begin
  849. inherited create('',usealign);
  850. symtabletype:=recordsymtable;
  851. end;
  852. { this procedure is reserved for inserting case variant into
  853. a record symtable }
  854. { the offset is the location of the start of the variant
  855. and datasize and dataalignment corresponds to
  856. the complete size (see code in pdecl unit) PM }
  857. procedure trecordsymtable.insertunionst(unionst : trecordsymtable;offset : longint);
  858. var
  859. sym : tsym;
  860. def : tdef;
  861. i : integer;
  862. varalignrecord,varalign,
  863. storesize,storealign : aint;
  864. bitsize: aint;
  865. begin
  866. storesize:=_datasize;
  867. storealign:=fieldalignment;
  868. _datasize:=offset;
  869. if (usefieldalignment=bit_alignment) then
  870. databitsize:=offset*8;
  871. { We move the ownership of the defs and symbols to the new recordsymtable.
  872. The old unionsymtable keeps the references, but doesn't own the
  873. objects anymore }
  874. unionst.DefList.OwnsObjects:=false;
  875. unionst.SymList.OwnsObjects:=false;
  876. { copy symbols }
  877. for i:=0 to unionst.SymList.Count-1 do
  878. begin
  879. sym:=TSym(unionst.SymList[i]);
  880. if sym.typ<>fieldvarsym then
  881. internalerror(200601272);
  882. { add to this record symtable }
  883. // unionst.SymList.List.List^[i].Data:=nil;
  884. sym.ChangeOwner(self);
  885. varalign:=tfieldvarsym(sym).vardef.alignment;
  886. if varalign=0 then
  887. varalign:=size_2_align(tfieldvarsym(sym).getsize);
  888. { retrieve size }
  889. if (usefieldalignment=bit_alignment) then
  890. begin
  891. { bit packed records are limited to high(aint) bits }
  892. { instead of bytes to avoid double precision }
  893. { arithmetic in offset calculations }
  894. if is_ordinal(tfieldvarsym(sym).vardef) then
  895. bitsize:=tfieldvarsym(sym).getpackedbitsize
  896. else
  897. begin
  898. bitsize:=tfieldvarsym(sym).getsize;
  899. if (bitsize>high(aint) div 8) then
  900. Message(sym_e_segment_too_large);
  901. bitsize:=bitsize*8;
  902. end;
  903. if bitsize>high(aint)-databitsize then
  904. begin
  905. Message(sym_e_segment_too_large);
  906. _datasize:=high(aint);
  907. databitsize:=high(aint);
  908. end
  909. else
  910. begin
  911. databitsize:=tfieldvarsym(sym).fieldoffset+offset*8;
  912. _datasize:=(databitsize+7) div 8;
  913. end;
  914. tfieldvarsym(sym).fieldoffset:=databitsize;
  915. varalignrecord:=field2recordalignment(tfieldvarsym(sym).fieldoffset div 8,varalign);
  916. end
  917. else
  918. begin
  919. if tfieldvarsym(sym).getsize>high(aint)-_datasize then
  920. begin
  921. Message(sym_e_segment_too_large);
  922. _datasize:=high(aint);
  923. end
  924. else
  925. _datasize:=tfieldvarsym(sym).fieldoffset+offset;
  926. { update address }
  927. tfieldvarsym(sym).fieldoffset:=_datasize;
  928. varalignrecord:=field2recordalignment(tfieldvarsym(sym).fieldoffset,varalign);
  929. end;
  930. { update alignment of this record }
  931. if (usefieldalignment<>C_alignment) then
  932. recordalignment:=max(recordalignment,varalignrecord);
  933. end;
  934. { update alignment for C records }
  935. if (usefieldalignment=C_alignment) then
  936. recordalignment:=max(recordalignment,unionst.recordalignment);
  937. { Register defs in the new record symtable }
  938. for i:=0 to unionst.DefList.Count-1 do
  939. begin
  940. def:=TDef(unionst.DefList[i]);
  941. def.ChangeOwner(self);
  942. end;
  943. _datasize:=storesize;
  944. fieldalignment:=storealign;
  945. end;
  946. {****************************************************************************
  947. TObjectSymtable
  948. ****************************************************************************}
  949. constructor tObjectSymtable.create(adefowner:tdef;const n:string;usealign:shortint);
  950. begin
  951. inherited create(n,usealign);
  952. symtabletype:=ObjectSymtable;
  953. defowner:=adefowner;
  954. end;
  955. function tObjectSymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  956. var
  957. hsym : tsym;
  958. begin
  959. result:=false;
  960. if not assigned(defowner) then
  961. internalerror(200602061);
  962. { procsym and propertysym have special code
  963. to override values in inherited classes. For other
  964. symbols check for duplicates }
  965. if not(sym.typ in [procsym,propertysym]) then
  966. begin
  967. { but private ids can be reused }
  968. hsym:=search_class_member(tobjectdef(defowner),hashedid.id);
  969. if assigned(hsym) and
  970. (
  971. (
  972. not(m_delphi in current_settings.modeswitches) and
  973. is_visible_for_object(hsym,tobjectdef(defowner))
  974. ) or
  975. (
  976. { In Delphi, you can repeat members of a parent class. You can't }
  977. { do this for objects however, and you (obviouly) can't }
  978. { declare two fields with the same name in a single class }
  979. (m_delphi in current_settings.modeswitches) and
  980. (
  981. is_object(tdef(defowner)) or
  982. (hsym.owner = self)
  983. )
  984. )
  985. ) then
  986. begin
  987. DuplicateSym(hashedid,sym,hsym);
  988. result:=true;
  989. end;
  990. end
  991. else
  992. begin
  993. if not(m_duplicate_names in current_settings.modeswitches) then
  994. result:=inherited checkduplicate(hashedid,sym);
  995. end;
  996. end;
  997. {****************************************************************************
  998. TAbstractLocalSymtable
  999. ****************************************************************************}
  1000. procedure tabstractlocalsymtable.ppuwrite(ppufile:tcompilerppufile);
  1001. var
  1002. oldtyp : byte;
  1003. begin
  1004. oldtyp:=ppufile.entrytyp;
  1005. ppufile.entrytyp:=subentryid;
  1006. { write definitions }
  1007. writedefs(ppufile);
  1008. { write symbols }
  1009. writesyms(ppufile);
  1010. ppufile.entrytyp:=oldtyp;
  1011. end;
  1012. function tabstractlocalsymtable.count_locals:longint;
  1013. var
  1014. i : longint;
  1015. sym : tsym;
  1016. begin
  1017. result:=0;
  1018. for i:=0 to SymList.Count-1 do
  1019. begin
  1020. sym:=tsym(SymList[i]);
  1021. { Count only varsyms, but ignore the funcretsym }
  1022. if (tsym(sym).typ in [localvarsym,paravarsym]) and
  1023. (tsym(sym)<>current_procinfo.procdef.funcretsym) and
  1024. (not(vo_is_parentfp in tabstractvarsym(sym).varoptions) or
  1025. (tstoredsym(sym).refs>0)) then
  1026. inc(result);
  1027. end;
  1028. end;
  1029. {****************************************************************************
  1030. TLocalSymtable
  1031. ****************************************************************************}
  1032. constructor tlocalsymtable.create(adefowner:tdef;level:byte);
  1033. begin
  1034. inherited create('');
  1035. defowner:=adefowner;
  1036. symtabletype:=localsymtable;
  1037. symtablelevel:=level;
  1038. end;
  1039. function tlocalsymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  1040. var
  1041. hsym : tsym;
  1042. begin
  1043. if not assigned(defowner) or
  1044. (defowner.typ<>procdef) then
  1045. internalerror(200602042);
  1046. result:=false;
  1047. hsym:=tsym(FindWithHash(hashedid));
  1048. if assigned(hsym) then
  1049. begin
  1050. { a local and the function can have the same
  1051. name in TP and Delphi, but RESULT not }
  1052. if (m_duplicate_names in current_settings.modeswitches) and
  1053. (hsym.typ in [absolutevarsym,localvarsym]) and
  1054. (vo_is_funcret in tabstractvarsym(hsym).varoptions) and
  1055. not((m_result in current_settings.modeswitches) and
  1056. (vo_is_result in tabstractvarsym(hsym).varoptions)) then
  1057. HideSym(hsym)
  1058. else
  1059. DuplicateSym(hashedid,sym,hsym);
  1060. result:=true;
  1061. exit;
  1062. end;
  1063. { check also parasymtable, this needs to be done here becuase
  1064. of the special situation with the funcret sym that needs to be
  1065. hidden for tp and delphi modes }
  1066. hsym:=tsym(tabstractprocdef(defowner).parast.FindWithHash(hashedid));
  1067. if assigned(hsym) then
  1068. begin
  1069. { a local and the function can have the same
  1070. name in TP and Delphi, but RESULT not }
  1071. if (m_duplicate_names in current_settings.modeswitches) and
  1072. (sym.typ in [absolutevarsym,localvarsym]) and
  1073. (vo_is_funcret in tabstractvarsym(sym).varoptions) and
  1074. not((m_result in current_settings.modeswitches) and
  1075. (vo_is_result in tabstractvarsym(sym).varoptions)) then
  1076. Hidesym(sym)
  1077. else
  1078. DuplicateSym(hashedid,sym,hsym);
  1079. result:=true;
  1080. exit;
  1081. end;
  1082. { check ObjectSymtable, skip this for funcret sym because
  1083. that will always be positive because it has the same name
  1084. as the procsym }
  1085. if not is_funcret_sym(sym) and
  1086. (defowner.typ=procdef) and
  1087. assigned(tprocdef(defowner)._class) and
  1088. (tprocdef(defowner).owner.defowner=tprocdef(defowner)._class) and
  1089. (
  1090. not(m_delphi in current_settings.modeswitches) or
  1091. is_object(tprocdef(defowner)._class)
  1092. ) then
  1093. result:=tprocdef(defowner)._class.symtable.checkduplicate(hashedid,sym);
  1094. end;
  1095. {****************************************************************************
  1096. TParaSymtable
  1097. ****************************************************************************}
  1098. constructor tparasymtable.create(adefowner:tdef;level:byte);
  1099. begin
  1100. inherited create('');
  1101. defowner:=adefowner;
  1102. symtabletype:=parasymtable;
  1103. symtablelevel:=level;
  1104. end;
  1105. function tparasymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  1106. begin
  1107. result:=inherited checkduplicate(hashedid,sym);
  1108. if result then
  1109. exit;
  1110. if not(m_duplicate_names in current_settings.modeswitches) and
  1111. (defowner.typ=procdef) and
  1112. assigned(tprocdef(defowner)._class) and
  1113. (tprocdef(defowner).owner.defowner=tprocdef(defowner)._class) and
  1114. (
  1115. not(m_delphi in current_settings.modeswitches) or
  1116. is_object(tprocdef(defowner)._class)
  1117. ) then
  1118. result:=tprocdef(defowner)._class.symtable.checkduplicate(hashedid,sym);
  1119. end;
  1120. {****************************************************************************
  1121. TAbstractUniTSymtable
  1122. ****************************************************************************}
  1123. constructor tabstractuniTSymtable.create(const n : string;id:word);
  1124. begin
  1125. inherited create(n);
  1126. moduleid:=id;
  1127. end;
  1128. function tabstractuniTSymtable.iscurrentunit:boolean;
  1129. begin
  1130. result:=assigned(current_module) and
  1131. (
  1132. (current_module.globalsymtable=self) or
  1133. (current_module.localsymtable=self)
  1134. );
  1135. end;
  1136. {****************************************************************************
  1137. TStaticSymtable
  1138. ****************************************************************************}
  1139. constructor tstaticsymtable.create(const n : string;id:word);
  1140. begin
  1141. inherited create(n,id);
  1142. symtabletype:=staticsymtable;
  1143. symtablelevel:=main_program_level;
  1144. end;
  1145. procedure tstaticsymtable.ppuload(ppufile:tcompilerppufile);
  1146. begin
  1147. inherited ppuload(ppufile);
  1148. { now we can deref the syms and defs }
  1149. deref;
  1150. end;
  1151. procedure tstaticsymtable.ppuwrite(ppufile:tcompilerppufile);
  1152. begin
  1153. inherited ppuwrite(ppufile);
  1154. end;
  1155. function tstaticsymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  1156. var
  1157. hsym : tsym;
  1158. begin
  1159. result:=false;
  1160. hsym:=tsym(FindWithHash(hashedid));
  1161. if assigned(hsym) then
  1162. begin
  1163. { Delphi (contrary to TP) you can have a symbol with the same name as the
  1164. unit, the unit can then not be accessed anymore using
  1165. <unit>.<id>, so we can hide the symbol }
  1166. if (m_delphi in current_settings.modeswitches) and
  1167. (hsym.typ=symconst.unitsym) then
  1168. HideSym(hsym)
  1169. else
  1170. DuplicateSym(hashedid,sym,hsym);
  1171. result:=true;
  1172. exit;
  1173. end;
  1174. if (current_module.localsymtable=self) and
  1175. assigned(current_module.globalsymtable) then
  1176. result:=tglobalsymtable(current_module.globalsymtable).checkduplicate(hashedid,sym);
  1177. end;
  1178. {****************************************************************************
  1179. TGlobalSymtable
  1180. ****************************************************************************}
  1181. constructor tglobalsymtable.create(const n : string;id:word);
  1182. begin
  1183. inherited create(n,id);
  1184. symtabletype:=globalsymtable;
  1185. symtablelevel:=main_program_level;
  1186. end;
  1187. procedure tglobalsymtable.ppuload(ppufile:tcompilerppufile);
  1188. begin
  1189. inherited ppuload(ppufile);
  1190. { now we can deref the syms and defs }
  1191. deref;
  1192. end;
  1193. procedure tglobalsymtable.ppuwrite(ppufile:tcompilerppufile);
  1194. begin
  1195. { write the symtable entries }
  1196. inherited ppuwrite(ppufile);
  1197. end;
  1198. function tglobalsymtable.checkduplicate(var hashedid:THashedIDString;sym:TSymEntry):boolean;
  1199. var
  1200. hsym : tsym;
  1201. begin
  1202. result:=false;
  1203. hsym:=tsym(FindWithHash(hashedid));
  1204. if assigned(hsym) then
  1205. begin
  1206. { Delphi (contrary to TP) you can have a symbol with the same name as the
  1207. unit, the unit can then not be accessed anymore using
  1208. <unit>.<id>, so we can hide the symbol }
  1209. if (m_delphi in current_settings.modeswitches) and
  1210. (hsym.typ=symconst.unitsym) then
  1211. HideSym(hsym)
  1212. else
  1213. DuplicateSym(hashedid,sym,hsym);
  1214. result:=true;
  1215. exit;
  1216. end;
  1217. end;
  1218. {****************************************************************************
  1219. TWITHSYMTABLE
  1220. ****************************************************************************}
  1221. constructor twithsymtable.create(aowner:tdef;ASymList:TFPHashObjectList;refnode:tobject{tnode});
  1222. begin
  1223. inherited create('');
  1224. symtabletype:=withsymtable;
  1225. withrefnode:=refnode;
  1226. { Replace SymList with the passed symlist }
  1227. SymList.free;
  1228. SymList:=ASymList;
  1229. defowner:=aowner;
  1230. end;
  1231. destructor twithsymtable.destroy;
  1232. begin
  1233. withrefnode.free;
  1234. { Disable SymList because we don't Own it }
  1235. SymList:=nil;
  1236. inherited destroy;
  1237. end;
  1238. procedure twithsymtable.clear;
  1239. begin
  1240. { remove no entry from a withsymtable as it is only a pointer to the
  1241. recorddef or objectdef symtable }
  1242. end;
  1243. procedure twithsymtable.insertdef(def:TDefEntry);
  1244. begin
  1245. { Definitions can't be registered in the withsymtable
  1246. because the withsymtable is removed after the with block.
  1247. We can't easily solve it here because the next symtable in the
  1248. stack is not known. }
  1249. internalerror(200602046);
  1250. end;
  1251. {****************************************************************************
  1252. TSTT_ExceptionSymtable
  1253. ****************************************************************************}
  1254. constructor tstt_excepTSymtable.create;
  1255. begin
  1256. inherited create('');
  1257. symtabletype:=stt_excepTSymtable;
  1258. end;
  1259. {****************************************************************************
  1260. TMacroSymtable
  1261. ****************************************************************************}
  1262. constructor tmacrosymtable.create(exported: boolean);
  1263. begin
  1264. inherited create('');
  1265. if exported then
  1266. symtabletype:=exportedmacrosymtable
  1267. else
  1268. symtabletype:=localmacrosymtable;
  1269. symtablelevel:=main_program_level;
  1270. end;
  1271. {*****************************************************************************
  1272. Helper Routines
  1273. *****************************************************************************}
  1274. function FullTypeName(def,otherdef:tdef):string;
  1275. var
  1276. s1,s2 : string;
  1277. begin
  1278. s1:=def.typename;
  1279. { When the names are the same try to include the unit name }
  1280. if assigned(otherdef) and
  1281. (def.owner.symtabletype in [globalsymtable,staticsymtable]) then
  1282. begin
  1283. s2:=otherdef.typename;
  1284. if upper(s1)=upper(s2) then
  1285. s1:=def.owner.realname^+'.'+s1;
  1286. end;
  1287. FullTypeName:=s1;
  1288. end;
  1289. procedure incompatibletypes(def1,def2:tdef);
  1290. begin
  1291. { When there is an errordef there is already an error message show }
  1292. if (def2.typ=errordef) or
  1293. (def1.typ=errordef) then
  1294. exit;
  1295. CGMessage2(type_e_incompatible_types,FullTypeName(def1,def2),FullTypeName(def2,def1));
  1296. end;
  1297. procedure hidesym(sym:TSymEntry);
  1298. begin
  1299. sym.realname:='$hidden'+sym.realname;
  1300. tsym(sym).visibility:=vis_hidden;
  1301. end;
  1302. procedure duplicatesym(var hashedid:THashedIDString;dupsym,origsym:TSymEntry);
  1303. var
  1304. st : TSymtable;
  1305. begin
  1306. Message1(sym_e_duplicate_id,tsym(origsym).realname);
  1307. { Write hint where the original symbol was found }
  1308. st:=finduniTSymtable(origsym.owner);
  1309. with tsym(origsym).fileinfo do
  1310. begin
  1311. if assigned(st) and
  1312. (st.symtabletype=globalsymtable) and
  1313. st.iscurrentunit then
  1314. Message2(sym_h_duplicate_id_where,current_module.sourcefiles.get_file_name(fileindex),tostr(line))
  1315. else if assigned(st.name) then
  1316. Message2(sym_h_duplicate_id_where,'unit '+st.name^,tostr(line));
  1317. end;
  1318. { Rename duplicate sym to an unreachable name, but it can be
  1319. inserted in the symtable without errors }
  1320. inc(dupnr);
  1321. hashedid.id:='dup'+tostr(dupnr)+hashedid.id;
  1322. if assigned(dupsym) then
  1323. include(tsym(dupsym).symoptions,sp_implicitrename);
  1324. end;
  1325. {*****************************************************************************
  1326. Search
  1327. *****************************************************************************}
  1328. procedure addsymref(sym:tsym);
  1329. begin
  1330. { symbol uses count }
  1331. sym.IncRefCount;
  1332. { unit uses count }
  1333. if assigned(current_module) and
  1334. (sym.owner.symtabletype=globalsymtable) then
  1335. begin
  1336. if tglobalsymtable(sym.owner).moduleid>=current_module.unitmapsize then
  1337. internalerror(200501152);
  1338. inc(current_module.unitmap[tglobalsymtable(sym.owner).moduleid].refs);
  1339. end;
  1340. end;
  1341. function is_visible_for_object(symst:tsymtable;symvisibility:tvisibility;contextobjdef:tobjectdef):boolean;
  1342. var
  1343. symownerdef : tobjectdef;
  1344. begin
  1345. result:=false;
  1346. { Get objdectdef owner of the symtable for the is_related checks }
  1347. if not assigned(symst) or
  1348. (symst.symtabletype<>objectsymtable) then
  1349. internalerror(200810285);
  1350. symownerdef:=tobjectdef(symst.defowner);
  1351. case symvisibility of
  1352. vis_private :
  1353. begin
  1354. { private symbols are allowed when we are in the same
  1355. module as they are defined }
  1356. result:=(symownerdef.owner.symtabletype in [globalsymtable,staticsymtable]) and
  1357. (symownerdef.owner.iscurrentunit);
  1358. end;
  1359. vis_strictprivate :
  1360. begin
  1361. result:=assigned(current_objectdef) and
  1362. (current_objectdef=symownerdef);
  1363. end;
  1364. vis_strictprotected :
  1365. begin
  1366. result:=assigned(current_objectdef) and
  1367. current_objectdef.is_related(symownerdef);
  1368. end;
  1369. vis_protected :
  1370. begin
  1371. { protected symbols are visible in the module that defines them and
  1372. also visible to related objects. The related object must be defined
  1373. in the current module }
  1374. result:=(
  1375. (
  1376. (symownerdef.owner.symtabletype in [globalsymtable,staticsymtable]) and
  1377. (symownerdef.owner.iscurrentunit)
  1378. ) or
  1379. (
  1380. assigned(contextobjdef) and
  1381. (contextobjdef.owner.symtabletype in [globalsymtable,staticsymtable]) and
  1382. (contextobjdef.owner.iscurrentunit) and
  1383. contextobjdef.is_related(symownerdef)
  1384. )
  1385. );
  1386. end;
  1387. vis_public,
  1388. vis_published :
  1389. result:=true;
  1390. end;
  1391. end;
  1392. function is_visible_for_object(pd:tprocdef;contextobjdef:tobjectdef):boolean;
  1393. begin
  1394. result:=is_visible_for_object(pd.owner,pd.visibility,contextobjdef);
  1395. end;
  1396. function is_visible_for_object(sym:tsym;contextobjdef:tobjectdef):boolean;
  1397. var
  1398. i : longint;
  1399. pd : tprocdef;
  1400. begin
  1401. if sym.typ=procsym then
  1402. begin
  1403. { A procsym is visible, when there is at least one of the procdefs visible }
  1404. result:=false;
  1405. for i:=0 to tprocsym(sym).ProcdefList.Count-1 do
  1406. begin
  1407. pd:=tprocdef(tprocsym(sym).ProcdefList[i]);
  1408. if (pd.owner=sym.owner) and
  1409. is_visible_for_object(pd,contextobjdef) then
  1410. begin
  1411. result:=true;
  1412. exit;
  1413. end;
  1414. end;
  1415. end
  1416. else
  1417. result:=is_visible_for_object(sym.owner,sym.visibility,contextobjdef);
  1418. end;
  1419. function searchsym(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  1420. var
  1421. hashedid : THashedIDString;
  1422. contextobjdef : tobjectdef;
  1423. stackitem : psymtablestackitem;
  1424. begin
  1425. result:=false;
  1426. hashedid.id:=s;
  1427. stackitem:=symtablestack.stack;
  1428. while assigned(stackitem) do
  1429. begin
  1430. srsymtable:=stackitem^.symtable;
  1431. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  1432. if assigned(srsym) then
  1433. begin
  1434. { use the class from withsymtable only when it is
  1435. defined in this unit }
  1436. if (srsymtable.symtabletype=withsymtable) and
  1437. assigned(srsymtable.defowner) and
  1438. (srsymtable.defowner.typ=objectdef) and
  1439. (srsymtable.defowner.owner.symtabletype in [globalsymtable,staticsymtable]) and
  1440. (srsymtable.defowner.owner.iscurrentunit) then
  1441. contextobjdef:=tobjectdef(srsymtable.defowner)
  1442. else
  1443. contextobjdef:=current_objectdef;
  1444. if (srsym.owner.symtabletype<>objectsymtable) or
  1445. is_visible_for_object(srsym,contextobjdef) then
  1446. begin
  1447. { we need to know if a procedure references symbols
  1448. in the static symtable, because then it can't be
  1449. inlined from outside this unit }
  1450. if assigned(current_procinfo) and
  1451. (srsym.owner.symtabletype=staticsymtable) then
  1452. include(current_procinfo.flags,pi_uses_static_symtable);
  1453. addsymref(srsym);
  1454. result:=true;
  1455. exit;
  1456. end;
  1457. end;
  1458. stackitem:=stackitem^.next;
  1459. end;
  1460. srsym:=nil;
  1461. srsymtable:=nil;
  1462. end;
  1463. function searchsym_type(const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  1464. var
  1465. hashedid : THashedIDString;
  1466. stackitem : psymtablestackitem;
  1467. begin
  1468. result:=false;
  1469. hashedid.id:=s;
  1470. stackitem:=symtablestack.stack;
  1471. while assigned(stackitem) do
  1472. begin
  1473. {
  1474. It is not possible to have type symbols in:
  1475. records
  1476. objects
  1477. parameters
  1478. Exception are generic definitions and specializations
  1479. that have the parameterized types inserted in the symtable.
  1480. }
  1481. srsymtable:=stackitem^.symtable;
  1482. if not(srsymtable.symtabletype in [recordsymtable,ObjectSymtable,parasymtable]) or
  1483. (assigned(srsymtable.defowner) and
  1484. (
  1485. (df_generic in tdef(srsymtable.defowner).defoptions) or
  1486. (df_specialization in tdef(srsymtable.defowner).defoptions))
  1487. ) then
  1488. begin
  1489. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  1490. if assigned(srsym) and
  1491. not(srsym.typ in [fieldvarsym,paravarsym]) and
  1492. (
  1493. (srsym.owner.symtabletype<>objectsymtable) or
  1494. is_visible_for_object(srsym,current_objectdef)
  1495. ) then
  1496. begin
  1497. { we need to know if a procedure references symbols
  1498. in the static symtable, because then it can't be
  1499. inlined from outside this unit }
  1500. if assigned(current_procinfo) and
  1501. (srsym.owner.symtabletype=staticsymtable) then
  1502. include(current_procinfo.flags,pi_uses_static_symtable);
  1503. addsymref(srsym);
  1504. result:=true;
  1505. exit;
  1506. end;
  1507. end;
  1508. stackitem:=stackitem^.next;
  1509. end;
  1510. result:=false;
  1511. srsym:=nil;
  1512. srsymtable:=nil;
  1513. end;
  1514. function searchsym_in_module(pm:pointer;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  1515. var
  1516. pmod : tmodule;
  1517. begin
  1518. pmod:=tmodule(pm);
  1519. result:=false;
  1520. if assigned(pmod.globalsymtable) then
  1521. begin
  1522. srsym:=tsym(pmod.globalsymtable.Find(s));
  1523. if assigned(srsym) then
  1524. begin
  1525. srsymtable:=pmod.globalsymtable;
  1526. addsymref(srsym);
  1527. result:=true;
  1528. exit;
  1529. end;
  1530. end;
  1531. { If the module is the current unit we also need
  1532. to search the local symtable }
  1533. if (pmod=current_module) and
  1534. assigned(pmod.localsymtable) then
  1535. begin
  1536. srsym:=tsym(pmod.localsymtable.Find(s));
  1537. if assigned(srsym) then
  1538. begin
  1539. srsymtable:=pmod.localsymtable;
  1540. addsymref(srsym);
  1541. result:=true;
  1542. exit;
  1543. end;
  1544. end;
  1545. srsym:=nil;
  1546. srsymtable:=nil;
  1547. end;
  1548. function searchsym_in_named_module(const unitname, symname: TIDString; out srsym: tsym; out srsymtable: tsymtable): boolean;
  1549. var
  1550. stackitem : psymtablestackitem;
  1551. begin
  1552. result:=false;
  1553. stackitem:=symtablestack.stack;
  1554. while assigned(stackitem) do
  1555. begin
  1556. srsymtable:=stackitem^.symtable;
  1557. if (srsymtable.symtabletype=globalsymtable) and
  1558. (srsymtable.name^=unitname) then
  1559. begin
  1560. srsym:=tsym(srsymtable.find(symname));
  1561. if not assigned(srsym) then
  1562. break;
  1563. result:=true;
  1564. exit;
  1565. end;
  1566. stackitem:=stackitem^.next;
  1567. end;
  1568. { If the module is the current unit we also need
  1569. to search the local symtable }
  1570. if (current_module.globalsymtable=srsymtable) and
  1571. assigned(current_module.localsymtable) then
  1572. begin
  1573. srsymtable:=current_module.localsymtable;
  1574. srsym:=tsym(srsymtable.find(symname));
  1575. if assigned(srsym) then
  1576. begin
  1577. result:=true;
  1578. exit;
  1579. end;
  1580. end;
  1581. end;
  1582. function searchsym_in_class(classh,contextclassh:tobjectdef;const s : TIDString;out srsym:tsym;out srsymtable:TSymtable):boolean;
  1583. var
  1584. hashedid : THashedIDString;
  1585. begin
  1586. { The contextclassh is used for visibility. The classh must be equal to
  1587. or be a parent of contextclassh. E.g. for inherited searches the classh is the
  1588. parent. }
  1589. if assigned(classh) and
  1590. not contextclassh.is_related(classh) then
  1591. internalerror(200811161);
  1592. result:=false;
  1593. hashedid.id:=s;
  1594. while assigned(classh) do
  1595. begin
  1596. srsymtable:=classh.symtable;
  1597. srsym:=tsym(srsymtable.FindWithHash(hashedid));
  1598. if assigned(srsym) and
  1599. is_visible_for_object(srsym,contextclassh) then
  1600. begin
  1601. addsymref(srsym);
  1602. result:=true;
  1603. exit;
  1604. end;
  1605. classh:=classh.childof;
  1606. end;
  1607. srsym:=nil;
  1608. srsymtable:=nil;
  1609. end;
  1610. function searchsym_in_class_by_msgint(classh:tobjectdef;msgid:longint;out srdef : tdef;out srsym:tsym;out srsymtable:TSymtable):boolean;
  1611. var
  1612. def : tdef;
  1613. i : longint;
  1614. begin
  1615. result:=false;
  1616. def:=nil;
  1617. while assigned(classh) do
  1618. begin
  1619. for i:=0 to classh.symtable.DefList.Count-1 do
  1620. begin
  1621. def:=tstoreddef(classh.symtable.DefList[i]);
  1622. { Find also all hidden private methods to
  1623. be compatible with delphi, see tw6203 (PFV) }
  1624. if (def.typ=procdef) and
  1625. (po_msgint in tprocdef(def).procoptions) and
  1626. (tprocdef(def).messageinf.i=msgid) then
  1627. begin
  1628. srdef:=def;
  1629. srsym:=tprocdef(def).procsym;
  1630. srsymtable:=classh.symtable;
  1631. addsymref(srsym);
  1632. result:=true;
  1633. exit;
  1634. end;
  1635. end;
  1636. classh:=classh.childof;
  1637. end;
  1638. srdef:=nil;
  1639. srsym:=nil;
  1640. srsymtable:=nil;
  1641. end;
  1642. function searchsym_in_class_by_msgstr(classh:tobjectdef;const s:string;out srsym:tsym;out srsymtable:TSymtable):boolean;
  1643. var
  1644. def : tdef;
  1645. i : longint;
  1646. begin
  1647. result:=false;
  1648. def:=nil;
  1649. while assigned(classh) do
  1650. begin
  1651. for i:=0 to classh.symtable.DefList.Count-1 do
  1652. begin
  1653. def:=tstoreddef(classh.symtable.DefList[i]);
  1654. { Find also all hidden private methods to
  1655. be compatible with delphi, see tw6203 (PFV) }
  1656. if (def.typ=procdef) and
  1657. (po_msgstr in tprocdef(def).procoptions) and
  1658. (tprocdef(def).messageinf.str^=s) then
  1659. begin
  1660. srsym:=tprocdef(def).procsym;
  1661. srsymtable:=classh.symtable;
  1662. addsymref(srsym);
  1663. result:=true;
  1664. exit;
  1665. end;
  1666. end;
  1667. classh:=classh.childof;
  1668. end;
  1669. srsym:=nil;
  1670. srsymtable:=nil;
  1671. end;
  1672. function search_assignment_operator(from_def,to_def:Tdef):Tprocdef;
  1673. var
  1674. sym : Tprocsym;
  1675. hashedid : THashedIDString;
  1676. curreq,
  1677. besteq : tequaltype;
  1678. currpd,
  1679. bestpd : tprocdef;
  1680. stackitem : psymtablestackitem;
  1681. begin
  1682. hashedid.id:='assign';
  1683. besteq:=te_incompatible;
  1684. bestpd:=nil;
  1685. stackitem:=symtablestack.stack;
  1686. while assigned(stackitem) do
  1687. begin
  1688. sym:=Tprocsym(stackitem^.symtable.FindWithHash(hashedid));
  1689. if sym<>nil then
  1690. begin
  1691. if sym.typ<>procsym then
  1692. internalerror(200402031);
  1693. { if the source type is an alias then this is only the second choice,
  1694. if you mess with this code, check tw4093 }
  1695. currpd:=sym.find_procdef_assignment_operator(from_def,to_def,curreq);
  1696. if curreq>besteq then
  1697. begin
  1698. besteq:=curreq;
  1699. bestpd:=currpd;
  1700. if (besteq=te_exact) then
  1701. break;
  1702. end;
  1703. end;
  1704. stackitem:=stackitem^.next;
  1705. end;
  1706. result:=bestpd;
  1707. end;
  1708. function search_system_type(const s: TIDString): ttypesym;
  1709. var
  1710. sym : tsym;
  1711. begin
  1712. sym:=tsym(systemunit.Find(s));
  1713. if not assigned(sym) or
  1714. (sym.typ<>typesym) then
  1715. cgmessage1(cg_f_unknown_system_type,s);
  1716. result:=ttypesym(sym);
  1717. end;
  1718. function search_named_unit_globaltype(const unitname, typename: TIDString): ttypesym;
  1719. var
  1720. contextobjdef : tobjectdef;
  1721. stackitem : psymtablestackitem;
  1722. srsymtable: tsymtable;
  1723. sym: tsym;
  1724. begin
  1725. if searchsym_in_named_module(unitname,typename,sym,srsymtable) and
  1726. (sym.typ=typesym) then
  1727. begin
  1728. result:=ttypesym(sym);
  1729. exit;
  1730. end
  1731. else
  1732. begin
  1733. cgmessage2(cg_f_unknown_type_in_unit,typename,unitname);
  1734. result:=nil;
  1735. end;
  1736. end;
  1737. function search_class_member(pd : tobjectdef;const s : string):tsym;
  1738. { searches n in symtable of pd and all anchestors }
  1739. var
  1740. hashedid : THashedIDString;
  1741. srsym : tsym;
  1742. begin
  1743. hashedid.id:=s;
  1744. while assigned(pd) do
  1745. begin
  1746. srsym:=tsym(pd.symtable.FindWithHash(hashedid));
  1747. if assigned(srsym) then
  1748. begin
  1749. search_class_member:=srsym;
  1750. exit;
  1751. end;
  1752. pd:=pd.childof;
  1753. end;
  1754. search_class_member:=nil;
  1755. end;
  1756. function search_macro(const s : string):tsym;
  1757. var
  1758. stackitem : psymtablestackitem;
  1759. hashedid : THashedIDString;
  1760. srsym : tsym;
  1761. begin
  1762. hashedid.id:=s;
  1763. { First search the localmacrosymtable before searching the
  1764. global macrosymtables from the units }
  1765. if assigned(current_module) then
  1766. begin
  1767. srsym:=tsym(current_module.localmacrosymtable.FindWithHash(hashedid));
  1768. if assigned(srsym) then
  1769. begin
  1770. result:= srsym;
  1771. exit;
  1772. end;
  1773. end;
  1774. stackitem:=macrosymtablestack.stack;
  1775. while assigned(stackitem) do
  1776. begin
  1777. srsym:=tsym(stackitem^.symtable.FindWithHash(hashedid));
  1778. if assigned(srsym) then
  1779. begin
  1780. result:= srsym;
  1781. exit;
  1782. end;
  1783. stackitem:=stackitem^.next;
  1784. end;
  1785. result:= nil;
  1786. end;
  1787. function defined_macro(const s : string):boolean;
  1788. var
  1789. mac: tmacro;
  1790. begin
  1791. mac:=tmacro(search_macro(s));
  1792. if assigned(mac) then
  1793. begin
  1794. mac.is_used:=true;
  1795. defined_macro:=mac.defined;
  1796. end
  1797. else
  1798. defined_macro:=false;
  1799. end;
  1800. {****************************************************************************
  1801. Object Helpers
  1802. ****************************************************************************}
  1803. function search_default_property(pd : tobjectdef) : tpropertysym;
  1804. { returns the default property of a class, searches also anchestors }
  1805. var
  1806. _defaultprop : tpropertysym;
  1807. begin
  1808. _defaultprop:=nil;
  1809. while assigned(pd) do
  1810. begin
  1811. pd.symtable.SymList.ForEachCall(@tstoredsymtable(pd.symtable).testfordefaultproperty,@_defaultprop);
  1812. if assigned(_defaultprop) then
  1813. break;
  1814. pd:=pd.childof;
  1815. end;
  1816. search_default_property:=_defaultprop;
  1817. end;
  1818. {****************************************************************************
  1819. Macro Helpers
  1820. ****************************************************************************}
  1821. procedure def_system_macro(const name : string);
  1822. var
  1823. mac : tmacro;
  1824. s: string;
  1825. begin
  1826. if name = '' then
  1827. internalerror(2004121202);
  1828. s:= upper(name);
  1829. mac:=tmacro(search_macro(s));
  1830. if not assigned(mac) then
  1831. begin
  1832. mac:=tmacro.create(s);
  1833. if assigned(current_module) then
  1834. current_module.localmacrosymtable.insert(mac)
  1835. else
  1836. initialmacrosymtable.insert(mac);
  1837. end;
  1838. if not mac.defined then
  1839. Message1(parser_c_macro_defined,mac.name);
  1840. mac.defined:=true;
  1841. end;
  1842. procedure set_system_macro(const name, value : string);
  1843. var
  1844. mac : tmacro;
  1845. s: string;
  1846. begin
  1847. if name = '' then
  1848. internalerror(2004121203);
  1849. s:= upper(name);
  1850. mac:=tmacro(search_macro(s));
  1851. if not assigned(mac) then
  1852. begin
  1853. mac:=tmacro.create(s);
  1854. if assigned(current_module) then
  1855. current_module.localmacrosymtable.insert(mac)
  1856. else
  1857. initialmacrosymtable.insert(mac);
  1858. end
  1859. else
  1860. begin
  1861. mac.is_compiler_var:=false;
  1862. if assigned(mac.buftext) then
  1863. freemem(mac.buftext,mac.buflen);
  1864. end;
  1865. Message2(parser_c_macro_set_to,mac.name,value);
  1866. mac.buflen:=length(value);
  1867. getmem(mac.buftext,mac.buflen);
  1868. move(value[1],mac.buftext^,mac.buflen);
  1869. mac.defined:=true;
  1870. end;
  1871. procedure set_system_compvar(const name, value : string);
  1872. var
  1873. mac : tmacro;
  1874. s: string;
  1875. begin
  1876. if name = '' then
  1877. internalerror(2004121204);
  1878. s:= upper(name);
  1879. mac:=tmacro(search_macro(s));
  1880. if not assigned(mac) then
  1881. begin
  1882. mac:=tmacro.create(s);
  1883. mac.is_compiler_var:=true;
  1884. if assigned(current_module) then
  1885. current_module.localmacrosymtable.insert(mac)
  1886. else
  1887. initialmacrosymtable.insert(mac);
  1888. end
  1889. else
  1890. begin
  1891. mac.is_compiler_var:=true;
  1892. if assigned(mac.buftext) then
  1893. freemem(mac.buftext,mac.buflen);
  1894. end;
  1895. Message2(parser_c_macro_set_to,mac.name,value);
  1896. mac.buflen:=length(value);
  1897. getmem(mac.buftext,mac.buflen);
  1898. move(value[1],mac.buftext^,mac.buflen);
  1899. mac.defined:=true;
  1900. end;
  1901. procedure undef_system_macro(const name : string);
  1902. var
  1903. mac : tmacro;
  1904. s: string;
  1905. begin
  1906. if name = '' then
  1907. internalerror(2004121205);
  1908. s:= upper(name);
  1909. mac:=tmacro(search_macro(s));
  1910. if not assigned(mac) then
  1911. {If not found, then it's already undefined.}
  1912. else
  1913. begin
  1914. if mac.defined then
  1915. Message1(parser_c_macro_undefined,mac.name);
  1916. mac.defined:=false;
  1917. mac.is_compiler_var:=false;
  1918. { delete old definition }
  1919. if assigned(mac.buftext) then
  1920. begin
  1921. freemem(mac.buftext,mac.buflen);
  1922. mac.buftext:=nil;
  1923. end;
  1924. end;
  1925. end;
  1926. {$ifdef UNITALIASES}
  1927. {****************************************************************************
  1928. TUNIT_ALIAS
  1929. ****************************************************************************}
  1930. constructor tunit_alias.create(const n:string);
  1931. var
  1932. i : longint;
  1933. begin
  1934. i:=pos('=',n);
  1935. if i=0 then
  1936. fail;
  1937. inherited createname(Copy(n,1,i-1));
  1938. newname:=stringdup(Copy(n,i+1,255));
  1939. end;
  1940. destructor tunit_alias.destroy;
  1941. begin
  1942. stringdispose(newname);
  1943. inherited destroy;
  1944. end;
  1945. procedure addunitalias(const n:string);
  1946. begin
  1947. unitaliases^.insert(tunit_alias,init(Upper(n))));
  1948. end;
  1949. function getunitalias(const n:string):string;
  1950. var
  1951. p : punit_alias;
  1952. begin
  1953. p:=punit_alias(unitaliases^.Find(Upper(n)));
  1954. if assigned(p) then
  1955. getunitalias:=punit_alias(p).newname^
  1956. else
  1957. getunitalias:=n;
  1958. end;
  1959. {$endif UNITALIASES}
  1960. {****************************************************************************
  1961. Init/Done Symtable
  1962. ****************************************************************************}
  1963. procedure InitSymtable;
  1964. begin
  1965. { Reset symbolstack }
  1966. symtablestack:=nil;
  1967. systemunit:=nil;
  1968. { create error syms and def }
  1969. generrorsym:=terrorsym.create;
  1970. generrordef:=terrordef.create;
  1971. { macros }
  1972. initialmacrosymtable:=tmacrosymtable.create(false);
  1973. macrosymtablestack:=TSymtablestack.create;
  1974. macrosymtablestack.push(initialmacrosymtable);
  1975. {$ifdef UNITALIASES}
  1976. { unit aliases }
  1977. unitaliases:=TFPHashObjectList.create;
  1978. {$endif}
  1979. { set some global vars to nil, might be important for the ide }
  1980. class_tobject:=nil;
  1981. interface_iunknown:=nil;
  1982. rec_tguid:=nil;
  1983. dupnr:=0;
  1984. end;
  1985. procedure DoneSymtable;
  1986. begin
  1987. generrorsym.owner:=nil;
  1988. generrorsym.free;
  1989. generrordef.owner:=nil;
  1990. generrordef.free;
  1991. initialmacrosymtable.free;
  1992. macrosymtablestack.free;
  1993. {$ifdef UNITALIASES}
  1994. unitaliases.free;
  1995. {$endif}
  1996. end;
  1997. end.