diff --git a/ets2panda/checker/types/ets/etsObjectType.cpp b/ets2panda/checker/types/ets/etsObjectType.cpp index 6af463b16dff19a4d72aade46e1e77cde4e21972..e203099420f14e617b1b5f95a575296de26e254f 100644 --- a/ets2panda/checker/types/ets/etsObjectType.cpp +++ b/ets2panda/checker/types/ets/etsObjectType.cpp @@ -21,6 +21,7 @@ #include "checker/types/ets/etsAsyncFuncReturnType.h" #include "checker/types/ets/etsEnumType.h" #include "compiler/lowering/phase.h" +#include "etsObjectTypeConstants.h" #include "ir/statements/annotationDeclaration.h" namespace ark::es2panda::checker { @@ -1297,61 +1298,50 @@ ETSChecker *ETSObjectType::GetETSChecker() void ETSObjectType::InstantiateProperties() const { + if (instantiating_ || propertiesInstantiated_) { + return; + } + instantiating_ = true; + ES2PANDA_ASSERT(relation_ != nullptr); auto *checker = relation_->GetChecker()->AsETSChecker(); if (baseType_ == nullptr || baseType_ == this) { checker->ResolveDeclaredMembersOfObject(this); + instantiating_ = false; return; } ES2PANDA_ASSERT(!propertiesInstantiated_); declNode_->Check(checker); - auto subst = effectiveSubstitution_ == nullptr - ? Substitution {} - : ETSChecker::ArenaSubstitutionToSubstitution(effectiveSubstitution_); + const Substitution arenaSubst = effectiveSubstitution_ == nullptr + ? Substitution {} + : ETSChecker::ArenaSubstitutionToSubstitution(effectiveSubstitution_); + const Substitution *subst = effectiveSubstitution_ != nullptr ? &arenaSubst : nullptr; + + const auto copyProps = [this, subst](auto &&srcMap, PropertyType dstKind) { + for (auto &kv : srcMap) { + auto *prop = kv.second; + auto *copied = CopyPropertyWithTypeArguments(prop, relation_, subst); + properties_[static_cast(dstKind)].emplace(prop->Name(), copied); + } + }; for (auto *const it : baseType_->ConstructSignatures()) { - auto *newSig = it->Substitute(relation_, &subst); + auto *newSig = it->Substitute(relation_, subst); constructSignatures_.push_back(newSig); } - for (auto const &[_, prop] : baseType_->InstanceFields()) { - (void)_; - auto *copiedProp = CopyPropertyWithTypeArguments(prop, relation_, &subst); - properties_[static_cast(PropertyType::INSTANCE_FIELD)].emplace(prop->Name(), copiedProp); - } - - for (auto const &[_, prop] : baseType_->StaticFields()) { - (void)_; - auto *copiedProp = CopyPropertyWithTypeArguments(prop, relation_, &subst); - properties_[static_cast(PropertyType::STATIC_FIELD)].emplace(prop->Name(), copiedProp); - } - - for (auto const &[_, prop] : baseType_->InstanceMethods()) { - (void)_; - auto *copiedProp = CopyPropertyWithTypeArguments(prop, relation_, &subst); - properties_[static_cast(PropertyType::INSTANCE_METHOD)].emplace(prop->Name(), copiedProp); - } - - for (auto const &[_, prop] : baseType_->StaticMethods()) { - (void)_; - auto *copiedProp = CopyPropertyWithTypeArguments(prop, relation_, &subst); - properties_[static_cast(PropertyType::STATIC_METHOD)].emplace(prop->Name(), copiedProp); - } + copyProps(baseType_->InstanceFields(), PropertyType::INSTANCE_FIELD); + copyProps(baseType_->StaticFields(), PropertyType::STATIC_FIELD); + copyProps(baseType_->InstanceMethods(), PropertyType::INSTANCE_METHOD); + copyProps(baseType_->StaticMethods(), PropertyType::STATIC_METHOD); + copyProps(baseType_->InstanceDecls(), PropertyType::INSTANCE_DECL); + copyProps(baseType_->StaticDecls(), PropertyType::STATIC_DECL); - for (auto const &[_, prop] : baseType_->InstanceDecls()) { - (void)_; - auto *copiedProp = CopyPropertyWithTypeArguments(prop, relation_, &subst); - properties_[static_cast(PropertyType::INSTANCE_DECL)].emplace(prop->Name(), copiedProp); - } - - for (auto const &[_, prop] : baseType_->StaticDecls()) { - (void)_; - auto *copiedProp = CopyPropertyWithTypeArguments(prop, relation_, &subst); - properties_[static_cast(PropertyType::STATIC_DECL)].emplace(prop->Name(), copiedProp); - } + propertiesInstantiated_ = false; + instantiating_ = false; } std::string ETSObjectType::NameToDescriptor(util::StringView name) diff --git a/ets2panda/checker/types/ets/etsObjectType.h b/ets2panda/checker/types/ets/etsObjectType.h index 672914ec1cb829fa580189c5013a7acebe4f1a29..e323ba3faa3be7a560a80b606bc504780041fe74 100644 --- a/ets2panda/checker/types/ets/etsObjectType.h +++ b/ets2panda/checker/types/ets/etsObjectType.h @@ -388,6 +388,11 @@ public: return propertiesInstantiated_; } + bool IsInstantiating() const noexcept + { + return instantiating_; + } + protected: virtual ETSFunctionType *CreateMethodTypeForProp(util::StringView name) const; @@ -464,6 +469,7 @@ private: TypeRelation *relation_ = nullptr; const ArenaSubstitution *effectiveSubstitution_ = nullptr; mutable bool propertiesInstantiated_ = false; + mutable bool instantiating_ {false}; mutable ArenaVector constructSignatures_; mutable PropertyHolder properties_; };