symtable.pas 84 KB

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