symsym.pas 104 KB

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