symsym.pas 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012
  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:string;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. terrorsym = class(Tsym)
  69. constructor create;
  70. end;
  71. { tprocsym }
  72. tprocsym = class(tstoredsym)
  73. protected
  74. FProcdefList : TFPObjectList;
  75. FProcdefDerefList : TFPList;
  76. public
  77. constructor create(const n : string);
  78. constructor ppuload(ppufile:tcompilerppufile);
  79. destructor destroy;override;
  80. { writes all declarations except the specified one }
  81. procedure write_parameter_lists(skipdef:tprocdef);
  82. { tests, if all procedures definitions are defined and not }
  83. { only forward }
  84. procedure check_forward;
  85. procedure ppuwrite(ppufile:tcompilerppufile);override;
  86. procedure buildderef;override;
  87. procedure deref;override;
  88. function find_procdef_bytype(pt:Tproctypeoption):Tprocdef;
  89. function find_procdef_bypara(para:TFPObjectList;retdef:tdef;cpoptions:tcompare_paras_options):Tprocdef;
  90. function find_procdef_byoptions(ops:tprocoptions): Tprocdef;
  91. function find_procdef_byprocvardef(d:Tprocvardef):Tprocdef;
  92. function find_procdef_assignment_operator(fromdef,todef:tdef;var besteq:tequaltype):Tprocdef;
  93. function find_procdef_enumerator_operator(fromdef,todef:tdef;var besteq:tequaltype):Tprocdef;
  94. property ProcdefList:TFPObjectList read FProcdefList;
  95. end;
  96. ttypesym = class(Tstoredsym)
  97. typedef : tdef;
  98. typedefderef : tderef;
  99. fprettyname : ansistring;
  100. constructor create(const n : string;def:tdef);
  101. constructor ppuload(ppufile:tcompilerppufile);
  102. procedure ppuwrite(ppufile:tcompilerppufile);override;
  103. procedure buildderef;override;
  104. procedure deref;override;
  105. function prettyname : string;override;
  106. end;
  107. tabstractvarsym = class(tstoredsym)
  108. varoptions : tvaroptions;
  109. notifications : Tlinkedlist;
  110. varspez : tvarspez; { sets the type of access }
  111. varregable : tvarregable;
  112. varstate : tvarstate;
  113. { Has the address of this variable potentially escaped the }
  114. { block in which is was declared? }
  115. { could also be part of tabstractnormalvarsym, but there's }
  116. { one byte left here till the next 4 byte alignment }
  117. addr_taken : boolean;
  118. constructor create(st:tsymtyp;const n : string;vsp:tvarspez;def:tdef;vopts:tvaroptions);
  119. constructor ppuload(st:tsymtyp;ppufile:tcompilerppufile);
  120. destructor destroy;override;
  121. procedure ppuwrite(ppufile:tcompilerppufile);override;
  122. procedure buildderef;override;
  123. procedure deref;override;
  124. function getsize : asizeint;
  125. function getpackedbitsize : longint;
  126. function is_regvar(refpara: boolean):boolean;
  127. procedure trigger_notifications(what:Tnotification_flag);
  128. function register_notification(flags:Tnotification_flags;
  129. callback:Tnotification_callback):cardinal;
  130. procedure unregister_notification(id:cardinal);
  131. private
  132. _vardef : tdef;
  133. vardefderef : tderef;
  134. procedure setvardef(def:tdef);
  135. public
  136. property vardef: tdef read _vardef write setvardef;
  137. end;
  138. tfieldvarsym = class(tabstractvarsym)
  139. fieldoffset : asizeint; { offset in record/object }
  140. cachedmangledname: pshortstring; { mangled name for ObjC or Java }
  141. constructor create(const n : string;vsp:tvarspez;def:tdef;vopts:tvaroptions);
  142. constructor ppuload(ppufile:tcompilerppufile);
  143. procedure ppuwrite(ppufile:tcompilerppufile);override;
  144. function mangledname:string;override;
  145. destructor destroy;override;
  146. end;
  147. tabstractnormalvarsym = class(tabstractvarsym)
  148. defaultconstsym : tsym;
  149. defaultconstsymderef : tderef;
  150. localloc : TLocation; { register/reference for local var }
  151. initialloc : TLocation; { initial location so it can still be initialized later after the location was changed by SSA }
  152. constructor create(st:tsymtyp;const n : string;vsp:tvarspez;def:tdef;vopts:tvaroptions);
  153. constructor ppuload(st:tsymtyp;ppufile:tcompilerppufile);
  154. procedure ppuwrite(ppufile:tcompilerppufile);override;
  155. procedure buildderef;override;
  156. procedure deref;override;
  157. end;
  158. tlocalvarsym = class(tabstractnormalvarsym)
  159. constructor create(const n : string;vsp:tvarspez;def:tdef;vopts:tvaroptions);
  160. constructor ppuload(ppufile:tcompilerppufile);
  161. procedure ppuwrite(ppufile:tcompilerppufile);override;
  162. end;
  163. tparavarsym = class(tabstractnormalvarsym)
  164. paraloc : array[tcallercallee] of TCGPara;
  165. paranr : word; { position of this parameter }
  166. { in MacPas mode, "univ" parameters mean that type checking should
  167. be disabled, except that the size of the passed parameter must
  168. match the size of the formal parameter }
  169. univpara : boolean;
  170. {$ifdef EXTDEBUG}
  171. eqval : tequaltype;
  172. {$endif EXTDEBUG}
  173. constructor create(const n : string;nr:word;vsp:tvarspez;def:tdef;vopts:tvaroptions);
  174. constructor ppuload(ppufile:tcompilerppufile);
  175. destructor destroy;override;
  176. procedure ppuwrite(ppufile:tcompilerppufile);override;
  177. end;
  178. tstaticvarsym = class(tabstractnormalvarsym)
  179. private
  180. _mangledname : pshortstring;
  181. public
  182. section : ansistring;
  183. constructor create(const n : string;vsp:tvarspez;def:tdef;vopts:tvaroptions);
  184. constructor create_dll(const n : string;vsp:tvarspez;def:tdef);
  185. constructor create_C(const n,mangled : string;vsp:tvarspez;def:tdef);
  186. constructor ppuload(ppufile:tcompilerppufile);
  187. destructor destroy;override;
  188. procedure ppuwrite(ppufile:tcompilerppufile);override;
  189. function mangledname:string;override;
  190. procedure set_mangledname(const s:string);
  191. end;
  192. tabsolutevarsym = class(tabstractvarsym)
  193. public
  194. abstyp : absolutetyp;
  195. {$ifdef i386}
  196. absseg : boolean;
  197. {$endif i386}
  198. asmname : pshortstring;
  199. addroffset : aword;
  200. ref : tpropaccesslist;
  201. constructor create(const n : string;def:tdef);
  202. constructor create_ref(const n : string;def:tdef;_ref:tpropaccesslist);
  203. destructor destroy;override;
  204. constructor ppuload(ppufile:tcompilerppufile);
  205. procedure buildderef;override;
  206. procedure deref;override;
  207. function mangledname : string;override;
  208. procedure ppuwrite(ppufile:tcompilerppufile);override;
  209. end;
  210. tpropaccesslisttypes=(palt_none,palt_read,palt_write,palt_stored);
  211. tpropertysym = class(Tstoredsym)
  212. propoptions : tpropertyoptions;
  213. overriddenpropsym : tpropertysym;
  214. overriddenpropsymderef : tderef;
  215. propdef : tdef;
  216. propdefderef : tderef;
  217. indexdef : tdef;
  218. indexdefderef : tderef;
  219. index,
  220. default : longint;
  221. dispid : longint;
  222. propaccesslist: array[tpropaccesslisttypes] of tpropaccesslist;
  223. constructor create(const n : string);
  224. destructor destroy;override;
  225. constructor ppuload(ppufile:tcompilerppufile);
  226. function getsize : asizeint;
  227. procedure ppuwrite(ppufile:tcompilerppufile);override;
  228. procedure buildderef;override;
  229. procedure deref;override;
  230. end;
  231. tconstvalue = record
  232. case integer of
  233. 0: (valueord : tconstexprint);
  234. 1: (valueordptr : tconstptruint);
  235. 2: (valueptr : pointer; len : longint);
  236. end;
  237. tconstsym = class(tstoredsym)
  238. constdef : tdef;
  239. constdefderef : tderef;
  240. consttyp : tconsttyp;
  241. value : tconstvalue;
  242. constructor create_ord(const n : string;t : tconsttyp;v : tconstexprint;def:tdef);
  243. constructor create_ordptr(const n : string;t : tconsttyp;v : tconstptruint;def:tdef);
  244. constructor create_ptr(const n : string;t : tconsttyp;v : pointer;def:tdef);
  245. constructor create_string(const n : string;t : tconsttyp;str:pchar;l:longint);
  246. constructor create_wstring(const n : string;t : tconsttyp;pw:pcompilerwidestring);
  247. constructor ppuload(ppufile:tcompilerppufile);
  248. destructor destroy;override;
  249. procedure buildderef;override;
  250. procedure deref;override;
  251. procedure ppuwrite(ppufile:tcompilerppufile);override;
  252. end;
  253. tenumsym = class(Tstoredsym)
  254. value : longint;
  255. definition : tenumdef;
  256. definitionderef : tderef;
  257. constructor create(const n : string;def : tenumdef;v : longint);
  258. constructor ppuload(ppufile:tcompilerppufile);
  259. procedure ppuwrite(ppufile:tcompilerppufile);override;
  260. procedure buildderef;override;
  261. procedure deref;override;
  262. end;
  263. tsyssym = class(Tstoredsym)
  264. number : longint;
  265. constructor create(const n : string;l : longint);
  266. constructor ppuload(ppufile:tcompilerppufile);
  267. destructor destroy;override;
  268. procedure ppuwrite(ppufile:tcompilerppufile);override;
  269. end;
  270. const
  271. maxmacrolen=16*1024;
  272. type
  273. pmacrobuffer = ^tmacrobuffer;
  274. tmacrobuffer = array[0..maxmacrolen-1] of char;
  275. tmacro = class(tstoredsym)
  276. {Normally true, but false when a previously defined macro is undef-ed}
  277. defined : boolean;
  278. {True if this is a mac style compiler variable, in which case no macro
  279. substitutions shall be done.}
  280. is_compiler_var : boolean;
  281. {Whether the macro was used. NOTE: A use of a macro which was never defined}
  282. {e. g. an IFDEF which returns false, will not be registered as used,}
  283. {since there is no place to register its use. }
  284. is_used : boolean;
  285. buftext : pchar;
  286. buflen : longint;
  287. constructor create(const n : string);
  288. constructor ppuload(ppufile:tcompilerppufile);
  289. procedure ppuwrite(ppufile:tcompilerppufile);override;
  290. destructor destroy;override;
  291. function GetCopy:tmacro;
  292. end;
  293. var
  294. generrorsym : tsym;
  295. implementation
  296. uses
  297. { global }
  298. verbose,
  299. { target }
  300. systems,
  301. { symtable }
  302. defutil,symtable,jvmdef,
  303. fmodule,
  304. { tree }
  305. node,
  306. { aasm }
  307. aasmtai,aasmdata,
  308. { codegen }
  309. paramgr,
  310. procinfo
  311. ;
  312. {****************************************************************************
  313. Helpers
  314. ****************************************************************************}
  315. {****************************************************************************
  316. TSYM (base for all symtypes)
  317. ****************************************************************************}
  318. constructor tstoredsym.create(st:tsymtyp;const n : string);
  319. begin
  320. inherited create(st,n);
  321. { Register in current_module }
  322. if assigned(current_module) then
  323. begin
  324. current_module.symlist.Add(self);
  325. SymId:=current_module.symlist.Count-1;
  326. end;
  327. end;
  328. constructor tstoredsym.ppuload(st:tsymtyp;ppufile:tcompilerppufile);
  329. begin
  330. SymId:=ppufile.getlongint;
  331. inherited Create(st,ppufile.getstring);
  332. { Register symbol }
  333. current_module.symlist[SymId]:=self;
  334. ppufile.getposinfo(fileinfo);
  335. visibility:=tvisibility(ppufile.getbyte);
  336. ppufile.getsmallset(symoptions);
  337. if sp_has_deprecated_msg in symoptions then
  338. deprecatedmsg:=stringdup(ppufile.getstring)
  339. else
  340. deprecatedmsg:=nil;
  341. end;
  342. procedure tstoredsym.ppuwrite(ppufile:tcompilerppufile);
  343. begin
  344. ppufile.putlongint(SymId);
  345. ppufile.putstring(realname);
  346. ppufile.putposinfo(fileinfo);
  347. ppufile.putbyte(byte(visibility));
  348. ppufile.putsmallset(symoptions);
  349. if sp_has_deprecated_msg in symoptions then
  350. ppufile.putstring(deprecatedmsg^);
  351. end;
  352. destructor tstoredsym.destroy;
  353. begin
  354. inherited destroy;
  355. end;
  356. {****************************************************************************
  357. TLABELSYM
  358. ****************************************************************************}
  359. constructor tlabelsym.create(const n : string);
  360. begin
  361. inherited create(labelsym,n);
  362. used:=false;
  363. defined:=false;
  364. nonlocal:=false;
  365. code:=nil;
  366. end;
  367. constructor tlabelsym.ppuload(ppufile:tcompilerppufile);
  368. begin
  369. inherited ppuload(labelsym,ppufile);
  370. code:=nil;
  371. used:=false;
  372. nonlocal:=false;
  373. defined:=true;
  374. end;
  375. procedure tlabelsym.ppuwrite(ppufile:tcompilerppufile);
  376. begin
  377. if owner.symtabletype=globalsymtable then
  378. Message(sym_e_ill_label_decl)
  379. else
  380. begin
  381. inherited ppuwrite(ppufile);
  382. ppufile.writeentry(iblabelsym);
  383. end;
  384. end;
  385. function tlabelsym.mangledname:string;
  386. begin
  387. if not(defined) then
  388. begin
  389. defined:=true;
  390. if nonlocal then
  391. current_asmdata.getglobaljumplabel(asmblocklabel)
  392. else
  393. current_asmdata.getjumplabel(asmblocklabel);
  394. end;
  395. result:=asmblocklabel.name;
  396. end;
  397. {****************************************************************************
  398. TUNITSYM
  399. ****************************************************************************}
  400. constructor tunitsym.create(const n : string;amodule : tobject);
  401. begin
  402. inherited create(unitsym,n);
  403. module:=amodule;
  404. end;
  405. constructor tunitsym.ppuload(ppufile:tcompilerppufile);
  406. begin
  407. inherited ppuload(unitsym,ppufile);
  408. module:=nil;
  409. end;
  410. destructor tunitsym.destroy;
  411. begin
  412. inherited destroy;
  413. end;
  414. procedure tunitsym.ppuwrite(ppufile:tcompilerppufile);
  415. begin
  416. inherited ppuwrite(ppufile);
  417. ppufile.writeentry(ibunitsym);
  418. end;
  419. {****************************************************************************
  420. TPROCSYM
  421. ****************************************************************************}
  422. constructor tprocsym.create(const n : string);
  423. begin
  424. inherited create(procsym,n);
  425. FProcdefList:=TFPObjectList.Create(false);
  426. FProcdefderefList:=nil;
  427. { the tprocdef have their own symoptions, make the procsym
  428. always visible }
  429. visibility:=vis_public;
  430. end;
  431. constructor tprocsym.ppuload(ppufile:tcompilerppufile);
  432. var
  433. pdderef : tderef;
  434. i,
  435. pdcnt : longint;
  436. begin
  437. inherited ppuload(procsym,ppufile);
  438. FProcdefList:=TFPObjectList.Create(false);
  439. FProcdefDerefList:=TFPList.Create;
  440. pdcnt:=ppufile.getword;
  441. for i:=1 to pdcnt do
  442. begin
  443. ppufile.getderef(pdderef);
  444. FProcdefDerefList.Add(Pointer(PtrInt(pdderef.dataidx)));
  445. end;
  446. end;
  447. destructor tprocsym.destroy;
  448. begin
  449. FProcdefList.Free;
  450. if assigned(FProcdefDerefList) then
  451. FProcdefDerefList.Free;
  452. inherited destroy;
  453. end;
  454. procedure tprocsym.ppuwrite(ppufile:tcompilerppufile);
  455. var
  456. i : longint;
  457. d : tderef;
  458. begin
  459. inherited ppuwrite(ppufile);
  460. ppufile.putword(FProcdefDerefList.Count);
  461. for i:=0 to FProcdefDerefList.Count-1 do
  462. begin
  463. d.dataidx:=PtrInt(FProcdefDerefList[i]);
  464. ppufile.putderef(d);
  465. end;
  466. ppufile.writeentry(ibprocsym);
  467. end;
  468. procedure tprocsym.write_parameter_lists(skipdef:tprocdef);
  469. var
  470. i : longint;
  471. pd : tprocdef;
  472. begin
  473. for i:=0 to ProcdefList.Count-1 do
  474. begin
  475. pd:=tprocdef(ProcdefList[i]);
  476. if pd<>skipdef then
  477. MessagePos1(pd.fileinfo,sym_h_param_list,pd.fullprocname(false));
  478. end;
  479. end;
  480. procedure tprocsym.check_forward;
  481. var
  482. i : longint;
  483. pd : tprocdef;
  484. begin
  485. for i:=0 to ProcdefList.Count-1 do
  486. begin
  487. pd:=tprocdef(ProcdefList[i]);
  488. if (pd.owner=owner) and (pd.forwarddef) then
  489. begin
  490. { For mode macpas. Make implicit externals (procedures declared in the interface
  491. section which do not have a counterpart in the implementation)
  492. to be an imported procedure }
  493. if (m_mac in current_settings.modeswitches) and
  494. (pd.interfacedef) then
  495. begin
  496. pd.setmangledname(target_info.CPrefix+tprocdef(pd).procsym.realname);
  497. if (not current_module.interface_only) then
  498. MessagePos1(pd.fileinfo,sym_w_forward_not_resolved,pd.fullprocname(false));
  499. end
  500. else
  501. begin
  502. MessagePos1(pd.fileinfo,sym_e_forward_not_resolved,pd.fullprocname(false));
  503. end;
  504. { Turn further error messages off }
  505. pd.forwarddef:=false;
  506. end;
  507. end;
  508. end;
  509. procedure tprocsym.buildderef;
  510. var
  511. i : longint;
  512. pd : tprocdef;
  513. d : tderef;
  514. begin
  515. if not assigned(FProcdefDerefList) then
  516. FProcdefDerefList:=TFPList.Create
  517. else
  518. FProcdefDerefList.Clear;
  519. for i:=0 to ProcdefList.Count-1 do
  520. begin
  521. pd:=tprocdef(ProcdefList[i]);
  522. { only write the proc definitions that belong
  523. to this procsym and are in the global symtable }
  524. if pd.owner=owner then
  525. begin
  526. d.build(pd);
  527. FProcdefDerefList.Add(Pointer(PtrInt(d.dataidx)));
  528. end;
  529. end;
  530. end;
  531. procedure tprocsym.deref;
  532. var
  533. i : longint;
  534. pd : tprocdef;
  535. d : tderef;
  536. begin
  537. { Clear all procdefs }
  538. ProcdefList.Clear;
  539. if not assigned(FProcdefDerefList) then
  540. internalerror(200611031);
  541. for i:=0 to FProcdefDerefList.Count-1 do
  542. begin
  543. d.dataidx:=PtrInt(FProcdefDerefList[i]);
  544. pd:=tprocdef(d.resolve);
  545. ProcdefList.Add(pd);
  546. end;
  547. end;
  548. function Tprocsym.Find_procdef_bytype(pt:Tproctypeoption):Tprocdef;
  549. var
  550. i : longint;
  551. pd : tprocdef;
  552. begin
  553. result:=nil;
  554. for i:=0 to ProcdefList.Count-1 do
  555. begin
  556. pd:=tprocdef(ProcdefList[i]);
  557. if pd.proctypeoption=pt then
  558. begin
  559. result:=pd;
  560. exit;
  561. end;
  562. end;
  563. end;
  564. function Tprocsym.Find_procdef_bypara(para:TFPObjectList;retdef:tdef;
  565. cpoptions:tcompare_paras_options):Tprocdef;
  566. var
  567. i : longint;
  568. pd : tprocdef;
  569. eq : tequaltype;
  570. begin
  571. result:=nil;
  572. for i:=0 to ProcdefList.Count-1 do
  573. begin
  574. pd:=tprocdef(ProcdefList[i]);
  575. if assigned(retdef) then
  576. eq:=compare_defs(retdef,pd.returndef,nothingn)
  577. else
  578. eq:=te_equal;
  579. if (eq>=te_equal) or
  580. ((cpo_allowconvert in cpoptions) and (eq>te_incompatible)) then
  581. begin
  582. eq:=compare_paras(para,pd.paras,cp_value_equal_const,cpoptions);
  583. if (eq>=te_equal) or
  584. ((cpo_allowconvert in cpoptions) and (eq>te_incompatible)) then
  585. begin
  586. result:=pd;
  587. exit;
  588. end;
  589. end;
  590. end;
  591. end;
  592. function tprocsym.find_procdef_byoptions(ops: tprocoptions): Tprocdef;
  593. var
  594. i : longint;
  595. pd : tprocdef;
  596. begin
  597. result:=nil;
  598. for i:=0 to ProcdefList.Count-1 do
  599. begin
  600. pd:=tprocdef(ProcdefList[i]);
  601. if ops * pd.procoptions = ops then
  602. begin
  603. result:=pd;
  604. exit;
  605. end;
  606. end;
  607. end;
  608. function Tprocsym.Find_procdef_byprocvardef(d:Tprocvardef):Tprocdef;
  609. var
  610. i : longint;
  611. bestpd,
  612. pd : tprocdef;
  613. eq,besteq : tequaltype;
  614. begin
  615. { This function will return the pprocdef of pprocsym that
  616. is the best match for procvardef. When there are multiple
  617. matches it returns nil.}
  618. result:=nil;
  619. bestpd:=nil;
  620. besteq:=te_incompatible;
  621. for i:=0 to ProcdefList.Count-1 do
  622. begin
  623. pd:=tprocdef(ProcdefList[i]);
  624. eq:=proc_to_procvar_equal(pd,d,false);
  625. if eq>=te_convert_l1 then
  626. begin
  627. { multiple procvars with the same equal level }
  628. if assigned(bestpd) and
  629. (besteq=eq) then
  630. exit;
  631. if eq>besteq then
  632. begin
  633. besteq:=eq;
  634. bestpd:=pd;
  635. end;
  636. end;
  637. end;
  638. result:=bestpd;
  639. end;
  640. function Tprocsym.Find_procdef_assignment_operator(fromdef,todef:tdef;var besteq:tequaltype):Tprocdef;
  641. var
  642. paraidx, realparamcount,
  643. i, j : longint;
  644. bestpd,
  645. hpd,
  646. pd : tprocdef;
  647. convtyp : tconverttype;
  648. eq : tequaltype;
  649. begin
  650. { This function will return the pprocdef of pprocsym that
  651. is the best match for fromdef and todef. }
  652. result:=nil;
  653. bestpd:=nil;
  654. besteq:=te_incompatible;
  655. for i:=0 to ProcdefList.Count-1 do
  656. begin
  657. pd:=tprocdef(ProcdefList[i]);
  658. if (pd.owner.symtabletype=staticsymtable) and not pd.owner.iscurrentunit then
  659. continue;
  660. if (equal_defs(todef,pd.returndef) or
  661. { shortstrings of different lengths are ok as result }
  662. (is_shortstring(todef) and is_shortstring(pd.returndef))) and
  663. { the result type must be always really equal and not an alias,
  664. if you mess with this code, check tw4093 }
  665. ((todef=pd.returndef) or
  666. (
  667. not(df_unique in todef.defoptions) and
  668. not(df_unique in pd.returndef.defoptions)
  669. )
  670. ) then
  671. begin
  672. paraidx:=0;
  673. { ignore vs_hidden parameters }
  674. while (paraidx<pd.paras.count) and
  675. assigned(pd.paras[paraidx]) and
  676. (vo_is_hidden_para in tparavarsym(pd.paras[paraidx]).varoptions) do
  677. inc(paraidx);
  678. realparamcount:=0;
  679. for j := 0 to pd.paras.Count-1 do
  680. if assigned(pd.paras[j]) and not (vo_is_hidden_para in tparavarsym(pd.paras[j]).varoptions) then
  681. inc(realparamcount);
  682. if (paraidx<pd.paras.count) and
  683. assigned(pd.paras[paraidx]) and
  684. (realparamcount = 1) then
  685. begin
  686. eq:=compare_defs_ext(fromdef,tparavarsym(pd.paras[paraidx]).vardef,nothingn,convtyp,hpd,[]);
  687. { alias? if yes, only l1 choice,
  688. if you mess with this code, check tw4093 }
  689. if (eq=te_exact) and
  690. (fromdef<>tparavarsym(pd.paras[paraidx]).vardef) and
  691. ((df_unique in fromdef.defoptions) or
  692. (df_unique in tparavarsym(pd.paras[paraidx]).vardef.defoptions)) then
  693. eq:=te_convert_l1;
  694. if eq=te_exact then
  695. begin
  696. besteq:=eq;
  697. result:=pd;
  698. exit;
  699. end;
  700. if eq>besteq then
  701. begin
  702. bestpd:=pd;
  703. besteq:=eq;
  704. end;
  705. end;
  706. end;
  707. end;
  708. result:=bestpd;
  709. end;
  710. function Tprocsym.find_procdef_enumerator_operator(fromdef,todef:tdef;var besteq:tequaltype):Tprocdef;
  711. var
  712. paraidx, realparamcount,
  713. i, j : longint;
  714. bestpd,
  715. hpd,
  716. pd : tprocdef;
  717. current : tpropertysym;
  718. convtyp : tconverttype;
  719. eq : tequaltype;
  720. begin
  721. { This function will return the pprocdef of pprocsym that
  722. is the best match for fromdef and todef. }
  723. result:=nil;
  724. bestpd:=nil;
  725. besteq:=te_incompatible;
  726. for i:=0 to ProcdefList.Count-1 do
  727. begin
  728. pd:=tprocdef(ProcdefList[i]);
  729. if (pd.owner.symtabletype=staticsymtable) and not pd.owner.iscurrentunit then
  730. continue;
  731. if not (is_class_or_interface_or_object(pd.returndef) or is_record(pd.returndef)) then
  732. continue;
  733. current := tpropertysym(tabstractrecorddef(pd.returndef).search_enumerator_current);
  734. if (current = nil) then
  735. continue;
  736. // compare current result def with the todef
  737. if (equal_defs(todef, current.propdef) or
  738. { shortstrings of different lengths are ok as result }
  739. (is_shortstring(todef) and is_shortstring(current.propdef))) and
  740. { the result type must be always really equal and not an alias,
  741. if you mess with this code, check tw4093 }
  742. ((todef=current.propdef) or
  743. (
  744. not(df_unique in todef.defoptions) and
  745. not(df_unique in current.propdef.defoptions)
  746. )
  747. ) then
  748. begin
  749. paraidx:=0;
  750. { ignore vs_hidden parameters }
  751. while (paraidx<pd.paras.count) and
  752. assigned(pd.paras[paraidx]) and
  753. (vo_is_hidden_para in tparavarsym(pd.paras[paraidx]).varoptions) do
  754. inc(paraidx);
  755. realparamcount:=0;
  756. for j := 0 to pd.paras.Count-1 do
  757. if assigned(pd.paras[j]) and not (vo_is_hidden_para in tparavarsym(pd.paras[j]).varoptions) then
  758. inc(realparamcount);
  759. if (paraidx<pd.paras.count) and
  760. assigned(pd.paras[paraidx]) and
  761. (realparamcount = 1) then
  762. begin
  763. eq:=compare_defs_ext(fromdef,tparavarsym(pd.paras[paraidx]).vardef,nothingn,convtyp,hpd,[]);
  764. { alias? if yes, only l1 choice,
  765. if you mess with this code, check tw4093 }
  766. if (eq=te_exact) and
  767. (fromdef<>tparavarsym(pd.paras[paraidx]).vardef) and
  768. ((df_unique in fromdef.defoptions) or
  769. (df_unique in tparavarsym(pd.paras[paraidx]).vardef.defoptions)) then
  770. eq:=te_convert_l1;
  771. if eq=te_exact then
  772. begin
  773. besteq:=eq;
  774. result:=pd;
  775. exit;
  776. end;
  777. if eq>besteq then
  778. begin
  779. bestpd:=pd;
  780. besteq:=eq;
  781. end;
  782. end;
  783. end;
  784. end;
  785. result:=bestpd;
  786. end;
  787. {****************************************************************************
  788. TERRORSYM
  789. ****************************************************************************}
  790. constructor terrorsym.create;
  791. begin
  792. inherited create(errorsym,'');
  793. end;
  794. {****************************************************************************
  795. TPROPERTYSYM
  796. ****************************************************************************}
  797. constructor tpropertysym.create(const n : string);
  798. var
  799. pap : tpropaccesslisttypes;
  800. begin
  801. inherited create(propertysym,n);
  802. propoptions:=[];
  803. index:=0;
  804. default:=0;
  805. propdef:=nil;
  806. indexdef:=nil;
  807. for pap:=low(tpropaccesslisttypes) to high(tpropaccesslisttypes) do
  808. propaccesslist[pap]:=tpropaccesslist.create;
  809. end;
  810. constructor tpropertysym.ppuload(ppufile:tcompilerppufile);
  811. var
  812. pap : tpropaccesslisttypes;
  813. begin
  814. inherited ppuload(propertysym,ppufile);
  815. ppufile.getsmallset(propoptions);
  816. ppufile.getderef(overriddenpropsymderef);
  817. ppufile.getderef(propdefderef);
  818. index:=ppufile.getlongint;
  819. default:=ppufile.getlongint;
  820. ppufile.getderef(indexdefderef);
  821. for pap:=low(tpropaccesslisttypes) to high(tpropaccesslisttypes) do
  822. propaccesslist[pap]:=ppufile.getpropaccesslist;
  823. end;
  824. destructor tpropertysym.destroy;
  825. var
  826. pap : tpropaccesslisttypes;
  827. begin
  828. for pap:=low(tpropaccesslisttypes) to high(tpropaccesslisttypes) do
  829. propaccesslist[pap].free;
  830. inherited destroy;
  831. end;
  832. procedure tpropertysym.buildderef;
  833. var
  834. pap : tpropaccesslisttypes;
  835. begin
  836. overriddenpropsymderef.build(overriddenpropsym);
  837. propdefderef.build(propdef);
  838. indexdefderef.build(indexdef);
  839. for pap:=low(tpropaccesslisttypes) to high(tpropaccesslisttypes) do
  840. propaccesslist[pap].buildderef;
  841. end;
  842. procedure tpropertysym.deref;
  843. var
  844. pap : tpropaccesslisttypes;
  845. begin
  846. overriddenpropsym:=tpropertysym(overriddenpropsymderef.resolve);
  847. indexdef:=tdef(indexdefderef.resolve);
  848. propdef:=tdef(propdefderef.resolve);
  849. for pap:=low(tpropaccesslisttypes) to high(tpropaccesslisttypes) do
  850. propaccesslist[pap].resolve;
  851. end;
  852. function tpropertysym.getsize : asizeint;
  853. begin
  854. getsize:=0;
  855. end;
  856. procedure tpropertysym.ppuwrite(ppufile:tcompilerppufile);
  857. var
  858. pap : tpropaccesslisttypes;
  859. begin
  860. inherited ppuwrite(ppufile);
  861. ppufile.putsmallset(propoptions);
  862. ppufile.putderef(overriddenpropsymderef);
  863. ppufile.putderef(propdefderef);
  864. ppufile.putlongint(index);
  865. ppufile.putlongint(default);
  866. ppufile.putderef(indexdefderef);
  867. for pap:=low(tpropaccesslisttypes) to high(tpropaccesslisttypes) do
  868. ppufile.putpropaccesslist(propaccesslist[pap]);
  869. ppufile.writeentry(ibpropertysym);
  870. end;
  871. {****************************************************************************
  872. TABSTRACTVARSYM
  873. ****************************************************************************}
  874. constructor tabstractvarsym.create(st:tsymtyp;const n : string;vsp:tvarspez;def:tdef;vopts:tvaroptions);
  875. begin
  876. inherited create(st,n);
  877. vardef:=def;
  878. varspez:=vsp;
  879. varstate:=vs_declared;
  880. varoptions:=vopts;
  881. end;
  882. constructor tabstractvarsym.ppuload(st:tsymtyp;ppufile:tcompilerppufile);
  883. begin
  884. inherited ppuload(st,ppufile);
  885. varstate:=vs_readwritten;
  886. varspez:=tvarspez(ppufile.getbyte);
  887. varregable:=tvarregable(ppufile.getbyte);
  888. addr_taken:=boolean(ppufile.getbyte);
  889. ppufile.getderef(vardefderef);
  890. ppufile.getsmallset(varoptions);
  891. end;
  892. destructor tabstractvarsym.destroy;
  893. begin
  894. if assigned(notifications) then
  895. notifications.destroy;
  896. inherited destroy;
  897. end;
  898. procedure tabstractvarsym.buildderef;
  899. begin
  900. vardefderef.build(vardef);
  901. end;
  902. procedure tabstractvarsym.deref;
  903. var
  904. oldvarregable: tvarregable;
  905. begin
  906. { setting the vardef also updates varregable. We just loaded this }
  907. { value from a ppu, so it must not be changed (e.g. tw7817a.pp/ }
  908. { tw7817b.pp: the address is taken of a local variable in an }
  909. { inlined procedure -> must remain non-regable when inlining) }
  910. oldvarregable:=varregable;
  911. vardef:=tdef(vardefderef.resolve);
  912. varregable:=oldvarregable;
  913. end;
  914. procedure tabstractvarsym.ppuwrite(ppufile:tcompilerppufile);
  915. var
  916. oldintfcrc : boolean;
  917. begin
  918. inherited ppuwrite(ppufile);
  919. ppufile.putbyte(byte(varspez));
  920. oldintfcrc:=ppufile.do_crc;
  921. ppufile.do_crc:=false;
  922. ppufile.putbyte(byte(varregable));
  923. ppufile.putbyte(byte(addr_taken));
  924. ppufile.do_crc:=oldintfcrc;
  925. ppufile.putderef(vardefderef);
  926. ppufile.putsmallset(varoptions);
  927. end;
  928. function tabstractvarsym.getsize : asizeint;
  929. begin
  930. if assigned(vardef) and
  931. ((vardef.typ<>arraydef) or
  932. is_dynamic_array(vardef) or
  933. (tarraydef(vardef).highrange>=tarraydef(vardef).lowrange)) then
  934. result:=vardef.size
  935. else
  936. result:=0;
  937. end;
  938. function tabstractvarsym.getpackedbitsize : longint;
  939. begin
  940. { bitpacking is only done for ordinals }
  941. if not is_ordinal(vardef) then
  942. internalerror(2006082010);
  943. result:=vardef.packedbitsize;
  944. end;
  945. function tabstractvarsym.is_regvar(refpara: boolean):boolean;
  946. begin
  947. { Register variables are not allowed in the following cases:
  948. - regvars are disabled
  949. - exceptions are used (after an exception is raised the contents of the
  950. registers is not valid anymore)
  951. - it has a local copy
  952. - the value needs to be in memory (i.e. reference counted) }
  953. result:=(cs_opt_regvar in current_settings.optimizerswitches) and
  954. not(pi_has_assembler_block in current_procinfo.flags) and
  955. not(pi_uses_exceptions in current_procinfo.flags) and
  956. not(pi_has_interproclabel in current_procinfo.flags) and
  957. not(vo_has_local_copy in varoptions) and
  958. ((refpara and
  959. (varregable <> vr_none)) or
  960. (not refpara and
  961. not(varregable in [vr_none,vr_addr])))
  962. {$if not defined(powerpc) and not defined(powerpc64)}
  963. and ((vardef.typ <> recorddef) or
  964. (varregable = vr_addr) or
  965. not(varstate in [vs_written,vs_readwritten]));
  966. {$endif}
  967. end;
  968. procedure tabstractvarsym.trigger_notifications(what:Tnotification_flag);
  969. var n:Tnotification;
  970. begin
  971. if assigned(notifications) then
  972. begin
  973. n:=Tnotification(notifications.first);
  974. while assigned(n) do
  975. begin
  976. if what in n.flags then
  977. n.callback(what,self);
  978. n:=Tnotification(n.next);
  979. end;
  980. end;
  981. end;
  982. function Tabstractvarsym.register_notification(flags:Tnotification_flags;callback:
  983. Tnotification_callback):cardinal;
  984. var n:Tnotification;
  985. begin
  986. if not assigned(notifications) then
  987. notifications:=Tlinkedlist.create;
  988. n:=Tnotification.create(flags,callback);
  989. register_notification:=n.id;
  990. notifications.concat(n);
  991. end;
  992. procedure Tabstractvarsym.unregister_notification(id:cardinal);
  993. var n:Tnotification;
  994. begin
  995. if not assigned(notifications) then
  996. internalerror(200212311)
  997. else
  998. begin
  999. n:=Tnotification(notifications.first);
  1000. while assigned(n) do
  1001. begin
  1002. if n.id=id then
  1003. begin
  1004. notifications.remove(n);
  1005. n.destroy;
  1006. exit;
  1007. end;
  1008. n:=Tnotification(n.next);
  1009. end;
  1010. internalerror(200212311)
  1011. end;
  1012. end;
  1013. procedure tabstractvarsym.setvardef(def:tdef);
  1014. begin
  1015. _vardef := def;
  1016. { can we load the value into a register ? }
  1017. if not assigned(owner) or
  1018. (owner.symtabletype in [localsymtable,parasymtable]) or
  1019. (
  1020. (owner.symtabletype=staticsymtable) and
  1021. not(cs_create_pic in current_settings.moduleswitches)
  1022. ) then
  1023. begin
  1024. if tstoreddef(vardef).is_intregable then
  1025. varregable:=vr_intreg
  1026. else
  1027. { $warning TODO: no fpu regvar in staticsymtable yet, need initialization with 0 }
  1028. if {(
  1029. not assigned(owner) or
  1030. (owner.symtabletype<>staticsymtable)
  1031. ) and }
  1032. tstoreddef(vardef).is_fpuregable then
  1033. begin
  1034. if use_vectorfpu(vardef) then
  1035. varregable:=vr_mmreg
  1036. else
  1037. varregable:=vr_fpureg;
  1038. end;
  1039. end;
  1040. end;
  1041. {****************************************************************************
  1042. TFIELDVARSYM
  1043. ****************************************************************************}
  1044. constructor tfieldvarsym.create(const n : string;vsp:tvarspez;def:tdef;vopts:tvaroptions);
  1045. begin
  1046. inherited create(fieldvarsym,n,vsp,def,vopts);
  1047. fieldoffset:=-1;
  1048. end;
  1049. constructor tfieldvarsym.ppuload(ppufile:tcompilerppufile);
  1050. begin
  1051. inherited ppuload(fieldvarsym,ppufile);
  1052. fieldoffset:=ppufile.getaint;
  1053. end;
  1054. procedure tfieldvarsym.ppuwrite(ppufile:tcompilerppufile);
  1055. begin
  1056. inherited ppuwrite(ppufile);
  1057. ppufile.putaint(fieldoffset);
  1058. ppufile.writeentry(ibfieldvarsym);
  1059. end;
  1060. function tfieldvarsym.mangledname:string;
  1061. var
  1062. srsym : tsym;
  1063. srsymtable : tsymtable;
  1064. begin
  1065. {$ifdef jvm}
  1066. if is_javaclass(tdef(owner.defowner)) then
  1067. begin
  1068. if assigned(cachedmangledname) then
  1069. result:=cachedmangledname^
  1070. else
  1071. begin
  1072. result:=jvmmangledbasename(self);
  1073. jvmaddtypeownerprefix(owner,result);
  1074. cachedmangledname:=stringdup(result);
  1075. end;
  1076. end
  1077. else
  1078. {$endif jvm}
  1079. if sp_static in symoptions then
  1080. begin
  1081. if searchsym(lower(owner.name^)+'_'+name,srsym,srsymtable) then
  1082. result:=srsym.mangledname
  1083. { when generating the debug info for the module in which the }
  1084. { symbol is defined, the localsymtable of that module is }
  1085. { already popped from the symtablestack }
  1086. else if searchsym_in_module(current_module,lower(owner.name^)+'_'+name,srsym,srsymtable) then
  1087. result:=srsym.mangledname
  1088. else
  1089. internalerror(2007012501);
  1090. end
  1091. else if is_objcclass(tdef(owner.defowner)) then
  1092. begin
  1093. if assigned(cachedmangledname) then
  1094. result:=cachedmangledname^
  1095. else
  1096. begin
  1097. result:=target_info.cprefix+'OBJC_IVAR_$_'+tobjectdef(owner.defowner).objextname^+'.'+RealName;
  1098. cachedmangledname:=stringdup(result);
  1099. end;
  1100. end
  1101. else
  1102. result:=inherited mangledname;
  1103. end;
  1104. destructor tfieldvarsym.destroy;
  1105. begin
  1106. stringdispose(cachedmangledname);
  1107. inherited destroy;
  1108. end;
  1109. {****************************************************************************
  1110. TABSTRACTNORMALVARSYM
  1111. ****************************************************************************}
  1112. constructor tabstractnormalvarsym.create(st:tsymtyp;const n : string;vsp:tvarspez;def:tdef;vopts:tvaroptions);
  1113. begin
  1114. inherited create(st,n,vsp,def,vopts);
  1115. fillchar(localloc,sizeof(localloc),0);
  1116. fillchar(initialloc,sizeof(initialloc),0);
  1117. defaultconstsym:=nil;
  1118. end;
  1119. constructor tabstractnormalvarsym.ppuload(st:tsymtyp;ppufile:tcompilerppufile);
  1120. begin
  1121. inherited ppuload(st,ppufile);
  1122. fillchar(localloc,sizeof(localloc),0);
  1123. fillchar(initialloc,sizeof(initialloc),0);
  1124. ppufile.getderef(defaultconstsymderef);
  1125. end;
  1126. procedure tabstractnormalvarsym.buildderef;
  1127. begin
  1128. inherited buildderef;
  1129. defaultconstsymderef.build(defaultconstsym);
  1130. end;
  1131. procedure tabstractnormalvarsym.deref;
  1132. begin
  1133. inherited deref;
  1134. defaultconstsym:=tsym(defaultconstsymderef.resolve);
  1135. end;
  1136. procedure tabstractnormalvarsym.ppuwrite(ppufile:tcompilerppufile);
  1137. begin
  1138. inherited ppuwrite(ppufile);
  1139. ppufile.putderef(defaultconstsymderef);
  1140. end;
  1141. {****************************************************************************
  1142. Tstaticvarsym
  1143. ****************************************************************************}
  1144. constructor tstaticvarsym.create(const n : string;vsp:tvarspez;def:tdef;vopts:tvaroptions);
  1145. begin
  1146. inherited create(staticvarsym,n,vsp,def,vopts);
  1147. _mangledname:=nil;
  1148. end;
  1149. constructor tstaticvarsym.create_dll(const n : string;vsp:tvarspez;def:tdef);
  1150. begin
  1151. tstaticvarsym(self).create(n,vsp,def,[vo_is_dll_var]);
  1152. end;
  1153. constructor tstaticvarsym.create_C(const n,mangled : string;vsp:tvarspez;def:tdef);
  1154. begin
  1155. tstaticvarsym(self).create(n,vsp,def,[]);
  1156. set_mangledname(mangled);
  1157. end;
  1158. constructor tstaticvarsym.ppuload(ppufile:tcompilerppufile);
  1159. begin
  1160. inherited ppuload(staticvarsym,ppufile);
  1161. if vo_has_mangledname in varoptions then
  1162. _mangledname:=stringdup(ppufile.getstring)
  1163. else
  1164. _mangledname:=nil;
  1165. if vo_has_section in varoptions then
  1166. section:=ppufile.getansistring;
  1167. end;
  1168. destructor tstaticvarsym.destroy;
  1169. begin
  1170. if assigned(_mangledname) then
  1171. begin
  1172. {$ifdef MEMDEBUG}
  1173. memmanglednames.start;
  1174. {$endif MEMDEBUG}
  1175. stringdispose(_mangledname);
  1176. {$ifdef MEMDEBUG}
  1177. memmanglednames.stop;
  1178. {$endif MEMDEBUG}
  1179. end;
  1180. inherited destroy;
  1181. end;
  1182. procedure tstaticvarsym.ppuwrite(ppufile:tcompilerppufile);
  1183. begin
  1184. inherited ppuwrite(ppufile);
  1185. if vo_has_mangledname in varoptions then
  1186. ppufile.putstring(_mangledname^);
  1187. if vo_has_section in varoptions then
  1188. ppufile.putansistring(section);
  1189. ppufile.writeentry(ibstaticvarsym);
  1190. end;
  1191. function tstaticvarsym.mangledname:string;
  1192. var
  1193. {$ifdef jvm}
  1194. tmpname: string;
  1195. {$else jvm}
  1196. prefix : string[2];
  1197. {$endif jvm}
  1198. begin
  1199. if not assigned(_mangledname) then
  1200. begin
  1201. {$ifdef jvm}
  1202. tmpname:=jvmmangledbasename(self);
  1203. jvmaddtypeownerprefix(owner,tmpname);
  1204. _mangledname:=stringdup(tmpname);
  1205. {$else jvm}
  1206. if (vo_is_typed_const in varoptions) then
  1207. prefix:='TC'
  1208. else
  1209. prefix:='U';
  1210. {$ifdef compress}
  1211. _mangledname:=stringdup(minilzw_encode(make_mangledname(prefix,owner,name)));
  1212. {$else compress}
  1213. _mangledname:=stringdup(make_mangledname(prefix,owner,name));
  1214. {$endif compress}
  1215. {$endif jvm}
  1216. end;
  1217. result:=_mangledname^;
  1218. end;
  1219. procedure tstaticvarsym.set_mangledname(const s:string);
  1220. begin
  1221. stringdispose(_mangledname);
  1222. {$if defined(jvm)}
  1223. internalerror(2011011202);
  1224. {$elseif defined(compress)}
  1225. _mangledname:=stringdup(minilzw_encode(s));
  1226. {$else}
  1227. _mangledname:=stringdup(s);
  1228. {$endif}
  1229. include(varoptions,vo_has_mangledname);
  1230. end;
  1231. {****************************************************************************
  1232. TLOCALVARSYM
  1233. ****************************************************************************}
  1234. constructor tlocalvarsym.create(const n : string;vsp:tvarspez;def:tdef;vopts:tvaroptions);
  1235. begin
  1236. inherited create(localvarsym,n,vsp,def,vopts);
  1237. end;
  1238. constructor tlocalvarsym.ppuload(ppufile:tcompilerppufile);
  1239. begin
  1240. inherited ppuload(localvarsym,ppufile);
  1241. end;
  1242. procedure tlocalvarsym.ppuwrite(ppufile:tcompilerppufile);
  1243. begin
  1244. inherited ppuwrite(ppufile);
  1245. ppufile.writeentry(iblocalvarsym);
  1246. end;
  1247. {****************************************************************************
  1248. TPARAVARSYM
  1249. ****************************************************************************}
  1250. constructor tparavarsym.create(const n : string;nr:word;vsp:tvarspez;def:tdef;vopts:tvaroptions);
  1251. begin
  1252. inherited create(paravarsym,n,vsp,def,vopts);
  1253. if (vsp in [vs_var,vs_value,vs_const,vs_constref]) then
  1254. varstate := vs_initialised;
  1255. paranr:=nr;
  1256. paraloc[calleeside].init;
  1257. paraloc[callerside].init;
  1258. end;
  1259. destructor tparavarsym.destroy;
  1260. begin
  1261. paraloc[calleeside].done;
  1262. paraloc[callerside].done;
  1263. inherited destroy;
  1264. end;
  1265. constructor tparavarsym.ppuload(ppufile:tcompilerppufile);
  1266. var
  1267. b : byte;
  1268. begin
  1269. inherited ppuload(paravarsym,ppufile);
  1270. paranr:=ppufile.getword;
  1271. univpara:=boolean(ppufile.getbyte);
  1272. { The var state of parameter symbols is fixed after writing them so
  1273. we write them to the unit file.
  1274. This enables constant folding for inline procedures loaded from units
  1275. }
  1276. varstate:=tvarstate(ppufile.getbyte);
  1277. paraloc[calleeside].init;
  1278. paraloc[callerside].init;
  1279. if vo_has_explicit_paraloc in varoptions then
  1280. begin
  1281. paraloc[callerside].alignment:=ppufile.getbyte;
  1282. b:=ppufile.getbyte;
  1283. if b<>sizeof(paraloc[callerside].location^) then
  1284. internalerror(200411154);
  1285. ppufile.getdata(paraloc[callerside].add_location^,sizeof(paraloc[callerside].location^));
  1286. paraloc[callerside].size:=paraloc[callerside].location^.size;
  1287. paraloc[callerside].intsize:=tcgsize2size[paraloc[callerside].size];
  1288. end;
  1289. end;
  1290. procedure tparavarsym.ppuwrite(ppufile:tcompilerppufile);
  1291. var
  1292. oldintfcrc : boolean;
  1293. begin
  1294. inherited ppuwrite(ppufile);
  1295. ppufile.putword(paranr);
  1296. ppufile.putbyte(byte(univpara));
  1297. { The var state of parameter symbols is fixed after writing them so
  1298. we write them to the unit file.
  1299. This enables constant folding for inline procedures loaded from units
  1300. }
  1301. oldintfcrc:=ppufile.do_crc;
  1302. ppufile.do_crc:=false;
  1303. ppufile.putbyte(ord(varstate));
  1304. ppufile.do_crc:=oldintfcrc;
  1305. if vo_has_explicit_paraloc in varoptions then
  1306. begin
  1307. paraloc[callerside].check_simple_location;
  1308. ppufile.putbyte(sizeof(paraloc[callerside].alignment));
  1309. ppufile.putbyte(sizeof(paraloc[callerside].location^));
  1310. ppufile.putdata(paraloc[callerside].location^,sizeof(paraloc[callerside].location^));
  1311. end;
  1312. ppufile.writeentry(ibparavarsym);
  1313. end;
  1314. {****************************************************************************
  1315. TABSOLUTEVARSYM
  1316. ****************************************************************************}
  1317. constructor tabsolutevarsym.create(const n : string;def:tdef);
  1318. begin
  1319. inherited create(absolutevarsym,n,vs_value,def,[]);
  1320. ref:=nil;
  1321. end;
  1322. constructor tabsolutevarsym.create_ref(const n : string;def:tdef;_ref:tpropaccesslist);
  1323. begin
  1324. inherited create(absolutevarsym,n,vs_value,def,[]);
  1325. ref:=_ref;
  1326. end;
  1327. destructor tabsolutevarsym.destroy;
  1328. begin
  1329. if assigned(ref) then
  1330. ref.free;
  1331. inherited destroy;
  1332. end;
  1333. constructor tabsolutevarsym.ppuload(ppufile:tcompilerppufile);
  1334. begin
  1335. inherited ppuload(absolutevarsym,ppufile);
  1336. ref:=nil;
  1337. asmname:=nil;
  1338. abstyp:=absolutetyp(ppufile.getbyte);
  1339. {$ifdef i386}
  1340. absseg:=false;
  1341. {$endif i386}
  1342. case abstyp of
  1343. tovar :
  1344. ref:=ppufile.getpropaccesslist;
  1345. toasm :
  1346. asmname:=stringdup(ppufile.getstring);
  1347. toaddr :
  1348. begin
  1349. addroffset:=ppufile.getaword;
  1350. {$ifdef i386}
  1351. absseg:=boolean(ppufile.getbyte);
  1352. {$endif i386}
  1353. end;
  1354. end;
  1355. end;
  1356. procedure tabsolutevarsym.ppuwrite(ppufile:tcompilerppufile);
  1357. begin
  1358. inherited ppuwrite(ppufile);
  1359. ppufile.putbyte(byte(abstyp));
  1360. case abstyp of
  1361. tovar :
  1362. ppufile.putpropaccesslist(ref);
  1363. toasm :
  1364. ppufile.putstring(asmname^);
  1365. toaddr :
  1366. begin
  1367. ppufile.putaword(addroffset);
  1368. {$ifdef i386}
  1369. ppufile.putbyte(byte(absseg));
  1370. {$endif i386}
  1371. end;
  1372. end;
  1373. ppufile.writeentry(ibabsolutevarsym);
  1374. end;
  1375. procedure tabsolutevarsym.buildderef;
  1376. begin
  1377. inherited buildderef;
  1378. if (abstyp=tovar) then
  1379. ref.buildderef;
  1380. end;
  1381. procedure tabsolutevarsym.deref;
  1382. begin
  1383. inherited deref;
  1384. { own absolute deref }
  1385. if (abstyp=tovar) then
  1386. ref.resolve;
  1387. end;
  1388. function tabsolutevarsym.mangledname : string;
  1389. begin
  1390. case abstyp of
  1391. toasm :
  1392. mangledname:=asmname^;
  1393. toaddr :
  1394. mangledname:='$'+tostr(addroffset);
  1395. else
  1396. internalerror(200411062);
  1397. end;
  1398. end;
  1399. {****************************************************************************
  1400. TCONSTSYM
  1401. ****************************************************************************}
  1402. constructor tconstsym.create_ord(const n : string;t : tconsttyp;v : tconstexprint;def:tdef);
  1403. begin
  1404. inherited create(constsym,n);
  1405. fillchar(value, sizeof(value), #0);
  1406. consttyp:=t;
  1407. value.valueord:=v;
  1408. constdef:=def;
  1409. end;
  1410. constructor tconstsym.create_ordptr(const n : string;t : tconsttyp;v : tconstptruint;def:tdef);
  1411. begin
  1412. inherited create(constsym,n);
  1413. fillchar(value, sizeof(value), #0);
  1414. consttyp:=t;
  1415. value.valueordptr:=v;
  1416. constdef:=def;
  1417. end;
  1418. constructor tconstsym.create_ptr(const n : string;t : tconsttyp;v : pointer;def:tdef);
  1419. begin
  1420. inherited create(constsym,n);
  1421. fillchar(value, sizeof(value), #0);
  1422. consttyp:=t;
  1423. value.valueptr:=v;
  1424. constdef:=def;
  1425. end;
  1426. constructor tconstsym.create_string(const n : string;t : tconsttyp;str:pchar;l:longint);
  1427. begin
  1428. inherited create(constsym,n);
  1429. fillchar(value, sizeof(value), #0);
  1430. consttyp:=t;
  1431. value.valueptr:=str;
  1432. constdef:=nil;
  1433. value.len:=l;
  1434. end;
  1435. constructor tconstsym.create_wstring(const n : string;t : tconsttyp;pw:pcompilerwidestring);
  1436. begin
  1437. inherited create(constsym,n);
  1438. fillchar(value, sizeof(value), #0);
  1439. consttyp:=t;
  1440. pcompilerwidestring(value.valueptr):=pw;
  1441. constdef:=nil;
  1442. value.len:=getlengthwidestring(pw);
  1443. end;
  1444. constructor tconstsym.ppuload(ppufile:tcompilerppufile);
  1445. var
  1446. pd : pbestreal;
  1447. ps : pnormalset;
  1448. pc : pchar;
  1449. pw : pcompilerwidestring;
  1450. i : longint;
  1451. begin
  1452. inherited ppuload(constsym,ppufile);
  1453. constdef:=nil;
  1454. consttyp:=tconsttyp(ppufile.getbyte);
  1455. fillchar(value, sizeof(value), #0);
  1456. case consttyp of
  1457. constord :
  1458. begin
  1459. ppufile.getderef(constdefderef);
  1460. value.valueord:=ppufile.getexprint;
  1461. end;
  1462. constpointer :
  1463. begin
  1464. ppufile.getderef(constdefderef);
  1465. value.valueordptr:=ppufile.getptruint;
  1466. end;
  1467. constwstring :
  1468. begin
  1469. initwidestring(pw);
  1470. setlengthwidestring(pw,ppufile.getlongint);
  1471. { don't use getdata, because the compilerwidechars may have to
  1472. be byteswapped
  1473. }
  1474. {$if sizeof(tcompilerwidechar) = 2}
  1475. for i:=0 to pw^.len-1 do
  1476. pw^.data[i]:=ppufile.getword;
  1477. {$elseif sizeof(tcompilerwidechar) = 4}
  1478. for i:=0 to pw^.len-1 do
  1479. pw^.data[i]:=cardinal(ppufile.getlongint);
  1480. {$else}
  1481. {$error Unsupported tcompilerwidechar size}
  1482. {$endif}
  1483. pcompilerwidestring(value.valueptr):=pw;
  1484. end;
  1485. conststring,
  1486. constresourcestring :
  1487. begin
  1488. value.len:=ppufile.getlongint;
  1489. getmem(pc,value.len+1);
  1490. ppufile.getdata(pc^,value.len);
  1491. pc[value.len]:=#0;
  1492. value.valueptr:=pc;
  1493. end;
  1494. constreal :
  1495. begin
  1496. new(pd);
  1497. pd^:=ppufile.getreal;
  1498. value.valueptr:=pd;
  1499. end;
  1500. constset :
  1501. begin
  1502. ppufile.getderef(constdefderef);
  1503. new(ps);
  1504. ppufile.getnormalset(ps^);
  1505. value.valueptr:=ps;
  1506. end;
  1507. constguid :
  1508. begin
  1509. new(pguid(value.valueptr));
  1510. ppufile.getdata(value.valueptr^,sizeof(tguid));
  1511. end;
  1512. constnil : ;
  1513. else
  1514. Message1(unit_f_ppu_invalid_entry,tostr(ord(consttyp)));
  1515. end;
  1516. end;
  1517. destructor tconstsym.destroy;
  1518. begin
  1519. case consttyp of
  1520. conststring,
  1521. constresourcestring :
  1522. freemem(pchar(value.valueptr),value.len+1);
  1523. constwstring :
  1524. donewidestring(pcompilerwidestring(value.valueptr));
  1525. constreal :
  1526. dispose(pbestreal(value.valueptr));
  1527. constset :
  1528. dispose(pnormalset(value.valueptr));
  1529. constguid :
  1530. dispose(pguid(value.valueptr));
  1531. end;
  1532. inherited destroy;
  1533. end;
  1534. procedure tconstsym.buildderef;
  1535. begin
  1536. if consttyp in [constord,constpointer,constset] then
  1537. constdefderef.build(constdef);
  1538. end;
  1539. procedure tconstsym.deref;
  1540. begin
  1541. if consttyp in [constord,constpointer,constset] then
  1542. constdef:=tdef(constdefderef.resolve);
  1543. end;
  1544. procedure tconstsym.ppuwrite(ppufile:tcompilerppufile);
  1545. begin
  1546. inherited ppuwrite(ppufile);
  1547. ppufile.putbyte(byte(consttyp));
  1548. case consttyp of
  1549. constnil : ;
  1550. constord :
  1551. begin
  1552. ppufile.putderef(constdefderef);
  1553. ppufile.putexprint(value.valueord);
  1554. end;
  1555. constpointer :
  1556. begin
  1557. ppufile.putderef(constdefderef);
  1558. ppufile.putptruint(value.valueordptr);
  1559. end;
  1560. constwstring :
  1561. begin
  1562. ppufile.putlongint(getlengthwidestring(pcompilerwidestring(value.valueptr)));
  1563. ppufile.putdata(pcompilerwidestring(value.valueptr)^.data^,pcompilerwidestring(value.valueptr)^.len*sizeof(tcompilerwidechar));
  1564. end;
  1565. conststring,
  1566. constresourcestring :
  1567. begin
  1568. ppufile.putlongint(value.len);
  1569. ppufile.putdata(pchar(value.valueptr)^,value.len);
  1570. end;
  1571. constreal :
  1572. ppufile.putreal(pbestreal(value.valueptr)^);
  1573. constset :
  1574. begin
  1575. ppufile.putderef(constdefderef);
  1576. ppufile.putnormalset(value.valueptr^);
  1577. end;
  1578. constguid :
  1579. ppufile.putdata(value.valueptr^,sizeof(tguid));
  1580. else
  1581. internalerror(13);
  1582. end;
  1583. ppufile.writeentry(ibconstsym);
  1584. end;
  1585. {****************************************************************************
  1586. TENUMSYM
  1587. ****************************************************************************}
  1588. constructor tenumsym.create(const n : string;def : tenumdef;v : longint);
  1589. begin
  1590. inherited create(enumsym,n);
  1591. definition:=def;
  1592. value:=v;
  1593. end;
  1594. constructor tenumsym.ppuload(ppufile:tcompilerppufile);
  1595. begin
  1596. inherited ppuload(enumsym,ppufile);
  1597. ppufile.getderef(definitionderef);
  1598. value:=ppufile.getlongint;
  1599. end;
  1600. procedure tenumsym.buildderef;
  1601. begin
  1602. definitionderef.build(definition);
  1603. end;
  1604. procedure tenumsym.deref;
  1605. begin
  1606. definition:=tenumdef(definitionderef.resolve);
  1607. end;
  1608. procedure tenumsym.ppuwrite(ppufile:tcompilerppufile);
  1609. begin
  1610. inherited ppuwrite(ppufile);
  1611. ppufile.putderef(definitionderef);
  1612. ppufile.putlongint(value);
  1613. ppufile.writeentry(ibenumsym);
  1614. end;
  1615. {****************************************************************************
  1616. TTYPESYM
  1617. ****************************************************************************}
  1618. constructor ttypesym.create(const n : string;def:tdef);
  1619. begin
  1620. inherited create(typesym,n);
  1621. typedef:=def;
  1622. { register the typesym for the definition }
  1623. if assigned(typedef) and
  1624. (typedef.typ<>errordef) and
  1625. not(assigned(typedef.typesym)) then
  1626. typedef.typesym:=self;
  1627. end;
  1628. constructor ttypesym.ppuload(ppufile:tcompilerppufile);
  1629. begin
  1630. inherited ppuload(typesym,ppufile);
  1631. ppufile.getderef(typedefderef);
  1632. fprettyname:=ppufile.getansistring;
  1633. end;
  1634. procedure ttypesym.buildderef;
  1635. begin
  1636. typedefderef.build(typedef);
  1637. end;
  1638. procedure ttypesym.deref;
  1639. begin
  1640. typedef:=tdef(typedefderef.resolve);
  1641. end;
  1642. procedure ttypesym.ppuwrite(ppufile:tcompilerppufile);
  1643. begin
  1644. inherited ppuwrite(ppufile);
  1645. ppufile.putderef(typedefderef);
  1646. ppufile.putansistring(fprettyname);
  1647. ppufile.writeentry(ibtypesym);
  1648. end;
  1649. function ttypesym.prettyname : string;
  1650. begin
  1651. if fprettyname<>'' then
  1652. result:=fprettyname
  1653. else
  1654. result:=inherited prettyname;
  1655. end;
  1656. {****************************************************************************
  1657. TSYSSYM
  1658. ****************************************************************************}
  1659. constructor tsyssym.create(const n : string;l : longint);
  1660. begin
  1661. inherited create(syssym,n);
  1662. number:=l;
  1663. end;
  1664. constructor tsyssym.ppuload(ppufile:tcompilerppufile);
  1665. begin
  1666. inherited ppuload(syssym,ppufile);
  1667. number:=ppufile.getlongint;
  1668. end;
  1669. destructor tsyssym.destroy;
  1670. begin
  1671. inherited destroy;
  1672. end;
  1673. procedure tsyssym.ppuwrite(ppufile:tcompilerppufile);
  1674. begin
  1675. inherited ppuwrite(ppufile);
  1676. ppufile.putlongint(number);
  1677. ppufile.writeentry(ibsyssym);
  1678. end;
  1679. {*****************************************************************************
  1680. TMacro
  1681. *****************************************************************************}
  1682. constructor tmacro.create(const n : string);
  1683. begin
  1684. inherited create(macrosym,n);
  1685. owner:=nil;
  1686. defined:=false;
  1687. is_used:=false;
  1688. is_compiler_var:=false;
  1689. buftext:=nil;
  1690. buflen:=0;
  1691. end;
  1692. constructor tmacro.ppuload(ppufile:tcompilerppufile);
  1693. begin
  1694. inherited ppuload(macrosym,ppufile);
  1695. defined:=boolean(ppufile.getbyte);
  1696. is_compiler_var:=boolean(ppufile.getbyte);
  1697. is_used:=false;
  1698. buflen:= ppufile.getlongint;
  1699. if buflen > 0 then
  1700. begin
  1701. getmem(buftext, buflen);
  1702. ppufile.getdata(buftext^, buflen)
  1703. end
  1704. else
  1705. buftext:=nil;
  1706. end;
  1707. destructor tmacro.destroy;
  1708. begin
  1709. if assigned(buftext) then
  1710. freemem(buftext);
  1711. inherited destroy;
  1712. end;
  1713. procedure tmacro.ppuwrite(ppufile:tcompilerppufile);
  1714. begin
  1715. inherited ppuwrite(ppufile);
  1716. ppufile.putbyte(byte(defined));
  1717. ppufile.putbyte(byte(is_compiler_var));
  1718. ppufile.putlongint(buflen);
  1719. if buflen > 0 then
  1720. ppufile.putdata(buftext^,buflen);
  1721. ppufile.writeentry(ibmacrosym);
  1722. end;
  1723. function tmacro.GetCopy:tmacro;
  1724. var
  1725. p : tmacro;
  1726. begin
  1727. p:=tmacro.create(realname);
  1728. p.defined:=defined;
  1729. p.is_used:=is_used;
  1730. p.is_compiler_var:=is_compiler_var;
  1731. p.buflen:=buflen;
  1732. if assigned(buftext) then
  1733. begin
  1734. getmem(p.buftext,buflen);
  1735. move(buftext^,p.buftext^,buflen);
  1736. end;
  1737. Result:=p;
  1738. end;
  1739. end.