1 /** 2 * Copyright (c) 2021 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_DEFINITION_H 17 #define ES2PANDA_PARSER_INCLUDE_AST_CLASS_DEFINITION_H 18 19 #include <binder/variable.h> 20 #include <ir/astNode.h> 21 #include <util/bitset.h> 22 23 namespace panda::es2panda::compiler { 24 class PandaGen; 25 } // namespace panda::es2panda::compiler 26 27 namespace panda::es2panda::checker { 28 class Checker; 29 class Type; 30 } // namespace panda::es2panda::checker 31 32 namespace panda::es2panda::binder { 33 class LocalScope; 34 } // namespace panda::es2panda::binder 35 36 namespace panda::es2panda::ir { 37 38 class Statement; 39 class Identifier; 40 class MethodDefinition; 41 class TSTypeParameterDeclaration; 42 class TSTypeParameterInstantiation; 43 class TSClassImplements; 44 class TSIndexSignature; 45 46 class ClassDefinition : public AstNode { 47 public: ClassDefinition(binder::LocalScope * scope,Identifier * ident,TSTypeParameterDeclaration * typeParams,TSTypeParameterInstantiation * superTypeParams,ArenaVector<TSClassImplements * > && implements,MethodDefinition * ctor,Expression * superClass,ArenaVector<Statement * > && body,ArenaVector<TSIndexSignature * > && indexSignatures,bool declare,bool abstract)48 explicit ClassDefinition(binder::LocalScope *scope, Identifier *ident, TSTypeParameterDeclaration *typeParams, 49 TSTypeParameterInstantiation *superTypeParams, 50 ArenaVector<TSClassImplements *> &&implements, MethodDefinition *ctor, 51 Expression *superClass, ArenaVector<Statement *> &&body, 52 ArenaVector<TSIndexSignature *> &&indexSignatures, bool declare, bool abstract) 53 : AstNode(AstNodeType::CLASS_DEFINITION), 54 scope_(scope), 55 ident_(ident), 56 typeParams_(typeParams), 57 superTypeParams_(superTypeParams), 58 implements_(std::move(implements)), 59 ctor_(ctor), 60 superClass_(superClass), 61 body_(std::move(body)), 62 indexSignatures_(std::move(indexSignatures)), 63 declare_(declare), 64 abstract_(abstract), 65 exportDefault_(false) 66 { 67 } 68 Scope()69 binder::LocalScope *Scope() const 70 { 71 return scope_; 72 } 73 Ident()74 const Identifier *Ident() const 75 { 76 return ident_; 77 } 78 Super()79 Expression *Super() 80 { 81 return superClass_; 82 } 83 Super()84 const Expression *Super() const 85 { 86 return superClass_; 87 } 88 Declare()89 bool Declare() const 90 { 91 return declare_; 92 } 93 Abstract()94 bool Abstract() const 95 { 96 return abstract_; 97 } 98 SetAsExportDefault()99 void SetAsExportDefault() 100 { 101 exportDefault_ = true; 102 } 103 Body()104 ArenaVector<Statement *> &Body() 105 { 106 return body_; 107 } 108 Body()109 const ArenaVector<Statement *> &Body() const 110 { 111 return body_; 112 } 113 Implements()114 ArenaVector<TSClassImplements *> &Implements() 115 { 116 return implements_; 117 } 118 Implements()119 const ArenaVector<TSClassImplements *> &Implements() const 120 { 121 return implements_; 122 } 123 Ctor()124 MethodDefinition *Ctor() 125 { 126 ASSERT(ctor_ != nullptr); 127 return ctor_; 128 } 129 130 const FunctionExpression *Ctor() const; 131 132 util::StringView GetName() const; 133 134 void Iterate(const NodeTraverser &cb) const override; 135 void Dump(ir::AstDumper *dumper) const override; 136 void Compile(compiler::PandaGen *pg) const override; 137 checker::Type *Check(checker::Checker *checker) const override; 138 void UpdateSelf(const NodeUpdater &cb, binder::Binder *binder) override; 139 140 private: 141 compiler::VReg CompileHeritageClause(compiler::PandaGen *pg) const; 142 void InitializeClassName(compiler::PandaGen *pg) const; 143 int32_t CreateClassStaticProperties(compiler::PandaGen *pg, util::BitSet &compiled) const; 144 void CompileMissingProperties(compiler::PandaGen *pg, const util::BitSet &compiled, compiler::VReg classReg) const; 145 146 binder::LocalScope *scope_; 147 Identifier *ident_; 148 TSTypeParameterDeclaration *typeParams_; 149 TSTypeParameterInstantiation *superTypeParams_; 150 ArenaVector<TSClassImplements *> implements_; 151 MethodDefinition *ctor_; 152 Expression *superClass_; 153 ArenaVector<Statement *> body_; 154 ArenaVector<TSIndexSignature *> indexSignatures_; 155 bool declare_; 156 bool abstract_; 157 bool exportDefault_; 158 }; 159 160 } // namespace panda::es2panda::ir 161 162 #endif 163