| 1583 | } |
| 1584 | |
| 1585 | bool GDScriptParser::parse_function_signature(FunctionNode *p_function, SuiteNode *p_body, const String &p_type, int p_signature_start) { |
| 1586 | if (!check(GDScriptTokenizer::Token::PARENTHESIS_CLOSE) && !is_at_end()) { |
| 1587 | bool default_used = false; |
| 1588 | do { |
| 1589 | if (check(GDScriptTokenizer::Token::PARENTHESIS_CLOSE)) { |
| 1590 | // Allow for trailing comma. |
| 1591 | break; |
| 1592 | } |
| 1593 | |
| 1594 | bool is_rest = false; |
| 1595 | if (match(GDScriptTokenizer::Token::PERIOD_PERIOD_PERIOD)) { |
| 1596 | is_rest = true; |
| 1597 | } |
| 1598 | |
| 1599 | ParameterNode *parameter = parse_parameter(); |
| 1600 | if (parameter == nullptr) { |
| 1601 | break; |
| 1602 | } |
| 1603 | |
| 1604 | if (p_function->is_vararg()) { |
| 1605 | push_error("Cannot have parameters after the rest parameter."); |
| 1606 | continue; |
| 1607 | } |
| 1608 | |
| 1609 | if (parameter->initializer != nullptr) { |
| 1610 | if (is_rest) { |
| 1611 | push_error("The rest parameter cannot have a default value."); |
| 1612 | continue; |
| 1613 | } |
| 1614 | default_used = true; |
| 1615 | } else { |
| 1616 | if (default_used && !is_rest) { |
| 1617 | push_error("Cannot have mandatory parameters after optional parameters."); |
| 1618 | continue; |
| 1619 | } |
| 1620 | } |
| 1621 | |
| 1622 | if (p_function->parameters_indices.has(parameter->identifier->name)) { |
| 1623 | push_error(vformat(R"(Parameter with name "%s" was already declared for this %s.)", parameter->identifier->name, p_type)); |
| 1624 | } else if (is_rest) { |
| 1625 | p_function->rest_parameter = parameter; |
| 1626 | p_body->add_local(parameter, current_function); |
| 1627 | } else { |
| 1628 | p_function->parameters_indices[parameter->identifier->name] = p_function->parameters.size(); |
| 1629 | p_function->parameters.push_back(parameter); |
| 1630 | p_body->add_local(parameter, current_function); |
| 1631 | } |
| 1632 | } while (match(GDScriptTokenizer::Token::COMMA)); |
| 1633 | } |
| 1634 | |
| 1635 | pop_multiline(); |
| 1636 | consume(GDScriptTokenizer::Token::PARENTHESIS_CLOSE, vformat(R"*(Expected closing ")" after %s parameters.)*", p_type)); |
| 1637 | |
| 1638 | if (match(GDScriptTokenizer::Token::FORWARD_ARROW)) { |
| 1639 | make_completion_context(COMPLETION_TYPE_NAME_OR_VOID, p_function); |
| 1640 | p_function->return_type = parse_type(true); |
| 1641 | if (p_function->return_type == nullptr) { |
| 1642 | push_error(R"(Expected return type or "void" after "->".)"); |
nothing calls this directly
no test coverage detected