From e3e72878778456fba41e7b898e256f321427cdaf Mon Sep 17 00:00:00 2001 From: Peter Seres Date: Mon, 2 Oct 2023 13:44:04 +0200 Subject: [PATCH 1/2] Default values now not cause problem in implicitly typed return. Fixes #13732 internal issue Change-Id: I317f3629fc9b553e5c1f026e575c9edcf7cc830c Signed-off-by: Seres Peter --- parser/ETSparser.cpp | 9 +- ..._implicitly_typed_return_void-expected.txt | 1479 +++++++++++++++++ ...parameter_implicitly_typed_return_void.ets | 22 + 3 files changed, 1506 insertions(+), 4 deletions(-) create mode 100644 test/parser/ets/default_parameter_implicitly_typed_return_void-expected.txt create mode 100644 test/parser/ets/default_parameter_implicitly_typed_return_void.ets diff --git a/parser/ETSparser.cpp b/parser/ETSparser.cpp index feece7121..06f13bc60 100644 --- a/parser/ETSparser.cpp +++ b/parser/ETSparser.cpp @@ -2173,10 +2173,11 @@ void ETSParser::AddProxyOverloadToMethodWithDefaultParams(ir::MethodDefinition * proxy_method += param->Ident()->Name().Mutf8() + ": " + GetNameForTypeNode(param->TypeAnnotation()) + ", "; } - std::string return_type = method->Function()->ReturnTypeAnnotation() != nullptr - ? GetNameForTypeNode(method->Function()->ReturnTypeAnnotation()) - : "void"; - proxy_method += "proxy_int:int): " + return_type + " { "; + const bool has_function_return_type = method->Function()->ReturnTypeAnnotation() != nullptr; + const std::string return_type = + has_function_return_type ? GetNameForTypeNode(method->Function()->ReturnTypeAnnotation()) : ""; + + proxy_method += has_function_return_type ? "proxy_int:int):" + return_type + "{" : "proxy_int:int) {"; auto const parameters_number = function->Params().size(); for (size_t i = 0U; i < parameters_number; i++) { diff --git a/test/parser/ets/default_parameter_implicitly_typed_return_void-expected.txt b/test/parser/ets/default_parameter_implicitly_typed_return_void-expected.txt new file mode 100644 index 000000000..fcf364402 --- /dev/null +++ b/test/parser/ets/default_parameter_implicitly_typed_return_void-expected.txt @@ -0,0 +1,1479 @@ +{ + "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": "foo", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 10 + }, + "end": { + "line": 16, + "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": 16, + "column": 10 + }, + "end": { + "line": 16, + "column": 13 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [ + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "t", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "string", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 17 + }, + "end": { + "line": 16, + "column": 23 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 17 + }, + "end": { + "line": 16, + "column": 24 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 17 + }, + "end": { + "line": 16, + "column": 24 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 14 + }, + "end": { + "line": 16, + "column": 24 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 14 + }, + "end": { + "line": 16, + "column": 24 + } + } + }, + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "x", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "string", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 29 + }, + "end": { + "line": 16, + "column": 35 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 29 + }, + "end": { + "line": 16, + "column": 37 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 29 + }, + "end": { + "line": 16, + "column": 37 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 25 + }, + "end": { + "line": 16, + "column": 37 + } + } + }, + "initializer": { + "type": "StringLiteral", + "value": "default", + "loc": { + "start": { + "line": 16, + "column": 38 + }, + "end": { + "line": 16, + "column": 47 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 25 + }, + "end": { + "line": 16, + "column": 47 + } + } + }, + { + "type": "ETSParameterExpression", + "name": { + "type": "Identifier", + "name": "y", + "typeAnnotation": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "string", + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 52 + }, + "end": { + "line": 16, + "column": 58 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 52 + }, + "end": { + "line": 16, + "column": 60 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 52 + }, + "end": { + "line": 16, + "column": 60 + } + } + }, + "decorators": [], + "loc": { + "start": { + "line": 16, + "column": 49 + }, + "end": { + "line": 16, + "column": 60 + } + } + }, + "initializer": { + "type": "StringLiteral", + "value": "another", + "loc": { + "start": { + "line": 16, + "column": 61 + }, + "end": { + "line": 16, + "column": 70 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 49 + }, + "end": { + "line": 16, + "column": 70 + } + } + } + ], + "body": { + "type": "BlockStatement", + "statements": [], + "loc": { + "start": { + "line": 16, + "column": 72 + }, + "end": { + "line": 18, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 13 + }, + "end": { + "line": 18, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 16, + "column": 13 + }, + "end": { + "line": 18, + "column": 2 + } + } + }, + "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": true, + "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": "t", + "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": "x", + "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": "y", + "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_int", + "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_int", + "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": "x", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "right": { + "type": "StringLiteral", + "value": "default", + "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": "IfStatement", + "test": { + "type": "BinaryExpression", + "operator": "==", + "left": { + "type": "BinaryExpression", + "operator": "&", + "left": { + "type": "BinaryExpression", + "operator": ">>", + "left": { + "type": "Identifier", + "name": "proxy_int", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "right": { + "type": "NumberLiteral", + "value": 2, + "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": "y", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "right": { + "type": "StringLiteral", + "value": "another", + "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": "Identifier", + "name": "foo", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + "arguments": [ + { + "type": "Identifier", + "name": "t", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + { + "type": "Identifier", + "name": "x", + "decorators": [], + "loc": { + "start": { + "line": 1, + "column": 3 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + { + "type": "Identifier", + "name": "y", + "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": 16, + "column": 1 + }, + "end": { + "line": 18, + "column": 2 + } + } + }, + { + "type": "MethodDefinition", + "key": { + "type": "Identifier", + "name": "main", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 10 + }, + "end": { + "line": 20, + "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": 20, + "column": 10 + }, + "end": { + "line": 20, + "column": 14 + } + } + }, + "generator": false, + "async": false, + "expression": false, + "params": [], + "returnType": { + "type": "ETSTypeReference", + "part": { + "type": "ETSTypeReferencePart", + "name": { + "type": "Identifier", + "name": "void", + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 18 + }, + "end": { + "line": 20, + "column": 22 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 18 + }, + "end": { + "line": 20, + "column": 23 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 18 + }, + "end": { + "line": 20, + "column": 23 + } + } + }, + "body": { + "type": "BlockStatement", + "statements": [ + { + "type": "ExpressionStatement", + "expression": { + "type": "CallExpression", + "callee": { + "type": "Identifier", + "name": "foo", + "decorators": [], + "loc": { + "start": { + "line": 21, + "column": 5 + }, + "end": { + "line": 21, + "column": 8 + } + } + }, + "arguments": [ + { + "type": "StringLiteral", + "value": "Something", + "loc": { + "start": { + "line": 21, + "column": 9 + }, + "end": { + "line": 21, + "column": 20 + } + } + } + ], + "optional": false, + "loc": { + "start": { + "line": 21, + "column": 5 + }, + "end": { + "line": 21, + "column": 21 + } + } + }, + "loc": { + "start": { + "line": 21, + "column": 5 + }, + "end": { + "line": 21, + "column": 22 + } + } + } + ], + "loc": { + "start": { + "line": 20, + "column": 22 + }, + "end": { + "line": 22, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 14 + }, + "end": { + "line": 22, + "column": 2 + } + } + }, + "loc": { + "start": { + "line": 20, + "column": 14 + }, + "end": { + "line": 22, + "column": 2 + } + } + }, + "overloads": [], + "decorators": [], + "loc": { + "start": { + "line": 20, + "column": 1 + }, + "end": { + "line": 22, + "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": 23, + "column": 1 + } + } +} diff --git a/test/parser/ets/default_parameter_implicitly_typed_return_void.ets b/test/parser/ets/default_parameter_implicitly_typed_return_void.ets new file mode 100644 index 000000000..2125d4812 --- /dev/null +++ b/test/parser/ets/default_parameter_implicitly_typed_return_void.ets @@ -0,0 +1,22 @@ +/* +Copyright (c) 2021-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 foo(t: string, x : string = "default", y: string = "another") { + +} + +function main(): void{ + foo("Something"); +} -- Gitee From 70f9df4789f4269d0d7eee2740c7394562881670 Mon Sep 17 00:00:00 2001 From: Ilya Trubachev Date: Mon, 16 Oct 2023 19:15:28 +0300 Subject: [PATCH 2/2] fix default parameters in static methods Signed-off-by: Ilya Trubachev --- checker/ETSAnalyzer.cpp | 5 ++++- checker/checker.h | 3 ++- ir/expressions/identifier.cpp | 7 +++++++ ir/expressions/memberExpression.cpp | 7 +++++++ parser/ETSparser.cpp | 23 ++++++++++++++++------- parser/ETSparser.h | 6 +++--- parser/TypedParser.cpp | 2 +- parser/TypedParser.h | 2 +- parser/parserImpl.cpp | 7 ++++--- parser/parserImpl.h | 5 +++-- 10 files changed, 48 insertions(+), 19 deletions(-) diff --git a/checker/ETSAnalyzer.cpp b/checker/ETSAnalyzer.cpp index e9a68f304..f89ad1183 100644 --- a/checker/ETSAnalyzer.cpp +++ b/checker/ETSAnalyzer.cpp @@ -14,6 +14,8 @@ */ #include "ETSAnalyzer.h" +#include "binder/binder.h" +#include "binder/ETSBinder.h" #include "checker/ets/typeRelationContext.h" #include "ir/base/catchClause.h" #include "ir/base/classProperty.h" @@ -679,7 +681,8 @@ checker::Type *ETSAnalyzer::Check(ir::ReturnStatement *st) const containing_func->Signature()->SetReturnType(func_return_type); containing_func->Signature()->RemoveSignatureFlag(checker::SignatureFlags::NEED_RETURN_TYPE); - + checker->Binder()->AsETSBinder()->BuildFunctionName(containing_func); + if (st->argument_ != nullptr && st->argument_->IsObjectExpression()) { st->argument_->AsObjectExpression()->SetPreferredType(func_return_type); } diff --git a/checker/checker.h b/checker/checker.h index 140727440..7da46362d 100644 --- a/checker/checker.h +++ b/checker/checker.h @@ -183,11 +183,12 @@ public: friend class TypeStackElement; friend class SavedCheckerContext; + binder::Binder *Binder() const; + protected: void Initialize(binder::Binder *binder); parser::Program *Program() const; void SetProgram(parser::Program *program); - binder::Binder *Binder() const; private: ArenaAllocator allocator_; diff --git a/ir/expressions/identifier.cpp b/ir/expressions/identifier.cpp index fd9d7fed6..674faf451 100644 --- a/ir/expressions/identifier.cpp +++ b/ir/expressions/identifier.cpp @@ -152,6 +152,13 @@ checker::Type *Identifier::Check(checker::ETSChecker *checker) } SetTsType(checker->ResolveIdentifier(this)); + if (TsType()->IsETSFunctionType()) { + for (auto* sig: TsType()->AsETSFunctionType()->CallSignatures()) { + if (sig->HasSignatureFlag(checker::SignatureFlags::NEED_RETURN_TYPE)) { + sig->Owner()->GetDeclNode()->Check(checker); + } + } + } return TsType(); } } // namespace panda::es2panda::ir diff --git a/ir/expressions/memberExpression.cpp b/ir/expressions/memberExpression.cpp index 8a4c2bfd8..452e77c7a 100644 --- a/ir/expressions/memberExpression.cpp +++ b/ir/expressions/memberExpression.cpp @@ -385,6 +385,13 @@ checker::Type *MemberExpression::Check(checker::ETSChecker *checker) prop_var_ = checker->ResolveMemberReference(this, obj_type_); checker->ValidatePropertyAccess(prop_var_, obj_type_, property_->Start()); SetTsType(checker->GetTypeOfVariable(prop_var_)); + if (prop_var_ && prop_var_->TsType() && prop_var_->TsType()->IsETSFunctionType()) { + for (auto* sig: prop_var_->TsType()->AsETSFunctionType()->CallSignatures()) { + if (sig->HasSignatureFlag(checker::SignatureFlags::NEED_RETURN_TYPE)) { + sig->Owner()->GetDeclNode()->Check(checker); + } + } + } return TsType(); } } // namespace panda::es2panda::ir diff --git a/parser/ETSparser.cpp b/parser/ETSparser.cpp index 06f13bc60..c5d944133 100644 --- a/parser/ETSparser.cpp +++ b/parser/ETSparser.cpp @@ -1306,7 +1306,7 @@ void ETSParser::ParseClassFieldDefiniton(ir::Identifier *field_name, ir::Modifie } } -ir::MethodDefinition *ETSParser::ParseClassMethodDefinition(ir::Identifier *method_name, ir::ModifierFlags modifiers) +ir::MethodDefinition *ETSParser::ParseClassMethodDefinition(ir::Identifier *method_name, ir::ModifierFlags modifiers, ir::Identifier *ident_node) { auto *cur_scope = Binder()->GetScope(); auto res = cur_scope->Find(method_name->Name(), binder::ResolveBindingOptions::ALL); @@ -1337,7 +1337,7 @@ ir::MethodDefinition *ETSParser::ParseClassMethodDefinition(ir::Identifier *meth method->SetRange(func_expr->Range()); CreateClassFunctionDeclaration(method); - AddProxyOverloadToMethodWithDefaultParams(method); + AddProxyOverloadToMethodWithDefaultParams(method, ident_node); return method; } @@ -1480,7 +1480,8 @@ void ETSParser::ValidateLabeledStatement(lexer::TokenType type) // NOLINTNEXTLINE(google-default-arguments) ir::AstNode *ETSParser::ParseClassElement([[maybe_unused]] const ArenaVector &properties, [[maybe_unused]] ir::ClassDefinitionModifiers modifiers, - [[maybe_unused]] ir::ModifierFlags flags) + [[maybe_unused]] ir::ModifierFlags flags, + [[maybe_unused]] ir::Identifier *ident_node) { auto start_loc = Lexer()->GetToken().Start(); auto saved_pos = Lexer()->Save(); // NOLINT(clang-analyzer-deadcode.DeadStores) @@ -1580,7 +1581,7 @@ ir::AstNode *ETSParser::ParseClassElement([[maybe_unused]] const ArenaVectorGetToken().Type() == lexer::TokenType::PUNCTUATOR_LEFT_PARENTHESIS || Lexer()->GetToken().Type() == lexer::TokenType::PUNCTUATOR_LESS_THAN) { - auto *class_method = ParseClassMethodDefinition(member_name, memberModifiers); + auto *class_method = ParseClassMethodDefinition(member_name, memberModifiers, ident_node); class_method->SetStart(start_loc); return class_method; } @@ -1909,7 +1910,7 @@ ir::ClassDefinition *ETSParser::ParseClassDefinition(ir::ClassDefinitionModifier ExpectToken(lexer::TokenType::PUNCTUATOR_LEFT_BRACE, false); // Parse ClassBody - auto [ctor, properties, bodyRange] = ParseClassBody(modifiers, flags); + auto [ctor, properties, bodyRange] = ParseClassBody(modifiers, flags, ident_node); CreateCCtor(class_ctx.GetScope()->StaticMethodScope(), properties, bodyRange.start); auto *class_scope = class_ctx.GetScope(); @@ -2120,7 +2121,7 @@ void ETSParser::CreateClassFunctionDeclaration(ir::MethodDefinition *method) method->Function()->AddFlag(ir::ScriptFunctionFlags::OVERLOAD); } -void ETSParser::AddProxyOverloadToMethodWithDefaultParams(ir::MethodDefinition *method) +void ETSParser::AddProxyOverloadToMethodWithDefaultParams(ir::MethodDefinition *method, ir::Identifier *ident_node) { if (method->IsConstructor()) { return; // TODO(szd): Fix constructors not working with default params @@ -2188,7 +2189,15 @@ void ETSParser::AddProxyOverloadToMethodWithDefaultParams(ir::MethodDefinition * } } - proxy_method += (return_type == "void") ? "" : cls_scope->Parent()->IsGlobalScope() ? "return " : "return this."; + if (return_type != "void") { + if (cls_scope->Parent()->IsGlobalScope()) { + proxy_method += "return "; + } else if (method->IsStatic()) { + proxy_method += "return " + ident_node->Name().Mutf8() + "."; + } else { + proxy_method += "return this."; + } + } proxy_method += function->Id()->Name().Mutf8() + "("; for (const auto *const it : function->Params()) { diff --git a/parser/ETSparser.h b/parser/ETSparser.h index 9d388f6b9..584bd148c 100644 --- a/parser/ETSparser.h +++ b/parser/ETSparser.h @@ -85,7 +85,7 @@ private: std::tuple ParseClassMemberAccessModifiers(); ir::ModifierFlags ParseClassFieldModifiers(bool seen_static); ir::ModifierFlags ParseClassMethodModifiers(bool seen_static); - ir::MethodDefinition *ParseClassMethodDefinition(ir::Identifier *method_name, ir::ModifierFlags modifiers); + ir::MethodDefinition *ParseClassMethodDefinition(ir::Identifier *method_name, ir::ModifierFlags modifiers, ir::Identifier *ident_node = nullptr); ir::ScriptFunction *ParseFunction(ParserStatus new_status); ir::MethodDefinition *ParseClassMethod(ClassElementDescriptor *desc, const ArenaVector &properties, ir::Expression *prop_name, lexer::SourcePosition *prop_end) override; @@ -108,7 +108,7 @@ private: ir::TypeNode *ParseWildcardType(TypeAnnotationParsingOptions *options); ir::TypeNode *ParseFunctionType(); void CreateClassFunctionDeclaration(ir::MethodDefinition *method); - void AddProxyOverloadToMethodWithDefaultParams(ir::MethodDefinition *method); + void AddProxyOverloadToMethodWithDefaultParams(ir::MethodDefinition *method, ir::Identifier *ident_node = nullptr); std::string GetNameForTypeNode(const ir::TypeNode *type_annotation); ir::TSInterfaceDeclaration *ParseInterfaceBody(ir::Identifier *name, bool is_static); bool IsArrowFunctionExpressionStart(); @@ -159,7 +159,7 @@ private: ir::ModifierFlags mod_flags = ir::ModifierFlags::NONE) override; // NOLINTNEXTLINE(google-default-arguments) ir::AstNode *ParseClassElement(const ArenaVector &properties, ir::ClassDefinitionModifiers modifiers, - ir::ModifierFlags flags = ir::ModifierFlags::NONE) override; + ir::ModifierFlags flags = ir::ModifierFlags::NONE, ir::Identifier *ident_node = nullptr) override; ir::Expression *ParseNewExpression() override; ir::Expression *ParseAsyncExpression(); ir::Expression *ParseAwaitExpression(); diff --git a/parser/TypedParser.cpp b/parser/TypedParser.cpp index ff4d43fbf..54087f050 100644 --- a/parser/TypedParser.cpp +++ b/parser/TypedParser.cpp @@ -1007,7 +1007,7 @@ ir::ClassDefinition *TypedParser::ParseClassDefinition(ir::ClassDefinitionModifi // NOLINTNEXTLINE(google-default-arguments) ir::AstNode *TypedParser::ParseClassElement(const ArenaVector &properties, - ir::ClassDefinitionModifiers modifiers, ir::ModifierFlags flags) + ir::ClassDefinitionModifiers modifiers, ir::ModifierFlags flags, [[maybe_unused]] ir::Identifier *ident_node) { if (Lexer()->GetToken().KeywordType() == lexer::TokenType::KEYW_STATIC && Lexer()->Lookahead() == lexer::LEX_CHAR_LEFT_BRACE) { diff --git a/parser/TypedParser.h b/parser/TypedParser.h index 3a81bfbdc..eb5f4d297 100644 --- a/parser/TypedParser.h +++ b/parser/TypedParser.h @@ -62,7 +62,7 @@ protected: ir::ModifierFlags flags = ir::ModifierFlags::NONE) override; // NOLINTNEXTLINE(google-default-arguments) ir::AstNode *ParseClassElement(const ArenaVector &properties, ir::ClassDefinitionModifiers modifiers, - ir::ModifierFlags flags = ir::ModifierFlags::NONE) override; + ir::ModifierFlags flags = ir::ModifierFlags::NONE, ir::Identifier *ident_node = nullptr) override; static bool CheckClassElementInterfaceBody(ir::AstNode *property, ArenaVector &properties); bool CheckClassElement(ir::AstNode *property, ir::MethodDefinition *&ctor, diff --git a/parser/parserImpl.cpp b/parser/parserImpl.cpp index ef8688279..c89925d59 100644 --- a/parser/parserImpl.cpp +++ b/parser/parserImpl.cpp @@ -599,7 +599,8 @@ ir::ClassElement *ParserImpl::ParseClassStaticBlock() // NOLINTNEXTLINE(google-default-arguments) ir::AstNode *ParserImpl::ParseClassElement(const ArenaVector &properties, [[maybe_unused]] ir::ClassDefinitionModifiers modifiers, - [[maybe_unused]] ir::ModifierFlags flags) + [[maybe_unused]] ir::ModifierFlags flags, + [[maybe_unused]] ir::Identifier *ident_node) { if (lexer_->GetToken().KeywordType() == lexer::TokenType::KEYW_STATIC && lexer_->Lookahead() == lexer::LEX_CHAR_LEFT_BRACE) { @@ -814,7 +815,7 @@ ir::ClassDefinition *ParserImpl::ParseClassDefinition(ir::ClassDefinitionModifie return class_definition; } -ParserImpl::ClassBody ParserImpl::ParseClassBody(ir::ClassDefinitionModifiers modifiers, ir::ModifierFlags flags) +ParserImpl::ClassBody ParserImpl::ParseClassBody(ir::ClassDefinitionModifiers modifiers, ir::ModifierFlags flags, ir::Identifier *ident_node) { auto saved_ctx = SavedStatusContext(&context_); @@ -832,7 +833,7 @@ ParserImpl::ClassBody ParserImpl::ParseClassBody(ir::ClassDefinitionModifiers mo continue; } - ir::AstNode *property = ParseClassElement(properties, modifiers, flags); + ir::AstNode *property = ParseClassElement(properties, modifiers, flags, ident_node); if (CheckClassElement(property, ctor, properties)) { continue; diff --git a/parser/parserImpl.h b/parser/parserImpl.h index e680020b4..1d2eda307 100644 --- a/parser/parserImpl.h +++ b/parser/parserImpl.h @@ -436,7 +436,8 @@ protected: // NOLINTNEXTLINE(google-default-arguments) virtual ir::AstNode *ParseClassElement(const ArenaVector &properties, ir::ClassDefinitionModifiers modifiers, - ir::ModifierFlags flags = ir::ModifierFlags::NONE); + ir::ModifierFlags flags = ir::ModifierFlags::NONE, + ir::Identifier *ident_node = nullptr); virtual bool CheckClassElement(ir::AstNode *property, ir::MethodDefinition *&ctor, ArenaVector &properties); virtual void ValidateClassMethodStart(ClassElementDescriptor *desc, ir::TypeNode *type_annotation); @@ -534,7 +535,7 @@ protected: virtual ir::Expression *ParseSuperClassReference(); using ClassBody = std::tuple, lexer::SourceRange>; - ClassBody ParseClassBody(ir::ClassDefinitionModifiers modifiers, ir::ModifierFlags flags = ir::ModifierFlags::NONE); + ClassBody ParseClassBody(ir::ClassDefinitionModifiers modifiers, ir::ModifierFlags flags = ir::ModifierFlags::NONE, ir::Identifier *ident_node = nullptr); virtual binder::Decl *BindClassName(ir::Identifier *ident_node); -- Gitee