symsym.pas 62 KB

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