From 74d9ef90c29049e2def3dd318052631e04f2424a Mon Sep 17 00:00:00 2001 From: shawn_hu_ls Date: Sun, 12 Mar 2023 22:23:40 +0800 Subject: [PATCH] Fix some typeinfer test cases cannot passed on es2abc Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/I6ME7Y?from=project-issue Signed-off-by: shawn_hu_ls --- .../core/emitter/typeExtractorEmitter.cpp | 4 + .../test-class-with-static-field-expected.txt | 261 +++++++ .../testcases/test-class-with-static-field.ts | 20 + .../test-function-with-this-expected.txt | 711 ++++++++++++++++++ .../testcases/test-function-with-this.ts | 25 + .../testcases/test-interface-expected.txt | 325 ++++++++ .../testcases/test-interface.ts | 21 + .../testcases_with_assert/test-interface.ts | 28 + .../typescript/extractor/typeExtractor.cpp | 14 +- es2panda/typescript/extractor/typeExtractor.h | 6 +- es2panda/typescript/extractor/typeSystem.h | 9 +- 11 files changed, 1410 insertions(+), 14 deletions(-) create mode 100644 es2panda/test/type_extractor/testcases/test-class-with-static-field-expected.txt create mode 100644 es2panda/test/type_extractor/testcases/test-class-with-static-field.ts create mode 100644 es2panda/test/type_extractor/testcases/test-function-with-this-expected.txt create mode 100644 es2panda/test/type_extractor/testcases/test-function-with-this.ts create mode 100644 es2panda/test/type_extractor/testcases/test-interface-expected.txt create mode 100644 es2panda/test/type_extractor/testcases/test-interface.ts create mode 100644 es2panda/test/type_extractor/testcases_with_assert/test-interface.ts diff --git a/es2panda/compiler/core/emitter/typeExtractorEmitter.cpp b/es2panda/compiler/core/emitter/typeExtractorEmitter.cpp index d42d7a6619..c6f0caed7e 100644 --- a/es2panda/compiler/core/emitter/typeExtractorEmitter.cpp +++ b/es2panda/compiler/core/emitter/typeExtractorEmitter.cpp @@ -121,6 +121,10 @@ void TypeExtractorEmitter::GenFunctionTypeInfo(panda::pandasm::Program *prog) co DCOUT << "[LOG]" << func_->name << ": -2 | " << pg_->TypedFunc().second << std::endl; } + if (typedInsns.empty()) { + return; + } + std::string literalId = std::string(recorder->GetRecordName()) + "_" + std::to_string(literalId_--); auto literalArrayInstance = panda::pandasm::LiteralArray(std::move(typedInsns)); diff --git a/es2panda/test/type_extractor/testcases/test-class-with-static-field-expected.txt b/es2panda/test/type_extractor/testcases/test-class-with-static-field-expected.txt new file mode 100644 index 0000000000..324e1a3d9a --- /dev/null +++ b/es2panda/test/type_extractor/testcases/test-class-with-static-field-expected.txt @@ -0,0 +1,261 @@ +======> literal array buffer <====== +------------------------------------ +slot _-1 +{ + index: 0 + tag: 0 + val: 2 +}, +{ + index: 1 + tag: 2 + val: 3 +}, +{ + index: 2 + tag: 0 + val: 24 +}, +{ + index: 3 + tag: 24 + val: _1 +}, +------------------------------------ +slot _0 +{ + index: 0 + tag: 0 + val: 2 +}, +{ + index: 1 + tag: 2 + val: 1 +}, +{ + index: 2 + tag: 0 + val: 24 +}, +{ + index: 3 + tag: 24 + val: _1 +}, +{ + index: 4 + tag: 0 + val: 2 +}, +{ + index: 5 + 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: no +}, +{ + index: 12 + tag: 0 + val: 25 +}, +{ + index: 13 + tag: 25 + 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: 0 +}, +{ + index: 20 + tag: 0 + val: 2 +}, +{ + index: 21 + tag: 2 + val: 1 +}, +{ + index: 22 + tag: 0 + val: 5 +}, +{ + index: 23 + tag: 5 + val: no +}, +{ + index: 24 + tag: 0 + val: 25 +}, +{ + index: 25 + tag: 25 + val: 4 +}, +{ + index: 26 + tag: 0 + val: 2 +}, +{ + index: 27 + tag: 2 + val: 0 +}, +{ + index: 28 + tag: 0 + val: 2 +}, +{ + index: 29 + tag: 2 + val: 0 +}, +{ + index: 30 + tag: 0 + val: 2 +}, +{ + index: 31 + tag: 2 + val: 0 +}, +------------------------------------ +slot _2 +{ + 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 _3 +{ + index: 0 + tag: 0 + val: 2 +}, +{ + index: 1 + tag: 2 + val: 0 +}, diff --git a/es2panda/test/type_extractor/testcases/test-class-with-static-field.ts b/es2panda/test/type_extractor/testcases/test-class-with-static-field.ts new file mode 100644 index 0000000000..16b81e2576 --- /dev/null +++ b/es2panda/test/type_extractor/testcases/test-class-with-static-field.ts @@ -0,0 +1,20 @@ +/* + * 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 Student { + no : number = 123; + static no : string = "123"; +} diff --git a/es2panda/test/type_extractor/testcases/test-function-with-this-expected.txt b/es2panda/test/type_extractor/testcases/test-function-with-this-expected.txt new file mode 100644 index 0000000000..dbfe968418 --- /dev/null +++ b/es2panda/test/type_extractor/testcases/test-function-with-this-expected.txt @@ -0,0 +1,711 @@ +======> 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: _6 +}, +------------------------------------ +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: _3 +}, +{ + index: 4 + tag: 0 + val: 2 +}, +{ + index: 5 + tag: 2 + val: -2 +}, +{ + index: 6 + tag: 0 + val: 24 +}, +{ + index: 7 + tag: 24 + val: _5 +}, +------------------------------------ +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: _5 +}, +------------------------------------ +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: _4 +}, +------------------------------------ +slot _-5 +{ + index: 0 + tag: 0 + val: 2 +}, +{ + index: 1 + tag: 2 + val: 1 +}, +{ + index: 2 + tag: 0 + val: 24 +}, +{ + index: 3 + tag: 24 + val: _6 +}, +{ + index: 4 + tag: 0 + val: 2 +}, +{ + index: 5 + tag: 2 + val: 5 +}, +{ + index: 6 + tag: 0 + val: 24 +}, +{ + index: 7 + tag: 24 + val: _1 +}, +{ + index: 8 + tag: 0 + val: 2 +}, +{ + index: 9 + tag: 2 + val: 12 +}, +{ + index: 10 + tag: 0 + val: 24 +}, +{ + index: 11 + tag: 24 + val: _2 +}, +------------------------------------ +slot _0 +{ + index: 0 + tag: 0 + val: 2 +}, +{ + index: 1 + tag: 2 + val: 6 +}, +{ + 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: 2 +}, +{ + index: 15 + 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: 0 +}, +{ + index: 10 + tag: 0 + val: 2 +}, +{ + index: 11 + tag: 2 + val: 0 +}, +{ + index: 12 + tag: 0 + val: 2 +}, +{ + index: 13 + tag: 2 + val: 0 +}, +{ + index: 14 + tag: 0 + val: 2 +}, +{ + index: 15 + tag: 2 + val: 0 +}, +------------------------------------ +slot _2 +{ + 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: 0 +}, +{ + index: 10 + tag: 0 + val: 2 +}, +{ + index: 11 + tag: 2 + val: 1 +}, +{ + index: 12 + tag: 0 + val: 5 +}, +{ + index: 13 + tag: 5 + val: dump +}, +{ + index: 14 + tag: 0 + val: 24 +}, +{ + index: 15 + tag: 24 + val: _3 +}, +{ + index: 16 + tag: 0 + val: 2 +}, +{ + index: 17 + tag: 2 + val: 0 +}, +{ + index: 18 + tag: 0 + val: 2 +}, +{ + index: 19 + tag: 2 + 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: 0 +}, +{ + index: 4 + tag: 0 + val: 5 +}, +{ + index: 5 + tag: 5 + val: dump +}, +{ + index: 6 + tag: 0 + val: 2 +}, +{ + index: 7 + tag: 2 + val: 1 +}, +{ + index: 8 + tag: 0 + val: 24 +}, +{ + index: 9 + tag: 24 + val: _4 +}, +{ + index: 10 + tag: 0 + val: 2 +}, +{ + index: 11 + tag: 2 + val: 0 +}, +{ + index: 12 + tag: 0 + val: 25 +}, +{ + index: 13 + tag: 25 + val: 3 +}, +------------------------------------ +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: 2 +}, +{ + index: 2 + tag: 0 + val: 24 +}, +{ + index: 3 + tag: 24 + val: _2 +}, +------------------------------------ +slot _6 +{ + 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: dump +}, +{ + index: 6 + tag: 0 + val: 2 +}, +{ + index: 7 + tag: 2 + val: 1 +}, +{ + index: 8 + tag: 0 + val: 24 +}, +{ + index: 9 + tag: 24 + val: _5 +}, +{ + index: 10 + tag: 0 + val: 2 +}, +{ + index: 11 + tag: 2 + val: 0 +}, +{ + index: 12 + tag: 0 + val: 25 +}, +{ + index: 13 + tag: 25 + val: 3 +}, +------------------------------------ +slot _7 +{ + 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 _8 +{ + index: 0 + tag: 0 + val: 2 +}, +{ + index: 1 + tag: 2 + val: 0 +}, +------------------------------------ +slot _9 +{ + index: 0 + tag: 0 + val: 5 +}, +{ + index: 1 + tag: 5 + val: dump +}, +{ + index: 2 + tag: 0 + val: 6 +}, +{ + index: 3 + tag: 6 + val: .dump +}, +{ + index: 4 + tag: 0 + val: 9 +}, +{ + index: 5 + tag: 9 + val: 0 +}, +{ + index: 6 + tag: 0 + val: 2 +}, +{ + index: 7 + tag: 2 + val: 1 +}, diff --git a/es2panda/test/type_extractor/testcases/test-function-with-this.ts b/es2panda/test/type_extractor/testcases/test-function-with-this.ts new file mode 100644 index 0000000000..66f6480b66 --- /dev/null +++ b/es2panda/test/type_extractor/testcases/test-function-with-this.ts @@ -0,0 +1,25 @@ +/* + * 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 Base {} + +class Student { + dump(this : Base) : void { + } +} + +function dump(this : Student) : void { +} diff --git a/es2panda/test/type_extractor/testcases/test-interface-expected.txt b/es2panda/test/type_extractor/testcases/test-interface-expected.txt new file mode 100644 index 0000000000..e9e1fd923c --- /dev/null +++ b/es2panda/test/type_extractor/testcases/test-interface-expected.txt @@ -0,0 +1,325 @@ +======> literal array buffer <====== +------------------------------------ +slot _-1 +{ + index: 0 + tag: 0 + val: 2 +}, +{ + index: 1 + tag: 2 + val: 3 +}, +{ + index: 2 + tag: 0 + val: 24 +}, +{ + index: 3 + tag: 24 + val: _3 +}, +{ + index: 4 + tag: 0 + val: 2 +}, +{ + index: 5 + tag: 2 + val: 6 +}, +{ + index: 6 + tag: 0 + val: 24 +}, +{ + index: 7 + tag: 24 + val: _2 +}, +------------------------------------ +slot _0 +{ + 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: 24 +}, +{ + index: 5 + tag: 24 + val: _2 +}, +{ + index: 6 + tag: 0 + val: 24 +}, +{ + index: 7 + tag: 24 + val: _3 +}, +{ + index: 8 + tag: 0 + val: 2 +}, +{ + index: 9 + tag: 2 + val: 0 +}, +------------------------------------ +slot _1 +{ + index: 0 + tag: 0 + val: 2 +}, +{ + index: 1 + tag: 2 + val: 8 +}, +{ + index: 2 + tag: 0 + val: 2 +}, +{ + index: 3 + tag: 2 + val: 0 +}, +{ + index: 4 + tag: 0 + val: 2 +}, +{ + index: 5 + tag: 2 + 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: 8 +}, +{ + index: 2 + tag: 0 + val: 2 +}, +{ + index: 3 + tag: 2 + val: 1 +}, +{ + index: 4 + tag: 0 + val: 24 +}, +{ + index: 5 + tag: 24 + val: _1 +}, +{ + index: 6 + tag: 0 + val: 2 +}, +{ + index: 7 + tag: 2 + val: 0 +}, +{ + index: 8 + tag: 0 + val: 2 +}, +{ + index: 9 + tag: 2 + val: 0 +}, +------------------------------------ +slot _3 +{ + 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: 1 +}, +{ + index: 8 + tag: 0 + val: 24 +}, +{ + index: 9 + tag: 24 + val: _2 +}, +{ + index: 10 + tag: 0 + val: 2 +}, +{ + index: 11 + tag: 2 + val: 0 +}, +{ + index: 12 + tag: 0 + val: 2 +}, +{ + index: 13 + tag: 2 + val: 0 +}, +{ + index: 14 + tag: 0 + val: 2 +}, +{ + index: 15 + tag: 2 + val: 0 +}, +{ + index: 16 + tag: 0 + val: 2 +}, +{ + index: 17 + tag: 2 + val: 0 +}, +------------------------------------ +slot _4 +{ + 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 _5 +{ + index: 0 + tag: 0 + val: 2 +}, +{ + index: 1 + tag: 2 + val: 0 +}, diff --git a/es2panda/test/type_extractor/testcases/test-interface.ts b/es2panda/test/type_extractor/testcases/test-interface.ts new file mode 100644 index 0000000000..2386d3f0c3 --- /dev/null +++ b/es2panda/test/type_extractor/testcases/test-interface.ts @@ -0,0 +1,21 @@ +/* + * 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. + */ + + +interface Student {} +interface Child extends Student {} +class Member implements Child {} + +let m : Child = new Member(); diff --git a/es2panda/test/type_extractor/testcases_with_assert/test-interface.ts b/es2panda/test/type_extractor/testcases_with_assert/test-interface.ts new file mode 100644 index 0000000000..ac01e35487 --- /dev/null +++ b/es2panda/test/type_extractor/testcases_with_assert/test-interface.ts @@ -0,0 +1,28 @@ +/* + * 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; +{ + interface Student {} + interface Child extends Student {} + class Member implements Child {} + + function dump(c : Child) : void { + AssertType(c, "interface"); + c = new Member(); + AssertType(c, "Member"); + } +} diff --git a/es2panda/typescript/extractor/typeExtractor.cpp b/es2panda/typescript/extractor/typeExtractor.cpp index 543b4652ac..238672b38c 100644 --- a/es2panda/typescript/extractor/typeExtractor.cpp +++ b/es2panda/typescript/extractor/typeExtractor.cpp @@ -380,7 +380,7 @@ int64_t TypeExtractor::GetTypeIndexFromClassDefinition(const ir::AstNode *node, return typeIndex; } -int64_t TypeExtractor::GetTypeIndexFromInterfaceNode(const ir::AstNode *node, bool isNewInstance) +int64_t TypeExtractor::GetTypeIndexFromInterfaceNode(const ir::AstNode *node, [[maybe_unused]] bool isNewInstance) { int64_t typeIndex = PrimitiveType::ANY; auto fn = [&node, &typeIndex, this](const util::StringView &name) { @@ -396,10 +396,6 @@ int64_t TypeExtractor::GetTypeIndexFromInterfaceNode(const ir::AstNode *node, bo fn(std::move(DEFAULT_NAME)); } - if (isNewInstance) { - typeIndex = GetTypeIndexFromClassInst(typeIndex); - } - TLOG(node->Type(), typeIndex); return typeIndex; } @@ -511,7 +507,7 @@ int64_t TypeExtractor::GetTypeIndexFromTSQualifiedNode(const ir::AstNode *node, return typeIndex; } -int64_t TypeExtractor::GetTypeIndexFromAnnotation(const ir::Expression *typeAnnotation) +int64_t TypeExtractor::GetTypeIndexFromAnnotation(const ir::Expression *typeAnnotation, bool isNewInstance) { if (typeAnnotation == nullptr) { return PrimitiveType::ANY; @@ -588,7 +584,7 @@ int64_t TypeExtractor::GetTypeIndexFromAnnotation(const ir::Expression *typeAnno case ir::AstNodeType::TS_TYPE_QUERY: return PrimitiveType::ANY; case ir::AstNodeType::TS_TYPE_REFERENCE: { // let a : A - return GetTypeIndexFromTypeReference(typeAnnotation->AsTSTypeReference()); + return GetTypeIndexFromTypeReference(typeAnnotation->AsTSTypeReference(), isNewInstance); } default: UNREACHABLE(); @@ -754,7 +750,7 @@ int64_t TypeExtractor::GetTypeIndexFromClassInst(int64_t typeIndex) return typeIndexTmp; } -int64_t TypeExtractor::GetTypeIndexFromTypeReference(const ir::TSTypeReference *typeReference) +int64_t TypeExtractor::GetTypeIndexFromTypeReference(const ir::TSTypeReference *typeReference, bool isNewInstance) { auto typeName = typeReference->TypeName(); ASSERT(typeName != nullptr); @@ -785,7 +781,7 @@ int64_t TypeExtractor::GetTypeIndexFromTypeReference(const ir::TSTypeReference * typeIndex = GetTypeIndexFromGenericInst(GetTypeIndexFromDeclNode(declNode, false), typeReference->TypeParams()); } else { - typeIndex = GetTypeIndexFromDeclNode(declNode, true); + typeIndex = GetTypeIndexFromDeclNode(declNode, isNewInstance); } recorder_->SetIdentifierTypeIndex(identifier, recorder_->GetClassType(typeIndex)); return typeIndex; diff --git a/es2panda/typescript/extractor/typeExtractor.h b/es2panda/typescript/extractor/typeExtractor.h index 524e5f0700..e9718c134e 100644 --- a/es2panda/typescript/extractor/typeExtractor.h +++ b/es2panda/typescript/extractor/typeExtractor.h @@ -41,7 +41,7 @@ public: TypeRecorder *Recorder() const; const ir::Identifier *GetIdentifierFromExpression(const ir::Expression *expression); - int64_t GetTypeIndexFromAnnotation(const ir::Expression *typeAnnotation); + 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); @@ -79,7 +79,7 @@ private: int64_t GetTypeIndexFromIdentifierNode(const ir::AstNode *node, bool isNewInstance); int64_t GetTypeIndexFromClassExpression(const ir::AstNode *node, bool isNewInstance); int64_t GetTypeIndexFromClassDefinition(const ir::AstNode *node, bool isNewInstance); - int64_t GetTypeIndexFromInterfaceNode(const ir::AstNode *node, bool isNewInstance); + int64_t GetTypeIndexFromInterfaceNode(const ir::AstNode *node, [[maybe_unused]] bool isNewInstance); int64_t GetTypeIndexFromFunctionNode(const ir::AstNode *node, [[maybe_unused]] bool isNewInstance); int64_t GetTypeIndexFromImportNode(const ir::AstNode *node, [[maybe_unused]] bool isNewInstance); int64_t GetTypeIndexFromTypeAliasNode(const ir::AstNode *node, [[maybe_unused]] bool isNewInstance); @@ -95,7 +95,7 @@ private: void HandleTypeAliasDeclaration(const ir::AstNode *node); // Helpers - int64_t GetTypeIndexFromTypeReference(const ir::TSTypeReference *typeReference); + int64_t GetTypeIndexFromTypeReference(const ir::TSTypeReference *typeReference, bool isNewInstance); int64_t GetTypeIndexFromTSLiteralType(const ir::TSLiteralType *tsLiteralType); // Builtin and Generic Helpers diff --git a/es2panda/typescript/extractor/typeSystem.h b/es2panda/typescript/extractor/typeSystem.h index d3c1dfda53..46826dff54 100644 --- a/es2panda/typescript/extractor/typeSystem.h +++ b/es2panda/typescript/extractor/typeSystem.h @@ -446,6 +446,11 @@ public: name_ = recorder_->GetAnonymousFunctionNames(func); } FillModifier(func); + if (func->ThisParams() != nullptr) { + auto identifier = func->ThisParams()->AsIdentifier(); + paramsTypeIndex_.emplace_back(extractor_->GetTypeIndexFromIdentifier(identifier)); + containThis_ = true; + } } FillParameters(func); FillReturn(func); @@ -730,7 +735,7 @@ private: void FillMethod(const ir::MethodDefinition *method) { auto fn = [&method, this](const FunctionType &functionType, const util::StringView &name) { - if (functionType.GetModifier() == Modifier::NONSTATIC) { + if ((functionType.GetModifier() & Modifier::STATIC) == 0) { methods_[name] = recorder_->GetNodeTypeIndex(method->Function()); } else { staticMethods_[name] = recorder_->GetNodeTypeIndex(method->Function()); @@ -938,7 +943,7 @@ private: for (const auto &t : interfaceDef->Extends()) { if (t != nullptr) { // TSTypeReference - heritages_.emplace_back(extractor_->GetTypeIndexFromAnnotation(t->Expr())); + heritages_.emplace_back(extractor_->GetTypeIndexFromAnnotation(t->Expr(), false)); } } } -- Gitee