comparison graal/com.oracle.truffle.api.dsl.test/src/com/oracle/truffle/api/dsl/test/ImportGuardsTest.java @ 16832:13cf9b6b325c

Truffle-DSL: implemented import guards feature.
author Christian Humer <christian.humer@gmail.com>
date Thu, 14 Aug 2014 16:49:18 +0200
parents
children 2db61eddcb97
comparison
equal deleted inserted replaced
16830:c3c07046a74b 16832:13cf9b6b325c
1 /*
2 * Copyright (c) 2012, 2013, 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.dsl.test;
24
25 import static com.oracle.truffle.api.dsl.test.TestHelper.*;
26
27 import org.junit.*;
28
29 import com.oracle.truffle.api.dsl.*;
30 import com.oracle.truffle.api.dsl.test.ImportGuardsTestFactory.ImportGuards6Factory;
31 import com.oracle.truffle.api.dsl.test.TypeSystemTest.ValueNode;
32
33 public class ImportGuardsTest {
34
35 @ImportGuards(Imports0.class)
36 @NodeChild("a")
37 static class ImportGuards0 extends ValueNode {
38
39 @Specialization(guards = "staticGuard")
40 int f0(int a) {
41 return a;
42 }
43 }
44
45 @NodeChild("a")
46 @ImportGuards(Imports0.class)
47 static class ImportGuards1 extends ValueNode {
48
49 @ExpectError("No compatible guard with method name 'nonStaticGuard' found. Please note that all signature types of the method guard must be declared in the type system.")
50 @Specialization(guards = "nonStaticGuard")
51 int f1(int a) {
52 return a;
53 }
54
55 @ExpectError("No compatible guard with method name 'protectedGuard' found. Please note that all signature types of the method guard must be declared in the type system.")
56 @Specialization(guards = "protectedGuard")
57 int f2(int a) {
58 return a;
59 }
60
61 @ExpectError("No compatible guard with method name 'packageGuard' found. Please note that all signature types of the method guard must be declared in the type system.")
62 @Specialization(guards = "packageGuard")
63 int f3(int a) {
64 return a;
65 }
66
67 @ExpectError("No compatible guard with method name 'privateGuard' found. Please note that all signature types of the method guard must be declared in the type system.")
68 @Specialization(guards = "privateGuard")
69 int f4(int a) {
70 return a;
71 }
72 }
73
74 public static class Imports0 {
75 public static boolean staticGuard(int a) {
76 return a == 0;
77 }
78
79 public boolean nonStaticGuard(int a) {
80 return a == 0;
81 }
82
83 protected static boolean protectedGuard(int a) {
84 return a == 0;
85 }
86
87 static boolean packageGuard(int a) {
88 return a == 0;
89 }
90
91 @SuppressWarnings("unused")
92 private static boolean privateGuard(int a) {
93 return a == 0;
94 }
95
96 }
97
98 @ExpectError("The specified import guard class 'com.oracle.truffle.api.dsl.test.ImportGuardsTest.Imports1' must be public.")
99 @NodeChild("a")
100 @ImportGuards(Imports1.class)
101 static class ImportGuards2 extends ValueNode {
102
103 int do1(int a) {
104 return a;
105 }
106 }
107
108 static class Imports1 {
109
110 }
111
112 @ExpectError("The specified import guard class 'com.oracle.truffle.api.dsl.test.ImportGuardsTest.Imports2' must be public.")
113 @NodeChild("a")
114 @ImportGuards(Imports2.class)
115 static class ImportGuards3 extends ValueNode {
116
117 int do1(int a) {
118 return a;
119 }
120 }
121
122 @ExpectError("The specified import guard class 'boolean' is not a declared type.")
123 @NodeChild("a")
124 @ImportGuards(boolean.class)
125 static class ImportGuards4 extends ValueNode {
126
127 int do1(int a) {
128 return a;
129 }
130 }
131
132 private static class Imports2 {
133
134 }
135
136 @ExpectError("At least import guard classes must be specified.")
137 @NodeChild("a")
138 @ImportGuards({})
139 static class ImportGuards5 extends ValueNode {
140
141 int do1(int a) {
142 return a;
143 }
144 }
145
146 @Test
147 public void testImportGuards6() {
148 // should use the guar declared in the node instead of the imported one.
149 assertRuns(ImportGuards6Factory.getInstance(), //
150 array(1, 1), //
151 array(1, 1));
152 }
153
154 @ImportGuards(Imports0.class)
155 @NodeChild("a")
156 static class ImportGuards6 extends ValueNode {
157
158 static boolean staticGuard(int a) {
159 return a == 1;
160 }
161
162 @Specialization(guards = "staticGuard")
163 int f0(int a) {
164 return a;
165 }
166 }
167
168 }