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 Ident()79 Identifier *Ident() 80 { 81 return ident_; 82 } 83 Super()84 Expression *Super() 85 { 86 return superClass_; 87 } 88 Super()89 const Expression *Super() const 90 { 91 return superClass_; 92 } 93 Declare()94 bool Declare() const 95 { 96 return declare_; 97 } 98 Abstract()99 bool Abstract() const 100 { 101 return abstract_; 102 } 103 SetAsExportDefault()104 void SetAsExportDefault() 105 { 106 exportDefault_ = true; 107 } 108 Body()109 ArenaVector<Statement *> &Body() 110 { 111 return body_; 112 } 113 Body()114 const ArenaVector<Statement *> &Body() const 115 { 116 return body_; 117 } 118 TypeParams()119 TSTypeParameterDeclaration *TypeParams() 120 { 121 return typeParams_; 122 } 123 TypeParams()124 const TSTypeParameterDeclaration *TypeParams() const 125 { 126 return typeParams_; 127 } 128 Implements()129 ArenaVector<TSClassImplements *> &Implements() 130 { 131 return implements_; 132 } 133 Implements()134 const ArenaVector<TSClassImplements *> &Implements() const 135 { 136 return implements_; 137 } 138 IndexSignatures()139 ArenaVector<TSIndexSignature *> &IndexSignatures() 140 { 141 return indexSignatures_; 142 } 143 IndexSignatures()144 const ArenaVector<TSIndexSignature *> &IndexSignatures() const 145 { 146 return indexSignatures_; 147 } 148 Ctor()149 MethodDefinition *Ctor() 150 { 151 ASSERT(ctor_ != nullptr); 152 return ctor_; 153 } 154 SuperTypeParams()155 const TSTypeParameterInstantiation *SuperTypeParams() const 156 { 157 return superTypeParams_; 158 } 159 SuperTypeParams()160 TSTypeParameterInstantiation *SuperTypeParams() 161 { 162 return superTypeParams_; 163 } 164 165 const FunctionExpression *Ctor() const; 166 167 util::StringView GetName() const; 168 169 void Iterate(const NodeTraverser &cb) const override; 170 void Dump(ir::AstDumper *dumper) const override; 171 void Compile(compiler::PandaGen *pg) const override; 172 checker::Type *Check(checker::Checker *checker) const override; 173 void UpdateSelf(const NodeUpdater &cb, binder::Binder *binder) override; 174 175 private: 176 compiler::VReg CompileHeritageClause(compiler::PandaGen *pg) const; 177 void InitializeClassName(compiler::PandaGen *pg) const; 178 int32_t CreateClassStaticProperties(compiler::PandaGen *pg, util::BitSet &compiled) const; 179 void CompileMissingProperties(compiler::PandaGen *pg, const util::BitSet &compiled, compiler::VReg classReg) const; 180 181 binder::LocalScope *scope_; 182 Identifier *ident_; 183 TSTypeParameterDeclaration *typeParams_; 184 TSTypeParameterInstantiation *superTypeParams_; 185 ArenaVector<TSClassImplements *> implements_; 186 MethodDefinition *ctor_; 187 Expression *superClass_; 188 ArenaVector<Statement *> body_; 189 ArenaVector<TSIndexSignature *> indexSignatures_; 190 bool declare_; 191 bool abstract_; 192 bool exportDefault_; 193 }; 194 195 } // namespace panda::es2panda::ir 196 197 #endif 198