symcreat.pas 12 KB

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