symtable.pas 77 KB

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