From dc307716bc32ff28101d7cea6e736e43293636a4 Mon Sep 17 00:00:00 2001 From: xucheng46 Date: Sun, 29 Jan 2023 20:29:38 +0800 Subject: [PATCH 1/2] Remove types in import entries Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/I6BKDW Test: parser tests, compiler tests, tsc tests, test262 Signed-off-by: xucheng46 Change-Id: I8c4b8a23cde6e951b1c26ef85ef18c4d296e5a2f --- es2panda/binder/binder.cpp | 11 ++++++++ es2panda/ir/expressions/identifier.h | 11 ++++++++ .../parser/module/sourceTextModuleRecord.cpp | 21 ++++++++++++++ .../parser/module/sourceTextModuleRecord.h | 8 ++++-- es2panda/parser/parserImpl.cpp | 3 ++ es2panda/parser/statementParser.cpp | 1 + .../test-ts-export-type.ts | 22 +++++++++++++++ .../test-ts-import-type-exec-expected.txt | 3 ++ .../test-ts-import-type-exec.ts | 28 +++++++++++++++++++ 9 files changed, 106 insertions(+), 2 deletions(-) create mode 100644 es2panda/test/compiler/ts/projects/ts_import_type_project_3/test-ts-export-type.ts create mode 100644 es2panda/test/compiler/ts/projects/ts_import_type_project_3/test-ts-import-type-exec-expected.txt create mode 100644 es2panda/test/compiler/ts/projects/ts_import_type_project_3/test-ts-import-type-exec.ts diff --git a/es2panda/binder/binder.cpp b/es2panda/binder/binder.cpp index f7dd8b5d56..bf5e640345 100644 --- a/es2panda/binder/binder.cpp +++ b/es2panda/binder/binder.cpp @@ -119,6 +119,10 @@ void Binder::IdentifierAnalysis(ResolveBindingFlags flags) ResolveReferences(program_->Ast()); AddMandatoryParams(); if (topScope_->IsModuleScope()) { + if (Program()->Extension() == ScriptExtension::TS) { + auto *moduleRecord = Program()->ModuleRecord(); + moduleRecord->RemoveImportEntry(); + } AssignIndexToModuleVariable(); } } @@ -241,6 +245,13 @@ void Binder::LookupIdentReference(ir::Identifier *ident) ident->SetTdz(); } + if (topScope_->IsModuleScope() && Program()->Extension() == ScriptExtension::TS && + decl->HasFlag(DeclarationFlags::IMPORT) && !ident->IsType()) { + auto name = ident->Name(); + auto *moduleRecord = Program()->ModuleRecord(); + moduleRecord->RemoveImportEntryTypeFlag(name); + } + ident->SetVariable(res.variable); } diff --git a/es2panda/ir/expressions/identifier.h b/es2panda/ir/expressions/identifier.h index 8bf87b5d86..b1e43937c6 100644 --- a/es2panda/ir/expressions/identifier.h +++ b/es2panda/ir/expressions/identifier.h @@ -39,6 +39,7 @@ enum class IdentifierFlags { OPTIONAL = 1 << 0, REFERENCE = 1 << 1, TDZ = 1 << 2, + TYPE = 1 << 3, }; DEFINE_BITOPS(IdentifierFlags) @@ -101,6 +102,16 @@ public: flags_ |= IdentifierFlags::REFERENCE; } + bool IsType() const + { + return (flags_ & IdentifierFlags::TYPE) != 0; + } + + void SetType() + { + flags_ |= IdentifierFlags::TYPE; + } + const std::vector &TSVariables() const { return tsVariables_; diff --git a/es2panda/parser/module/sourceTextModuleRecord.cpp b/es2panda/parser/module/sourceTextModuleRecord.cpp index a6b213ed4a..e5e0ab85fb 100644 --- a/es2panda/parser/module/sourceTextModuleRecord.cpp +++ b/es2panda/parser/module/sourceTextModuleRecord.cpp @@ -187,4 +187,25 @@ namespace panda::es2panda::parser { (*index)++; } } + + void SourceTextModuleRecord::RemoveImportEntryTypeFlag(const util::StringView name) + { + ASSERT(!name.Empty()); + auto regularImport = regularImportEntries_.find(name); + if (regularImport != regularImportEntries_.end()) { + regularImport->second->typeFlag_ = false; + } + } + + void SourceTextModuleRecord::RemoveImportEntry() + { + for (auto iter = regularImportEntries_.begin(); iter != regularImportEntries_.end();) + { + if (iter->second->typeFlag_) { + iter = regularImportEntries_.erase(iter); + } else { + iter++; + } + } + } } // namespace panda::es2panda::parser diff --git a/es2panda/parser/module/sourceTextModuleRecord.h b/es2panda/parser/module/sourceTextModuleRecord.h index 7e08fbe0f4..826508676b 100644 --- a/es2panda/parser/module/sourceTextModuleRecord.h +++ b/es2panda/parser/module/sourceTextModuleRecord.h @@ -52,13 +52,14 @@ public: util::StringView importName_; const ir::Identifier *localId_; const ir::Identifier *importId_; + bool typeFlag_; ImportEntry(const util::StringView localName, const util::StringView importName, int moduleRequestIdx, const ir::Identifier *localId, const ir::Identifier *importId) : moduleRequestIdx_(moduleRequestIdx), localName_(localName), importName_(importName), - localId_(localId), importId_(importId) {} + localId_(localId), importId_(importId), typeFlag_(true) {} ImportEntry(const util::StringView localName, int moduleRequestIdx, const ir::Identifier *localId) - : moduleRequestIdx_(moduleRequestIdx), localName_(localName), localId_(localId) {} + : moduleRequestIdx_(moduleRequestIdx), localName_(localName), localId_(localId), typeFlag_(true) {} }; struct ExportEntry { @@ -99,6 +100,9 @@ public: void AssignIndexToModuleVariable(binder::ModuleScope *moduleScope); + void RemoveImportEntryTypeFlag(const util::StringView source); + void RemoveImportEntry(); + using ModuleRequestList = ArenaVector; using ModuleRequestMap = ArenaMap; using ModuleRequestIdxMap = ArenaMap; diff --git a/es2panda/parser/parserImpl.cpp b/es2panda/parser/parserImpl.cpp index a245d55f62..e89c9ccdf4 100644 --- a/es2panda/parser/parserImpl.cpp +++ b/es2panda/parser/parserImpl.cpp @@ -302,6 +302,7 @@ ir::TSTypeReference *ParserImpl::ParseTsConstExpression() { auto *identRef = AllocNode(lexer_->GetToken().Ident()); identRef->SetReference(); + identRef->SetType(); identRef->SetRange(lexer_->GetToken().Loc()); auto *typeReference = AllocNode(identRef, nullptr); @@ -1054,6 +1055,7 @@ ir::Expression *ParserImpl::ParseTsTypeReferenceOrQuery(bool parseQuery) ir::Expression *typeName = AllocNode(lexer_->GetToken().Ident()); typeName->SetRange(lexer_->GetToken().Loc()); typeName->AsIdentifier()->SetReference(); + typeName->AsIdentifier()->SetType(); if (lexer_->Lookahead() == LEX_CHAR_LESS_THAN) { lexer_->ForwardToken(lexer::TokenType::PUNCTUATOR_LESS_THAN, 1); @@ -2826,6 +2828,7 @@ ir::ClassDefinition *ParserImpl::ParseClassDefinition(bool isDeclaration, bool i ir::Expression *expr = AllocNode(lexer_->GetToken().Ident()); expr->SetRange(lexer_->GetToken().Loc()); expr->AsIdentifier()->SetReference(); + expr->AsIdentifier()->SetType(); lexer_->NextToken(); diff --git a/es2panda/parser/statementParser.cpp b/es2panda/parser/statementParser.cpp index e8f67909e3..fd56131f24 100644 --- a/es2panda/parser/statementParser.cpp +++ b/es2panda/parser/statementParser.cpp @@ -777,6 +777,7 @@ ir::TSInterfaceDeclaration *ParserImpl::ParseTsInterfaceDeclaration() lexer::SourcePosition heritageEnd = lexer_->GetToken().End(); ir::Expression *expr = AllocNode(lexer_->GetToken().Ident()); expr->AsIdentifier()->SetReference(); + expr->AsIdentifier()->SetType(); expr->SetRange(lexer_->GetToken().Loc()); if (lexer_->Lookahead() == LEX_CHAR_LESS_THAN) { diff --git a/es2panda/test/compiler/ts/projects/ts_import_type_project_3/test-ts-export-type.ts b/es2panda/test/compiler/ts/projects/ts_import_type_project_3/test-ts-export-type.ts new file mode 100644 index 0000000000..a663fe914b --- /dev/null +++ b/es2panda/test/compiler/ts/projects/ts_import_type_project_3/test-ts-export-type.ts @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2022 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. + */ + + +interface A { + a : string; +} +var b : A = {a : "b"}; +export type {A}; +export {b}; diff --git a/es2panda/test/compiler/ts/projects/ts_import_type_project_3/test-ts-import-type-exec-expected.txt b/es2panda/test/compiler/ts/projects/ts_import_type_project_3/test-ts-import-type-exec-expected.txt new file mode 100644 index 0000000000..de980441c3 --- /dev/null +++ b/es2panda/test/compiler/ts/projects/ts_import_type_project_3/test-ts-import-type-exec-expected.txt @@ -0,0 +1,3 @@ +a +b +c diff --git a/es2panda/test/compiler/ts/projects/ts_import_type_project_3/test-ts-import-type-exec.ts b/es2panda/test/compiler/ts/projects/ts_import_type_project_3/test-ts-import-type-exec.ts new file mode 100644 index 0000000000..c4e1271eaf --- /dev/null +++ b/es2panda/test/compiler/ts/projects/ts_import_type_project_3/test-ts-import-type-exec.ts @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2022 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. + */ + + +import {A, b} from "./test-ts-export-type"; + +var a : A = {a : "a"}; +print(a.a); +print(b.a); + +class C implements A { + a = "c"; +} +var c = new C(); +print(c.a) + \ No newline at end of file -- Gitee From 3ccbcf3a82e5f1724c21649643501fd777fd01de Mon Sep 17 00:00:00 2001 From: songqi Date: Fri, 24 Feb 2023 15:04:23 +0800 Subject: [PATCH 2/2] Remove unused moduleRequest Signed-off-by: songqi Change-Id: I3a717bbb42466205a94304e858a691a0e4b8ae07 --- es2panda/binder/binder.cpp | 3 +- .../parser/module/sourceTextModuleRecord.cpp | 32 +++++++++++++++++-- .../parser/module/sourceTextModuleRecord.h | 1 + .../test-ts-export-type.ts | 5 +-- .../test-ts-import-type-exec.ts | 3 +- .../test-ts-import-type-exec-expected.txt | 2 ++ .../test-ts-import-type-exec.ts | 23 +++++++++++++ 7 files changed, 62 insertions(+), 7 deletions(-) create mode 100644 es2panda/test/compiler/ts/projects/ts_import_type_project_4/test-ts-import-type-exec-expected.txt create mode 100644 es2panda/test/compiler/ts/projects/ts_import_type_project_4/test-ts-import-type-exec.ts diff --git a/es2panda/binder/binder.cpp b/es2panda/binder/binder.cpp index bf5e640345..23989ffce4 100644 --- a/es2panda/binder/binder.cpp +++ b/es2panda/binder/binder.cpp @@ -122,6 +122,7 @@ void Binder::IdentifierAnalysis(ResolveBindingFlags flags) if (Program()->Extension() == ScriptExtension::TS) { auto *moduleRecord = Program()->ModuleRecord(); moduleRecord->RemoveImportEntry(); + moduleRecord->RemoveModuleRequest(); } AssignIndexToModuleVariable(); } @@ -763,7 +764,7 @@ void Binder::AddDeclarationName(const util::StringView &name, DeclType type) return; } variableNames_.insert(name); - + if (type == DeclType::ENUM) { return; } diff --git a/es2panda/parser/module/sourceTextModuleRecord.cpp b/es2panda/parser/module/sourceTextModuleRecord.cpp index e5e0ab85fb..ee2be93984 100644 --- a/es2panda/parser/module/sourceTextModuleRecord.cpp +++ b/es2panda/parser/module/sourceTextModuleRecord.cpp @@ -13,6 +13,8 @@ * limitations under the License. */ +#include + #include "sourceTextModuleRecord.h" #include @@ -199,8 +201,7 @@ namespace panda::es2panda::parser { void SourceTextModuleRecord::RemoveImportEntry() { - for (auto iter = regularImportEntries_.begin(); iter != regularImportEntries_.end();) - { + for (auto iter = regularImportEntries_.begin(); iter != regularImportEntries_.end();) { if (iter->second->typeFlag_) { iter = regularImportEntries_.erase(iter); } else { @@ -208,4 +209,31 @@ namespace panda::es2panda::parser { } } } + + void SourceTextModuleRecord::RemoveModuleRequest() + { + std::set moduleRequestIdxList; + for (auto iter = regularImportEntries_.begin(); iter != regularImportEntries_.end(); iter++) { + moduleRequestIdxList.insert(iter->second->moduleRequestIdx_); + } + for (auto iter = namespaceImportEntries_.begin(); iter != namespaceImportEntries_.end(); iter++) { + moduleRequestIdxList.insert((*iter)->moduleRequestIdx_); + } + + for (auto iter = moduleRequestsIdxMap_.begin(); iter != moduleRequestsIdxMap_.end();) { + if (moduleRequestIdxList.find(iter->first) == moduleRequestIdxList.end()) { + const util::StringView source = iter->second; + iter = moduleRequestsIdxMap_.erase(iter); + moduleRequestsMap_.erase(source); + for (auto it = moduleRequests_.begin(); it != moduleRequests_.end(); it++) { + if (*it == source) { + moduleRequests_.erase(it); + break; + } + } + } else { + iter++; + } + } + } } // namespace panda::es2panda::parser diff --git a/es2panda/parser/module/sourceTextModuleRecord.h b/es2panda/parser/module/sourceTextModuleRecord.h index 826508676b..65476038f6 100644 --- a/es2panda/parser/module/sourceTextModuleRecord.h +++ b/es2panda/parser/module/sourceTextModuleRecord.h @@ -102,6 +102,7 @@ public: void RemoveImportEntryTypeFlag(const util::StringView source); void RemoveImportEntry(); + void RemoveModuleRequest(); using ModuleRequestList = ArenaVector; using ModuleRequestMap = ArenaMap; diff --git a/es2panda/test/compiler/ts/projects/ts_import_type_project_3/test-ts-export-type.ts b/es2panda/test/compiler/ts/projects/ts_import_type_project_3/test-ts-export-type.ts index a663fe914b..119d2c85bc 100644 --- a/es2panda/test/compiler/ts/projects/ts_import_type_project_3/test-ts-export-type.ts +++ b/es2panda/test/compiler/ts/projects/ts_import_type_project_3/test-ts-export-type.ts @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * 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 @@ -17,6 +17,7 @@ interface A { a : string; } + var b : A = {a : "b"}; export type {A}; -export {b}; +export {b}; diff --git a/es2panda/test/compiler/ts/projects/ts_import_type_project_3/test-ts-import-type-exec.ts b/es2panda/test/compiler/ts/projects/ts_import_type_project_3/test-ts-import-type-exec.ts index c4e1271eaf..b75e92c8f7 100644 --- a/es2panda/test/compiler/ts/projects/ts_import_type_project_3/test-ts-import-type-exec.ts +++ b/es2panda/test/compiler/ts/projects/ts_import_type_project_3/test-ts-import-type-exec.ts @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * 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 @@ -25,4 +25,3 @@ class C implements A { } var c = new C(); print(c.a) - \ No newline at end of file diff --git a/es2panda/test/compiler/ts/projects/ts_import_type_project_4/test-ts-import-type-exec-expected.txt b/es2panda/test/compiler/ts/projects/ts_import_type_project_4/test-ts-import-type-exec-expected.txt new file mode 100644 index 0000000000..422c2b7ab3 --- /dev/null +++ b/es2panda/test/compiler/ts/projects/ts_import_type_project_4/test-ts-import-type-exec-expected.txt @@ -0,0 +1,2 @@ +a +b diff --git a/es2panda/test/compiler/ts/projects/ts_import_type_project_4/test-ts-import-type-exec.ts b/es2panda/test/compiler/ts/projects/ts_import_type_project_4/test-ts-import-type-exec.ts new file mode 100644 index 0000000000..e12d0d894b --- /dev/null +++ b/es2panda/test/compiler/ts/projects/ts_import_type_project_4/test-ts-import-type-exec.ts @@ -0,0 +1,23 @@ +/* + * 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. + */ + + +import type {type1} from "./null-file"; +import {type2} from "./null-file"; + +var a : type1 = {a : "a"}; +var b : type2 = {b : "b"}; +print(a.a); +print(b.b); -- Gitee