symsym.pas 93 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809
  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,compinnr,
  23. { target }
  24. globtype,globals,widestr,constexp,
  25. { symtable }
  26. symconst,symbase,symtype,symdef,defcmp,
  27. cclasses,
  28. { aasm }
  29. aasmbase,
  30. cpuinfo,cgbase,cgutils,parabase
  31. ;
  32. type
  33. { this class is the base for all symbol objects }
  34. tstoredsym = class(tsym)
  35. private
  36. procedure writeentry(ppufile: tcompilerppufile; ibnr: byte);
  37. protected
  38. procedure ppuwrite_platform(ppufile: tcompilerppufile);virtual;
  39. procedure ppuload_platform(ppufile: tcompilerppufile);virtual;
  40. public
  41. { this is Nil if the symbol has no RTTI attributes }
  42. rtti_attribute_list : trtti_attribute_list;
  43. constructor create(st:tsymtyp;const n : string;doregister:boolean);
  44. constructor ppuload(st:tsymtyp;ppufile:tcompilerppufile);
  45. destructor destroy;override;
  46. procedure ppuwrite(ppufile:tcompilerppufile);virtual;
  47. { this is called directly after ppuload }
  48. procedure ppuload_subentries(ppufile:tcompilerppufile);virtual;
  49. { this is called directly after ppuwrite }
  50. procedure ppuwrite_subentries(ppufile:tcompilerppufile);virtual;
  51. procedure buildderef; override;
  52. procedure register_sym; override;
  53. end;
  54. tlabelsym = class(tstoredsym)
  55. used,
  56. defined,
  57. nonlocal : boolean;
  58. { points to the matching node, only valid resultdef pass is run and
  59. the goto<->label relation in the node tree is created, should
  60. be a tnode }
  61. code : pointer;
  62. { points to the jump buffer }
  63. jumpbuf : tstoredsym;
  64. { when the label is defined in an asm block, this points to the
  65. generated asmlabel }
  66. asmblocklabel : tasmlabel;
  67. constructor create(const n : string);virtual;
  68. constructor ppuload(ppufile:tcompilerppufile);
  69. { do not override this routine in platform-specific subclasses,
  70. override ppuwrite_platform instead }
  71. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  72. function mangledname:TSymStr;override;
  73. end;
  74. tlabelsymclass = class of tlabelsym;
  75. tunitsym = class(Tstoredsym)
  76. module : tobject; { tmodule }
  77. constructor create(const n : string;amodule : tobject;doregister:boolean);virtual;
  78. constructor ppuload(ppufile:tcompilerppufile);
  79. destructor destroy;override;
  80. { do not override this routine in platform-specific subclasses,
  81. override ppuwrite_platform instead }
  82. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  83. end;
  84. tunitsymclass = class of tunitsym;
  85. tprogramparasym = class(Tstoredsym)
  86. isoindex : dword;
  87. constructor create(const n : string;i : dword);virtual;
  88. constructor ppuload(ppufile:tcompilerppufile);
  89. destructor destroy;override;
  90. { do not override this routine in platform-specific subclasses,
  91. override ppuwrite_platform instead }
  92. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  93. end;
  94. tprogramparasymclass = class of tprogramparasym;
  95. tnamespacesym = class(Tstoredsym)
  96. unitsym:tsym;
  97. unitsymderef:tderef;
  98. constructor create(const n : string);virtual;
  99. constructor ppuload(ppufile:tcompilerppufile);
  100. { do not override this routine in platform-specific subclasses,
  101. override ppuwrite_platform instead }
  102. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  103. procedure buildderef;override;
  104. procedure deref;override;
  105. end;
  106. tnamespacesymclass = class of tnamespacesym;
  107. terrorsym = class(Tsym)
  108. constructor create;
  109. procedure register_sym; override;
  110. end;
  111. { tprocsym }
  112. tprocsym = class(tstoredsym)
  113. protected
  114. FProcdefList : TFPObjectList;
  115. FProcdefDerefList : TFPList;
  116. public
  117. constructor create(const n : string);virtual;
  118. constructor ppuload(ppufile:tcompilerppufile);
  119. destructor destroy;override;
  120. { writes all declarations except the specified one }
  121. procedure write_parameter_lists(skipdef:tprocdef);
  122. { tests, if all procedures definitions are defined and not }
  123. { only forward }
  124. procedure check_forward; virtual;
  125. { do not override this routine in platform-specific subclasses,
  126. override ppuwrite_platform instead }
  127. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  128. procedure buildderef;override;
  129. procedure deref;override;
  130. function find_procdef_bytype(pt:Tproctypeoption):Tprocdef;
  131. function find_bytype_parameterless(pt:Tproctypeoption):Tprocdef;
  132. function find_procdef_bypara(para:TFPObjectList;retdef:tdef;cpoptions:tcompare_paras_options):Tprocdef;
  133. function find_procdef_bytype_and_para(pt:Tproctypeoption;para:TFPObjectList;retdef:tdef;cpoptions:tcompare_paras_options):Tprocdef;
  134. function find_procdef_byoptions(ops:tprocoptions): Tprocdef;
  135. function find_procdef_byprocvardef(d:Tprocvardef):Tprocdef;
  136. function find_procdef_assignment_operator(fromdef,todef:tdef;var besteq:tequaltype):Tprocdef;
  137. function find_procdef_enumerator_operator(fromdef,todef:tdef;var besteq:tequaltype):Tprocdef;
  138. property ProcdefList:TFPObjectList read FProcdefList;
  139. end;
  140. tprocsymclass = class of tprocsym;
  141. ttypesym = class(Tstoredsym)
  142. public
  143. typedef : tdef;
  144. typedefderef : tderef;
  145. fprettyname : ansistring;
  146. constructor create(const n : string;def:tdef;doregister:boolean);virtual;
  147. destructor destroy;override;
  148. constructor ppuload(ppufile:tcompilerppufile);
  149. { do not override this routine in platform-specific subclasses,
  150. override ppuwrite_platform instead }
  151. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  152. procedure buildderef;override;
  153. procedure deref;override;
  154. function prettyname : string;override;
  155. end;
  156. ttypesymclass = class of ttypesym;
  157. tabstractvarsym = class(tstoredsym)
  158. varoptions : tvaroptions;
  159. varspez : tvarspez; { sets the type of access }
  160. varregable : tvarregable;
  161. varstate : tvarstate;
  162. { Has the address of this variable potentially escaped the
  163. block in which is was declared?
  164. could also be part of tabstractnormalvarsym, but there's
  165. one byte left here till the next 4 byte alignment }
  166. addr_taken : boolean;
  167. { true if the variable is accessed in a different scope }
  168. different_scope : boolean;
  169. constructor create(st:tsymtyp;const n : string;vsp:tvarspez;def:tdef;vopts:tvaroptions;doregister:boolean);
  170. constructor ppuload(st:tsymtyp;ppufile:tcompilerppufile);
  171. procedure ppuwrite(ppufile:tcompilerppufile);override;
  172. procedure buildderef;override;
  173. procedure deref;override;
  174. function getsize : asizeint;
  175. function getpackedbitsize : longint;
  176. function is_regvar(refpara: boolean):boolean;
  177. private
  178. _vardef : tdef;
  179. vardefderef : tderef;
  180. procedure setregable;
  181. procedure setvardef(const def: tdef);
  182. procedure setvardef_and_regable(def:tdef);
  183. public
  184. property vardef: tdef read _vardef write setvardef_and_regable;
  185. end;
  186. tfieldvarsym = class(tabstractvarsym)
  187. { offset in record/object, for bitpacked fields the offset is
  188. given in bit, else in bytes }
  189. fieldoffset : asizeint;
  190. {$ifdef llvm}
  191. { the llvm version of the record does not support variants, }
  192. { so the llvm equivalent field may not be at the exact same }
  193. { offset -> store the difference (bits for bitpacked records, }
  194. { bytes otherwise) }
  195. offsetfromllvmfield : aint;
  196. { number of the closest field in the llvm definition }
  197. llvmfieldnr : longint;
  198. {$endif llvm}
  199. externalname : pshortstring;
  200. {$ifdef symansistr}
  201. cachedmangledname: TSymStr; { mangled name for ObjC or Java }
  202. {$else symansistr}
  203. cachedmangledname: pshortstring; { mangled name for ObjC or Java }
  204. {$endif symansistr}
  205. constructor create(const n : string;vsp:tvarspez;def:tdef;vopts:tvaroptions;doregister:boolean);virtual;
  206. constructor ppuload(ppufile:tcompilerppufile);
  207. { do not override this routine in platform-specific subclasses,
  208. override ppuwrite_platform instead }
  209. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  210. procedure set_externalname(const s:string);virtual;
  211. function mangledname:TSymStr;override;
  212. destructor destroy;override;
  213. end;
  214. tfieldvarsymclass = class of tfieldvarsym;
  215. tabstractnormalvarsym = class(tabstractvarsym)
  216. defaultconstsym : tsym;
  217. defaultconstsymderef : tderef;
  218. { register/reference for local var }
  219. localloc : TLocation;
  220. { initial location so it can still be initialized later after the location was changed by SSA }
  221. initialloc : TLocation;
  222. { migrated to a parentfpstruct because of nested access (not written to ppu, because not important and would change interface crc) }
  223. inparentfpstruct : boolean;
  224. { the variable is not living at entry of the scope, so it does not need to be initialized if it is a reg. var
  225. (not written to ppu, because not important and would change interface crc) }
  226. noregvarinitneeded : boolean;
  227. constructor create(st:tsymtyp;const n : string;vsp:tvarspez;def:tdef;vopts:tvaroptions;doregister:boolean);
  228. constructor ppuload(st:tsymtyp;ppufile:tcompilerppufile);
  229. function globalasmsym: boolean;
  230. procedure ppuwrite(ppufile:tcompilerppufile);override;
  231. procedure buildderef;override;
  232. procedure deref;override;
  233. end;
  234. tlocalvarsym = class(tabstractnormalvarsym)
  235. constructor create(const n : string;vsp:tvarspez;def:tdef;vopts:tvaroptions;doregister:boolean);virtual;
  236. constructor ppuload(ppufile:tcompilerppufile);
  237. { do not override this routine in platform-specific subclasses,
  238. override ppuwrite_platform instead }
  239. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  240. end;
  241. tlocalvarsymclass = class of tlocalvarsym;
  242. tparavarsym = class(tabstractnormalvarsym)
  243. paraloc : array[callerside..calleeside] of TCGPara;
  244. paranr : word; { position of this parameter }
  245. { in MacPas mode, "univ" parameters mean that type checking should
  246. be disabled, except that the size of the passed parameter must
  247. match the size of the formal parameter }
  248. univpara : boolean;
  249. {$ifdef EXTDEBUG}
  250. eqval : tequaltype;
  251. {$endif EXTDEBUG}
  252. constructor create(const n : string;nr:word;vsp:tvarspez;def:tdef;vopts:tvaroptions);virtual;
  253. constructor ppuload(ppufile:tcompilerppufile);
  254. destructor destroy;override;
  255. { do not override this routine in platform-specific subclasses,
  256. override ppuwrite_platform instead }
  257. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  258. function needs_finalization: boolean;
  259. end;
  260. tparavarsymclass = class of tparavarsym;
  261. tstaticvarsym = class(tabstractnormalvarsym)
  262. protected
  263. {$ifdef symansistr}
  264. _mangledbasename,
  265. _mangledname : TSymStr;
  266. {$else symansistr}
  267. _mangledbasename,
  268. _mangledname : pshortstring;
  269. {$endif symansistr}
  270. public
  271. section : ansistring;
  272. { if a text buffer has been defined as being initialized from command line
  273. parameters as it is done by iso pascal with the program symbols,
  274. isoindex contains the parameter number }
  275. isoindex : dword;
  276. { if this static variable was created based on a class field variable then this is set
  277. to the symbol of the corresponding class field }
  278. fieldvarsym : tfieldvarsym;
  279. fieldvarsymderef : tderef;
  280. constructor create(const n : string;vsp:tvarspez;def:tdef;vopts:tvaroptions;doregister:boolean);virtual;
  281. constructor create_dll(const n : string;vsp:tvarspez;def:tdef);virtual;
  282. constructor create_C(const n: string; const mangled : TSymStr;vsp:tvarspez;def:tdef);virtual;
  283. constructor create_from_fieldvar(const n:string;fieldvar:tfieldvarsym);virtual;
  284. constructor ppuload(ppufile:tcompilerppufile);
  285. destructor destroy;override;
  286. { do not override this routine in platform-specific subclasses,
  287. override ppuwrite_platform instead }
  288. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  289. procedure buildderef;override;
  290. procedure deref;override;
  291. function mangledname:TSymStr;override;
  292. procedure set_mangledbasename(const s: TSymStr);
  293. function mangledbasename: TSymStr;
  294. procedure set_mangledname(const s:TSymStr);virtual;
  295. procedure set_raw_mangledname(const s:TSymStr);
  296. end;
  297. tstaticvarsymclass = class of tstaticvarsym;
  298. tabsolutevarsym = class(tabstractvarsym)
  299. public
  300. abstyp : absolutetyp;
  301. asmname : pshortstring;
  302. addroffset : PUint;
  303. ref : tpropaccesslist;
  304. constructor create(const n : string;def:tdef);virtual;
  305. constructor create_ref(const n : string;def:tdef;_ref:tpropaccesslist);virtual;
  306. destructor destroy;override;
  307. constructor ppuload(ppufile:tcompilerppufile);
  308. procedure buildderef;override;
  309. procedure deref;override;
  310. function mangledname : TSymStr;override;
  311. { do not override this routine in platform-specific subclasses,
  312. override ppuwrite_platform instead }
  313. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  314. end;
  315. tabsolutevarsymclass = class of tabsolutevarsym;
  316. tpropaccesslisttypes=(palt_none,palt_read,palt_write,palt_stored);
  317. tpropertysym = class(Tstoredsym)
  318. protected
  319. procedure finalize_getter_or_setter_for_sym(getset: tpropaccesslisttypes; sym: tsym; fielddef: tdef; accessordef: tprocdef); virtual;
  320. public
  321. propoptions : tpropertyoptions;
  322. overriddenpropsym : tpropertysym;
  323. overriddenpropsymderef : tderef;
  324. propdef : tdef;
  325. propdefderef : tderef;
  326. indexdef : tdef;
  327. indexdefderef : tderef;
  328. index,
  329. default : longint;
  330. dispid : longint;
  331. propaccesslist: array[tpropaccesslisttypes] of tpropaccesslist;
  332. parast : tsymtable;
  333. constructor create(const n : string);virtual;
  334. destructor destroy;override;
  335. constructor ppuload(ppufile:tcompilerppufile);
  336. function getsize : asizeint;
  337. { do not override this routine in platform-specific subclasses,
  338. override ppuwrite_platform instead }
  339. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  340. procedure buildderef;override;
  341. procedure deref;override;
  342. function getpropaccesslist(pap:tpropaccesslisttypes;out plist:tpropaccesslist):boolean;
  343. { copies the settings of the current propertysym to p; a bit like
  344. a form of getcopy, but without the name }
  345. procedure makeduplicate(p: tpropertysym; readprocdef, writeprocdef: tprocdef; out paranr: word);
  346. procedure add_accessor_parameters(readprocdef, writeprocdef: tprocdef);
  347. procedure add_index_parameter(var paranr: word; readprocdef, writeprocdef: tprocdef);
  348. { set up the accessors for this property }
  349. procedure add_getter_or_setter_for_sym(getset: tpropaccesslisttypes; sym: tsym; fielddef: tdef; accessordef: tprocdef);
  350. procedure register_override(overriddenprop: tpropertysym);
  351. { inherit the read/write property }
  352. procedure inherit_accessor(getset: tpropaccesslisttypes); virtual;
  353. end;
  354. tpropertysymclass = class of tpropertysym;
  355. tconstvalue = record
  356. case integer of
  357. 0: (valueord : tconstexprint);
  358. 1: (valueordptr : tconstptruint);
  359. 2: (valueptr : pointer; len : longint);
  360. end;
  361. tconstsym = class(tstoredsym)
  362. constdef : tdef;
  363. constdefderef : tderef;
  364. consttyp : tconsttyp;
  365. value : tconstvalue;
  366. constructor create_ord(const n : string;t : tconsttyp;v : tconstexprint;def:tdef);virtual;
  367. constructor create_ordptr(const n : string;t : tconsttyp;v : tconstptruint;def:tdef);virtual;
  368. constructor create_ptr(const n : string;t : tconsttyp;v : pointer;def:tdef);virtual;
  369. constructor create_string(const n : string;t : tconsttyp;str:pchar;l:longint;def:tdef);virtual;
  370. constructor create_wstring(const n : string;t : tconsttyp;pw:pcompilerwidestring);virtual;
  371. constructor ppuload(ppufile:tcompilerppufile);
  372. destructor destroy;override;
  373. procedure buildderef;override;
  374. procedure deref;override;
  375. { do not override this routine in platform-specific subclasses,
  376. override ppuwrite_platform instead }
  377. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  378. end;
  379. tconstsymclass = class of tconstsym;
  380. tenumsym = class(Tstoredsym)
  381. value : longint;
  382. definition : tenumdef;
  383. definitionderef : tderef;
  384. constructor create(const n : string;def : tenumdef;v : longint);virtual;
  385. constructor ppuload(ppufile:tcompilerppufile);
  386. { do not override this routine in platform-specific subclasses,
  387. override ppuwrite_platform instead }
  388. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  389. procedure buildderef;override;
  390. procedure deref;override;
  391. end;
  392. tenumsymclass = class of tenumsym;
  393. tsyssym = class(Tstoredsym)
  394. number : tinlinenumber;
  395. constructor create(const n : string;l : tinlinenumber);virtual;
  396. constructor ppuload(ppufile:tcompilerppufile);
  397. destructor destroy;override;
  398. { do not override this routine in platform-specific subclasses,
  399. override ppuwrite_platform instead }
  400. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  401. class function find_by_number(l:longint):tsyssym;
  402. end;
  403. tsyssymclass = class of tsyssym;
  404. const
  405. maxmacrolen=16*1024;
  406. type
  407. pmacrobuffer = ^tmacrobuffer;
  408. tmacrobuffer = array[0..maxmacrolen-1] of char;
  409. tmacro = class(tstoredsym)
  410. {Normally true, but false when a previously defined macro is undef-ed}
  411. defined : boolean;
  412. {True if this is a mac style compiler variable, in which case no macro
  413. substitutions shall be done.}
  414. is_compiler_var : boolean;
  415. {Whether the macro was used. NOTE: A use of a macro which was never defined}
  416. {e. g. an IFDEF which returns false, will not be registered as used,}
  417. {since there is no place to register its use. }
  418. is_used : boolean;
  419. buftext : pchar;
  420. buflen : longint;
  421. constructor create(const n : string);
  422. constructor ppuload(ppufile:tcompilerppufile);
  423. { do not override this routine in platform-specific subclasses,
  424. override ppuwrite_platform instead }
  425. procedure ppuwrite(ppufile:tcompilerppufile);override;final;
  426. destructor destroy;override;
  427. function GetCopy:tmacro;
  428. end;
  429. var
  430. generrorsym : tsym;
  431. clabelsym: tlabelsymclass;
  432. cunitsym: tunitsymclass;
  433. cprogramparasym: tprogramparasymclass;
  434. cnamespacesym: tnamespacesymclass;
  435. cprocsym: tprocsymclass;
  436. ctypesym: ttypesymclass;
  437. cfieldvarsym: tfieldvarsymclass;
  438. clocalvarsym: tlocalvarsymclass;
  439. cparavarsym: tparavarsymclass;
  440. cstaticvarsym: tstaticvarsymclass;
  441. cabsolutevarsym: tabsolutevarsymclass;
  442. cpropertysym: tpropertysymclass;
  443. cconstsym: tconstsymclass;
  444. cenumsym: tenumsymclass;
  445. csyssym: tsyssymclass;
  446. { generate internal static field name based on regular field name }
  447. function internal_static_field_name(const fieldname: TSymStr): TSymStr;
  448. function get_high_value_sym(vs: tparavarsym):tsym; { marking it as inline causes IE 200311075 during loading from ppu file }
  449. procedure check_hints(const srsym: tsym; const symoptions: tsymoptions; const deprecatedmsg : pshortstring);inline;
  450. procedure check_hints(const srsym: tsym; const symoptions: tsymoptions; const deprecatedmsg : pshortstring;filepos:tfileposinfo);
  451. implementation
  452. uses
  453. { global }
  454. verbose,
  455. { target }
  456. systems,
  457. { symtable }
  458. defutil,symtable,
  459. fmodule,
  460. { tree }
  461. node,
  462. { aasm }
  463. aasmdata,
  464. { codegen }
  465. paramgr,
  466. procinfo,
  467. { ppu }
  468. entfile
  469. ;
  470. {****************************************************************************
  471. Helpers
  472. ****************************************************************************}
  473. function internal_static_field_name(const fieldname: TSymStr): TSymStr;
  474. begin
  475. result:='$_static_'+fieldname;
  476. end;
  477. function get_high_value_sym(vs: tparavarsym):tsym;
  478. begin
  479. result := tsym(vs.owner.Find('high'+vs.name));
  480. end;
  481. procedure check_hints(const srsym: tsym; const symoptions: tsymoptions; const deprecatedmsg : pshortstring);
  482. begin
  483. check_hints(srsym,symoptions,deprecatedmsg,current_filepos);
  484. end;
  485. procedure check_hints(const srsym: tsym; const symoptions: tsymoptions; const deprecatedmsg : pshortstring;filepos:tfileposinfo);
  486. begin
  487. if not assigned(srsym) then
  488. internalerror(200602051);
  489. if sp_hint_deprecated in symoptions then
  490. if (sp_has_deprecated_msg in symoptions) and (deprecatedmsg <> nil) then
  491. MessagePos2(filepos,sym_w_deprecated_symbol_with_msg,srsym.realname,deprecatedmsg^)
  492. else
  493. MessagePos1(filepos,sym_w_deprecated_symbol,srsym.realname);
  494. if sp_hint_experimental in symoptions then
  495. MessagePos1(filepos,sym_w_experimental_symbol,srsym.realname);
  496. if sp_hint_platform in symoptions then
  497. MessagePos1(filepos,sym_w_non_portable_symbol,srsym.realname);
  498. if sp_hint_library in symoptions then
  499. MessagePos1(filepos,sym_w_library_symbol,srsym.realname);
  500. if sp_hint_unimplemented in symoptions then
  501. MessagePos1(filepos,sym_w_non_implemented_symbol,srsym.realname);
  502. end;
  503. {****************************************************************************
  504. TSYM (base for all symtypes)
  505. ****************************************************************************}
  506. constructor tstoredsym.create(st:tsymtyp;const n : string;doregister:boolean);
  507. begin
  508. inherited create(st,n);
  509. end;
  510. constructor tstoredsym.ppuload(st:tsymtyp;ppufile:tcompilerppufile);
  511. begin
  512. inherited Create(st,ppufile.getstring);
  513. SymId:=ppufile.getlongint;
  514. current_module.symlist[SymId]:=self;
  515. ppufile.getposinfo(fileinfo);
  516. visibility:=tvisibility(ppufile.getbyte);
  517. ppufile.getsmallset(symoptions);
  518. if sp_has_deprecated_msg in symoptions then
  519. deprecatedmsg:=ppufile.getpshortstring
  520. else
  521. deprecatedmsg:=nil;
  522. end;
  523. procedure tstoredsym.ppuwrite(ppufile:tcompilerppufile);
  524. var
  525. oldintfcrc : boolean;
  526. begin
  527. ppufile.putstring(realname);
  528. ppufile.putlongint(SymId);
  529. ppufile.putposinfo(fileinfo);
  530. ppufile.putbyte(byte(visibility));
  531. { symoptions can differ between interface and implementation, except
  532. for overload (this is checked in pdecsub.proc_add_definition() )
  533. These differences can lead to compiler crashes, so ignore them.
  534. This does mean that changing e.g. the "deprecated" state of a symbol
  535. by itself will not trigger a recompilation of dependent units.
  536. }
  537. oldintfcrc:=ppufile.do_interface_crc;
  538. ppufile.do_interface_crc:=false;
  539. ppufile.putsmallset(symoptions);
  540. if sp_has_deprecated_msg in symoptions then
  541. ppufile.putstring(deprecatedmsg^);
  542. ppufile.do_interface_crc:=oldintfcrc;
  543. end;
  544. procedure tstoredsym.ppuload_subentries(ppufile: tcompilerppufile);
  545. begin
  546. { by default: do nothing }
  547. end;
  548. procedure tstoredsym.ppuwrite_subentries(ppufile: tcompilerppufile);
  549. begin
  550. { by default: do nothing }
  551. end;
  552. procedure tstoredsym.buildderef;
  553. begin
  554. inherited;
  555. if not registered then
  556. register_sym;
  557. end;
  558. procedure tstoredsym.writeentry(ppufile: tcompilerppufile; ibnr: byte);
  559. begin
  560. ppuwrite_platform(ppufile);
  561. ppufile.writeentry(ibnr);
  562. end;
  563. procedure tstoredsym.ppuwrite_platform(ppufile: tcompilerppufile);
  564. begin
  565. { by default: do nothing }
  566. end;
  567. procedure tstoredsym.ppuload_platform(ppufile: tcompilerppufile);
  568. begin
  569. { by default: do nothing }
  570. end;
  571. destructor tstoredsym.destroy;
  572. begin
  573. rtti_attribute_list.free;
  574. inherited destroy;
  575. end;
  576. procedure tstoredsym.register_sym;
  577. begin
  578. if registered then
  579. exit;
  580. { Register in current_module }
  581. if assigned(current_module) then
  582. begin
  583. current_module.symlist.Add(self);
  584. SymId:=current_module.symlist.Count-1;
  585. end
  586. else
  587. SymId:=symid_registered_nost;
  588. end;
  589. {****************************************************************************
  590. TLABELSYM
  591. ****************************************************************************}
  592. constructor tlabelsym.create(const n : string);
  593. begin
  594. inherited create(labelsym,n,true);
  595. used:=false;
  596. defined:=false;
  597. nonlocal:=false;
  598. code:=nil;
  599. end;
  600. constructor tlabelsym.ppuload(ppufile:tcompilerppufile);
  601. begin
  602. inherited ppuload(labelsym,ppufile);
  603. code:=nil;
  604. used:=false;
  605. nonlocal:=false;
  606. defined:=true;
  607. ppuload_platform(ppufile);
  608. end;
  609. procedure tlabelsym.ppuwrite(ppufile:tcompilerppufile);
  610. begin
  611. if owner.symtabletype=globalsymtable then
  612. Message(sym_e_ill_label_decl)
  613. else
  614. begin
  615. inherited ppuwrite(ppufile);
  616. writeentry(ppufile,iblabelsym);
  617. end;
  618. end;
  619. function tlabelsym.mangledname:TSymStr;
  620. begin
  621. if (asmblocklabel=nil) then
  622. begin
  623. if nonlocal then
  624. current_asmdata.getglobaljumplabel(asmblocklabel)
  625. else
  626. current_asmdata.getjumplabel(asmblocklabel);
  627. end;
  628. result:=asmblocklabel.name;
  629. end;
  630. {****************************************************************************
  631. TUNITSYM
  632. ****************************************************************************}
  633. constructor tunitsym.create(const n : string;amodule : tobject;doregister:boolean);
  634. begin
  635. inherited create(unitsym,n,doregister);
  636. module:=amodule;
  637. end;
  638. constructor tunitsym.ppuload(ppufile:tcompilerppufile);
  639. begin
  640. inherited ppuload(unitsym,ppufile);
  641. module:=nil;
  642. ppuload_platform(ppufile);
  643. end;
  644. destructor tunitsym.destroy;
  645. begin
  646. inherited destroy;
  647. end;
  648. procedure tunitsym.ppuwrite(ppufile:tcompilerppufile);
  649. begin
  650. inherited ppuwrite(ppufile);
  651. writeentry(ppufile,ibunitsym);
  652. end;
  653. {****************************************************************************
  654. TPROGRAMPARASYM
  655. ****************************************************************************}
  656. constructor tprogramparasym.create(const n : string; i : dword);
  657. begin
  658. inherited create(programparasym,n,true);
  659. isoindex:=i;
  660. end;
  661. constructor tprogramparasym.ppuload(ppufile : tcompilerppufile);
  662. begin
  663. { program parameter syms (iso pascal style) might be never written to a ppu }
  664. internalerror(2015050102);
  665. end;
  666. destructor tprogramparasym.destroy;
  667. begin
  668. inherited destroy;
  669. end;
  670. procedure tprogramparasym.ppuwrite(ppufile : tcompilerppufile);
  671. begin
  672. { program parameter syms (iso pascal style) might be never written to a ppu }
  673. internalerror(2015050101);
  674. end;
  675. {****************************************************************************
  676. TNAMESPACESYM
  677. ****************************************************************************}
  678. constructor tnamespacesym.create(const n : string);
  679. begin
  680. inherited create(namespacesym,n,true);
  681. unitsym:=nil;
  682. unitsymderef.reset;
  683. end;
  684. constructor tnamespacesym.ppuload(ppufile:tcompilerppufile);
  685. begin
  686. inherited ppuload(namespacesym,ppufile);
  687. ppufile.getderef(unitsymderef);
  688. ppuload_platform(ppufile);
  689. end;
  690. procedure tnamespacesym.ppuwrite(ppufile:tcompilerppufile);
  691. begin
  692. inherited ppuwrite(ppufile);
  693. ppufile.putderef(unitsymderef);
  694. writeentry(ppufile,ibnamespacesym);
  695. end;
  696. procedure tnamespacesym.buildderef;
  697. begin
  698. inherited buildderef;
  699. unitsymderef.build(unitsym);
  700. end;
  701. procedure tnamespacesym.deref;
  702. begin
  703. inherited deref;
  704. unitsym:=tsym(unitsymderef.resolve);
  705. end;
  706. {****************************************************************************
  707. TPROCSYM
  708. ****************************************************************************}
  709. constructor tprocsym.create(const n : string);
  710. var
  711. i: longint;
  712. begin
  713. if not(ts_lowercase_proc_start in current_settings.targetswitches) or
  714. (n='') then
  715. inherited create(procsym,n,true)
  716. else
  717. begin
  718. { YToX -> yToX
  719. RC64Encode -> rc64Encode
  720. Test -> test
  721. }
  722. i:=2;
  723. while i<=length(n) do
  724. begin
  725. if not(n[i] in ['A'..'Z']) then
  726. begin
  727. if (i>2) and
  728. (n[i] in ['a'..'z']) then
  729. dec(i);
  730. break;
  731. end;
  732. inc(i);
  733. end;
  734. inherited create(procsym,lower(copy(n,1,i-1))+copy(n,i,length(n)),true);
  735. end;
  736. FProcdefList:=TFPObjectList.Create(false);
  737. FProcdefderefList:=nil;
  738. { the tprocdef have their own symoptions, make the procsym
  739. always visible }
  740. visibility:=vis_public;
  741. end;
  742. constructor tprocsym.ppuload(ppufile:tcompilerppufile);
  743. var
  744. pdderef : tderef;
  745. i,
  746. pdcnt : longint;
  747. begin
  748. inherited ppuload(procsym,ppufile);
  749. FProcdefList:=TFPObjectList.Create(false);
  750. FProcdefDerefList:=TFPList.Create;
  751. pdcnt:=ppufile.getword;
  752. for i:=1 to pdcnt do
  753. begin
  754. ppufile.getderef(pdderef);
  755. FProcdefDerefList.Add(Pointer(PtrInt(pdderef.dataidx)));
  756. end;
  757. ppuload_platform(ppufile);
  758. end;
  759. destructor tprocsym.destroy;
  760. begin
  761. FProcdefList.Free;
  762. if assigned(FProcdefDerefList) then
  763. FProcdefDerefList.Free;
  764. inherited destroy;
  765. end;
  766. procedure tprocsym.ppuwrite(ppufile:tcompilerppufile);
  767. var
  768. i : longint;
  769. d : tderef;
  770. begin
  771. inherited ppuwrite(ppufile);
  772. if fprocdefdereflist=nil then
  773. internalerror(2013121801);
  774. ppufile.putword(FProcdefDerefList.Count);
  775. for i:=0 to FProcdefDerefList.Count-1 do
  776. begin
  777. d.dataidx:=PtrInt(FProcdefDerefList[i]);
  778. ppufile.putderef(d);
  779. end;
  780. writeentry(ppufile,ibprocsym);
  781. end;
  782. procedure tprocsym.write_parameter_lists(skipdef:tprocdef);
  783. var
  784. i : longint;
  785. pd : tprocdef;
  786. begin
  787. for i:=0 to ProcdefList.Count-1 do
  788. begin
  789. pd:=tprocdef(ProcdefList[i]);
  790. if pd<>skipdef then
  791. MessagePos1(pd.fileinfo,sym_e_param_list,pd.fullprocname(false));
  792. end;
  793. end;
  794. procedure tprocsym.check_forward;
  795. var
  796. i : longint;
  797. pd : tprocdef;
  798. begin
  799. for i:=0 to ProcdefList.Count-1 do
  800. begin
  801. pd:=tprocdef(ProcdefList[i]);
  802. if (pd.owner=owner) and (pd.forwarddef) then
  803. begin
  804. { For mode macpas. Make implicit externals (procedures declared in the interface
  805. section which do not have a counterpart in the implementation)
  806. to be an imported procedure }
  807. if (m_mac in current_settings.modeswitches) and
  808. (pd.interfacedef) then
  809. begin
  810. pd.setmangledname(target_info.CPrefix+tprocdef(pd).procsym.realname);
  811. if (not current_module.interface_only) then
  812. MessagePos1(pd.fileinfo,sym_w_forward_not_resolved,pd.fullprocname(false));
  813. end
  814. else
  815. begin
  816. MessagePos1(pd.fileinfo,sym_e_forward_not_resolved,pd.fullprocname(false));
  817. end;
  818. { Turn further error messages off }
  819. pd.forwarddef:=false;
  820. end;
  821. end;
  822. end;
  823. procedure tprocsym.buildderef;
  824. var
  825. i : longint;
  826. pd : tprocdef;
  827. d : tderef;
  828. begin
  829. inherited;
  830. if not assigned(FProcdefDerefList) then
  831. FProcdefDerefList:=TFPList.Create
  832. else
  833. FProcdefDerefList.Clear;
  834. for i:=0 to ProcdefList.Count-1 do
  835. begin
  836. pd:=tprocdef(ProcdefList[i]);
  837. { only write the proc definitions that belong
  838. to this procsym and are in the global symtable }
  839. if pd.owner=owner then
  840. begin
  841. d.build(pd);
  842. FProcdefDerefList.Add(Pointer(PtrInt(d.dataidx)));
  843. end;
  844. end;
  845. end;
  846. procedure tprocsym.deref;
  847. var
  848. i : longint;
  849. pd : tprocdef;
  850. d : tderef;
  851. begin
  852. { Clear all procdefs }
  853. ProcdefList.Clear;
  854. if not assigned(FProcdefDerefList) then
  855. internalerror(200611031);
  856. for i:=0 to FProcdefDerefList.Count-1 do
  857. begin
  858. d.dataidx:=PtrInt(FProcdefDerefList[i]);
  859. pd:=tprocdef(d.resolve);
  860. ProcdefList.Add(pd);
  861. end;
  862. end;
  863. function Tprocsym.Find_procdef_bytype(pt:Tproctypeoption):Tprocdef;
  864. var
  865. i : longint;
  866. pd : tprocdef;
  867. begin
  868. result:=nil;
  869. for i:=0 to ProcdefList.Count-1 do
  870. begin
  871. pd:=tprocdef(ProcdefList[i]);
  872. if pd.proctypeoption=pt then
  873. begin
  874. result:=pd;
  875. exit;
  876. end;
  877. end;
  878. end;
  879. function tprocsym.find_bytype_parameterless(pt: Tproctypeoption): Tprocdef;
  880. var
  881. i,j : longint;
  882. pd : tprocdef;
  883. found : boolean;
  884. begin
  885. result:=nil;
  886. for i:=0 to ProcdefList.Count-1 do
  887. begin
  888. pd:=tprocdef(ProcdefList[i]);
  889. if (pd.proctypeoption=pt) then
  890. begin
  891. found:=true;
  892. for j:=0 to pd.paras.count-1 do
  893. begin
  894. if not(vo_is_hidden_para in tparavarsym(pd.paras[j]).varoptions) then
  895. begin
  896. found:=false;
  897. break;
  898. end;
  899. end;
  900. if found then
  901. begin
  902. result:=pd;
  903. exit;
  904. end;
  905. end;
  906. end;
  907. end;
  908. function check_procdef_paras(pd:tprocdef;para:TFPObjectList;retdef:tdef;
  909. cpoptions:tcompare_paras_options): tprocdef;
  910. var
  911. eq: tequaltype;
  912. begin
  913. result:=nil;
  914. if assigned(retdef) then
  915. eq:=compare_defs(retdef,pd.returndef,nothingn)
  916. else
  917. eq:=te_equal;
  918. if (eq>=te_equal) or
  919. ((cpo_allowconvert in cpoptions) and (eq>te_incompatible)) then
  920. begin
  921. eq:=compare_paras(para,pd.paras,cp_value_equal_const,cpoptions);
  922. if (eq>=te_equal) or
  923. ((cpo_allowconvert in cpoptions) and (eq>te_incompatible)) then
  924. begin
  925. result:=pd;
  926. exit;
  927. end;
  928. end;
  929. end;
  930. function Tprocsym.Find_procdef_bypara(para:TFPObjectList;retdef:tdef;
  931. cpoptions:tcompare_paras_options):Tprocdef;
  932. var
  933. i : longint;
  934. pd : tprocdef;
  935. begin
  936. result:=nil;
  937. for i:=0 to ProcdefList.Count-1 do
  938. begin
  939. pd:=tprocdef(ProcdefList[i]);
  940. result:=check_procdef_paras(pd,para,retdef,cpoptions);
  941. if assigned(result) then
  942. exit;
  943. end;
  944. end;
  945. function Tprocsym.find_procdef_bytype_and_para(pt:Tproctypeoption;
  946. para:TFPObjectList;retdef:tdef;cpoptions:tcompare_paras_options):Tprocdef;
  947. var
  948. i : longint;
  949. pd : tprocdef;
  950. begin
  951. result:=nil;
  952. for i:=0 to ProcdefList.Count-1 do
  953. begin
  954. pd:=tprocdef(ProcdefList[i]);
  955. if pd.proctypeoption=pt then
  956. begin
  957. result:=check_procdef_paras(pd,para,retdef,cpoptions);
  958. if assigned(result) then
  959. exit;
  960. end;
  961. end;
  962. end;
  963. function tprocsym.find_procdef_byoptions(ops: tprocoptions): Tprocdef;
  964. var
  965. i : longint;
  966. pd : tprocdef;
  967. begin
  968. result:=nil;
  969. for i:=0 to ProcdefList.Count-1 do
  970. begin
  971. pd:=tprocdef(ProcdefList[i]);
  972. if ops * pd.procoptions = ops then
  973. begin
  974. result:=pd;
  975. exit;
  976. end;
  977. end;
  978. end;
  979. function Tprocsym.Find_procdef_byprocvardef(d:Tprocvardef):Tprocdef;
  980. var
  981. i : longint;
  982. bestpd,
  983. pd : tprocdef;
  984. eq,besteq : tequaltype;
  985. sym: tsym;
  986. ps: tprocsym;
  987. begin
  988. { This function will return the pprocdef of pprocsym that
  989. is the best match for procvardef. When there are multiple
  990. matches it returns nil.}
  991. result:=nil;
  992. bestpd:=nil;
  993. besteq:=te_incompatible;
  994. ps:=self;
  995. repeat
  996. for i:=0 to ps.ProcdefList.Count-1 do
  997. begin
  998. pd:=tprocdef(ps.ProcdefList[i]);
  999. eq:=proc_to_procvar_equal(pd,d,false);
  1000. if eq>=te_convert_l1 then
  1001. begin
  1002. { multiple procvars with the same equal level }
  1003. if assigned(bestpd) and
  1004. (besteq=eq) then
  1005. exit;
  1006. if eq>besteq then
  1007. begin
  1008. besteq:=eq;
  1009. bestpd:=pd;
  1010. end;
  1011. end;
  1012. end;
  1013. { maybe TODO: also search class helpers? -- this code is similar to
  1014. what happens in htypechk in
  1015. tcallcandidates.collect_overloads_in_struct: keep searching in
  1016. parent types in case the currently found procdef is marked as
  1017. "overload" and we haven't found a proper match yet }
  1018. if assigned(ps.owner.defowner) and
  1019. (ps.owner.defowner.typ=objectdef) and
  1020. assigned(tobjectdef(ps.owner.defowner).childof) and
  1021. (not assigned(bestpd) or
  1022. (po_overload in bestpd.procoptions)) then
  1023. begin
  1024. sym:=tsym(tobjectdef(ps.owner.defowner).childof.symtable.find(ps.name));
  1025. if assigned(sym) and
  1026. (sym.typ=procsym) then
  1027. ps:=tprocsym(sym)
  1028. else
  1029. ps:=nil;
  1030. end
  1031. else
  1032. ps:=nil;
  1033. until (besteq>=te_equal) or
  1034. not assigned(ps);
  1035. result:=bestpd;
  1036. end;
  1037. function Tprocsym.Find_procdef_assignment_operator(fromdef,todef:tdef;var besteq:tequaltype):Tprocdef;
  1038. var
  1039. paraidx, realparamcount,
  1040. i, j : longint;
  1041. bestpd,
  1042. hpd,
  1043. pd : tprocdef;
  1044. convtyp : tconverttype;
  1045. eq : tequaltype;
  1046. begin
  1047. { This function will return the pprocdef of pprocsym that
  1048. is the best match for fromdef and todef. }
  1049. result:=nil;
  1050. bestpd:=nil;
  1051. besteq:=te_incompatible;
  1052. for i:=0 to ProcdefList.Count-1 do
  1053. begin
  1054. pd:=tprocdef(ProcdefList[i]);
  1055. if (pd.owner.symtabletype=staticsymtable) and not pd.owner.iscurrentunit then
  1056. continue;
  1057. if (equal_defs(todef,pd.returndef) or
  1058. { shortstrings of different lengths are ok as result }
  1059. (is_shortstring(todef) and is_shortstring(pd.returndef))) and
  1060. { the result type must be always really equal and not an alias,
  1061. if you mess with this code, check tw4093 }
  1062. ((todef=pd.returndef) or
  1063. (
  1064. not(df_unique in todef.defoptions) and
  1065. not(df_unique in pd.returndef.defoptions)
  1066. )
  1067. ) then
  1068. begin
  1069. paraidx:=0;
  1070. { ignore vs_hidden parameters }
  1071. while (paraidx<pd.paras.count) and
  1072. assigned(pd.paras[paraidx]) and
  1073. (vo_is_hidden_para in tparavarsym(pd.paras[paraidx]).varoptions) do
  1074. inc(paraidx);
  1075. realparamcount:=0;
  1076. for j := 0 to pd.paras.Count-1 do
  1077. if assigned(pd.paras[j]) and not (vo_is_hidden_para in tparavarsym(pd.paras[j]).varoptions) then
  1078. inc(realparamcount);
  1079. if (paraidx<pd.paras.count) and
  1080. assigned(pd.paras[paraidx]) and
  1081. (realparamcount = 1) then
  1082. begin
  1083. eq:=compare_defs_ext(fromdef,tparavarsym(pd.paras[paraidx]).vardef,nothingn,convtyp,hpd,[]);
  1084. { alias? if yes, only l1 choice,
  1085. if you mess with this code, check tw4093 }
  1086. if (eq=te_exact) and
  1087. (fromdef<>tparavarsym(pd.paras[paraidx]).vardef) and
  1088. ((df_unique in fromdef.defoptions) or
  1089. (df_unique in tparavarsym(pd.paras[paraidx]).vardef.defoptions)) then
  1090. eq:=te_convert_l1;
  1091. if eq=te_exact then
  1092. begin
  1093. besteq:=eq;
  1094. result:=pd;
  1095. exit;
  1096. end;
  1097. if eq>besteq then
  1098. begin
  1099. bestpd:=pd;
  1100. besteq:=eq;
  1101. end;
  1102. end;
  1103. end;
  1104. end;
  1105. result:=bestpd;
  1106. end;
  1107. function Tprocsym.find_procdef_enumerator_operator(fromdef,todef:tdef;var besteq:tequaltype):Tprocdef;
  1108. var
  1109. paraidx, realparamcount,
  1110. i, j : longint;
  1111. bestpd,
  1112. hpd,
  1113. pd : tprocdef;
  1114. current : tpropertysym;
  1115. convtyp : tconverttype;
  1116. eq : tequaltype;
  1117. begin
  1118. { This function will return the pprocdef of pprocsym that
  1119. is the best match for fromdef and todef. }
  1120. result:=nil;
  1121. bestpd:=nil;
  1122. besteq:=te_incompatible;
  1123. for i:=0 to ProcdefList.Count-1 do
  1124. begin
  1125. pd:=tprocdef(ProcdefList[i]);
  1126. if (pd.owner.symtabletype=staticsymtable) and not pd.owner.iscurrentunit then
  1127. continue;
  1128. if not (is_class_or_interface_or_object(pd.returndef) or is_record(pd.returndef)) then
  1129. continue;
  1130. current := tpropertysym(tabstractrecorddef(pd.returndef).search_enumerator_current);
  1131. if (current = nil) then
  1132. continue;
  1133. // compare current result def with the todef
  1134. if (equal_defs(todef, current.propdef) or
  1135. { shortstrings of different lengths are ok as result }
  1136. (is_shortstring(todef) and is_shortstring(current.propdef))) and
  1137. { the result type must be always really equal and not an alias,
  1138. if you mess with this code, check tw4093 }
  1139. ((todef=current.propdef) or
  1140. (
  1141. not(df_unique in todef.defoptions) and
  1142. not(df_unique in current.propdef.defoptions)
  1143. )
  1144. ) then
  1145. begin
  1146. paraidx:=0;
  1147. { ignore vs_hidden parameters }
  1148. while (paraidx<pd.paras.count) and
  1149. assigned(pd.paras[paraidx]) and
  1150. (vo_is_hidden_para in tparavarsym(pd.paras[paraidx]).varoptions) do
  1151. inc(paraidx);
  1152. realparamcount:=0;
  1153. for j := 0 to pd.paras.Count-1 do
  1154. if assigned(pd.paras[j]) and not (vo_is_hidden_para in tparavarsym(pd.paras[j]).varoptions) then
  1155. inc(realparamcount);
  1156. if (paraidx<pd.paras.count) and
  1157. assigned(pd.paras[paraidx]) and
  1158. (realparamcount = 1) then
  1159. begin
  1160. eq:=compare_defs_ext(fromdef,tparavarsym(pd.paras[paraidx]).vardef,nothingn,convtyp,hpd,[]);
  1161. { alias? if yes, only l1 choice,
  1162. if you mess with this code, check tw4093 }
  1163. if (eq=te_exact) and
  1164. (fromdef<>tparavarsym(pd.paras[paraidx]).vardef) and
  1165. ((df_unique in fromdef.defoptions) or
  1166. (df_unique in tparavarsym(pd.paras[paraidx]).vardef.defoptions)) then
  1167. eq:=te_convert_l1;
  1168. if eq=te_exact then
  1169. begin
  1170. besteq:=eq;
  1171. result:=pd;
  1172. exit;
  1173. end;
  1174. if eq>besteq then
  1175. begin
  1176. bestpd:=pd;
  1177. besteq:=eq;
  1178. end;
  1179. end;
  1180. end;
  1181. end;
  1182. result:=bestpd;
  1183. end;
  1184. {****************************************************************************
  1185. TERRORSYM
  1186. ****************************************************************************}
  1187. constructor terrorsym.create;
  1188. begin
  1189. inherited create(errorsym,'');
  1190. end;
  1191. procedure terrorsym.register_sym;
  1192. begin
  1193. { these should never be written to a ppu file, since they don't
  1194. derive from tstoredsym }
  1195. Internalerror(2015101801);
  1196. end;
  1197. {****************************************************************************
  1198. TPROPERTYSYM
  1199. ****************************************************************************}
  1200. procedure tpropertysym.finalize_getter_or_setter_for_sym(getset: tpropaccesslisttypes; sym: tsym; fielddef: tdef; accessordef: tprocdef);
  1201. begin
  1202. { do nothing by default }
  1203. end;
  1204. constructor tpropertysym.create(const n : string);
  1205. var
  1206. pap : tpropaccesslisttypes;
  1207. begin
  1208. inherited create(propertysym,n,true);
  1209. propoptions:=[];
  1210. index:=0;
  1211. default:=0;
  1212. propdef:=nil;
  1213. propdefderef.reset;
  1214. indexdef:=nil;
  1215. indexdefderef.reset;
  1216. parast:=nil;
  1217. for pap:=low(tpropaccesslisttypes) to high(tpropaccesslisttypes) do
  1218. propaccesslist[pap]:=tpropaccesslist.create;
  1219. end;
  1220. constructor tpropertysym.ppuload(ppufile:tcompilerppufile);
  1221. var
  1222. pap : tpropaccesslisttypes;
  1223. begin
  1224. inherited ppuload(propertysym,ppufile);
  1225. ppufile.getsmallset(propoptions);
  1226. if ppo_overrides in propoptions then
  1227. ppufile.getderef(overriddenpropsymderef);
  1228. ppufile.getderef(propdefderef);
  1229. index:=ppufile.getlongint;
  1230. default:=ppufile.getlongint;
  1231. ppufile.getderef(indexdefderef);
  1232. for pap:=low(tpropaccesslisttypes) to high(tpropaccesslisttypes) do
  1233. propaccesslist[pap]:=ppufile.getpropaccesslist;
  1234. ppuload_platform(ppufile);
  1235. if [ppo_hasparameters,ppo_overrides]*propoptions=[ppo_hasparameters] then
  1236. begin
  1237. parast:=tparasymtable.create(nil,0);
  1238. tparasymtable(parast).ppuload(ppufile);
  1239. end
  1240. else
  1241. parast:=nil;
  1242. end;
  1243. destructor tpropertysym.destroy;
  1244. var
  1245. pap : tpropaccesslisttypes;
  1246. begin
  1247. for pap:=low(tpropaccesslisttypes) to high(tpropaccesslisttypes) do
  1248. propaccesslist[pap].free;
  1249. parast.free;
  1250. inherited destroy;
  1251. end;
  1252. procedure tpropertysym.buildderef;
  1253. var
  1254. pap : tpropaccesslisttypes;
  1255. begin
  1256. inherited;
  1257. propdefderef.build(propdef);
  1258. indexdefderef.build(indexdef);
  1259. for pap:=low(tpropaccesslisttypes) to high(tpropaccesslisttypes) do
  1260. propaccesslist[pap].buildderef;
  1261. if ppo_overrides in propoptions then
  1262. overriddenpropsymderef.build(overriddenpropsym)
  1263. else
  1264. if ppo_hasparameters in propoptions then
  1265. tparasymtable(parast).buildderef;
  1266. end;
  1267. procedure tpropertysym.deref;
  1268. var
  1269. pap : tpropaccesslisttypes;
  1270. begin
  1271. indexdef:=tdef(indexdefderef.resolve);
  1272. propdef:=tdef(propdefderef.resolve);
  1273. for pap:=low(tpropaccesslisttypes) to high(tpropaccesslisttypes) do
  1274. propaccesslist[pap].resolve;
  1275. if ppo_overrides in propoptions then
  1276. begin
  1277. overriddenpropsym:=tpropertysym(overriddenpropsymderef.resolve);
  1278. if ppo_hasparameters in propoptions then
  1279. parast:=overriddenpropsym.parast.getcopy;
  1280. end
  1281. else
  1282. if ppo_hasparameters in propoptions then
  1283. tparasymtable(parast).deref(false)
  1284. end;
  1285. function tpropertysym.getpropaccesslist(pap:tpropaccesslisttypes;out plist:tpropaccesslist):boolean;
  1286. var
  1287. hpropsym : tpropertysym;
  1288. begin
  1289. result:=false;
  1290. { find property in the overridden list }
  1291. hpropsym:=self;
  1292. repeat
  1293. plist:=hpropsym.propaccesslist[pap];
  1294. if not plist.empty then
  1295. begin
  1296. result:=true;
  1297. exit;
  1298. end;
  1299. hpropsym:=hpropsym.overriddenpropsym;
  1300. until not assigned(hpropsym);
  1301. end;
  1302. procedure tpropertysym.add_accessor_parameters(readprocdef, writeprocdef: tprocdef);
  1303. var
  1304. i: integer;
  1305. orig, hparavs: tparavarsym;
  1306. begin
  1307. for i := 0 to parast.SymList.Count - 1 do
  1308. begin
  1309. orig:=tparavarsym(parast.SymList[i]);
  1310. if assigned(readprocdef) then
  1311. begin
  1312. hparavs:=cparavarsym.create(orig.RealName,orig.paranr,orig.varspez,orig.vardef,[]);
  1313. readprocdef.parast.insert(hparavs);
  1314. end;
  1315. if assigned(writeprocdef) then
  1316. begin
  1317. hparavs:=cparavarsym.create(orig.RealName,orig.paranr,orig.varspez,orig.vardef,[]);
  1318. writeprocdef.parast.insert(hparavs);
  1319. end;
  1320. end;
  1321. end;
  1322. procedure tpropertysym.add_index_parameter(var paranr: word; readprocdef, writeprocdef: tprocdef);
  1323. var
  1324. hparavs: tparavarsym;
  1325. begin
  1326. inc(paranr);
  1327. if assigned(readprocdef) then
  1328. begin
  1329. hparavs:=cparavarsym.create('$index',10*paranr,vs_value,indexdef,[]);
  1330. readprocdef.parast.insert(hparavs);
  1331. end;
  1332. if assigned(writeprocdef) then
  1333. begin
  1334. hparavs:=cparavarsym.create('$index',10*paranr,vs_value,indexdef,[]);
  1335. writeprocdef.parast.insert(hparavs);
  1336. end;
  1337. end;
  1338. procedure tpropertysym.add_getter_or_setter_for_sym(getset: tpropaccesslisttypes; sym: tsym; fielddef: tdef; accessordef: tprocdef);
  1339. var
  1340. cpo: tcompare_paras_options;
  1341. begin
  1342. case sym.typ of
  1343. procsym :
  1344. begin
  1345. { search procdefs matching accessordef }
  1346. { we ignore hidden stuff here because the property access symbol might have
  1347. non default calling conventions which might change the hidden stuff;
  1348. see tw3216.pp (FK) }
  1349. cpo:=[cpo_allowdefaults,cpo_ignorehidden];
  1350. { allow var-parameters for setters in case of VARPROPSETTER+ }
  1351. if (getset=palt_write) and
  1352. (cs_varpropsetter in current_settings.localswitches) then
  1353. include(cpo,cpo_ignorevarspez);
  1354. propaccesslist[getset].procdef:=tprocsym(sym).find_procdef_bypara(accessordef.paras,accessordef.returndef,cpo);
  1355. if not assigned(propaccesslist[getset].procdef) or
  1356. { because of cpo_ignorehidden we need to compare if it is a static class method and we have a class property }
  1357. ((sp_static in symoptions)<>tprocdef(propaccesslist[getset].procdef).no_self_node) then
  1358. Message(parser_e_ill_property_access_sym)
  1359. else
  1360. finalize_getter_or_setter_for_sym(getset,sym,fielddef,accessordef);
  1361. end;
  1362. fieldvarsym :
  1363. begin
  1364. if not assigned(fielddef) then
  1365. internalerror(200310071);
  1366. if compare_defs(fielddef,propdef,nothingn)>=te_equal then
  1367. begin
  1368. { property parameters are allowed if this is
  1369. an indexed property, because the index is then
  1370. the parameter.
  1371. Note: In the help of Kylix it is written
  1372. that it isn't allowed, but the compiler accepts it (PFV) }
  1373. if (ppo_hasparameters in propoptions) or
  1374. ((sp_static in symoptions) <> (sp_static in sym.symoptions)) then
  1375. Message(parser_e_ill_property_access_sym)
  1376. else
  1377. finalize_getter_or_setter_for_sym(getset,sym,fielddef,accessordef);
  1378. end
  1379. else
  1380. IncompatibleTypes(fielddef,propdef);
  1381. end;
  1382. else
  1383. Message(parser_e_ill_property_access_sym);
  1384. end;
  1385. end;
  1386. procedure tpropertysym.register_override(overriddenprop: tpropertysym);
  1387. begin
  1388. overriddenpropsym:=tpropertysym(overriddenprop);
  1389. include(propoptions,ppo_overrides);
  1390. end;
  1391. procedure tpropertysym.inherit_accessor(getset: tpropaccesslisttypes);
  1392. begin
  1393. { nothing to do by default }
  1394. end;
  1395. procedure tpropertysym.makeduplicate(p: tpropertysym; readprocdef, writeprocdef: tprocdef; out paranr: word);
  1396. begin
  1397. { inherit all type related entries }
  1398. p.indexdef:=indexdef;
  1399. p.propdef:=propdef;
  1400. p.index:=index;
  1401. p.default:=default;
  1402. p.propoptions:=propoptions;
  1403. paranr:=0;
  1404. if ppo_hasparameters in propoptions then
  1405. begin
  1406. p.parast:=parast.getcopy;
  1407. p.add_accessor_parameters(readprocdef,writeprocdef);
  1408. paranr:=p.parast.SymList.Count;
  1409. end;
  1410. if ppo_indexed in p.propoptions then
  1411. p.add_index_parameter(paranr,readprocdef,writeprocdef);
  1412. end;
  1413. function tpropertysym.getsize : asizeint;
  1414. begin
  1415. getsize:=0;
  1416. end;
  1417. procedure tpropertysym.ppuwrite(ppufile:tcompilerppufile);
  1418. var
  1419. pap : tpropaccesslisttypes;
  1420. begin
  1421. inherited ppuwrite(ppufile);
  1422. ppufile.putsmallset(propoptions);
  1423. if ppo_overrides in propoptions then
  1424. ppufile.putderef(overriddenpropsymderef);
  1425. ppufile.putderef(propdefderef);
  1426. ppufile.putlongint(index);
  1427. ppufile.putlongint(default);
  1428. ppufile.putderef(indexdefderef);
  1429. for pap:=low(tpropaccesslisttypes) to high(tpropaccesslisttypes) do
  1430. ppufile.putpropaccesslist(propaccesslist[pap]);
  1431. writeentry(ppufile,ibpropertysym);
  1432. if [ppo_hasparameters,ppo_overrides]*propoptions=[ppo_hasparameters] then
  1433. tparasymtable(parast).ppuwrite(ppufile);
  1434. end;
  1435. {****************************************************************************
  1436. TABSTRACTVARSYM
  1437. ****************************************************************************}
  1438. constructor tabstractvarsym.create(st:tsymtyp;const n : string;vsp:tvarspez;def:tdef;vopts:tvaroptions;doregister:boolean);
  1439. begin
  1440. inherited create(st,n,doregister);
  1441. vardef:=def;
  1442. vardefderef.reset;
  1443. varspez:=vsp;
  1444. varstate:=vs_declared;
  1445. varoptions:=vopts;
  1446. end;
  1447. constructor tabstractvarsym.ppuload(st:tsymtyp;ppufile:tcompilerppufile);
  1448. begin
  1449. inherited ppuload(st,ppufile);
  1450. varstate:=vs_readwritten;
  1451. varspez:=tvarspez(ppufile.getbyte);
  1452. varregable:=tvarregable(ppufile.getbyte);
  1453. addr_taken:=ppufile.getboolean;
  1454. different_scope:=ppufile.getboolean;
  1455. ppufile.getderef(vardefderef);
  1456. ppufile.getsmallset(varoptions);
  1457. end;
  1458. procedure tabstractvarsym.buildderef;
  1459. begin
  1460. inherited;
  1461. vardefderef.build(vardef);
  1462. end;
  1463. procedure tabstractvarsym.deref;
  1464. begin
  1465. { assigning vardef also updates varregable. We just loaded this }
  1466. { value from a ppu, so it must not be changed (e.g. tw7817a.pp/ }
  1467. { tw7817b.pp: the address is taken of a local variable in an }
  1468. { inlined procedure -> must remain non-regable when inlining) }
  1469. setvardef(tdef(vardefderef.resolve));
  1470. end;
  1471. procedure tabstractvarsym.ppuwrite(ppufile:tcompilerppufile);
  1472. var
  1473. oldintfcrc : boolean;
  1474. begin
  1475. inherited ppuwrite(ppufile);
  1476. ppufile.putbyte(byte(varspez));
  1477. oldintfcrc:=ppufile.do_crc;
  1478. ppufile.do_crc:=false;
  1479. ppufile.putbyte(byte(varregable));
  1480. ppufile.putboolean(addr_taken);
  1481. ppufile.putboolean(different_scope);
  1482. ppufile.do_crc:=oldintfcrc;
  1483. ppufile.putderef(vardefderef);
  1484. ppufile.putsmallset(varoptions);
  1485. end;
  1486. function tabstractvarsym.getsize : asizeint;
  1487. begin
  1488. if assigned(vardef) and
  1489. ((vardef.typ<>arraydef) or
  1490. is_dynamic_array(vardef) or
  1491. (tarraydef(vardef).highrange>=tarraydef(vardef).lowrange)) then
  1492. result:=vardef.size
  1493. else
  1494. result:=0;
  1495. end;
  1496. function tabstractvarsym.getpackedbitsize : longint;
  1497. begin
  1498. { bitpacking is only done for ordinals }
  1499. if not is_ordinal(vardef) then
  1500. internalerror(2006082010);
  1501. result:=vardef.packedbitsize;
  1502. end;
  1503. function tabstractvarsym.is_regvar(refpara: boolean):boolean;
  1504. begin
  1505. { Register variables are not allowed in the following cases:
  1506. - regvars are disabled
  1507. - exceptions are used (after an exception is raised the contents of the
  1508. registers is not valid anymore)
  1509. - it has a local copy
  1510. - the value needs to be in memory (i.e. reference counted) }
  1511. result:=(cs_opt_regvar in current_settings.optimizerswitches) and
  1512. not(pi_has_assembler_block in current_procinfo.flags) and
  1513. not(pi_uses_exceptions in current_procinfo.flags) and
  1514. not(pi_has_interproclabel in current_procinfo.flags) and
  1515. ((refpara and
  1516. (varregable <> vr_none)) or
  1517. (not refpara and
  1518. not(varregable in [vr_none,vr_addr])))
  1519. {$if not defined(powerpc) and not defined(powerpc64)}
  1520. and ((vardef.typ <> recorddef) or
  1521. (varregable = vr_addr) or
  1522. not(varstate in [vs_written,vs_readwritten]));
  1523. {$endif}
  1524. end;
  1525. procedure tabstractvarsym.setvardef_and_regable(def:tdef);
  1526. begin
  1527. setvardef(def);
  1528. setregable;
  1529. end;
  1530. procedure tabstractvarsym.setregable;
  1531. begin
  1532. if vo_volatile in varoptions then
  1533. exit;
  1534. { can we load the value into a register ? }
  1535. if not assigned(owner) or
  1536. (owner.symtabletype in [localsymtable, parasymtable]) or
  1537. (
  1538. (owner.symtabletype=staticsymtable) and
  1539. not(cs_create_pic in current_settings.moduleswitches)
  1540. ) then
  1541. begin
  1542. if (tstoreddef(vardef).is_intregable and
  1543. { we could keep all aint*2 records in registers, but this causes
  1544. too much spilling for CPUs with 8-16 registers so keep only
  1545. parameters and function results of this type in register because they are normally
  1546. passed by register anyways
  1547. This can be changed, as soon as we have full ssa (FK) }
  1548. ((typ=paravarsym) or
  1549. (vo_is_funcret in varoptions) or
  1550. (tstoreddef(vardef).typ<>recorddef) or
  1551. (tstoreddef(vardef).size<=sizeof(aint)))) or
  1552. { const parameters can be put into registers if the def fits into a register }
  1553. (tstoreddef(vardef).is_const_intregable and
  1554. (typ=paravarsym) and
  1555. (varspez=vs_const)) then
  1556. varregable:=vr_intreg
  1557. else if tstoreddef(vardef).is_fpuregable then
  1558. begin
  1559. if use_vectorfpu(vardef) then
  1560. varregable:=vr_mmreg
  1561. else
  1562. varregable:=vr_fpureg;
  1563. end;
  1564. end;
  1565. end;
  1566. procedure tabstractvarsym.setvardef(const def: tdef);
  1567. begin
  1568. _vardef := def;
  1569. end;
  1570. {****************************************************************************
  1571. TFIELDVARSYM
  1572. ****************************************************************************}
  1573. constructor tfieldvarsym.create(const n : string;vsp:tvarspez;def:tdef;vopts:tvaroptions;doregister:boolean);
  1574. begin
  1575. inherited create(fieldvarsym,n,vsp,def,vopts,doregister);
  1576. fieldoffset:=-1;
  1577. end;
  1578. constructor tfieldvarsym.ppuload(ppufile:tcompilerppufile);
  1579. begin
  1580. inherited ppuload(fieldvarsym,ppufile);
  1581. fieldoffset:=ppufile.getasizeint;
  1582. if (vo_has_mangledname in varoptions) then
  1583. externalname:=ppufile.getpshortstring
  1584. else
  1585. externalname:=nil;
  1586. ppuload_platform(ppufile);
  1587. end;
  1588. procedure tfieldvarsym.ppuwrite(ppufile:tcompilerppufile);
  1589. begin
  1590. inherited ppuwrite(ppufile);
  1591. ppufile.putasizeint(fieldoffset);
  1592. if (vo_has_mangledname in varoptions) then
  1593. ppufile.putstring(externalname^);
  1594. writeentry(ppufile,ibfieldvarsym);
  1595. end;
  1596. procedure tfieldvarsym.set_externalname(const s: string);
  1597. begin
  1598. internalerror(2014033001);
  1599. end;
  1600. function tfieldvarsym.mangledname:TSymStr;
  1601. var
  1602. srsym : tsym;
  1603. srsymtable : tsymtable;
  1604. begin
  1605. if sp_static in symoptions then
  1606. begin
  1607. if searchsym(lower(owner.name^)+'_'+name,srsym,srsymtable) then
  1608. result:=srsym.mangledname
  1609. { when generating the debug info for the module in which the }
  1610. { symbol is defined, the localsymtable of that module is }
  1611. { already popped from the symtablestack }
  1612. else if searchsym_in_module(current_module,lower(owner.name^)+'_'+name,srsym,srsymtable) then
  1613. result:=srsym.mangledname
  1614. else
  1615. internalerror(2007012501);
  1616. end
  1617. else if is_objcclass(tdef(owner.defowner)) then
  1618. begin
  1619. {$ifdef symansistr}
  1620. if cachedmangledname<>'' then
  1621. result:=cachedmangledname
  1622. {$else symansistr}
  1623. if assigned(cachedmangledname) then
  1624. result:=cachedmangledname^
  1625. {$endif symansistr}
  1626. else
  1627. begin
  1628. result:=target_info.cprefix+'OBJC_IVAR_$_'+tobjectdef(owner.defowner).objextname^+'.'+RealName;
  1629. {$ifdef symansistr}
  1630. cachedmangledname:=result;
  1631. {$else symansistr}
  1632. cachedmangledname:=stringdup(result);
  1633. {$endif symansistr}
  1634. end;
  1635. end
  1636. else
  1637. result:=inherited mangledname;
  1638. end;
  1639. destructor tfieldvarsym.destroy;
  1640. begin
  1641. {$ifndef symansistr}
  1642. stringdispose(cachedmangledname);
  1643. {$endif symansistr}
  1644. stringdispose(externalname);
  1645. inherited destroy;
  1646. end;
  1647. {****************************************************************************
  1648. TABSTRACTNORMALVARSYM
  1649. ****************************************************************************}
  1650. constructor tabstractnormalvarsym.create(st:tsymtyp;const n : string;vsp:tvarspez;def:tdef;vopts:tvaroptions;doregister:boolean);
  1651. begin
  1652. inherited create(st,n,vsp,def,vopts,doregister);
  1653. fillchar(localloc,sizeof(localloc),0);
  1654. fillchar(initialloc,sizeof(initialloc),0);
  1655. defaultconstsym:=nil;
  1656. defaultconstsymderef.reset;
  1657. end;
  1658. constructor tabstractnormalvarsym.ppuload(st:tsymtyp;ppufile:tcompilerppufile);
  1659. begin
  1660. inherited ppuload(st,ppufile);
  1661. fillchar(localloc,sizeof(localloc),0);
  1662. fillchar(initialloc,sizeof(initialloc),0);
  1663. ppufile.getderef(defaultconstsymderef);
  1664. end;
  1665. function tabstractnormalvarsym.globalasmsym: boolean;
  1666. begin
  1667. result:=
  1668. (owner.symtabletype=globalsymtable) or
  1669. (create_smartlink and
  1670. not(tf_smartlink_sections in target_info.flags)) or
  1671. current_module.islibrary or
  1672. (assigned(current_procinfo) and
  1673. ((po_inline in current_procinfo.procdef.procoptions) or
  1674. { globalasmsym is called normally before the body of a subroutine is parsed
  1675. so we cannot know if it will be auto inlined, so make all symbols of it
  1676. global if asked }
  1677. (not(po_noinline in current_procinfo.procdef.procoptions) and
  1678. (cs_opt_autoinline in current_settings.optimizerswitches)))
  1679. ) or
  1680. (vo_is_public in varoptions);
  1681. end;
  1682. procedure tabstractnormalvarsym.buildderef;
  1683. begin
  1684. inherited buildderef;
  1685. defaultconstsymderef.build(defaultconstsym);
  1686. end;
  1687. procedure tabstractnormalvarsym.deref;
  1688. begin
  1689. inherited deref;
  1690. defaultconstsym:=tsym(defaultconstsymderef.resolve);
  1691. end;
  1692. procedure tabstractnormalvarsym.ppuwrite(ppufile:tcompilerppufile);
  1693. begin
  1694. inherited ppuwrite(ppufile);
  1695. ppufile.putderef(defaultconstsymderef);
  1696. end;
  1697. {****************************************************************************
  1698. Tstaticvarsym
  1699. ****************************************************************************}
  1700. constructor tstaticvarsym.create(const n : string;vsp:tvarspez;def:tdef;vopts:tvaroptions;doregister:boolean);
  1701. begin
  1702. inherited create(staticvarsym,n,vsp,def,vopts,doregister);
  1703. fieldvarsymderef.reset;
  1704. {$ifdef symansistr}
  1705. _mangledname:='';
  1706. {$else symansistr}
  1707. _mangledname:=nil;
  1708. {$endif symansistr}
  1709. end;
  1710. constructor tstaticvarsym.create_dll(const n : string;vsp:tvarspez;def:tdef);
  1711. begin
  1712. tstaticvarsym(self).create(n,vsp,def,[vo_is_dll_var],true);
  1713. end;
  1714. constructor tstaticvarsym.create_C(const n: string; const mangled : TSymStr;vsp:tvarspez;def:tdef);
  1715. begin
  1716. tstaticvarsym(self).create(n,vsp,def,[],true);
  1717. set_mangledname(mangled);
  1718. end;
  1719. constructor tstaticvarsym.create_from_fieldvar(const n: string;fieldvar:tfieldvarsym);
  1720. begin
  1721. create(internal_static_field_name(n),vs_value,fieldvar.vardef,[],true);
  1722. fieldvarsym:=fieldvar;
  1723. end;
  1724. constructor tstaticvarsym.ppuload(ppufile:tcompilerppufile);
  1725. begin
  1726. inherited ppuload(staticvarsym,ppufile);
  1727. {$ifdef symansistr}
  1728. if vo_has_mangledname in varoptions then
  1729. _mangledname:=ppufile.getansistring
  1730. else
  1731. _mangledname:='';
  1732. {$else symansistr}
  1733. if vo_has_mangledname in varoptions then
  1734. _mangledname:=ppufile.getpshortstring
  1735. else
  1736. _mangledname:=nil;
  1737. {$endif symansistr}
  1738. if vo_has_section in varoptions then
  1739. section:=ppufile.getansistring;
  1740. ppufile.getderef(fieldvarsymderef);
  1741. ppuload_platform(ppufile);
  1742. end;
  1743. destructor tstaticvarsym.destroy;
  1744. begin
  1745. {$ifndef symansistr}
  1746. if assigned(_mangledname) then
  1747. begin
  1748. {$ifdef MEMDEBUG}
  1749. memmanglednames.start;
  1750. {$endif MEMDEBUG}
  1751. stringdispose(_mangledname);
  1752. {$ifdef MEMDEBUG}
  1753. memmanglednames.stop;
  1754. {$endif MEMDEBUG}
  1755. end;
  1756. stringdispose(_mangledbasename);
  1757. {$endif}
  1758. inherited destroy;
  1759. end;
  1760. procedure tstaticvarsym.ppuwrite(ppufile:tcompilerppufile);
  1761. begin
  1762. inherited ppuwrite(ppufile);
  1763. { write mangledname rather than _mangledname in case the mangledname
  1764. has not been calculated yet (can happen in case only the
  1765. mangledbasename has been set) }
  1766. if vo_has_mangledname in varoptions then
  1767. {$ifdef symansistr}
  1768. ppufile.putansistring(mangledname);
  1769. {$else symansistr}
  1770. ppufile.putstring(mangledname);
  1771. {$endif symansistr}
  1772. if vo_has_section in varoptions then
  1773. ppufile.putansistring(section);
  1774. ppufile.putderef(fieldvarsymderef);
  1775. writeentry(ppufile,ibstaticvarsym);
  1776. end;
  1777. procedure tstaticvarsym.buildderef;
  1778. begin
  1779. inherited buildderef;
  1780. fieldvarsymderef.build(fieldvarsym);
  1781. end;
  1782. procedure tstaticvarsym.deref;
  1783. begin
  1784. inherited deref;
  1785. fieldvarsym:=tfieldvarsym(fieldvarsymderef.resolve);
  1786. end;
  1787. function tstaticvarsym.mangledname:TSymStr;
  1788. var
  1789. usename,
  1790. prefix : TSymStr;
  1791. begin
  1792. {$ifdef symansistr}
  1793. if _mangledname='' then
  1794. {$else symansistr}
  1795. if not assigned(_mangledname) then
  1796. {$endif symansistr}
  1797. begin
  1798. if (vo_is_typed_const in varoptions) then
  1799. prefix:='TC'
  1800. else
  1801. prefix:='U';
  1802. {$ifdef symansistr}
  1803. if _mangledbasename='' then
  1804. usename:=name
  1805. else
  1806. usename:=_mangledbasename;
  1807. _mangledname:=make_mangledname(prefix,owner,usename);
  1808. {$else symansistr}
  1809. if not assigned(_mangledbasename) then
  1810. usename:=name
  1811. else
  1812. usename:=_mangledbasename^;
  1813. _mangledname:=stringdup(make_mangledname(prefix,owner,usename));
  1814. {$endif symansistr}
  1815. end;
  1816. {$ifdef symansistr}
  1817. result:=_mangledname;
  1818. {$else symansistr}
  1819. result:=_mangledname^;
  1820. {$endif symansistr}
  1821. end;
  1822. procedure tstaticvarsym.set_mangledbasename(const s: TSymStr);
  1823. begin
  1824. {$ifdef symansistr}
  1825. _mangledbasename:=s;
  1826. _mangledname:='';
  1827. {$else symansistr}
  1828. stringdispose(_mangledname);
  1829. stringdispose(_mangledbasename);
  1830. _mangledbasename:=stringdup(s);
  1831. {$endif symansistr}
  1832. include(varoptions,vo_has_mangledname);
  1833. end;
  1834. function tstaticvarsym.mangledbasename: TSymStr;
  1835. begin
  1836. {$ifdef symansistr}
  1837. result:=_mangledbasename;
  1838. {$else symansistr}
  1839. if assigned(_mangledbasename) then
  1840. result:=_mangledbasename^
  1841. else
  1842. result:='';
  1843. {$endif symansistr}
  1844. end;
  1845. procedure tstaticvarsym.set_mangledname(const s:TSymStr);
  1846. begin
  1847. {$ifdef symansistr}
  1848. _mangledname:=s;
  1849. {$else symansistr}
  1850. stringdispose(_mangledname);
  1851. _mangledname:=stringdup(s);
  1852. {$endif symansistr}
  1853. include(varoptions,vo_has_mangledname);
  1854. end;
  1855. procedure tstaticvarsym.set_raw_mangledname(const s: TSymStr);
  1856. begin
  1857. {$ifndef symansistr}
  1858. stringdispose(_mangledname);
  1859. _mangledname:=stringdup(s);
  1860. {$else}
  1861. _mangledname:=s;
  1862. {$endif}
  1863. include(varoptions,vo_has_mangledname);
  1864. end;
  1865. {****************************************************************************
  1866. TLOCALVARSYM
  1867. ****************************************************************************}
  1868. constructor tlocalvarsym.create(const n : string;vsp:tvarspez;def:tdef;vopts:tvaroptions;doregister:boolean);
  1869. begin
  1870. inherited create(localvarsym,n,vsp,def,vopts,doregister);
  1871. end;
  1872. constructor tlocalvarsym.ppuload(ppufile:tcompilerppufile);
  1873. begin
  1874. inherited ppuload(localvarsym,ppufile);
  1875. ppuload_platform(ppufile);
  1876. end;
  1877. procedure tlocalvarsym.ppuwrite(ppufile:tcompilerppufile);
  1878. begin
  1879. inherited ppuwrite(ppufile);
  1880. writeentry(ppufile,iblocalvarsym);
  1881. end;
  1882. {****************************************************************************
  1883. TPARAVARSYM
  1884. ****************************************************************************}
  1885. constructor tparavarsym.create(const n : string;nr:word;vsp:tvarspez;def:tdef;vopts:tvaroptions);
  1886. begin
  1887. inherited create(paravarsym,n,vsp,def,vopts,true);
  1888. if (vsp in [vs_var,vs_value,vs_const,vs_constref]) and
  1889. not(vo_is_funcret in vopts) then
  1890. varstate := vs_initialised;
  1891. paranr:=nr;
  1892. paraloc[calleeside].init;
  1893. paraloc[callerside].init;
  1894. end;
  1895. destructor tparavarsym.destroy;
  1896. begin
  1897. paraloc[calleeside].done;
  1898. paraloc[callerside].done;
  1899. inherited destroy;
  1900. end;
  1901. constructor tparavarsym.ppuload(ppufile:tcompilerppufile);
  1902. begin
  1903. inherited ppuload(paravarsym,ppufile);
  1904. paranr:=ppufile.getword;
  1905. univpara:=ppufile.getboolean;
  1906. { The var state of parameter symbols is fixed after writing them so
  1907. we write them to the unit file.
  1908. This enables constant folding for inline procedures loaded from units
  1909. }
  1910. varstate:=tvarstate(ppufile.getbyte);
  1911. { read usage info }
  1912. refs:=ppufile.getbyte;
  1913. paraloc[calleeside].init;
  1914. paraloc[callerside].init;
  1915. if vo_has_explicit_paraloc in varoptions then
  1916. paraloc[callerside].ppuload(ppufile);
  1917. ppuload_platform(ppufile);
  1918. end;
  1919. procedure tparavarsym.ppuwrite(ppufile:tcompilerppufile);
  1920. var
  1921. oldintfcrc : boolean;
  1922. begin
  1923. inherited ppuwrite(ppufile);
  1924. ppufile.putword(paranr);
  1925. ppufile.putboolean(univpara);
  1926. { The var state of parameter symbols is fixed after writing them so
  1927. we write them to the unit file.
  1928. This enables constant folding for inline procedures loaded from units
  1929. }
  1930. oldintfcrc:=ppufile.do_crc;
  1931. ppufile.do_crc:=false;
  1932. ppufile.putbyte(ord(varstate));
  1933. { write also info about the usage of parameters,
  1934. the absolute usage does not matter }
  1935. ppufile.putbyte(min(1,refs));
  1936. ppufile.do_crc:=oldintfcrc;
  1937. if vo_has_explicit_paraloc in varoptions then
  1938. begin
  1939. paraloc[callerside].check_simple_location;
  1940. paraloc[callerside].ppuwrite(ppufile);
  1941. end;
  1942. writeentry(ppufile,ibparavarsym);
  1943. end;
  1944. function tparavarsym.needs_finalization:boolean;
  1945. begin
  1946. result:=(varspez=vs_value) and
  1947. (is_managed_type(vardef) or
  1948. (
  1949. (not (tabstractprocdef(owner.defowner).proccalloption in cdecl_pocalls)) and
  1950. (not paramanager.use_stackalloc) and
  1951. (is_open_array(vardef) or is_array_of_const(vardef))
  1952. )
  1953. );
  1954. end;
  1955. {****************************************************************************
  1956. TABSOLUTEVARSYM
  1957. ****************************************************************************}
  1958. constructor tabsolutevarsym.create(const n : string;def:tdef);
  1959. begin
  1960. inherited create(absolutevarsym,n,vs_value,def,[],true);
  1961. ref:=nil;
  1962. end;
  1963. constructor tabsolutevarsym.create_ref(const n : string;def:tdef;_ref:tpropaccesslist);
  1964. begin
  1965. inherited create(absolutevarsym,n,vs_value,def,[],true);
  1966. ref:=_ref;
  1967. end;
  1968. destructor tabsolutevarsym.destroy;
  1969. begin
  1970. if assigned(ref) then
  1971. ref.free;
  1972. inherited destroy;
  1973. end;
  1974. constructor tabsolutevarsym.ppuload(ppufile:tcompilerppufile);
  1975. begin
  1976. inherited ppuload(absolutevarsym,ppufile);
  1977. ref:=nil;
  1978. asmname:=nil;
  1979. abstyp:=absolutetyp(ppufile.getbyte);
  1980. case abstyp of
  1981. tovar :
  1982. ref:=ppufile.getpropaccesslist;
  1983. toasm :
  1984. asmname:=ppufile.getpshortstring;
  1985. toaddr :
  1986. addroffset:=ppufile.getpuint;
  1987. end;
  1988. ppuload_platform(ppufile);
  1989. end;
  1990. procedure tabsolutevarsym.ppuwrite(ppufile:tcompilerppufile);
  1991. begin
  1992. inherited ppuwrite(ppufile);
  1993. ppufile.putbyte(byte(abstyp));
  1994. case abstyp of
  1995. tovar :
  1996. ppufile.putpropaccesslist(ref);
  1997. toasm :
  1998. ppufile.putstring(asmname^);
  1999. toaddr :
  2000. ppufile.putpuint(addroffset);
  2001. end;
  2002. writeentry(ppufile,ibabsolutevarsym);
  2003. end;
  2004. procedure tabsolutevarsym.buildderef;
  2005. begin
  2006. inherited buildderef;
  2007. if (abstyp=tovar) then
  2008. ref.buildderef;
  2009. end;
  2010. procedure tabsolutevarsym.deref;
  2011. begin
  2012. inherited deref;
  2013. { own absolute deref }
  2014. if (abstyp=tovar) then
  2015. ref.resolve;
  2016. end;
  2017. function tabsolutevarsym.mangledname : TSymStr;
  2018. begin
  2019. case abstyp of
  2020. toasm :
  2021. mangledname:=asmname^;
  2022. toaddr :
  2023. mangledname:='$'+tostr(addroffset);
  2024. else
  2025. internalerror(200411062);
  2026. end;
  2027. end;
  2028. {****************************************************************************
  2029. TCONSTSYM
  2030. ****************************************************************************}
  2031. constructor tconstsym.create_ord(const n : string;t : tconsttyp;v : tconstexprint;def:tdef);
  2032. begin
  2033. inherited create(constsym,n,true);
  2034. fillchar(value, sizeof(value), #0);
  2035. consttyp:=t;
  2036. value.valueord:=v;
  2037. constdef:=def;
  2038. constdefderef.reset;
  2039. end;
  2040. constructor tconstsym.create_ordptr(const n : string;t : tconsttyp;v : tconstptruint;def:tdef);
  2041. begin
  2042. inherited create(constsym,n,true);
  2043. fillchar(value, sizeof(value), #0);
  2044. consttyp:=t;
  2045. value.valueordptr:=v;
  2046. constdef:=def;
  2047. constdefderef.reset;
  2048. end;
  2049. constructor tconstsym.create_ptr(const n : string;t : tconsttyp;v : pointer;def:tdef);
  2050. begin
  2051. inherited create(constsym,n,true);
  2052. fillchar(value, sizeof(value), #0);
  2053. consttyp:=t;
  2054. value.valueptr:=v;
  2055. constdef:=def;
  2056. constdefderef.reset;
  2057. end;
  2058. constructor tconstsym.create_string(const n : string;t : tconsttyp;str:pchar;l:longint;def: tdef);
  2059. begin
  2060. inherited create(constsym,n,true);
  2061. fillchar(value, sizeof(value), #0);
  2062. consttyp:=t;
  2063. value.valueptr:=str;
  2064. if assigned(def) then
  2065. constdef:=def
  2066. else
  2067. constdef:=carraydef.getreusable(cansichartype,l);
  2068. constdefderef.reset;
  2069. value.len:=l;
  2070. end;
  2071. constructor tconstsym.create_wstring(const n : string;t : tconsttyp;pw:pcompilerwidestring);
  2072. begin
  2073. inherited create(constsym,n,true);
  2074. fillchar(value, sizeof(value), #0);
  2075. consttyp:=t;
  2076. pcompilerwidestring(value.valueptr):=pw;
  2077. constdef:=carraydef.getreusable(cwidechartype,getlengthwidestring(pw));
  2078. constdefderef.reset;
  2079. value.len:=getlengthwidestring(pw);
  2080. end;
  2081. constructor tconstsym.ppuload(ppufile:tcompilerppufile);
  2082. var
  2083. pd : pbestreal;
  2084. ps : pnormalset;
  2085. pc : pchar;
  2086. pw : pcompilerwidestring;
  2087. i : longint;
  2088. begin
  2089. inherited ppuload(constsym,ppufile);
  2090. constdef:=nil;
  2091. consttyp:=tconsttyp(ppufile.getbyte);
  2092. fillchar(value, sizeof(value), #0);
  2093. case consttyp of
  2094. constord :
  2095. begin
  2096. ppufile.getderef(constdefderef);
  2097. value.valueord:=ppufile.getexprint;
  2098. end;
  2099. constpointer :
  2100. begin
  2101. ppufile.getderef(constdefderef);
  2102. value.valueordptr:=ppufile.getptruint;
  2103. end;
  2104. constwstring :
  2105. begin
  2106. initwidestring(pw);
  2107. setlengthwidestring(pw,ppufile.getlongint);
  2108. { don't use getdata, because the compilerwidechars may have to
  2109. be byteswapped
  2110. }
  2111. {$if sizeof(tcompilerwidechar) = 2}
  2112. for i:=0 to pw^.len-1 do
  2113. pw^.data[i]:=ppufile.getword;
  2114. {$elseif sizeof(tcompilerwidechar) = 4}
  2115. for i:=0 to pw^.len-1 do
  2116. pw^.data[i]:=cardinal(ppufile.getlongint);
  2117. {$else}
  2118. {$error Unsupported tcompilerwidechar size}
  2119. {$endif}
  2120. pcompilerwidestring(value.valueptr):=pw;
  2121. end;
  2122. conststring,
  2123. constresourcestring :
  2124. begin
  2125. ppufile.getderef(constdefderef);
  2126. value.len:=ppufile.getlongint;
  2127. getmem(pc,value.len+1);
  2128. ppufile.getdata(pc^,value.len);
  2129. pc[value.len]:=#0;
  2130. value.valueptr:=pc;
  2131. end;
  2132. constreal :
  2133. begin
  2134. ppufile.getderef(constdefderef);
  2135. new(pd);
  2136. pd^:=ppufile.getreal;
  2137. value.valueptr:=pd;
  2138. end;
  2139. constset :
  2140. begin
  2141. ppufile.getderef(constdefderef);
  2142. new(ps);
  2143. ppufile.getnormalset(ps^);
  2144. value.valueptr:=ps;
  2145. end;
  2146. constguid :
  2147. begin
  2148. ppufile.getderef(constdefderef);
  2149. new(pguid(value.valueptr));
  2150. ppufile.getdata(value.valueptr^,sizeof(tguid));
  2151. end;
  2152. constnil :
  2153. ppufile.getderef(constdefderef);
  2154. else
  2155. Message1(unit_f_ppu_invalid_entry,tostr(ord(consttyp)));
  2156. end;
  2157. ppuload_platform(ppufile);
  2158. end;
  2159. destructor tconstsym.destroy;
  2160. begin
  2161. case consttyp of
  2162. constnone:
  2163. internalerror(2019050703);
  2164. constord,
  2165. constpointer,
  2166. constnil:
  2167. ;
  2168. conststring,
  2169. constresourcestring :
  2170. freemem(pchar(value.valueptr),value.len+1);
  2171. constwstring :
  2172. donewidestring(pcompilerwidestring(value.valueptr));
  2173. constreal :
  2174. dispose(pbestreal(value.valueptr));
  2175. constset :
  2176. dispose(pnormalset(value.valueptr));
  2177. constguid :
  2178. dispose(pguid(value.valueptr));
  2179. end;
  2180. inherited destroy;
  2181. end;
  2182. procedure tconstsym.buildderef;
  2183. begin
  2184. inherited;
  2185. case consttyp of
  2186. constnil,constord,constreal,constpointer,constset,conststring,constresourcestring,constguid:
  2187. constdefderef.build(constdef);
  2188. constwstring:
  2189. ;
  2190. else
  2191. internalerror(2015120802);
  2192. end;
  2193. end;
  2194. procedure tconstsym.deref;
  2195. begin
  2196. case consttyp of
  2197. constnil,constord,constreal,constpointer,constset,conststring,constresourcestring,constguid:
  2198. constdef:=tdef(constdefderef.resolve);
  2199. constwstring:
  2200. constdef:=carraydef.getreusable(cwidechartype,getlengthwidestring(pcompilerwidestring(value.valueptr)));
  2201. else
  2202. internalerror(2015120801);
  2203. end
  2204. end;
  2205. procedure tconstsym.ppuwrite(ppufile:tcompilerppufile);
  2206. begin
  2207. inherited ppuwrite(ppufile);
  2208. ppufile.putbyte(byte(consttyp));
  2209. case consttyp of
  2210. constnil :
  2211. ppufile.putderef(constdefderef);
  2212. constord :
  2213. begin
  2214. ppufile.putderef(constdefderef);
  2215. ppufile.putexprint(value.valueord);
  2216. end;
  2217. constpointer :
  2218. begin
  2219. ppufile.putderef(constdefderef);
  2220. ppufile.putptruint(value.valueordptr);
  2221. end;
  2222. constwstring :
  2223. begin
  2224. { no need to store the def, we can reconstruct it }
  2225. ppufile.putlongint(getlengthwidestring(pcompilerwidestring(value.valueptr)));
  2226. ppufile.putdata(pcompilerwidestring(value.valueptr)^.data^,pcompilerwidestring(value.valueptr)^.len*sizeof(tcompilerwidechar));
  2227. end;
  2228. conststring,
  2229. constresourcestring :
  2230. begin
  2231. ppufile.putderef(constdefderef);
  2232. ppufile.putlongint(value.len);
  2233. ppufile.putdata(pchar(value.valueptr)^,value.len);
  2234. end;
  2235. constreal :
  2236. begin
  2237. ppufile.putderef(constdefderef);
  2238. ppufile.putreal(pbestreal(value.valueptr)^);
  2239. end;
  2240. constset :
  2241. begin
  2242. ppufile.putderef(constdefderef);
  2243. ppufile.putnormalset(value.valueptr^);
  2244. end;
  2245. constguid :
  2246. begin
  2247. ppufile.putderef(constdefderef);
  2248. ppufile.putdata(value.valueptr^,sizeof(tguid));
  2249. end;
  2250. else
  2251. internalerror(13);
  2252. end;
  2253. writeentry(ppufile,ibconstsym);
  2254. end;
  2255. {****************************************************************************
  2256. TENUMSYM
  2257. ****************************************************************************}
  2258. constructor tenumsym.create(const n : string;def : tenumdef;v : longint);
  2259. begin
  2260. inherited create(enumsym,n,true);
  2261. definition:=def;
  2262. definitionderef.reset;
  2263. value:=v;
  2264. end;
  2265. constructor tenumsym.ppuload(ppufile:tcompilerppufile);
  2266. begin
  2267. inherited ppuload(enumsym,ppufile);
  2268. ppufile.getderef(definitionderef);
  2269. value:=ppufile.getlongint;
  2270. ppuload_platform(ppufile);
  2271. end;
  2272. procedure tenumsym.buildderef;
  2273. begin
  2274. inherited;
  2275. definitionderef.build(definition);
  2276. end;
  2277. procedure tenumsym.deref;
  2278. begin
  2279. definition:=tenumdef(definitionderef.resolve);
  2280. end;
  2281. procedure tenumsym.ppuwrite(ppufile:tcompilerppufile);
  2282. begin
  2283. inherited ppuwrite(ppufile);
  2284. ppufile.putderef(definitionderef);
  2285. ppufile.putlongint(value);
  2286. writeentry(ppufile,ibenumsym);
  2287. end;
  2288. {****************************************************************************
  2289. TTYPESYM
  2290. ****************************************************************************}
  2291. constructor ttypesym.create(const n : string;def:tdef;doregister:boolean);
  2292. begin
  2293. inherited create(typesym,n,doregister);
  2294. typedef:=def;
  2295. typedefderef.reset;
  2296. { register the typesym for the definition }
  2297. if assigned(typedef) and
  2298. (typedef.typ<>errordef) and
  2299. not(assigned(typedef.typesym)) then
  2300. typedef.typesym:=self;
  2301. end;
  2302. destructor ttypesym.destroy;
  2303. begin
  2304. inherited destroy;
  2305. end;
  2306. constructor ttypesym.ppuload(ppufile:tcompilerppufile);
  2307. begin
  2308. inherited ppuload(typesym,ppufile);
  2309. ppufile.getderef(typedefderef);
  2310. fprettyname:=ppufile.getansistring;
  2311. ppuload_platform(ppufile);
  2312. end;
  2313. procedure ttypesym.buildderef;
  2314. begin
  2315. inherited;
  2316. typedefderef.build(typedef);
  2317. end;
  2318. procedure ttypesym.deref;
  2319. begin
  2320. typedef:=tdef(typedefderef.resolve);
  2321. end;
  2322. procedure ttypesym.ppuwrite(ppufile:tcompilerppufile);
  2323. begin
  2324. inherited ppuwrite(ppufile);
  2325. ppufile.putderef(typedefderef);
  2326. ppufile.putansistring(fprettyname);
  2327. writeentry(ppufile,ibtypesym);
  2328. end;
  2329. function ttypesym.prettyname : string;
  2330. begin
  2331. if fprettyname<>'' then
  2332. result:=fprettyname
  2333. else
  2334. result:=inherited prettyname;
  2335. end;
  2336. {****************************************************************************
  2337. TSYSSYM
  2338. ****************************************************************************}
  2339. var
  2340. syssym_list : TFPHashObjectList;
  2341. constructor tsyssym.create(const n : string;l : tinlinenumber);
  2342. var
  2343. s : shortstring;
  2344. begin
  2345. inherited create(syssym,n,true);
  2346. number:=l;
  2347. str(longint(l),s);
  2348. if assigned(syssym_list.find(s)) then
  2349. internalerror(2016060303);
  2350. syssym_list.add(s,self);
  2351. end;
  2352. constructor tsyssym.ppuload(ppufile:tcompilerppufile);
  2353. var
  2354. s : shortstring;
  2355. begin
  2356. inherited ppuload(syssym,ppufile);
  2357. number:=tinlinenumber(ppufile.getlongint);
  2358. ppuload_platform(ppufile);
  2359. str(longint(number),s);
  2360. if assigned(syssym_list.find(s)) then
  2361. internalerror(2016060304);
  2362. syssym_list.add(s,self);
  2363. end;
  2364. destructor tsyssym.destroy;
  2365. begin
  2366. inherited destroy;
  2367. end;
  2368. procedure tsyssym.ppuwrite(ppufile:tcompilerppufile);
  2369. begin
  2370. inherited ppuwrite(ppufile);
  2371. ppufile.putlongint(longint(number));
  2372. writeentry(ppufile,ibsyssym);
  2373. end;
  2374. class function tsyssym.find_by_number(l:longint):tsyssym;
  2375. var
  2376. s : shortstring;
  2377. begin
  2378. str(l,s);
  2379. result:=tsyssym(syssym_list.find(s));
  2380. end;
  2381. {*****************************************************************************
  2382. TMacro
  2383. *****************************************************************************}
  2384. constructor tmacro.create(const n : string);
  2385. begin
  2386. inherited create(macrosym,n,true);
  2387. owner:=nil;
  2388. defined:=false;
  2389. is_used:=false;
  2390. is_compiler_var:=false;
  2391. buftext:=nil;
  2392. buflen:=0;
  2393. end;
  2394. constructor tmacro.ppuload(ppufile:tcompilerppufile);
  2395. begin
  2396. inherited ppuload(macrosym,ppufile);
  2397. defined:=ppufile.getboolean;
  2398. is_compiler_var:=ppufile.getboolean;
  2399. is_used:=false;
  2400. buflen:= ppufile.getlongint;
  2401. if buflen > 0 then
  2402. begin
  2403. getmem(buftext, buflen);
  2404. ppufile.getdata(buftext^, buflen)
  2405. end
  2406. else
  2407. buftext:=nil;
  2408. end;
  2409. destructor tmacro.destroy;
  2410. begin
  2411. if assigned(buftext) then
  2412. freemem(buftext);
  2413. inherited destroy;
  2414. end;
  2415. procedure tmacro.ppuwrite(ppufile:tcompilerppufile);
  2416. begin
  2417. inherited ppuwrite(ppufile);
  2418. ppufile.putboolean(defined);
  2419. ppufile.putboolean(is_compiler_var);
  2420. ppufile.putlongint(buflen);
  2421. if buflen > 0 then
  2422. ppufile.putdata(buftext^,buflen);
  2423. writeentry(ppufile,ibmacrosym);
  2424. end;
  2425. function tmacro.GetCopy:tmacro;
  2426. var
  2427. p : tmacro;
  2428. begin
  2429. p:=tmacro.create(realname);
  2430. p.defined:=defined;
  2431. p.is_used:=is_used;
  2432. p.is_compiler_var:=is_compiler_var;
  2433. p.buflen:=buflen;
  2434. if assigned(buftext) then
  2435. begin
  2436. getmem(p.buftext,buflen);
  2437. move(buftext^,p.buftext^,buflen);
  2438. end;
  2439. Result:=p;
  2440. end;
  2441. procedure init_symsym;
  2442. begin
  2443. syssym_list:=tfphashobjectlist.create(false);
  2444. end;
  2445. procedure done_symsym;
  2446. begin
  2447. syssym_list.free;
  2448. end;
  2449. initialization
  2450. register_initdone_proc(@init_symsym,@done_symsym);
  2451. end.