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/base/classProperty.h> 21 #include <ir/base/methodDefinition.h> 22 #include <ir/expressions/privateIdentifier.h> 23 #include <util/bitset.h> 24 25 namespace panda::es2panda::compiler { 26 class PandaGen; 27 } // namespace panda::es2panda::compiler 28 29 namespace panda::es2panda::checker { 30 class Checker; 31 class Type; 32 } // namespace panda::es2panda::checker 33 34 namespace panda::es2panda::binder { 35 class LocalScope; 36 } // namespace panda::es2panda::binder 37 38 namespace panda::es2panda::ir { 39 40 enum class FieldType { 41 NONE = 0, 42 NUMBER = (1 << 0), 43 STRING = (1 << 1), 44 BOOLEAN = (1 << 2), 45 TS_TYPE_REF = (1 << 3), 46 }; 47 DEFINE_BITOPS(FieldType) 48 49 class Identifier; 50 class MethodDefinition; 51 class TSTypeParameterDeclaration; 52 class TSTypeParameterInstantiation; 53 class TSClassImplements; 54 class TSIndexSignature; 55 56 class ClassDefinition : public AstNode { 57 public: ClassDefinition(binder::ClassScope * scope,Identifier * ident,TSTypeParameterDeclaration * typeParams,TSTypeParameterInstantiation * superTypeParams,ArenaVector<TSClassImplements * > && implements,MethodDefinition * ctor,MethodDefinition * staticInitializer,MethodDefinition * instanceInitializer,Expression * superClass,ArenaVector<Statement * > && body,ArenaVector<TSIndexSignature * > && indexSignatures,bool declare,bool abstract)58 explicit ClassDefinition(binder::ClassScope *scope, Identifier *ident, TSTypeParameterDeclaration *typeParams, 59 TSTypeParameterInstantiation *superTypeParams, 60 ArenaVector<TSClassImplements *> &&implements, MethodDefinition *ctor, 61 MethodDefinition *staticInitializer, MethodDefinition *instanceInitializer, 62 Expression *superClass, ArenaVector<Statement *> &&body, 63 ArenaVector<TSIndexSignature *> &&indexSignatures, bool declare, bool abstract) 64 : AstNode(AstNodeType::CLASS_DEFINITION), 65 scope_(scope), 66 ident_(ident), 67 typeParams_(typeParams), 68 superTypeParams_(superTypeParams), 69 implements_(std::move(implements)), 70 ctor_(ctor), 71 staticInitializer_(staticInitializer), 72 instanceInitializer_(instanceInitializer), 73 superClass_(superClass), 74 body_(std::move(body)), 75 indexSignatures_(std::move(indexSignatures)), 76 declare_(declare), 77 abstract_(abstract), 78 exportDefault_(false) 79 { 80 } 81 Scope()82 binder::ClassScope *Scope() const 83 { 84 return scope_; 85 } 86 Ident()87 const Identifier *Ident() const 88 { 89 return ident_; 90 } 91 Ident()92 Identifier *Ident() 93 { 94 return ident_; 95 } 96 Super()97 Expression *Super() 98 { 99 return superClass_; 100 } 101 Super()102 const Expression *Super() const 103 { 104 return superClass_; 105 } 106 Declare()107 bool Declare() const 108 { 109 return declare_; 110 } 111 Abstract()112 bool Abstract() const 113 { 114 return abstract_; 115 } 116 SetAsExportDefault()117 void SetAsExportDefault() 118 { 119 exportDefault_ = true; 120 } 121 Body()122 ArenaVector<Statement *> &Body() 123 { 124 return body_; 125 } 126 Body()127 const ArenaVector<Statement *> &Body() const 128 { 129 return body_; 130 } 131 AddToBody(Statement * statement)132 void AddToBody(Statement *statement) 133 { 134 body_.push_back(statement); 135 } 136 TypeParams()137 TSTypeParameterDeclaration *TypeParams() 138 { 139 return typeParams_; 140 } 141 TypeParams()142 const TSTypeParameterDeclaration *TypeParams() const 143 { 144 return typeParams_; 145 } 146 Implements()147 ArenaVector<TSClassImplements *> &Implements() 148 { 149 return implements_; 150 } 151 Implements()152 const ArenaVector<TSClassImplements *> &Implements() const 153 { 154 return implements_; 155 } 156 IndexSignatures()157 ArenaVector<TSIndexSignature *> &IndexSignatures() 158 { 159 return indexSignatures_; 160 } 161 IndexSignatures()162 const ArenaVector<TSIndexSignature *> &IndexSignatures() const 163 { 164 return indexSignatures_; 165 } 166 Ctor()167 MethodDefinition *Ctor() 168 { 169 ASSERT(ctor_ != nullptr); 170 return ctor_; 171 } 172 StaticInitializer()173 MethodDefinition *StaticInitializer() const 174 { 175 return staticInitializer_; 176 } 177 InstanceInitializer()178 MethodDefinition *InstanceInitializer() const 179 { 180 return instanceInitializer_; 181 } 182 SuperTypeParams()183 const TSTypeParameterInstantiation *SuperTypeParams() const 184 { 185 return superTypeParams_; 186 } 187 SuperTypeParams()188 TSTypeParameterInstantiation *SuperTypeParams() 189 { 190 return superTypeParams_; 191 } 192 NeedStaticInitializer()193 bool NeedStaticInitializer() const 194 { 195 return needStaticInitializer_; 196 } 197 NeedInstanceInitializer()198 bool NeedInstanceInitializer() const 199 { 200 return needInstanceInitializer_; 201 } 202 GetSlot(const Expression * key)203 uint32_t GetSlot(const Expression *key) const 204 { 205 return scope_->GetSlot(key); 206 } 207 HasInstancePrivateMethod()208 bool HasInstancePrivateMethod() const 209 { 210 return scope_->instanceMethodValidation_ != 0; 211 } 212 HasStaticPrivateMethod()213 bool HasStaticPrivateMethod() const 214 { 215 return scope_->staticMethodValidation_ != 0; 216 } 217 SetSendable()218 void SetSendable() 219 { 220 isSendable_ = true; 221 } 222 IsSendable()223 bool IsSendable() const 224 { 225 return isSendable_; 226 } 227 228 const FunctionExpression *Ctor() const; 229 230 util::StringView GetName() const; 231 232 void BuildClassEnvironment(bool useDefineSemantic); 233 234 void Iterate(const NodeTraverser &cb) const override; 235 void Dump(ir::AstDumper *dumper) const override; 236 void Compile(compiler::PandaGen *pg) const override; 237 checker::Type *Check(checker::Checker *checker) const override; 238 void UpdateSelf(const NodeUpdater &cb, binder::Binder *binder) override; 239 240 private: 241 compiler::VReg CompileHeritageClause(compiler::PandaGen *pg) const; 242 void InitializeClassName(compiler::PandaGen *pg) const; 243 int32_t CreateClassPublicBuffer(compiler::PandaGen *pg, util::BitSet &compiled, int32_t fieldTypeBufIdx = 0) const; 244 int32_t CreateClassPrivateBuffer(compiler::PandaGen *pg) const; 245 void CompileMissingProperties(compiler::PandaGen *pg, const util::BitSet &compiled, compiler::VReg classReg) const; 246 void StaticInitialize(compiler::PandaGen *pg, compiler::VReg classReg) const; 247 void InstanceInitialize(compiler::PandaGen *pg, compiler::VReg classReg) const; 248 void CompileComputedKeys(compiler::PandaGen *pg) const; 249 int32_t CreateFieldTypeBuffer(compiler::PandaGen *pg) const; 250 void CompileSendableClass(compiler::PandaGen *pg) const; 251 void CompileGetterOrSetter(compiler::PandaGen *pg, compiler::VReg dest, const MethodDefinition *prop) const; 252 253 binder::ClassScope *scope_; 254 Identifier *ident_; 255 TSTypeParameterDeclaration *typeParams_; 256 TSTypeParameterInstantiation *superTypeParams_; 257 ArenaVector<TSClassImplements *> implements_; 258 MethodDefinition *ctor_; 259 MethodDefinition *staticInitializer_; 260 MethodDefinition *instanceInitializer_; 261 Expression *superClass_; 262 ArenaVector<Statement *> body_; 263 ArenaVector<TSIndexSignature *> indexSignatures_; 264 bool declare_; 265 bool abstract_; 266 bool exportDefault_; 267 bool needStaticInitializer_ {false}; 268 bool needInstanceInitializer_ {false}; 269 bool hasComputedKey_ {false}; 270 bool hasPrivateElement_ {false}; 271 bool isSendable_ {false}; 272 }; 273 274 } // namespace panda::es2panda::ir 275 276 #endif 277