~ubuntu-branches/ubuntu/karmic/pypy/karmic

« back to all changes in this revision

Viewing changes to pypy/translator/jvm/builtin.py

  • Committer: Bazaar Package Importer
  • Author(s): Alexandre Fayolle
  • Date: 2007-04-13 09:33:09 UTC
  • Revision ID: james.westby@ubuntu.com-20070413093309-yoojh4jcoocu2krz
Tags: upstream-1.0.0
ImportĀ upstreamĀ versionĀ 1.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
7
 
 
8
# ______________________________________________________________________
 
9
# Mapping of built-in OOTypes to JVM types
 
10
 
 
11
class JvmBuiltInType(jvmtype.JvmClassType):
 
12
    
 
13
    """
 
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.
 
17
    """
 
18
    
 
19
    def __init__(self, db, classty, OOTYPE):
 
20
        jvmtype.JvmClassType.__init__(self, classty.name)
 
21
        self.db = db
 
22
        self.OOTYPE = OOTYPE
 
23
        self.gen = Generifier(OOTYPE)
 
24
    
 
25
    def __eq__(self, other):
 
26
        return isinstance(other, JvmBuiltInType) and other.name == self.name
 
27
 
 
28
    def __hash__(self):
 
29
        return hash(self.name)
 
30
 
 
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)
 
35
        return jvmgen.Field(
 
36
            self.descriptor.class_name(), fieldnm, jfieldty, False)
 
37
 
 
38
    def lookup_method(self, methodnm):
 
39
        """ Given the method name, returns a jvmgen.Method object """
 
40
 
 
41
        # Look for a shortcut method in our table of remappings:
 
42
        try:
 
43
            key = (self.OOTYPE.__class__, methodnm)
 
44
            return built_in_methods[key]
 
45
        except KeyError: pass
 
46
 
 
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)
 
54
 
 
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)
 
59
        else:
 
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)
 
66
 
 
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.
 
70
 
 
71
bridged_objects = (
 
72
    ootype.DictItemsIterator,
 
73
    )
 
74
 
 
75
built_in_methods = {
 
76
 
 
77
    # Note: String and StringBuilder are rebound in ootype, and thus
 
78
    # .__class__ is required
 
79
    
 
80
    (ootype.StringBuilder.__class__, "ll_allocate"):
 
81
    jvmgen.Method.v(jStringBuilder, "ensureCapacity", (jInt,), jVoid),
 
82
    
 
83
    (ootype.StringBuilder.__class__, "ll_build"):
 
84
    jvmgen.Method.v(jStringBuilder, "toString", (), jString),
 
85
 
 
86
    (ootype.String.__class__, "ll_streq"):
 
87
    jvmgen.Method.v(jString, "equals", (jObject,), jBool),
 
88
 
 
89
    (ootype.String.__class__, "ll_strlen"):
 
90
    jvmgen.Method.v(jString, "length", (), jInt),
 
91
    
 
92
    (ootype.String.__class__, "ll_stritem_nonneg"):
 
93
    jvmgen.Method.v(jString, "charAt", (jInt,), jChar),
 
94
 
 
95
    (ootype.String.__class__, "ll_startswith"):
 
96
    jvmgen.Method.v(jString, "startsWith", (jString,), jBool),
 
97
 
 
98
    (ootype.String.__class__, "ll_endswith"):
 
99
    jvmgen.Method.v(jString, "endsWith", (jString,), jBool),
 
100
 
 
101
    (ootype.String.__class__, "ll_strcmp"):
 
102
    jvmgen.Method.v(jString, "compareTo", (jString,), jInt),
 
103
 
 
104
    (ootype.String.__class__, "ll_upper"):
 
105
    jvmgen.Method.v(jString, "toUpperCase", (), jString),
 
106
 
 
107
    (ootype.String.__class__, "ll_lower"):
 
108
    jvmgen.Method.v(jString, "toLowerCase", (), jString),
 
109
 
 
110
    (ootype.String.__class__, "ll_contains"):
 
111
    jvmgen.Method.v(jString, "contains", (jCharSequence,), jBool),
 
112
 
 
113
    (ootype.String.__class__, "ll_replace_chr_chr"):
 
114
    jvmgen.Method.v(jString, "replace", (jChar, jChar), jString),
 
115
 
 
116
    (ootype.Dict, "ll_set"):
 
117
    jvmgen.Method.v(jHashMap, "put", (jObject, jObject), jObject),
 
118
    
 
119
    (ootype.Dict, "ll_get"):
 
120
    jvmgen.Method.v(jHashMap, "get", (jObject,), jObject),
 
121
 
 
122
    (ootype.Dict, "ll_contains"):
 
123
    jvmgen.Method.v(jHashMap, "containsKey", (jObject,), jBool),
 
124
 
 
125
    (ootype.Dict, "ll_length"):
 
126
    jvmgen.Method.v(jHashMap, "size", (), jInt),
 
127
    
 
128
    (ootype.Dict, "ll_clear"):
 
129
    jvmgen.Method.v(jHashMap, "clear", (), jVoid),
 
130
 
 
131
    (ootype.List, "ll_length"):
 
132
    jvmgen.Method.v(jArrayList, "size", (), jInt),
 
133
 
 
134
    (ootype.List, "ll_getitem_fast"):
 
135
    jvmgen.Method.v(jArrayList, "get", (jInt,), jObject),
 
136
 
 
137
    }