procdefutil.pas 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751
  1. {
  2. Copyright (c) 2018 by Jonas Maebe
  3. Copyright (c) 2011-2021 by Blaise.ru
  4. This unit provides helpers for creating procdefs
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. {$i fpcdefs.inc}
  19. unit procdefutil;
  20. interface
  21. uses
  22. globtype,procinfo,
  23. symconst,symtype,symdef,
  24. node,nbas;
  25. { create a nested procdef that will be used to outline code from a procedure;
  26. astruct should usually be nil, except in special cases like the Windows SEH
  27. exception handling funclets }
  28. function create_outline_procdef(const basesymname: string; astruct: tabstractrecorddef; potype: tproctypeoption; resultdef: tdef): tprocdef;
  29. procedure convert_to_funcref_intf(const n:tidstring;var def:tdef);
  30. function adjust_funcref(var def:tdef;sym,dummysym:tsym):boolean;
  31. { functionality related to capturing local variables for anonymous functions }
  32. function get_or_create_capturer(pd:tprocdef):tsym;
  33. function capturer_add_anonymous_proc(owner:tprocinfo;pd:tprocdef;out capturer:tsym):tobjectdef;
  34. function capturer_add_procvar_or_proc(owner:tprocinfo;n:tnode;out capturer:tsym;out capturen:tnode):tobjectdef;
  35. procedure initialize_capturer(ctx:tprocinfo;var stmt:tstatementnode);
  36. procedure postprocess_capturer(ctx:tprocinfo);
  37. procedure convert_captured_syms(pd:tprocdef;tree:tnode);
  38. implementation
  39. uses
  40. cutils,cclasses,verbose,globals,
  41. fmodule,
  42. pass_1,
  43. nobj,ncal,nmem,nld,nutils,
  44. ngenutil,
  45. symbase,symsym,symtable,defutil,defcmp,
  46. htypechk,
  47. pparautl,psub;
  48. function create_outline_procdef(const basesymname: string; astruct: tabstractrecorddef; potype: tproctypeoption; resultdef: tdef): tprocdef;
  49. var
  50. st:TSymTable;
  51. checkstack: psymtablestackitem;
  52. oldsymtablestack: tsymtablestack;
  53. sym:tprocsym;
  54. begin
  55. { get actual procedure symtable (skip withsymtables, etc.) }
  56. st:=nil;
  57. checkstack:=symtablestack.stack;
  58. while assigned(checkstack) do
  59. begin
  60. st:=checkstack^.symtable;
  61. if st.symtabletype in [staticsymtable,globalsymtable,localsymtable] then
  62. break;
  63. checkstack:=checkstack^.next;
  64. end;
  65. { Create a nested procedure, even from main_program_level.
  66. Furthermore, force procdef and procsym into the same symtable
  67. (by default, defs are registered with symtablestack.top which may be
  68. something temporary like exceptsymtable - in that case, procdef can be
  69. destroyed before procsym, leaving invalid pointers). }
  70. oldsymtablestack:=symtablestack;
  71. symtablestack:=nil;
  72. result:=cprocdef.create(max(normal_function_level,st.symtablelevel)+1,true);
  73. result.returndef:=resultdef;
  74. { if the parent is a generic or a specialization, the new function is one
  75. as well }
  76. if st.symtabletype=localsymtable then
  77. result.defoptions:=result.defoptions+(tstoreddef(st.defowner).defoptions*[df_generic,df_specialization]);
  78. symtablestack:=oldsymtablestack;
  79. st.insertdef(result);
  80. result.struct:=astruct;
  81. { tabstractprocdef constructor sets po_delphi_nested_cc whenever
  82. nested procvars modeswitch is active. We must be independent of this switch. }
  83. exclude(result.procoptions,po_delphi_nested_cc);
  84. result.proctypeoption:=potype;
  85. { always use the default calling convention }
  86. result.proccalloption:=pocall_default;
  87. include(result.procoptions,po_hascallingconvention);
  88. handle_calling_convention(result,hcc_default_actions_impl);
  89. sym:=cprocsym.create(basesymname+result.unique_id_str);
  90. st.insertsym(sym);
  91. result.procsym:=sym;
  92. proc_add_definition(result);
  93. { the code will be assigned directly to the "code" field later }
  94. result.forwarddef:=false;
  95. result.aliasnames.insert(result.mangledname);
  96. end;
  97. function fileinfo_to_suffix(const fileinfo:tfileposinfo):tsymstr;inline;
  98. begin
  99. result:=tostr(fileinfo.moduleindex)+'_'+
  100. tostr(fileinfo.fileindex)+'_'+
  101. tostr(fileinfo.line)+'_'+
  102. tostr(fileinfo.column);
  103. end;
  104. const
  105. anon_funcref_prefix='$FuncRef_';
  106. capturer_class_name='$CapturerClass';
  107. { the leading $ is only added when registering the var symbol }
  108. capturer_var_name='Capturer';
  109. keepalive_suffix='_keepalive';
  110. outer_self_field_name='OuterSelf';
  111. procedure convert_to_funcref_intf(const n:tidstring;var def:tdef);
  112. var
  113. oldsymtablestack : tsymtablestack;
  114. pvdef : tprocvardef absolute def;
  115. intfdef : tobjectdef;
  116. invokedef : tprocdef;
  117. psym : tprocsym;
  118. sym : tsym;
  119. st : tsymtable;
  120. i : longint;
  121. name : tidstring;
  122. begin
  123. if def.typ<>procvardef then
  124. internalerror(2021040201);
  125. if not (po_is_function_ref in tprocvardef(pvdef).procoptions) then
  126. internalerror(2021022101);
  127. if n='' then
  128. name:=anon_funcref_prefix+fileinfo_to_suffix(current_filepos)
  129. else
  130. name:=n;
  131. intfdef:=cobjectdef.create(odt_interfacecom,name,interface_iunknown,true);
  132. include(intfdef.objectoptions,oo_is_funcref);
  133. include(intfdef.objectoptions,oo_is_invokable);
  134. include(intfdef.objectoptions,oo_has_virtual);
  135. intfdef.typesym:=pvdef.typesym;
  136. pvdef.typesym:=nil;
  137. intfdef.defoptions:=intfdef.defoptions+pvdef.defoptions*[df_generic,df_specialization];
  138. { also inherit the general flags from the surrounding structured type or
  139. function }
  140. if assigned(current_structdef) then
  141. begin
  142. intfdef.defoptions:=intfdef.defoptions+current_structdef.defoptions*[df_generic,df_specialization];
  143. end
  144. else if assigned(current_procinfo) then
  145. begin
  146. intfdef.defoptions:=intfdef.defoptions+current_procinfo.procdef.defoptions*[df_generic,df_specialization];
  147. end;
  148. if cs_generate_rtti in current_settings.localswitches then
  149. include(intfdef.objectoptions,oo_can_have_published);
  150. oldsymtablestack:=symtablestack;
  151. symtablestack:=nil;
  152. invokedef:=tprocdef(pvdef.getcopyas(procdef,pc_normal_no_paras,'',false));
  153. invokedef.struct:=intfdef;
  154. invokedef.forwarddef:=false;
  155. include(invokedef.procoptions,po_overload);
  156. include(invokedef.procoptions,po_virtualmethod);
  157. invokedef.procsym:=cprocsym.create(method_name_funcref_invoke_decl);
  158. if cs_generate_rtti in current_settings.localswitches then
  159. invokedef.visibility:=vis_published
  160. else
  161. invokedef.visibility:=vis_public;
  162. intfdef.symtable.insertsym(invokedef.procsym);
  163. intfdef.symtable.insertdef(invokedef);
  164. { we need to do this even if the def isn't a generic/specialization itself,
  165. but *belongs* to one }
  166. if intfdef.defoptions*[df_generic,df_specialization]<>[] then
  167. begin
  168. if assigned(pvdef.genericdef) and (pvdef.genericdef.typ<>objectdef) then
  169. internalerror(2021040501);
  170. intfdef.genericdef:=pvdef.genericdef;
  171. { in case of a generic we move all involved syms/defs to the interface }
  172. intfdef.genericparas:=pvdef.genericparas;
  173. pvdef.genericparas:=nil;
  174. if assigned(intfdef.genericparas) then
  175. for i:=0 to intfdef.genericparas.count-1 do
  176. begin
  177. sym:=tsym(intfdef.genericparas[i]);
  178. if sym.owner<>pvdef.parast then
  179. continue;
  180. sym.changeowner(intfdef.symtable);
  181. if (sym.typ=typesym) and (ttypesym(sym).typedef.owner=pvdef.parast) then
  182. ttypesym(sym).typedef.changeowner(intfdef.symtable);
  183. end;
  184. end;
  185. { now move the symtable over }
  186. invokedef.parast.free;
  187. invokedef.parast:=pvdef.parast;
  188. invokedef.parast.defowner:=invokedef;
  189. pvdef.parast:=nil;
  190. for i:=0 to invokedef.parast.symlist.count-1 do
  191. begin
  192. sym:=tsym(invokedef.parast.symlist[i]);
  193. if sym.typ<>paravarsym then
  194. continue;
  195. if tparavarsym(sym).vardef=pvdef then
  196. tparavarsym(sym).vardef:=intfdef;
  197. end;
  198. symtablestack:=oldsymtablestack;
  199. if invokedef.returndef=pvdef then
  200. invokedef.returndef:=intfdef;
  201. handle_calling_convention(invokedef,hcc_default_actions_intf_struct);
  202. proc_add_definition(invokedef);
  203. invokedef.calcparas;
  204. { def is not owned, so it can be simply freed }
  205. def.free;
  206. def:=intfdef;
  207. end;
  208. function adjust_funcref(var def:tdef;sym,dummysym:tsym):boolean;
  209. var
  210. sympos : tfileposinfo;
  211. name : string;
  212. begin
  213. result:=false;
  214. if (def.typ<>procvardef) and not is_funcref(def) then
  215. internalerror(2022020401);
  216. if assigned(sym) and not (sym.typ=typesym) then
  217. internalerror(2022020402);
  218. { these always support everything, no "of object" or
  219. "is_nested" is allowed }
  220. if is_nested_pd(tprocvardef(def)) or
  221. is_methodpointer(def) then
  222. cgmessage(type_e_function_reference_kind);
  223. if not (po_is_block in tprocvardef(def).procoptions) then
  224. begin
  225. if assigned(dummysym) then
  226. ttypesym(dummysym).typedef:=nil;
  227. if assigned(sym) then
  228. begin
  229. ttypesym(sym).typedef:=nil;
  230. name:=sym.name;
  231. end
  232. else
  233. name:='';
  234. convert_to_funcref_intf(name,def);
  235. if assigned(sym) then
  236. ttypesym(sym).typedef:=def;
  237. if assigned(dummysym) then
  238. ttypesym(dummysym).typedef:=def;
  239. build_vmt(tobjectdef(def));
  240. result:=true;
  241. end
  242. else
  243. begin
  244. if assigned(sym) and (sym.refs>0) then
  245. begin
  246. { find where the symbol was used and trigger
  247. a "symbol not completely defined" error }
  248. if not fileinfo_of_typesym_in_def(def,sym,sympos) then
  249. sympos:=sym.fileinfo;
  250. messagepos1(sympos,type_e_type_is_not_completly_defined,sym.realname);
  251. end;
  252. end;
  253. end;
  254. function funcref_intf_for_proc(pd:tabstractprocdef;const suffix:string):tobjectdef;
  255. var
  256. name : tsymstr;
  257. sym : tsym;
  258. symowner : tsymtable;
  259. oldsymtablestack: TSymtablestack;
  260. invokedef: tprocdef;
  261. begin
  262. if pd.is_generic then
  263. internalerror(2022010710);
  264. name:='funcrefintf_'+suffix;
  265. if pd.owner.symtabletype=globalsymtable then
  266. symowner:=current_module.localsymtable
  267. else
  268. symowner:=pd.owner;
  269. sym:=tsym(symowner.find(name));
  270. if assigned(sym) then
  271. begin
  272. if sym.typ<>typesym then
  273. internalerror(2022010708);
  274. if not is_funcref(ttypesym(sym).typedef) then
  275. internalerror(2022010709);
  276. result:=tobjectdef(ttypesym(sym).typedef);
  277. exit;
  278. end;
  279. name:='$'+name;
  280. result:=cobjectdef.create(odt_interfacecom,name,interface_iunknown,false);
  281. include(result.objectoptions,oo_is_funcref);
  282. include(result.objectoptions,oo_is_invokable);
  283. sym:=ctypesym.create(name,result);
  284. oldsymtablestack:=symtablestack;
  285. symtablestack:=nil;
  286. invokedef:=tprocdef(pd.getcopyas(procdef,pc_normal_no_hidden,'',false));
  287. invokedef.struct:=result;
  288. invokedef.visibility:=vis_public;
  289. invokedef.procsym:=cprocsym.create(method_name_funcref_invoke_decl);
  290. invokedef.parast.symtablelevel:=normal_function_level;
  291. invokedef.localst.symtablelevel:=normal_function_level;
  292. include(invokedef.procoptions,po_virtualmethod);
  293. exclude(invokedef.procoptions,po_staticmethod);
  294. exclude(invokedef.procoptions,po_classmethod);
  295. invokedef.forwarddef:=false;
  296. symtablestack:=oldsymtablestack;
  297. result.symtable.insertsym(invokedef.procsym);
  298. result.symtable.insertdef(invokedef);
  299. proc_add_definition(invokedef);
  300. invokedef.calcparas;
  301. include(result.objectoptions,oo_has_virtual);
  302. symowner.insertsym(sym);
  303. symowner.insertdef(result);
  304. addsymref(sym);
  305. build_vmt(result);
  306. end;
  307. {.$define DEBUG_CAPTURER}
  308. function acceptable_typ(sym:tabstractvarsym;typ :tsymtyp) : boolean;
  309. begin
  310. acceptable_typ:=false;
  311. if (sym.typ=typ) then
  312. acceptable_typ:=true
  313. else if (sym.typ=absolutevarsym) and (tabsolutevarsym(sym).reftyp=typ) then
  314. acceptable_typ:=true;
  315. end;
  316. function get_capturer(pd:tprocdef):tabstractvarsym;
  317. function getsym(st:tsymtable;typ:tsymtyp):tabstractvarsym;
  318. begin
  319. result:=tabstractvarsym(st.find(capturer_var_name));
  320. if not assigned(result) then
  321. internalerror(2022010703);
  322. if not acceptable_typ(result,typ) then
  323. internalerror(2022010704);
  324. if not is_class(result.vardef) then
  325. internalerror(2022010705);
  326. end;
  327. begin
  328. case pd.proctypeoption of
  329. potype_unitfinalize,
  330. potype_unitinit,
  331. potype_proginit:
  332. begin
  333. if not assigned(pd.owner) then
  334. internalerror(2022052401);
  335. if pd.owner.symtabletype<>staticsymtable then
  336. internalerror(2022052402);
  337. result:=getsym(pd.owner,staticvarsym);
  338. end;
  339. else
  340. begin
  341. if not assigned(pd.localst) then
  342. internalerror(2022020502);
  343. result:=getsym(pd.localst,localvarsym);
  344. end;
  345. end;
  346. end;
  347. function get_capturer_alive(pd:tprocdef):tabstractvarsym;
  348. function getsym(st:tsymtable;typ:tsymtyp):tabstractvarsym;
  349. begin
  350. result:=tabstractvarsym(st.find(capturer_var_name+keepalive_suffix));
  351. if not assigned(result) then
  352. internalerror(2022051703);
  353. if not acceptable_typ(result,typ) then
  354. internalerror(2022051704);
  355. if not is_interfacecom(result.vardef) then
  356. internalerror(2022051705);
  357. end;
  358. begin
  359. case pd.proctypeoption of
  360. potype_unitfinalize,
  361. potype_unitinit,
  362. potype_proginit:
  363. begin
  364. if not assigned(pd.owner) then
  365. internalerror(2022052403);
  366. if pd.owner.symtabletype<>staticsymtable then
  367. internalerror(2022052404);
  368. result:=getsym(pd.owner,staticvarsym);
  369. end;
  370. else
  371. begin
  372. if not assigned(pd.localst) then
  373. internalerror(2022051702);
  374. result:=getsym(pd.localst,localvarsym);
  375. end;
  376. end;
  377. end;
  378. function get_or_create_capturer(pd:tprocdef):tsym;
  379. var
  380. name : tsymstr;
  381. parent,
  382. def : tobjectdef;
  383. typesym : tsym;
  384. keepalive : tabstractvarsym;
  385. intfimpl : TImplementedInterface;
  386. st : tsymtable;
  387. begin
  388. if pd.has_capturer then
  389. begin
  390. result:=get_capturer(pd);
  391. end
  392. else
  393. begin
  394. parent:=tobjectdef(search_system_type('TINTERFACEDOBJECT').typedef);
  395. if not is_class(parent) then
  396. internalerror(2022010706);
  397. name:=capturer_class_name+'_'+fileinfo_to_suffix(pd.fileinfo);
  398. case pd.proctypeoption of
  399. potype_unitfinalize,
  400. potype_unitinit,
  401. potype_proginit:
  402. st:=pd.owner;
  403. else
  404. st:=pd.localst;
  405. end;
  406. def:=cobjectdef.create(odt_class,name,parent,false);
  407. include(def.objectoptions,oo_is_capturer);
  408. typesym:=ctypesym.create(name,def);
  409. typesym.fileinfo:=pd.fileinfo;
  410. st.insertdef(def);
  411. st.insertsym(typesym);
  412. addsymref(typesym);
  413. if df_generic in pd.defoptions then
  414. include(def.defoptions,df_generic);
  415. { don't set df_specialization as in that case genericdef needs to be
  416. set, but the local symtables are freed once a unit is finished }
  417. {if df_specialization in pd.defoptions then
  418. begin
  419. if not assigned(pd.genericdef) or (pd.genericdef.typ<>procdef) then
  420. internalerror(2022020501);
  421. def.genericdef:=tstoreddef(get_capturer(tprocdef(pd.genericdef)).vardef);
  422. include(def.defoptions,df_specialization);
  423. end;}
  424. if st.symtabletype=localsymtable then
  425. result:=clocalvarsym.create('$'+capturer_var_name,vs_value,def,[vo_is_internal])
  426. else
  427. result:=cstaticvarsym.create('$'+capturer_var_name,vs_value,def,[vo_is_internal]);
  428. result.fileinfo:=pd.fileinfo;
  429. st.insertsym(result);
  430. addsymref(result);
  431. if st.symtabletype=localsymtable then
  432. keepalive:=clocalvarsym.create('$'+capturer_var_name+keepalive_suffix,vs_value,interface_iunknown,[vo_is_internal])
  433. else
  434. keepalive:=cstaticvarsym.create('$'+capturer_var_name+keepalive_suffix,vs_value,interface_iunknown,[vo_is_internal]);
  435. keepalive.fileinfo:=pd.fileinfo;
  436. st.insertsym(keepalive);
  437. addsymref(keepalive);
  438. if st.symtabletype<>localsymtable then
  439. begin
  440. cnodeutils.insertbssdata(tstaticvarsym(result));
  441. cnodeutils.insertbssdata(tstaticvarsym(keepalive));
  442. end;
  443. { avoid warnings as these symbols are initialized using initialize_capturer
  444. after parsing the body }
  445. tabstractvarsym(result).varstate:=vs_readwritten;
  446. keepalive.varstate:=vs_readwritten;
  447. pd.has_capturer:=true;
  448. end;
  449. end;
  450. function can_be_captured(sym:tsym;curpd:tprocdef):boolean;
  451. begin
  452. result:=false;
  453. if (sym.typ=procsym) and assigned(curpd) and (curpd.procsym=sym) then
  454. exit(true);
  455. if not (sym.typ in [localvarsym,paravarsym]) then
  456. exit;
  457. if tabstractnormalvarsym(sym).varoptions*[vo_is_result,vo_is_funcret]<>[] then
  458. exit;
  459. if sym.typ=paravarsym then
  460. begin
  461. if (tparavarsym(sym).varspez in [vs_out,vs_var]) and
  462. not (vo_is_self in tparavarsym(sym).varoptions) then
  463. exit;
  464. if is_open_array(tparavarsym(sym).vardef) then
  465. exit;
  466. end;
  467. result:=true;
  468. end;
  469. type
  470. tsym_mapping = record
  471. oldsym:tsym;
  472. newsym:tsym;
  473. end;
  474. psym_mapping = ^tsym_mapping;
  475. function replace_self_sym(var n:tnode;arg:pointer):foreachnoderesult;
  476. var
  477. mapping : psym_mapping absolute arg;
  478. ld : tloadnode;
  479. begin
  480. if n.nodetype=loadn then
  481. begin
  482. ld:=tloadnode(n);
  483. if ld.symtableentry=mapping^.oldsym then
  484. begin
  485. ld.symtableentry:=mapping^.newsym;
  486. { make sure that the node is processed again }
  487. ld.resultdef:=nil;
  488. if assigned(ld.left) then
  489. begin
  490. { no longer loaded through the frame pointer }
  491. ld.left.free;
  492. ld.left:=nil;
  493. end;
  494. typecheckpass(n);
  495. end;
  496. end;
  497. result:=fen_true;
  498. end;
  499. procedure capture_captured_syms(pd:tprocdef;owner:tprocinfo;capturedef:tobjectdef;oldpd:tprocdef);
  500. var
  501. curpd : tprocdef;
  502. subcapturer : tobjectdef;
  503. symstodo : TFPList;
  504. i : longint;
  505. sym : tsym;
  506. fieldsym : tfieldvarsym;
  507. fieldname : tsymstr;
  508. fielddef : tdef;
  509. begin
  510. if not pd.was_anonymous or not assigned(pd.capturedsyms) or (pd.capturedsyms.count=0) then
  511. exit;
  512. { capture all variables that the original procdef captured }
  513. curpd:=owner.procdef;
  514. subcapturer:=capturedef;
  515. symstodo:=tfplist.create;
  516. for i:=0 to pd.capturedsyms.count-1 do
  517. if can_be_captured(pcapturedsyminfo(pd.capturedsyms[i])^.sym,oldpd) and
  518. (pcapturedsyminfo(pd.capturedsyms[i])^.sym.typ<>procsym) then
  519. symstodo.add(pcapturedsyminfo(pd.capturedsyms[i])^.sym);
  520. while symstodo.count>0 do
  521. begin
  522. { we know we have symbols left to capture thus we either have a
  523. symbol that's located in the capturer of the current procdef or
  524. we need to put in the OuterSelf reference }
  525. if curpd=owner.procdef then
  526. subcapturer:=capturedef
  527. else
  528. subcapturer:=tobjectdef(tabstractvarsym(get_or_create_capturer(curpd)).vardef);
  529. i:=0;
  530. while i<symstodo.count do
  531. begin
  532. sym:=tsym(symstodo[i]);
  533. if (sym.owner=curpd.localst) or
  534. (sym.owner=curpd.parast) then
  535. begin
  536. {$ifdef DEBUG_CAPTURER}writeln('Symbol ',sym.name,' captured from ',curpd.procsym.name);{$endif}
  537. { the symbol belongs to the current procdef, so add a field to
  538. the capturer if it doesn't already exist }
  539. if vo_is_self in tabstractnormalvarsym(sym).varoptions then
  540. fieldname:=outer_self_field_name
  541. else
  542. fieldname:=sym.name;
  543. fieldsym:=tfieldvarsym(subcapturer.symtable.find(fieldname));
  544. if not assigned(fieldsym) then
  545. begin
  546. {$ifdef DEBUG_CAPTURER}writeln('Adding field ',fieldname,' to ',subcapturer.typesym.name);{$endif}
  547. fielddef:=tabstractvarsym(sym).vardef;
  548. if vo_is_self in tabstractnormalvarsym(sym).varoptions then
  549. begin
  550. fieldname:='$'+fieldname;
  551. if not is_implicit_pointer_object_type(fielddef) then
  552. fielddef:=cpointerdef.getreusable(fielddef);
  553. end;
  554. fieldsym:=cfieldvarsym.create(fieldname,vs_value,fielddef,[]);
  555. fieldsym.fileinfo:=sym.fileinfo;
  556. subcapturer.symtable.insertsym(fieldsym);
  557. tabstractrecordsymtable(subcapturer.symtable).addfield(fieldsym,vis_public);
  558. end;
  559. if not assigned(tabstractnormalvarsym(sym).capture_sym) then
  560. tabstractnormalvarsym(sym).capture_sym:=fieldsym
  561. else if tabstractnormalvarsym(sym).capture_sym<>fieldsym then
  562. internalerror(2022011602);
  563. symstodo.delete(i);
  564. end
  565. else if sym=pd.procsym then
  566. { no explicit capturing needed here }
  567. symstodo.delete(i)
  568. else
  569. inc(i);
  570. end;
  571. if symstodo.count>0 then
  572. begin
  573. if curpd.owner.symtabletype<>localsymtable then
  574. internalerror(2022011001);
  575. { there are still symbols left, so before we move to the parent
  576. procdef we add the OuterSelf field to set up the chain of
  577. capturers }
  578. {$ifdef DEBUG_CAPTURER}writeln('Initialize capturer for ',curpd.procsym.name);{$endif}
  579. { we no longer need the curpd, but we need the parent, so change
  580. curpd here }
  581. curpd:=tprocdef(curpd.owner.defowner);
  582. if curpd.typ<>procdef then
  583. internalerror(2022011002);
  584. if not assigned(subcapturer.symtable.find(outer_self_field_name)) then
  585. begin
  586. {$ifdef DEBUG_CAPTURER}writeln('Adding field OuterSelf to ',subcapturer.typesym.name);{$endif}
  587. if subcapturer.owner.symtablelevel>normal_function_level then
  588. begin
  589. { the outer self is the capturer of the outer procdef }
  590. sym:=get_or_create_capturer(curpd);
  591. { ensure that the outer capturer isn't put into a register anymore }
  592. tabstractvarsym(sym).different_scope:=true;
  593. tabstractvarsym(sym).varregable:=vr_none;
  594. end
  595. else
  596. begin
  597. { the outer self is the self of the method }
  598. if not (curpd.owner.symtabletype in [objectsymtable,recordsymtable]) then
  599. internalerror(2022011603);
  600. sym:=tsym(curpd.parast.find('self'));
  601. if not assigned(sym) then
  602. internalerror(2022011604);
  603. end;
  604. { add the keep alive IUnknown symbol }
  605. fieldsym:=cfieldvarsym.create('$'+outer_self_field_name+keepalive_suffix,vs_value,interface_iunknown,[]);
  606. fieldsym.fileinfo:=sym.fileinfo;
  607. subcapturer.symtable.insertsym(fieldsym);
  608. tabstractrecordsymtable(subcapturer.symtable).addfield(fieldsym,vis_public);
  609. { add the capturer symbol }
  610. fieldsym:=cfieldvarsym.create('$'+outer_self_field_name,vs_value,tabstractvarsym(sym).vardef,[]);
  611. fieldsym.fileinfo:=sym.fileinfo;
  612. subcapturer.symtable.insertsym(fieldsym);
  613. tabstractrecordsymtable(subcapturer.symtable).addfield(fieldsym,vis_public);
  614. if (sym.typ=paravarsym) and (vo_is_self in tparavarsym(sym).varoptions) then
  615. begin
  616. if assigned(tparavarsym(sym).capture_sym) then
  617. internalerror(2022011705);
  618. tparavarsym(sym).capture_sym:=fieldsym;
  619. end;
  620. end;
  621. end;
  622. end;
  623. symstodo.free;
  624. end;
  625. function retrieve_sym_for_filepos(var n:tnode;arg:pointer):foreachnoderesult;
  626. var
  627. sym : ^tsym absolute arg;
  628. begin
  629. if assigned(sym^) then
  630. exit(fen_norecurse_true);
  631. result:=fen_false;
  632. if not (n.resultdef.typ in [procdef,procvardef]) then
  633. exit;
  634. if n.nodetype=loadn then
  635. begin
  636. sym^:=tloadnode(n).symtableentry;
  637. result:=fen_norecurse_true;
  638. end
  639. else if n.nodetype=subscriptn then
  640. begin
  641. sym^:=tsubscriptnode(n).vs;
  642. result:=fen_norecurse_true;
  643. end;
  644. end;
  645. function collect_syms_to_capture(var n:tnode;arg:pointer):foreachnoderesult;
  646. var
  647. pd : tprocdef absolute arg;
  648. sym : tsym;
  649. begin
  650. result:=fen_false;
  651. if n.nodetype<>loadn then
  652. exit;
  653. sym:=tsym(tloadnode(n).symtableentry);
  654. if not (sym.owner.symtabletype in [parasymtable,localsymtable]) then
  655. exit;
  656. if sym.owner.symtablelevel>normal_function_level then begin
  657. pd.add_captured_sym(sym,tloadnode(n).resultdef,n.fileinfo);
  658. result:=fen_true;
  659. end;
  660. end;
  661. type
  662. tselfinfo=record
  663. selfsym:tsym;
  664. ignore:tsym;
  665. end;
  666. pselfinfo=^tselfinfo;
  667. function find_self_sym(var n:tnode;arg:pointer):foreachnoderesult;
  668. var
  669. info : pselfinfo absolute arg;
  670. begin
  671. result:=fen_false;
  672. if assigned(info^.selfsym) then
  673. exit(fen_norecurse_true);
  674. if n.nodetype<>loadn then
  675. exit;
  676. if tloadnode(n).symtableentry.typ<>paravarsym then
  677. exit;
  678. if tloadnode(n).symtableentry=info^.ignore then
  679. exit;
  680. if vo_is_self in tparavarsym(tloadnode(n).symtableentry).varoptions then
  681. begin
  682. info^.selfsym:=tparavarsym(tloadnode(n).symtableentry);
  683. result:=fen_norecurse_true;
  684. end;
  685. end;
  686. function find_outermost_loaded_sym(var n:tnode;arg:pointer):foreachnoderesult;
  687. var
  688. sym : ^tsym absolute arg;
  689. begin
  690. if assigned(sym^) then
  691. exit(fen_norecurse_true);
  692. result:=fen_false;
  693. if n.nodetype<>loadn then
  694. exit;
  695. if not (n.resultdef.typ in [procdef,procvardef]) then
  696. exit;
  697. sym^:=tloadnode(n).symtableentry;
  698. result:=fen_norecurse_true;
  699. end;
  700. function find_procdef(var n:tnode;arg:pointer):foreachnoderesult;
  701. var
  702. pd : ^tprocdef absolute arg;
  703. begin
  704. if assigned(pd^) then
  705. exit(fen_norecurse_true);
  706. result:=fen_false;
  707. if n.resultdef.typ<>procdef then
  708. exit;
  709. pd^:=tprocdef(n.resultdef);
  710. result:=fen_norecurse_true;
  711. end;
  712. function capturer_add_procvar_or_proc(owner:tprocinfo;n:tnode;out capturer:tsym;out capturen:tnode):tobjectdef;
  713. function create_paras(pd:tprocdef):tcallparanode;
  714. var
  715. para : tparavarsym;
  716. i : longint;
  717. begin
  718. result:=nil;
  719. for i:=0 to pd.paras.count-1 do
  720. begin
  721. para:=tparavarsym(pd.paras[i]);
  722. if vo_is_hidden_para in para.varoptions then
  723. continue;
  724. result:=ccallparanode.create(cloadnode.create(para,pd.parast),result);
  725. end;
  726. end;
  727. function find_nested_procinfo(pd:tprocdef):tcgprocinfo;
  728. var
  729. tmp,
  730. res : tprocinfo;
  731. begin
  732. tmp:=owner;
  733. while assigned(tmp) and (tmp.procdef.parast.symtablelevel>=normal_function_level) do
  734. begin
  735. res:=tmp.find_nestedproc_by_pd(pd);
  736. if assigned(res) then
  737. exit(tcgprocinfo(res));
  738. tmp:=tmp.parent;
  739. end;
  740. result:=nil;
  741. end;
  742. procedure swap_symtable(var st1,st2:tsymtable);
  743. var
  744. st : tsymtable;
  745. owner : tdefentry;
  746. level : byte;
  747. begin
  748. { first swap the symtables themselves }
  749. st:=st1;
  750. st1:=st2;
  751. st2:=st;
  752. { then swap the symtables' owners }
  753. owner:=st1.defowner;
  754. st1.defowner:=st2.defowner;
  755. st2.defowner:=owner;
  756. { and finally the symtable level }
  757. level:=st1.symtablelevel;
  758. st1.symtablelevel:=st2.symtablelevel;
  759. st2.symtablelevel:=level;
  760. end;
  761. procedure print_procinfo(pi:tcgprocinfo);
  762. begin
  763. { Print the node to tree.log }
  764. if paraprintnodetree <> 0 then
  765. pi.printproc('after parsing');
  766. {$ifdef DEBUG_NODE_XML}
  767. { Methods of generic classes don't get any code generated, so output
  768. the node tree here }
  769. if (df_generic in pi.procdef.defoptions) then
  770. pi.XMLPrintProc(True);
  771. {$endif DEBUG_NODE_XML}
  772. end;
  773. var
  774. ps : tprocsym;
  775. oldpd,
  776. pd : tprocdef;
  777. pinested,
  778. pi : tcgprocinfo;
  779. sym,
  780. fpsym,
  781. selfsym : tsym;
  782. invokename : tsymstr;
  783. capturedef : tobjectdef;
  784. capturesyms : tfplist;
  785. captured : pcapturedsyminfo;
  786. implintf : TImplementedInterface;
  787. i : longint;
  788. stmt : tstatementnode;
  789. n1 : tnode;
  790. fieldsym : tfieldvarsym;
  791. selfinfo : tselfinfo;
  792. begin
  793. if not (n.resultdef.typ in [procdef,procvardef]) then
  794. internalerror(2022022101);
  795. capturer:=nil;
  796. capturen:=nil;
  797. pinested:=nil;
  798. oldpd:=nil;
  799. { determine a unique name for the variable, field for function of the
  800. node we're trying to load }
  801. sym:=nil;
  802. if not foreachnodestatic(pm_preprocess,n,@find_outermost_loaded_sym,@sym) then
  803. internalerror(2022022102);
  804. result:=funcref_intf_for_proc(tabstractprocdef(n.resultdef),fileinfo_to_suffix(sym.fileinfo));
  805. if (sym.typ=procsym) and (sym.owner.symtabletype=localsymtable) then
  806. begin
  807. { this is assigning a nested function, so retrieve the correct procdef
  808. so that we can then retrieve the procinfo for it }
  809. if n.resultdef.typ=procdef then
  810. pd:=tprocdef(n.resultdef)
  811. else
  812. begin
  813. pd:=nil;
  814. if not foreachnodestatic(pm_preprocess,n,@find_procdef,@pd) then
  815. internalerror(2022041801);
  816. if not assigned(pd) then
  817. internalerror(2022041802);
  818. end;
  819. { check whether all captured symbols can indeed be captured }
  820. capturesyms:=pd.capturedsyms;
  821. if assigned(capturesyms) then
  822. for i:=0 to capturesyms.count-1 do
  823. begin
  824. captured:=pcapturedsyminfo(capturesyms[i]);
  825. if not can_be_captured(captured^.sym,pd) then
  826. MessagePos1(captured^.fileinfo,sym_e_symbol_no_capture,captured^.sym.realname);
  827. end;
  828. if not (df_generic in owner.procdef.defoptions) then
  829. begin
  830. pinested:=find_nested_procinfo(pd);
  831. if not assigned(pinested) then
  832. internalerror(2022041803);
  833. oldpd:=pd;
  834. if pinested.parent<>owner then
  835. begin
  836. { we need to capture this into the owner of the nested function
  837. instead }
  838. owner:=pinested;
  839. capturer:=get_or_create_capturer(pinested.procdef);
  840. if not assigned(capturer) then
  841. internalerror(2022041804);
  842. end;
  843. end;
  844. end
  845. else if (n.resultdef.typ=procvardef) and
  846. (po_delphi_nested_cc in tprocvardef(n.resultdef).procoptions) then
  847. begin
  848. MessagePos(n.fileinfo,type_e_nested_procvar_to_funcref);
  849. exit;
  850. end
  851. else
  852. pinested:=nil;
  853. if df_generic in owner.procdef.defoptions then
  854. exit;
  855. if not assigned(capturer) then
  856. capturer:=get_or_create_capturer(owner.procdef);
  857. if not (capturer.typ in [localvarsym,staticvarsym]) then
  858. internalerror(2022022103);
  859. capturedef:=tobjectdef(tabstractvarsym(capturer).vardef);
  860. if not is_class(capturedef) then
  861. internalerror(2022022104);
  862. implintf:=find_implemented_interface(capturedef,result);
  863. if assigned(implintf) then
  864. begin
  865. { this is already captured into a method of the capturer, so nothing
  866. further to do }
  867. exit;
  868. end;
  869. implintf:=capturedef.register_implemented_interface(result,true);
  870. invokename:=method_name_funcref_invoke_decl+'__FPCINTERNAL__'+fileinfo_to_suffix(sym.fileinfo);
  871. ps:=cprocsym.create(invokename);
  872. pd:=tprocdef(tabstractprocdef(n.resultdef).getcopyas(procdef,pc_normal_no_hidden,'',false));
  873. pd.aliasnames.clear;
  874. pd.procsym:=ps;
  875. pd.struct:=capturedef;
  876. pd.changeowner(capturedef.symtable);
  877. pd.parast.symtablelevel:=normal_function_level;
  878. pd.localst.symtablelevel:=normal_function_level;
  879. { reset procoptions }
  880. pd.procoptions:=[];
  881. ps.ProcdefList.Add(pd);
  882. pd.forwarddef:=false;
  883. { set procinfo and current_procinfo.procdef }
  884. pi:=tcgprocinfo(cprocinfo.create(nil));
  885. pi.procdef:=pd;
  886. if not assigned(pinested) then
  887. begin
  888. insert_funcret_para(pd);
  889. insert_funcret_local(pd);
  890. { we always do a call, namely to the provided function }
  891. include(pi.flags,pi_do_call);
  892. end
  893. else
  894. begin
  895. { the original nested function now calls the method }
  896. include(pinested.flags,pi_do_call);
  897. { swap the para and local symtables of the nested and new routine }
  898. swap_symtable(pinested.procdef.parast,pd.parast);
  899. swap_symtable(pinested.procdef.localst,pd.localst);
  900. { fix function return symbol }
  901. pd.funcretsym:=pinested.procdef.funcretsym;
  902. pinested.procdef.funcretsym:=nil;
  903. pinested.procdef.reset_after_conv;
  904. insert_funcret_para(pinested.procdef);
  905. insert_funcret_local(pinested.procdef);
  906. { the nested function needs access to the parent's framepointer to
  907. access the capturer }
  908. insert_parentfp_para(pinested.procdef);
  909. pd.copied_from:=pinested.procdef;
  910. end;
  911. { to simplify some checks, but only after insert_funcret_para }
  912. pd.was_anonymous:=true;
  913. capturedef.symtable.insertsym(ps);
  914. owner.addnestedproc(pi);
  915. { remove self and parentfp parameter if any as that will be replaced by
  916. the capturer }
  917. selfsym:=nil;
  918. fpsym:=nil;
  919. for i:=0 to pd.parast.symlist.count-1 do
  920. begin
  921. sym:=tsym(pd.parast.symlist[i]);
  922. if sym.typ<>paravarsym then
  923. continue;
  924. if vo_is_self in tparavarsym(sym).varoptions then
  925. selfsym:=sym
  926. else if vo_is_parentfp in tparavarsym(sym).varoptions then
  927. fpsym:=sym;
  928. if assigned(selfsym) and assigned(fpsym) then
  929. break;
  930. end;
  931. if assigned(selfsym) then
  932. pd.parast.deletesym(selfsym);
  933. if assigned(fpsym) then
  934. pd.parast.deletesym(fpsym);
  935. pd.calcparas;
  936. if assigned(pinested) then
  937. pinested.procdef.calcparas;
  938. insert_self_and_vmt_para(pd);
  939. if assigned(pinested) then
  940. begin
  941. { when we're assigning a nested function to a function reference we
  942. move the code of the nested function to the newly created capturer
  943. method (including the captured symbols) and have the original nested
  944. function simply call that function-turned-method }
  945. pi.code:=pinested.code;
  946. pinested.code:=internalstatements(stmt);
  947. end
  948. else
  949. pi.code:=internalstatements(stmt);
  950. selfinfo.selfsym:=nil;
  951. selfinfo.ignore:=nil;
  952. fieldsym:=nil;
  953. if assigned(pinested) then
  954. begin
  955. n1:=ccallnode.create(create_paras(pinested.procdef),ps,capturedef.symtable,cloadnode.create(capturer,capturer.owner),[],nil);
  956. { captured variables cannot be in registers }
  957. make_not_regable(tcallnode(n1).methodpointer,[ra_addr_regable,ra_addr_taken]);
  958. end
  959. else if n.resultdef.typ=procvardef then
  960. begin
  961. { store the procvar in a field so that it won't be changed if the
  962. procvar itself is changed }
  963. fieldsym:=cfieldvarsym.create('$'+fileinfo_to_suffix(n.fileinfo),vs_value,n.resultdef,[]);
  964. fieldsym.fileinfo:=n.fileinfo;
  965. capturedef.symtable.insertsym(fieldsym);
  966. tabstractrecordsymtable(capturedef.symtable).addfield(fieldsym,vis_public);
  967. capturen:=csubscriptnode.create(fieldsym,cloadnode.create(capturer,capturer.owner));
  968. selfsym:=tsym(pd.parast.find('self'));
  969. if not assigned(selfsym) then
  970. internalerror(2022052301);
  971. selfinfo.ignore:=selfsym;
  972. n1:=ccallnode.create_procvar(create_paras(pd),csubscriptnode.create(fieldsym,cloadnode.create(selfsym,selfsym.owner)));
  973. end
  974. else
  975. begin
  976. if n.nodetype<>loadn then
  977. internalerror(2022032401);
  978. if tloadnode(n).symtableentry.typ<>procsym then
  979. internalerror(2022032402);
  980. n1:=ccallnode.create(create_paras(pd),tprocsym(tloadnode(n).symtableentry),tloadnode(n).symtable,tloadnode(n).left,[],nil);
  981. tloadnode(n).left:=nil;
  982. end;
  983. if assigned(pd.returndef) and not is_void(pd.returndef) then
  984. begin
  985. if assigned(pinested) then
  986. sym:=pinested.procdef.funcretsym
  987. else
  988. sym:=pd.funcretsym;
  989. n1:=cassignmentnode.create(
  990. cloadnode.create(sym,sym.owner),
  991. n1
  992. );
  993. { captured variables cannot be in registers }
  994. make_not_regable(tassignmentnode(n1).left,[ra_addr_regable,ra_addr_taken]);
  995. end;
  996. addstatement(stmt,n1);
  997. pd.aliasnames.insert(pd.mangledname);
  998. if assigned(pinested) then
  999. begin
  1000. { transfer all captured syms }
  1001. capturesyms:=pinested.procdef.capturedsyms;
  1002. if assigned(capturesyms) then
  1003. begin
  1004. for i:=0 to capturesyms.count-1 do
  1005. begin
  1006. captured:=pcapturedsyminfo(capturesyms[i]);
  1007. pi.add_captured_sym(captured^.sym,captured^.def,captured^.fileinfo);
  1008. dispose(captured);
  1009. end;
  1010. capturesyms.clear;
  1011. end;
  1012. { the original nested function now needs to capture only the capturer }
  1013. pinested.procdef.add_captured_sym(capturer,capturedef,n.fileinfo);
  1014. end
  1015. { does this need to capture Self? }
  1016. else if not foreachnodestatic(pm_postprocess,n,@find_self_sym,@selfinfo) then
  1017. begin
  1018. { is this a method of the current class? }
  1019. if (n.resultdef.typ=procdef) and
  1020. assigned(tprocdef(n.resultdef).struct) and
  1021. not (po_staticmethod in tprocdef(n.resultdef).procoptions) and
  1022. assigned(current_procinfo.procdef.struct) and
  1023. def_is_related(current_procinfo.procdef.struct,tprocdef(n.resultdef).struct) then
  1024. begin
  1025. selfinfo.selfsym:=tsym(current_procinfo.procdef.parast.find('self'));
  1026. if not assigned(selfinfo.selfsym) then
  1027. internalerror(2022110601);
  1028. end
  1029. else
  1030. { does this need some other local variable or parameter? }
  1031. foreachnodestatic(pm_postprocess,n,@collect_syms_to_capture,@pd)
  1032. end;
  1033. if assigned(selfinfo.selfsym) and not assigned(fieldsym) then
  1034. { this isn't a procdef that was captured into a field, so capture the
  1035. self }
  1036. pd.add_captured_sym(selfinfo.selfsym,tabstractvarsym(selfinfo.selfsym).vardef,n.fileinfo);
  1037. print_procinfo(pi);
  1038. if assigned(pinested) then
  1039. print_procinfo(pinested);
  1040. implintf.AddMapping(upcase(result.objrealname^+'.')+method_name_funcref_invoke_find,upcase(invokename));
  1041. capture_captured_syms(pd,owner,capturedef,oldpd);
  1042. end;
  1043. function capturer_add_anonymous_proc(owner:tprocinfo;pd:tprocdef;out capturer:tsym):tobjectdef;
  1044. var
  1045. capturedef : tobjectdef;
  1046. implintf : TImplementedInterface;
  1047. invokename : tsymstr;
  1048. i : longint;
  1049. outerself,
  1050. fpsym,
  1051. selfsym,
  1052. sym : tsym;
  1053. info : pcapturedsyminfo;
  1054. pi : tprocinfo;
  1055. mapping : tsym_mapping;
  1056. invokedef,
  1057. parentdef,
  1058. curpd : tprocdef;
  1059. begin
  1060. capturer:=nil;
  1061. result:=funcref_intf_for_proc(pd,fileinfo_to_suffix(pd.fileinfo));
  1062. if df_generic in pd.defoptions then
  1063. begin
  1064. if (po_anonymous in pd.procoptions) and
  1065. assigned(pd.capturedsyms) and
  1066. (pd.capturedsyms.count>0) then
  1067. begin
  1068. { only check whether the symbols can be captured, but don't
  1069. convert anything to avoid problems }
  1070. for i:=0 to pd.capturedsyms.count-1 do
  1071. begin
  1072. info:=pcapturedsyminfo(pd.capturedsyms[i]);
  1073. if not can_be_captured(info^.sym,pd) then
  1074. MessagePos1(info^.fileinfo,sym_e_symbol_no_capture,info^.sym.realname)
  1075. end;
  1076. end;
  1077. exit;
  1078. end;
  1079. capturer:=get_or_create_capturer(owner.procdef);
  1080. if not (capturer.typ in [localvarsym,staticvarsym]) then
  1081. internalerror(2022010711);
  1082. capturedef:=tobjectdef(tabstractvarsym(capturer).vardef);
  1083. if not is_class(capturedef) then
  1084. internalerror(2022010712);
  1085. implintf:=find_implemented_interface(capturedef,result);
  1086. if assigned(implintf) then
  1087. begin
  1088. { this can only already be an implemented interface if a named procdef
  1089. was assigned to a function ref at an earlier point, an anonymous
  1090. function can be used only once }
  1091. if po_anonymous in pd.procoptions then
  1092. internalerror(2022010713);
  1093. exit;
  1094. end;
  1095. implintf:=capturedef.register_implemented_interface(result,true);
  1096. invokename:=method_name_funcref_invoke_decl+'__FPCINTERNAL__'+fileinfo_to_suffix(pd.fileinfo);
  1097. if po_anonymous in pd.procoptions then
  1098. begin
  1099. { turn the anonymous function into a method of the capturer }
  1100. pd.changeowner(capturedef.symtable);
  1101. pd.struct:=capturedef;
  1102. exclude(pd.procoptions,po_anonymous);
  1103. exclude(pd.procoptions,po_delphi_nested_cc);
  1104. exclude(pd.procoptions,po_staticmethod);
  1105. exclude(pd.procoptions,po_classmethod);
  1106. pd.was_anonymous:=true;
  1107. pd.procsym.ChangeOwnerAndName(capturedef.symtable,upcase(invokename));
  1108. pd.procsym.realname:=invokename;
  1109. pd.parast.symtablelevel:=normal_function_level;
  1110. pd.localst.symtablelevel:=normal_function_level;
  1111. { retrieve framepointer and self parameters if any }
  1112. fpsym:=nil;
  1113. selfsym:=nil;
  1114. for i:=0 to pd.parast.symlist.count-1 do
  1115. begin
  1116. sym:=tsym(pd.parast.symlist[i]);
  1117. if sym.typ<>paravarsym then
  1118. continue;
  1119. if vo_is_parentfp in tparavarsym(sym).varoptions then
  1120. fpsym:=sym
  1121. else if vo_is_self in tparavarsym(sym).varoptions then
  1122. selfsym:=sym;
  1123. if assigned(fpsym) and assigned(selfsym) then
  1124. break;
  1125. end;
  1126. { get rid of the framepointer parameter }
  1127. if assigned(fpsym) then
  1128. pd.parast.deletesym(fpsym);
  1129. outerself:=nil;
  1130. { complain about all symbols that can't be captured and add the symbols
  1131. to this procdefs capturedsyms if it isn't a top level function }
  1132. if assigned(pd.capturedsyms) and (pd.capturedsyms.count>0) then
  1133. begin
  1134. for i:=0 to pd.capturedsyms.count-1 do
  1135. begin
  1136. info:=pcapturedsyminfo(pd.capturedsyms[i]);
  1137. if not can_be_captured(info^.sym,pd) then
  1138. MessagePos1(info^.fileinfo,sym_e_symbol_no_capture,info^.sym.realname)
  1139. else if info^.sym=selfsym then
  1140. begin
  1141. { we need to replace the captured "dummy" self parameter
  1142. with the real self parameter symbol from the surrounding
  1143. method }
  1144. if not assigned(outerself) then
  1145. outerself:=tsym(owner.get_normal_proc.procdef.parast.find('self'));
  1146. if not assigned(outerself) then
  1147. internalerror(2022010905);
  1148. { the anonymous function can only be a direct child of the
  1149. owner }
  1150. pi:=owner.get_first_nestedproc;
  1151. while assigned(pi) do
  1152. begin
  1153. if pi.procdef=pd then
  1154. break;
  1155. pi:=tprocinfo(pi.next);
  1156. end;
  1157. if not assigned(pi) then
  1158. internalerror(2022010906);
  1159. mapping.oldsym:=selfsym;
  1160. mapping.newsym:=outerself;
  1161. { replace all uses of the captured Self by the new Self
  1162. parameter }
  1163. foreachnodestatic(pm_preprocess,tcgprocinfo(pi).code,@replace_self_sym,@mapping);
  1164. { update the captured symbol }
  1165. info^.sym:=outerself;
  1166. info^.def:=tabstractvarsym(outerself).vardef;
  1167. end
  1168. else if info^.sym.owner.defowner<>owner.procdef then
  1169. owner.procdef.add_captured_sym(info^.sym,info^.def,info^.fileinfo);
  1170. end;
  1171. end;
  1172. { delete the original self parameter }
  1173. if assigned(selfsym) then
  1174. pd.parast.deletesym(selfsym);
  1175. { note: don't call insert_self_and_vmt_para here, as that is later on
  1176. done when building the VMT }
  1177. end
  1178. else
  1179. internalerror(2022022201);
  1180. implintf.AddMapping(upcase(result.objrealname^+'.')+method_name_funcref_invoke_find,upcase(invokename));
  1181. capture_captured_syms(pd,owner,capturedef,nil);
  1182. end;
  1183. function load_capturer(capturer:tabstractvarsym):tnode;inline;
  1184. begin
  1185. result:=cloadnode.create(capturer,capturer.owner);
  1186. end;
  1187. function instantiate_capturer(capturer_sym:tabstractvarsym):tnode;
  1188. var
  1189. capturer_def : tobjectdef;
  1190. ctor : tprocsym;
  1191. begin
  1192. capturer_def:=tobjectdef(capturer_sym.vardef);
  1193. { Neither TInterfacedObject, nor TCapturer have a custom constructor }
  1194. ctor:=tprocsym(class_tobject.symtable.Find('CREATE'));
  1195. if not assigned(ctor) then
  1196. internalerror(2022010801);
  1197. { Insert "Capturer := TCapturer.Create()" as the first statement of the routine }
  1198. result:=cloadvmtaddrnode.create(ctypenode.create(capturer_def));
  1199. result:=ccallnode.create(nil,ctor,capturer_def.symtable,result,[],nil);
  1200. result:=cassignmentnode.create(load_capturer(capturer_sym),result);
  1201. end;
  1202. procedure initialize_captured_paras(pd:tprocdef;capturer:tabstractvarsym;var stmt:tstatementnode);
  1203. var
  1204. i : longint;
  1205. psym: tparavarsym;
  1206. n : tnode;
  1207. begin
  1208. for i:=0 to pd.paras.count-1 do
  1209. begin
  1210. psym:=tparavarsym(pd.paras[i]);
  1211. if not psym.is_captured then
  1212. continue;
  1213. {$ifdef DEBUG_CAPTURER}writeln(#9'initialize captured parameter ',psym.RealName);{$endif}
  1214. n:=cloadnode.create(psym,psym.owner);
  1215. if psym.capture_sym.owner.defowner<>capturer.vardef then
  1216. internalerror(2022010903);
  1217. if (vo_is_self in psym.varoptions) and not is_implicit_pointer_object_type(psym.vardef) then
  1218. n:=caddrnode.create(n);
  1219. n:=cassignmentnode.create(
  1220. csubscriptnode.create(psym.capture_sym,cloadnode.create(capturer,capturer.owner)),
  1221. n
  1222. );
  1223. addstatement(stmt,n);
  1224. end;
  1225. end;
  1226. procedure attach_outer_capturer(ctx:tprocinfo;capturer:tabstractvarsym;var stmt:tstatementnode);
  1227. var
  1228. alivefield,
  1229. selffield : tfieldvarsym;
  1230. outeralive,
  1231. outercapturer : tabstractvarsym;
  1232. alivenode,
  1233. selfnode : tnode;
  1234. begin
  1235. if not ctx.procdef.was_anonymous and
  1236. not (ctx.procdef.owner.symtabletype=localsymtable) then
  1237. exit;
  1238. selffield:=tfieldvarsym(tobjectdef(capturer.vardef).symtable.find(outer_self_field_name));
  1239. if not assigned(selffield) then
  1240. { we'll simply assume that we don't need the outer capturer }
  1241. exit;
  1242. alivefield:=tfieldvarsym(tobjectdef(capturer.vardef).symtable.find(outer_self_field_name+keepalive_suffix));
  1243. if not assigned(alivefield) then
  1244. internalerror(2022051701);
  1245. if ctx.procdef.was_anonymous then
  1246. begin
  1247. selfnode:=load_self_node;
  1248. alivenode:=selfnode.getcopy;
  1249. end
  1250. else
  1251. begin
  1252. outercapturer:=get_capturer(tprocdef(ctx.procdef.owner.defowner));
  1253. if not assigned(outercapturer) then
  1254. internalerror(2022011605);
  1255. selfnode:=cloadnode.create(outercapturer,outercapturer.owner);
  1256. make_not_regable(selfnode,[ra_different_scope]);
  1257. outeralive:=get_capturer_alive(tprocdef(ctx.procdef.owner.defowner));
  1258. if not assigned(outeralive) then
  1259. internalerror(2022051706);
  1260. alivenode:=cloadnode.create(outeralive,outeralive.owner);
  1261. make_not_regable(alivenode,[ra_different_scope]);
  1262. end;
  1263. addstatement(stmt,cassignmentnode.create(
  1264. csubscriptnode.create(
  1265. selffield,
  1266. cloadnode.create(
  1267. capturer,
  1268. capturer.owner
  1269. )
  1270. ),
  1271. selfnode));
  1272. addstatement(stmt,cassignmentnode.create(
  1273. csubscriptnode.create(
  1274. alivefield,
  1275. cloadnode.create(
  1276. capturer,
  1277. capturer.owner
  1278. )
  1279. ),
  1280. alivenode));
  1281. end;
  1282. procedure initialize_capturer(ctx:tprocinfo;var stmt:tstatementnode);
  1283. var
  1284. capturer_sym,
  1285. keepalive_sym : tabstractvarsym;
  1286. begin
  1287. if ctx.procdef.has_capturer then
  1288. begin
  1289. capturer_sym:=get_capturer(ctx.procdef);
  1290. {$ifdef DEBUG_CAPTURER}writeln('initialize_capturer @ ',ctx.procdef.procsym.RealName);{$endif}
  1291. addstatement(stmt,instantiate_capturer(capturer_sym));
  1292. attach_outer_capturer(ctx,capturer_sym,stmt);
  1293. initialize_captured_paras(ctx.procdef,capturer_sym,stmt);
  1294. keepalive_sym:=get_capturer_alive(ctx.procdef);
  1295. if not assigned(keepalive_sym) then
  1296. internalerror(2022010701);
  1297. addstatement(stmt,cassignmentnode.create(cloadnode.create(keepalive_sym,keepalive_sym.owner),load_capturer(capturer_sym)));
  1298. end;
  1299. end;
  1300. procedure postprocess_capturer(ctx: tprocinfo);
  1301. var
  1302. def: tobjectdef;
  1303. begin
  1304. if not ctx.procdef.has_capturer then
  1305. exit;
  1306. def:=tobjectdef(get_capturer(ctx.procdef).vardef);
  1307. {$ifdef DEBUG_CAPTURER}writeln('process capturer ',def.typesym.Name);{$endif}
  1308. { These two are delayed until this point because
  1309. ... we have been adding fields on-the-fly }
  1310. tabstractrecordsymtable(def.symtable).addalignmentpadding;
  1311. { ... we have been adding interfaces on-the-fly }
  1312. build_vmt(def);
  1313. end;
  1314. type
  1315. tconvert_arg=record
  1316. mappings:tfplist;
  1317. end;
  1318. pconvert_arg=^tconvert_arg;
  1319. tconvert_mapping=record
  1320. oldsym:tsym;
  1321. newsym:tsym;
  1322. olddef:tdef;
  1323. selfnode:tnode;
  1324. end;
  1325. pconvert_mapping=^tconvert_mapping;
  1326. function convert_captured_sym(var n:tnode;arg:pointer):foreachnoderesult;
  1327. var
  1328. convertarg : pconvert_arg absolute arg;
  1329. mapping : pconvert_mapping;
  1330. i : longint;
  1331. old_filepos : tfileposinfo;
  1332. loadprocvar : boolean;
  1333. paras,
  1334. mp : tnode;
  1335. cnf : tcallnodeflags;
  1336. paraold,
  1337. paranew : tcallparanode;
  1338. begin
  1339. result:=fen_true;
  1340. if not (n.nodetype in [loadn,calln]) then
  1341. exit;
  1342. for i:=0 to convertarg^.mappings.count-1 do
  1343. begin
  1344. mapping:=convertarg^.mappings[i];
  1345. case n.nodetype of
  1346. loadn:
  1347. begin
  1348. if tloadnode(n).symtableentry<>mapping^.oldsym then
  1349. continue;
  1350. old_filepos:=current_filepos;
  1351. current_filepos:=n.fileinfo;
  1352. loadprocvar:=nf_load_procvar in n.flags;
  1353. n.free;
  1354. n:=csubscriptnode.create(mapping^.newsym,mapping^.selfnode.getcopy);
  1355. if loadprocvar then
  1356. include(n.flags,nf_load_procvar);
  1357. if (mapping^.oldsym.typ=paravarsym) and
  1358. (vo_is_self in tparavarsym(mapping^.oldsym).varoptions) and
  1359. not is_implicit_pointer_object_type(tparavarsym(mapping^.oldsym).vardef) then
  1360. n:=cderefnode.create(n);
  1361. typecheckpass(n);
  1362. current_filepos:=old_filepos;
  1363. break;
  1364. end;
  1365. calln:
  1366. begin
  1367. if mapping^.oldsym.typ<>procsym then
  1368. continue;
  1369. if tcallnode(n).symtableprocentry<>tprocsym(mapping^.oldsym) then
  1370. continue;
  1371. if tcallnode(n).procdefinition<>tprocdef(mapping^.olddef) then
  1372. continue;
  1373. old_filepos:=current_filepos;
  1374. current_filepos:=n.fileinfo;
  1375. loadprocvar:=nf_load_procvar in n.flags;
  1376. paras:=tcallnode(n).left;
  1377. paraold:=tcallparanode(paras);
  1378. paranew:=nil;
  1379. while assigned(paraold) do
  1380. begin
  1381. if not (vo_is_hidden_para in paraold.parasym.varoptions) then
  1382. begin
  1383. paranew:=ccallparanode.create(paraold.left,paranew);
  1384. paraold.left:=nil;
  1385. end;
  1386. paraold:=tcallparanode(paraold.right);
  1387. end;
  1388. reverseparameters(paranew);
  1389. if assigned(tcallnode(n).methodpointer) then
  1390. internalerror(2023120802);
  1391. cnf:=tcallnode(n).callnodeflags;
  1392. n.free;
  1393. n:=ccallnode.create(paranew,tprocsym(mapping^.newsym),mapping^.newsym.owner,mapping^.selfnode.getcopy,cnf,nil);
  1394. if loadprocvar then
  1395. include(n.flags,nf_load_procvar);
  1396. typecheckpass(n);
  1397. current_filepos:=old_filepos;
  1398. break;
  1399. end;
  1400. else
  1401. internalerror(2023120801);
  1402. end;
  1403. end;
  1404. end;
  1405. procedure convert_captured_syms(pd:tprocdef;tree:tnode);
  1406. function self_tree_for_sym(selfsym:tsym;fieldsym:tsym):tnode;
  1407. var
  1408. fieldowner : tdef;
  1409. newsym : tsym;
  1410. begin
  1411. result:=cloadnode.create(selfsym,selfsym.owner);
  1412. fieldowner:=tdef(fieldsym.owner.defowner);
  1413. newsym:=selfsym;
  1414. while (tabstractvarsym(newsym).vardef<>fieldowner) do
  1415. begin
  1416. newsym:=tsym(tobjectdef(tabstractvarsym(newsym).vardef).symtable.find(outer_self_field_name));
  1417. if not assigned(newsym) then
  1418. internalerror(2022011101);
  1419. result:=csubscriptnode.create(newsym,result);
  1420. end;
  1421. end;
  1422. var
  1423. i,j : longint;
  1424. capturer : tobjectdef;
  1425. tocapture,
  1426. capturedsyms : tfplist;
  1427. convertarg : tconvert_arg;
  1428. mapping : pconvert_mapping;
  1429. invokepd : tprocdef;
  1430. selfsym,
  1431. sym : tsym;
  1432. info: pcapturedsyminfo;
  1433. begin
  1434. {$ifdef DEBUG_CAPTURER}writeln('Converting captured symbols of ',pd.procsym.name);{$endif}
  1435. convertarg.mappings:=tfplist.create;
  1436. capturedsyms:=tfplist.create;
  1437. if pd.was_anonymous and
  1438. assigned(pd.capturedsyms) and
  1439. (pd.capturedsyms.count>0) then
  1440. begin
  1441. {$ifdef DEBUG_CAPTURER}writeln('Converting symbols of converted anonymous function ',pd.procsym.name);{$endif}
  1442. { this is a converted anonymous function, so rework all symbols that
  1443. now belong to the new Self }
  1444. selfsym:=tsym(pd.parast.find('self'));
  1445. if not assigned(selfsym) then
  1446. internalerror(2022010809);
  1447. for i:=0 to pd.capturedsyms.count-1 do
  1448. begin
  1449. sym:=tsym(pcapturedsyminfo(pd.capturedsyms[i])^.sym);
  1450. if not can_be_captured(sym,pd) and
  1451. not (
  1452. (sym.typ=procsym) and
  1453. assigned(pd.copied_from) and
  1454. (pd.copied_from.procsym=sym)
  1455. ) then
  1456. continue;
  1457. {$ifdef DEBUG_CAPTURER}writeln('Replacing symbol ',sym.Name);{$endif}
  1458. new(mapping);
  1459. mapping^.oldsym:=sym;
  1460. if sym.typ=procsym then
  1461. begin
  1462. if not assigned(pd.copied_from) or
  1463. (pd.copied_from.procsym<>sym) then
  1464. internalerror(2023123001);
  1465. mapping^.newsym:=pd.procsym;
  1466. end
  1467. else
  1468. mapping^.newsym:=tabstractnormalvarsym(sym).capture_sym;
  1469. mapping^.olddef:=pcapturedsyminfo(pd.capturedsyms[i])^.def;
  1470. if not assigned(mapping^.newsym) then
  1471. internalerror(2022010810);
  1472. mapping^.selfnode:=self_tree_for_sym(selfsym,mapping^.newsym);
  1473. convertarg.mappings.add(mapping);
  1474. capturedsyms.add(sym);
  1475. end;
  1476. end;
  1477. if (pd.parast.symtablelevel>normal_function_level) and
  1478. assigned(pd.capturedsyms) and
  1479. (pd.capturedsyms.count>0) then
  1480. begin
  1481. if pd.was_anonymous then
  1482. internalerror(2022081201);
  1483. {$ifdef DEBUG_CAPTURER}writeln('Converting symbols of nested function ',pd.procsym.name);{$endif}
  1484. { this is a nested function, so rework all symbols that are used from
  1485. a parent function, but that might have been captured }
  1486. for i:=0 to pd.capturedsyms.count-1 do
  1487. begin
  1488. sym:=tsym(pcapturedsyminfo(pd.capturedsyms[i])^.sym);
  1489. if not can_be_captured(sym,pd) or
  1490. (sym.typ=procsym) or
  1491. not assigned(tabstractnormalvarsym(sym).capture_sym) then
  1492. continue;
  1493. {$ifdef DEBUG_CAPTURER}writeln('Replacing symbol ',sym.Name);{$endif}
  1494. new(mapping);
  1495. mapping^.oldsym:=sym;
  1496. mapping^.newsym:=tabstractnormalvarsym(sym).capture_sym;
  1497. mapping^.olddef:=pcapturedsyminfo(pd.capturedsyms[i])^.def;
  1498. capturer:=tobjectdef(mapping^.newsym.owner.defowner);
  1499. if not is_class(capturer) then
  1500. internalerror(2022012701);
  1501. if not (capturer.typesym.owner.symtabletype in [localsymtable,staticsymtable]) then
  1502. internalerror(2022012702);
  1503. selfsym:=tsym(capturer.typesym.owner.find(capturer_var_name));
  1504. if not assigned(selfsym) then
  1505. internalerror(2022012703);
  1506. mapping^.selfnode:=self_tree_for_sym(selfsym,mapping^.newsym);
  1507. convertarg.mappings.add(mapping);
  1508. capturedsyms.add(sym);
  1509. end;
  1510. end;
  1511. if pd.has_capturer then
  1512. begin
  1513. {$ifdef DEBUG_CAPTURER}writeln('Converting symbols of function ',pd.procsym.name,' with capturer');{$endif}
  1514. { this procedure has a capturer, so rework all symbols that are
  1515. captured in that capturer }
  1516. selfsym:=get_capturer(pd);
  1517. { only capture those symbols that weren't captured already by one of
  1518. the above if-clauses and thus are now listed in capturedsyms }
  1519. tocapture:=tfplist.create;
  1520. for i:=0 to pd.localst.symlist.count-1 do
  1521. begin
  1522. sym:=tsym(pd.localst.symlist[i]);
  1523. if sym.typ<>localvarsym then
  1524. continue;
  1525. if assigned(tabstractnormalvarsym(sym).capture_sym) then
  1526. if capturedsyms.indexof(sym)<0 then
  1527. tocapture.add(sym);
  1528. end;
  1529. for i:=0 to pd.parast.symlist.count-1 do
  1530. begin
  1531. sym:=tsym(pd.parast.symlist[i]);
  1532. if sym.typ<>paravarsym then
  1533. continue;
  1534. if assigned(tabstractnormalvarsym(sym).capture_sym) and
  1535. { no need to adjust accesses to the outermost Self inside the
  1536. outermost method }
  1537. not (vo_is_self in tabstractvarsym(sym).varoptions) then
  1538. if capturedsyms.indexof(sym)<0 then
  1539. tocapture.add(sym);
  1540. end;
  1541. convertarg.mappings.capacity:=convertarg.mappings.count+tocapture.count;
  1542. for i:=0 to tocapture.count-1 do
  1543. begin
  1544. new(mapping);
  1545. mapping^.oldsym:=tsym(tocapture[i]);
  1546. {$ifdef DEBUG_CAPTURER}writeln('Replacing symbol ',mapping^.oldsym.Name);{$endif}
  1547. mapping^.newsym:=tabstractnormalvarsym(mapping^.oldsym).capture_sym;
  1548. if not assigned(mapping^.newsym) then
  1549. internalerror(2022010805);
  1550. mapping^.selfnode:=self_tree_for_sym(selfsym,mapping^.newsym);
  1551. convertarg.mappings.add(mapping);
  1552. end;
  1553. tocapture.free;
  1554. end;
  1555. { not required anymore }
  1556. capturedsyms.free;
  1557. if convertarg.mappings.count>0 then
  1558. foreachnodestatic(pm_postprocess,tree,@convert_captured_sym,@convertarg);
  1559. for i:=0 to convertarg.mappings.count-1 do
  1560. begin
  1561. mapping:=pconvert_mapping(convertarg.mappings[i]);
  1562. mapping^.selfnode.free;
  1563. dispose(mapping);
  1564. end;
  1565. convertarg.mappings.free;
  1566. end;
  1567. end.