symsym.pas 93 KB

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