symsym.pas 82 KB

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