From 73fa6ff84e5770395df7f14a1079035b29467490 Mon Sep 17 00:00:00 2001 From: Zelentsov Dmitry Date: Fri, 3 Nov 2023 18:58:21 +0300 Subject: [PATCH] Implement Clone() method for complex AST expressions. Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/I8FKFB?from=project-issue Test: build Signed-off-by: Zelentsov Dmitry --- .gitignore | 1 + ets2panda/checker/ETSAnalyzer.cpp | 5 +- ets2panda/ir/astNode.cpp | 2 +- ets2panda/ir/astNode.h | 7 ++ ets2panda/ir/base/classDefinition.cpp | 2 +- ets2panda/ir/base/classDefinition.h | 56 +++++---- ets2panda/ir/base/classElement.cpp | 10 +- ets2panda/ir/base/classElement.h | 38 +++++-- ets2panda/ir/base/classProperty.cpp | 32 +++++- ets2panda/ir/base/classProperty.h | 21 +++- ets2panda/ir/base/decorator.cpp | 4 +- ets2panda/ir/base/decorator.h | 2 +- ets2panda/ir/base/metaProperty.cpp | 2 +- ets2panda/ir/base/metaProperty.h | 2 +- ets2panda/ir/base/methodDefinition.cpp | 44 +++++++- ets2panda/ir/base/methodDefinition.h | 50 ++++++--- ets2panda/ir/base/property.cpp | 6 +- ets2panda/ir/base/property.h | 2 +- ets2panda/ir/base/scriptFunction.cpp | 15 +-- ets2panda/ir/base/scriptFunction.h | 106 ++++++++++-------- ets2panda/ir/base/spreadElement.cpp | 6 +- ets2panda/ir/base/spreadElement.h | 2 +- ets2panda/ir/base/templateElement.cpp | 2 +- ets2panda/ir/base/templateElement.h | 2 +- ets2panda/ir/base/tsIndexSignature.cpp | 25 ++++- ets2panda/ir/base/tsIndexSignature.h | 25 +++-- ets2panda/ir/base/tsMethodSignature.cpp | 3 +- ets2panda/ir/base/tsMethodSignature.h | 26 +++-- ets2panda/ir/base/tsPropertySignature.cpp | 23 +++- ets2panda/ir/base/tsPropertySignature.h | 21 +++- ets2panda/ir/base/tsSignatureDeclaration.cpp | 2 +- ets2panda/ir/base/tsSignatureDeclaration.h | 26 +++-- ets2panda/ir/ets/etsClassLiteral.cpp | 4 +- ets2panda/ir/ets/etsClassLiteral.h | 2 +- ets2panda/ir/ets/etsLaunchExpression.cpp | 4 +- ets2panda/ir/ets/etsLaunchExpression.h | 2 +- .../ir/ets/etsNewArrayInstanceExpression.cpp | 7 +- .../ir/ets/etsNewArrayInstanceExpression.h | 2 +- .../ir/ets/etsNewClassInstanceExpression.cpp | 28 ++++- .../ir/ets/etsNewClassInstanceExpression.h | 28 +++-- .../etsNewMultiDimArrayInstanceExpression.cpp | 29 ++++- .../etsNewMultiDimArrayInstanceExpression.h | 23 +++- ets2panda/ir/ets/etsPackageDeclaration.cpp | 19 +++- ets2panda/ir/ets/etsPackageDeclaration.h | 16 ++- ets2panda/ir/ets/etsParameterExpression.cpp | 4 +- ets2panda/ir/ets/etsParameterExpression.h | 2 +- ets2panda/ir/ets/etsStructDeclaration.cpp | 23 ++++ ets2panda/ir/ets/etsStructDeclaration.h | 24 +++- ets2panda/ir/expression.cpp | 2 +- ets2panda/ir/expression.h | 8 -- ets2panda/ir/expressions/arrayExpression.cpp | 6 +- ets2panda/ir/expressions/arrayExpression.h | 2 +- .../expressions/arrowFunctionExpression.cpp | 31 ++++- .../ir/expressions/arrowFunctionExpression.h | 32 ++++-- .../ir/expressions/assignmentExpression.cpp | 6 +- .../ir/expressions/assignmentExpression.h | 2 +- ets2panda/ir/expressions/awaitExpression.cpp | 4 +- ets2panda/ir/expressions/awaitExpression.h | 2 +- ets2panda/ir/expressions/binaryExpression.cpp | 6 +- ets2panda/ir/expressions/binaryExpression.h | 2 +- ets2panda/ir/expressions/callExpression.cpp | 34 +++++- ets2panda/ir/expressions/callExpression.h | 53 +++++---- ets2panda/ir/expressions/chainExpression.cpp | 4 +- ets2panda/ir/expressions/chainExpression.h | 2 +- ets2panda/ir/expressions/classExpression.cpp | 20 +++- ets2panda/ir/expressions/classExpression.h | 17 ++- .../ir/expressions/conditionalExpression.cpp | 8 +- .../ir/expressions/conditionalExpression.h | 2 +- .../ir/expressions/functionExpression.cpp | 18 ++- ets2panda/ir/expressions/functionExpression.h | 22 +++- ets2panda/ir/expressions/identifier.cpp | 4 +- ets2panda/ir/expressions/identifier.h | 2 +- ets2panda/ir/expressions/importExpression.cpp | 4 +- ets2panda/ir/expressions/importExpression.h | 2 +- .../ir/expressions/literals/bigIntLiteral.cpp | 2 +- .../ir/expressions/literals/bigIntLiteral.h | 2 +- .../expressions/literals/booleanLiteral.cpp | 2 +- .../ir/expressions/literals/booleanLiteral.h | 2 +- .../ir/expressions/literals/charLiteral.cpp | 2 +- .../ir/expressions/literals/charLiteral.h | 2 +- .../ir/expressions/literals/nullLiteral.cpp | 2 +- .../ir/expressions/literals/nullLiteral.h | 2 +- .../ir/expressions/literals/numberLiteral.cpp | 2 +- .../ir/expressions/literals/numberLiteral.h | 2 +- .../ir/expressions/literals/regExpLiteral.cpp | 2 +- .../ir/expressions/literals/regExpLiteral.h | 2 +- .../ir/expressions/literals/stringLiteral.cpp | 2 +- .../ir/expressions/literals/stringLiteral.h | 2 +- ets2panda/ir/expressions/memberExpression.cpp | 6 +- ets2panda/ir/expressions/memberExpression.h | 2 +- ets2panda/ir/expressions/newExpression.cpp | 6 +- ets2panda/ir/expressions/newExpression.h | 2 +- ets2panda/ir/expressions/objectExpression.cpp | 6 +- ets2panda/ir/expressions/objectExpression.h | 2 +- .../ir/expressions/omittedExpression.cpp | 2 +- ets2panda/ir/expressions/omittedExpression.h | 2 +- .../ir/expressions/sequenceExpression.cpp | 4 +- ets2panda/ir/expressions/sequenceExpression.h | 2 +- ets2panda/ir/expressions/superExpression.cpp | 2 +- ets2panda/ir/expressions/superExpression.h | 2 +- .../expressions/taggedTemplateExpression.cpp | 9 +- .../ir/expressions/taggedTemplateExpression.h | 2 +- ets2panda/ir/expressions/templateLiteral.cpp | 6 +- ets2panda/ir/expressions/templateLiteral.h | 2 +- ets2panda/ir/expressions/thisExpression.cpp | 2 +- ets2panda/ir/expressions/thisExpression.h | 2 +- ets2panda/ir/expressions/unaryExpression.cpp | 4 +- ets2panda/ir/expressions/unaryExpression.h | 2 +- ets2panda/ir/expressions/updateExpression.cpp | 4 +- ets2panda/ir/expressions/updateExpression.h | 2 +- ets2panda/ir/expressions/yieldExpression.cpp | 4 +- ets2panda/ir/expressions/yieldExpression.h | 2 +- ets2panda/ir/statement.cpp | 2 +- ets2panda/ir/statement.h | 8 -- .../ir/ts/tsTypeParameterInstantiation.cpp | 5 +- .../ir/ts/tsTypeParameterInstantiation.h | 2 +- ets2panda/ir/typeNode.cpp | 3 +- ets2panda/ir/typeNode.h | 2 +- 118 files changed, 846 insertions(+), 365 deletions(-) diff --git a/.gitignore b/.gitignore index d0f42167dbd..2a23da7872b 100644 --- a/.gitignore +++ b/.gitignore @@ -17,3 +17,4 @@ ets2panda/linter*/test_rules/**/results ets2panda/linter*/**/package-lock.json **/compile_commands.json .cache +.vscode diff --git a/ets2panda/checker/ETSAnalyzer.cpp b/ets2panda/checker/ETSAnalyzer.cpp index ec729cf2294..ddc11feb498 100644 --- a/ets2panda/checker/ETSAnalyzer.cpp +++ b/ets2panda/checker/ETSAnalyzer.cpp @@ -82,7 +82,7 @@ checker::Type *ETSAnalyzer::Check(ir::ClassDefinition *node) const checker::Type *ETSAnalyzer::Check(ir::ClassProperty *st) const { - ASSERT(st->Key()->IsIdentifier()); + ASSERT(st->Id() != nullptr); ETSChecker *checker = GetETSChecker(); if (st->TsType() != nullptr) { @@ -97,8 +97,7 @@ checker::Type *ETSAnalyzer::Check(ir::ClassProperty *st) const checker->AddStatus(checker::CheckerStatus::IN_STATIC_CONTEXT); } - st->SetTsType( - checker->CheckVariableDeclaration(st->Key()->AsIdentifier(), st->TypeAnnotation(), st->Value(), st->flags_)); + st->SetTsType(checker->CheckVariableDeclaration(st->Id(), st->TypeAnnotation(), st->Value(), st->Modifiers())); return st->TsType(); } diff --git a/ets2panda/ir/astNode.cpp b/ets2panda/ir/astNode.cpp index bceb87943b4..4db50cff077 100644 --- a/ets2panda/ir/astNode.cpp +++ b/ets2panda/ir/astNode.cpp @@ -92,7 +92,7 @@ bool AstNode::IsAnyChild(const NodePredicate &cb) const void AnnotatedAstNode::CloneTypeAnnotation(ArenaAllocator *const allocator) { if (auto *annotation = const_cast(TypeAnnotation()); annotation != nullptr) { - SetTsTypeAnnotation(annotation->Clone(allocator, this)->AsTypeNode()); + SetTsTypeAnnotation(annotation->Clone(allocator, this)); } } diff --git a/ets2panda/ir/astNode.h b/ets2panda/ir/astNode.h index 950d346f6f7..f164f1660c7 100644 --- a/ets2panda/ir/astNode.h +++ b/ets2panda/ir/astNode.h @@ -448,6 +448,13 @@ public: [[nodiscard]] ir::BlockStatement *GetTopStatement(); [[nodiscard]] const ir::BlockStatement *GetTopStatement() const; + // NOLINTNEXTLINE(google-default-arguments) + [[nodiscard]] virtual AstNode *Clone([[maybe_unused]] ArenaAllocator *const allocator, + [[maybe_unused]] AstNode *const parent = nullptr) + { + UNREACHABLE(); + } + virtual void TransformChildren(const NodeTransformer &cb) = 0; virtual void Iterate(const NodeTraverser &cb) const = 0; void TransformChildrenRecursively(const NodeTransformer &cb); diff --git a/ets2panda/ir/base/classDefinition.cpp b/ets2panda/ir/base/classDefinition.cpp index 12e57a0dbcb..31520c1ffd1 100644 --- a/ets2panda/ir/base/classDefinition.cpp +++ b/ets2panda/ir/base/classDefinition.cpp @@ -1,5 +1,5 @@ /** - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021 - 2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at diff --git a/ets2panda/ir/base/classDefinition.h b/ets2panda/ir/base/classDefinition.h index 7341aab83e5..b952989226c 100644 --- a/ets2panda/ir/base/classDefinition.h +++ b/ets2panda/ir/base/classDefinition.h @@ -1,5 +1,5 @@ /** - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021 - 2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -50,6 +50,12 @@ DEFINE_BITOPS(ClassDefinitionModifiers) class ClassDefinition : public TypedAstNode { public: + ClassDefinition() = delete; + ~ClassDefinition() override = default; + + NO_COPY_SEMANTIC(ClassDefinition); + NO_MOVE_SEMANTIC(ClassDefinition); + explicit ClassDefinition(varbinder::LocalScope *scope, const util::StringView &private_id, Identifier *ident, TSTypeParameterDeclaration *type_params, TSTypeParameterInstantiation *super_type_params, ArenaVector &&implements, MethodDefinition *ctor, @@ -104,82 +110,82 @@ public: return scope_; } - const Identifier *Ident() const + [[nodiscard]] const Identifier *Ident() const noexcept { return ident_; } - Identifier *Ident() + [[nodiscard]] Identifier *Ident() noexcept { return ident_; } - void SetIdent(ir::Identifier *ident) + void SetIdent(ir::Identifier *ident) noexcept { ident_ = ident; } - const util::StringView &PrivateId() const + [[nodiscard]] const util::StringView &PrivateId() const noexcept { return private_id_; } - const util::StringView &InternalName() const + [[nodiscard]] const util::StringView &InternalName() const noexcept { return private_id_; } - void SetInternalName(util::StringView internal_name) + void SetInternalName(util::StringView internal_name) noexcept { private_id_ = internal_name; } - Expression *Super() + [[nodiscard]] Expression *Super() noexcept { return super_class_; } - const Expression *Super() const + [[nodiscard]] const Expression *Super() const noexcept { return super_class_; } - bool IsGlobal() const + [[nodiscard]] bool IsGlobal() const noexcept { return (modifiers_ & ClassDefinitionModifiers::GLOBAL) != 0; } - bool IsExtern() const + [[nodiscard]] bool IsExtern() const noexcept { return (modifiers_ & ClassDefinitionModifiers::EXTERN) != 0; } - bool IsInner() const + [[nodiscard]] bool IsInner() const noexcept { return (modifiers_ & ClassDefinitionModifiers::INNER) != 0; } - bool IsGlobalInitialized() const + [[nodiscard]] bool IsGlobalInitialized() const noexcept { return (modifiers_ & ClassDefinitionModifiers::GLOBAL_INITIALIZED) != 0; } - es2panda::Language Language() const + [[nodiscard]] es2panda::Language Language() const noexcept { return lang_; } - void SetGlobalInitialized() + void SetGlobalInitialized() noexcept { modifiers_ |= ClassDefinitionModifiers::GLOBAL_INITIALIZED; } - void SetInnerModifier() + void SetInnerModifier() noexcept { modifiers_ |= ClassDefinitionModifiers::INNER; } - ClassDefinitionModifiers Modifiers() const + [[nodiscard]] ClassDefinitionModifiers Modifiers() const noexcept { return modifiers_; } @@ -193,37 +199,37 @@ public: body_.insert(body_.end(), body.begin(), body.end()); } - ArenaVector &Body() + [[nodiscard]] ArenaVector &Body() noexcept { return body_; } - const ArenaVector &Body() const + [[nodiscard]] const ArenaVector &Body() const noexcept { return body_; } - MethodDefinition *Ctor() + [[nodiscard]] MethodDefinition *Ctor() noexcept { return ctor_; } - ArenaVector &Implements() + [[nodiscard]] ArenaVector &Implements() noexcept { return implements_; } - const ArenaVector &Implements() const + [[nodiscard]] const ArenaVector &Implements() const noexcept { return implements_; } - const ir::TSTypeParameterDeclaration *TypeParams() const + [[nodiscard]] const ir::TSTypeParameterDeclaration *TypeParams() const noexcept { return type_params_; } - ir::TSTypeParameterDeclaration *TypeParams() + [[nodiscard]] ir::TSTypeParameterDeclaration *TypeParams() noexcept { return type_params_; } @@ -235,7 +241,9 @@ public: void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; + void Dump(ir::AstDumper *dumper) const override; + void Compile(compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check(checker::TSChecker *checker) override; diff --git a/ets2panda/ir/base/classElement.cpp b/ets2panda/ir/base/classElement.cpp index 192aecf3038..62b0ebb1003 100644 --- a/ets2panda/ir/base/classElement.cpp +++ b/ets2panda/ir/base/classElement.cpp @@ -20,17 +20,17 @@ namespace panda::es2panda::ir { -Identifier *ClassElement::Id() +Identifier *ClassElement::Id() noexcept { - return key_->AsIdentifier(); + return key_->IsIdentifier() ? key_->AsIdentifier() : nullptr; } -const Identifier *ClassElement::Id() const +const Identifier *ClassElement::Id() const noexcept { - return key_->AsIdentifier(); + return key_->IsIdentifier() ? key_->AsIdentifier() : nullptr; } -bool ClassElement::IsPrivateElement() const +bool ClassElement::IsPrivateElement() const noexcept { if (IsClassStaticBlock()) { return false; diff --git a/ets2panda/ir/base/classElement.h b/ets2panda/ir/base/classElement.h index ea99fb15665..034f63036d5 100644 --- a/ets2panda/ir/base/classElement.h +++ b/ets2panda/ir/base/classElement.h @@ -23,8 +23,14 @@ class Expression; class ClassElement : public TypedStatement { public: - explicit ClassElement(AstNodeType element_type, Expression *key, Expression *value, ModifierFlags modifiers, - ArenaAllocator *allocator, bool is_computed) + ClassElement() = delete; + ~ClassElement() override = default; + + NO_COPY_SEMANTIC(ClassElement); + NO_MOVE_SEMANTIC(ClassElement); + + explicit ClassElement(AstNodeType const element_type, Expression *const key, Expression *const value, + ModifierFlags const modifiers, ArenaAllocator *const allocator, bool const is_computed) : TypedStatement(element_type, modifiers), key_(key), value_(value), @@ -33,37 +39,38 @@ public: { } - Identifier *Id(); - const Identifier *Id() const; + [[nodiscard]] Identifier *Id() noexcept; + + [[nodiscard]] const Identifier *Id() const noexcept; - Expression *Key() + [[nodiscard]] Expression *Key() noexcept { return key_; } - const Expression *Key() const + [[nodiscard]] const Expression *Key() const noexcept { return key_; } - Expression *Value() + [[nodiscard]] Expression *Value() noexcept { return value_; } - const Expression *Value() const + [[nodiscard]] const Expression *Value() const noexcept { return value_; } - bool IsPrivateElement() const; + [[nodiscard]] bool IsPrivateElement() const noexcept; - const ArenaVector &Decorators() const + [[nodiscard]] const ArenaVector &Decorators() const noexcept { return decorators_; } - bool IsComputed() const + [[nodiscard]] bool IsComputed() const noexcept { return is_computed_; } @@ -73,7 +80,14 @@ public: decorators_ = std::move(decorators); } - virtual PrivateFieldKind ToPrivateFieldKind(bool is_static) const = 0; + void AddDecorator(ir::Decorator *const decorator) + { + if (decorator != nullptr) { + decorators_.emplace_back(decorator); + } + } + + [[nodiscard]] virtual PrivateFieldKind ToPrivateFieldKind(bool is_static) const = 0; protected: // NOLINTBEGIN(misc-non-private-member-variables-in-classes) diff --git a/ets2panda/ir/base/classProperty.cpp b/ets2panda/ir/base/classProperty.cpp index 24bfd844c1b..081a992abfc 100644 --- a/ets2panda/ir/base/classProperty.cpp +++ b/ets2panda/ir/base/classProperty.cpp @@ -1,5 +1,5 @@ /** - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021 - 2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -26,9 +26,6 @@ #include "ir/expression.h" #include "ir/expressions/identifier.h" -#include -#include - namespace panda::es2panda::ir { void ClassProperty::TransformChildren(const NodeTransformer &cb) { @@ -100,4 +97,31 @@ checker::Type *ClassProperty::Check(checker::ETSChecker *checker) { return checker->GetAnalyzer()->Check(this); } + +// NOLINTNEXTLINE(google-default-arguments) +ClassProperty *ClassProperty::Clone(ArenaAllocator *const allocator, AstNode *const parent) +{ + auto *const key = key_->Clone(allocator)->AsExpression(); + auto *const value = value_->Clone(allocator)->AsExpression(); + auto *const type_annotation = type_annotation_->Clone(allocator, this); + + if (auto *const clone = allocator->New(key, value, type_annotation, flags_, allocator, is_computed_); + clone != nullptr) { + if (parent != nullptr) { + clone->SetParent(parent); + } + + key->SetParent(clone); + value->SetParent(clone); + type_annotation->SetParent(clone); + + for (auto *const decorator : decorators_) { + clone->AddDecorator(decorator->Clone(allocator, clone)); + } + + return clone; + } + + throw Error(ErrorType::GENERIC, "", CLONE_ALLOCATION_ERROR); +} } // namespace panda::es2panda::ir diff --git a/ets2panda/ir/base/classProperty.h b/ets2panda/ir/base/classProperty.h index 82738acdab8..fc9773fc0f7 100644 --- a/ets2panda/ir/base/classProperty.h +++ b/ets2panda/ir/base/classProperty.h @@ -28,28 +28,37 @@ class TypeNode; class ClassProperty : public ClassElement { public: - explicit ClassProperty(Expression *key, Expression *value, TypeNode *type_annotation, ModifierFlags modifiers, - ArenaAllocator *allocator, bool is_computed) + ClassProperty() = delete; + ~ClassProperty() override = default; + + NO_COPY_SEMANTIC(ClassProperty); + NO_MOVE_SEMANTIC(ClassProperty); + + explicit ClassProperty(Expression *const key, Expression *const value, TypeNode *const type_annotation, + ModifierFlags const modifiers, ArenaAllocator *const allocator, bool const is_computed) : ClassElement(AstNodeType::CLASS_PROPERTY, key, value, modifiers, allocator, is_computed), type_annotation_(type_annotation) { } - // NOTE: csabahurton. friend relationship can be removed once there are getters for private fields - friend class checker::ETSAnalyzer; - TypeNode *TypeAnnotation() const + [[nodiscard]] TypeNode *TypeAnnotation() const noexcept { return type_annotation_; } - PrivateFieldKind ToPrivateFieldKind(bool is_static) const override + [[nodiscard]] PrivateFieldKind ToPrivateFieldKind(bool const is_static) const override { return is_static ? PrivateFieldKind::STATIC_FIELD : PrivateFieldKind::FIELD; } + // NOLINTNEXTLINE(google-default-arguments) + [[nodiscard]] ClassProperty *Clone(ArenaAllocator *allocator, AstNode *parent = nullptr) override; + void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; + void Dump(ir::AstDumper *dumper) const override; + void Compile(compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; checker::Type *Check(checker::TSChecker *checker) override; diff --git a/ets2panda/ir/base/decorator.cpp b/ets2panda/ir/base/decorator.cpp index 3b9e5c419b8..beb884f79b0 100644 --- a/ets2panda/ir/base/decorator.cpp +++ b/ets2panda/ir/base/decorator.cpp @@ -60,9 +60,9 @@ checker::Type *Decorator::Check(checker::ETSChecker *checker) } // NOLINTNEXTLINE(google-default-arguments) -Statement *Decorator::Clone(ArenaAllocator *const allocator, AstNode *const parent) +Decorator *Decorator::Clone(ArenaAllocator *const allocator, AstNode *const parent) { - auto *const expr = expr_ != nullptr ? expr_->Clone(allocator) : nullptr; + auto *const expr = expr_ != nullptr ? expr_->Clone(allocator)->AsExpression() : nullptr; if (auto *const clone = allocator->New(expr); clone != nullptr) { if (expr != nullptr) { diff --git a/ets2panda/ir/base/decorator.h b/ets2panda/ir/base/decorator.h index aadfb87af22..e85858d4b34 100644 --- a/ets2panda/ir/base/decorator.h +++ b/ets2panda/ir/base/decorator.h @@ -37,7 +37,7 @@ public: } // NOLINTNEXTLINE(google-default-arguments) - [[nodiscard]] Statement *Clone(ArenaAllocator *allocator, AstNode *parent = nullptr) override; + [[nodiscard]] Decorator *Clone(ArenaAllocator *allocator, AstNode *parent = nullptr) override; void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; diff --git a/ets2panda/ir/base/metaProperty.cpp b/ets2panda/ir/base/metaProperty.cpp index f88b13cc03b..cba2f3402a8 100644 --- a/ets2panda/ir/base/metaProperty.cpp +++ b/ets2panda/ir/base/metaProperty.cpp @@ -70,7 +70,7 @@ checker::Type *MetaProperty::Check([[maybe_unused]] checker::ETSChecker *checker } // NOLINTNEXTLINE(google-default-arguments) -Expression *MetaProperty::Clone(ArenaAllocator *const allocator, AstNode *const parent) +MetaProperty *MetaProperty::Clone(ArenaAllocator *const allocator, AstNode *const parent) { if (auto *const clone = allocator->New(kind_); clone != nullptr) { if (parent != nullptr) { diff --git a/ets2panda/ir/base/metaProperty.h b/ets2panda/ir/base/metaProperty.h index 7a3d4e7f173..6ba62e83feb 100644 --- a/ets2panda/ir/base/metaProperty.h +++ b/ets2panda/ir/base/metaProperty.h @@ -44,7 +44,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; // NOLINTNEXTLINE(google-default-arguments) - [[nodiscard]] Expression *Clone(ArenaAllocator *allocator, AstNode *parent = nullptr) override; + [[nodiscard]] MetaProperty *Clone(ArenaAllocator *allocator, AstNode *parent = nullptr) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; diff --git a/ets2panda/ir/base/methodDefinition.cpp b/ets2panda/ir/base/methodDefinition.cpp index cff3cf3c6bd..0f59befb68d 100644 --- a/ets2panda/ir/base/methodDefinition.cpp +++ b/ets2panda/ir/base/methodDefinition.cpp @@ -33,17 +33,17 @@ namespace panda::es2panda::ir { -ScriptFunction *MethodDefinition::Function() +ScriptFunction *MethodDefinition::Function() noexcept { - return value_->AsFunctionExpression()->Function(); + return value_->IsFunctionExpression() ? value_->AsFunctionExpression()->Function() : nullptr; } -const ScriptFunction *MethodDefinition::Function() const +const ScriptFunction *MethodDefinition::Function() const noexcept { - return value_->AsFunctionExpression()->Function(); + return value_->IsFunctionExpression() ? value_->AsFunctionExpression()->Function() : nullptr; } -PrivateFieldKind MethodDefinition::ToPrivateFieldKind(bool is_static) const +PrivateFieldKind MethodDefinition::ToPrivateFieldKind(bool const is_static) const { switch (kind_) { case MethodDefinitionKind::METHOD: { @@ -331,4 +331,38 @@ void MethodDefinition::CheckMethodModifiers(checker::ETSChecker *checker) "Invalid method modifier(s): a static method can't have abstract, final or override modifier.", Start()); } } + +// NOLINTNEXTLINE(google-default-arguments) +MethodDefinition *MethodDefinition::Clone(ArenaAllocator *const allocator, AstNode *const parent) +{ + auto *const key = key_ != nullptr ? key_->Clone(allocator)->AsExpression() : nullptr; + auto *const value = value_ != nullptr ? value_->Clone(allocator)->AsExpression() : nullptr; + + if (auto *const clone = allocator->New(kind_, key, value, flags_, allocator, is_computed_); + clone != nullptr) { + if (parent != nullptr) { + clone->SetParent(parent); + } + + if (key != nullptr) { + key->SetParent(clone); + } + + if (value != nullptr) { + value->SetParent(clone); + } + + for (auto *const decorator : decorators_) { + clone->AddDecorator(decorator->Clone(allocator, clone)); + } + + for (auto *const overloads : overloads_) { + clone->AddOverload(overloads->Clone(allocator, clone)); + } + + return clone; + } + + throw Error(ErrorType::GENERIC, "", CLONE_ALLOCATION_ERROR); +} } // namespace panda::es2panda::ir diff --git a/ets2panda/ir/base/methodDefinition.h b/ets2panda/ir/base/methodDefinition.h index 079bddf66cf..3040a05bd0e 100644 --- a/ets2panda/ir/base/methodDefinition.h +++ b/ets2panda/ir/base/methodDefinition.h @@ -1,5 +1,5 @@ /** - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021 - 2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -16,41 +16,54 @@ #ifndef ES2PANDA_PARSER_INCLUDE_AST_METHOD_DEFINITION_H #define ES2PANDA_PARSER_INCLUDE_AST_METHOD_DEFINITION_H -#include "checker/types/ets/etsObjectType.h" -#include "checker/types/signature.h" #include "ir/base/classElement.h" +namespace panda::es2panda::checker { + +class ETSObjectType; +class Signature; + +} // namespace panda::es2panda::checker + namespace panda::es2panda::ir { + class Expression; +class ScriptFunction; enum class MethodDefinitionKind { NONE, CONSTRUCTOR, METHOD, EXTENSION_METHOD, GET, SET }; class MethodDefinition : public ClassElement { public: - explicit MethodDefinition(MethodDefinitionKind kind, Expression *key, Expression *value, ModifierFlags modifiers, - ArenaAllocator *allocator, bool is_computed) + MethodDefinition() = delete; + ~MethodDefinition() override = default; + + NO_COPY_SEMANTIC(MethodDefinition); + NO_MOVE_SEMANTIC(MethodDefinition); + + explicit MethodDefinition(MethodDefinitionKind const kind, Expression *const key, Expression *const value, + ModifierFlags const modifiers, ArenaAllocator *const allocator, bool const is_computed) : ClassElement(AstNodeType::METHOD_DEFINITION, key, value, modifiers, allocator, is_computed), kind_(kind), overloads_(allocator->Adapter()) { } - MethodDefinitionKind Kind() const + [[nodiscard]] MethodDefinitionKind Kind() const noexcept { return kind_; } - bool IsConstructor() const + [[nodiscard]] bool IsConstructor() const noexcept { return kind_ == MethodDefinitionKind::CONSTRUCTOR; } - bool IsExtensionMethod() const + [[nodiscard]] bool IsExtensionMethod() const noexcept { return kind_ == MethodDefinitionKind::EXTENSION_METHOD; } - const ArenaVector &Overloads() const + [[nodiscard]] const ArenaVector &Overloads() const noexcept { return overloads_; } @@ -60,19 +73,21 @@ public: overloads_ = std::move(overloads); } - void AddOverload(MethodDefinition *overload) + void AddOverload(MethodDefinition *const overload) { - overloads_.push_back(overload); + overloads_.emplace_back(overload); } - bool HasOverload(MethodDefinition *overload) + [[nodiscard]] bool HasOverload(MethodDefinition *overload) noexcept { return std::find(overloads_.begin(), overloads_.end(), overload) != overloads_.end(); } - ScriptFunction *Function(); - const ScriptFunction *Function() const; - PrivateFieldKind ToPrivateFieldKind(bool is_static) const override; + [[nodiscard]] ScriptFunction *Function() noexcept; + [[nodiscard]] const ScriptFunction *Function() const noexcept; + + [[nodiscard]] PrivateFieldKind ToPrivateFieldKind(bool is_static) const override; + void CheckMethodModifiers(checker::ETSChecker *checker); void CheckExtensionMethod(checker::ETSChecker *checker, ScriptFunction *extension_func); void CheckExtensionIsShadowedByMethod(checker::ETSChecker *checker, checker::ETSObjectType *obj_type, @@ -82,9 +97,14 @@ public: ScriptFunction *extension_func, checker::Signature *sigature); + // NOLINTNEXTLINE(google-default-arguments) + [[nodiscard]] MethodDefinition *Clone(ArenaAllocator *allocator, AstNode *parent = nullptr) override; + void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; + void Dump(ir::AstDumper *dumper) const override; + void Compile([[maybe_unused]] compiler::PandaGen *pg) const override; void Compile([[maybe_unused]] compiler::ETSGen *etsg) const override; checker::Type *Check([[maybe_unused]] checker::TSChecker *checker) override; diff --git a/ets2panda/ir/base/property.cpp b/ets2panda/ir/base/property.cpp index f607be57b12..bc0c8e754ec 100644 --- a/ets2panda/ir/base/property.cpp +++ b/ets2panda/ir/base/property.cpp @@ -33,10 +33,10 @@ Property::Property([[maybe_unused]] Tag const tag, Expression *const key, Expres } // NOLINTNEXTLINE(google-default-arguments) -Expression *Property::Clone(ArenaAllocator *const allocator, AstNode *const parent) +Property *Property::Clone(ArenaAllocator *const allocator, AstNode *const parent) { - auto *const key = key_ != nullptr ? key_->Clone(allocator) : nullptr; - auto *const value = value_ != nullptr ? value_->Clone(allocator) : nullptr; + auto *const key = key_ != nullptr ? key_->Clone(allocator)->AsExpression() : nullptr; + auto *const value = value_ != nullptr ? value_->Clone(allocator)->AsExpression() : nullptr; if (auto *const clone = allocator->New(Tag {}, key, value); clone != nullptr) { if (key != nullptr) { diff --git a/ets2panda/ir/base/property.h b/ets2panda/ir/base/property.h index 91d91512c44..1d40da12463 100644 --- a/ets2panda/ir/base/property.h +++ b/ets2panda/ir/base/property.h @@ -103,7 +103,7 @@ public: } // NOLINTNEXTLINE(google-default-arguments) - [[nodiscard]] Expression *Clone(ArenaAllocator *allocator, AstNode *parent = nullptr) override; + [[nodiscard]] Property *Clone(ArenaAllocator *allocator, AstNode *parent = nullptr) override; bool ConvertibleToPatternProperty(); ValidationInfo ValidateExpression(); diff --git a/ets2panda/ir/base/scriptFunction.cpp b/ets2panda/ir/base/scriptFunction.cpp index d7477e92b5b..bebc4021c33 100644 --- a/ets2panda/ir/base/scriptFunction.cpp +++ b/ets2panda/ir/base/scriptFunction.cpp @@ -26,26 +26,17 @@ #include "ir/ts/tsTypeParameterDeclaration.h" namespace panda::es2panda::ir { -bool ScriptFunction::HasBody() const -{ - return body_ != nullptr; -} - -ir::ScriptFunctionFlags ScriptFunction::Flags() const -{ - return func_flags_; -} -size_t ScriptFunction::FormalParamsLength() const +std::size_t ScriptFunction::FormalParamsLength() const noexcept { - size_t length = 0; + std::size_t length = 0U; for (const auto *param : params_) { if (param->IsRestElement() || param->IsAssignmentPattern()) { break; } - length++; + ++length; } return length; diff --git a/ets2panda/ir/base/scriptFunction.h b/ets2panda/ir/base/scriptFunction.h index 93e13442271..94fb4d61845 100644 --- a/ets2panda/ir/base/scriptFunction.h +++ b/ets2panda/ir/base/scriptFunction.h @@ -1,5 +1,5 @@ /** - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021 - 2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -30,7 +30,14 @@ class TSTypeParameterDeclaration; class TypeNode; class ScriptFunction : public AstNode { +private: public: + ScriptFunction() = delete; + ~ScriptFunction() override = default; + + NO_COPY_SEMANTIC(ScriptFunction); + NO_MOVE_SEMANTIC(ScriptFunction); + explicit ScriptFunction(varbinder::FunctionScope *scope, ArenaVector &¶ms, TSTypeParameterDeclaration *type_params, AstNode *body, TypeNode *return_type_annotation, ir::ScriptFunctionFlags func_flags, bool declare, Language lang) @@ -61,164 +68,167 @@ public: { } - const Identifier *Id() const + [[nodiscard]] const Identifier *Id() const noexcept { return id_; } - Identifier *Id() + [[nodiscard]] Identifier *Id() noexcept { return id_; } - const checker::Signature *Signature() const + [[nodiscard]] const checker::Signature *Signature() const noexcept { return signature_; } - checker::Signature *Signature() + [[nodiscard]] checker::Signature *Signature() noexcept { return signature_; } - const ArenaVector &Params() const + [[nodiscard]] const ArenaVector &Params() const noexcept { return params_; } - ArenaVector &Params() + [[nodiscard]] ArenaVector &Params() noexcept { return params_; } - const TSTypeParameterDeclaration *TypeParams() const + [[nodiscard]] const TSTypeParameterDeclaration *TypeParams() const noexcept { return type_params_; } - TSTypeParameterDeclaration *TypeParams() + [[nodiscard]] TSTypeParameterDeclaration *TypeParams() noexcept { return type_params_; } - const AstNode *Body() const + [[nodiscard]] const AstNode *Body() const noexcept { return body_; } - AstNode *Body() + [[nodiscard]] AstNode *Body() noexcept { return body_; } - void SetBody(AstNode *body) + void SetBody(AstNode *body) noexcept { body_ = body; } - const TypeNode *ReturnTypeAnnotation() const + [[nodiscard]] const TypeNode *ReturnTypeAnnotation() const noexcept { return return_type_annotation_; } - TypeNode *ReturnTypeAnnotation() + [[nodiscard]] TypeNode *ReturnTypeAnnotation() noexcept { return return_type_annotation_; } - void SetReturnTypeAnnotation(TypeNode *node) + void SetReturnTypeAnnotation(TypeNode *node) noexcept { return_type_annotation_ = node; } - bool IsEntryPoint() const + [[nodiscard]] bool IsEntryPoint() const noexcept { return (func_flags_ & ir::ScriptFunctionFlags::ENTRY_POINT) != 0; } - bool IsGenerator() const + [[nodiscard]] bool IsGenerator() const noexcept { return (func_flags_ & ir::ScriptFunctionFlags::GENERATOR) != 0; } - bool IsAsyncFunc() const + [[nodiscard]] bool IsAsyncFunc() const noexcept { return (func_flags_ & ir::ScriptFunctionFlags::ASYNC) != 0; } - bool IsArrow() const + [[nodiscard]] bool IsArrow() const noexcept { return (func_flags_ & ir::ScriptFunctionFlags::ARROW) != 0; } - bool IsOverload() const + [[nodiscard]] bool IsOverload() const noexcept { return (func_flags_ & ir::ScriptFunctionFlags::OVERLOAD) != 0; } - bool IsConstructor() const + [[nodiscard]] bool IsConstructor() const noexcept { return (func_flags_ & ir::ScriptFunctionFlags::CONSTRUCTOR) != 0; } - bool IsGetter() const + [[nodiscard]] bool IsGetter() const noexcept { return (func_flags_ & ir::ScriptFunctionFlags::GETTER) != 0; } - bool IsSetter() const + [[nodiscard]] bool IsSetter() const noexcept { return (func_flags_ & ir::ScriptFunctionFlags::SETTER) != 0; } - bool IsMethod() const + [[nodiscard]] bool IsMethod() const noexcept { return (func_flags_ & ir::ScriptFunctionFlags::METHOD) != 0; } - bool IsProxy() const + [[nodiscard]] bool IsProxy() const noexcept { return (func_flags_ & ir::ScriptFunctionFlags::PROXY) != 0; } - bool IsStaticBlock() const + [[nodiscard]] bool IsStaticBlock() const noexcept { return (func_flags_ & ir::ScriptFunctionFlags::STATIC_BLOCK) != 0; } - bool IsEnum() const + [[nodiscard]] bool IsEnum() const noexcept { return (func_flags_ & ir::ScriptFunctionFlags::ENUM) != 0; } - bool IsHidden() const + [[nodiscard]] bool IsHidden() const noexcept { return (func_flags_ & ir::ScriptFunctionFlags::HIDDEN) != 0; } - bool IsExternal() const + [[nodiscard]] bool IsExternal() const noexcept { return (func_flags_ & ir::ScriptFunctionFlags::EXTERNAL) != 0; } - bool IsImplicitSuperCallNeeded() const + [[nodiscard]] bool IsImplicitSuperCallNeeded() const noexcept { return (func_flags_ & ir::ScriptFunctionFlags::IMPLICIT_SUPER_CALL_NEEDED) != 0; } - bool HasBody() const; + [[nodiscard]] bool HasBody() const noexcept + { + return body_ != nullptr; + } - bool IsThrowing() const + [[nodiscard]] bool IsThrowing() const noexcept { return (func_flags_ & ir::ScriptFunctionFlags::THROWS) != 0; } - bool IsRethrowing() const + [[nodiscard]] bool IsRethrowing() const noexcept { return (func_flags_ & ir::ScriptFunctionFlags::RETHROWS) != 0; } - bool IsDefaultParamProxy() const noexcept + [[nodiscard]] bool IsDefaultParamProxy() const noexcept { return (func_flags_ & ir::ScriptFunctionFlags::DEFAULT_PARAM_PROXY) != 0; } @@ -228,44 +238,47 @@ public: AddFlag(ir::ScriptFunctionFlags::DEFAULT_PARAM_PROXY); } - bool IsDynamic() const + [[nodiscard]] bool IsDynamic() const noexcept { return lang_.IsDynamic(); } - bool IsExtensionMethod() const + [[nodiscard]] bool IsExtensionMethod() const noexcept { return (func_flags_ & ir::ScriptFunctionFlags::INSTANCE_EXTENSION_METHOD) != 0; } - bool Declare() const + [[nodiscard]] bool Declare() const noexcept { return declare_; } - ir::ScriptFunctionFlags Flags() const; + [[nodiscard]] ir::ScriptFunctionFlags Flags() const noexcept + { + return func_flags_; + } - void SetIdent(Identifier *id) + void SetIdent(Identifier *id) noexcept { id_ = id; } - void SetSignature(checker::Signature *signature) + void SetSignature(checker::Signature *signature) noexcept { signature_ = signature; } - void AddFlag(ir::ScriptFunctionFlags flags) + void AddFlag(ir::ScriptFunctionFlags flags) noexcept { func_flags_ |= flags; } - void AddModifier(ir::ModifierFlags flags) + void AddModifier(ir::ModifierFlags flags) noexcept { flags_ |= flags; } - size_t FormalParamsLength() const; + [[nodiscard]] std::size_t FormalParamsLength() const noexcept; bool IsScopeBearer() const override { @@ -277,15 +290,16 @@ public: return scope_; } - void TransformChildren(const NodeTransformer &cb) override; - - es2panda::Language Language() const + [[nodiscard]] es2panda::Language Language() const { return lang_; } + void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; + void Dump(ir::AstDumper *dumper) const override; + void Compile([[maybe_unused]] compiler::PandaGen *pg) const override; void Compile([[maybe_unused]] compiler::ETSGen *etsg) const override; checker::Type *Check([[maybe_unused]] checker::TSChecker *checker) override; diff --git a/ets2panda/ir/base/spreadElement.cpp b/ets2panda/ir/base/spreadElement.cpp index 1f995a1f116..e7ff3e06229 100644 --- a/ets2panda/ir/base/spreadElement.cpp +++ b/ets2panda/ir/base/spreadElement.cpp @@ -31,16 +31,16 @@ SpreadElement::SpreadElement([[maybe_unused]] Tag const tag, SpreadElement const optional_ = other.optional_; if (other.argument_ != nullptr) { - argument_ = other.argument_->Clone(allocator, this); + argument_ = other.argument_->Clone(allocator, this)->AsExpression(); } for (auto *decorator : other.decorators_) { - decorators_.emplace_back(decorator->Clone(allocator, this)->AsDecorator()); + decorators_.emplace_back(decorator->Clone(allocator, this)); } } // NOLINTNEXTLINE(google-default-arguments) -Expression *SpreadElement::Clone(ArenaAllocator *const allocator, AstNode *const parent) +SpreadElement *SpreadElement::Clone(ArenaAllocator *const allocator, AstNode *const parent) { if (auto *const clone = allocator->New(Tag {}, *this, allocator); clone != nullptr) { if (parent != nullptr) { diff --git a/ets2panda/ir/base/spreadElement.h b/ets2panda/ir/base/spreadElement.h index 037cc59ca40..11ef4991fb6 100644 --- a/ets2panda/ir/base/spreadElement.h +++ b/ets2panda/ir/base/spreadElement.h @@ -69,7 +69,7 @@ public: } // NOLINTNEXTLINE(google-default-arguments) - [[nodiscard]] Expression *Clone(ArenaAllocator *allocator, AstNode *parent = nullptr) override; + [[nodiscard]] SpreadElement *Clone(ArenaAllocator *allocator, AstNode *parent = nullptr) override; ValidationInfo ValidateExpression(); [[nodiscard]] bool ConvertibleToRest(bool is_declaration, bool allow_pattern = true); diff --git a/ets2panda/ir/base/templateElement.cpp b/ets2panda/ir/base/templateElement.cpp index 8a0e7d317ee..d48fe642795 100644 --- a/ets2panda/ir/base/templateElement.cpp +++ b/ets2panda/ir/base/templateElement.cpp @@ -51,7 +51,7 @@ checker::Type *TemplateElement::Check([[maybe_unused]] checker::ETSChecker *chec } // NOLINTNEXTLINE(google-default-arguments) -Expression *TemplateElement::Clone(ArenaAllocator *const allocator, AstNode *const parent) +TemplateElement *TemplateElement::Clone(ArenaAllocator *const allocator, AstNode *const parent) { if (auto *const clone = allocator->New(raw_, cooked_); clone != nullptr) { if (parent != nullptr) { diff --git a/ets2panda/ir/base/templateElement.h b/ets2panda/ir/base/templateElement.h index b373d7aff8c..55280eb77bb 100644 --- a/ets2panda/ir/base/templateElement.h +++ b/ets2panda/ir/base/templateElement.h @@ -45,7 +45,7 @@ public: } // NOLINTNEXTLINE(google-default-arguments) - [[nodiscard]] Expression *Clone(ArenaAllocator *allocator, AstNode *parent = nullptr) override; + [[nodiscard]] TemplateElement *Clone(ArenaAllocator *allocator, AstNode *parent = nullptr) override; void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; diff --git a/ets2panda/ir/base/tsIndexSignature.cpp b/ets2panda/ir/base/tsIndexSignature.cpp index 09598d5b248..1297bd78f32 100644 --- a/ets2panda/ir/base/tsIndexSignature.cpp +++ b/ets2panda/ir/base/tsIndexSignature.cpp @@ -1,5 +1,5 @@ /** - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021 - 2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -16,13 +16,12 @@ #include "tsIndexSignature.h" #include "ir/astDump.h" -#include "ir/typeNode.h" #include "ir/expressions/identifier.h" #include "checker/TSchecker.h" namespace panda::es2panda::ir { -TSIndexSignature::TSIndexSignatureKind TSIndexSignature::Kind() const +TSIndexSignature::TSIndexSignatureKind TSIndexSignature::Kind() const noexcept { return param_->AsIdentifier()->TypeAnnotation()->IsTSNumberKeyword() ? TSIndexSignatureKind::NUMBER : TSIndexSignatureKind::STRING; @@ -78,4 +77,24 @@ checker::Type *TSIndexSignature::Check([[maybe_unused]] checker::ETSChecker *che { return nullptr; } + +// NOLINTNEXTLINE(google-default-arguments) +TSIndexSignature *TSIndexSignature::Clone(ArenaAllocator *const allocator, AstNode *const parent) +{ + auto *const param = param_ != nullptr ? param_->Clone(allocator)->AsExpression() : nullptr; + auto *const type_annotation = type_annotation_->Clone(allocator); + + if (auto *const clone = allocator->New(param, type_annotation, readonly_); clone != nullptr) { + if (parent != nullptr) { + clone->SetParent(parent); + } + if (param != nullptr) { + param->SetParent(clone); + } + type_annotation->SetParent(clone); + return clone; + } + + throw Error(ErrorType::GENERIC, "", CLONE_ALLOCATION_ERROR); +} } // namespace panda::es2panda::ir diff --git a/ets2panda/ir/base/tsIndexSignature.h b/ets2panda/ir/base/tsIndexSignature.h index db29746c915..ed9bc3d8c1b 100644 --- a/ets2panda/ir/base/tsIndexSignature.h +++ b/ets2panda/ir/base/tsIndexSignature.h @@ -1,5 +1,5 @@ /** - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021 - 2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -16,14 +16,20 @@ #ifndef ES2PANDA_PARSER_INCLUDE_AST_TS_INDEX_SIGNATURE_H #define ES2PANDA_PARSER_INCLUDE_AST_TS_INDEX_SIGNATURE_H -#include "ir/statement.h" +#include "ir/typeNode.h" namespace panda::es2panda::ir { class TSIndexSignature : public TypedAstNode { public: enum class TSIndexSignatureKind { NUMBER, STRING }; - explicit TSIndexSignature(Expression *param, TypeNode *type_annotation, bool readonly) + TSIndexSignature() = delete; + ~TSIndexSignature() override = default; + + NO_COPY_SEMANTIC(TSIndexSignature); + NO_MOVE_SEMANTIC(TSIndexSignature); + + explicit TSIndexSignature(Expression *const param, TypeNode *const type_annotation, bool const readonly) : TypedAstNode(AstNodeType::TS_INDEX_SIGNATURE), param_(param), type_annotation_(type_annotation), @@ -31,26 +37,31 @@ public: { } - const Expression *Param() const + [[nodiscard]] const Expression *Param() const noexcept { return param_; } - const TypeNode *TypeAnnotation() const + [[nodiscard]] const TypeNode *TypeAnnotation() const noexcept { return type_annotation_; } - bool Readonly() const + [[nodiscard]] bool Readonly() const noexcept { return readonly_; } - TSIndexSignatureKind Kind() const; + [[nodiscard]] TSIndexSignatureKind Kind() const noexcept; + + // NOLINTNEXTLINE(google-default-arguments) + [[nodiscard]] TSIndexSignature *Clone(ArenaAllocator *allocator, AstNode *parent = nullptr) override; void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; + void Dump(ir::AstDumper *dumper) const override; + void Compile([[maybe_unused]] compiler::PandaGen *pg) const override; checker::Type *Check([[maybe_unused]] checker::TSChecker *checker) override; checker::Type *Check([[maybe_unused]] checker::ETSChecker *checker) override; diff --git a/ets2panda/ir/base/tsMethodSignature.cpp b/ets2panda/ir/base/tsMethodSignature.cpp index c6f203b24b9..4e09c0dd5b2 100644 --- a/ets2panda/ir/base/tsMethodSignature.cpp +++ b/ets2panda/ir/base/tsMethodSignature.cpp @@ -1,5 +1,5 @@ /** - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021 - 2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -17,7 +17,6 @@ #include "varbinder/scope.h" #include "ir/astDump.h" -#include "ir/typeNode.h" #include "ir/ts/tsTypeParameter.h" #include "ir/ts/tsTypeParameterDeclaration.h" diff --git a/ets2panda/ir/base/tsMethodSignature.h b/ets2panda/ir/base/tsMethodSignature.h index ffa4dca3870..849f463aa4e 100644 --- a/ets2panda/ir/base/tsMethodSignature.h +++ b/ets2panda/ir/base/tsMethodSignature.h @@ -1,5 +1,5 @@ /** - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021 - 2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -16,13 +16,19 @@ #ifndef ES2PANDA_PARSER_INCLUDE_AST_TS_METHOD_SIGNATURE_H #define ES2PANDA_PARSER_INCLUDE_AST_TS_METHOD_SIGNATURE_H -#include "ir/statement.h" +#include "ir/typeNode.h" namespace panda::es2panda::ir { class TSTypeParameterDeclaration; class TSMethodSignature : public AstNode { public: + TSMethodSignature() = delete; + ~TSMethodSignature() override = default; + + NO_COPY_SEMANTIC(TSMethodSignature); + NO_MOVE_SEMANTIC(TSMethodSignature); + explicit TSMethodSignature(varbinder::Scope *scope, Expression *key, TSTypeParameterDeclaration *type_params, ArenaVector &¶ms, TypeNode *return_type_annotation, bool computed, bool optional) @@ -47,44 +53,46 @@ public: return scope_; } - const Expression *Key() const + [[nodiscard]] const Expression *Key() const noexcept { return key_; } - Expression *Key() + [[nodiscard]] Expression *Key() noexcept { return key_; } - const TSTypeParameterDeclaration *TypeParams() const + [[nodiscard]] const TSTypeParameterDeclaration *TypeParams() const noexcept { return type_params_; } - const ArenaVector &Params() const + [[nodiscard]] const ArenaVector &Params() const noexcept { return params_; } - const TypeNode *ReturnTypeAnnotation() const + [[nodiscard]] const TypeNode *ReturnTypeAnnotation() const noexcept { return return_type_annotation_; } - bool Computed() const + [[nodiscard]] bool Computed() const noexcept { return computed_; } - bool Optional() const + [[nodiscard]] bool Optional() const noexcept { return optional_; } void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; + void Dump(ir::AstDumper *dumper) const override; + void Compile([[maybe_unused]] compiler::PandaGen *pg) const override; checker::Type *Check([[maybe_unused]] checker::TSChecker *checker) override; checker::Type *Check([[maybe_unused]] checker::ETSChecker *checker) override; diff --git a/ets2panda/ir/base/tsPropertySignature.cpp b/ets2panda/ir/base/tsPropertySignature.cpp index 3cfddf1f7fa..8c5312bdc4a 100644 --- a/ets2panda/ir/base/tsPropertySignature.cpp +++ b/ets2panda/ir/base/tsPropertySignature.cpp @@ -1,5 +1,5 @@ /** - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021 - 2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -74,4 +74,25 @@ checker::Type *TSPropertySignature::Check([[maybe_unused]] checker::ETSChecker * { return nullptr; } + +// NOLINTNEXTLINE(google-default-arguments) +TSPropertySignature *TSPropertySignature::Clone(ArenaAllocator *const allocator, AstNode *const parent) +{ + auto *const key = key_ != nullptr ? key_->Clone(allocator)->AsExpression() : nullptr; + auto *const type_annotation = TypeAnnotation()->Clone(allocator); + + if (auto *const clone = allocator->New(key, type_annotation, computed_, optional_, readonly_); + clone != nullptr) { + if (parent != nullptr) { + clone->SetParent(parent); + } + if (key != nullptr) { + key->SetParent(clone); + } + type_annotation->SetParent(clone); + return clone; + } + + throw Error(ErrorType::GENERIC, "", CLONE_ALLOCATION_ERROR); +} } // namespace panda::es2panda::ir diff --git a/ets2panda/ir/base/tsPropertySignature.h b/ets2panda/ir/base/tsPropertySignature.h index b39ebfdd0dc..c9ba0b1fb51 100644 --- a/ets2panda/ir/base/tsPropertySignature.h +++ b/ets2panda/ir/base/tsPropertySignature.h @@ -23,6 +23,12 @@ class TypeNode; class TSPropertySignature : public AnnotatedAstNode { public: + TSPropertySignature() = delete; + ~TSPropertySignature() override = default; + + NO_COPY_SEMANTIC(TSPropertySignature); + NO_MOVE_SEMANTIC(TSPropertySignature); + explicit TSPropertySignature(Expression *key, TypeNode *type_annotation, bool computed, bool optional, bool readonly) : AnnotatedAstNode(AstNodeType::TS_PROPERTY_SIGNATURE, type_annotation), @@ -33,34 +39,39 @@ public: { } - const Expression *Key() const + [[nodiscard]] const Expression *Key() const noexcept { return key_; } - Expression *Key() + [[nodiscard]] Expression *Key() noexcept { return key_; } - bool Computed() const + [[nodiscard]] bool Computed() const noexcept { return computed_; } - bool Optional() const + [[nodiscard]] bool Optional() const noexcept { return optional_; } - bool Readonly() const + [[nodiscard]] bool Readonly() const noexcept { return readonly_; } + // NOLINTNEXTLINE(google-default-arguments) + [[nodiscard]] TSPropertySignature *Clone(ArenaAllocator *allocator, AstNode *parent = nullptr) override; + void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; + void Dump(ir::AstDumper *dumper) const override; + void Compile([[maybe_unused]] compiler::PandaGen *pg) const override; checker::Type *Check([[maybe_unused]] checker::TSChecker *checker) override; checker::Type *Check([[maybe_unused]] checker::ETSChecker *checker) override; diff --git a/ets2panda/ir/base/tsSignatureDeclaration.cpp b/ets2panda/ir/base/tsSignatureDeclaration.cpp index 734c4613f9c..a36cf714a6a 100644 --- a/ets2panda/ir/base/tsSignatureDeclaration.cpp +++ b/ets2panda/ir/base/tsSignatureDeclaration.cpp @@ -1,5 +1,5 @@ /** - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021 - 2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at diff --git a/ets2panda/ir/base/tsSignatureDeclaration.h b/ets2panda/ir/base/tsSignatureDeclaration.h index bd89af7fa3b..20028f3aa84 100644 --- a/ets2panda/ir/base/tsSignatureDeclaration.h +++ b/ets2panda/ir/base/tsSignatureDeclaration.h @@ -1,5 +1,5 @@ /** - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021 - 2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -16,7 +16,7 @@ #ifndef ES2PANDA_PARSER_INCLUDE_AST_TS_SIGNATURE_DECLARATION_H #define ES2PANDA_PARSER_INCLUDE_AST_TS_SIGNATURE_DECLARATION_H -#include "ir/statement.h" +#include "ir/astNode.h" namespace panda::es2panda::ir { class TSTypeParameterDeclaration; @@ -25,9 +25,15 @@ class TSSignatureDeclaration : public TypedAstNode { public: enum class TSSignatureDeclarationKind { CALL_SIGNATURE, CONSTRUCT_SIGNATURE }; - explicit TSSignatureDeclaration(varbinder::Scope *scope, TSSignatureDeclarationKind kind, - TSTypeParameterDeclaration *type_params, ArenaVector &¶ms, - TypeNode *return_type_annotation) + TSSignatureDeclaration() = delete; + ~TSSignatureDeclaration() override = default; + + NO_COPY_SEMANTIC(TSSignatureDeclaration); + NO_MOVE_SEMANTIC(TSSignatureDeclaration); + + explicit TSSignatureDeclaration(varbinder::Scope *const scope, TSSignatureDeclarationKind const kind, + TSTypeParameterDeclaration *const type_params, ArenaVector &¶ms, + TypeNode *const return_type_annotation) : TypedAstNode(AstNodeType::TS_SIGNATURE_DECLARATION), scope_(scope), kind_(kind), @@ -47,29 +53,31 @@ public: return scope_; } - const TSTypeParameterDeclaration *TypeParams() const + [[nodiscard]] const TSTypeParameterDeclaration *TypeParams() const noexcept { return type_params_; } - const ArenaVector &Params() const + [[nodiscard]] const ArenaVector &Params() const noexcept { return params_; } - const TypeNode *ReturnTypeAnnotation() const + [[nodiscard]] const TypeNode *ReturnTypeAnnotation() const noexcept { return return_type_annotation_; } - TSSignatureDeclarationKind Kind() const + [[nodiscard]] TSSignatureDeclarationKind Kind() const noexcept { return kind_; } void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; + void Dump(ir::AstDumper *dumper) const override; + void Compile([[maybe_unused]] compiler::PandaGen *pg) const override; checker::Type *Check([[maybe_unused]] checker::TSChecker *checker) override; checker::Type *Check([[maybe_unused]] checker::ETSChecker *checker) override; diff --git a/ets2panda/ir/ets/etsClassLiteral.cpp b/ets2panda/ir/ets/etsClassLiteral.cpp index 79c11f531ec..9d461309a1e 100644 --- a/ets2panda/ir/ets/etsClassLiteral.cpp +++ b/ets2panda/ir/ets/etsClassLiteral.cpp @@ -77,9 +77,9 @@ checker::Type *ETSClassLiteral::Check([[maybe_unused]] checker::ETSChecker *chec } // NOLINTNEXTLINE(google-default-arguments) -Expression *ETSClassLiteral::Clone(ArenaAllocator *const allocator, AstNode *const parent) +ETSClassLiteral *ETSClassLiteral::Clone(ArenaAllocator *const allocator, AstNode *const parent) { - auto *const expr = expr_ != nullptr ? expr_->Clone(allocator)->AsTypeNode() : nullptr; + auto *const expr = expr_ != nullptr ? expr_->Clone(allocator) : nullptr; if (auto *const clone = allocator->New(expr); clone != nullptr) { if (expr != nullptr) { diff --git a/ets2panda/ir/ets/etsClassLiteral.h b/ets2panda/ir/ets/etsClassLiteral.h index 64dadfd65bb..44fbf9c8ba0 100644 --- a/ets2panda/ir/ets/etsClassLiteral.h +++ b/ets2panda/ir/ets/etsClassLiteral.h @@ -31,7 +31,7 @@ public: explicit ETSClassLiteral(ir::TypeNode *const expr) : Expression(AstNodeType::ETS_CLASS_LITERAL), expr_(expr) {} // NOLINTNEXTLINE(google-default-arguments) - [[nodiscard]] Expression *Clone(ArenaAllocator *allocator, AstNode *parent = nullptr) override; + [[nodiscard]] ETSClassLiteral *Clone(ArenaAllocator *allocator, AstNode *parent = nullptr) override; void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; diff --git a/ets2panda/ir/ets/etsLaunchExpression.cpp b/ets2panda/ir/ets/etsLaunchExpression.cpp index f5d880a47f8..280a1f05e25 100644 --- a/ets2panda/ir/ets/etsLaunchExpression.cpp +++ b/ets2panda/ir/ets/etsLaunchExpression.cpp @@ -117,9 +117,9 @@ bool ETSLaunchExpression::IsStaticCall() const } // NOLINTNEXTLINE(google-default-arguments) -Expression *ETSLaunchExpression::Clone(ArenaAllocator *const allocator, AstNode *const parent) +ETSLaunchExpression *ETSLaunchExpression::Clone(ArenaAllocator *const allocator, AstNode *const parent) { - auto *const expr = expr_ != nullptr ? expr_->Clone(allocator)->AsCallExpression() : nullptr; + auto *const expr = expr_ != nullptr ? expr_->Clone(allocator) : nullptr; if (auto *const clone = allocator->New(expr); clone != nullptr) { if (expr != nullptr) { diff --git a/ets2panda/ir/ets/etsLaunchExpression.h b/ets2panda/ir/ets/etsLaunchExpression.h index db99d9e6a58..2863db99480 100644 --- a/ets2panda/ir/ets/etsLaunchExpression.h +++ b/ets2panda/ir/ets/etsLaunchExpression.h @@ -33,7 +33,7 @@ public: explicit ETSLaunchExpression(CallExpression *expr); // NOLINTNEXTLINE(google-default-arguments) - [[nodiscard]] Expression *Clone(ArenaAllocator *allocator, AstNode *parent = nullptr) override; + [[nodiscard]] ETSLaunchExpression *Clone(ArenaAllocator *allocator, AstNode *parent = nullptr) override; void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; diff --git a/ets2panda/ir/ets/etsNewArrayInstanceExpression.cpp b/ets2panda/ir/ets/etsNewArrayInstanceExpression.cpp index bf3d63aafe6..b56d07341e7 100644 --- a/ets2panda/ir/ets/etsNewArrayInstanceExpression.cpp +++ b/ets2panda/ir/ets/etsNewArrayInstanceExpression.cpp @@ -71,10 +71,11 @@ checker::Type *ETSNewArrayInstanceExpression::Check([[maybe_unused]] checker::ET } // NOLINTNEXTLINE(google-default-arguments) -Expression *ETSNewArrayInstanceExpression::Clone(ArenaAllocator *const allocator, AstNode *const parent) +ETSNewArrayInstanceExpression *ETSNewArrayInstanceExpression::Clone(ArenaAllocator *const allocator, + AstNode *const parent) { - auto *const type_ref = type_reference_ != nullptr ? type_reference_->Clone(allocator)->AsTypeNode() : nullptr; - auto *const dimension = dimension_ != nullptr ? dimension_->Clone(allocator) : nullptr; + auto *const type_ref = type_reference_ != nullptr ? type_reference_->Clone(allocator) : nullptr; + auto *const dimension = dimension_ != nullptr ? dimension_->Clone(allocator)->AsExpression() : nullptr; if (auto *const clone = allocator->New(type_ref, dimension); clone != nullptr) { if (type_ref != nullptr) { diff --git a/ets2panda/ir/ets/etsNewArrayInstanceExpression.h b/ets2panda/ir/ets/etsNewArrayInstanceExpression.h index 553bb751203..9cafcc56e83 100644 --- a/ets2panda/ir/ets/etsNewArrayInstanceExpression.h +++ b/ets2panda/ir/ets/etsNewArrayInstanceExpression.h @@ -36,7 +36,7 @@ public: } // NOLINTNEXTLINE(google-default-arguments) - [[nodiscard]] Expression *Clone(ArenaAllocator *allocator, AstNode *parent = nullptr) override; + [[nodiscard]] ETSNewArrayInstanceExpression *Clone(ArenaAllocator *allocator, AstNode *parent = nullptr) override; void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; diff --git a/ets2panda/ir/ets/etsNewClassInstanceExpression.cpp b/ets2panda/ir/ets/etsNewClassInstanceExpression.cpp index 0e55a8ac981..e0a0ce1b2db 100644 --- a/ets2panda/ir/ets/etsNewClassInstanceExpression.cpp +++ b/ets2panda/ir/ets/etsNewClassInstanceExpression.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 - 2023 Huawei Device Co., Ltd. + * Copyright (c) 2021-2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -184,4 +184,30 @@ checker::Type *ETSNewClassInstanceExpression::Check([[maybe_unused]] checker::ET return TsType(); } + +ETSNewClassInstanceExpression::ETSNewClassInstanceExpression(ETSNewClassInstanceExpression const &other, + ArenaAllocator *const allocator) + : Expression(static_cast(other)), arguments_(allocator->Adapter()), signature_(other.signature_) +{ + type_reference_ = other.type_reference_->Clone(allocator, this)->AsExpression(); + class_def_ = other.class_def_->Clone(allocator, this)->AsClassDefinition(); + + for (auto *const argument : other.arguments_) { + arguments_.emplace_back(argument->Clone(allocator, this)->AsExpression()); + } +} + +// NOLINTNEXTLINE(google-default-arguments) +ETSNewClassInstanceExpression *ETSNewClassInstanceExpression::Clone(ArenaAllocator *const allocator, + AstNode *const parent) +{ + if (auto *const clone = allocator->New(*this, allocator); clone != nullptr) { + if (parent != nullptr) { + clone->SetParent(parent); + } + return clone; + } + + throw Error(ErrorType::GENERIC, "", CLONE_ALLOCATION_ERROR); +} } // namespace panda::es2panda::ir diff --git a/ets2panda/ir/ets/etsNewClassInstanceExpression.h b/ets2panda/ir/ets/etsNewClassInstanceExpression.h index ffed6e65cdd..25cd7607ff9 100644 --- a/ets2panda/ir/ets/etsNewClassInstanceExpression.h +++ b/ets2panda/ir/ets/etsNewClassInstanceExpression.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 - 2023 Huawei Device Co., Ltd. + * Copyright (c) 2021-2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -29,8 +29,15 @@ class ClassDefinition; class ETSNewClassInstanceExpression : public Expression { public: - explicit ETSNewClassInstanceExpression(ir::Expression *type_reference, ArenaVector &&arguments, - ir::ClassDefinition *class_definition) + ETSNewClassInstanceExpression() = delete; + ~ETSNewClassInstanceExpression() override = default; + + NO_COPY_SEMANTIC(ETSNewClassInstanceExpression); + NO_MOVE_SEMANTIC(ETSNewClassInstanceExpression); + + explicit ETSNewClassInstanceExpression(ir::Expression *const type_reference, + ArenaVector &&arguments, + ir::ClassDefinition *const class_definition) : Expression(AstNodeType::ETS_NEW_CLASS_INSTANCE_EXPRESSION), type_reference_(type_reference), arguments_(std::move(arguments)), @@ -38,22 +45,24 @@ public: { } - ir::ClassDefinition *ClassDefinition() + explicit ETSNewClassInstanceExpression(ETSNewClassInstanceExpression const &other, ArenaAllocator *allocator); + + [[nodiscard]] ir::ClassDefinition *ClassDefinition() noexcept { return class_def_; } - const ir::ClassDefinition *ClassDefinition() const + [[nodiscard]] const ir::ClassDefinition *ClassDefinition() const noexcept { return class_def_; } - ir::Expression *GetTypeRef() const + [[nodiscard]] ir::Expression *GetTypeRef() const noexcept { return type_reference_; } - ArenaVector GetArguments() const + [[nodiscard]] ArenaVector GetArguments() const noexcept { return arguments_; } @@ -67,9 +76,14 @@ public: ir::Expression *name, checker::Signature *signature, const ArenaVector &arguments); + // NOLINTNEXTLINE(google-default-arguments) + [[nodiscard]] ETSNewClassInstanceExpression *Clone(ArenaAllocator *allocator, AstNode *parent = nullptr) override; + void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; + void Dump(ir::AstDumper *dumper) const override; + void Compile([[maybe_unused]] compiler::PandaGen *pg) const override; void Compile([[maybe_unused]] compiler::ETSGen *etsg) const override; checker::Type *Check([[maybe_unused]] checker::TSChecker *checker) override; diff --git a/ets2panda/ir/ets/etsNewMultiDimArrayInstanceExpression.cpp b/ets2panda/ir/ets/etsNewMultiDimArrayInstanceExpression.cpp index 3671c96b28d..73a9fafa1f1 100644 --- a/ets2panda/ir/ets/etsNewMultiDimArrayInstanceExpression.cpp +++ b/ets2panda/ir/ets/etsNewMultiDimArrayInstanceExpression.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 - 2023 Huawei Device Co., Ltd. + * Copyright (c) 2021-2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -71,4 +71,31 @@ checker::Type *ETSNewMultiDimArrayInstanceExpression::Check([[maybe_unused]] che signature_ = checker->CreateBuiltinArraySignature(element_type->AsETSArrayType(), dimensions_.size()); return TsType(); } + +ETSNewMultiDimArrayInstanceExpression::ETSNewMultiDimArrayInstanceExpression( + ETSNewMultiDimArrayInstanceExpression const &other, ArenaAllocator *const allocator) + : Expression(static_cast(other)), + dimensions_(allocator->Adapter()), + signature_(other.signature_) +{ + type_reference_ = other.type_reference_->Clone(allocator, this); + + for (auto *const dimension : other.dimensions_) { + dimensions_.emplace_back(dimension->Clone(allocator, this)->AsExpression()); + } +} + +// NOLINTNEXTLINE(google-default-arguments) +ETSNewMultiDimArrayInstanceExpression *ETSNewMultiDimArrayInstanceExpression::Clone(ArenaAllocator *const allocator, + AstNode *const parent) +{ + if (auto *const clone = allocator->New(*this, allocator); clone != nullptr) { + if (parent != nullptr) { + clone->SetParent(parent); + } + return clone; + } + + throw Error(ErrorType::GENERIC, "", CLONE_ALLOCATION_ERROR); +} } // namespace panda::es2panda::ir diff --git a/ets2panda/ir/ets/etsNewMultiDimArrayInstanceExpression.h b/ets2panda/ir/ets/etsNewMultiDimArrayInstanceExpression.h index 1cf38efa643..2ee2a3d4d4c 100644 --- a/ets2panda/ir/ets/etsNewMultiDimArrayInstanceExpression.h +++ b/ets2panda/ir/ets/etsNewMultiDimArrayInstanceExpression.h @@ -1,5 +1,5 @@ /** - * Copyright (c) 2021-2022 Huawei Device Co., Ltd. + * Copyright (c) 2021-2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -26,7 +26,13 @@ namespace panda::es2panda::ir { class ETSNewMultiDimArrayInstanceExpression : public Expression { public: - explicit ETSNewMultiDimArrayInstanceExpression(ir::TypeNode *type_reference, + ETSNewMultiDimArrayInstanceExpression() = delete; + ~ETSNewMultiDimArrayInstanceExpression() override = default; + + NO_COPY_SEMANTIC(ETSNewMultiDimArrayInstanceExpression); + NO_MOVE_SEMANTIC(ETSNewMultiDimArrayInstanceExpression); + + explicit ETSNewMultiDimArrayInstanceExpression(ir::TypeNode *const type_reference, ArenaVector &&dimensions) : Expression(AstNodeType::ETS_NEW_MULTI_DIM_ARRAY_INSTANCE_EXPRESSION), type_reference_(type_reference), @@ -34,19 +40,28 @@ public: { } - checker::Signature *Signature() + explicit ETSNewMultiDimArrayInstanceExpression(ETSNewMultiDimArrayInstanceExpression const &other, + ArenaAllocator *allocator); + + [[nodiscard]] checker::Signature *Signature() noexcept { return signature_; } - const checker::Signature *Signature() const + [[nodiscard]] const checker::Signature *Signature() const noexcept { return signature_; } + // NOLINTNEXTLINE(google-default-arguments) + [[nodiscard]] ETSNewMultiDimArrayInstanceExpression *Clone(ArenaAllocator *allocator, + AstNode *parent = nullptr) override; + void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; + void Dump(ir::AstDumper *dumper) const override; + void Compile([[maybe_unused]] compiler::PandaGen *pg) const override; void Compile([[maybe_unused]] compiler::ETSGen *etsg) const override; checker::Type *Check([[maybe_unused]] checker::TSChecker *checker) override; diff --git a/ets2panda/ir/ets/etsPackageDeclaration.cpp b/ets2panda/ir/ets/etsPackageDeclaration.cpp index b31ed8e71ba..d78bc700074 100644 --- a/ets2panda/ir/ets/etsPackageDeclaration.cpp +++ b/ets2panda/ir/ets/etsPackageDeclaration.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 - 2023 Huawei Device Co., Ltd. + * Copyright (c) 2021-2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -50,4 +50,21 @@ checker::Type *ETSPackageDeclaration::Check([[maybe_unused]] checker::ETSChecker { return nullptr; } + +// NOLINTNEXTLINE(google-default-arguments) +ETSPackageDeclaration *ETSPackageDeclaration::Clone(ArenaAllocator *const allocator, AstNode *const parent) +{ + auto const name = name_ != nullptr ? name_->Clone(allocator, this)->AsExpression() : nullptr; + if (auto *const clone = allocator->New(name); clone != nullptr) { + if (name != nullptr) { + name->SetParent(clone); + } + if (parent != nullptr) { + clone->SetParent(parent); + } + return clone; + } + + throw Error(ErrorType::GENERIC, "", CLONE_ALLOCATION_ERROR); +} } // namespace panda::es2panda::ir diff --git a/ets2panda/ir/ets/etsPackageDeclaration.h b/ets2panda/ir/ets/etsPackageDeclaration.h index 8ca20442d3d..50249d8c681 100644 --- a/ets2panda/ir/ets/etsPackageDeclaration.h +++ b/ets2panda/ir/ets/etsPackageDeclaration.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 - 2023 Huawei Device Co., Ltd. + * Copyright (c) 2021-2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -22,13 +22,25 @@ namespace panda::es2panda::ir { class ETSPackageDeclaration : public Statement { public: - explicit ETSPackageDeclaration(ir::Expression *name) : Statement(AstNodeType::ETS_PACKAGE_DECLARATION), name_(name) + ETSPackageDeclaration() = delete; + ~ETSPackageDeclaration() override = default; + + NO_COPY_SEMANTIC(ETSPackageDeclaration); + NO_MOVE_SEMANTIC(ETSPackageDeclaration); + + explicit ETSPackageDeclaration(ir::Expression *const name) + : Statement(AstNodeType::ETS_PACKAGE_DECLARATION), name_(name) { } + // NOLINTNEXTLINE(google-default-arguments) + [[nodiscard]] ETSPackageDeclaration *Clone(ArenaAllocator *allocator, AstNode *parent = nullptr) override; + void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; + void Dump(ir::AstDumper *dumper) const override; + void Compile([[maybe_unused]] compiler::PandaGen *pg) const override; void Compile([[maybe_unused]] compiler::ETSGen *etsg) const override; checker::Type *Check([[maybe_unused]] checker::TSChecker *checker) override; diff --git a/ets2panda/ir/ets/etsParameterExpression.cpp b/ets2panda/ir/ets/etsParameterExpression.cpp index bb0172b423f..3d002681077 100644 --- a/ets2panda/ir/ets/etsParameterExpression.cpp +++ b/ets2panda/ir/ets/etsParameterExpression.cpp @@ -163,11 +163,11 @@ checker::Type *ETSParameterExpression::Check(checker::ETSChecker *const checker) } // NOLINTNEXTLINE(google-default-arguments) -Expression *ETSParameterExpression::Clone(ArenaAllocator *const allocator, AstNode *const parent) +ETSParameterExpression *ETSParameterExpression::Clone(ArenaAllocator *const allocator, AstNode *const parent) { auto *const ident_or_spread = spread_ != nullptr ? spread_->Clone(allocator)->AsAnnotatedExpression() : ident_->Clone(allocator)->AsAnnotatedExpression(); - auto *const initializer = initializer_ != nullptr ? initializer_->Clone(allocator) : nullptr; + auto *const initializer = initializer_ != nullptr ? initializer_->Clone(allocator)->AsExpression() : nullptr; if (auto *const clone = allocator->New(ident_or_spread, initializer); clone != nullptr) { ident_or_spread->SetParent(clone); diff --git a/ets2panda/ir/ets/etsParameterExpression.h b/ets2panda/ir/ets/etsParameterExpression.h index 4ea4ed1e32a..7786d77fc30 100644 --- a/ets2panda/ir/ets/etsParameterExpression.h +++ b/ets2panda/ir/ets/etsParameterExpression.h @@ -65,7 +65,7 @@ public: } // NOLINTNEXTLINE(google-default-arguments) - [[nodiscard]] Expression *Clone(ArenaAllocator *allocator, AstNode *parent = nullptr) override; + [[nodiscard]] ETSParameterExpression *Clone(ArenaAllocator *allocator, AstNode *parent = nullptr) override; void Iterate(const NodeTraverser &cb) const override; void TransformChildren(const NodeTransformer &cb) override; diff --git a/ets2panda/ir/ets/etsStructDeclaration.cpp b/ets2panda/ir/ets/etsStructDeclaration.cpp index 8bd7ed765a9..14f7101ad09 100644 --- a/ets2panda/ir/ets/etsStructDeclaration.cpp +++ b/ets2panda/ir/ets/etsStructDeclaration.cpp @@ -68,4 +68,27 @@ checker::Type *ETSStructDeclaration::Check(checker::ETSChecker *checker) { return checker->GetAnalyzer()->Check(this); } + +// NOLINTNEXTLINE(google-default-arguments) +ETSStructDeclaration *ETSStructDeclaration::Clone(ArenaAllocator *const allocator, AstNode *const parent) +{ + auto *const def = def_ != nullptr ? def_->Clone(allocator, this)->AsClassDefinition() : nullptr; + + if (auto *const clone = allocator->New(def, allocator); clone != nullptr) { + for (auto *const decorator : decorators_) { + clone->AddDecorator(decorator->Clone(allocator, clone)); + } + + if (def != nullptr) { + def->SetParent(clone); + } + + if (parent != nullptr) { + clone->SetParent(parent); + } + return clone; + } + + throw Error(ErrorType::GENERIC, "", CLONE_ALLOCATION_ERROR); +} } // namespace panda::es2panda::ir diff --git a/ets2panda/ir/ets/etsStructDeclaration.h b/ets2panda/ir/ets/etsStructDeclaration.h index 90a448f581d..2368350de75 100644 --- a/ets2panda/ir/ets/etsStructDeclaration.h +++ b/ets2panda/ir/ets/etsStructDeclaration.h @@ -21,22 +21,28 @@ namespace panda::es2panda::ir { class ETSStructDeclaration : public Statement { public: - explicit ETSStructDeclaration(ClassDefinition *def, ArenaAllocator *allocator) + ETSStructDeclaration() = delete; + ~ETSStructDeclaration() override = default; + + NO_COPY_SEMANTIC(ETSStructDeclaration); + NO_MOVE_SEMANTIC(ETSStructDeclaration); + + explicit ETSStructDeclaration(ClassDefinition *const def, ArenaAllocator *const allocator) : Statement(AstNodeType::STRUCT_DECLARATION), def_(def), decorators_(allocator->Adapter()) { } - ClassDefinition *Definition() + [[nodiscard]] ClassDefinition *Definition() noexcept { return def_; } - const ClassDefinition *Definition() const + [[nodiscard]] const ClassDefinition *Definition() const noexcept { return def_; } - const ArenaVector &Decorators() const + [[nodiscard]] const ArenaVector &Decorators() const noexcept { return decorators_; } @@ -46,14 +52,24 @@ public: decorators_ = std::move(decorators); } + void AddDecorator(Decorator *const decorator) + { + decorators_.emplace_back(decorator); + } + bool CanHaveDecorator([[maybe_unused]] bool in_ts) const override { return true; } + // NOLINTNEXTLINE(google-default-arguments) + [[nodiscard]] ETSStructDeclaration *Clone(ArenaAllocator *allocator, AstNode *parent = nullptr) override; + void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; + void Dump(ir::AstDumper *dumper) const override; + void Compile(compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; diff --git a/ets2panda/ir/expression.cpp b/ets2panda/ir/expression.cpp index 6d117c469e9..6415d5e1726 100644 --- a/ets2panda/ir/expression.cpp +++ b/ets2panda/ir/expression.cpp @@ -21,7 +21,7 @@ namespace panda::es2panda::ir { void AnnotatedExpression::CloneTypeAnnotation(ArenaAllocator *const allocator) { if (auto *annotation = const_cast(TypeAnnotation()); annotation != nullptr) { - SetTsTypeAnnotation(annotation->Clone(allocator, this)->AsTypeNode()); + SetTsTypeAnnotation(annotation->Clone(allocator, this)); } } diff --git a/ets2panda/ir/expression.h b/ets2panda/ir/expression.h index 833fa158902..5b5479c3d62 100644 --- a/ets2panda/ir/expression.h +++ b/ets2panda/ir/expression.h @@ -97,14 +97,6 @@ public: return reinterpret_cast(this); } - // NOLINTNEXTLINE(google-default-arguments) - [[nodiscard]] virtual Expression *Clone([[maybe_unused]] ArenaAllocator *const allocator, - [[maybe_unused]] AstNode *const parent = nullptr) - { - UNREACHABLE(); - return nullptr; - } - protected: explicit Expression(AstNodeType const type) : TypedAstNode(type) {} explicit Expression(AstNodeType const type, ModifierFlags const flags) : TypedAstNode(type, flags) {} diff --git a/ets2panda/ir/expressions/arrayExpression.cpp b/ets2panda/ir/expressions/arrayExpression.cpp index c3c36bef08d..6b46553a1db 100644 --- a/ets2panda/ir/expressions/arrayExpression.cpp +++ b/ets2panda/ir/expressions/arrayExpression.cpp @@ -46,16 +46,16 @@ ArrayExpression::ArrayExpression([[maybe_unused]] Tag const tag, ArrayExpression optional_ = other.optional_; for (auto *element : other.elements_) { - elements_.emplace_back(element->Clone(allocator, this)); + elements_.emplace_back(element->Clone(allocator, this)->AsExpression()); } for (auto *decorator : other.decorators_) { - decorators_.emplace_back(decorator->Clone(allocator, this)->AsDecorator()); + decorators_.emplace_back(decorator->Clone(allocator, this)); } } // NOLINTNEXTLINE(google-default-arguments) -Expression *ArrayExpression::Clone(ArenaAllocator *const allocator, AstNode *const parent) +ArrayExpression *ArrayExpression::Clone(ArenaAllocator *const allocator, AstNode *const parent) { if (auto *const clone = allocator->New(Tag {}, *this, allocator); clone != nullptr) { if (parent != nullptr) { diff --git a/ets2panda/ir/expressions/arrayExpression.h b/ets2panda/ir/expressions/arrayExpression.h index 5848b480b54..bbe3c830fd3 100644 --- a/ets2panda/ir/expressions/arrayExpression.h +++ b/ets2panda/ir/expressions/arrayExpression.h @@ -99,7 +99,7 @@ public: } // NOLINTNEXTLINE(google-default-arguments) - [[nodiscard]] Expression *Clone(ArenaAllocator *allocator, AstNode *parent = nullptr) override; + [[nodiscard]] ArrayExpression *Clone(ArenaAllocator *allocator, AstNode *parent = nullptr) override; [[nodiscard]] bool ConvertibleToArrayPattern(); [[nodiscard]] ValidationInfo ValidateExpression(); diff --git a/ets2panda/ir/expressions/arrowFunctionExpression.cpp b/ets2panda/ir/expressions/arrowFunctionExpression.cpp index 0cb1b3b65fd..c07a110c37a 100644 --- a/ets2panda/ir/expressions/arrowFunctionExpression.cpp +++ b/ets2panda/ir/expressions/arrowFunctionExpression.cpp @@ -1,5 +1,5 @@ /** - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021 - 2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -61,4 +61,33 @@ checker::Type *ArrowFunctionExpression::Check(checker::ETSChecker *checker) { return checker->GetAnalyzer()->Check(this); } + +ArrowFunctionExpression::ArrowFunctionExpression(ArrowFunctionExpression const &other, ArenaAllocator *const allocator) + : Expression(static_cast(other)), + captured_vars_(allocator->Adapter()), + propagate_this_(other.propagate_this_) +{ + func_ = other.func_->Clone(allocator, this)->AsScriptFunction(); + + for (auto *const variable : other.captured_vars_) { + captured_vars_.emplace_back(variable); + } + + if (other.resolved_lambda_ != nullptr) { + resolved_lambda_ = other.resolved_lambda_->Clone(allocator, this)->AsClassDefinition(); + } +} + +// NOLINTNEXTLINE(google-default-arguments) +ArrowFunctionExpression *ArrowFunctionExpression::Clone(ArenaAllocator *const allocator, AstNode *const parent) +{ + if (auto *const clone = allocator->New(*this, allocator); clone != nullptr) { + if (parent != nullptr) { + clone->SetParent(parent); + } + return clone; + } + + throw Error(ErrorType::GENERIC, "", CLONE_ALLOCATION_ERROR); +} } // namespace panda::es2panda::ir diff --git a/ets2panda/ir/expressions/arrowFunctionExpression.h b/ets2panda/ir/expressions/arrowFunctionExpression.h index 2d78a104125..0cc830afadc 100644 --- a/ets2panda/ir/expressions/arrowFunctionExpression.h +++ b/ets2panda/ir/expressions/arrowFunctionExpression.h @@ -1,5 +1,5 @@ /** - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021 - 2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -27,53 +27,65 @@ class ScriptFunction; class ArrowFunctionExpression : public Expression { public: - explicit ArrowFunctionExpression(ArenaAllocator *allocator, ScriptFunction *func) + ArrowFunctionExpression() = delete; + ~ArrowFunctionExpression() override = default; + + NO_COPY_SEMANTIC(ArrowFunctionExpression); + NO_MOVE_SEMANTIC(ArrowFunctionExpression); + + explicit ArrowFunctionExpression(ArenaAllocator *const allocator, ScriptFunction *const func) : Expression(AstNodeType::ARROW_FUNCTION_EXPRESSION), func_(func), captured_vars_(allocator->Adapter()) { } + + explicit ArrowFunctionExpression(ArrowFunctionExpression const &other, ArenaAllocator *allocator); + // NOTE (csabahurton): friend relationship can be removed once there are getters for private fields friend class compiler::ETSCompiler; - const ScriptFunction *Function() const + [[nodiscard]] const ScriptFunction *Function() const noexcept { return func_; } - ScriptFunction *Function() + [[nodiscard]] ScriptFunction *Function() noexcept { return func_; } - const ClassDefinition *ResolvedLambda() const + [[nodiscard]] const ClassDefinition *ResolvedLambda() const noexcept { return resolved_lambda_; } - ClassDefinition *ResolvedLambda() + [[nodiscard]] ClassDefinition *ResolvedLambda() noexcept { return resolved_lambda_; } - ArenaVector &CapturedVars() + [[nodiscard]] ArenaVector &CapturedVars() noexcept { return captured_vars_; } - const ArenaVector &CapturedVars() const + [[nodiscard]] const ArenaVector &CapturedVars() const noexcept { return captured_vars_; } - void SetResolvedLambda(ClassDefinition *lambda) + void SetResolvedLambda(ClassDefinition *const lambda) noexcept { resolved_lambda_ = lambda; } - void SetPropagateThis() + void SetPropagateThis() noexcept { propagate_this_ = true; } + // NOLINTNEXTLINE(google-default-arguments) + [[nodiscard]] ArrowFunctionExpression *Clone(ArenaAllocator *allocator, AstNode *parent = nullptr) override; + void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; diff --git a/ets2panda/ir/expressions/assignmentExpression.cpp b/ets2panda/ir/expressions/assignmentExpression.cpp index a6ba7d6ee05..4c3d14510fc 100644 --- a/ets2panda/ir/expressions/assignmentExpression.cpp +++ b/ets2panda/ir/expressions/assignmentExpression.cpp @@ -322,10 +322,10 @@ AssignmentExpression::AssignmentExpression([[maybe_unused]] Tag const tag, Assig } // NOLINTNEXTLINE(google-default-arguments) -Expression *AssignmentExpression::Clone(ArenaAllocator *const allocator, AstNode *const parent) +AssignmentExpression *AssignmentExpression::Clone(ArenaAllocator *const allocator, AstNode *const parent) { - auto *const left = left_ != nullptr ? left_->Clone(allocator) : nullptr; - auto *const right = right_ != nullptr ? right_->Clone(allocator) : nullptr; + auto *const left = left_ != nullptr ? left_->Clone(allocator)->AsExpression() : nullptr; + auto *const right = right_ != nullptr ? right_->Clone(allocator)->AsExpression() : nullptr; if (auto *const clone = allocator->New(Tag {}, *this, left, right); clone != nullptr) { if (parent != nullptr) { diff --git a/ets2panda/ir/expressions/assignmentExpression.h b/ets2panda/ir/expressions/assignmentExpression.h index 5f14a1c1cff..bebc40be2a0 100644 --- a/ets2panda/ir/expressions/assignmentExpression.h +++ b/ets2panda/ir/expressions/assignmentExpression.h @@ -108,7 +108,7 @@ public: } // NOLINTNEXTLINE(google-default-arguments) - [[nodiscard]] Expression *Clone(ArenaAllocator *allocator, AstNode *parent = nullptr) override; + [[nodiscard]] AssignmentExpression *Clone(ArenaAllocator *allocator, AstNode *parent = nullptr) override; [[nodiscard]] bool ConvertibleToAssignmentPattern(bool must_be_pattern = true); diff --git a/ets2panda/ir/expressions/awaitExpression.cpp b/ets2panda/ir/expressions/awaitExpression.cpp index 636a7e5bc25..f14a8676b0d 100644 --- a/ets2panda/ir/expressions/awaitExpression.cpp +++ b/ets2panda/ir/expressions/awaitExpression.cpp @@ -94,9 +94,9 @@ checker::Type *AwaitExpression::Check(checker::ETSChecker *checker) } // NOLINTNEXTLINE(google-default-arguments) -Expression *AwaitExpression::Clone(ArenaAllocator *const allocator, AstNode *const parent) +AwaitExpression *AwaitExpression::Clone(ArenaAllocator *const allocator, AstNode *const parent) { - auto *const argument = argument_ != nullptr ? argument_->Clone(allocator) : nullptr; + auto *const argument = argument_ != nullptr ? argument_->Clone(allocator)->AsExpression() : nullptr; if (auto *const clone = allocator->New(argument); clone != nullptr) { if (argument != nullptr) { diff --git a/ets2panda/ir/expressions/awaitExpression.h b/ets2panda/ir/expressions/awaitExpression.h index 17240df98c3..e90a8081323 100644 --- a/ets2panda/ir/expressions/awaitExpression.h +++ b/ets2panda/ir/expressions/awaitExpression.h @@ -35,7 +35,7 @@ public: } // NOLINTNEXTLINE(google-default-arguments) - [[nodiscard]] Expression *Clone(ArenaAllocator *allocator, AstNode *parent = nullptr) override; + [[nodiscard]] AwaitExpression *Clone(ArenaAllocator *allocator, AstNode *parent = nullptr) override; void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; diff --git a/ets2panda/ir/expressions/binaryExpression.cpp b/ets2panda/ir/expressions/binaryExpression.cpp index 440afa17f2d..641dfd9d0e5 100644 --- a/ets2panda/ir/expressions/binaryExpression.cpp +++ b/ets2panda/ir/expressions/binaryExpression.cpp @@ -296,10 +296,10 @@ checker::Type *BinaryExpression::Check(checker::ETSChecker *checker) } // NOLINTNEXTLINE(google-default-arguments) -Expression *BinaryExpression::Clone(ArenaAllocator *const allocator, AstNode *const parent) +BinaryExpression *BinaryExpression::Clone(ArenaAllocator *const allocator, AstNode *const parent) { - auto *const left = left_ != nullptr ? left_->Clone(allocator) : nullptr; - auto *const right = right_ != nullptr ? right_->Clone(allocator) : nullptr; + auto *const left = left_ != nullptr ? left_->Clone(allocator)->AsExpression() : nullptr; + auto *const right = right_ != nullptr ? right_->Clone(allocator)->AsExpression() : nullptr; if (auto *const clone = allocator->New(left, right, operator_); clone != nullptr) { if (operation_type_ != nullptr) { diff --git a/ets2panda/ir/expressions/binaryExpression.h b/ets2panda/ir/expressions/binaryExpression.h index c2a1a47bbbc..b2eb6bf8232 100644 --- a/ets2panda/ir/expressions/binaryExpression.h +++ b/ets2panda/ir/expressions/binaryExpression.h @@ -113,7 +113,7 @@ public: } // NOLINTNEXTLINE(google-default-arguments) - [[nodiscard]] Expression *Clone(ArenaAllocator *allocator, AstNode *parent = nullptr) override; + [[nodiscard]] BinaryExpression *Clone(ArenaAllocator *allocator, AstNode *parent = nullptr) override; void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; diff --git a/ets2panda/ir/expressions/callExpression.cpp b/ets2panda/ir/expressions/callExpression.cpp index b74f16794da..ef2e51225fb 100644 --- a/ets2panda/ir/expressions/callExpression.cpp +++ b/ets2panda/ir/expressions/callExpression.cpp @@ -1,5 +1,5 @@ /** - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021 - 2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -533,4 +533,36 @@ checker::Type *CallExpression::InitAnonymousLambdaCallee(checker::ETSChecker *ch checker->Relation()->IsAssignableTo(callee_type, func_iface); return func_iface; } + +CallExpression::CallExpression(CallExpression const &other, ArenaAllocator *const allocator) + : MaybeOptionalExpression(static_cast(other)), + arguments_(allocator->Adapter()), + signature_(other.signature_), + trailing_comma_(other.trailing_comma_), + is_trailing_block_in_new_line_(other.is_trailing_block_in_new_line_) +{ + callee_ = other.callee_->Clone(allocator, this)->AsExpression(); + type_params_ = other.type_params_->Clone(allocator, this); + + for (auto *const argument : other.arguments_) { + arguments_.emplace_back(argument->Clone(allocator, this)->AsExpression()); + } + + if (other.trailing_block_ != nullptr) { + trailing_block_ = other.trailing_block_->Clone(allocator, this)->AsBlockStatement(); + } +} + +// NOLINTNEXTLINE(google-default-arguments) +CallExpression *CallExpression::Clone(ArenaAllocator *const allocator, AstNode *const parent) +{ + if (auto *const clone = allocator->New(*this, allocator); clone != nullptr) { + if (parent != nullptr) { + clone->SetParent(parent); + } + return clone; + } + + throw Error(ErrorType::GENERIC, "", CLONE_ALLOCATION_ERROR); +} } // namespace panda::es2panda::ir diff --git a/ets2panda/ir/expressions/callExpression.h b/ets2panda/ir/expressions/callExpression.h index d68ca9daca7..a89e9b45da4 100644 --- a/ets2panda/ir/expressions/callExpression.h +++ b/ets2panda/ir/expressions/callExpression.h @@ -1,5 +1,5 @@ /** - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021 - 2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -29,8 +29,15 @@ class TSTypeParameterInstantiation; class CallExpression : public MaybeOptionalExpression { public: - explicit CallExpression(Expression *callee, ArenaVector &&arguments, - TSTypeParameterInstantiation *type_params, bool optional, bool trailing_comma = false) + CallExpression() = delete; + ~CallExpression() override = default; + + NO_COPY_SEMANTIC(CallExpression); + NO_MOVE_SEMANTIC(CallExpression); + + explicit CallExpression(Expression *const callee, ArenaVector &&arguments, + TSTypeParameterInstantiation *const type_params, bool const optional, + bool const trailing_comma = false) : MaybeOptionalExpression(AstNodeType::CALL_EXPRESSION, optional), callee_(callee), arguments_(std::move(arguments)), @@ -39,93 +46,101 @@ public: { } - const Expression *Callee() const + explicit CallExpression(CallExpression const &other, ArenaAllocator *allocator); + + [[nodiscard]] const Expression *Callee() const noexcept { return callee_; } - Expression *Callee() + [[nodiscard]] Expression *Callee() noexcept { return callee_; } - void SetCallee(Expression *callee) + void SetCallee(Expression *callee) noexcept { callee_ = callee; } - const TSTypeParameterInstantiation *TypeParams() const + [[nodiscard]] const TSTypeParameterInstantiation *TypeParams() const noexcept { return type_params_; } - TSTypeParameterInstantiation *TypeParams() + [[nodiscard]] TSTypeParameterInstantiation *TypeParams() noexcept { return type_params_; } - const ArenaVector &Arguments() const + [[nodiscard]] const ArenaVector &Arguments() const noexcept { return arguments_; } - ArenaVector &Arguments() + [[nodiscard]] ArenaVector &Arguments() noexcept { return arguments_; } - bool HasTrailingComma() const + [[nodiscard]] bool HasTrailingComma() const noexcept { return trailing_comma_; } - checker::Signature *Signature() + [[nodiscard]] checker::Signature *Signature() noexcept { return signature_; } - checker::Signature *Signature() const + [[nodiscard]] checker::Signature *Signature() const noexcept { return signature_; } - void SetSignature(checker::Signature *signature) + void SetSignature(checker::Signature *const signature) noexcept { signature_ = signature; } - void SetTypeParams(TSTypeParameterInstantiation *type_params) + void SetTypeParams(TSTypeParameterInstantiation *const type_params) noexcept { type_params_ = type_params; } - void SetTrailingBlock(ir::BlockStatement *block) + void SetTrailingBlock(ir::BlockStatement *const block) noexcept { trailing_block_ = block; } - ir::BlockStatement *TrailingBlock() const + [[nodiscard]] ir::BlockStatement *TrailingBlock() const noexcept { return trailing_block_; } - void SetIsTrailingBlockInNewLine(bool is_new_line) + void SetIsTrailingBlockInNewLine(bool const is_new_line) noexcept { is_trailing_block_in_new_line_ = is_new_line; } - bool IsTrailingBlockInNewLine() const + [[nodiscard]] bool IsTrailingBlockInNewLine() const noexcept { return is_trailing_block_in_new_line_; } + // NOLINTNEXTLINE(google-default-arguments) + [[nodiscard]] CallExpression *Clone(ArenaAllocator *allocator, AstNode *parent = nullptr) override; + void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; + void Dump(ir::AstDumper *dumper) const override; + void Compile([[maybe_unused]] compiler::PandaGen *pg) const override; void Compile([[maybe_unused]] compiler::ETSGen *etsg) const override; checker::Type *Check([[maybe_unused]] checker::TSChecker *checker) override; checker::Type *Check([[maybe_unused]] checker::ETSChecker *checker) override; + checker::Signature *ResolveCallExtensionFunction(checker::ETSFunctionType *function_type, checker::ETSChecker *checker); checker::Signature *ResolveCallForETSExtensionFuncHelperType(checker::ETSExtensionFuncHelperType *type, diff --git a/ets2panda/ir/expressions/chainExpression.cpp b/ets2panda/ir/expressions/chainExpression.cpp index ce71eac8388..40de7dc7215 100644 --- a/ets2panda/ir/expressions/chainExpression.cpp +++ b/ets2panda/ir/expressions/chainExpression.cpp @@ -68,9 +68,9 @@ checker::Type *ChainExpression::Check([[maybe_unused]] checker::ETSChecker *chec } // NOLINTNEXTLINE(google-default-arguments) -Expression *ChainExpression::Clone(ArenaAllocator *const allocator, AstNode *const parent) +ChainExpression *ChainExpression::Clone(ArenaAllocator *const allocator, AstNode *const parent) { - auto *const expression = expression_ != nullptr ? expression_->Clone(allocator) : nullptr; + auto *const expression = expression_ != nullptr ? expression_->Clone(allocator)->AsExpression() : nullptr; if (auto *const clone = allocator->New(expression); clone != nullptr) { if (expression != nullptr) { diff --git a/ets2panda/ir/expressions/chainExpression.h b/ets2panda/ir/expressions/chainExpression.h index 252ece9500b..03eafe1ac9a 100644 --- a/ets2panda/ir/expressions/chainExpression.h +++ b/ets2panda/ir/expressions/chainExpression.h @@ -39,7 +39,7 @@ public: } // NOLINTNEXTLINE(google-default-arguments) - [[nodiscard]] Expression *Clone(ArenaAllocator *allocator, AstNode *parent = nullptr) override; + [[nodiscard]] ChainExpression *Clone(ArenaAllocator *allocator, AstNode *parent = nullptr) override; void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; diff --git a/ets2panda/ir/expressions/classExpression.cpp b/ets2panda/ir/expressions/classExpression.cpp index ecefc3a8faa..e8f268a9717 100644 --- a/ets2panda/ir/expressions/classExpression.cpp +++ b/ets2panda/ir/expressions/classExpression.cpp @@ -1,5 +1,5 @@ /** - * Copyright (c) 2021-2022 Huawei Device Co., Ltd. + * Copyright (c) 2021-2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -48,4 +48,22 @@ checker::Type *ClassExpression::Check([[maybe_unused]] checker::ETSChecker *chec { return nullptr; } + +// NOLINTNEXTLINE(google-default-arguments) +ClassExpression *ClassExpression::Clone(ArenaAllocator *const allocator, AstNode *const parent) +{ + auto *const def = def_ != nullptr ? def_->Clone(allocator)->AsClassDefinition() : nullptr; + + if (auto *const clone = allocator->New(def); clone != nullptr) { + if (parent != nullptr) { + clone->SetParent(parent); + } + if (def != nullptr) { + def->SetParent(clone); + } + return clone; + } + + throw Error(ErrorType::GENERIC, "", CLONE_ALLOCATION_ERROR); +} } // namespace panda::es2panda::ir diff --git a/ets2panda/ir/expressions/classExpression.h b/ets2panda/ir/expressions/classExpression.h index 1ab8e4a09ce..df7b06021f8 100644 --- a/ets2panda/ir/expressions/classExpression.h +++ b/ets2panda/ir/expressions/classExpression.h @@ -1,5 +1,5 @@ /** - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021 - 2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -23,17 +23,28 @@ class ClassDefinition; class ClassExpression : public Expression { public: - explicit ClassExpression(ClassDefinition *def) : Expression(AstNodeType::CLASS_EXPRESSION), def_(def) {} + ClassExpression() = delete; + ~ClassExpression() override = default; - const ClassDefinition *Definition() const + NO_COPY_SEMANTIC(ClassExpression); + NO_MOVE_SEMANTIC(ClassExpression); + + explicit ClassExpression(ClassDefinition *const def) : Expression(AstNodeType::CLASS_EXPRESSION), def_(def) {} + + [[nodiscard]] const ClassDefinition *Definition() const noexcept { return def_; } + // NOLINTNEXTLINE(google-default-arguments) + [[nodiscard]] ClassExpression *Clone(ArenaAllocator *allocator, AstNode *parent = nullptr) override; + void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; + void Dump(ir::AstDumper *dumper) const override; void Compile([[maybe_unused]] compiler::PandaGen *pg) const override; + checker::Type *Check([[maybe_unused]] checker::TSChecker *checker) override; checker::Type *Check([[maybe_unused]] checker::ETSChecker *checker) override; diff --git a/ets2panda/ir/expressions/conditionalExpression.cpp b/ets2panda/ir/expressions/conditionalExpression.cpp index b60d48679a5..2d8a66e5a5d 100644 --- a/ets2panda/ir/expressions/conditionalExpression.cpp +++ b/ets2panda/ir/expressions/conditionalExpression.cpp @@ -150,11 +150,11 @@ checker::Type *ConditionalExpression::Check(checker::ETSChecker *checker) } // NOLINTNEXTLINE(google-default-arguments) -Expression *ConditionalExpression::Clone(ArenaAllocator *const allocator, AstNode *const parent) +ConditionalExpression *ConditionalExpression::Clone(ArenaAllocator *const allocator, AstNode *const parent) { - auto *const test = test_ != nullptr ? test_->Clone(allocator) : nullptr; - auto *const consequent = consequent_ != nullptr ? consequent_->Clone(allocator) : nullptr; - auto *const alternate = alternate_ != nullptr ? alternate_->Clone(allocator) : nullptr; + auto *const test = test_ != nullptr ? test_->Clone(allocator)->AsExpression() : nullptr; + auto *const consequent = consequent_ != nullptr ? consequent_->Clone(allocator)->AsExpression() : nullptr; + auto *const alternate = alternate_ != nullptr ? alternate_->Clone(allocator)->AsExpression() : nullptr; if (auto *const clone = allocator->New(test, consequent, alternate); clone != nullptr) { if (test != nullptr) { diff --git a/ets2panda/ir/expressions/conditionalExpression.h b/ets2panda/ir/expressions/conditionalExpression.h index 8e0e7a10d8f..6ee5774d7be 100644 --- a/ets2panda/ir/expressions/conditionalExpression.h +++ b/ets2panda/ir/expressions/conditionalExpression.h @@ -58,7 +58,7 @@ public: } // NOLINTNEXTLINE(google-default-arguments) - [[nodiscard]] Expression *Clone(ArenaAllocator *allocator, AstNode *parent = nullptr) override; + [[nodiscard]] ConditionalExpression *Clone(ArenaAllocator *allocator, AstNode *parent = nullptr) override; void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; diff --git a/ets2panda/ir/expressions/functionExpression.cpp b/ets2panda/ir/expressions/functionExpression.cpp index 7f9c194231c..4620b5a322a 100644 --- a/ets2panda/ir/expressions/functionExpression.cpp +++ b/ets2panda/ir/expressions/functionExpression.cpp @@ -1,5 +1,5 @@ /** - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021 - 2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -82,4 +82,20 @@ checker::Type *FunctionExpression::Check([[maybe_unused]] checker::ETSChecker *c { return nullptr; } + +// NOLINTNEXTLINE(google-default-arguments) +FunctionExpression *FunctionExpression::Clone(ArenaAllocator *const allocator, AstNode *const parent) +{ + auto *const func = func_->Clone(allocator)->AsScriptFunction(); + + if (auto *const clone = allocator->New(func); clone != nullptr) { + func->SetParent(clone); + if (parent != nullptr) { + clone->SetParent(parent); + } + return clone; + } + + throw Error(ErrorType::GENERIC, "", CLONE_ALLOCATION_ERROR); +} } // namespace panda::es2panda::ir diff --git a/ets2panda/ir/expressions/functionExpression.h b/ets2panda/ir/expressions/functionExpression.h index 4b219185787..a5565fcf499 100644 --- a/ets2panda/ir/expressions/functionExpression.h +++ b/ets2panda/ir/expressions/functionExpression.h @@ -1,5 +1,5 @@ /** - * Copyright (c) 2021-2022 Huawei Device Co., Ltd. + * Copyright (c) 2021-2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -23,23 +23,37 @@ class ScriptFunction; class FunctionExpression : public Expression { public: - explicit FunctionExpression(ScriptFunction *func) : Expression(AstNodeType::FUNCTION_EXPRESSION), func_(func) {} + FunctionExpression() = delete; + ~FunctionExpression() override = default; - const ScriptFunction *Function() const + NO_COPY_SEMANTIC(FunctionExpression); + NO_MOVE_SEMANTIC(FunctionExpression); + + explicit FunctionExpression(ScriptFunction *const func) : Expression(AstNodeType::FUNCTION_EXPRESSION), func_(func) + { + } + + [[nodiscard]] const ScriptFunction *Function() const noexcept { return func_; } - ScriptFunction *Function() + [[nodiscard]] ScriptFunction *Function() noexcept { return func_; } + // NOLINTNEXTLINE(google-default-arguments) + [[nodiscard]] FunctionExpression *Clone(ArenaAllocator *allocator, AstNode *parent = nullptr) override; + void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; + void Dump(ir::AstDumper *dumper) const override; + void Compile([[maybe_unused]] compiler::PandaGen *pg) const override; void Compile(compiler::ETSGen *etsg) const override; + checker::Type *Check([[maybe_unused]] checker::TSChecker *checker) override; checker::Type *Check([[maybe_unused]] checker::ETSChecker *checker) override; diff --git a/ets2panda/ir/expressions/identifier.cpp b/ets2panda/ir/expressions/identifier.cpp index e1f2b3cd2a5..1ff485d8949 100644 --- a/ets2panda/ir/expressions/identifier.cpp +++ b/ets2panda/ir/expressions/identifier.cpp @@ -35,12 +35,12 @@ Identifier::Identifier([[maybe_unused]] Tag const tag, Identifier const &other, variable_ = other.variable_; for (auto *decorator : other.decorators_) { - decorators_.emplace_back(decorator->Clone(allocator, this)->AsDecorator()); + decorators_.emplace_back(decorator->Clone(allocator, this)); } } // NOLINTNEXTLINE(google-default-arguments) -Expression *Identifier::Clone(ArenaAllocator *const allocator, AstNode *const parent) +Identifier *Identifier::Clone(ArenaAllocator *const allocator, AstNode *const parent) { if (auto *const clone = allocator->New(Tag {}, *this, allocator); clone != nullptr) { if (parent != nullptr) { diff --git a/ets2panda/ir/expressions/identifier.h b/ets2panda/ir/expressions/identifier.h index a6ee3dd78ef..5cfa53c9c22 100644 --- a/ets2panda/ir/expressions/identifier.h +++ b/ets2panda/ir/expressions/identifier.h @@ -186,7 +186,7 @@ public: } // NOLINTNEXTLINE(google-default-arguments) - [[nodiscard]] Expression *Clone(ArenaAllocator *allocator, AstNode *parent = nullptr) override; + [[nodiscard]] Identifier *Clone(ArenaAllocator *allocator, AstNode *parent = nullptr) override; [[nodiscard]] ValidationInfo ValidateExpression(); diff --git a/ets2panda/ir/expressions/importExpression.cpp b/ets2panda/ir/expressions/importExpression.cpp index e812123c13e..d9b39dcd391 100644 --- a/ets2panda/ir/expressions/importExpression.cpp +++ b/ets2panda/ir/expressions/importExpression.cpp @@ -50,9 +50,9 @@ checker::Type *ImportExpression::Check([[maybe_unused]] checker::ETSChecker *che } // NOLINTNEXTLINE(google-default-arguments) -Expression *ImportExpression::Clone(ArenaAllocator *const allocator, AstNode *const parent) +ImportExpression *ImportExpression::Clone(ArenaAllocator *const allocator, AstNode *const parent) { - auto *const source = source_ != nullptr ? source_->Clone(allocator) : nullptr; + auto *const source = source_ != nullptr ? source_->Clone(allocator)->AsExpression() : nullptr; if (auto *const clone = allocator->New(source); clone != nullptr) { if (source != nullptr) { diff --git a/ets2panda/ir/expressions/importExpression.h b/ets2panda/ir/expressions/importExpression.h index d275dd9a9a4..884e3dbc294 100644 --- a/ets2panda/ir/expressions/importExpression.h +++ b/ets2panda/ir/expressions/importExpression.h @@ -30,7 +30,7 @@ public: explicit ImportExpression(Expression *source) : Expression(AstNodeType::IMPORT_EXPRESSION), source_(source) {} // NOLINTNEXTLINE(google-default-arguments) - [[nodiscard]] Expression *Clone(ArenaAllocator *allocator, AstNode *parent = nullptr) override; + [[nodiscard]] ImportExpression *Clone(ArenaAllocator *allocator, AstNode *parent = nullptr) override; void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; diff --git a/ets2panda/ir/expressions/literals/bigIntLiteral.cpp b/ets2panda/ir/expressions/literals/bigIntLiteral.cpp index b59d2f83c9a..5887c20cd73 100644 --- a/ets2panda/ir/expressions/literals/bigIntLiteral.cpp +++ b/ets2panda/ir/expressions/literals/bigIntLiteral.cpp @@ -51,7 +51,7 @@ checker::Type *BigIntLiteral::Check([[maybe_unused]] checker::ETSChecker *checke } // NOLINTNEXTLINE(google-default-arguments) -Expression *BigIntLiteral::Clone(ArenaAllocator *const allocator, AstNode *const parent) +BigIntLiteral *BigIntLiteral::Clone(ArenaAllocator *const allocator, AstNode *const parent) { if (auto *const clone = allocator->New(src_); clone != nullptr) { if (parent != nullptr) { diff --git a/ets2panda/ir/expressions/literals/bigIntLiteral.h b/ets2panda/ir/expressions/literals/bigIntLiteral.h index a017c4912ac..32d4c40d303 100644 --- a/ets2panda/ir/expressions/literals/bigIntLiteral.h +++ b/ets2panda/ir/expressions/literals/bigIntLiteral.h @@ -36,7 +36,7 @@ public: } // NOLINTNEXTLINE(google-default-arguments) - [[nodiscard]] Expression *Clone(ArenaAllocator *allocator, AstNode *parent = nullptr) override; + [[nodiscard]] BigIntLiteral *Clone(ArenaAllocator *allocator, AstNode *parent = nullptr) override; void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; diff --git a/ets2panda/ir/expressions/literals/booleanLiteral.cpp b/ets2panda/ir/expressions/literals/booleanLiteral.cpp index 81fc1cb1471..d73eed1c05f 100644 --- a/ets2panda/ir/expressions/literals/booleanLiteral.cpp +++ b/ets2panda/ir/expressions/literals/booleanLiteral.cpp @@ -54,7 +54,7 @@ checker::Type *BooleanLiteral::Check([[maybe_unused]] checker::ETSChecker *check } // NOLINTNEXTLINE(google-default-arguments) -Expression *BooleanLiteral::Clone(ArenaAllocator *const allocator, AstNode *const parent) +BooleanLiteral *BooleanLiteral::Clone(ArenaAllocator *const allocator, AstNode *const parent) { if (auto *const clone = allocator->New(boolean_); clone != nullptr) { if (parent != nullptr) { diff --git a/ets2panda/ir/expressions/literals/booleanLiteral.h b/ets2panda/ir/expressions/literals/booleanLiteral.h index 8643f5b573c..e2a69e617e7 100644 --- a/ets2panda/ir/expressions/literals/booleanLiteral.h +++ b/ets2panda/ir/expressions/literals/booleanLiteral.h @@ -35,7 +35,7 @@ public: } // NOLINTNEXTLINE(google-default-arguments) - [[nodiscard]] Expression *Clone(ArenaAllocator *allocator, AstNode *parent = nullptr) override; + [[nodiscard]] BooleanLiteral *Clone(ArenaAllocator *allocator, AstNode *parent = nullptr) override; void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; diff --git a/ets2panda/ir/expressions/literals/charLiteral.cpp b/ets2panda/ir/expressions/literals/charLiteral.cpp index 6596de25771..4f333ee45fd 100644 --- a/ets2panda/ir/expressions/literals/charLiteral.cpp +++ b/ets2panda/ir/expressions/literals/charLiteral.cpp @@ -52,7 +52,7 @@ checker::Type *CharLiteral::Check([[maybe_unused]] checker::ETSChecker *checker) } // NOLINTNEXTLINE(google-default-arguments) -Expression *CharLiteral::Clone(ArenaAllocator *const allocator, AstNode *const parent) +CharLiteral *CharLiteral::Clone(ArenaAllocator *const allocator, AstNode *const parent) { if (auto *const clone = allocator->New(char_); clone != nullptr) { if (parent != nullptr) { diff --git a/ets2panda/ir/expressions/literals/charLiteral.h b/ets2panda/ir/expressions/literals/charLiteral.h index 048d73884fd..f1bf1e8f105 100644 --- a/ets2panda/ir/expressions/literals/charLiteral.h +++ b/ets2panda/ir/expressions/literals/charLiteral.h @@ -41,7 +41,7 @@ public: } // NOLINTNEXTLINE(google-default-arguments) - [[nodiscard]] Expression *Clone(ArenaAllocator *allocator, AstNode *parent = nullptr) override; + [[nodiscard]] CharLiteral *Clone(ArenaAllocator *allocator, AstNode *parent = nullptr) override; void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; diff --git a/ets2panda/ir/expressions/literals/nullLiteral.cpp b/ets2panda/ir/expressions/literals/nullLiteral.cpp index 10868ce8325..8074c1471e6 100644 --- a/ets2panda/ir/expressions/literals/nullLiteral.cpp +++ b/ets2panda/ir/expressions/literals/nullLiteral.cpp @@ -54,7 +54,7 @@ checker::Type *NullLiteral::Check([[maybe_unused]] checker::ETSChecker *checker) } // NOLINTNEXTLINE(google-default-arguments) -Expression *NullLiteral::Clone(ArenaAllocator *const allocator, AstNode *const parent) +NullLiteral *NullLiteral::Clone(ArenaAllocator *const allocator, AstNode *const parent) { if (auto *const clone = allocator->New(); clone != nullptr) { if (parent != nullptr) { diff --git a/ets2panda/ir/expressions/literals/nullLiteral.h b/ets2panda/ir/expressions/literals/nullLiteral.h index 1fa60a74f0f..a89a07b58b8 100644 --- a/ets2panda/ir/expressions/literals/nullLiteral.h +++ b/ets2panda/ir/expressions/literals/nullLiteral.h @@ -29,7 +29,7 @@ public: explicit NullLiteral() : Literal(AstNodeType::NULL_LITERAL) {} // NOLINTNEXTLINE(google-default-arguments) - [[nodiscard]] Expression *Clone(ArenaAllocator *allocator, AstNode *parent = nullptr) override; + [[nodiscard]] NullLiteral *Clone(ArenaAllocator *allocator, AstNode *parent = nullptr) override; void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; diff --git a/ets2panda/ir/expressions/literals/numberLiteral.cpp b/ets2panda/ir/expressions/literals/numberLiteral.cpp index d411de92385..c2e40f43201 100644 --- a/ets2panda/ir/expressions/literals/numberLiteral.cpp +++ b/ets2panda/ir/expressions/literals/numberLiteral.cpp @@ -111,7 +111,7 @@ checker::Type *NumberLiteral::Check([[maybe_unused]] checker::ETSChecker *checke } // NOLINTNEXTLINE(google-default-arguments) -Expression *NumberLiteral::Clone(ArenaAllocator *const allocator, AstNode *const parent) +NumberLiteral *NumberLiteral::Clone(ArenaAllocator *const allocator, AstNode *const parent) { if (auto *const clone = allocator->New(number_); clone != nullptr) { if (parent != nullptr) { diff --git a/ets2panda/ir/expressions/literals/numberLiteral.h b/ets2panda/ir/expressions/literals/numberLiteral.h index e7391b8e403..da244592eed 100644 --- a/ets2panda/ir/expressions/literals/numberLiteral.h +++ b/ets2panda/ir/expressions/literals/numberLiteral.h @@ -50,7 +50,7 @@ public: [[nodiscard]] bool HasFloatingPoint() const; // NOLINTNEXTLINE(google-default-arguments) - [[nodiscard]] Expression *Clone(ArenaAllocator *allocator, AstNode *parent = nullptr) override; + [[nodiscard]] NumberLiteral *Clone(ArenaAllocator *allocator, AstNode *parent = nullptr) override; void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; diff --git a/ets2panda/ir/expressions/literals/regExpLiteral.cpp b/ets2panda/ir/expressions/literals/regExpLiteral.cpp index d278513f9fe..6409bc62eda 100644 --- a/ets2panda/ir/expressions/literals/regExpLiteral.cpp +++ b/ets2panda/ir/expressions/literals/regExpLiteral.cpp @@ -46,7 +46,7 @@ checker::Type *RegExpLiteral::Check([[maybe_unused]] checker::ETSChecker *checke } // NOLINTNEXTLINE(google-default-arguments) -Expression *RegExpLiteral::Clone(ArenaAllocator *const allocator, AstNode *const parent) +RegExpLiteral *RegExpLiteral::Clone(ArenaAllocator *const allocator, AstNode *const parent) { if (auto *const clone = allocator->New(pattern_, flags_, flags_str_); clone != nullptr) { if (parent != nullptr) { diff --git a/ets2panda/ir/expressions/literals/regExpLiteral.h b/ets2panda/ir/expressions/literals/regExpLiteral.h index 8ee1c453e53..8eff4811abb 100644 --- a/ets2panda/ir/expressions/literals/regExpLiteral.h +++ b/ets2panda/ir/expressions/literals/regExpLiteral.h @@ -45,7 +45,7 @@ public: } // NOLINTNEXTLINE(google-default-arguments) - [[nodiscard]] Expression *Clone(ArenaAllocator *allocator, AstNode *parent = nullptr) override; + [[nodiscard]] RegExpLiteral *Clone(ArenaAllocator *allocator, AstNode *parent = nullptr) override; void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; diff --git a/ets2panda/ir/expressions/literals/stringLiteral.cpp b/ets2panda/ir/expressions/literals/stringLiteral.cpp index 2c45cb252ec..f07aa1b456a 100644 --- a/ets2panda/ir/expressions/literals/stringLiteral.cpp +++ b/ets2panda/ir/expressions/literals/stringLiteral.cpp @@ -63,7 +63,7 @@ checker::Type *StringLiteral::Check([[maybe_unused]] checker::ETSChecker *checke } // NOLINTNEXTLINE(google-default-arguments) -Expression *StringLiteral::Clone(ArenaAllocator *const allocator, AstNode *const parent) +StringLiteral *StringLiteral::Clone(ArenaAllocator *const allocator, AstNode *const parent) { if (auto *const clone = allocator->New(str_); clone != nullptr) { if (parent != nullptr) { diff --git a/ets2panda/ir/expressions/literals/stringLiteral.h b/ets2panda/ir/expressions/literals/stringLiteral.h index da7f22fdadb..9efadad2191 100644 --- a/ets2panda/ir/expressions/literals/stringLiteral.h +++ b/ets2panda/ir/expressions/literals/stringLiteral.h @@ -42,7 +42,7 @@ public: } // NOLINTNEXTLINE(google-default-arguments) - [[nodiscard]] Expression *Clone(ArenaAllocator *allocator, AstNode *parent = nullptr) override; + [[nodiscard]] StringLiteral *Clone(ArenaAllocator *allocator, AstNode *parent = nullptr) override; void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; diff --git a/ets2panda/ir/expressions/memberExpression.cpp b/ets2panda/ir/expressions/memberExpression.cpp index 44936411def..253697f0753 100644 --- a/ets2panda/ir/expressions/memberExpression.cpp +++ b/ets2panda/ir/expressions/memberExpression.cpp @@ -469,10 +469,10 @@ checker::Type *MemberExpression::Check(checker::ETSChecker *checker) } // NOLINTNEXTLINE(google-default-arguments) -Expression *MemberExpression::Clone(ArenaAllocator *const allocator, AstNode *const parent) +MemberExpression *MemberExpression::Clone(ArenaAllocator *const allocator, AstNode *const parent) { - auto *const object = object_ != nullptr ? object_->Clone(allocator) : nullptr; - auto *const property = property_ != nullptr ? property_->Clone(allocator) : nullptr; + auto *const object = object_ != nullptr ? object_->Clone(allocator)->AsExpression() : nullptr; + auto *const property = property_ != nullptr ? property_->Clone(allocator)->AsExpression() : nullptr; if (auto *const clone = allocator->New(Tag {}, object, property); clone != nullptr) { if (parent != nullptr) { diff --git a/ets2panda/ir/expressions/memberExpression.h b/ets2panda/ir/expressions/memberExpression.h index 47c172e8430..a47b12398f7 100644 --- a/ets2panda/ir/expressions/memberExpression.h +++ b/ets2panda/ir/expressions/memberExpression.h @@ -142,7 +142,7 @@ public: [[nodiscard]] bool IsPrivateReference() const noexcept; // NOLINTNEXTLINE(google-default-arguments) - [[nodiscard]] Expression *Clone(ArenaAllocator *allocator, AstNode *parent = nullptr) override; + [[nodiscard]] MemberExpression *Clone(ArenaAllocator *allocator, AstNode *parent = nullptr) override; void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; diff --git a/ets2panda/ir/expressions/newExpression.cpp b/ets2panda/ir/expressions/newExpression.cpp index 565ba8caa42..ada99894644 100644 --- a/ets2panda/ir/expressions/newExpression.cpp +++ b/ets2panda/ir/expressions/newExpression.cpp @@ -28,16 +28,16 @@ NewExpression::NewExpression([[maybe_unused]] Tag const tag, NewExpression const : Expression(static_cast(other)), arguments_(allocator->Adapter()) { if (other.callee_ != nullptr) { - callee_ = other.callee_->Clone(allocator, this); + callee_ = other.callee_->Clone(allocator, this)->AsExpression(); } for (auto *argument : other.arguments_) { - arguments_.emplace_back(argument->Clone(allocator, this)); + arguments_.emplace_back(argument->Clone(allocator, this)->AsExpression()); } } // NOLINTNEXTLINE(google-default-arguments) -Expression *NewExpression::Clone(ArenaAllocator *const allocator, AstNode *const parent) +NewExpression *NewExpression::Clone(ArenaAllocator *const allocator, AstNode *const parent) { if (auto *const clone = allocator->New(Tag {}, *this, allocator); clone != nullptr) { if (parent != nullptr) { diff --git a/ets2panda/ir/expressions/newExpression.h b/ets2panda/ir/expressions/newExpression.h index 2cefd3ce6d7..6fc447c3815 100644 --- a/ets2panda/ir/expressions/newExpression.h +++ b/ets2panda/ir/expressions/newExpression.h @@ -48,7 +48,7 @@ public: } // NOLINTNEXTLINE(google-default-arguments) - [[nodiscard]] Expression *Clone(ArenaAllocator *allocator, AstNode *parent = nullptr) override; + [[nodiscard]] NewExpression *Clone(ArenaAllocator *allocator, AstNode *parent = nullptr) override; void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; diff --git a/ets2panda/ir/expressions/objectExpression.cpp b/ets2panda/ir/expressions/objectExpression.cpp index f3b4b4ebff0..c5e6218895d 100644 --- a/ets2panda/ir/expressions/objectExpression.cpp +++ b/ets2panda/ir/expressions/objectExpression.cpp @@ -58,16 +58,16 @@ ObjectExpression::ObjectExpression([[maybe_unused]] Tag const tag, ObjectExpress optional_ = other.optional_; for (auto *property : other.properties_) { - properties_.emplace_back(property->Clone(allocator, this)); + properties_.emplace_back(property->Clone(allocator, this)->AsExpression()); } for (auto *decorator : other.decorators_) { - decorators_.emplace_back(decorator->Clone(allocator, this)->AsDecorator()); + decorators_.emplace_back(decorator->Clone(allocator, this)); } } // NOLINTNEXTLINE(google-default-arguments) -Expression *ObjectExpression::Clone(ArenaAllocator *const allocator, AstNode *const parent) +ObjectExpression *ObjectExpression::Clone(ArenaAllocator *const allocator, AstNode *const parent) { if (auto *const clone = allocator->New(Tag {}, *this, allocator); clone != nullptr) { if (parent != nullptr) { diff --git a/ets2panda/ir/expressions/objectExpression.h b/ets2panda/ir/expressions/objectExpression.h index b258313d0b9..70121771fe5 100644 --- a/ets2panda/ir/expressions/objectExpression.h +++ b/ets2panda/ir/expressions/objectExpression.h @@ -83,7 +83,7 @@ public: } // NOLINTNEXTLINE(google-default-arguments) - [[nodiscard]] Expression *Clone(ArenaAllocator *allocator, AstNode *parent = nullptr) override; + [[nodiscard]] ObjectExpression *Clone(ArenaAllocator *allocator, AstNode *parent = nullptr) override; [[nodiscard]] ValidationInfo ValidateExpression(); [[nodiscard]] bool ConvertibleToObjectPattern(); diff --git a/ets2panda/ir/expressions/omittedExpression.cpp b/ets2panda/ir/expressions/omittedExpression.cpp index b24ea45beba..ab2a356ac6b 100644 --- a/ets2panda/ir/expressions/omittedExpression.cpp +++ b/ets2panda/ir/expressions/omittedExpression.cpp @@ -40,7 +40,7 @@ checker::Type *OmittedExpression::Check([[maybe_unused]] checker::ETSChecker *ch } // NOLINTNEXTLINE(google-default-arguments) -Expression *OmittedExpression::Clone(ArenaAllocator *const allocator, AstNode *const parent) +OmittedExpression *OmittedExpression::Clone(ArenaAllocator *const allocator, AstNode *const parent) { if (auto *const clone = allocator->New(); clone != nullptr) { if (parent != nullptr) { diff --git a/ets2panda/ir/expressions/omittedExpression.h b/ets2panda/ir/expressions/omittedExpression.h index ee722bf0c33..20f7f5ead9e 100644 --- a/ets2panda/ir/expressions/omittedExpression.h +++ b/ets2panda/ir/expressions/omittedExpression.h @@ -31,7 +31,7 @@ public: void TransformChildren(const NodeTransformer &cb) override; // NOLINTNEXTLINE(google-default-arguments) - [[nodiscard]] Expression *Clone(ArenaAllocator *allocator, AstNode *parent = nullptr) override; + [[nodiscard]] OmittedExpression *Clone(ArenaAllocator *allocator, AstNode *parent = nullptr) override; void Iterate(const NodeTraverser &cb) const override; void Dump(ir::AstDumper *dumper) const override; diff --git a/ets2panda/ir/expressions/sequenceExpression.cpp b/ets2panda/ir/expressions/sequenceExpression.cpp index cde77a042f7..f855c5826d3 100644 --- a/ets2panda/ir/expressions/sequenceExpression.cpp +++ b/ets2panda/ir/expressions/sequenceExpression.cpp @@ -24,12 +24,12 @@ SequenceExpression::SequenceExpression([[maybe_unused]] Tag const tag, SequenceE : Expression(static_cast(other)), sequence_(allocator->Adapter()) { for (auto *sequence : other.sequence_) { - sequence_.emplace_back(sequence->Clone(allocator, this)); + sequence_.emplace_back(sequence->Clone(allocator, this)->AsExpression()); } } // NOLINTNEXTLINE(google-default-arguments) -Expression *SequenceExpression::Clone(ArenaAllocator *const allocator, AstNode *const parent) +SequenceExpression *SequenceExpression::Clone(ArenaAllocator *const allocator, AstNode *const parent) { if (auto *const clone = allocator->New(Tag {}, *this, allocator); clone != nullptr) { if (parent != nullptr) { diff --git a/ets2panda/ir/expressions/sequenceExpression.h b/ets2panda/ir/expressions/sequenceExpression.h index 29a1fd2aec4..79e9974db8d 100644 --- a/ets2panda/ir/expressions/sequenceExpression.h +++ b/ets2panda/ir/expressions/sequenceExpression.h @@ -48,7 +48,7 @@ public: } // NOLINTNEXTLINE(google-default-arguments) - [[nodiscard]] Expression *Clone(ArenaAllocator *allocator, AstNode *parent = nullptr) override; + [[nodiscard]] SequenceExpression *Clone(ArenaAllocator *allocator, AstNode *parent = nullptr) override; void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; diff --git a/ets2panda/ir/expressions/superExpression.cpp b/ets2panda/ir/expressions/superExpression.cpp index 8858bffbc9e..b7d291a57f3 100644 --- a/ets2panda/ir/expressions/superExpression.cpp +++ b/ets2panda/ir/expressions/superExpression.cpp @@ -65,7 +65,7 @@ checker::Type *SuperExpression::Check([[maybe_unused]] checker::ETSChecker *chec } // NOLINTNEXTLINE(google-default-arguments) -Expression *SuperExpression::Clone(ArenaAllocator *const allocator, AstNode *const parent) +SuperExpression *SuperExpression::Clone(ArenaAllocator *const allocator, AstNode *const parent) { if (auto *const clone = allocator->New(); clone != nullptr) { if (parent != nullptr) { diff --git a/ets2panda/ir/expressions/superExpression.h b/ets2panda/ir/expressions/superExpression.h index 31b28359759..27d45416083 100644 --- a/ets2panda/ir/expressions/superExpression.h +++ b/ets2panda/ir/expressions/superExpression.h @@ -29,7 +29,7 @@ public: explicit SuperExpression() : Expression(AstNodeType::SUPER_EXPRESSION) {} // NOLINTNEXTLINE(google-default-arguments) - [[nodiscard]] Expression *Clone(ArenaAllocator *allocator, AstNode *parent = nullptr) override; + [[nodiscard]] SuperExpression *Clone(ArenaAllocator *allocator, AstNode *parent = nullptr) override; void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; diff --git a/ets2panda/ir/expressions/taggedTemplateExpression.cpp b/ets2panda/ir/expressions/taggedTemplateExpression.cpp index 3960c0d1c3e..b68a413ab31 100644 --- a/ets2panda/ir/expressions/taggedTemplateExpression.cpp +++ b/ets2panda/ir/expressions/taggedTemplateExpression.cpp @@ -83,12 +83,11 @@ checker::Type *TaggedTemplateExpression::Check([[maybe_unused]] checker::ETSChec } // NOLINTNEXTLINE(google-default-arguments) -Expression *TaggedTemplateExpression::Clone(ArenaAllocator *const allocator, AstNode *const parent) +TaggedTemplateExpression *TaggedTemplateExpression::Clone(ArenaAllocator *const allocator, AstNode *const parent) { - auto *const tag = tag_ != nullptr ? tag_->Clone(allocator) : nullptr; - auto *const quasi = quasi_ != nullptr ? quasi_->Clone(allocator)->AsTemplateLiteral() : nullptr; - auto *const type_params = - type_params_ != nullptr ? type_params_->Clone(allocator)->AsTSTypeParameterInstantiation() : nullptr; + auto *const tag = tag_ != nullptr ? tag_->Clone(allocator)->AsExpression() : nullptr; + auto *const quasi = quasi_ != nullptr ? quasi_->Clone(allocator) : nullptr; + auto *const type_params = type_params_ != nullptr ? type_params_->Clone(allocator) : nullptr; if (auto *const clone = allocator->New(tag, quasi, type_params); clone != nullptr) { if (tag != nullptr) { diff --git a/ets2panda/ir/expressions/taggedTemplateExpression.h b/ets2panda/ir/expressions/taggedTemplateExpression.h index 194571cf79c..6aeabf80f7b 100644 --- a/ets2panda/ir/expressions/taggedTemplateExpression.h +++ b/ets2panda/ir/expressions/taggedTemplateExpression.h @@ -52,7 +52,7 @@ public: } // NOLINTNEXTLINE(google-default-arguments) - [[nodiscard]] Expression *Clone(ArenaAllocator *allocator, AstNode *parent = nullptr) override; + [[nodiscard]] TaggedTemplateExpression *Clone(ArenaAllocator *allocator, AstNode *parent = nullptr) override; void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; diff --git a/ets2panda/ir/expressions/templateLiteral.cpp b/ets2panda/ir/expressions/templateLiteral.cpp index 82a52458f67..61c58acd1ee 100644 --- a/ets2panda/ir/expressions/templateLiteral.cpp +++ b/ets2panda/ir/expressions/templateLiteral.cpp @@ -30,16 +30,16 @@ TemplateLiteral::TemplateLiteral([[maybe_unused]] Tag const tag, TemplateLiteral expressions_(allocator->Adapter()) { for (auto *quasy : other.quasis_) { - quasis_.emplace_back(quasy->Clone(allocator, this)->AsTemplateElement()); + quasis_.emplace_back(quasy->Clone(allocator, this)); } for (auto *expression : other.expressions_) { - expressions_.emplace_back(expression->Clone(allocator, this)); + expressions_.emplace_back(expression->Clone(allocator, this)->AsExpression()); } } // NOLINTNEXTLINE(google-default-arguments) -Expression *TemplateLiteral::Clone(ArenaAllocator *const allocator, AstNode *const parent) +TemplateLiteral *TemplateLiteral::Clone(ArenaAllocator *const allocator, AstNode *const parent) { if (auto *const clone = allocator->New(Tag {}, *this, allocator); clone != nullptr) { if (parent != nullptr) { diff --git a/ets2panda/ir/expressions/templateLiteral.h b/ets2panda/ir/expressions/templateLiteral.h index 3ec24e8007c..bc2d31666dc 100644 --- a/ets2panda/ir/expressions/templateLiteral.h +++ b/ets2panda/ir/expressions/templateLiteral.h @@ -49,7 +49,7 @@ public: } // NOLINTNEXTLINE(google-default-arguments) - [[nodiscard]] Expression *Clone(ArenaAllocator *allocator, AstNode *parent = nullptr) override; + [[nodiscard]] TemplateLiteral *Clone(ArenaAllocator *allocator, AstNode *parent = nullptr) override; void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; diff --git a/ets2panda/ir/expressions/thisExpression.cpp b/ets2panda/ir/expressions/thisExpression.cpp index 4bfd2754411..0c91e916486 100644 --- a/ets2panda/ir/expressions/thisExpression.cpp +++ b/ets2panda/ir/expressions/thisExpression.cpp @@ -109,7 +109,7 @@ checker::Type *ThisExpression::Check(checker::ETSChecker *checker) } // NOLINTNEXTLINE(google-default-arguments) -Expression *ThisExpression::Clone(ArenaAllocator *const allocator, AstNode *const parent) +ThisExpression *ThisExpression::Clone(ArenaAllocator *const allocator, AstNode *const parent) { if (auto *const clone = allocator->New(); clone != nullptr) { if (parent != nullptr) { diff --git a/ets2panda/ir/expressions/thisExpression.h b/ets2panda/ir/expressions/thisExpression.h index a98f7f39df0..6ffa359d072 100644 --- a/ets2panda/ir/expressions/thisExpression.h +++ b/ets2panda/ir/expressions/thisExpression.h @@ -29,7 +29,7 @@ public: explicit ThisExpression() : Expression(AstNodeType::THIS_EXPRESSION) {} // NOLINTNEXTLINE(google-default-arguments) - [[nodiscard]] Expression *Clone(ArenaAllocator *allocator, AstNode *parent = nullptr) override; + [[nodiscard]] ThisExpression *Clone(ArenaAllocator *allocator, AstNode *parent = nullptr) override; void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; diff --git a/ets2panda/ir/expressions/unaryExpression.cpp b/ets2panda/ir/expressions/unaryExpression.cpp index 2f6ced1150f..a73cbef0771 100644 --- a/ets2panda/ir/expressions/unaryExpression.cpp +++ b/ets2panda/ir/expressions/unaryExpression.cpp @@ -305,9 +305,9 @@ checker::Type *UnaryExpression::Check(checker::ETSChecker *checker) } // NOLINTNEXTLINE(google-default-arguments) -Expression *UnaryExpression::Clone(ArenaAllocator *const allocator, AstNode *const parent) +UnaryExpression *UnaryExpression::Clone(ArenaAllocator *const allocator, AstNode *const parent) { - auto *const argument = argument_ != nullptr ? argument_->Clone(allocator) : nullptr; + auto *const argument = argument_ != nullptr ? argument_->Clone(allocator)->AsExpression() : nullptr; if (auto *const clone = allocator->New(argument, operator_); clone != nullptr) { if (argument != nullptr) { diff --git a/ets2panda/ir/expressions/unaryExpression.h b/ets2panda/ir/expressions/unaryExpression.h index 1e2fb24c0b3..ade78126852 100644 --- a/ets2panda/ir/expressions/unaryExpression.h +++ b/ets2panda/ir/expressions/unaryExpression.h @@ -54,7 +54,7 @@ public: } // NOLINTNEXTLINE(google-default-arguments) - [[nodiscard]] Expression *Clone(ArenaAllocator *allocator, AstNode *parent = nullptr) override; + [[nodiscard]] UnaryExpression *Clone(ArenaAllocator *allocator, AstNode *parent = nullptr) override; void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; diff --git a/ets2panda/ir/expressions/updateExpression.cpp b/ets2panda/ir/expressions/updateExpression.cpp index e72ca193e8d..ae49e98ae65 100644 --- a/ets2panda/ir/expressions/updateExpression.cpp +++ b/ets2panda/ir/expressions/updateExpression.cpp @@ -149,9 +149,9 @@ checker::Type *UpdateExpression::Check(checker::ETSChecker *checker) } // NOLINTNEXTLINE(google-default-arguments) -Expression *UpdateExpression::Clone(ArenaAllocator *const allocator, AstNode *const parent) +UpdateExpression *UpdateExpression::Clone(ArenaAllocator *const allocator, AstNode *const parent) { - auto *const argument = argument_ != nullptr ? argument_->Clone(allocator) : nullptr; + auto *const argument = argument_ != nullptr ? argument_->Clone(allocator)->AsExpression() : nullptr; if (auto *const clone = allocator->New(argument, operator_, prefix_); clone != nullptr) { if (argument != nullptr) { diff --git a/ets2panda/ir/expressions/updateExpression.h b/ets2panda/ir/expressions/updateExpression.h index 34901b1845b..494fbaf2981 100644 --- a/ets2panda/ir/expressions/updateExpression.h +++ b/ets2panda/ir/expressions/updateExpression.h @@ -54,7 +54,7 @@ public: } // NOLINTNEXTLINE(google-default-arguments) - [[nodiscard]] Expression *Clone(ArenaAllocator *allocator, AstNode *parent = nullptr) override; + [[nodiscard]] UpdateExpression *Clone(ArenaAllocator *allocator, AstNode *parent = nullptr) override; void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; diff --git a/ets2panda/ir/expressions/yieldExpression.cpp b/ets2panda/ir/expressions/yieldExpression.cpp index 4aadc7a5458..465498c428b 100644 --- a/ets2panda/ir/expressions/yieldExpression.cpp +++ b/ets2panda/ir/expressions/yieldExpression.cpp @@ -70,9 +70,9 @@ checker::Type *YieldExpression::Check([[maybe_unused]] checker::ETSChecker *chec } // NOLINTNEXTLINE(google-default-arguments) -Expression *YieldExpression::Clone(ArenaAllocator *const allocator, AstNode *const parent) +YieldExpression *YieldExpression::Clone(ArenaAllocator *const allocator, AstNode *const parent) { - auto *const argument = argument_ != nullptr ? argument_->Clone(allocator) : nullptr; + auto *const argument = argument_ != nullptr ? argument_->Clone(allocator)->AsExpression() : nullptr; if (auto *const clone = allocator->New(argument, delegate_); clone != nullptr) { if (argument != nullptr) { diff --git a/ets2panda/ir/expressions/yieldExpression.h b/ets2panda/ir/expressions/yieldExpression.h index 9e8e85608b7..3c2f5282e2f 100644 --- a/ets2panda/ir/expressions/yieldExpression.h +++ b/ets2panda/ir/expressions/yieldExpression.h @@ -47,7 +47,7 @@ public: } // NOLINTNEXTLINE(google-default-arguments) - [[nodiscard]] Expression *Clone(ArenaAllocator *allocator, AstNode *parent = nullptr) override; + [[nodiscard]] YieldExpression *Clone(ArenaAllocator *allocator, AstNode *parent = nullptr) override; void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; diff --git a/ets2panda/ir/statement.cpp b/ets2panda/ir/statement.cpp index 8eb886a24ea..4bec7797487 100644 --- a/ets2panda/ir/statement.cpp +++ b/ets2panda/ir/statement.cpp @@ -20,7 +20,7 @@ namespace panda::es2panda::ir { void AnnotatedStatement::CloneTypeAnnotation(ArenaAllocator *const allocator) { if (auto *annotation = const_cast(TypeAnnotation()); annotation != nullptr) { - SetTsTypeAnnotation(annotation->Clone(allocator, this)->AsTypeNode()); + SetTsTypeAnnotation(annotation->Clone(allocator, this)); } } diff --git a/ets2panda/ir/statement.h b/ets2panda/ir/statement.h index 76e05d1d4bf..c1004f771cf 100644 --- a/ets2panda/ir/statement.h +++ b/ets2panda/ir/statement.h @@ -36,14 +36,6 @@ public: virtual void SetReturnType([[maybe_unused]] checker::ETSChecker *checker, [[maybe_unused]] checker::Type *type) {} - // NOLINTNEXTLINE(google-default-arguments) - [[nodiscard]] virtual Statement *Clone([[maybe_unused]] ArenaAllocator *const allocator, - [[maybe_unused]] AstNode *const parent = nullptr) - { - UNREACHABLE(); - return nullptr; - } - protected: explicit Statement(AstNodeType type) : AstNode(type) {} explicit Statement(AstNodeType type, ModifierFlags flags) : AstNode(type, flags) {} diff --git a/ets2panda/ir/ts/tsTypeParameterInstantiation.cpp b/ets2panda/ir/ts/tsTypeParameterInstantiation.cpp index 614e9798df1..f9906355134 100644 --- a/ets2panda/ir/ts/tsTypeParameterInstantiation.cpp +++ b/ets2panda/ir/ts/tsTypeParameterInstantiation.cpp @@ -27,12 +27,13 @@ TSTypeParameterInstantiation::TSTypeParameterInstantiation([[maybe_unused]] Tag : Expression(static_cast(other)), params_(allocator->Adapter()) { for (auto *param : other.params_) { - params_.emplace_back(param->Clone(allocator, this)->AsTypeNode()); + params_.emplace_back(param->Clone(allocator, this)); } } // NOLINTNEXTLINE(google-default-arguments) -Expression *TSTypeParameterInstantiation::Clone(ArenaAllocator *const allocator, AstNode *const parent) +TSTypeParameterInstantiation *TSTypeParameterInstantiation::Clone(ArenaAllocator *const allocator, + AstNode *const parent) { if (auto *const clone = allocator->New(Tag {}, *this, allocator); clone != nullptr) { if (parent != nullptr) { diff --git a/ets2panda/ir/ts/tsTypeParameterInstantiation.h b/ets2panda/ir/ts/tsTypeParameterInstantiation.h index 727e08911e5..09db9a4b1df 100644 --- a/ets2panda/ir/ts/tsTypeParameterInstantiation.h +++ b/ets2panda/ir/ts/tsTypeParameterInstantiation.h @@ -43,7 +43,7 @@ public: } // NOLINTNEXTLINE(google-default-arguments) - [[nodiscard]] Expression *Clone(ArenaAllocator *allocator, AstNode *parent = nullptr) override; + [[nodiscard]] TSTypeParameterInstantiation *Clone(ArenaAllocator *allocator, AstNode *parent = nullptr) override; void TransformChildren(const NodeTransformer &cb) override; void Iterate(const NodeTraverser &cb) const override; diff --git a/ets2panda/ir/typeNode.cpp b/ets2panda/ir/typeNode.cpp index 991f0a7d954..be22e10ee70 100644 --- a/ets2panda/ir/typeNode.cpp +++ b/ets2panda/ir/typeNode.cpp @@ -14,13 +14,14 @@ */ #include "typeNode.h" +#include "astNode.h" #include "opaqueTypeNode.h" #include "es2panda.h" namespace panda::es2panda::ir { // NOLINTNEXTLINE(google-default-arguments) -Expression *TypeNode::Clone(ArenaAllocator *const allocator, AstNode *const parent) +TypeNode *TypeNode::Clone(ArenaAllocator *const allocator, AstNode *const parent) { if (auto *const type = TsType(); type != nullptr) { if (auto *const clone = allocator->New(type); clone != nullptr) { diff --git a/ets2panda/ir/typeNode.h b/ets2panda/ir/typeNode.h index c92340a3a1f..7933f9b5e40 100644 --- a/ets2panda/ir/typeNode.h +++ b/ets2panda/ir/typeNode.h @@ -48,7 +48,7 @@ public: } // NOLINTNEXTLINE(google-default-arguments) - [[nodiscard]] Expression *Clone(ArenaAllocator *allocator, AstNode *parent = nullptr) override; + [[nodiscard]] TypeNode *Clone(ArenaAllocator *allocator, AstNode *parent = nullptr) override; protected: explicit TypeNode(AstNodeType const type) : Expression(type) {} -- Gitee