symsym.pas 61 KB

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