symsym.pas 84 KB

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