blob: 8c1f168d950e767f6804df226f0b7d10847c5ea0 [file] [log] [blame]
// statements.cc -- Go frontend statements.
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
#include "go-system.h"
#include <gmp.h>
#include "go-c.h"
#include "types.h"
#include "expressions.h"
#include "gogo.h"
#include "runtime.h"
#include "backend.h"
#include "statements.h"
#include "ast-dump.h"
// Class Statement.
Statement::Statement(Statement_classification classification,
source_location location)
: classification_(classification), location_(location)
{
}
Statement::~Statement()
{
}
// Traverse the tree. The work of walking the components is handled
// by the subclasses.
int
Statement::traverse(Block* block, size_t* pindex, Traverse* traverse)
{
if (this->classification_ == STATEMENT_ERROR)
return TRAVERSE_CONTINUE;
unsigned int traverse_mask = traverse->traverse_mask();
if ((traverse_mask & Traverse::traverse_statements) != 0)
{
int t = traverse->statement(block, pindex, this);
if (t == TRAVERSE_EXIT)
return TRAVERSE_EXIT;
else if (t == TRAVERSE_SKIP_COMPONENTS)
return TRAVERSE_CONTINUE;
}
// No point in checking traverse_mask here--a statement may contain
// other blocks or statements, and if we got here we always want to
// walk them.
return this->do_traverse(traverse);
}
// Traverse the contents of a statement.
int
Statement::traverse_contents(Traverse* traverse)
{
return this->do_traverse(traverse);
}
// Traverse assignments.
bool
Statement::traverse_assignments(Traverse_assignments* tassign)
{
if (this->classification_ == STATEMENT_ERROR)
return false;
return this->do_traverse_assignments(tassign);
}
// Traverse an expression in a statement. This is a helper function
// for child classes.
int
Statement::traverse_expression(Traverse* traverse, Expression** expr)
{
if ((traverse->traverse_mask()
& (Traverse::traverse_types | Traverse::traverse_expressions)) == 0)
return TRAVERSE_CONTINUE;
return Expression::traverse(expr, traverse);
}
// Traverse an expression list in a statement. This is a helper
// function for child classes.
int
Statement::traverse_expression_list(Traverse* traverse,
Expression_list* expr_list)
{
if (expr_list == NULL)
return TRAVERSE_CONTINUE;
if ((traverse->traverse_mask()
& (Traverse::traverse_types | Traverse::traverse_expressions)) == 0)
return TRAVERSE_CONTINUE;
return expr_list->traverse(traverse);
}
// Traverse a type in a statement. This is a helper function for
// child classes.
int
Statement::traverse_type(Traverse* traverse, Type* type)
{
if ((traverse->traverse_mask()
& (Traverse::traverse_types | Traverse::traverse_expressions)) == 0)
return TRAVERSE_CONTINUE;
return Type::traverse(type, traverse);
}
// Set type information for unnamed constants. This is really done by
// the child class.
void
Statement::determine_types()
{
this->do_determine_types();
}
// If this is a thunk statement, return it.
Thunk_statement*
Statement::thunk_statement()
{
Thunk_statement* ret = this->convert<Thunk_statement, STATEMENT_GO>();
if (ret == NULL)
ret = this->convert<Thunk_statement, STATEMENT_DEFER>();
return ret;
}
// Convert a Statement to the backend representation. This is really
// done by the child class.
Bstatement*
Statement::get_backend(Translate_context* context)
{
if (this->classification_ == STATEMENT_ERROR)
return context->backend()->error_statement();
return this->do_get_backend(context);
}
// Dump AST representation for a statement to a dump context.
void
Statement::dump_statement(Ast_dump_context* ast_dump_context) const
{
this->do_dump_statement(ast_dump_context);
}
// Note that this statement is erroneous. This is called by children
// when they discover an error.
void
Statement::set_is_error()
{
this->classification_ = STATEMENT_ERROR;
}
// For children to call to report an error conveniently.
void
Statement::report_error(const char* msg)
{
error_at(this->location_, "%s", msg);
this->set_is_error();
}
// An error statement, used to avoid crashing after we report an
// error.
class Error_statement : public Statement
{
public:
Error_statement(source_location location)
: Statement(STATEMENT_ERROR, location)
{ }
protected:
int
do_traverse(Traverse*)
{ return TRAVERSE_CONTINUE; }
Bstatement*
do_get_backend(Translate_context*)
{ go_unreachable(); }
void
do_dump_statement(Ast_dump_context*) const;
};
// Dump the AST representation for an error statement.
void
Error_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
{
ast_dump_context->print_indent();
ast_dump_context->ostream() << "Error statement" << std::endl;
}
// Make an error statement.
Statement*
Statement::make_error_statement(source_location location)
{
return new Error_statement(location);
}
// Class Variable_declaration_statement.
Variable_declaration_statement::Variable_declaration_statement(
Named_object* var)
: Statement(STATEMENT_VARIABLE_DECLARATION, var->var_value()->location()),
var_(var)
{
}
// We don't actually traverse the variable here; it was traversed
// while traversing the Block.
int
Variable_declaration_statement::do_traverse(Traverse*)
{
return TRAVERSE_CONTINUE;
}
// Traverse the assignments in a variable declaration. Note that this
// traversal is different from the usual traversal.
bool
Variable_declaration_statement::do_traverse_assignments(
Traverse_assignments* tassign)
{
tassign->initialize_variable(this->var_);
return true;
}
// Lower the variable's initialization expression.
Statement*
Variable_declaration_statement::do_lower(Gogo* gogo, Named_object* function,
Block*, Statement_inserter* inserter)
{
this->var_->var_value()->lower_init_expression(gogo, function, inserter);
return this;
}
// Convert a variable declaration to the backend representation.
Bstatement*
Variable_declaration_statement::do_get_backend(Translate_context* context)
{
Variable* var = this->var_->var_value();
Bvariable* bvar = this->var_->get_backend_variable(context->gogo(),
context->function());
tree init = var->get_init_tree(context->gogo(), context->function());
Bexpression* binit = init == NULL ? NULL : tree_to_expr(init);
if (!var->is_in_heap())
{
go_assert(binit != NULL);
return context->backend()->init_statement(bvar, binit);
}
// Something takes the address of this variable, so the value is
// stored in the heap. Initialize it to newly allocated memory
// space, and assign the initial value to the new space.
source_location loc = this->location();
Named_object* newfn = context->gogo()->lookup_global("new");
go_assert(newfn != NULL && newfn->is_function_declaration());
Expression* func = Expression::make_func_reference(newfn, NULL, loc);
Expression_list* params = new Expression_list();
params->push_back(Expression::make_type(var->type(), loc));
Expression* call = Expression::make_call(func, params, false, loc);
context->gogo()->lower_expression(context->function(), NULL, &call);
Temporary_statement* temp = Statement::make_temporary(NULL, call, loc);
Bstatement* btemp = temp->get_backend(context);
Bstatement* set = NULL;
if (binit != NULL)
{
Expression* e = Expression::make_temporary_reference(temp, loc);
e = Expression::make_unary(OPERATOR_MULT, e, loc);
Bexpression* be = tree_to_expr(e->get_tree(context));
set = context->backend()->assignment_statement(be, binit, loc);
}
Expression* ref = Expression::make_temporary_reference(temp, loc);
Bexpression* bref = tree_to_expr(ref->get_tree(context));
Bstatement* sinit = context->backend()->init_statement(bvar, bref);
std::vector<Bstatement*> stats;
stats.reserve(3);
stats.push_back(btemp);
if (set != NULL)
stats.push_back(set);
stats.push_back(sinit);
return context->backend()->statement_list(stats);
}
// Dump the AST representation for a variable declaration.
void
Variable_declaration_statement::do_dump_statement(
Ast_dump_context* ast_dump_context) const
{
ast_dump_context->print_indent();
go_assert(var_->is_variable());
ast_dump_context->ostream() << "var " << this->var_->name() << " ";
Variable* var = this->var_->var_value();
if (var->has_type())
{
ast_dump_context->dump_type(var->type());
ast_dump_context->ostream() << " ";
}
if (var->init() != NULL)
{
ast_dump_context->ostream() << "= ";
ast_dump_context->dump_expression(var->init());
}
ast_dump_context->ostream() << std::endl;
}
// Make a variable declaration.
Statement*
Statement::make_variable_declaration(Named_object* var)
{
return new Variable_declaration_statement(var);
}
// Class Temporary_statement.
// Return the type of the temporary variable.
Type*
Temporary_statement::type() const
{
return this->type_ != NULL ? this->type_ : this->init_->type();
}
// Traversal.
int
Temporary_statement::do_traverse(Traverse* traverse)
{
if (this->type_ != NULL
&& this->traverse_type(traverse, this->type_) == TRAVERSE_EXIT)
return TRAVERSE_EXIT;
if (this->init_ == NULL)
return TRAVERSE_CONTINUE;
else
return this->traverse_expression(traverse, &this->init_);
}
// Traverse assignments.
bool
Temporary_statement::do_traverse_assignments(Traverse_assignments* tassign)
{
if (this->init_ == NULL)
return false;
tassign->value(&this->init_, true, true);
return true;
}
// Determine types.
void
Temporary_statement::do_determine_types()
{
if (this->type_ != NULL && this->type_->is_abstract())
this->type_ = this->type_->make_non_abstract_type();
if (this->init_ != NULL)
{
if (this->type_ == NULL)
this->init_->determine_type_no_context();
else
{
Type_context context(this->type_, false);
this->init_->determine_type(&context);
}
}
if (this->type_ == NULL)
{
this->type_ = this->init_->type();
go_assert(!this->type_->is_abstract());
}
}
// Check types.
void
Temporary_statement::do_check_types(Gogo*)
{
if (this->type_ != NULL && this->init_ != NULL)
{
std::string reason;
bool ok;
if (this->are_hidden_fields_ok_)
ok = Type::are_assignable_hidden_ok(this->type_, this->init_->type(),
&reason);
else
ok = Type::are_assignable(this->type_, this->init_->type(), &reason);
if (!ok)
{
if (reason.empty())
error_at(this->location(), "incompatible types in assignment");
else
error_at(this->location(), "incompatible types in assignment (%s)",
reason.c_str());
this->set_is_error();
}
}
}
// Convert to backend representation.
Bstatement*
Temporary_statement::do_get_backend(Translate_context* context)
{
go_assert(this->bvariable_ == NULL);
// FIXME: Permitting FUNCTION to be NULL here is a temporary measure
// until we have a better representation of the init function.
Named_object* function = context->function();
Bfunction* bfunction;
if (function == NULL)
bfunction = NULL;
else
bfunction = tree_to_function(function->func_value()->get_decl());
Btype* btype = this->type()->get_backend(context->gogo());
Bexpression* binit;
if (this->init_ == NULL)
binit = NULL;
else if (this->type_ == NULL)
binit = tree_to_expr(this->init_->get_tree(context));
else
{
Expression* init = Expression::make_cast(this->type_, this->init_,
this->location());
context->gogo()->lower_expression(context->function(), NULL, &init);
binit = tree_to_expr(init->get_tree(context));
}
Bstatement* statement;
this->bvariable_ =
context->backend()->temporary_variable(bfunction, context->bblock(),
btype, binit,
this->is_address_taken_,
this->location(), &statement);
return statement;
}
// Return the backend variable.
Bvariable*
Temporary_statement::get_backend_variable(Translate_context* context) const
{
if (this->bvariable_ == NULL)
{
go_assert(saw_errors());
return context->backend()->error_variable();
}
return this->bvariable_;
}
// Dump the AST represemtation for a temporary statement
void
Temporary_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
{
ast_dump_context->print_indent();
ast_dump_context->dump_temp_variable_name(this);
if (this->type_ != NULL)
{
ast_dump_context->ostream() << " ";
ast_dump_context->dump_type(this->type_);
}
if (this->init_ != NULL)
{
ast_dump_context->ostream() << " = ";
ast_dump_context->dump_expression(this->init_);
}
ast_dump_context->ostream() << std::endl;
}
// Make and initialize a temporary variable in BLOCK.
Temporary_statement*
Statement::make_temporary(Type* type, Expression* init,
source_location location)
{
return new Temporary_statement(type, init, location);
}
// An assignment statement.
class Assignment_statement : public Statement
{
public:
Assignment_statement(Expression* lhs, Expression* rhs,
source_location location)
: Statement(STATEMENT_ASSIGNMENT, location),
lhs_(lhs), rhs_(rhs), are_hidden_fields_ok_(false)
{ }
// Note that it is OK for this assignment statement to set hidden
// fields.
void
set_hidden_fields_are_ok()
{ this->are_hidden_fields_ok_ = true; }
protected:
int
do_traverse(Traverse* traverse);
bool
do_traverse_assignments(Traverse_assignments*);
void
do_determine_types();
void
do_check_types(Gogo*);
Bstatement*
do_get_backend(Translate_context*);
void
do_dump_statement(Ast_dump_context*) const;
private:
// Left hand side--the lvalue.
Expression* lhs_;
// Right hand side--the rvalue.
Expression* rhs_;
// True if this statement may set hidden fields in the assignment
// statement. This is used for generated method stubs.
bool are_hidden_fields_ok_;
};
// Traversal.
int
Assignment_statement::do_traverse(Traverse* traverse)
{
if (this->traverse_expression(traverse, &this->lhs_) == TRAVERSE_EXIT)
return TRAVERSE_EXIT;
return this->traverse_expression(traverse, &this->rhs_);
}
bool
Assignment_statement::do_traverse_assignments(Traverse_assignments* tassign)
{
tassign->assignment(&this->lhs_, &this->rhs_);
return true;
}
// Set types for the assignment.
void
Assignment_statement::do_determine_types()
{
this->lhs_->determine_type_no_context();
Type_context context(this->lhs_->type(), false);
this->rhs_->determine_type(&context);
}
// Check types for an assignment.
void
Assignment_statement::do_check_types(Gogo*)
{
// The left hand side must be either addressable, a map index
// expression, or the blank identifier.
if (!this->lhs_->is_addressable()
&& this->lhs_->map_index_expression() == NULL
&& !this->lhs_->is_sink_expression())
{
if (!this->lhs_->type()->is_error())
this->report_error(_("invalid left hand side of assignment"));
return;
}
Type* lhs_type = this->lhs_->type();
Type* rhs_type = this->rhs_->type();
std::string reason;
bool ok;
if (this->are_hidden_fields_ok_)
ok = Type::are_assignable_hidden_ok(lhs_type, rhs_type, &reason);
else
ok = Type::are_assignable(lhs_type, rhs_type, &reason);
if (!ok)
{
if (reason.empty())
error_at(this->location(), "incompatible types in assignment");
else
error_at(this->location(), "incompatible types in assignment (%s)",
reason.c_str());
this->set_is_error();
}
if (lhs_type->is_error() || rhs_type->is_error())
this->set_is_error();
}
// Convert an assignment statement to the backend representation.
Bstatement*
Assignment_statement::do_get_backend(Translate_context* context)
{
tree rhs_tree = this->rhs_->get_tree(context);
if (this->lhs_->is_sink_expression())
return context->backend()->expression_statement(tree_to_expr(rhs_tree));
tree lhs_tree = this->lhs_->get_tree(context);
rhs_tree = Expression::convert_for_assignment(context, this->lhs_->type(),
this->rhs_->type(), rhs_tree,
this->location());
return context->backend()->assignment_statement(tree_to_expr(lhs_tree),
tree_to_expr(rhs_tree),
this->location());
}
// Dump the AST representation for an assignment statement.
void
Assignment_statement::do_dump_statement(Ast_dump_context* ast_dump_context)
const
{
ast_dump_context->print_indent();
ast_dump_context->dump_expression(this->lhs_);
ast_dump_context->ostream() << " = " ;
ast_dump_context->dump_expression(this->rhs_);
ast_dump_context->ostream() << std::endl;
}
// Make an assignment statement.
Statement*
Statement::make_assignment(Expression* lhs, Expression* rhs,
source_location location)
{
return new Assignment_statement(lhs, rhs, location);
}
// The Move_ordered_evals class is used to find any subexpressions of
// an expression that have an evaluation order dependency. It creates
// temporary variables to hold them.
class Move_ordered_evals : public Traverse
{
public:
Move_ordered_evals(Block* block)
: Traverse(traverse_expressions),
block_(block)
{ }
protected:
int
expression(Expression**);
private:
// The block where new temporary variables should be added.
Block* block_;
};
int
Move_ordered_evals::expression(Expression** pexpr)
{
// We have to look at subexpressions first.
if ((*pexpr)->traverse_subexpressions(this) == TRAVERSE_EXIT)
return TRAVERSE_EXIT;
if ((*pexpr)->must_eval_in_order())
{
source_location loc = (*pexpr)->location();
Temporary_statement* temp = Statement::make_temporary(NULL, *pexpr, loc);
this->block_->add_statement(temp);
*pexpr = Expression::make_temporary_reference(temp, loc);
}
return TRAVERSE_SKIP_COMPONENTS;
}
// An assignment operation statement.
class Assignment_operation_statement : public Statement
{
public:
Assignment_operation_statement(Operator op, Expression* lhs, Expression* rhs,
source_location location)
: Statement(STATEMENT_ASSIGNMENT_OPERATION, location),
op_(op), lhs_(lhs), rhs_(rhs)
{ }
protected:
int
do_traverse(Traverse*);
bool
do_traverse_assignments(Traverse_assignments*)
{ go_unreachable(); }
Statement*
do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
Bstatement*
do_get_backend(Translate_context*)
{ go_unreachable(); }
void
do_dump_statement(Ast_dump_context*) const;
private:
// The operator (OPERATOR_PLUSEQ, etc.).
Operator op_;
// Left hand side.
Expression* lhs_;
// Right hand side.
Expression* rhs_;
};
// Traversal.
int
Assignment_operation_statement::do_traverse(Traverse* traverse)
{
if (this->traverse_expression(traverse, &this->lhs_) == TRAVERSE_EXIT)
return TRAVERSE_EXIT;
return this->traverse_expression(traverse, &this->rhs_);
}
// Lower an assignment operation statement to a regular assignment
// statement.
Statement*
Assignment_operation_statement::do_lower(Gogo*, Named_object*,
Block* enclosing, Statement_inserter*)
{
source_location loc = this->location();
// We have to evaluate the left hand side expression only once. We
// do this by moving out any expression with side effects.
Block* b = new Block(enclosing, loc);
Move_ordered_evals moe(b);
this->lhs_->traverse_subexpressions(&moe);
Expression* lval = this->lhs_->copy();
Operator op;
switch (this->op_)
{
case OPERATOR_PLUSEQ:
op = OPERATOR_PLUS;
break;
case OPERATOR_MINUSEQ:
op = OPERATOR_MINUS;
break;
case OPERATOR_OREQ:
op = OPERATOR_OR;
break;
case OPERATOR_XOREQ:
op = OPERATOR_XOR;
break;
case OPERATOR_MULTEQ:
op = OPERATOR_MULT;
break;
case OPERATOR_DIVEQ:
op = OPERATOR_DIV;
break;
case OPERATOR_MODEQ:
op = OPERATOR_MOD;
break;
case OPERATOR_LSHIFTEQ:
op = OPERATOR_LSHIFT;
break;
case OPERATOR_RSHIFTEQ:
op = OPERATOR_RSHIFT;
break;
case OPERATOR_ANDEQ:
op = OPERATOR_AND;
break;
case OPERATOR_BITCLEAREQ:
op = OPERATOR_BITCLEAR;
break;
default:
go_unreachable();
}
Expression* binop = Expression::make_binary(op, lval, this->rhs_, loc);
Statement* s = Statement::make_assignment(this->lhs_, binop, loc);
if (b->statements()->empty())
{
delete b;
return s;
}
else
{
b->add_statement(s);
return Statement::make_block_statement(b, loc);
}
}
// Dump the AST representation for an assignment operation statement
void
Assignment_operation_statement::do_dump_statement(
Ast_dump_context* ast_dump_context) const
{
ast_dump_context->print_indent();
ast_dump_context->dump_expression(this->lhs_);
ast_dump_context->dump_operator(this->op_);
ast_dump_context->dump_expression(this->rhs_);
ast_dump_context->ostream() << std::endl;
}
// Make an assignment operation statement.
Statement*
Statement::make_assignment_operation(Operator op, Expression* lhs,
Expression* rhs, source_location location)
{
return new Assignment_operation_statement(op, lhs, rhs, location);
}
// A tuple assignment statement. This differs from an assignment
// statement in that the right-hand-side expressions are evaluated in
// parallel.
class Tuple_assignment_statement : public Statement
{
public:
Tuple_assignment_statement(Expression_list* lhs, Expression_list* rhs,
source_location location)
: Statement(STATEMENT_TUPLE_ASSIGNMENT, location),
lhs_(lhs), rhs_(rhs), are_hidden_fields_ok_(false)
{ }
// Note that it is OK for this assignment statement to set hidden
// fields.
void
set_hidden_fields_are_ok()
{ this->are_hidden_fields_ok_ = true; }
protected:
int
do_traverse(Traverse* traverse);
bool
do_traverse_assignments(Traverse_assignments*)
{ go_unreachable(); }
Statement*
do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
Bstatement*
do_get_backend(Translate_context*)
{ go_unreachable(); }
void
do_dump_statement(Ast_dump_context*) const;
private:
// Left hand side--a list of lvalues.
Expression_list* lhs_;
// Right hand side--a list of rvalues.
Expression_list* rhs_;
// True if this statement may set hidden fields in the assignment
// statement. This is used for generated method stubs.
bool are_hidden_fields_ok_;
};
// Traversal.
int
Tuple_assignment_statement::do_traverse(Traverse* traverse)
{
if (this->traverse_expression_list(traverse, this->lhs_) == TRAVERSE_EXIT)
return TRAVERSE_EXIT;
return this->traverse_expression_list(traverse, this->rhs_);
}
// Lower a tuple assignment. We use temporary variables to split it
// up into a set of single assignments.
Statement*
Tuple_assignment_statement::do_lower(Gogo*, Named_object*, Block* enclosing,
Statement_inserter*)
{
source_location loc = this->location();
Block* b = new Block(enclosing, loc);
// First move out any subexpressions on the left hand side. The
// right hand side will be evaluated in the required order anyhow.
Move_ordered_evals moe(b);
for (Expression_list::const_iterator plhs = this->lhs_->begin();
plhs != this->lhs_->end();
++plhs)
(*plhs)->traverse_subexpressions(&moe);
std::vector<Temporary_statement*> temps;
temps.reserve(this->lhs_->size());
Expression_list::const_iterator prhs = this->rhs_->begin();
for (Expression_list::const_iterator plhs = this->lhs_->begin();
plhs != this->lhs_->end();
++plhs, ++prhs)
{
go_assert(prhs != this->rhs_->end());
if ((*plhs)->is_error_expression()
|| (*plhs)->type()->is_error()
|| (*prhs)->is_error_expression()
|| (*prhs)->type()->is_error())
continue;
if ((*plhs)->is_sink_expression())
{
b->add_statement(Statement::make_statement(*prhs, true));
continue;
}
Temporary_statement* temp = Statement::make_temporary((*plhs)->type(),
*prhs, loc);
if (this->are_hidden_fields_ok_)
temp->set_hidden_fields_are_ok();
b->add_statement(temp);
temps.push_back(temp);
}
go_assert(prhs == this->rhs_->end());
prhs = this->rhs_->begin();
std::vector<Temporary_statement*>::const_iterator ptemp = temps.begin();
for (Expression_list::const_iterator plhs = this->lhs_->begin();
plhs != this->lhs_->end();
++plhs, ++prhs)
{
if ((*plhs)->is_error_expression()
|| (*plhs)->type()->is_error()
|| (*prhs)->is_error_expression()
|| (*prhs)->type()->is_error())
continue;
if ((*plhs)->is_sink_expression())
continue;
Expression* ref = Expression::make_temporary_reference(*ptemp, loc);
Statement* s = Statement::make_assignment(*plhs, ref, loc);
if (this->are_hidden_fields_ok_)
{
Assignment_statement* as = static_cast<Assignment_statement*>(s);
as->set_hidden_fields_are_ok();
}
b->add_statement(s);
++ptemp;
}
go_assert(ptemp == temps.end());
return Statement::make_block_statement(b, loc);
}
// Dump the AST representation for a tuple assignment statement.
void
Tuple_assignment_statement::do_dump_statement(
Ast_dump_context* ast_dump_context) const
{
ast_dump_context->print_indent();
ast_dump_context->dump_expression_list(this->lhs_);
ast_dump_context->ostream() << " = ";
ast_dump_context->dump_expression_list(this->rhs_);
ast_dump_context->ostream() << std::endl;
}
// Make a tuple assignment statement.
Statement*
Statement::make_tuple_assignment(Expression_list* lhs, Expression_list* rhs,
source_location location)
{
return new Tuple_assignment_statement(lhs, rhs, location);
}
// A tuple assignment from a map index expression.
// v, ok = m[k]
class Tuple_map_assignment_statement : public Statement
{
public:
Tuple_map_assignment_statement(Expression* val, Expression* present,
Expression* map_index,
source_location location)
: Statement(STATEMENT_TUPLE_MAP_ASSIGNMENT, location),
val_(val), present_(present), map_index_(map_index)
{ }
protected:
int
do_traverse(Traverse* traverse);
bool
do_traverse_assignments(Traverse_assignments*)
{ go_unreachable(); }
Statement*
do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
Bstatement*
do_get_backend(Translate_context*)
{ go_unreachable(); }
void
do_dump_statement(Ast_dump_context*) const;
private:
// Lvalue which receives the value from the map.
Expression* val_;
// Lvalue which receives whether the key value was present.
Expression* present_;
// The map index expression.
Expression* map_index_;
};
// Traversal.
int
Tuple_map_assignment_statement::do_traverse(Traverse* traverse)
{
if (this->traverse_expression(traverse, &this->val_) == TRAVERSE_EXIT
|| this->traverse_expression(traverse, &this->present_) == TRAVERSE_EXIT)
return TRAVERSE_EXIT;
return this->traverse_expression(traverse, &this->map_index_);
}
// Lower a tuple map assignment.
Statement*
Tuple_map_assignment_statement::do_lower(Gogo*, Named_object*,
Block* enclosing, Statement_inserter*)
{
source_location loc = this->location();
Map_index_expression* map_index = this->map_index_->map_index_expression();
if (map_index == NULL)
{
this->report_error(_("expected map index on right hand side"));
return Statement::make_error_statement(loc);
}
Map_type* map_type = map_index->get_map_type();
if (map_type == NULL)
return Statement::make_error_statement(loc);
Block* b = new Block(enclosing, loc);
// Move out any subexpressions to make sure that functions are
// called in the required order.
Move_ordered_evals moe(b);
this->val_->traverse_subexpressions(&moe);
this->present_->traverse_subexpressions(&moe);
// Copy the key value into a temporary so that we can take its
// address without pushing the value onto the heap.
// var key_temp KEY_TYPE = MAP_INDEX
Temporary_statement* key_temp =
Statement::make_temporary(map_type->key_type(), map_index->index(), loc);
b->add_statement(key_temp);
// var val_temp VAL_TYPE
Temporary_statement* val_temp =
Statement::make_temporary(map_type->val_type(), NULL, loc);
b->add_statement(val_temp);
// var present_temp bool
Temporary_statement* present_temp =
Statement::make_temporary(Type::lookup_bool_type(), NULL, loc);
b->add_statement(present_temp);
// present_temp = mapaccess2(DESCRIPTOR, MAP, &key_temp, &val_temp)
Expression* a1 = Expression::make_type_descriptor(map_type, loc);
Expression* a2 = map_index->map();
Temporary_reference_expression* ref =
Expression::make_temporary_reference(key_temp, loc);
Expression* a3 = Expression::make_unary(OPERATOR_AND, ref, loc);
ref = Expression::make_temporary_reference(val_temp, loc);
Expression* a4 = Expression::make_unary(OPERATOR_AND, ref, loc);
Expression* call = Runtime::make_call(Runtime::MAPACCESS2, loc, 4,
a1, a2, a3, a4);
ref = Expression::make_temporary_reference(present_temp, loc);
ref->set_is_lvalue();
Statement* s = Statement::make_assignment(ref, call, loc);
b->add_statement(s);
// val = val_temp
ref = Expression::make_temporary_reference(val_temp, loc);
s = Statement::make_assignment(this->val_, ref, loc);
b->add_statement(s);
// present = present_temp
ref = Expression::make_temporary_reference(present_temp, loc);
s = Statement::make_assignment(this->present_, ref, loc);
b->add_statement(s);
return Statement::make_block_statement(b, loc);
}
// Dump the AST representation for a tuple map assignment statement.
void
Tuple_map_assignment_statement::do_dump_statement(
Ast_dump_context* ast_dump_context) const
{
ast_dump_context->print_indent();
ast_dump_context->dump_expression(this->val_);
ast_dump_context->ostream() << ", ";
ast_dump_context->dump_expression(this->present_);
ast_dump_context->ostream() << " = ";
ast_dump_context->dump_expression(this->map_index_);
ast_dump_context->ostream() << std::endl;
}
// Make a map assignment statement which returns a pair of values.
Statement*
Statement::make_tuple_map_assignment(Expression* val, Expression* present,
Expression* map_index,
source_location location)
{
return new Tuple_map_assignment_statement(val, present, map_index, location);
}
// Assign a pair of entries to a map.
// m[k] = v, p
class Map_assignment_statement : public Statement
{
public:
Map_assignment_statement(Expression* map_index,
Expression* val, Expression* should_set,
source_location location)
: Statement(STATEMENT_MAP_ASSIGNMENT, location),
map_index_(map_index), val_(val), should_set_(should_set)
{ }
protected:
int
do_traverse(Traverse* traverse);
bool
do_traverse_assignments(Traverse_assignments*)
{ go_unreachable(); }
Statement*
do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
Bstatement*
do_get_backend(Translate_context*)
{ go_unreachable(); }
void
do_dump_statement(Ast_dump_context*) const;
private:
// A reference to the map index which should be set or deleted.
Expression* map_index_;
// The value to add to the map.
Expression* val_;
// Whether or not to add the value.
Expression* should_set_;
};
// Traverse a map assignment.
int
Map_assignment_statement::do_traverse(Traverse* traverse)
{
if (this->traverse_expression(traverse, &this->map_index_) == TRAVERSE_EXIT
|| this->traverse_expression(traverse, &this->val_) == TRAVERSE_EXIT)
return TRAVERSE_EXIT;
return this->traverse_expression(traverse, &this->should_set_);
}
// Lower a map assignment to a function call.
Statement*
Map_assignment_statement::do_lower(Gogo*, Named_object*, Block* enclosing,
Statement_inserter*)
{
source_location loc = this->location();
Map_index_expression* map_index = this->map_index_->map_index_expression();
if (map_index == NULL)
{
this->report_error(_("expected map index on left hand side"));
return Statement::make_error_statement(loc);
}
Map_type* map_type = map_index->get_map_type();
if (map_type == NULL)
return Statement::make_error_statement(loc);
Block* b = new Block(enclosing, loc);
// Evaluate the map first to get order of evaluation right.
// map_temp := m // we are evaluating m[k] = v, p
Temporary_statement* map_temp = Statement::make_temporary(map_type,
map_index->map(),
loc);
b->add_statement(map_temp);
// var key_temp MAP_KEY_TYPE = k
Temporary_statement* key_temp =
Statement::make_temporary(map_type->key_type(), map_index->index(), loc);
b->add_statement(key_temp);
// var val_temp MAP_VAL_TYPE = v
Temporary_statement* val_temp =
Statement::make_temporary(map_type->val_type(), this->val_, loc);
b->add_statement(val_temp);
// var insert_temp bool = p
Temporary_statement* insert_temp =
Statement::make_temporary(Type::lookup_bool_type(), this->should_set_,
loc);
b->add_statement(insert_temp);
// mapassign2(map_temp, &key_temp, &val_temp, p)
Expression* p1 = Expression::make_temporary_reference(map_temp, loc);
Expression* ref = Expression::make_temporary_reference(key_temp, loc);
Expression* p2 = Expression::make_unary(OPERATOR_AND, ref, loc);
ref = Expression::make_temporary_reference(val_temp, loc);
Expression* p3 = Expression::make_unary(OPERATOR_AND, ref, loc);
Expression* p4 = Expression::make_temporary_reference(insert_temp, loc);
Expression* call = Runtime::make_call(Runtime::MAPASSIGN2, loc, 4,
p1, p2, p3, p4);
Statement* s = Statement::make_statement(call, true);
b->add_statement(s);
return Statement::make_block_statement(b, loc);
}
// Dump the AST representation for a map assignment statement.
void
Map_assignment_statement::do_dump_statement(
Ast_dump_context* ast_dump_context) const
{
ast_dump_context->print_indent();
ast_dump_context->dump_expression(this->map_index_);
ast_dump_context->ostream() << " = ";
ast_dump_context->dump_expression(this->val_);
ast_dump_context->ostream() << ", ";
ast_dump_context->dump_expression(this->should_set_);
ast_dump_context->ostream() << std::endl;
}
// Make a statement which assigns a pair of entries to a map.
Statement*
Statement::make_map_assignment(Expression* map_index,
Expression* val, Expression* should_set,
source_location location)
{
return new Map_assignment_statement(map_index, val, should_set, location);
}
// A tuple assignment from a receive statement.
class Tuple_receive_assignment_statement : public Statement
{
public:
Tuple_receive_assignment_statement(Expression* val, Expression* closed,
Expression* channel, bool for_select,
source_location location)
: Statement(STATEMENT_TUPLE_RECEIVE_ASSIGNMENT, location),
val_(val), closed_(closed), channel_(channel), for_select_(for_select)
{ }
protected:
int
do_traverse(Traverse* traverse);
bool
do_traverse_assignments(Traverse_assignments*)
{ go_unreachable(); }
Statement*
do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
Bstatement*
do_get_backend(Translate_context*)
{ go_unreachable(); }
void
do_dump_statement(Ast_dump_context*) const;
private:
// Lvalue which receives the value from the channel.
Expression* val_;
// Lvalue which receives whether the channel is closed.
Expression* closed_;
// The channel on which we receive the value.
Expression* channel_;
// Whether this is for a select statement.
bool for_select_;
};
// Traversal.
int
Tuple_receive_assignment_statement::do_traverse(Traverse* traverse)
{
if (this->traverse_expression(traverse, &this->val_) == TRAVERSE_EXIT
|| this->traverse_expression(traverse, &this->closed_) == TRAVERSE_EXIT)
return TRAVERSE_EXIT;
return this->traverse_expression(traverse, &this->channel_);
}
// Lower to a function call.
Statement*
Tuple_receive_assignment_statement::do_lower(Gogo*, Named_object*,
Block* enclosing,
Statement_inserter*)
{
source_location loc = this->location();
Channel_type* channel_type = this->channel_->type()->channel_type();
if (channel_type == NULL)
{
this->report_error(_("expected channel"));
return Statement::make_error_statement(loc);
}
if (!channel_type->may_receive())
{
this->report_error(_("invalid receive on send-only channel"));
return Statement::make_error_statement(loc);
}
Block* b = new Block(enclosing, loc);
// Make sure that any subexpressions on the left hand side are
// evaluated in the right order.
Move_ordered_evals moe(b);
this->val_->traverse_subexpressions(&moe);
this->closed_->traverse_subexpressions(&moe);
// var val_temp ELEMENT_TYPE
Temporary_statement* val_temp =
Statement::make_temporary(channel_type->element_type(), NULL, loc);
b->add_statement(val_temp);
// var closed_temp bool
Temporary_statement* closed_temp =
Statement::make_temporary(Type::lookup_bool_type(), NULL, loc);
b->add_statement(closed_temp);
// closed_temp = chanrecv[23](channel, &val_temp)
Temporary_reference_expression* ref =
Expression::make_temporary_reference(val_temp, loc);
Expression* p2 = Expression::make_unary(OPERATOR_AND, ref, loc);
Expression* call = Runtime::make_call((this->for_select_
? Runtime::CHANRECV3
: Runtime::CHANRECV2),
loc, 2, this->channel_, p2);
ref = Expression::make_temporary_reference(closed_temp, loc);
ref->set_is_lvalue();
Statement* s = Statement::make_assignment(ref, call, loc);
b->add_statement(s);
// val = val_temp
ref = Expression::make_temporary_reference(val_temp, loc);
s = Statement::make_assignment(this->val_, ref, loc);
b->add_statement(s);
// closed = closed_temp
ref = Expression::make_temporary_reference(closed_temp, loc);
s = Statement::make_assignment(this->closed_, ref, loc);
b->add_statement(s);
return Statement::make_block_statement(b, loc);
}
// Dump the AST representation for a tuple receive statement.
void
Tuple_receive_assignment_statement::do_dump_statement(
Ast_dump_context* ast_dump_context) const
{
ast_dump_context->print_indent();
ast_dump_context->dump_expression(this->val_);
ast_dump_context->ostream() << ", ";
ast_dump_context->dump_expression(this->closed_);
ast_dump_context->ostream() << " <- ";
ast_dump_context->dump_expression(this->channel_);
ast_dump_context->ostream() << std::endl;
}
// Make a nonblocking receive statement.
Statement*
Statement::make_tuple_receive_assignment(Expression* val, Expression* closed,
Expression* channel,
bool for_select,
source_location location)
{
return new Tuple_receive_assignment_statement(val, closed, channel,
for_select, location);
}
// An assignment to a pair of values from a type guard. This is a
// conditional type guard. v, ok = i.(type).
class Tuple_type_guard_assignment_statement : public Statement
{
public:
Tuple_type_guard_assignment_statement(Expression* val, Expression* ok,
Expression* expr, Type* type,
source_location location)
: Statement(STATEMENT_TUPLE_TYPE_GUARD_ASSIGNMENT, location),
val_(val), ok_(ok), expr_(expr), type_(type)
{ }
protected:
int
do_traverse(Traverse*);
bool
do_traverse_assignments(Traverse_assignments*)
{ go_unreachable(); }
Statement*
do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
Bstatement*
do_get_backend(Translate_context*)
{ go_unreachable(); }
void
do_dump_statement(Ast_dump_context*) const;
private:
Call_expression*
lower_to_type(Runtime::Function);
void
lower_to_object_type(Block*, Runtime::Function);
// The variable which recieves the converted value.
Expression* val_;
// The variable which receives the indication of success.
Expression* ok_;
// The expression being converted.
Expression* expr_;
// The type to which the expression is being converted.
Type* type_;
};
// Traverse a type guard tuple assignment.
int
Tuple_type_guard_assignment_statement::do_traverse(Traverse* traverse)
{
if (this->traverse_expression(traverse, &this->val_) == TRAVERSE_EXIT
|| this->traverse_expression(traverse, &this->ok_) == TRAVERSE_EXIT
|| this->traverse_type(traverse, this->type_) == TRAVERSE_EXIT)
return TRAVERSE_EXIT;
return this->traverse_expression(traverse, &this->expr_);
}
// Lower to a function call.
Statement*
Tuple_type_guard_assignment_statement::do_lower(Gogo*, Named_object*,
Block* enclosing,
Statement_inserter*)
{
source_location loc = this->location();
Type* expr_type = this->expr_->type();
if (expr_type->interface_type() == NULL)
{
if (!expr_type->is_error() && !this->type_->is_error())
this->report_error(_("type assertion only valid for interface types"));
return Statement::make_error_statement(loc);
}
Block* b = new Block(enclosing, loc);
// Make sure that any subexpressions on the left hand side are
// evaluated in the right order.
Move_ordered_evals moe(b);
this->val_->traverse_subexpressions(&moe);
this->ok_->traverse_subexpressions(&moe);
bool expr_is_empty = expr_type->interface_type()->is_empty();
Call_expression* call;
if (this->type_->interface_type() != NULL)
{
if (this->type_->interface_type()->is_empty())
call = Runtime::make_call((expr_is_empty
? Runtime::IFACEE2E2
: Runtime::IFACEI2E2),
loc, 1, this->expr_);
else
call = this->lower_to_type(expr_is_empty
? Runtime::IFACEE2I2
: Runtime::IFACEI2I2);
}
else if (this->type_->points_to() != NULL)
call = this->lower_to_type(expr_is_empty
? Runtime::IFACEE2T2P
: Runtime::IFACEI2T2P);
else
{
this->lower_to_object_type(b,
(expr_is_empty
? Runtime::IFACEE2T2
: Runtime::IFACEI2T2));
call = NULL;
}
if (call != NULL)
{
Expression* res = Expression::make_call_result(call, 0);
res = Expression::make_unsafe_cast(this->type_, res, loc);
Statement* s = Statement::make_assignment(this->val_, res, loc);
b->add_statement(s);
res = Expression::make_call_result(call, 1);
s = Statement::make_assignment(this->ok_, res, loc);
b->add_statement(s);
}
return Statement::make_block_statement(b, loc);
}
// Lower a conversion to a non-empty interface type or a pointer type.
Call_expression*
Tuple_type_guard_assignment_statement::lower_to_type(Runtime::Function code)
{
source_location loc = this->location();
return Runtime::make_call(code, loc, 2,
Expression::make_type_descriptor(this->type_, loc),
this->expr_);
}
// Lower a conversion to a non-interface non-pointer type.
void
Tuple_type_guard_assignment_statement::lower_to_object_type(
Block* b,
Runtime::Function code)
{
source_location loc = this->location();
// var val_temp TYPE
Temporary_statement* val_temp = Statement::make_temporary(this->type_,
NULL, loc);
b->add_statement(val_temp);
// ok = CODE(type_descriptor, expr, &val_temp)
Expression* p1 = Expression::make_type_descriptor(this->type_, loc);
Expression* ref = Expression::make_temporary_reference(val_temp, loc);
Expression* p3 = Expression::make_unary(OPERATOR_AND, ref, loc);
Expression* call = Runtime::make_call(code, loc, 3, p1, this->expr_, p3);
Statement* s = Statement::make_assignment(this->ok_, call, loc);
b->add_statement(s);
// val = val_temp
ref = Expression::make_temporary_reference(val_temp, loc);
s = Statement::make_assignment(this->val_, ref, loc);
b->add_statement(s);
}
// Dump the AST representation for a tuple type guard statement.
void
Tuple_type_guard_assignment_statement::do_dump_statement(
Ast_dump_context* ast_dump_context) const
{
ast_dump_context->print_indent();
ast_dump_context->dump_expression(this->val_);
ast_dump_context->ostream() << ", ";
ast_dump_context->dump_expression(this->ok_);
ast_dump_context->ostream() << " = ";
ast_dump_context->dump_expression(this->expr_);
ast_dump_context->ostream() << " . ";
ast_dump_context->dump_type(this->type_);
ast_dump_context->ostream() << std::endl;
}
// Make an assignment from a type guard to a pair of variables.
Statement*
Statement::make_tuple_type_guard_assignment(Expression* val, Expression* ok,
Expression* expr, Type* type,
source_location location)
{
return new Tuple_type_guard_assignment_statement(val, ok, expr, type,
location);
}
// An expression statement.
class Expression_statement : public Statement
{
public:
Expression_statement(Expression* expr, bool is_ignored)
: Statement(STATEMENT_EXPRESSION, expr->location()),
expr_(expr), is_ignored_(is_ignored)
{ }
Expression*
expr()
{ return this->expr_; }
protected:
int
do_traverse(Traverse* traverse)
{ return this->traverse_expression(traverse, &this->expr_); }
void
do_determine_types()
{ this->expr_->determine_type_no_context(); }
void
do_check_types(Gogo*);
bool
do_may_fall_through() const;
Bstatement*
do_get_backend(Translate_context* context);
void
do_dump_statement(Ast_dump_context*) const;
private:
Expression* expr_;
// Whether the value of this expression is being explicitly ignored.
bool is_ignored_;
};
// Check the types of an expression statement. The only check we do
// is to possibly give an error about discarding the value of the
// expression.
void
Expression_statement::do_check_types(Gogo*)
{
if (!this->is_ignored_)
this->expr_->discarding_value();
}
// An expression statement may fall through unless it is a call to a
// function which does not return.
bool
Expression_statement::do_may_fall_through() const
{
const Call_expression* call = this->expr_->call_expression();
if (call != NULL)
{
const Expression* fn = call->fn();
const Func_expression* fe = fn->func_expression();
if (fe != NULL)
{
const Named_object* no = fe->named_object();
Function_type* fntype;
if (no->is_function())
fntype = no->func_value()->type();
else if (no->is_function_declaration())
fntype = no->func_declaration_value()->type();
else
fntype = NULL;
// The builtin function panic does not return.
if (fntype != NULL && fntype->is_builtin() && no->name() == "panic")
return false;
}
}
return true;
}
// Convert to backend representation.
Bstatement*
Expression_statement::do_get_backend(Translate_context* context)
{
tree expr_tree = this->expr_->get_tree(context);
return context->backend()->expression_statement(tree_to_expr(expr_tree));
}
// Dump the AST representation for an expression statement
void
Expression_statement::do_dump_statement(Ast_dump_context* ast_dump_context)
const
{
ast_dump_context->print_indent();
ast_dump_context->dump_expression(expr_);
ast_dump_context->ostream() << std::endl;
}
// Make an expression statement from an Expression.
Statement*
Statement::make_statement(Expression* expr, bool is_ignored)
{
return new Expression_statement(expr, is_ignored);
}
// A block statement--a list of statements which may include variable
// definitions.
class Block_statement : public Statement
{
public:
Block_statement(Block* block, source_location location)
: Statement(STATEMENT_BLOCK, location),
block_(block)
{ }
protected:
int
do_traverse(Traverse* traverse)
{ return this->block_->traverse(traverse); }
void
do_determine_types()
{ this->block_->determine_types(); }
bool
do_may_fall_through() const
{ return this->block_->may_fall_through(); }
Bstatement*
do_get_backend(Translate_context* context);
void
do_dump_statement(Ast_dump_context*) const;
private:
Block* block_;
};
// Convert a block to the backend representation of a statement.
Bstatement*
Block_statement::do_get_backend(Translate_context* context)
{
Bblock* bblock = this->block_->get_backend(context);
return context->backend()->block_statement(bblock);
}
// Dump the AST for a block statement
void
Block_statement::do_dump_statement(Ast_dump_context*) const
{
// block statement braces are dumped when traversing.
}
// Make a block statement.
Statement*
Statement::make_block_statement(Block* block, source_location location)
{
return new Block_statement(block, location);
}
// An increment or decrement statement.
class Inc_dec_statement : public Statement
{
public:
Inc_dec_statement(bool is_inc, Expression* expr)
: Statement(STATEMENT_INCDEC, expr->location()),
expr_(expr), is_inc_(is_inc)
{ }
protected:
int
do_traverse(Traverse* traverse)
{ return this->traverse_expression(traverse, &this->expr_); }
bool
do_traverse_assignments(Traverse_assignments*)
{ go_unreachable(); }
Statement*
do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
Bstatement*
do_get_backend(Translate_context*)
{ go_unreachable(); }
void
do_dump_statement(Ast_dump_context*) const;
private:
// The l-value to increment or decrement.
Expression* expr_;
// Whether to increment or decrement.
bool is_inc_;
};
// Lower to += or -=.
Statement*
Inc_dec_statement::do_lower(Gogo*, Named_object*, Block*, Statement_inserter*)
{
source_location loc = this->location();
mpz_t oval;
mpz_init_set_ui(oval, 1UL);
Expression* oexpr = Expression::make_integer(&oval, NULL, loc);
mpz_clear(oval);
Operator op = this->is_inc_ ? OPERATOR_PLUSEQ : OPERATOR_MINUSEQ;
return Statement::make_assignment_operation(op, this->expr_, oexpr, loc);
}
// Dump the AST representation for a inc/dec statement.
void
Inc_dec_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
{
ast_dump_context->print_indent();
ast_dump_context->dump_expression(expr_);
ast_dump_context->ostream() << (is_inc_? "++": "--") << std::endl;
}
// Make an increment statement.
Statement*
Statement::make_inc_statement(Expression* expr)
{
return new Inc_dec_statement(true, expr);
}
// Make a decrement statement.
Statement*
Statement::make_dec_statement(Expression* expr)
{
return new Inc_dec_statement(false, expr);
}
// Class Thunk_statement. This is the base class for go and defer
// statements.
// Constructor.
Thunk_statement::Thunk_statement(Statement_classification classification,
Call_expression* call,
source_location location)
: Statement(classification, location),
call_(call), struct_type_(NULL)
{
}
// Return whether this is a simple statement which does not require a
// thunk.
bool
Thunk_statement::is_simple(Function_type* fntype) const
{
// We need a thunk to call a method, or to pass a variable number of
// arguments.
if (fntype->is_method() || fntype->is_varargs())
return false;
// A defer statement requires a thunk to set up for whether the
// function can call recover.
if (this->classification() == STATEMENT_DEFER)
return false;
// We can only permit a single parameter of pointer type.
const Typed_identifier_list* parameters = fntype->parameters();
if (parameters != NULL
&& (parameters->size() > 1
|| (parameters->size() == 1
&& parameters->begin()->type()->points_to() == NULL)))
return false;
// If the function returns multiple values, or returns a type other
// than integer, floating point, or pointer, then it may get a
// hidden first parameter, in which case we need the more
// complicated approach. This is true even though we are going to
// ignore the return value.
const Typed_identifier_list* results = fntype->results();
if (results != NULL
&& (results->size() > 1
|| (results->size() == 1
&& !results->begin()->type()->is_basic_type()
&& results->begin()->type()->points_to() == NULL)))
return false;
// If this calls something which is not a simple function, then we
// need a thunk.
Expression* fn = this->call_->call_expression()->fn();
if (fn->interface_field_reference_expression() != NULL)
return false;
return true;
}
// Traverse a thunk statement.
int
Thunk_statement::do_traverse(Traverse* traverse)
{
return this->traverse_expression(traverse, &this->call_);
}
// We implement traverse_assignment for a thunk statement because it
// effectively copies the function call.
bool
Thunk_statement::do_traverse_assignments(Traverse_assignments* tassign)
{
Expression* fn = this->call_->call_expression()->fn();
Expression* fn2 = fn;
tassign->value(&fn2, true, false);
return true;
}
// Determine types in a thunk statement.
void
Thunk_statement::do_determine_types()
{
this->call_->determine_type_no_context();
// Now that we know the types of the call, build the struct used to
// pass parameters.
Call_expression* ce = this->call_->call_expression();
if (ce == NULL)
return;
Function_type* fntype = ce->get_function_type();
if (fntype != NULL && !this->is_simple(fntype))
this->struct_type_ = this->build_struct(fntype);
}
// Check types in a thunk statement.
void
Thunk_statement::do_check_types(Gogo*)
{
Call_expression* ce = this->call_->call_expression();
if (ce == NULL)
{
if (!this->call_->is_error_expression())
this->report_error("expected call expression");
return;
}
}
// The Traverse class used to find and simplify thunk statements.
class Simplify_thunk_traverse : public Traverse
{
public:
Simplify_thunk_traverse(Gogo* gogo)
: Traverse(traverse_functions | traverse_blocks),
gogo_(gogo), function_(NULL)
{ }
int
function(Named_object*);
int
block(Block*);
private:
// General IR.
Gogo* gogo_;
// The function we are traversing.
Named_object* function_;
};
// Keep track of the current function while looking for thunks.
int
Simplify_thunk_traverse::function(Named_object* no)
{
go_assert(this->function_ == NULL);
this->function_ = no;
int t = no->func_value()->traverse(this);
this->function_ = NULL;
if (t == TRAVERSE_EXIT)
return t;
return TRAVERSE_SKIP_COMPONENTS;
}
// Look for thunks in a block.
int
Simplify_thunk_traverse::block(Block* b)
{
// The parser ensures that thunk statements always appear at the end
// of a block.
if (b->statements()->size() < 1)
return TRAVERSE_CONTINUE;
Thunk_statement* stat = b->statements()->back()->thunk_statement();
if (stat == NULL)
return TRAVERSE_CONTINUE;
if (stat->simplify_statement(this->gogo_, this->function_, b))
return TRAVERSE_SKIP_COMPONENTS;
return TRAVERSE_CONTINUE;
}
// Simplify all thunk statements.
void
Gogo::simplify_thunk_statements()
{
Simplify_thunk_traverse thunk_traverse(this);
this->traverse(&thunk_traverse);
}
// Return true if the thunk function is a constant, which means that
// it does not need to be passed to the thunk routine.
bool
Thunk_statement::is_constant_function() const
{
Call_expression* ce = this->call_->call_expression();
Function_type* fntype = ce->get_function_type();
if (fntype == NULL)
{
go_assert(saw_errors());
return false;
}
if (fntype->is_builtin())
return true;
Expression* fn = ce->fn();
if (fn->func_expression() != NULL)
return fn->func_expression()->closure() == NULL;
if (fn->interface_field_reference_expression() != NULL)
return true;
return false;
}
// Simplify complex thunk statements into simple ones. A complicated
// thunk statement is one which takes anything other than zero
// parameters or a single pointer parameter. We rewrite it into code
// which allocates a struct, stores the parameter values into the
// struct, and does a simple go or defer statement which passes the
// struct to a thunk. The thunk does the real call.
bool
Thunk_statement::simplify_statement(Gogo* gogo, Named_object* function,
Block* block)
{
if (this->classification() == STATEMENT_ERROR)
return false;
if (this->call_->is_error_expression())
return false;
if (this->classification() == STATEMENT_DEFER)
{
// Make sure that the defer stack exists for the function. We
// will use when converting this statement to the backend
// representation, but we want it to exist when we start
// converting the function.
function->func_value()->defer_stack(this->location());
}
Call_expression* ce = this->call_->call_expression();
Function_type* fntype = ce->get_function_type();
if (fntype == NULL)
{
go_assert(saw_errors());
this->set_is_error();
return false;
}
if (this->is_simple(fntype))
return false;
Expression* fn = ce->fn();
Interface_field_reference_expression* interface_method =
fn->interface_field_reference_expression();
source_location location = this->location();
std::string thunk_name = Gogo::thunk_name();
// Build the thunk.
this->build_thunk(gogo, thunk_name);
// Generate code to call the thunk.
// Get the values to store into the struct which is the single
// argument to the thunk.
Expression_list* vals = new Expression_list();
if (!this->is_constant_function())
vals->push_back(fn);
if (interface_method != NULL)
vals->push_back(interface_method->expr());
if (ce->args() != NULL)
{
for (Expression_list::const_iterator p = ce->args()->begin();
p != ce->args()->end();
++p)
vals->push_back(*p);
}
// Build the struct.
Expression* constructor =
Expression::make_struct_composite_literal(this->struct_type_, vals,
location);
// Allocate the initialized struct on the heap.
constructor = Expression::make_heap_composite(constructor, location);
// Look up the thunk.
Named_object* named_thunk = gogo->lookup(thunk_name, NULL);
go_assert(named_thunk != NULL && named_thunk->is_function());
// Build the call.
Expression* func = Expression::make_func_reference(named_thunk, NULL,
location);
Expression_list* params = new Expression_list();
params->push_back(constructor);
Call_expression* call = Expression::make_call(func, params, false, location);
// Build the simple go or defer statement.
Statement* s;
if (this->classification() == STATEMENT_GO)
s = Statement::make_go_statement(call, location);
else if (this->classification() == STATEMENT_DEFER)
s = Statement::make_defer_statement(call, location);
else
go_unreachable();
// The current block should end with the go statement.
go_assert(block->statements()->size() >= 1);
go_assert(block->statements()->back() == this);
block->replace_statement(block->statements()->size() - 1, s);
// We already ran the determine_types pass, so we need to run it now
// for the new statement.
s->determine_types();
// Sanity check.
gogo->check_types_in_block(block);
// Return true to tell the block not to keep looking at statements.
return true;
}
// Set the name to use for thunk parameter N.
void
Thunk_statement::thunk_field_param(int n, char* buf, size_t buflen)
{
snprintf(buf, buflen, "a%d", n);
}
// Build a new struct type to hold the parameters for a complicated
// thunk statement. FNTYPE is the type of the function call.
Struct_type*
Thunk_statement::build_struct(Function_type* fntype)
{
source_location location = this->location();
Struct_field_list* fields = new Struct_field_list();
Call_expression* ce = this->call_->call_expression();
Expression* fn = ce->fn();
if (!this->is_constant_function())
{
// The function to call.
fields->push_back(Struct_field(Typed_identifier("fn", fntype,
location)));
}
// If this thunk statement calls a method on an interface, we pass
// the interface object to the thunk.
Interface_field_reference_expression* interface_method =
fn->interface_field_reference_expression();
if (interface_method != NULL)
{
Typed_identifier tid("object", interface_method->expr()->type(),
location);
fields->push_back(Struct_field(tid));
}
// The predeclared recover function has no argument. However, we
// add an argument when building recover thunks. Handle that here.
if (ce->is_recover_call())
{
fields->push_back(Struct_field(Typed_identifier("can_recover",
Type::lookup_bool_type(),
location)));
}
const Expression_list* args = ce->args();
if (args != NULL)
{
int i = 0;
for (Expression_list::const_iterator p = args->begin();
p != args->end();
++p, ++i)
{
char buf[50];
this->thunk_field_param(i, buf, sizeof buf);
fields->push_back(Struct_field(Typed_identifier(buf, (*p)->type(),
location)));
}
}
return Type::make_struct_type(fields, location);
}
// Build the thunk we are going to call. This is a brand new, albeit
// artificial, function.
void
Thunk_statement::build_thunk(Gogo* gogo, const std::string& thunk_name)
{
source_location location = this->location();
Call_expression* ce = this->call_->call_expression();
bool may_call_recover = false;
if (this->classification() == STATEMENT_DEFER)
{
Func_expression* fn = ce->fn()->func_expression();
if (fn == NULL)
may_call_recover = true;
else
{
const Named_object* no = fn->named_object();
if (!no->is_function())
may_call_recover = true;
else
may_call_recover = no->func_value()->calls_recover();
}
}
// Build the type of the thunk. The thunk takes a single parameter,
// which is a pointer to the special structure we build.
const char* const parameter_name = "__go_thunk_parameter";
Typed_identifier_list* thunk_parameters = new Typed_identifier_list();
Type* pointer_to_struct_type = Type::make_pointer_type(this->struct_type_);
thunk_parameters->push_back(Typed_identifier(parameter_name,
pointer_to_struct_type,
location));
Typed_identifier_list* thunk_results = NULL;
if (may_call_recover)
{
// When deferring a function which may call recover, add a
// return value, to disable tail call optimizations which will
// break the way we check whether recover is permitted.
thunk_results = new Typed_identifier_list();
thunk_results->push_back(Typed_identifier("", Type::lookup_bool_type(),
location));
}
Function_type* thunk_type = Type::make_function_type(NULL, thunk_parameters,
thunk_results,
location);
// Start building the thunk.
Named_object* function = gogo->start_function(thunk_name, thunk_type, true,
location);
gogo->start_block(location);
// For a defer statement, start with a call to
// __go_set_defer_retaddr. */
Label* retaddr_label = NULL;
if (may_call_recover)
{
retaddr_label = gogo->add_label_reference("retaddr", location, false);
Expression* arg = Expression::make_label_addr(retaddr_label, location);
Expression* call = Runtime::make_call(Runtime::SET_DEFER_RETADDR,
location, 1, arg);
// This is a hack to prevent the middle-end from deleting the
// label.
gogo->start_block(location);
gogo->add_statement(Statement::make_goto_statement(retaddr_label,
location));
Block* then_block = gogo->finish_block(location);
then_block->determine_types();
Statement* s = Statement::make_if_statement(call, then_block, NULL,
location);
s->determine_types();
gogo->add_statement(s);
}
// Get a reference to the parameter.
Named_object* named_parameter = gogo->lookup(parameter_name, NULL);
go_assert(named_parameter != NULL && named_parameter->is_variable());
// Build the call. Note that the field names are the same as the
// ones used in build_struct.
Expression* thunk_parameter = Expression::make_var_reference(named_parameter,
location);
thunk_parameter = Expression::make_unary(OPERATOR_MULT, thunk_parameter,
location);
Interface_field_reference_expression* interface_method =
ce->fn()->interface_field_reference_expression();
Expression* func_to_call;
unsigned int next_index;
if (this->is_constant_function())
{
func_to_call = ce->fn();
next_index = 0;
}
else
{
func_to_call = Expression::make_field_reference(thunk_parameter,
0, location);
next_index = 1;
}
if (interface_method != NULL)
{
// The main program passes the interface object.
go_assert(next_index == 0);
Expression* r = Expression::make_field_reference(thunk_parameter, 0,
location);
const std::string& name(interface_method->name());
func_to_call = Expression::make_interface_field_reference(r, name,
location);
next_index = 1;
}
Expression_list* call_params = new Expression_list();
const Struct_field_list* fields = this->struct_type_->fields();
Struct_field_list::const_iterator p = fields->begin();
for (unsigned int i = 0; i < next_index; ++i)
++p;
bool is_recover_call = ce->is_recover_call();
Expression* recover_arg = NULL;
for (; p != fields->end(); ++p, ++next_index)
{
Expression* thunk_param = Expression::make_var_reference(named_parameter,
location);
thunk_param = Expression::make_unary(OPERATOR_MULT, thunk_param,
location);
Expression* param = Expression::make_field_reference(thunk_param,
next_index,
location);
if (!is_recover_call)
call_params->push_back(param);
else
{
go_assert(call_params->empty());
recover_arg = param;
}
}
if (call_params->empty())
{
delete call_params;
call_params = NULL;
}
Call_expression* call = Expression::make_call(func_to_call, call_params,
false, location);
// This call expression was already lowered before entering the
// thunk statement. Don't try to lower varargs again, as that will
// cause confusion for, e.g., method calls which already have a
// receiver parameter.
call->set_varargs_are_lowered();
Statement* call_statement = Statement::make_statement(call, true);
gogo->add_statement(call_statement);
// If this is a defer statement, the label comes immediately after
// the call.
if (may_call_recover)
{
gogo->add_label_definition("retaddr", location);
Expression_list* vals = new Expression_list();
vals->push_back(Expression::make_boolean(false, location));
gogo->add_statement(Statement::make_return_statement(vals, location));
}
Block* b = gogo->finish_block(location);
gogo->add_block(b, location);
gogo->lower_block(function, b);
// We already ran the determine_types pass, so we need to run it
// just for the call statement now. The other types are known.
call_statement->determine_types();
if (may_call_recover || recover_arg != NULL)
{
// Dig up the call expression, which may have been changed
// during lowering.
go_assert(call_statement->classification() == STATEMENT_EXPRESSION);
Expression_statement* es =
static_cast<Expression_statement*>(call_statement);
Call_expression* ce = es->expr()->call_expression();
go_assert(ce != NULL);
if (may_call_recover)
ce->set_is_deferred();
if (recover_arg != NULL)
ce->set_recover_arg(recover_arg);
}
// That is all the thunk has to do.
gogo->finish_function(location);
}
// Get the function and argument expressions.
bool
Thunk_statement::get_fn_and_arg(Expression** pfn, Expression** parg)
{
if (this->call_->is_error_expression())
return false;
Call_expression* ce = this->call_->call_expression();
*pfn = ce->fn();
const Expression_list* args = ce->args();
if (args == NULL || args->empty())
*parg = Expression::make_nil(this->location());
else
{
go_assert(args->size() == 1);
*parg = args->front();
}
return true;
}
// Class Go_statement.
Bstatement*
Go_statement::do_get_backend(Translate_context* context)
{
Expression* fn;
Expression* arg;
if (!this->get_fn_and_arg(&fn, &arg))
return context->backend()->error_statement();
Expression* call = Runtime::make_call(Runtime::GO, this->location(), 2,
fn, arg);
tree call_tree = call->get_tree(context);
Bexpression* call_bexpr = tree_to_expr(call_tree);
return context->backend()->expression_statement(call_bexpr);
}
// Dump the AST representation for go statement.
void
Go_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
{
ast_dump_context->print_indent();
ast_dump_context->ostream() << "go ";
ast_dump_context->dump_expression(this->call());
ast_dump_context->ostream() << std::endl;
}
// Make a go statement.
Statement*
Statement::make_go_statement(Call_expression* call, source_location location)
{
return new Go_statement(call, location);
}
// Class Defer_statement.
Bstatement*
Defer_statement::do_get_backend(Translate_context* context)
{
Expression* fn;
Expression* arg;
if (!this->get_fn_and_arg(&fn, &arg))
return context->backend()->error_statement();
source_location loc = this->location();
Expression* ds = context->function()->func_value()->defer_stack(loc);
Expression* call = Runtime::make_call(Runtime::DEFER, loc, 3,
ds, fn, arg);
tree call_tree = call->get_tree(context);
Bexpression* call_bexpr = tree_to_expr(call_tree);
return context->backend()->expression_statement(call_bexpr);
}
// Dump the AST representation for defer statement.
void
Defer_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
{
ast_dump_context->print_indent();
ast_dump_context->ostream() << "defer ";
ast_dump_context->dump_expression(this->call());
ast_dump_context->ostream() << std::endl;
}
// Make a defer statement.
Statement*
Statement::make_defer_statement(Call_expression* call,
source_location location)
{
return new Defer_statement(call, location);
}
// Class Return_statement.
// Traverse assignments. We treat each return value as a top level
// RHS in an expression.
bool
Return_statement::do_traverse_assignments(Traverse_assignments* tassign)
{
Expression_list* vals = this->vals_;
if (vals != NULL)
{
for (Expression_list::iterator p = vals->begin();
p != vals->end();
++p)
tassign->value(&*p, true, true);
}
return true;
}
// Lower a return statement. If we are returning a function call
// which returns multiple values which match the current function,
// split up the call's results. If the return statement lists
// explicit values, implement this statement by assigning the values
// to the result variables and change this statement to a naked
// return. This lets panic/recover work correctly.
Statement*
Return_statement::do_lower(Gogo*, Named_object* function, Block* enclosing,
Statement_inserter*)
{
if (this->is_lowered_)
return this;
Expression_list* vals = this->vals_;
this->vals_ = NULL;
this->is_lowered_ = true;
source_location loc = this->location();
size_t vals_count = vals == NULL ? 0 : vals->size();
Function::Results* results = function->func_value()->result_variables();
size_t results_count = results == NULL ? 0 : results->size();
if (vals_count == 0)
{
if (results_count > 0 && !function->func_value()->results_are_named())
{
this->report_error(_("not enough arguments to return"));
return this;
}
return this;
}
if (results_count == 0)
{
this->report_error(_("return with value in function "
"with no return type"));
return this;
}
// If the current function has multiple return values, and we are
// returning a single call expression, split up the call expression.
if (results_count > 1
&& vals->size() == 1
&& vals->front()->call_expression() != NULL)
{
Call_expression* call = vals->front()->call_expression();
delete vals;
vals = new Expression_list;
for (size_t i = 0; i < results_count; ++i)
vals->push_back(Expression::make_call_result(call, i));
vals_count = results_count;
}
if (vals_count < results_count)
{
this->report_error(_("not enough arguments to return"));
return this;
}
if (vals_count > results_count)
{
this->report_error(_("too many values in return statement"));
return this;
}
Block* b = new Block(enclosing, loc);
Expression_list* lhs = new Expression_list();
Expression_list* rhs = new Expression_list();
Expression_list::const_iterator pe = vals->begin();
int i = 1;
for (Function::Results::const_iterator pr = results->begin();
pr != results->end();
++pr, ++pe, ++i)
{
Named_object* rv = *pr;
Expression* e = *pe;
// Check types now so that we give a good error message. The
// result type is known. We determine the expression type
// early.
Type *rvtype = rv->result_var_value()->type();
Type_context type_context(rvtype, false);
e->determine_type(&type_context);
std::string reason;
bool ok;
if (this->are_hidden_fields_ok_)
ok = Type::are_assignable_hidden_ok(rvtype, e->type(), &reason);
else
ok = Type::are_assignable(rvtype, e->type(), &reason);
if (ok)
{
Expression* ve = Expression::make_var_reference(rv, e->location());
lhs->push_back(ve);
rhs->push_back(e);
}
else
{
if (reason.empty())
error_at(e->location(), "incompatible type for return value %d", i);
else
error_at(e->location(),
"incompatible type for return value %d (%s)",
i, reason.c_str());
}
}
go_assert(lhs->size() == rhs->size());
if (lhs->empty())
;
else if (lhs->size() == 1)
{
Statement* s = Statement::make_assignment(lhs->front(), rhs->front(),
loc);
if (this->are_hidden_fields_ok_)
{
Assignment_statement* as = static_cast<Assignment_statement*>(s);
as->set_hidden_fields_are_ok();
}
b->add_statement(s);
delete lhs;
delete rhs;
}
else
{
Statement* s = Statement::make_tuple_assignment(lhs, rhs, loc);
if (this->are_hidden_fields_ok_)
{
Tuple_assignment_statement* tas =
static_cast<Tuple_assignment_statement*>(s);
tas->set_hidden_fields_are_ok();
}
b->add_statement(s);
}
b->add_statement(this);
delete vals;
return Statement::make_block_statement(b, loc);
}
// Convert a return statement to the backend representation.
Bstatement*
Return_statement::do_get_backend(Translate_context* context)
{
source_location loc = this->location();
Function* function = context->function()->func_value();
tree fndecl = function->get_decl();
Function::Results* results = function->result_variables();
std::vector<Bexpression*> retvals;
if (results != NULL && !results->empty())
{
retvals.reserve(results->size());
for (Function::Results::const_iterator p = results->begin();
p != results->end();
p++)
{
Expression* vr = Expression::make_var_reference(*p, loc);
retvals.push_back(tree_to_expr(vr->get_tree(context)));
}
}
return context->backend()->return_statement(tree_to_function(fndecl),
retvals, loc);
}
// Dump the AST representation for a return statement.
void
Return_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
{
ast_dump_context->print_indent();
ast_dump_context->ostream() << "return " ;
ast_dump_context->dump_expression_list(this->vals_);
ast_dump_context->ostream() << std::endl;
}
// Make a return statement.
Return_statement*
Statement::make_return_statement(Expression_list* vals,
source_location location)
{
return new Return_statement(vals, location);
}
// A break or continue statement.
class Bc_statement : public Statement
{
public:
Bc_statement(bool is_break, Unnamed_label* label, source_location location)
: Statement(STATEMENT_BREAK_OR_CONTINUE, location),
label_(label), is_break_(is_break)
{ }
bool
is_break() const
{ return this->is_break_; }
protected:
int
do_traverse(Traverse*)
{ return TRAVERSE_CONTINUE; }
bool
do_may_fall_through() const
{ return false; }
Bstatement*
do_get_backend(Translate_context* context)
{ return this->label_->get_goto(context, this->location()); }
void
do_dump_statement(Ast_dump_context*) const;
private:
// The label that this branches to.
Unnamed_label* label_;
// True if this is "break", false if it is "continue".
bool is_break_;
};
// Dump the AST representation for a break/continue statement
void
Bc_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
{
ast_dump_context->print_indent();
ast_dump_context->ostream() << (this->is_break_ ? "break" : "continue");
if (this->label_ != NULL)
{
ast_dump_context->ostream() << " ";
ast_dump_context->dump_label_name(this->label_);
}
ast_dump_context->ostream() << std::endl;
}
// Make a break statement.
Statement*
Statement::make_break_statement(Unnamed_label* label, source_location location)
{
return new Bc_statement(true, label, location);
}
// Make a continue statement.
Statement*
Statement::make_continue_statement(Unnamed_label* label,
source_location location)
{
return new Bc_statement(false, label, location);
}
// A goto statement.
class Goto_statement : public Statement
{
public:
Goto_statement(Label* label, source_location location)
: Statement(STATEMENT_GOTO, location),
label_(label)
{ }
protected:
int
do_traverse(Traverse*)
{ return TRAVERSE_CONTINUE; }
void
do_check_types(Gogo*);
bool
do_may_fall_through() const
{ return false; }
Bstatement*
do_get_backend(Translate_context*);
void
do_dump_statement(Ast_dump_context*) const;
private:
Label* label_;
};
// Check types for a label. There aren't any types per se, but we use
// this to give an error if the label was never defined.
void
Goto_statement::do_check_types(Gogo*)
{
if (!this->label_->is_defined())
{
error_at(this->location(), "reference to undefined label %qs",
Gogo::message_name(this->label_->name()).c_str());
this->set_is_error();
}
}
// Convert the goto statement to the backend representation.
Bstatement*
Goto_statement::do_get_backend(Translate_context* context)
{
Blabel* blabel = this->label_->get_backend_label(context);
return context->backend()->goto_statement(blabel, this->location());
}
// Dump the AST representation for a goto statement.
void
Goto_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
{
ast_dump_context->print_indent();
ast_dump_context->ostream() << "goto " << this->label_->name() << std::endl;
}
// Make a goto statement.
Statement*
Statement::make_goto_statement(Label* label, source_location location)
{
return new Goto_statement(label, location);
}
// A goto statement to an unnamed label.
class Goto_unnamed_statement : public Statement
{
public:
Goto_unnamed_statement(Unnamed_label* label, source_location location)
: Statement(STATEMENT_GOTO_UNNAMED, location),
label_(label)
{ }
protected:
int
do_traverse(Traverse*)
{ return TRAVERSE_CONTINUE; }
bool
do_may_fall_through() const
{ return false; }
Bstatement*
do_get_backend(Translate_context* context)
{ return this->label_->get_goto(context, this->location()); }
void
do_dump_statement(Ast_dump_context*) const;
private:
Unnamed_label* label_;
};
// Dump the AST representation for an unnamed goto statement
void
Goto_unnamed_statement::do_dump_statement(
Ast_dump_context* ast_dump_context) const
{
ast_dump_context->print_indent();
ast_dump_context->ostream() << "goto ";
ast_dump_context->dump_label_name(this->label_);
ast_dump_context->ostream() << std::endl;
}
// Make a goto statement to an unnamed label.
Statement*
Statement::make_goto_unnamed_statement(Unnamed_label* label,
source_location location)
{
return new Goto_unnamed_statement(label, location);
}
// Class Label_statement.
// Traversal.
int
Label_statement::do_traverse(Traverse*)
{
return TRAVERSE_CONTINUE;
}
// Return the backend representation of the statement defining this
// label.
Bstatement*
Label_statement::do_get_backend(Translate_context* context)
{
Blabel* blabel = this->label_->get_backend_label(context);
return context->backend()->label_definition_statement(blabel);
}
// Dump the AST for a label definition statement.
void
Label_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
{
ast_dump_context->print_indent();
ast_dump_context->ostream() << this->label_->name() << ":" << std::endl;
}
// Make a label statement.
Statement*
Statement::make_label_statement(Label* label, source_location location)
{
return new Label_statement(label, location);
}
// An unnamed label statement.
class Unnamed_label_statement : public Statement
{
public:
Unnamed_label_statement(Unnamed_label* label)
: Statement(STATEMENT_UNNAMED_LABEL, label->location()),
label_(label)
{ }
protected:
int
do_traverse(Traverse*)
{ return TRAVERSE_CONTINUE; }
Bstatement*
do_get_backend(Translate_context* context)
{ return this->label_->get_definition(context); }
void
do_dump_statement(Ast_dump_context*) const;
private:
// The label.
Unnamed_label* label_;
};
// Dump the AST representation for an unnamed label definition statement.
void
Unnamed_label_statement::do_dump_statement(Ast_dump_context* ast_dump_context)
const
{
ast_dump_context->print_indent();
ast_dump_context->dump_label_name(this->label_);
ast_dump_context->ostream() << ":" << std::endl;
}
// Make an unnamed label statement.
Statement*
Statement::make_unnamed_label_statement(Unnamed_label* label)
{
return new Unnamed_label_statement(label);
}
// An if statement.
class If_statement : public Statement
{
public:
If_statement(Expression* cond, Block* then_block, Block* else_block,
source_location location)
: Statement(STATEMENT_IF, location),
cond_(cond), then_block_(then_block), else_block_(else_block)
{ }
protected:
int
do_traverse(Traverse*);
void
do_determine_types();
void
do_check_types(Gogo*);
bool
do_may_fall_through() const;
Bstatement*
do_get_backend(Translate_context*);
void
do_dump_statement(Ast_dump_context*) const;
private:
Expression* cond_;
Block* then_block_;
Block* else_block_;
};
// Traversal.
int
If_statement::do_traverse(Traverse* traverse)
{
if (this->traverse_expression(traverse, &this->cond_) == TRAVERSE_EXIT
|| this->then_block_->traverse(traverse) == TRAVERSE_EXIT)
return TRAVERSE_EXIT;
if (this->else_block_ != NULL)
{
if (this->else_block_->traverse(traverse) == TRAVERSE_EXIT)
return TRAVERSE_EXIT;
}
return TRAVERSE_CONTINUE;
}
void
If_statement::do_determine_types()
{
Type_context context(Type::lookup_bool_type(), false);
this->cond_->determine_type(&context);
this->then_block_->determine_types();
if (this->else_block_ != NULL)
this->else_block_->determine_types();
}
// Check types.
void
If_statement::do_check_types(Gogo*)
{
Type* type = this->cond_->type();
if (type->is_error())
this->set_is_error();
else if (!type->is_boolean_type())
this->report_error(_("expected boolean expression"));
}
// Whether the overall statement may fall through.
bool
If_statement::do_may_fall_through() const
{
return (this->else_block_ == NULL
|| this->then_block_->may_fall_through()
|| this->else_block_->may_fall_through());
}
// Get the backend representation.
Bstatement*
If_statement::do_get_backend(Translate_context* context)
{
go_assert(this->cond_->type()->is_boolean_type()
|| this->cond_->type()->is_error());
tree cond_tree = this->cond_->get_tree(context);
Bexpression* cond_expr = tree_to_expr(cond_tree);
Bblock* then_block = this->then_block_->get_backend(context);
Bblock* else_block = (this->else_block_ == NULL
? NULL
: this->else_block_->get_backend(context));
return context->backend()->if_statement(cond_expr, then_block,
else_block, this->location());
}
// Dump the AST representation for an if statement
void
If_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
{
ast_dump_context->print_indent();
ast_dump_context->ostream() << "if ";
ast_dump_context->dump_expression(this->cond_);
ast_dump_context->ostream() << std::endl;
if (ast_dump_context->dump_subblocks())
{
ast_dump_context->dump_block(this->then_block_);
if (this->else_block_ != NULL)
{
ast_dump_context->print_indent();
ast_dump_context->ostream() << "else" << std::endl;
ast_dump_context->dump_block(this->else_block_);
}
}
}
// Make an if statement.
Statement*
Statement::make_if_statement(Expression* cond, Block* then_block,
Block* else_block, source_location location)
{
return new If_statement(cond, then_block, else_block, location);
}
// Class Case_clauses::Hash_integer_value.
class Case_clauses::Hash_integer_value
{
public:
size_t
operator()(