symcreat.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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; 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; 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. // procedure add_missing_parent_constructors_impl(obj: tobjectdef);
  49. { goes through all defs in st to add implementations for synthetic methods
  50. added earlier }
  51. procedure add_synthetic_method_implementations(st: tsymtable);
  52. implementation
  53. uses
  54. verbose,systems,
  55. symtype,symsym,symtable,defutil,
  56. pbase,pdecobj,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; 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. pd:=method_dec(astruct,is_classdef);
  93. if assigned(pd) then
  94. result:=true;
  95. parse_only:=oldparse_only;
  96. end;
  97. function str_parse_method_impl(str: ansistring; is_classdef: boolean):boolean;
  98. var
  99. oldparse_only: boolean;
  100. begin
  101. Message1(parser_d_internal_parser_string,str);
  102. oldparse_only:=parse_only;
  103. parse_only:=false;
  104. result:=false;
  105. { inject the string in the scanner }
  106. str:=str+'end;';
  107. current_scanner.substitutemacro('meth_impl_macro',@str[1],length(str),current_scanner.line_no,current_scanner.inputfile.ref_index);
  108. current_scanner.readtoken(false);
  109. { and parse it... }
  110. read_proc(is_classdef);
  111. parse_only:=oldparse_only;
  112. result:=true;
  113. end;
  114. procedure add_missing_parent_constructors_intf(obj: tobjectdef);
  115. var
  116. parent: tobjectdef;
  117. def: tdef;
  118. pd: tprocdef;
  119. newpd,
  120. parentpd: tprocdef;
  121. i: longint;
  122. srsym: tsym;
  123. srsymtable: tsymtable;
  124. isclassmethod: boolean;
  125. str: ansistring;
  126. sstate: tscannerstate;
  127. begin
  128. if not assigned(obj.childof) then
  129. exit;
  130. sstate.valid:=false;
  131. parent:=obj.childof;
  132. { find all constructor in the parent }
  133. for i:=0 to tobjectsymtable(parent.symtable).deflist.count-1 do
  134. begin
  135. def:=tdef(tobjectsymtable(parent.symtable).deflist[i]);
  136. if (def.typ<>procdef) or
  137. (tprocdef(def).proctypeoption<>potype_constructor) then
  138. continue;
  139. pd:=tprocdef(def);
  140. { do we have this constructor too? (don't use
  141. search_struct_member/searchsym_in_class, since those will
  142. search parents too) }
  143. if searchsym_in_record(obj,pd.procsym.name,srsym,srsymtable) then
  144. begin
  145. { there's a symbol with the same name, is it a constructor
  146. with the same parameters? }
  147. if srsym.typ=procsym then
  148. begin
  149. parentpd:=tprocsym(srsym).find_procdef_bytype_and_para(
  150. potype_constructor,pd.paras,tprocdef(def).returndef,
  151. [cpo_ignorehidden,cpo_ignoreuniv,cpo_openequalisexact]);
  152. if assigned(parentpd) then
  153. continue;
  154. end;
  155. end;
  156. { if we get here, we did not find it in the current objectdef ->
  157. add }
  158. if not sstate.valid then
  159. replace_scanner('parent_constructors_intf',sstate);
  160. isclassmethod:=
  161. (po_classmethod in tprocdef(pd).procoptions) and
  162. not(tprocdef(pd).proctypeoption in [potype_constructor,potype_destructor]);
  163. { + 'overload' for Delphi modes }
  164. str:=tprocdef(pd).customprocname([pno_proctypeoption,pno_paranames,pno_noclassmarker,pno_noleadingdollar])+'overload;';
  165. if not str_parse_method_dec(str,isclassmethod,obj,newpd) then
  166. internalerror(2011032001);
  167. newpd.synthetickind:=tsk_anon_inherited;
  168. end;
  169. restore_scanner(sstate);
  170. end;
  171. procedure implement_anon_inherited(pd: tprocdef);
  172. var
  173. str: ansistring;
  174. isclassmethod: boolean;
  175. begin
  176. isclassmethod:=(po_classmethod in pd.procoptions);
  177. str:=pd.customprocname([pno_proctypeoption,pno_paranames,pno_ownername,pno_noclassmarker,pno_noleadingdollar]);
  178. str:=str+'begin inherited end;';
  179. str_parse_method_impl(str,isclassmethod);
  180. end;
  181. procedure implement_jvm_clone(pd: tprocdef);
  182. var
  183. struct: tabstractrecorddef;
  184. str: ansistring;
  185. i: longint;
  186. sym: tsym;
  187. fsym: tfieldvarsym;
  188. begin
  189. if not(pd.owner.defowner.typ in [recorddef,objectdef]) then
  190. internalerror(2011032802);
  191. struct:=tabstractrecorddef(pd.owner.defowner);
  192. { anonymous record types must get an artificial name, so we can generate
  193. a typecast at the scanner level }
  194. if (struct.typ=recorddef) and
  195. not assigned(struct.typesym) then
  196. internalerror(2011032812);
  197. str:=pd.customprocname([pno_proctypeoption,pno_paranames,pno_ownername,pno_noclassmarker,pno_noleadingdollar]);
  198. { the inherited clone will already copy all fields in a shallow way ->
  199. copy records/regular arrays in a regular way }
  200. str:=str+'begin result:=inherited;';
  201. for i:=0 to struct.symtable.symlist.count-1 do
  202. begin
  203. sym:=tsym(struct.symtable.symlist[i]);
  204. if (sym.typ=fieldvarsym) then
  205. begin
  206. fsym:=tfieldvarsym(sym);
  207. if (fsym.vardef.typ=recorddef) or
  208. ((fsym.vardef.typ=arraydef) and
  209. not is_dynamic_array(fsym.vardef)) or
  210. ((fsym.vardef.typ=setdef) and
  211. not is_smallset(fsym.vardef)) then
  212. str:=str+struct.typesym.realname+'(result).'+fsym.realname+':='+fsym.realname+';';
  213. end;
  214. end;
  215. str:=str+'end;';
  216. str_parse_method_impl(str,false);
  217. end;
  218. procedure implement_record_deepcopy(pd: tprocdef);
  219. var
  220. struct: tabstractrecorddef;
  221. str: ansistring;
  222. i: longint;
  223. sym: tsym;
  224. fsym: tfieldvarsym;
  225. begin
  226. if not(pd.owner.defowner.typ in [recorddef,objectdef]) then
  227. internalerror(2011032810);
  228. struct:=tabstractrecorddef(pd.owner.defowner);
  229. { anonymous record types must get an artificial name, so we can generate
  230. a typecast at the scanner level }
  231. if (struct.typ=recorddef) and
  232. not assigned(struct.typesym) then
  233. internalerror(2011032811);
  234. str:=pd.customprocname([pno_proctypeoption,pno_paranames,pno_ownername,pno_noclassmarker,pno_noleadingdollar]);
  235. { copy all fields }
  236. str:=str+'begin ';
  237. for i:=0 to struct.symtable.symlist.count-1 do
  238. begin
  239. sym:=tsym(struct.symtable.symlist[i]);
  240. if (sym.typ=fieldvarsym) then
  241. begin
  242. fsym:=tfieldvarsym(sym);
  243. str:=str+'result.'+fsym.realname+':='+fsym.realname+';';
  244. end;
  245. end;
  246. str:=str+'end;';
  247. str_parse_method_impl(str,false);
  248. end;
  249. procedure implement_empty(pd: tprocdef);
  250. var
  251. str: ansistring;
  252. isclassmethod: boolean;
  253. begin
  254. isclassmethod:=
  255. (po_classmethod in pd.procoptions) and
  256. not(pd.proctypeoption in [potype_constructor,potype_destructor]);
  257. str:=pd.customprocname([pno_proctypeoption,pno_paranames,pno_ownername,pno_noclassmarker,pno_noleadingdollar]);
  258. str:=str+'begin end;';
  259. str_parse_method_impl(str,isclassmethod);
  260. end;
  261. procedure add_synthetic_method_implementations_for_struct(struct: tabstractrecorddef);
  262. var
  263. i : longint;
  264. def : tdef;
  265. pd : tprocdef;
  266. begin
  267. for i:=0 to struct.symtable.deflist.count-1 do
  268. begin
  269. def:=tdef(struct.symtable.deflist[i]);
  270. if (def.typ<>procdef) then
  271. continue;
  272. pd:=tprocdef(def);
  273. case pd.synthetickind of
  274. tsk_none:
  275. ;
  276. tsk_anon_inherited:
  277. implement_anon_inherited(pd);
  278. tsk_jvm_clone:
  279. implement_jvm_clone(pd);
  280. tsk_record_deepcopy:
  281. implement_record_deepcopy(pd);
  282. tsk_empty,
  283. { special handling for this one is done in tnodeutils.wrap_proc_body }
  284. tsk_tcinit:
  285. implement_empty(pd);
  286. else
  287. internalerror(2011032801);
  288. end;
  289. end;
  290. end;
  291. procedure add_synthetic_method_implementations(st: tsymtable);
  292. var
  293. i: longint;
  294. def: tdef;
  295. sstate: tscannerstate;
  296. begin
  297. { only necessary for the JVM target currently }
  298. if not (target_info.system in [system_jvm_java32]) then
  299. exit;
  300. sstate.valid:=false;
  301. for i:=0 to st.deflist.count-1 do
  302. begin
  303. def:=tdef(st.deflist[i]);
  304. if (is_javaclass(def) and
  305. not(oo_is_external in tobjectdef(def).objectoptions)) or
  306. (def.typ=recorddef) then
  307. begin
  308. if not sstate.valid then
  309. replace_scanner('synthetic_impl',sstate);
  310. add_synthetic_method_implementations_for_struct(tabstractrecorddef(def));
  311. { also complete nested types }
  312. add_synthetic_method_implementations(tabstractrecorddef(def).symtable);
  313. end;
  314. end;
  315. restore_scanner(sstate);
  316. end;
  317. end.