symtable.pas 81 KB

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