symsym.pas 59 KB

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