pypy / pypy / translator / jvm / database.py

nikomatsakis 81e6e18 
nikomatsakis 63e9713 

nikomatsakis 81e6e18 
nikomatsakis 63e9713 
nikomatsakis 79bc8a0 
Antonio Cuni 3ec40eb 
nikomatsakis 9dd3b0b 
nikomatsakis ffaabf1 
nikomatsakis 591751a 
nikomatsakis 5f61e7b 
nikomatsakis 63e9713 

Maciej Fijalkows… 3527c38 

Maciej Fijalkows… 66e4dc3 

nikomatsakis ac206fe 
nikomatsakis 63e9713 

nikomatsakis ac206fe 
nikomatsakis 63e9713 
nikomatsakis 7d326ae 
nikomatsakis 63e9713 
nikomatsakis 7d326ae 
nikomatsakis 79bc8a0 
nikomatsakis 12fabb8 
nikomatsakis c32743e 

Maciej Fijalkows… 66e4dc3 
nikomatsakis 7d326ae 
nikomatsakis 5ca27a8 
nikomatsakis fb770cd 



nikomatsakis 5ca27a8 
nikomatsakis 7d326ae 

Maciej Fijalkows… 66e4dc3 
nikomatsakis 5f61e7b 
nikomatsakis 7e06dda 

nikomatsakis 3a49fda 








nikomatsakis 7e06dda 

nikomatsakis 3a49fda 
Maciej Fijalkows… 66e4dc3 




nikomatsakis 7e06dda 
nikomatsakis 63e9713 





nikomatsakis 79bc8a0 
nikomatsakis 63e9713 






nikomatsakis 5f61e7b 
nikomatsakis 63e9713 
nikomatsakis 79bc8a0 
nikomatsakis 5f61e7b 

nikomatsakis 7d326ae 
nikomatsakis 63e9713 

Maciej Fijalkows… 66e4dc3 
nikomatsakis 63e9713 

nikomatsakis 12fabb8 







nikomatsakis 63e9713 



nikomatsakis 5ca27a8 
nikomatsakis 9051760 







Maciej Fijalkows… 66e4dc3 
nikomatsakis 9051760 


Maciej Fijalkows… 66e4dc3 
nikomatsakis 9051760 

Maciej Fijalkows… 66e4dc3 
nikomatsakis 7e06dda 
nikomatsakis 9051760 

nikomatsakis 9a60550 







Antonio Cuni 2f24a9f 








nikomatsakis 63e9713 
nikomatsakis 7d326ae 
nikomatsakis 79bc8a0 

nikomatsakis 7d326ae 

nikomatsakis 79bc8a0 
nikomatsakis 9a60550 
nikomatsakis 9051760 
nikomatsakis 7d326ae 
nikomatsakis 79bc8a0 
nikomatsakis 63e9713 
nikomatsakis 5ca27a8 








Maciej Fijalkows… 66e4dc3 
nikomatsakis 5ca27a8 




nikomatsakis 591751a 








nikomatsakis 5ca27a8 


nikomatsakis 10ccd99 



Armin Rigo 4ac21ef 

nikomatsakis 10ccd99 


nikomatsakis daf9112 

nikomatsakis 10ccd99 
nikomatsakis 63e9713 


nikomatsakis 5f61e7b 

nikomatsakis 63e9713 

nikomatsakis 4a2b130 






nikomatsakis 63e9713 
nikomatsakis 10ccd99 
nikomatsakis 4a2b130 
nikomatsakis c32743e 



nikomatsakis 7d326ae 
nikomatsakis 5f61e7b 
nikomatsakis 5ca27a8 
nikomatsakis 5f61e7b 

nikomatsakis 63e9713 
nikomatsakis 5f61e7b 

nikomatsakis 4a2b130 



Maciej Fijalkows… 66e4dc3 
nikomatsakis 4a2b130 
nikomatsakis 5f61e7b 




nikomatsakis c32743e 
nikomatsakis 5f61e7b 
nikomatsakis 63e9713 
nikomatsakis 5f61e7b 
nikomatsakis c32743e 
Antonio Cuni a8dcb66 








nikomatsakis 5f61e7b 

nikomatsakis c32743e 

nikomatsakis 5ca27a8 
nikomatsakis 591751a 
nikomatsakis c32743e 
nikomatsakis 5f61e7b 


nikomatsakis 5ca27a8 




Maciej Fijalkows… 66e4dc3 
nikomatsakis 5ca27a8 

nikomatsakis 63e9713 

nikomatsakis c32743e 
nikomatsakis 7d326ae 




Maciej Fijalkows… 66e4dc3 
nikomatsakis 7d326ae 



nikomatsakis 5f61e7b 
nikomatsakis 63e9713 
nikomatsakis 7d326ae 




nikomatsakis 81e6e18 
nikomatsakis 5ca27a8 
nikomatsakis fb770cd 





nikomatsakis 5ca27a8 


nikomatsakis fb770cd 

nikomatsakis 5ca27a8 
nikomatsakis fb770cd 





Maciej Fijalkows… 66e4dc3 
nikomatsakis fb770cd 

nikomatsakis 5ca27a8 








nikomatsakis fb770cd 
nikomatsakis 5ca27a8 



nikomatsakis fb770cd 






nikomatsakis 9a60550 
nikomatsakis fb770cd 
nikomatsakis 5ca27a8 

nikomatsakis fb770cd 



















nikomatsakis 5ca27a8 


nikomatsakis 63e9713 
nikomatsakis 591751a 
nikomatsakis 63e9713 
nikomatsakis 591751a 

nikomatsakis c32743e 
nikomatsakis a9aa3c9 
Maciej Fijalkows… 66e4dc3 



Michał Bendowski 5ba6249 
Maciej Fijalkows… 66e4dc3 





Antonio Cuni 3ec40eb 
nikomatsakis c32743e 

nikomatsakis 591751a 
nikomatsakis 63e9713 

nikomatsakis a9aa3c9 

nikomatsakis 63e9713 
nikomatsakis a9aa3c9 

nikomatsakis 63e9713 
nikomatsakis 591751a 
nikomatsakis 63e9713 



Maciej Fijalkows… 66e4dc3 
nikomatsakis 5f61e7b 
nikomatsakis 63e9713 
nikomatsakis 7d326ae 
nikomatsakis 63e9713 


nikomatsakis 7d326ae 









nikomatsakis 63e9713 


Maciej Fijalkows… 66e4dc3 










Antonio Cuni 3ec40eb 
nikomatsakis ffaabf1 
nikomatsakis 63e9713 



Maciej Fijalkows… 66e4dc3 









nikomatsakis 52de096 


nikomatsakis 1106c34 
Maciej Fijalkows… 66e4dc3 
nikomatsakis 1106c34 
Maciej Fijalkows… 66e4dc3 
nikomatsakis 1106c34 
Maciej Fijalkows… 66e4dc3 
nikomatsakis 1106c34 
Maciej Fijalkows… 66e4dc3 
nikomatsakis 63e9713 

nikomatsakis 5f61e7b 
Maciej Fijalkows… 66e4dc3 




nikomatsakis 7d326ae 
nikomatsakis 63e9713 
nikomatsakis 7d326ae 
nikomatsakis 63e9713 


nikomatsakis 3a49fda 

Maciej Fijalkows… 66e4dc3 
nikomatsakis 63e9713 

Maciej Fijalkows… 66e4dc3 

nikomatsakis a9aa3c9 


nikomatsakis 63e9713 

nikomatsakis 5f61e7b 
nikomatsakis 63e9713 
nikomatsakis 5f61e7b 
nikomatsakis 63e9713 
nikomatsakis 5f61e7b 
nikomatsakis 5ca27a8 
Carl Friedrich B… 5eba859 

Maciej Fijalkows… 3527c38 
nikomatsakis 63e9713 
nikomatsakis 7d326ae 
Maciej Fijalkows… 66e4dc3 















Maciej Fijalkows… 3527c38 




nikomatsakis 63e9713 




nikomatsakis 5ca27a8 
nikomatsakis 7d326ae 




  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
"""
The database centralizes information about the state of our translation,
and the mapping between the OOTypeSystem and the Java type system.
"""

from cStringIO import StringIO
from pypy.rpython.lltypesystem import lltype, rffi
from pypy.rpython.ootypesystem import ootype, rclass
from pypy.rpython.ootypesystem.module import ll_os
from pypy.translator.jvm import node, methods
from pypy.translator.jvm.option import getoption
from pypy.translator.jvm.builtin import JvmBuiltInType
from pypy.translator.oosupport.database import Database as OODatabase
from pypy.annotation.signature import annotation
from pypy.annotation.model import annotation_to_lltype
import pypy.translator.jvm.constant as jvmconst
import pypy.translator.jvm.typesystem as jvm

# ______________________________________________________________________
# Database object

class Database(OODatabase):
    def __init__(self, genoo):
        OODatabase.__init__(self, genoo)
        
        # Private attributes:
        self._jasmin_files = [] # list of strings --- .j files we made
        self._classes = {} # Maps ootype class objects to node.Class objects,
                           # and JvmType objects as well
        self._functions = {}      # graph -> jvm.Method

        # (jargtypes, jrettype) -> node.StaticMethodInterface
        self._delegates = {}

        # (INSTANCE, method_name) -> node.StaticMethodImplementation
        self._bound_methods = {}

        self._function_names = {} # graph --> function_name

        self._constants = {}      # flowmodel.Variable --> jvm.Const

        # Create information about the Main class we will build:
        #
        #    It will have two static fields, 'ilink' and 'pypy'.  The
        #    first points to an instance of the interface pypy.Interlink
        #    which we will be generated.  The second points to an instance
        #    of pypy.PyPy which was created with this Interlink instance.
        #
        #    The Interlink class provides the bridge between static helper
        #    code and dynamically generated classes.  Since there is one
        #    Main per set of translated code, this also allows multiple
        #    PyPy interpreters to overlap with one another.
        #
        #    These are public attributes that are referenced from
        #    elsewhere in the code using
        #    jvm.Generator.push_interlink() and .push_pypy().
        self.jPyPyMain = jvm.JvmClassType(self._pkg('Main'))
        self.pypy_field = jvm.Field.s(self.jPyPyMain, 'pypy', jvm.jPyPy)
        self.interlink_field = jvm.Field.s(self.jPyPyMain, 'ilink',
                                           jvm.jPyPyInterlink)

    # _________________________________________________________________
    # Java String vs Byte Array
    #
    # We allow the user to configure whether Python strings are stored
    # as Java strings, or as byte arrays.  The latter saves space; the
    # former may be faster.  

    using_byte_array = False

    # XXX have to fill this in
    
    # _________________________________________________________________
    # Miscellaneous
    
    def _uniq(self, nm):
        return nm + "_" + str(self.unique())

    def _pkg(self, nm):
        return "%s.%s" % (getoption('package'), nm)

    def class_name(self, TYPE):
        jtype = self.lltype_to_cts(TYPE)
        assert isinstance(jtype, jvm.JvmClassType)
        return jtype.name

    def add_jasmin_file(self, jfile):
        """ Adds to the list of files we need to run jasmin on """
        self._jasmin_files.append(jfile)

    def jasmin_files(self):
        """ Returns list of files we need to run jasmin on """
        return self._jasmin_files

    # _________________________________________________________________
    # Node Creation
    #
    # Creates nodes that represents classes, functions, simple constants.

    def create_interlink_node(self, methods):
        """ This is invoked by create_interlinke_node() in
        jvm/prebuiltnodes.py.  It creates a Class node that will
        be an instance of the Interlink interface, which is used
        to allow the static java code to throw PyPy exceptions and the
        like.

        The 'methods' argument should be a dictionary whose keys are
        method names and whose entries are jvm.Method objects which
        the corresponding method should invoke. """

        nm = self._pkg(self._uniq('InterlinkImplementation'))
        cls = node.Class(nm, supercls=jvm.jObject)
        for method_name, helper in methods.items():
            cls.add_method(node.InterlinkFunction(cls, method_name, helper))
        cls.add_interface(jvm.jPyPyInterlink)
        self.jInterlinkImplementation = cls
        self.pending_node(cls)

    def types_for_graph(self, graph):
        """
        Given a graph, returns a tuple like so:
          ( (java argument types...), java return type )
        For example, if the graph took two strings and returned a bool,
        then the return would be:
          ( (jString, jString), jBool )
        """
        ARGS = [v.concretetype for v in graph.getargs()]
        RESULT = graph.getreturnvar().concretetype
        return self.types_for_signature(ARGS, RESULT)

    def types_for_signature(self, ARGS, RESULT):
        ARGS = [ARG for ARG in ARGS if ARG is not ootype.Void]
        jargtypes = tuple([self.lltype_to_cts(ARG) for ARG in ARGS])
        jrettype = self.lltype_to_cts(RESULT)
        return jargtypes, jrettype
    
    def _function_for_graph(self, classobj, funcnm, is_static, graph):
        
        """
        Creates a node.Function object for a particular graph.  Adds
        the method to 'classobj', which should be a node.Class object.
        """
        jargtypes, jrettype = self.types_for_graph(graph)
        funcobj = node.GraphFunction(
            self, classobj, funcnm, jargtypes, jrettype, graph, is_static)
        return funcobj
    
    def _translate_record(self, OOTYPE):
        assert OOTYPE is not ootype.ROOT

        # Create class object if it does not already exist:
        if OOTYPE in self._classes:
            return self._classes[OOTYPE]

        # Create the class object first
        clsnm = self._pkg(self._uniq('Record'))
        clsobj = node.Class(clsnm, jvm.jObject)
        self._classes[OOTYPE] = clsobj

        # Add fields:
        self._translate_class_fields(clsobj, OOTYPE)

        # generate toString
        dump_method = methods.RecordDumpMethod(self, OOTYPE, clsobj)
        clsobj.add_method(dump_method)

        # generate equals and hash
        equals_method = methods.DeepEqualsMethod(self, OOTYPE, clsobj)
        clsobj.add_method(equals_method)
        hash_method = methods.DeepHashMethod(self, OOTYPE, clsobj)
        clsobj.add_method(hash_method)

        self.pending_node(clsobj)
        return clsobj

    def _translate_superclass_of(self, OOSUB):
        """
        Invoked to translate OOSUB's super class.  Normally just invokes
        pending_class, but we in the case of exceptions.Exception we
        need to return Throwable to please the JVM.
        """
        OOSUPER = OOSUB._superclass
        if OOSUB._name == "exceptions.Exception":
            return jvm.jPyPyThrowable
        return self.pending_class(OOSUPER)

    def _translate_instance(self, OOTYPE):
        assert isinstance(OOTYPE, ootype.Instance)
        assert OOTYPE is not ootype.ROOT

        # Create class object if it does not already exist:
        if OOTYPE in self._classes:
            return self._classes[OOTYPE]

        # Create the class object first
        clsnm = self._pkg(self._uniq(OOTYPE._name))
        clsobj = node.Class(clsnm)
        self._classes[OOTYPE] = clsobj

        # Resolve super class 
        assert OOTYPE._superclass
        supercls = self._translate_superclass_of(OOTYPE)
        clsobj.set_super_class(supercls)

        # TODO --- mangle field and method names?  Must be
        # deterministic, or use hashtable to avoid conflicts between
        # classes?
        
        # Add fields:
        self._translate_class_fields(clsobj, OOTYPE)
            
        # Add methods:
        for mname, mimpl in OOTYPE._methods.iteritems():
            if not hasattr(mimpl, 'graph'):
                # Abstract method
                METH = mimpl._TYPE
                arglist = [self.lltype_to_cts(ARG) for ARG in METH.ARGS
                           if ARG is not ootype.Void]
                returntype = self.lltype_to_cts(METH.RESULT)
                clsobj.add_abstract_method(jvm.Method.v(
                    clsobj, mname, arglist, returntype))
            else:
                # if the first argument's type is not a supertype of
                # this class it means that this method this method is
                # not really used by the class: don't render it, else
                # there would be a type mismatch.
                args =  mimpl.graph.getargs()
                SELF = args[0].concretetype
                if not ootype.isSubclass(OOTYPE, SELF): continue
                mobj = self._function_for_graph(
                    clsobj, mname, False, mimpl.graph)
                # XXX: this logic is broken: it might happen that there are
                # ootype.Instance which contains a meth whose graph is exactly
                # the same as the meth in the superclass: in this case,
                # len(graphs) == 1 but we cannot just mark the method as final
                # (or we can, but we should avoid to emit the method in the
                # subclass, then)
                ## graphs = OOTYPE._lookup_graphs(mname)
                ## if len(graphs) == 1:
                ##     mobj.is_final = True
                clsobj.add_method(mobj)

        # currently, we always include a special "dump" method for debugging
        # purposes
        dump_method = node.InstanceDumpMethod(self, OOTYPE, clsobj)
        clsobj.add_method(dump_method)

        self.pending_node(clsobj)
        return clsobj

    def _translate_class_fields(self, clsobj, OOTYPE):
        for fieldnm, (FIELDOOTY, fielddef) in OOTYPE._fields.iteritems():
            if FIELDOOTY is ootype.Void: continue
            fieldty = self.lltype_to_cts(FIELDOOTY)
            clsobj.add_field(
                jvm.Field(clsobj.name, fieldnm, fieldty, False, FIELDOOTY),
                fielddef)

    def pending_class(self, OOTYPE):
        return self.lltype_to_cts(OOTYPE)

    def pending_function(self, graph):
        """
        This is invoked when a standalone function is to be compiled.
        It creates a class named after the function with a single
        method, invoke().  This class is added to the worklist.
        Returns a jvm.Method object that allows this function to be
        invoked.
        """
        if graph in self._functions:
            return self._functions[graph]
        classnm = self._pkg(self._uniq(graph.name))
        classobj = node.Class(classnm, self.pending_class(ootype.ROOT))
        funcobj = self._function_for_graph(classobj, "invoke", True, graph)
        classobj.add_method(funcobj)
        self.pending_node(classobj)
        res = self._functions[graph] = funcobj.method()
        return res

    def record_delegate(self, TYPE):
        """
        Creates and returns a StaticMethodInterface type; this type
        represents an abstract base class for functions with a given
        signature, represented by TYPE, a ootype.StaticMethod
        instance.
        """

        # Translate argument/return types into java types, check if
        # we already have such a delegate:
        jargs = tuple([self.lltype_to_cts(ARG) for ARG in TYPE.ARGS
                       if ARG is not ootype.Void])
        jret = self.lltype_to_cts(TYPE.RESULT)
        return self.record_delegate_sig(jargs, jret)

    def record_delegate_sig(self, jargs, jret):
        """
        Like record_delegate, but the signature is in terms of java
        types.  jargs is a list of JvmTypes, one for each argument,
        and jret is a Jvm.  Note that jargs does NOT include an
        entry for the this pointer of the resulting object.  
        """
        key = (jargs, jret)
        if key in self._delegates:
            return self._delegates[key]

        # TODO: Make an intelligent name for this interface by
        # mangling the list of parameters
        name = self._pkg(self._uniq('Delegate'))

        # Create a new one if we do not:
        interface = node.StaticMethodInterface(name, jargs, jret)
        self._delegates[key] = interface
        self.pending_node(interface)
        return interface
    
    def record_delegate_standalone_func_impl(self, graph):
        """
        Creates a class with an invoke() method that invokes the given
        graph.  This object can be used as a function pointer.  It
        will extend the appropriate delegate for the graph's
        signature.
        """
        jargtypes, jrettype = self.types_for_graph(graph)
        super_class = self.record_delegate_sig(jargtypes, jrettype)
        pfunc = self.pending_function(graph)
        implnm = self._pkg(self._uniq(graph.name+'_delegate'))
        n = node.StaticMethodImplementation(implnm, super_class, None, pfunc)
        self.pending_node(n)
        return n

    def record_delegate_bound_method_impl(self, INSTANCE, method_name):
        """
        Creates an object with an invoke() method which invokes
        a method named method_name on an instance of INSTANCE.
        """
        key = (INSTANCE, method_name)
        if key in self._bound_methods:
            return self._bound_methods[key]
        METH_TYPE = INSTANCE._lookup(method_name)[1]._TYPE
        super_class = self.record_delegate(METH_TYPE)
        self_class = self.lltype_to_cts(INSTANCE)
        mthd_obj = self_class.lookup_method(method_name)
        implnm = self._pkg(self._uniq(
            self_class.simple_name()+"_"+method_name+"_delegate"))
        n = self._bound_methods[key] = node.StaticMethodImplementation(
            implnm, super_class, self_class, mthd_obj)
        self.pending_node(n)
        return n

    # _________________________________________________________________
    # toString functions
    #
    # Obtains an appropriate method for serializing an object of
    # any type.
    
    _toString_methods = {
        ootype.Signed:jvm.INTTOSTRINGI,
        ootype.Unsigned:jvm.PYPYSERIALIZEUINT,
        ootype.SignedLongLong:jvm.LONGTOSTRINGL,
        ootype.UnsignedLongLong: jvm.PYPYSERIALIZEULONG,
        ootype.Float:jvm.PYPYSERIALIZEDOUBLE,
        ootype.Bool:jvm.PYPYSERIALIZEBOOLEAN,
        ootype.Void:jvm.PYPYSERIALIZEVOID,
        ootype.Char:jvm.PYPYESCAPEDCHAR,
        ootype.UniChar:jvm.PYPYESCAPEDUNICHAR,
        ootype.String:jvm.PYPYESCAPEDSTRING,
        ootype.Unicode:jvm.PYPYESCAPEDUNICODE,
        rffi.SHORT:jvm.SHORTTOSTRINGS,
        }

    def toString_method_for_ootype(self, OOTYPE):
        """
        Assuming than an instance of type OOTYPE is pushed on the
        stack, returns a Method object that you can invoke.  This method
        will return a string representing the contents of that type.

        Do something like:
        
        > gen.load(var)
        > mthd = db.toString_method_for_ootype(var.concretetype)
        > mthd.invoke(gen)

        to print the value of 'var'.
        """
        return self._toString_methods.get(OOTYPE, jvm.PYPYSERIALIZEOBJECT)

    # _________________________________________________________________
    # Type translation functions
    #
    # Functions which translate from OOTypes to JvmType instances.
    # FIX --- JvmType and their Class nodes should not be different.

    def escape_name(self, nm):
        # invoked by oosupport/function.py; our names don't need escaping?
        return nm

    def llvar_to_cts(self, llv):
        """ Returns a tuple (JvmType, str) with the translated type
        and name of the given variable"""
        return self.lltype_to_cts(llv.concretetype), llv.name

    # Dictionary for scalar types; in this case, if we see the key, we
    # will return the value
    ootype_to_scalar = {
        ootype.Void:             jvm.jVoid,
        ootype.Signed:           jvm.jInt,
        ootype.Unsigned:         jvm.jInt,
        ootype.SignedLongLong:   jvm.jLong,
        ootype.UnsignedLongLong: jvm.jLong,
        ootype.Bool:             jvm.jBool,
        ootype.Float:            jvm.jDouble,
        ootype.Char:             jvm.jChar,    # byte would be sufficient, but harder
        ootype.UniChar:          jvm.jChar,
        ootype.Class:            jvm.jClass,
        ootype.ROOT:             jvm.jObject,  # treat like a scalar
        rffi.SHORT:              jvm.jShort,
    }

    # Dictionary for non-scalar types; in this case, if we see the key, we
    # will return a JvmBuiltInType based on the value
    ootype_to_builtin = {
        ootype.String:           jvm.jString,
        ootype.Unicode:          jvm.jString,
        ootype.StringBuilder:    jvm.jStringBuilder,
        ootype.UnicodeBuilder:   jvm.jStringBuilder,
        ootype.List:             jvm.jArrayList,
        ootype.Dict:             jvm.jHashMap,
        ootype.DictItemsIterator:jvm.jPyPyDictItemsIterator,
        ootype.CustomDict:       jvm.jPyPyCustomDict,
        ootype.WeakReference:    jvm.jPyPyWeakRef,
        ll_os.STAT_RESULT:       jvm.jPyPyStatResult,

        # These are some configured records that are generated by Java
        # code.  
        #ootype.Record({"item0": ootype.Signed, "item1": ootype.Signed}):
        #jvm.jPyPyRecordSignedSigned,
        #ootype.Record({"item0": ootype.Float, "item1": ootype.Signed}):
        #jvm.jPyPyRecordFloatSigned,
        #ootype.Record({"item0": ootype.Float, "item1": ootype.Float}):
        #jvm.jPyPyRecordFloatFloat,
        #ootype.Record({"item0": ootype.String, "item1": ootype.String}):
        #jvm.jPyPyRecordStringString,        
        }

    def lltype_to_cts(self, OOT):
        import sys
        res = self._lltype_to_cts(OOT)
        return res

    def _lltype_to_cts(self, OOT):
        """ Returns an instance of JvmType corresponding to
        the given OOType """

        # Handle built-in types:
        if OOT in self.ootype_to_scalar:
            return self.ootype_to_scalar[OOT]
        if (isinstance(OOT, lltype.Ptr) and
            isinstance(OOT.TO, lltype.OpaqueType)):
            return jvm.jObject
        if OOT in self.ootype_to_builtin:
            return JvmBuiltInType(self, self.ootype_to_builtin[OOT], OOT)
        if isinstance(OOT, ootype.Array):
            return self._array_type(OOT.ITEM)
        if OOT.__class__ in self.ootype_to_builtin:
            return JvmBuiltInType(
                self, self.ootype_to_builtin[OOT.__class__], OOT)

        # Handle non-built-in-types:
        if isinstance(OOT, ootype.Instance):
            return self._translate_instance(OOT)
        if isinstance(OOT, ootype.Record):
            return self._translate_record(OOT)
        if isinstance(OOT, ootype.StaticMethod):
            return self.record_delegate(OOT)
        if OOT is ootype.Object:
            return jvm.jObject

        assert False, "Untranslatable type %s!" % OOT

    ooitemtype_to_array = {
        ootype.Signed   : jvm.jIntArray,
        ootype.Unsigned : jvm.jIntArray,
        ootype.Char     : jvm.jCharArray,
        ootype.Bool     : jvm.jBoolArray,
        ootype.UniChar  : jvm.jCharArray,
        ootype.String   : jvm.jStringArray,
        ootype.Float    : jvm.jDoubleArray,
        ootype.Void     : jvm.jVoidArray,
    }

    def _array_type(self, ITEM):
        if ITEM in self.ooitemtype_to_array:
            return self.ooitemtype_to_array[ITEM]
        return jvm.jObjectArray

    def annotation_to_cts(self, _tp):
        s_tp = annotation(_tp)
        TP = annotation_to_lltype(s_tp)
        return self.lltype_to_cts(TP)

    # _________________________________________________________________
    # Uh....
    #
    # These functions are invoked by the code in oosupport, but I
    # don't think we need them or use them otherwise.

    def record_function(self, graph, name):
        self._function_names[graph] = name

    def graph_name(self, graph):
        # XXX: graph name are not guaranteed to be unique
        return self._function_names.get(graph, None)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.