token_(Token::make_invalid_token(Linemap::unknown_location())),
unget_token_(Token::make_invalid_token(Linemap::unknown_location())),
unget_token_valid_(false),
+ is_erroneous_function_(false),
gogo_(gogo),
break_stack_(NULL),
continue_stack_(NULL),
iota_(0),
- enclosing_vars_()
+ enclosing_vars_(),
+ type_switch_vars_()
{
}
else
{
error_at(this->location(), "expected field name");
+ this->gogo_->mark_locals_used();
while (!token->is_op(OPERATOR_SEMICOLON)
&& !token->is_op(OPERATOR_RCURLY)
&& !token->is_eof())
if (!this->peek_token()->is_identifier())
{
error_at(this->location(), "expected field name");
+ this->gogo_->mark_locals_used();
while (!token->is_op(OPERATOR_SEMICOLON)
&& !token->is_op(OPERATOR_RCURLY)
&& !token->is_eof())
if (!token->is_eof() || !saw_errors())
error_at(this->location(), "expected %<}%>");
+ this->gogo_->mark_locals_used();
+
// Skip ahead to the end of the block, in hopes of avoiding
// lots of meaningless errors.
Location ret = token->location();
"name list not allowed in interface type");
else
error_at(location, "expected signature or type name");
+ this->gogo_->mark_locals_used();
token = this->peek_token();
while (!token->is_eof()
&& !token->is_op(OPERATOR_SEMICOLON)
if (type->is_error_type())
{
+ this->gogo_->mark_locals_used();
while (!this->peek_token()->is_op(OPERATOR_SEMICOLON)
&& !this->peek_token()->is_eof())
this->advance_token();
type = this->type();
if (type->is_error_type())
{
+ this->gogo_->mark_locals_used();
while (!this->peek_token()->is_op(OPERATOR_EQ)
&& !this->peek_token()->is_op(OPERATOR_SEMICOLON)
&& !this->peek_token()->is_eof())
// the right number of values, but it might. Declare the variables,
// and then assign the results of the call to them.
+ Named_object* first_var = NULL;
unsigned int index = 0;
bool any_new = false;
for (Typed_identifier_list::const_iterator pv = vars->begin();
++pv, ++index)
{
Expression* init = Expression::make_call_result(call, index);
- this->init_var(*pv, type, init, is_coloneq, false, &any_new);
+ Named_object* no = this->init_var(*pv, type, init, is_coloneq, false,
+ &any_new);
+
+ if (this->gogo_->in_global_scope() && no->is_variable())
+ {
+ if (first_var == NULL)
+ first_var = no;
+ else
+ {
+ // The subsequent vars have an implicit dependency on
+ // the first one, so that everything gets initialized in
+ // the right order and so that we detect cycles
+ // correctly.
+ this->gogo_->record_var_depends_on(no->var_value(), first_var);
+ }
+ }
}
if (is_coloneq && !any_new)
// initializer can be assigned to the type.
Variable* var = new Variable(type, init, false, false, false,
location);
+ var->set_is_used();
static int count;
char buf[30];
snprintf(buf, sizeof buf, "sink$%d", count);
return this->gogo_->add_variable(buf, var);
}
}
+ if (type != NULL)
+ this->gogo_->add_type_to_verify(type);
return this->gogo_->add_sink();
}
// FunctionDecl = "func" identifier Signature [ Block ] .
// MethodDecl = "func" Receiver identifier Signature [ Block ] .
-// gcc extension:
+// Deprecated gcc extension:
// FunctionDecl = "func" identifier Signature
// __asm__ "(" string_lit ")" .
// This extension means a function whose real name is the identifier
-// inside the asm.
+// inside the asm. This extension will be removed at some future
+// date. It has been replaced with //extern comments.
void
Parse::function_decl()
{
go_assert(this->peek_token()->is_keyword(KEYWORD_FUNC));
Location location = this->location();
+ std::string extern_name = this->lex_->extern_name();
const Token* token = this->advance_token();
Typed_identifier* rec = NULL;
this->advance_token();
Function_type* fntype = this->signature(rec, this->location());
- if (fntype == NULL)
- return;
Named_object* named_object = NULL;
if (!this->peek_token()->is_op(OPERATOR_LCURLY))
{
if (named_object == NULL && !Gogo::is_sink_name(name))
- this->gogo_->declare_function(name, fntype, location);
+ {
+ if (fntype == NULL)
+ this->gogo_->add_erroneous_name(name);
+ else
+ {
+ named_object = this->gogo_->declare_function(name, fntype,
+ location);
+ if (!extern_name.empty()
+ && named_object->is_function_declaration())
+ {
+ Function_declaration* fd =
+ named_object->func_declaration_value();
+ fd->set_asm_name(extern_name);
+ }
+ }
+ }
}
else
{
+ bool hold_is_erroneous_function = this->is_erroneous_function_;
+ if (fntype == NULL)
+ {
+ fntype = Type::make_function_type(NULL, NULL, NULL, location);
+ this->is_erroneous_function_ = true;
+ if (!Gogo::is_sink_name(name))
+ this->gogo_->add_erroneous_name(name);
+ name = this->gogo_->pack_hidden_name("_", false);
+ }
this->gogo_->start_function(name, fntype, true, location);
Location end_loc = this->block();
this->gogo_->finish_function(end_loc);
+ this->is_erroneous_function_ = hold_is_erroneous_function;
}
}
if (!token->is_identifier())
{
error_at(this->location(), "method has no receiver");
+ this->gogo_->mark_locals_used();
while (!token->is_eof() && !token->is_op(OPERATOR_RPAREN))
token = this->advance_token();
if (!token->is_eof())
if (!token->is_identifier())
{
error_at(this->location(), "expected receiver name or type");
+ this->gogo_->mark_locals_used();
int c = token->is_op(OPERATOR_LPAREN) ? 1 : 0;
while (!token->is_eof())
{
error_at(this->location(), "method has multiple receivers");
else
error_at(this->location(), "expected %<)%>");
+ this->gogo_->mark_locals_used();
while (!token->is_eof() && !token->is_op(OPERATOR_RPAREN))
token = this->advance_token();
if (!token->is_eof())
}
case Named_object::NAMED_OBJECT_VAR:
case Named_object::NAMED_OBJECT_RESULT_VAR:
+ this->mark_var_used(named_object);
return Expression::make_var_reference(named_object, location);
case Named_object::NAMED_OBJECT_SINK:
if (may_be_sink)
return Expression::make_func_reference(named_object, NULL,
location);
case Named_object::NAMED_OBJECT_UNKNOWN:
- return Expression::make_unknown_reference(named_object, location);
+ {
+ Unknown_expression* ue =
+ Expression::make_unknown_reference(named_object, location);
+ if (this->is_erroneous_function_)
+ ue->set_no_error_message();
+ return ue;
+ }
+ case Named_object::NAMED_OBJECT_ERRONEOUS:
+ return Expression::make_error(location);
default:
go_unreachable();
}
if (token->is_op(OPERATOR_LPAREN))
{
this->advance_token();
- ret = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
+ ret = this->expression(PRECEDENCE_NORMAL, may_be_sink, true, NULL);
if (!this->peek_token()->is_op(OPERATOR_RPAREN))
error_at(this->location(), "missing %<)%>");
else
{
go_assert(var->is_variable() || var->is_result_variable());
+ this->mark_var_used(var);
+
Named_object* this_function = this->gogo_->current_function();
Named_object* closure = this_function->func_value()->closure_var();
{
error_at(this->location(), "expected %<,%> or %<}%>");
+ this->gogo_->mark_locals_used();
int depth = 0;
while (!token->is_eof()
&& (depth > 0 || !token->is_op(OPERATOR_RCURLY)))
hold_enclosing_vars.swap(this->enclosing_vars_);
Function_type* type = this->signature(NULL, location);
+ bool fntype_is_error = false;
if (type == NULL)
- type = Type::make_function_type(NULL, NULL, NULL, location);
+ {
+ type = Type::make_function_type(NULL, NULL, NULL, location);
+ fntype_is_error = true;
+ }
// For a function literal, the next token must be a '{'. If we
// don't see that, then we may have a type expression.
if (!this->peek_token()->is_op(OPERATOR_LCURLY))
return Expression::make_type(type, location);
+ bool hold_is_erroneous_function = this->is_erroneous_function_;
+ if (fntype_is_error)
+ this->is_erroneous_function_ = true;
+
Bc_stack* hold_break_stack = this->break_stack_;
Bc_stack* hold_continue_stack = this->continue_stack_;
this->break_stack_ = NULL;
this->break_stack_ = hold_break_stack;
this->continue_stack_ = hold_continue_stack;
+ this->is_erroneous_function_ = hold_is_erroneous_function;
+
hold_enclosing_vars.swap(this->enclosing_vars_);
Expression* closure = this->create_closure(no, &hold_enclosing_vars,
{
if (this->peek_token()->is_op(OPERATOR_LCURLY))
{
- if (is_parenthesized)
+ if (!may_be_composite_lit)
+ {
+ Type* t = ret->type();
+ if (t->named_type() != NULL
+ || t->forward_declaration_type() != NULL)
+ error_at(start_loc,
+ _("parentheses required around this composite literal"
+ "to avoid parsing ambiguity"));
+ }
+ else if (is_parenthesized)
error_at(start_loc,
"cannot parenthesize type in composite literal");
ret = this->composite_lit(ret->type(), 0, ret->location());
return Expression::make_const_reference(named_object, location);
case Named_object::NAMED_OBJECT_VAR:
case Named_object::NAMED_OBJECT_RESULT_VAR:
+ this->mark_var_used(named_object);
return Expression::make_var_reference(named_object, location);
case Named_object::NAMED_OBJECT_SINK:
return Expression::make_sink(location);
case Named_object::NAMED_OBJECT_FUNC_DECLARATION:
return Expression::make_func_reference(named_object, NULL, location);
case Named_object::NAMED_OBJECT_UNKNOWN:
- return Expression::make_unknown_reference(named_object, location);
+ {
+ Unknown_expression* ue =
+ Expression::make_unknown_reference(named_object, location);
+ if (this->is_erroneous_function_)
+ ue->set_no_error_message();
+ return ue;
+ }
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);
+ {
+ // These cases can arise for a field name in a composite
+ // literal.
+ Unknown_expression* ue =
+ Expression::make_unknown_reference(named_object, location);
+ if (this->is_erroneous_function_)
+ ue->set_no_error_message();
+ return ue;
+ }
+ case Named_object::NAMED_OBJECT_ERRONEOUS:
+ return Expression::make_error(location);
default:
error_at(this->location(), "unexpected type of identifier");
return Expression::make_error(location);
{
if (!exp->is_error_expression())
error_at(token->location(), "non-name on left side of %<:=%>");
+ this->gogo_->mark_locals_used();
while (!token->is_op(OPERATOR_SEMICOLON)
&& !token->is_eof())
token = this->advance_token();
++p)
{
Named_object* no = this->gogo_->lookup((*p)->name(), NULL);
- go_assert(no != NULL);
- if (!no->is_result_variable())
+ if (no == NULL)
+ go_assert(saw_errors());
+ else if (!no->is_result_variable())
error_at(location, "%qs is shadowed during return",
(*p)->message_name().c_str());
}
bool saw_simple_stat = false;
Expression* cond = NULL;
- bool saw_send_stmt;
+ bool saw_send_stmt = false;
if (this->simple_stat_may_start_here())
{
cond = this->simple_stat(false, &saw_send_stmt, NULL, NULL);
Expression* switch_val = NULL;
bool saw_send_stmt;
Type_switch type_switch;
+ bool have_type_switch_block = false;
if (this->simple_stat_may_start_here())
{
switch_val = this->simple_stat(false, &saw_send_stmt, NULL,
id_loc));
if (is_coloneq)
{
- // This must be a TypeSwitchGuard.
+ // This must be a TypeSwitchGuard. It is in a
+ // different block from any initial SimpleStat.
+ if (saw_simple_stat)
+ {
+ this->gogo_->start_block(id_loc);
+ have_type_switch_block = true;
+ }
+
switch_val = this->simple_stat(false, &saw_send_stmt, NULL,
&type_switch);
if (!type_switch.found)
if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
this->advance_token();
if (!this->peek_token()->is_op(OPERATOR_LCURLY))
- return;
+ {
+ if (have_type_switch_block)
+ this->gogo_->add_block(this->gogo_->finish_block(location),
+ location);
+ this->gogo_->add_block(this->gogo_->finish_block(location),
+ location);
+ return;
+ }
if (type_switch.found)
type_switch.expr = Expression::make_error(location);
}
else
{
error_at(this->location(), "expected %<{%>");
+ if (have_type_switch_block)
+ this->gogo_->add_block(this->gogo_->finish_block(this->location()),
+ location);
this->gogo_->add_block(this->gogo_->finish_block(this->location()),
location);
return;
if (statement != NULL)
this->gogo_->add_statement(statement);
+ if (have_type_switch_block)
+ this->gogo_->add_block(this->gogo_->finish_block(this->location()),
+ location);
+
this->gogo_->add_block(this->gogo_->finish_block(this->location()),
location);
}
Named_object* switch_no = NULL;
if (!type_switch.name.empty())
{
- Variable* switch_var = new Variable(NULL, type_switch.expr, false, false,
- false, type_switch.location);
- switch_no = this->gogo_->add_variable(type_switch.name, switch_var);
+ if (Gogo::is_sink_name(type_switch.name))
+ error_at(type_switch.location,
+ "no new variables on left side of %<:=%>");
+ else
+ {
+ Variable* switch_var = new Variable(NULL, type_switch.expr, false,
+ false, false,
+ type_switch.location);
+ switch_no = this->gogo_->add_variable(type_switch.name, switch_var);
+ }
}
Type_switch_statement* statement =
Variable* v = new Variable(type, init, false, false, false,
location);
v->set_is_type_switch_var();
- this->gogo_->add_variable(switch_no->name(), v);
+ Named_object* no = this->gogo_->add_variable(switch_no->name(), v);
+
+ // We don't want to issue an error if the compiler
+ // introduced special variable is not used. Instead we want
+ // to issue an error if the variable defined by the switch
+ // is not used. That is handled via type_switch_vars_ and
+ // Parse::mark_var_used.
+ v->set_is_used();
+ this->type_switch_vars_[no] = switch_no;
}
this->statement_list();
statements = this->gogo_->finish_block(this->location());
types->push_back(t);
else
{
+ this->gogo_->mark_locals_used();
token = this->peek_token();
while (!token->is_op(OPERATOR_COLON)
&& !token->is_op(OPERATOR_COMMA)
if (token->is_op(OPERATOR_COLONEQ))
{
// case rv := <-c:
- if (!this->advance_token()->is_op(OPERATOR_CHANOP))
+ this->advance_token();
+ Expression* e = this->expression(PRECEDENCE_NORMAL, false, false,
+ NULL);
+ Receive_expression* re = e->receive_expression();
+ if (re == NULL)
{
- error_at(this->location(), "expected %<<-%>");
+ if (!e->is_error_expression())
+ error_at(this->location(), "expected receive expression");
return false;
}
if (recv_var == "_")
}
*is_send = false;
*varname = gogo->pack_hidden_name(recv_var, is_rv_exported);
- this->advance_token();
- *channel = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
+ *channel = re->channel();
return true;
}
else if (token->is_op(OPERATOR_COMMA))
if (token->is_op(OPERATOR_COLONEQ))
{
// case rv, rc := <-c:
- if (!this->advance_token()->is_op(OPERATOR_CHANOP))
+ this->advance_token();
+ Expression* e = this->expression(PRECEDENCE_NORMAL, false,
+ false, NULL);
+ Receive_expression* re = e->receive_expression();
+ if (re == NULL)
{
- error_at(this->location(), "expected %<<-%>");
+ if (!e->is_error_expression())
+ error_at(this->location(),
+ "expected receive expression");
return false;
}
if (recv_var == "_" && recv_closed == "_")
if (recv_closed != "_")
*closedname = gogo->pack_hidden_name(recv_closed,
is_rc_exported);
- this->advance_token();
- *channel = this->expression(PRECEDENCE_NORMAL, false, true,
- NULL);
+ *channel = re->channel();
return true;
}
else
{
error_at(this->location(), "expected declaration");
+ this->gogo_->mark_locals_used();
do
this->advance_token();
while (!this->peek_token()->is_eof()
bool
Parse::skip_past_error(Operator op)
{
+ this->gogo_->mark_locals_used();
const Token* token = this->peek_token();
while (!token->is_op(op))
{
}
return expr;
}
+
+// Mark a variable as used.
+
+void
+Parse::mark_var_used(Named_object* no)
+{
+ if (no->is_variable())
+ {
+ no->var_value()->set_is_used();
+
+ // When a type switch uses := to define a variable, then for
+ // each case with a single type we introduce a new variable with
+ // the appropriate type. When we do, if the newly introduced
+ // variable is used, then the type switch variable is used.
+ Type_switch_vars::iterator p = this->type_switch_vars_.find(no);
+ if (p != this->type_switch_vars_.end())
+ p->second->var_value()->set_is_used();
+ }
+}