symsym.pas 96 KB

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