|
| 1 | +# Copyright 2018 Braxton Mckee |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from nativepython.type_wrappers.wrapper import Wrapper |
| 16 | +from nativepython.type_wrappers.refcounted_wrapper import RefcountedWrapper |
| 17 | +from nativepython.typed_expression import TypedExpression |
| 18 | +from nativepython.type_wrappers.exceptions import generateThrowException |
| 19 | +import nativepython.type_wrappers.runtime_functions as runtime_functions |
| 20 | + |
| 21 | +from typed_python import NoneType, Int64, _types |
| 22 | + |
| 23 | +import nativepython.native_ast as native_ast |
| 24 | +import nativepython |
| 25 | + |
| 26 | + |
| 27 | +typeWrapper = lambda x: nativepython.python_object_representation.typedPythonTypeToTypeWrapper(x) |
| 28 | + |
| 29 | +def makeAlternativeWrapper(t): |
| 30 | + if t.__typed_python_category__ == "ConcreteAlternative": |
| 31 | + return ConcreteAlternativeWrapper(t) |
| 32 | + |
| 33 | + if _types.all_alternatives_empty(t): |
| 34 | + return SimpleAlternativeWrapper(t) |
| 35 | + else: |
| 36 | + return AlternativeWrapper(t) |
| 37 | + |
| 38 | +class SimpleAlternativeWrapper(Wrapper): |
| 39 | + """Wrapper around alternatives with all empty arguments.""" |
| 40 | + is_pod = True |
| 41 | + is_empty = False |
| 42 | + is_pass_by_ref = False |
| 43 | + |
| 44 | + def __init__(self, t): |
| 45 | + super().__init__(t) |
| 46 | + |
| 47 | + self.layoutType = native_ast.UInt8 |
| 48 | + |
| 49 | + def getNativeLayoutType(self): |
| 50 | + return self.layoutType |
| 51 | + |
| 52 | + def convert_default_initialize(self, context, target): |
| 53 | + return self.convert_copy_initialize( |
| 54 | + context, |
| 55 | + target, |
| 56 | + nativepython.python_object_representation.pythonObjectRepresentation(context, self.typeRepresentation()) |
| 57 | + ) |
| 58 | + |
| 59 | + def convert_destroy(self, context, target): |
| 60 | + pass |
| 61 | + |
| 62 | + def convert_assign(self, context, target, toStore): |
| 63 | + assert target.isReference |
| 64 | + context.pushEffect( |
| 65 | + target.expr.store(toStore.nonref_expr) |
| 66 | + ) |
| 67 | + |
| 68 | + def convert_copy_initialize(self, context, target, toStore): |
| 69 | + assert target.isReference |
| 70 | + context.pushEffect( |
| 71 | + target.expr.store(toStore.nonref_expr) |
| 72 | + ) |
| 73 | + |
| 74 | +class AlternativeWrapper(RefcountedWrapper): |
| 75 | + is_empty = False |
| 76 | + is_pod = False |
| 77 | + is_pass_by_ref = True |
| 78 | + |
| 79 | + def __init__(self, t): |
| 80 | + super().__init__(t) |
| 81 | + |
| 82 | + element_types = [('refcount', native_ast.Int64), ('which', native_ast.Int64), ('data',native_ast.UInt8)] |
| 83 | + |
| 84 | + self.alternativeType = t |
| 85 | + self.layoutType = native_ast.Type.Struct(element_types=element_types,name=t.__qualname__+"Layout").pointer() |
| 86 | + self._alternatives = None |
| 87 | + |
| 88 | + @property |
| 89 | + def alternatives(self): |
| 90 | + """Return a list of type wrappers for our alternative bodies. |
| 91 | +
|
| 92 | + This function has to be deferred until after the object is created if we have recursive alternatives. |
| 93 | + """ |
| 94 | + if self._alternatives is None: |
| 95 | + self._alternatives = [typeWrapper(x.ElementType) for x in self.typeRepresentation.__typed_python_alternatives__] |
| 96 | + return self._alternatives |
| 97 | + |
| 98 | + def getNativeLayoutType(self): |
| 99 | + return self.layoutType |
| 100 | + |
| 101 | + def on_refcount_zero(self, context, instance): |
| 102 | + return ( |
| 103 | + context.converter.defineNativeFunction( |
| 104 | + "destructor_" + str(self.typeRepresentation), |
| 105 | + ('destructor', self), |
| 106 | + [self], |
| 107 | + typeWrapper(NoneType()), |
| 108 | + self.generateNativeDestructorFunction |
| 109 | + ) |
| 110 | + .call(instance) |
| 111 | + ) |
| 112 | + |
| 113 | + def refAs(self, context, instance, whichIx): |
| 114 | + return context.pushReference( |
| 115 | + self.alternatives[whichIx].typeRepresentation, |
| 116 | + instance.nonref_expr.ElementPtrIntegers(0,2).cast(self.alternatives[whichIx].getNativeLayoutType().pointer()) |
| 117 | + ) |
| 118 | + |
| 119 | + def generateNativeDestructorFunction(self, context, out, instance): |
| 120 | + with context.switch(instance.nonref_expr.ElementPtrIntegers(0,1).load(), range(len(self.alternatives)), False) as indicesAndContexts: |
| 121 | + for ix, subcontext in indicesAndContexts: |
| 122 | + with subcontext: |
| 123 | + self.refAs(context, instance, ix).convert_destroy() |
| 124 | + |
| 125 | + context.pushEffect(runtime_functions.free.call(instance.nonref_expr.cast(native_ast.UInt8Ptr))) |
| 126 | + |
| 127 | + def convert_attribute(self, context, instance, attribute, nocheck=False): |
| 128 | + pass |
| 129 | + |
| 130 | + def generateConstructor(self, context, out, *args): |
| 131 | + pass |
| 132 | + |
0 commit comments