From fcc8dfc8480e9db3f7a3e9fea4b8d80478275b6a Mon Sep 17 00:00:00 2001 From: huangyu Date: Tue, 16 May 2023 15:10:07 +0800 Subject: [PATCH] Fix wrong generic class instance type recorded for non-class type Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/I6XPXW Test: es2panda/test/type_extractor/testcases_with_assert Signed-off-by: huangyu Change-Id: I0f502f5ce3091b523d06001465b0cb084a193497 --- ...st-builtin-generic-class-type-expected.txt | 371 +++++++++ .../test-builtin-generic-class-type.ts | 19 + .../test-class-generic-type-expected.txt | 769 ++++++++++++++++++ .../testcases/test-class-generic-type.ts | 26 + .../test-class-extends-constructor-type.ts | 57 ++ ...est-constructor-type-as-type-annotation.ts | 50 ++ .../typescript/extractor/typeExtractor.cpp | 35 +- es2panda/typescript/extractor/typeExtractor.h | 5 +- es2panda/typescript/extractor/typeRecorder.h | 9 + es2panda/typescript/extractor/typeSystem.h | 2 +- 10 files changed, 1327 insertions(+), 16 deletions(-) create mode 100644 es2panda/test/type_extractor/testcases/test-builtin-generic-class-type-expected.txt create mode 100644 es2panda/test/type_extractor/testcases/test-builtin-generic-class-type.ts create mode 100644 es2panda/test/type_extractor/testcases/test-class-generic-type-expected.txt create mode 100644 es2panda/test/type_extractor/testcases/test-class-generic-type.ts create mode 100644 es2panda/test/type_extractor/testcases_with_assert/test-class-extends-constructor-type.ts create mode 100644 es2panda/test/type_extractor/testcases_with_assert/test-constructor-type-as-type-annotation.ts diff --git a/es2panda/test/type_extractor/testcases/test-builtin-generic-class-type-expected.txt b/es2panda/test/type_extractor/testcases/test-builtin-generic-class-type-expected.txt new file mode 100644 index 0000000000..7a463133aa --- /dev/null +++ b/es2panda/test/type_extractor/testcases/test-builtin-generic-class-type-expected.txt @@ -0,0 +1,371 @@ +======> 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: 40 +}, +{ + index: 4 + tag: 0 + val: 2 +}, +{ + index: 5 + tag: 2 + val: 3 +}, +{ + index: 6 + tag: 0 + val: 24 +}, +{ + index: 7 + tag: 24 + val: _1 +}, +{ + index: 8 + tag: 0 + val: 2 +}, +{ + index: 9 + tag: 2 + val: 4 +}, +{ + index: 10 + tag: 0 + val: 24 +}, +{ + index: 11 + tag: 24 + val: _3 +}, +{ + index: 12 + tag: 0 + val: 2 +}, +{ + index: 13 + tag: 2 + val: 5 +}, +{ + index: 14 + tag: 0 + val: 24 +}, +{ + index: 15 + tag: 24 + val: _5 +}, +------------------------------------ +slot _0 +{ + index: 0 + tag: 0 + val: 2 +}, +{ + index: 1 + tag: 2 + val: 5 +}, +{ + index: 2 + tag: 0 + val: 24 +}, +{ + index: 3 + tag: 24 + val: _1 +}, +{ + index: 4 + tag: 0 + val: 24 +}, +{ + index: 5 + tag: 24 + val: _2 +}, +{ + index: 6 + tag: 0 + val: 24 +}, +{ + index: 7 + tag: 24 + val: _3 +}, +{ + index: 8 + tag: 0 + val: 24 +}, +{ + index: 9 + tag: 24 + val: _4 +}, +{ + index: 10 + tag: 0 + val: 24 +}, +{ + index: 11 + tag: 24 + val: _5 +}, +{ + index: 12 + tag: 0 + val: 2 +}, +{ + index: 13 + tag: 2 + val: 0 +}, +------------------------------------ +slot _1 +{ + index: 0 + tag: 0 + val: 2 +}, +{ + index: 1 + tag: 2 + val: 2 +}, +{ + index: 2 + tag: 0 + val: 25 +}, +{ + index: 3 + tag: 25 + val: 40 +}, +------------------------------------ +slot _2 +{ + index: 0 + tag: 0 + val: 2 +}, +{ + index: 1 + tag: 2 + val: 9 +}, +{ + index: 2 + tag: 0 + val: 25 +}, +{ + index: 3 + tag: 25 + val: 44 +}, +{ + index: 4 + tag: 0 + val: 2 +}, +{ + index: 5 + tag: 2 + val: 1 +}, +{ + index: 6 + tag: 0 + val: 25 +}, +{ + index: 7 + tag: 25 + val: 1 +}, +------------------------------------ +slot _3 +{ + index: 0 + tag: 0 + val: 2 +}, +{ + index: 1 + tag: 2 + val: 2 +}, +{ + index: 2 + tag: 0 + val: 24 +}, +{ + index: 3 + tag: 24 + val: _2 +}, +------------------------------------ +slot _4 +{ + index: 0 + tag: 0 + val: 2 +}, +{ + index: 1 + tag: 2 + val: 9 +}, +{ + index: 2 + tag: 0 + val: 25 +}, +{ + index: 3 + tag: 25 + val: 44 +}, +{ + index: 4 + tag: 0 + val: 2 +}, +{ + index: 5 + tag: 2 + val: 1 +}, +{ + index: 6 + tag: 0 + val: 25 +}, +{ + index: 7 + tag: 25 + val: 4 +}, +------------------------------------ +slot _5 +{ + index: 0 + tag: 0 + val: 2 +}, +{ + index: 1 + tag: 2 + val: 2 +}, +{ + index: 2 + tag: 0 + val: 24 +}, +{ + index: 3 + tag: 24 + val: _4 +}, +------------------------------------ +slot _6 +{ + 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 +}, +------------------------------------ +slot _7 +{ + index: 0 + tag: 0 + val: 2 +}, +{ + index: 1 + tag: 2 + val: 1 +}, +{ + index: 2 + tag: 0 + val: 2 +}, +{ + index: 3 + tag: 2 + val: 2 +}, +------------------------------------ +slot _8 +{ + index: 0 + tag: 0 + val: 5 +}, +{ + index: 1 + tag: 5 + val: a, b +}, diff --git a/es2panda/test/type_extractor/testcases/test-builtin-generic-class-type.ts b/es2panda/test/type_extractor/testcases/test-builtin-generic-class-type.ts new file mode 100644 index 0000000000..b07cff9e93 --- /dev/null +++ b/es2panda/test/type_extractor/testcases/test-builtin-generic-class-type.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. + */ + + +let m = new Map(); +let n: Array = [1,2]; +let s: Array = ["a, b"]; diff --git a/es2panda/test/type_extractor/testcases/test-class-generic-type-expected.txt b/es2panda/test/type_extractor/testcases/test-class-generic-type-expected.txt new file mode 100644 index 0000000000..e529d463d9 --- /dev/null +++ b/es2panda/test/type_extractor/testcases/test-class-generic-type-expected.txt @@ -0,0 +1,769 @@ +======> literal array buffer <====== +------------------------------------ +slot _-1 +{ + index: 0 + tag: 0 + val: 2 +}, +{ + index: 1 + tag: 2 + val: -1 +}, +{ + index: 2 + tag: 0 + val: 24 +}, +{ + index: 3 + tag: 24 + val: _3 +}, +{ + index: 4 + tag: 0 + val: 2 +}, +{ + index: 5 + tag: 2 + val: -2 +}, +{ + index: 6 + tag: 0 + val: 24 +}, +{ + index: 7 + tag: 24 + val: _1 +}, +------------------------------------ +slot _-2 +{ + index: 0 + tag: 0 + val: 2 +}, +{ + index: 1 + tag: 2 + val: -1 +}, +{ + index: 2 + tag: 0 + val: 24 +}, +{ + index: 3 + tag: 24 + val: _2 +}, +{ + index: 4 + tag: 0 + val: 2 +}, +{ + index: 5 + tag: 2 + val: -2 +}, +{ + index: 6 + tag: 0 + val: 24 +}, +{ + index: 7 + tag: 24 + val: _4 +}, +------------------------------------ +slot _-3 +{ + index: 0 + tag: 0 + val: 2 +}, +{ + index: 1 + tag: 2 + val: 3 +}, +{ + index: 2 + tag: 0 + val: 24 +}, +{ + index: 3 + tag: 24 + val: _1 +}, +{ + index: 4 + tag: 0 + val: 2 +}, +{ + index: 5 + tag: 2 + val: 9 +}, +{ + index: 6 + tag: 0 + val: 24 +}, +{ + index: 7 + tag: 24 + val: _6 +}, +{ + index: 8 + tag: 0 + val: 2 +}, +{ + index: 9 + tag: 2 + val: 14 +}, +{ + index: 10 + tag: 0 + val: 24 +}, +{ + index: 11 + tag: 24 + val: _8 +}, +------------------------------------ +slot _0 +{ + index: 0 + tag: 0 + val: 2 +}, +{ + index: 1 + tag: 2 + val: 8 +}, +{ + index: 2 + tag: 0 + val: 24 +}, +{ + index: 3 + tag: 24 + val: _1 +}, +{ + index: 4 + tag: 0 + val: 24 +}, +{ + index: 5 + tag: 24 + val: _2 +}, +{ + index: 6 + tag: 0 + val: 24 +}, +{ + index: 7 + tag: 24 + val: _3 +}, +{ + index: 8 + tag: 0 + val: 24 +}, +{ + index: 9 + tag: 24 + val: _4 +}, +{ + index: 10 + tag: 0 + val: 24 +}, +{ + index: 11 + tag: 24 + val: _5 +}, +{ + index: 12 + tag: 0 + val: 24 +}, +{ + index: 13 + tag: 24 + val: _6 +}, +{ + index: 14 + tag: 0 + val: 24 +}, +{ + index: 15 + tag: 24 + val: _7 +}, +{ + index: 16 + tag: 0 + val: 24 +}, +{ + index: 17 + tag: 24 + val: _8 +}, +{ + index: 18 + tag: 0 + val: 2 +}, +{ + index: 19 + tag: 2 + val: 0 +}, +------------------------------------ +slot _1 +{ + index: 0 + tag: 0 + val: 2 +}, +{ + index: 1 + tag: 2 + val: 1 +}, +{ + index: 2 + tag: 0 + val: 2 +}, +{ + index: 3 + tag: 2 + val: 0 +}, +{ + index: 4 + tag: 0 + val: 25 +}, +{ + index: 5 + tag: 25 + val: 0 +}, +{ + index: 6 + tag: 0 + val: 2 +}, +{ + index: 7 + tag: 2 + val: 0 +}, +{ + index: 8 + tag: 0 + val: 2 +}, +{ + index: 9 + tag: 2 + val: 1 +}, +{ + index: 10 + tag: 0 + val: 5 +}, +{ + index: 11 + tag: 5 + val: value +}, +{ + index: 12 + tag: 0 + val: 2 +}, +{ + index: 13 + tag: 2 + val: -1 +}, +{ + index: 14 + tag: 0 + val: 2 +}, +{ + index: 15 + tag: 2 + val: 0 +}, +{ + index: 16 + tag: 0 + val: 2 +}, +{ + index: 17 + tag: 2 + val: 0 +}, +{ + index: 18 + tag: 0 + val: 2 +}, +{ + index: 19 + tag: 2 + val: 1 +}, +{ + index: 20 + tag: 0 + val: 5 +}, +{ + index: 21 + tag: 5 + val: constructor +}, +{ + index: 22 + tag: 0 + val: 24 +}, +{ + index: 23 + tag: 24 + val: _2 +}, +{ + index: 24 + tag: 0 + val: 2 +}, +{ + index: 25 + tag: 2 + val: 0 +}, +{ + index: 26 + tag: 0 + val: 2 +}, +{ + index: 27 + tag: 2 + val: 1 +}, +{ + index: 28 + tag: 0 + val: 5 +}, +{ + index: 29 + tag: 5 + val: foo +}, +{ + index: 30 + tag: 0 + val: 24 +}, +{ + index: 31 + tag: 24 + val: _3 +}, +------------------------------------ +slot _10 +{ + index: 0 + tag: 0 + val: 5 +}, +{ + index: 1 + tag: 5 + val: foo +}, +{ + index: 2 + tag: 0 + val: 6 +}, +{ + index: 3 + tag: 6 + val: .foo +}, +{ + index: 4 + tag: 0 + val: 9 +}, +{ + index: 5 + tag: 9 + val: 0 +}, +{ + index: 6 + tag: 0 + val: 2 +}, +{ + index: 7 + tag: 2 + val: 0 +}, +------------------------------------ +slot _2 +{ + index: 0 + tag: 0 + val: 2 +}, +{ + index: 1 + tag: 2 + val: 3 +}, +{ + index: 2 + tag: 0 + val: 2 +}, +{ + index: 3 + tag: 2 + val: 0 +}, +{ + index: 4 + tag: 0 + val: 5 +}, +{ + index: 5 + tag: 5 + val: constructor +}, +{ + index: 6 + tag: 0 + val: 2 +}, +{ + index: 7 + tag: 2 + val: 0 +}, +{ + index: 8 + tag: 0 + val: 2 +}, +{ + index: 9 + tag: 2 + val: 1 +}, +{ + index: 10 + tag: 0 + val: 2 +}, +{ + index: 11 + tag: 2 + val: -1 +}, +{ + index: 12 + tag: 0 + val: 25 +}, +{ + index: 13 + tag: 25 + val: 0 +}, +------------------------------------ +slot _3 +{ + index: 0 + tag: 0 + val: 2 +}, +{ + index: 1 + tag: 2 + val: 3 +}, +{ + index: 2 + tag: 0 + val: 2 +}, +{ + index: 3 + tag: 2 + val: 4 +}, +{ + index: 4 + tag: 0 + val: 5 +}, +{ + index: 5 + tag: 5 + val: foo +}, +{ + index: 6 + tag: 0 + val: 2 +}, +{ + index: 7 + tag: 2 + val: 0 +}, +{ + index: 8 + tag: 0 + val: 2 +}, +{ + index: 9 + tag: 2 + val: 0 +}, +{ + index: 10 + tag: 0 + val: 25 +}, +{ + index: 11 + tag: 25 + val: 0 +}, +------------------------------------ +slot _4 +{ + index: 0 + tag: 0 + val: 2 +}, +{ + index: 1 + tag: 2 + val: 2 +}, +{ + index: 2 + tag: 0 + val: 24 +}, +{ + index: 3 + tag: 24 + val: _1 +}, +------------------------------------ +slot _5 +{ + index: 0 + tag: 0 + val: 2 +}, +{ + index: 1 + tag: 2 + val: 10 +}, +{ + index: 2 + tag: 0 + val: 24 +}, +{ + index: 3 + tag: 24 + val: _1 +}, +{ + index: 4 + tag: 0 + val: 2 +}, +{ + index: 5 + tag: 2 + val: 1 +}, +{ + index: 6 + tag: 0 + val: 25 +}, +{ + index: 7 + tag: 25 + val: 1 +}, +------------------------------------ +slot _6 +{ + index: 0 + tag: 0 + val: 2 +}, +{ + index: 1 + tag: 2 + val: 2 +}, +{ + index: 2 + tag: 0 + val: 24 +}, +{ + index: 3 + tag: 24 + val: _5 +}, +------------------------------------ +slot _7 +{ + index: 0 + tag: 0 + val: 2 +}, +{ + index: 1 + tag: 2 + val: 10 +}, +{ + index: 2 + tag: 0 + val: 24 +}, +{ + index: 3 + tag: 24 + val: _1 +}, +{ + index: 4 + tag: 0 + val: 2 +}, +{ + index: 5 + tag: 2 + val: 1 +}, +{ + index: 6 + tag: 0 + val: 25 +}, +{ + index: 7 + tag: 25 + val: 4 +}, +------------------------------------ +slot _8 +{ + index: 0 + tag: 0 + val: 2 +}, +{ + index: 1 + tag: 2 + val: 2 +}, +{ + index: 2 + tag: 0 + val: 24 +}, +{ + index: 3 + tag: 24 + val: _7 +}, +------------------------------------ +slot _9 +{ + 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-class-generic-type.ts b/es2panda/test/type_extractor/testcases/test-class-generic-type.ts new file mode 100644 index 0000000000..e51c1bc175 --- /dev/null +++ b/es2panda/test/type_extractor/testcases/test-class-generic-type.ts @@ -0,0 +1,26 @@ +/* + * 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 C { + value: T; + constructor(t: T) { + this.value = t; + } + static foo() {}; +} + +let c1 = new C(1); +let c2 = new C("a"); diff --git a/es2panda/test/type_extractor/testcases_with_assert/test-class-extends-constructor-type.ts b/es2panda/test/type_extractor/testcases_with_assert/test-class-extends-constructor-type.ts new file mode 100644 index 0000000000..7fdd243450 --- /dev/null +++ b/es2panda/test/type_extractor/testcases_with_assert/test-class-extends-constructor-type.ts @@ -0,0 +1,57 @@ +/* + * 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. + */ + + +declare function AssertType(value: any, type: string): void; + +type Constructor = new (...args: any[]) => T; + +interface IEventified { + emit(str: string): void; +} + +function Eventify (base: Constructor): Constructor { + AssertType(base, "any"); + class Eventified extends (base as unknown as any) {}; + return Eventified as unknown as Constructor; +} +AssertType(Eventify, "(any) => any"); + +class Base { + say() { + print('Hello'); + } +}; + +class MyClass extends Eventify(Base) { + emit(str: string): void { + print(str); + } +}; + +function test(o: MyClass): string { + AssertType(o, "MyClass"); + o.say(); + o.emit('World'); + return "OK"; +} +AssertType(test, "(MyClass) => string"); + +let myclass = new MyClass(); +AssertType(myclass, "MyClass"); +AssertType(myclass.emit, "(string) => void"); + +let ret = test(myclass); +AssertType(ret, "string"); \ No newline at end of file diff --git a/es2panda/test/type_extractor/testcases_with_assert/test-constructor-type-as-type-annotation.ts b/es2panda/test/type_extractor/testcases_with_assert/test-constructor-type-as-type-annotation.ts new file mode 100644 index 0000000000..f34a0eb2c4 --- /dev/null +++ b/es2panda/test/type_extractor/testcases_with_assert/test-constructor-type-as-type-annotation.ts @@ -0,0 +1,50 @@ +/* + * 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. + */ + + +declare function AssertType(value: any, type: string): void; + +type Ctor = new () => T; + +function classConstructorTypeTest(ctor: Ctor) +{ + const obj = new ctor(); + AssertType(ctor, "any"); + AssertType(obj, "any"); + return obj; +} + +function typeOfClassTest(ctor: typeof MyClass) +{ + const obj = new ctor; + AssertType(ctor, "any"); + AssertType(obj, "any"); + return obj; +} + +class MyClass { + public id: number = 1; + constructor() {}; +} + +let obj1 = new MyClass(); + +const myctor = MyClass; +let obj2 = classConstructorTypeTest(myctor); +let obj3 = typeOfClassTest(myctor); + +AssertType(obj1, "MyClass"); +AssertType(obj2, "any"); +AssertType(obj3, "any"); \ No newline at end of file diff --git a/es2panda/typescript/extractor/typeExtractor.cpp b/es2panda/typescript/extractor/typeExtractor.cpp index 6ec75948cf..adb6a1f29a 100644 --- a/es2panda/typescript/extractor/typeExtractor.cpp +++ b/es2panda/typescript/extractor/typeExtractor.cpp @@ -345,9 +345,6 @@ int64_t TypeExtractor::GetTypeIndexFromDeclNode(const ir::AstNode *node, bool is int64_t TypeExtractor::GetTypeIndexFromIdentifierNode(const ir::AstNode *node, bool isNewInstance) { auto typeIndex = recorder_->GetNodeTypeIndex(node); - if (isNewInstance && typeIndex != PrimitiveType::ANY) { - typeIndex = GetTypeIndexFromClassInst(typeIndex); - } TLOG(node->Type(), typeIndex); return typeIndex; } @@ -377,7 +374,7 @@ int64_t TypeExtractor::GetTypeIndexFromClassDefinition(const ir::AstNode *node, } if (isNewInstance) { - typeIndex = GetTypeIndexFromClassInst(typeIndex); + typeIndex = GetTypeIndexFromClassInst(typeIndex, node); } TLOG(node->Type(), typeIndex); @@ -624,8 +621,7 @@ int64_t TypeExtractor::GetTypeIndexFromInitializer(const ir::Expression *initial auto declNode = GetDeclNodeFromInitializer(initializer, &identifier); if (declNode != nullptr) { if (initializer->IsNewExpression() && initializer->AsNewExpression()->TypeParams() != nullptr) { - typeIndex = GetTypeIndexFromGenericInst(GetTypeIndexFromDeclNode(declNode, false), - initializer->AsNewExpression()->TypeParams()); + typeIndex = GetTypeIndexFromGenericInst(declNode, initializer->AsNewExpression()->TypeParams()); } else { typeIndex = GetTypeIndexFromDeclNode(declNode, initializer->IsNewExpression()); } @@ -782,8 +778,18 @@ void TypeExtractor::HandleNewlyGenFuncExpression(const ir::AstNode *node) } } -int64_t TypeExtractor::GetTypeIndexFromClassInst(int64_t typeIndex) +int64_t TypeExtractor::GetTypeIndexFromClassInst(int64_t typeIndex, const ir::AstNode *node, int64_t builtinTypeIndex) { + // A class instance type should point to a class type or a builtin type + ASSERT((node == nullptr) ^ (builtinTypeIndex == PrimitiveType::ANY)); + if (node && !node->IsClassDefinition()) { + return PrimitiveType::ANY; + } + + if (builtinTypeIndex != PrimitiveType::ANY && !recorder_->isBuiltinType(builtinTypeIndex)) { + return PrimitiveType::ANY; + } + auto typeIndexTmp = recorder_->GetClassInst(typeIndex); if (typeIndexTmp == PrimitiveType::ANY) { ClassInstType classInstType(this, typeIndex); @@ -820,8 +826,7 @@ int64_t TypeExtractor::GetTypeIndexFromTypeReference(const ir::TSTypeReference * if (declNode != nullptr) { int64_t typeIndex = PrimitiveType::ANY; if (typeReference->TypeParams() != nullptr) { - typeIndex = GetTypeIndexFromGenericInst(GetTypeIndexFromDeclNode(declNode, false), - typeReference->TypeParams()); + typeIndex = GetTypeIndexFromGenericInst(declNode, typeReference->TypeParams()); } else { typeIndex = GetTypeIndexFromDeclNode(declNode, isNewInstance); } @@ -853,7 +858,7 @@ int64_t TypeExtractor::GetTypeIndexFromBuiltin(const util::StringView &name, auto typeIndexBuiltin = GetBuiltinTypeIndex(name); if (typeIndexBuiltin != PrimitiveType::ANY) { if (node == nullptr) { - return GetTypeIndexFromClassInst(typeIndexBuiltin); + return GetTypeIndexFromClassInst(typeIndexBuiltin, nullptr, typeIndexBuiltin); } return GetTypeIndexFromBuiltinInst(typeIndexBuiltin, node); } @@ -874,12 +879,16 @@ int64_t TypeExtractor::GetTypeIndexFromBuiltinInst(int64_t typeIndexBuiltin, // New instance for builtin generic type BuiltinInstType builtinInstType(this, allTypes); - return GetTypeIndexFromClassInst(builtinInstType.GetTypeIndexShift()); + return GetTypeIndexFromClassInst(builtinInstType.GetTypeIndexShift(), nullptr, typeIndexBuiltin); } -int64_t TypeExtractor::GetTypeIndexFromGenericInst(int64_t typeIndexGeneric, +int64_t TypeExtractor::GetTypeIndexFromGenericInst(const ir::AstNode *declNode, const ir::TSTypeParameterInstantiation *node) { + if (!declNode->IsClassDefinition()) { + return PrimitiveType::ANY; + } + int64_t typeIndexGeneric = GetTypeIndexFromDeclNode(declNode, false); std::vector allTypes = {typeIndexGeneric}; for (const auto &t : node->Params()) { allTypes.emplace_back(GetTypeIndexFromAnnotation(t)); @@ -891,7 +900,7 @@ int64_t TypeExtractor::GetTypeIndexFromGenericInst(int64_t typeIndexGeneric, // New instance for generic type GenericInstType genericInstType(this, allTypes); - return GetTypeIndexFromClassInst(genericInstType.GetTypeIndexShift()); + return GetTypeIndexFromClassInst(genericInstType.GetTypeIndexShift(), declNode); } bool TypeExtractor::IsExportNode(const ir::AstNode *node) const diff --git a/es2panda/typescript/extractor/typeExtractor.h b/es2panda/typescript/extractor/typeExtractor.h index d0b5935502..2d3d5c6595 100644 --- a/es2panda/typescript/extractor/typeExtractor.h +++ b/es2panda/typescript/extractor/typeExtractor.h @@ -44,7 +44,8 @@ public: int64_t GetTypeIndexFromAnnotation(const ir::Expression *typeAnnotation, bool isNewInstance = true); int64_t GetTypeIndexFromIdentifier(const ir::Identifier *identifier); int64_t GetTypeIndexFromInitializer(const ir::Expression *initializer); - int64_t GetTypeIndexFromClassInst(int64_t typeIndex); + int64_t GetTypeIndexFromClassInst(int64_t typeIndex, const ir::AstNode *node, + int64_t builtinTypeIndex = TypeRecorder::PRIMITIVETYPE_ANY); void SetGenericParamTypeMap(const ArenaMap *genericParamTypeMap) { @@ -102,7 +103,7 @@ private: // Builtin and Generic Helpers int64_t GetTypeIndexFromBuiltin(const util::StringView &name, const ir::TSTypeParameterInstantiation *node); int64_t GetTypeIndexFromBuiltinInst(int64_t typeIndexBuiltin, const ir::TSTypeParameterInstantiation *node); - int64_t GetTypeIndexFromGenericInst(int64_t typeIndexGeneric, const ir::TSTypeParameterInstantiation *node); + int64_t GetTypeIndexFromGenericInst(const ir::AstNode *declNode, const ir::TSTypeParameterInstantiation *node); // Other Helpers bool IsExportNode(const ir::AstNode *node) const; diff --git a/es2panda/typescript/extractor/typeRecorder.h b/es2panda/typescript/extractor/typeRecorder.h index 465b1e51bf..dd0cede423 100644 --- a/es2panda/typescript/extractor/typeRecorder.h +++ b/es2panda/typescript/extractor/typeRecorder.h @@ -115,7 +115,16 @@ public: ALWAYS_INLINE void Dump(const parser::Program *program) const; + bool isBuiltinType(const int64_t typeIndex) + { + if (typeIndex > BUILTINTYPE_HEAD && typeIndex < USERTYPEINDEXHEAD) { + return true; + } + return false; + } + static constexpr int64_t PRIMITIVETYPE_ANY = 0; + static constexpr int64_t BUILTINTYPE_HEAD = 20; static constexpr int64_t USERTYPEINDEXHEAD = 100; private: diff --git a/es2panda/typescript/extractor/typeSystem.h b/es2panda/typescript/extractor/typeSystem.h index 361c74faec..baf5bbf8c9 100644 --- a/es2panda/typescript/extractor/typeSystem.h +++ b/es2panda/typescript/extractor/typeSystem.h @@ -790,7 +790,7 @@ private: // Create class instance type stands for 'this' if (!classDef->Abstract() && (fieldsWithInitNum_ > 0U || methodsWithBodyNum_ > 0U)) { - (void)extractor_->GetTypeIndexFromClassInst(typeIndexShift_); + (void)extractor_->GetTypeIndexFromClassInst(typeIndexShift_, classDef); } } -- Gitee