From f16e2de8c02aedace062f391d2fd8e07009cf0ee Mon Sep 17 00:00:00 2001 From: xucheng46 Date: Sun, 29 Jan 2023 20:29:38 +0800 Subject: [PATCH] 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