1 /** 2 * Copyright (c) 2021-2025 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_PARSER_INCLUDE_AST_CLASS_PROPERTY_H 17 #define ES2PANDA_PARSER_INCLUDE_AST_CLASS_PROPERTY_H 18 19 #include "ir/base/classElement.h" 20 #include "ir/statements/annotationUsage.h" 21 #include "ir/annotationAllowed.h" 22 #include "ir/jsDocAllowed.h" 23 24 namespace ark::es2panda::checker { 25 class ETSAnalyzer; 26 } // namespace ark::es2panda::checker 27 28 namespace ark::es2panda::ir { 29 class Expression; 30 class TypeNode; 31 32 class ClassProperty : public JsDocAllowed<AnnotationAllowed<ClassElement>> { 33 public: 34 ClassProperty() = delete; 35 ~ClassProperty() override = default; 36 37 NO_COPY_SEMANTIC(ClassProperty); 38 NO_MOVE_SEMANTIC(ClassProperty); 39 // CC-OFFNXT(G.FUN.01-CPP) solid logic ClassProperty(Expression * const key,Expression * const value,TypeNode * const typeAnnotation,ModifierFlags const modifiers,ArenaAllocator * const allocator,bool const isComputed)40 explicit ClassProperty(Expression *const key, Expression *const value, TypeNode *const typeAnnotation, 41 ModifierFlags const modifiers, ArenaAllocator *const allocator, bool const isComputed) 42 : JsDocAllowed<AnnotationAllowed<ClassElement>>(AstNodeType::CLASS_PROPERTY, key, value, modifiers, allocator, 43 isComputed), 44 typeAnnotation_(typeAnnotation) 45 { 46 } 47 IsDefaultAccessModifier()48 [[nodiscard]] bool IsDefaultAccessModifier() const noexcept 49 { 50 return isDefault_; 51 } 52 SetDefaultAccessModifier(bool isDefault)53 void SetDefaultAccessModifier(bool isDefault) 54 { 55 isDefault_ = isDefault; 56 } 57 TypeAnnotation()58 [[nodiscard]] TypeNode *TypeAnnotation() const noexcept 59 { 60 return typeAnnotation_; 61 } 62 SetTypeAnnotation(TypeNode * typeAnnotation)63 void SetTypeAnnotation(TypeNode *typeAnnotation) noexcept 64 { 65 typeAnnotation_ = typeAnnotation; 66 } 67 ToPrivateFieldKind(bool const isStatic)68 [[nodiscard]] PrivateFieldKind ToPrivateFieldKind(bool const isStatic) const override 69 { 70 return isStatic ? PrivateFieldKind::STATIC_FIELD : PrivateFieldKind::FIELD; 71 } 72 73 [[nodiscard]] ClassProperty *Clone(ArenaAllocator *allocator, AstNode *parent) override; 74 75 void TransformChildren(const NodeTransformer &cb, std::string_view transformationName) override; 76 void Iterate(const NodeTraverser &cb) const override; 77 78 void Dump(ir::AstDumper *dumper) const override; 79 void Dump(ir::SrcDumper *dumper) const override; 80 void Compile(compiler::PandaGen *pg) const override; 81 void Compile(compiler::ETSGen *etsg) const override; 82 checker::Type *Check(checker::TSChecker *checker) override; 83 checker::VerifiedType Check(checker::ETSChecker *checker) override; 84 Accept(ASTVisitorT * v)85 void Accept(ASTVisitorT *v) override 86 { 87 v->Accept(this); 88 } 89 NeedInitInStaticBlock()90 [[nodiscard]] bool NeedInitInStaticBlock() const 91 { 92 return needInitInStaticBlock_; 93 } 94 SetInitInStaticBlock(bool needInitInStaticBlock)95 void SetInitInStaticBlock(bool needInitInStaticBlock) 96 { 97 needInitInStaticBlock_ = needInitInStaticBlock; 98 } 99 100 protected: 101 ClassProperty *Construct(ArenaAllocator *allocator) override; 102 void CopyTo(AstNode *other) const override; 103 104 private: 105 void DumpPrefix(ir::SrcDumper *dumper) const; 106 void DumpModifiers(ir::SrcDumper *dumper) const; 107 bool RegisterUnexportedForDeclGen(ir::SrcDumper *dumper) const; 108 bool DumpNamespaceForDeclGen(ir::SrcDumper *dumper) const; 109 void DumpCheckerTypeForDeclGen(ir::SrcDumper *dumper) const; 110 111 friend class SizeOfNodeTest; 112 TypeNode *typeAnnotation_; 113 bool isDefault_ = false; 114 bool needInitInStaticBlock_ = false; 115 }; 116 } // namespace ark::es2panda::ir 117 118 #endif 119