else
go_unreachable();
- Bexpression* ret = context->backend()->var_expression(bvar, loc);
+ Bexpression* ret =
+ context->backend()->var_expression(bvar, this->in_lvalue_pos_, loc);
if (is_in_heap)
ret = context->backend()->indirect_expression(btype, ret, true, loc);
return ret;
{
Gogo* gogo = context->gogo();
Bvariable* bvar = this->statement_->get_backend_variable(context);
- Bexpression* ret = gogo->backend()->var_expression(bvar, this->location());
+ Varexpr_context ve_ctxt = (this->is_lvalue_ ? VE_lvalue : VE_rvalue);
+
+ Bexpression* ret = gogo->backend()->var_expression(bvar, ve_ctxt,
+ this->location());
// The backend can't always represent the same set of recursive types
// that the Go frontend can. In some cases this means that a
Location loc = this->location();
Gogo* gogo = context->gogo();
Bvariable* bvar = this->statement_->get_backend_variable(context);
- Bexpression* var_ref = gogo->backend()->var_expression(bvar, loc);
+ Bexpression* lvar_ref = gogo->backend()->var_expression(bvar, VE_rvalue, loc);
Bexpression* bexpr = this->expr_->get_backend(context);
- Bstatement* set = gogo->backend()->assignment_statement(var_ref, bexpr, loc);
- var_ref = gogo->backend()->var_expression(bvar, loc);
+ Bstatement* set = gogo->backend()->assignment_statement(lvar_ref, bexpr, loc);
+ Bexpression* var_ref = gogo->backend()->var_expression(bvar, VE_lvalue, loc);
Bexpression* ret = gogo->backend()->compound_expression(set, var_ref, loc);
return ret;
}
this->bvar_ =
gogo->backend()->temporary_variable(fn_ctx, context->bblock(), bt, NULL,
false, loc, &decl);
- Bexpression* var_ref = gogo->backend()->var_expression(this->bvar_, loc);
+ Bexpression* var_ref =
+ gogo->backend()->var_expression(this->bvar_, VE_lvalue, loc);
var_ref = gogo->backend()->compound_expression(decl, var_ref, loc);
return var_ref;
}
- return gogo->backend()->var_expression(this->bvar_, loc);
+ return gogo->backend()->var_expression(this->bvar_, VE_lvalue, loc);
}
// Ast dump for sink expression.
Named_object* no = this->fn_;
Location loc = no->location();
if (this->dvar_ != NULL)
- return context->backend()->var_expression(this->dvar_, loc);
+ return context->backend()->var_expression(this->dvar_, VE_rvalue, loc);
Gogo* gogo = context->gogo();
std::string var_name;
}
this->dvar_ = bvar;
- return gogo->backend()->var_expression(bvar, loc);
+ return gogo->backend()->var_expression(bvar, VE_rvalue, loc);
}
// Print a function descriptor expression.
{
Temporary_statement* temp = sut->temporary();
Bvariable* bvar = temp->get_backend_variable(context);
- Bexpression* bvar_expr = gogo->backend()->var_expression(bvar, loc);
+ Bexpression* bvar_expr =
+ gogo->backend()->var_expression(bvar, VE_lvalue, loc);
Bexpression* bval = sut->expression()->get_backend(context);
Bstatement* bassign =
gogo->backend()->implicit_variable_set_init(implicit, buf, btype,
true, copy_to_heap, false,
bexpr);
- bexpr = gogo->backend()->var_expression(implicit, loc);
+ bexpr = gogo->backend()->var_expression(implicit, VE_lvalue, loc);
// If we are not copying a slice initializer to the heap,
// then it can be changed by the program, so if it can
&& this->expr_->type()->has_pointer())
{
Bexpression* root =
- gogo->backend()->var_expression(implicit, loc);
+ gogo->backend()->var_expression(implicit, VE_lvalue, loc);
root = gogo->backend()->address_expression(root, loc);
Type* type = Type::make_pointer_type(this->expr_->type());
gogo->add_gc_root(Expression::make_backend(root, type, loc));
true, false, btype, loc);
gogo->backend()->immutable_struct_set_init(decl, buf, true, false,
btype, loc, bexpr);
- bexpr = gogo->backend()->var_expression(decl, loc);
+ bexpr = gogo->backend()->var_expression(decl, VE_lvalue, loc);
}
go_assert(!this->create_temp_ || this->expr_->is_variable());
Bvariable* space_temp =
gogo->backend()->temporary_variable(fndecl, context->bblock(), btype,
space, true, loc, &decl);
- space = gogo->backend()->var_expression(space_temp, loc);
+ space = gogo->backend()->var_expression(space_temp, VE_lvalue, loc);
Btype* expr_btype = this->expr_->type()->get_backend(gogo);
Bexpression* ref =
gogo->backend()->indirect_expression(expr_btype, space, true, loc);
Bexpression* bexpr = this->expr_->get_backend(context);
Bstatement* assn = gogo->backend()->assignment_statement(ref, bexpr, loc);
decl = gogo->backend()->compound_statement(decl, assn);
- space = gogo->backend()->var_expression(space_temp, loc);
+ space = gogo->backend()->var_expression(space_temp, VE_rvalue, loc);
return gogo->backend()->compound_expression(decl, space, loc);
}
Gogo* gogo = context->gogo();
Location loc = Linemap::predeclared_location();
if (this->bvar_ != NULL)
- return gogo->backend()->var_expression(this->bvar_, this->location());
+ return gogo->backend()->var_expression(this->bvar_, VE_rvalue,
+ this->location());
const Typed_identifier_list* interface_methods = this->itype_->methods();
go_assert(!interface_methods->empty());
this->bvar_ =
gogo->backend()->immutable_struct_reference(mangled_name, asm_name,
btype, loc);
- return gogo->backend()->var_expression(this->bvar_, this->location());
+ return gogo->backend()->var_expression(this->bvar_, VE_rvalue,
+ this->location());
}
// The first element is the type descriptor.
!is_public, btype, loc);
gogo->backend()->immutable_struct_set_init(this->bvar_, mangled_name, false,
!is_public, btype, loc, ctor);
- return gogo->backend()->var_expression(this->bvar_, loc);
+ return gogo->backend()->var_expression(this->bvar_, VE_lvalue, loc);
}
void
return this;
}
+
+// Helper class to locate a root Var_expression within an expression
+// tree and mark it as being in an "lvalue" or assignment
+// context. Examples:
+//
+// x, y = 40, foo(w)
+// x[2] = bar(v)
+// x.z.w[blah(v + u)], y.another = 2, 3
+//
+// In the code above, vars "x" and "y" appear in lvalue / assignment
+// context, whereas the other vars "v", "u", etc are in rvalue context.
+//
+// Note: at the moment the Var_expression version of "do_copy()"
+// defaults to returning the original object, not a new object,
+// meaning that a given Var_expression can be referenced from more
+// than one place in the tree. This means that when we want to mark a
+// Var_expression as having lvalue semantics, we need to make a copy
+// of it. Example:
+//
+// mystruct.myfield += 42
+//
+// When this is lowered to eliminate the += operator, we get a tree
+//
+// mystruct.myfield = mystruct.field + 42
+//
+// in which the "mystruct" same Var_expression is referenced on both
+// LHS and RHS subtrees. This in turn means that if we try to mark the
+// LHS Var_expression the RHS Var_expression will also be marked. To
+// address this issue, the code below clones any var_expression before
+// applying an lvalue marking.
+//
+
+class Mark_lvalue_varexprs : public Traverse
+{
+ public:
+ Mark_lvalue_varexprs()
+ : Traverse(traverse_expressions)
+ { }
+
+ protected:
+ int
+ expression(Expression**);
+
+ private:
+};
+
+int Mark_lvalue_varexprs::expression(Expression** ppexpr)
+{
+ Expression* e = *ppexpr;
+
+ Var_expression* ve = e->var_expression();
+ if (ve)
+ {
+ ve = new Var_expression(ve->named_object(), ve->location());
+ ve->set_in_lvalue_pos();
+ *ppexpr = ve;
+ return TRAVERSE_EXIT;
+ }
+
+ Field_reference_expression* fre = e->field_reference_expression();
+ if (fre != NULL)
+ return TRAVERSE_CONTINUE;
+
+ Array_index_expression* aie = e->array_index_expression();
+ if (aie != NULL)
+ {
+ Mark_lvalue_varexprs mlve;
+ aie->array()->traverse_subexpressions(&mlve);
+ return TRAVERSE_EXIT;
+ }
+
+ return TRAVERSE_EXIT;
+}
+
// Convert an assignment statement to the backend representation.
Bstatement*
return context->backend()->expression_statement(rhs);
}
+ Mark_lvalue_varexprs mlve;
+ Expression::traverse(&this->lhs_, &mlve);
+
Bexpression* lhs = this->lhs_->get_backend(context);
Expression* conv =
Expression::convert_for_assignment(context->gogo(), this->lhs_->type(),