From 98f04c4601c734fb2c038d3e39a2915b033091d6 Mon Sep 17 00:00:00 2001 From: Vadim Nikolaev Date: Mon, 5 Aug 2024 18:41:18 +0300 Subject: [PATCH] [ArkTS][Frontend] Added CTE for interface setter Added compile-time error for interface setter which has more than one parameter Set pointer to the end of setter parameter declaration block Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/IAVTFT Testing: runner --parse tests Change-Id: Id79dad79efa961d2b7324b2533b3f71d1175d0c2 Signed-off-by: Vadim Nikolaev --- ets2panda/parser/ASparser.cpp | 4 +--- ets2panda/parser/ETSparserClasses.cpp | 5 ++++- ets2panda/parser/TSparser.cpp | 8 ++----- ets2panda/parser/parserImpl.cpp | 21 +++++++++++++------ ets2panda/parser/parserImpl.h | 1 + ...er_with_more_than_one_formal_parameter.sts | 20 ++++++++++++++++++ 6 files changed, 43 insertions(+), 16 deletions(-) create mode 100644 ets2panda/test/ast/parser/ets/setter_with_more_than_one_formal_parameter.sts diff --git a/ets2panda/parser/ASparser.cpp b/ets2panda/parser/ASparser.cpp index bed6e27bc7..bae2886608 100644 --- a/ets2panda/parser/ASparser.cpp +++ b/ets2panda/parser/ASparser.cpp @@ -1203,9 +1203,7 @@ void ASParser::ValidateClassMethodStart(ClassElementDescriptor *desc, ir::TypeNo void ASParser::ValidateClassSetter(ClassElementDescriptor *desc, const ArenaVector &properties, ir::Expression *propName, ir::ScriptFunction *func) { - if (func->Params().size() != 1) { - ThrowSyntaxError("Setter must have exactly one formal parameter"); - } + ValidateGetterSetter(ir::MethodDefinitionKind::SET, func->Params().size()); if ((desc->modifiers & ir::ModifierFlags::STATIC) == 0) { ir::ModifierFlags access = GetAccessability(desc->modifiers); diff --git a/ets2panda/parser/ETSparserClasses.cpp b/ets2panda/parser/ETSparserClasses.cpp index de66723267..d08cc7b59a 100644 --- a/ets2panda/parser/ETSparserClasses.cpp +++ b/ets2panda/parser/ETSparserClasses.cpp @@ -896,10 +896,13 @@ ir::MethodDefinition *ETSParser::ParseInterfaceMethod(ir::ModifierFlags flags, i if ((flags & ir::ModifierFlags::STATIC) == 0 && body == nullptr) { func->AddModifier(ir::ModifierFlags::ABSTRACT); } + + ValidateGetterSetter(methodKind, func->Params().size()); + func->SetRange({startLoc, body != nullptr ? body->End() : func->ReturnTypeAnnotation() != nullptr ? func->ReturnTypeAnnotation()->End() : func->Params().empty() ? Lexer()->GetToken().End() - : (*func->Params().end())->End()}); + : func->Params().back()->End()}); auto *funcExpr = AllocNode(func); funcExpr->SetRange(func->Range()); diff --git a/ets2panda/parser/TSparser.cpp b/ets2panda/parser/TSparser.cpp index 0173a37252..50a9846059 100644 --- a/ets2panda/parser/TSparser.cpp +++ b/ets2panda/parser/TSparser.cpp @@ -1931,9 +1931,7 @@ ir::MethodDefinition *TSParser::ParseClassMethod(ClassElementDescriptor *desc, void TSParser::ValidateClassSetter(ClassElementDescriptor *desc, const ArenaVector &properties, ir::Expression *propName, ir::ScriptFunction *func) { - if (func->Params().size() != 1) { - ThrowSyntaxError("Setter must have exactly one formal parameter"); - } + ValidateGetterSetter(ir::MethodDefinitionKind::SET, func->Params().size()); if ((desc->modifiers & ir::ModifierFlags::STATIC) == 0) { ir::ModifierFlags access = GetAccessability(desc->modifiers); @@ -1944,9 +1942,7 @@ void TSParser::ValidateClassSetter(ClassElementDescriptor *desc, const ArenaVect void TSParser::ValidateClassGetter(ClassElementDescriptor *desc, const ArenaVector &properties, ir::Expression *propName, ir::ScriptFunction *func) { - if (!func->Params().empty()) { - ThrowSyntaxError("Getter must not have formal parameters"); - } + ValidateGetterSetter(ir::MethodDefinitionKind::GET, func->Params().size()); if ((desc->modifiers & ir::ModifierFlags::STATIC) == 0) { ir::ModifierFlags access = GetAccessability(desc->modifiers); diff --git a/ets2panda/parser/parserImpl.cpp b/ets2panda/parser/parserImpl.cpp index 9547a67a47..5efec2378b 100644 --- a/ets2panda/parser/parserImpl.cpp +++ b/ets2panda/parser/parserImpl.cpp @@ -415,22 +415,31 @@ void ParserImpl::ValidateClassMethodStart(ClassElementDescriptor *desc, [[maybe_ } } +void ParserImpl::ValidateGetterSetter(ir::MethodDefinitionKind methodDefinition, size_t number) const +{ + if (methodDefinition == ir::MethodDefinitionKind::SET) { + if (number != 1) { + ThrowSyntaxError("Setter must have exactly one formal parameter"); + } + } else if (methodDefinition == ir::MethodDefinitionKind::GET) { + if (number != 0) { + ThrowSyntaxError("Getter must not have formal parameters"); + } + } +} + void ParserImpl::ValidateClassSetter([[maybe_unused]] ClassElementDescriptor *desc, [[maybe_unused]] const ArenaVector &properties, [[maybe_unused]] ir::Expression *propName, ir::ScriptFunction *func) { - if (func->Params().size() != 1) { - ThrowSyntaxError("Setter must have exactly one formal parameter"); - } + ValidateGetterSetter(ir::MethodDefinitionKind::SET, func->Params().size()); } void ParserImpl::ValidateClassGetter([[maybe_unused]] ClassElementDescriptor *desc, [[maybe_unused]] const ArenaVector &properties, [[maybe_unused]] ir::Expression *propName, ir::ScriptFunction *func) { - if (!func->Params().empty()) { - ThrowSyntaxError("Getter must not have formal parameters"); - } + ValidateGetterSetter(ir::MethodDefinitionKind::GET, func->Params().size()); } ir::MethodDefinition *ParserImpl::ParseClassMethod(ClassElementDescriptor *desc, diff --git a/ets2panda/parser/parserImpl.h b/ets2panda/parser/parserImpl.h index 7319916a62..9cb359fd52 100644 --- a/ets2panda/parser/parserImpl.h +++ b/ets2panda/parser/parserImpl.h @@ -362,6 +362,7 @@ protected: virtual ir::MethodDefinition *ParseClassMethod(ClassElementDescriptor *desc, const ArenaVector &properties, ir::Expression *propName, lexer::SourcePosition *propEnd); + void ValidateGetterSetter(ir::MethodDefinitionKind methodDefinition, size_t number) const; virtual void ValidateClassSetter(ClassElementDescriptor *desc, const ArenaVector &properties, ir::Expression *propName, ir::ScriptFunction *func); virtual void ValidateClassGetter(ClassElementDescriptor *desc, const ArenaVector &properties, diff --git a/ets2panda/test/ast/parser/ets/setter_with_more_than_one_formal_parameter.sts b/ets2panda/test/ast/parser/ets/setter_with_more_than_one_formal_parameter.sts new file mode 100644 index 0000000000..edaa36452e --- /dev/null +++ b/ets2panda/test/ast/parser/ets/setter_with_more_than_one_formal_parameter.sts @@ -0,0 +1,20 @@ +/* + * 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 Set { + set value(a: int, b: int, c : int); + } + + /* @@? 17:39 Error SyntaxError: Setter must have exactly one formal parameter */ -- Gitee