symsym.pas 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl, Pierre Muller
  3. Implementation for the symbols types of the symtable
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit symsym;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. { common }
  22. cutils,
  23. { target }
  24. globtype,globals,widestr,constexp,
  25. { symtable }
  26. symconst,symbase,symtype,symdef,defcmp,
  27. { ppu }
  28. ppu,finput,
  29. cclasses,symnot,
  30. { aasm }
  31. aasmbase,
  32. cpuinfo,cpubase,cgbase,cgutils,parabase
  33. ;
  34. type
  35. { this class is the base for all symbol objects }
  36. tstoredsym = class(tsym)
  37. public
  38. constructor create(st:tsymtyp;const n : string);
  39. constructor ppuload(st:tsymtyp;ppufile:tcompilerppufile);
  40. destructor destroy;override;
  41. procedure ppuwrite(ppufile:tcompilerppufile);virtual;
  42. end;
  43. tlabelsym = class(tstoredsym)
  44. used,
  45. defined : boolean;
  46. { points to the matching node, only valid resultdef pass is run and
  47. the goto<->label relation in the node tree is created, should
  48. be a tnode }
  49. code : pointer;
  50. { when the label is defined in an asm block, this points to the
  51. generated asmlabel }
  52. asmblocklabel : tasmlabel;
  53. constructor create(const n : string);
  54. constructor ppuload(ppufile:tcompilerppufile);
  55. procedure ppuwrite(ppufile:tcompilerppufile);override;
  56. function mangledname:string;override;
  57. end;
  58. tunitsym = class(Tstoredsym)
  59. module : tobject; { tmodule }
  60. constructor create(const n : string;amodule : tobject);
  61. constructor ppuload(ppufile:tcompilerppufile);
  62. destructor destroy;override;
  63. procedure ppuwrite(ppufile:tcompilerppufile);override;
  64. end;
  65. terrorsym = class(Tsym)
  66. constructor create;
  67. end;
  68. tprocsym = class(tstoredsym)
  69. protected
  70. FProcdefList : TFPObjectList;
  71. FProcdefDerefList : TFPList;
  72. public
  73. overloadchecked : boolean;
  74. constructor create(const n : string);
  75. constructor ppuload(ppufile:tcompilerppufile);
  76. destructor destroy;override;
  77. { writes all declarations except the specified one }
  78. procedure write_parameter_lists(skipdef:tprocdef);
  79. { tests, if all procedures definitions are defined and not }
  80. { only forward }
  81. procedure check_forward;
  82. procedure unchain_overload;
  83. procedure ppuwrite(ppufile:tcompilerppufile);override;
  84. procedure buildderef;override;
  85. procedure deref;override;
  86. procedure add_para_match_to(Aprocsym:Tprocsym;cpoptions:tcompare_paras_options);
  87. function find_procdef_bytype(pt:Tproctypeoption):Tprocdef;
  88. function find_procdef_bypara(para:TFPObjectList;retdef:tdef;cpoptions:tcompare_paras_options):Tprocdef;
  89. function find_procdef_byprocvardef(d:Tprocvardef):Tprocdef;
  90. function find_procdef_assignment_operator(fromdef,todef:tdef;var besteq:tequaltype):Tprocdef;
  91. { currobjdef is the object def to assume, this is necessary for protected and
  92. private, context is the object def we're really in, this is for the strict stuff }
  93. function is_visible_for_object(currobjdef:tdef;context:tdef):boolean;override;
  94. property ProcdefList:TFPObjectList read FProcdefList;
  95. end;
  96. ttypesym = class(Tstoredsym)
  97. typedef : tdef;
  98. typedefderef : tderef;
  99. constructor create(const n : string;def:tdef);
  100. constructor ppuload(ppufile:tcompilerppufile);
  101. procedure ppuwrite(ppufile:tcompilerppufile);override;
  102. procedure buildderef;override;
  103. procedure deref;override;
  104. end;
  105. tabstractvarsym = class(tstoredsym)
  106. varoptions : tvaroptions;
  107. notifications : Tlinkedlist;
  108. varspez : tvarspez; { sets the type of access }
  109. varregable : tvarregable;
  110. varstate : tvarstate;
  111. { Has the address of this variable potentially escaped the }
  112. { block in which is was declared? }
  113. { could also be part of tabstractnormalvarsym, but there's }
  114. { one byte left here till the next 4 byte alignment }
  115. addr_taken : boolean;
  116. constructor create(st:tsymtyp;const n : string;vsp:tvarspez;def:tdef;vopts:tvaroptions);
  117. constructor ppuload(st:tsymtyp;ppufile:tcompilerppufile);
  118. destructor destroy;override;
  119. procedure ppuwrite(ppufile:tcompilerppufile);override;
  120. procedure buildderef;override;
  121. procedure deref;override;
  122. function getsize : aint;
  123. function getpackedbitsize : longint;
  124. function is_regvar(refpara: boolean):boolean;
  125. procedure trigger_notifications(what:Tnotification_flag);
  126. function register_notification(flags:Tnotification_flags;
  127. callback:Tnotification_callback):cardinal;
  128. procedure unregister_notification(id:cardinal);
  129. private
  130. procedure setvardef(def:tdef);
  131. _vardef : tdef;
  132. vardefderef : tderef;
  133. public
  134. property vardef: tdef read _vardef write setvardef;
  135. end;
  136. tfieldvarsym = class(tabstractvarsym)
  137. fieldoffset : aint; { offset in record/object }
  138. constructor create(const n : string;vsp:tvarspez;def:tdef;vopts:tvaroptions);
  139. constructor ppuload(ppufile:tcompilerppufile);
  140. procedure ppuwrite(ppufile:tcompilerppufile);override;
  141. function mangledname:string;override;
  142. end;
  143. tabstractnormalvarsym = class(tabstractvarsym)
  144. defaultconstsym : tsym;
  145. defaultconstsymderef : tderef;
  146. localloc : TLocation; { register/reference for local var }
  147. initialloc : TLocation; { initial location so it can still be initialized later after the location was changed by SSA }
  148. constructor create(st:tsymtyp;const n : string;vsp:tvarspez;def:tdef;vopts:tvaroptions);
  149. constructor ppuload(st:tsymtyp;ppufile:tcompilerppufile);
  150. procedure ppuwrite(ppufile:tcompilerppufile);override;
  151. procedure buildderef;override;
  152. procedure deref;override;
  153. end;
  154. tlocalvarsym = class(tabstractnormalvarsym)
  155. constructor create(const n : string;vsp:tvarspez;def:tdef;vopts:tvaroptions);
  156. constructor ppuload(ppufile:tcompilerppufile);
  157. procedure ppuwrite(ppufile:tcompilerppufile);override;
  158. end;
  159. tparavarsym = class(tabstractnormalvarsym)
  160. paraloc : array[tcallercallee] of TCGPara;
  161. paranr : word; { position of this parameter }
  162. {$ifdef EXTDEBUG}
  163. eqval : tequaltype;
  164. {$endif EXTDEBUG}
  165. constructor create(const n : string;nr:word;vsp:tvarspez;def:tdef;vopts:tvaroptions);
  166. constructor ppuload(ppufile:tcompilerppufile);
  167. destructor destroy;override;
  168. procedure ppuwrite(ppufile:tcompilerppufile);override;
  169. end;
  170. tstaticvarsym = class(tabstractnormalvarsym)
  171. private
  172. _mangledname : pshortstring;
  173. public
  174. constructor create(const n : string;vsp:tvarspez;def:tdef;vopts:tvaroptions);
  175. constructor create_dll(const n : string;vsp:tvarspez;def:tdef);
  176. constructor create_C(const n,mangled : string;vsp:tvarspez;def:tdef);
  177. constructor ppuload(ppufile:tcompilerppufile);
  178. destructor destroy;override;
  179. procedure ppuwrite(ppufile:tcompilerppufile);override;
  180. function mangledname:string;override;
  181. procedure set_mangledname(const s:string);
  182. end;
  183. tabsolutevarsym = class(tabstractvarsym)
  184. public
  185. abstyp : absolutetyp;
  186. {$ifdef i386}
  187. absseg : boolean;
  188. {$endif i386}
  189. asmname : pshortstring;
  190. addroffset : aint;
  191. ref : tpropaccesslist;
  192. constructor create(const n : string;def:tdef);
  193. constructor create_ref(const n : string;def:tdef;_ref:tpropaccesslist);
  194. destructor destroy;override;
  195. constructor ppuload(ppufile:tcompilerppufile);
  196. procedure buildderef;override;
  197. procedure deref;override;
  198. function mangledname : string;override;
  199. procedure ppuwrite(ppufile:tcompilerppufile);override;
  200. end;
  201. tpropaccesslisttypes=(palt_none,palt_read,palt_write,palt_stored);
  202. tpropertysym = class(Tstoredsym)
  203. propoptions : tpropertyoptions;
  204. overridenpropsym : tpropertysym;
  205. overridenpropsymderef : tderef;
  206. propdef : tdef;
  207. propdefderef : tderef;
  208. indexdef : tdef;
  209. indexdefderef : tderef;
  210. index,
  211. default : longint;
  212. propaccesslist : array[tpropaccesslisttypes] of tpropaccesslist;
  213. constructor create(const n : string);
  214. destructor destroy;override;
  215. constructor ppuload(ppufile:tcompilerppufile);
  216. function getsize : aint;
  217. procedure ppuwrite(ppufile:tcompilerppufile);override;
  218. procedure buildderef;override;
  219. procedure deref;override;
  220. end;
  221. tconstvalue = record
  222. case integer of
  223. 0: (valueord : tconstexprint);
  224. 1: (valueordptr : tconstptruint);
  225. 2: (valueptr : pointer; len : longint);
  226. end;
  227. tconstsym = class(tstoredsym)
  228. constdef : tdef;
  229. constdefderef : tderef;
  230. consttyp : tconsttyp;
  231. value : tconstvalue;
  232. constructor create_ord(const n : string;t : tconsttyp;v : tconstexprint;def:tdef);
  233. constructor create_ordptr(const n : string;t : tconsttyp;v : tconstptruint;def:tdef);
  234. constructor create_ptr(const n : string;t : tconsttyp;v : pointer;def:tdef);
  235. constructor create_string(const n : string;t : tconsttyp;str:pchar;l:longint);
  236. constructor create_wstring(const n : string;t : tconsttyp;pw:pcompilerwidestring);
  237. constructor ppuload(ppufile:tcompilerppufile);
  238. destructor destroy;override;
  239. procedure buildderef;override;
  240. procedure deref;override;
  241. procedure ppuwrite(ppufile:tcompilerppufile);override;
  242. end;
  243. tenumsym = class(Tstoredsym)
  244. value : longint;
  245. definition : tenumdef;
  246. definitionderef : tderef;
  247. nextenum : tenumsym;
  248. constructor create(const n : string;def : tenumdef;v : longint);
  249. constructor ppuload(ppufile:tcompilerppufile);
  250. procedure ppuwrite(ppufile:tcompilerppufile);override;
  251. procedure buildderef;override;
  252. procedure deref;override;
  253. procedure order;
  254. end;
  255. tsyssym = class(Tstoredsym)
  256. number : longint;
  257. constructor create(const n : string;l : longint);
  258. constructor ppuload(ppufile:tcompilerppufile);
  259. destructor destroy;override;
  260. procedure ppuwrite(ppufile:tcompilerppufile);override;
  261. end;
  262. const
  263. maxmacrolen=16*1024;
  264. type
  265. pmacrobuffer = ^tmacrobuffer;
  266. tmacrobuffer = array[0..maxmacrolen-1] of char;
  267. tmacro = class(tstoredsym)
  268. {Normally true, but false when a previously defined macro is undef-ed}
  269. defined : boolean;
  270. {True if this is a mac style compiler variable, in which case no macro
  271. substitutions shall be done.}
  272. is_compiler_var : boolean;
  273. {Whether the macro was used. NOTE: A use of a macro which was never defined}
  274. {e. g. an IFDEF which returns false, will not be registered as used,}
  275. {since there is no place to register its use. }
  276. is_used : boolean;
  277. buftext : pchar;
  278. buflen : longint;
  279. constructor create(const n : string);
  280. constructor ppuload(ppufile:tcompilerppufile);
  281. procedure ppuwrite(ppufile:tcompilerppufile);override;
  282. destructor destroy;override;
  283. function GetCopy:tmacro;
  284. end;
  285. var
  286. generrorsym : tsym;
  287. implementation
  288. uses
  289. { global }
  290. verbose,
  291. { target }
  292. systems,
  293. { symtable }
  294. defutil,symtable,
  295. fmodule,
  296. { tree }
  297. node,
  298. { aasm }
  299. aasmtai,aasmdata,
  300. { codegen }
  301. paramgr,
  302. procinfo
  303. ;
  304. {****************************************************************************
  305. Helpers
  306. ****************************************************************************}
  307. {****************************************************************************
  308. TSYM (base for all symtypes)
  309. ****************************************************************************}
  310. constructor tstoredsym.create(st:tsymtyp;const n : string);
  311. begin
  312. inherited create(st,n);
  313. { Register in current_module }
  314. if assigned(current_module) then
  315. begin
  316. current_module.symlist.Add(self);
  317. SymId:=current_module.symlist.Count-1;
  318. end;
  319. end;
  320. constructor tstoredsym.ppuload(st:tsymtyp;ppufile:tcompilerppufile);
  321. begin
  322. SymId:=ppufile.getlongint;
  323. inherited Create(st,ppufile.getstring);
  324. { Register symbol }
  325. current_module.symlist[SymId]:=self;
  326. ppufile.getposinfo(fileinfo);
  327. ppufile.getsmallset(symoptions);
  328. end;
  329. procedure tstoredsym.ppuwrite(ppufile:tcompilerppufile);
  330. begin
  331. ppufile.putlongint(SymId);
  332. ppufile.putstring(realname);
  333. ppufile.putposinfo(fileinfo);
  334. ppufile.putsmallset(symoptions);
  335. end;
  336. destructor tstoredsym.destroy;
  337. begin
  338. inherited destroy;
  339. end;
  340. {****************************************************************************
  341. TLABELSYM
  342. ****************************************************************************}
  343. constructor tlabelsym.create(const n : string);
  344. begin
  345. inherited create(labelsym,n);
  346. used:=false;
  347. defined:=false;
  348. code:=nil;
  349. end;
  350. constructor tlabelsym.ppuload(ppufile:tcompilerppufile);
  351. begin
  352. inherited ppuload(labelsym,ppufile);
  353. code:=nil;
  354. used:=false;
  355. defined:=true;
  356. end;
  357. procedure tlabelsym.ppuwrite(ppufile:tcompilerppufile);
  358. begin
  359. if owner.symtabletype=globalsymtable then
  360. Message(sym_e_ill_label_decl)
  361. else
  362. begin
  363. inherited ppuwrite(ppufile);
  364. ppufile.writeentry(iblabelsym);
  365. end;
  366. end;
  367. function tlabelsym.mangledname:string;
  368. begin
  369. if not(defined) then
  370. begin
  371. defined:=true;
  372. current_asmdata.getjumplabel(asmblocklabel);
  373. end;
  374. result:=asmblocklabel.name;
  375. end;
  376. {****************************************************************************
  377. TUNITSYM
  378. ****************************************************************************}
  379. constructor tunitsym.create(const n : string;amodule : tobject);
  380. begin
  381. inherited create(unitsym,n);
  382. module:=amodule;
  383. end;
  384. constructor tunitsym.ppuload(ppufile:tcompilerppufile);
  385. begin
  386. inherited ppuload(unitsym,ppufile);
  387. module:=nil;
  388. end;
  389. destructor tunitsym.destroy;
  390. begin
  391. inherited destroy;
  392. end;
  393. procedure tunitsym.ppuwrite(ppufile:tcompilerppufile);
  394. begin
  395. inherited ppuwrite(ppufile);
  396. ppufile.writeentry(ibunitsym);
  397. end;
  398. {****************************************************************************
  399. TPROCSYM
  400. ****************************************************************************}
  401. constructor tprocsym.create(const n : string);
  402. begin
  403. inherited create(procsym,n);
  404. FProcdefList:=TFPObjectList.Create(false);
  405. FProcdefderefList:=nil;
  406. { the tprocdef have their own symoptions, make the procsym
  407. always visible }
  408. symoptions:=[sp_public];
  409. overloadchecked:=false;
  410. end;
  411. constructor tprocsym.ppuload(ppufile:tcompilerppufile);
  412. var
  413. pdderef : tderef;
  414. i,
  415. pdcnt : longint;
  416. begin
  417. inherited ppuload(procsym,ppufile);
  418. FProcdefList:=TFPObjectList.Create(false);
  419. FProcdefDerefList:=TFPList.Create;
  420. pdcnt:=ppufile.getword;
  421. for i:=1 to pdcnt do
  422. begin
  423. ppufile.getderef(pdderef);
  424. FProcdefDerefList.Add(Pointer(PtrInt(pdderef.dataidx)));
  425. end;
  426. end;
  427. destructor tprocsym.destroy;
  428. begin
  429. FProcdefList.Free;
  430. if assigned(FProcdefDerefList) then
  431. FProcdefDerefList.Free;
  432. inherited destroy;
  433. end;
  434. procedure tprocsym.ppuwrite(ppufile:tcompilerppufile);
  435. var
  436. i : longint;
  437. d : tderef;
  438. begin
  439. inherited ppuwrite(ppufile);
  440. ppufile.putword(FProcdefDerefList.Count);
  441. for i:=0 to FProcdefDerefList.Count-1 do
  442. begin
  443. d.dataidx:=PtrInt(FProcdefDerefList[i]);
  444. ppufile.putderef(d);
  445. end;
  446. ppufile.writeentry(ibprocsym);
  447. end;
  448. procedure tprocsym.write_parameter_lists(skipdef:tprocdef);
  449. var
  450. i : longint;
  451. pd : tprocdef;
  452. begin
  453. for i:=0 to ProcdefList.Count-1 do
  454. begin
  455. pd:=tprocdef(ProcdefList[i]);
  456. if pd<>skipdef then
  457. MessagePos1(pd.fileinfo,sym_h_param_list,pd.fullprocname(false));
  458. end;
  459. end;
  460. procedure tprocsym.check_forward;
  461. var
  462. i : longint;
  463. pd : tprocdef;
  464. begin
  465. for i:=0 to ProcdefList.Count-1 do
  466. begin
  467. pd:=tprocdef(ProcdefList[i]);
  468. if (pd.owner=owner) and (pd.forwarddef) then
  469. begin
  470. { For mode macpas. Make implicit externals (procedures declared in the interface
  471. section which do not have a counterpart in the implementation)
  472. to be an imported procedure }
  473. if (m_mac in current_settings.modeswitches) and
  474. (pd.interfacedef) then
  475. begin
  476. pd.forwarddef:=false;
  477. pd.setmangledname(target_info.CPrefix+tprocdef(pd).procsym.realname);
  478. end
  479. else
  480. begin
  481. MessagePos1(pd.fileinfo,sym_e_forward_not_resolved,pd.fullprocname(false));
  482. { Turn further error messages off }
  483. pd.forwarddef:=false;
  484. end
  485. end;
  486. end;
  487. end;
  488. procedure tprocsym.buildderef;
  489. var
  490. i : longint;
  491. pd : tprocdef;
  492. d : tderef;
  493. begin
  494. if not assigned(FProcdefDerefList) then
  495. FProcdefDerefList:=TFPList.Create
  496. else
  497. FProcdefDerefList.Clear;
  498. for i:=0 to ProcdefList.Count-1 do
  499. begin
  500. pd:=tprocdef(ProcdefList[i]);
  501. { only write the proc definitions that belong
  502. to this procsym and are in the global symtable }
  503. if pd.owner=owner then
  504. begin
  505. d.build(pd);
  506. FProcdefDerefList.Add(Pointer(PtrInt(d.dataidx)));
  507. end;
  508. end;
  509. end;
  510. procedure tprocsym.deref;
  511. var
  512. i : longint;
  513. pd : tprocdef;
  514. d : tderef;
  515. begin
  516. { Clear all procdefs }
  517. ProcdefList.Clear;
  518. if not assigned(FProcdefDerefList) then
  519. internalerror(200611031);
  520. for i:=0 to FProcdefDerefList.Count-1 do
  521. begin
  522. d.dataidx:=PtrInt(FProcdefDerefList[i]);
  523. pd:=tprocdef(d.resolve);
  524. ProcdefList.Add(pd);
  525. end;
  526. end;
  527. procedure Tprocsym.add_para_match_to(Aprocsym:Tprocsym;cpoptions:tcompare_paras_options);
  528. var
  529. i : longint;
  530. pd : tprocdef;
  531. begin
  532. for i:=0 to ProcdefList.Count-1 do
  533. begin
  534. pd:=tprocdef(ProcdefList[i]);
  535. if Aprocsym.find_procdef_bypara(pd.paras,nil,cpoptions)=nil then
  536. Aprocsym.ProcdefList.Add(pd);
  537. end;
  538. end;
  539. function Tprocsym.Find_procdef_bytype(pt:Tproctypeoption):Tprocdef;
  540. var
  541. i : longint;
  542. pd : tprocdef;
  543. begin
  544. result:=nil;
  545. for i:=0 to ProcdefList.Count-1 do
  546. begin
  547. pd:=tprocdef(ProcdefList[i]);
  548. if pd.proctypeoption=pt then
  549. begin
  550. result:=pd;
  551. exit;
  552. end;
  553. end;
  554. end;
  555. function Tprocsym.Find_procdef_bypara(para:TFPObjectList;retdef:tdef;
  556. cpoptions:tcompare_paras_options):Tprocdef;
  557. var
  558. i : longint;
  559. pd : tprocdef;
  560. eq : tequaltype;
  561. begin
  562. result:=nil;
  563. for i:=0 to ProcdefList.Count-1 do
  564. begin
  565. pd:=tprocdef(ProcdefList[i]);
  566. if assigned(retdef) then
  567. eq:=compare_defs(retdef,pd.returndef,nothingn)
  568. else
  569. eq:=te_equal;
  570. if (eq>=te_equal) or
  571. ((cpo_allowconvert in cpoptions) and (eq>te_incompatible)) then
  572. begin
  573. eq:=compare_paras(para,pd.paras,cp_value_equal_const,cpoptions);
  574. if (eq>=te_equal) or
  575. ((cpo_allowconvert in cpoptions) and (eq>te_incompatible)) then
  576. begin
  577. result:=pd;
  578. exit;
  579. end;
  580. end;
  581. end;
  582. end;
  583. function Tprocsym.Find_procdef_byprocvardef(d:Tprocvardef):Tprocdef;
  584. var
  585. i : longint;
  586. bestpd,
  587. pd : tprocdef;
  588. eq,besteq : tequaltype;
  589. begin
  590. { This function will return the pprocdef of pprocsym that
  591. is the best match for procvardef. When there are multiple
  592. matches it returns nil.}
  593. result:=nil;
  594. bestpd:=nil;
  595. besteq:=te_incompatible;
  596. for i:=0 to ProcdefList.Count-1 do
  597. begin
  598. pd:=tprocdef(ProcdefList[i]);
  599. eq:=proc_to_procvar_equal(pd,d);
  600. if eq>=te_equal then
  601. begin
  602. { multiple procvars with the same equal level }
  603. if assigned(bestpd) and
  604. (besteq=eq) then
  605. exit;
  606. if eq>besteq then
  607. begin
  608. besteq:=eq;
  609. bestpd:=pd;
  610. end;
  611. end;
  612. end;
  613. result:=bestpd;
  614. end;
  615. function Tprocsym.Find_procdef_assignment_operator(fromdef,todef:tdef;var besteq:tequaltype):Tprocdef;
  616. var
  617. paraidx,
  618. i : longint;
  619. bestpd,
  620. hpd,
  621. pd : tprocdef;
  622. convtyp : tconverttype;
  623. eq : tequaltype;
  624. begin
  625. { This function will return the pprocdef of pprocsym that
  626. is the best match for procvardef. When there are multiple
  627. matches it returns nil.}
  628. result:=nil;
  629. bestpd:=nil;
  630. besteq:=te_incompatible;
  631. for i:=0 to ProcdefList.Count-1 do
  632. begin
  633. pd:=tprocdef(ProcdefList[i]);
  634. if equal_defs(todef,pd.returndef) and
  635. { the result type must be always really equal and not an alias,
  636. if you mess with this code, check tw4093 }
  637. ((todef=pd.returndef) or
  638. (
  639. not(df_unique in todef.defoptions) and
  640. not(df_unique in pd.returndef.defoptions)
  641. )
  642. ) then
  643. begin
  644. paraidx:=0;
  645. { ignore vs_hidden parameters }
  646. while (paraidx<pd.paras.count) and
  647. assigned(pd.paras[paraidx]) and
  648. (vo_is_hidden_para in tparavarsym(pd.paras[paraidx]).varoptions) do
  649. inc(paraidx);
  650. if (paraidx<pd.paras.count) and
  651. assigned(pd.paras[paraidx]) then
  652. begin
  653. eq:=compare_defs_ext(fromdef,tparavarsym(pd.paras[paraidx]).vardef,nothingn,convtyp,hpd,[]);
  654. { alias? if yes, only l1 choice,
  655. if you mess with this code, check tw4093 }
  656. if (eq=te_exact) and
  657. (fromdef<>tparavarsym(pd.paras[paraidx]).vardef) and
  658. ((df_unique in fromdef.defoptions) or
  659. (df_unique in tparavarsym(pd.paras[paraidx]).vardef.defoptions)) then
  660. eq:=te_convert_l1;
  661. if eq=te_exact then
  662. begin
  663. besteq:=eq;
  664. result:=pd;
  665. exit;
  666. end;
  667. if eq>besteq then
  668. begin
  669. bestpd:=pd;
  670. besteq:=eq;
  671. end;
  672. end;
  673. end;
  674. end;
  675. result:=bestpd;
  676. end;
  677. procedure tprocsym.unchain_overload;
  678. var
  679. i : longint;
  680. pd : tprocdef;
  681. begin
  682. { remove all overloaded procdefs from the
  683. procdeflist that are not in the current symtable }
  684. overloadchecked:=false;
  685. { reset new lists }
  686. for i:=0 to ProcdefList.Count-1 do
  687. begin
  688. pd:=tprocdef(ProcdefList[i]);
  689. { only keep the proc definitions:
  690. - are not deref'd (def=nil)
  691. - are in the same symtable as the procsym (for example both
  692. are in the staticsymtable) }
  693. if not(pd.owner=owner) then
  694. ProcdefList[i]:=nil;
  695. end;
  696. { Remove cleared entries }
  697. ProcdefList.Pack;
  698. end;
  699. function tprocsym.is_visible_for_object(currobjdef:tdef;context:tdef):boolean;
  700. var
  701. i : longint;
  702. pd : tprocdef;
  703. begin
  704. { This procsym is visible, when there is at least
  705. one of the procdefs visible }
  706. result:=false;
  707. for i:=0 to ProcdefList.Count-1 do
  708. begin
  709. pd:=tprocdef(ProcdefList[i]);
  710. if (pd.owner=owner) and
  711. pd.is_visible_for_object(tobjectdef(currobjdef),tobjectdef(context)) then
  712. begin
  713. result:=true;
  714. exit;
  715. end;
  716. end;
  717. end;
  718. {****************************************************************************
  719. TERRORSYM
  720. ****************************************************************************}
  721. constructor terrorsym.create;
  722. begin
  723. inherited create(errorsym,'');
  724. end;
  725. {****************************************************************************
  726. TPROPERTYSYM
  727. ****************************************************************************}
  728. constructor tpropertysym.create(const n : string);
  729. var
  730. pap : tpropaccesslisttypes;
  731. begin
  732. inherited create(propertysym,n);
  733. propoptions:=[];
  734. index:=0;
  735. default:=0;
  736. propdef:=nil;
  737. indexdef:=nil;
  738. for pap:=low(tpropaccesslisttypes) to high(tpropaccesslisttypes) do
  739. propaccesslist[pap]:=tpropaccesslist.create;
  740. end;
  741. constructor tpropertysym.ppuload(ppufile:tcompilerppufile);
  742. var
  743. pap : tpropaccesslisttypes;
  744. begin
  745. inherited ppuload(propertysym,ppufile);
  746. ppufile.getsmallset(propoptions);
  747. ppufile.getderef(overridenpropsymderef);
  748. ppufile.getderef(propdefderef);
  749. index:=ppufile.getlongint;
  750. default:=ppufile.getlongint;
  751. ppufile.getderef(indexdefderef);
  752. for pap:=low(tpropaccesslisttypes) to high(tpropaccesslisttypes) do
  753. propaccesslist[pap]:=ppufile.getpropaccesslist;
  754. end;
  755. destructor tpropertysym.destroy;
  756. var
  757. pap : tpropaccesslisttypes;
  758. begin
  759. for pap:=low(tpropaccesslisttypes) to high(tpropaccesslisttypes) do
  760. propaccesslist[pap].free;
  761. inherited destroy;
  762. end;
  763. procedure tpropertysym.buildderef;
  764. var
  765. pap : tpropaccesslisttypes;
  766. begin
  767. overridenpropsymderef.build(overridenpropsym);
  768. propdefderef.build(propdef);
  769. indexdefderef.build(indexdef);
  770. for pap:=low(tpropaccesslisttypes) to high(tpropaccesslisttypes) do
  771. propaccesslist[pap].buildderef;
  772. end;
  773. procedure tpropertysym.deref;
  774. var
  775. pap : tpropaccesslisttypes;
  776. begin
  777. overridenpropsym:=tpropertysym(overridenpropsymderef.resolve);
  778. indexdef:=tdef(indexdefderef.resolve);
  779. propdef:=tdef(propdefderef.resolve);
  780. for pap:=low(tpropaccesslisttypes) to high(tpropaccesslisttypes) do
  781. propaccesslist[pap].resolve;
  782. end;
  783. function tpropertysym.getsize : aint;
  784. begin
  785. getsize:=0;
  786. end;
  787. procedure tpropertysym.ppuwrite(ppufile:tcompilerppufile);
  788. var
  789. pap : tpropaccesslisttypes;
  790. begin
  791. inherited ppuwrite(ppufile);
  792. ppufile.putsmallset(propoptions);
  793. ppufile.putderef(overridenpropsymderef);
  794. ppufile.putderef(propdefderef);
  795. ppufile.putlongint(index);
  796. ppufile.putlongint(default);
  797. ppufile.putderef(indexdefderef);
  798. for pap:=low(tpropaccesslisttypes) to high(tpropaccesslisttypes) do
  799. ppufile.putpropaccesslist(propaccesslist[pap]);
  800. ppufile.writeentry(ibpropertysym);
  801. end;
  802. {****************************************************************************
  803. TABSTRACTVARSYM
  804. ****************************************************************************}
  805. constructor tabstractvarsym.create(st:tsymtyp;const n : string;vsp:tvarspez;def:tdef;vopts:tvaroptions);
  806. begin
  807. inherited create(st,n);
  808. vardef:=def;
  809. varspez:=vsp;
  810. varstate:=vs_declared;
  811. varoptions:=vopts;
  812. end;
  813. constructor tabstractvarsym.ppuload(st:tsymtyp;ppufile:tcompilerppufile);
  814. begin
  815. inherited ppuload(st,ppufile);
  816. varstate:=vs_readwritten;
  817. varspez:=tvarspez(ppufile.getbyte);
  818. varregable:=tvarregable(ppufile.getbyte);
  819. addr_taken:=boolean(ppufile.getbyte);
  820. ppufile.getderef(vardefderef);
  821. ppufile.getsmallset(varoptions);
  822. end;
  823. destructor tabstractvarsym.destroy;
  824. begin
  825. if assigned(notifications) then
  826. notifications.destroy;
  827. inherited destroy;
  828. end;
  829. procedure tabstractvarsym.buildderef;
  830. begin
  831. vardefderef.build(vardef);
  832. end;
  833. procedure tabstractvarsym.deref;
  834. var
  835. oldvarregable: tvarregable;
  836. begin
  837. { setting the vardef also updates varregable. We just loaded this }
  838. { value from a ppu, so it must not be changed (e.g. tw7817a.pp/ }
  839. { tw7817b.pp: the address is taken of a local variable in an }
  840. { inlined procedure -> must remain non-regable when inlining) }
  841. oldvarregable:=varregable;
  842. vardef:=tdef(vardefderef.resolve);
  843. varregable:=oldvarregable;
  844. end;
  845. procedure tabstractvarsym.ppuwrite(ppufile:tcompilerppufile);
  846. var
  847. oldintfcrc : boolean;
  848. begin
  849. inherited ppuwrite(ppufile);
  850. ppufile.putbyte(byte(varspez));
  851. oldintfcrc:=ppufile.do_crc;
  852. ppufile.do_crc:=false;
  853. ppufile.putbyte(byte(varregable));
  854. ppufile.putbyte(byte(addr_taken));
  855. ppufile.do_crc:=oldintfcrc;
  856. ppufile.putderef(vardefderef);
  857. ppufile.putsmallset(varoptions);
  858. end;
  859. function tabstractvarsym.getsize : aint;
  860. begin
  861. if assigned(vardef) and
  862. ((vardef.typ<>arraydef) or
  863. is_dynamic_array(vardef) or
  864. (tarraydef(vardef).highrange>=tarraydef(vardef).lowrange)) then
  865. result:=vardef.size
  866. else
  867. result:=0;
  868. end;
  869. function tabstractvarsym.getpackedbitsize : longint;
  870. begin
  871. { bitpacking is only done for ordinals }
  872. if not is_ordinal(vardef) then
  873. internalerror(2006082010);
  874. result:=vardef.packedbitsize;
  875. end;
  876. function tabstractvarsym.is_regvar(refpara: boolean):boolean;
  877. begin
  878. { Register variables are not allowed in the following cases:
  879. - regvars are disabled
  880. - exceptions are used (after an exception is raised the contents of the
  881. registers is not valid anymore)
  882. - it has a local copy
  883. - the value needs to be in memory (i.e. reference counted) }
  884. result:=(cs_opt_regvar in current_settings.optimizerswitches) and
  885. not(pi_has_assembler_block in current_procinfo.flags) and
  886. not(pi_uses_exceptions in current_procinfo.flags) and
  887. not(vo_has_local_copy in varoptions) and
  888. ((refpara and
  889. (varregable <> vr_none)) or
  890. (not refpara and
  891. not(varregable in [vr_none,vr_addr])))
  892. {$if not defined(powerpc) and not defined(powerpc64)}
  893. and ((vardef.typ <> recorddef) or
  894. (varregable = vr_addr) or
  895. not(varstate in [vs_written,vs_readwritten]));
  896. {$endif}
  897. end;
  898. procedure tabstractvarsym.trigger_notifications(what:Tnotification_flag);
  899. var n:Tnotification;
  900. begin
  901. if assigned(notifications) then
  902. begin
  903. n:=Tnotification(notifications.first);
  904. while assigned(n) do
  905. begin
  906. if what in n.flags then
  907. n.callback(what,self);
  908. n:=Tnotification(n.next);
  909. end;
  910. end;
  911. end;
  912. function Tabstractvarsym.register_notification(flags:Tnotification_flags;callback:
  913. Tnotification_callback):cardinal;
  914. var n:Tnotification;
  915. begin
  916. if not assigned(notifications) then
  917. notifications:=Tlinkedlist.create;
  918. n:=Tnotification.create(flags,callback);
  919. register_notification:=n.id;
  920. notifications.concat(n);
  921. end;
  922. procedure Tabstractvarsym.unregister_notification(id:cardinal);
  923. var n:Tnotification;
  924. begin
  925. if not assigned(notifications) then
  926. internalerror(200212311)
  927. else
  928. begin
  929. n:=Tnotification(notifications.first);
  930. while assigned(n) do
  931. begin
  932. if n.id=id then
  933. begin
  934. notifications.remove(n);
  935. n.destroy;
  936. exit;
  937. end;
  938. n:=Tnotification(n.next);
  939. end;
  940. internalerror(200212311)
  941. end;
  942. end;
  943. procedure tabstractvarsym.setvardef(def:tdef);
  944. begin
  945. _vardef := def;
  946. { can we load the value into a register ? }
  947. if not assigned(owner) or
  948. (owner.symtabletype in [localsymtable,parasymtable]) or
  949. (
  950. (owner.symtabletype=staticsymtable) and
  951. not(cs_create_pic in current_settings.moduleswitches)
  952. ) then
  953. begin
  954. if tstoreddef(vardef).is_intregable then
  955. varregable:=vr_intreg
  956. else
  957. { $warning TODO: no fpu regvar in staticsymtable yet, need initialization with 0 }
  958. if {(
  959. not assigned(owner) or
  960. (owner.symtabletype<>staticsymtable)
  961. ) and }
  962. tstoreddef(vardef).is_fpuregable then
  963. begin
  964. {$ifdef x86}
  965. if use_sse(vardef) then
  966. varregable:=vr_mmreg
  967. else
  968. {$else x86}
  969. varregable:=vr_fpureg;
  970. {$endif x86}
  971. end;
  972. end;
  973. end;
  974. {****************************************************************************
  975. TFIELDVARSYM
  976. ****************************************************************************}
  977. constructor tfieldvarsym.create(const n : string;vsp:tvarspez;def:tdef;vopts:tvaroptions);
  978. begin
  979. inherited create(fieldvarsym,n,vsp,def,vopts);
  980. fieldoffset:=-1;
  981. end;
  982. constructor tfieldvarsym.ppuload(ppufile:tcompilerppufile);
  983. begin
  984. inherited ppuload(fieldvarsym,ppufile);
  985. fieldoffset:=ppufile.getaint;
  986. end;
  987. procedure tfieldvarsym.ppuwrite(ppufile:tcompilerppufile);
  988. begin
  989. inherited ppuwrite(ppufile);
  990. ppufile.putaint(fieldoffset);
  991. ppufile.writeentry(ibfieldvarsym);
  992. end;
  993. function tfieldvarsym.mangledname:string;
  994. var
  995. srsym : tsym;
  996. srsymtable : tsymtable;
  997. begin
  998. if sp_static in symoptions then
  999. begin
  1000. if searchsym(lower(owner.name^)+'_'+name,srsym,srsymtable) then
  1001. result:=srsym.mangledname
  1002. { when generating the debug info for the module in which the }
  1003. { symbol is defined, the localsymtable of that module is }
  1004. { already popped from the symtablestack }
  1005. else if searchsym_in_module(current_module,lower(owner.name^)+'_'+name,srsym,srsymtable) then
  1006. result:=srsym.mangledname
  1007. else
  1008. internalerror(2007012501);
  1009. end
  1010. else
  1011. result:=inherited mangledname;
  1012. end;
  1013. {****************************************************************************
  1014. TABSTRACTNORMALVARSYM
  1015. ****************************************************************************}
  1016. constructor tabstractnormalvarsym.create(st:tsymtyp;const n : string;vsp:tvarspez;def:tdef;vopts:tvaroptions);
  1017. begin
  1018. inherited create(st,n,vsp,def,vopts);
  1019. fillchar(localloc,sizeof(localloc),0);
  1020. fillchar(initialloc,sizeof(initialloc),0);
  1021. defaultconstsym:=nil;
  1022. end;
  1023. constructor tabstractnormalvarsym.ppuload(st:tsymtyp;ppufile:tcompilerppufile);
  1024. begin
  1025. inherited ppuload(st,ppufile);
  1026. fillchar(localloc,sizeof(localloc),0);
  1027. fillchar(initialloc,sizeof(initialloc),0);
  1028. ppufile.getderef(defaultconstsymderef);
  1029. end;
  1030. procedure tabstractnormalvarsym.buildderef;
  1031. begin
  1032. inherited buildderef;
  1033. defaultconstsymderef.build(defaultconstsym);
  1034. end;
  1035. procedure tabstractnormalvarsym.deref;
  1036. begin
  1037. inherited deref;
  1038. defaultconstsym:=tsym(defaultconstsymderef.resolve);
  1039. end;
  1040. procedure tabstractnormalvarsym.ppuwrite(ppufile:tcompilerppufile);
  1041. begin
  1042. inherited ppuwrite(ppufile);
  1043. ppufile.putderef(defaultconstsymderef);
  1044. end;
  1045. {****************************************************************************
  1046. Tstaticvarsym
  1047. ****************************************************************************}
  1048. constructor tstaticvarsym.create(const n : string;vsp:tvarspez;def:tdef;vopts:tvaroptions);
  1049. begin
  1050. inherited create(staticvarsym,n,vsp,def,vopts);
  1051. _mangledname:=nil;
  1052. end;
  1053. constructor tstaticvarsym.create_dll(const n : string;vsp:tvarspez;def:tdef);
  1054. begin
  1055. tstaticvarsym(self).create(n,vsp,def,[vo_is_dll_var]);
  1056. end;
  1057. constructor tstaticvarsym.create_C(const n,mangled : string;vsp:tvarspez;def:tdef);
  1058. begin
  1059. tstaticvarsym(self).create(n,vsp,def,[]);
  1060. set_mangledname(mangled);
  1061. end;
  1062. constructor tstaticvarsym.ppuload(ppufile:tcompilerppufile);
  1063. begin
  1064. inherited ppuload(staticvarsym,ppufile);
  1065. if vo_has_mangledname in varoptions then
  1066. _mangledname:=stringdup(ppufile.getstring)
  1067. else
  1068. _mangledname:=nil;
  1069. end;
  1070. destructor tstaticvarsym.destroy;
  1071. begin
  1072. if assigned(_mangledname) then
  1073. begin
  1074. {$ifdef MEMDEBUG}
  1075. memmanglednames.start;
  1076. {$endif MEMDEBUG}
  1077. stringdispose(_mangledname);
  1078. {$ifdef MEMDEBUG}
  1079. memmanglednames.stop;
  1080. {$endif MEMDEBUG}
  1081. end;
  1082. inherited destroy;
  1083. end;
  1084. procedure tstaticvarsym.ppuwrite(ppufile:tcompilerppufile);
  1085. begin
  1086. inherited ppuwrite(ppufile);
  1087. if vo_has_mangledname in varoptions then
  1088. ppufile.putstring(_mangledname^);
  1089. ppufile.writeentry(ibstaticvarsym);
  1090. end;
  1091. function tstaticvarsym.mangledname:string;
  1092. var
  1093. prefix : string[2];
  1094. begin
  1095. if not assigned(_mangledname) then
  1096. begin
  1097. if (vo_is_typed_const in varoptions) then
  1098. prefix:='TC'
  1099. else
  1100. prefix:='U';
  1101. {$ifdef compress}
  1102. _mangledname:=stringdup(minilzw_encode(make_mangledname(prefix,owner,name)));
  1103. {$else}
  1104. _mangledname:=stringdup(make_mangledname(prefix,owner,name));
  1105. {$endif}
  1106. end;
  1107. result:=_mangledname^;
  1108. end;
  1109. procedure tstaticvarsym.set_mangledname(const s:string);
  1110. begin
  1111. stringdispose(_mangledname);
  1112. {$ifdef compress}
  1113. _mangledname:=stringdup(minilzw_encode(s));
  1114. {$else}
  1115. _mangledname:=stringdup(s);
  1116. {$endif}
  1117. include(varoptions,vo_has_mangledname);
  1118. end;
  1119. {****************************************************************************
  1120. TLOCALVARSYM
  1121. ****************************************************************************}
  1122. constructor tlocalvarsym.create(const n : string;vsp:tvarspez;def:tdef;vopts:tvaroptions);
  1123. begin
  1124. inherited create(localvarsym,n,vsp,def,vopts);
  1125. end;
  1126. constructor tlocalvarsym.ppuload(ppufile:tcompilerppufile);
  1127. begin
  1128. inherited ppuload(localvarsym,ppufile);
  1129. end;
  1130. procedure tlocalvarsym.ppuwrite(ppufile:tcompilerppufile);
  1131. begin
  1132. inherited ppuwrite(ppufile);
  1133. ppufile.writeentry(iblocalvarsym);
  1134. end;
  1135. {****************************************************************************
  1136. TPARAVARSYM
  1137. ****************************************************************************}
  1138. constructor tparavarsym.create(const n : string;nr:word;vsp:tvarspez;def:tdef;vopts:tvaroptions);
  1139. begin
  1140. inherited create(paravarsym,n,vsp,def,vopts);
  1141. if (vsp in [vs_var,vs_value,vs_const]) then
  1142. varstate := vs_initialised;
  1143. paranr:=nr;
  1144. paraloc[calleeside].init;
  1145. paraloc[callerside].init;
  1146. end;
  1147. destructor tparavarsym.destroy;
  1148. begin
  1149. paraloc[calleeside].done;
  1150. paraloc[callerside].done;
  1151. inherited destroy;
  1152. end;
  1153. constructor tparavarsym.ppuload(ppufile:tcompilerppufile);
  1154. var
  1155. b : byte;
  1156. begin
  1157. inherited ppuload(paravarsym,ppufile);
  1158. paranr:=ppufile.getword;
  1159. { The var state of parameter symbols is fixed after writing them so
  1160. we write them to the unit file.
  1161. This enables constant folding for inline procedures loaded from units
  1162. }
  1163. varstate:=tvarstate(ppufile.getbyte);
  1164. paraloc[calleeside].init;
  1165. paraloc[callerside].init;
  1166. if vo_has_explicit_paraloc in varoptions then
  1167. begin
  1168. b:=ppufile.getbyte;
  1169. if b<>sizeof(paraloc[callerside].location^) then
  1170. internalerror(200411154);
  1171. ppufile.getdata(paraloc[callerside].add_location^,sizeof(paraloc[callerside].location^));
  1172. paraloc[callerside].size:=paraloc[callerside].location^.size;
  1173. paraloc[callerside].intsize:=tcgsize2size[paraloc[callerside].size];
  1174. end;
  1175. end;
  1176. procedure tparavarsym.ppuwrite(ppufile:tcompilerppufile);
  1177. var
  1178. oldintfcrc : boolean;
  1179. begin
  1180. inherited ppuwrite(ppufile);
  1181. ppufile.putword(paranr);
  1182. { The var state of parameter symbols is fixed after writing them so
  1183. we write them to the unit file.
  1184. This enables constant folding for inline procedures loaded from units
  1185. }
  1186. oldintfcrc:=ppufile.do_crc;
  1187. ppufile.do_crc:=false;
  1188. ppufile.putbyte(ord(varstate));
  1189. ppufile.do_crc:=oldintfcrc;
  1190. if vo_has_explicit_paraloc in varoptions then
  1191. begin
  1192. paraloc[callerside].check_simple_location;
  1193. ppufile.putbyte(sizeof(paraloc[callerside].location^));
  1194. ppufile.putdata(paraloc[callerside].location^,sizeof(paraloc[callerside].location^));
  1195. end;
  1196. ppufile.writeentry(ibparavarsym);
  1197. end;
  1198. {****************************************************************************
  1199. TABSOLUTEVARSYM
  1200. ****************************************************************************}
  1201. constructor tabsolutevarsym.create(const n : string;def:tdef);
  1202. begin
  1203. inherited create(absolutevarsym,n,vs_value,def,[]);
  1204. ref:=nil;
  1205. end;
  1206. constructor tabsolutevarsym.create_ref(const n : string;def:tdef;_ref:tpropaccesslist);
  1207. begin
  1208. inherited create(absolutevarsym,n,vs_value,def,[]);
  1209. ref:=_ref;
  1210. end;
  1211. destructor tabsolutevarsym.destroy;
  1212. begin
  1213. if assigned(ref) then
  1214. ref.free;
  1215. inherited destroy;
  1216. end;
  1217. constructor tabsolutevarsym.ppuload(ppufile:tcompilerppufile);
  1218. begin
  1219. inherited ppuload(absolutevarsym,ppufile);
  1220. ref:=nil;
  1221. asmname:=nil;
  1222. abstyp:=absolutetyp(ppufile.getbyte);
  1223. {$ifdef i386}
  1224. absseg:=false;
  1225. {$endif i386}
  1226. case abstyp of
  1227. tovar :
  1228. ref:=ppufile.getpropaccesslist;
  1229. toasm :
  1230. asmname:=stringdup(ppufile.getstring);
  1231. toaddr :
  1232. begin
  1233. addroffset:=ppufile.getaint;
  1234. {$ifdef i386}
  1235. absseg:=boolean(ppufile.getbyte);
  1236. {$endif i386}
  1237. end;
  1238. end;
  1239. end;
  1240. procedure tabsolutevarsym.ppuwrite(ppufile:tcompilerppufile);
  1241. begin
  1242. inherited ppuwrite(ppufile);
  1243. ppufile.putbyte(byte(abstyp));
  1244. case abstyp of
  1245. tovar :
  1246. ppufile.putpropaccesslist(ref);
  1247. toasm :
  1248. ppufile.putstring(asmname^);
  1249. toaddr :
  1250. begin
  1251. ppufile.putaint(addroffset);
  1252. {$ifdef i386}
  1253. ppufile.putbyte(byte(absseg));
  1254. {$endif i386}
  1255. end;
  1256. end;
  1257. ppufile.writeentry(ibabsolutevarsym);
  1258. end;
  1259. procedure tabsolutevarsym.buildderef;
  1260. begin
  1261. inherited buildderef;
  1262. if (abstyp=tovar) then
  1263. ref.buildderef;
  1264. end;
  1265. procedure tabsolutevarsym.deref;
  1266. begin
  1267. inherited deref;
  1268. { own absolute deref }
  1269. if (abstyp=tovar) then
  1270. ref.resolve;
  1271. end;
  1272. function tabsolutevarsym.mangledname : string;
  1273. begin
  1274. case abstyp of
  1275. toasm :
  1276. mangledname:=asmname^;
  1277. toaddr :
  1278. mangledname:='$'+tostr(addroffset);
  1279. else
  1280. internalerror(200411062);
  1281. end;
  1282. end;
  1283. {****************************************************************************
  1284. TCONSTSYM
  1285. ****************************************************************************}
  1286. constructor tconstsym.create_ord(const n : string;t : tconsttyp;v : tconstexprint;def:tdef);
  1287. begin
  1288. inherited create(constsym,n);
  1289. fillchar(value, sizeof(value), #0);
  1290. consttyp:=t;
  1291. value.valueord:=v;
  1292. constdef:=def;
  1293. end;
  1294. constructor tconstsym.create_ordptr(const n : string;t : tconsttyp;v : tconstptruint;def:tdef);
  1295. begin
  1296. inherited create(constsym,n);
  1297. fillchar(value, sizeof(value), #0);
  1298. consttyp:=t;
  1299. value.valueordptr:=v;
  1300. constdef:=def;
  1301. end;
  1302. constructor tconstsym.create_ptr(const n : string;t : tconsttyp;v : pointer;def:tdef);
  1303. begin
  1304. inherited create(constsym,n);
  1305. fillchar(value, sizeof(value), #0);
  1306. consttyp:=t;
  1307. value.valueptr:=v;
  1308. constdef:=def;
  1309. end;
  1310. constructor tconstsym.create_string(const n : string;t : tconsttyp;str:pchar;l:longint);
  1311. begin
  1312. inherited create(constsym,n);
  1313. fillchar(value, sizeof(value), #0);
  1314. consttyp:=t;
  1315. value.valueptr:=str;
  1316. constdef:=nil;
  1317. value.len:=l;
  1318. end;
  1319. constructor tconstsym.create_wstring(const n : string;t : tconsttyp;pw:pcompilerwidestring);
  1320. begin
  1321. inherited create(constsym,n);
  1322. fillchar(value, sizeof(value), #0);
  1323. consttyp:=t;
  1324. pcompilerwidestring(value.valueptr):=pw;
  1325. constdef:=nil;
  1326. value.len:=getlengthwidestring(pw);
  1327. end;
  1328. constructor tconstsym.ppuload(ppufile:tcompilerppufile);
  1329. var
  1330. pd : pbestreal;
  1331. ps : pnormalset;
  1332. pc : pchar;
  1333. pw : pcompilerwidestring;
  1334. begin
  1335. inherited ppuload(constsym,ppufile);
  1336. constdef:=nil;
  1337. consttyp:=tconsttyp(ppufile.getbyte);
  1338. fillchar(value, sizeof(value), #0);
  1339. case consttyp of
  1340. constord :
  1341. begin
  1342. ppufile.getderef(constdefderef);
  1343. value.valueord:=ppufile.getexprint;
  1344. end;
  1345. constpointer :
  1346. begin
  1347. ppufile.getderef(constdefderef);
  1348. value.valueordptr:=ppufile.getptruint;
  1349. end;
  1350. constwstring :
  1351. begin
  1352. initwidestring(pw);
  1353. setlengthwidestring(pw,ppufile.getlongint);
  1354. ppufile.getdata(pw^.data,pw^.len*sizeof(tcompilerwidechar));
  1355. pcompilerwidestring(value.valueptr):=pw;
  1356. end;
  1357. conststring,
  1358. constresourcestring :
  1359. begin
  1360. value.len:=ppufile.getlongint;
  1361. getmem(pc,value.len+1);
  1362. ppufile.getdata(pc^,value.len);
  1363. value.valueptr:=pc;
  1364. end;
  1365. constreal :
  1366. begin
  1367. new(pd);
  1368. pd^:=ppufile.getreal;
  1369. value.valueptr:=pd;
  1370. end;
  1371. constset :
  1372. begin
  1373. ppufile.getderef(constdefderef);
  1374. new(ps);
  1375. ppufile.getnormalset(ps^);
  1376. value.valueptr:=ps;
  1377. end;
  1378. constguid :
  1379. begin
  1380. new(pguid(value.valueptr));
  1381. ppufile.getdata(value.valueptr^,sizeof(tguid));
  1382. end;
  1383. constnil : ;
  1384. else
  1385. Message1(unit_f_ppu_invalid_entry,tostr(ord(consttyp)));
  1386. end;
  1387. end;
  1388. destructor tconstsym.destroy;
  1389. begin
  1390. case consttyp of
  1391. conststring,
  1392. constresourcestring :
  1393. freemem(pchar(value.valueptr),value.len+1);
  1394. constwstring :
  1395. donewidestring(pcompilerwidestring(value.valueptr));
  1396. constreal :
  1397. dispose(pbestreal(value.valueptr));
  1398. constset :
  1399. dispose(pnormalset(value.valueptr));
  1400. constguid :
  1401. dispose(pguid(value.valueptr));
  1402. end;
  1403. inherited destroy;
  1404. end;
  1405. procedure tconstsym.buildderef;
  1406. begin
  1407. if consttyp in [constord,constpointer,constset] then
  1408. constdefderef.build(constdef);
  1409. end;
  1410. procedure tconstsym.deref;
  1411. begin
  1412. if consttyp in [constord,constpointer,constset] then
  1413. constdef:=tdef(constdefderef.resolve);
  1414. end;
  1415. procedure tconstsym.ppuwrite(ppufile:tcompilerppufile);
  1416. begin
  1417. inherited ppuwrite(ppufile);
  1418. ppufile.putbyte(byte(consttyp));
  1419. case consttyp of
  1420. constnil : ;
  1421. constord :
  1422. begin
  1423. ppufile.putderef(constdefderef);
  1424. ppufile.putexprint(value.valueord);
  1425. end;
  1426. constpointer :
  1427. begin
  1428. ppufile.putderef(constdefderef);
  1429. ppufile.putptruint(value.valueordptr);
  1430. end;
  1431. constwstring :
  1432. begin
  1433. ppufile.putlongint(getlengthwidestring(pcompilerwidestring(value.valueptr)));
  1434. ppufile.putdata(pcompilerwidestring(value.valueptr)^.data,pcompilerwidestring(value.valueptr)^.len*sizeof(tcompilerwidechar));
  1435. end;
  1436. conststring,
  1437. constresourcestring :
  1438. begin
  1439. ppufile.putlongint(value.len);
  1440. ppufile.putdata(pchar(value.valueptr)^,value.len);
  1441. end;
  1442. constreal :
  1443. ppufile.putreal(pbestreal(value.valueptr)^);
  1444. constset :
  1445. begin
  1446. ppufile.putderef(constdefderef);
  1447. ppufile.putnormalset(value.valueptr^);
  1448. end;
  1449. constguid :
  1450. ppufile.putdata(value.valueptr^,sizeof(tguid));
  1451. else
  1452. internalerror(13);
  1453. end;
  1454. ppufile.writeentry(ibconstsym);
  1455. end;
  1456. {****************************************************************************
  1457. TENUMSYM
  1458. ****************************************************************************}
  1459. constructor tenumsym.create(const n : string;def : tenumdef;v : longint);
  1460. begin
  1461. inherited create(enumsym,n);
  1462. definition:=def;
  1463. value:=v;
  1464. { First entry? Then we need to set the minval }
  1465. if def.firstenum=nil then
  1466. begin
  1467. if v>0 then
  1468. def.has_jumps:=true;
  1469. def.setmin(v);
  1470. def.setmax(v);
  1471. end
  1472. else
  1473. begin
  1474. { check for jumps }
  1475. if v>def.max+1 then
  1476. def.has_jumps:=true;
  1477. { update low and high }
  1478. if def.min>v then
  1479. def.setmin(v);
  1480. if def.max<v then
  1481. def.setmax(v);
  1482. end;
  1483. order;
  1484. end;
  1485. constructor tenumsym.ppuload(ppufile:tcompilerppufile);
  1486. begin
  1487. inherited ppuload(enumsym,ppufile);
  1488. ppufile.getderef(definitionderef);
  1489. value:=ppufile.getlongint;
  1490. nextenum := Nil;
  1491. end;
  1492. procedure tenumsym.buildderef;
  1493. begin
  1494. definitionderef.build(definition);
  1495. end;
  1496. procedure tenumsym.deref;
  1497. begin
  1498. definition:=tenumdef(definitionderef.resolve);
  1499. order;
  1500. end;
  1501. procedure tenumsym.order;
  1502. var
  1503. sym : tenumsym;
  1504. begin
  1505. sym := tenumsym(definition.firstenum);
  1506. if sym = nil then
  1507. begin
  1508. definition.firstenum := self;
  1509. nextenum := nil;
  1510. exit;
  1511. end;
  1512. { reorder the symbols in increasing value }
  1513. if value < sym.value then
  1514. begin
  1515. nextenum := sym;
  1516. definition.firstenum := self;
  1517. end
  1518. else
  1519. begin
  1520. while (sym.value <= value) and assigned(sym.nextenum) do
  1521. sym := sym.nextenum;
  1522. nextenum := sym.nextenum;
  1523. sym.nextenum := self;
  1524. end;
  1525. end;
  1526. procedure tenumsym.ppuwrite(ppufile:tcompilerppufile);
  1527. begin
  1528. inherited ppuwrite(ppufile);
  1529. ppufile.putderef(definitionderef);
  1530. ppufile.putlongint(value);
  1531. ppufile.writeentry(ibenumsym);
  1532. end;
  1533. {****************************************************************************
  1534. TTYPESYM
  1535. ****************************************************************************}
  1536. constructor ttypesym.create(const n : string;def:tdef);
  1537. begin
  1538. inherited create(typesym,n);
  1539. typedef:=def;
  1540. { register the typesym for the definition }
  1541. if assigned(typedef) and
  1542. (typedef.typ<>errordef) and
  1543. not(assigned(typedef.typesym)) then
  1544. typedef.typesym:=self;
  1545. end;
  1546. constructor ttypesym.ppuload(ppufile:tcompilerppufile);
  1547. begin
  1548. inherited ppuload(typesym,ppufile);
  1549. ppufile.getderef(typedefderef);
  1550. end;
  1551. procedure ttypesym.buildderef;
  1552. begin
  1553. typedefderef.build(typedef);
  1554. end;
  1555. procedure ttypesym.deref;
  1556. begin
  1557. typedef:=tdef(typedefderef.resolve);
  1558. end;
  1559. procedure ttypesym.ppuwrite(ppufile:tcompilerppufile);
  1560. begin
  1561. inherited ppuwrite(ppufile);
  1562. ppufile.putderef(typedefderef);
  1563. ppufile.writeentry(ibtypesym);
  1564. end;
  1565. {****************************************************************************
  1566. TSYSSYM
  1567. ****************************************************************************}
  1568. constructor tsyssym.create(const n : string;l : longint);
  1569. begin
  1570. inherited create(syssym,n);
  1571. number:=l;
  1572. end;
  1573. constructor tsyssym.ppuload(ppufile:tcompilerppufile);
  1574. begin
  1575. inherited ppuload(syssym,ppufile);
  1576. number:=ppufile.getlongint;
  1577. end;
  1578. destructor tsyssym.destroy;
  1579. begin
  1580. inherited destroy;
  1581. end;
  1582. procedure tsyssym.ppuwrite(ppufile:tcompilerppufile);
  1583. begin
  1584. inherited ppuwrite(ppufile);
  1585. ppufile.putlongint(number);
  1586. ppufile.writeentry(ibsyssym);
  1587. end;
  1588. {*****************************************************************************
  1589. TMacro
  1590. *****************************************************************************}
  1591. constructor tmacro.create(const n : string);
  1592. begin
  1593. inherited create(macrosym,n);
  1594. owner:=nil;
  1595. defined:=false;
  1596. is_used:=false;
  1597. is_compiler_var:=false;
  1598. buftext:=nil;
  1599. buflen:=0;
  1600. end;
  1601. constructor tmacro.ppuload(ppufile:tcompilerppufile);
  1602. begin
  1603. inherited ppuload(macrosym,ppufile);
  1604. defined:=boolean(ppufile.getbyte);
  1605. is_compiler_var:=boolean(ppufile.getbyte);
  1606. is_used:=false;
  1607. buflen:= ppufile.getlongint;
  1608. if buflen > 0 then
  1609. begin
  1610. getmem(buftext, buflen);
  1611. ppufile.getdata(buftext^, buflen)
  1612. end
  1613. else
  1614. buftext:=nil;
  1615. end;
  1616. destructor tmacro.destroy;
  1617. begin
  1618. if assigned(buftext) then
  1619. freemem(buftext);
  1620. inherited destroy;
  1621. end;
  1622. procedure tmacro.ppuwrite(ppufile:tcompilerppufile);
  1623. begin
  1624. inherited ppuwrite(ppufile);
  1625. ppufile.putbyte(byte(defined));
  1626. ppufile.putbyte(byte(is_compiler_var));
  1627. ppufile.putlongint(buflen);
  1628. if buflen > 0 then
  1629. ppufile.putdata(buftext^,buflen);
  1630. ppufile.writeentry(ibmacrosym);
  1631. end;
  1632. function tmacro.GetCopy:tmacro;
  1633. var
  1634. p : tmacro;
  1635. begin
  1636. p:=tmacro.create(realname);
  1637. p.defined:=defined;
  1638. p.is_used:=is_used;
  1639. p.is_compiler_var:=is_compiler_var;
  1640. p.buflen:=buflen;
  1641. if assigned(buftext) then
  1642. begin
  1643. getmem(p.buftext,buflen);
  1644. move(buftext^,p.buftext^,buflen);
  1645. end;
  1646. Result:=p;
  1647. end;
  1648. end.