symsym.pas 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912
  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 : longint;
  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 : longint;
  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 : longint;
  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 : longint;
  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. searchsym(lower(owner.name^)+'_'+name,srsym,srsymtable);
  995. if assigned(srsym) then
  996. result:=srsym.mangledname;
  997. end
  998. else
  999. result:=inherited mangledname;
  1000. end;
  1001. {****************************************************************************
  1002. TABSTRACTNORMALVARSYM
  1003. ****************************************************************************}
  1004. constructor tabstractnormalvarsym.create(st:tsymtyp;const n : string;vsp:tvarspez;def:tdef;vopts:tvaroptions);
  1005. begin
  1006. inherited create(st,n,vsp,def,vopts);
  1007. fillchar(localloc,sizeof(localloc),0);
  1008. fillchar(initialloc,sizeof(initialloc),0);
  1009. defaultconstsym:=nil;
  1010. end;
  1011. constructor tabstractnormalvarsym.ppuload(st:tsymtyp;ppufile:tcompilerppufile);
  1012. begin
  1013. inherited ppuload(st,ppufile);
  1014. fillchar(localloc,sizeof(localloc),0);
  1015. fillchar(initialloc,sizeof(initialloc),0);
  1016. ppufile.getderef(defaultconstsymderef);
  1017. end;
  1018. procedure tabstractnormalvarsym.buildderef;
  1019. begin
  1020. inherited buildderef;
  1021. defaultconstsymderef.build(defaultconstsym);
  1022. end;
  1023. procedure tabstractnormalvarsym.deref;
  1024. begin
  1025. inherited deref;
  1026. defaultconstsym:=tsym(defaultconstsymderef.resolve);
  1027. end;
  1028. procedure tabstractnormalvarsym.ppuwrite(ppufile:tcompilerppufile);
  1029. begin
  1030. inherited ppuwrite(ppufile);
  1031. ppufile.putderef(defaultconstsymderef);
  1032. end;
  1033. {****************************************************************************
  1034. Tstaticvarsym
  1035. ****************************************************************************}
  1036. constructor tstaticvarsym.create(const n : string;vsp:tvarspez;def:tdef;vopts:tvaroptions);
  1037. begin
  1038. inherited create(staticvarsym,n,vsp,def,vopts);
  1039. _mangledname:=nil;
  1040. end;
  1041. constructor tstaticvarsym.create_dll(const n : string;vsp:tvarspez;def:tdef);
  1042. begin
  1043. tstaticvarsym(self).create(n,vsp,def,[vo_is_dll_var]);
  1044. end;
  1045. constructor tstaticvarsym.create_C(const n,mangled : string;vsp:tvarspez;def:tdef);
  1046. begin
  1047. tstaticvarsym(self).create(n,vsp,def,[]);
  1048. set_mangledname(mangled);
  1049. end;
  1050. constructor tstaticvarsym.ppuload(ppufile:tcompilerppufile);
  1051. begin
  1052. inherited ppuload(staticvarsym,ppufile);
  1053. if vo_has_mangledname in varoptions then
  1054. _mangledname:=stringdup(ppufile.getstring)
  1055. else
  1056. _mangledname:=nil;
  1057. end;
  1058. destructor tstaticvarsym.destroy;
  1059. begin
  1060. if assigned(_mangledname) then
  1061. begin
  1062. {$ifdef MEMDEBUG}
  1063. memmanglednames.start;
  1064. {$endif MEMDEBUG}
  1065. stringdispose(_mangledname);
  1066. {$ifdef MEMDEBUG}
  1067. memmanglednames.stop;
  1068. {$endif MEMDEBUG}
  1069. end;
  1070. inherited destroy;
  1071. end;
  1072. procedure tstaticvarsym.ppuwrite(ppufile:tcompilerppufile);
  1073. begin
  1074. inherited ppuwrite(ppufile);
  1075. if vo_has_mangledname in varoptions then
  1076. ppufile.putstring(_mangledname^);
  1077. ppufile.writeentry(ibstaticvarsym);
  1078. end;
  1079. function tstaticvarsym.mangledname:string;
  1080. var
  1081. prefix : string[2];
  1082. begin
  1083. if not assigned(_mangledname) then
  1084. begin
  1085. if (vo_is_typed_const in varoptions) then
  1086. prefix:='TC'
  1087. else
  1088. prefix:='U';
  1089. {$ifdef compress}
  1090. _mangledname:=stringdup(minilzw_encode(make_mangledname(prefix,owner,name)));
  1091. {$else}
  1092. _mangledname:=stringdup(make_mangledname(prefix,owner,name));
  1093. {$endif}
  1094. end;
  1095. result:=_mangledname^;
  1096. end;
  1097. procedure tstaticvarsym.set_mangledname(const s:string);
  1098. begin
  1099. stringdispose(_mangledname);
  1100. {$ifdef compress}
  1101. _mangledname:=stringdup(minilzw_encode(s));
  1102. {$else}
  1103. _mangledname:=stringdup(s);
  1104. {$endif}
  1105. include(varoptions,vo_has_mangledname);
  1106. end;
  1107. {****************************************************************************
  1108. TLOCALVARSYM
  1109. ****************************************************************************}
  1110. constructor tlocalvarsym.create(const n : string;vsp:tvarspez;def:tdef;vopts:tvaroptions);
  1111. begin
  1112. inherited create(localvarsym,n,vsp,def,vopts);
  1113. end;
  1114. constructor tlocalvarsym.ppuload(ppufile:tcompilerppufile);
  1115. begin
  1116. inherited ppuload(localvarsym,ppufile);
  1117. end;
  1118. procedure tlocalvarsym.ppuwrite(ppufile:tcompilerppufile);
  1119. begin
  1120. inherited ppuwrite(ppufile);
  1121. ppufile.writeentry(iblocalvarsym);
  1122. end;
  1123. {****************************************************************************
  1124. TPARAVARSYM
  1125. ****************************************************************************}
  1126. constructor tparavarsym.create(const n : string;nr:word;vsp:tvarspez;def:tdef;vopts:tvaroptions);
  1127. begin
  1128. inherited create(paravarsym,n,vsp,def,vopts);
  1129. if (vsp in [vs_var,vs_value,vs_const]) then
  1130. varstate := vs_initialised;
  1131. paranr:=nr;
  1132. paraloc[calleeside].init;
  1133. paraloc[callerside].init;
  1134. end;
  1135. destructor tparavarsym.destroy;
  1136. begin
  1137. paraloc[calleeside].done;
  1138. paraloc[callerside].done;
  1139. inherited destroy;
  1140. end;
  1141. constructor tparavarsym.ppuload(ppufile:tcompilerppufile);
  1142. var
  1143. b : byte;
  1144. begin
  1145. inherited ppuload(paravarsym,ppufile);
  1146. paranr:=ppufile.getword;
  1147. { The var state of parameter symbols is fixed after writing them so
  1148. we write them to the unit file.
  1149. This enables constant folding for inline procedures loaded from units
  1150. }
  1151. varstate:=tvarstate(ppufile.getbyte);
  1152. paraloc[calleeside].init;
  1153. paraloc[callerside].init;
  1154. if vo_has_explicit_paraloc in varoptions then
  1155. begin
  1156. b:=ppufile.getbyte;
  1157. if b<>sizeof(paraloc[callerside].location^) then
  1158. internalerror(200411154);
  1159. ppufile.getdata(paraloc[callerside].add_location^,sizeof(paraloc[callerside].location^));
  1160. paraloc[callerside].size:=paraloc[callerside].location^.size;
  1161. paraloc[callerside].intsize:=tcgsize2size[paraloc[callerside].size];
  1162. end;
  1163. end;
  1164. procedure tparavarsym.ppuwrite(ppufile:tcompilerppufile);
  1165. var
  1166. oldintfcrc : boolean;
  1167. begin
  1168. inherited ppuwrite(ppufile);
  1169. ppufile.putword(paranr);
  1170. { The var state of parameter symbols is fixed after writing them so
  1171. we write them to the unit file.
  1172. This enables constant folding for inline procedures loaded from units
  1173. }
  1174. oldintfcrc:=ppufile.do_crc;
  1175. ppufile.do_crc:=false;
  1176. ppufile.putbyte(ord(varstate));
  1177. ppufile.do_crc:=oldintfcrc;
  1178. if vo_has_explicit_paraloc in varoptions then
  1179. begin
  1180. paraloc[callerside].check_simple_location;
  1181. ppufile.putbyte(sizeof(paraloc[callerside].location^));
  1182. ppufile.putdata(paraloc[callerside].location^,sizeof(paraloc[callerside].location^));
  1183. end;
  1184. ppufile.writeentry(ibparavarsym);
  1185. end;
  1186. {****************************************************************************
  1187. TABSOLUTEVARSYM
  1188. ****************************************************************************}
  1189. constructor tabsolutevarsym.create(const n : string;def:tdef);
  1190. begin
  1191. inherited create(absolutevarsym,n,vs_value,def,[]);
  1192. ref:=nil;
  1193. end;
  1194. constructor tabsolutevarsym.create_ref(const n : string;def:tdef;_ref:tpropaccesslist);
  1195. begin
  1196. inherited create(absolutevarsym,n,vs_value,def,[]);
  1197. ref:=_ref;
  1198. end;
  1199. destructor tabsolutevarsym.destroy;
  1200. begin
  1201. if assigned(ref) then
  1202. ref.free;
  1203. inherited destroy;
  1204. end;
  1205. constructor tabsolutevarsym.ppuload(ppufile:tcompilerppufile);
  1206. begin
  1207. inherited ppuload(absolutevarsym,ppufile);
  1208. ref:=nil;
  1209. asmname:=nil;
  1210. abstyp:=absolutetyp(ppufile.getbyte);
  1211. {$ifdef i386}
  1212. absseg:=false;
  1213. {$endif i386}
  1214. case abstyp of
  1215. tovar :
  1216. ref:=ppufile.getpropaccesslist;
  1217. toasm :
  1218. asmname:=stringdup(ppufile.getstring);
  1219. toaddr :
  1220. begin
  1221. addroffset:=ppufile.getaint;
  1222. {$ifdef i386}
  1223. absseg:=boolean(ppufile.getbyte);
  1224. {$endif i386}
  1225. end;
  1226. end;
  1227. end;
  1228. procedure tabsolutevarsym.ppuwrite(ppufile:tcompilerppufile);
  1229. begin
  1230. inherited ppuwrite(ppufile);
  1231. ppufile.putbyte(byte(abstyp));
  1232. case abstyp of
  1233. tovar :
  1234. ppufile.putpropaccesslist(ref);
  1235. toasm :
  1236. ppufile.putstring(asmname^);
  1237. toaddr :
  1238. begin
  1239. ppufile.putaint(addroffset);
  1240. {$ifdef i386}
  1241. ppufile.putbyte(byte(absseg));
  1242. {$endif i386}
  1243. end;
  1244. end;
  1245. ppufile.writeentry(ibabsolutevarsym);
  1246. end;
  1247. procedure tabsolutevarsym.buildderef;
  1248. begin
  1249. inherited buildderef;
  1250. if (abstyp=tovar) then
  1251. ref.buildderef;
  1252. end;
  1253. procedure tabsolutevarsym.deref;
  1254. begin
  1255. inherited deref;
  1256. { own absolute deref }
  1257. if (abstyp=tovar) then
  1258. ref.resolve;
  1259. end;
  1260. function tabsolutevarsym.mangledname : string;
  1261. begin
  1262. case abstyp of
  1263. toasm :
  1264. mangledname:=asmname^;
  1265. toaddr :
  1266. mangledname:='$'+tostr(addroffset);
  1267. else
  1268. internalerror(200411062);
  1269. end;
  1270. end;
  1271. {****************************************************************************
  1272. TCONSTSYM
  1273. ****************************************************************************}
  1274. constructor tconstsym.create_ord(const n : string;t : tconsttyp;v : tconstexprint;def:tdef);
  1275. begin
  1276. inherited create(constsym,n);
  1277. fillchar(value, sizeof(value), #0);
  1278. consttyp:=t;
  1279. value.valueord:=v;
  1280. ResStrIndex:=0;
  1281. constdef:=def;
  1282. end;
  1283. constructor tconstsym.create_ordptr(const n : string;t : tconsttyp;v : tconstptruint;def:tdef);
  1284. begin
  1285. inherited create(constsym,n);
  1286. fillchar(value, sizeof(value), #0);
  1287. consttyp:=t;
  1288. value.valueordptr:=v;
  1289. ResStrIndex:=0;
  1290. constdef:=def;
  1291. end;
  1292. constructor tconstsym.create_ptr(const n : string;t : tconsttyp;v : pointer;def:tdef);
  1293. begin
  1294. inherited create(constsym,n);
  1295. fillchar(value, sizeof(value), #0);
  1296. consttyp:=t;
  1297. value.valueptr:=v;
  1298. ResStrIndex:=0;
  1299. constdef:=def;
  1300. end;
  1301. constructor tconstsym.create_string(const n : string;t : tconsttyp;str:pchar;l:longint);
  1302. begin
  1303. inherited create(constsym,n);
  1304. fillchar(value, sizeof(value), #0);
  1305. consttyp:=t;
  1306. value.valueptr:=str;
  1307. constdef:=nil;
  1308. value.len:=l;
  1309. end;
  1310. constructor tconstsym.create_wstring(const n : string;t : tconsttyp;pw:pcompilerwidestring);
  1311. begin
  1312. inherited create(constsym,n);
  1313. fillchar(value, sizeof(value), #0);
  1314. consttyp:=t;
  1315. pcompilerwidestring(value.valueptr):=pw;
  1316. constdef:=nil;
  1317. value.len:=getlengthwidestring(pw);
  1318. end;
  1319. constructor tconstsym.ppuload(ppufile:tcompilerppufile);
  1320. var
  1321. pd : pbestreal;
  1322. ps : pnormalset;
  1323. pc : pchar;
  1324. pw : pcompilerwidestring;
  1325. begin
  1326. inherited ppuload(constsym,ppufile);
  1327. constdef:=nil;
  1328. consttyp:=tconsttyp(ppufile.getbyte);
  1329. fillchar(value, sizeof(value), #0);
  1330. case consttyp of
  1331. constord :
  1332. begin
  1333. ppufile.getderef(constdefderef);
  1334. value.valueord:=ppufile.getexprint;
  1335. end;
  1336. constpointer :
  1337. begin
  1338. ppufile.getderef(constdefderef);
  1339. value.valueordptr:=ppufile.getptruint;
  1340. end;
  1341. constwstring :
  1342. begin
  1343. initwidestring(pw);
  1344. setlengthwidestring(pw,ppufile.getlongint);
  1345. ppufile.getdata(pw^.data,pw^.len*sizeof(tcompilerwidechar));
  1346. pcompilerwidestring(value.valueptr):=pw;
  1347. end;
  1348. conststring,
  1349. constresourcestring :
  1350. begin
  1351. value.len:=ppufile.getlongint;
  1352. getmem(pc,value.len+1);
  1353. ppufile.getdata(pc^,value.len);
  1354. if consttyp=constresourcestring then
  1355. ResStrIndex:=ppufile.getlongint;
  1356. value.valueptr:=pc;
  1357. end;
  1358. constreal :
  1359. begin
  1360. new(pd);
  1361. pd^:=ppufile.getreal;
  1362. value.valueptr:=pd;
  1363. end;
  1364. constset :
  1365. begin
  1366. ppufile.getderef(constdefderef);
  1367. new(ps);
  1368. ppufile.getnormalset(ps^);
  1369. value.valueptr:=ps;
  1370. end;
  1371. constguid :
  1372. begin
  1373. new(pguid(value.valueptr));
  1374. ppufile.getdata(value.valueptr^,sizeof(tguid));
  1375. end;
  1376. constnil : ;
  1377. else
  1378. Message1(unit_f_ppu_invalid_entry,tostr(ord(consttyp)));
  1379. end;
  1380. end;
  1381. destructor tconstsym.destroy;
  1382. begin
  1383. case consttyp of
  1384. conststring,
  1385. constresourcestring :
  1386. freemem(pchar(value.valueptr),value.len+1);
  1387. constwstring :
  1388. donewidestring(pcompilerwidestring(value.valueptr));
  1389. constreal :
  1390. dispose(pbestreal(value.valueptr));
  1391. constset :
  1392. dispose(pnormalset(value.valueptr));
  1393. constguid :
  1394. dispose(pguid(value.valueptr));
  1395. end;
  1396. inherited destroy;
  1397. end;
  1398. procedure tconstsym.buildderef;
  1399. begin
  1400. if consttyp in [constord,constpointer,constset] then
  1401. constdefderef.build(constdef);
  1402. end;
  1403. procedure tconstsym.deref;
  1404. begin
  1405. if consttyp in [constord,constpointer,constset] then
  1406. constdef:=tdef(constdefderef.resolve);
  1407. end;
  1408. procedure tconstsym.ppuwrite(ppufile:tcompilerppufile);
  1409. begin
  1410. inherited ppuwrite(ppufile);
  1411. ppufile.putbyte(byte(consttyp));
  1412. case consttyp of
  1413. constnil : ;
  1414. constord :
  1415. begin
  1416. ppufile.putderef(constdefderef);
  1417. ppufile.putexprint(value.valueord);
  1418. end;
  1419. constpointer :
  1420. begin
  1421. ppufile.putderef(constdefderef);
  1422. ppufile.putptruint(value.valueordptr);
  1423. end;
  1424. constwstring :
  1425. begin
  1426. ppufile.putlongint(getlengthwidestring(pcompilerwidestring(value.valueptr)));
  1427. ppufile.putdata(pcompilerwidestring(value.valueptr)^.data,pcompilerwidestring(value.valueptr)^.len*sizeof(tcompilerwidechar));
  1428. end;
  1429. conststring,
  1430. constresourcestring :
  1431. begin
  1432. ppufile.putlongint(value.len);
  1433. ppufile.putdata(pchar(value.valueptr)^,value.len);
  1434. if consttyp=constresourcestring then
  1435. ppufile.putlongint(ResStrIndex);
  1436. end;
  1437. constreal :
  1438. ppufile.putreal(pbestreal(value.valueptr)^);
  1439. constset :
  1440. begin
  1441. ppufile.putderef(constdefderef);
  1442. ppufile.putnormalset(value.valueptr^);
  1443. end;
  1444. constguid :
  1445. ppufile.putdata(value.valueptr^,sizeof(tguid));
  1446. else
  1447. internalerror(13);
  1448. end;
  1449. ppufile.writeentry(ibconstsym);
  1450. end;
  1451. {****************************************************************************
  1452. TENUMSYM
  1453. ****************************************************************************}
  1454. constructor tenumsym.create(const n : string;def : tenumdef;v : longint);
  1455. begin
  1456. inherited create(enumsym,n);
  1457. definition:=def;
  1458. value:=v;
  1459. { First entry? Then we need to set the minval }
  1460. if def.firstenum=nil then
  1461. begin
  1462. if v>0 then
  1463. def.has_jumps:=true;
  1464. def.setmin(v);
  1465. def.setmax(v);
  1466. end
  1467. else
  1468. begin
  1469. { check for jumps }
  1470. if v>def.max+1 then
  1471. def.has_jumps:=true;
  1472. { update low and high }
  1473. if def.min>v then
  1474. def.setmin(v);
  1475. if def.max<v then
  1476. def.setmax(v);
  1477. end;
  1478. order;
  1479. end;
  1480. constructor tenumsym.ppuload(ppufile:tcompilerppufile);
  1481. begin
  1482. inherited ppuload(enumsym,ppufile);
  1483. ppufile.getderef(definitionderef);
  1484. value:=ppufile.getlongint;
  1485. nextenum := Nil;
  1486. end;
  1487. procedure tenumsym.buildderef;
  1488. begin
  1489. definitionderef.build(definition);
  1490. end;
  1491. procedure tenumsym.deref;
  1492. begin
  1493. definition:=tenumdef(definitionderef.resolve);
  1494. order;
  1495. end;
  1496. procedure tenumsym.order;
  1497. var
  1498. sym : tenumsym;
  1499. begin
  1500. sym := tenumsym(definition.firstenum);
  1501. if sym = nil then
  1502. begin
  1503. definition.firstenum := self;
  1504. nextenum := nil;
  1505. exit;
  1506. end;
  1507. { reorder the symbols in increasing value }
  1508. if value < sym.value then
  1509. begin
  1510. nextenum := sym;
  1511. definition.firstenum := self;
  1512. end
  1513. else
  1514. begin
  1515. while (sym.value <= value) and assigned(sym.nextenum) do
  1516. sym := sym.nextenum;
  1517. nextenum := sym.nextenum;
  1518. sym.nextenum := self;
  1519. end;
  1520. end;
  1521. procedure tenumsym.ppuwrite(ppufile:tcompilerppufile);
  1522. begin
  1523. inherited ppuwrite(ppufile);
  1524. ppufile.putderef(definitionderef);
  1525. ppufile.putlongint(value);
  1526. ppufile.writeentry(ibenumsym);
  1527. end;
  1528. {****************************************************************************
  1529. TTYPESYM
  1530. ****************************************************************************}
  1531. constructor ttypesym.create(const n : string;def:tdef);
  1532. begin
  1533. inherited create(typesym,n);
  1534. typedef:=def;
  1535. { register the typesym for the definition }
  1536. if assigned(typedef) and
  1537. (typedef.typ<>errordef) and
  1538. not(assigned(typedef.typesym)) then
  1539. typedef.typesym:=self;
  1540. end;
  1541. constructor ttypesym.ppuload(ppufile:tcompilerppufile);
  1542. begin
  1543. inherited ppuload(typesym,ppufile);
  1544. ppufile.getderef(typedefderef);
  1545. end;
  1546. procedure ttypesym.buildderef;
  1547. begin
  1548. typedefderef.build(typedef);
  1549. end;
  1550. procedure ttypesym.deref;
  1551. begin
  1552. typedef:=tdef(typedefderef.resolve);
  1553. end;
  1554. procedure ttypesym.ppuwrite(ppufile:tcompilerppufile);
  1555. begin
  1556. inherited ppuwrite(ppufile);
  1557. ppufile.putderef(typedefderef);
  1558. ppufile.writeentry(ibtypesym);
  1559. end;
  1560. {****************************************************************************
  1561. TSYSSYM
  1562. ****************************************************************************}
  1563. constructor tsyssym.create(const n : string;l : longint);
  1564. begin
  1565. inherited create(syssym,n);
  1566. number:=l;
  1567. end;
  1568. constructor tsyssym.ppuload(ppufile:tcompilerppufile);
  1569. begin
  1570. inherited ppuload(syssym,ppufile);
  1571. number:=ppufile.getlongint;
  1572. end;
  1573. destructor tsyssym.destroy;
  1574. begin
  1575. inherited destroy;
  1576. end;
  1577. procedure tsyssym.ppuwrite(ppufile:tcompilerppufile);
  1578. begin
  1579. inherited ppuwrite(ppufile);
  1580. ppufile.putlongint(number);
  1581. ppufile.writeentry(ibsyssym);
  1582. end;
  1583. {*****************************************************************************
  1584. TMacro
  1585. *****************************************************************************}
  1586. constructor tmacro.create(const n : string);
  1587. begin
  1588. inherited create(macrosym,n);
  1589. owner:=nil;
  1590. defined:=false;
  1591. is_used:=false;
  1592. is_compiler_var:=false;
  1593. buftext:=nil;
  1594. buflen:=0;
  1595. end;
  1596. constructor tmacro.ppuload(ppufile:tcompilerppufile);
  1597. begin
  1598. inherited ppuload(macrosym,ppufile);
  1599. defined:=boolean(ppufile.getbyte);
  1600. is_compiler_var:=boolean(ppufile.getbyte);
  1601. is_used:=false;
  1602. buflen:= ppufile.getlongint;
  1603. if buflen > 0 then
  1604. begin
  1605. getmem(buftext, buflen);
  1606. ppufile.getdata(buftext^, buflen)
  1607. end
  1608. else
  1609. buftext:=nil;
  1610. end;
  1611. destructor tmacro.destroy;
  1612. begin
  1613. if assigned(buftext) then
  1614. freemem(buftext);
  1615. inherited destroy;
  1616. end;
  1617. procedure tmacro.ppuwrite(ppufile:tcompilerppufile);
  1618. begin
  1619. inherited ppuwrite(ppufile);
  1620. ppufile.putbyte(byte(defined));
  1621. ppufile.putbyte(byte(is_compiler_var));
  1622. ppufile.putlongint(buflen);
  1623. if buflen > 0 then
  1624. ppufile.putdata(buftext^,buflen);
  1625. ppufile.writeentry(ibmacrosym);
  1626. end;
  1627. function tmacro.GetCopy:tmacro;
  1628. var
  1629. p : tmacro;
  1630. begin
  1631. p:=tmacro.create(realname);
  1632. p.defined:=defined;
  1633. p.is_used:=is_used;
  1634. p.is_compiler_var:=is_compiler_var;
  1635. p.buflen:=buflen;
  1636. if assigned(buftext) then
  1637. begin
  1638. getmem(p.buftext,buflen);
  1639. move(buftext^,p.buftext^,buflen);
  1640. end;
  1641. Result:=p;
  1642. end;
  1643. end.