From 929f1959692351a8a18329ea493d419a442480ec Mon Sep 17 00:00:00 2001 From: songqi Date: Tue, 1 Aug 2023 19:07:55 +0800 Subject: [PATCH] Reset parent for transformed AstNodes Issue: I7SR5T Tests: test_es2abc Signed-off-by: songqi Change-Id: I8984c469d65bdc12d00263eaa743e966b335e050 --- es2panda/binder/binder.cpp | 15 ++++++ es2panda/binder/binder.h | 2 + es2panda/parser/transformer/transformer.cpp | 4 -- .../class/test-class-property1-expected.txt | 49 +++++++++++++++++++ .../bytecode/ts/class/test-class-property1.ts | 19 +++++++ es2panda/test/runner.py | 1 + 6 files changed, 86 insertions(+), 4 deletions(-) create mode 100644 es2panda/test/bytecode/ts/class/test-class-property1-expected.txt create mode 100644 es2panda/test/bytecode/ts/class/test-class-property1.ts diff --git a/es2panda/binder/binder.cpp b/es2panda/binder/binder.cpp index 7496cc6aa4..bb9ff93029 100644 --- a/es2panda/binder/binder.cpp +++ b/es2panda/binder/binder.cpp @@ -572,6 +572,10 @@ void Binder::BuildClassDefinition(ir::ClassDefinition *classDef) ResolveReference(classDef, stmt); } + if (extension_ == ScriptExtension::TS) { + ResetParentForTransformedAtsNode(classDef, classDef->Ctor()); + } + for (auto *iter : classDef->IndexSignatures()) { ResolveReference(classDef, iter); } @@ -806,6 +810,17 @@ void Binder::ResolveReferences(const ir::AstNode *parent) parent->Iterate([this, parent](auto *childNode) { ResolveReference(parent, childNode); }); } +void Binder::ResetParentForTransformedAtsNodes(const ir::AstNode *parent) +{ + parent->Iterate([this, parent](auto *childNode) { ResetParentForTransformedAtsNode(parent, childNode); }); +} + +void Binder::ResetParentForTransformedAtsNode(const ir::AstNode *parent, ir::AstNode *childNode) +{ + childNode->SetParent(parent); + ResetParentForTransformedAtsNodes(childNode); +} + void Binder::AddMandatoryParam(const std::string_view &name) { ASSERT(scope_->IsFunctionVariableScope()); diff --git a/es2panda/binder/binder.h b/es2panda/binder/binder.h index 1f51a5ed9e..3ec11c72f5 100644 --- a/es2panda/binder/binder.h +++ b/es2panda/binder/binder.h @@ -210,6 +210,8 @@ private: void LookupIdentReference(ir::Identifier *ident); void ResolveReference(const ir::AstNode *parent, ir::AstNode *childNode); void ResolveReferences(const ir::AstNode *parent); + void ResetParentForTransformedAtsNode(const ir::AstNode *parent, ir::AstNode *childNode); + void ResetParentForTransformedAtsNodes(const ir::AstNode *parent); void ValidateExportDecl(const ir::ExportNamedDeclaration *exportDecl); void StoreAndCheckSpecialFunctionName(std::string &internalNameStr, std::string recordName); void ReplaceConstReferenceWithInitialization(const ir::Identifier *ident, const Decl *decl); diff --git a/es2panda/parser/transformer/transformer.cpp b/es2panda/parser/transformer/transformer.cpp index 0a2b7f0f75..79aee10f76 100644 --- a/es2panda/parser/transformer/transformer.cpp +++ b/es2panda/parser/transformer/transformer.cpp @@ -2032,10 +2032,6 @@ void Transformer::CheckTransformedAstNode(const ir::AstNode *parent, ir::AstNode (childNode->AsClassProperty()->IsStatic() || childNode->AsClassProperty()->Value() != nullptr)) { return; } - if (childNode->IsMethodDefinition() && - childNode->AsMethodDefinition()->Kind() == ir::MethodDefinitionKind::CONSTRUCTOR) { - return; - } if (childNode->IsDecorator()) { return; } diff --git a/es2panda/test/bytecode/ts/class/test-class-property1-expected.txt b/es2panda/test/bytecode/ts/class/test-class-property1-expected.txt new file mode 100644 index 0000000000..b80bd4ee09 --- /dev/null +++ b/es2panda/test/bytecode/ts/class/test-class-property1-expected.txt @@ -0,0 +1,49 @@ +.language ECMAScript + +.function any .#4021257117105775747#(any a0, any a1, any a2) { +label_1: +label_0: + tryldglobalbyname 0x0, print + sta v0 + ldlexvar 0x0, 0x1 + sta v1 + lda v0 + callarg1 0x1, v1 + ldundefined + returnundefined +label_2: +} + +.function any .A(any a0, any a1, any a2) { +label_1: +label_0: + newlexenv 0x2 + lda a1 + stlexvar 0x0, 0x0 + lda a2 + stlexvar 0x0, 0x1 + ldlexvar 0x0, 0x1 + sta v0 + definefunc 0x0, .#4021257117105775747#, 0x0 + stobjbyname 0x1, prop, v0 + ldlexvar 0x0, 0x1 + return +label_2: +} + +.function any .func_main_0(any a0, any a1, any a2) { +label_1: +label_0: + ldhole + sta v0 + defineclasswithbuffer 0x0, .A, _0, 0x0, v0 + sta v0 + ldobjbyname 0x1, prototype + lda v0 + sttoglobalrecord 0x3, A + ldundefined + returnundefined +label_2: +} + + diff --git a/es2panda/test/bytecode/ts/class/test-class-property1.ts b/es2panda/test/bytecode/ts/class/test-class-property1.ts new file mode 100644 index 0000000000..b644116408 --- /dev/null +++ b/es2panda/test/bytecode/ts/class/test-class-property1.ts @@ -0,0 +1,19 @@ +/* + * Copyright (c) 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +class A { + private prop = () => { print(this) } +} \ No newline at end of file diff --git a/es2panda/test/runner.py b/es2panda/test/runner.py index 0b9428c5ce..3bf5ff4d59 100755 --- a/es2panda/test/runner.py +++ b/es2panda/test/runner.py @@ -1506,6 +1506,7 @@ def main(): runner = BytecodeRunner(args) runner.add_directory("bytecode/commonjs", "js", ["--commonjs", "--dump-assembly"]) runner.add_directory("bytecode/js", "js", ["--dump-assembly"]) + runner.add_directory("bytecode/ts", "ts", ["--dump-assembly"]) runners.append(runner) -- Gitee