symtable.pas 75 KB

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