symtable.pas 82 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511
  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. {$ifdef GDB}
  72. procedure concatstabto(asmlist : taasmoutput);virtual;
  73. function getnewtypecount : word; override;
  74. {$endif GDB}
  75. procedure testfordefaultproperty(p : TNamedIndexItem;arg:pointer);
  76. end;
  77. tabstractrecordsymtable = class(tstoredsymtable)
  78. public
  79. datasize : aint;
  80. usefieldalignment, { alignment to use for fields (PACKRECORDS value), -1 is C style }
  81. recordalignment, { alignment required when inserting this record }
  82. fieldalignment, { alignment current alignment used when fields are inserted }
  83. padalignment : shortint; { size to a multiple of which the symtable has to be rounded up }
  84. constructor create(const n:string;usealign:shortint);
  85. procedure ppuload(ppufile:tcompilerppufile);override;
  86. procedure ppuwrite(ppufile:tcompilerppufile);override;
  87. procedure load_references(ppufile:tcompilerppufile;locals:boolean);override;
  88. procedure write_references(ppufile:tcompilerppufile;locals:boolean);override;
  89. procedure insertfield(sym:tfieldvarsym;addsym:boolean);
  90. procedure addalignmentpadding;
  91. end;
  92. trecordsymtable = class(tabstractrecordsymtable)
  93. public
  94. constructor create(usealign:shortint);
  95. procedure insertunionst(unionst : trecordsymtable;offset : longint);
  96. end;
  97. tobjectsymtable = class(tabstractrecordsymtable)
  98. public
  99. constructor create(const n:string;usealign:shortint);
  100. procedure insert(sym : tsymentry);override;
  101. end;
  102. tabstractlocalsymtable = class(tstoredsymtable)
  103. public
  104. procedure ppuwrite(ppufile:tcompilerppufile);override;
  105. end;
  106. tlocalsymtable = class(tabstractlocalsymtable)
  107. public
  108. constructor create(level:byte);
  109. procedure insert(sym : tsymentry);override;
  110. end;
  111. tparasymtable = class(tabstractlocalsymtable)
  112. public
  113. constructor create(level:byte);
  114. procedure insert(sym : tsymentry);override;
  115. end;
  116. tabstractunitsymtable = class(tstoredsymtable)
  117. public
  118. {$ifdef GDB}
  119. dbx_count : longint;
  120. prev_dbx_counter : plongint;
  121. dbx_count_ok : boolean;
  122. {$endif GDB}
  123. constructor create(const n : string;id:word);
  124. {$ifdef GDB}
  125. procedure concattypestabto(asmlist : taasmoutput);
  126. {$endif GDB}
  127. function iscurrentunit:boolean;override;
  128. end;
  129. tglobalsymtable = class(tabstractunitsymtable)
  130. public
  131. unittypecount : word;
  132. constructor create(const n : string;id:word);
  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;id:word);
  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 set_system_compvar(const name, value : string);
  203. procedure undef_system_macro(const name : string);
  204. {*** symtable stack ***}
  205. {$ifdef DEBUG}
  206. procedure test_symtablestack;
  207. procedure list_symtablestack;
  208. {$endif DEBUG}
  209. {$ifdef UNITALIASES}
  210. type
  211. punit_alias = ^tunit_alias;
  212. tunit_alias = object(TNamedIndexItem)
  213. newname : pstring;
  214. constructor init(const n:string);
  215. destructor done;virtual;
  216. end;
  217. var
  218. unitaliases : pdictionary;
  219. procedure addunitalias(const n:string);
  220. function getunitalias(const n:string):string;
  221. {$endif UNITALIASES}
  222. {*** Init / Done ***}
  223. procedure InitSymtable;
  224. procedure DoneSymtable;
  225. const
  226. overloaded_names : array [NOTOKEN..last_overloaded] of string[16] =
  227. ('error',
  228. 'plus','minus','star','slash','equal',
  229. 'greater','lower','greater_or_equal',
  230. 'lower_or_equal',
  231. 'sym_diff','starstar',
  232. 'as','is','in','or',
  233. 'and','div','mod','not','shl','shr','xor',
  234. 'assign');
  235. implementation
  236. uses
  237. { global }
  238. verbose,globals,
  239. { target }
  240. systems,
  241. { symtable }
  242. symutil,defcmp,
  243. { module }
  244. fmodule,
  245. {$ifdef GDB}
  246. gdb,
  247. {$endif GDB}
  248. { codegen }
  249. procinfo
  250. ;
  251. var
  252. dupnr : longint; { unique number for duplicate symbols }
  253. {*****************************************************************************
  254. TStoredSymtable
  255. *****************************************************************************}
  256. procedure tstoredsymtable.ppuload(ppufile:tcompilerppufile);
  257. begin
  258. { load definitions }
  259. loaddefs(ppufile);
  260. { load symbols }
  261. loadsyms(ppufile);
  262. end;
  263. procedure tstoredsymtable.ppuwrite(ppufile:tcompilerppufile);
  264. begin
  265. { write definitions }
  266. writedefs(ppufile);
  267. { write symbols }
  268. writesyms(ppufile);
  269. end;
  270. procedure tstoredsymtable.loaddefs(ppufile:tcompilerppufile);
  271. var
  272. hp : tdef;
  273. b : byte;
  274. begin
  275. { load start of definition section, which holds the amount of defs }
  276. if ppufile.readentry<>ibstartdefs then
  277. Message(unit_f_ppu_read_error);
  278. ppufile.getlongint;
  279. { read definitions }
  280. repeat
  281. b:=ppufile.readentry;
  282. case b of
  283. ibpointerdef : hp:=tpointerdef.ppuload(ppufile);
  284. ibarraydef : hp:=tarraydef.ppuload(ppufile);
  285. iborddef : hp:=torddef.ppuload(ppufile);
  286. ibfloatdef : hp:=tfloatdef.ppuload(ppufile);
  287. ibprocdef : hp:=tprocdef.ppuload(ppufile);
  288. ibshortstringdef : hp:=tstringdef.loadshort(ppufile);
  289. iblongstringdef : hp:=tstringdef.loadlong(ppufile);
  290. {$ifdef ansistring_bits}
  291. ibansistring16def : hp:=tstringdef.loadansi(ppufile,sb_16);
  292. ibansistring32def : hp:=tstringdef.loadansi(ppufile,sb_32);
  293. ibansistring64def : hp:=tstringdef.loadansi(ppufile,sb_64);
  294. {$else}
  295. ibansistringdef : hp:=tstringdef.loadansi(ppufile);
  296. {$endif}
  297. ibwidestringdef : hp:=tstringdef.loadwide(ppufile);
  298. ibrecorddef : hp:=trecorddef.ppuload(ppufile);
  299. ibobjectdef : hp:=tobjectdef.ppuload(ppufile);
  300. ibenumdef : hp:=tenumdef.ppuload(ppufile);
  301. ibsetdef : hp:=tsetdef.ppuload(ppufile);
  302. ibprocvardef : hp:=tprocvardef.ppuload(ppufile);
  303. ibfiledef : hp:=tfiledef.ppuload(ppufile);
  304. ibclassrefdef : hp:=tclassrefdef.ppuload(ppufile);
  305. ibformaldef : hp:=tformaldef.ppuload(ppufile);
  306. ibvariantdef : hp:=tvariantdef.ppuload(ppufile);
  307. ibenddefs : break;
  308. ibend : Message(unit_f_ppu_read_error);
  309. else
  310. Message1(unit_f_ppu_invalid_entry,tostr(b));
  311. end;
  312. hp.owner:=self;
  313. defindex.insert(hp);
  314. until false;
  315. end;
  316. procedure tstoredsymtable.loadsyms(ppufile:tcompilerppufile);
  317. var
  318. b : byte;
  319. sym : tsym;
  320. begin
  321. { load start of definition section, which holds the amount of defs }
  322. if ppufile.readentry<>ibstartsyms then
  323. Message(unit_f_ppu_read_error);
  324. { skip amount of symbols, not used currently }
  325. ppufile.getlongint;
  326. { now read the symbols }
  327. repeat
  328. b:=ppufile.readentry;
  329. case b of
  330. ibtypesym : sym:=ttypesym.ppuload(ppufile);
  331. ibprocsym : sym:=tprocsym.ppuload(ppufile);
  332. ibconstsym : sym:=tconstsym.ppuload(ppufile);
  333. ibglobalvarsym : sym:=tglobalvarsym.ppuload(ppufile);
  334. iblocalvarsym : sym:=tlocalvarsym.ppuload(ppufile);
  335. ibparavarsym : sym:=tparavarsym.ppuload(ppufile);
  336. ibfieldvarsym : sym:=tfieldvarsym.ppuload(ppufile);
  337. ibabsolutevarsym : sym:=tabsolutevarsym.ppuload(ppufile);
  338. ibenumsym : sym:=tenumsym.ppuload(ppufile);
  339. ibtypedconstsym : sym:=ttypedconstsym.ppuload(ppufile);
  340. ibpropertysym : sym:=tpropertysym.ppuload(ppufile);
  341. ibunitsym : sym:=tunitsym.ppuload(ppufile);
  342. iblabelsym : sym:=tlabelsym.ppuload(ppufile);
  343. ibsyssym : sym:=tsyssym.ppuload(ppufile);
  344. ibrttisym : sym:=trttisym.ppuload(ppufile);
  345. ibmacrosym : sym:=tmacro.ppuload(ppufile);
  346. ibendsyms : break;
  347. ibend : Message(unit_f_ppu_read_error);
  348. else
  349. Message1(unit_f_ppu_invalid_entry,tostr(b));
  350. end;
  351. sym.owner:=self;
  352. symindex.insert(sym);
  353. symsearch.insert(sym);
  354. until false;
  355. end;
  356. procedure tstoredsymtable.writedefs(ppufile:tcompilerppufile);
  357. var
  358. pd : tstoreddef;
  359. begin
  360. { each definition get a number, write then the amount of defs to the
  361. ibstartdef entry }
  362. ppufile.putlongint(defindex.count);
  363. ppufile.writeentry(ibstartdefs);
  364. { now write the definition }
  365. pd:=tstoreddef(defindex.first);
  366. while assigned(pd) do
  367. begin
  368. pd.ppuwrite(ppufile);
  369. pd:=tstoreddef(pd.indexnext);
  370. end;
  371. { write end of definitions }
  372. ppufile.writeentry(ibenddefs);
  373. end;
  374. procedure tstoredsymtable.writesyms(ppufile:tcompilerppufile);
  375. var
  376. pd : Tstoredsym;
  377. begin
  378. { each definition get a number, write then the amount of syms and the
  379. datasize to the ibsymdef entry }
  380. ppufile.putlongint(symindex.count);
  381. ppufile.writeentry(ibstartsyms);
  382. { foreach is used to write all symbols }
  383. pd:=Tstoredsym(symindex.first);
  384. while assigned(pd) do
  385. begin
  386. pd.ppuwrite(ppufile);
  387. pd:=Tstoredsym(pd.indexnext);
  388. end;
  389. { end of symbols }
  390. ppufile.writeentry(ibendsyms);
  391. end;
  392. procedure tstoredsymtable.load_references(ppufile:tcompilerppufile;locals:boolean);
  393. var
  394. b : byte;
  395. d : tderef;
  396. sym : Tsym;
  397. prdef : tstoreddef;
  398. begin
  399. b:=ppufile.readentry;
  400. if b <> ibbeginsymtablebrowser then
  401. Message1(unit_f_ppu_invalid_entry,tostr(b));
  402. repeat
  403. b:=ppufile.readentry;
  404. case b of
  405. ibsymref :
  406. begin
  407. ppufile.getderef(d);
  408. sym:=Tsym(d.resolve);
  409. if assigned(sym) then
  410. sym.load_references(ppufile,locals);
  411. end;
  412. ibdefref :
  413. begin
  414. ppufile.getderef(d);
  415. prdef:=tstoreddef(d.resolve);
  416. if assigned(prdef) then
  417. begin
  418. if prdef.deftype<>procdef then
  419. Message(unit_f_ppu_read_error);
  420. tprocdef(prdef).load_references(ppufile,locals);
  421. end;
  422. end;
  423. ibendsymtablebrowser :
  424. break;
  425. else
  426. Message1(unit_f_ppu_invalid_entry,tostr(b));
  427. end;
  428. until false;
  429. end;
  430. procedure tstoredsymtable.write_references(ppufile:tcompilerppufile;locals:boolean);
  431. var
  432. pd : Tsym;
  433. begin
  434. ppufile.writeentry(ibbeginsymtablebrowser);
  435. { write all symbols }
  436. pd:=Tsym(symindex.first);
  437. while assigned(pd) do
  438. begin
  439. pd.write_references(ppufile,locals);
  440. pd:=Tsym(pd.indexnext);
  441. end;
  442. ppufile.writeentry(ibendsymtablebrowser);
  443. end;
  444. procedure tstoredsymtable.buildderef;
  445. var
  446. hp : tdef;
  447. hs : tsym;
  448. begin
  449. { interface definitions }
  450. hp:=tdef(defindex.first);
  451. while assigned(hp) do
  452. begin
  453. hp.buildderef;
  454. hp:=tdef(hp.indexnext);
  455. end;
  456. { interface symbols }
  457. hs:=tsym(symindex.first);
  458. while assigned(hs) do
  459. begin
  460. hs.buildderef;
  461. hs:=tsym(hs.indexnext);
  462. end;
  463. end;
  464. procedure tstoredsymtable.buildderefimpl;
  465. var
  466. hp : tdef;
  467. begin
  468. { definitions }
  469. hp:=tdef(defindex.first);
  470. while assigned(hp) do
  471. begin
  472. hp.buildderefimpl;
  473. hp:=tdef(hp.indexnext);
  474. end;
  475. end;
  476. procedure tstoredsymtable.deref;
  477. var
  478. hp : tdef;
  479. hs : tsym;
  480. begin
  481. { first deref the interface ttype symbols. This is needs
  482. to be done before the interface defs are derefed, because
  483. the interface defs can contain references to the type symbols
  484. which then already need to contain a resolved restype field (PFV) }
  485. hs:=tsym(symindex.first);
  486. while assigned(hs) do
  487. begin
  488. if hs.typ=typesym then
  489. hs.deref;
  490. hs:=tsym(hs.indexnext);
  491. end;
  492. { deref the interface definitions }
  493. hp:=tdef(defindex.first);
  494. while assigned(hp) do
  495. begin
  496. hp.deref;
  497. hp:=tdef(hp.indexnext);
  498. end;
  499. { deref the interface symbols }
  500. hs:=tsym(symindex.first);
  501. while assigned(hs) do
  502. begin
  503. if hs.typ<>typesym then
  504. hs.deref;
  505. hs:=tsym(hs.indexnext);
  506. end;
  507. end;
  508. procedure tstoredsymtable.derefimpl;
  509. var
  510. hp : tdef;
  511. begin
  512. { definitions }
  513. hp:=tdef(defindex.first);
  514. while assigned(hp) do
  515. begin
  516. hp.derefimpl;
  517. hp:=tdef(hp.indexnext);
  518. end;
  519. end;
  520. procedure tstoredsymtable.duplicatesym(dupsym,sym:tsymentry);
  521. var
  522. st : tsymtable;
  523. begin
  524. Message1(sym_e_duplicate_id,tsym(sym).realname);
  525. st:=findunitsymtable(sym.owner);
  526. with tsym(sym).fileinfo do
  527. begin
  528. if assigned(st) and
  529. (st.symtabletype=globalsymtable) and
  530. (not st.iscurrentunit) then
  531. Message2(sym_h_duplicate_id_where,'unit '+st.name^,tostr(line))
  532. else
  533. Message2(sym_h_duplicate_id_where,current_module.sourcefiles.get_file_name(fileindex),tostr(line));
  534. end;
  535. { Rename duplicate sym to an unreachable name, but it can be
  536. inserted in the symtable without errors }
  537. if assigned(dupsym) then
  538. begin
  539. inc(dupnr);
  540. dupsym.name:='dup'+tostr(dupnr)+dupsym.name;
  541. end;
  542. end;
  543. procedure tstoredsymtable.insert(sym:tsymentry);
  544. var
  545. hsym : tsym;
  546. begin
  547. { set owner and sym indexnb }
  548. sym.owner:=self;
  549. { check the current symtable }
  550. hsym:=tsym(search(sym.name));
  551. if assigned(hsym) then
  552. begin
  553. { in TP and Delphi you can have a local with the
  554. same name as the function, the function is then hidden for
  555. the user. (Under delphi it can still be accessed using result),
  556. but don't allow hiding of RESULT }
  557. if (m_duplicate_names in aktmodeswitches) and
  558. (sym.typ in [localvarsym,paravarsym,absolutevarsym]) and
  559. (vo_is_funcret in tabstractvarsym(sym).varoptions) and
  560. not((m_result in aktmodeswitches) and
  561. (vo_is_result in tabstractvarsym(sym).varoptions)) then
  562. sym.name:='hidden'+sym.name
  563. else
  564. DuplicateSym(sym,hsym);
  565. end;
  566. { register definition of typesym }
  567. if (sym.typ = typesym) and
  568. assigned(ttypesym(sym).restype.def) then
  569. begin
  570. if not(assigned(ttypesym(sym).restype.def.owner)) and
  571. (ttypesym(sym).restype.def.deftype<>errordef) then
  572. registerdef(ttypesym(sym).restype.def);
  573. end;
  574. { insert in index and search hash }
  575. symindex.insert(sym);
  576. symsearch.insert(sym);
  577. end;
  578. function tstoredsymtable.speedsearch(const s : stringid;speedvalue : cardinal) : tsymentry;
  579. var
  580. hp : Tsym;
  581. newref : tref;
  582. begin
  583. hp:=Tsym(inherited speedsearch(s,speedvalue));
  584. if assigned(hp) then
  585. begin
  586. { reject non static members in static procedures }
  587. if (symtabletype=objectsymtable) and
  588. not(sp_static in hp.symoptions) and
  589. allow_only_static then
  590. Message(sym_e_only_static_in_static);
  591. { unit uses count }
  592. if assigned(current_module) and
  593. (symtabletype=globalsymtable) then
  594. begin
  595. if tglobalsymtable(self).moduleid>=current_module.unitmapsize then
  596. internalerror(200501152);
  597. inc(current_module.unitmap[tglobalsymtable(self).moduleid].refs);
  598. end;
  599. if make_ref and (cs_browser in aktmoduleswitches) then
  600. begin
  601. newref:=tref.create(hp.lastref,@akttokenpos);
  602. { for symbols that are in tables without browser info or syssyms }
  603. if hp.refcount=0 then
  604. begin
  605. hp.defref:=newref;
  606. hp.lastref:=newref;
  607. end
  608. else
  609. if resolving_forward and assigned(hp.defref) then
  610. { put it as second reference }
  611. begin
  612. newref.nextref:=hp.defref.nextref;
  613. hp.defref.nextref:=newref;
  614. hp.lastref.nextref:=nil;
  615. end
  616. else
  617. hp.lastref:=newref;
  618. inc(hp.refcount);
  619. end;
  620. if make_ref then
  621. inc(hp.refs);
  622. end; { value was not found }
  623. speedsearch:=hp;
  624. end;
  625. {**************************************
  626. Callbacks
  627. **************************************}
  628. procedure TStoredSymtable.check_forward(sym : TNamedIndexItem;arg:pointer);
  629. begin
  630. if tsym(sym).typ=procsym then
  631. tprocsym(sym).check_forward
  632. { check also object method table }
  633. { we needn't to test the def list }
  634. { because each object has to have a type sym,
  635. only test objects declarations, not type renamings }
  636. else
  637. if (tsym(sym).typ=typesym) and
  638. assigned(ttypesym(sym).restype.def) and
  639. (ttypesym(sym).restype.def.typesym=ttypesym(sym)) 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),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),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),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;id:word);
  1180. begin
  1181. inherited create(n);
  1182. moduleid:=id;
  1183. symsearch.usehash;
  1184. {$ifdef GDB}
  1185. { reset GDB things }
  1186. prev_dbx_counter := dbx_counter;
  1187. dbx_counter := nil;
  1188. dbx_count := -1;
  1189. {$endif GDB}
  1190. end;
  1191. function tabstractunitsymtable.iscurrentunit:boolean;
  1192. begin
  1193. result:=assigned(current_module) and
  1194. (
  1195. (current_module.globalsymtable=self) or
  1196. (current_module.localsymtable=self)
  1197. );
  1198. end;
  1199. {$ifdef GDB}
  1200. procedure tabstractunitsymtable.concattypestabto(asmlist : taasmoutput);
  1201. procedure dowritestabs(asmlist:taasmoutput;st:tsymtable);
  1202. var
  1203. p : tstoreddef;
  1204. begin
  1205. p:=tstoreddef(st.defindex.first);
  1206. while assigned(p) do
  1207. begin
  1208. { also insert local types for the current unit }
  1209. if iscurrentunit then
  1210. begin
  1211. case p.deftype of
  1212. procdef :
  1213. if assigned(tprocdef(p).localst) then
  1214. dowritestabs(asmlist,tprocdef(p).localst);
  1215. objectdef :
  1216. dowritestabs(asmlist,tobjectdef(p).symtable);
  1217. end;
  1218. end;
  1219. if (p.stab_state=stab_state_used) then
  1220. p.concatstabto(asmlist);
  1221. p:=tstoreddef(p.indexnext);
  1222. end;
  1223. end;
  1224. var
  1225. old_writing_def_stabs : boolean;
  1226. prev_dbx_count : plongint;
  1227. begin
  1228. if not assigned(name) then
  1229. name := stringdup('Main_program');
  1230. asmList.concat(tai_comment.Create(strpnew('Begin unit '+name^+' has index '+tostr(moduleid))));
  1231. if cs_gdb_dbx in aktglobalswitches then
  1232. begin
  1233. if dbx_count_ok then
  1234. begin
  1235. asmList.concat(tai_comment.Create(strpnew('"repeated" unit '+name^
  1236. +' has index '+tostr(moduleid)+' dbx count = '+tostr(dbx_count))));
  1237. asmList.concat(Tai_stabs.Create(strpnew('"'+name^+'",'
  1238. +tostr(N_EXCL)+',0,0,'+tostr(dbx_count))));
  1239. exit;
  1240. end
  1241. else if not iscurrentunit then
  1242. begin
  1243. prev_dbx_count := dbx_counter;
  1244. dbx_counter := nil;
  1245. do_count_dbx:=false;
  1246. if (symtabletype = globalsymtable) then
  1247. asmList.concat(Tai_stabs.Create(strpnew('"'+name^+'",'+tostr(N_BINCL)+',0,0,0')));
  1248. dbx_counter := @dbx_count;
  1249. dbx_count:=0;
  1250. do_count_dbx:=assigned(dbx_counter);
  1251. end;
  1252. end;
  1253. old_writing_def_stabs:=writing_def_stabs;
  1254. writing_def_stabs:=true;
  1255. dowritestabs(asmlist,self);
  1256. writing_def_stabs:=old_writing_def_stabs;
  1257. if cs_gdb_dbx in aktglobalswitches then
  1258. begin
  1259. if not iscurrentunit then
  1260. begin
  1261. dbx_counter := prev_dbx_count;
  1262. do_count_dbx:=false;
  1263. asmList.concat(Tai_stabs.Create(strpnew('"'+name^+'",'
  1264. +tostr(N_EINCL)+',0,0,0')));
  1265. do_count_dbx:=assigned(dbx_counter);
  1266. dbx_count_ok := {true}false;
  1267. end;
  1268. end;
  1269. asmList.concat(tai_comment.Create(strpnew('End unit '+name^+' has index '+tostr(moduleid))));
  1270. end;
  1271. {$endif GDB}
  1272. {****************************************************************************
  1273. TStaticSymtable
  1274. ****************************************************************************}
  1275. constructor tstaticsymtable.create(const n : string;id:word);
  1276. begin
  1277. inherited create(n,id);
  1278. symtabletype:=staticsymtable;
  1279. symtablelevel:=main_program_level;
  1280. end;
  1281. procedure tstaticsymtable.ppuload(ppufile:tcompilerppufile);
  1282. begin
  1283. next:=symtablestack;
  1284. symtablestack:=self;
  1285. inherited ppuload(ppufile);
  1286. { now we can deref the syms and defs }
  1287. deref;
  1288. { restore symtablestack }
  1289. symtablestack:=next;
  1290. end;
  1291. procedure tstaticsymtable.ppuwrite(ppufile:tcompilerppufile);
  1292. begin
  1293. inherited ppuwrite(ppufile);
  1294. end;
  1295. procedure tstaticsymtable.load_references(ppufile:tcompilerppufile;locals:boolean);
  1296. begin
  1297. inherited load_references(ppufile,locals);
  1298. end;
  1299. procedure tstaticsymtable.write_references(ppufile:tcompilerppufile;locals:boolean);
  1300. begin
  1301. inherited write_references(ppufile,locals);
  1302. end;
  1303. procedure tstaticsymtable.insert(sym:tsymentry);
  1304. var
  1305. hsym : tsym;
  1306. begin
  1307. { also check the global symtable }
  1308. if assigned(next) and
  1309. (next.symtabletype=globalsymtable) and
  1310. (next.iscurrentunit) then
  1311. begin
  1312. hsym:=tsym(next.search(sym.name));
  1313. if assigned(hsym) then
  1314. begin
  1315. { Delphi you can have a symbol with the same name as the
  1316. unit, the unit can then not be accessed anymore using
  1317. <unit>.<id>, so we can hide the symbol }
  1318. if (m_duplicate_names in aktmodeswitches) and
  1319. (hsym.typ=symconst.unitsym) then
  1320. hsym.owner.rename(hsym.name,'hidden'+hsym.name)
  1321. else
  1322. DuplicateSym(sym,hsym);
  1323. end;
  1324. end;
  1325. inherited insert(sym);
  1326. end;
  1327. {****************************************************************************
  1328. TGlobalSymtable
  1329. ****************************************************************************}
  1330. constructor tglobalsymtable.create(const n : string;id:word);
  1331. begin
  1332. inherited create(n,id);
  1333. symtabletype:=globalsymtable;
  1334. symtablelevel:=main_program_level;
  1335. {$ifdef GDB}
  1336. if cs_gdb_dbx in aktglobalswitches then
  1337. begin
  1338. dbx_count := 0;
  1339. unittypecount:=1;
  1340. pglobaltypecount := @unittypecount;
  1341. {moduleid:=current_module.unitcount;}
  1342. {debugList.concat(tai_comment.Create(strpnew('Global '+name^+' has index '+tostr(moduleid))));
  1343. debugList.concat(Tai_stabs.Create(strpnew('"'+name^+'",'+tostr(N_BINCL)+',0,0,0')));}
  1344. {inc(current_module.unitcount);}
  1345. { we can't use dbx_vcount, because we don't know
  1346. if the object file will be loaded before or afeter PM }
  1347. dbx_count_ok:=false;
  1348. dbx_counter:=@dbx_count;
  1349. do_count_dbx:=true;
  1350. end;
  1351. {$endif GDB}
  1352. end;
  1353. procedure tglobalsymtable.ppuload(ppufile:tcompilerppufile);
  1354. {$ifdef GDB}
  1355. var
  1356. b : byte;
  1357. {$endif GDB}
  1358. begin
  1359. {$ifdef GDB}
  1360. if cs_gdb_dbx in aktglobalswitches then
  1361. begin
  1362. UnitTypeCount:=1;
  1363. PglobalTypeCount:=@UnitTypeCount;
  1364. end;
  1365. {$endif GDB}
  1366. next:=symtablestack;
  1367. symtablestack:=self;
  1368. inherited ppuload(ppufile);
  1369. { now we can deref the syms and defs }
  1370. deref;
  1371. { restore symtablestack }
  1372. symtablestack:=next;
  1373. { read dbx count }
  1374. {$ifdef GDB}
  1375. if (current_module.flags and uf_has_dbx)<>0 then
  1376. begin
  1377. b:=ppufile.readentry;
  1378. if b<>ibdbxcount then
  1379. Message(unit_f_ppu_dbx_count_problem)
  1380. else
  1381. dbx_count:=ppufile.getlongint;
  1382. {$IfDef EXTDEBUG}
  1383. writeln('Read dbx_count ',dbx_count,' in unit ',name^,'.ppu');
  1384. {$ENDIF EXTDEBUG}
  1385. { we can't use dbx_vcount, because we don't know
  1386. if the object file will be loaded before or afeter PM }
  1387. dbx_count_ok := {true}false;
  1388. end
  1389. else
  1390. begin
  1391. dbx_count:=-1;
  1392. dbx_count_ok:=false;
  1393. end;
  1394. {$endif GDB}
  1395. end;
  1396. procedure tglobalsymtable.ppuwrite(ppufile:tcompilerppufile);
  1397. begin
  1398. { write the symtable entries }
  1399. inherited ppuwrite(ppufile);
  1400. { write dbx count }
  1401. {$ifdef GDB}
  1402. if cs_gdb_dbx in aktglobalswitches then
  1403. begin
  1404. {$IfDef EXTDEBUG}
  1405. writeln('Writing dbx_count ',dbx_count,' in unit ',name^,'.ppu');
  1406. {$ENDIF EXTDEBUG}
  1407. ppufile.do_crc:=false;
  1408. ppufile.putlongint(dbx_count);
  1409. ppufile.writeentry(ibdbxcount);
  1410. ppufile.do_crc:=true;
  1411. end;
  1412. {$endif GDB}
  1413. end;
  1414. procedure tglobalsymtable.load_references(ppufile:tcompilerppufile;locals:boolean);
  1415. begin
  1416. inherited load_references(ppufile,locals);
  1417. end;
  1418. procedure tglobalsymtable.write_references(ppufile:tcompilerppufile;locals:boolean);
  1419. begin
  1420. inherited write_references(ppufile,locals);
  1421. end;
  1422. procedure tglobalsymtable.insert(sym:tsymentry);
  1423. var
  1424. hsym : tsym;
  1425. begin
  1426. hsym:=tsym(search(sym.name));
  1427. if assigned(hsym) then
  1428. begin
  1429. { Delphi you can have a symbol with the same name as the
  1430. unit, the unit can then not be accessed anymore using
  1431. <unit>.<id>, so we can hide the symbol }
  1432. if (m_duplicate_names in aktmodeswitches) and
  1433. (hsym.typ=symconst.unitsym) then
  1434. hsym.owner.rename(hsym.name,'hidden'+hsym.name)
  1435. else
  1436. DuplicateSym(sym,hsym);
  1437. end;
  1438. inherited insert(sym);
  1439. end;
  1440. {$ifdef GDB}
  1441. function tglobalsymtable.getnewtypecount : word;
  1442. begin
  1443. if not (cs_gdb_dbx in aktglobalswitches) then
  1444. getnewtypecount:=inherited getnewtypecount
  1445. else
  1446. begin
  1447. getnewtypecount:=unittypecount;
  1448. inc(unittypecount);
  1449. end;
  1450. end;
  1451. {$endif}
  1452. {****************************************************************************
  1453. TWITHSYMTABLE
  1454. ****************************************************************************}
  1455. constructor twithsymtable.create(aowner:tdef;asymsearch:TDictionary;refnode:pointer{tnode});
  1456. begin
  1457. inherited create('');
  1458. symtabletype:=withsymtable;
  1459. withrefnode:=refnode;
  1460. { we don't need the symsearch }
  1461. symsearch.free;
  1462. { set the defaults }
  1463. symsearch:=asymsearch;
  1464. defowner:=aowner;
  1465. end;
  1466. destructor twithsymtable.destroy;
  1467. begin
  1468. tobject(withrefnode).free;
  1469. symsearch:=nil;
  1470. inherited destroy;
  1471. end;
  1472. procedure twithsymtable.clear;
  1473. begin
  1474. { remove no entry from a withsymtable as it is only a pointer to the
  1475. recorddef or objectdef symtable }
  1476. end;
  1477. {****************************************************************************
  1478. TSTT_ExceptionSymtable
  1479. ****************************************************************************}
  1480. constructor tstt_exceptsymtable.create;
  1481. begin
  1482. inherited create('');
  1483. symtabletype:=stt_exceptsymtable;
  1484. end;
  1485. {****************************************************************************
  1486. TMacroSymtable
  1487. ****************************************************************************}
  1488. constructor tmacrosymtable.create(exported: boolean);
  1489. begin
  1490. inherited create('');
  1491. if exported then
  1492. symtabletype:=exportedmacrosymtable
  1493. else
  1494. symtabletype:=localmacrosymtable;
  1495. symtablelevel:=main_program_level;
  1496. end;
  1497. procedure tmacrosymtable.ppuload(ppufile:tcompilerppufile);
  1498. begin
  1499. next:=macrosymtablestack;
  1500. macrosymtablestack:=self;
  1501. inherited ppuload(ppufile);
  1502. { restore symtablestack }
  1503. macrosymtablestack:=next;
  1504. end;
  1505. {*****************************************************************************
  1506. Helper Routines
  1507. *****************************************************************************}
  1508. function findunitsymtable(st:tsymtable):tsymtable;
  1509. begin
  1510. findunitsymtable:=nil;
  1511. repeat
  1512. if not assigned(st) then
  1513. internalerror(5566561);
  1514. case st.symtabletype of
  1515. localsymtable,
  1516. parasymtable,
  1517. staticsymtable :
  1518. exit;
  1519. globalsymtable :
  1520. begin
  1521. findunitsymtable:=st;
  1522. exit;
  1523. end;
  1524. objectsymtable :
  1525. st:=st.defowner.owner;
  1526. recordsymtable :
  1527. begin
  1528. { don't continue when the current
  1529. symtable is used for variant records }
  1530. if trecorddef(st.defowner).isunion then
  1531. begin
  1532. findunitsymtable:=nil;
  1533. exit;
  1534. end
  1535. else
  1536. st:=st.defowner.owner;
  1537. end;
  1538. else
  1539. internalerror(5566562);
  1540. end;
  1541. until false;
  1542. end;
  1543. function FullTypeName(def,otherdef:tdef):string;
  1544. var
  1545. s1,s2 : string;
  1546. begin
  1547. s1:=def.typename;
  1548. { When the names are the same try to include the unit name }
  1549. if assigned(otherdef) and
  1550. (def.owner.symtabletype in [globalsymtable,staticsymtable]) then
  1551. begin
  1552. s2:=otherdef.typename;
  1553. if upper(s1)=upper(s2) then
  1554. s1:=def.owner.realname^+'.'+s1;
  1555. end;
  1556. FullTypeName:=s1;
  1557. end;
  1558. procedure incompatibletypes(def1,def2:tdef);
  1559. begin
  1560. { When there is an errordef there is already an error message show }
  1561. if (def2.deftype=errordef) or
  1562. (def1.deftype=errordef) then
  1563. exit;
  1564. CGMessage2(type_e_incompatible_types,FullTypeName(def1,def2),FullTypeName(def2,def1));
  1565. end;
  1566. {*****************************************************************************
  1567. Search
  1568. *****************************************************************************}
  1569. function searchsym(const s : stringid;var srsym:tsym;var srsymtable:tsymtable):boolean;
  1570. var
  1571. speedvalue : cardinal;
  1572. topclass : tobjectdef;
  1573. context : tobjectdef;
  1574. begin
  1575. speedvalue:=getspeedvalue(s);
  1576. srsymtable:=symtablestack;
  1577. while assigned(srsymtable) do
  1578. begin
  1579. srsym:=tsym(srsymtable.speedsearch(s,speedvalue));
  1580. if assigned(srsym) then
  1581. begin
  1582. topclass:=nil;
  1583. { use the class from withsymtable only when it is
  1584. defined in this unit }
  1585. if (srsymtable.symtabletype=withsymtable) and
  1586. assigned(srsymtable.defowner) and
  1587. (srsymtable.defowner.deftype=objectdef) and
  1588. (srsymtable.defowner.owner.symtabletype in [globalsymtable,staticsymtable]) and
  1589. (srsymtable.defowner.owner.iscurrentunit) then
  1590. topclass:=tobjectdef(srsymtable.defowner)
  1591. else
  1592. begin
  1593. if assigned(current_procinfo) then
  1594. topclass:=current_procinfo.procdef._class;
  1595. end;
  1596. if assigned(current_procinfo) then
  1597. context:=current_procinfo.procdef._class
  1598. else
  1599. context:=nil;
  1600. if tsym(srsym).is_visible_for_object(topclass,context) then
  1601. begin
  1602. { we need to know if a procedure references symbols
  1603. in the static symtable, because then it can't be
  1604. inlined from outside this unit }
  1605. if assigned(current_procinfo) and
  1606. (srsym.owner.symtabletype=staticsymtable) then
  1607. include(current_procinfo.flags,pi_uses_static_symtable);
  1608. searchsym:=true;
  1609. exit;
  1610. end;
  1611. end;
  1612. srsymtable:=srsymtable.next;
  1613. end;
  1614. searchsym:=false;
  1615. end;
  1616. function searchsym_type(const s : stringid;var srsym:tsym;var srsymtable:tsymtable):boolean;
  1617. var
  1618. speedvalue : cardinal;
  1619. begin
  1620. speedvalue:=getspeedvalue(s);
  1621. srsymtable:=symtablestack;
  1622. while assigned(srsymtable) do
  1623. begin
  1624. {
  1625. It is not possible to have type defintions in:
  1626. records
  1627. objects
  1628. parameters
  1629. }
  1630. if not(srsymtable.symtabletype in [recordsymtable,objectsymtable,parasymtable]) then
  1631. begin
  1632. srsym:=tsym(srsymtable.speedsearch(s,speedvalue));
  1633. if assigned(srsym) and
  1634. (not assigned(current_procinfo) or
  1635. tsym(srsym).is_visible_for_object(current_procinfo.procdef._class,current_procinfo.procdef._class)) then
  1636. begin
  1637. result:=true;
  1638. exit;
  1639. end
  1640. end;
  1641. srsymtable:=srsymtable.next;
  1642. end;
  1643. result:=false;
  1644. end;
  1645. function searchsymonlyin(p : tsymtable;const s : stringid):tsym;
  1646. var
  1647. srsym : tsym;
  1648. begin
  1649. { the caller have to take care if srsym=nil }
  1650. if assigned(p) then
  1651. begin
  1652. srsym:=tsym(p.search(s));
  1653. if assigned(srsym) then
  1654. begin
  1655. searchsymonlyin:=srsym;
  1656. exit;
  1657. end;
  1658. { also check in the local symtbale if it exists }
  1659. if (p.symtabletype=globalsymtable) and
  1660. (p.iscurrentunit) then
  1661. begin
  1662. srsym:=tsym(current_module.localsymtable.search(s));
  1663. if assigned(srsym) then
  1664. begin
  1665. searchsymonlyin:=srsym;
  1666. exit;
  1667. end;
  1668. end
  1669. end;
  1670. searchsymonlyin:=nil;
  1671. end;
  1672. function searchsym_in_class(classh:tobjectdef;const s : stringid):tsym;
  1673. var
  1674. speedvalue : cardinal;
  1675. topclassh : tobjectdef;
  1676. sym : tsym;
  1677. begin
  1678. speedvalue:=getspeedvalue(s);
  1679. { when the class passed is defined in this unit we
  1680. need to use the scope of that class. This is a trick
  1681. that can be used to access protected members in other
  1682. units. At least kylix supports it this way (PFV) }
  1683. if assigned(classh) and
  1684. (classh.owner.symtabletype in [globalsymtable,staticsymtable]) and
  1685. classh.owner.iscurrentunit then
  1686. topclassh:=classh
  1687. else
  1688. begin
  1689. if assigned(current_procinfo) then
  1690. topclassh:=current_procinfo.procdef._class
  1691. else
  1692. topclassh:=nil;
  1693. end;
  1694. sym:=nil;
  1695. while assigned(classh) do
  1696. begin
  1697. sym:=tsym(classh.symtable.speedsearch(s,speedvalue));
  1698. if assigned(sym) and
  1699. tsym(sym).is_visible_for_object(topclassh,current_procinfo.procdef._class) then
  1700. break
  1701. else
  1702. sym:=nil;
  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.iscurrentunit 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.iscurrentunit 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. besteq:tequaltype;
  1810. begin
  1811. st:=symtablestack;
  1812. sv:=getspeedvalue('assign');
  1813. while st<>nil do
  1814. begin
  1815. sym:=Tprocsym(st.speedsearch('assign',sv));
  1816. if sym<>nil then
  1817. begin
  1818. if sym.typ<>procsym then
  1819. internalerror(200402031);
  1820. { if the source type is an alias then this is only the second choice,
  1821. if you mess with this code, check tw4093 }
  1822. search_assignment_operator:=sym.search_procdef_assignment_operator(from_def,to_def,besteq);
  1823. if (search_assignment_operator<>nil) and (besteq=te_exact) then
  1824. break;
  1825. end;
  1826. st:=st.next;
  1827. end;
  1828. end;
  1829. function searchsystype(const s: stringid; var srsym: ttypesym): boolean;
  1830. var
  1831. symowner: tsymtable;
  1832. begin
  1833. if not(cs_compilesystem in aktmoduleswitches) then
  1834. srsym := ttypesym(searchsymonlyin(systemunit,s))
  1835. else
  1836. searchsym(s,tsym(srsym),symowner);
  1837. searchsystype :=
  1838. assigned(srsym) and
  1839. (srsym.typ = typesym);
  1840. end;
  1841. {$ifdef notused}
  1842. function searchsysvar(const s: stringid; var srsym: tsym; var symowner: tsymtable): boolean;
  1843. begin
  1844. if not(cs_compilesystem in aktmoduleswitches) then
  1845. begin
  1846. srsym := searchsymonlyin(systemunit,s);
  1847. symowner := systemunit;
  1848. end
  1849. else
  1850. searchsym(s,tsym(srsym),symowner);
  1851. searchsysvar :=
  1852. assigned(srsym) and
  1853. (srsym.typ = globalvarsym);
  1854. end;
  1855. {$endif notused}
  1856. function search_class_member(pd : tobjectdef;const s : string):tsym;
  1857. { searches n in symtable of pd and all anchestors }
  1858. var
  1859. speedvalue : cardinal;
  1860. srsym : tsym;
  1861. begin
  1862. speedvalue:=getspeedvalue(s);
  1863. while assigned(pd) do
  1864. begin
  1865. srsym:=tsym(pd.symtable.speedsearch(s,speedvalue));
  1866. if assigned(srsym) then
  1867. begin
  1868. search_class_member:=srsym;
  1869. exit;
  1870. end;
  1871. pd:=pd.childof;
  1872. end;
  1873. search_class_member:=nil;
  1874. end;
  1875. function search_macro(const s : string):tsym;
  1876. var
  1877. p : tsymtable;
  1878. speedvalue : cardinal;
  1879. srsym : tsym;
  1880. begin
  1881. speedvalue:= getspeedvalue(s);
  1882. p:=macrosymtablestack;
  1883. while assigned(p) do
  1884. begin
  1885. srsym:=tsym(p.speedsearch(s,speedvalue));
  1886. if assigned(srsym) then
  1887. begin
  1888. search_macro:= srsym;
  1889. exit;
  1890. end;
  1891. p:=p.next;
  1892. end;
  1893. search_macro:= nil;
  1894. end;
  1895. {*****************************************************************************
  1896. Definition Helpers
  1897. *****************************************************************************}
  1898. procedure globaldef(const s : string;var t:ttype);
  1899. var st : string;
  1900. symt : tsymtable;
  1901. srsym : tsym;
  1902. srsymtable : tsymtable;
  1903. begin
  1904. srsym := nil;
  1905. if pos('.',s) > 0 then
  1906. begin
  1907. st := copy(s,1,pos('.',s)-1);
  1908. searchsym(st,srsym,srsymtable);
  1909. st := copy(s,pos('.',s)+1,255);
  1910. if assigned(srsym) then
  1911. begin
  1912. if srsym.typ = unitsym then
  1913. begin
  1914. symt := tunitsym(srsym).unitsymtable;
  1915. srsym := tsym(symt.search(st));
  1916. end else srsym := nil;
  1917. end;
  1918. end else st := s;
  1919. if srsym = nil then
  1920. searchsym(st,srsym,srsymtable);
  1921. if srsym = nil then
  1922. srsym:=searchsymonlyin(systemunit,st);
  1923. if (not assigned(srsym)) or
  1924. (srsym.typ<>typesym) then
  1925. begin
  1926. Message(type_e_type_id_expected);
  1927. t:=generrortype;
  1928. exit;
  1929. end;
  1930. t := ttypesym(srsym).restype;
  1931. end;
  1932. {****************************************************************************
  1933. Object Helpers
  1934. ****************************************************************************}
  1935. procedure search_class_overloads(aprocsym : tprocsym);
  1936. { searches n in symtable of pd and all anchestors }
  1937. var
  1938. speedvalue : cardinal;
  1939. srsym : tprocsym;
  1940. s : string;
  1941. objdef : tobjectdef;
  1942. begin
  1943. if aprocsym.overloadchecked then
  1944. exit;
  1945. aprocsym.overloadchecked:=true;
  1946. if (aprocsym.owner.symtabletype<>objectsymtable) then
  1947. internalerror(200111021);
  1948. objdef:=tobjectdef(aprocsym.owner.defowner);
  1949. { we start in the parent }
  1950. if not assigned(objdef.childof) then
  1951. exit;
  1952. objdef:=objdef.childof;
  1953. s:=aprocsym.name;
  1954. speedvalue:=getspeedvalue(s);
  1955. while assigned(objdef) do
  1956. begin
  1957. srsym:=tprocsym(objdef.symtable.speedsearch(s,speedvalue));
  1958. if assigned(srsym) then
  1959. begin
  1960. if (srsym.typ<>procsym) then
  1961. internalerror(200111022);
  1962. if srsym.is_visible_for_object(tobjectdef(aprocsym.owner.defowner),tobjectdef(aprocsym.owner.defowner)) then
  1963. begin
  1964. srsym.add_para_match_to(Aprocsym,[cpo_ignorehidden,cpo_allowdefaults]);
  1965. { we can stop if the overloads were already added
  1966. for the found symbol }
  1967. if srsym.overloadchecked then
  1968. break;
  1969. end;
  1970. end;
  1971. { next parent }
  1972. objdef:=objdef.childof;
  1973. end;
  1974. end;
  1975. procedure tstoredsymtable.testfordefaultproperty(p : TNamedIndexItem;arg:pointer);
  1976. begin
  1977. if (tsym(p).typ=propertysym) and
  1978. (ppo_defaultproperty in tpropertysym(p).propoptions) then
  1979. ppointer(arg)^:=p;
  1980. end;
  1981. function search_default_property(pd : tobjectdef) : tpropertysym;
  1982. { returns the default property of a class, searches also anchestors }
  1983. var
  1984. _defaultprop : tpropertysym;
  1985. begin
  1986. _defaultprop:=nil;
  1987. while assigned(pd) do
  1988. begin
  1989. pd.symtable.foreach(@tstoredsymtable(pd.symtable).testfordefaultproperty,@_defaultprop);
  1990. if assigned(_defaultprop) then
  1991. break;
  1992. pd:=pd.childof;
  1993. end;
  1994. search_default_property:=_defaultprop;
  1995. end;
  1996. {****************************************************************************
  1997. Macro Helpers
  1998. ****************************************************************************}
  1999. {NOTE: Initially, macrosymtablestack contains initialmacrosymtable.}
  2000. procedure def_system_macro(const name : string);
  2001. var
  2002. mac : tmacro;
  2003. s: string;
  2004. begin
  2005. if name = '' then
  2006. internalerror(2004121201);
  2007. s:= upper(name);
  2008. mac:=tmacro(search_macro(s));
  2009. if not assigned(mac) then
  2010. begin
  2011. mac:=tmacro.create(s);
  2012. if macrosymtablestack.symtabletype=localmacrosymtable then
  2013. macrosymtablestack.insert(mac)
  2014. else
  2015. macrosymtablestack.next.insert(mac)
  2016. end;
  2017. if not mac.defined then
  2018. Message1(parser_c_macro_defined,mac.name);
  2019. mac.defined:=true;
  2020. end;
  2021. procedure set_system_macro(const name, value : string);
  2022. var
  2023. mac : tmacro;
  2024. s: string;
  2025. begin
  2026. if name = '' then
  2027. internalerror(2004121201);
  2028. s:= upper(name);
  2029. mac:=tmacro(search_macro(s));
  2030. if not assigned(mac) then
  2031. begin
  2032. mac:=tmacro.create(s);
  2033. if macrosymtablestack.symtabletype=localmacrosymtable then
  2034. macrosymtablestack.insert(mac)
  2035. else
  2036. macrosymtablestack.next.insert(mac)
  2037. end
  2038. else
  2039. begin
  2040. mac.is_compiler_var:=false;
  2041. if assigned(mac.buftext) then
  2042. freemem(mac.buftext,mac.buflen);
  2043. end;
  2044. Message2(parser_c_macro_set_to,mac.name,value);
  2045. mac.buflen:=length(value);
  2046. getmem(mac.buftext,mac.buflen);
  2047. move(value[1],mac.buftext^,mac.buflen);
  2048. mac.defined:=true;
  2049. end;
  2050. procedure set_system_compvar(const name, value : string);
  2051. var
  2052. mac : tmacro;
  2053. s: string;
  2054. begin
  2055. if name = '' then
  2056. internalerror(2004121201);
  2057. s:= upper(name);
  2058. mac:=tmacro(search_macro(s));
  2059. if not assigned(mac) then
  2060. begin
  2061. mac:=tmacro.create(s);
  2062. mac.is_compiler_var:=true;
  2063. if macrosymtablestack.symtabletype=localmacrosymtable then
  2064. macrosymtablestack.insert(mac)
  2065. else
  2066. macrosymtablestack.next.insert(mac)
  2067. end
  2068. else
  2069. begin
  2070. mac.is_compiler_var:=true;
  2071. if assigned(mac.buftext) then
  2072. freemem(mac.buftext,mac.buflen);
  2073. end;
  2074. Message2(parser_c_macro_set_to,mac.name,value);
  2075. mac.buflen:=length(value);
  2076. getmem(mac.buftext,mac.buflen);
  2077. move(value[1],mac.buftext^,mac.buflen);
  2078. mac.defined:=true;
  2079. end;
  2080. procedure undef_system_macro(const name : string);
  2081. var
  2082. mac : tmacro;
  2083. s: string;
  2084. begin
  2085. if name = '' then
  2086. internalerror(2004121201);
  2087. s:= upper(name);
  2088. mac:=tmacro(search_macro(s));
  2089. if not assigned(mac) then
  2090. {If not found, then it's already undefined.}
  2091. else
  2092. begin
  2093. if mac.defined then
  2094. Message1(parser_c_macro_undefined,mac.name);
  2095. mac.defined:=false;
  2096. mac.is_compiler_var:=false;
  2097. { delete old definition }
  2098. if assigned(mac.buftext) then
  2099. begin
  2100. freemem(mac.buftext,mac.buflen);
  2101. mac.buftext:=nil;
  2102. end;
  2103. end;
  2104. end;
  2105. {$ifdef UNITALIASES}
  2106. {****************************************************************************
  2107. TUNIT_ALIAS
  2108. ****************************************************************************}
  2109. constructor tunit_alias.create(const n:string);
  2110. var
  2111. i : longint;
  2112. begin
  2113. i:=pos('=',n);
  2114. if i=0 then
  2115. fail;
  2116. inherited createname(Copy(n,1,i-1));
  2117. newname:=stringdup(Copy(n,i+1,255));
  2118. end;
  2119. destructor tunit_alias.destroy;
  2120. begin
  2121. stringdispose(newname);
  2122. inherited destroy;
  2123. end;
  2124. procedure addunitalias(const n:string);
  2125. begin
  2126. unitaliases^.insert(tunit_alias,init(Upper(n))));
  2127. end;
  2128. function getunitalias(const n:string):string;
  2129. var
  2130. p : punit_alias;
  2131. begin
  2132. p:=punit_alias(unitaliases^.search(Upper(n)));
  2133. if assigned(p) then
  2134. getunitalias:=punit_alias(p).newname^
  2135. else
  2136. getunitalias:=n;
  2137. end;
  2138. {$endif UNITALIASES}
  2139. {****************************************************************************
  2140. Symtable Stack
  2141. ****************************************************************************}
  2142. {$ifdef DEBUG}
  2143. procedure test_symtablestack;
  2144. var
  2145. p : tsymtable;
  2146. i : longint;
  2147. begin
  2148. p:=symtablestack;
  2149. i:=0;
  2150. while assigned(p) do
  2151. begin
  2152. inc(i);
  2153. p:=p.next;
  2154. if i>500 then
  2155. Message(sym_f_internal_error_in_symtablestack);
  2156. end;
  2157. end;
  2158. procedure list_symtablestack;
  2159. var
  2160. p : tsymtable;
  2161. i : longint;
  2162. begin
  2163. p:=symtablestack;
  2164. i:=0;
  2165. while assigned(p) do
  2166. begin
  2167. inc(i);
  2168. writeln(i,' ',p.name^);
  2169. p:=p.next;
  2170. if i>500 then
  2171. Message(sym_f_internal_error_in_symtablestack);
  2172. end;
  2173. end;
  2174. {$endif DEBUG}
  2175. {****************************************************************************
  2176. Init/Done Symtable
  2177. ****************************************************************************}
  2178. procedure InitSymtable;
  2179. begin
  2180. { Reset symbolstack }
  2181. registerdef:=false;
  2182. symtablestack:=nil;
  2183. macrosymtablestack:=nil;
  2184. systemunit:=nil;
  2185. {$ifdef GDB}
  2186. globaltypecount:=1;
  2187. pglobaltypecount:=@globaltypecount;
  2188. {$endif GDB}
  2189. { create error syms and def }
  2190. generrorsym:=terrorsym.create;
  2191. generrortype.setdef(terrordef.create);
  2192. {$ifdef UNITALIASES}
  2193. { unit aliases }
  2194. unitaliases:=tdictionary.create;
  2195. {$endif}
  2196. initialmacrosymtable:= tmacrosymtable.create(false);
  2197. macrosymtablestack:= initialmacrosymtable;
  2198. { set some global vars to nil, might be important for the ide }
  2199. class_tobject:=nil;
  2200. interface_iunknown:=nil;
  2201. rec_tguid:=nil;
  2202. dupnr:=0;
  2203. end;
  2204. procedure DoneSymtable;
  2205. begin
  2206. generrorsym.free;
  2207. generrortype.def.free;
  2208. {$ifdef UNITALIASES}
  2209. unitaliases.free;
  2210. {$endif}
  2211. initialmacrosymtable.Free;
  2212. end;
  2213. end.