Parse::Parse(Lex* lex, Gogo* gogo)
: lex_(lex),
- token_(Token::make_invalid_token(0)),
- unget_token_(Token::make_invalid_token(0)),
+ 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_()
{
}
// The location of the current token.
-source_location
+Location
Parse::location()
{
return this->peek_token()->location();
// ExpressionList = Expression { "," Expression } .
+// If MAY_BE_COMPOSITE_LIT is true, an expression may be a composite
+// literal.
+
// If MAY_BE_SINK is true, the expressions in the list may be "_".
Expression_list*
-Parse::expression_list(Expression* first, bool may_be_sink)
+Parse::expression_list(Expression* first, bool may_be_sink,
+ bool may_be_composite_lit)
{
Expression_list* ret = new Expression_list();
if (first != NULL)
ret->push_back(first);
while (true)
{
- ret->push_back(this->expression(PRECEDENCE_NORMAL, may_be_sink, true,
- NULL));
+ ret->push_back(this->expression(PRECEDENCE_NORMAL, may_be_sink,
+ may_be_composite_lit, NULL));
const Token* token = this->peek_token();
if (!token->is_op(OPERATOR_COMMA))
return ret;
// Most expression lists permit a trailing comma.
- source_location location = token->location();
+ Location location = token->location();
this->advance_token();
if (!this->expression_may_start_here())
{
return this->interface_type();
else if (token->is_keyword(KEYWORD_FUNC))
{
- source_location location = token->location();
+ Location location = token->location();
this->advance_token();
Type* type = this->signature(NULL, location);
if (type == NULL)
Type*
Parse::type_name(bool issue_error)
{
- source_location location = this->location();
+ Location location = this->location();
std::string name;
Named_object* package;
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())
{
Type*
Parse::map_type()
{
- source_location location = this->location();
+ Location location = this->location();
go_assert(this->peek_token()->is_keyword(KEYWORD_MAP));
if (!this->advance_token()->is_op(OPERATOR_LSQUARE))
{
Parse::struct_type()
{
go_assert(this->peek_token()->is_keyword(KEYWORD_STRUCT));
- source_location location = this->location();
+ Location location = this->location();
if (!this->advance_token()->is_op(OPERATOR_LCURLY))
{
- source_location token_loc = this->location();
+ Location token_loc = this->location();
if (this->peek_token()->is_op(OPERATOR_SEMICOLON)
&& this->advance_token()->is_op(OPERATOR_LCURLY))
error_at(token_loc, "unexpected semicolon or newline before %<{%>");
Parse::field_decl(Struct_field_list* sfl)
{
const Token* token = this->peek_token();
- source_location location = token->location();
+ Location location = token->location();
bool is_anonymous;
bool is_anonymous_pointer;
if (token->is_op(OPERATOR_MULT))
{
std::string id = token->identifier();
bool is_id_exported = token->is_identifier_exported();
- source_location id_location = token->location();
+ Location id_location = token->location();
token = this->advance_token();
is_anonymous = (token->is_op(OPERATOR_SEMICOLON)
|| token->is_op(OPERATOR_RCURLY)
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())
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
// This returns NULL on a parse error.
Function_type*
-Parse::signature(Typed_identifier* receiver, source_location location)
+Parse::signature(Typed_identifier* receiver, Location location)
{
bool is_varargs = false;
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();
Typed_identifier_list*
Parse::parameter_list(bool* is_varargs)
{
- source_location location = this->location();
+ Location location = this->location();
Typed_identifier_list* ret = new Typed_identifier_list();
bool saw_error = false;
{
std::string name = token->identifier();
bool is_exported = token->is_identifier_exported();
- source_location location = token->location();
+ Location location = token->location();
token = this->advance_token();
if (!token->is_op(OPERATOR_COMMA))
{
if (!parameters_have_names)
{
Type* type;
- source_location location = this->location();
+ Location location = this->location();
if (!this->peek_token()->is_identifier())
{
if (!this->peek_token()->is_op(OPERATOR_ELLIPSIS))
return this->parameters(presults, NULL);
else
{
- source_location location = this->location();
+ Location location = this->location();
Type* type = this->type();
if (type->is_error_type())
{
// Returns the location of the closing brace.
-source_location
+Location
Parse::block()
{
if (!this->peek_token()->is_op(OPERATOR_LCURLY))
{
- source_location loc = this->location();
+ Location loc = this->location();
if (this->peek_token()->is_op(OPERATOR_SEMICOLON)
&& this->advance_token()->is_op(OPERATOR_LCURLY))
error_at(loc, "unexpected semicolon or newline before %<{%>");
else
{
error_at(this->location(), "expected %<{%>");
- return UNKNOWN_LOCATION;
+ return Linemap::unknown_location();
}
}
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.
- source_location ret = token->location();
+ Location ret = token->location();
int nest = 0;
while (!token->is_eof())
{
}
}
- source_location ret = token->location();
+ Location ret = token->location();
this->advance_token();
return ret;
}
Parse::interface_type()
{
go_assert(this->peek_token()->is_keyword(KEYWORD_INTERFACE));
- source_location location = this->location();
+ Location location = this->location();
if (!this->advance_token()->is_op(OPERATOR_LCURLY))
{
- source_location token_loc = this->location();
+ Location token_loc = this->location();
if (this->peek_token()->is_op(OPERATOR_SEMICOLON)
&& this->advance_token()->is_op(OPERATOR_LCURLY))
error_at(token_loc, "unexpected semicolon or newline before %<{%>");
std::string name = token->identifier();
bool is_exported = token->is_identifier_exported();
- source_location location = token->location();
+ Location location = token->location();
if (this->advance_token()->is_op(OPERATOR_LPAREN))
{
"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)
void
Parse::decl(void (Parse::*pfn)(void*), void* varg)
{
+ if (this->peek_token()->is_eof())
+ {
+ if (!saw_errors())
+ error_at(this->location(), "unexpected end of file");
+ return;
+ }
+
if (!this->peek_token()->is_op(OPERATOR_LPAREN))
(this->*pfn)(varg);
else
else
{
this->advance_token();
- expr_list = this->expression_list(NULL, false);
+ expr_list = this->expression_list(NULL, false, true);
*last_type = type;
if (*last_expr_list != NULL)
delete *last_expr_list;
}
std::string name = token->identifier();
bool is_exported = token->is_identifier_exported();
- source_location location = token->location();
+ Location location = token->location();
token = this->advance_token();
// The scope of the type name starts at the point where the
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();
Typed_identifier_list til;
this->identifier_list(&til);
- source_location location = this->location();
+ Location location = this->location();
Type* type = NULL;
Expression_list* init = NULL;
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())
if (this->peek_token()->is_op(OPERATOR_EQ))
{
this->advance_token();
- init = this->expression_list(NULL, false);
+ init = this->expression_list(NULL, false, true);
}
}
else
{
this->advance_token();
- init = this->expression_list(NULL, false);
+ init = this->expression_list(NULL, false, true);
}
this->init_vars(&til, type, init, false, location);
void
Parse::init_vars(const Typed_identifier_list* til, Type* type,
Expression_list* init, bool is_coloneq,
- source_location location)
+ Location location)
{
// Check for an initialization which can yield multiple values.
if (init != NULL && init->size() == 1 && til->size() > 1)
bool
Parse::init_vars_from_call(const Typed_identifier_list* vars, Type* type,
Expression* expr, bool is_coloneq,
- source_location location)
+ Location location)
{
Call_expression* call = expr->call_expression();
if (call == NULL)
// 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)
bool
Parse::init_vars_from_map(const Typed_identifier_list* vars, Type* type,
Expression* expr, bool is_coloneq,
- source_location location)
+ Location location)
{
Index_expression* index = expr->index_expression();
if (index == NULL)
bool
Parse::init_vars_from_receive(const Typed_identifier_list* vars, Type* type,
Expression* expr, bool is_coloneq,
- source_location location)
+ Location location)
{
Receive_expression* receive = expr->receive_expression();
if (receive == NULL)
Statement* s = Statement::make_tuple_receive_assignment(val_var,
received_var,
receive->channel(),
- false,
location);
if (!this->gogo_->in_global_scope())
bool
Parse::init_vars_from_type_guard(const Typed_identifier_list* vars,
Type* type, Expression* expr,
- bool is_coloneq, source_location location)
+ bool is_coloneq, Location location)
{
Type_guard_expression* type_guard = expr->type_guard_expression();
if (type_guard == NULL)
Parse::init_var(const Typed_identifier& tid, Type* type, Expression* init,
bool is_coloneq, bool type_from_init, bool* is_new)
{
- source_location location = tid.location();
+ Location location = tid.location();
if (Gogo::is_sink_name(tid.name()))
{
if (!type_from_init && init != NULL)
{
- if (!this->gogo_->in_global_scope())
- this->gogo_->add_statement(Statement::make_statement(init));
- else
+ if (this->gogo_->in_global_scope())
return this->create_dummy_global(type, init, location);
+ else if (type == NULL)
+ this->gogo_->add_statement(Statement::make_statement(init, true));
+ else
+ {
+ // With both a type and an initializer, create a dummy
+ // variable so that we will check whether the
+ // 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);
+ ++count;
+ return this->gogo_->add_variable(buf, var);
+ }
}
+ if (type != NULL)
+ this->gogo_->add_type_to_verify(type);
return this->gogo_->add_sink();
}
Named_object*
Parse::create_dummy_global(Type* type, Expression* init,
- source_location location)
+ Location location)
{
if (type == NULL && init == NULL)
type = Type::lookup_bool_type();
// In order to support both "a, b := 1, 0" and "a, b = 1, 0" we accept
// tuple assignments here as well.
+// If MAY_BE_COMPOSITE_LIT is true, the expression on the right hand
+// side may be a composite literal.
+
// If P_RANGE_CLAUSE is not NULL, then this will recognize a
// RangeClause.
void
Parse::simple_var_decl_or_assignment(const std::string& name,
- source_location location,
+ Location location,
+ bool may_be_composite_lit,
Range_clause* p_range_clause,
Type_switch* p_type_switch)
{
std::string id = token->identifier();
bool is_id_exported = token->is_identifier_exported();
- source_location id_location = token->location();
+ Location id_location = token->location();
token = this->advance_token();
if (!token->is_op(OPERATOR_COMMA))
exprs->push_back(this->id_to_expression(p->name(),
p->location()));
- Expression_list* more_exprs = this->expression_list(NULL, true);
+ Expression_list* more_exprs =
+ this->expression_list(NULL, true, may_be_composite_lit);
for (Expression_list::const_iterator p = more_exprs->begin();
p != more_exprs->end();
++p)
exprs->push_back(*p);
delete more_exprs;
- this->tuple_assignment(exprs, p_range_clause);
+ this->tuple_assignment(exprs, may_be_composite_lit, p_range_clause);
return;
}
}
Expression_list* init;
if (p_type_switch == NULL)
- init = this->expression_list(NULL, false);
+ init = this->expression_list(NULL, false, may_be_composite_lit);
else
{
bool is_type_switch = false;
- Expression* expr = this->expression(PRECEDENCE_NORMAL, false, true,
+ Expression* expr = this->expression(PRECEDENCE_NORMAL, false,
+ may_be_composite_lit,
&is_type_switch);
if (is_type_switch)
{
else
{
this->advance_token();
- init = this->expression_list(expr, false);
+ init = this->expression_list(expr, false, may_be_composite_lit);
}
}
// 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));
- source_location location = this->location();
+ 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;
// Check for the easy error of a newline before the opening brace.
if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
{
- source_location semi_loc = this->location();
+ Location semi_loc = this->location();
if (this->advance_token()->is_op(OPERATOR_LCURLY))
error_at(this->location(),
"unexpected semicolon or newline before %<{%>");
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);
- source_location end_loc = this->block();
+ Location end_loc = this->block();
this->gogo_->finish_function(end_loc);
+ this->is_erroneous_function_ = hold_is_erroneous_function;
}
}
std::string name;
const Token* token = this->advance_token();
- source_location location = token->location();
+ Location location = token->location();
if (!token->is_op(OPERATOR_MULT))
{
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 Token::TOKEN_IDENTIFIER:
{
- source_location location = token->location();
+ Location location = token->location();
std::string id = token->identifier();
bool is_exported = token->is_identifier_exported();
std::string packed = this->gogo_->pack_hidden_name(id, is_exported);
}
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();
}
this->advance_token();
return ret;
+ case Token::TOKEN_CHARACTER:
+ ret = Expression::make_character(token->character_value(), NULL,
+ token->location());
+ this->advance_token();
+ return ret;
+
case Token::TOKEN_INTEGER:
ret = Expression::make_integer(token->integer_value(), NULL,
token->location());
case KEYWORD_MAP:
case KEYWORD_STRUCT:
{
- source_location location = token->location();
+ Location location = token->location();
return Expression::make_type(this->type(), location);
}
default:
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
{
// Here we call array_type directly, as this is the only
// case where an ellipsis is permitted for an array type.
- source_location location = token->location();
+ Location location = token->location();
return Expression::make_type(this->array_type(true), location);
}
break;
Expression*
Parse::enclosing_var_reference(Named_object* in_function, Named_object* var,
- source_location location)
+ Location location)
{
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();
// 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
// 1. In [][][]int{{{1}}} it will be 2.
Expression*
-Parse::composite_lit(Type* type, int depth, source_location location)
+Parse::composite_lit(Type* type, int depth, Location location)
{
go_assert(this->peek_token()->is_op(OPERATOR_LCURLY));
this->advance_token();
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();
+ 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
{
{
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)))
Expression*
Parse::function_lit()
{
- source_location location = this->location();
+ Location location = this->location();
go_assert(this->peek_token()->is_keyword(KEYWORD_FUNC));
this->advance_token();
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;
Named_object* no = this->gogo_->start_function("", type, true, location);
- source_location end_loc = this->block();
+ Location end_loc = this->block();
this->gogo_->finish_function(end_loc);
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,
Expression*
Parse::create_closure(Named_object* function, Enclosing_vars* enclosing_vars,
- source_location location)
+ Location location)
{
if (enclosing_vars->empty())
return NULL;
Parse::primary_expr(bool may_be_sink, bool may_be_composite_lit,
bool* is_type_switch)
{
- source_location start_loc = this->location();
+ Location start_loc = this->location();
bool is_parenthesized = this->peek_token()->is_op(OPERATOR_LPAREN);
Expression* ret = this->operand(may_be_sink);
{
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());
}
else if (this->peek_token()->is_op(OPERATOR_LPAREN))
{
- source_location loc = this->location();
+ Location loc = this->location();
this->advance_token();
Expression* expr = this->expression(PRECEDENCE_NORMAL, false, true,
NULL);
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);
+ }
}
}
Parse::selector(Expression* left, bool* is_type_switch)
{
go_assert(this->peek_token()->is_op(OPERATOR_DOT));
- source_location location = this->location();
+ Location location = this->location();
const Token* token = this->advance_token();
if (token->is_identifier())
Expression*
Parse::index(Expression* expr)
{
- source_location location = this->location();
+ Location location = this->location();
go_assert(this->peek_token()->is_op(OPERATOR_LSQUARE));
this->advance_token();
const Token* token = this->advance_token();
if (!token->is_op(OPERATOR_RPAREN))
{
- args = this->expression_list(NULL, false);
+ args = this->expression_list(NULL, false, true);
token = this->peek_token();
if (token->is_op(OPERATOR_ELLIPSIS))
{
// Return an expression for a single unqualified identifier.
Expression*
-Parse::id_to_expression(const std::string& name, source_location location)
+Parse::id_to_expression(const std::string& name, Location location)
{
Named_object* in_function;
Named_object* named_object = this->gogo_->lookup(name, &in_function);
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.
+ 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);
}
Operator op = token->op();
- source_location binop_location = token->location();
+ Location binop_location = token->location();
if (precedence >= right_precedence)
{
default:
return false;
}
+ case Token::TOKEN_CHARACTER:
case Token::TOKEN_INTEGER:
case Token::TOKEN_FLOAT:
case Token::TOKEN_IMAGINARY:
|| token->is_op(OPERATOR_MULT)
|| token->is_op(OPERATOR_AND))
{
- source_location location = token->location();
+ Location location = token->location();
Operator op = token->op();
this->advance_token();
{
std::string identifier = token->identifier();
bool is_exported = token->is_identifier_exported();
- source_location location = token->location();
+ Location location = token->location();
if (this->advance_token()->is_op(OPERATOR_COLON))
{
this->advance_token();
case Token::TOKEN_OPERATOR:
if (token->is_op(OPERATOR_LCURLY))
{
- source_location location = token->location();
+ Location location = token->location();
this->gogo_->start_block(location);
- source_location end_loc = this->block();
+ Location end_loc = this->block();
this->gogo_->add_block(this->gogo_->finish_block(end_loc),
location);
}
break;
case Token::TOKEN_STRING:
+ case Token::TOKEN_CHARACTER:
case Token::TOKEN_INTEGER:
case Token::TOKEN_FLOAT:
case Token::TOKEN_IMAGINARY:
return this->expression_may_start_here();
case Token::TOKEN_STRING:
+ case Token::TOKEN_CHARACTER:
case Token::TOKEN_INTEGER:
case Token::TOKEN_FLOAT:
case Token::TOKEN_IMAGINARY:
// Label = identifier .
void
-Parse::labeled_stmt(const std::string& label_name, source_location location)
+Parse::labeled_stmt(const std::string& label_name, Location location)
{
Label* label = this->gogo_->add_label_definition(label_name, location);
{
std::string identifier = token->identifier();
bool is_exported = token->is_identifier_exported();
- source_location location = token->location();
+ Location location = token->location();
token = this->advance_token();
if (token->is_op(OPERATOR_COLONEQ)
{
identifier = this->gogo_->pack_hidden_name(identifier, is_exported);
this->simple_var_decl_or_assignment(identifier, location,
+ may_be_composite_lit,
p_range_clause,
(token->is_op(OPERATOR_COLONEQ)
? p_type_switch
this->inc_dec_stat(this->verify_not_sink(exp));
else if (token->is_op(OPERATOR_COMMA)
|| token->is_op(OPERATOR_EQ))
- this->assignment(exp, p_range_clause);
+ this->assignment(exp, may_be_composite_lit, p_range_clause);
else if (token->is_op(OPERATOR_PLUSEQ)
|| token->is_op(OPERATOR_MINUSEQ)
|| token->is_op(OPERATOR_OREQ)
|| token->is_op(OPERATOR_RSHIFTEQ)
|| token->is_op(OPERATOR_ANDEQ)
|| token->is_op(OPERATOR_BITCLEAREQ))
- this->assignment(this->verify_not_sink(exp), p_range_clause);
+ this->assignment(this->verify_not_sink(exp), may_be_composite_lit,
+ p_range_clause);
else if (return_exp != NULL)
return this->verify_not_sink(exp);
else
- this->expression_stat(this->verify_not_sink(exp));
+ {
+ exp = this->verify_not_sink(exp);
+
+ if (token->is_op(OPERATOR_COLONEQ))
+ {
+ 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();
+ return NULL;
+ }
+
+ this->expression_stat(exp);
+ }
return NULL;
}
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 .
Parse::send_stmt(Expression* channel)
{
go_assert(this->peek_token()->is_op(OPERATOR_CHANOP));
- source_location loc = this->location();
+ Location loc = this->location();
this->advance_token();
Expression* val = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
Statement* s = Statement::make_send_statement(channel, val, loc);
// EXP is an expression that we have already parsed.
+// If MAY_BE_COMPOSITE_LIT is true, an expression on the right hand
+// side may be a composite literal.
+
// If RANGE_CLAUSE is not NULL, then this will recognize a
// RangeClause.
void
-Parse::assignment(Expression* expr, Range_clause* p_range_clause)
+Parse::assignment(Expression* expr, bool may_be_composite_lit,
+ Range_clause* p_range_clause)
{
Expression_list* vars;
if (!this->peek_token()->is_op(OPERATOR_COMMA))
else
{
this->advance_token();
- vars = this->expression_list(expr, true);
+ vars = this->expression_list(expr, true, may_be_composite_lit);
}
- this->tuple_assignment(vars, p_range_clause);
+ this->tuple_assignment(vars, may_be_composite_lit, p_range_clause);
}
// An assignment statement. LHS is the list of expressions which
// appear on the left hand side.
+// If MAY_BE_COMPOSITE_LIT is true, an expression on the right hand
+// side may be a composite literal.
+
// If RANGE_CLAUSE is not NULL, then this will recognize a
// RangeClause.
void
-Parse::tuple_assignment(Expression_list* lhs, Range_clause* p_range_clause)
+Parse::tuple_assignment(Expression_list* lhs, bool may_be_composite_lit,
+ Range_clause* p_range_clause)
{
const Token* token = this->peek_token();
if (!token->is_op(OPERATOR_EQ)
return;
}
Operator op = token->op();
- source_location location = token->location();
+ Location location = token->location();
token = this->advance_token();
return;
}
- Expression_list* vals = this->expression_list(NULL, false);
+ Expression_list* vals = this->expression_list(NULL, false,
+ may_be_composite_lit);
// We've parsed everything; check for errors.
if (lhs == NULL || vals == NULL)
Expression* channel = receive->channel();
Statement* s = Statement::make_tuple_receive_assignment(val, success,
channel,
- false,
location);
this->gogo_->add_statement(s);
}
go_assert(this->peek_token()->is_keyword(KEYWORD_GO)
|| this->peek_token()->is_keyword(KEYWORD_DEFER));
bool is_go = this->peek_token()->is_keyword(KEYWORD_GO);
- source_location stat_location = this->location();
+ Location stat_location = this->location();
this->advance_token();
- source_location expr_location = this->location();
+ Location expr_location = this->location();
Expression* expr = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
Call_expression* call_expr = expr->call_expression();
if (call_expr == NULL)
Parse::return_stat()
{
go_assert(this->peek_token()->is_keyword(KEYWORD_RETURN));
- source_location location = this->location();
+ Location location = this->location();
this->advance_token();
Expression_list* vals = NULL;
if (this->expression_may_start_here())
- vals = this->expression_list(NULL, false);
+ vals = this->expression_list(NULL, false, true);
this->gogo_->add_statement(Statement::make_return_statement(vals, location));
+
+ if (vals == NULL
+ && this->gogo_->current_function()->func_value()->results_are_named())
+ {
+ Named_object* function = this->gogo_->current_function();
+ Function::Results* results = function->func_value()->result_variables();
+ for (Function::Results::const_iterator p = results->begin();
+ p != results->end();
+ ++p)
+ {
+ Named_object* no = this->gogo_->lookup((*p)->name(), NULL);
+ 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());
+ }
+ }
}
-// IfStmt = "if" [ SimpleStmt ";" ] Expression Block [ "else" Statement ] .
+// IfStmt = "if" [ SimpleStmt ";" ] Expression Block
+// [ "else" ( IfStmt | Block ) ] .
void
Parse::if_stat()
{
go_assert(this->peek_token()->is_keyword(KEYWORD_IF));
- source_location location = this->location();
+ Location location = this->location();
this->advance_token();
this->gogo_->start_block(location);
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);
}
this->gogo_->start_block(this->location());
- source_location end_loc = this->block();
+ Location end_loc = this->block();
Block* then_block = this->gogo_->finish_block(end_loc);
// Check for the easy error of a newline before "else".
if (this->peek_token()->is_op(OPERATOR_SEMICOLON))
{
- source_location semi_loc = this->location();
+ Location semi_loc = this->location();
if (this->advance_token()->is_keyword(KEYWORD_ELSE))
error_at(this->location(),
"unexpected semicolon or newline before %<else%>");
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());
}
Parse::switch_stat(Label* label)
{
go_assert(this->peek_token()->is_keyword(KEYWORD_SWITCH));
- source_location location = this->location();
+ Location location = this->location();
this->advance_token();
this->gogo_->start_block(location);
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,
const Token* token = this->peek_token();
std::string identifier = token->identifier();
bool is_exported = token->is_identifier_exported();
- source_location id_loc = token->location();
+ Location id_loc = token->location();
token = this->advance_token();
bool is_coloneq = token->is_op(OPERATOR_COLONEQ);
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_LCURLY))
{
- source_location token_loc = this->location();
+ Location token_loc = this->location();
if (this->peek_token()->is_op(OPERATOR_SEMICOLON)
&& this->advance_token()->is_op(OPERATOR_LCURLY))
error_at(token_loc, "unexpected semicolon or newline before %<{%>");
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);
}
Statement*
Parse::expr_switch_body(Label* label, Expression* switch_val,
- source_location location)
+ Location location)
{
Switch_statement* statement = Statement::make_switch_statement(switch_val,
location);
void
Parse::expr_case_clause(Case_clauses* clauses, bool* saw_default)
{
- source_location location = this->location();
+ Location location = this->location();
bool is_default = false;
Expression_list* vals = this->expr_switch_case(&is_default);
if (token->is_keyword(KEYWORD_CASE))
{
this->advance_token();
- return this->expression_list(NULL, false);
+ return this->expression_list(NULL, false, true);
}
else if (token->is_keyword(KEYWORD_DEFAULT))
{
Statement*
Parse::type_switch_body(Label* label, const Type_switch& type_switch,
- source_location location)
+ 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 =
Parse::type_case_clause(Named_object* switch_no, Type_case_clauses* clauses,
bool* saw_default)
{
- source_location location = this->location();
+ Location location = this->location();
std::vector<Type*> types;
bool is_default = false;
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());
while (true)
{
Type* t = this->type();
+
if (!t->is_error_type())
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)
+ && !token->is_op(OPERATOR_RCURLY)
+ && !token->is_eof())
+ token = this->advance_token();
+ }
+
if (!this->peek_token()->is_op(OPERATOR_COMMA))
break;
this->advance_token();
Parse::select_stat(Label* label)
{
go_assert(this->peek_token()->is_keyword(KEYWORD_SELECT));
- source_location location = this->location();
+ Location location = this->location();
const Token* token = this->advance_token();
if (!token->is_op(OPERATOR_LCURLY))
{
- source_location token_loc = token->location();
+ Location token_loc = token->location();
if (token->is_op(OPERATOR_SEMICOLON)
&& this->advance_token()->is_op(OPERATOR_LCURLY))
error_at(token_loc, "unexpected semicolon or newline before %<{%>");
void
Parse::comm_clause(Select_clauses* clauses, bool* saw_default)
{
- source_location location = this->location();
+ Location location = this->location();
bool is_send = false;
Expression* channel = NULL;
Expression* val = NULL;
Gogo* gogo = this->gogo_;
std::string recv_var = token->identifier();
bool is_rv_exported = token->is_identifier_exported();
- source_location recv_var_loc = token->location();
+ Location recv_var_loc = token->location();
token = this->advance_token();
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))
{
std::string recv_closed = token->identifier();
bool is_rc_exported = token->is_identifier_exported();
- source_location recv_closed_loc = token->location();
+ Location recv_closed_loc = token->location();
closed_is_id = true;
token = this->advance_token();
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;
}
Parse::for_stat(Label* label)
{
go_assert(this->peek_token()->is_keyword(KEYWORD_FOR));
- source_location location = this->location();
+ Location location = this->location();
const Token* token = this->advance_token();
// Open a block to hold any variables defined in the init statement
// For_statement.
this->gogo_->start_block(this->location());
- source_location end_loc = this->block();
+ Location end_loc = this->block();
Block* statements = this->gogo_->finish_block(end_loc);
if (sfor != NULL)
Range_clause* p_range_clause)
{
go_assert(this->peek_token()->is_keyword(KEYWORD_RANGE));
- source_location location = this->location();
+ Location location = this->location();
p_range_clause->found = true;
Parse::break_stat()
{
go_assert(this->peek_token()->is_keyword(KEYWORD_BREAK));
- source_location location = this->location();
+ Location location = this->location();
const Token* token = this->advance_token();
Statement* enclosing;
{
// 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(),
+ Linemap::unknown_location(), false);
error_at(token->location(), "invalid break label %qs",
Gogo::message_name(token->identifier()).c_str());
Parse::continue_stat()
{
go_assert(this->peek_token()->is_keyword(KEYWORD_CONTINUE));
- source_location location = this->location();
+ Location location = this->location();
const Token* token = this->advance_token();
Statement* enclosing;
{
// 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(),
+ Linemap::unknown_location(), false);
error_at(token->location(), "invalid continue label %qs",
Gogo::message_name(token->identifier()).c_str());
Parse::goto_stat()
{
go_assert(this->peek_token()->is_keyword(KEYWORD_GOTO));
- source_location location = this->location();
+ Location location = this->location();
const Token* token = this->advance_token();
if (!token->is_identifier())
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();
Parse::package_clause()
{
const Token* token = this->peek_token();
- source_location location = token->location();
+ Location location = token->location();
std::string name;
if (!token->is_keyword(KEYWORD_PACKAGE))
{
Parse::import_spec(void*)
{
const Token* token = this->peek_token();
- source_location location = token->location();
+ Location location = token->location();
std::string local_name;
bool is_local_name_exported = false;
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();
+ }
+}