// 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))
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;
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);
// 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)
// 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,
Location location,
+ bool may_be_composite_lit,
Range_clause* p_range_clause,
Type_switch* p_type_switch)
{
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));
Location location = this->location();
+ std::string extern_name = this->lex_->extern_name();
const Token* token = this->advance_token();
Typed_identifier* rec = NULL;
{
if (named_object == NULL && !Gogo::is_sink_name(name))
{
- if (fntype != NULL)
- this->gogo_->declare_function(name, fntype, location);
- else
+ 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
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
{
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());
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))
{
{
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
// 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;
}
- 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)
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
++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);
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))
{
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 =