From 878e1cd84f4e2bb420368cbdc37c75a8567954aa Mon Sep 17 00:00:00 2001 From: Vivien Voros Date: Tue, 16 Jan 2024 12:06:28 +0100 Subject: [PATCH 01/19] [ArkTS frontend] overriding methods with optional parameters Overriding a method with optional parameter causes error. Fixes internal issue #15203 Change-Id: If45a44317c73c535b22505f5d87708b00b27470e Signed-off-by: Vivien Voros --- ets2panda/checker/ets/function.cpp | 2 +- .../test/parser/ets/override-expected.txt | 2543 +++++++++++++++++ ets2panda/test/parser/ets/override.ets | 25 + ets2panda/test/runtime/ets/Override-3.ets | 42 + 4 files changed, 2611 insertions(+), 1 deletion(-) create mode 100644 ets2panda/test/parser/ets/override-expected.txt create mode 100644 ets2panda/test/parser/ets/override.ets create mode 100644 ets2panda/test/runtime/ets/Override-3.ets diff --git a/ets2panda/checker/ets/function.cpp b/ets2panda/checker/ets/function.cpp index 5c9d216477..e6f781ed57 100644 --- a/ets2panda/checker/ets/function.cpp +++ b/ets2panda/checker/ets/function.cpp @@ -1255,7 +1255,7 @@ void ETSChecker::CheckOverride(Signature *signature) iter = iter->SuperType(); } - if (!isOverriding && signature->Function()->IsOverride()) { + if (!isOverriding && signature->Function()->IsOverride() && !(signature->Function()->IsDefaultParamProxy())) { ThrowTypeError({"Method ", signature->Function()->Id()->Name(), signature, " in ", signature->Owner(), " not overriding any method"}, signature->Function()->Start()); diff --git a/ets2panda/test/parser/ets/override-expected.txt b/ets2panda/test/parser/ets/override-expected.txt new file mode 100644 index 0000000000..6fb7ad9495 --- /dev/null +++ b/ets2panda/test/parser/ets/override-expected.txt @@ -0,0 +1,2543 @@ +{ + "type": "Program", + "statements": [ + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "B", + "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": "foo", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 3 + }, + "end": { + "line": 17, + "column": 6 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": false, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "foo", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 3 + }, + "end": { + "line": 17, + "column": 6 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "a", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 10 + }, + "end": { + "line": 17, + "column": 11 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 10 + }, + "end": { + "line": 17, + "column": 12 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 10 + }, + "end": { + "line": 17, + "column": 12 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 7 + }, + "end": { + "line": 17, + "column": 12 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 7 + }, + "end": { + "line": 17, + "column": 12 + } + } + }, + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "op", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 18 + }, + "end": { + "line": 17, + "column": 19 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 18 + }, + "end": { + "line": 17, + "column": 20 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 18 + }, + "end": { + "line": 17, + "column": 20 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 13 + }, + "end": { + "line": 17, + "column": 20 + } + } + }, + "initializer": { + "type": "UndefinedLiteral", + "value": undefined, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 13 + }, + "end": { + "line": 17, + "column": 20 + } + } + } + ], + "returnType": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 22 + }, + "end": { + "line": 17, + "column": 23 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 22 + }, + "end": { + "line": 17, + "column": 25 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 22 + }, + "end": { + "line": 17, + "column": 25 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ReturnStatement", + "argument": { + "type": "Identifier", + "name": "a", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 33 + }, + "end": { + "line": 17, + "column": 34 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 26 + }, + "end": { + "line": 17, + "column": 34 + } + } + } + ], + "loc": { + "start": { + "line": 17, + "column": 24 + }, + "end": { + "line": 17, + "column": 36 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 6 + }, + "end": { + "line": 17, + "column": 36 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 6 + }, + "end": { + "line": 17, + "column": 36 + } + } + }, + "overloads": [ + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "foo_proxy", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": false, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "foo_proxy", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "a", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "op", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "$proxy_mask$", + "typeAnnotation": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + } + ], + "returnType": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "IfStatement", + "test": { + "type": "BinaryExpression", + "operator": "==", + "left": { + "type": "BinaryExpression", + "operator": "&", + "left": { + "type": "BinaryExpression", + "operator": ">>", + "left": { + "type": "Identifier", + "name": "$proxy_mask$", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "right": { + "type": "NumberLiteral", + "value": 1, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "right": { + "type": "NumberLiteral", + "value": 1, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "right": { + "type": "NumberLiteral", + "value": 1, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "consequent": { + "type": "BlockStatement", + "statements": [ + { + "type": "ExpressionStatement", + "expression": { + "type": "AssignmentExpression", + "operator": "=", + "left": { + "type": "Identifier", + "name": "op", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "right": { + "type": "UndefinedLiteral", + "value": undefined, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "alternate": null, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + { + "type": "ReturnStatement", + "argument": { + "type": "CallExpression", + "callee": { + "type": "MemberExpression", + "object": { + "type": "ThisExpression", + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "property": { + "type": "Identifier", + "name": "foo", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "computed": false, + "optional": false, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "arguments": [ + { + "type": "Identifier", + "name": "a", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + { + "type": "Identifier", + "name": "op", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + } + ], + "optional": false, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 3 + } + } + } + ], + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 3 + }, + "end": { + "line": 17, + "column": 36 + } + } + }, + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "constructor", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "kind": "constructor", + "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": [], + "body": { + "type": "BlockStatement", + "statements": [], + "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": 1, + "column": 1 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 2 + }, + "end": { + "line": 18, + "column": 2 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 12 + }, + "end": { + "line": 18, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 18, + "column": 2 + } + } + }, + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 7 + }, + "end": { + "line": 19, + "column": 8 + } + } + }, + "typeParameters": { + "type": "TSTypeParameterDeclaration", + "params": [ + { + "type": "TSTypeParameter", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 9 + }, + "end": { + "line": 19, + "column": 10 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 9 + }, + "end": { + "line": 19, + "column": 11 + } + } + } + ], + "loc": { + "start": { + "line": 19, + "column": 8 + }, + "end": { + "line": 19, + "column": 11 + } + } + }, + "superClass": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "B", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 20 + }, + "end": { + "line": 19, + "column": 21 + } + } + }, + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 22 + }, + "end": { + "line": 19, + "column": 23 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 22 + }, + "end": { + "line": 19, + "column": 24 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 22 + }, + "end": { + "line": 19, + "column": 24 + } + } + } + ], + "loc": { + "start": { + "line": 19, + "column": 21 + }, + "end": { + "line": 19, + "column": 24 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 20 + }, + "end": { + "line": 19, + "column": 26 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 20 + }, + "end": { + "line": 19, + "column": 26 + } + } + }, + "implements": [], + "body": [ + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "foo", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 12 + }, + "end": { + "line": 20, + "column": 15 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": false, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "foo", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 12 + }, + "end": { + "line": 20, + "column": 15 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "a", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 19 + }, + "end": { + "line": 20, + "column": 20 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 19 + }, + "end": { + "line": 20, + "column": 21 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 19 + }, + "end": { + "line": 20, + "column": 21 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 16 + }, + "end": { + "line": 20, + "column": 21 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 16 + }, + "end": { + "line": 20, + "column": 21 + } + } + }, + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "op", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 27 + }, + "end": { + "line": 20, + "column": 28 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 27 + }, + "end": { + "line": 20, + "column": 29 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 27 + }, + "end": { + "line": 20, + "column": 29 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 22 + }, + "end": { + "line": 20, + "column": 29 + } + } + }, + "initializer": { + "type": "UndefinedLiteral", + "value": undefined, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 22 + }, + "end": { + "line": 20, + "column": 29 + } + } + } + ], + "returnType": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 31 + }, + "end": { + "line": 20, + "column": 32 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 31 + }, + "end": { + "line": 20, + "column": 34 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 31 + }, + "end": { + "line": 20, + "column": 34 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ReturnStatement", + "argument": { + "type": "Identifier", + "name": "a", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 42 + }, + "end": { + "line": 20, + "column": 43 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 35 + }, + "end": { + "line": 20, + "column": 43 + } + } + } + ], + "loc": { + "start": { + "line": 20, + "column": 33 + }, + "end": { + "line": 20, + "column": 45 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 15 + }, + "end": { + "line": 20, + "column": 45 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 15 + }, + "end": { + "line": 20, + "column": 45 + } + } + }, + "overloads": [ + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "foo_proxy", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": false, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "foo_proxy", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "a", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "op", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "$proxy_mask$", + "typeAnnotation": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + } + ], + "returnType": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "T", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "IfStatement", + "test": { + "type": "BinaryExpression", + "operator": "==", + "left": { + "type": "BinaryExpression", + "operator": "&", + "left": { + "type": "BinaryExpression", + "operator": ">>", + "left": { + "type": "Identifier", + "name": "$proxy_mask$", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "right": { + "type": "NumberLiteral", + "value": 1, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "right": { + "type": "NumberLiteral", + "value": 1, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "right": { + "type": "NumberLiteral", + "value": 1, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "consequent": { + "type": "BlockStatement", + "statements": [ + { + "type": "ExpressionStatement", + "expression": { + "type": "AssignmentExpression", + "operator": "=", + "left": { + "type": "Identifier", + "name": "op", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "right": { + "type": "UndefinedLiteral", + "value": undefined, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "alternate": null, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + { + "type": "ReturnStatement", + "argument": { + "type": "CallExpression", + "callee": { + "type": "MemberExpression", + "object": { + "type": "ThisExpression", + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "property": { + "type": "Identifier", + "name": "foo", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "computed": false, + "optional": false, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "arguments": [ + { + "type": "Identifier", + "name": "a", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + { + "type": "Identifier", + "name": "op", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + } + ], + "optional": false, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 3 + } + } + } + ], + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 3 + }, + "end": { + "line": 20, + "column": 45 + } + } + }, + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "constructor", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "kind": "constructor", + "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": [], + "body": { + "type": "BlockStatement", + "statements": [], + "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": 1, + "column": 1 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 2 + }, + "end": { + "line": 21, + "column": 2 + } + } + } + ], + "loc": { + "start": { + "line": 19, + "column": 25 + }, + "end": { + "line": 21, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 19, + "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": "_$init$_", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": true, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "_$init$_", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "statements": [], + "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": 1, + "column": 1 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "main", + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 10 + }, + "end": { + "line": 22, + "column": 14 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": true, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "main", + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 10 + }, + "end": { + "line": 22, + "column": 14 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "statements": [], + "loc": { + "start": { + "line": 22, + "column": 16 + }, + "end": { + "line": 24, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 14 + }, + "end": { + "line": 24, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 14 + }, + "end": { + "line": 24, + "column": 2 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 1 + }, + "end": { + "line": 24, + "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": 26, + "column": 1 + } + } +} diff --git a/ets2panda/test/parser/ets/override.ets b/ets2panda/test/parser/ets/override.ets new file mode 100644 index 0000000000..90460dc700 --- /dev/null +++ b/ets2panda/test/parser/ets/override.ets @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2024 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 B { + foo(a: T, op?: T): T { return a } +} +class A extends B { + override foo(a: T, op?: T): T { return a } +} +function main(){ + +} + diff --git a/ets2panda/test/runtime/ets/Override-3.ets b/ets2panda/test/runtime/ets/Override-3.ets new file mode 100644 index 0000000000..f3294dc066 --- /dev/null +++ b/ets2panda/test/runtime/ets/Override-3.ets @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2024 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 B { + foo(a: int, op?: int): int { return a } +} +class A extends B { + override foo(a: int, op?: int): int { return a } +} + +class C { + foo(a: T, op?: T): T { return a } +} +class D extends C { + override foo(a: T, op?: T): T { return a } +} + +function main(){ + let a = new A(); + let b = new B(); + let c = new C(); + let d = new D(); + + assert(b.foo(5) == a.foo(5)); + assert(b.foo(6) != a.foo(5)); + assert(b.foo(5) != a.foo(6)); + assert(c.foo("x") == d.foo("x")); + assert(c.foo("x") != d.foo("y")); + assert(c.foo("y") != d.foo("x")); +} -- Gitee From b1c6df77bb6907548a535e99717e9364620dde7b Mon Sep 17 00:00:00 2001 From: Anna Antipina Date: Thu, 11 Jan 2024 18:12:52 +0300 Subject: [PATCH 02/19] Title: Implement string constructor for NullishType Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/I8VHWY Description: enable the ability to create 'new String(NullishType)' Test: ${ARK_SOURCE_DIR}/tests/tests-u-runner/runner.sh ${ARK_SOURCE_DIR} --ets-runtime --build-dir="${ARK_BUILD_DIR}" --heap-verifier="fail_on_verification:pre:into:before_g1_concurrent:post" --timeout=30 --force-generate --test-file string_from_nullish.ets Signed-off-by: Anna Antipina --- ets2panda/compiler/core/ETSGen.h | 56 ++++++++++++++++++- ets2panda/compiler/scripts/signatures.yaml | 12 ++++ .../test/runtime/ets/string_from_nullish.ets | 38 +++++++++++++ 3 files changed, 105 insertions(+), 1 deletion(-) create mode 100644 ets2panda/test/runtime/ets/string_from_nullish.ets diff --git a/ets2panda/compiler/core/ETSGen.h b/ets2panda/compiler/core/ETSGen.h index 456d8c01f5..b1c5907351 100644 --- a/ets2panda/compiler/core/ETSGen.h +++ b/ets2panda/compiler/core/ETSGen.h @@ -983,13 +983,67 @@ private: } } + template + bool ResolveStringFromNullishBuiltin(const ir::AstNode *node, checker::Signature *signature, + const ArenaVector &arguments) + { + if (signature->InternalName() != Signatures::BUILTIN_STRING_FROM_NULLISH_CTOR) { + return false; + } + auto argExpr = arguments[0]; + if (argExpr->IsExpression()) { + if (argExpr->AsExpression()->IsNullLiteral()) { + LoadAccumulatorString(node, "null"); + return true; + } + if (argExpr->AsExpression()->IsUndefinedLiteral()) { + LoadAccumulatorString(node, "undefined"); + return true; + } + } + + Label *isNull = AllocLabel(); + Label *end = AllocLabel(); +#ifdef PANDA_WITH_ETS + Label *isUndefined = AllocLabel(); +#endif + COMPILE_ARG(0); + LoadAccumulator(node, arg0); + if (argExpr->TsType()->IsNullish()) { + BranchIfNull(node, isNull); +#ifdef PANDA_WITH_ETS + Sa().Emit(node); + BranchIfTrue(node, isUndefined); +#endif + } + LoadAccumulator(node, arg0); + CastToString(node); + StoreAccumulator(node, arg0); + Ra().Emit(node, Signatures::BUILTIN_STRING_FROM_STRING_CTOR, arg0, dummyReg_); + JumpTo(node, end); + if (argExpr->TsType()->IsNullish()) { + SetLabel(node, isNull); + LoadAccumulatorString(node, "null"); +#ifdef PANDA_WITH_ETS + JumpTo(node, end); + SetLabel(node, isUndefined); + LoadAccumulatorString(node, "undefined"); +#endif + } + SetLabel(node, end); + return true; + } + template void CallImpl(const ir::AstNode *node, checker::Signature *signature, const ArenaVector &arguments) { RegScope rs(this); - const auto name = signature->InternalName(); + if (ResolveStringFromNullishBuiltin(node, signature, arguments)) { + return; + } + const auto name = signature->InternalName(); switch (arguments.size()) { case 0U: { Ra().Emit(node, name, dummyReg_, dummyReg_); diff --git a/ets2panda/compiler/scripts/signatures.yaml b/ets2panda/compiler/scripts/signatures.yaml index 37f0738757..c079795062 100644 --- a/ets2panda/compiler/scripts/signatures.yaml +++ b/ets2panda/compiler/scripts/signatures.yaml @@ -304,6 +304,18 @@ signatures: return_type: PRIMITIVE_VOID ref: BUILTIN_OBJECT_CTOR + - callee: BUILTIN_STRING + method_name: $CTOR + params: [BUILTIN_STRING] + return_type: PRIMITIVE_VOID + ref: BUILTIN_STRING_FROM_STRING_CTOR + + - callee: BUILTIN_STRING + method_name: $CTOR + params: [BUILTIN_OBJECT] + return_type: PRIMITIVE_VOID + ref: BUILTIN_STRING_FROM_NULLISH_CTOR + - callee: BUILTIN_OBJECT method_name: toString params: [] diff --git a/ets2panda/test/runtime/ets/string_from_nullish.ets b/ets2panda/test/runtime/ets/string_from_nullish.ets new file mode 100644 index 0000000000..b2aae3aa50 --- /dev/null +++ b/ets2panda/test/runtime/ets/string_from_nullish.ets @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2023-2024 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. + */ + +function main(): int { + let z: NullishType = new Number(1); + let z1 = undefined; + let z2 = null; + let z3: Object = new String("0"); + let z4: Object = new Number(1); + let x = new String(z) + let x1 = new String(z1) + let x2 = new String(z2) + let x3 = new String(z3) + let x4 = new String(z4) + let x5 = new String(null) + let x6 = new String(undefined) + let actual: String[] = [new String(z), new String(z1), new String(z2), new String(z3), new String(z4), new String(null), new String(undefined)] + let expected: String[] = ["1", "undefined", "null", "0", "1", "null", "undefined"] + for (let i = 0; i < expected.length; i++) { + if (actual[i] != expected[i]) { + console.log("Failed: actual is \"" + actual[i] + "\" expected is \"" + expected[i] + "\"") + return 1; + } + } + return 0; +} -- Gitee From 4bed064d9817566a67ab373f86d115ddbbdca2a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ferenc=20Horv=C3=A1th?= Date: Wed, 17 Jan 2024 16:22:37 +0100 Subject: [PATCH 03/19] Fix the instantiation of tuple types MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes internal issue #15207 Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/I8XAME Testing: All required pre-merge tests passed. Results are available in the ggwatcher. Signed-off-by: Ferenc Horváth Change-Id: I07666818f493ea8f61f75b04bb95cdbc47a68e32 --- ets2panda/checker/types/ets/etsTupleType.cpp | 3 +- .../compiler/ets/nullableTuple-expected.txt | 797 ++++++++++++++++++ ets2panda/test/compiler/ets/nullableTuple.ets | 24 + 3 files changed, 823 insertions(+), 1 deletion(-) create mode 100644 ets2panda/test/compiler/ets/nullableTuple-expected.txt create mode 100644 ets2panda/test/compiler/ets/nullableTuple.ets diff --git a/ets2panda/checker/types/ets/etsTupleType.cpp b/ets2panda/checker/types/ets/etsTupleType.cpp index 99afd7bf64..aaaf104a04 100644 --- a/ets2panda/checker/types/ets/etsTupleType.cpp +++ b/ets2panda/checker/types/ets/etsTupleType.cpp @@ -185,7 +185,8 @@ void ETSTupleType::Cast(TypeRelation *const relation, Type *const target) Type *ETSTupleType::Instantiate([[maybe_unused]] ArenaAllocator *allocator, [[maybe_unused]] TypeRelation *relation, [[maybe_unused]] GlobalTypesHolder *globalTypes) { - return this; + return allocator->New(GetTupleTypesList(), + ElementType()->Instantiate(allocator, relation, globalTypes), GetSpreadType()); } } // namespace panda::es2panda::checker diff --git a/ets2panda/test/compiler/ets/nullableTuple-expected.txt b/ets2panda/test/compiler/ets/nullableTuple-expected.txt new file mode 100644 index 0000000000..6cd2174f4a --- /dev/null +++ b/ets2panda/test/compiler/ets/nullableTuple-expected.txt @@ -0,0 +1,797 @@ +{ + "type": "Program", + "statements": [ + { + "type": "TSTypeAliasDeclaration", + "id": { + "type": "Identifier", + "name": "TNumberStringPair", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 6 + }, + "end": { + "line": 16, + "column": 23 + } + } + }, + "typeAnnotation": { + "type": "ETSTuple", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "number", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 27 + }, + "end": { + "line": 16, + "column": 33 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 27 + }, + "end": { + "line": 16, + "column": 34 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 27 + }, + "end": { + "line": 16, + "column": 34 + } + } + }, + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "string", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 35 + }, + "end": { + "line": 16, + "column": 41 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 35 + }, + "end": { + "line": 16, + "column": 42 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 35 + }, + "end": { + "line": 16, + "column": 42 + } + } + } + ], + "spreadType": null, + "loc": { + "start": { + "line": 16, + "column": 26 + }, + "end": { + "line": 16, + "column": 43 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 16, + "column": 43 + } + } + }, + { + "type": "TSTypeAliasDeclaration", + "id": { + "type": "Identifier", + "name": "TTupleUnion", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 6 + }, + "end": { + "line": 17, + "column": 17 + } + } + }, + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "TNumberStringPair", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 20 + }, + "end": { + "line": 17, + "column": 37 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 20 + }, + "end": { + "line": 17, + "column": 38 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 20 + }, + "end": { + "line": 17, + "column": 38 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 1 + }, + "end": { + "line": 17, + "column": 48 + } + } + }, + { + "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": "_$init$_", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": true, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "_$init$_", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "statements": [], + "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": 1, + "column": 1 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "main", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 10 + }, + "end": { + "line": 19, + "column": 14 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": true, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "main", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 10 + }, + "end": { + "line": 19, + "column": 14 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "returnType": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "void", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 18 + }, + "end": { + "line": 19, + "column": 22 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 18 + }, + "end": { + "line": 19, + "column": 24 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 18 + }, + "end": { + "line": 19, + "column": 24 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "x", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "TNumberStringPair", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 11 + }, + "end": { + "line": 20, + "column": 28 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 11 + }, + "end": { + "line": 20, + "column": 30 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 11 + }, + "end": { + "line": 20, + "column": 30 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 8 + }, + "end": { + "line": 20, + "column": 9 + } + } + }, + "init": { + "type": "ArrayExpression", + "elements": [ + { + "type": "NumberLiteral", + "value": 12, + "loc": { + "start": { + "line": 20, + "column": 32 + }, + "end": { + "line": 20, + "column": 36 + } + } + }, + { + "type": "StringLiteral", + "value": "Hello World", + "loc": { + "start": { + "line": 20, + "column": 38 + }, + "end": { + "line": 20, + "column": 51 + } + } + } + ], + "loc": { + "start": { + "line": 20, + "column": 31 + }, + "end": { + "line": 20, + "column": 52 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 8 + }, + "end": { + "line": 20, + "column": 52 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 20, + "column": 4 + }, + "end": { + "line": 20, + "column": 53 + } + } + }, + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "y", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "TTupleUnion", + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 11 + }, + "end": { + "line": 21, + "column": 22 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 11 + }, + "end": { + "line": 21, + "column": 24 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 11 + }, + "end": { + "line": 21, + "column": 24 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 8 + }, + "end": { + "line": 21, + "column": 9 + } + } + }, + "init": { + "type": "UndefinedLiteral", + "value": undefined, + "loc": { + "start": { + "line": 21, + "column": 25 + }, + "end": { + "line": 21, + "column": 34 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 8 + }, + "end": { + "line": 21, + "column": 34 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 21, + "column": 4 + }, + "end": { + "line": 21, + "column": 35 + } + } + }, + { + "type": "AssertStatement", + "test": { + "type": "BinaryExpression", + "operator": "==", + "left": { + "type": "MemberExpression", + "object": { + "type": "Identifier", + "name": "x", + "decorators": [], + "loc": { + "start": { + "line": 23, + "column": 11 + }, + "end": { + "line": 23, + "column": 12 + } + } + }, + "property": { + "type": "NumberLiteral", + "value": 0, + "loc": { + "start": { + "line": 23, + "column": 13 + }, + "end": { + "line": 23, + "column": 14 + } + } + }, + "computed": true, + "optional": false, + "loc": { + "start": { + "line": 23, + "column": 11 + }, + "end": { + "line": 23, + "column": 15 + } + } + }, + "right": { + "type": "NumberLiteral", + "value": 12, + "loc": { + "start": { + "line": 23, + "column": 19 + }, + "end": { + "line": 23, + "column": 23 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 11 + }, + "end": { + "line": 23, + "column": 23 + } + } + }, + "second": null, + "loc": { + "start": { + "line": 23, + "column": 4 + }, + "end": { + "line": 23, + "column": 24 + } + } + } + ], + "loc": { + "start": { + "line": 19, + "column": 23 + }, + "end": { + "line": 24, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 14 + }, + "end": { + "line": 24, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 14 + }, + "end": { + "line": 24, + "column": 2 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 1 + }, + "end": { + "line": 24, + "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": 25, + "column": 1 + } + } +} diff --git a/ets2panda/test/compiler/ets/nullableTuple.ets b/ets2panda/test/compiler/ets/nullableTuple.ets new file mode 100644 index 0000000000..7891b82c56 --- /dev/null +++ b/ets2panda/test/compiler/ets/nullableTuple.ets @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2024 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. + */ + +type TNumberStringPair = [number, string]; +type TTupleUnion = TNumberStringPair|undefined; + +function main(): void { + let x: TNumberStringPair = [12.0, "Hello World"]; + let y: TTupleUnion = undefined; + + assert x[0] == 12.0; +} -- Gitee From 2e9bc214dec0302cf050a329e657c68902229462 Mon Sep 17 00:00:00 2001 From: Martin Sajti Date: Fri, 5 Jan 2024 15:53:39 +0100 Subject: [PATCH 04/19] Get enum value, when used with '&' operator in binary expression Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/I8UVCM Internal issue: #15017 Test: build, new runtime test Signed-off-by: Martin Sajti --- ets2panda/checker/ets/arithmetic.cpp | 13 ++++++++ ets2panda/compiler/core/ETSGen.cpp | 7 ++++ ets2panda/ir/astNode.h | 2 +- ets2panda/ir/astNodeFlags.h | 1 + ets2panda/test/runtime/ets/Enum6.ets | 50 ++++++++++++++++++++++++++++ 5 files changed, 72 insertions(+), 1 deletion(-) create mode 100644 ets2panda/test/runtime/ets/Enum6.ets diff --git a/ets2panda/checker/ets/arithmetic.cpp b/ets2panda/checker/ets/arithmetic.cpp index 66b26f3539..46b8207153 100644 --- a/ets2panda/checker/ets/arithmetic.cpp +++ b/ets2panda/checker/ets/arithmetic.cpp @@ -291,6 +291,19 @@ checker::Type *ETSChecker::CheckBinaryOperatorBitwise(ir::Expression *left, ir:: bool isEqualOp, checker::Type *const leftType, checker::Type *const rightType, Type *unboxedL, Type *unboxedR) { + // NOTE (mmartin): These need to be done for other binary expressions, but currently it's not defined precisely when + // to apply this conversion + + if (leftType->IsETSEnumType()) { + left->AddAstNodeFlags(ir::AstNodeFlags::ENUM_GET_VALUE); + unboxedL = GlobalIntType(); + } + + if (rightType->IsETSEnumType()) { + right->AddAstNodeFlags(ir::AstNodeFlags::ENUM_GET_VALUE); + unboxedR = GlobalIntType(); + } + if (leftType->IsETSUnionType() || rightType->IsETSUnionType()) { ThrowTypeError("Bad operand type, unions are not allowed in binary expressions except equality.", pos); } diff --git a/ets2panda/compiler/core/ETSGen.cpp b/ets2panda/compiler/core/ETSGen.cpp index c659e48884..d0291d7da0 100644 --- a/ets2panda/compiler/core/ETSGen.cpp +++ b/ets2panda/compiler/core/ETSGen.cpp @@ -982,6 +982,13 @@ void ETSGen::ApplyConversion(const ir::AstNode *node, const checker::Type *targe { auto ttctx = TargetTypeContext(this, targetType); + if (node->HasAstNodeFlags(ir::AstNodeFlags::ENUM_GET_VALUE)) { + Ra().Emit( + node, node->AsExpression()->TsType()->AsETSEnumType()->GetValueMethod().globalSignature->InternalName(), + dummyReg_, 0); + node->RemoveAstNodeFlags(ir::AstNodeFlags::ENUM_GET_VALUE); + } + if ((node->GetBoxingUnboxingFlags() & ir::BoxingUnboxingFlags::BOXING_FLAG) != 0U) { ApplyBoxingConversion(node); return; diff --git a/ets2panda/ir/astNode.h b/ets2panda/ir/astNode.h index f6eedfb88e..4b20f93db3 100644 --- a/ets2panda/ir/astNode.h +++ b/ets2panda/ir/astNode.h @@ -450,7 +450,7 @@ public: { \ return ((member_name)&flag) != 0U; \ } \ - void Remove##flag_type(flag_type flag) noexcept \ + void Remove##flag_type(flag_type flag) const noexcept \ { \ (member_name) &= ~flag; \ } diff --git a/ets2panda/ir/astNodeFlags.h b/ets2panda/ir/astNodeFlags.h index f6420bb131..fb74be021b 100644 --- a/ets2panda/ir/astNodeFlags.h +++ b/ets2panda/ir/astNodeFlags.h @@ -22,6 +22,7 @@ namespace panda::es2panda::ir { enum class AstNodeFlags { NO_OPTS = 0, CHECKCAST = 1U << 0U, + ENUM_GET_VALUE = 1U << 1U, }; enum class ModifierFlags : uint32_t { diff --git a/ets2panda/test/runtime/ets/Enum6.ets b/ets2panda/test/runtime/ets/Enum6.ets new file mode 100644 index 0000000000..39b8f0c5db --- /dev/null +++ b/ets2panda/test/runtime/ets/Enum6.ets @@ -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. + */ + +enum Access { + READ = 1, + WRITE = 2, + READWRITE = 3 +} + +enum TestEnum { + A = 144, + B, + C = 795 +} + +function main(): void { + test_access_enum_bitwise(Access.WRITE); + assert (Access.WRITE & Access.READWRITE) == 2; + + test_test_enum_bitwise(TestEnum.C); + assert (TestEnum.B & TestEnum.A) == 144 + +} + +function test_access_enum_bitwise(enum_val: Access): void { + assert (enum_val & Access.READ) == 0 + assert (enum_val & Access.WRITE) == 2 + assert (enum_val | Access.READ) == 3 + assert (enum_val | Access.READWRITE) == 3 + assert (enum_val & Access.READWRITE) == 2 +} + +function test_test_enum_bitwise(enum_val: TestEnum): void { + assert (enum_val & TestEnum.A) == 16 + assert (enum_val | TestEnum.A) == 923 + assert (enum_val & TestEnum.B) == 17 + assert (enum_val | TestEnum.B) == 923 +} -- Gitee From 7c1495066cf577dd15fa8004bd6b05b8da54d41c Mon Sep 17 00:00:00 2001 From: Martin Sajti Date: Wed, 10 Jan 2024 14:13:19 +0100 Subject: [PATCH 05/19] Tuple instantiation and LUB for type parameters Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/I8VHL5 Internal issue: #15174, #15184 Test: build, new checker test, new runtime test Signed-off-by: Martin Sajti --- ets2panda/checker/ETSAnalyzer.cpp | 2 +- ets2panda/checker/types/ets/etsObjectType.cpp | 51 +- ets2panda/checker/types/ets/etsObjectType.h | 1 + ets2panda/checker/types/ets/etsTupleType.cpp | 17 + ets2panda/checker/types/ets/etsTupleType.h | 1 + ets2panda/ir/ets/etsTuple.cpp | 84 +- ets2panda/ir/ets/etsTuple.h | 6 +- ets2panda/ir/expressions/memberExpression.cpp | 7 - .../compiler/ets/tuple_types_19-expected.txt | 2271 +++++++++++++++++ .../test/compiler/ets/tuple_types_19.ets | 31 + .../test/runtime/ets/tuple_types_runtime.ets | 11 + 11 files changed, 2410 insertions(+), 72 deletions(-) create mode 100644 ets2panda/test/compiler/ets/tuple_types_19-expected.txt create mode 100644 ets2panda/test/compiler/ets/tuple_types_19.ets diff --git a/ets2panda/checker/ETSAnalyzer.cpp b/ets2panda/checker/ETSAnalyzer.cpp index 653bcf2c2e..77a8d79564 100644 --- a/ets2panda/checker/ETSAnalyzer.cpp +++ b/ets2panda/checker/ETSAnalyzer.cpp @@ -725,7 +725,7 @@ checker::Type *ETSAnalyzer::Check(ir::ArrayExpression *expr) const currentElement->Start()); } - const checker::CastingContext cast( + checker::AssignmentContext( checker->Relation(), currentElement, elementType, compareType, currentElement->Start(), {"Array initializer's type is not assignable to tuple type at index: ", idx}); diff --git a/ets2panda/checker/types/ets/etsObjectType.cpp b/ets2panda/checker/types/ets/etsObjectType.cpp index 4dfa0dba6f..17cbff5b6f 100644 --- a/ets2panda/checker/types/ets/etsObjectType.cpp +++ b/ets2panda/checker/types/ets/etsObjectType.cpp @@ -604,46 +604,57 @@ void ETSObjectType::Cast(TypeRelation *const relation, Type *const target) conversion::Forbidden(relation); } -void ETSObjectType::IsSupertypeOf(TypeRelation *relation, Type *source) +bool ETSObjectType::DefaultObjectTypeChecks(const ETSChecker *const etsChecker, TypeRelation *const relation, + Type *const source) { - relation->Result(false); - auto *const etsChecker = relation->GetChecker()->AsETSChecker(); - - if (source->IsETSUnionType()) { - bool res = std::all_of(source->AsETSUnionType()->ConstituentTypes().begin(), - source->AsETSUnionType()->ConstituentTypes().end(), [this, relation](Type *ct) { - relation->Result(false); - IsSupertypeOf(relation, ct); - return relation->IsTrue(); - }); - relation->Result(res); - return; - } - // 3.8.3 Subtyping among Array Types auto const *const base = GetConstOriginalBaseType(); if (base == etsChecker->GlobalETSObjectType() && source->IsETSArrayType()) { relation->Result(true); - return; + return true; + } + + if (source->IsETSTypeParameter()) { + IsSupertypeOf(relation, source->AsETSTypeParameter()->GetConstraintType()); + return true; } if (!source->IsETSObjectType() || !source->AsETSObjectType()->HasObjectFlag(ETSObjectFlags::CLASS | ETSObjectFlags::INTERFACE | ETSObjectFlags::NULL_TYPE)) { - return; + return true; } if ((!ContainsNull() && source->ContainsNull()) || (!ContainsUndefined() && source->ContainsUndefined())) { - return; + return true; } // All classes and interfaces are subtypes of Object if (base == etsChecker->GlobalETSObjectType() || base == etsChecker->GlobalETSNullishObjectType()) { relation->Result(true); - return; + return true; } IdenticalUptoNullability(relation, source); - if (relation->IsTrue()) { + return relation->IsTrue(); +} + +void ETSObjectType::IsSupertypeOf(TypeRelation *relation, Type *source) +{ + relation->Result(false); + auto *const etsChecker = relation->GetChecker()->AsETSChecker(); + + if (source->IsETSUnionType()) { + bool res = std::all_of(source->AsETSUnionType()->ConstituentTypes().begin(), + source->AsETSUnionType()->ConstituentTypes().end(), [this, relation](Type *ct) { + relation->Result(false); + IsSupertypeOf(relation, ct); + return relation->IsTrue(); + }); + relation->Result(res); + return; + } + + if (DefaultObjectTypeChecks(etsChecker, relation, source)) { return; } diff --git a/ets2panda/checker/types/ets/etsObjectType.h b/ets2panda/checker/types/ets/etsObjectType.h index 548bacce30..464754a587 100644 --- a/ets2panda/checker/types/ets/etsObjectType.h +++ b/ets2panda/checker/types/ets/etsObjectType.h @@ -482,6 +482,7 @@ public: Type *Substitute(TypeRelation *relation, const Substitution *substitution) override; void Cast(TypeRelation *relation, Type *target) override; bool CastNumericObject(TypeRelation *relation, Type *target); + bool DefaultObjectTypeChecks(const ETSChecker *etsChecker, TypeRelation *relation, Type *source); void IsSupertypeOf(TypeRelation *relation, Type *source) override; Type *AsSuper(Checker *checker, varbinder::Variable *sourceVar) override; diff --git a/ets2panda/checker/types/ets/etsTupleType.cpp b/ets2panda/checker/types/ets/etsTupleType.cpp index aaaf104a04..08e74f9828 100644 --- a/ets2panda/checker/types/ets/etsTupleType.cpp +++ b/ets2panda/checker/types/ets/etsTupleType.cpp @@ -14,7 +14,10 @@ */ #include "etsTupleType.h" + +#include "checker/ETSchecker.h" #include "checker/ets/conversion.h" +#include "ir/ets/etsTuple.h" namespace panda::es2panda::checker { void ETSTupleType::ToString(std::stringstream &ss) const @@ -129,6 +132,20 @@ void ETSTupleType::AssignmentTarget(TypeRelation *const relation, Type *const so relation->Result(true); } +Type *ETSTupleType::Substitute(TypeRelation *relation, const Substitution *substitution) +{ + auto *const checker = relation->GetChecker()->AsETSChecker(); + ArenaVector newTypeList(checker->Allocator()->Adapter()); + + for (auto *const tupleTypeListElement : GetTupleTypesList()) { + newTypeList.emplace_back(tupleTypeListElement->Substitute(relation, substitution)); + } + + auto *newSpreadType = spreadType_ == nullptr ? nullptr : spreadType_->Substitute(relation, substitution); + auto *newElementType = ir::ETSTuple::CalculateLUBForTuple(checker, newTypeList, newSpreadType); + return checker->Allocator()->New(std::move(newTypeList), newElementType, newSpreadType); +} + void ETSTupleType::Cast(TypeRelation *const relation, Type *const target) { // NOTE(mmartin): Might be not the correct casting rules, as these aren't defined yet diff --git a/ets2panda/checker/types/ets/etsTupleType.h b/ets2panda/checker/types/ets/etsTupleType.h index 7e84e5f87a..249dbe58cf 100644 --- a/ets2panda/checker/types/ets/etsTupleType.h +++ b/ets2panda/checker/types/ets/etsTupleType.h @@ -85,6 +85,7 @@ public: void Identical(TypeRelation *relation, Type *other) override; void AssignmentTarget(TypeRelation *relation, Type *source) override; bool AssignmentSource(TypeRelation *relation, Type *target) override; + Type *Substitute(TypeRelation *relation, const Substitution *substitution) override; void Cast(TypeRelation *relation, Type *target) override; Type *Instantiate(ArenaAllocator *allocator, TypeRelation *relation, GlobalTypesHolder *globalTypes) override; diff --git a/ets2panda/ir/ets/etsTuple.cpp b/ets2panda/ir/ets/etsTuple.cpp index dcb984b54b..929fce73a1 100644 --- a/ets2panda/ir/ets/etsTuple.cpp +++ b/ets2panda/ir/ets/etsTuple.cpp @@ -15,7 +15,6 @@ #include "etsTuple.h" -#include "checker/ETSchecker.h" #include "checker/types/ets/etsTupleType.h" #include "ir/astDump.h" @@ -79,7 +78,36 @@ checker::Type *ETSTuple::Check([[maybe_unused]] checker::ETSChecker *const check return GetType(checker); } -void ETSTuple::SetNullUndefinedFlags(std::pair &containsNullOrUndefined, const checker::Type *const type) +checker::Type *ETSTuple::GetType(checker::ETSChecker *const checker) +{ + if (TsType() != nullptr) { + return TsType(); + } + + ArenaVector typeList(checker->Allocator()->Adapter()); + + for (auto *const typeAnnotation : GetTupleTypeAnnotationsList()) { + auto *const checkedType = checker->GetTypeFromTypeAnnotation(typeAnnotation); + typeList.emplace_back(checkedType); + } + + if (HasSpreadType()) { + ASSERT(spreadType_->IsTSArrayType()); + auto *const arrayType = spreadType_->GetType(checker); + ASSERT(arrayType->IsETSArrayType()); + spreadType_->SetTsType(arrayType->AsETSArrayType()->ElementType()); + } + + auto *const spreadElementType = spreadType_ != nullptr ? spreadType_->TsType() : nullptr; + + auto *const tupleType = checker->Allocator()->New( + typeList, CalculateLUBForTuple(checker, typeList, spreadElementType), spreadElementType); + + SetTsType(tupleType); + return TsType(); +} + +static void SetNullUndefinedFlags(std::pair &containsNullOrUndefined, const checker::Type *const type) { if (type->HasTypeFlag(checker::TypeFlag::NULLISH)) { containsNullOrUndefined.first = true; @@ -101,7 +129,7 @@ checker::Type *ETSTuple::CalculateLUBForTuple(checker::ETSChecker *const checker bool allElementsAreSame = std::all_of(typeList.begin(), typeList.end(), - [this, &checker, &typeList, &containsNullOrUndefined](checker::Type *const element) { + [&checker, &typeList, &containsNullOrUndefined](checker::Type *const element) { SetNullUndefinedFlags(containsNullOrUndefined, element); return checker->Relation()->IsIdenticalTo(typeList[0], element); }); @@ -113,16 +141,13 @@ checker::Type *ETSTuple::CalculateLUBForTuple(checker::ETSChecker *const checker // If only one type present in the tuple, that will be the holder array type. If any two not identical types // present, primitives will be boxed, and LUB is calculated for all of them. - // That makes it possible to assign eg. `[int, int, ...int[]]` tuple type to `int[]` array type. Because a `short[]` - // array already isn't assignable to `int[]` array, that preserve that the `[int, short, ...int[]]` tuple type's - // element type will be calculated to `Object[]`, which is not assignable to `int[]` array either. + // That makes it possible to assign eg. `[int, int, ...int[]]` tuple type to `int[]` array type. Because a + // `short[]` array already isn't assignable to `int[]` array, that preserve that the `[int, short, ...int[]]` + // tuple type's element type will be calculated to `Object[]`, which is not assignable to `int[]` array either. if (allElementsAreSame) { return typeList[0]; } - auto *const savedRelationNode = checker->Relation()->GetNode(); - checker->Relation()->SetNode(this); - auto getBoxedTypeOrType = [&checker](checker::Type *const type) { auto *const boxedType = checker->PrimitiveTypeAsETSBuiltinType(type); return boxedType == nullptr ? type : boxedType; @@ -131,11 +156,19 @@ checker::Type *ETSTuple::CalculateLUBForTuple(checker::ETSChecker *const checker checker::Type *lubType = getBoxedTypeOrType(typeList[0]); for (std::size_t idx = 1; idx < typeList.size(); ++idx) { + if (typeList[idx]->IsETSTypeParameter()) { + lubType = typeList[idx]->AsETSTypeParameter()->GetConstraintType(); + continue; + } lubType = checker->FindLeastUpperBound(lubType, getBoxedTypeOrType(typeList[idx])); } if (spreadType != nullptr) { - lubType = checker->FindLeastUpperBound(lubType, getBoxedTypeOrType(spreadType)); + if (spreadType->IsETSTypeParameter()) { + lubType = spreadType->AsETSTypeParameter()->GetConstraintType(); + } else { + lubType = checker->FindLeastUpperBound(lubType, getBoxedTypeOrType(spreadType)); + } } const auto nullishUndefinedFlags = @@ -148,38 +181,7 @@ checker::Type *ETSTuple::CalculateLUBForTuple(checker::ETSChecker *const checker checker->GetGlobalTypesHolder()); } - checker->Relation()->SetNode(savedRelationNode); - return lubType; } -checker::Type *ETSTuple::GetType(checker::ETSChecker *const checker) -{ - if (TsType() != nullptr) { - return TsType(); - } - - ArenaVector typeList(checker->Allocator()->Adapter()); - - for (auto *const typeAnnotation : GetTupleTypeAnnotationsList()) { - auto *const checkedType = checker->GetTypeFromTypeAnnotation(typeAnnotation); - typeList.emplace_back(checkedType); - } - - if (HasSpreadType()) { - ASSERT(spreadType_->IsTSArrayType()); - auto *const arrayType = spreadType_->GetType(checker); - ASSERT(arrayType->IsETSArrayType()); - spreadType_->SetTsType(arrayType->AsETSArrayType()->ElementType()); - } - - auto *const spreadElementType = spreadType_ != nullptr ? spreadType_->TsType() : nullptr; - - auto *const tupleType = checker->Allocator()->New( - typeList, CalculateLUBForTuple(checker, typeList, spreadElementType), spreadElementType); - - SetTsType(tupleType); - return TsType(); -} - } // namespace panda::es2panda::ir diff --git a/ets2panda/ir/ets/etsTuple.h b/ets2panda/ir/ets/etsTuple.h index ab4eff12f4..43379d9213 100644 --- a/ets2panda/ir/ets/etsTuple.h +++ b/ets2panda/ir/ets/etsTuple.h @@ -16,6 +16,7 @@ #ifndef ES2PANDA_IR_ETS_TUPLE_H #define ES2PANDA_IR_ETS_TUPLE_H +#include "checker/ETSchecker.h" #include "ir/typeNode.h" namespace panda::es2panda::ir { @@ -75,9 +76,8 @@ public: checker::Type *Check([[maybe_unused]] checker::ETSChecker *checker) override; checker::Type *GetType([[maybe_unused]] checker::ETSChecker *checker) override; - void SetNullUndefinedFlags(std::pair &containsNullOrUndefined, const checker::Type *type); - checker::Type *CalculateLUBForTuple(checker::ETSChecker *checker, ArenaVector &typeList, - checker::Type *spreadType); + static checker::Type *CalculateLUBForTuple(checker::ETSChecker *checker, ArenaVector &typeList, + checker::Type *spreadType); void Accept(ASTVisitorT *v) override { diff --git a/ets2panda/ir/expressions/memberExpression.cpp b/ets2panda/ir/expressions/memberExpression.cpp index 9f2c98d44f..c705d084eb 100644 --- a/ets2panda/ir/expressions/memberExpression.cpp +++ b/ets2panda/ir/expressions/memberExpression.cpp @@ -330,14 +330,7 @@ checker::Type *MemberExpression::CheckTupleAccessMethod(checker::ETSChecker *che // NOTE(mmartin): this can be replaced with the general type mapper, once implemented if ((GetBoxingUnboxingFlags() & ir::BoxingUnboxingFlags::UNBOXING_FLAG) != 0U) { - auto *const savedNode = checker->Relation()->GetNode(); - if (savedNode == nullptr) { - checker->Relation()->SetNode(this); - } - SetTupleConvertedType(checker->PrimitiveTypeAsETSBuiltinType(tupleTypeAtIdx)); - - checker->Relation()->SetNode(savedNode); } if (tupleTypeAtIdx->IsETSObjectType() && baseType->AsETSArrayType()->ElementType()->IsETSObjectType()) { diff --git a/ets2panda/test/compiler/ets/tuple_types_19-expected.txt b/ets2panda/test/compiler/ets/tuple_types_19-expected.txt new file mode 100644 index 0000000000..c1e89ef573 --- /dev/null +++ b/ets2panda/test/compiler/ets/tuple_types_19-expected.txt @@ -0,0 +1,2271 @@ +{ + "type": "Program", + "statements": [ + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "TuplePair", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 7 + }, + "end": { + "line": 16, + "column": 16 + } + } + }, + "typeParameters": { + "type": "TSTypeParameterDeclaration", + "params": [ + { + "type": "TSTypeParameter", + "name": { + "type": "Identifier", + "name": "X", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 17 + }, + "end": { + "line": 16, + "column": 18 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 17 + }, + "end": { + "line": 16, + "column": 19 + } + } + }, + { + "type": "TSTypeParameter", + "name": { + "type": "Identifier", + "name": "Y", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 20 + }, + "end": { + "line": 16, + "column": 21 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 20 + }, + "end": { + "line": 16, + "column": 22 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 16 + }, + "end": { + "line": 16, + "column": 22 + } + } + }, + "superClass": null, + "implements": [], + "body": [ + { + "type": "ClassProperty", + "key": { + "type": "Identifier", + "name": "one", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 17, + "column": 8 + } + } + }, + "accessibility": "public", + "static": false, + "readonly": false, + "declare": false, + "optional": false, + "computed": false, + "typeAnnotation": { + "type": "ETSTuple", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "X", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 11 + }, + "end": { + "line": 17, + "column": 12 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 11 + }, + "end": { + "line": 17, + "column": 13 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 11 + }, + "end": { + "line": 17, + "column": 13 + } + } + }, + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Y", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 14 + }, + "end": { + "line": 17, + "column": 15 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 14 + }, + "end": { + "line": 17, + "column": 16 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 14 + }, + "end": { + "line": 17, + "column": 16 + } + } + } + ], + "spreadType": null, + "loc": { + "start": { + "line": 17, + "column": 10 + }, + "end": { + "line": 18, + "column": 11 + } + } + }, + "definite": false, + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 18, + "column": 11 + } + } + }, + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "provide", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 12 + }, + "end": { + "line": 18, + "column": 19 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": false, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "provide", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 12 + }, + "end": { + "line": 18, + "column": 19 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "returnType": { + "type": "ETSTuple", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "X", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 24 + }, + "end": { + "line": 18, + "column": 25 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 24 + }, + "end": { + "line": 18, + "column": 26 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 24 + }, + "end": { + "line": 18, + "column": 26 + } + } + }, + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Y", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 27 + }, + "end": { + "line": 18, + "column": 28 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 27 + }, + "end": { + "line": 18, + "column": 29 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 27 + }, + "end": { + "line": 18, + "column": 29 + } + } + } + ], + "spreadType": null, + "loc": { + "start": { + "line": 18, + "column": 23 + }, + "end": { + "line": 18, + "column": 31 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ReturnStatement", + "argument": { + "type": "MemberExpression", + "object": { + "type": "ThisExpression", + "loc": { + "start": { + "line": 18, + "column": 39 + }, + "end": { + "line": 18, + "column": 43 + } + } + }, + "property": { + "type": "Identifier", + "name": "one", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 44 + }, + "end": { + "line": 18, + "column": 47 + } + } + }, + "computed": false, + "optional": false, + "loc": { + "start": { + "line": 18, + "column": 39 + }, + "end": { + "line": 18, + "column": 47 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 32 + }, + "end": { + "line": 18, + "column": 48 + } + } + } + ], + "loc": { + "start": { + "line": 18, + "column": 30 + }, + "end": { + "line": 18, + "column": 50 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 19 + }, + "end": { + "line": 18, + "column": 50 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 19 + }, + "end": { + "line": 18, + "column": 50 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 5 + }, + "end": { + "line": 18, + "column": 50 + } + } + }, + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "constructor", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "kind": "constructor", + "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": [], + "body": { + "type": "BlockStatement", + "statements": [], + "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": 1, + "column": 1 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 2 + }, + "end": { + "line": 19, + "column": 2 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 23 + }, + "end": { + "line": 19, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 19, + "column": 2 + } + } + }, + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "TupleProvider", + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 7 + }, + "end": { + "line": 21, + "column": 20 + } + } + }, + "typeParameters": { + "type": "TSTypeParameterDeclaration", + "params": [ + { + "type": "TSTypeParameter", + "name": { + "type": "Identifier", + "name": "X", + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 21 + }, + "end": { + "line": 21, + "column": 22 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 21 + }, + "end": { + "line": 21, + "column": 23 + } + } + } + ], + "loc": { + "start": { + "line": 21, + "column": 20 + }, + "end": { + "line": 21, + "column": 23 + } + } + }, + "superClass": null, + "implements": [], + "body": [ + { + "type": "ClassProperty", + "key": { + "type": "Identifier", + "name": "memb", + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 5 + }, + "end": { + "line": 22, + "column": 9 + } + } + }, + "accessibility": "public", + "static": false, + "readonly": false, + "declare": false, + "optional": false, + "computed": false, + "typeAnnotation": { + "type": "ETSTuple", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "X", + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 12 + }, + "end": { + "line": 22, + "column": 13 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 12 + }, + "end": { + "line": 22, + "column": 14 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 12 + }, + "end": { + "line": 22, + "column": 14 + } + } + }, + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "X", + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 15 + }, + "end": { + "line": 22, + "column": 16 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 15 + }, + "end": { + "line": 22, + "column": 17 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 15 + }, + "end": { + "line": 22, + "column": 17 + } + } + } + ], + "spreadType": null, + "loc": { + "start": { + "line": 22, + "column": 11 + }, + "end": { + "line": 22, + "column": 18 + } + } + }, + "definite": false, + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 5 + }, + "end": { + "line": 22, + "column": 18 + } + } + }, + { + "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": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "value", + "typeAnnotation": { + "type": "ETSTuple", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "X", + "decorators": [], + "loc": { + "start": { + "line": 23, + "column": 25 + }, + "end": { + "line": 23, + "column": 26 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 25 + }, + "end": { + "line": 23, + "column": 27 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 25 + }, + "end": { + "line": 23, + "column": 27 + } + } + }, + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "X", + "decorators": [], + "loc": { + "start": { + "line": 23, + "column": 28 + }, + "end": { + "line": 23, + "column": 29 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 28 + }, + "end": { + "line": 23, + "column": 30 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 28 + }, + "end": { + "line": 23, + "column": 30 + } + } + } + ], + "spreadType": null, + "loc": { + "start": { + "line": 23, + "column": 24 + }, + "end": { + "line": 23, + "column": 31 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 23, + "column": 17 + }, + "end": { + "line": 23, + "column": 31 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 17 + }, + "end": { + "line": 23, + "column": 31 + } + } + } + ], + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ExpressionStatement", + "expression": { + "type": "AssignmentExpression", + "operator": "=", + "left": { + "type": "MemberExpression", + "object": { + "type": "ThisExpression", + "loc": { + "start": { + "line": 23, + "column": 34 + }, + "end": { + "line": 23, + "column": 38 + } + } + }, + "property": { + "type": "Identifier", + "name": "memb", + "decorators": [], + "loc": { + "start": { + "line": 23, + "column": 39 + }, + "end": { + "line": 23, + "column": 43 + } + } + }, + "computed": false, + "optional": false, + "loc": { + "start": { + "line": 23, + "column": 34 + }, + "end": { + "line": 23, + "column": 43 + } + } + }, + "right": { + "type": "Identifier", + "name": "value", + "decorators": [], + "loc": { + "start": { + "line": 23, + "column": 46 + }, + "end": { + "line": 23, + "column": 51 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 34 + }, + "end": { + "line": 23, + "column": 51 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 34 + }, + "end": { + "line": 23, + "column": 52 + } + } + } + ], + "loc": { + "start": { + "line": 23, + "column": 32 + }, + "end": { + "line": 23, + "column": 53 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 16 + }, + "end": { + "line": 23, + "column": 53 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 16 + }, + "end": { + "line": 23, + "column": 53 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 23, + "column": 5 + }, + "end": { + "line": 23, + "column": 53 + } + } + }, + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "publish", + "decorators": [], + "loc": { + "start": { + "line": 24, + "column": 12 + }, + "end": { + "line": 24, + "column": 19 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": false, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "publish", + "decorators": [], + "loc": { + "start": { + "line": 24, + "column": 12 + }, + "end": { + "line": 24, + "column": 19 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "returnType": { + "type": "ETSTuple", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "X", + "decorators": [], + "loc": { + "start": { + "line": 24, + "column": 24 + }, + "end": { + "line": 24, + "column": 25 + } + } + }, + "loc": { + "start": { + "line": 24, + "column": 24 + }, + "end": { + "line": 24, + "column": 26 + } + } + }, + "loc": { + "start": { + "line": 24, + "column": 24 + }, + "end": { + "line": 24, + "column": 26 + } + } + }, + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "X", + "decorators": [], + "loc": { + "start": { + "line": 24, + "column": 27 + }, + "end": { + "line": 24, + "column": 28 + } + } + }, + "loc": { + "start": { + "line": 24, + "column": 27 + }, + "end": { + "line": 24, + "column": 29 + } + } + }, + "loc": { + "start": { + "line": 24, + "column": 27 + }, + "end": { + "line": 24, + "column": 29 + } + } + } + ], + "spreadType": null, + "loc": { + "start": { + "line": 24, + "column": 23 + }, + "end": { + "line": 24, + "column": 31 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ReturnStatement", + "argument": { + "type": "MemberExpression", + "object": { + "type": "ThisExpression", + "loc": { + "start": { + "line": 24, + "column": 39 + }, + "end": { + "line": 24, + "column": 43 + } + } + }, + "property": { + "type": "Identifier", + "name": "memb", + "decorators": [], + "loc": { + "start": { + "line": 24, + "column": 44 + }, + "end": { + "line": 24, + "column": 48 + } + } + }, + "computed": false, + "optional": false, + "loc": { + "start": { + "line": 24, + "column": 39 + }, + "end": { + "line": 24, + "column": 48 + } + } + }, + "loc": { + "start": { + "line": 24, + "column": 32 + }, + "end": { + "line": 24, + "column": 49 + } + } + } + ], + "loc": { + "start": { + "line": 24, + "column": 30 + }, + "end": { + "line": 24, + "column": 51 + } + } + }, + "loc": { + "start": { + "line": 24, + "column": 19 + }, + "end": { + "line": 24, + "column": 51 + } + } + }, + "loc": { + "start": { + "line": 24, + "column": 19 + }, + "end": { + "line": 24, + "column": 51 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 24, + "column": 5 + }, + "end": { + "line": 24, + "column": 51 + } + } + } + ], + "loc": { + "start": { + "line": 21, + "column": 24 + }, + "end": { + "line": 25, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 1 + }, + "end": { + "line": 25, + "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": "_$init$_", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": true, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "_$init$_", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "statements": [], + "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": 1, + "column": 1 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "foo", + "decorators": [], + "loc": { + "start": { + "line": 27, + "column": 10 + }, + "end": { + "line": 27, + "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": 27, + "column": 10 + }, + "end": { + "line": 27, + "column": 13 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "number_tup", + "typeAnnotation": { + "type": "ETSTuple", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Double", + "decorators": [], + "loc": { + "start": { + "line": 28, + "column": 22 + }, + "end": { + "line": 28, + "column": 28 + } + } + }, + "loc": { + "start": { + "line": 28, + "column": 22 + }, + "end": { + "line": 28, + "column": 29 + } + } + }, + "loc": { + "start": { + "line": 28, + "column": 22 + }, + "end": { + "line": 28, + "column": 29 + } + } + }, + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Double", + "decorators": [], + "loc": { + "start": { + "line": 28, + "column": 30 + }, + "end": { + "line": 28, + "column": 36 + } + } + }, + "loc": { + "start": { + "line": 28, + "column": 30 + }, + "end": { + "line": 28, + "column": 37 + } + } + }, + "loc": { + "start": { + "line": 28, + "column": 30 + }, + "end": { + "line": 28, + "column": 37 + } + } + } + ], + "spreadType": null, + "loc": { + "start": { + "line": 28, + "column": 21 + }, + "end": { + "line": 28, + "column": 39 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 28, + "column": 9 + }, + "end": { + "line": 28, + "column": 19 + } + } + }, + "init": { + "type": "ArrayExpression", + "elements": [ + { + "type": "NumberLiteral", + "value": 42, + "loc": { + "start": { + "line": 28, + "column": 41 + }, + "end": { + "line": 28, + "column": 45 + } + } + }, + { + "type": "NumberLiteral", + "value": 43, + "loc": { + "start": { + "line": 28, + "column": 47 + }, + "end": { + "line": 28, + "column": 51 + } + } + } + ], + "loc": { + "start": { + "line": 28, + "column": 40 + }, + "end": { + "line": 28, + "column": 52 + } + } + }, + "loc": { + "start": { + "line": 28, + "column": 9 + }, + "end": { + "line": 28, + "column": 52 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 28, + "column": 5 + }, + "end": { + "line": 28, + "column": 53 + } + } + }, + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "tn", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "TupleProvider", + "decorators": [], + "loc": { + "start": { + "line": 29, + "column": 13 + }, + "end": { + "line": 29, + "column": 26 + } + } + }, + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "number", + "decorators": [], + "loc": { + "start": { + "line": 29, + "column": 27 + }, + "end": { + "line": 29, + "column": 33 + } + } + }, + "loc": { + "start": { + "line": 29, + "column": 27 + }, + "end": { + "line": 29, + "column": 34 + } + } + }, + "loc": { + "start": { + "line": 29, + "column": 27 + }, + "end": { + "line": 29, + "column": 34 + } + } + } + ], + "loc": { + "start": { + "line": 29, + "column": 26 + }, + "end": { + "line": 29, + "column": 34 + } + } + }, + "loc": { + "start": { + "line": 29, + "column": 13 + }, + "end": { + "line": 29, + "column": 36 + } + } + }, + "loc": { + "start": { + "line": 29, + "column": 13 + }, + "end": { + "line": 29, + "column": 36 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 29, + "column": 9 + }, + "end": { + "line": 29, + "column": 11 + } + } + }, + "init": { + "type": "ETSNewClassInstanceExpression", + "typeReference": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "TupleProvider", + "decorators": [], + "loc": { + "start": { + "line": 29, + "column": 41 + }, + "end": { + "line": 29, + "column": 54 + } + } + }, + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "number", + "decorators": [], + "loc": { + "start": { + "line": 29, + "column": 55 + }, + "end": { + "line": 29, + "column": 61 + } + } + }, + "loc": { + "start": { + "line": 29, + "column": 55 + }, + "end": { + "line": 29, + "column": 62 + } + } + }, + "loc": { + "start": { + "line": 29, + "column": 55 + }, + "end": { + "line": 29, + "column": 62 + } + } + } + ], + "loc": { + "start": { + "line": 29, + "column": 54 + }, + "end": { + "line": 29, + "column": 62 + } + } + }, + "loc": { + "start": { + "line": 29, + "column": 41 + }, + "end": { + "line": 29, + "column": 63 + } + } + }, + "loc": { + "start": { + "line": 29, + "column": 41 + }, + "end": { + "line": 29, + "column": 63 + } + } + }, + "arguments": [ + { + "type": "Identifier", + "name": "number_tup", + "decorators": [], + "loc": { + "start": { + "line": 29, + "column": 63 + }, + "end": { + "line": 29, + "column": 73 + } + } + } + ], + "loc": { + "start": { + "line": 29, + "column": 37 + }, + "end": { + "line": 29, + "column": 75 + } + } + }, + "loc": { + "start": { + "line": 29, + "column": 9 + }, + "end": { + "line": 29, + "column": 75 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 29, + "column": 5 + }, + "end": { + "line": 29, + "column": 75 + } + } + }, + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "result", + "typeAnnotation": { + "type": "ETSTuple", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Number", + "decorators": [], + "loc": { + "start": { + "line": 30, + "column": 18 + }, + "end": { + "line": 30, + "column": 24 + } + } + }, + "loc": { + "start": { + "line": 30, + "column": 18 + }, + "end": { + "line": 30, + "column": 25 + } + } + }, + "loc": { + "start": { + "line": 30, + "column": 18 + }, + "end": { + "line": 30, + "column": 25 + } + } + }, + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Number", + "decorators": [], + "loc": { + "start": { + "line": 30, + "column": 26 + }, + "end": { + "line": 30, + "column": 32 + } + } + }, + "loc": { + "start": { + "line": 30, + "column": 26 + }, + "end": { + "line": 30, + "column": 33 + } + } + }, + "loc": { + "start": { + "line": 30, + "column": 26 + }, + "end": { + "line": 30, + "column": 33 + } + } + } + ], + "spreadType": null, + "loc": { + "start": { + "line": 30, + "column": 17 + }, + "end": { + "line": 30, + "column": 35 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 30, + "column": 9 + }, + "end": { + "line": 30, + "column": 15 + } + } + }, + "init": { + "type": "CallExpression", + "callee": { + "type": "MemberExpression", + "object": { + "type": "Identifier", + "name": "tn", + "decorators": [], + "loc": { + "start": { + "line": 30, + "column": 36 + }, + "end": { + "line": 30, + "column": 38 + } + } + }, + "property": { + "type": "Identifier", + "name": "publish", + "decorators": [], + "loc": { + "start": { + "line": 30, + "column": 39 + }, + "end": { + "line": 30, + "column": 46 + } + } + }, + "computed": false, + "optional": false, + "loc": { + "start": { + "line": 30, + "column": 36 + }, + "end": { + "line": 30, + "column": 46 + } + } + }, + "arguments": [], + "optional": false, + "loc": { + "start": { + "line": 30, + "column": 36 + }, + "end": { + "line": 30, + "column": 48 + } + } + }, + "loc": { + "start": { + "line": 30, + "column": 9 + }, + "end": { + "line": 30, + "column": 48 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 30, + "column": 5 + }, + "end": { + "line": 30, + "column": 49 + } + } + } + ], + "loc": { + "start": { + "line": 27, + "column": 16 + }, + "end": { + "line": 31, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 27, + "column": 13 + }, + "end": { + "line": 31, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 27, + "column": 13 + }, + "end": { + "line": 31, + "column": 2 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 27, + "column": 1 + }, + "end": { + "line": 31, + "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": 32, + "column": 1 + } + } +} diff --git a/ets2panda/test/compiler/ets/tuple_types_19.ets b/ets2panda/test/compiler/ets/tuple_types_19.ets new file mode 100644 index 0000000000..cb744db81f --- /dev/null +++ b/ets2panda/test/compiler/ets/tuple_types_19.ets @@ -0,0 +1,31 @@ +/* + * 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 TuplePair { + one: [X, Y] + public provide(): [X, Y] { return this.one; } +} + +class TupleProvider { + memb: [X, X]; + constructor(value: [X, X]) { this.memb = value;} + public publish(): [X, X] { return this.memb; } +} + +function foo() { + let number_tup: [Double, Double] = [42.0, 43.0]; + let tn: TupleProvider = new TupleProvider(number_tup); + let result: [Number, Number] = tn.publish(); +} diff --git a/ets2panda/test/runtime/ets/tuple_types_runtime.ets b/ets2panda/test/runtime/ets/tuple_types_runtime.ets index 5f57797cab..046ad690bb 100644 --- a/ets2panda/test/runtime/ets/tuple_types_runtime.ets +++ b/ets2panda/test/runtime/ets/tuple_types_runtime.ets @@ -28,6 +28,12 @@ class B extends A { } } +class TupleProvider { + memb: [X, X]; + constructor(value: [X, X]) { this.memb = value;} + public publish(): [X, X] { return this.memb; } +} + function main(): void { const tup_1: [string, number] = ["a", 12]; @@ -126,4 +132,9 @@ function main(): void { assert(a_b_tup[0].foo() == 1); assert(a_b_tup[1].foo() == 2); assert(a_b_tup[2].foo() == 2); + + let number_tup: [Double, Double] = [42.0, 43.0]; + let tn: TupleProvider = new TupleProvider(number_tup); + let result: [Number, Number] = tn.publish(); + assert result[0] == 42.0; } -- Gitee From 7e7b47b3e0fe4fbf6d4509f3c360f4ef7d4c5bfa Mon Sep 17 00:00:00 2001 From: Soma Simon Date: Tue, 16 Jan 2024 11:32:09 +0100 Subject: [PATCH 06/19] [ArkTS frontend] Fix cannot reference 'this' in this context Save context at proxy methods to fix the problem with this expression. Create a better check if a proxy method has return type. Fixes internal issue #14623 Change-Id: I03f41a1e90a6bde387ab504ce886694a69f8a40f Signed-off-by: Soma Simon --- ets2panda/checker/ETSAnalyzer.cpp | 1 + ets2panda/parser/ETSparser.cpp | 2 +- ..._implicitly_typed_return_void-expected.txt | 4 +- .../test/parser/ets/proxy_method-expected.txt | 1440 +++++++++++++++++ ets2panda/test/parser/ets/proxy_method.ets | 24 + .../test/runtime/ets/InferProxymethodType.ets | 37 + 6 files changed, 1505 insertions(+), 3 deletions(-) create mode 100644 ets2panda/test/parser/ets/proxy_method-expected.txt create mode 100644 ets2panda/test/parser/ets/proxy_method.ets create mode 100644 ets2panda/test/runtime/ets/InferProxymethodType.ets diff --git a/ets2panda/checker/ETSAnalyzer.cpp b/ets2panda/checker/ETSAnalyzer.cpp index 77a8d79564..f2d00c77db 100644 --- a/ets2panda/checker/ETSAnalyzer.cpp +++ b/ets2panda/checker/ETSAnalyzer.cpp @@ -1145,6 +1145,7 @@ checker::Type *ETSAnalyzer::Check(ir::CallExpression *expr) const } if (expr->Signature()->HasSignatureFlag(checker::SignatureFlags::NEED_RETURN_TYPE)) { + checker::SavedCheckerContext savedCtx(checker, checker->Context().Status(), expr->Signature()->Owner()); expr->Signature()->OwnerVar()->Declaration()->Node()->Check(checker); returnType = expr->Signature()->ReturnType(); // NOTE(vpukhov): #14902 substituted signature is not updated diff --git a/ets2panda/parser/ETSparser.cpp b/ets2panda/parser/ETSparser.cpp index b6d884410d..8bacd3eba6 100644 --- a/ets2panda/parser/ETSparser.cpp +++ b/ets2panda/parser/ETSparser.cpp @@ -2299,7 +2299,7 @@ ir::MethodDefinition *ETSParser::CreateProxyMethodDefinition(ir::MethodDefinitio } proxyMethod += ' '; - if (returnType != "void") { + if ((function->AsScriptFunction()->Flags() & ir::ScriptFunctionFlags::HAS_RETURN) != 0) { proxyMethod += "return "; } diff --git a/ets2panda/test/parser/ets/default_parameter_implicitly_typed_return_void-expected.txt b/ets2panda/test/parser/ets/default_parameter_implicitly_typed_return_void-expected.txt index 1e35125767..bb716f90ae 100644 --- a/ets2panda/test/parser/ets/default_parameter_implicitly_typed_return_void-expected.txt +++ b/ets2panda/test/parser/ets/default_parameter_implicitly_typed_return_void-expected.txt @@ -1100,8 +1100,8 @@ } }, { - "type": "ReturnStatement", - "argument": { + "type": "ExpressionStatement", + "expression": { "type": "CallExpression", "callee": { "type": "Identifier", diff --git a/ets2panda/test/parser/ets/proxy_method-expected.txt b/ets2panda/test/parser/ets/proxy_method-expected.txt new file mode 100644 index 0000000000..fa34056abf --- /dev/null +++ b/ets2panda/test/parser/ets/proxy_method-expected.txt @@ -0,0 +1,1440 @@ +{ + "type": "Program", + "statements": [ + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 7 + }, + "end": { + "line": 16, + "column": 8 + } + } + }, + "superClass": null, + "implements": [], + "body": [ + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "met", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 17, + "column": 8 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": false, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "met", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 17, + "column": 8 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "s", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "string", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 12 + }, + "end": { + "line": 17, + "column": 18 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 12 + }, + "end": { + "line": 17, + "column": 19 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 12 + }, + "end": { + "line": 17, + "column": 19 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 9 + }, + "end": { + "line": 17, + "column": 19 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 9 + }, + "end": { + "line": 17, + "column": 19 + } + } + }, + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "q", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "string", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 24 + }, + "end": { + "line": 17, + "column": 30 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 24 + }, + "end": { + "line": 17, + "column": 31 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 24 + }, + "end": { + "line": 17, + "column": 31 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 20 + }, + "end": { + "line": 17, + "column": 31 + } + } + }, + "initializer": { + "type": "UndefinedLiteral", + "value": undefined, + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 20 + }, + "end": { + "line": 17, + "column": 31 + } + } + } + ], + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ExpressionStatement", + "expression": { + "type": "CallExpression", + "callee": { + "type": "MemberExpression", + "object": { + "type": "Identifier", + "name": "console", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 9 + }, + "end": { + "line": 18, + "column": 16 + } + } + }, + "property": { + "type": "Identifier", + "name": "log", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 17 + }, + "end": { + "line": 18, + "column": 20 + } + } + }, + "computed": false, + "optional": false, + "loc": { + "start": { + "line": 18, + "column": 9 + }, + "end": { + "line": 18, + "column": 20 + } + } + }, + "arguments": [ + { + "type": "BinaryExpression", + "operator": "+", + "left": { + "type": "StringLiteral", + "value": "met ", + "loc": { + "start": { + "line": 18, + "column": 21 + }, + "end": { + "line": 18, + "column": 27 + } + } + }, + "right": { + "type": "Identifier", + "name": "s", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 30 + }, + "end": { + "line": 18, + "column": 31 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 21 + }, + "end": { + "line": 18, + "column": 31 + } + } + } + ], + "optional": false, + "loc": { + "start": { + "line": 18, + "column": 9 + }, + "end": { + "line": 18, + "column": 32 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 9 + }, + "end": { + "line": 18, + "column": 32 + } + } + } + ], + "loc": { + "start": { + "line": 17, + "column": 31 + }, + "end": { + "line": 19, + "column": 6 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 8 + }, + "end": { + "line": 19, + "column": 6 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 8 + }, + "end": { + "line": 19, + "column": 6 + } + } + }, + "overloads": [ + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "met_proxy", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": false, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "met_proxy", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "s", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "string", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "q", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "string", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "$proxy_mask$", + "typeAnnotation": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + } + ], + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "IfStatement", + "test": { + "type": "BinaryExpression", + "operator": "==", + "left": { + "type": "BinaryExpression", + "operator": "&", + "left": { + "type": "BinaryExpression", + "operator": ">>", + "left": { + "type": "Identifier", + "name": "$proxy_mask$", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "right": { + "type": "NumberLiteral", + "value": 1, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "right": { + "type": "NumberLiteral", + "value": 1, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "right": { + "type": "NumberLiteral", + "value": 1, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "consequent": { + "type": "BlockStatement", + "statements": [ + { + "type": "ExpressionStatement", + "expression": { + "type": "AssignmentExpression", + "operator": "=", + "left": { + "type": "Identifier", + "name": "q", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "right": { + "type": "UndefinedLiteral", + "value": undefined, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "alternate": null, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + { + "type": "ExpressionStatement", + "expression": { + "type": "CallExpression", + "callee": { + "type": "MemberExpression", + "object": { + "type": "ThisExpression", + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "property": { + "type": "Identifier", + "name": "met", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "computed": false, + "optional": false, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "arguments": [ + { + "type": "Identifier", + "name": "s", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + { + "type": "Identifier", + "name": "q", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + } + ], + "optional": false, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 3 + } + } + } + ], + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 5 + }, + "end": { + "line": 19, + "column": 6 + } + } + }, + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "constructor", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "kind": "constructor", + "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": [], + "body": { + "type": "BlockStatement", + "statements": [], + "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": 1, + "column": 1 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 2 + }, + "end": { + "line": 20, + "column": 2 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 9 + }, + "end": { + "line": 20, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 20, + "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": "_$init$_", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": true, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "_$init$_", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "statements": [], + "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": 1, + "column": 1 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "main", + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 10 + }, + "end": { + "line": 22, + "column": 14 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": true, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "main", + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 10 + }, + "end": { + "line": 22, + "column": 14 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ExpressionStatement", + "expression": { + "type": "CallExpression", + "callee": { + "type": "MemberExpression", + "object": { + "type": "ETSNewClassInstanceExpression", + "typeReference": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 23, + "column": 9 + }, + "end": { + "line": 23, + "column": 10 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 9 + }, + "end": { + "line": 23, + "column": 11 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 9 + }, + "end": { + "line": 23, + "column": 11 + } + } + }, + "arguments": [], + "loc": { + "start": { + "line": 23, + "column": 5 + }, + "end": { + "line": 23, + "column": 13 + } + } + }, + "property": { + "type": "Identifier", + "name": "met", + "decorators": [], + "loc": { + "start": { + "line": 23, + "column": 13 + }, + "end": { + "line": 23, + "column": 16 + } + } + }, + "computed": false, + "optional": false, + "loc": { + "start": { + "line": 23, + "column": 5 + }, + "end": { + "line": 23, + "column": 16 + } + } + }, + "arguments": [ + { + "type": "StringLiteral", + "value": "S", + "loc": { + "start": { + "line": 23, + "column": 17 + }, + "end": { + "line": 23, + "column": 20 + } + } + } + ], + "optional": false, + "loc": { + "start": { + "line": 23, + "column": 5 + }, + "end": { + "line": 23, + "column": 21 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 5 + }, + "end": { + "line": 23, + "column": 21 + } + } + } + ], + "loc": { + "start": { + "line": 22, + "column": 17 + }, + "end": { + "line": 24, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 14 + }, + "end": { + "line": 24, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 14 + }, + "end": { + "line": 24, + "column": 2 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 1 + }, + "end": { + "line": 24, + "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": 25, + "column": 1 + } + } +} diff --git a/ets2panda/test/parser/ets/proxy_method.ets b/ets2panda/test/parser/ets/proxy_method.ets new file mode 100644 index 0000000000..d058f71495 --- /dev/null +++ b/ets2panda/test/parser/ets/proxy_method.ets @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2024 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 A { + met(s: string, q?: string){ + console.log("met " + s) + } +} + +function main() { + new A().met('S') +} diff --git a/ets2panda/test/runtime/ets/InferProxymethodType.ets b/ets2panda/test/runtime/ets/InferProxymethodType.ets new file mode 100644 index 0000000000..334bb0f6ec --- /dev/null +++ b/ets2panda/test/runtime/ets/InferProxymethodType.ets @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2024 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 A { + met(s: string, q?: string){ + return true; + } + + mat2(){ + return this.met("cat","a"); + } +} + +function main() { + let newA = new A(); + let a1 = new A().met('S'); + let a2 = new A().met('S',"a"); + let a3 = newA.met('S', 'a'); + let b = new A().mat2(); + + assert a1; + assert a2; + assert a3; + assert b; +} -- Gitee From e06da0567e55cf38b296cec98b66ebd1e5687185 Mon Sep 17 00:00:00 2001 From: ozerovnikita Date: Thu, 21 Dec 2023 19:28:37 +0300 Subject: [PATCH 07/19] fix CTE in assignment non-functional non-functional variable to a functional type Description: replaced ThrowError() call that returns Unresolved reference with ThrowTypeError() call with error description Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/I8S702 Signed-off-by: ozerovnikita --- ets2panda/checker/ets/helpers.cpp | 3 +- ...l_variable_to_functional_type-expected.txt | 894 ++++++++++++++++++ ...functional_variable_to_functional_type.ets | 26 + ...variable_to_functional_type_1-expected.txt | 449 +++++++++ ...nctional_variable_to_functional_type_1.ets | 21 + 5 files changed, 1392 insertions(+), 1 deletion(-) create mode 100644 ets2panda/test/parser/ets/assignment_non-functional_variable_to_functional_type-expected.txt create mode 100644 ets2panda/test/parser/ets/assignment_non-functional_variable_to_functional_type.ets create mode 100644 ets2panda/test/parser/ets/assignment_non-functional_variable_to_functional_type_1-expected.txt create mode 100644 ets2panda/test/parser/ets/assignment_non-functional_variable_to_functional_type_1.ets diff --git a/ets2panda/checker/ets/helpers.cpp b/ets2panda/checker/ets/helpers.cpp index e720f7b96f..77aa904632 100644 --- a/ets2panda/checker/ets/helpers.cpp +++ b/ets2panda/checker/ets/helpers.cpp @@ -586,7 +586,8 @@ void ETSChecker::ValidateAssignmentIdentifier(ir::Identifier *const ident, varbi if (targetType->IsETSObjectType() && targetType->AsETSObjectType()->HasObjectFlag(ETSObjectFlags::FUNCTIONAL)) { if (!type->IsETSFunctionType() && !(type->IsETSObjectType() && type->AsETSObjectType()->HasObjectFlag(ETSObjectFlags::FUNCTIONAL))) { - ThrowError(ident); + ThrowTypeError({"Assigning a non-functional variable \"", ident->Name(), "\" to a functional type"}, + ident->Start()); } return; diff --git a/ets2panda/test/parser/ets/assignment_non-functional_variable_to_functional_type-expected.txt b/ets2panda/test/parser/ets/assignment_non-functional_variable_to_functional_type-expected.txt new file mode 100644 index 0000000000..6ca5ac47b0 --- /dev/null +++ b/ets2panda/test/parser/ets/assignment_non-functional_variable_to_functional_type-expected.txt @@ -0,0 +1,894 @@ +{ + "type": "Program", + "statements": [ + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "__Observable__", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 16 + }, + "end": { + "line": 16, + "column": 30 + } + } + }, + "superClass": null, + "implements": [], + "body": [ + { + "type": "ClassProperty", + "key": { + "type": "Identifier", + "name": "__memo_state_observable_handler__", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 12 + }, + "end": { + "line": 17, + "column": 45 + } + } + }, + "accessibility": "public", + "static": false, + "readonly": false, + "declare": false, + "optional": false, + "computed": false, + "typeAnnotation": { + "type": "ETSFunctionType", + "params": [], + "returnType": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Observable", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 53 + }, + "end": { + "line": 17, + "column": 63 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 53 + }, + "end": { + "line": 17, + "column": 64 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 53 + }, + "end": { + "line": 17, + "column": 64 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 47 + }, + "end": { + "line": 17, + "column": 64 + } + } + }, + "definite": false, + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 12 + }, + "end": { + "line": 17, + "column": 64 + } + } + }, + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "constructor", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "kind": "constructor", + "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": [], + "body": { + "type": "BlockStatement", + "statements": [], + "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": 1, + "column": 1 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 2 + }, + "end": { + "line": 18, + "column": 2 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 31 + }, + "end": { + "line": 18, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 10 + }, + "end": { + "line": 18, + "column": 2 + } + } + }, + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "Observable", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 7 + }, + "end": { + "line": 20, + "column": 17 + } + } + }, + "superClass": null, + "implements": [], + "body": [ + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "installOn", + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 12 + }, + "end": { + "line": 21, + "column": 21 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": true, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "installOn", + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 12 + }, + "end": { + "line": 21, + "column": 21 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "value", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Object", + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 29 + }, + "end": { + "line": 21, + "column": 35 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 29 + }, + "end": { + "line": 21, + "column": 36 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 29 + }, + "end": { + "line": 21, + "column": 36 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 22 + }, + "end": { + "line": 21, + "column": 36 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 22 + }, + "end": { + "line": 21, + "column": 36 + } + } + }, + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "observable", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Observable", + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 49 + }, + "end": { + "line": 21, + "column": 59 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 49 + }, + "end": { + "line": 21, + "column": 60 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 49 + }, + "end": { + "line": 21, + "column": 60 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 37 + }, + "end": { + "line": 21, + "column": 60 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 37 + }, + "end": { + "line": 21, + "column": 60 + } + } + } + ], + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ExpressionStatement", + "expression": { + "type": "AssignmentExpression", + "operator": "=", + "left": { + "type": "MemberExpression", + "object": { + "type": "TSAsExpression", + "expression": { + "type": "Identifier", + "name": "value", + "decorators": [], + "loc": { + "start": { + "line": 23, + "column": 10 + }, + "end": { + "line": 23, + "column": 15 + } + } + }, + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "__Observable__", + "decorators": [], + "loc": { + "start": { + "line": 23, + "column": 19 + }, + "end": { + "line": 23, + "column": 33 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 19 + }, + "end": { + "line": 23, + "column": 34 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 19 + }, + "end": { + "line": 23, + "column": 34 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 9 + }, + "end": { + "line": 23, + "column": 34 + } + } + }, + "property": { + "type": "Identifier", + "name": "__memo_state_observable_handler__", + "decorators": [], + "loc": { + "start": { + "line": 23, + "column": 35 + }, + "end": { + "line": 23, + "column": 68 + } + } + }, + "computed": false, + "optional": false, + "loc": { + "start": { + "line": 23, + "column": 9 + }, + "end": { + "line": 23, + "column": 68 + } + } + }, + "right": { + "type": "Identifier", + "name": "observable", + "decorators": [], + "loc": { + "start": { + "line": 23, + "column": 71 + }, + "end": { + "line": 23, + "column": 81 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 9 + }, + "end": { + "line": 23, + "column": 81 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 9 + }, + "end": { + "line": 23, + "column": 81 + } + } + } + ], + "loc": { + "start": { + "line": 22, + "column": 5 + }, + "end": { + "line": 24, + "column": 6 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 21 + }, + "end": { + "line": 24, + "column": 6 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 21 + }, + "end": { + "line": 24, + "column": 6 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 5 + }, + "end": { + "line": 24, + "column": 6 + } + } + }, + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "constructor", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "kind": "constructor", + "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": [], + "body": { + "type": "BlockStatement", + "statements": [], + "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": 1, + "column": 1 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 26, + "column": 2 + }, + "end": { + "line": 26, + "column": 2 + } + } + } + ], + "loc": { + "start": { + "line": 20, + "column": 18 + }, + "end": { + "line": 26, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 1 + }, + "end": { + "line": 26, + "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": "_$init$_", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": true, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "_$init$_", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "statements": [], + "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": 1, + "column": 1 + } + } + }, + "overloads": [], + "decorators": [], + "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": 1, + "column": 1 + } + } + } + ], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 27, + "column": 1 + } + } +} +TypeError: Assigning a non-functional variable "observable" to a functional type [assignment_non-functional_variable_to_functional_type.ets:23:71] diff --git a/ets2panda/test/parser/ets/assignment_non-functional_variable_to_functional_type.ets b/ets2panda/test/parser/ets/assignment_non-functional_variable_to_functional_type.ets new file mode 100644 index 0000000000..4288ab1f6b --- /dev/null +++ b/ets2panda/test/parser/ets/assignment_non-functional_variable_to_functional_type.ets @@ -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. + */ + +abstract class __Observable__ { + public __memo_state_observable_handler__: () => Observable|undefined; +} + +class Observable { + static installOn(value: Object, observable: Observable|undefined) + { + (value as __Observable__).__memo_state_observable_handler__ = observable + } + +} diff --git a/ets2panda/test/parser/ets/assignment_non-functional_variable_to_functional_type_1-expected.txt b/ets2panda/test/parser/ets/assignment_non-functional_variable_to_functional_type_1-expected.txt new file mode 100644 index 0000000000..366185c853 --- /dev/null +++ b/ets2panda/test/parser/ets/assignment_non-functional_variable_to_functional_type_1-expected.txt @@ -0,0 +1,449 @@ +{ + "type": "Program", + "statements": [ + { + "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": "_$init$_", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": true, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "_$init$_", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "statements": [], + "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": 1, + "column": 1 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "main", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 10 + }, + "end": { + "line": 16, + "column": 14 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": true, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "main", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 10 + }, + "end": { + "line": 16, + "column": 14 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "a", + "typeAnnotation": { + "type": "ETSFunctionType", + "params": [], + "returnType": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 18, + "column": 18 + }, + "end": { + "line": 18, + "column": 21 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 12 + }, + "end": { + "line": 18, + "column": 21 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 9 + }, + "end": { + "line": 18, + "column": 10 + } + } + }, + "init": null, + "loc": { + "start": { + "line": 18, + "column": 9 + }, + "end": { + "line": 18, + "column": 10 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 18, + "column": 5 + }, + "end": { + "line": 18, + "column": 10 + } + } + }, + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "b", + "typeAnnotation": { + "type": "ETSPrimitiveType", + "loc": { + "start": { + "line": 19, + "column": 12 + }, + "end": { + "line": 19, + "column": 15 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 9 + }, + "end": { + "line": 19, + "column": 10 + } + } + }, + "init": { + "type": "NumberLiteral", + "value": 5, + "loc": { + "start": { + "line": 19, + "column": 18 + }, + "end": { + "line": 19, + "column": 19 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 9 + }, + "end": { + "line": 19, + "column": 19 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 19, + "column": 5 + }, + "end": { + "line": 19, + "column": 19 + } + } + }, + { + "type": "ExpressionStatement", + "expression": { + "type": "AssignmentExpression", + "operator": "=", + "left": { + "type": "Identifier", + "name": "a", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 5 + }, + "end": { + "line": 20, + "column": 6 + } + } + }, + "right": { + "type": "Identifier", + "name": "b", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 9 + }, + "end": { + "line": 20, + "column": 10 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 5 + }, + "end": { + "line": 20, + "column": 10 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 5 + }, + "end": { + "line": 20, + "column": 10 + } + } + } + ], + "loc": { + "start": { + "line": 17, + "column": 1 + }, + "end": { + "line": 21, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 14 + }, + "end": { + "line": 21, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 14 + }, + "end": { + "line": 21, + "column": 2 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 21, + "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": 22, + "column": 1 + } + } +} +TypeError: Assigning a non-functional variable "b" to a functional type [assignment_non-functional_variable_to_functional_type_1.ets:20:9] diff --git a/ets2panda/test/parser/ets/assignment_non-functional_variable_to_functional_type_1.ets b/ets2panda/test/parser/ets/assignment_non-functional_variable_to_functional_type_1.ets new file mode 100644 index 0000000000..fec2befa80 --- /dev/null +++ b/ets2panda/test/parser/ets/assignment_non-functional_variable_to_functional_type_1.ets @@ -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. + */ + +function main() +{ + let a: () => int + let b: int = 5 + a = b +} -- Gitee From 4251c4a691f9b95526c69301fa074c2e8250850d Mon Sep 17 00:00:00 2001 From: Molokanov Yaroslav Date: Thu, 28 Dec 2023 14:18:41 +0300 Subject: [PATCH 08/19] ValidateSignature method fix for CodeCheck Reducing ValidateSignature method cyclomatic complexity and size Signed-off-by: Molokanov Yaroslav --- ets2panda/checker/ETSchecker.h | 5 + ets2panda/checker/ets/function.cpp | 163 +++++++++++++++++------------ 2 files changed, 99 insertions(+), 69 deletions(-) diff --git a/ets2panda/checker/ETSchecker.h b/ets2panda/checker/ETSchecker.h index 60b5410d3c..44cd7dfbda 100644 --- a/ets2panda/checker/ETSchecker.h +++ b/ets2panda/checker/ETSchecker.h @@ -313,6 +313,11 @@ public: Signature *ValidateSignature(Signature *signature, const ir::TSTypeParameterInstantiation *typeArguments, const ArenaVector &arguments, const lexer::SourcePosition &pos, TypeRelationFlag initialFlags, const std::vector &argTypeInferenceRequired); + bool ValidateSignatureRequiredParams(Signature *substitutedSig, const ArenaVector &arguments, + TypeRelationFlag flags, const std::vector &argTypeInferenceRequired, + bool throwError); + bool ValidateSignatureRestParams(Signature *substitutedSig, const ArenaVector &arguments, + TypeRelationFlag flags, bool throwError); Signature *ValidateSignatures(ArenaVector &signatures, const ir::TSTypeParameterInstantiation *typeArguments, const ArenaVector &arguments, const lexer::SourcePosition &pos, diff --git a/ets2panda/checker/ets/function.cpp b/ets2panda/checker/ets/function.cpp index e6f781ed57..7dfecd77be 100644 --- a/ets2panda/checker/ets/function.cpp +++ b/ets2panda/checker/ets/function.cpp @@ -182,38 +182,14 @@ Signature *ETSChecker::ValidateParameterlessConstructor(Signature *signature, co return signature; } -Signature *ETSChecker::ValidateSignature(Signature *signature, const ir::TSTypeParameterInstantiation *typeArguments, - const ArenaVector &arguments, - const lexer::SourcePosition &pos, TypeRelationFlag flags, - const std::vector &argTypeInferenceRequired) +bool ETSChecker::ValidateSignatureRequiredParams(Signature *substitutedSig, + const ArenaVector &arguments, TypeRelationFlag flags, + const std::vector &argTypeInferenceRequired, bool throwError) { - if (signature->Function()->IsDefaultParamProxy() && ((flags & TypeRelationFlag::CHECK_PROXY) == 0)) { - return nullptr; - } - - Signature *substitutedSig = MaybeSubstituteTypeParameters(this, signature, typeArguments, arguments, pos, flags); - if (substitutedSig == nullptr) { - return nullptr; - } - - auto const hasRestParameter = substitutedSig->RestVar() != nullptr; std::size_t const argumentCount = arguments.size(); std::size_t const parameterCount = substitutedSig->MinArgCount(); - auto const throwError = (flags & TypeRelationFlag::NO_THROW) == 0; - - if (!signature->Function()->IsDefaultParamProxy()) { - if (argumentCount < parameterCount || (argumentCount > parameterCount && !hasRestParameter)) { - if (throwError) { - ThrowTypeError({"Expected ", parameterCount, " arguments, got ", argumentCount, "."}, pos); - } - return nullptr; - } - } - - // Check all required formal parameter(s) first - auto const count = std::min(parameterCount, argumentCount); - std::size_t index = 0U; - for (; index < count; ++index) { + auto count = std::min(parameterCount, argumentCount); + for (std::size_t index = 0; index < count; ++index) { auto &argument = arguments[index]; if (argument->IsObjectExpression()) { @@ -221,7 +197,7 @@ Signature *ETSChecker::ValidateSignature(Signature *signature, const ir::TSTypeP // No chance to check the argument at this point continue; } - return nullptr; + return false; } if (argument->IsMemberExpression()) { @@ -231,7 +207,7 @@ Signature *ETSChecker::ValidateSignature(Signature *signature, const ir::TSTypeP if (throwError) { ThrowTypeError("Spread argument cannot be passed for ordinary parameter.", argument->Start()); } - return nullptr; + return false; } if (argTypeInferenceRequired[index]) { @@ -241,7 +217,7 @@ Signature *ETSChecker::ValidateSignature(Signature *signature, const ir::TSTypeP if (CheckLambdaAssignable(substitutedSig->Function()->Params()[index], lambda)) { continue; } - return nullptr; + return false; } if (argument->IsArrayExpression()) { @@ -251,50 +227,99 @@ Signature *ETSChecker::ValidateSignature(Signature *signature, const ir::TSTypeP auto *const argumentType = argument->Check(this); - if (auto const invocationCtx = checker::InvocationContext( - Relation(), argument, argumentType, substitutedSig->Params()[index]->TsType(), argument->Start(), - {INVALID_CALL_ARGUMENT_1, index, INVALID_CALL_ARGUMENT_2}, flags); - !invocationCtx.IsInvocable()) { - return nullptr; + auto const invocationCtx = checker::InvocationContext( + Relation(), argument, argumentType, substitutedSig->Params()[index]->TsType(), argument->Start(), + {INVALID_CALL_ARGUMENT_1, index, INVALID_CALL_ARGUMENT_2}, flags); + if (!invocationCtx.IsInvocable()) { + return false; } } - // Check rest parameter(s) if any exists - if (hasRestParameter && index < argumentCount) { - auto const restCount = argumentCount - index; - - for (; index < argumentCount; ++index) { - auto &argument = arguments[index]; - - if (argument->IsSpreadElement()) { - if (restCount > 1U) { - if (throwError) { - ThrowTypeError("Spread argument for the rest parameter can be only one.", argument->Start()); - } - return nullptr; - } + return true; +} - auto *const restArgument = argument->AsSpreadElement()->Argument(); - auto *const argumentType = restArgument->Check(this); +bool ETSChecker::ValidateSignatureRestParams(Signature *substitutedSig, const ArenaVector &arguments, + TypeRelationFlag flags, bool throwError) +{ + std::size_t const argumentCount = arguments.size(); + std::size_t const parameterCount = substitutedSig->MinArgCount(); + auto count = std::min(parameterCount, argumentCount); + auto const restCount = argumentCount - count; - if (auto const invocationCtx = checker::InvocationContext( - Relation(), restArgument, argumentType, substitutedSig->RestVar()->TsType(), argument->Start(), - {INVALID_CALL_ARGUMENT_1, index, INVALID_CALL_ARGUMENT_3}, flags); - !invocationCtx.IsInvocable()) { - return nullptr; - } - } else { - auto *const argumentType = argument->Check(this); - - if (auto const invocationCtx = checker::InvocationContext( - Relation(), argument, argumentType, - substitutedSig->RestVar()->TsType()->AsETSArrayType()->ElementType(), argument->Start(), - {INVALID_CALL_ARGUMENT_1, index, INVALID_CALL_ARGUMENT_3}, flags); - !invocationCtx.IsInvocable()) { - return nullptr; - } + for (std::size_t index = count; index < argumentCount; ++index) { + auto &argument = arguments[index]; + + if (!argument->IsSpreadElement()) { + auto const invocationCtx = checker::InvocationContext( + Relation(), argument, argument->Check(this), + substitutedSig->RestVar()->TsType()->AsETSArrayType()->ElementType(), argument->Start(), + {INVALID_CALL_ARGUMENT_1, index, INVALID_CALL_ARGUMENT_3}, flags); + if (!invocationCtx.IsInvocable()) { + return false; } + continue; } + + if (restCount > 1U) { + if (throwError) { + ThrowTypeError("Spread argument for the rest parameter can be only one.", argument->Start()); + } + return false; + } + + auto *const restArgument = argument->AsSpreadElement()->Argument(); + + auto const invocationCtx = checker::InvocationContext( + Relation(), restArgument, restArgument->Check(this), substitutedSig->RestVar()->TsType(), argument->Start(), + {INVALID_CALL_ARGUMENT_1, index, INVALID_CALL_ARGUMENT_3}, flags); + if (!invocationCtx.IsInvocable()) { + return false; + } + } + + return true; +} + +Signature *ETSChecker::ValidateSignature(Signature *signature, const ir::TSTypeParameterInstantiation *typeArguments, + const ArenaVector &arguments, + const lexer::SourcePosition &pos, TypeRelationFlag flags, + const std::vector &argTypeInferenceRequired) +{ + if (signature->Function()->IsDefaultParamProxy() && ((flags & TypeRelationFlag::CHECK_PROXY) == 0)) { + return nullptr; + } + + Signature *substitutedSig = MaybeSubstituteTypeParameters(this, signature, typeArguments, arguments, pos, flags); + if (substitutedSig == nullptr) { + return nullptr; + } + + auto const hasRestParameter = substitutedSig->RestVar() != nullptr; + std::size_t const argumentCount = arguments.size(); + std::size_t const parameterCount = substitutedSig->MinArgCount(); + auto const throwError = (flags & TypeRelationFlag::NO_THROW) == 0; + + if (!signature->Function()->IsDefaultParamProxy()) { + if (argumentCount < parameterCount || (argumentCount > parameterCount && !hasRestParameter)) { + if (throwError) { + ThrowTypeError({"Expected ", parameterCount, " arguments, got ", argumentCount, "."}, pos); + } + return nullptr; + } + } + + auto count = std::min(parameterCount, argumentCount); + // Check all required formal parameter(s) first + if (!ValidateSignatureRequiredParams(substitutedSig, arguments, flags, argTypeInferenceRequired, throwError)) { + return nullptr; + } + + // Check rest parameter(s) if any exists + if (!hasRestParameter || count >= argumentCount) { + return substitutedSig; + } + if (!ValidateSignatureRestParams(substitutedSig, arguments, flags, throwError)) { + return nullptr; } return substitutedSig; -- Gitee From d6f029ed706f55ac70cbaa24cf3686d41d90feb1 Mon Sep 17 00:00:00 2001 From: Martin Sajti Date: Fri, 19 Jan 2024 15:46:57 +0100 Subject: [PATCH 09/19] Fix getter/setter instantiation for generic classes Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/I8XLK2 Internal issue: #15308 Test: build, new checker test Signed-off-by: Martin Sajti --- ets2panda/checker/types/ets/etsObjectType.cpp | 3 +- .../generic_class_getter_setter-expected.txt | 1064 +++++++++++++++++ .../ets/generic_class_getter_setter.ets | 23 + 3 files changed, 1089 insertions(+), 1 deletion(-) create mode 100644 ets2panda/test/compiler/ets/generic_class_getter_setter-expected.txt create mode 100644 ets2panda/test/compiler/ets/generic_class_getter_setter.ets diff --git a/ets2panda/checker/types/ets/etsObjectType.cpp b/ets2panda/checker/types/ets/etsObjectType.cpp index 17cbff5b6f..ab6f4fb3f5 100644 --- a/ets2panda/checker/types/ets/etsObjectType.cpp +++ b/ets2panda/checker/types/ets/etsObjectType.cpp @@ -780,7 +780,8 @@ static varbinder::LocalVariable *CopyPropertyWithTypeArguments(varbinder::LocalV const Substitution *substitution) { auto *const checker = relation->GetChecker()->AsETSChecker(); - auto *const copiedPropType = checker->GetTypeOfVariable(prop)->Substitute(relation, substitution); + auto *const varType = ETSChecker::IsVariableGetterSetter(prop) ? prop->TsType() : checker->GetTypeOfVariable(prop); + auto *const copiedPropType = varType->Substitute(relation, substitution); auto *const copiedProp = prop->Copy(checker->Allocator(), prop->Declaration()); copiedPropType->SetVariable(copiedProp); copiedProp->SetTsType(copiedPropType); diff --git a/ets2panda/test/compiler/ets/generic_class_getter_setter-expected.txt b/ets2panda/test/compiler/ets/generic_class_getter_setter-expected.txt new file mode 100644 index 0000000000..aa42a01485 --- /dev/null +++ b/ets2panda/test/compiler/ets/generic_class_getter_setter-expected.txt @@ -0,0 +1,1064 @@ +{ + "type": "Program", + "statements": [ + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "A", + "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": "memb", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "kind": "get", + "accessibility": "public", + "static": false, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "memb", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "returnType": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "number", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 17 + }, + "end": { + "line": 17, + "column": 23 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 17 + }, + "end": { + "line": 17, + "column": 25 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 17 + }, + "end": { + "line": 17, + "column": 25 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ReturnStatement", + "argument": { + "type": "NumberLiteral", + "value": 1, + "loc": { + "start": { + "line": 17, + "column": 33 + }, + "end": { + "line": 17, + "column": 36 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 26 + }, + "end": { + "line": 17, + "column": 37 + } + } + } + ], + "loc": { + "start": { + "line": 17, + "column": 24 + }, + "end": { + "line": 17, + "column": 39 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 13 + }, + "end": { + "line": 17, + "column": 39 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 13 + }, + "end": { + "line": 17, + "column": 39 + } + } + }, + "overloads": [ + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "memb", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "kind": "set", + "accessibility": "public", + "static": false, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "memb", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "a0", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "number", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 18 + }, + "end": { + "line": 18, + "column": 24 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 18 + }, + "end": { + "line": 18, + "column": 25 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 18 + }, + "end": { + "line": 18, + "column": 25 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 14 + }, + "end": { + "line": 18, + "column": 25 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 14 + }, + "end": { + "line": 18, + "column": 25 + } + } + } + ], + "returnType": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "void", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 27 + }, + "end": { + "line": 18, + "column": 31 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 27 + }, + "end": { + "line": 18, + "column": 33 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 27 + }, + "end": { + "line": 18, + "column": 33 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [], + "loc": { + "start": { + "line": 18, + "column": 32 + }, + "end": { + "line": 18, + "column": 34 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 13 + }, + "end": { + "line": 18, + "column": 34 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 13 + }, + "end": { + "line": 18, + "column": 34 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 13 + }, + "end": { + "line": 18, + "column": 34 + } + } + } + ], + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 13 + }, + "end": { + "line": 17, + "column": 39 + } + } + }, + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "constructor", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "kind": "constructor", + "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": [], + "body": { + "type": "BlockStatement", + "statements": [], + "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": 1, + "column": 1 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 2 + }, + "end": { + "line": 19, + "column": 2 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 12 + }, + "end": { + "line": 19, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 19, + "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": "_$init$_", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": true, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "_$init$_", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "statements": [], + "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": 1, + "column": 1 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "main", + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 10 + }, + "end": { + "line": 21, + "column": 14 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": true, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "main", + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 10 + }, + "end": { + "line": 21, + "column": 14 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "returnType": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "void", + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 18 + }, + "end": { + "line": 21, + "column": 22 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 18 + }, + "end": { + "line": 21, + "column": 24 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 18 + }, + "end": { + "line": 21, + "column": 24 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "memb", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "number", + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 15 + }, + "end": { + "line": 22, + "column": 21 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 15 + }, + "end": { + "line": 22, + "column": 23 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 15 + }, + "end": { + "line": 22, + "column": 23 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 9 + }, + "end": { + "line": 22, + "column": 13 + } + } + }, + "init": { + "type": "MemberExpression", + "object": { + "type": "ETSNewClassInstanceExpression", + "typeReference": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 28 + }, + "end": { + "line": 22, + "column": 29 + } + } + }, + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Object", + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 30 + }, + "end": { + "line": 22, + "column": 36 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 30 + }, + "end": { + "line": 22, + "column": 37 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 30 + }, + "end": { + "line": 22, + "column": 37 + } + } + } + ], + "loc": { + "start": { + "line": 22, + "column": 29 + }, + "end": { + "line": 22, + "column": 37 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 28 + }, + "end": { + "line": 22, + "column": 38 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 28 + }, + "end": { + "line": 22, + "column": 38 + } + } + }, + "arguments": [], + "loc": { + "start": { + "line": 22, + "column": 24 + }, + "end": { + "line": 22, + "column": 40 + } + } + }, + "property": { + "type": "Identifier", + "name": "memb", + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 40 + }, + "end": { + "line": 22, + "column": 44 + } + } + }, + "computed": false, + "optional": false, + "loc": { + "start": { + "line": 22, + "column": 24 + }, + "end": { + "line": 22, + "column": 44 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 9 + }, + "end": { + "line": 22, + "column": 44 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 22, + "column": 5 + }, + "end": { + "line": 22, + "column": 45 + } + } + } + ], + "loc": { + "start": { + "line": 21, + "column": 23 + }, + "end": { + "line": 23, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 14 + }, + "end": { + "line": 23, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 14 + }, + "end": { + "line": 23, + "column": 2 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 1 + }, + "end": { + "line": 23, + "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": 24, + "column": 1 + } + } +} diff --git a/ets2panda/test/compiler/ets/generic_class_getter_setter.ets b/ets2panda/test/compiler/ets/generic_class_getter_setter.ets new file mode 100644 index 0000000000..3ee99d0898 --- /dev/null +++ b/ets2panda/test/compiler/ets/generic_class_getter_setter.ets @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2024 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 A { + get memb(): number { return 1.0; } + set memb(a0: number): void {} +} + +function main(): void { + let memb: number = new A().memb; +} -- Gitee From d3cfef1e5f374e9a3da8c50b0523b24843bbe6bf Mon Sep 17 00:00:00 2001 From: zhao-xinyuan8 Date: Thu, 11 Jan 2024 03:39:39 +0000 Subject: [PATCH 10/19] Fix new array creation workaround logic Remove new array creation workaround logic for ETS_ARRAY and GENERIC types. Remove errorneous case launch_expression.ets Signed-off-by: zhao-xinyuan8 --- ets2panda/checker/ETSAnalyzer.cpp | 2 - .../ets/launch_expression-expected.txt | 1265 +++++++++-------- .../test/compiler/ets/launch_expression.ets | 3 +- 3 files changed, 641 insertions(+), 629 deletions(-) diff --git a/ets2panda/checker/ETSAnalyzer.cpp b/ets2panda/checker/ETSAnalyzer.cpp index f2d00c77db..885e81d208 100644 --- a/ets2panda/checker/ETSAnalyzer.cpp +++ b/ets2panda/checker/ETSAnalyzer.cpp @@ -506,9 +506,7 @@ checker::Type *ETSAnalyzer::Check(ir::ETSNewArrayInstanceExpression *expr) const checker->ValidateArrayIndex(expr->dimension_, true); if (!elementType->HasTypeFlag(TypeFlag::ETS_PRIMITIVE) && !elementType->IsNullish() && - !elementType->HasTypeFlag(TypeFlag::GENERIC) && !elementType->HasTypeFlag(TypeFlag::ETS_ARRAY) && elementType->ToAssemblerName().str() != "Ball") { - // Check only valid for ETS_PRIMITIVE and IsNullish, GENERIC and ETS_ARRAY are workaround checks for stdlib // Ball is workaround for koala ui lib if (elementType->IsETSObjectType()) { auto *calleeObj = elementType->AsETSObjectType(); diff --git a/ets2panda/test/compiler/ets/launch_expression-expected.txt b/ets2panda/test/compiler/ets/launch_expression-expected.txt index ef4e77e942..e0578ae161 100644 --- a/ets2panda/test/compiler/ets/launch_expression-expected.txt +++ b/ets2panda/test/compiler/ets/launch_expression-expected.txt @@ -1,6 +1,131 @@ { "type": "Program", "statements": [ + { + "type": "TSTypeAliasDeclaration", + "id": { + "type": "Identifier", + "name": "P", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 6 + }, + "end": { + "line": 20, + "column": 7 + } + } + }, + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Promise", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 10 + }, + "end": { + "line": 20, + "column": 17 + } + } + }, + "typeParams": { + "type": "TSTypeParameterInstantiation", + "params": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Int", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 18 + }, + "end": { + "line": 20, + "column": 21 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 18 + }, + "end": { + "line": 20, + "column": 22 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 18 + }, + "end": { + "line": 20, + "column": 22 + } + } + } + ], + "loc": { + "start": { + "line": 20, + "column": 17 + }, + "end": { + "line": 20, + "column": 22 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 10 + }, + "end": { + "line": 20, + "column": 24 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 10 + }, + "end": { + "line": 20, + "column": 24 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 1 + }, + "end": { + "line": 21, + "column": 9 + } + } + }, { "type": "ClassDeclaration", "definition": { @@ -627,11 +752,11 @@ "decorators": [], "loc": { "start": { - "line": 20, + "line": 21, "column": 10 }, "end": { - "line": 20, + "line": 21, "column": 19 } } @@ -651,11 +776,11 @@ "decorators": [], "loc": { "start": { - "line": 20, + "line": 21, "column": 10 }, "end": { - "line": 20, + "line": 21, "column": 19 } } @@ -673,11 +798,11 @@ "type": "ETSPrimitiveType", "loc": { "start": { - "line": 20, + "line": 21, "column": 28 }, "end": { - "line": 20, + "line": 21, "column": 31 } } @@ -685,22 +810,22 @@ "decorators": [], "loc": { "start": { - "line": 20, + "line": 21, "column": 20 }, "end": { - "line": 20, + "line": 21, "column": 31 } } }, "loc": { "start": { - "line": 20, + "line": 21, "column": 20 }, "end": { - "line": 20, + "line": 21, "column": 31 } } @@ -714,11 +839,11 @@ "type": "ETSPrimitiveType", "loc": { "start": { - "line": 20, + "line": 21, "column": 41 }, "end": { - "line": 20, + "line": 21, "column": 44 } } @@ -726,22 +851,22 @@ "decorators": [], "loc": { "start": { - "line": 20, + "line": 21, "column": 33 }, "end": { - "line": 20, + "line": 21, "column": 44 } } }, "loc": { "start": { - "line": 20, + "line": 21, "column": 33 }, "end": { - "line": 20, + "line": 21, "column": 44 } } @@ -757,33 +882,33 @@ "decorators": [], "loc": { "start": { - "line": 20, + "line": 21, "column": 47 }, "end": { - "line": 20, + "line": 21, "column": 51 } } }, "loc": { "start": { - "line": 20, + "line": 21, "column": 47 }, "end": { - "line": 20, + "line": 21, "column": 53 } } }, "loc": { "start": { - "line": 20, + "line": 21, "column": 47 }, "end": { - "line": 20, + "line": 21, "column": 53 } } @@ -802,11 +927,11 @@ "decorators": [], "loc": { "start": { - "line": 21, + "line": 22, "column": 9 }, "end": { - "line": 21, + "line": 22, "column": 15 } } @@ -817,22 +942,22 @@ "decorators": [], "loc": { "start": { - "line": 21, + "line": 22, "column": 19 }, "end": { - "line": 21, + "line": 22, "column": 25 } } }, "loc": { "start": { - "line": 21, + "line": 22, "column": 9 }, "end": { - "line": 21, + "line": 22, "column": 25 } } @@ -845,11 +970,11 @@ "argument": null, "loc": { "start": { - "line": 22, + "line": 23, "column": 9 }, "end": { - "line": 22, + "line": 23, "column": 16 } } @@ -857,11 +982,11 @@ ], "loc": { "start": { - "line": 21, + "line": 22, "column": 27 }, "end": { - "line": 23, + "line": 24, "column": 6 } } @@ -869,11 +994,11 @@ "alternate": null, "loc": { "start": { - "line": 21, + "line": 22, "column": 5 }, "end": { - "line": 23, + "line": 24, "column": 6 } } @@ -890,11 +1015,11 @@ "decorators": [], "loc": { "start": { - "line": 24, + "line": 25, "column": 5 }, "end": { - "line": 24, + "line": 25, "column": 12 } } @@ -905,11 +1030,11 @@ "decorators": [], "loc": { "start": { - "line": 24, + "line": 25, "column": 13 }, "end": { - "line": 24, + "line": 25, "column": 20 } } @@ -918,11 +1043,11 @@ "optional": false, "loc": { "start": { - "line": 24, + "line": 25, "column": 5 }, "end": { - "line": 24, + "line": 25, "column": 20 } } @@ -942,11 +1067,11 @@ "value": "Values of type int are not equal: ", "loc": { "start": { - "line": 24, + "line": 25, "column": 21 }, "end": { - "line": 24, + "line": 25, "column": 57 } } @@ -957,22 +1082,22 @@ "decorators": [], "loc": { "start": { - "line": 24, + "line": 25, "column": 60 }, "end": { - "line": 24, + "line": 25, "column": 66 } } }, "loc": { "start": { - "line": 24, + "line": 25, "column": 21 }, "end": { - "line": 24, + "line": 25, "column": 66 } } @@ -982,22 +1107,22 @@ "value": " != ", "loc": { "start": { - "line": 24, + "line": 25, "column": 69 }, "end": { - "line": 24, + "line": 25, "column": 75 } } }, "loc": { "start": { - "line": 24, + "line": 25, "column": 21 }, "end": { - "line": 24, + "line": 25, "column": 75 } } @@ -1008,22 +1133,22 @@ "decorators": [], "loc": { "start": { - "line": 24, + "line": 25, "column": 78 }, "end": { - "line": 24, + "line": 25, "column": 84 } } }, "loc": { "start": { - "line": 24, + "line": 25, "column": 21 }, "end": { - "line": 24, + "line": 25, "column": 84 } } @@ -1032,22 +1157,22 @@ "optional": false, "loc": { "start": { - "line": 24, + "line": 25, "column": 5 }, "end": { - "line": 24, + "line": 25, "column": 85 } } }, "loc": { "start": { - "line": 24, + "line": 25, "column": 5 }, "end": { - "line": 24, + "line": 25, "column": 86 } } @@ -1066,33 +1191,33 @@ "decorators": [], "loc": { "start": { - "line": 25, + "line": 26, "column": 15 }, "end": { - "line": 25, + "line": 26, "column": 20 } } }, "loc": { "start": { - "line": 25, + "line": 26, "column": 15 }, "end": { - "line": 25, + "line": 26, "column": 21 } } }, "loc": { "start": { - "line": 25, + "line": 26, "column": 15 }, "end": { - "line": 25, + "line": 26, "column": 21 } } @@ -1100,22 +1225,22 @@ "arguments": [], "loc": { "start": { - "line": 25, + "line": 26, "column": 11 }, "end": { - "line": 25, + "line": 26, "column": 23 } } }, "loc": { "start": { - "line": 25, + "line": 26, "column": 5 }, "end": { - "line": 25, + "line": 26, "column": 23 } } @@ -1123,33 +1248,33 @@ ], "loc": { "start": { - "line": 20, + "line": 21, "column": 52 }, "end": { - "line": 26, + "line": 27, "column": 2 } } }, "loc": { "start": { - "line": 20, + "line": 21, "column": 19 }, "end": { - "line": 26, + "line": 27, "column": 2 } } }, "loc": { "start": { - "line": 20, + "line": 21, "column": 19 }, "end": { - "line": 26, + "line": 27, "column": 2 } } @@ -1158,11 +1283,11 @@ "decorators": [], "loc": { "start": { - "line": 20, + "line": 21, "column": 1 }, "end": { - "line": 26, + "line": 27, "column": 2 } } @@ -1175,11 +1300,11 @@ "decorators": [], "loc": { "start": { - "line": 27, + "line": 28, "column": 10 }, "end": { - "line": 27, + "line": 28, "column": 14 } } @@ -1199,11 +1324,11 @@ "decorators": [], "loc": { "start": { - "line": 27, + "line": 28, "column": 10 }, "end": { - "line": 27, + "line": 28, "column": 14 } } @@ -1221,11 +1346,11 @@ "type": "ETSPrimitiveType", "loc": { "start": { - "line": 27, + "line": 28, "column": 18 }, "end": { - "line": 27, + "line": 28, "column": 21 } } @@ -1233,22 +1358,22 @@ "decorators": [], "loc": { "start": { - "line": 27, + "line": 28, "column": 15 }, "end": { - "line": 27, + "line": 28, "column": 21 } } }, "loc": { "start": { - "line": 27, + "line": 28, "column": 15 }, "end": { - "line": 27, + "line": 28, "column": 21 } } @@ -1264,33 +1389,33 @@ "decorators": [], "loc": { "start": { - "line": 27, + "line": 28, "column": 25 }, "end": { - "line": 27, + "line": 28, "column": 28 } } }, "loc": { "start": { - "line": 27, + "line": 28, "column": 25 }, "end": { - "line": 27, + "line": 28, "column": 30 } } }, "loc": { "start": { - "line": 27, + "line": 28, "column": 25 }, "end": { - "line": 27, + "line": 28, "column": 30 } } @@ -1312,11 +1437,11 @@ "decorators": [], "loc": { "start": { - "line": 28, + "line": 29, "column": 9 }, "end": { - "line": 28, + "line": 29, "column": 10 } } @@ -1326,22 +1451,22 @@ "value": 0, "loc": { "start": { - "line": 28, + "line": 29, "column": 14 }, "end": { - "line": 28, + "line": 29, "column": 15 } } }, "loc": { "start": { - "line": 28, + "line": 29, "column": 9 }, "end": { - "line": 28, + "line": 29, "column": 15 } } @@ -1355,11 +1480,11 @@ "decorators": [], "loc": { "start": { - "line": 28, + "line": 29, "column": 19 }, "end": { - "line": 28, + "line": 29, "column": 20 } } @@ -1370,33 +1495,33 @@ "decorators": [], "loc": { "start": { - "line": 28, + "line": 29, "column": 23 }, "end": { - "line": 28, + "line": 29, "column": 28 } } }, "loc": { "start": { - "line": 28, + "line": 29, "column": 19 }, "end": { - "line": 28, + "line": 29, "column": 28 } } }, "loc": { "start": { - "line": 28, + "line": 29, "column": 9 }, "end": { - "line": 28, + "line": 29, "column": 28 } } @@ -1414,11 +1539,11 @@ "decorators": [], "loc": { "start": { - "line": 29, + "line": 30, "column": 16 }, "end": { - "line": 29, + "line": 30, "column": 17 } } @@ -1429,11 +1554,11 @@ "decorators": [], "loc": { "start": { - "line": 29, + "line": 30, "column": 18 }, "end": { - "line": 29, + "line": 30, "column": 19 } } @@ -1442,22 +1567,22 @@ "optional": false, "loc": { "start": { - "line": 29, + "line": 30, "column": 16 }, "end": { - "line": 29, + "line": 30, "column": 20 } } }, "loc": { "start": { - "line": 29, + "line": 30, "column": 9 }, "end": { - "line": 29, + "line": 30, "column": 21 } } @@ -1465,11 +1590,11 @@ ], "loc": { "start": { - "line": 28, + "line": 29, "column": 30 }, "end": { - "line": 30, + "line": 31, "column": 6 } } @@ -1477,11 +1602,11 @@ "alternate": null, "loc": { "start": { - "line": 28, + "line": 29, "column": 5 }, "end": { - "line": 30, + "line": 31, "column": 6 } } @@ -1502,116 +1627,60 @@ "type": "ETSTypeReferencePart", "name": { "type": "Identifier", - "name": "Promise", + "name": "P", "decorators": [], "loc": { "start": { - "line": 31, + "line": 32, "column": 12 }, "end": { - "line": 31, - "column": 19 - } - } - }, - "typeParams": { - "type": "TSTypeParameterInstantiation", - "params": [ - { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "Int", - "decorators": [], - "loc": { - "start": { - "line": 31, - "column": 20 - }, - "end": { - "line": 31, - "column": 23 - } - } - }, - "loc": { - "start": { - "line": 31, - "column": 20 - }, - "end": { - "line": 31, - "column": 24 - } - } - }, - "loc": { - "start": { - "line": 31, - "column": 20 - }, - "end": { - "line": 31, - "column": 24 - } - } - } - ], - "loc": { - "start": { - "line": 31, - "column": 19 - }, - "end": { - "line": 31, - "column": 24 + "line": 32, + "column": 13 } } }, "loc": { "start": { - "line": 31, + "line": 32, "column": 12 }, "end": { - "line": 31, - "column": 25 + "line": 32, + "column": 14 } } }, "loc": { "start": { - "line": 31, + "line": 32, "column": 12 }, "end": { - "line": 31, - "column": 25 + "line": 32, + "column": 14 } } }, "loc": { "start": { - "line": 31, - "column": 27 + "line": 32, + "column": 16 }, "end": { - "line": 31, - "column": 28 + "line": 32, + "column": 17 } } }, "decorators": [], "loc": { "start": { - "line": 31, + "line": 32, "column": 9 }, "end": { - "line": 31, + "line": 32, "column": 10 } } @@ -1624,94 +1693,38 @@ "type": "ETSTypeReferencePart", "name": { "type": "Identifier", - "name": "Promise", + "name": "P", "decorators": [], "loc": { "start": { - "line": 31, - "column": 33 - }, - "end": { - "line": 31, - "column": 40 - } - } - }, - "typeParams": { - "type": "TSTypeParameterInstantiation", - "params": [ - { - "type": "ETSTypeReference", - "part": { - "type": "ETSTypeReferencePart", - "name": { - "type": "Identifier", - "name": "Int", - "decorators": [], - "loc": { - "start": { - "line": 31, - "column": 41 - }, - "end": { - "line": 31, - "column": 44 - } - } - }, - "loc": { - "start": { - "line": 31, - "column": 41 - }, - "end": { - "line": 31, - "column": 45 - } - } - }, - "loc": { - "start": { - "line": 31, - "column": 41 - }, - "end": { - "line": 31, - "column": 45 - } - } - } - ], - "loc": { - "start": { - "line": 31, - "column": 40 + "line": 32, + "column": 22 }, "end": { - "line": 31, - "column": 45 + "line": 32, + "column": 23 } } }, "loc": { "start": { - "line": 31, - "column": 33 + "line": 32, + "column": 22 }, "end": { - "line": 31, - "column": 46 + "line": 32, + "column": 24 } } }, "loc": { "start": { - "line": 31, - "column": 33 + "line": 32, + "column": 22 }, "end": { - "line": 31, - "column": 46 + "line": 32, + "column": 24 } } }, @@ -1721,34 +1734,34 @@ "decorators": [], "loc": { "start": { - "line": 31, - "column": 46 + "line": 32, + "column": 24 }, "end": { - "line": 31, - "column": 51 + "line": 32, + "column": 29 } } }, "loc": { "start": { - "line": 31, - "column": 29 + "line": 32, + "column": 18 }, "end": { - "line": 31, - "column": 52 + "line": 32, + "column": 30 } } }, "loc": { "start": { - "line": 31, + "line": 32, "column": 9 }, "end": { - "line": 31, - "column": 52 + "line": 32, + "column": 30 } } } @@ -1756,12 +1769,12 @@ "kind": "let", "loc": { "start": { - "line": 31, + "line": 32, "column": 5 }, "end": { - "line": 31, - "column": 52 + "line": 32, + "column": 30 } } }, @@ -1778,11 +1791,11 @@ "decorators": [], "loc": { "start": { - "line": 32, + "line": 33, "column": 14 }, "end": { - "line": 32, + "line": 33, "column": 15 } } @@ -1792,22 +1805,22 @@ "value": 0, "loc": { "start": { - "line": 32, + "line": 33, "column": 18 }, "end": { - "line": 32, + "line": 33, "column": 19 } } }, "loc": { "start": { - "line": 32, + "line": 33, "column": 14 }, "end": { - "line": 32, + "line": 33, "column": 19 } } @@ -1816,11 +1829,11 @@ "kind": "let", "loc": { "start": { - "line": 32, + "line": 33, "column": 10 }, "end": { - "line": 32, + "line": 33, "column": 19 } } @@ -1834,11 +1847,11 @@ "decorators": [], "loc": { "start": { - "line": 32, + "line": 33, "column": 21 }, "end": { - "line": 32, + "line": 33, "column": 22 } } @@ -1849,22 +1862,22 @@ "decorators": [], "loc": { "start": { - "line": 32, + "line": 33, "column": 25 }, "end": { - "line": 32, + "line": 33, "column": 30 } } }, "loc": { "start": { - "line": 32, + "line": 33, "column": 21 }, "end": { - "line": 32, + "line": 33, "column": 30 } } @@ -1879,22 +1892,22 @@ "decorators": [], "loc": { "start": { - "line": 32, + "line": 33, "column": 34 }, "end": { - "line": 32, + "line": 33, "column": 35 } } }, "loc": { "start": { - "line": 32, + "line": 33, "column": 32 }, "end": { - "line": 32, + "line": 33, "column": 35 } } @@ -1915,11 +1928,11 @@ "decorators": [], "loc": { "start": { - "line": 33, + "line": 34, "column": 9 }, "end": { - "line": 33, + "line": 34, "column": 10 } } @@ -1930,11 +1943,11 @@ "decorators": [], "loc": { "start": { - "line": 33, + "line": 34, "column": 11 }, "end": { - "line": 33, + "line": 34, "column": 12 } } @@ -1943,11 +1956,11 @@ "optional": false, "loc": { "start": { - "line": 33, + "line": 34, "column": 9 }, "end": { - "line": 33, + "line": 34, "column": 13 } } @@ -1962,11 +1975,11 @@ "decorators": [], "loc": { "start": { - "line": 33, + "line": 34, "column": 23 }, "end": { - "line": 33, + "line": 34, "column": 27 } } @@ -1984,11 +1997,11 @@ "decorators": [], "loc": { "start": { - "line": 33, + "line": 34, "column": 28 }, "end": { - "line": 33, + "line": 34, "column": 29 } } @@ -1998,22 +2011,22 @@ "value": 1, "loc": { "start": { - "line": 33, + "line": 34, "column": 30 }, "end": { - "line": 33, + "line": 34, "column": 31 } } }, "loc": { "start": { - "line": 33, + "line": 34, "column": 28 }, "end": { - "line": 33, + "line": 34, "column": 31 } } @@ -2024,22 +2037,22 @@ "decorators": [], "loc": { "start": { - "line": 33, + "line": 34, "column": 32 }, "end": { - "line": 33, + "line": 34, "column": 33 } } }, "loc": { "start": { - "line": 33, + "line": 34, "column": 28 }, "end": { - "line": 33, + "line": 34, "column": 33 } } @@ -2048,44 +2061,44 @@ "optional": false, "loc": { "start": { - "line": 33, + "line": 34, "column": 23 }, "end": { - "line": 33, + "line": 34, "column": 34 } } }, "loc": { "start": { - "line": 33, + "line": 34, "column": 16 }, "end": { - "line": 33, + "line": 34, "column": 34 } } }, "loc": { "start": { - "line": 33, + "line": 34, "column": 9 }, "end": { - "line": 33, + "line": 34, "column": 34 } } }, "loc": { "start": { - "line": 33, + "line": 34, "column": 9 }, "end": { - "line": 33, + "line": 34, "column": 35 } } @@ -2093,22 +2106,22 @@ ], "loc": { "start": { - "line": 32, + "line": 33, "column": 37 }, "end": { - "line": 34, + "line": 35, "column": 6 } } }, "loc": { "start": { - "line": 32, + "line": 33, "column": 5 }, "end": { - "line": 34, + "line": 35, "column": 6 } } @@ -2124,11 +2137,11 @@ "decorators": [], "loc": { "start": { - "line": 35, + "line": 36, "column": 9 }, "end": { - "line": 35, + "line": 36, "column": 15 } } @@ -2138,22 +2151,22 @@ "value": 0, "loc": { "start": { - "line": 35, + "line": 36, "column": 18 }, "end": { - "line": 35, + "line": 36, "column": 19 } } }, "loc": { "start": { - "line": 35, + "line": 36, "column": 9 }, "end": { - "line": 35, + "line": 36, "column": 19 } } @@ -2162,11 +2175,11 @@ "kind": "let", "loc": { "start": { - "line": 35, + "line": 36, "column": 5 }, "end": { - "line": 35, + "line": 36, "column": 19 } } @@ -2184,11 +2197,11 @@ "decorators": [], "loc": { "start": { - "line": 36, + "line": 37, "column": 14 }, "end": { - "line": 36, + "line": 37, "column": 15 } } @@ -2198,22 +2211,22 @@ "value": 0, "loc": { "start": { - "line": 36, + "line": 37, "column": 18 }, "end": { - "line": 36, + "line": 37, "column": 19 } } }, "loc": { "start": { - "line": 36, + "line": 37, "column": 14 }, "end": { - "line": 36, + "line": 37, "column": 19 } } @@ -2222,11 +2235,11 @@ "kind": "let", "loc": { "start": { - "line": 36, + "line": 37, "column": 10 }, "end": { - "line": 36, + "line": 37, "column": 19 } } @@ -2240,11 +2253,11 @@ "decorators": [], "loc": { "start": { - "line": 36, + "line": 37, "column": 21 }, "end": { - "line": 36, + "line": 37, "column": 22 } } @@ -2255,22 +2268,22 @@ "decorators": [], "loc": { "start": { - "line": 36, + "line": 37, "column": 25 }, "end": { - "line": 36, + "line": 37, "column": 30 } } }, "loc": { "start": { - "line": 36, + "line": 37, "column": 21 }, "end": { - "line": 36, + "line": 37, "column": 30 } } @@ -2285,22 +2298,22 @@ "decorators": [], "loc": { "start": { - "line": 36, + "line": 37, "column": 34 }, "end": { - "line": 36, + "line": 37, "column": 35 } } }, "loc": { "start": { - "line": 36, + "line": 37, "column": 32 }, "end": { - "line": 36, + "line": 37, "column": 35 } } @@ -2319,11 +2332,11 @@ "decorators": [], "loc": { "start": { - "line": 37, + "line": 38, "column": 9 }, "end": { - "line": 37, + "line": 38, "column": 15 } } @@ -2337,11 +2350,11 @@ "decorators": [], "loc": { "start": { - "line": 37, + "line": 38, "column": 18 }, "end": { - "line": 37, + "line": 38, "column": 24 } } @@ -2361,11 +2374,11 @@ "decorators": [], "loc": { "start": { - "line": 37, + "line": 38, "column": 27 }, "end": { - "line": 37, + "line": 38, "column": 28 } } @@ -2376,11 +2389,11 @@ "decorators": [], "loc": { "start": { - "line": 37, + "line": 38, "column": 29 }, "end": { - "line": 37, + "line": 38, "column": 30 } } @@ -2389,11 +2402,11 @@ "optional": false, "loc": { "start": { - "line": 37, + "line": 38, "column": 27 }, "end": { - "line": 37, + "line": 38, "column": 31 } } @@ -2404,11 +2417,11 @@ "decorators": [], "loc": { "start": { - "line": 37, + "line": 38, "column": 32 }, "end": { - "line": 37, + "line": 38, "column": 47 } } @@ -2417,11 +2430,11 @@ "optional": false, "loc": { "start": { - "line": 37, + "line": 38, "column": 27 }, "end": { - "line": 37, + "line": 38, "column": 47 } } @@ -2430,11 +2443,11 @@ "optional": false, "loc": { "start": { - "line": 37, + "line": 38, "column": 27 }, "end": { - "line": 37, + "line": 38, "column": 49 } } @@ -2447,11 +2460,11 @@ "decorators": [], "loc": { "start": { - "line": 37, + "line": 38, "column": 52 }, "end": { - "line": 37, + "line": 38, "column": 53 } } @@ -2462,11 +2475,11 @@ "decorators": [], "loc": { "start": { - "line": 37, + "line": 38, "column": 54 }, "end": { - "line": 37, + "line": 38, "column": 55 } } @@ -2475,55 +2488,55 @@ "optional": false, "loc": { "start": { - "line": 37, + "line": 38, "column": 52 }, "end": { - "line": 37, + "line": 38, "column": 56 } } }, "loc": { "start": { - "line": 37, + "line": 38, "column": 27 }, "end": { - "line": 37, + "line": 38, "column": 56 } } }, "loc": { "start": { - "line": 37, + "line": 38, "column": 18 }, "end": { - "line": 37, + "line": 38, "column": 56 } } }, "loc": { "start": { - "line": 37, + "line": 38, "column": 9 }, "end": { - "line": 37, + "line": 38, "column": 56 } } }, "loc": { "start": { - "line": 37, + "line": 38, "column": 9 }, "end": { - "line": 37, + "line": 38, "column": 57 } } @@ -2531,22 +2544,22 @@ ], "loc": { "start": { - "line": 36, + "line": 37, "column": 37 }, "end": { - "line": 38, + "line": 39, "column": 6 } } }, "loc": { "start": { - "line": 36, + "line": 37, "column": 5 }, "end": { - "line": 38, + "line": 39, "column": 6 } } @@ -2559,22 +2572,22 @@ "decorators": [], "loc": { "start": { - "line": 39, + "line": 40, "column": 12 }, "end": { - "line": 39, + "line": 40, "column": 18 } } }, "loc": { "start": { - "line": 39, + "line": 40, "column": 5 }, "end": { - "line": 39, + "line": 40, "column": 19 } } @@ -2582,33 +2595,33 @@ ], "loc": { "start": { - "line": 27, + "line": 28, "column": 29 }, "end": { - "line": 40, + "line": 41, "column": 2 } } }, "loc": { "start": { - "line": 27, + "line": 28, "column": 14 }, "end": { - "line": 40, + "line": 41, "column": 2 } } }, "loc": { "start": { - "line": 27, + "line": 28, "column": 14 }, "end": { - "line": 40, + "line": 41, "column": 2 } } @@ -2617,11 +2630,11 @@ "decorators": [], "loc": { "start": { - "line": 27, + "line": 28, "column": 1 }, "end": { - "line": 40, + "line": 41, "column": 2 } } @@ -2634,11 +2647,11 @@ "decorators": [], "loc": { "start": { - "line": 41, + "line": 42, "column": 10 }, "end": { - "line": 41, + "line": 42, "column": 18 } } @@ -2658,11 +2671,11 @@ "decorators": [], "loc": { "start": { - "line": 41, + "line": 42, "column": 10 }, "end": { - "line": 41, + "line": 42, "column": 18 } } @@ -2680,11 +2693,11 @@ "type": "ETSPrimitiveType", "loc": { "start": { - "line": 41, + "line": 42, "column": 22 }, "end": { - "line": 41, + "line": 42, "column": 25 } } @@ -2692,22 +2705,22 @@ "decorators": [], "loc": { "start": { - "line": 41, + "line": 42, "column": 19 }, "end": { - "line": 41, + "line": 42, "column": 25 } } }, "loc": { "start": { - "line": 41, + "line": 42, "column": 19 }, "end": { - "line": 41, + "line": 42, "column": 25 } } @@ -2717,11 +2730,11 @@ "type": "ETSPrimitiveType", "loc": { "start": { - "line": 41, + "line": 42, "column": 29 }, "end": { - "line": 41, + "line": 42, "column": 32 } } @@ -2743,11 +2756,11 @@ "decorators": [], "loc": { "start": { - "line": 42, + "line": 43, "column": 9 }, "end": { - "line": 42, + "line": 43, "column": 10 } } @@ -2757,22 +2770,22 @@ "value": 0, "loc": { "start": { - "line": 42, + "line": 43, "column": 14 }, "end": { - "line": 42, + "line": 43, "column": 15 } } }, "loc": { "start": { - "line": 42, + "line": 43, "column": 9 }, "end": { - "line": 42, + "line": 43, "column": 15 } } @@ -2786,11 +2799,11 @@ "decorators": [], "loc": { "start": { - "line": 42, + "line": 43, "column": 19 }, "end": { - "line": 42, + "line": 43, "column": 20 } } @@ -2801,33 +2814,33 @@ "decorators": [], "loc": { "start": { - "line": 42, + "line": 43, "column": 23 }, "end": { - "line": 42, + "line": 43, "column": 28 } } }, "loc": { "start": { - "line": 42, + "line": 43, "column": 19 }, "end": { - "line": 42, + "line": 43, "column": 28 } } }, "loc": { "start": { - "line": 42, + "line": 43, "column": 9 }, "end": { - "line": 42, + "line": 43, "column": 28 } } @@ -2845,11 +2858,11 @@ "decorators": [], "loc": { "start": { - "line": 43, + "line": 44, "column": 16 }, "end": { - "line": 43, + "line": 44, "column": 17 } } @@ -2860,11 +2873,11 @@ "decorators": [], "loc": { "start": { - "line": 43, + "line": 44, "column": 18 }, "end": { - "line": 43, + "line": 44, "column": 19 } } @@ -2873,22 +2886,22 @@ "optional": false, "loc": { "start": { - "line": 43, + "line": 44, "column": 16 }, "end": { - "line": 43, + "line": 44, "column": 20 } } }, "loc": { "start": { - "line": 43, + "line": 44, "column": 9 }, "end": { - "line": 43, + "line": 44, "column": 21 } } @@ -2896,11 +2909,11 @@ ], "loc": { "start": { - "line": 42, + "line": 43, "column": 30 }, "end": { - "line": 44, + "line": 45, "column": 6 } } @@ -2908,11 +2921,11 @@ "alternate": null, "loc": { "start": { - "line": 42, + "line": 43, "column": 5 }, "end": { - "line": 44, + "line": 45, "column": 6 } } @@ -2928,11 +2941,11 @@ "decorators": [], "loc": { "start": { - "line": 45, + "line": 46, "column": 9 }, "end": { - "line": 45, + "line": 46, "column": 15 } } @@ -2942,22 +2955,22 @@ "value": 0, "loc": { "start": { - "line": 45, + "line": 46, "column": 18 }, "end": { - "line": 45, + "line": 46, "column": 19 } } }, "loc": { "start": { - "line": 45, + "line": 46, "column": 9 }, "end": { - "line": 45, + "line": 46, "column": 19 } } @@ -2966,11 +2979,11 @@ "kind": "let", "loc": { "start": { - "line": 45, + "line": 46, "column": 5 }, "end": { - "line": 45, + "line": 46, "column": 19 } } @@ -2988,11 +3001,11 @@ "decorators": [], "loc": { "start": { - "line": 46, + "line": 47, "column": 14 }, "end": { - "line": 46, + "line": 47, "column": 15 } } @@ -3002,22 +3015,22 @@ "value": 0, "loc": { "start": { - "line": 46, + "line": 47, "column": 18 }, "end": { - "line": 46, + "line": 47, "column": 19 } } }, "loc": { "start": { - "line": 46, + "line": 47, "column": 14 }, "end": { - "line": 46, + "line": 47, "column": 19 } } @@ -3026,11 +3039,11 @@ "kind": "let", "loc": { "start": { - "line": 46, + "line": 47, "column": 10 }, "end": { - "line": 46, + "line": 47, "column": 19 } } @@ -3044,11 +3057,11 @@ "decorators": [], "loc": { "start": { - "line": 46, + "line": 47, "column": 21 }, "end": { - "line": 46, + "line": 47, "column": 22 } } @@ -3059,22 +3072,22 @@ "decorators": [], "loc": { "start": { - "line": 46, + "line": 47, "column": 25 }, "end": { - "line": 46, + "line": 47, "column": 30 } } }, "loc": { "start": { - "line": 46, + "line": 47, "column": 21 }, "end": { - "line": 46, + "line": 47, "column": 30 } } @@ -3089,22 +3102,22 @@ "decorators": [], "loc": { "start": { - "line": 46, + "line": 47, "column": 34 }, "end": { - "line": 46, + "line": 47, "column": 35 } } }, "loc": { "start": { - "line": 46, + "line": 47, "column": 32 }, "end": { - "line": 46, + "line": 47, "column": 35 } } @@ -3123,11 +3136,11 @@ "decorators": [], "loc": { "start": { - "line": 47, + "line": 48, "column": 9 }, "end": { - "line": 47, + "line": 48, "column": 15 } } @@ -3141,11 +3154,11 @@ "decorators": [], "loc": { "start": { - "line": 47, + "line": 48, "column": 18 }, "end": { - "line": 47, + "line": 48, "column": 24 } } @@ -3161,11 +3174,11 @@ "decorators": [], "loc": { "start": { - "line": 47, + "line": 48, "column": 27 }, "end": { - "line": 47, + "line": 48, "column": 35 } } @@ -3183,11 +3196,11 @@ "decorators": [], "loc": { "start": { - "line": 47, + "line": 48, "column": 36 }, "end": { - "line": 47, + "line": 48, "column": 37 } } @@ -3197,22 +3210,22 @@ "value": 1, "loc": { "start": { - "line": 47, + "line": 48, "column": 38 }, "end": { - "line": 47, + "line": 48, "column": 39 } } }, "loc": { "start": { - "line": 47, + "line": 48, "column": 36 }, "end": { - "line": 47, + "line": 48, "column": 39 } } @@ -3223,22 +3236,22 @@ "decorators": [], "loc": { "start": { - "line": 47, + "line": 48, "column": 40 }, "end": { - "line": 47, + "line": 48, "column": 41 } } }, "loc": { "start": { - "line": 47, + "line": 48, "column": 36 }, "end": { - "line": 47, + "line": 48, "column": 41 } } @@ -3247,11 +3260,11 @@ "optional": false, "loc": { "start": { - "line": 47, + "line": 48, "column": 27 }, "end": { - "line": 47, + "line": 48, "column": 42 } } @@ -3264,11 +3277,11 @@ "decorators": [], "loc": { "start": { - "line": 47, + "line": 48, "column": 45 }, "end": { - "line": 47, + "line": 48, "column": 46 } } @@ -3279,11 +3292,11 @@ "decorators": [], "loc": { "start": { - "line": 47, + "line": 48, "column": 47 }, "end": { - "line": 47, + "line": 48, "column": 48 } } @@ -3292,55 +3305,55 @@ "optional": false, "loc": { "start": { - "line": 47, + "line": 48, "column": 45 }, "end": { - "line": 47, + "line": 48, "column": 49 } } }, "loc": { "start": { - "line": 47, + "line": 48, "column": 27 }, "end": { - "line": 47, + "line": 48, "column": 49 } } }, "loc": { "start": { - "line": 47, + "line": 48, "column": 18 }, "end": { - "line": 47, + "line": 48, "column": 49 } } }, "loc": { "start": { - "line": 47, + "line": 48, "column": 9 }, "end": { - "line": 47, + "line": 48, "column": 49 } } }, "loc": { "start": { - "line": 47, + "line": 48, "column": 9 }, "end": { - "line": 47, + "line": 48, "column": 50 } } @@ -3348,22 +3361,22 @@ ], "loc": { "start": { - "line": 46, + "line": 47, "column": 37 }, "end": { - "line": 48, + "line": 49, "column": 6 } } }, "loc": { "start": { - "line": 46, + "line": 47, "column": 5 }, "end": { - "line": 48, + "line": 49, "column": 6 } } @@ -3376,22 +3389,22 @@ "decorators": [], "loc": { "start": { - "line": 49, + "line": 50, "column": 12 }, "end": { - "line": 49, + "line": 50, "column": 18 } } }, "loc": { "start": { - "line": 49, + "line": 50, "column": 5 }, "end": { - "line": 49, + "line": 50, "column": 19 } } @@ -3399,33 +3412,33 @@ ], "loc": { "start": { - "line": 41, + "line": 42, "column": 33 }, "end": { - "line": 50, + "line": 51, "column": 2 } } }, "loc": { "start": { - "line": 41, + "line": 42, "column": 18 }, "end": { - "line": 50, + "line": 51, "column": 2 } } }, "loc": { "start": { - "line": 41, + "line": 42, "column": 18 }, "end": { - "line": 50, + "line": 51, "column": 2 } } @@ -3434,11 +3447,11 @@ "decorators": [], "loc": { "start": { - "line": 41, + "line": 42, "column": 1 }, "end": { - "line": 50, + "line": 51, "column": 2 } } @@ -3451,11 +3464,11 @@ "decorators": [], "loc": { "start": { - "line": 51, + "line": 52, "column": 17 }, "end": { - "line": 51, + "line": 52, "column": 21 } } @@ -3475,11 +3488,11 @@ "decorators": [], "loc": { "start": { - "line": 51, + "line": 52, "column": 17 }, "end": { - "line": 51, + "line": 52, "column": 21 } } @@ -3492,11 +3505,11 @@ "type": "ETSPrimitiveType", "loc": { "start": { - "line": 51, + "line": 52, "column": 25 }, "end": { - "line": 51, + "line": 52, "column": 28 } } @@ -3517,11 +3530,11 @@ "decorators": [], "loc": { "start": { - "line": 52, + "line": 53, "column": 5 }, "end": { - "line": 52, + "line": 53, "column": 6 } } @@ -3531,11 +3544,11 @@ "value": 0, "loc": { "start": { - "line": 52, + "line": 53, "column": 7 }, "end": { - "line": 52, + "line": 53, "column": 8 } } @@ -3544,11 +3557,11 @@ "optional": false, "loc": { "start": { - "line": 52, + "line": 53, "column": 5 }, "end": { - "line": 52, + "line": 53, "column": 9 } } @@ -3558,33 +3571,33 @@ "value": 2, "loc": { "start": { - "line": 52, + "line": 53, "column": 12 }, "end": { - "line": 52, + "line": 53, "column": 13 } } }, "loc": { "start": { - "line": 52, + "line": 53, "column": 5 }, "end": { - "line": 52, + "line": 53, "column": 13 } } }, "loc": { "start": { - "line": 52, + "line": 53, "column": 5 }, "end": { - "line": 52, + "line": 53, "column": 14 } } @@ -3602,11 +3615,11 @@ "decorators": [], "loc": { "start": { - "line": 53, + "line": 54, "column": 5 }, "end": { - "line": 53, + "line": 54, "column": 6 } } @@ -3616,11 +3629,11 @@ "value": 0, "loc": { "start": { - "line": 53, + "line": 54, "column": 7 }, "end": { - "line": 53, + "line": 54, "column": 8 } } @@ -3629,11 +3642,11 @@ "optional": false, "loc": { "start": { - "line": 53, + "line": 54, "column": 5 }, "end": { - "line": 53, + "line": 54, "column": 9 } } @@ -3643,33 +3656,33 @@ "value": 6, "loc": { "start": { - "line": 53, + "line": 54, "column": 12 }, "end": { - "line": 53, + "line": 54, "column": 13 } } }, "loc": { "start": { - "line": 53, + "line": 54, "column": 5 }, "end": { - "line": 53, + "line": 54, "column": 13 } } }, "loc": { "start": { - "line": 53, + "line": 54, "column": 5 }, "end": { - "line": 53, + "line": 54, "column": 14 } } @@ -3687,11 +3700,11 @@ "decorators": [], "loc": { "start": { - "line": 54, + "line": 55, "column": 5 }, "end": { - "line": 54, + "line": 55, "column": 6 } } @@ -3701,11 +3714,11 @@ "value": 1, "loc": { "start": { - "line": 54, + "line": 55, "column": 7 }, "end": { - "line": 54, + "line": 55, "column": 8 } } @@ -3714,11 +3727,11 @@ "optional": false, "loc": { "start": { - "line": 54, + "line": 55, "column": 5 }, "end": { - "line": 54, + "line": 55, "column": 9 } } @@ -3728,33 +3741,33 @@ "value": 2, "loc": { "start": { - "line": 54, + "line": 55, "column": 12 }, "end": { - "line": 54, + "line": 55, "column": 13 } } }, "loc": { "start": { - "line": 54, + "line": 55, "column": 5 }, "end": { - "line": 54, + "line": 55, "column": 13 } } }, "loc": { "start": { - "line": 54, + "line": 55, "column": 5 }, "end": { - "line": 54, + "line": 55, "column": 14 } } @@ -3772,11 +3785,11 @@ "decorators": [], "loc": { "start": { - "line": 55, + "line": 56, "column": 5 }, "end": { - "line": 55, + "line": 56, "column": 6 } } @@ -3786,11 +3799,11 @@ "value": 1, "loc": { "start": { - "line": 55, + "line": 56, "column": 7 }, "end": { - "line": 55, + "line": 56, "column": 8 } } @@ -3799,11 +3812,11 @@ "optional": false, "loc": { "start": { - "line": 55, + "line": 56, "column": 5 }, "end": { - "line": 55, + "line": 56, "column": 9 } } @@ -3813,33 +3826,33 @@ "value": 7, "loc": { "start": { - "line": 55, + "line": 56, "column": 12 }, "end": { - "line": 55, + "line": 56, "column": 13 } } }, "loc": { "start": { - "line": 55, + "line": 56, "column": 5 }, "end": { - "line": 55, + "line": 56, "column": 13 } } }, "loc": { "start": { - "line": 55, + "line": 56, "column": 5 }, "end": { - "line": 55, + "line": 56, "column": 14 } } @@ -3855,11 +3868,11 @@ "decorators": [], "loc": { "start": { - "line": 56, + "line": 57, "column": 9 }, "end": { - "line": 56, + "line": 57, "column": 19 } } @@ -3872,11 +3885,11 @@ "decorators": [], "loc": { "start": { - "line": 56, + "line": 57, "column": 22 }, "end": { - "line": 56, + "line": 57, "column": 30 } } @@ -3888,11 +3901,11 @@ "decorators": [], "loc": { "start": { - "line": 56, + "line": 57, "column": 31 }, "end": { - "line": 56, + "line": 57, "column": 32 } } @@ -3901,22 +3914,22 @@ "optional": false, "loc": { "start": { - "line": 56, + "line": 57, "column": 22 }, "end": { - "line": 56, + "line": 57, "column": 33 } } }, "loc": { "start": { - "line": 56, + "line": 57, "column": 9 }, "end": { - "line": 56, + "line": 57, "column": 33 } } @@ -3925,11 +3938,11 @@ "kind": "let", "loc": { "start": { - "line": 56, + "line": 57, "column": 5 }, "end": { - "line": 56, + "line": 57, "column": 34 } } @@ -3945,11 +3958,11 @@ "decorators": [], "loc": { "start": { - "line": 57, + "line": 58, "column": 9 }, "end": { - "line": 57, + "line": 58, "column": 10 } } @@ -3964,11 +3977,11 @@ "decorators": [], "loc": { "start": { - "line": 57, + "line": 58, "column": 20 }, "end": { - "line": 57, + "line": 58, "column": 24 } } @@ -3980,11 +3993,11 @@ "decorators": [], "loc": { "start": { - "line": 57, + "line": 58, "column": 25 }, "end": { - "line": 57, + "line": 58, "column": 26 } } @@ -3993,33 +4006,33 @@ "optional": false, "loc": { "start": { - "line": 57, + "line": 58, "column": 20 }, "end": { - "line": 57, + "line": 58, "column": 27 } } }, "loc": { "start": { - "line": 57, + "line": 58, "column": 13 }, "end": { - "line": 57, + "line": 58, "column": 27 } } }, "loc": { "start": { - "line": 57, + "line": 58, "column": 9 }, "end": { - "line": 57, + "line": 58, "column": 27 } } @@ -4028,11 +4041,11 @@ "kind": "let", "loc": { "start": { - "line": 57, + "line": 58, "column": 5 }, "end": { - "line": 57, + "line": 58, "column": 28 } } @@ -4048,11 +4061,11 @@ "decorators": [], "loc": { "start": { - "line": 58, + "line": 59, "column": 9 }, "end": { - "line": 58, + "line": 59, "column": 18 } } @@ -4067,11 +4080,11 @@ "decorators": [], "loc": { "start": { - "line": 58, + "line": 59, "column": 21 }, "end": { - "line": 58, + "line": 59, "column": 22 } } @@ -4082,11 +4095,11 @@ "decorators": [], "loc": { "start": { - "line": 58, + "line": 59, "column": 23 }, "end": { - "line": 58, + "line": 59, "column": 38 } } @@ -4095,11 +4108,11 @@ "optional": false, "loc": { "start": { - "line": 58, + "line": 59, "column": 21 }, "end": { - "line": 58, + "line": 59, "column": 38 } } @@ -4108,22 +4121,22 @@ "optional": false, "loc": { "start": { - "line": 58, + "line": 59, "column": 21 }, "end": { - "line": 58, + "line": 59, "column": 40 } } }, "loc": { "start": { - "line": 58, + "line": 59, "column": 9 }, "end": { - "line": 58, + "line": 59, "column": 40 } } @@ -4132,11 +4145,11 @@ "kind": "let", "loc": { "start": { - "line": 58, + "line": 59, "column": 5 }, "end": { - "line": 58, + "line": 59, "column": 41 } } @@ -4151,11 +4164,11 @@ "decorators": [], "loc": { "start": { - "line": 59, + "line": 60, "column": 5 }, "end": { - "line": 59, + "line": 60, "column": 14 } } @@ -4169,11 +4182,11 @@ "decorators": [], "loc": { "start": { - "line": 59, + "line": 60, "column": 15 }, "end": { - "line": 59, + "line": 60, "column": 24 } } @@ -4182,22 +4195,22 @@ "type": "ETSPrimitiveType", "loc": { "start": { - "line": 59, + "line": 60, "column": 28 }, "end": { - "line": 59, + "line": 60, "column": 31 } } }, "loc": { "start": { - "line": 59, + "line": 60, "column": 15 }, "end": { - "line": 59, + "line": 60, "column": 24 } } @@ -4208,11 +4221,11 @@ "decorators": [], "loc": { "start": { - "line": 59, + "line": 60, "column": 33 }, "end": { - "line": 59, + "line": 60, "column": 43 } } @@ -4221,22 +4234,22 @@ "optional": false, "loc": { "start": { - "line": 59, + "line": 60, "column": 5 }, "end": { - "line": 59, + "line": 60, "column": 44 } } }, "loc": { "start": { - "line": 59, + "line": 60, "column": 5 }, "end": { - "line": 59, + "line": 60, "column": 45 } } @@ -4248,22 +4261,22 @@ "value": 0, "loc": { "start": { - "line": 60, + "line": 61, "column": 12 }, "end": { - "line": 60, + "line": 61, "column": 13 } } }, "loc": { "start": { - "line": 60, + "line": 61, "column": 5 }, "end": { - "line": 60, + "line": 61, "column": 14 } } @@ -4271,33 +4284,33 @@ ], "loc": { "start": { - "line": 51, + "line": 52, "column": 29 }, "end": { - "line": 61, + "line": 62, "column": 2 } } }, "loc": { "start": { - "line": 51, + "line": 52, "column": 21 }, "end": { - "line": 61, + "line": 62, "column": 2 } } }, "loc": { "start": { - "line": 51, + "line": 52, "column": 21 }, "end": { - "line": 61, + "line": 62, "column": 2 } } @@ -4306,11 +4319,11 @@ "decorators": [], "loc": { "start": { - "line": 51, + "line": 52, "column": 8 }, "end": { - "line": 61, + "line": 62, "column": 2 } } @@ -4345,7 +4358,7 @@ "column": 1 }, "end": { - "line": 62, + "line": 63, "column": 1 } } diff --git a/ets2panda/test/compiler/ets/launch_expression.ets b/ets2panda/test/compiler/ets/launch_expression.ets index 011deb2dee..362b63db11 100644 --- a/ets2panda/test/compiler/ets/launch_expression.ets +++ b/ets2panda/test/compiler/ets/launch_expression.ets @@ -17,6 +17,7 @@ let count = 2 let n = 9 let a: int[] = new int[count]; let v: int[] = new int[count]; +type P = Promise | null function assert_eq(value1: int, value2: int): void { if (value1 == value2) { return; @@ -28,7 +29,7 @@ function ufib(n: int) : Int { if (n >= 0 && n < count) { return v[n]; } - let p: Promise[] = new Promise[count] + let p: P[] = new P[count] for (let i = 0; i < count; ++i) { p[i] = launch ufib(n-1-i); } -- Gitee From b8e6d036591381264a22ac311b4c2bd988720a07 Mon Sep 17 00:00:00 2001 From: Martin Sajti Date: Fri, 22 Dec 2023 17:17:05 +0100 Subject: [PATCH 11/19] Class getter and setter generation Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/I8QPT7 Internal issue: #15142 Test: build, new checker tests Change-Id: Icd1d5db370c2503933ef3a8b944ecaf1a70ada8a Signed-off-by: Gergo Torok Signed-off-by: Martin Sajti --- ets2panda/checker/ETSchecker.h | 7 ++ ets2panda/checker/ets/helpers.cpp | 89 +++++++++++++++++++ ets2panda/checker/ets/object.cpp | 62 ++++++++++++- .../ets/interfacePropertyDeclarations.cpp | 11 ++- .../test/runtime/ets/classGetterSetter.ets | 33 +++++++ ets2panda/varbinder/variableFlags.h | 1 + 6 files changed, 196 insertions(+), 7 deletions(-) create mode 100644 ets2panda/test/runtime/ets/classGetterSetter.ets diff --git a/ets2panda/checker/ETSchecker.h b/ets2panda/checker/ETSchecker.h index 44cd7dfbda..e565e924bd 100644 --- a/ets2panda/checker/ETSchecker.h +++ b/ets2panda/checker/ETSchecker.h @@ -177,6 +177,7 @@ public: varbinder::Variable *ResolveInstanceExtension(const ir::MemberExpression *memberExpr); void CheckImplicitSuper(ETSObjectType *classType, Signature *ctorSig); void CheckValidInheritance(ETSObjectType *classType, ir::ClassDefinition *classDef); + void TransformProperties(ETSObjectType *classType); void CheckGetterSetterProperties(ETSObjectType *classType); void AddElementsToModuleObject(ETSObjectType *moduleObj, const util::StringView &str); Type *FindLeastUpperBound(Type *source, Type *target); @@ -547,6 +548,12 @@ public: void ModifyPreferredType(ir::ArrayExpression *arrayExpr, Type *newPreferredType); Type *SelectGlobalIntegerTypeForNumeric(Type *type); + void GenerateGetterSetterBody(ETSChecker *checker, ArenaVector &stmts, + ArenaVector ¶ms, ir::ClassProperty *field, + varbinder::FunctionParamScope *paramScope, bool isSetter); + static ir::MethodDefinition *GenerateDefaultGetterSetter(ir::ClassProperty *field, varbinder::ClassScope *scope, + bool isSetter, ETSChecker *checker); + // Exception ETSObjectType *CheckExceptionOrErrorType(checker::Type *type, lexer::SourcePosition pos); diff --git a/ets2panda/checker/ets/helpers.cpp b/ets2panda/checker/ets/helpers.cpp index 77aa904632..5c1b3fd187 100644 --- a/ets2panda/checker/ets/helpers.cpp +++ b/ets2panda/checker/ets/helpers.cpp @@ -2633,6 +2633,95 @@ std::string GenerateImplicitInstantiateArg(varbinder::LocalVariable *instantiate return implicitInstantiateArgument; } +void ETSChecker::GenerateGetterSetterBody(ETSChecker *checker, ArenaVector &stmts, + ArenaVector ¶ms, ir::ClassProperty *const field, + varbinder::FunctionParamScope *paramScope, bool isSetter) +{ + if (!isSetter) { + stmts.push_back(checker->Allocator()->New(field->Key())); + return; + } + + auto *paramIdent = field->Key()->AsIdentifier()->Clone(checker->Allocator()); + paramIdent->SetTsTypeAnnotation(field->TypeAnnotation()->Clone(checker->Allocator())); + paramIdent->TypeAnnotation()->SetParent(paramIdent); + + auto *paramExpression = checker->AllocNode(paramIdent, nullptr); + paramExpression->SetRange(paramIdent->Range()); + auto *const paramVar = std::get<2>(paramScope->AddParamDecl(checker->Allocator(), paramExpression)); + + paramIdent->SetVariable(paramVar); + paramExpression->SetVariable(paramVar); + + params.push_back(paramExpression); + + auto *assignmentExpression = checker->AllocNode( + field->Key(), paramExpression, lexer::TokenType::PUNCTUATOR_SUBSTITUTION); + + assignmentExpression->SetRange({field->Start(), field->End()}); + + stmts.push_back(checker->AllocNode(assignmentExpression)); + stmts.push_back(checker->Allocator()->New(nullptr)); +} + +ir::MethodDefinition *ETSChecker::GenerateDefaultGetterSetter(ir::ClassProperty *const field, + varbinder::ClassScope *classScope, bool isSetter, + ETSChecker *checker) +{ + auto *paramScope = checker->Allocator()->New(checker->Allocator(), classScope); + auto *functionScope = checker->Allocator()->New(checker->Allocator(), paramScope); + + functionScope->BindParamScope(paramScope); + paramScope->BindFunctionScope(functionScope); + + auto flags = ir::ModifierFlags::PUBLIC; + + ArenaVector params(checker->Allocator()->Adapter()); + ArenaVector stmts(checker->Allocator()->Adapter()); + checker->GenerateGetterSetterBody(checker, stmts, params, field, paramScope, isSetter); + + auto *body = checker->AllocNode(checker->Allocator(), std::move(stmts)); + auto funcFlags = isSetter ? ir::ScriptFunctionFlags::SETTER : ir::ScriptFunctionFlags::GETTER; + auto *const returnTypeAnn = isSetter ? nullptr : field->TypeAnnotation(); + auto *func = + checker->AllocNode(ir::FunctionSignature(nullptr, std::move(params), returnTypeAnn), body, + funcFlags, flags, true, Language(Language::Id::ETS)); + + func->SetRange(field->Range()); + func->SetScope(functionScope); + body->SetScope(functionScope); + + auto *methodIdent = field->Key()->AsIdentifier()->Clone(checker->Allocator()); + auto *decl = checker->Allocator()->New( + checker->Allocator(), field->Key()->AsIdentifier()->Name(), + field->Key()->AsIdentifier()->Variable()->Declaration()->Node()); + auto *var = functionScope->AddDecl(checker->Allocator(), decl, ScriptExtension::ETS); + + methodIdent->SetVariable(var); + + auto *funcExpr = checker->AllocNode(func); + funcExpr->SetRange(func->Range()); + func->AddFlag(ir::ScriptFunctionFlags::METHOD); + + auto *method = checker->AllocNode(ir::MethodDefinitionKind::METHOD, methodIdent, funcExpr, + flags, checker->Allocator(), false); + + method->Id()->SetMutator(); + method->SetRange(field->Range()); + method->Function()->SetIdent(method->Id()); + method->Function()->AddModifier(method->Modifiers()); + method->SetVariable(var); + + paramScope->BindNode(func); + functionScope->BindNode(func); + + checker->VarBinder()->AsETSBinder()->ResolveMethodDefinition(method); + functionScope->BindName(classScope->Node()->AsClassDefinition()->InternalName()); + method->Check(checker); + + return method; +} + bool ETSChecker::TryTransformingToStaticInvoke(ir::Identifier *const ident, const Type *resolvedType) { ASSERT(ident->Parent()->IsCallExpression()); diff --git a/ets2panda/checker/ets/object.cpp b/ets2panda/checker/ets/object.cpp index 6fbf90b9fc..f09789c926 100644 --- a/ets2panda/checker/ets/object.cpp +++ b/ets2panda/checker/ets/object.cpp @@ -719,6 +719,7 @@ void ETSChecker::ValidateOverriding(ETSObjectType *classType, const lexer::Sourc if (isGetterSetter && !functionOverridden) { for (auto *field : classType->Fields()) { if (field->Name() == (*it)->Name()) { + field->Declaration()->Node()->AddModifier(ir::ModifierFlags::SETTER); it = abstractsToBeImplemented.erase(it); functionOverridden = true; break; @@ -813,6 +814,7 @@ void ETSChecker::CheckClassDefinition(ir::ClassDefinition *classDef) } ValidateOverriding(classType, classDef->Start()); + TransformProperties(classType); CheckValidInheritance(classType, classDef); CheckConstFields(classType); CheckGetterSetterProperties(classType); @@ -1441,7 +1443,7 @@ void ETSChecker::CheckValidInheritance(ETSObjectType *classType, ir::ClassDefini continue; } - if (!IsSameDeclarationType(it, found)) { + if (!IsSameDeclarationType(it, found) && !it->HasFlag(varbinder::VariableFlags::GETTER_SETTER)) { const char *targetType {}; if (it->HasFlag(varbinder::VariableFlags::PROPERTY)) { @@ -1468,6 +1470,64 @@ void ETSChecker::CheckValidInheritance(ETSObjectType *classType, ir::ClassDefini } } +void ETSChecker::TransformProperties(ETSObjectType *classType) +{ + auto propertyList = classType->Fields(); + auto *const classDef = classType->GetDeclNode()->AsClassDefinition(); + + for (auto *const field : propertyList) { + ASSERT(field->Declaration()->Node()->IsClassProperty()); + auto *const classProp = field->Declaration()->Node()->AsClassProperty(); + + if ((field->Declaration()->Node()->Modifiers() & ir::ModifierFlags::SETTER) == 0U) { + continue; + } + + field->AddFlag(varbinder::VariableFlags::GETTER_SETTER); + + auto *const scope = this->Scope(); + ASSERT(scope->IsClassScope()); + + ir::MethodDefinition *getter = GenerateDefaultGetterSetter(classProp, scope->AsClassScope(), false, this); + classDef->Body().push_back(getter); + classType->AddProperty(getter->Variable()->AsLocalVariable()); + + auto *const methodScope = scope->AsClassScope()->InstanceMethodScope(); + auto name = getter->Key()->AsIdentifier()->Name(); + + auto *const decl = Allocator()->New(Allocator(), name, getter); + auto *const var = methodScope->AddDecl(Allocator(), decl, ScriptExtension::ETS); + var->AddFlag(varbinder::VariableFlags::METHOD); + + if (var == nullptr) { + auto *const prevDecl = methodScope->FindDecl(name); + ASSERT(prevDecl->IsFunctionDecl()); + prevDecl->Node()->AsMethodDefinition()->AddOverload(getter); + + if (!classProp->IsReadonly()) { + ir::MethodDefinition *const setter = + GenerateDefaultGetterSetter(classProp, scope->AsClassScope(), true, this); + + classType->AddProperty(setter->Variable()->AsLocalVariable()); + prevDecl->Node()->AsMethodDefinition()->AddOverload(setter); + } + + getter->Function()->Id()->SetVariable( + methodScope->FindLocal(name, varbinder::ResolveBindingOptions::BINDINGS)); + continue; + } + + if (!classProp->IsReadonly()) { + ir::MethodDefinition *const setter = + GenerateDefaultGetterSetter(classProp, scope->AsClassScope(), true, this); + + classType->AddProperty(setter->Variable()->AsLocalVariable()); + getter->AddOverload(setter); + } + getter->Function()->Id()->SetVariable(var); + } +} + void ETSChecker::CheckGetterSetterProperties(ETSObjectType *classType) { auto const checkGetterSetter = [this](varbinder::LocalVariable *var, util::StringView name) { diff --git a/ets2panda/compiler/lowering/ets/interfacePropertyDeclarations.cpp b/ets2panda/compiler/lowering/ets/interfacePropertyDeclarations.cpp index 85ccf455cf..97b56809eb 100644 --- a/ets2panda/compiler/lowering/ets/interfacePropertyDeclarations.cpp +++ b/ets2panda/compiler/lowering/ets/interfacePropertyDeclarations.cpp @@ -49,14 +49,12 @@ static ir::MethodDefinition *GenerateGetterOrSetter(checker::ETSChecker *const c paramIdent->SetTsTypeAnnotation(field->TypeAnnotation()->Clone(checker->Allocator())); paramIdent->TypeAnnotation()->SetParent(paramIdent); - auto paramExpression = checker->AllocNode(paramIdent, nullptr); + auto *const paramExpression = checker->AllocNode(paramIdent, nullptr); paramExpression->SetRange(paramIdent->Range()); - const auto [_, __, param_var] = paramScope->AddParamDecl(checker->Allocator(), paramExpression); - (void)_; - (void)__; + auto *const paramVar = std::get<2>(paramScope->AddParamDecl(checker->Allocator(), paramExpression)); - paramIdent->SetVariable(param_var); - paramExpression->SetVariable(param_var); + paramIdent->SetVariable(paramVar); + paramExpression->SetVariable(paramVar); params.push_back(paramExpression); } @@ -122,6 +120,7 @@ static ir::Expression *UpdateInterfacePropertys(checker::ETSChecker *const check auto *decl = checker->Allocator()->New(checker->Allocator(), name, getter); auto var = methodScope->AddDecl(checker->Allocator(), decl, ScriptExtension::ETS); + var->AddFlag(varbinder::VariableFlags::METHOD); if (var == nullptr) { auto prevDecl = methodScope->FindDecl(name); diff --git a/ets2panda/test/runtime/ets/classGetterSetter.ets b/ets2panda/test/runtime/ets/classGetterSetter.ets new file mode 100644 index 0000000000..29b68b3979 --- /dev/null +++ b/ets2panda/test/runtime/ets/classGetterSetter.ets @@ -0,0 +1,33 @@ +/* + * 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 inter { + get my_field (): int; + set my_field(param: int); +} + +class A implements inter { + my_field: int = 3; +} + +function main() { + let a0: A = new A(); + assert a0.my_field == 3; + + let a1: inter = new A(); + assert a1.my_field == 3; + a1.my_field = 5; + assert a1.my_field == 5; +} diff --git a/ets2panda/varbinder/variableFlags.h b/ets2panda/varbinder/variableFlags.h index 87e165e4ce..3c9fa9813a 100644 --- a/ets2panda/varbinder/variableFlags.h +++ b/ets2panda/varbinder/variableFlags.h @@ -152,6 +152,7 @@ enum class VariableFlags : uint64_t { BUILTIN_TYPE = 1ULL << 32ULL, BOXED = 1ULL << 33ULL, + GETTER_SETTER = 1ULL << 34ULL, HOIST_VAR = HOIST | VAR, CLASS_OR_INTERFACE = CLASS | INTERFACE, -- Gitee From e915f9c2065fd8b9b0ce4bc2deca11b31ffced30 Mon Sep 17 00:00:00 2001 From: Martin Sajti Date: Wed, 3 Jan 2024 14:58:32 +0100 Subject: [PATCH 12/19] Fix ETSObjectType::Identical for function types Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/I8TEO3 Internal issue: #15034 Test: build, new checker tests Signed-off-by: Martin Sajti --- ets2panda/checker/types/ets/etsObjectType.cpp | 2 +- ets2panda/checker/types/ets/etsTupleType.cpp | 4 + .../compiler/ets/tuple_types_16-expected.txt | 1523 +++++++++++++++++ .../test/compiler/ets/tuple_types_16.ets | 24 + 4 files changed, 1552 insertions(+), 1 deletion(-) create mode 100644 ets2panda/test/compiler/ets/tuple_types_16-expected.txt create mode 100644 ets2panda/test/compiler/ets/tuple_types_16.ets diff --git a/ets2panda/checker/types/ets/etsObjectType.cpp b/ets2panda/checker/types/ets/etsObjectType.cpp index ab6f4fb3f5..cb10fbfff5 100644 --- a/ets2panda/checker/types/ets/etsObjectType.cpp +++ b/ets2panda/checker/types/ets/etsObjectType.cpp @@ -321,7 +321,7 @@ void ETSObjectType::IdenticalUptoNullability(TypeRelation *relation, Type *other return; } - if (relation->IgnoreTypeParameters() || (this == other)) { + if ((relation->IgnoreTypeParameters() && !HasObjectFlag(ETSObjectFlags::FUNCTIONAL)) || (this == other)) { relation->Result(true); return; } diff --git a/ets2panda/checker/types/ets/etsTupleType.cpp b/ets2panda/checker/types/ets/etsTupleType.cpp index 08e74f9828..92f9feb317 100644 --- a/ets2panda/checker/types/ets/etsTupleType.cpp +++ b/ets2panda/checker/types/ets/etsTupleType.cpp @@ -23,8 +23,12 @@ namespace panda::es2panda::checker { void ETSTupleType::ToString(std::stringstream &ss) const { ss << "["; + for (const auto *const type : typeList_) { type->ToString(ss); + if (type != typeList_.back()) { + ss << ","; + } } if (spreadType_ != nullptr) { diff --git a/ets2panda/test/compiler/ets/tuple_types_16-expected.txt b/ets2panda/test/compiler/ets/tuple_types_16-expected.txt new file mode 100644 index 0000000000..ed97776aec --- /dev/null +++ b/ets2panda/test/compiler/ets/tuple_types_16-expected.txt @@ -0,0 +1,1523 @@ +{ + "type": "Program", + "statements": [ + { + "type": "TSTypeAliasDeclaration", + "id": { + "type": "Identifier", + "name": "Func1", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 6 + }, + "end": { + "line": 16, + "column": 11 + } + } + }, + "typeAnnotation": { + "type": "ETSFunctionType", + "params": [ + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "value", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "String", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 22 + }, + "end": { + "line": 16, + "column": 28 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 22 + }, + "end": { + "line": 16, + "column": 29 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 22 + }, + "end": { + "line": 16, + "column": 29 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 15 + }, + "end": { + "line": 16, + "column": 29 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 15 + }, + "end": { + "line": 16, + "column": 29 + } + } + } + ], + "returnType": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "void", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 33 + }, + "end": { + "line": 16, + "column": 37 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 33 + }, + "end": { + "line": 17, + "column": 5 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 33 + }, + "end": { + "line": 17, + "column": 5 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 14 + }, + "end": { + "line": 17, + "column": 5 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 17, + "column": 5 + } + } + }, + { + "type": "TSTypeAliasDeclaration", + "id": { + "type": "Identifier", + "name": "Func0", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 6 + }, + "end": { + "line": 17, + "column": 11 + } + } + }, + "typeAnnotation": { + "type": "ETSFunctionType", + "params": [], + "returnType": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "void", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 20 + }, + "end": { + "line": 17, + "column": 24 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 20 + }, + "end": { + "line": 17, + "column": 25 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 20 + }, + "end": { + "line": 17, + "column": 25 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 14 + }, + "end": { + "line": 17, + "column": 25 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 1 + }, + "end": { + "line": 17, + "column": 25 + } + } + }, + { + "type": "TSTypeAliasDeclaration", + "id": { + "type": "Identifier", + "name": "FuncTuple2", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 6 + }, + "end": { + "line": 18, + "column": 16 + } + } + }, + "typeAnnotation": { + "type": "ETSTuple", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Func1", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 20 + }, + "end": { + "line": 18, + "column": 25 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 20 + }, + "end": { + "line": 18, + "column": 26 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 20 + }, + "end": { + "line": 18, + "column": 26 + } + } + }, + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Func1", + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 27 + }, + "end": { + "line": 18, + "column": 32 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 27 + }, + "end": { + "line": 18, + "column": 33 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 27 + }, + "end": { + "line": 18, + "column": 33 + } + } + } + ], + "spreadType": null, + "loc": { + "start": { + "line": 18, + "column": 19 + }, + "end": { + "line": 19, + "column": 5 + } + } + }, + "loc": { + "start": { + "line": 18, + "column": 1 + }, + "end": { + "line": 19, + "column": 5 + } + } + }, + { + "type": "TSTypeAliasDeclaration", + "id": { + "type": "Identifier", + "name": "FuncTuple3", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 6 + }, + "end": { + "line": 19, + "column": 16 + } + } + }, + "typeAnnotation": { + "type": "ETSTuple", + "types": [ + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Func1", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 20 + }, + "end": { + "line": 19, + "column": 25 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 20 + }, + "end": { + "line": 19, + "column": 26 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 20 + }, + "end": { + "line": 19, + "column": 26 + } + } + }, + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Func1", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 27 + }, + "end": { + "line": 19, + "column": 32 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 27 + }, + "end": { + "line": 19, + "column": 33 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 27 + }, + "end": { + "line": 19, + "column": 33 + } + } + }, + { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "Func0", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 34 + }, + "end": { + "line": 19, + "column": 39 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 34 + }, + "end": { + "line": 19, + "column": 40 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 34 + }, + "end": { + "line": 19, + "column": 40 + } + } + } + ], + "spreadType": null, + "loc": { + "start": { + "line": 19, + "column": 19 + }, + "end": { + "line": 21, + "column": 9 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 1 + }, + "end": { + "line": 21, + "column": 9 + } + } + }, + { + "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": "_$init$_", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": true, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "_$init$_", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "statements": [], + "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": 1, + "column": 1 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "main", + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 10 + }, + "end": { + "line": 21, + "column": 14 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": true, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "main", + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 10 + }, + "end": { + "line": 21, + "column": 14 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "returnType": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "void", + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 18 + }, + "end": { + "line": 21, + "column": 22 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 18 + }, + "end": { + "line": 21, + "column": 24 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 18 + }, + "end": { + "line": 21, + "column": 24 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "t2", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "FuncTuple2", + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 11 + }, + "end": { + "line": 22, + "column": 21 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 11 + }, + "end": { + "line": 22, + "column": 23 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 11 + }, + "end": { + "line": 22, + "column": 23 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 7 + }, + "end": { + "line": 22, + "column": 9 + } + } + }, + "init": { + "type": "ArrayExpression", + "elements": [ + { + "type": "ArrowFunctionExpression", + "function": { + "type": "ScriptFunction", + "id": null, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "value", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "String", + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 33 + }, + "end": { + "line": 22, + "column": 39 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 33 + }, + "end": { + "line": 22, + "column": 40 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 33 + }, + "end": { + "line": 22, + "column": 40 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 26 + }, + "end": { + "line": 22, + "column": 40 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 26 + }, + "end": { + "line": 22, + "column": 40 + } + } + } + ], + "body": { + "type": "BlockStatement", + "statements": [], + "loc": { + "start": { + "line": 22, + "column": 44 + }, + "end": { + "line": 22, + "column": 46 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 25 + }, + "end": { + "line": 22, + "column": 46 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 25 + }, + "end": { + "line": 22, + "column": 46 + } + } + }, + { + "type": "ArrowFunctionExpression", + "function": { + "type": "ScriptFunction", + "id": null, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "value", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "String", + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 56 + }, + "end": { + "line": 22, + "column": 62 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 56 + }, + "end": { + "line": 22, + "column": 63 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 56 + }, + "end": { + "line": 22, + "column": 63 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 49 + }, + "end": { + "line": 22, + "column": 63 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 49 + }, + "end": { + "line": 22, + "column": 63 + } + } + } + ], + "body": { + "type": "BlockStatement", + "statements": [], + "loc": { + "start": { + "line": 22, + "column": 67 + }, + "end": { + "line": 22, + "column": 69 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 48 + }, + "end": { + "line": 22, + "column": 69 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 48 + }, + "end": { + "line": 22, + "column": 69 + } + } + } + ], + "loc": { + "start": { + "line": 22, + "column": 24 + }, + "end": { + "line": 22, + "column": 70 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 7 + }, + "end": { + "line": 22, + "column": 70 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 22, + "column": 3 + }, + "end": { + "line": 22, + "column": 70 + } + } + }, + { + "type": "VariableDeclaration", + "declarations": [ + { + "type": "VariableDeclarator", + "id": { + "type": "Identifier", + "name": "t3", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "FuncTuple3", + "decorators": [], + "loc": { + "start": { + "line": 23, + "column": 11 + }, + "end": { + "line": 23, + "column": 21 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 11 + }, + "end": { + "line": 23, + "column": 23 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 11 + }, + "end": { + "line": 23, + "column": 23 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 23, + "column": 7 + }, + "end": { + "line": 23, + "column": 9 + } + } + }, + "init": { + "type": "ArrayExpression", + "elements": [ + { + "type": "ArrowFunctionExpression", + "function": { + "type": "ScriptFunction", + "id": null, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "value", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "String", + "decorators": [], + "loc": { + "start": { + "line": 23, + "column": 33 + }, + "end": { + "line": 23, + "column": 39 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 33 + }, + "end": { + "line": 23, + "column": 40 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 33 + }, + "end": { + "line": 23, + "column": 40 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 23, + "column": 26 + }, + "end": { + "line": 23, + "column": 40 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 26 + }, + "end": { + "line": 23, + "column": 40 + } + } + } + ], + "body": { + "type": "BlockStatement", + "statements": [], + "loc": { + "start": { + "line": 23, + "column": 44 + }, + "end": { + "line": 23, + "column": 46 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 25 + }, + "end": { + "line": 23, + "column": 46 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 25 + }, + "end": { + "line": 23, + "column": 46 + } + } + }, + { + "type": "ArrowFunctionExpression", + "function": { + "type": "ScriptFunction", + "id": null, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "value", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "String", + "decorators": [], + "loc": { + "start": { + "line": 23, + "column": 56 + }, + "end": { + "line": 23, + "column": 62 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 56 + }, + "end": { + "line": 23, + "column": 63 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 56 + }, + "end": { + "line": 23, + "column": 63 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 23, + "column": 49 + }, + "end": { + "line": 23, + "column": 63 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 49 + }, + "end": { + "line": 23, + "column": 63 + } + } + } + ], + "body": { + "type": "BlockStatement", + "statements": [], + "loc": { + "start": { + "line": 23, + "column": 67 + }, + "end": { + "line": 23, + "column": 69 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 48 + }, + "end": { + "line": 23, + "column": 69 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 48 + }, + "end": { + "line": 23, + "column": 69 + } + } + }, + { + "type": "ArrowFunctionExpression", + "function": { + "type": "ScriptFunction", + "id": null, + "generator": false, + "async": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "statements": [], + "loc": { + "start": { + "line": 23, + "column": 77 + }, + "end": { + "line": 23, + "column": 79 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 71 + }, + "end": { + "line": 23, + "column": 79 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 71 + }, + "end": { + "line": 23, + "column": 79 + } + } + } + ], + "loc": { + "start": { + "line": 23, + "column": 24 + }, + "end": { + "line": 23, + "column": 80 + } + } + }, + "loc": { + "start": { + "line": 23, + "column": 7 + }, + "end": { + "line": 23, + "column": 80 + } + } + } + ], + "kind": "let", + "loc": { + "start": { + "line": 23, + "column": 3 + }, + "end": { + "line": 23, + "column": 80 + } + } + } + ], + "loc": { + "start": { + "line": 21, + "column": 23 + }, + "end": { + "line": 24, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 14 + }, + "end": { + "line": 24, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 14 + }, + "end": { + "line": 24, + "column": 2 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 1 + }, + "end": { + "line": 24, + "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": 25, + "column": 1 + } + } +} diff --git a/ets2panda/test/compiler/ets/tuple_types_16.ets b/ets2panda/test/compiler/ets/tuple_types_16.ets new file mode 100644 index 0000000000..d0069f32aa --- /dev/null +++ b/ets2panda/test/compiler/ets/tuple_types_16.ets @@ -0,0 +1,24 @@ +/* + * 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. + */ + +type Func1 = (value: String) => void +type Func0 = () => void; +type FuncTuple2 = [Func1, Func1] +type FuncTuple3 = [Func1, Func1, Func0] + +function main(): void { + let t2: FuncTuple2 = [(value: String) => {}, (value: String) => {}] + let t3: FuncTuple3 = [(value: String) => {}, (value: String) => {}, () => {}] +} -- Gitee From 2954a0d015cddf2430e3faad8328dc57383d4b9f Mon Sep 17 00:00:00 2001 From: Istvan Romai Date: Thu, 18 Jan 2024 08:50:07 +0100 Subject: [PATCH 13/19] [ArkTS frontend] Missing toString() method for arrays Added .toString() handling to arrays Fixes internal issue: #14766 Change-Id: Idc271497e2795456d840ad0a8c7416a99b10169c Signed-off-by: Istvan Romai --- ets2panda/checker/ETSAnalyzer.cpp | 22 ++++++++++++++++------ ets2panda/checker/ETSAnalyzer.h | 1 + ets2panda/compiler/core/ETSGen.cpp | 3 ++- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/ets2panda/checker/ETSAnalyzer.cpp b/ets2panda/checker/ETSAnalyzer.cpp index 885e81d208..ad052f8e19 100644 --- a/ets2panda/checker/ETSAnalyzer.cpp +++ b/ets2panda/checker/ETSAnalyzer.cpp @@ -1254,6 +1254,15 @@ checker::Type *ETSAnalyzer::Check([[maybe_unused]] ir::ImportExpression *expr) c UNREACHABLE(); } +checker::Type *ETSAnalyzer::SetAndAdjustType(ETSChecker *checker, ir::MemberExpression *expr, + ETSObjectType *objectType) const +{ + expr->SetObjectType(objectType); + auto [resType, resVar] = expr->ResolveObjectMember(checker); + expr->SetPropVar(resVar); + return expr->AdjustType(checker, resType); +} + checker::Type *ETSAnalyzer::Check(ir::MemberExpression *expr) const { ETSChecker *checker = GetETSChecker(); @@ -1284,15 +1293,16 @@ checker::Type *ETSAnalyzer::Check(ir::MemberExpression *expr) const return expr->AdjustType(checker, expr->CheckComputed(checker, baseType)); } - if (baseType->IsETSArrayType() && expr->Property()->AsIdentifier()->Name().Is("length")) { - return expr->AdjustType(checker, checker->GlobalIntType()); + if (baseType->IsETSArrayType()) { + if (expr->Property()->AsIdentifier()->Name().Is("length")) { + return expr->AdjustType(checker, checker->GlobalIntType()); + } + + return SetAndAdjustType(checker, expr, checker->GlobalETSObjectType()); } if (baseType->IsETSObjectType()) { - expr->SetObjectType(baseType->AsETSObjectType()); - auto [resType, resVar] = expr->ResolveObjectMember(checker); - expr->SetPropVar(resVar); - return expr->AdjustType(checker, resType); + return SetAndAdjustType(checker, expr, baseType->AsETSObjectType()); } if (baseType->IsETSEnumType() || baseType->IsETSStringEnumType()) { diff --git a/ets2panda/checker/ETSAnalyzer.h b/ets2panda/checker/ETSAnalyzer.h index db1f0439fb..c284a6db26 100644 --- a/ets2panda/checker/ETSAnalyzer.h +++ b/ets2panda/checker/ETSAnalyzer.h @@ -44,6 +44,7 @@ private: bool isFunctionalInterface, bool isUnionTypeWithFunctionalInterface) const; checker::Type *GetReturnType(ir::CallExpression *expr, checker::Type *calleeType) const; checker::Type *GetFunctionReturnType(ir::ReturnStatement *st, ir::ScriptFunction *containingFunc) const; + checker::Type *SetAndAdjustType(ETSChecker *checker, ir::MemberExpression *expr, ETSObjectType *objectType) const; }; } // namespace panda::es2panda::checker diff --git a/ets2panda/compiler/core/ETSGen.cpp b/ets2panda/compiler/core/ETSGen.cpp index d0291d7da0..b85b2df8bd 100644 --- a/ets2panda/compiler/core/ETSGen.cpp +++ b/ets2panda/compiler/core/ETSGen.cpp @@ -2387,7 +2387,8 @@ void ETSGen::StringBuilderAppend(const ir::AstNode *node, VReg builder) signature = Signatures::BUILTIN_STRING_BUILDER_APPEND_BUILTIN_STRING; } - if ((GetAccumulatorType()->IsETSObjectType() || GetAccumulatorType()->IsETSTypeParameter()) && + if ((GetAccumulatorType()->IsETSObjectType() || GetAccumulatorType()->IsETSTypeParameter() || + GetAccumulatorType()->IsETSArrayType()) && !GetAccumulatorType()->IsETSStringType()) { if (Checker()->MayHaveNullValue(GetAccumulatorType())) { Label *ifnull = AllocLabel(); -- Gitee From eafe7b14098998aca147ce5252203a554897897c Mon Sep 17 00:00:00 2001 From: Anna Antipina Date: Tue, 23 Jan 2024 18:04:28 +0300 Subject: [PATCH 14/19] Title: Fix update expression compiling Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/I8YE1Q Description: Fix boxing-unboxing flags in Update expression compiling Test: ${ARK_SOURCE_DIR}/tests/tests-u-runner/runner.sh ${ARK_SOURCE_DIR} --ets-runtime --build-dir="${ARK_BUILD_DIR}" --heap-verifier="fail_on_verification:pre:into:before_g1_concurrent:post" --timeout=30 --force-generate --test-file UpdateExpression.ets Signed-off-by: Anna Antipina - --- ets2panda/compiler/core/ETSCompiler.cpp | 9 ++++----- ets2panda/test/runtime/ets/UpdateExpression.ets | 6 ++++++ 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/ets2panda/compiler/core/ETSCompiler.cpp b/ets2panda/compiler/core/ETSCompiler.cpp index c95eceb6d3..fd2476ce94 100644 --- a/ets2panda/compiler/core/ETSCompiler.cpp +++ b/ets2panda/compiler/core/ETSCompiler.cpp @@ -1189,8 +1189,11 @@ void ETSCompiler::Compile(const ir::UpdateExpression *expr) const const auto argumentUnboxingFlags = static_cast(expr->Argument()->GetBoxingUnboxingFlags() & ir::BoxingUnboxingFlags::UNBOXING_FLAG); + // workaround so argument_ does not get auto unboxed by lref.GetValue() + expr->Argument()->SetBoxingUnboxingFlags(ir::BoxingUnboxingFlags::NONE); + lref.GetValue(); + if (expr->IsPrefix()) { - lref.GetValue(); expr->Argument()->SetBoxingUnboxingFlags(argumentUnboxingFlags); etsg->ApplyConversion(expr->Argument(), nullptr); @@ -1209,10 +1212,6 @@ void ETSCompiler::Compile(const ir::UpdateExpression *expr) const return; } - // workaround so argument_ does not get auto unboxed by lref.GetValue() - expr->Argument()->SetBoxingUnboxingFlags(ir::BoxingUnboxingFlags::NONE); - lref.GetValue(); - compiler::RegScope rs(etsg); compiler::VReg originalValueReg = etsg->AllocReg(); etsg->StoreAccumulator(expr->Argument(), originalValueReg); diff --git a/ets2panda/test/runtime/ets/UpdateExpression.ets b/ets2panda/test/runtime/ets/UpdateExpression.ets index 4cb626c2c2..ef41ddc581 100644 --- a/ets2panda/test/runtime/ets/UpdateExpression.ets +++ b/ets2panda/test/runtime/ets/UpdateExpression.ets @@ -92,4 +92,10 @@ function main(): void { assert ++s as int == -32768; assert --s as int == 32767; } + + { + let b: Int[] = [1, 2] + assert ++b[1] == 3 + assert --b[1] == 2 + } } -- Gitee From 4d711f3d6abdd0e490d74a6b30d05743bc84e2d3 Mon Sep 17 00:00:00 2001 From: Robert Sipka Date: Wed, 17 Jan 2024 14:27:46 +0100 Subject: [PATCH 15/19] [ArkTs] Remove some redundant code parts around source handling Remove redundant parsedSources_ vector Rename sourceFilePath_ to sourceFile_ Remove unused fileName_ member Remove redundant absoluteName_ member Signed-off-by: Robert Sipka --- ets2panda/compiler/core/ETSemitter.cpp | 4 +-- ets2panda/compiler/core/emitter.cpp | 2 +- ets2panda/parser/ETSparser.cpp | 16 +++++------ ets2panda/parser/ETSparser.h | 3 +-- ets2panda/parser/program/program.h | 37 +++++++------------------- ets2panda/varbinder/ETSBinder.cpp | 2 +- 6 files changed, 21 insertions(+), 43 deletions(-) diff --git a/ets2panda/compiler/core/ETSemitter.cpp b/ets2panda/compiler/core/ETSemitter.cpp index 04faea2890..116c3a7432 100644 --- a/ets2panda/compiler/core/ETSemitter.cpp +++ b/ets2panda/compiler/core/ETSemitter.cpp @@ -412,7 +412,7 @@ void ETSEmitter::GenInterfaceRecord(const ir::TSInterfaceDeclaration *interfaceD } interfaceRecord.metadata->SetAccessFlags(accessFlags); - interfaceRecord.sourceFile = Context()->VarBinder()->Program()->AbsoluteName().Mutf8(); + interfaceRecord.sourceFile = Context()->VarBinder()->Program()->SourceFile().GetAbsolutePath().Mutf8(); interfaceRecord.metadata->SetAttributeValue(Signatures::EXTENDS_ATTRIBUTE, Signatures::BUILTIN_OBJECT); for (auto *it : baseType->Interfaces()) { @@ -456,7 +456,7 @@ void ETSEmitter::GenClassRecord(const ir::ClassDefinition *classDef, bool extern } classRecord.metadata->SetAccessFlags(accessFlags); - classRecord.sourceFile = Context()->VarBinder()->Program()->AbsoluteName().Mutf8(); + classRecord.sourceFile = Context()->VarBinder()->Program()->SourceFile().GetAbsolutePath().Mutf8(); auto *baseType = classDef->TsType()->AsETSObjectType(); if (baseType->SuperType() != nullptr) { diff --git a/ets2panda/compiler/core/emitter.cpp b/ets2panda/compiler/core/emitter.cpp index 6242436e0b..bfc94a084d 100644 --- a/ets2panda/compiler/core/emitter.cpp +++ b/ets2panda/compiler/core/emitter.cpp @@ -257,7 +257,7 @@ void FunctionEmitter::GenFunctionCatchTables(pandasm::Function *func) void FunctionEmitter::GenSourceFileDebugInfo(pandasm::Function *func) { - func->sourceFile = std::string {cg_->VarBinder()->Program()->AbsoluteName()}; + func->sourceFile = std::string {cg_->VarBinder()->Program()->SourceFile().GetAbsolutePath()}; if (!cg_->IsDebug()) { return; diff --git a/ets2panda/parser/ETSparser.cpp b/ets2panda/parser/ETSparser.cpp index 8bacd3eba6..7d587b6939 100644 --- a/ets2panda/parser/ETSparser.cpp +++ b/ets2panda/parser/ETSparser.cpp @@ -190,7 +190,6 @@ void ETSParser::ParseETSGlobalScript(lexer::SourcePosition startLoc, ArenaVector for (const auto &item : items) { auto resolved = ResolveImportPath(item); resolvedParsedSources_.emplace(item, resolved); - parsedSources_.push_back(resolved); } }; // clang-format on @@ -288,7 +287,7 @@ static bool IsCompitableExtension(const std::string &extension) return extension == ".ets" || extension == ".ts"; } -void ETSParser::CollectDefaultSources() +std::vector ETSParser::CollectDefaultSources() { std::vector paths; std::vector stdlib = {"std/core", "std/math", "std/containers", @@ -319,9 +318,9 @@ void ETSParser::CollectDefaultSources() std::string filePath = path + "/" + entry->d_name; if (fileName == "Object.ets") { - parsedSources_.emplace(parsedSources_.begin(), filePath); + paths.emplace(paths.begin(), filePath); } else { - parsedSources_.emplace_back(filePath); + paths.emplace_back(filePath); } } @@ -340,13 +339,14 @@ void ETSParser::CollectDefaultSources() baseName.append(entry.path().string().substr(pos, entry.path().string().size())); if (entry.path().filename().string() == "Object.ets") { - parsedSources_.emplace(parsedSources_.begin(), baseName); + paths.emplace(paths.begin(), baseName); } else { - parsedSources_.emplace_back(baseName); + paths.emplace_back(baseName); } } } #endif + return paths; } ETSParser::ImportData ETSParser::GetImportData(const std::string &path) @@ -598,9 +598,7 @@ void ETSParser::ParseDefaultSources() ParseImportDeclarations(statements); GetContext().Status() &= ~ParserStatus::IN_DEFAULT_IMPORTS; - CollectDefaultSources(); - - ParseSources(parsedSources_, true); + ParseSources(CollectDefaultSources(), true); } void ETSParser::ParseSource(const SourceFile &sourceFile) diff --git a/ets2panda/parser/ETSparser.h b/ets2panda/parser/ETSparser.h index 89490be064..ca8cec8114 100644 --- a/ets2panda/parser/ETSparser.h +++ b/ets2panda/parser/ETSparser.h @@ -124,7 +124,7 @@ private: static int NFTWCallBack(const char *fpath, const struct stat * /*unused*/, int tflag, struct FTW * /*unused*/); #endif void ParseTopLevelDeclaration(ArenaVector &statements); - void CollectDefaultSources(); + std::vector CollectDefaultSources(); void CollectUserSourcesFromIndex(const std::string &path, const std::string &resolvedPath, std::vector &userPaths); std::string ResolveImportPath(const std::string &path); @@ -382,7 +382,6 @@ public: private: parser::Program *globalProgram_; - std::vector parsedSources_; std::vector insertingNodes_ {}; std::unordered_map resolvedParsedSources_; }; diff --git a/ets2panda/parser/program/program.h b/ets2panda/parser/program/program.h index b1a29f3b1f..a289ac73b6 100644 --- a/ets2panda/parser/program/program.h +++ b/ets2panda/parser/program/program.h @@ -94,24 +94,19 @@ public: return sourceCode_; } - util::StringView SourceFilePath() const + const util::StringView &SourceFilePath() const { - return sourceFilePath_.GetPath(); + return sourceFile_.GetPath(); } - util::StringView SourceFileFolder() const + const util::Path &SourceFile() const { - return sourceFileFolder_; + return sourceFile_; } - util::StringView FileName() const - { - return fileName_; - } - - util::StringView AbsoluteName() const + util::StringView SourceFileFolder() const { - return absoluteName_; + return sourceFileFolder_; } util::StringView ResolvedFilePath() const @@ -163,17 +158,15 @@ public: const util::StringView &sourceFileFolder) { sourceCode_ = sourceCode; - sourceFilePath_ = util::Path(sourceFilePath, Allocator()); + sourceFile_ = util::Path(sourceFilePath, Allocator()); sourceFileFolder_ = sourceFileFolder; - absoluteName_ = util::UString(os::GetAbsolutePath(sourceFilePath.Utf8()), Allocator()).View(); } void SetSource(const panda::es2panda::SourceFile &sourceFile) { sourceCode_ = util::UString(sourceFile.source, Allocator()).View(); - sourceFilePath_ = util::Path(sourceFile.filePath, Allocator()); + sourceFile_ = util::Path(sourceFile.filePath, Allocator()); sourceFileFolder_ = util::UString(sourceFile.fileFolder, Allocator()).View(); - absoluteName_ = sourceFilePath_.GetAbsolutePath(); resolvedFilePath_ = util::UString(sourceFile.resolvedPath, Allocator()).View(); } @@ -187,16 +180,6 @@ public: packageName_ = packageName; } - void SetFileName(util::StringView fileName) - { - fileName_ = util::UString(fileName, Allocator()).View(); - } - - void SetAbsoluteName(util::StringView absouleName) - { - absoluteName_ = util::UString(absouleName, Allocator()).View(); - } - const bool &IsEntryPoint() const { return entryPoint_; @@ -225,11 +208,9 @@ private: ir::BlockStatement *ast_ {}; ir::ClassDefinition *globalClass_ {}; util::StringView sourceCode_ {}; - util::Path sourceFilePath_ {}; + util::Path sourceFile_ {}; util::StringView sourceFileFolder_ {}; util::StringView packageName_ {}; - util::StringView fileName_ {}; - util::StringView absoluteName_ {}; util::StringView resolvedFilePath_ {}; ExternalSource externalSources_; ScriptKind kind_ {}; diff --git a/ets2panda/varbinder/ETSBinder.cpp b/ets2panda/varbinder/ETSBinder.cpp index 3df41bd5eb..06d08f3553 100644 --- a/ets2panda/varbinder/ETSBinder.cpp +++ b/ets2panda/varbinder/ETSBinder.cpp @@ -1036,7 +1036,7 @@ void ETSBinder::BuildETSNewClassInstanceExpression(ir::ETSNewClassInstanceExpres void ETSBinder::BuildImportDeclaration(ir::ETSImportDeclaration *decl) { - if (decl->Source()->Str() == Program()->AbsoluteName()) { + if (decl->Source()->Str() == Program()->SourceFile().GetAbsolutePath()) { return; } -- Gitee From 294fcc740579f0884f633914a3f581bc8f545d91 Mon Sep 17 00:00:00 2001 From: peterseres Date: Tue, 19 Dec 2023 16:09:13 +0100 Subject: [PATCH 16/19] Change-Id: Ib692ef522d53042e347e9ba79d11f7a82a57f709 Title: Add new ninja target Description: es2panda-pre-test to run parser, runtime, ets-cts, function test cases clang-tidy and clang format. This commit contain the Python script. Fixes internal issue #13849 Testing: This commit and issue doesn't contains test cases because it's a Python script and a little modification in cmake. Signed-off-by: Peter Seres --- ets2panda/scripts/es2panda_pre_test.py | 184 +++++++++++++++++++++++++ 1 file changed, 184 insertions(+) create mode 100644 ets2panda/scripts/es2panda_pre_test.py diff --git a/ets2panda/scripts/es2panda_pre_test.py b/ets2panda/scripts/es2panda_pre_test.py new file mode 100644 index 0000000000..217f955c94 --- /dev/null +++ b/ets2panda/scripts/es2panda_pre_test.py @@ -0,0 +1,184 @@ +# +# Copyright (c) 2022-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. +# +import re +import argparse +import subprocess +import time + +from enum import Enum + + +class TestTypes(Enum): + NORMAL = 0 + CLANGTIDY = 1 + CLANGFORMAT = 2 + + +def print_summary(log_lines, searched_words, end_word=""): + was_any_log = False + should_print = False + + for line in log_lines: + for word in searched_words: + if any(word in line for word in searched_words): + should_print = True + was_any_log = True + + if end_word and end_word in line: + should_print=False + + if should_print: + print(line.strip()) + + if not was_any_log: + print("Problem occourd: ", was_any_log) + + print(f"\n") + + +def print_progress_to_console(line, test_type, pattern_progress_bar, pattern_clang_tidy, pattern_clang_format): + match test_type: + case TestTypes.NORMAL: + match = pattern_progress_bar.search(line) + if match: + print(line, end='') + case TestTypes.CLANGTIDY: + match = re.match(pattern_clang_tidy, line) + if match: + print(f"[{match.group(1)}/{match.group(2)}] {match.group(3)}") + case TestTypes.CLANGFORMAT: + match = re.match(pattern_clang_format, line) + if match: + print(f"[{match.group(1)}/{match.group(2)}] Clang-format: {match.group(3)} \n") + + +def test_process_runner(runtime_process, test_type): + pattern_progress_bar = re.compile(r'\b(\d{1,3}%)\|(.+?)\| (\d+)/(\d+) \[\d{2}:\d{2}<\d{2}:\d{2},\s*\d+\.\d+it/s\]$') + pattern_clang_tidy = re.compile(r"^\[(\d+)\/(\d+)\]\s+Done\s+clang-tidy:\s+.*?\/es2panda\/(.+\.cpp)$") + pattern_clang_format = re.compile(r"\[(\d+)/(\d+)\]\s+Running clang-format: (.+)") + + should_print = False + log_lines = [] + + for line in runtime_process.stdout: + log_lines.append(line) + print_progress_to_console(line, test_type, pattern_progress_bar, pattern_clang_tidy, pattern_clang_format) + + return log_lines + + +def command_runner(command_to_run, test_type): + try: + process = subprocess.Popen(command_to_run, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True) + process_log = test_process_runner(process, test_type) + + except subprocess.CalledProcessError as e: + process_log = str(e) + print("An error occurred during processing:", process_log) + + except Exception as e: + process_log = str(e) + print("An error occurred during processing:", process_log) + + return process_log + +def print_summary_to_file(log, file_name, word_for_success, word_for_fail, test_name): + has_to_write_out_the_log = False + + for line in log: + if word_for_fail in line: + has_to_write_out_the_log = True + elif word_for_success in line: + print(line) + + if has_to_write_out_the_log: + with open(file_name, "w") as file: + for line in log: + file.write(line) + + print(f"{test_name} is faild. Please check the log file: {file_name}") + + +def main(): + parser = argparse.ArgumentParser(description="Program description.") + parser.add_argument("--working-dir", required=False, help="Path to the working dir") + parser.add_argument("--build-root", required=False, help="Path to the working dir") + + args = parser.parse_args() + print(f"Argumentum: Working dir: {args.working_dir}") + print(f"Argumentum: Build root: {args.build_root}") + + command_helper = ["python3", f"{args.working_dir}/tests/tests-u-runner/main.py", "--build-dir", f"{args.build_root}", + "--processes", "all", "--show-progress", "--force-generate" + ] + + # Run Parser tests + parser_test_run_command = command_helper + ["--parser" ] + + parser_log = command_runner(parser_test_run_command, TestTypes.NORMAL) + + # Run Runtime tests + runtime_test_run_command = command_helper + ["--ets-runtime", "--timeout", "60"] + + runtime_log = command_runner(runtime_test_run_command, TestTypes.NORMAL) + + # Functional tests + functional_test_run_command = command_helper + ["--ets-func-tests"] + + functional_log = command_runner(functional_test_run_command, TestTypes.NORMAL) + + # Run Ets-cts test + ets_cts_test_run_command = command_helper + ["--ets-cts"] + + ets_cts_log = command_runner(ets_cts_test_run_command, TestTypes.NORMAL) + + # Run Clang-format + clang_format_run_command = ["ninja", "-C", f"{args.build_root}", "code-style-check"] + + clang_format_log = command_runner(clang_format_run_command, TestTypes.CLANGFORMAT) + + # Run Clang-tidy + clang_tidy_run_command = [f"{args.working_dir}/scripts/clang-tidy/clang_tidy_check.py", "--filename-filter=ets2panda", + f"{args.working_dir}", f"{args.build_root}" + ] + + clang_tidy_log = command_runner(clang_tidy_run_command,TestTypes.CLANGTIDY) + + print("\n\nTest results: \n") + + print(f"\nParser test results:") + print_summary(parser_log, ["New failures at", "Summary("]) + + print(f"\nRuntime test results:") + print_summary(runtime_log, ["New failures at", "Summary("]) + + print(f"\nFunctional test results:") + print_summary(functional_log, ["Summary("]) + + print(f"\nEts-cts test results:") + print_summary(ets_cts_log, ["New failures at", "Summary("]) + + print(f"\nClang-format results:") + print_summary_to_file(clang_format_log, "clang-format.log", "Clang-format was passed successfully!", "Failed:", "Clang-format") + + print(f"\nClang-tidy results:") + print_summary(clang_tidy_log,["Clang-tidy was passed successfully!", "Failed:"], "Done clang-tidy:") + + +if __name__ == "__main__": + start_time = time.perf_counter() + main() + end_time = time.perf_counter() + print("Runtime: ", (end_time - start_time)/60, " minute" ) -- Gitee From c682325d268281dd6a43ba85c8c88f1cf94e1864 Mon Sep 17 00:00:00 2001 From: Soma Simon Date: Fri, 19 Jan 2024 11:24:35 +0100 Subject: [PATCH 17/19] [ArkTS frontend] Fix call static interface method with type alias Fix condition when creating the searchflag, to find the property Fixed internal issue #14507 Change-Id: Iba1ae4c08e48752f182c21bbb9c3fcc2425bf69d Signed-off-by: Soma Simon --- ets2panda/checker/ets/object.cpp | 6 ++- .../ets/call_static_with_type_alias.ets | 52 +++++++++++++++++++ 2 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 ets2panda/test/runtime/ets/call_static_with_type_alias.ets diff --git a/ets2panda/checker/ets/object.cpp b/ets2panda/checker/ets/object.cpp index f09789c926..8c06deccbc 100644 --- a/ets2panda/checker/ets/object.cpp +++ b/ets2panda/checker/ets/object.cpp @@ -1250,8 +1250,10 @@ PropertySearchFlags ETSChecker::GetSearchFlags(const ir::MemberExpression *const { auto searchFlag = GetInitialSearchFlags(memberExpr); searchFlag |= PropertySearchFlags::SEARCH_IN_BASE | PropertySearchFlags::SEARCH_IN_INTERFACES; - - if (targetRef != nullptr && targetRef->HasFlag(varbinder::VariableFlags::CLASS_OR_INTERFACE)) { + if (targetRef != nullptr && + (targetRef->HasFlag(varbinder::VariableFlags::CLASS_OR_INTERFACE) || + (targetRef->HasFlag(varbinder::VariableFlags::TYPE_ALIAS) && + targetRef->TsType()->Variable()->HasFlag(varbinder::VariableFlags::CLASS_OR_INTERFACE)))) { searchFlag &= ~(PropertySearchFlags::SEARCH_INSTANCE); } else if (memberExpr->Object()->IsThisExpression() || (memberExpr->Object()->IsIdentifier() && memberExpr->ObjType()->GetDeclNode() != nullptr && diff --git a/ets2panda/test/runtime/ets/call_static_with_type_alias.ets b/ets2panda/test/runtime/ets/call_static_with_type_alias.ets new file mode 100644 index 0000000000..0a86ff562a --- /dev/null +++ b/ets2panda/test/runtime/ets/call_static_with_type_alias.ets @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2024 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 I { + static inc(p: number): number { + return p + 1; + } +} + +class C1 { + static inc2(p: number): number { + return p + 2; + } +} + +class C2 implements I { +} + +type TI = I; +type TTI = TI; + +type TC1 = C1; +type TTC1 = TC1; + +type TC2 = C2; +type TTC2 = TC2; + +function main(): void { + assert I.inc(20) == 21; + assert TI.inc(20) == 21; + assert TTI.inc(20) == 21; + + assert C1.inc2(20) == 22; + assert TC1.inc2(20) == 22; + assert TTC1.inc2(20) == 22; + + assert C2.inc(20) == 21; + assert TC2.inc(20) == 21; + assert TTC2.inc(20) == 21; +} -- Gitee From 2ad35af9507f668661a489be79294ec240427373 Mon Sep 17 00:00:00 2001 From: aleksisch Date: Fri, 12 Jan 2024 14:52:45 +0300 Subject: [PATCH 18/19] make field & register traverse deterministic Signed-off-by: aleksisch --- ets2panda/checker/types/ets/etsObjectType.cpp | 7 ++-- ets2panda/checker/types/ets/etsObjectType.h | 2 +- ets2panda/compiler/core/envScope.cpp | 2 +- ets2panda/compiler/core/regScope.cpp | 4 +- .../lowering/scopesInit/savedBindingsCtx.cpp | 2 +- ets2panda/parser/ETSparser.cpp | 39 ++++++++++++------- ets2panda/parser/ETSparser.h | 1 + ets2panda/varbinder/scope.h | 7 ++++ 8 files changed, 42 insertions(+), 22 deletions(-) diff --git a/ets2panda/checker/types/ets/etsObjectType.cpp b/ets2panda/checker/types/ets/etsObjectType.cpp index cb10fbfff5..2a7695b053 100644 --- a/ets2panda/checker/types/ets/etsObjectType.cpp +++ b/ets2panda/checker/types/ets/etsObjectType.cpp @@ -272,12 +272,11 @@ std::vector ETSObjectType::ForeignProperties() return foreignProps; } -std::unordered_map ETSObjectType::CollectAllProperties() const +ArenaMap ETSObjectType::CollectAllProperties() const { - std::unordered_map propMap; + ArenaMap propMap(allocator_->Adapter()); EnsurePropertiesInstantiated(); - propMap.reserve(properties_.size()); - Iterate([&propMap](const varbinder::LocalVariable *var) { propMap.insert({var->Name(), var}); }); + Iterate([&propMap](const varbinder::LocalVariable *var) { propMap.emplace(var->Name(), var); }); return propMap; } diff --git a/ets2panda/checker/types/ets/etsObjectType.h b/ets2panda/checker/types/ets/etsObjectType.h index 464754a587..ff6cca7a94 100644 --- a/ets2panda/checker/types/ets/etsObjectType.h +++ b/ets2panda/checker/types/ets/etsObjectType.h @@ -545,7 +545,7 @@ private: propertiesInstantiated_ = true; } } - std::unordered_map CollectAllProperties() const; + ArenaMap CollectAllProperties() const; void IdenticalUptoNullability(TypeRelation *relation, Type *other); bool CastWideningNarrowing(TypeRelation *relation, Type *target, TypeFlag unboxFlags, TypeFlag wideningFlags, TypeFlag narrowingFlags); diff --git a/ets2panda/compiler/core/envScope.cpp b/ets2panda/compiler/core/envScope.cpp index 7adc372f8f..cb870fdce8 100644 --- a/ets2panda/compiler/core/envScope.cpp +++ b/ets2panda/compiler/core/envScope.cpp @@ -60,7 +60,7 @@ void LoopEnvScope::CopyBindings(PandaGen *pg, varbinder::VariableScope *scope, v ASSERT(scope->NeedLexEnv()); - for (const auto &[_, variable] : scope_->Bindings()) { + for (const auto &[_, variable] : scope_->OrderedBindings(pg_->Allocator())) { (void)_; if (!variable->HasFlag(flag)) { continue; diff --git a/ets2panda/compiler/core/regScope.cpp b/ets2panda/compiler/core/regScope.cpp index 6109ffd80f..9f7a6d7e41 100644 --- a/ets2panda/compiler/core/regScope.cpp +++ b/ets2panda/compiler/core/regScope.cpp @@ -52,7 +52,7 @@ LocalRegScope::LocalRegScope(CodeGen *cg, varbinder::Scope *scope) : RegScope(cg prevScope_ = cg_->scope_; cg_->scope_ = scope; - for (const auto &[_, var] : scope->Bindings()) { + for (const auto &[_, var] : scope->OrderedBindings(cg_->Allocator())) { (void)_; if (!var->LexicalBound() && var->IsLocalVariable()) { var->AsLocalVariable()->BindVReg(cg_->AllocReg()); @@ -99,7 +99,7 @@ void FunctionRegScope::InitializeParams(const StoreParamCb &cb) paramReg++; } - for (const auto it : funcScope->Bindings()) { + for (const auto it : funcScope->OrderedBindings(cg_->Allocator())) { auto *const var = std::get<1>(it); if (var->Declaration()->IsParameterDecl() || var->Declaration()->IsTypeAliasDecl()) { continue; diff --git a/ets2panda/compiler/lowering/scopesInit/savedBindingsCtx.cpp b/ets2panda/compiler/lowering/scopesInit/savedBindingsCtx.cpp index 72bab547bd..c8197d78c2 100644 --- a/ets2panda/compiler/lowering/scopesInit/savedBindingsCtx.cpp +++ b/ets2panda/compiler/lowering/scopesInit/savedBindingsCtx.cpp @@ -26,7 +26,7 @@ void ImportDeclarationContext::BindImportDecl(ir::ImportDeclaration *importDecl) { varbinder::ModuleScope::ImportDeclList declList(Allocator()->Adapter()); - for (const auto &[name, variable] : VarBinder()->GetScope()->Bindings()) { + for (const auto &[name, variable] : VarBinder()->GetScope()->OrderedBindings(Allocator())) { if (SavedBindings().find(name) != SavedBindings().end()) { continue; } diff --git a/ets2panda/parser/ETSparser.cpp b/ets2panda/parser/ETSparser.cpp index 7d587b6939..0634e8bdfa 100644 --- a/ets2panda/parser/ETSparser.cpp +++ b/ets2panda/parser/ETSparser.cpp @@ -287,12 +287,9 @@ static bool IsCompitableExtension(const std::string &extension) return extension == ".ets" || extension == ".ts"; } -std::vector ETSParser::CollectDefaultSources() +std::vector ETSParser::UnixApiDefaultSources([[maybe_unused]] const std::vector &stdlib) { std::vector paths; - std::vector stdlib = {"std/core", "std/math", "std/containers", - "std/time", "std/interop/js", "escompat"}; - #ifdef USE_UNIX_SYSCALL for (auto const &path : stdlib) { auto resolvedPath = ResolveImportPath(path); @@ -303,6 +300,7 @@ std::vector ETSParser::CollectDefaultSources() } struct dirent *entry; + std::set orderedFiles; while ((entry = readdir(dir)) != nullptr) { if (entry->d_type != DT_REG) { continue; @@ -310,7 +308,6 @@ std::vector ETSParser::CollectDefaultSources() std::string fileName = entry->d_name; std::string::size_type pos = fileName.find_last_of('.'); - if (pos == std::string::npos || !IsCompitableExtension(fileName.substr(pos))) { continue; } @@ -320,14 +317,27 @@ std::vector ETSParser::CollectDefaultSources() if (fileName == "Object.ets") { paths.emplace(paths.begin(), filePath); } else { - paths.emplace_back(filePath); + orderedFiles.emplace(filePath); } } - + paths.insert(paths.end(), orderedFiles.begin(), orderedFiles.end()); closedir(dir); } +#endif + return paths; +} + +std::vector ETSParser::CollectDefaultSources() +{ + std::vector stdlib = {"std/core", "std/math", "std/containers", + "std/time", "std/interop/js", "escompat"}; + +#ifdef USE_UNIX_SYSCALL + return UnixApiDefaultSources(stdlib); #else + std::vector paths; for (auto const &path : stdlib) { + std::set orderedFiles; for (auto const &entry : fs::directory_iterator(ResolveImportPath(path))) { if (!fs::is_regular_file(entry) || !IsCompitableExtension(entry.path().extension().string())) { continue; @@ -341,12 +351,13 @@ std::vector ETSParser::CollectDefaultSources() if (entry.path().filename().string() == "Object.ets") { paths.emplace(paths.begin(), baseName); } else { - paths.emplace_back(baseName); + orderedFiles.emplace(baseName); } } + paths.insert(paths.end(), orderedFiles.begin(), orderedFiles.end()); } -#endif return paths; +#endif } ETSParser::ImportData ETSParser::GetImportData(const std::string &path) @@ -464,7 +475,7 @@ void ETSParser::CollectUserSourcesFromIndex([[maybe_unused]] const std::string & #ifdef USE_UNIX_SYSCALL DIR *dir = opendir(resolvedPath.c_str()); bool isIndex = false; - std::vector tmpPaths; + std::set tmpPaths; if (dir == nullptr) { ThrowSyntaxError({"Cannot open folder: ", resolvedPath}); @@ -489,9 +500,9 @@ void ETSParser::CollectUserSourcesFromIndex([[maybe_unused]] const std::string & isIndex = true; break; } else if (fileName == "Object.ets") { - tmpPaths.emplace(userPaths.begin(), filePath); + userPaths.emplace(userPaths.begin(), filePath); } else { - tmpPaths.emplace_back(filePath); + tmpPaths.insert(filePath); } } @@ -531,6 +542,7 @@ std::tuple, bool> ETSParser::CollectUserSources(const s } else if (fs::exists(resolvedPath + "/index.ts")) { userPaths.emplace_back(path + "/index.ts"); } else { + std::set orderedFiles; for (auto const &entry : fs::directory_iterator(resolvedPath)) { if (!fs::is_regular_file(entry) || !IsCompitableExtension(entry.path().extension().string())) { continue; @@ -540,8 +552,9 @@ std::tuple, bool> ETSParser::CollectUserSources(const s std::size_t pos = entry.path().string().find_last_of(panda::os::file::File::GetPathDelim()); baseName.append(entry.path().string().substr(pos, entry.path().string().size())); - userPaths.emplace_back(baseName); + orderedFiles.emplace(baseName); } + userPaths.insert(userPaths.begin(), orderedFiles.begin(), orderedFiles.end()); } #endif return {userPaths, false}; diff --git a/ets2panda/parser/ETSparser.h b/ets2panda/parser/ETSparser.h index ca8cec8114..12938fa9da 100644 --- a/ets2panda/parser/ETSparser.h +++ b/ets2panda/parser/ETSparser.h @@ -124,6 +124,7 @@ private: static int NFTWCallBack(const char *fpath, const struct stat * /*unused*/, int tflag, struct FTW * /*unused*/); #endif void ParseTopLevelDeclaration(ArenaVector &statements); + std::vector UnixApiDefaultSources(const std::vector &stdlib); std::vector CollectDefaultSources(); void CollectUserSourcesFromIndex(const std::string &path, const std::string &resolvedPath, std::vector &userPaths); diff --git a/ets2panda/varbinder/scope.h b/ets2panda/varbinder/scope.h index b53625d68e..f1030ca408 100644 --- a/ets2panda/varbinder/scope.h +++ b/ets2panda/varbinder/scope.h @@ -245,6 +245,13 @@ public: return bindings_; } + ArenaMap OrderedBindings(ArenaAllocator *allocator) const + { + ArenaMap result(allocator->Adapter()); + result.insert(bindings_.begin(), bindings_.end()); + return result; + } + virtual Variable *AddBinding(ArenaAllocator *allocator, Variable *currentVariable, Decl *newDecl, [[maybe_unused]] ScriptExtension extension) = 0; -- Gitee From a75d378a83ecb98491e5d22530621221f2bf64af Mon Sep 17 00:00:00 2001 From: Vivien Voros Date: Thu, 11 Jan 2024 10:49:37 +0100 Subject: [PATCH 19/19] 'type' is hard keyword Change 'type' to soft keyword. Fixes internal issue #13941 Change-Id: Ia3a52c904e2ccc12b0c37448aa1f5eddf59a93e4 Signed-off-by: Vivien Voros --- ets2panda/lexer/scripts/keywords.yaml | 3 +- ets2panda/lexer/token/token.cpp | 1 + ets2panda/parser/ETSparser.cpp | 15 +- ets2panda/parser/ETSparser.h | 1 + .../parser/ets/user_defined_22-expected.txt | 830 ++++++++++++++++++ ets2panda/test/parser/ets/user_defined_22.ets | 22 + .../parser/ets/user_defined_23-expected.txt | 1 + ets2panda/test/parser/ets/user_defined_23.ets | 18 + .../parser/ets/user_defined_24-expected.txt | 1 + ets2panda/test/parser/ets/user_defined_24.ets | 18 + .../parser/ets/user_defined_25-expected.txt | 1 + ets2panda/test/parser/ets/user_defined_25.ets | 18 + 12 files changed, 923 insertions(+), 6 deletions(-) create mode 100644 ets2panda/test/parser/ets/user_defined_22-expected.txt create mode 100644 ets2panda/test/parser/ets/user_defined_22.ets create mode 100644 ets2panda/test/parser/ets/user_defined_23-expected.txt create mode 100644 ets2panda/test/parser/ets/user_defined_23.ets create mode 100644 ets2panda/test/parser/ets/user_defined_24-expected.txt create mode 100644 ets2panda/test/parser/ets/user_defined_24.ets create mode 100644 ets2panda/test/parser/ets/user_defined_25-expected.txt create mode 100644 ets2panda/test/parser/ets/user_defined_25.ets diff --git a/ets2panda/lexer/scripts/keywords.yaml b/ets2panda/lexer/scripts/keywords.yaml index e437a74c86..d6f8a5540f 100644 --- a/ets2panda/lexer/scripts/keywords.yaml +++ b/ets2panda/lexer/scripts/keywords.yaml @@ -430,8 +430,7 @@ keywords: - name: 'type' token: KEYW_TYPE - keyword: [ets] - keyword_like: [js, ts] + keyword_like: [ets, js, ts] - name: 'typeof' token: KEYW_TYPEOF diff --git a/ets2panda/lexer/token/token.cpp b/ets2panda/lexer/token/token.cpp index cf4e3e3033..1135f0f0a3 100644 --- a/ets2panda/lexer/token/token.cpp +++ b/ets2panda/lexer/token/token.cpp @@ -107,6 +107,7 @@ bool Token::IsDefinableTypeName() const case TokenType::KEYW_INT: case TokenType::KEYW_CHAR: case TokenType::KEYW_LONG: + case TokenType::KEYW_TYPE: return true; default: return false; diff --git a/ets2panda/parser/ETSparser.cpp b/ets2panda/parser/ETSparser.cpp index 0634e8bdfa..b91b25f816 100644 --- a/ets2panda/parser/ETSparser.cpp +++ b/ets2panda/parser/ETSparser.cpp @@ -741,6 +741,11 @@ void ETSParser::ParseTopLevelNextTokenDefault(ArenaVector &stat return; } + if (IsTypeKeyword()) { + ParseTopLevelType(statements, defaultExport, currentPos, &ETSParser::ParseTypeAliasDeclaration); + return; + } + if (initFunction != nullptr) { if (auto *const statement = ParseTopLevelStatement(); statement != nullptr) { statement->SetParent(initFunction->Body()); @@ -809,10 +814,6 @@ lexer::SourcePosition ETSParser::ParseTopLevelNextTokenResolution(ArenaVectorGetToken().KeywordType() == lexer::TokenType::KEYW_STRUCT); } +bool ETSParser::IsTypeKeyword() const +{ + return (Lexer()->GetToken().Type() == lexer::TokenType::LITERAL_IDENT && + Lexer()->GetToken().KeywordType() == lexer::TokenType::KEYW_TYPE); +} + void ETSParser::ValidateInstanceOfExpression(ir::Expression *expr) { ValidateGroupedExpression(expr); diff --git a/ets2panda/parser/ETSparser.h b/ets2panda/parser/ETSparser.h index 12938fa9da..ffaee6e91b 100644 --- a/ets2panda/parser/ETSparser.h +++ b/ets2panda/parser/ETSparser.h @@ -325,6 +325,7 @@ private: } bool IsStructKeyword() const; + bool IsTypeKeyword() const; // NOLINTNEXTLINE(google-default-arguments) ir::Expression *ParseExpression(ExpressionParseFlags flags = ExpressionParseFlags::NO_OPTS) override; diff --git a/ets2panda/test/parser/ets/user_defined_22-expected.txt b/ets2panda/test/parser/ets/user_defined_22-expected.txt new file mode 100644 index 0000000000..6b33f24143 --- /dev/null +++ b/ets2panda/test/parser/ets/user_defined_22-expected.txt @@ -0,0 +1,830 @@ +{ + "type": "Program", + "statements": [ + { + "type": "ClassDeclaration", + "definition": { + "id": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 7 + }, + "end": { + "line": 16, + "column": 8 + } + } + }, + "superClass": null, + "implements": [], + "body": [ + { + "type": "ClassProperty", + "key": { + "type": "Identifier", + "name": "type", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 3 + }, + "end": { + "line": 17, + "column": 7 + } + } + }, + "value": { + "type": "NumberLiteral", + "value": 0, + "loc": { + "start": { + "line": 17, + "column": 18 + }, + "end": { + "line": 17, + "column": 19 + } + } + }, + "accessibility": "public", + "static": false, + "readonly": false, + "declare": false, + "optional": false, + "computed": false, + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "number", + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 9 + }, + "end": { + "line": 17, + "column": 15 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 9 + }, + "end": { + "line": 17, + "column": 17 + } + } + }, + "loc": { + "start": { + "line": 17, + "column": 9 + }, + "end": { + "line": 17, + "column": 17 + } + } + }, + "definite": false, + "decorators": [], + "loc": { + "start": { + "line": 17, + "column": 3 + }, + "end": { + "line": 17, + "column": 19 + } + } + }, + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "constructor", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "kind": "constructor", + "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": [], + "body": { + "type": "BlockStatement", + "statements": [], + "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": 1, + "column": 1 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 18, + "column": 2 + }, + "end": { + "line": 18, + "column": 2 + } + } + } + ], + "loc": { + "start": { + "line": 16, + "column": 9 + }, + "end": { + "line": 18, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 1 + }, + "end": { + "line": 18, + "column": 2 + } + } + }, + { + "type": "TSTypeAliasDeclaration", + "id": { + "type": "Identifier", + "name": "B", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 6 + }, + "end": { + "line": 19, + "column": 7 + } + } + }, + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "string", + "decorators": [], + "loc": { + "start": { + "line": 19, + "column": 10 + }, + "end": { + "line": 19, + "column": 16 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 10 + }, + "end": { + "line": 20, + "column": 4 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 10 + }, + "end": { + "line": 20, + "column": 4 + } + } + }, + "loc": { + "start": { + "line": 19, + "column": 1 + }, + "end": { + "line": 20, + "column": 4 + } + } + }, + { + "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": "_$init$_", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "kind": "method", + "accessibility": "public", + "static": true, + "optional": false, + "computed": false, + "value": { + "type": "FunctionExpression", + "function": { + "type": "ScriptFunction", + "id": { + "type": "Identifier", + "name": "_$init$_", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ExpressionStatement", + "expression": { + "type": "AssignmentExpression", + "operator": "=", + "left": { + "type": "Identifier", + "name": "a", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 5 + }, + "end": { + "line": 20, + "column": 6 + } + } + }, + "right": { + "type": "ETSNewClassInstanceExpression", + "typeReference": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 13 + }, + "end": { + "line": 20, + "column": 14 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 13 + }, + "end": { + "line": 21, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 13 + }, + "end": { + "line": 21, + "column": 2 + } + } + }, + "arguments": [], + "loc": { + "start": { + "line": 20, + "column": 9 + }, + "end": { + "line": 21, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 5 + }, + "end": { + "line": 21, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 1 + }, + "end": { + "line": 21, + "column": 2 + } + } + }, + { + "type": "ExpressionStatement", + "expression": { + "type": "MemberExpression", + "object": { + "type": "Identifier", + "name": "a", + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 1 + }, + "end": { + "line": 21, + "column": 2 + } + } + }, + "property": { + "type": "Identifier", + "name": "type", + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 3 + }, + "end": { + "line": 21, + "column": 7 + } + } + }, + "computed": false, + "optional": false, + "loc": { + "start": { + "line": 21, + "column": 1 + }, + "end": { + "line": 21, + "column": 7 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 1 + }, + "end": { + "line": 21, + "column": 7 + } + } + }, + { + "type": "ExpressionStatement", + "expression": { + "type": "AssignmentExpression", + "operator": "=", + "left": { + "type": "Identifier", + "name": "b", + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 5 + }, + "end": { + "line": 22, + "column": 6 + } + } + }, + "right": { + "type": "StringLiteral", + "value": "", + "loc": { + "start": { + "line": 22, + "column": 12 + }, + "end": { + "line": 22, + "column": 14 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 5 + }, + "end": { + "line": 22, + "column": 14 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 1 + }, + "end": { + "line": 22, + "column": 14 + } + } + } + ], + "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": 1, + "column": 1 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 1 + } + } + }, + { + "type": "ClassProperty", + "key": { + "type": "Identifier", + "name": "a", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 5 + }, + "end": { + "line": 20, + "column": 6 + } + } + }, + "value": { + "type": "ETSNewClassInstanceExpression", + "typeReference": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "A", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 13 + }, + "end": { + "line": 20, + "column": 14 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 13 + }, + "end": { + "line": 21, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 13 + }, + "end": { + "line": 21, + "column": 2 + } + } + }, + "arguments": [], + "loc": { + "start": { + "line": 20, + "column": 9 + }, + "end": { + "line": 21, + "column": 2 + } + } + }, + "accessibility": "public", + "static": true, + "readonly": false, + "declare": false, + "optional": false, + "computed": false, + "definite": false, + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 5 + }, + "end": { + "line": 21, + "column": 2 + } + } + }, + { + "type": "ClassProperty", + "key": { + "type": "Identifier", + "name": "b", + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 5 + }, + "end": { + "line": 22, + "column": 6 + } + } + }, + "accessibility": "public", + "static": true, + "readonly": false, + "declare": false, + "optional": false, + "computed": false, + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "B", + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 8 + }, + "end": { + "line": 22, + "column": 9 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 8 + }, + "end": { + "line": 22, + "column": 11 + } + } + }, + "loc": { + "start": { + "line": 22, + "column": 8 + }, + "end": { + "line": 22, + "column": 11 + } + } + }, + "definite": false, + "decorators": [], + "loc": { + "start": { + "line": 22, + "column": 5 + }, + "end": { + "line": 22, + "column": 11 + } + } + } + ], + "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": 23, + "column": 1 + } + } +} diff --git a/ets2panda/test/parser/ets/user_defined_22.ets b/ets2panda/test/parser/ets/user_defined_22.ets new file mode 100644 index 0000000000..75ace1622e --- /dev/null +++ b/ets2panda/test/parser/ets/user_defined_22.ets @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2024 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 A { + type: number = 0 +} +type B = string +let a = new A +a.type +let b: B = "" diff --git a/ets2panda/test/parser/ets/user_defined_23-expected.txt b/ets2panda/test/parser/ets/user_defined_23-expected.txt new file mode 100644 index 0000000000..6fb05ccbb5 --- /dev/null +++ b/ets2panda/test/parser/ets/user_defined_23-expected.txt @@ -0,0 +1 @@ +SyntaxError: Cannot be used as user-defined type. [user_defined_23.ets:16:7] diff --git a/ets2panda/test/parser/ets/user_defined_23.ets b/ets2panda/test/parser/ets/user_defined_23.ets new file mode 100644 index 0000000000..43577e643e --- /dev/null +++ b/ets2panda/test/parser/ets/user_defined_23.ets @@ -0,0 +1,18 @@ +/* + * Copyright (c) 2024 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 type { + type: number = 0 +} diff --git a/ets2panda/test/parser/ets/user_defined_24-expected.txt b/ets2panda/test/parser/ets/user_defined_24-expected.txt new file mode 100644 index 0000000000..458482bb15 --- /dev/null +++ b/ets2panda/test/parser/ets/user_defined_24-expected.txt @@ -0,0 +1 @@ +SyntaxError: Cannot be used as user-defined type. [user_defined_24.ets:16:8] diff --git a/ets2panda/test/parser/ets/user_defined_24.ets b/ets2panda/test/parser/ets/user_defined_24.ets new file mode 100644 index 0000000000..c52cb720a5 --- /dev/null +++ b/ets2panda/test/parser/ets/user_defined_24.ets @@ -0,0 +1,18 @@ +/* + * Copyright (c) 2024 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. + */ + +struct type { + type: number = 0 +} diff --git a/ets2panda/test/parser/ets/user_defined_25-expected.txt b/ets2panda/test/parser/ets/user_defined_25-expected.txt new file mode 100644 index 0000000000..105c5ae761 --- /dev/null +++ b/ets2panda/test/parser/ets/user_defined_25-expected.txt @@ -0,0 +1 @@ +SyntaxError: Cannot be used as user-defined type. [user_defined_25.ets:16:11] diff --git a/ets2panda/test/parser/ets/user_defined_25.ets b/ets2panda/test/parser/ets/user_defined_25.ets new file mode 100644 index 0000000000..db7c0eb73b --- /dev/null +++ b/ets2panda/test/parser/ets/user_defined_25.ets @@ -0,0 +1,18 @@ +/* + * Copyright (c) 2024 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 type { + type: number = 0 +} -- Gitee