|
| 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.refcounted_wrapper import RefcountedWrapper |
| 16 | +from nativepython.typed_expression import TypedExpression |
| 17 | +from nativepython.type_wrappers.exceptions import generateThrowException |
| 18 | +import nativepython.type_wrappers.runtime_functions as runtime_functions |
| 19 | + |
| 20 | +from typed_python import NoneType, Int64, String |
| 21 | + |
| 22 | +import nativepython.native_ast as native_ast |
| 23 | +import nativepython |
| 24 | + |
| 25 | +from nativepython.native_ast import VoidPtr |
| 26 | + |
| 27 | +typeWrapper = lambda t: nativepython.python_object_representation.typedPythonTypeToTypeWrapper(t) |
| 28 | + |
| 29 | +class StringWrapper(RefcountedWrapper): |
| 30 | + is_pod = False |
| 31 | + is_empty = False |
| 32 | + is_pass_by_ref = True |
| 33 | + |
| 34 | + def __init__(self): |
| 35 | + super().__init__(String()) |
| 36 | + |
| 37 | + self.layoutType = native_ast.Type.Struct(element_types=( |
| 38 | + ('refcount', native_ast.Int64), |
| 39 | + ('data', native_ast.UInt8) |
| 40 | + ), name='StringLayout').pointer() |
| 41 | + |
| 42 | + def getNativeLayoutType(self): |
| 43 | + return self.layoutType |
| 44 | + |
| 45 | + def on_refcount_zero(self, context, instance): |
| 46 | + assert instance.isReference |
| 47 | + return runtime_functions.destroy_string.call(instance.nonref_expr.cast(native_ast.VoidPtr)) |
| 48 | + |
| 49 | + def convert_bin_op(self, context, left, op, right): |
| 50 | + if right.expr_type == left.expr_type: |
| 51 | + if op.matches.Add: |
| 52 | + return context.push(str, lambda strRef: |
| 53 | + strRef.expr.store( |
| 54 | + runtime_functions.string_concat.call( |
| 55 | + left.nonref_expr.cast(VoidPtr), |
| 56 | + right.nonref_expr.cast(VoidPtr) |
| 57 | + ).cast(self.layoutType) |
| 58 | + ) |
| 59 | + ) |
| 60 | + |
| 61 | + return super().convert_bin_op(context, left, op, right) |
| 62 | + |
| 63 | + def convert_getitem(self, context, expr, item): |
| 64 | + item = item.toInt64() |
| 65 | + |
| 66 | + len_expr = self.convert_len(context, expr) |
| 67 | + |
| 68 | + with context.ifelse((item.nonref_expr.lt(len_expr.nonref_expr.negate())).bitor(item.nonref_expr.gte(len_expr.nonref_expr))) as (true,false): |
| 69 | + with true: |
| 70 | + context.pushException(IndexError, "string index out of range") |
| 71 | + |
| 72 | + return context.push(str, lambda strRef: |
| 73 | + strRef.expr.store( |
| 74 | + runtime_functions.string_getitem_int64.call(expr.nonref_expr.cast(native_ast.VoidPtr), item.nonref_expr) |
| 75 | + .cast(self.layoutType) |
| 76 | + ) |
| 77 | + ) |
| 78 | + |
| 79 | + def convert_len_native(self, expr): |
| 80 | + return native_ast.Expression.Branch( |
| 81 | + cond=expr, |
| 82 | + false=native_ast.const_int_expr(0), |
| 83 | + true=expr.ElementPtrIntegers(0,1).ElementPtrIntegers(4).cast(native_ast.Int32.pointer()).load().cast(native_ast.Int64) |
| 84 | + ) |
| 85 | + |
| 86 | + def convert_len(self, context, expr): |
| 87 | + return context.pushPod(int, self.convert_len_native(expr.nonref_expr)) |
| 88 | + |
| 89 | + def constant(self, context, s): |
| 90 | + return context.push(str, lambda strRef: |
| 91 | + strRef.expr.store( |
| 92 | + runtime_functions.string_from_utf8_and_len.call( |
| 93 | + native_ast.const_utf8_cstr(s), |
| 94 | + native_ast.const_int_expr(len(s)) |
| 95 | + ).cast(self.layoutType) |
| 96 | + ) |
| 97 | + ) |
| 98 | + |
| 99 | + |
| 100 | + |
0 commit comments