1 /** 2 * Copyright (c) 2024 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 ES2PANDA_IR_TYPED_H 17 #define ES2PANDA_IR_TYPED_H 18 19 #include "ir/astNode.h" 20 #include "ir/statement.h" 21 22 namespace ark::es2panda::checker { 23 // NOLINTBEGIN(readability-redundant-declaration) 24 bool IsTypeError(Type const *tp); 25 // NOLINTEND(readability-redundant-declaration) 26 } // namespace ark::es2panda::checker 27 28 namespace ark::es2panda::ir { 29 30 template <typename T> 31 class Typed : public T { 32 public: 33 Typed() = delete; 34 ~Typed() override = default; 35 36 NO_COPY_OPERATOR(Typed); 37 NO_MOVE_SEMANTIC(Typed); 38 TsType()39 [[nodiscard]] checker::Type const *TsType() const 40 { 41 return tsType_; 42 } 43 TsType()44 [[nodiscard]] checker::Type *TsType() 45 { 46 return tsType_; 47 } 48 SetTsType(checker::Type * tsType)49 void SetTsType(checker::Type *tsType) noexcept 50 { 51 tsType_ = tsType; 52 } 53 IsTyped()54 bool IsTyped() const override 55 { 56 return true; 57 } 58 59 protected: Typed(AstNodeType const type)60 explicit Typed(AstNodeType const type) : T(type) {} Typed(AstNodeType const type,ModifierFlags const flags)61 explicit Typed(AstNodeType const type, ModifierFlags const flags) : T(type, flags) {} 62 63 // NOTE: when cloning node its type is not copied but removed empty so that it can be re-checked further. Typed(Typed const & other)64 Typed(Typed const &other) : T(static_cast<T const &>(other)) {} 65 66 private: 67 checker::Type *tsType_ {}; 68 }; 69 70 class TypedAstNode : public Typed<AstNode> { 71 public: 72 TypedAstNode() = delete; 73 ~TypedAstNode() override = default; 74 75 NO_COPY_OPERATOR(TypedAstNode); 76 NO_MOVE_SEMANTIC(TypedAstNode); 77 78 protected: TypedAstNode(AstNodeType const type)79 explicit TypedAstNode(AstNodeType const type) : Typed<AstNode>(type) {} TypedAstNode(AstNodeType const type,ModifierFlags const flags)80 explicit TypedAstNode(AstNodeType const type, ModifierFlags const flags) : Typed<AstNode>(type, flags) {} 81 TypedAstNode(TypedAstNode const & other)82 TypedAstNode(TypedAstNode const &other) : Typed<AstNode>(static_cast<Typed<AstNode> const &>(other)) {} 83 }; 84 85 class AnnotatedAstNode : public Annotated<AstNode> { 86 public: 87 AnnotatedAstNode() = delete; 88 ~AnnotatedAstNode() override = default; 89 90 NO_COPY_OPERATOR(AnnotatedAstNode); 91 NO_MOVE_SEMANTIC(AnnotatedAstNode); 92 93 protected: AnnotatedAstNode(AstNodeType const type,TypeNode * const typeAnnotation)94 explicit AnnotatedAstNode(AstNodeType const type, TypeNode *const typeAnnotation) 95 : Annotated<AstNode>(type, typeAnnotation) 96 { 97 } AnnotatedAstNode(AstNodeType const type)98 explicit AnnotatedAstNode(AstNodeType const type) : Annotated<AstNode>(type) {} AnnotatedAstNode(AstNodeType const type,ModifierFlags const flags)99 explicit AnnotatedAstNode(AstNodeType const type, ModifierFlags const flags) : Annotated<AstNode>(type, flags) {} 100 AnnotatedAstNode(AnnotatedAstNode const & other)101 AnnotatedAstNode(AnnotatedAstNode const &other) : Annotated<AstNode>(static_cast<Annotated<AstNode> const &>(other)) 102 { 103 } 104 }; 105 106 class TypedStatement : public Typed<Statement> { 107 public: 108 TypedStatement() = delete; 109 ~TypedStatement() override = default; 110 111 NO_COPY_OPERATOR(TypedStatement); 112 NO_MOVE_SEMANTIC(TypedStatement); 113 114 protected: TypedStatement(AstNodeType type)115 explicit TypedStatement(AstNodeType type) : Typed<Statement>(type) {}; TypedStatement(AstNodeType type,ModifierFlags flags)116 explicit TypedStatement(AstNodeType type, ModifierFlags flags) : Typed<Statement>(type, flags) {}; 117 TypedStatement(TypedStatement const & other)118 TypedStatement(TypedStatement const &other) : Typed<Statement>(static_cast<Typed<Statement> const &>(other)) {} 119 120 inline static checker::Type *const CHECKED = reinterpret_cast<checker::Type *>(0x01); 121 }; 122 123 class AnnotatedStatement : public Annotated<Statement> { 124 public: 125 AnnotatedStatement() = delete; 126 ~AnnotatedStatement() override = default; 127 128 NO_COPY_OPERATOR(AnnotatedStatement); 129 NO_MOVE_SEMANTIC(AnnotatedStatement); 130 131 protected: AnnotatedStatement(AstNodeType type,TypeNode * typeAnnotation)132 explicit AnnotatedStatement(AstNodeType type, TypeNode *typeAnnotation) : Annotated<Statement>(type, typeAnnotation) 133 { 134 } 135 AnnotatedStatement(AstNodeType type)136 explicit AnnotatedStatement(AstNodeType type) : Annotated<Statement>(type) {} AnnotatedStatement(AstNodeType type,ModifierFlags flags)137 explicit AnnotatedStatement(AstNodeType type, ModifierFlags flags) : Annotated<Statement>(type, flags) {} 138 AnnotatedStatement(AnnotatedStatement const & other)139 AnnotatedStatement(AnnotatedStatement const &other) 140 : Annotated<Statement>(static_cast<Annotated<Statement> const &>(other)) 141 { 142 } 143 }; 144 145 } // namespace ark::es2panda::ir 146 147 #endif 148