symtable.pas 79 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl, Pierre Muller
  4. This unit handles the symbol tables
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit symtable;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. { common }
  23. cutils,cclasses,
  24. { global }
  25. cpuinfo,globtype,tokens,
  26. { symtable }
  27. symconst,symbase,symtype,symdef,symsym,
  28. { ppu }
  29. ppu,symppu,
  30. { assembler }
  31. aasmbase,aasmtai,aasmcpu,
  32. { cg }
  33. paramgr
  34. ;
  35. {****************************************************************************
  36. Symtable types
  37. ****************************************************************************}
  38. type
  39. tstoredsymtable = class(tsymtable)
  40. private
  41. b_needs_init_final : boolean;
  42. procedure _needs_init_final(p : tnamedindexitem;arg:pointer);
  43. procedure check_forward(sym : TNamedIndexItem;arg:pointer);
  44. procedure labeldefined(p : TNamedIndexItem;arg:pointer);
  45. procedure unitsymbolused(p : TNamedIndexItem;arg:pointer);
  46. procedure varsymbolused(p : TNamedIndexItem;arg:pointer);
  47. procedure TestPrivate(p : TNamedIndexItem;arg:pointer);
  48. procedure objectprivatesymbolused(p : TNamedIndexItem;arg:pointer);
  49. {$ifdef GDB}
  50. private
  51. procedure concatstab(p : TNamedIndexItem;arg:pointer);
  52. procedure resetstab(p : TNamedIndexItem;arg:pointer);
  53. procedure concattypestab(p : TNamedIndexItem;arg:pointer);
  54. {$endif}
  55. procedure unchain_overloads(p : TNamedIndexItem;arg:pointer);
  56. procedure loaddefs(ppufile:tcompilerppufile);
  57. procedure loadsyms(ppufile:tcompilerppufile);
  58. procedure writedefs(ppufile:tcompilerppufile);
  59. procedure writesyms(ppufile:tcompilerppufile);
  60. public
  61. { load/write }
  62. procedure ppuload(ppufile:tcompilerppufile);virtual;
  63. procedure ppuwrite(ppufile:tcompilerppufile);virtual;
  64. procedure load_references(ppufile:tcompilerppufile;locals:boolean);virtual;
  65. procedure write_references(ppufile:tcompilerppufile;locals:boolean);virtual;
  66. procedure deref;virtual;
  67. procedure derefimpl;virtual;
  68. procedure insert(sym : tsymentry);override;
  69. function speedsearch(const s : stringid;speedvalue : cardinal) : tsymentry;override;
  70. procedure allsymbolsused;
  71. procedure allprivatesused;
  72. procedure allunitsused;
  73. procedure check_forwards;
  74. procedure checklabels;
  75. function needs_init_final : boolean;
  76. procedure unchain_overloaded;
  77. procedure chainoperators;
  78. {$ifdef GDB}
  79. procedure concatstabto(asmlist : taasmoutput);virtual;
  80. function getnewtypecount : word; override;
  81. {$endif GDB}
  82. procedure testfordefaultproperty(p : TNamedIndexItem;arg:pointer);
  83. end;
  84. tabstractrecordsymtable = class(tstoredsymtable)
  85. public
  86. procedure ppuload(ppufile:tcompilerppufile);override;
  87. procedure ppuwrite(ppufile:tcompilerppufile);override;
  88. procedure load_references(ppufile:tcompilerppufile;locals:boolean);override;
  89. procedure write_references(ppufile:tcompilerppufile;locals:boolean);override;
  90. procedure insertvardata(sym : tsymentry);override;
  91. end;
  92. trecordsymtable = class(tabstractrecordsymtable)
  93. public
  94. constructor create;
  95. procedure insert_in(tsymt : tsymtable;offset : longint);
  96. end;
  97. tobjectsymtable = class(tabstractrecordsymtable)
  98. public
  99. constructor create(const n:string);
  100. procedure insert(sym : tsymentry);override;
  101. end;
  102. tabstractlocalsymtable = class(tstoredsymtable)
  103. public
  104. procedure ppuload(ppufile:tcompilerppufile);override;
  105. procedure ppuwrite(ppufile:tcompilerppufile);override;
  106. procedure load_references(ppufile:tcompilerppufile;locals:boolean);override;
  107. procedure write_references(ppufile:tcompilerppufile;locals:boolean);override;
  108. end;
  109. tlocalsymtable = class(tabstractlocalsymtable)
  110. public
  111. constructor create;
  112. procedure insert(sym : tsymentry);override;
  113. procedure insertvardata(sym : tsymentry);override;
  114. procedure insertconstdata(sym : tsymentry);override;
  115. end;
  116. tparasymtable = class(tabstractlocalsymtable)
  117. public
  118. constructor create;
  119. procedure insert(sym : tsymentry);override;
  120. procedure insertvardata(sym : tsymentry);override;
  121. end;
  122. tabstractunitsymtable = class(tstoredsymtable)
  123. public
  124. {$ifdef GDB}
  125. dbx_count : longint;
  126. prev_dbx_counter : plongint;
  127. dbx_count_ok : boolean;
  128. is_stab_written : boolean;
  129. {$endif GDB}
  130. constructor create(const n : string);
  131. {$ifdef GDB}
  132. procedure concattypestabto(asmlist : taasmoutput);
  133. {$endif GDB}
  134. procedure insertvardata(sym : tsymentry);override;
  135. procedure insertconstdata(sym : tsymentry);override;
  136. end;
  137. tglobalsymtable = class(tabstractunitsymtable)
  138. public
  139. unittypecount : word;
  140. unitsym : tunitsym;
  141. constructor create(const n : string);
  142. destructor destroy;override;
  143. procedure ppuload(ppufile:tcompilerppufile);override;
  144. procedure ppuwrite(ppufile:tcompilerppufile);override;
  145. procedure insert(sym : tsymentry);override;
  146. procedure insertvardata(sym : tsymentry);override;
  147. {$ifdef GDB}
  148. function getnewtypecount : word; override;
  149. {$endif}
  150. end;
  151. tstaticsymtable = class(tabstractunitsymtable)
  152. public
  153. constructor create(const n : string);
  154. procedure ppuload(ppufile:tcompilerppufile);override;
  155. procedure ppuwrite(ppufile:tcompilerppufile);override;
  156. procedure load_references(ppufile:tcompilerppufile;locals:boolean);override;
  157. procedure write_references(ppufile:tcompilerppufile;locals:boolean);override;
  158. procedure insert(sym : tsymentry);override;
  159. procedure insertvardata(sym : tsymentry);override;
  160. end;
  161. twithsymtable = class(tsymtable)
  162. direct_with : boolean;
  163. { in fact it is a tnode }
  164. withnode : pointer;
  165. { tnode to load of direct with var }
  166. { already usable before firstwith
  167. needed for firstpass of function parameters PM }
  168. withrefnode : pointer;
  169. constructor create(aowner:tdef;asymsearch:TDictionary);
  170. destructor destroy;override;
  171. procedure clear;override;
  172. end;
  173. tstt_exceptsymtable = class(tsymtable)
  174. public
  175. constructor create;
  176. end;
  177. var
  178. constsymtable : tsymtable; { symtable were the constants can be inserted }
  179. systemunit : tglobalsymtable; { pointer to the system unit }
  180. read_member : boolean; { reading members of an symtable }
  181. lexlevel : longint; { level of code }
  182. { 1 for main procedure }
  183. { 2 for normal function or proc }
  184. { higher for locals }
  185. {****************************************************************************
  186. Functions
  187. ****************************************************************************}
  188. {*** Misc ***}
  189. procedure globaldef(const s : string;var t:ttype);
  190. function findunitsymtable(st:tsymtable):tsymtable;
  191. procedure duplicatesym(sym:tsym);
  192. {*** Search ***}
  193. function searchsym(const s : stringid;var srsym:tsym;var srsymtable:tsymtable):boolean;
  194. function searchsymonlyin(p : tsymtable;const s : stringid):tsym;
  195. function searchsym_in_class(classh:tobjectdef;const s : stringid):tsym;
  196. function searchsystype(const s: stringid; var srsym: ttypesym): boolean;
  197. function searchsysvar(const s: stringid; var srsym: tvarsym; var symowner: tsymtable): boolean;
  198. function search_class_member(pd : tobjectdef;const s : string):tsym;
  199. {*** Object Helpers ***}
  200. function search_default_property(pd : tobjectdef) : tpropertysym;
  201. {*** symtable stack ***}
  202. procedure dellexlevel;
  203. procedure RestoreUnitSyms;
  204. {$ifdef DEBUG}
  205. procedure test_symtablestack;
  206. procedure list_symtablestack;
  207. {$endif DEBUG}
  208. {$ifdef UNITALIASES}
  209. type
  210. punit_alias = ^tunit_alias;
  211. tunit_alias = object(TNamedIndexItem)
  212. newname : pstring;
  213. constructor init(const n:string);
  214. destructor done;virtual;
  215. end;
  216. var
  217. unitaliases : pdictionary;
  218. procedure addunitalias(const n:string);
  219. function getunitalias(const n:string):string;
  220. {$endif UNITALIASES}
  221. {*** Init / Done ***}
  222. procedure InitSymtable;
  223. procedure DoneSymtable;
  224. type
  225. toverloaded_operators = array[NOTOKEN..last_overloaded] of tprocsym;
  226. var
  227. overloaded_operators : toverloaded_operators;
  228. { unequal is not equal}
  229. const
  230. overloaded_names : array [NOTOKEN..last_overloaded] of string[16] =
  231. ('error',
  232. 'plus','minus','star','slash','equal',
  233. 'greater','lower','greater_or_equal',
  234. 'lower_or_equal',
  235. 'sym_diff','starstar',
  236. 'as','is','in','or',
  237. 'and','div','mod','not','shl','shr','xor',
  238. 'assign');
  239. implementation
  240. uses
  241. { global }
  242. verbose,globals,
  243. { target }
  244. systems,
  245. { module }
  246. fmodule,
  247. {$ifdef GDB}
  248. gdb,
  249. {$endif GDB}
  250. { codegen }
  251. cgbase
  252. ;
  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. ibansistringdef : hp:=tstringdef.loadansi(ppufile);
  291. ibwidestringdef : hp:=tstringdef.loadwide(ppufile);
  292. ibrecorddef : hp:=trecorddef.ppuload(ppufile);
  293. ibobjectdef : hp:=tobjectdef.ppuload(ppufile);
  294. ibenumdef : hp:=tenumdef.ppuload(ppufile);
  295. ibsetdef : hp:=tsetdef.ppuload(ppufile);
  296. ibprocvardef : hp:=tprocvardef.ppuload(ppufile);
  297. ibfiledef : hp:=tfiledef.ppuload(ppufile);
  298. ibclassrefdef : hp:=tclassrefdef.ppuload(ppufile);
  299. ibformaldef : hp:=tformaldef.ppuload(ppufile);
  300. ibvariantdef : hp:=tvariantdef.ppuload(ppufile);
  301. ibenddefs : break;
  302. ibend : Message(unit_f_ppu_read_error);
  303. else
  304. Message1(unit_f_ppu_invalid_entry,tostr(b));
  305. end;
  306. hp.owner:=self;
  307. defindex.insert(hp);
  308. until false;
  309. end;
  310. procedure tstoredsymtable.loadsyms(ppufile:tcompilerppufile);
  311. var
  312. b : byte;
  313. sym : tsym;
  314. begin
  315. { load start of definition section, which holds the amount of defs }
  316. if ppufile.readentry<>ibstartsyms then
  317. Message(unit_f_ppu_read_error);
  318. { skip amount of symbols, not used currently }
  319. ppufile.getlongint;
  320. { load datasize,dataalignment of this symboltable }
  321. datasize:=ppufile.getlongint;
  322. dataalignment:=ppufile.getlongint;
  323. { now read the symbols }
  324. repeat
  325. b:=ppufile.readentry;
  326. case b of
  327. ibtypesym : sym:=ttypesym.ppuload(ppufile);
  328. ibprocsym : sym:=tprocsym.ppuload(ppufile);
  329. ibconstsym : sym:=tconstsym.ppuload(ppufile);
  330. ibvarsym : sym:=tvarsym.ppuload(ppufile);
  331. ibfuncretsym : sym:=tfuncretsym.ppuload(ppufile);
  332. ibabsolutesym : sym:=tabsolutesym.ppuload(ppufile);
  333. ibenumsym : sym:=tenumsym.ppuload(ppufile);
  334. ibtypedconstsym : sym:=ttypedconstsym.ppuload(ppufile);
  335. ibpropertysym : sym:=tpropertysym.ppuload(ppufile);
  336. ibunitsym : sym:=tunitsym.ppuload(ppufile);
  337. iblabelsym : sym:=tlabelsym.ppuload(ppufile);
  338. ibsyssym : sym:=tsyssym.ppuload(ppufile);
  339. ibrttisym : sym:=trttisym.ppuload(ppufile);
  340. ibendsyms : break;
  341. ibend : Message(unit_f_ppu_read_error);
  342. else
  343. Message1(unit_f_ppu_invalid_entry,tostr(b));
  344. end;
  345. sym.owner:=self;
  346. symindex.insert(sym);
  347. symsearch.insert(sym);
  348. until false;
  349. end;
  350. procedure tstoredsymtable.writedefs(ppufile:tcompilerppufile);
  351. var
  352. pd : tstoreddef;
  353. begin
  354. { each definition get a number, write then the amount of defs to the
  355. ibstartdef entry }
  356. ppufile.putlongint(defindex.count);
  357. ppufile.writeentry(ibstartdefs);
  358. { now write the definition }
  359. pd:=tstoreddef(defindex.first);
  360. while assigned(pd) do
  361. begin
  362. pd.ppuwrite(ppufile);
  363. pd:=tstoreddef(pd.indexnext);
  364. end;
  365. { write end of definitions }
  366. ppufile.writeentry(ibenddefs);
  367. end;
  368. procedure tstoredsymtable.writesyms(ppufile:tcompilerppufile);
  369. var
  370. pd : tstoredsym;
  371. begin
  372. { each definition get a number, write then the amount of syms and the
  373. datasize to the ibsymdef entry }
  374. ppufile.putlongint(symindex.count);
  375. ppufile.putlongint(datasize);
  376. ppufile.putlongint(dataalignment);
  377. ppufile.writeentry(ibstartsyms);
  378. { foreach is used to write all symbols }
  379. pd:=tstoredsym(symindex.first);
  380. while assigned(pd) do
  381. begin
  382. pd.ppuwrite(ppufile);
  383. pd:=tstoredsym(pd.indexnext);
  384. end;
  385. { end of symbols }
  386. ppufile.writeentry(ibendsyms);
  387. end;
  388. procedure tstoredsymtable.load_references(ppufile:tcompilerppufile;locals:boolean);
  389. var
  390. b : byte;
  391. sym : tstoredsym;
  392. prdef : tstoreddef;
  393. begin
  394. b:=ppufile.readentry;
  395. if b <> ibbeginsymtablebrowser then
  396. Message1(unit_f_ppu_invalid_entry,tostr(b));
  397. repeat
  398. b:=ppufile.readentry;
  399. case b of
  400. ibsymref :
  401. begin
  402. sym:=tstoredsym(ppufile.getderef);
  403. resolvesym(pointer(sym));
  404. if assigned(sym) then
  405. sym.load_references(ppufile,locals);
  406. end;
  407. ibdefref :
  408. begin
  409. prdef:=tstoreddef(ppufile.getderef);
  410. resolvedef(pointer(prdef));
  411. if assigned(prdef) then
  412. begin
  413. if prdef.deftype<>procdef then
  414. Message(unit_f_ppu_read_error);
  415. tprocdef(prdef).load_references(ppufile,locals);
  416. end;
  417. end;
  418. ibendsymtablebrowser :
  419. break;
  420. else
  421. Message1(unit_f_ppu_invalid_entry,tostr(b));
  422. end;
  423. until false;
  424. end;
  425. procedure tstoredsymtable.write_references(ppufile:tcompilerppufile;locals:boolean);
  426. var
  427. pd : tstoredsym;
  428. begin
  429. ppufile.writeentry(ibbeginsymtablebrowser);
  430. { write all symbols }
  431. pd:=tstoredsym(symindex.first);
  432. while assigned(pd) do
  433. begin
  434. pd.write_references(ppufile,locals);
  435. pd:=tstoredsym(pd.indexnext);
  436. end;
  437. ppufile.writeentry(ibendsymtablebrowser);
  438. end;
  439. procedure tstoredsymtable.deref;
  440. var
  441. hp : tdef;
  442. hs : tsym;
  443. begin
  444. { deref the interface definitions }
  445. hp:=tdef(defindex.first);
  446. while assigned(hp) do
  447. begin
  448. hp.deref;
  449. hp:=tdef(hp.indexnext);
  450. end;
  451. { first deref the interface ttype symbols }
  452. hs:=tsym(symindex.first);
  453. while assigned(hs) do
  454. begin
  455. if hs.typ=typesym then
  456. hs.deref;
  457. hs:=tsym(hs.indexnext);
  458. end;
  459. { deref the interface symbols }
  460. hs:=tsym(symindex.first);
  461. while assigned(hs) do
  462. begin
  463. if hs.typ<>typesym then
  464. hs.deref;
  465. hs:=tsym(hs.indexnext);
  466. end;
  467. end;
  468. procedure tstoredsymtable.derefimpl;
  469. var
  470. hp : tdef;
  471. begin
  472. { deref the implementation part of definitions }
  473. hp:=tdef(defindex.first);
  474. while assigned(hp) do
  475. begin
  476. hp.derefimpl;
  477. hp:=tdef(hp.indexnext);
  478. end;
  479. end;
  480. procedure tstoredsymtable.insert(sym:tsymentry);
  481. var
  482. hsym : tsym;
  483. begin
  484. { set owner and sym indexnb }
  485. sym.owner:=self;
  486. { check the current symtable }
  487. hsym:=tsym(search(sym.name));
  488. if assigned(hsym) then
  489. begin
  490. { in TP and Delphi you can have a local with the
  491. same name as the function, the function is then hidden for
  492. the user. (Under delphi it can still be accessed using result),
  493. but don't allow hiding of RESULT }
  494. if (m_duplicate_names in aktmodeswitches) and
  495. (hsym.typ=funcretsym) and
  496. not((m_result in aktmodeswitches) and
  497. (hsym.name='RESULT')) then
  498. hsym.owner.rename(hsym.name,'hidden'+hsym.name)
  499. else
  500. begin
  501. DuplicateSym(hsym);
  502. exit;
  503. end;
  504. end;
  505. { register definition of typesym }
  506. if (sym.typ = typesym) and
  507. assigned(ttypesym(sym).restype.def) then
  508. begin
  509. if not(assigned(ttypesym(sym).restype.def.owner)) and
  510. (ttypesym(sym).restype.def.deftype<>errordef) then
  511. registerdef(ttypesym(sym).restype.def);
  512. {$ifdef GDB}
  513. if (cs_debuginfo in aktmoduleswitches) and assigned(debuglist) and
  514. (symtabletype in [globalsymtable,staticsymtable]) then
  515. begin
  516. ttypesym(sym).isusedinstab := true;
  517. {sym.concatstabto(debuglist);}
  518. end;
  519. {$endif GDB}
  520. end;
  521. { insert in index and search hash }
  522. symindex.insert(sym);
  523. symsearch.insert(sym);
  524. end;
  525. function tstoredsymtable.speedsearch(const s : stringid;speedvalue : cardinal) : tsymentry;
  526. var
  527. hp : tstoredsym;
  528. newref : tref;
  529. begin
  530. hp:=tstoredsym(inherited speedsearch(s,speedvalue));
  531. if assigned(hp) then
  532. begin
  533. { reject non static members in static procedures }
  534. if (symtabletype=objectsymtable) and
  535. not(sp_static in hp.symoptions) and
  536. allow_only_static then
  537. Message(sym_e_only_static_in_static);
  538. { unit uses count }
  539. if (unitid<>0) and
  540. (symtabletype = globalsymtable) and
  541. assigned(tglobalsymtable(self).unitsym) then
  542. inc(tglobalsymtable(self).unitsym.refs);
  543. {$ifdef GDB}
  544. { if it is a type, we need the stabs of this type
  545. this might be the cause of the class debug problems
  546. as TCHILDCLASS.Create did not generate appropriate
  547. stabs debug info if TCHILDCLASS wasn't used anywhere else PM }
  548. if (cs_debuginfo in aktmoduleswitches) and
  549. (hp.typ=typesym) and
  550. make_ref then
  551. begin
  552. if assigned(ttypesym(hp).restype.def) then
  553. tstoreddef(ttypesym(hp).restype.def).numberstring
  554. else
  555. ttypesym(hp).isusedinstab:=true;
  556. end;
  557. {$endif GDB}
  558. { unitsym are only loaded for browsing PM }
  559. { this was buggy anyway because we could use }
  560. { unitsyms from other units in _USES !! }
  561. {if (symtabletype=unitsymtable) and (hp.typ=unitsym) and
  562. assigned(current_module) and (current_module.globalsymtable<>.load) then
  563. hp:=nil;}
  564. if assigned(hp) and
  565. make_ref and
  566. (cs_browser in aktmoduleswitches) then
  567. begin
  568. newref:=tref.create(hp.lastref,@akttokenpos);
  569. { for symbols that are in tables without
  570. browser info or syssyms (PM) }
  571. if hp.refcount=0 then
  572. begin
  573. hp.defref:=newref;
  574. hp.lastref:=newref;
  575. end
  576. else
  577. if resolving_forward and assigned(hp.defref) then
  578. { put it as second reference }
  579. begin
  580. newref.nextref:=hp.defref.nextref;
  581. hp.defref.nextref:=newref;
  582. hp.lastref.nextref:=nil;
  583. end
  584. else
  585. hp.lastref:=newref;
  586. inc(hp.refcount);
  587. end;
  588. if assigned(hp) and make_ref then
  589. begin
  590. inc(hp.refs);
  591. end;
  592. end;
  593. speedsearch:=hp;
  594. end;
  595. {**************************************
  596. Callbacks
  597. **************************************}
  598. procedure TStoredSymtable.check_forward(sym : TNamedIndexItem;arg:pointer);
  599. begin
  600. if tsym(sym).typ=procsym then
  601. tprocsym(sym).check_forward
  602. { check also object method table }
  603. { we needn't to test the def list }
  604. { because each object has to have a type sym }
  605. else
  606. if (tsym(sym).typ=typesym) and
  607. assigned(ttypesym(sym).restype.def) and
  608. (ttypesym(sym).restype.def.deftype=objectdef) then
  609. tobjectdef(ttypesym(sym).restype.def).check_forwards;
  610. end;
  611. procedure TStoredSymtable.labeldefined(p : TNamedIndexItem;arg:pointer);
  612. begin
  613. if (tsym(p).typ=labelsym) and
  614. not(tlabelsym(p).defined) then
  615. begin
  616. if tlabelsym(p).used then
  617. Message1(sym_e_label_used_and_not_defined,tlabelsym(p).realname)
  618. else
  619. Message1(sym_w_label_not_defined,tlabelsym(p).realname);
  620. end;
  621. end;
  622. procedure TStoredSymtable.unitsymbolused(p : TNamedIndexItem;arg:pointer);
  623. begin
  624. if (tsym(p).typ=unitsym) and
  625. (tunitsym(p).refs=0) and
  626. { do not claim for unit name itself !! }
  627. assigned(tunitsym(p).unitsymtable) and
  628. (tunitsym(p).unitsymtable.symtabletype=globalsymtable) then
  629. MessagePos2(tsym(p).fileinfo,sym_n_unit_not_used,p.name,current_module.modulename^);
  630. end;
  631. procedure TStoredSymtable.varsymbolused(p : TNamedIndexItem;arg:pointer);
  632. begin
  633. if (tsym(p).typ=varsym) and
  634. ((tsym(p).owner.symtabletype in
  635. [parasymtable,localsymtable,objectsymtable,staticsymtable])) then
  636. begin
  637. { unused symbol should be reported only if no }
  638. { error is reported }
  639. { if the symbol is in a register it is used }
  640. { also don't count the value parameters which have local copies }
  641. { also don't claim for high param of open parameters (PM) }
  642. if (Errorcount<>0) or
  643. (copy(p.name,1,3)='val') or
  644. (copy(p.name,1,4)='high') then
  645. exit;
  646. if (tvarsym(p).refs=0) then
  647. begin
  648. if (tsym(p).owner.symtabletype=parasymtable) or (vo_is_local_copy in tvarsym(p).varoptions) then
  649. begin
  650. MessagePos1(tsym(p).fileinfo,sym_h_para_identifier_not_used,tsym(p).realname);
  651. end
  652. else if (tsym(p).owner.symtabletype=objectsymtable) then
  653. MessagePos2(tsym(p).fileinfo,sym_n_private_identifier_not_used,tsym(p).owner.realname^,tsym(p).realname)
  654. else
  655. MessagePos1(tsym(p).fileinfo,sym_n_local_identifier_not_used,tsym(p).realname);
  656. end
  657. else if tvarsym(p).varstate=vs_assigned then
  658. begin
  659. if (tsym(p).owner.symtabletype=parasymtable) then
  660. begin
  661. if not(tvarsym(p).varspez in [vs_var,vs_out]) then
  662. MessagePos1(tsym(p).fileinfo,sym_h_para_identifier_only_set,tsym(p).realname)
  663. end
  664. else if (vo_is_local_copy in tvarsym(p).varoptions) then
  665. begin
  666. if not(tvarsym(p).varspez in [vs_var,vs_out]) then
  667. MessagePos1(tsym(p).fileinfo,sym_h_para_identifier_only_set,tsym(p).realname);
  668. end
  669. else if (tsym(p).owner.symtabletype=objectsymtable) then
  670. MessagePos2(tsym(p).fileinfo,sym_n_private_identifier_only_set,tsym(p).owner.realname^,tsym(p).realname)
  671. else if (tsym(p).owner.symtabletype<>parasymtable) then
  672. if not (vo_is_exported in tvarsym(p).varoptions) then
  673. MessagePos1(tsym(p).fileinfo,sym_n_local_identifier_only_set,tsym(p).realname);
  674. end;
  675. end
  676. else if ((tsym(p).owner.symtabletype in
  677. [objectsymtable,parasymtable,localsymtable,staticsymtable])) then
  678. begin
  679. if (Errorcount<>0) then
  680. exit;
  681. { do not claim for inherited private fields !! }
  682. if (tstoredsym(p).refs=0) and (tsym(p).owner.symtabletype=objectsymtable) then
  683. MessagePos2(tsym(p).fileinfo,sym_n_private_method_not_used,tsym(p).owner.realname^,tsym(p).realname)
  684. { units references are problematic }
  685. else if (tstoredsym(p).refs=0) and not(tsym(p).typ in [funcretsym,enumsym,unitsym]) then
  686. if (tsym(p).typ<>procsym) or not (tprocsym(p).is_global) or
  687. { all program functions are declared global
  688. but unused should still be signaled PM }
  689. ((tsym(p).owner.symtabletype=staticsymtable) and
  690. not current_module.is_unit) then
  691. MessagePos2(tsym(p).fileinfo,sym_h_local_symbol_not_used,SymTypeName[tsym(p).typ],tsym(p).realname);
  692. end;
  693. end;
  694. procedure TStoredSymtable.TestPrivate(p : TNamedIndexItem;arg:pointer);
  695. begin
  696. if sp_private in tsym(p).symoptions then
  697. varsymbolused(p,arg);
  698. end;
  699. procedure TStoredSymtable.objectprivatesymbolused(p : TNamedIndexItem;arg:pointer);
  700. begin
  701. {
  702. Don't test simple object aliases PM
  703. }
  704. if (tsym(p).typ=typesym) and
  705. (ttypesym(p).restype.def.deftype=objectdef) and
  706. (ttypesym(p).restype.def.typesym=tsym(p)) then
  707. tobjectdef(ttypesym(p).restype.def).symtable.foreach({$ifdef FPCPROCVAR}@{$endif}TestPrivate,nil);
  708. end;
  709. procedure tstoredsymtable.unchain_overloads(p : TNamedIndexItem;arg:pointer);
  710. begin
  711. if tsym(p).typ=procsym then
  712. tprocsym(p).unchain_overload;
  713. end;
  714. {$ifdef GDB}
  715. procedure TStoredSymtable.concatstab(p : TNamedIndexItem;arg:pointer);
  716. begin
  717. if tsym(p).typ <> procsym then
  718. tstoredsym(p).concatstabto(TAAsmOutput(arg));
  719. end;
  720. procedure TStoredSymtable.resetstab(p : TNamedIndexItem;arg:pointer);
  721. begin
  722. if tsym(p).typ <> procsym then
  723. tstoredsym(p).isstabwritten:=false;
  724. end;
  725. procedure TStoredSymtable.concattypestab(p : TNamedIndexItem;arg:pointer);
  726. begin
  727. if tsym(p).typ = typesym then
  728. begin
  729. tstoredsym(p).isstabwritten:=false;
  730. tstoredsym(p).concatstabto(TAAsmOutput(arg));
  731. end;
  732. end;
  733. function tstoredsymtable.getnewtypecount : word;
  734. begin
  735. getnewtypecount:=pglobaltypecount^;
  736. inc(pglobaltypecount^);
  737. end;
  738. {$endif GDB}
  739. procedure tstoredsymtable.chainoperators;
  740. var
  741. t : ttoken;
  742. srsym : tsym;
  743. srsymtable,
  744. storesymtablestack : tsymtable;
  745. begin
  746. storesymtablestack:=symtablestack;
  747. symtablestack:=self;
  748. make_ref:=false;
  749. for t:=first_overloaded to last_overloaded do
  750. begin
  751. overloaded_operators[t]:=nil;
  752. { each operator has a unique lowercased internal name PM }
  753. while assigned(symtablestack) do
  754. begin
  755. searchsym(overloaded_names[t],srsym,srsymtable);
  756. if not assigned(srsym) then
  757. begin
  758. if (t=_STARSTAR) then
  759. begin
  760. symtablestack:=systemunit;
  761. searchsym('POWER',srsym,srsymtable);
  762. end;
  763. end;
  764. if assigned(srsym) then
  765. begin
  766. if (srsym.typ<>procsym) then
  767. internalerror(12344321);
  768. { use this procsym as start ? }
  769. if not assigned(overloaded_operators[t]) then
  770. overloaded_operators[t]:=tprocsym(srsym)
  771. else
  772. { already got a procsym, only add defs of the current procsym }
  773. Tprocsym(srsym).concat_procdefs_to(overloaded_operators[t]);
  774. symtablestack:=srsym.owner.next;
  775. end
  776. else
  777. begin
  778. symtablestack:=nil;
  779. end;
  780. { search for same procsym in other units }
  781. end;
  782. symtablestack:=self;
  783. end;
  784. make_ref:=true;
  785. symtablestack:=storesymtablestack;
  786. end;
  787. {***********************************************
  788. Process all entries
  789. ***********************************************}
  790. { checks, if all procsyms and methods are defined }
  791. procedure tstoredsymtable.check_forwards;
  792. begin
  793. foreach({$ifdef FPCPROCVAR}@{$endif}check_forward,nil);
  794. end;
  795. procedure tstoredsymtable.checklabels;
  796. begin
  797. foreach({$ifdef FPCPROCVAR}@{$endif}labeldefined,nil);
  798. end;
  799. procedure tstoredsymtable.allunitsused;
  800. begin
  801. foreach({$ifdef FPCPROCVAR}@{$endif}unitsymbolused,nil);
  802. end;
  803. procedure tstoredsymtable.allsymbolsused;
  804. begin
  805. foreach({$ifdef FPCPROCVAR}@{$endif}varsymbolused,nil);
  806. end;
  807. procedure tstoredsymtable.allprivatesused;
  808. begin
  809. foreach({$ifdef FPCPROCVAR}@{$endif}objectprivatesymbolused,nil);
  810. end;
  811. procedure tstoredsymtable.unchain_overloaded;
  812. begin
  813. foreach({$ifdef FPCPROCVAR}@{$endif}unchain_overloads,nil);
  814. end;
  815. {$ifdef GDB}
  816. procedure tstoredsymtable.concatstabto(asmlist : taasmoutput);
  817. begin
  818. if symtabletype in [inlineparasymtable,inlinelocalsymtable] then
  819. foreach({$ifdef FPCPROCVAR}@{$endif}resetstab,nil);
  820. foreach({$ifdef FPCPROCVAR}@{$endif}concatstab,asmlist);
  821. end;
  822. {$endif}
  823. procedure TStoredSymtable._needs_init_final(p : tnamedindexitem;arg:pointer);
  824. begin
  825. if (not b_needs_init_final) and
  826. (tsym(p).typ=varsym) and
  827. assigned(tvarsym(p).vartype.def) and
  828. not is_class(tvarsym(p).vartype.def) and
  829. tstoreddef(tvarsym(p).vartype.def).needs_inittable then
  830. b_needs_init_final:=true;
  831. end;
  832. { returns true, if p contains data which needs init/final code }
  833. function tstoredsymtable.needs_init_final : boolean;
  834. begin
  835. b_needs_init_final:=false;
  836. foreach({$ifdef FPCPROCVAR}@{$endif}_needs_init_final,nil);
  837. needs_init_final:=b_needs_init_final;
  838. end;
  839. {****************************************************************************
  840. TAbstractRecordSymtable
  841. ****************************************************************************}
  842. procedure tabstractrecordsymtable.ppuload(ppufile:tcompilerppufile);
  843. var
  844. storesymtable : tsymtable;
  845. begin
  846. storesymtable:=aktrecordsymtable;
  847. aktrecordsymtable:=self;
  848. inherited ppuload(ppufile);
  849. aktrecordsymtable:=storesymtable;
  850. end;
  851. procedure tabstractrecordsymtable.ppuwrite(ppufile:tcompilerppufile);
  852. var
  853. oldtyp : byte;
  854. storesymtable : tsymtable;
  855. begin
  856. storesymtable:=aktrecordsymtable;
  857. aktrecordsymtable:=self;
  858. oldtyp:=ppufile.entrytyp;
  859. ppufile.entrytyp:=subentryid;
  860. inherited ppuwrite(ppufile);
  861. ppufile.entrytyp:=oldtyp;
  862. aktrecordsymtable:=storesymtable;
  863. end;
  864. procedure tabstractrecordsymtable.load_references(ppufile:tcompilerppufile;locals:boolean);
  865. var
  866. storesymtable : tsymtable;
  867. begin
  868. storesymtable:=aktrecordsymtable;
  869. aktrecordsymtable:=self;
  870. inherited load_references(ppufile,locals);
  871. aktrecordsymtable:=storesymtable;
  872. end;
  873. procedure tabstractrecordsymtable.write_references(ppufile:tcompilerppufile;locals:boolean);
  874. var
  875. storesymtable : tsymtable;
  876. begin
  877. storesymtable:=aktrecordsymtable;
  878. aktrecordsymtable:=self;
  879. inherited write_references(ppufile,locals);
  880. aktrecordsymtable:=storesymtable;
  881. end;
  882. procedure tabstractrecordsymtable.insertvardata(sym : tsymentry);
  883. var
  884. l,varalign : longint;
  885. vardef : tdef;
  886. begin
  887. if sym.typ<>varsym then
  888. internalerror(200208251);
  889. l:=tvarsym(sym).getvaluesize;
  890. vardef:=tvarsym(sym).vartype.def;
  891. { this symbol can't be loaded to a register }
  892. exclude(tvarsym(sym).varoptions,vo_regable);
  893. exclude(tvarsym(sym).varoptions,vo_fpuregable);
  894. { get the alignment size }
  895. if (aktalignment.recordalignmax=-1) then
  896. begin
  897. varalign:=vardef.alignment;
  898. if (varalign>4) and
  899. ((varalign mod 4)<>0) and
  900. (vardef.deftype=arraydef) then
  901. Message1(sym_w_wrong_C_pack,vardef.typename);
  902. if varalign=0 then
  903. varalign:=l;
  904. if (dataalignment<aktalignment.maxCrecordalign) then
  905. begin
  906. if (varalign>16) and (dataalignment<32) then
  907. dataalignment:=32
  908. else if (varalign>12) and (dataalignment<16) then
  909. dataalignment:=16
  910. { 12 is needed for long double }
  911. else if (varalign>8) and (dataalignment<12) then
  912. dataalignment:=12
  913. else if (varalign>4) and (dataalignment<8) then
  914. dataalignment:=8
  915. else if (varalign>2) and (dataalignment<4) then
  916. dataalignment:=4
  917. else if (varalign>1) and (dataalignment<2) then
  918. dataalignment:=2;
  919. end;
  920. dataalignment:=min(dataalignment,aktalignment.maxCrecordalign);
  921. end
  922. else
  923. varalign:=vardef.alignment;
  924. if varalign=0 then
  925. varalign:=size_2_align(l);
  926. varalign:=used_align(varalign,aktalignment.recordalignmin,dataalignment);
  927. tvarsym(sym).address:=align(datasize,varalign);
  928. datasize:=tvarsym(sym).address+l;
  929. end;
  930. {****************************************************************************
  931. TRecordSymtable
  932. ****************************************************************************}
  933. constructor trecordsymtable.create;
  934. begin
  935. inherited create('');
  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.insert_in(tsymt : tsymtable;offset : longint);
  944. var
  945. ps,nps : tvarsym;
  946. pd,npd : tdef;
  947. storesize,storealign : longint;
  948. begin
  949. storesize:=tsymt.datasize;
  950. storealign:=tsymt.dataalignment;
  951. tsymt.datasize:=offset;
  952. ps:=tvarsym(symindex.first);
  953. while assigned(ps) do
  954. begin
  955. nps:=tvarsym(ps.indexnext);
  956. { remove from current symtable }
  957. symindex.deleteindex(ps);
  958. ps.left:=nil;
  959. ps.right:=nil;
  960. { add to symt }
  961. ps.owner:=tsymt;
  962. tsymt.datasize:=ps.address+offset;
  963. tsymt.symindex.insert(ps);
  964. tsymt.symsearch.insert(ps);
  965. { update address }
  966. ps.address:=tsymt.datasize;
  967. { next }
  968. ps:=nps;
  969. end;
  970. pd:=tdef(defindex.first);
  971. while assigned(pd) do
  972. begin
  973. npd:=tdef(pd.indexnext);
  974. defindex.deleteindex(pd);
  975. pd.left:=nil;
  976. pd.right:=nil;
  977. tsymt.registerdef(pd);
  978. pd:=npd;
  979. end;
  980. tsymt.datasize:=storesize;
  981. tsymt.dataalignment:=storealign;
  982. end;
  983. {****************************************************************************
  984. TObjectSymtable
  985. ****************************************************************************}
  986. constructor tobjectsymtable.create(const n:string);
  987. begin
  988. inherited create(n);
  989. symtabletype:=objectsymtable;
  990. end;
  991. procedure tobjectsymtable.insert(sym:tsymentry);
  992. var
  993. hsym : tsym;
  994. begin
  995. { check for duplicate field id in inherited classes }
  996. if (sym.typ=varsym) and
  997. assigned(defowner) and
  998. (
  999. not(m_delphi in aktmodeswitches) or
  1000. is_object(tdef(defowner))
  1001. ) then
  1002. begin
  1003. { but private ids can be reused }
  1004. hsym:=search_class_member(tobjectdef(defowner),sym.name);
  1005. if assigned(hsym) and
  1006. tstoredsym(hsym).is_visible_for_object(tobjectdef(defowner)) then
  1007. begin
  1008. DuplicateSym(hsym);
  1009. exit;
  1010. end;
  1011. end;
  1012. inherited insert(sym);
  1013. end;
  1014. {****************************************************************************
  1015. TAbstractLocalSymtable
  1016. ****************************************************************************}
  1017. procedure tabstractlocalsymtable.ppuload(ppufile:tcompilerppufile);
  1018. var
  1019. storesymtable : tsymtable;
  1020. begin
  1021. storesymtable:=aktlocalsymtable;
  1022. aktlocalsymtable:=self;
  1023. inherited ppuload(ppufile);
  1024. aktlocalsymtable:=storesymtable;
  1025. end;
  1026. procedure tabstractlocalsymtable.ppuwrite(ppufile:tcompilerppufile);
  1027. var
  1028. oldtyp : byte;
  1029. storesymtable : tsymtable;
  1030. begin
  1031. storesymtable:=aktlocalsymtable;
  1032. aktlocalsymtable:=self;
  1033. oldtyp:=ppufile.entrytyp;
  1034. ppufile.entrytyp:=subentryid;
  1035. { write definitions }
  1036. writedefs(ppufile);
  1037. { write symbols }
  1038. writesyms(ppufile);
  1039. ppufile.entrytyp:=oldtyp;
  1040. aktlocalsymtable:=storesymtable;
  1041. end;
  1042. procedure tabstractlocalsymtable.load_references(ppufile:tcompilerppufile;locals:boolean);
  1043. var
  1044. storesymtable : tsymtable;
  1045. begin
  1046. storesymtable:=aktlocalsymtable;
  1047. aktlocalsymtable:=self;
  1048. inherited load_references(ppufile,locals);
  1049. aktlocalsymtable:=storesymtable;
  1050. end;
  1051. procedure tabstractlocalsymtable.write_references(ppufile:tcompilerppufile;locals:boolean);
  1052. var
  1053. storesymtable : tsymtable;
  1054. begin
  1055. storesymtable:=aktlocalsymtable;
  1056. aktlocalsymtable:=self;
  1057. inherited write_references(ppufile,locals);
  1058. aktlocalsymtable:=storesymtable;
  1059. end;
  1060. {****************************************************************************
  1061. TLocalSymtable
  1062. ****************************************************************************}
  1063. constructor tlocalsymtable.create;
  1064. begin
  1065. inherited create('');
  1066. symtabletype:=localsymtable;
  1067. end;
  1068. procedure tlocalsymtable.insert(sym:tsymentry);
  1069. var
  1070. hsym : tsym;
  1071. begin
  1072. if assigned(next) then
  1073. begin
  1074. if (next.symtabletype=parasymtable) then
  1075. begin
  1076. hsym:=tsym(next.search(sym.name));
  1077. if assigned(hsym) then
  1078. begin
  1079. { a parameter and the function can have the same
  1080. name in TP and Delphi, but RESULT not }
  1081. if (m_duplicate_names in aktmodeswitches) and
  1082. (sym.typ=funcretsym) and
  1083. not((m_result in aktmodeswitches) and
  1084. (sym.name='RESULT')) then
  1085. sym.name:='hidden'+sym.name
  1086. else
  1087. begin
  1088. DuplicateSym(hsym);
  1089. exit;
  1090. end;
  1091. end;
  1092. end;
  1093. { check for duplicate id in local symtable of methods }
  1094. if assigned(next.next) and
  1095. { funcretsym is allowed !! }
  1096. (sym.typ <> funcretsym) and
  1097. (next.next.symtabletype=objectsymtable) then
  1098. begin
  1099. hsym:=search_class_member(tobjectdef(next.next.defowner),sym.name);
  1100. if assigned(hsym) and
  1101. { private ids can be reused }
  1102. (not(sp_private in hsym.symoptions) or
  1103. (hsym.owner.defowner.owner.symtabletype<>globalsymtable)) then
  1104. begin
  1105. { delphi allows to reuse the names in a class, but not
  1106. in object (tp7 compatible) }
  1107. if not((m_delphi in aktmodeswitches) and
  1108. is_class(tdef(next.next.defowner))) then
  1109. begin
  1110. DuplicateSym(hsym);
  1111. exit;
  1112. end;
  1113. end;
  1114. end;
  1115. end;
  1116. inherited insert(sym);
  1117. end;
  1118. procedure tlocalsymtable.insertvardata(sym : tsymentry);
  1119. var
  1120. l,varalign : longint;
  1121. begin
  1122. if not(sym.typ in [varsym,funcretsym]) then
  1123. internalerror(200208255);
  1124. case sym.typ of
  1125. varsym :
  1126. begin
  1127. tvarsym(sym).varstate:=vs_declared;
  1128. l:=tvarsym(sym).getvaluesize;
  1129. varalign:=size_2_align(l);
  1130. varalign:=used_align(varalign,aktalignment.localalignmin,aktalignment.localalignmax);
  1131. {$ifdef powerpc}
  1132. { on the powerpc, the local variables are accessed with a positiv offset }
  1133. tvarsym(sym).address:=align(datasize,varalign);
  1134. datasize:=tvarsym(sym).address+l;
  1135. {$else powerpc}
  1136. tvarsym(sym).address:=align(datasize+l,varalign);
  1137. datasize:=tvarsym(sym).address;
  1138. {$endif powerpc}
  1139. end;
  1140. funcretsym :
  1141. begin
  1142. { if retoffset is already set then reuse it, this is needed
  1143. when inserting the result variable }
  1144. if procinfo.return_offset<>0 then
  1145. tfuncretsym(sym).address:=procinfo.return_offset
  1146. else
  1147. begin
  1148. { allocate space in local if ret in register }
  1149. if paramanager.ret_in_reg(tfuncretsym(sym).returntype.def) then
  1150. begin
  1151. l:=tfuncretsym(sym).returntype.def.size;
  1152. varalign:=size_2_align(l);
  1153. varalign:=used_align(varalign,aktalignment.localalignmin,dataalignment);
  1154. tfuncretsym(sym).address:=align(datasize+l,varalign);
  1155. datasize:=tfuncretsym(sym).address;
  1156. procinfo.return_offset:=-tfuncretsym(sym).address;
  1157. end;
  1158. end;
  1159. end;
  1160. end;
  1161. end;
  1162. procedure tlocalsymtable.insertconstdata(sym : tsymentry);
  1163. var
  1164. storefilepos : tfileposinfo;
  1165. curconstsegment : taasmoutput;
  1166. address,l,varalign : longint;
  1167. begin
  1168. { Note: this is the same code as tabstractunitsymtable.insertconstdata }
  1169. if sym.typ<>typedconstsym then
  1170. internalerror(200208254);
  1171. storefilepos:=aktfilepos;
  1172. aktfilepos:=tsym(sym).fileinfo;
  1173. if ttypedconstsym(sym).is_writable then
  1174. curconstsegment:=datasegment
  1175. else
  1176. curconstsegment:=consts;
  1177. l:=ttypedconstsym(sym).getsize;
  1178. varalign:=size_2_align(l);
  1179. varalign:=used_align(varalign,aktalignment.constalignmin,aktalignment.constalignmax);
  1180. address:=align(datasize,varalign);
  1181. { insert cut for smartlinking or alignment }
  1182. if (cs_create_smart in aktmoduleswitches) then
  1183. curconstSegment.concat(Tai_cut.Create)
  1184. else if (address<>datasize) then
  1185. curconstSegment.concat(Tai_align.create(varalign));
  1186. datasize:=address+l;
  1187. {$ifdef GDB}
  1188. if cs_debuginfo in aktmoduleswitches then
  1189. concatstabto(curconstsegment);
  1190. {$endif GDB}
  1191. if (cs_create_smart in aktmoduleswitches) or
  1192. DLLSource then
  1193. begin
  1194. curconstSegment.concat(Tai_symbol.Createdataname_global(
  1195. ttypedconstsym(sym).mangledname,ttypedconstsym(sym).getsize));
  1196. end
  1197. else
  1198. begin
  1199. curconstSegment.concat(Tai_symbol.Createdataname(
  1200. ttypedconstsym(sym).mangledname,ttypedconstsym(sym).getsize));
  1201. end;
  1202. aktfilepos:=storefilepos;
  1203. end;
  1204. {****************************************************************************
  1205. TParaSymtable
  1206. ****************************************************************************}
  1207. constructor tparasymtable.create;
  1208. begin
  1209. inherited create('');
  1210. symtabletype:=parasymtable;
  1211. dataalignment:=aktalignment.paraalign;
  1212. end;
  1213. procedure tparasymtable.insert(sym:tsymentry);
  1214. var
  1215. hsym : tsym;
  1216. begin
  1217. { check for duplicate id in para symtable of methods }
  1218. if assigned(procinfo) and
  1219. assigned(procinfo._class) and
  1220. { but not in nested procedures !}
  1221. (not(assigned(procinfo.parent)) or
  1222. (assigned(procinfo.parent) and
  1223. not(assigned(procinfo.parent._class)))
  1224. ) and
  1225. { funcretsym is allowed !! }
  1226. (sym.typ<>funcretsym) then
  1227. begin
  1228. hsym:=search_class_member(procinfo._class,sym.name);
  1229. { private ids can be reused }
  1230. if assigned(hsym) and
  1231. tstoredsym(hsym).is_visible_for_object(procinfo._class) then
  1232. begin
  1233. { delphi allows to reuse the names in a class, but not
  1234. in object (tp7 compatible) }
  1235. if not((m_delphi in aktmodeswitches) and
  1236. is_class_or_interface(procinfo._class)) then
  1237. begin
  1238. DuplicateSym(hsym);
  1239. exit;
  1240. end;
  1241. end;
  1242. end;
  1243. inherited insert(sym);
  1244. end;
  1245. procedure tparasymtable.insertvardata(sym : tsymentry);
  1246. var
  1247. l,varalign : longint;
  1248. is_cdecl : boolean;
  1249. begin
  1250. if sym.typ<>varsym then
  1251. internalerror(200208253);
  1252. { retrieve cdecl status }
  1253. if defowner.deftype<>procdef then
  1254. internalerror(200208256);
  1255. is_cdecl:=(tprocdef(defowner).proccalloption in [pocall_cdecl,pocall_cppdecl]);
  1256. { here we need the size of a push instead of the
  1257. size of the data }
  1258. l:=tvarsym(sym).getpushsize(is_cdecl);
  1259. varalign:=size_2_align(l);
  1260. tvarsym(sym).varstate:=vs_assigned;
  1261. { we need the new datasize already aligned so we can't
  1262. use the align_address here }
  1263. tvarsym(sym).address:=datasize;
  1264. varalign:=used_align(varalign,dataalignment,dataalignment);
  1265. datasize:=align(tvarsym(sym).address+l,varalign);
  1266. end;
  1267. {****************************************************************************
  1268. TAbstractUnitSymtable
  1269. ****************************************************************************}
  1270. constructor tabstractunitsymtable.create(const n : string);
  1271. begin
  1272. inherited create(n);
  1273. symsearch.usehash;
  1274. {$ifdef GDB}
  1275. { reset GDB things }
  1276. prev_dbx_counter := dbx_counter;
  1277. dbx_counter := nil;
  1278. is_stab_written:=false;
  1279. dbx_count := -1;
  1280. {$endif GDB}
  1281. end;
  1282. procedure tabstractunitsymtable.insertvardata(sym : tsymentry);
  1283. var
  1284. l,varalign : longint;
  1285. storefilepos : tfileposinfo;
  1286. begin
  1287. if sym.typ<>varsym then
  1288. internalerror(200208252);
  1289. storefilepos:=aktfilepos;
  1290. aktfilepos:=tsym(sym).fileinfo;
  1291. if (vo_is_thread_var in tvarsym(sym).varoptions) then
  1292. l:=pointer_size
  1293. else
  1294. l:=tvarsym(sym).getvaluesize;
  1295. varalign:=size_2_align(l);
  1296. varalign:=used_align(varalign,aktalignment.varalignmin,aktalignment.varalignmax);
  1297. tvarsym(sym).address:=align(datasize,varalign);
  1298. { insert cut for smartlinking or alignment }
  1299. if (cs_create_smart in aktmoduleswitches) then
  1300. bssSegment.concat(Tai_cut.Create)
  1301. else if (tvarsym(sym).address<>datasize) then
  1302. bssSegment.concat(Tai_align.create(varalign));
  1303. datasize:=tvarsym(sym).address+l;
  1304. {$ifdef GDB}
  1305. if cs_debuginfo in aktmoduleswitches then
  1306. concatstabto(bsssegment);
  1307. {$endif GDB}
  1308. if (symtabletype=globalsymtable) or
  1309. (cs_create_smart in aktmoduleswitches) or
  1310. DLLSource or
  1311. (vo_is_exported in tvarsym(sym).varoptions) or
  1312. (vo_is_C_var in tvarsym(sym).varoptions) then
  1313. bssSegment.concat(Tai_datablock.Create_global(tvarsym(sym).mangledname,l))
  1314. else
  1315. bssSegment.concat(Tai_datablock.Create(tvarsym(sym).mangledname,l));
  1316. aktfilepos:=storefilepos;
  1317. end;
  1318. procedure tabstractunitsymtable.insertconstdata(sym : tsymentry);
  1319. var
  1320. storefilepos : tfileposinfo;
  1321. curconstsegment : taasmoutput;
  1322. address,l,varalign : longint;
  1323. begin
  1324. if sym.typ<>typedconstsym then
  1325. internalerror(200208254);
  1326. storefilepos:=aktfilepos;
  1327. aktfilepos:=tsym(sym).fileinfo;
  1328. if ttypedconstsym(sym).is_writable then
  1329. curconstsegment:=datasegment
  1330. else
  1331. curconstsegment:=consts;
  1332. l:=ttypedconstsym(sym).getsize;
  1333. varalign:=size_2_align(l);
  1334. varalign:=used_align(varalign,aktalignment.constalignmin,aktalignment.constalignmax);
  1335. address:=align(datasize,varalign);
  1336. { insert cut for smartlinking or alignment }
  1337. if (cs_create_smart in aktmoduleswitches) then
  1338. curconstSegment.concat(Tai_cut.Create)
  1339. else if (address<>datasize) then
  1340. curconstSegment.concat(Tai_align.create(varalign));
  1341. datasize:=address+l;
  1342. {$ifdef GDB}
  1343. if cs_debuginfo in aktmoduleswitches then
  1344. concatstabto(curconstsegment);
  1345. {$endif GDB}
  1346. if (symtabletype=globalsymtable) or
  1347. (cs_create_smart in aktmoduleswitches) or
  1348. DLLSource then
  1349. begin
  1350. curconstSegment.concat(Tai_symbol.Createdataname_global(
  1351. ttypedconstsym(sym).mangledname,ttypedconstsym(sym).getsize));
  1352. end
  1353. else
  1354. begin
  1355. curconstSegment.concat(Tai_symbol.Createdataname(
  1356. ttypedconstsym(sym).mangledname,ttypedconstsym(sym).getsize));
  1357. end;
  1358. aktfilepos:=storefilepos;
  1359. end;
  1360. {$ifdef GDB}
  1361. procedure tabstractunitsymtable.concattypestabto(asmlist : taasmoutput);
  1362. var prev_dbx_count : plongint;
  1363. begin
  1364. if is_stab_written then
  1365. exit;
  1366. if not assigned(name) then
  1367. name := stringdup('Main_program');
  1368. if (symtabletype = globalsymtable) and
  1369. (current_module.globalsymtable<>self) then
  1370. begin
  1371. unitid:=current_module.unitcount;
  1372. inc(current_module.unitcount);
  1373. end;
  1374. asmList.concat(tai_comment.Create(strpnew('Begin unit '+name^+' has index '+tostr(unitid))));
  1375. if cs_gdb_dbx in aktglobalswitches then
  1376. begin
  1377. if dbx_count_ok then
  1378. begin
  1379. asmList.concat(tai_comment.Create(strpnew('"repeated" unit '+name^
  1380. +' has index '+tostr(unitid)+' dbx count = '+tostr(dbx_count))));
  1381. asmList.concat(Tai_stabs.Create(strpnew('"'+name^+'",'
  1382. +tostr(N_EXCL)+',0,0,'+tostr(dbx_count))));
  1383. exit;
  1384. end
  1385. else if (current_module.globalsymtable<>self) then
  1386. begin
  1387. prev_dbx_count := dbx_counter;
  1388. dbx_counter := nil;
  1389. do_count_dbx:=false;
  1390. if (symtabletype = globalsymtable) and (unitid<>0) then
  1391. asmList.concat(Tai_stabs.Create(strpnew('"'+name^+'",'+tostr(N_BINCL)+',0,0,0')));
  1392. dbx_counter := @dbx_count;
  1393. dbx_count:=0;
  1394. do_count_dbx:=assigned(dbx_counter);
  1395. end;
  1396. end;
  1397. foreach({$ifdef FPCPROCVAR}@{$endif}concattypestab,asmlist);
  1398. if cs_gdb_dbx in aktglobalswitches then
  1399. begin
  1400. if (current_module.globalsymtable<>self) then
  1401. begin
  1402. dbx_counter := prev_dbx_count;
  1403. do_count_dbx:=false;
  1404. asmList.concat(tai_comment.Create(strpnew('End unit '+name^
  1405. +' has index '+tostr(unitid))));
  1406. asmList.concat(Tai_stabs.Create(strpnew('"'+name^+'",'
  1407. +tostr(N_EINCL)+',0,0,0')));
  1408. do_count_dbx:=assigned(dbx_counter);
  1409. dbx_count_ok := {true}false;
  1410. end;
  1411. end;
  1412. is_stab_written:=true;
  1413. end;
  1414. {$endif GDB}
  1415. {****************************************************************************
  1416. TStaticSymtable
  1417. ****************************************************************************}
  1418. constructor tstaticsymtable.create(const n : string);
  1419. begin
  1420. inherited create(n);
  1421. symtabletype:=staticsymtable;
  1422. end;
  1423. procedure tstaticsymtable.ppuload(ppufile:tcompilerppufile);
  1424. begin
  1425. aktstaticsymtable:=self;
  1426. next:=symtablestack;
  1427. symtablestack:=self;
  1428. inherited ppuload(ppufile);
  1429. { now we can deref the syms and defs }
  1430. deref;
  1431. { restore symtablestack }
  1432. symtablestack:=next;
  1433. end;
  1434. procedure tstaticsymtable.ppuwrite(ppufile:tcompilerppufile);
  1435. begin
  1436. aktstaticsymtable:=self;
  1437. inherited ppuwrite(ppufile);
  1438. end;
  1439. procedure tstaticsymtable.load_references(ppufile:tcompilerppufile;locals:boolean);
  1440. begin
  1441. aktstaticsymtable:=self;
  1442. inherited load_references(ppufile,locals);
  1443. end;
  1444. procedure tstaticsymtable.write_references(ppufile:tcompilerppufile;locals:boolean);
  1445. begin
  1446. aktstaticsymtable:=self;
  1447. inherited write_references(ppufile,locals);
  1448. end;
  1449. procedure tstaticsymtable.insert(sym:tsymentry);
  1450. var
  1451. hsym : tsym;
  1452. begin
  1453. { also check the global symtable }
  1454. if assigned(next) and
  1455. (next.unitid=0) then
  1456. begin
  1457. hsym:=tsym(next.search(sym.name));
  1458. if assigned(hsym) then
  1459. begin
  1460. DuplicateSym(hsym);
  1461. exit;
  1462. end;
  1463. end;
  1464. inherited insert(sym);
  1465. end;
  1466. procedure tstaticsymtable.insertvardata(sym : tsymentry);
  1467. begin
  1468. inherited insertvardata(sym);
  1469. { enable unitialized warning for local symbols }
  1470. if sym.typ=varsym then
  1471. tvarsym(sym).varstate:=vs_declared;
  1472. end;
  1473. {****************************************************************************
  1474. TGlobalSymtable
  1475. ****************************************************************************}
  1476. constructor tglobalsymtable.create(const n : string);
  1477. begin
  1478. inherited create(n);
  1479. symtabletype:=globalsymtable;
  1480. unitid:=0;
  1481. unitsym:=nil;
  1482. {$ifdef GDB}
  1483. if cs_gdb_dbx in aktglobalswitches then
  1484. begin
  1485. dbx_count := 0;
  1486. unittypecount:=1;
  1487. pglobaltypecount := @unittypecount;
  1488. {unitid:=current_module.unitcount;}
  1489. debugList.concat(tai_comment.Create(strpnew('Global '+name^+' has index '+tostr(unitid))));
  1490. debugList.concat(Tai_stabs.Create(strpnew('"'+name^+'",'+tostr(N_BINCL)+',0,0,0')));
  1491. {inc(current_module.unitcount);}
  1492. dbx_count_ok:=false;
  1493. dbx_counter:=@dbx_count;
  1494. do_count_dbx:=true;
  1495. end;
  1496. {$endif GDB}
  1497. end;
  1498. destructor tglobalsymtable.destroy;
  1499. var
  1500. pus : tunitsym;
  1501. begin
  1502. pus:=unitsym;
  1503. while assigned(pus) do
  1504. begin
  1505. unitsym:=pus.prevsym;
  1506. pus.prevsym:=nil;
  1507. pus.unitsymtable:=nil;
  1508. pus:=unitsym;
  1509. end;
  1510. inherited destroy;
  1511. end;
  1512. procedure tglobalsymtable.ppuload(ppufile:tcompilerppufile);
  1513. begin
  1514. {$ifdef GDB}
  1515. if cs_gdb_dbx in aktglobalswitches then
  1516. begin
  1517. UnitTypeCount:=1;
  1518. PglobalTypeCount:=@UnitTypeCount;
  1519. end;
  1520. {$endif GDB}
  1521. symtablelevel:=0;
  1522. {$ifndef NEWMAP}
  1523. current_module.map^[0]:=self;
  1524. {$else NEWMAP}
  1525. current_module.globalsymtable:=self;
  1526. {$endif NEWMAP}
  1527. next:=symtablestack;
  1528. symtablestack:=self;
  1529. inherited ppuload(ppufile);
  1530. { now we can deref the syms and defs }
  1531. deref;
  1532. { restore symtablestack }
  1533. symtablestack:=next;
  1534. {$ifdef NEWMAP}
  1535. { necessary for dependencies }
  1536. current_module.globalsymtable:=nil;
  1537. {$endif NEWMAP}
  1538. end;
  1539. procedure tglobalsymtable.ppuwrite(ppufile:tcompilerppufile);
  1540. begin
  1541. { write the symtable entries }
  1542. inherited ppuwrite(ppufile);
  1543. { write dbx count }
  1544. {$ifdef GDB}
  1545. if cs_gdb_dbx in aktglobalswitches then
  1546. begin
  1547. {$IfDef EXTDEBUG}
  1548. writeln('Writing dbx_count ',dbx_count,' in unit ',name^,'.ppu');
  1549. {$ENDIF EXTDEBUG}
  1550. ppufile.do_crc:=false;
  1551. ppufile.putlongint(dbx_count);
  1552. ppufile.writeentry(ibdbxcount);
  1553. ppufile.do_crc:=true;
  1554. end;
  1555. {$endif GDB}
  1556. end;
  1557. procedure tglobalsymtable.insert(sym:tsymentry);
  1558. var
  1559. hsym : tsym;
  1560. begin
  1561. { also check the global symtable }
  1562. if assigned(next) and
  1563. (next.unitid=0) then
  1564. begin
  1565. hsym:=tsym(next.search(sym.name));
  1566. if assigned(hsym) then
  1567. begin
  1568. DuplicateSym(hsym);
  1569. exit;
  1570. end;
  1571. end;
  1572. hsym:=tsym(search(sym.name));
  1573. if assigned(hsym) then
  1574. begin
  1575. { Delphi you can have a symbol with the same name as the
  1576. unit, the unit can then not be accessed anymore using
  1577. <unit>.<id>, so we can hide the symbol }
  1578. if (m_duplicate_names in aktmodeswitches) and
  1579. (hsym.typ=symconst.unitsym) then
  1580. hsym.owner.rename(hsym.name,'hidden'+hsym.name)
  1581. else
  1582. begin
  1583. DuplicateSym(hsym);
  1584. exit;
  1585. end;
  1586. end;
  1587. inherited insert(sym);
  1588. end;
  1589. procedure tglobalsymtable.insertvardata(sym : tsymentry);
  1590. begin
  1591. inherited insertvardata(sym);
  1592. { this symbol can't be loaded to a register }
  1593. if sym.typ=varsym then
  1594. begin
  1595. exclude(tvarsym(sym).varoptions,vo_regable);
  1596. exclude(tvarsym(sym).varoptions,vo_fpuregable);
  1597. end;
  1598. end;
  1599. {$ifdef GDB}
  1600. function tglobalsymtable.getnewtypecount : word;
  1601. begin
  1602. if not (cs_gdb_dbx in aktglobalswitches) then
  1603. getnewtypecount:=inherited getnewtypecount
  1604. else
  1605. begin
  1606. getnewtypecount:=unittypecount;
  1607. inc(unittypecount);
  1608. end;
  1609. end;
  1610. {$endif}
  1611. {****************************************************************************
  1612. TWITHSYMTABLE
  1613. ****************************************************************************}
  1614. constructor twithsymtable.create(aowner:tdef;asymsearch:TDictionary);
  1615. begin
  1616. inherited create('');
  1617. symtabletype:=withsymtable;
  1618. direct_with:=false;
  1619. withnode:=nil;
  1620. withrefnode:=nil;
  1621. { we don't need the symsearch }
  1622. symsearch.free;
  1623. { set the defaults }
  1624. symsearch:=asymsearch;
  1625. defowner:=aowner;
  1626. end;
  1627. destructor twithsymtable.destroy;
  1628. begin
  1629. symsearch:=nil;
  1630. inherited destroy;
  1631. end;
  1632. procedure twithsymtable.clear;
  1633. begin
  1634. { remove no entry from a withsymtable as it is only a pointer to the
  1635. recorddef or objectdef symtable }
  1636. end;
  1637. {****************************************************************************
  1638. TSTT_ExceptionSymtable
  1639. ****************************************************************************}
  1640. constructor tstt_exceptsymtable.create;
  1641. begin
  1642. inherited create('');
  1643. symtabletype:=stt_exceptsymtable;
  1644. end;
  1645. {*****************************************************************************
  1646. Helper Routines
  1647. *****************************************************************************}
  1648. function findunitsymtable(st:tsymtable):tsymtable;
  1649. begin
  1650. findunitsymtable:=nil;
  1651. repeat
  1652. if not assigned(st) then
  1653. internalerror(5566561);
  1654. case st.symtabletype of
  1655. localsymtable,
  1656. parasymtable,
  1657. staticsymtable :
  1658. exit;
  1659. globalsymtable :
  1660. begin
  1661. findunitsymtable:=st;
  1662. exit;
  1663. end;
  1664. objectsymtable :
  1665. st:=st.defowner.owner;
  1666. recordsymtable :
  1667. begin
  1668. { don't continue when the current
  1669. symtable is used for variant records }
  1670. if trecorddef(st.defowner).isunion then
  1671. begin
  1672. findunitsymtable:=nil;
  1673. exit;
  1674. end
  1675. else
  1676. st:=st.defowner.owner;
  1677. end;
  1678. else
  1679. internalerror(5566562);
  1680. end;
  1681. until false;
  1682. end;
  1683. procedure duplicatesym(sym:tsym);
  1684. var
  1685. st : tsymtable;
  1686. begin
  1687. Message1(sym_e_duplicate_id,sym.realname);
  1688. st:=findunitsymtable(sym.owner);
  1689. with sym.fileinfo do
  1690. begin
  1691. if assigned(st) and (st.unitid<>0) then
  1692. Message2(sym_h_duplicate_id_where,'unit '+st.name^,tostr(line))
  1693. else
  1694. Message2(sym_h_duplicate_id_where,current_module.sourcefiles.get_file_name(fileindex),tostr(line));
  1695. end;
  1696. end;
  1697. {*****************************************************************************
  1698. Search
  1699. *****************************************************************************}
  1700. function searchsym(const s : stringid;var srsym:tsym;var srsymtable:tsymtable):boolean;
  1701. var
  1702. speedvalue : cardinal;
  1703. begin
  1704. speedvalue:=getspeedvalue(s);
  1705. srsymtable:=symtablestack;
  1706. while assigned(srsymtable) do
  1707. begin
  1708. srsym:=tsym(srsymtable.speedsearch(s,speedvalue));
  1709. if assigned(srsym) and
  1710. tstoredsym(srsym).is_visible_for_proc(aktprocdef) then
  1711. begin
  1712. searchsym:=true;
  1713. exit;
  1714. end
  1715. else
  1716. srsymtable:=srsymtable.next;
  1717. end;
  1718. searchsym:=false;
  1719. end;
  1720. function searchsymonlyin(p : tsymtable;const s : stringid):tsym;
  1721. var
  1722. srsym : tsym;
  1723. begin
  1724. { the caller have to take care if srsym=nil }
  1725. if assigned(p) then
  1726. begin
  1727. srsym:=tsym(p.search(s));
  1728. if assigned(srsym) then
  1729. begin
  1730. searchsymonlyin:=srsym;
  1731. exit;
  1732. end;
  1733. { also check in the local symtbale if it exists }
  1734. if (p=tsymtable(current_module.globalsymtable)) then
  1735. begin
  1736. srsym:=tsym(current_module.localsymtable.search(s));
  1737. if assigned(srsym) then
  1738. begin
  1739. searchsymonlyin:=srsym;
  1740. exit;
  1741. end;
  1742. end
  1743. end;
  1744. searchsymonlyin:=nil;
  1745. end;
  1746. function searchsym_in_class(classh:tobjectdef;const s : stringid):tsym;
  1747. var
  1748. speedvalue : cardinal;
  1749. topclassh : tobjectdef;
  1750. sym : tsym;
  1751. begin
  1752. speedvalue:=getspeedvalue(s);
  1753. { when the class passed is defined in this unit we
  1754. need to use the scope of that class. This is a trick
  1755. that can be used to access protected members in other
  1756. units. At least kylix supports it this way (PFV) }
  1757. if (classh.owner.symtabletype in [globalsymtable,staticsymtable]) and
  1758. (classh.owner.unitid=0) then
  1759. topclassh:=classh
  1760. else
  1761. topclassh:=nil;
  1762. sym:=nil;
  1763. while assigned(classh) do
  1764. begin
  1765. sym:=tsym(classh.symtable.speedsearch(s,speedvalue));
  1766. if assigned(sym) then
  1767. begin
  1768. if assigned(topclassh) then
  1769. begin
  1770. if tstoredsym(sym).is_visible_for_object(topclassh) then
  1771. break;
  1772. end
  1773. else
  1774. begin
  1775. if tstoredsym(sym).is_visible_for_proc(aktprocdef) then
  1776. break;
  1777. end;
  1778. end;
  1779. classh:=classh.childof;
  1780. end;
  1781. searchsym_in_class:=sym;
  1782. end;
  1783. function searchsystype(const s: stringid; var srsym: ttypesym): boolean;
  1784. var
  1785. symowner: tsymtable;
  1786. begin
  1787. if not(cs_compilesystem in aktmoduleswitches) then
  1788. srsym := ttypesym(searchsymonlyin(systemunit,s))
  1789. else
  1790. searchsym(s,srsym,symowner);
  1791. searchsystype :=
  1792. assigned(srsym) and
  1793. (srsym.typ = typesym);
  1794. end;
  1795. function searchsysvar(const s: stringid; var srsym: tvarsym; var symowner: tsymtable): boolean;
  1796. begin
  1797. if not(cs_compilesystem in aktmoduleswitches) then
  1798. begin
  1799. srsym := tvarsym(searchsymonlyin(systemunit,s));
  1800. symowner := systemunit;
  1801. end
  1802. else
  1803. searchsym(s,srsym,symowner);
  1804. searchsysvar :=
  1805. assigned(srsym) and
  1806. (srsym.typ = varsym);
  1807. end;
  1808. function search_class_member(pd : tobjectdef;const s : string):tsym;
  1809. { searches n in symtable of pd and all anchestors }
  1810. var
  1811. speedvalue : cardinal;
  1812. srsym : tsym;
  1813. begin
  1814. speedvalue:=getspeedvalue(s);
  1815. while assigned(pd) do
  1816. begin
  1817. srsym:=tsym(pd.symtable.speedsearch(s,speedvalue));
  1818. if assigned(srsym) then
  1819. begin
  1820. search_class_member:=srsym;
  1821. exit;
  1822. end;
  1823. pd:=pd.childof;
  1824. end;
  1825. search_class_member:=nil;
  1826. end;
  1827. {*****************************************************************************
  1828. Definition Helpers
  1829. *****************************************************************************}
  1830. procedure globaldef(const s : string;var t:ttype);
  1831. var st : string;
  1832. symt : tsymtable;
  1833. srsym : tsym;
  1834. srsymtable : tsymtable;
  1835. begin
  1836. srsym := nil;
  1837. if pos('.',s) > 0 then
  1838. begin
  1839. st := copy(s,1,pos('.',s)-1);
  1840. searchsym(st,srsym,srsymtable);
  1841. st := copy(s,pos('.',s)+1,255);
  1842. if assigned(srsym) then
  1843. begin
  1844. if srsym.typ = unitsym then
  1845. begin
  1846. symt := tunitsym(srsym).unitsymtable;
  1847. srsym := tsym(symt.search(st));
  1848. end else srsym := nil;
  1849. end;
  1850. end else st := s;
  1851. if srsym = nil then
  1852. searchsym(st,srsym,srsymtable);
  1853. if srsym = nil then
  1854. srsym:=searchsymonlyin(systemunit,st);
  1855. if (not assigned(srsym)) or
  1856. (srsym.typ<>typesym) then
  1857. begin
  1858. Message(type_e_type_id_expected);
  1859. t:=generrortype;
  1860. exit;
  1861. end;
  1862. t := ttypesym(srsym).restype;
  1863. end;
  1864. {****************************************************************************
  1865. Object Helpers
  1866. ****************************************************************************}
  1867. var
  1868. _defaultprop : tpropertysym;
  1869. procedure tstoredsymtable.testfordefaultproperty(p : TNamedIndexItem;arg:pointer);
  1870. begin
  1871. if (tsym(p).typ=propertysym) and
  1872. (ppo_defaultproperty in tpropertysym(p).propoptions) then
  1873. _defaultprop:=tpropertysym(p);
  1874. end;
  1875. function search_default_property(pd : tobjectdef) : tpropertysym;
  1876. { returns the default property of a class, searches also anchestors }
  1877. begin
  1878. _defaultprop:=nil;
  1879. while assigned(pd) do
  1880. begin
  1881. pd.symtable.foreach({$ifdef FPCPROCVAR}@{$endif}tstoredsymtable(pd.symtable).testfordefaultproperty,nil);
  1882. if assigned(_defaultprop) then
  1883. break;
  1884. pd:=pd.childof;
  1885. end;
  1886. search_default_property:=_defaultprop;
  1887. end;
  1888. {$ifdef UNITALIASES}
  1889. {****************************************************************************
  1890. TUNIT_ALIAS
  1891. ****************************************************************************}
  1892. constructor tunit_alias.create(const n:string);
  1893. var
  1894. i : longint;
  1895. begin
  1896. i:=pos('=',n);
  1897. if i=0 then
  1898. fail;
  1899. inherited createname(Copy(n,1,i-1));
  1900. newname:=stringdup(Copy(n,i+1,255));
  1901. end;
  1902. destructor tunit_alias.destroy;
  1903. begin
  1904. stringdispose(newname);
  1905. inherited destroy;
  1906. end;
  1907. procedure addunitalias(const n:string);
  1908. begin
  1909. unitaliases^.insert(tunit_alias,init(Upper(n))));
  1910. end;
  1911. function getunitalias(const n:string):string;
  1912. var
  1913. p : punit_alias;
  1914. begin
  1915. p:=punit_alias(unitaliases^.search(Upper(n)));
  1916. if assigned(p) then
  1917. getunitalias:=punit_alias(p).newname^
  1918. else
  1919. getunitalias:=n;
  1920. end;
  1921. {$endif UNITALIASES}
  1922. {****************************************************************************
  1923. Symtable Stack
  1924. ****************************************************************************}
  1925. procedure dellexlevel;
  1926. var
  1927. p : tsymtable;
  1928. begin
  1929. p:=symtablestack;
  1930. symtablestack:=p.next;
  1931. { symbol tables of unit interfaces are never disposed }
  1932. { this is handle by the unit unitm }
  1933. if not(p.symtabletype in [globalsymtable,stt_exceptsymtable]) then
  1934. p.free;
  1935. end;
  1936. procedure RestoreUnitSyms;
  1937. var
  1938. p : tsymtable;
  1939. begin
  1940. p:=symtablestack;
  1941. while assigned(p) do
  1942. begin
  1943. if (p.symtabletype=globalsymtable) and
  1944. assigned(tglobalsymtable(p).unitsym) and
  1945. ((tglobalsymtable(p).unitsym.owner=current_module.globalsymtable) or
  1946. (tglobalsymtable(p).unitsym.owner=current_module.localsymtable)) then
  1947. tglobalsymtable(p).unitsym.restoreunitsym;
  1948. p:=p.next;
  1949. end;
  1950. end;
  1951. {$ifdef DEBUG}
  1952. procedure test_symtablestack;
  1953. var
  1954. p : tsymtable;
  1955. i : longint;
  1956. begin
  1957. p:=symtablestack;
  1958. i:=0;
  1959. while assigned(p) do
  1960. begin
  1961. inc(i);
  1962. p:=p.next;
  1963. if i>500 then
  1964. Message(sym_f_internal_error_in_symtablestack);
  1965. end;
  1966. end;
  1967. procedure list_symtablestack;
  1968. var
  1969. p : tsymtable;
  1970. i : longint;
  1971. begin
  1972. p:=symtablestack;
  1973. i:=0;
  1974. while assigned(p) do
  1975. begin
  1976. inc(i);
  1977. writeln(i,' ',p.name^);
  1978. p:=p.next;
  1979. if i>500 then
  1980. Message(sym_f_internal_error_in_symtablestack);
  1981. end;
  1982. end;
  1983. {$endif DEBUG}
  1984. {****************************************************************************
  1985. Init/Done Symtable
  1986. ****************************************************************************}
  1987. procedure InitSymtable;
  1988. var
  1989. token : ttoken;
  1990. begin
  1991. { Reset symbolstack }
  1992. registerdef:=false;
  1993. read_member:=false;
  1994. symtablestack:=nil;
  1995. systemunit:=nil;
  1996. {$ifdef GDB}
  1997. firstglobaldef:=nil;
  1998. lastglobaldef:=nil;
  1999. globaltypecount:=1;
  2000. pglobaltypecount:=@globaltypecount;
  2001. {$endif GDB}
  2002. { create error syms and def }
  2003. generrorsym:=terrorsym.create;
  2004. generrortype.setdef(terrordef.create);
  2005. {$ifdef UNITALIASES}
  2006. { unit aliases }
  2007. unitaliases:=tdictionary.create;
  2008. {$endif}
  2009. for token:=first_overloaded to last_overloaded do
  2010. overloaded_operators[token]:=nil;
  2011. end;
  2012. procedure DoneSymtable;
  2013. begin
  2014. generrorsym.free;
  2015. generrortype.def.free;
  2016. {$ifdef UNITALIASES}
  2017. unitaliases.free;
  2018. {$endif}
  2019. end;
  2020. end.
  2021. {
  2022. $Log$
  2023. Revision 1.72 2002-09-09 19:41:46 peter
  2024. * real fix internalerror for dup ids in union sym
  2025. Revision 1.71 2002/09/09 17:34:16 peter
  2026. * tdicationary.replace added to replace and item in a dictionary. This
  2027. is only allowed for the same name
  2028. * varsyms are inserted in symtable before the types are parsed. This
  2029. fixes the long standing "var longint : longint" bug
  2030. - consume_idlist and idstringlist removed. The loops are inserted
  2031. at the callers place and uses the symtable for duplicate id checking
  2032. Revision 1.70 2002/09/05 19:29:45 peter
  2033. * memdebug enhancements
  2034. Revision 1.69 2002/08/25 19:25:21 peter
  2035. * sym.insert_in_data removed
  2036. * symtable.insertvardata/insertconstdata added
  2037. * removed insert_in_data call from symtable.insert, it needs to be
  2038. called separatly. This allows to deref the address calculation
  2039. * procedures now calculate the parast addresses after the procedure
  2040. directives are parsed. This fixes the cdecl parast problem
  2041. * push_addr_param has an extra argument that specifies if cdecl is used
  2042. or not
  2043. Revision 1.68 2002/08/18 20:06:27 peter
  2044. * inlining is now also allowed in interface
  2045. * renamed write/load to ppuwrite/ppuload
  2046. * tnode storing in ppu
  2047. * nld,ncon,nbas are already updated for storing in ppu
  2048. Revision 1.67 2002/08/17 09:23:43 florian
  2049. * first part of procinfo rewrite
  2050. Revision 1.66 2002/08/11 13:24:15 peter
  2051. * saving of asmsymbols in ppu supported
  2052. * asmsymbollist global is removed and moved into a new class
  2053. tasmlibrarydata that will hold the info of a .a file which
  2054. corresponds with a single module. Added librarydata to tmodule
  2055. to keep the library info stored for the module. In the future the
  2056. objectfiles will also be stored to the tasmlibrarydata class
  2057. * all getlabel/newasmsymbol and friends are moved to the new class
  2058. Revision 1.65 2002/07/23 09:51:27 daniel
  2059. * Tried to make Tprocsym.defs protected. I didn't succeed but the cleanups
  2060. are worth comitting.
  2061. Revision 1.64 2002/07/16 15:34:21 florian
  2062. * exit is now a syssym instead of a keyword
  2063. Revision 1.63 2002/07/15 19:44:53 florian
  2064. * fixed crash with default parameters and stdcall calling convention
  2065. Revision 1.62 2002/07/01 18:46:28 peter
  2066. * internal linker
  2067. * reorganized aasm layer
  2068. Revision 1.61 2002/05/18 13:34:19 peter
  2069. * readded missing revisions
  2070. Revision 1.60 2002/05/16 19:46:45 carl
  2071. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  2072. + try to fix temp allocation (still in ifdef)
  2073. + generic constructor calls
  2074. + start of tassembler / tmodulebase class cleanup
  2075. Revision 1.58 2002/05/12 16:53:15 peter
  2076. * moved entry and exitcode to ncgutil and cgobj
  2077. * foreach gets extra argument for passing local data to the
  2078. iterator function
  2079. * -CR checks also class typecasts at runtime by changing them
  2080. into as
  2081. * fixed compiler to cycle with the -CR option
  2082. * fixed stabs with elf writer, finally the global variables can
  2083. be watched
  2084. * removed a lot of routines from cga unit and replaced them by
  2085. calls to cgobj
  2086. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  2087. u32bit then the other is typecasted also to u32bit without giving
  2088. a rangecheck warning/error.
  2089. * fixed pascal calling method with reversing also the high tree in
  2090. the parast, detected by tcalcst3 test
  2091. Revision 1.57 2002/04/04 19:06:05 peter
  2092. * removed unused units
  2093. * use tlocation.size in cg.a_*loc*() routines
  2094. Revision 1.56 2002/03/04 19:10:11 peter
  2095. * removed compiler warnings
  2096. Revision 1.55 2002/02/03 09:30:07 peter
  2097. * more fixes for protected handling
  2098. Revision 1.54 2002/01/29 21:30:25 peter
  2099. * allow also dup id in delphi mode in interfaces
  2100. Revision 1.53 2002/01/29 19:46:00 peter
  2101. * fixed recordsymtable.insert_in() for inserting variant record fields
  2102. to not used symtable.insert() because that also updates alignmentinfo
  2103. which was already set
  2104. Revision 1.52 2002/01/24 18:25:50 peter
  2105. * implicit result variable generation for assembler routines
  2106. * removed m_tp modeswitch, use m_tp7 or not(m_fpc) instead
  2107. }