symcreat.pas 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. {
  2. Copyright (c) 2011 by Jonas Maebe
  3. This unit provides helpers for creating new syms/defs based on string
  4. representations.
  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 symcreat;
  20. interface
  21. uses
  22. finput,tokens,scanner,
  23. symconst,symdef,symbase;
  24. type
  25. tscannerstate = record
  26. old_scanner: tscannerfile;
  27. old_token: ttoken;
  28. old_c: char;
  29. valid: boolean;
  30. end;
  31. { save/restore the scanner state before/after injecting }
  32. procedure replace_scanner(const tempname: string; out sstate: tscannerstate);
  33. procedure restore_scanner(const sstate: tscannerstate);
  34. { parses a (class or regular) method/constructor/destructor declaration from
  35. str, as if it were declared in astruct's declaration body
  36. WARNING: save the scanner state before calling this routine, and restore
  37. when done. }
  38. function str_parse_method_dec(str: ansistring; potype: tproctypeoption; is_classdef: boolean; astruct: tabstractrecorddef; out pd: tprocdef): boolean;
  39. { parses a (class or regular) method/constructor/destructor implementation
  40. from str, as if it appeared in the current unit's implementation section
  41. WARNING: save the scanner state before calling this routine, and restore
  42. when done. }
  43. function str_parse_method_impl(str: ansistring; usefwpd: tprocdef; is_classdef: boolean):boolean;
  44. { in the JVM, constructors are not automatically inherited (so you can hide
  45. them). To emulate the Pascal behaviour, we have to automatically add
  46. all parent constructors to the current class as well.}
  47. procedure add_missing_parent_constructors_intf(obj: tobjectdef);
  48. { goes through all defs in st to add implementations for synthetic methods
  49. added earlier }
  50. procedure add_synthetic_method_implementations(st: tsymtable);
  51. procedure finish_copied_procdef(var pd: tprocdef; const realname: string; newparentst: tsymtable; newstruct: tabstractrecorddef);
  52. implementation
  53. uses
  54. cutils,verbose,systems,comphook,
  55. symtype,symsym,symtable,defutil,
  56. pbase,pdecobj,pdecsub,psub,
  57. defcmp;
  58. procedure replace_scanner(const tempname: string; out sstate: tscannerstate);
  59. begin
  60. { would require saving of idtoken, pattern etc }
  61. if (token=_ID) then
  62. internalerror(2011032201);
  63. sstate.old_scanner:=current_scanner;
  64. sstate.old_token:=token;
  65. sstate.old_c:=c;
  66. sstate.valid:=true;
  67. current_scanner:=tscannerfile.Create('_Macro_.'+tempname);
  68. end;
  69. procedure restore_scanner(const sstate: tscannerstate);
  70. begin
  71. if sstate.valid then
  72. begin
  73. current_scanner.free;
  74. current_scanner:=sstate.old_scanner;
  75. token:=sstate.old_token;
  76. c:=sstate.old_c;
  77. end;
  78. end;
  79. function str_parse_method_dec(str: ansistring; potype: tproctypeoption; is_classdef: boolean; astruct: tabstractrecorddef; out pd: tprocdef): boolean;
  80. var
  81. oldparse_only: boolean;
  82. begin
  83. Message1(parser_d_internal_parser_string,str);
  84. oldparse_only:=parse_only;
  85. parse_only:=true;
  86. result:=false;
  87. { inject the string in the scanner }
  88. str:=str+'end;';
  89. current_scanner.substitutemacro('meth_head_macro',@str[1],length(str),current_scanner.line_no,current_scanner.inputfile.ref_index);
  90. current_scanner.readtoken(false);
  91. { and parse it... }
  92. case potype of
  93. potype_class_constructor:
  94. pd:=class_constructor_head(astruct);
  95. potype_class_destructor:
  96. pd:=class_destructor_head(astruct);
  97. potype_constructor:
  98. pd:=constructor_head;
  99. potype_destructor:
  100. pd:=destructor_head;
  101. else
  102. pd:=method_dec(astruct,is_classdef);
  103. end;
  104. if assigned(pd) then
  105. result:=true;
  106. parse_only:=oldparse_only;
  107. end;
  108. function str_parse_method_impl(str: ansistring; usefwpd: tprocdef; is_classdef: boolean):boolean;
  109. var
  110. oldparse_only: boolean;
  111. tmpstr: ansistring;
  112. begin
  113. if ((status.verbosity and v_debug)<>0) then
  114. begin
  115. if assigned(usefwpd) then
  116. Message1(parser_d_internal_parser_string,usefwpd.customprocname([pno_proctypeoption,pno_paranames,pno_noclassmarker,pno_noleadingdollar]))
  117. else
  118. begin
  119. if is_classdef then
  120. tmpstr:='class '
  121. else
  122. tmpstr:='';
  123. Message1(parser_d_internal_parser_string,tmpstr+str);
  124. end;
  125. end;
  126. oldparse_only:=parse_only;
  127. parse_only:=false;
  128. result:=false;
  129. { inject the string in the scanner }
  130. str:=str+'end;';
  131. current_scanner.substitutemacro('meth_impl_macro',@str[1],length(str),current_scanner.line_no,current_scanner.inputfile.ref_index);
  132. current_scanner.readtoken(false);
  133. { and parse it... }
  134. read_proc(is_classdef,usefwpd);
  135. parse_only:=oldparse_only;
  136. result:=true;
  137. end;
  138. procedure add_missing_parent_constructors_intf(obj: tobjectdef);
  139. var
  140. parent: tobjectdef;
  141. def: tdef;
  142. parentpd,
  143. childpd: tprocdef;
  144. i: longint;
  145. srsym: tsym;
  146. srsymtable: tsymtable;
  147. begin
  148. if (oo_is_external in obj.objectoptions) or
  149. not assigned(obj.childof) then
  150. exit;
  151. parent:=obj.childof;
  152. { find all constructor in the parent }
  153. for i:=0 to tobjectsymtable(parent.symtable).deflist.count-1 do
  154. begin
  155. def:=tdef(tobjectsymtable(parent.symtable).deflist[i]);
  156. if (def.typ<>procdef) or
  157. (tprocdef(def).proctypeoption<>potype_constructor) or
  158. not is_visible_for_object(tprocdef(def),obj) then
  159. continue;
  160. parentpd:=tprocdef(def);
  161. { do we have this constructor too? (don't use
  162. search_struct_member/searchsym_in_class, since those will
  163. search parents too) }
  164. if searchsym_in_record(obj,parentpd.procsym.name,srsym,srsymtable) then
  165. begin
  166. { there's a symbol with the same name, is it a constructor
  167. with the same parameters? }
  168. if srsym.typ=procsym then
  169. begin
  170. childpd:=tprocsym(srsym).find_procdef_bytype_and_para(
  171. potype_constructor,parentpd.paras,nil,
  172. [cpo_ignorehidden,cpo_ignoreuniv,cpo_openequalisexact]);
  173. if assigned(childpd) then
  174. continue;
  175. end;
  176. end;
  177. { if we get here, we did not find it in the current objectdef ->
  178. add }
  179. childpd:=tprocdef(parentpd.getcopy);
  180. finish_copied_procdef(childpd,parentpd.procsym.realname,obj.symtable,obj);
  181. exclude(childpd.procoptions,po_external);
  182. include(childpd.procoptions,po_overload);
  183. childpd.synthetickind:=tsk_anon_inherited;
  184. include(obj.objectoptions,oo_has_constructor);
  185. end;
  186. end;
  187. procedure implement_anon_inherited(pd: tprocdef);
  188. var
  189. str: ansistring;
  190. isclassmethod: boolean;
  191. begin
  192. isclassmethod:=
  193. (po_classmethod in pd.procoptions) and
  194. not(pd.proctypeoption in [potype_constructor,potype_destructor]);
  195. str:='begin inherited end;';
  196. str_parse_method_impl(str,pd,isclassmethod);
  197. end;
  198. procedure implement_jvm_clone(pd: tprocdef);
  199. var
  200. struct: tabstractrecorddef;
  201. str: ansistring;
  202. i: longint;
  203. sym: tsym;
  204. fsym: tfieldvarsym;
  205. begin
  206. if not(pd.struct.typ in [recorddef,objectdef]) then
  207. internalerror(2011032802);
  208. struct:=pd.struct;
  209. { anonymous record types must get an artificial name, so we can generate
  210. a typecast at the scanner level }
  211. if (struct.typ=recorddef) and
  212. not assigned(struct.typesym) then
  213. internalerror(2011032812);
  214. { the inherited clone will already copy all fields in a shallow way ->
  215. copy records/regular arrays in a regular way }
  216. str:='begin clone:=inherited;';
  217. for i:=0 to struct.symtable.symlist.count-1 do
  218. begin
  219. sym:=tsym(struct.symtable.symlist[i]);
  220. if (sym.typ=fieldvarsym) then
  221. begin
  222. fsym:=tfieldvarsym(sym);
  223. if (fsym.vardef.typ=recorddef) or
  224. ((fsym.vardef.typ=arraydef) and
  225. not is_dynamic_array(fsym.vardef)) or
  226. ((fsym.vardef.typ=setdef) and
  227. not is_smallset(fsym.vardef)) then
  228. str:=str+struct.typesym.realname+'(clone).'+fsym.realname+':='+fsym.realname+';';
  229. end;
  230. end;
  231. str:=str+'end;';
  232. str_parse_method_impl(str,pd,false);
  233. end;
  234. procedure implement_record_deepcopy(pd: tprocdef);
  235. var
  236. struct: tabstractrecorddef;
  237. str: ansistring;
  238. i: longint;
  239. sym: tsym;
  240. fsym: tfieldvarsym;
  241. begin
  242. if not(pd.struct.typ in [recorddef,objectdef]) then
  243. internalerror(2011032810);
  244. struct:=pd.struct;
  245. { anonymous record types must get an artificial name, so we can generate
  246. a typecast at the scanner level }
  247. if (struct.typ=recorddef) and
  248. not assigned(struct.typesym) then
  249. internalerror(2011032811);
  250. { copy all fields }
  251. str:='begin ';
  252. for i:=0 to struct.symtable.symlist.count-1 do
  253. begin
  254. sym:=tsym(struct.symtable.symlist[i]);
  255. if (sym.typ=fieldvarsym) then
  256. begin
  257. fsym:=tfieldvarsym(sym);
  258. str:=str+'result.'+fsym.realname+':='+fsym.realname+';';
  259. end;
  260. end;
  261. str:=str+'end;';
  262. str_parse_method_impl(str,pd,false);
  263. end;
  264. procedure implement_empty(pd: tprocdef);
  265. var
  266. str: ansistring;
  267. isclassmethod: boolean;
  268. begin
  269. isclassmethod:=
  270. (po_classmethod in pd.procoptions) and
  271. not(pd.proctypeoption in [potype_constructor,potype_destructor]);
  272. str:='begin end;';
  273. str_parse_method_impl(str,pd,isclassmethod);
  274. end;
  275. procedure add_synthetic_method_implementations_for_struct(struct: tabstractrecorddef);
  276. var
  277. i : longint;
  278. def : tdef;
  279. pd : tprocdef;
  280. begin
  281. for i:=0 to struct.symtable.deflist.count-1 do
  282. begin
  283. def:=tdef(struct.symtable.deflist[i]);
  284. if (def.typ<>procdef) then
  285. continue;
  286. pd:=tprocdef(def);
  287. case pd.synthetickind of
  288. tsk_none:
  289. ;
  290. tsk_anon_inherited:
  291. implement_anon_inherited(pd);
  292. tsk_jvm_clone:
  293. implement_jvm_clone(pd);
  294. tsk_record_deepcopy:
  295. implement_record_deepcopy(pd);
  296. tsk_empty,
  297. { special handling for this one is done in tnodeutils.wrap_proc_body }
  298. tsk_tcinit:
  299. implement_empty(pd);
  300. else
  301. internalerror(2011032801);
  302. end;
  303. end;
  304. end;
  305. procedure add_synthetic_method_implementations(st: tsymtable);
  306. var
  307. i: longint;
  308. def: tdef;
  309. sstate: tscannerstate;
  310. begin
  311. { only necessary for the JVM target currently }
  312. if not (target_info.system in [system_jvm_java32]) then
  313. exit;
  314. sstate.valid:=false;
  315. for i:=0 to st.deflist.count-1 do
  316. begin
  317. def:=tdef(st.deflist[i]);
  318. if (is_javaclass(def) and
  319. not(oo_is_external in tobjectdef(def).objectoptions)) or
  320. (def.typ=recorddef) then
  321. begin
  322. if not sstate.valid then
  323. replace_scanner('synthetic_impl',sstate);
  324. add_synthetic_method_implementations_for_struct(tabstractrecorddef(def));
  325. { also complete nested types }
  326. add_synthetic_method_implementations(tabstractrecorddef(def).symtable);
  327. end;
  328. end;
  329. restore_scanner(sstate);
  330. end;
  331. procedure finish_copied_procdef(var pd: tprocdef; const realname: string; newparentst: tsymtable; newstruct: tabstractrecorddef);
  332. var
  333. sym: tsym;
  334. parasym: tparavarsym;
  335. ps: tprocsym;
  336. hdef: tdef;
  337. stname: string;
  338. i: longint;
  339. begin
  340. { associate the procdef with a procsym in the owner }
  341. if not(pd.proctypeoption in [potype_class_constructor,potype_class_destructor]) then
  342. stname:=upper(realname)
  343. else
  344. stname:=lower(realname);
  345. sym:=tsym(newparentst.find(stname));
  346. if assigned(sym) then
  347. begin
  348. if sym.typ<>procsym then
  349. internalerror(2011040601);
  350. ps:=tprocsym(sym);
  351. end
  352. else
  353. begin
  354. ps:=tprocsym.create(realname);
  355. newparentst.insert(ps);
  356. end;
  357. pd.procsym:=ps;
  358. pd.struct:=newstruct;
  359. { in case of methods, replace the special parameter types with new ones }
  360. if assigned(newstruct) then
  361. begin
  362. symtablestack.push(pd.parast);
  363. for i:=0 to pd.paras.count-1 do
  364. begin
  365. parasym:=tparavarsym(pd.paras[i]);
  366. if vo_is_self in parasym.varoptions then
  367. begin
  368. if parasym.vardef.typ=classrefdef then
  369. parasym.vardef:=tclassrefdef.create(newstruct)
  370. else
  371. parasym.vardef:=newstruct;
  372. end
  373. end;
  374. { also fix returndef in case of a constructor }
  375. if pd.proctypeoption=potype_constructor then
  376. pd.returndef:=newstruct;
  377. symtablestack.pop(pd.parast);
  378. end;
  379. proc_add_definition(pd);
  380. end;
  381. end.