symtable.pas 77 KB

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