symsym.pas 58 KB

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