diff --git a/checker/ets/typeRelationContext.cpp b/checker/ets/typeRelationContext.cpp index 822e391d5fc81e88fe34f612fe11cbba8cc50143..395e0e2e9d5e554c3a6221a5159b1c0a56424721 100644 --- a/checker/ets/typeRelationContext.cpp +++ b/checker/ets/typeRelationContext.cpp @@ -147,13 +147,17 @@ void InstantiationContext::InstantiateType(ETSObjectType *type, ir::TSTypeParame checker::ScopeContext scope_ctx(checker_, type_param_decl->Scope()); - // TODO(mmartin): are type_arg_vars_ needed? + // Instantiation algorithm is the following: + // 1. Instantiate 'type' by modifying its type types of type params + // 2. Make a copy of 'type' + // 3. Restore origin 'type' parmas types. + std::vector origin_types(type_param_decl->Params().size()); for (size_t idx = 0; idx < type_param_decl->Params().size(); idx++) { auto *const param_var = type_param_decl->Params()[idx]->Name()->Variable()->AsLocalVariable(); auto *const type_param = param_var->TsType()->AsETSTypeParameter(); + origin_types[idx] = type_param->GetType(); type_param->SetType(type_arg_types[idx]); - type_arg_vars_.push_back(param_var); } result_ = type->Instantiate(checker_->Allocator(), checker_->Relation(), checker_->GetGlobalTypesHolder()) @@ -162,6 +166,10 @@ void InstantiationContext::InstantiateType(ETSObjectType *type, ir::TSTypeParame type->GetInstantiationMap().try_emplace(hash, result_); result_->AddTypeFlag(TypeFlag::GENERIC); result_->SetTypeArguments(std::move(type_arg_types)); - type_arg_vars_.clear(); + for (size_t idx = 0; idx < type_param_decl->Params().size(); idx++) { + auto *const param_var = type_param_decl->Params()[idx]->Name()->Variable()->AsLocalVariable(); + auto *const type_param = param_var->TsType()->AsETSTypeParameter(); + type_param->SetType(origin_types[idx]); + } } } // namespace panda::es2panda::checker diff --git a/checker/ets/typeRelationContext.h b/checker/ets/typeRelationContext.h index 75d077ac612b07d66d30727cb5bf95e5326b9bce..fa4c68da86cdc53eaa4b6eb7f84182d2c9244090 100644 --- a/checker/ets/typeRelationContext.h +++ b/checker/ets/typeRelationContext.h @@ -148,7 +148,7 @@ class InstantiationContext { public: InstantiationContext(ETSChecker *checker, ETSObjectType *type, ir::TSTypeParameterInstantiation *type_args, const lexer::SourcePosition &pos) - : checker_(checker), type_arg_vars_(checker->Allocator()->Adapter()) + : checker_(checker) { ir::TSTypeParameterDeclaration *type_param_decl = nullptr; @@ -164,8 +164,7 @@ public: InstantiateType(type, type_param_decl, type_args); } - InstantiationContext(ETSChecker *checker, ETSObjectType *type, ArenaVector &type_args) - : checker_(checker), type_arg_vars_(checker->Allocator()->Adapter()) + InstantiationContext(ETSChecker *checker, ETSObjectType *type, ArenaVector &type_args) : checker_(checker) { ir::TSTypeParameterDeclaration *type_param_decl = nullptr; @@ -197,7 +196,6 @@ private: util::StringView GetHashFromTypeArguments(ArenaVector &type_arg_types); ETSChecker *checker_; - ArenaVector type_arg_vars_; ETSObjectType *result_ {}; }; diff --git a/checker/types/type.h b/checker/types/type.h index 42e2f2988c3d86d82c9f7e2de42d0dbe05b6c6ee..5893407bec941c4d06453739999a5b5e14521d1f 100644 --- a/checker/types/type.h +++ b/checker/types/type.h @@ -94,7 +94,7 @@ public: bool IsETSDynamicType() const { - return TypeFlags() == TypeFlag::ETS_DYNAMIC_TYPE; + return IsETSObjectType() && HasTypeFlag(TypeFlag::ETS_DYNAMIC_FLAG); } ETSDynamicType *AsETSDynamicType() diff --git a/test/parser/ets/promise-expected.txt b/test/parser/ets/promise-expected.txt new file mode 100644 index 0000000000000000000000000000000000000000..5a162a4eb08f12f7f7b2af92095ed022282d26e6 --- /dev/null +++ b/test/parser/ets/promise-expected.txt @@ -0,0 +1,995 @@ +{ + "type": "Program", + "statements": [ + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "P", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 7 + }, + "end": { + "line": 16, + "column": 8 + } + } + }, + "typeParameters": { + "type": "TSTypeParameterDeclaration", + "params": [ + { + "type": "TSTypeParameter", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 9 + }, + "end": { + "line": 16, + "column": 10 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 9 + }, + "end": { + "line": 16, + "column": 11 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 8 + }, + "end": { + "line": 16, + "column": 11 + } + } + }, + "superClass": null, + "implements": [], + "body": [ + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "constructor", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "kind": "constructor", + "accessibility": "public", + "static": false, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "constructor", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "Identifier", + "name": "callback", + "typeAnnotation": { + "type": "ETSFunctionType", + "params": [ + { + "type": "Identifier", + "name": "resolve", + "typeAnnotation": { + "type": "ETSFunctionType", + "params": [ + { + "type": "Identifier", + "name": "value", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 45 + }, + "end": { + "line": 17, + "column": 46 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 45 + }, + "end": { + "line": 17, + "column": 47 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 45 + }, + "end": { + "line": 17, + "column": 47 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 38 + }, + "end": { + "line": 17, + "column": 47 + } + } + } + ], + "returnType": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 17, + "column": 51 + }, + "end": { + "line": 17, + "column": 55 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 37 + }, + "end": { + "line": 17, + "column": 55 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 28 + }, + "end": { + "line": 17, + "column": 55 + } + } + } + ], + "returnType": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 17, + "column": 60 + }, + "end": { + "line": 17, + "column": 64 + } + } + } + "throwMarker": "throws", + "loc": { + "start": { + "line": 17, + "column": 27 + }, + "end": { + "line": 17, + "column": 64 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 17 + }, + "end": { + "line": 17, + "column": 64 + } + } + } + ], + "body": { + "type": "BlockStatement", + "statements": [], + "loc": { + "start": { + "line": 17, + "column": 73 + }, + "end": { + "line": 18, + "column": 6 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 16 + }, + "end": { + "line": 18, + "column": 6 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 16 + }, + "end": { + "line": 18, + "column": 6 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 18, + "column": 6 + } + } + }, + { + "type": "ClassProperty", + "key": { + "type": "Identifier", + "name": "ref", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 13 + }, + "end": { + "line": 20, + "column": 16 + } + } + }, + "accessibility": "private", + "static": false, + "readonly": false, + "declare": false, + "optional": false, + "computed": false, + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Object", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 18 + }, + "end": { + "line": 20, + "column": 24 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 18 + }, + "end": { + "line": 20, + "column": 25 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 18 + }, + "end": { + "line": 20, + "column": 25 + } + } + }, + "definite": false, + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 12 + }, + "end": { + "line": 21, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 21, + "column": 2 + } + } + }, + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "ETSGLOBAL", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "superClass": null, + "implements": [], + "body": [ + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "foo", + "decorators": [], + "loc": { + "start": { + "line": 23, + "column": 10 + }, + "end": { + "line": 23, + "column": 13 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": true, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "foo", + "decorators": [], + "loc": { + "start": { + "line": 23, + "column": 10 + }, + "end": { + "line": 23, + "column": 13 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "returnType": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "P", + "decorators": [], + "loc": { + "start": { + "line": 23, + "column": 17 + }, + "end": { + "line": 23, + "column": 18 + } + } + }, + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Object", + "decorators": [], + "loc": { + "start": { + "line": 23, + "column": 19 + }, + "end": { + "line": 23, + "column": 25 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 19 + }, + "end": { + "line": 23, + "column": 26 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 19 + }, + "end": { + "line": 23, + "column": 26 + } + } + } + ], + "loc": { + "start": { + "line": 23, + "column": 18 + }, + "end": { + "line": 23, + "column": 26 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 17 + }, + "end": { + "line": 23, + "column": 28 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 17 + }, + "end": { + "line": 23, + "column": 28 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ReturnStatement", + "argument": { + "type": "ETSNewClassInstanceExpression", + "typeReference": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "P", + "decorators": [], + "loc": { + "start": { + "line": 24, + "column": 16 + }, + "end": { + "line": 24, + "column": 17 + } + } + }, + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Object", + "decorators": [], + "loc": { + "start": { + "line": 24, + "column": 18 + }, + "end": { + "line": 24, + "column": 24 + } + } + }, + "loc": { + "start": { + "line": 24, + "column": 18 + }, + "end": { + "line": 24, + "column": 25 + } + } + }, + "loc": { + "start": { + "line": 24, + "column": 18 + }, + "end": { + "line": 24, + "column": 25 + } + } + } + ], + "loc": { + "start": { + "line": 24, + "column": 17 + }, + "end": { + "line": 24, + "column": 25 + } + } + }, + "loc": { + "start": { + "line": 24, + "column": 16 + }, + "end": { + "line": 24, + "column": 26 + } + } + }, + "loc": { + "start": { + "line": 24, + "column": 16 + }, + "end": { + "line": 24, + "column": 26 + } + } + }, + "arguments": [ + { + "type": "ArrowFunctionExpression", + "function": { + "type": "ScriptFunction", + "id": null, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "Identifier", + "name": "resolve", + "typeAnnotation": { + "type": "ETSFunctionType", + "params": [ + { + "type": "Identifier", + "name": "obj", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Object", + "decorators": [], + "loc": { + "start": { + "line": 24, + "column": 42 + }, + "end": { + "line": 24, + "column": 48 + } + } + }, + "loc": { + "start": { + "line": 24, + "column": 42 + }, + "end": { + "line": 24, + "column": 49 + } + } + }, + "loc": { + "start": { + "line": 24, + "column": 42 + }, + "end": { + "line": 24, + "column": 49 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 24, + "column": 37 + }, + "end": { + "line": 24, + "column": 49 + } + } + } + ], + "returnType": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 24, + "column": 53 + }, + "end": { + "line": 24, + "column": 57 + } + } + }, + "loc": { + "start": { + "line": 24, + "column": 36 + }, + "end": { + "line": 24, + "column": 57 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 24, + "column": 27 + }, + "end": { + "line": 24, + "column": 57 + } + } + } + ], + "returnType": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 24, + "column": 60 + }, + "end": { + "line": 24, + "column": 64 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ExpressionStatement", + "expression": { + "type": "CallExpression", + "callee": { + "type": "Identifier", + "name": "resolve", + "decorators": [], + "loc": { + "start": { + "line": 25, + "column": 9 + }, + "end": { + "line": 25, + "column": 16 + } + } + }, + "arguments": [ + { + "type": "StringLiteral", + "value": "resolved", + "loc": { + "start": { + "line": 25, + "column": 17 + }, + "end": { + "line": 25, + "column": 27 + } + } + } + ], + "optional": false, + "loc": { + "start": { + "line": 25, + "column": 9 + }, + "end": { + "line": 25, + "column": 28 + } + } + }, + "loc": { + "start": { + "line": 25, + "column": 9 + }, + "end": { + "line": 25, + "column": 29 + } + } + } + ], + "loc": { + "start": { + "line": 24, + "column": 68 + }, + "end": { + "line": 26, + "column": 6 + } + } + }, + "loc": { + "start": { + "line": 24, + "column": 26 + }, + "end": { + "line": 26, + "column": 6 + } + } + }, + "loc": { + "start": { + "line": 24, + "column": 26 + }, + "end": { + "line": 26, + "column": 6 + } + } + } + ], + "loc": { + "start": { + "line": 24, + "column": 12 + }, + "end": { + "line": 26, + "column": 8 + } + } + }, + "loc": { + "start": { + "line": 24, + "column": 5 + }, + "end": { + "line": 26, + "column": 8 + } + } + } + ], + "loc": { + "start": { + "line": 23, + "column": 27 + }, + "end": { + "line": 27, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 13 + }, + "end": { + "line": 27, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 13 + }, + "end": { + "line": 27, + "column": 2 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 23, + "column": 1 + }, + "end": { + "line": 27, + "column": 2 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 28, + "column": 1 + } + } +} diff --git a/test/parser/ets/promise.ets b/test/parser/ets/promise.ets new file mode 100644 index 0000000000000000000000000000000000000000..7e731aa24988d3445772b7f96501729e5719fe1c --- /dev/null +++ b/test/parser/ets/promise.ets @@ -0,0 +1,27 @@ +/* + * 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 P { + constructor(callback: (resolve: (value: T) => void) => void throws) { + } + + private ref: Object; +} + +function foo(): P { + return new P((resolve: (obj: Object) => void): void => { + resolve("resolved"); + }); +}