symsym.pas 95 KB

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