symsym.pas 59 KB

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