symsym.pas 94 KB

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