1# Copyright (c) 2021-2022 Huawei Device Co., Ltd. 2# Licensed under the Apache License, Version 2.0 (the "License"); 3# you may not use this file except in compliance with the License. 4# You may obtain a copy of the License at 5# 6# http://www.apache.org/licenses/LICENSE-2.0 7# 8# Unless required by applicable law or agreed to in writing, software 9# distributed under the License is distributed on an "AS IS" BASIS, 10# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11# See the License for the specific language governing permissions and 12# limitations under the License. 13 14require 'delegate' 15 16def array_type?(type) 17 type[-1] == '[' 18end 19 20def get_object_type(type) 21 if array_type?(type) 22 type = "Array" 23 end 24 t = Runtime::coretypes.find { |t| t.managed_class == type } 25 return '%s *' % t.mirror_class if t 26 d = Runtime::coretypes.find { |d| d.managed_class == "Default" } 27 return '%s *' % d.mirror_class if d 28 'panda::ObjectHeader *' 29end 30 31def get_type(type) 32 @type_map ||= { 33 'void' => ['void'], 34 'u1' => ['uint8_t'], 35 'i8' => ['int8_t'], 36 'u8' => ['uint8_t'], 37 'i16' => ['int16_t'], 38 'u16' => ['uint16_t'], 39 'i32' => ['int32_t'], 40 'u32' => ['uint32_t'], 41 'i64' => ['int64_t'], 42 'u64' => ['uint64_t'], 43 'f32' => ['float'], 44 'f64' => ['double'], 45 'any' => ['uint64_t'], 46 'acc' => ['uint64_t'], 47 'string_id' => ['uint32_t'], 48 'method_id' => ['uint32_t'], 49 } 50 @type_map[type] || get_object_type(type) 51end 52 53def get_ret_type(type) 54 @ret_type_map ||= { 55 'void' => 'void', 56 'u1' => 'uint8_t', 57 'i8' => 'int8_t', 58 'u8' => 'uint8_t', 59 'i16' => 'int16_t', 60 'u16' => 'uint16_t', 61 'i32' => 'int32_t', 62 'u32' => 'uint32_t', 63 'i64' => 'int64_t', 64 'u64' => 'uint64_t', 65 'f32' => 'float', 66 'f64' => 'double', 67 'any' => 'uint64_t', 68 'acc' => 'DecodedTaggedValue', 69 'string_id' => 'uint32_t', 70 'method_id' => 'uint32_t', 71 } 72 @ret_type_map[type] || get_object_type(type) 73end 74 75def get_effective_type(type) 76 get_type(type) 77end 78 79def get_ret_effective_type(type) 80 get_ret_type(type) 81end 82 83class Intrinsic < SimpleDelegator 84 def need_abi_wrapper? 85 Object.send(:get_ret_type, signature.ret) != Object.send(:get_ret_effective_type, signature.ret) || 86 signature.args.any? { |arg| Object.send(:get_ret_type, arg) != Object.send(:get_ret_effective_type, arg) } 87 end 88 89 def enum_name 90 res = name.gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2') 91 res.gsub(/([a-z\d])([A-Z])/,'\1_\2').upcase 92 end 93 94 def wrapper_impl 95 return "" unless has_impl? 96 return impl + 'AbiWrapper' if need_abi_wrapper? 97 impl 98 end 99 100 def has_impl? 101 respond_to?(:impl) 102 end 103end 104 105module Runtime 106 module_function 107 108 def intrinsics 109 @data.intrinsics.map do |intrinsic| 110 Intrinsic.new(intrinsic) 111 end 112 end 113 114 def intrinsics_namespace 115 @data.intrinsics_namespace 116 end 117 118 def coretypes 119 @data.coretypes 120 end 121 122 def wrap_data(data) 123 @data = data 124 end 125end 126 127def Gen.on_require(data) 128 Runtime.wrap_data(data) 129end 130