2
0

symsym.pas 93 KB

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