symsym.pas 103 KB

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