symcreat.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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:=
  177. (po_classmethod in pd.procoptions) and
  178. not(pd.proctypeoption in [potype_constructor,potype_destructor]);
  179. str:=pd.customprocname([pno_proctypeoption,pno_paranames,pno_ownername,pno_noclassmarker,pno_noleadingdollar]);
  180. str:=str+'begin inherited end;';
  181. str_parse_method_impl(str,isclassmethod);
  182. end;
  183. procedure implement_jvm_clone(pd: tprocdef);
  184. var
  185. struct: tabstractrecorddef;
  186. str: ansistring;
  187. i: longint;
  188. sym: tsym;
  189. fsym: tfieldvarsym;
  190. begin
  191. if not(pd.owner.defowner.typ in [recorddef,objectdef]) then
  192. internalerror(2011032802);
  193. struct:=tabstractrecorddef(pd.owner.defowner);
  194. { anonymous record types must get an artificial name, so we can generate
  195. a typecast at the scanner level }
  196. if (struct.typ=recorddef) and
  197. not assigned(struct.typesym) then
  198. internalerror(2011032812);
  199. str:=pd.customprocname([pno_proctypeoption,pno_paranames,pno_ownername,pno_noclassmarker,pno_noleadingdollar]);
  200. { the inherited clone will already copy all fields in a shallow way ->
  201. copy records/regular arrays in a regular way }
  202. str:=str+'begin result:=inherited;';
  203. for i:=0 to struct.symtable.symlist.count-1 do
  204. begin
  205. sym:=tsym(struct.symtable.symlist[i]);
  206. if (sym.typ=fieldvarsym) then
  207. begin
  208. fsym:=tfieldvarsym(sym);
  209. if (fsym.vardef.typ=recorddef) or
  210. ((fsym.vardef.typ=arraydef) and
  211. not is_dynamic_array(fsym.vardef)) or
  212. ((fsym.vardef.typ=setdef) and
  213. not is_smallset(fsym.vardef)) then
  214. str:=str+struct.typesym.realname+'(result).'+fsym.realname+':='+fsym.realname+';';
  215. end;
  216. end;
  217. str:=str+'end;';
  218. str_parse_method_impl(str,false);
  219. end;
  220. procedure implement_record_deepcopy(pd: tprocdef);
  221. var
  222. struct: tabstractrecorddef;
  223. str: ansistring;
  224. i: longint;
  225. sym: tsym;
  226. fsym: tfieldvarsym;
  227. begin
  228. if not(pd.owner.defowner.typ in [recorddef,objectdef]) then
  229. internalerror(2011032810);
  230. struct:=tabstractrecorddef(pd.owner.defowner);
  231. { anonymous record types must get an artificial name, so we can generate
  232. a typecast at the scanner level }
  233. if (struct.typ=recorddef) and
  234. not assigned(struct.typesym) then
  235. internalerror(2011032811);
  236. str:=pd.customprocname([pno_proctypeoption,pno_paranames,pno_ownername,pno_noclassmarker,pno_noleadingdollar]);
  237. { copy all fields }
  238. str:=str+'begin ';
  239. for i:=0 to struct.symtable.symlist.count-1 do
  240. begin
  241. sym:=tsym(struct.symtable.symlist[i]);
  242. if (sym.typ=fieldvarsym) then
  243. begin
  244. fsym:=tfieldvarsym(sym);
  245. str:=str+'result.'+fsym.realname+':='+fsym.realname+';';
  246. end;
  247. end;
  248. str:=str+'end;';
  249. str_parse_method_impl(str,false);
  250. end;
  251. procedure implement_empty(pd: tprocdef);
  252. var
  253. str: ansistring;
  254. isclassmethod: boolean;
  255. begin
  256. isclassmethod:=
  257. (po_classmethod in pd.procoptions) and
  258. not(pd.proctypeoption in [potype_constructor,potype_destructor]);
  259. str:=pd.customprocname([pno_proctypeoption,pno_paranames,pno_ownername,pno_noclassmarker,pno_noleadingdollar]);
  260. str:=str+'begin end;';
  261. str_parse_method_impl(str,isclassmethod);
  262. end;
  263. procedure add_synthetic_method_implementations_for_struct(struct: tabstractrecorddef);
  264. var
  265. i : longint;
  266. def : tdef;
  267. pd : tprocdef;
  268. begin
  269. for i:=0 to struct.symtable.deflist.count-1 do
  270. begin
  271. def:=tdef(struct.symtable.deflist[i]);
  272. if (def.typ<>procdef) then
  273. continue;
  274. pd:=tprocdef(def);
  275. case pd.synthetickind of
  276. tsk_none:
  277. ;
  278. tsk_anon_inherited:
  279. implement_anon_inherited(pd);
  280. tsk_jvm_clone:
  281. implement_jvm_clone(pd);
  282. tsk_record_deepcopy:
  283. implement_record_deepcopy(pd);
  284. tsk_empty,
  285. { special handling for this one is done in tnodeutils.wrap_proc_body }
  286. tsk_tcinit:
  287. implement_empty(pd);
  288. else
  289. internalerror(2011032801);
  290. end;
  291. end;
  292. end;
  293. procedure add_synthetic_method_implementations(st: tsymtable);
  294. var
  295. i: longint;
  296. def: tdef;
  297. sstate: tscannerstate;
  298. begin
  299. { only necessary for the JVM target currently }
  300. if not (target_info.system in [system_jvm_java32]) then
  301. exit;
  302. sstate.valid:=false;
  303. for i:=0 to st.deflist.count-1 do
  304. begin
  305. def:=tdef(st.deflist[i]);
  306. if (is_javaclass(def) and
  307. not(oo_is_external in tobjectdef(def).objectoptions)) or
  308. (def.typ=recorddef) then
  309. begin
  310. if not sstate.valid then
  311. replace_scanner('synthetic_impl',sstate);
  312. add_synthetic_method_implementations_for_struct(tabstractrecorddef(def));
  313. { also complete nested types }
  314. add_synthetic_method_implementations(tabstractrecorddef(def).symtable);
  315. end;
  316. end;
  317. restore_scanner(sstate);
  318. end;
  319. end.