symsym.pas 99 KB

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