symsym.pas 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985
  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. objcoffsetmangledname: pshortstring; { mangled name of offset, calculated as needed }
  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,
  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. if sp_static in symoptions then
  1066. begin
  1067. if searchsym(lower(owner.name^)+'_'+name,srsym,srsymtable) then
  1068. result:=srsym.mangledname
  1069. { when generating the debug info for the module in which the }
  1070. { symbol is defined, the localsymtable of that module is }
  1071. { already popped from the symtablestack }
  1072. else if searchsym_in_module(current_module,lower(owner.name^)+'_'+name,srsym,srsymtable) then
  1073. result:=srsym.mangledname
  1074. else
  1075. internalerror(2007012501);
  1076. end
  1077. else if is_objcclass(tdef(owner.defowner)) then
  1078. begin
  1079. if assigned(objcoffsetmangledname) then
  1080. result:=objcoffsetmangledname^
  1081. else
  1082. begin
  1083. result:=target_info.cprefix+'OBJC_IVAR_$_'+tobjectdef(owner.defowner).objextname^+'.'+RealName;
  1084. objcoffsetmangledname:=stringdup(result);
  1085. end;
  1086. end
  1087. else
  1088. result:=inherited mangledname;
  1089. end;
  1090. destructor tfieldvarsym.destroy;
  1091. begin
  1092. stringdispose(objcoffsetmangledname);
  1093. inherited destroy;
  1094. end;
  1095. {****************************************************************************
  1096. TABSTRACTNORMALVARSYM
  1097. ****************************************************************************}
  1098. constructor tabstractnormalvarsym.create(st:tsymtyp;const n : string;vsp:tvarspez;def:tdef;vopts:tvaroptions);
  1099. begin
  1100. inherited create(st,n,vsp,def,vopts);
  1101. fillchar(localloc,sizeof(localloc),0);
  1102. fillchar(initialloc,sizeof(initialloc),0);
  1103. defaultconstsym:=nil;
  1104. end;
  1105. constructor tabstractnormalvarsym.ppuload(st:tsymtyp;ppufile:tcompilerppufile);
  1106. begin
  1107. inherited ppuload(st,ppufile);
  1108. fillchar(localloc,sizeof(localloc),0);
  1109. fillchar(initialloc,sizeof(initialloc),0);
  1110. ppufile.getderef(defaultconstsymderef);
  1111. end;
  1112. procedure tabstractnormalvarsym.buildderef;
  1113. begin
  1114. inherited buildderef;
  1115. defaultconstsymderef.build(defaultconstsym);
  1116. end;
  1117. procedure tabstractnormalvarsym.deref;
  1118. begin
  1119. inherited deref;
  1120. defaultconstsym:=tsym(defaultconstsymderef.resolve);
  1121. end;
  1122. procedure tabstractnormalvarsym.ppuwrite(ppufile:tcompilerppufile);
  1123. begin
  1124. inherited ppuwrite(ppufile);
  1125. ppufile.putderef(defaultconstsymderef);
  1126. end;
  1127. {****************************************************************************
  1128. Tstaticvarsym
  1129. ****************************************************************************}
  1130. constructor tstaticvarsym.create(const n : string;vsp:tvarspez;def:tdef;vopts:tvaroptions);
  1131. begin
  1132. inherited create(staticvarsym,n,vsp,def,vopts);
  1133. _mangledname:=nil;
  1134. end;
  1135. constructor tstaticvarsym.create_dll(const n : string;vsp:tvarspez;def:tdef);
  1136. begin
  1137. tstaticvarsym(self).create(n,vsp,def,[vo_is_dll_var]);
  1138. end;
  1139. constructor tstaticvarsym.create_C(const n,mangled : string;vsp:tvarspez;def:tdef);
  1140. begin
  1141. tstaticvarsym(self).create(n,vsp,def,[]);
  1142. set_mangledname(mangled);
  1143. end;
  1144. constructor tstaticvarsym.ppuload(ppufile:tcompilerppufile);
  1145. begin
  1146. inherited ppuload(staticvarsym,ppufile);
  1147. if vo_has_mangledname in varoptions then
  1148. _mangledname:=stringdup(ppufile.getstring)
  1149. else
  1150. _mangledname:=nil;
  1151. if vo_has_section in varoptions then
  1152. section:=ppufile.getansistring;
  1153. end;
  1154. destructor tstaticvarsym.destroy;
  1155. begin
  1156. if assigned(_mangledname) then
  1157. begin
  1158. {$ifdef MEMDEBUG}
  1159. memmanglednames.start;
  1160. {$endif MEMDEBUG}
  1161. stringdispose(_mangledname);
  1162. {$ifdef MEMDEBUG}
  1163. memmanglednames.stop;
  1164. {$endif MEMDEBUG}
  1165. end;
  1166. inherited destroy;
  1167. end;
  1168. procedure tstaticvarsym.ppuwrite(ppufile:tcompilerppufile);
  1169. begin
  1170. inherited ppuwrite(ppufile);
  1171. if vo_has_mangledname in varoptions then
  1172. ppufile.putstring(_mangledname^);
  1173. if vo_has_section in varoptions then
  1174. ppufile.putansistring(section);
  1175. ppufile.writeentry(ibstaticvarsym);
  1176. end;
  1177. function tstaticvarsym.mangledname:string;
  1178. var
  1179. prefix : string[2];
  1180. begin
  1181. if not assigned(_mangledname) then
  1182. begin
  1183. if (vo_is_typed_const in varoptions) then
  1184. prefix:='TC'
  1185. else
  1186. prefix:='U';
  1187. {$ifdef compress}
  1188. _mangledname:=stringdup(minilzw_encode(make_mangledname(prefix,owner,name)));
  1189. {$else}
  1190. _mangledname:=stringdup(make_mangledname(prefix,owner,name));
  1191. {$endif}
  1192. end;
  1193. result:=_mangledname^;
  1194. end;
  1195. procedure tstaticvarsym.set_mangledname(const s:string);
  1196. begin
  1197. stringdispose(_mangledname);
  1198. {$ifdef compress}
  1199. _mangledname:=stringdup(minilzw_encode(s));
  1200. {$else}
  1201. _mangledname:=stringdup(s);
  1202. {$endif}
  1203. include(varoptions,vo_has_mangledname);
  1204. end;
  1205. {****************************************************************************
  1206. TLOCALVARSYM
  1207. ****************************************************************************}
  1208. constructor tlocalvarsym.create(const n : string;vsp:tvarspez;def:tdef;vopts:tvaroptions);
  1209. begin
  1210. inherited create(localvarsym,n,vsp,def,vopts);
  1211. end;
  1212. constructor tlocalvarsym.ppuload(ppufile:tcompilerppufile);
  1213. begin
  1214. inherited ppuload(localvarsym,ppufile);
  1215. end;
  1216. procedure tlocalvarsym.ppuwrite(ppufile:tcompilerppufile);
  1217. begin
  1218. inherited ppuwrite(ppufile);
  1219. ppufile.writeentry(iblocalvarsym);
  1220. end;
  1221. {****************************************************************************
  1222. TPARAVARSYM
  1223. ****************************************************************************}
  1224. constructor tparavarsym.create(const n : string;nr:word;vsp:tvarspez;def:tdef;vopts:tvaroptions);
  1225. begin
  1226. inherited create(paravarsym,n,vsp,def,vopts);
  1227. if (vsp in [vs_var,vs_value,vs_const,vs_constref]) then
  1228. varstate := vs_initialised;
  1229. paranr:=nr;
  1230. paraloc[calleeside].init;
  1231. paraloc[callerside].init;
  1232. end;
  1233. destructor tparavarsym.destroy;
  1234. begin
  1235. paraloc[calleeside].done;
  1236. paraloc[callerside].done;
  1237. inherited destroy;
  1238. end;
  1239. constructor tparavarsym.ppuload(ppufile:tcompilerppufile);
  1240. var
  1241. b : byte;
  1242. begin
  1243. inherited ppuload(paravarsym,ppufile);
  1244. paranr:=ppufile.getword;
  1245. univpara:=boolean(ppufile.getbyte);
  1246. { The var state of parameter symbols is fixed after writing them so
  1247. we write them to the unit file.
  1248. This enables constant folding for inline procedures loaded from units
  1249. }
  1250. varstate:=tvarstate(ppufile.getbyte);
  1251. paraloc[calleeside].init;
  1252. paraloc[callerside].init;
  1253. if vo_has_explicit_paraloc in varoptions then
  1254. begin
  1255. paraloc[callerside].alignment:=ppufile.getbyte;
  1256. b:=ppufile.getbyte;
  1257. if b<>sizeof(paraloc[callerside].location^) then
  1258. internalerror(200411154);
  1259. ppufile.getdata(paraloc[callerside].add_location^,sizeof(paraloc[callerside].location^));
  1260. paraloc[callerside].size:=paraloc[callerside].location^.size;
  1261. paraloc[callerside].intsize:=tcgsize2size[paraloc[callerside].size];
  1262. end;
  1263. end;
  1264. procedure tparavarsym.ppuwrite(ppufile:tcompilerppufile);
  1265. var
  1266. oldintfcrc : boolean;
  1267. begin
  1268. inherited ppuwrite(ppufile);
  1269. ppufile.putword(paranr);
  1270. ppufile.putbyte(byte(univpara));
  1271. { The var state of parameter symbols is fixed after writing them so
  1272. we write them to the unit file.
  1273. This enables constant folding for inline procedures loaded from units
  1274. }
  1275. oldintfcrc:=ppufile.do_crc;
  1276. ppufile.do_crc:=false;
  1277. ppufile.putbyte(ord(varstate));
  1278. ppufile.do_crc:=oldintfcrc;
  1279. if vo_has_explicit_paraloc in varoptions then
  1280. begin
  1281. paraloc[callerside].check_simple_location;
  1282. ppufile.putbyte(sizeof(paraloc[callerside].alignment));
  1283. ppufile.putbyte(sizeof(paraloc[callerside].location^));
  1284. ppufile.putdata(paraloc[callerside].location^,sizeof(paraloc[callerside].location^));
  1285. end;
  1286. ppufile.writeentry(ibparavarsym);
  1287. end;
  1288. {****************************************************************************
  1289. TABSOLUTEVARSYM
  1290. ****************************************************************************}
  1291. constructor tabsolutevarsym.create(const n : string;def:tdef);
  1292. begin
  1293. inherited create(absolutevarsym,n,vs_value,def,[]);
  1294. ref:=nil;
  1295. end;
  1296. constructor tabsolutevarsym.create_ref(const n : string;def:tdef;_ref:tpropaccesslist);
  1297. begin
  1298. inherited create(absolutevarsym,n,vs_value,def,[]);
  1299. ref:=_ref;
  1300. end;
  1301. destructor tabsolutevarsym.destroy;
  1302. begin
  1303. if assigned(ref) then
  1304. ref.free;
  1305. inherited destroy;
  1306. end;
  1307. constructor tabsolutevarsym.ppuload(ppufile:tcompilerppufile);
  1308. begin
  1309. inherited ppuload(absolutevarsym,ppufile);
  1310. ref:=nil;
  1311. asmname:=nil;
  1312. abstyp:=absolutetyp(ppufile.getbyte);
  1313. {$ifdef i386}
  1314. absseg:=false;
  1315. {$endif i386}
  1316. case abstyp of
  1317. tovar :
  1318. ref:=ppufile.getpropaccesslist;
  1319. toasm :
  1320. asmname:=stringdup(ppufile.getstring);
  1321. toaddr :
  1322. begin
  1323. addroffset:=ppufile.getaword;
  1324. {$ifdef i386}
  1325. absseg:=boolean(ppufile.getbyte);
  1326. {$endif i386}
  1327. end;
  1328. end;
  1329. end;
  1330. procedure tabsolutevarsym.ppuwrite(ppufile:tcompilerppufile);
  1331. begin
  1332. inherited ppuwrite(ppufile);
  1333. ppufile.putbyte(byte(abstyp));
  1334. case abstyp of
  1335. tovar :
  1336. ppufile.putpropaccesslist(ref);
  1337. toasm :
  1338. ppufile.putstring(asmname^);
  1339. toaddr :
  1340. begin
  1341. ppufile.putaword(addroffset);
  1342. {$ifdef i386}
  1343. ppufile.putbyte(byte(absseg));
  1344. {$endif i386}
  1345. end;
  1346. end;
  1347. ppufile.writeentry(ibabsolutevarsym);
  1348. end;
  1349. procedure tabsolutevarsym.buildderef;
  1350. begin
  1351. inherited buildderef;
  1352. if (abstyp=tovar) then
  1353. ref.buildderef;
  1354. end;
  1355. procedure tabsolutevarsym.deref;
  1356. begin
  1357. inherited deref;
  1358. { own absolute deref }
  1359. if (abstyp=tovar) then
  1360. ref.resolve;
  1361. end;
  1362. function tabsolutevarsym.mangledname : string;
  1363. begin
  1364. case abstyp of
  1365. toasm :
  1366. mangledname:=asmname^;
  1367. toaddr :
  1368. mangledname:='$'+tostr(addroffset);
  1369. else
  1370. internalerror(200411062);
  1371. end;
  1372. end;
  1373. {****************************************************************************
  1374. TCONSTSYM
  1375. ****************************************************************************}
  1376. constructor tconstsym.create_ord(const n : string;t : tconsttyp;v : tconstexprint;def:tdef);
  1377. begin
  1378. inherited create(constsym,n);
  1379. fillchar(value, sizeof(value), #0);
  1380. consttyp:=t;
  1381. value.valueord:=v;
  1382. constdef:=def;
  1383. end;
  1384. constructor tconstsym.create_ordptr(const n : string;t : tconsttyp;v : tconstptruint;def:tdef);
  1385. begin
  1386. inherited create(constsym,n);
  1387. fillchar(value, sizeof(value), #0);
  1388. consttyp:=t;
  1389. value.valueordptr:=v;
  1390. constdef:=def;
  1391. end;
  1392. constructor tconstsym.create_ptr(const n : string;t : tconsttyp;v : pointer;def:tdef);
  1393. begin
  1394. inherited create(constsym,n);
  1395. fillchar(value, sizeof(value), #0);
  1396. consttyp:=t;
  1397. value.valueptr:=v;
  1398. constdef:=def;
  1399. end;
  1400. constructor tconstsym.create_string(const n : string;t : tconsttyp;str:pchar;l:longint);
  1401. begin
  1402. inherited create(constsym,n);
  1403. fillchar(value, sizeof(value), #0);
  1404. consttyp:=t;
  1405. value.valueptr:=str;
  1406. constdef:=nil;
  1407. value.len:=l;
  1408. end;
  1409. constructor tconstsym.create_wstring(const n : string;t : tconsttyp;pw:pcompilerwidestring);
  1410. begin
  1411. inherited create(constsym,n);
  1412. fillchar(value, sizeof(value), #0);
  1413. consttyp:=t;
  1414. pcompilerwidestring(value.valueptr):=pw;
  1415. constdef:=nil;
  1416. value.len:=getlengthwidestring(pw);
  1417. end;
  1418. constructor tconstsym.ppuload(ppufile:tcompilerppufile);
  1419. var
  1420. pd : pbestreal;
  1421. ps : pnormalset;
  1422. pc : pchar;
  1423. pw : pcompilerwidestring;
  1424. i : longint;
  1425. begin
  1426. inherited ppuload(constsym,ppufile);
  1427. constdef:=nil;
  1428. consttyp:=tconsttyp(ppufile.getbyte);
  1429. fillchar(value, sizeof(value), #0);
  1430. case consttyp of
  1431. constord :
  1432. begin
  1433. ppufile.getderef(constdefderef);
  1434. value.valueord:=ppufile.getexprint;
  1435. end;
  1436. constpointer :
  1437. begin
  1438. ppufile.getderef(constdefderef);
  1439. value.valueordptr:=ppufile.getptruint;
  1440. end;
  1441. constwstring :
  1442. begin
  1443. initwidestring(pw);
  1444. setlengthwidestring(pw,ppufile.getlongint);
  1445. { don't use getdata, because the compilerwidechars may have to
  1446. be byteswapped
  1447. }
  1448. {$if sizeof(tcompilerwidechar) = 2}
  1449. for i:=0 to pw^.len-1 do
  1450. pw^.data[i]:=ppufile.getword;
  1451. {$elseif sizeof(tcompilerwidechar) = 4}
  1452. for i:=0 to pw^.len-1 do
  1453. pw^.data[i]:=cardinal(ppufile.getlongint);
  1454. {$else}
  1455. {$error Unsupported tcompilerwidechar size}
  1456. {$endif}
  1457. pcompilerwidestring(value.valueptr):=pw;
  1458. end;
  1459. conststring,
  1460. constresourcestring :
  1461. begin
  1462. value.len:=ppufile.getlongint;
  1463. getmem(pc,value.len+1);
  1464. ppufile.getdata(pc^,value.len);
  1465. pc[value.len]:=#0;
  1466. value.valueptr:=pc;
  1467. end;
  1468. constreal :
  1469. begin
  1470. new(pd);
  1471. pd^:=ppufile.getreal;
  1472. value.valueptr:=pd;
  1473. end;
  1474. constset :
  1475. begin
  1476. ppufile.getderef(constdefderef);
  1477. new(ps);
  1478. ppufile.getnormalset(ps^);
  1479. value.valueptr:=ps;
  1480. end;
  1481. constguid :
  1482. begin
  1483. new(pguid(value.valueptr));
  1484. ppufile.getdata(value.valueptr^,sizeof(tguid));
  1485. end;
  1486. constnil : ;
  1487. else
  1488. Message1(unit_f_ppu_invalid_entry,tostr(ord(consttyp)));
  1489. end;
  1490. end;
  1491. destructor tconstsym.destroy;
  1492. begin
  1493. case consttyp of
  1494. conststring,
  1495. constresourcestring :
  1496. freemem(pchar(value.valueptr),value.len+1);
  1497. constwstring :
  1498. donewidestring(pcompilerwidestring(value.valueptr));
  1499. constreal :
  1500. dispose(pbestreal(value.valueptr));
  1501. constset :
  1502. dispose(pnormalset(value.valueptr));
  1503. constguid :
  1504. dispose(pguid(value.valueptr));
  1505. end;
  1506. inherited destroy;
  1507. end;
  1508. procedure tconstsym.buildderef;
  1509. begin
  1510. if consttyp in [constord,constpointer,constset] then
  1511. constdefderef.build(constdef);
  1512. end;
  1513. procedure tconstsym.deref;
  1514. begin
  1515. if consttyp in [constord,constpointer,constset] then
  1516. constdef:=tdef(constdefderef.resolve);
  1517. end;
  1518. procedure tconstsym.ppuwrite(ppufile:tcompilerppufile);
  1519. begin
  1520. inherited ppuwrite(ppufile);
  1521. ppufile.putbyte(byte(consttyp));
  1522. case consttyp of
  1523. constnil : ;
  1524. constord :
  1525. begin
  1526. ppufile.putderef(constdefderef);
  1527. ppufile.putexprint(value.valueord);
  1528. end;
  1529. constpointer :
  1530. begin
  1531. ppufile.putderef(constdefderef);
  1532. ppufile.putptruint(value.valueordptr);
  1533. end;
  1534. constwstring :
  1535. begin
  1536. ppufile.putlongint(getlengthwidestring(pcompilerwidestring(value.valueptr)));
  1537. ppufile.putdata(pcompilerwidestring(value.valueptr)^.data^,pcompilerwidestring(value.valueptr)^.len*sizeof(tcompilerwidechar));
  1538. end;
  1539. conststring,
  1540. constresourcestring :
  1541. begin
  1542. ppufile.putlongint(value.len);
  1543. ppufile.putdata(pchar(value.valueptr)^,value.len);
  1544. end;
  1545. constreal :
  1546. ppufile.putreal(pbestreal(value.valueptr)^);
  1547. constset :
  1548. begin
  1549. ppufile.putderef(constdefderef);
  1550. ppufile.putnormalset(value.valueptr^);
  1551. end;
  1552. constguid :
  1553. ppufile.putdata(value.valueptr^,sizeof(tguid));
  1554. else
  1555. internalerror(13);
  1556. end;
  1557. ppufile.writeentry(ibconstsym);
  1558. end;
  1559. {****************************************************************************
  1560. TENUMSYM
  1561. ****************************************************************************}
  1562. constructor tenumsym.create(const n : string;def : tenumdef;v : longint);
  1563. begin
  1564. inherited create(enumsym,n);
  1565. definition:=def;
  1566. value:=v;
  1567. end;
  1568. constructor tenumsym.ppuload(ppufile:tcompilerppufile);
  1569. begin
  1570. inherited ppuload(enumsym,ppufile);
  1571. ppufile.getderef(definitionderef);
  1572. value:=ppufile.getlongint;
  1573. end;
  1574. procedure tenumsym.buildderef;
  1575. begin
  1576. definitionderef.build(definition);
  1577. end;
  1578. procedure tenumsym.deref;
  1579. begin
  1580. definition:=tenumdef(definitionderef.resolve);
  1581. end;
  1582. procedure tenumsym.ppuwrite(ppufile:tcompilerppufile);
  1583. begin
  1584. inherited ppuwrite(ppufile);
  1585. ppufile.putderef(definitionderef);
  1586. ppufile.putlongint(value);
  1587. ppufile.writeentry(ibenumsym);
  1588. end;
  1589. {****************************************************************************
  1590. TTYPESYM
  1591. ****************************************************************************}
  1592. constructor ttypesym.create(const n : string;def:tdef);
  1593. begin
  1594. inherited create(typesym,n);
  1595. typedef:=def;
  1596. { register the typesym for the definition }
  1597. if assigned(typedef) and
  1598. (typedef.typ<>errordef) and
  1599. not(assigned(typedef.typesym)) then
  1600. typedef.typesym:=self;
  1601. end;
  1602. constructor ttypesym.ppuload(ppufile:tcompilerppufile);
  1603. begin
  1604. inherited ppuload(typesym,ppufile);
  1605. ppufile.getderef(typedefderef);
  1606. fprettyname:=ppufile.getansistring;
  1607. end;
  1608. procedure ttypesym.buildderef;
  1609. begin
  1610. typedefderef.build(typedef);
  1611. end;
  1612. procedure ttypesym.deref;
  1613. begin
  1614. typedef:=tdef(typedefderef.resolve);
  1615. end;
  1616. procedure ttypesym.ppuwrite(ppufile:tcompilerppufile);
  1617. begin
  1618. inherited ppuwrite(ppufile);
  1619. ppufile.putderef(typedefderef);
  1620. ppufile.putansistring(fprettyname);
  1621. ppufile.writeentry(ibtypesym);
  1622. end;
  1623. function ttypesym.prettyname : string;
  1624. begin
  1625. if fprettyname<>'' then
  1626. result:=fprettyname
  1627. else
  1628. result:=inherited prettyname;
  1629. end;
  1630. {****************************************************************************
  1631. TSYSSYM
  1632. ****************************************************************************}
  1633. constructor tsyssym.create(const n : string;l : longint);
  1634. begin
  1635. inherited create(syssym,n);
  1636. number:=l;
  1637. end;
  1638. constructor tsyssym.ppuload(ppufile:tcompilerppufile);
  1639. begin
  1640. inherited ppuload(syssym,ppufile);
  1641. number:=ppufile.getlongint;
  1642. end;
  1643. destructor tsyssym.destroy;
  1644. begin
  1645. inherited destroy;
  1646. end;
  1647. procedure tsyssym.ppuwrite(ppufile:tcompilerppufile);
  1648. begin
  1649. inherited ppuwrite(ppufile);
  1650. ppufile.putlongint(number);
  1651. ppufile.writeentry(ibsyssym);
  1652. end;
  1653. {*****************************************************************************
  1654. TMacro
  1655. *****************************************************************************}
  1656. constructor tmacro.create(const n : string);
  1657. begin
  1658. inherited create(macrosym,n);
  1659. owner:=nil;
  1660. defined:=false;
  1661. is_used:=false;
  1662. is_compiler_var:=false;
  1663. buftext:=nil;
  1664. buflen:=0;
  1665. end;
  1666. constructor tmacro.ppuload(ppufile:tcompilerppufile);
  1667. begin
  1668. inherited ppuload(macrosym,ppufile);
  1669. defined:=boolean(ppufile.getbyte);
  1670. is_compiler_var:=boolean(ppufile.getbyte);
  1671. is_used:=false;
  1672. buflen:= ppufile.getlongint;
  1673. if buflen > 0 then
  1674. begin
  1675. getmem(buftext, buflen);
  1676. ppufile.getdata(buftext^, buflen)
  1677. end
  1678. else
  1679. buftext:=nil;
  1680. end;
  1681. destructor tmacro.destroy;
  1682. begin
  1683. if assigned(buftext) then
  1684. freemem(buftext);
  1685. inherited destroy;
  1686. end;
  1687. procedure tmacro.ppuwrite(ppufile:tcompilerppufile);
  1688. begin
  1689. inherited ppuwrite(ppufile);
  1690. ppufile.putbyte(byte(defined));
  1691. ppufile.putbyte(byte(is_compiler_var));
  1692. ppufile.putlongint(buflen);
  1693. if buflen > 0 then
  1694. ppufile.putdata(buftext^,buflen);
  1695. ppufile.writeentry(ibmacrosym);
  1696. end;
  1697. function tmacro.GetCopy:tmacro;
  1698. var
  1699. p : tmacro;
  1700. begin
  1701. p:=tmacro.create(realname);
  1702. p.defined:=defined;
  1703. p.is_used:=is_used;
  1704. p.is_compiler_var:=is_compiler_var;
  1705. p.buflen:=buflen;
  1706. if assigned(buftext) then
  1707. begin
  1708. getmem(p.buftext,buflen);
  1709. move(buftext^,p.buftext^,buflen);
  1710. end;
  1711. Result:=p;
  1712. end;
  1713. end.