Mercurial > hg > graal-compiler
comparison graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/OnAdoptTest.java @ 15346:1cd02b4d90d1
onAdopt callback for ASTs
author | Michael Haupt <michael.haupt@oracle.com> |
---|---|
date | Wed, 23 Apr 2014 15:23:18 +0200 |
parents | |
children | 1cde96b96673 |
comparison
equal
deleted
inserted
replaced
15311:820c6d353358 | 15346:1cd02b4d90d1 |
---|---|
1 /* | |
2 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. | |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | |
4 * | |
5 * This code is free software; you can redistribute it and/or modify it | |
6 * under the terms of the GNU General Public License version 2 only, as | |
7 * published by the Free Software Foundation. | |
8 * | |
9 * This code is distributed in the hope that it will be useful, but WITHOUT | |
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
12 * version 2 for more details (a copy is included in the LICENSE file that | |
13 * accompanied this code). | |
14 * | |
15 * You should have received a copy of the GNU General Public License version | |
16 * 2 along with this work; if not, write to the Free Software Foundation, | |
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | |
18 * | |
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | |
20 * or visit www.oracle.com if you need additional information or have any | |
21 * questions. | |
22 */ | |
23 package com.oracle.truffle.api.test; | |
24 | |
25 import org.junit.*; | |
26 | |
27 import com.oracle.truffle.api.*; | |
28 import com.oracle.truffle.api.frame.*; | |
29 import com.oracle.truffle.api.nodes.*; | |
30 | |
31 /** | |
32 * <h3>Inserting Extra Nodes into the AST Transparently</h3> | |
33 * | |
34 * <p> | |
35 * The {@link Node} class provides a callback that is invoked whenever a node is adopted in an AST | |
36 * by insertion or replacement. Node classes can override the {@code onAdopt()} method to run extra | |
37 * functionality upon adoption. | |
38 * </p> | |
39 * | |
40 * <p> | |
41 * This test demonstrates how node instances of a specific class can be automatically wrapped in | |
42 * extra nodes when they are inserted into the AST. | |
43 * </p> | |
44 */ | |
45 public class OnAdoptTest { | |
46 | |
47 static class Root extends RootNode { | |
48 | |
49 @Child private Base child1; | |
50 @Child private Base child2; | |
51 | |
52 public Root(Base child1, Base child2) { | |
53 super(null); | |
54 this.child1 = child1; | |
55 this.child2 = child2; | |
56 } | |
57 | |
58 @Override | |
59 public Object execute(VirtualFrame frame) { | |
60 return child1.executeInt(frame) + child2.executeInt(frame); | |
61 } | |
62 | |
63 } | |
64 | |
65 abstract static class Base extends Node { | |
66 public abstract int executeInt(VirtualFrame frame); | |
67 } | |
68 | |
69 static class Wrapper extends Base { | |
70 | |
71 @Child private Base wrappee; | |
72 | |
73 public Wrapper(Base wrappee) { | |
74 this.wrappee = wrappee; | |
75 } | |
76 | |
77 @Override | |
78 public int executeInt(VirtualFrame frame) { | |
79 return 1 + wrappee.executeInt(frame); | |
80 } | |
81 | |
82 } | |
83 | |
84 abstract static class GenBase extends Base { | |
85 | |
86 private final int k; | |
87 | |
88 public GenBase(int k) { | |
89 this.k = k; | |
90 } | |
91 | |
92 @Override | |
93 public int executeInt(VirtualFrame frame) { | |
94 return k; | |
95 } | |
96 | |
97 } | |
98 | |
99 static class Gen extends GenBase { | |
100 public Gen(int k) { | |
101 super(k); | |
102 } | |
103 } | |
104 | |
105 static class GenWrapped extends GenBase { | |
106 | |
107 public GenWrapped(int k) { | |
108 super(k); | |
109 } | |
110 | |
111 @Override | |
112 protected void onAdopt() { | |
113 Wrapper w = new Wrapper(this); | |
114 this.replace(w); | |
115 } | |
116 | |
117 } | |
118 | |
119 @Test | |
120 public void testOnInsert() { | |
121 TruffleRuntime runtime = Truffle.getRuntime(); | |
122 Base b1 = new Gen(11); | |
123 Base b2 = new GenWrapped(11); | |
124 Root r = new Root(b1, b2); | |
125 CallTarget ct = runtime.createCallTarget(r); | |
126 Object result = ct.call(); | |
127 Assert.assertEquals(23, result); | |
128 } | |
129 | |
130 } |