13af6ab5fSopenharmony_ci/**
23af6ab5fSopenharmony_ci * Copyright (c) 2021-2024 Huawei Device Co., Ltd.
33af6ab5fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
43af6ab5fSopenharmony_ci * you may not use this file except in compliance with the License.
53af6ab5fSopenharmony_ci * You may obtain a copy of the License at
63af6ab5fSopenharmony_ci *
73af6ab5fSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0
83af6ab5fSopenharmony_ci *
93af6ab5fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
103af6ab5fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
113af6ab5fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
123af6ab5fSopenharmony_ci * See the License for the specific language governing permissions and
133af6ab5fSopenharmony_ci * limitations under the License.
143af6ab5fSopenharmony_ci */
153af6ab5fSopenharmony_ci
163af6ab5fSopenharmony_ci#include "objectExpression.h"
173af6ab5fSopenharmony_ci
183af6ab5fSopenharmony_ci#include "ir/base/decorator.h"
193af6ab5fSopenharmony_ci#include "util/helpers.h"
203af6ab5fSopenharmony_ci#include "compiler/base/literals.h"
213af6ab5fSopenharmony_ci#include "compiler/core/pandagen.h"
223af6ab5fSopenharmony_ci#include "compiler/core/ETSGen.h"
233af6ab5fSopenharmony_ci#include "checker/TSchecker.h"
243af6ab5fSopenharmony_ci#include "checker/ETSchecker.h"
253af6ab5fSopenharmony_ci#include "checker/ets/typeRelationContext.h"
263af6ab5fSopenharmony_ci#include "checker/ts/destructuringContext.h"
273af6ab5fSopenharmony_ci#include "ir/astDump.h"
283af6ab5fSopenharmony_ci#include "ir/srcDump.h"
293af6ab5fSopenharmony_ci#include "ir/typeNode.h"
303af6ab5fSopenharmony_ci#include "ir/base/property.h"
313af6ab5fSopenharmony_ci#include "ir/base/scriptFunction.h"
323af6ab5fSopenharmony_ci#include "ir/base/spreadElement.h"
333af6ab5fSopenharmony_ci#include "ir/expressions/arrayExpression.h"
343af6ab5fSopenharmony_ci#include "ir/expressions/assignmentExpression.h"
353af6ab5fSopenharmony_ci#include "ir/expressions/identifier.h"
363af6ab5fSopenharmony_ci#include "ir/statements/variableDeclarator.h"
373af6ab5fSopenharmony_ci#include "ir/validationInfo.h"
383af6ab5fSopenharmony_ci
393af6ab5fSopenharmony_cinamespace ark::es2panda::ir {
403af6ab5fSopenharmony_ciObjectExpression::ObjectExpression([[maybe_unused]] Tag const tag, ObjectExpression const &other,
413af6ab5fSopenharmony_ci                                   ArenaAllocator *const allocator)
423af6ab5fSopenharmony_ci    : AnnotatedExpression(static_cast<AnnotatedExpression const &>(other), allocator),
433af6ab5fSopenharmony_ci      decorators_(allocator->Adapter()),
443af6ab5fSopenharmony_ci      properties_(allocator->Adapter())
453af6ab5fSopenharmony_ci{
463af6ab5fSopenharmony_ci    preferredType_ = other.preferredType_;
473af6ab5fSopenharmony_ci    isDeclaration_ = other.isDeclaration_;
483af6ab5fSopenharmony_ci    trailingComma_ = other.trailingComma_;
493af6ab5fSopenharmony_ci    optional_ = other.optional_;
503af6ab5fSopenharmony_ci
513af6ab5fSopenharmony_ci    for (auto *property : other.properties_) {
523af6ab5fSopenharmony_ci        properties_.emplace_back(property->Clone(allocator, this)->AsExpression());
533af6ab5fSopenharmony_ci    }
543af6ab5fSopenharmony_ci
553af6ab5fSopenharmony_ci    for (auto *decorator : other.decorators_) {
563af6ab5fSopenharmony_ci        decorators_.emplace_back(decorator->Clone(allocator, this));
573af6ab5fSopenharmony_ci    }
583af6ab5fSopenharmony_ci}
593af6ab5fSopenharmony_ci
603af6ab5fSopenharmony_ciObjectExpression *ObjectExpression::Clone(ArenaAllocator *const allocator, AstNode *const parent)
613af6ab5fSopenharmony_ci{
623af6ab5fSopenharmony_ci    if (auto *const clone = allocator->New<ObjectExpression>(Tag {}, *this, allocator); clone != nullptr) {
633af6ab5fSopenharmony_ci        if (parent != nullptr) {
643af6ab5fSopenharmony_ci            clone->SetParent(parent);
653af6ab5fSopenharmony_ci        }
663af6ab5fSopenharmony_ci        return clone;
673af6ab5fSopenharmony_ci    }
683af6ab5fSopenharmony_ci    throw Error(ErrorType::GENERIC, "", CLONE_ALLOCATION_ERROR);
693af6ab5fSopenharmony_ci}
703af6ab5fSopenharmony_ci
713af6ab5fSopenharmony_cistatic std::pair<ValidationInfo, bool> ValidateProperty(Property *prop, bool &foundProto)
723af6ab5fSopenharmony_ci{
733af6ab5fSopenharmony_ci    ValidationInfo info = prop->ValidateExpression();
743af6ab5fSopenharmony_ci    if (prop->Kind() == PropertyKind::PROTO) {
753af6ab5fSopenharmony_ci        if (foundProto) {
763af6ab5fSopenharmony_ci            return {{"Duplicate __proto__ fields are not allowed in object literals", prop->Key()->Start()}, true};
773af6ab5fSopenharmony_ci        }
783af6ab5fSopenharmony_ci
793af6ab5fSopenharmony_ci        foundProto = true;
803af6ab5fSopenharmony_ci    }
813af6ab5fSopenharmony_ci
823af6ab5fSopenharmony_ci    return {info, false};
833af6ab5fSopenharmony_ci}
843af6ab5fSopenharmony_ci
853af6ab5fSopenharmony_ciValidationInfo ObjectExpression::ValidateExpression()
863af6ab5fSopenharmony_ci{
873af6ab5fSopenharmony_ci    if (optional_) {
883af6ab5fSopenharmony_ci        return {"Unexpected token '?'.", Start()};
893af6ab5fSopenharmony_ci    }
903af6ab5fSopenharmony_ci
913af6ab5fSopenharmony_ci    if (TypeAnnotation() != nullptr) {
923af6ab5fSopenharmony_ci        return {"Unexpected token.", TypeAnnotation()->Start()};
933af6ab5fSopenharmony_ci    }
943af6ab5fSopenharmony_ci
953af6ab5fSopenharmony_ci    ValidationInfo info;
963af6ab5fSopenharmony_ci    bool foundProto = false;
973af6ab5fSopenharmony_ci
983af6ab5fSopenharmony_ci    for (auto *it : properties_) {
993af6ab5fSopenharmony_ci        switch (it->Type()) {
1003af6ab5fSopenharmony_ci            case AstNodeType::OBJECT_EXPRESSION:
1013af6ab5fSopenharmony_ci            case AstNodeType::ARRAY_EXPRESSION: {
1023af6ab5fSopenharmony_ci                return {"Unexpected token.", it->Start()};
1033af6ab5fSopenharmony_ci            }
1043af6ab5fSopenharmony_ci            case AstNodeType::SPREAD_ELEMENT: {
1053af6ab5fSopenharmony_ci                info = it->AsSpreadElement()->ValidateExpression();
1063af6ab5fSopenharmony_ci                break;
1073af6ab5fSopenharmony_ci            }
1083af6ab5fSopenharmony_ci            case AstNodeType::PROPERTY: {
1093af6ab5fSopenharmony_ci                auto *prop = it->AsProperty();
1103af6ab5fSopenharmony_ci                bool ret = false;
1113af6ab5fSopenharmony_ci                std::tie(info, ret) = ValidateProperty(prop, foundProto);
1123af6ab5fSopenharmony_ci                if (ret) {
1133af6ab5fSopenharmony_ci                    return info;
1143af6ab5fSopenharmony_ci                }
1153af6ab5fSopenharmony_ci                break;
1163af6ab5fSopenharmony_ci            }
1173af6ab5fSopenharmony_ci            default: {
1183af6ab5fSopenharmony_ci                break;
1193af6ab5fSopenharmony_ci            }
1203af6ab5fSopenharmony_ci        }
1213af6ab5fSopenharmony_ci
1223af6ab5fSopenharmony_ci        if (info.Fail()) {
1233af6ab5fSopenharmony_ci            break;
1243af6ab5fSopenharmony_ci        }
1253af6ab5fSopenharmony_ci    }
1263af6ab5fSopenharmony_ci
1273af6ab5fSopenharmony_ci    return info;
1283af6ab5fSopenharmony_ci}
1293af6ab5fSopenharmony_ci
1303af6ab5fSopenharmony_cibool ObjectExpression::ConvertibleToObjectPattern()
1313af6ab5fSopenharmony_ci{
1323af6ab5fSopenharmony_ci    // NOTE: rsipka. throw more precise messages in case of false results
1333af6ab5fSopenharmony_ci    bool restFound = false;
1343af6ab5fSopenharmony_ci    bool convResult = true;
1353af6ab5fSopenharmony_ci
1363af6ab5fSopenharmony_ci    for (auto *it : properties_) {
1373af6ab5fSopenharmony_ci        switch (it->Type()) {
1383af6ab5fSopenharmony_ci            case AstNodeType::ARRAY_EXPRESSION: {
1393af6ab5fSopenharmony_ci                convResult = it->AsArrayExpression()->ConvertibleToArrayPattern();
1403af6ab5fSopenharmony_ci                break;
1413af6ab5fSopenharmony_ci            }
1423af6ab5fSopenharmony_ci            case AstNodeType::SPREAD_ELEMENT: {
1433af6ab5fSopenharmony_ci                if (!restFound && it == properties_.back() && !trailingComma_) {
1443af6ab5fSopenharmony_ci                    convResult = it->AsSpreadElement()->ConvertibleToRest(isDeclaration_, false);
1453af6ab5fSopenharmony_ci                } else {
1463af6ab5fSopenharmony_ci                    convResult = false;
1473af6ab5fSopenharmony_ci                }
1483af6ab5fSopenharmony_ci
1493af6ab5fSopenharmony_ci                restFound = true;
1503af6ab5fSopenharmony_ci                break;
1513af6ab5fSopenharmony_ci            }
1523af6ab5fSopenharmony_ci            case AstNodeType::OBJECT_EXPRESSION: {
1533af6ab5fSopenharmony_ci                convResult = it->AsObjectExpression()->ConvertibleToObjectPattern();
1543af6ab5fSopenharmony_ci                break;
1553af6ab5fSopenharmony_ci            }
1563af6ab5fSopenharmony_ci            case AstNodeType::ASSIGNMENT_EXPRESSION: {
1573af6ab5fSopenharmony_ci                convResult = it->AsAssignmentExpression()->ConvertibleToAssignmentPattern();
1583af6ab5fSopenharmony_ci                break;
1593af6ab5fSopenharmony_ci            }
1603af6ab5fSopenharmony_ci            case AstNodeType::META_PROPERTY_EXPRESSION:
1613af6ab5fSopenharmony_ci            case AstNodeType::CHAIN_EXPRESSION:
1623af6ab5fSopenharmony_ci            case AstNodeType::SEQUENCE_EXPRESSION: {
1633af6ab5fSopenharmony_ci                convResult = false;
1643af6ab5fSopenharmony_ci                break;
1653af6ab5fSopenharmony_ci            }
1663af6ab5fSopenharmony_ci            case AstNodeType::PROPERTY: {
1673af6ab5fSopenharmony_ci                convResult = it->AsProperty()->ConvertibleToPatternProperty();
1683af6ab5fSopenharmony_ci                break;
1693af6ab5fSopenharmony_ci            }
1703af6ab5fSopenharmony_ci            default: {
1713af6ab5fSopenharmony_ci                break;
1723af6ab5fSopenharmony_ci            }
1733af6ab5fSopenharmony_ci        }
1743af6ab5fSopenharmony_ci
1753af6ab5fSopenharmony_ci        if (!convResult) {
1763af6ab5fSopenharmony_ci            break;
1773af6ab5fSopenharmony_ci        }
1783af6ab5fSopenharmony_ci    }
1793af6ab5fSopenharmony_ci
1803af6ab5fSopenharmony_ci    SetType(AstNodeType::OBJECT_PATTERN);
1813af6ab5fSopenharmony_ci    return convResult;
1823af6ab5fSopenharmony_ci}
1833af6ab5fSopenharmony_ci
1843af6ab5fSopenharmony_civoid ObjectExpression::SetDeclaration()
1853af6ab5fSopenharmony_ci{
1863af6ab5fSopenharmony_ci    isDeclaration_ = true;
1873af6ab5fSopenharmony_ci}
1883af6ab5fSopenharmony_ci
1893af6ab5fSopenharmony_civoid ObjectExpression::SetOptional(bool optional)
1903af6ab5fSopenharmony_ci{
1913af6ab5fSopenharmony_ci    optional_ = optional;
1923af6ab5fSopenharmony_ci}
1933af6ab5fSopenharmony_ci
1943af6ab5fSopenharmony_civoid ObjectExpression::TransformChildren(const NodeTransformer &cb, std::string_view transformationName)
1953af6ab5fSopenharmony_ci{
1963af6ab5fSopenharmony_ci    for (auto *&it : decorators_) {
1973af6ab5fSopenharmony_ci        if (auto *transformedNode = cb(it); it != transformedNode) {
1983af6ab5fSopenharmony_ci            it->SetTransformedNode(transformationName, transformedNode);
1993af6ab5fSopenharmony_ci            it = transformedNode->AsDecorator();
2003af6ab5fSopenharmony_ci        }
2013af6ab5fSopenharmony_ci    }
2023af6ab5fSopenharmony_ci
2033af6ab5fSopenharmony_ci    for (auto *&it : properties_) {
2043af6ab5fSopenharmony_ci        if (auto *transformedNode = cb(it); it != transformedNode) {
2053af6ab5fSopenharmony_ci            it->SetTransformedNode(transformationName, transformedNode);
2063af6ab5fSopenharmony_ci            it = transformedNode->AsExpression();
2073af6ab5fSopenharmony_ci        }
2083af6ab5fSopenharmony_ci    }
2093af6ab5fSopenharmony_ci
2103af6ab5fSopenharmony_ci    if (auto *typeAnnotation = TypeAnnotation(); typeAnnotation != nullptr) {
2113af6ab5fSopenharmony_ci        if (auto *transformedNode = cb(typeAnnotation); typeAnnotation != transformedNode) {
2123af6ab5fSopenharmony_ci            typeAnnotation->SetTransformedNode(transformationName, transformedNode);
2133af6ab5fSopenharmony_ci            SetTsTypeAnnotation(static_cast<TypeNode *>(transformedNode));
2143af6ab5fSopenharmony_ci        }
2153af6ab5fSopenharmony_ci    }
2163af6ab5fSopenharmony_ci}
2173af6ab5fSopenharmony_ci
2183af6ab5fSopenharmony_civoid ObjectExpression::Iterate(const NodeTraverser &cb) const
2193af6ab5fSopenharmony_ci{
2203af6ab5fSopenharmony_ci    for (auto *it : decorators_) {
2213af6ab5fSopenharmony_ci        cb(it);
2223af6ab5fSopenharmony_ci    }
2233af6ab5fSopenharmony_ci
2243af6ab5fSopenharmony_ci    for (auto *it : properties_) {
2253af6ab5fSopenharmony_ci        cb(it);
2263af6ab5fSopenharmony_ci    }
2273af6ab5fSopenharmony_ci
2283af6ab5fSopenharmony_ci    if (TypeAnnotation() != nullptr) {
2293af6ab5fSopenharmony_ci        cb(TypeAnnotation());
2303af6ab5fSopenharmony_ci    }
2313af6ab5fSopenharmony_ci}
2323af6ab5fSopenharmony_ci
2333af6ab5fSopenharmony_civoid ObjectExpression::Dump(ir::AstDumper *dumper) const
2343af6ab5fSopenharmony_ci{
2353af6ab5fSopenharmony_ci    dumper->Add({{"type", (type_ == AstNodeType::OBJECT_EXPRESSION) ? "ObjectExpression" : "ObjectPattern"},
2363af6ab5fSopenharmony_ci                 {"decorators", AstDumper::Optional(decorators_)},
2373af6ab5fSopenharmony_ci                 {"properties", properties_},
2383af6ab5fSopenharmony_ci                 {"typeAnnotation", AstDumper::Optional(TypeAnnotation())},
2393af6ab5fSopenharmony_ci                 {"optional", AstDumper::Optional(optional_)}});
2403af6ab5fSopenharmony_ci}
2413af6ab5fSopenharmony_ci
2423af6ab5fSopenharmony_civoid ObjectExpression::Dump(ir::SrcDumper *dumper) const
2433af6ab5fSopenharmony_ci{
2443af6ab5fSopenharmony_ci    dumper->Add("{");
2453af6ab5fSopenharmony_ci    if (!properties_.empty()) {
2463af6ab5fSopenharmony_ci        dumper->IncrIndent();
2473af6ab5fSopenharmony_ci        dumper->Endl();
2483af6ab5fSopenharmony_ci        for (auto property : properties_) {
2493af6ab5fSopenharmony_ci            property->Dump(dumper);
2503af6ab5fSopenharmony_ci            dumper->Add(",");
2513af6ab5fSopenharmony_ci            if (property == properties_.back()) {
2523af6ab5fSopenharmony_ci                dumper->DecrIndent();
2533af6ab5fSopenharmony_ci            }
2543af6ab5fSopenharmony_ci            dumper->Endl();
2553af6ab5fSopenharmony_ci        }
2563af6ab5fSopenharmony_ci    }
2573af6ab5fSopenharmony_ci    dumper->Add("}");
2583af6ab5fSopenharmony_ci}
2593af6ab5fSopenharmony_ci
2603af6ab5fSopenharmony_civoid ObjectExpression::Compile([[maybe_unused]] compiler::PandaGen *pg) const
2613af6ab5fSopenharmony_ci{
2623af6ab5fSopenharmony_ci    pg->GetAstCompiler()->Compile(this);
2633af6ab5fSopenharmony_ci}
2643af6ab5fSopenharmony_ci
2653af6ab5fSopenharmony_cichecker::Type *ObjectExpression::CheckPattern(checker::TSChecker *checker)
2663af6ab5fSopenharmony_ci{
2673af6ab5fSopenharmony_ci    checker::ObjectDescriptor *desc = checker->Allocator()->New<checker::ObjectDescriptor>(checker->Allocator());
2683af6ab5fSopenharmony_ci
2693af6ab5fSopenharmony_ci    bool isOptional = false;
2703af6ab5fSopenharmony_ci
2713af6ab5fSopenharmony_ci    for (auto it = properties_.rbegin(); it != properties_.rend(); it++) {
2723af6ab5fSopenharmony_ci        if ((*it)->IsRestElement()) {
2733af6ab5fSopenharmony_ci            ASSERT((*it)->AsRestElement()->Argument()->IsIdentifier());
2743af6ab5fSopenharmony_ci            util::StringView indexInfoName("x");
2753af6ab5fSopenharmony_ci            auto *newIndexInfo =
2763af6ab5fSopenharmony_ci                checker->Allocator()->New<checker::IndexInfo>(checker->GlobalAnyType(), indexInfoName, false);
2773af6ab5fSopenharmony_ci            desc->stringIndexInfo = newIndexInfo;
2783af6ab5fSopenharmony_ci            continue;
2793af6ab5fSopenharmony_ci        }
2803af6ab5fSopenharmony_ci
2813af6ab5fSopenharmony_ci        ASSERT((*it)->IsProperty());
2823af6ab5fSopenharmony_ci        auto *prop = (*it)->AsProperty();
2833af6ab5fSopenharmony_ci
2843af6ab5fSopenharmony_ci        if (prop->IsComputed()) {
2853af6ab5fSopenharmony_ci            continue;
2863af6ab5fSopenharmony_ci        }
2873af6ab5fSopenharmony_ci
2883af6ab5fSopenharmony_ci        varbinder::LocalVariable *foundVar = desc->FindProperty(prop->Key()->AsIdentifier()->Name());
2893af6ab5fSopenharmony_ci        checker::Type *patternParamType = checker->GlobalAnyType();
2903af6ab5fSopenharmony_ci        varbinder::Variable *bindingVar = nullptr;
2913af6ab5fSopenharmony_ci
2923af6ab5fSopenharmony_ci        if (prop->IsShorthand()) {
2933af6ab5fSopenharmony_ci            switch (prop->Value()->Type()) {
2943af6ab5fSopenharmony_ci                case ir::AstNodeType::IDENTIFIER: {
2953af6ab5fSopenharmony_ci                    const ir::Identifier *ident = prop->Value()->AsIdentifier();
2963af6ab5fSopenharmony_ci                    ASSERT(ident->Variable());
2973af6ab5fSopenharmony_ci                    bindingVar = ident->Variable();
2983af6ab5fSopenharmony_ci                    break;
2993af6ab5fSopenharmony_ci                }
3003af6ab5fSopenharmony_ci                case ir::AstNodeType::ASSIGNMENT_PATTERN: {
3013af6ab5fSopenharmony_ci                    auto *assignmentPattern = prop->Value()->AsAssignmentPattern();
3023af6ab5fSopenharmony_ci                    patternParamType = assignmentPattern->Right()->Check(checker);
3033af6ab5fSopenharmony_ci                    ASSERT(assignmentPattern->Left()->AsIdentifier()->Variable());
3043af6ab5fSopenharmony_ci                    bindingVar = assignmentPattern->Left()->AsIdentifier()->Variable();
3053af6ab5fSopenharmony_ci                    isOptional = true;
3063af6ab5fSopenharmony_ci                    break;
3073af6ab5fSopenharmony_ci                }
3083af6ab5fSopenharmony_ci                default: {
3093af6ab5fSopenharmony_ci                    UNREACHABLE();
3103af6ab5fSopenharmony_ci                }
3113af6ab5fSopenharmony_ci            }
3123af6ab5fSopenharmony_ci        } else {
3133af6ab5fSopenharmony_ci            switch (prop->Value()->Type()) {
3143af6ab5fSopenharmony_ci                case ir::AstNodeType::IDENTIFIER: {
3153af6ab5fSopenharmony_ci                    bindingVar = prop->Value()->AsIdentifier()->Variable();
3163af6ab5fSopenharmony_ci                    break;
3173af6ab5fSopenharmony_ci                }
3183af6ab5fSopenharmony_ci                case ir::AstNodeType::ARRAY_PATTERN: {
3193af6ab5fSopenharmony_ci                    patternParamType = prop->Value()->AsArrayPattern()->CheckPattern(checker);
3203af6ab5fSopenharmony_ci                    break;
3213af6ab5fSopenharmony_ci                }
3223af6ab5fSopenharmony_ci                case ir::AstNodeType::OBJECT_PATTERN: {
3233af6ab5fSopenharmony_ci                    patternParamType = prop->Value()->AsObjectPattern()->CheckPattern(checker);
3243af6ab5fSopenharmony_ci                    break;
3253af6ab5fSopenharmony_ci                }
3263af6ab5fSopenharmony_ci                case ir::AstNodeType::ASSIGNMENT_PATTERN: {
3273af6ab5fSopenharmony_ci                    isOptional = CheckAssignmentPattern(prop, bindingVar, patternParamType, checker, foundVar);
3283af6ab5fSopenharmony_ci                    break;
3293af6ab5fSopenharmony_ci                }
3303af6ab5fSopenharmony_ci                default: {
3313af6ab5fSopenharmony_ci                    UNREACHABLE();
3323af6ab5fSopenharmony_ci                }
3333af6ab5fSopenharmony_ci            }
3343af6ab5fSopenharmony_ci        }
3353af6ab5fSopenharmony_ci
3363af6ab5fSopenharmony_ci        if (bindingVar != nullptr) {
3373af6ab5fSopenharmony_ci            bindingVar->SetTsType(patternParamType);
3383af6ab5fSopenharmony_ci        }
3393af6ab5fSopenharmony_ci
3403af6ab5fSopenharmony_ci        if (foundVar != nullptr) {
3413af6ab5fSopenharmony_ci            continue;
3423af6ab5fSopenharmony_ci        }
3433af6ab5fSopenharmony_ci
3443af6ab5fSopenharmony_ci        varbinder::LocalVariable *patternVar = varbinder::Scope::CreateVar(
3453af6ab5fSopenharmony_ci            checker->Allocator(), prop->Key()->AsIdentifier()->Name(), varbinder::VariableFlags::PROPERTY, *it);
3463af6ab5fSopenharmony_ci        patternVar->SetTsType(patternParamType);
3473af6ab5fSopenharmony_ci
3483af6ab5fSopenharmony_ci        if (isOptional) {
3493af6ab5fSopenharmony_ci            patternVar->AddFlag(varbinder::VariableFlags::OPTIONAL);
3503af6ab5fSopenharmony_ci        }
3513af6ab5fSopenharmony_ci
3523af6ab5fSopenharmony_ci        desc->properties.insert(desc->properties.begin(), patternVar);
3533af6ab5fSopenharmony_ci    }
3543af6ab5fSopenharmony_ci
3553af6ab5fSopenharmony_ci    checker::Type *returnType = checker->Allocator()->New<checker::ObjectLiteralType>(desc);
3563af6ab5fSopenharmony_ci    returnType->AsObjectType()->AddObjectFlag(checker::ObjectFlags::RESOLVED_MEMBERS);
3573af6ab5fSopenharmony_ci    return returnType;
3583af6ab5fSopenharmony_ci}
3593af6ab5fSopenharmony_ci
3603af6ab5fSopenharmony_cibool ObjectExpression::CheckAssignmentPattern(Property *prop, varbinder::Variable *&bindingVar,
3613af6ab5fSopenharmony_ci                                              checker::Type *&patternParamType, checker::TSChecker *checker,
3623af6ab5fSopenharmony_ci                                              varbinder::LocalVariable *foundVar)
3633af6ab5fSopenharmony_ci{
3643af6ab5fSopenharmony_ci    auto *assignmentPattern = prop->Value()->AsAssignmentPattern();
3653af6ab5fSopenharmony_ci
3663af6ab5fSopenharmony_ci    if (assignmentPattern->Left()->IsIdentifier()) {
3673af6ab5fSopenharmony_ci        bindingVar = assignmentPattern->Left()->AsIdentifier()->Variable();
3683af6ab5fSopenharmony_ci        patternParamType = checker->GetBaseTypeOfLiteralType(assignmentPattern->Right()->Check(checker));
3693af6ab5fSopenharmony_ci        return true;
3703af6ab5fSopenharmony_ci    }
3713af6ab5fSopenharmony_ci
3723af6ab5fSopenharmony_ci    if (assignmentPattern->Left()->IsArrayPattern()) {
3733af6ab5fSopenharmony_ci        auto savedContext = checker::SavedCheckerContext(checker, checker::CheckerStatus::FORCE_TUPLE);
3743af6ab5fSopenharmony_ci        auto destructuringContext = checker::ArrayDestructuringContext(
3753af6ab5fSopenharmony_ci            {checker, assignmentPattern->Left()->AsArrayPattern(), false, true, nullptr, assignmentPattern->Right()});
3763af6ab5fSopenharmony_ci
3773af6ab5fSopenharmony_ci        if (foundVar != nullptr) {
3783af6ab5fSopenharmony_ci            destructuringContext.SetInferredType(
3793af6ab5fSopenharmony_ci                checker->CreateUnionType({foundVar->TsType(), destructuringContext.InferredType()}));
3803af6ab5fSopenharmony_ci        }
3813af6ab5fSopenharmony_ci
3823af6ab5fSopenharmony_ci        destructuringContext.Start();
3833af6ab5fSopenharmony_ci        patternParamType = destructuringContext.InferredType();
3843af6ab5fSopenharmony_ci        return true;
3853af6ab5fSopenharmony_ci    }
3863af6ab5fSopenharmony_ci
3873af6ab5fSopenharmony_ci    ASSERT(assignmentPattern->Left()->IsObjectPattern());
3883af6ab5fSopenharmony_ci    auto savedContext = checker::SavedCheckerContext(checker, checker::CheckerStatus::FORCE_TUPLE);
3893af6ab5fSopenharmony_ci    auto destructuringContext = checker::ObjectDestructuringContext(
3903af6ab5fSopenharmony_ci        {checker, assignmentPattern->Left()->AsObjectPattern(), false, true, nullptr, assignmentPattern->Right()});
3913af6ab5fSopenharmony_ci
3923af6ab5fSopenharmony_ci    if (foundVar != nullptr) {
3933af6ab5fSopenharmony_ci        destructuringContext.SetInferredType(
3943af6ab5fSopenharmony_ci            checker->CreateUnionType({foundVar->TsType(), destructuringContext.InferredType()}));
3953af6ab5fSopenharmony_ci    }
3963af6ab5fSopenharmony_ci
3973af6ab5fSopenharmony_ci    destructuringContext.Start();
3983af6ab5fSopenharmony_ci    patternParamType = destructuringContext.InferredType();
3993af6ab5fSopenharmony_ci    return true;
4003af6ab5fSopenharmony_ci}
4013af6ab5fSopenharmony_ci
4023af6ab5fSopenharmony_cichecker::Type *ObjectExpression::Check(checker::TSChecker *checker)
4033af6ab5fSopenharmony_ci{
4043af6ab5fSopenharmony_ci    return checker->GetAnalyzer()->Check(this);
4053af6ab5fSopenharmony_ci}
4063af6ab5fSopenharmony_ci
4073af6ab5fSopenharmony_civoid ObjectExpression::Compile(compiler::ETSGen *etsg) const
4083af6ab5fSopenharmony_ci{
4093af6ab5fSopenharmony_ci    etsg->GetAstCompiler()->Compile(this);
4103af6ab5fSopenharmony_ci}
4113af6ab5fSopenharmony_ci
4123af6ab5fSopenharmony_cichecker::Type *ObjectExpression::Check(checker::ETSChecker *checker)
4133af6ab5fSopenharmony_ci{
4143af6ab5fSopenharmony_ci    return checker->GetAnalyzer()->Check(this);
4153af6ab5fSopenharmony_ci}
4163af6ab5fSopenharmony_ci}  // namespace ark::es2panda::ir
417