From 52e4f329f4784ccebd121611928de65f4ae771c1 Mon Sep 17 00:00:00 2001 From: huangyu Date: Mon, 24 Apr 2023 20:41:39 +0800 Subject: [PATCH] Support distinguish if function is generated from namspace/enum syntax Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/I6YMZV Test: Add type extractor test test-namespace-enum-transformed-function.ts Signed-off-by: huangyu Change-Id: I946c66f8e5fe38bf67fb24ee7e92c35995601aba --- .../core/emitter/typeExtractorEmitter.cpp | 2 +- ...ace-enum-transformed-function-expected.txt | 209 ++++++++++++++++++ ...est-namespace-enum-transformed-function.ts | 42 ++++ .../typescript/extractor/typeExtractor.cpp | 42 ++++ es2panda/typescript/extractor/typeExtractor.h | 1 + es2panda/typescript/extractor/typeSystem.h | 5 + 6 files changed, 300 insertions(+), 1 deletion(-) create mode 100644 es2panda/test/type_extractor/testcases/test-namespace-enum-transformed-function-expected.txt create mode 100644 es2panda/test/type_extractor/testcases/test-namespace-enum-transformed-function.ts diff --git a/es2panda/compiler/core/emitter/typeExtractorEmitter.cpp b/es2panda/compiler/core/emitter/typeExtractorEmitter.cpp index 586c42665e..c07c148f95 100644 --- a/es2panda/compiler/core/emitter/typeExtractorEmitter.cpp +++ b/es2panda/compiler/core/emitter/typeExtractorEmitter.cpp @@ -117,7 +117,7 @@ void TypeExtractorEmitter::GenFunctionTypeInfo(panda::pandasm::Program *prog) co index++; } - if (pg_->TypedFunc().first > extractor::TypeRecorder::PRIMITIVETYPE_ANY) { + if (pg_->TypedFunc().first != extractor::TypeRecorder::PRIMITIVETYPE_ANY) { // -1 for function type GenInsnTypeInfo(recorder, static_cast(-1), pg_->TypedFunc().first, typedInsns); DCOUT << "[LOG]" << func_->name << ": -1 | " << pg_->TypedFunc().first << std::endl; diff --git a/es2panda/test/type_extractor/testcases/test-namespace-enum-transformed-function-expected.txt b/es2panda/test/type_extractor/testcases/test-namespace-enum-transformed-function-expected.txt new file mode 100644 index 0000000000..6f5b814143 --- /dev/null +++ b/es2panda/test/type_extractor/testcases/test-namespace-enum-transformed-function-expected.txt @@ -0,0 +1,209 @@ +======> literal array buffer <====== +------------------------------------ +slot _-1 +{ + index: 0 + tag: 0 + val: 2 +}, +{ + index: 1 + tag: 2 + val: -1 +}, +{ + index: 2 + tag: 0 + val: 25 +}, +{ + index: 3 + tag: 25 + val: 254 +}, +------------------------------------ +slot _-2 +{ + index: 0 + tag: 0 + val: 2 +}, +{ + index: 1 + tag: 2 + val: -1 +}, +{ + index: 2 + tag: 0 + val: 25 +}, +{ + index: 3 + tag: 25 + val: 255 +}, +------------------------------------ +slot _-3 +{ + index: 0 + tag: 0 + val: 2 +}, +{ + index: 1 + tag: 2 + val: -1 +}, +{ + index: 2 + tag: 0 + val: 25 +}, +{ + index: 3 + tag: 25 + val: 254 +}, +------------------------------------ +slot _-4 +{ + index: 0 + tag: 0 + val: 2 +}, +{ + index: 1 + tag: 2 + val: -1 +}, +{ + index: 2 + tag: 0 + val: 25 +}, +{ + index: 3 + tag: 25 + val: 255 +}, +------------------------------------ +slot _-5 +{ + index: 0 + tag: 0 + val: 2 +}, +{ + index: 1 + tag: 2 + val: -1 +}, +{ + index: 2 + tag: 0 + val: 25 +}, +{ + index: 3 + tag: 25 + val: 254 +}, +------------------------------------ +slot _-6 +{ + index: 0 + tag: 0 + val: 2 +}, +{ + index: 1 + tag: 2 + val: -1 +}, +{ + index: 2 + tag: 0 + val: 25 +}, +{ + index: 3 + tag: 25 + val: 255 +}, +------------------------------------ +slot _-7 +{ + index: 0 + tag: 0 + val: 2 +}, +{ + index: 1 + tag: 2 + val: -1 +}, +{ + index: 2 + tag: 0 + val: 25 +}, +{ + index: 3 + tag: 25 + val: 255 +}, +------------------------------------ +slot _0 +{ + index: 0 + tag: 0 + val: 2 +}, +{ + index: 1 + tag: 2 + val: 0 +}, +{ + index: 2 + tag: 0 + val: 2 +}, +{ + index: 3 + tag: 2 + val: 0 +}, +------------------------------------ +slot _1 +{ + index: 0 + tag: 2 + val: 0 +}, +{ + index: 1 + tag: 2 + val: 0 +}, +{ + index: 2 + tag: 2 + val: 0 +}, +{ + index: 3 + tag: 2 + val: 0 +}, +{ + index: 4 + tag: 2 + val: 0 +}, +{ + index: 5 + tag: 2 + val: 0 +}, diff --git a/es2panda/test/type_extractor/testcases/test-namespace-enum-transformed-function.ts b/es2panda/test/type_extractor/testcases/test-namespace-enum-transformed-function.ts new file mode 100644 index 0000000000..2dd2f4400e --- /dev/null +++ b/es2panda/test/type_extractor/testcases/test-namespace-enum-transformed-function.ts @@ -0,0 +1,42 @@ +/* + * 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. + */ + + +namespace CLASS { + export module Description { + export enum AccessFlag { + Public, + Private, + Protected + } + } +} + +module FUNCTION { + export enum Modifier { + Abstract, + Static + } +} + +namespace FUNCTION { + export let AccessFlag = CLASS.Description.AccessFlag; +} + +enum LANGUAGE { + ETS, + TS, + JS +} diff --git a/es2panda/typescript/extractor/typeExtractor.cpp b/es2panda/typescript/extractor/typeExtractor.cpp index 238672b38c..6ec75948cf 100644 --- a/es2panda/typescript/extractor/typeExtractor.cpp +++ b/es2panda/typescript/extractor/typeExtractor.cpp @@ -17,10 +17,12 @@ #include #include +#include #include #include #include #include +#include #include #include #include @@ -102,6 +104,8 @@ TypeExtractor::TypeExtractor(const ir::BlockStatement *rootNode, bool typeDtsExt std::bind(&TypeExtractor::HandleInterfaceDeclaration, this, std::placeholders::_1); handlerMap_[ir::AstNodeType::TS_TYPE_ALIAS_DECLARATION] = std::bind(&TypeExtractor::HandleTypeAliasDeclaration, this, std::placeholders::_1); + handlerMap_[ir::AstNodeType::EXPRESSION_STATEMENT] = + std::bind(&TypeExtractor::HandleNewlyGenFuncExpression, this, std::placeholders::_1); } void TypeExtractor::StartTypeExtractor(const parser::Program *program) @@ -740,6 +744,44 @@ void TypeExtractor::HandleTypeAliasDeclaration(const ir::AstNode *node) } } +void TypeExtractor::HandleNewlyGenFuncExpression(const ir::AstNode *node) +{ + if (!node->Original()) { + return; + } + auto originalNode = node->Original(); + int64_t typeFlag = PrimitiveType::ANY; + switch (originalNode->Type()) { + case ir::AstNodeType::TS_MODULE_DECLARATION: { + typeFlag = extractor::BuiltinFlag::NAMESPACE_FUNCTION; + break; + } + case ir::AstNodeType::TS_ENUM_DECLARATION: { + typeFlag = extractor::BuiltinFlag::ENUM_FUNCTION; + break; + } + case ir::AstNodeType::EXPORT_NAMED_DECLARATION: { + auto decl = originalNode->AsExportNamedDeclaration()->Decl(); + if (!decl) { + return; + } + if (decl->IsTSModuleDeclaration()) { + typeFlag = extractor::BuiltinFlag::NAMESPACE_FUNCTION; + } else if (decl->IsTSEnumDeclaration()) { + typeFlag = extractor::BuiltinFlag::ENUM_FUNCTION; + } + break; + } + default: { + return; + } + } + if (typeFlag != PrimitiveType::ANY) { + auto funcExpr = node->AsExpressionStatement()->GetExpression()->AsCallExpression()->Callee(); + recorder_->SetNodeTypeIndex(funcExpr->AsFunctionExpression()->Function(), typeFlag); + } +} + int64_t TypeExtractor::GetTypeIndexFromClassInst(int64_t typeIndex) { auto typeIndexTmp = recorder_->GetClassInst(typeIndex); diff --git a/es2panda/typescript/extractor/typeExtractor.h b/es2panda/typescript/extractor/typeExtractor.h index e9718c134e..d0b5935502 100644 --- a/es2panda/typescript/extractor/typeExtractor.h +++ b/es2panda/typescript/extractor/typeExtractor.h @@ -93,6 +93,7 @@ private: void HandleClassDeclaration(const ir::AstNode *node); void HandleInterfaceDeclaration(const ir::AstNode *node); void HandleTypeAliasDeclaration(const ir::AstNode *node); + void HandleNewlyGenFuncExpression(const ir::AstNode *node); // Helpers int64_t GetTypeIndexFromTypeReference(const ir::TSTypeReference *typeReference, bool isNewInstance); diff --git a/es2panda/typescript/extractor/typeSystem.h b/es2panda/typescript/extractor/typeSystem.h index 1420492ac3..361c74faec 100644 --- a/es2panda/typescript/extractor/typeSystem.h +++ b/es2panda/typescript/extractor/typeSystem.h @@ -222,6 +222,11 @@ enum UserType : uint8_t { INDEXSIG }; +enum BuiltinFlag : int8_t { + NAMESPACE_FUNCTION = -1, + ENUM_FUNCTION = -2 +}; + enum FuncModifier : uint8_t { NOMODIFIER = 0, STATIC = 1 << 2, -- Gitee