symsym.pas 63 KB

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