symsym.pas 102 KB

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