1
from pypy.translator.jvm import typesystem as jvmtype
2
from pypy.translator.jvm import generator as jvmgen
3
from pypy.rpython.ootypesystem import ootype
4
from pypy.translator.jvm.typesystem import \
5
jInt, jVoid, jStringBuilder, jString, jPyPy, jChar, jArrayList, jObject, \
6
jBool, jHashMap, jPyPyDictItemsIterator, Generifier, jCharSequence
8
# ______________________________________________________________________
9
# Mapping of built-in OOTypes to JVM types
11
class JvmBuiltInType(jvmtype.JvmClassType):
14
Represents built-in types to JVM. May optionally be associated
15
with an OOTYPE; if it is, then we will support lookup of the OOTYPE
16
methods and will re-map them as needed to the JVM equivalents.
19
def __init__(self, db, classty, OOTYPE):
20
jvmtype.JvmClassType.__init__(self, classty.name)
23
self.gen = Generifier(OOTYPE)
25
def __eq__(self, other):
26
return isinstance(other, JvmBuiltInType) and other.name == self.name
29
return hash(self.name)
31
def lookup_field(self, fieldnm):
32
""" Given a field name, returns a jvmgen.Field object """
33
_, FIELDTY = self.OOTYPE._lookup_field(fieldnm)
34
jfieldty = self.db.lltype_to_cts(FIELDTY)
36
self.descriptor.class_name(), fieldnm, jfieldty, False)
38
def lookup_method(self, methodnm):
39
""" Given the method name, returns a jvmgen.Method object """
41
# Look for a shortcut method in our table of remappings:
43
key = (self.OOTYPE.__class__, methodnm)
44
return built_in_methods[key]
47
# Otherwise, determine the Method object automagically
48
# First, map the OOTYPE arguments and results to
49
# the java types they will be at runtime. Note that
50
# we must use the erased types for this.
51
ARGS, RESULT = self.gen.erased_types(methodnm)
52
jargtypes = [self.db.lltype_to_cts(P) for P in ARGS]
53
jrettype = self.db.lltype_to_cts(RESULT)
55
if self.OOTYPE.__class__ in bridged_objects:
56
# Bridged objects are ones where we have written a java class
57
# that has methods with the correct names and types already
58
return jvmgen.Method.v(self, methodnm, jargtypes, jrettype)
60
# By default, we assume it is a static method on the PyPy
61
# object, that takes an instance of this object as the first
62
# argument. The other arguments we just convert to java versions,
63
# except for generics.
64
jargtypes = [self] + jargtypes
65
return jvmgen.Method.s(jPyPy, methodnm, jargtypes, jrettype)
67
# When we lookup a method on a BuiltInClassNode, we first check the
68
# 'built_in_methods' and 'bridged_objects' tables. This allows us to
69
# redirect to other methods if we like.
72
ootype.DictItemsIterator,
77
# Note: String and StringBuilder are rebound in ootype, and thus
78
# .__class__ is required
80
(ootype.StringBuilder.__class__, "ll_allocate"):
81
jvmgen.Method.v(jStringBuilder, "ensureCapacity", (jInt,), jVoid),
83
(ootype.StringBuilder.__class__, "ll_build"):
84
jvmgen.Method.v(jStringBuilder, "toString", (), jString),
86
(ootype.String.__class__, "ll_streq"):
87
jvmgen.Method.v(jString, "equals", (jObject,), jBool),
89
(ootype.String.__class__, "ll_strlen"):
90
jvmgen.Method.v(jString, "length", (), jInt),
92
(ootype.String.__class__, "ll_stritem_nonneg"):
93
jvmgen.Method.v(jString, "charAt", (jInt,), jChar),
95
(ootype.String.__class__, "ll_startswith"):
96
jvmgen.Method.v(jString, "startsWith", (jString,), jBool),
98
(ootype.String.__class__, "ll_endswith"):
99
jvmgen.Method.v(jString, "endsWith", (jString,), jBool),
101
(ootype.String.__class__, "ll_strcmp"):
102
jvmgen.Method.v(jString, "compareTo", (jString,), jInt),
104
(ootype.String.__class__, "ll_upper"):
105
jvmgen.Method.v(jString, "toUpperCase", (), jString),
107
(ootype.String.__class__, "ll_lower"):
108
jvmgen.Method.v(jString, "toLowerCase", (), jString),
110
(ootype.String.__class__, "ll_contains"):
111
jvmgen.Method.v(jString, "contains", (jCharSequence,), jBool),
113
(ootype.String.__class__, "ll_replace_chr_chr"):
114
jvmgen.Method.v(jString, "replace", (jChar, jChar), jString),
116
(ootype.Dict, "ll_set"):
117
jvmgen.Method.v(jHashMap, "put", (jObject, jObject), jObject),
119
(ootype.Dict, "ll_get"):
120
jvmgen.Method.v(jHashMap, "get", (jObject,), jObject),
122
(ootype.Dict, "ll_contains"):
123
jvmgen.Method.v(jHashMap, "containsKey", (jObject,), jBool),
125
(ootype.Dict, "ll_length"):
126
jvmgen.Method.v(jHashMap, "size", (), jInt),
128
(ootype.Dict, "ll_clear"):
129
jvmgen.Method.v(jHashMap, "clear", (), jVoid),
131
(ootype.List, "ll_length"):
132
jvmgen.Method.v(jArrayList, "size", (), jInt),
134
(ootype.List, "ll_getitem_fast"):
135
jvmgen.Method.v(jArrayList, "get", (jInt,), jObject),