1
- import { FunctionJp } from "@specs-feup/clava/api/Joinpoints.js" ;
1
+ import { FunctionJp , Joinpoint , Statement } from "@specs-feup/clava/api/Joinpoints.js" ;
2
2
import { AHandler } from "./AHandler.js" ;
3
3
import ClavaJoinPoints from "@specs-feup/clava/api/clava/ClavaJoinPoints.js" ;
4
4
import Clava from "@specs-feup/clava/api/clava/Clava.js" ;
5
+ import Query from "@specs-feup/lara/api/weaver/Query.js" ;
5
6
6
7
export class ReimplementableHandler extends AHandler {
7
8
constructor ( libraryPrefix : string , additionalSources : string [ ] ) {
@@ -10,16 +11,49 @@ export class ReimplementableHandler extends AHandler {
10
11
for ( const source of additionalSources ) {
11
12
const file = ClavaJoinPoints . file ( source ) ;
12
13
Clava . addFile ( file ) ;
14
+ //Clava.rebuild();
13
15
}
14
16
}
15
17
16
18
protected buildFunctionImpl ( signature : Record < string , any > , newSig : FunctionJp ) : FunctionJp {
17
19
const newFun = newSig . copy ( ) as FunctionJp ;
18
20
19
- const comment = ClavaJoinPoints . comment ( "TODO: Implement this function" ) ;
21
+ const stmts : Joinpoint [ ] = [ ] ;
22
+ const impl = this . findExitstingImpl ( signature ) ;
20
23
21
- const scope = ClavaJoinPoints . scope ( comment ) ;
24
+ if ( impl . length > 0 ) {
25
+ stmts . push ( ...impl ) ;
26
+ }
27
+ else {
28
+ const comment = ClavaJoinPoints . comment ( "TODO: Implement this function" ) ;
29
+ stmts . push ( comment ) ;
30
+ }
31
+
32
+ const scope = ClavaJoinPoints . scope ( ...stmts ) ;
22
33
newFun . setBody ( scope ) ;
23
34
return newFun ;
24
35
}
36
+
37
+ private findExitstingImpl ( signature : Record < string , any > ) : Joinpoint [ ] {
38
+ const jps : Joinpoint [ ] = [ ] ;
39
+
40
+ for ( const fun of Query . search ( FunctionJp , { name : signature . name } ) ) {
41
+ console . log ( fun ) ;
42
+ if ( ! fun . isImplementation ) {
43
+ continue ;
44
+ }
45
+ if ( fun . params . length !== signature . parameters . length ) {
46
+ continue ;
47
+ }
48
+ if ( fun . returnType . code !== signature . returnType ) {
49
+ continue ;
50
+ }
51
+ for ( const child of fun . body . children ) {
52
+ const stmt = ClavaJoinPoints . stmtLiteral ( child . code ) ;
53
+ jps . push ( stmt ) ;
54
+ return jps ;
55
+ }
56
+ }
57
+ return jps ;
58
+ }
25
59
}
0 commit comments