symsym.pas 93 KB

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