2
0

symsym.pas 102 KB

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