symsym.pas 97 KB

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