symsym.pas 93 KB

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