symsym.pas 96 KB

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