• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
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 
16 #ifndef COMPILER_OPTIMIZER_IR_IR_DYN_BASE_TYPES_H
17 #define COMPILER_OPTIMIZER_IR_IR_DYN_BASE_TYPES_H
18 
19 #include "compiler/optimizer/ir/datatype.h"
20 #include "source_languages.h"
21 
22 #include <optional>
23 
24 namespace panda::compiler {
NumericDataTypeToAnyType(panda::compiler::DataType::Type type,panda::compiler::SourceLanguage language)25 inline AnyBaseType NumericDataTypeToAnyType(panda::compiler::DataType::Type type,
26                                             [[maybe_unused]] panda::compiler::SourceLanguage language)
27 {
28     switch (type) {
29         case panda::compiler::DataType::Type::INT32:
30             return panda::compiler::AnyBaseType::ECMASCRIPT_INT_TYPE;
31         case panda::compiler::DataType::Type::UINT32:
32         case panda::compiler::DataType::Type::INT64:
33         case panda::compiler::DataType::Type::UINT64:
34             return panda::compiler::AnyBaseType::UNDEFINED_TYPE;
35         case panda::compiler::DataType::Type::FLOAT64:
36             return panda::compiler::AnyBaseType::ECMASCRIPT_DOUBLE_TYPE;
37         default:
38             UNREACHABLE();
39     }
40 }
41 
GetAnyStringType(panda::compiler::SourceLanguage language)42 inline AnyBaseType GetAnyStringType([[maybe_unused]] panda::compiler::SourceLanguage language)
43 {
44     return panda::compiler::AnyBaseType::ECMASCRIPT_STRING_TYPE;
45 }
46 
47 /*
48  * Checks that the exact type of `any` value that is subtype of `type` also can be subtype of `super_type`.
49  * Returns `true` if the `type` is equal to or subtype of `super_type` (i.e `type` is `STRING_TYPE` and
50  * `super_type` is `OBJECT_TYPE`).
51  * Returns `false` if there is no subtype relationship beetween `type` an `super_type` (i.e `type` is `INT_TYPE`
52  * and `super_type` is `OBJECT_TYPE`)
53  * Return `nullopt` if the `super_type` is subtype of `type` (i.e `type` is `OBJECT` and `super_type` is `STRING`).
54  * In this case we need to check exact type at the runtime.
55  */
IsAnyTypeCanBeSubtypeOf(AnyBaseType super_type,AnyBaseType type,panda::compiler::SourceLanguage language)56 inline std::optional<bool> IsAnyTypeCanBeSubtypeOf(AnyBaseType super_type, AnyBaseType type,
57                                                    [[maybe_unused]] panda::compiler::SourceLanguage language)
58 {
59     if (super_type == type) {
60         return true;
61     }
62     switch (super_type) {
63         case panda::compiler::AnyBaseType::ECMASCRIPT_OBJECT_TYPE:
64             return type == panda::compiler::AnyBaseType::ECMASCRIPT_STRING_TYPE;
65         case panda::compiler::AnyBaseType::ECMASCRIPT_STRING_TYPE:
66             if (type == panda::compiler::AnyBaseType::ECMASCRIPT_OBJECT_TYPE) {
67                 return std::nullopt;
68             }
69             return false;
70         default:
71             break;
72     }
73     return false;
74 }
75 
AnyBaseTypeToDataType(AnyBaseType any_type)76 inline panda::compiler::DataType::Type AnyBaseTypeToDataType([[maybe_unused]] AnyBaseType any_type)
77 {
78     return panda::compiler::DataType::Type::ANY;
79 }
80 
AnyTypeTypeToString(AnyBaseType any_type)81 inline const char *AnyTypeTypeToString(AnyBaseType any_type)
82 {
83     return "UNDEFINED_TYPE";
84 }
85 
86 } // namespace panda::compiler
87 
88 #endif  // COMPILER_OPTIMIZER_IR_IR_DYN_BASE_TYPES_H
89