2
0

symsym.pas 94 KB

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