Skip to content

Commit 0b238b3

Browse files
committed
type check nodes
1 parent 01de4b4 commit 0b238b3

File tree

2 files changed

+235
-0
lines changed

2 files changed

+235
-0
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* The Universal Permissive License (UPL), Version 1.0
6+
*
7+
* Subject to the condition set forth below, permission is hereby granted to any
8+
* person obtaining a copy of this software, associated documentation and/or
9+
* data (collectively the "Software"), free of charge and under any and all
10+
* copyright rights in the Software, and any and all patent rights owned or
11+
* freely licensable by each licensor hereunder covering either (i) the
12+
* unmodified Software as contributed to or provided by such licensor, or (ii)
13+
* the Larger Works (as defined below), to deal in both
14+
*
15+
* (a) the Software, and
16+
*
17+
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
18+
* one is included with the Software each a "Larger Work" to which the Software
19+
* is contributed by such licensors),
20+
*
21+
* without restriction, including without limitation the rights to copy, create
22+
* derivative works of, display, perform, and distribute the Software and make,
23+
* use, sell, offer for sale, import, export, have made, and have sold the
24+
* Software and the Larger Work(s), and to sublicense the foregoing rights on
25+
* either these or other terms.
26+
*
27+
* This license is subject to the following condition:
28+
*
29+
* The above copyright notice and either this complete permission notice or at a
30+
* minimum a reference to the UPL must be included in all copies or substantial
31+
* portions of the Software.
32+
*
33+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
39+
* SOFTWARE.
40+
*/
41+
package com.oracle.graal.python.nodes.classes;
42+
43+
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
44+
import com.oracle.graal.python.builtins.objects.type.LazyPythonClass;
45+
import com.oracle.graal.python.builtins.objects.type.PythonBuiltinClass;
46+
import com.oracle.graal.python.builtins.objects.type.PythonClass;
47+
import com.oracle.graal.python.nodes.PNodeWithContext;
48+
import com.oracle.graal.python.nodes.object.IsBuiltinClassProfile;
49+
import com.oracle.truffle.api.dsl.Cached;
50+
import com.oracle.truffle.api.dsl.Specialization;
51+
import com.oracle.truffle.api.profiles.BranchProfile;
52+
import com.oracle.truffle.api.profiles.ConditionProfile;
53+
54+
public abstract class IsFixedSubtypeMRONode extends PNodeWithContext {
55+
56+
private final PythonBuiltinClassType clazz;
57+
58+
private final ConditionProfile equalsProfile = ConditionProfile.createBinaryProfile();
59+
private final ConditionProfile innerEqualsProfile = ConditionProfile.createBinaryProfile();
60+
private final BranchProfile falseProfile = BranchProfile.create();
61+
62+
protected IsFixedSubtypeMRONode(PythonBuiltinClassType clazz) {
63+
this.clazz = clazz;
64+
}
65+
66+
public static IsFixedSubtypeMRONode create(PythonBuiltinClassType type) {
67+
return IsFixedSubtypeMRONodeGen.create(type);
68+
}
69+
70+
public abstract boolean execute(LazyPythonClass derived);
71+
72+
@Specialization
73+
protected boolean isSubtype(PythonBuiltinClassType derived) {
74+
if (equalsProfile.profile(derived == clazz)) {
75+
return true;
76+
}
77+
PythonBuiltinClassType current = derived;
78+
while (current != PythonBuiltinClassType.PythonObject) {
79+
if (innerEqualsProfile.profile(derived == clazz)) {
80+
return true;
81+
}
82+
current = current.getBase();
83+
}
84+
falseProfile.enter();
85+
return false;
86+
}
87+
88+
@Specialization
89+
protected boolean isSubtype(PythonBuiltinClass derived) {
90+
return isSubtype(derived.getType());
91+
}
92+
93+
@Specialization
94+
protected boolean isSubtype(PythonClass derived,
95+
@Cached("create()") IsBuiltinClassProfile profile) {
96+
97+
for (PythonClass mro : derived.getMethodResolutionOrder()) {
98+
if (profile.profileClass(mro, clazz)) {
99+
return true;
100+
}
101+
}
102+
falseProfile.enter();
103+
return false;
104+
}
105+
}
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
/*
2+
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* The Universal Permissive License (UPL), Version 1.0
6+
*
7+
* Subject to the condition set forth below, permission is hereby granted to any
8+
* person obtaining a copy of this software, associated documentation and/or
9+
* data (collectively the "Software"), free of charge and under any and all
10+
* copyright rights in the Software, and any and all patent rights owned or
11+
* freely licensable by each licensor hereunder covering either (i) the
12+
* unmodified Software as contributed to or provided by such licensor, or (ii)
13+
* the Larger Works (as defined below), to deal in both
14+
*
15+
* (a) the Software, and
16+
*
17+
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
18+
* one is included with the Software each a "Larger Work" to which the Software
19+
* is contributed by such licensors),
20+
*
21+
* without restriction, including without limitation the rights to copy, create
22+
* derivative works of, display, perform, and distribute the Software and make,
23+
* use, sell, offer for sale, import, export, have made, and have sold the
24+
* Software and the Larger Work(s), and to sublicense the foregoing rights on
25+
* either these or other terms.
26+
*
27+
* This license is subject to the following condition:
28+
*
29+
* The above copyright notice and either this complete permission notice or at a
30+
* minimum a reference to the UPL must be included in all copies or substantial
31+
* portions of the Software.
32+
*
33+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
39+
* SOFTWARE.
40+
*/
41+
package com.oracle.graal.python.nodes.classes;
42+
43+
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
44+
import com.oracle.graal.python.builtins.objects.type.LazyPythonClass;
45+
import com.oracle.graal.python.builtins.objects.type.PythonBuiltinClass;
46+
import com.oracle.graal.python.builtins.objects.type.PythonClass;
47+
import com.oracle.graal.python.nodes.PNodeWithContext;
48+
import com.oracle.graal.python.nodes.object.IsBuiltinClassProfile;
49+
import com.oracle.truffle.api.dsl.Cached;
50+
import com.oracle.truffle.api.dsl.Specialization;
51+
import com.oracle.truffle.api.profiles.BranchProfile;
52+
import com.oracle.truffle.api.profiles.ConditionProfile;
53+
54+
public abstract class IsSubtypeMRONode extends PNodeWithContext {
55+
56+
private final ConditionProfile equalsProfile = ConditionProfile.createBinaryProfile();
57+
private final ConditionProfile innerEqualsProfile = ConditionProfile.createBinaryProfile();
58+
private final BranchProfile falseProfile = BranchProfile.create();
59+
60+
public static IsSubtypeMRONode create() {
61+
return IsSubtypeMRONodeGen.create();
62+
}
63+
64+
public abstract boolean execute(LazyPythonClass derived, LazyPythonClass clazz);
65+
66+
@Specialization
67+
protected boolean isSubtype(PythonBuiltinClassType derived, PythonBuiltinClassType clazz) {
68+
if (equalsProfile.profile(derived == clazz)) {
69+
return true;
70+
}
71+
PythonBuiltinClassType current = derived;
72+
while (current != PythonBuiltinClassType.PythonObject) {
73+
if (innerEqualsProfile.profile(derived == clazz)) {
74+
return true;
75+
}
76+
current = current.getBase();
77+
}
78+
falseProfile.enter();
79+
return false;
80+
}
81+
82+
@Specialization
83+
protected boolean isSubtype(PythonBuiltinClass derived, PythonBuiltinClassType clazz) {
84+
return isSubtype(derived.getType(), clazz);
85+
}
86+
87+
@Specialization
88+
protected boolean isSubtype(PythonBuiltinClassType derived, PythonBuiltinClass clazz) {
89+
return isSubtype(derived, clazz.getType());
90+
}
91+
92+
protected static boolean isBuiltinClass(PythonClass clazz) {
93+
return clazz instanceof PythonBuiltinClass;
94+
}
95+
96+
@Specialization(guards = "!isBuiltinClass(clazz)")
97+
protected static boolean isSubtype(@SuppressWarnings("unused") PythonBuiltinClassType derived, @SuppressWarnings("unused") PythonClass clazz) {
98+
return false; // a builtin class never derives from a non-builtin class
99+
}
100+
101+
@Specialization(guards = "!isBuiltinClass(clazz)")
102+
protected static boolean isSubtype(@SuppressWarnings("unused") PythonBuiltinClass derived, @SuppressWarnings("unused") PythonClass clazz) {
103+
return false; // a builtin class never derives from a non-builtin class
104+
}
105+
106+
@Specialization
107+
protected boolean isSubtype(PythonClass derived, PythonBuiltinClassType clazz,
108+
@Cached("create()") IsBuiltinClassProfile profile) {
109+
110+
for (PythonClass mro : derived.getMethodResolutionOrder()) {
111+
if (profile.profileClass(mro, clazz)) {
112+
return true;
113+
}
114+
}
115+
falseProfile.enter();
116+
return false;
117+
}
118+
119+
@Specialization
120+
protected boolean isSubtype(PythonClass derived, PythonClass clazz) {
121+
122+
for (PythonClass mro : derived.getMethodResolutionOrder()) {
123+
if (mro == clazz) {
124+
return true;
125+
}
126+
}
127+
falseProfile.enter();
128+
return false;
129+
}
130+
}

0 commit comments

Comments
 (0)