symsym.pas 63 KB

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