// If we get here it means that a single nested function refers to
// two different variables defined in enclosing functions, and both
// variables have the same name. I think this is impossible.
- gcc_unreachable();
+ go_unreachable();
}
// Class Parse.
bool ok = true;
if (named_object == NULL)
{
- if (package != NULL)
- ok = false;
- else
+ if (package == NULL)
named_object = this->gogo_->add_unknown_name(name, location);
+ else
+ {
+ const std::string& packname(package->package_value()->name());
+ error_at(location, "reference to undefined identifier %<%s.%s%>",
+ Gogo::message_name(packname).c_str(),
+ Gogo::message_name(name).c_str());
+ issue_error = false;
+ ok = false;
+ }
}
else if (named_object->is_type())
{
else if (named_object->is_unknown() || named_object->is_type_declaration())
return Type::make_forward_declaration(named_object);
else
- gcc_unreachable();
+ go_unreachable();
}
// ArrayType = "[" [ ArrayLength ] "]" ElementType .
return Type::make_channel_type(send, receive, element_type);
}
+// Give an error for a duplicate parameter or receiver name.
+
+void
+Parse::check_signature_names(const Typed_identifier_list* params,
+ Parse::Names* names)
+{
+ for (Typed_identifier_list::const_iterator p = params->begin();
+ p != params->end();
+ ++p)
+ {
+ if (p->name().empty() || Gogo::is_sink_name(p->name()))
+ continue;
+ std::pair<std::string, const Typed_identifier*> val =
+ std::make_pair(p->name(), &*p);
+ std::pair<Parse::Names::iterator, bool> ins = names->insert(val);
+ if (!ins.second)
+ {
+ error_at(p->location(), "redefinition of %qs",
+ Gogo::message_name(p->name()).c_str());
+ inform(ins.first->second->location(),
+ "previous definition of %qs was here",
+ Gogo::message_name(p->name()).c_str());
+ }
+ }
+}
+
// Signature = Parameters [ Result ] .
// RECEIVER is the receiver if there is one, or NULL. LOCATION is the
Typed_identifier_list* params;
bool params_ok = this->parameters(¶ms, &is_varargs);
- Typed_identifier_list* result = NULL;
+ Typed_identifier_list* results = NULL;
if (this->peek_token()->is_op(OPERATOR_LPAREN)
|| this->type_may_start_here())
{
- if (!this->result(&result))
+ if (!this->result(&results))
return NULL;
}
if (!params_ok)
return NULL;
- Function_type* ret = Type::make_function_type(receiver, params, result,
+ Parse::Names names;
+ if (params != NULL)
+ this->check_signature_names(params, &names);
+ if (results != NULL)
+ this->check_signature_names(results, &names);
+
+ Function_type* ret = Type::make_function_type(receiver, params, results,
location);
if (is_varargs)
ret->set_is_varargs();
if (!type_from_init && init != NULL)
{
if (!this->gogo_->in_global_scope())
- this->gogo_->add_statement(Statement::make_statement(init));
+ this->gogo_->add_statement(Statement::make_statement(init, true));
else
return this->create_dummy_global(type, init, location);
}
case Named_object::NAMED_OBJECT_UNKNOWN:
return Expression::make_unknown_reference(named_object, location);
default:
- gcc_unreachable();
+ go_unreachable();
}
}
- gcc_unreachable();
+ go_unreachable();
case Token::TOKEN_STRING:
ret = Expression::make_string(token->string_value(), token->location());
// LiteralValue = "{" [ ElementList [ "," ] ] "}" .
// ElementList = Element { "," Element } .
// Element = [ Key ":" ] Value .
-// Key = Expression .
+// Key = FieldName | ElementIndex .
+// FieldName = identifier .
+// ElementIndex = Expression .
// Value = Expression | LiteralValue .
// We have already seen the type if there is one, and we are now
const Token* token = this->peek_token();
- if (!token->is_op(OPERATOR_LCURLY))
+ if (token->is_identifier())
+ {
+ std::string identifier = token->identifier();
+ bool is_exported = token->is_identifier_exported();
+ source_location location = token->location();
+
+ if (this->advance_token()->is_op(OPERATOR_COLON))
+ {
+ // This may be a field name. We don't know for sure--it
+ // could also be an expression for an array index. We
+ // don't want to parse it as an expression because may
+ // trigger various errors, e.g., if this identifier
+ // happens to be the name of a package.
+ Gogo* gogo = this->gogo_;
+ val = this->id_to_expression(gogo->pack_hidden_name(identifier,
+ is_exported),
+ location);
+ }
+ else
+ {
+ this->unget_token(Token::make_identifier_token(identifier,
+ is_exported,
+ location));
+ val = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
+ }
+ }
+ else if (!token->is_op(OPERATOR_LCURLY))
val = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
else
{
else
this->advance_token();
if (expr->is_error_expression())
- return expr;
- ret = Expression::make_cast(ret->type(), expr, loc);
+ ret = expr;
+ else
+ {
+ Type* t = ret->type();
+ if (t->classification() == Type::TYPE_ARRAY
+ && t->array_type()->length() != NULL
+ && t->array_type()->length()->is_nil_expression())
+ {
+ error_at(ret->location(),
+ "invalid use of %<...%> in type conversion");
+ ret = Expression::make_error(loc);
+ }
+ else
+ ret = Expression::make_cast(t, expr, loc);
+ }
}
}
return Expression::make_func_reference(named_object, NULL, location);
case Named_object::NAMED_OBJECT_UNKNOWN:
return Expression::make_unknown_reference(named_object, location);
+ case Named_object::NAMED_OBJECT_PACKAGE:
+ case Named_object::NAMED_OBJECT_TYPE:
+ case Named_object::NAMED_OBJECT_TYPE_DECLARATION:
+ // These cases can arise for a field name in a composite
+ // literal.
+ return Expression::make_unknown_reference(named_object, location);
default:
error_at(this->location(), "unexpected type of identifier");
return Expression::make_error(location);
case Token::TOKEN_IMAGINARY:
return true;
default:
- gcc_unreachable();
+ go_unreachable();
}
}
void
Parse::expression_stat(Expression* exp)
{
- exp->discarding_value();
- this->gogo_->add_statement(Statement::make_statement(exp));
+ this->gogo_->add_statement(Statement::make_statement(exp, false));
}
// SendStmt = Channel "<-" Expression .
else if (token->is_op(OPERATOR_MINUSMINUS))
this->gogo_->add_statement(Statement::make_dec_statement(exp));
else
- gcc_unreachable();
+ go_unreachable();
this->advance_token();
}
this->gogo_->add_statement(Statement::make_return_statement(vals, location));
}
-// IfStmt = "if" [ SimpleStmt ";" ] Expression Block [ "else" Statement ] .
+// IfStmt = "if" [ SimpleStmt ";" ] Expression Block
+// [ "else" ( IfStmt | Block ) ] .
void
Parse::if_stat()
Block* else_block = NULL;
if (this->peek_token()->is_keyword(KEYWORD_ELSE))
{
- this->advance_token();
- // We create a block to gather the statement.
this->gogo_->start_block(this->location());
- this->statement(NULL);
+ const Token* token = this->advance_token();
+ if (token->is_keyword(KEYWORD_IF))
+ this->if_stat();
+ else if (token->is_op(OPERATOR_LCURLY))
+ this->block();
+ else
+ {
+ error_at(this->location(), "expected %<if%> or %<{%>");
+ this->statement(NULL);
+ }
else_block = this->gogo_->finish_block(this->location());
}
{
// If there is a label with this name, mark it as used to
// avoid a useless error about an unused label.
- this->gogo_->add_label_reference(token->identifier());
+ this->gogo_->add_label_reference(token->identifier(), 0, false);
error_at(token->location(), "invalid break label %qs",
Gogo::message_name(token->identifier()).c_str());
else if (enclosing->classification() == Statement::STATEMENT_SELECT)
label = enclosing->select_statement()->break_label();
else
- gcc_unreachable();
+ go_unreachable();
this->gogo_->add_statement(Statement::make_break_statement(label,
location));
{
// If there is a label with this name, mark it as used to
// avoid a useless error about an unused label.
- this->gogo_->add_label_reference(token->identifier());
+ this->gogo_->add_label_reference(token->identifier(), 0, false);
error_at(token->location(), "invalid continue label %qs",
Gogo::message_name(token->identifier()).c_str());
else if (enclosing->classification() == Statement::STATEMENT_FOR_RANGE)
label = enclosing->for_range_statement()->continue_label();
else
- gcc_unreachable();
+ go_unreachable();
this->gogo_->add_statement(Statement::make_continue_statement(label,
location));
error_at(this->location(), "expected label for goto");
else
{
- Label* label = this->gogo_->add_label_reference(token->identifier());
+ Label* label = this->gogo_->add_label_reference(token->identifier(),
+ location, true);
Statement* s = Statement::make_goto_statement(label, location);
this->gogo_->add_statement(s);
this->advance_token();