diff --git a/ets2panda/parser/ASparser.cpp b/ets2panda/parser/ASparser.cpp index bed6e27bc7555604942c3b50b100cb51becb21a4..bae2886608409ec6daa0a585a55df76a17c7072a 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 de667232677dc558f48c698e5eb82c4fe2c1aaa8..d08cc7b59a2fc6446a464696555c6f3a6922a54d 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 0173a3725255616a40a9d257196fca94054c65ed..50a9846059e9074d16d7703e332daf354ab169a5 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 9547a67a47ca8459a23bb84c98dbb35d65431a0c..5efec2378bede7455285991c1b1aae116c3fd789 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 7319916a624883c266493c4ba90340cd17645cd5..9cb359fd527b67490c800f0df972dcb246a2cce9 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 0000000000000000000000000000000000000000..edaa36452e13f736eb1bcf4ef1f3198738583db2 --- /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 */