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