#include <gmp.h>
-#ifndef ENABLE_BUILD_WITH_CXX
-extern "C"
-{
-#endif
-
-#include "intl.h"
-#include "tree.h"
-#include "gimple.h"
-#include "convert.h"
-#include "tree-iterator.h"
-#include "tree-flow.h"
-#include "real.h"
-
-#ifndef ENABLE_BUILD_WITH_CXX
-}
-#endif
-
#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.
{
if (expr_list == NULL)
return TRAVERSE_CONTINUE;
- if ((traverse->traverse_mask() & Traverse::traverse_expressions) == 0)
+ if ((traverse->traverse_mask()
+ & (Traverse::traverse_types | Traverse::traverse_expressions)) == 0)
return TRAVERSE_CONTINUE;
return expr_list->traverse(traverse);
}
return ret;
}
-// Get a tree for a Statement. This is really done by the child
-// class.
+// Convert a Statement to the backend representation. This is really
+// done by the child class.
-tree
-Statement::get_tree(Translate_context* context)
+Bstatement*
+Statement::get_backend(Translate_context* context)
{
if (this->classification_ == STATEMENT_ERROR)
- return error_mark_node;
-
- return this->do_get_tree(context);
+ return context->backend()->error_statement();
+ return this->do_get_backend(context);
}
-// Build tree nodes and set locations.
+// Dump AST representation for a statement to a dump context.
-tree
-Statement::build_stmt_1(int tree_code_value, tree node)
+void
+Statement::dump_statement(Ast_dump_context* ast_dump_context) const
{
- tree ret = build1(static_cast<tree_code>(tree_code_value),
- void_type_node, node);
- SET_EXPR_LOCATION(ret, this->location_);
- return ret;
+ this->do_dump_statement(ast_dump_context);
}
// Note that this statement is erroneous. This is called by children
do_traverse(Traverse*)
{ return TRAVERSE_CONTINUE; }
- tree
- do_get_tree(Translate_context*)
- { gcc_unreachable(); }
+ 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*
return true;
}
-// Return the tree for a variable declaration.
+// Lower the variable's initialization expression.
-tree
-Variable_declaration_statement::do_get_tree(Translate_context* context)
+Statement*
+Variable_declaration_statement::do_lower(Gogo* gogo, Named_object* function,
+ Block*, Statement_inserter* inserter)
{
- tree val = this->var_->get_tree(context->gogo(), context->function());
- if (val == error_mark_node || TREE_TYPE(val) == error_mark_node)
- return error_mark_node;
- Variable* variable = this->var_->var_value();
+ this->var_->var_value()->lower_init_expression(gogo, function, inserter);
+ return this;
+}
+
+// Convert a variable declaration to the backend representation.
- tree init = variable->get_init_tree(context->gogo(), context->function());
- if (init == error_mark_node)
- return error_mark_node;
+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 this variable lives on the heap, we need to allocate it now.
- if (!variable->is_in_heap())
+ if (!var->is_in_heap())
{
- DECL_INITIAL(val) = init;
- return this->build_stmt_1(DECL_EXPR, val);
+ go_assert(binit != NULL);
+ return context->backend()->init_statement(bvar, binit);
}
- else
+
+ // 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)
{
- gcc_assert(TREE_CODE(val) == INDIRECT_REF);
- tree decl = TREE_OPERAND(val, 0);
- gcc_assert(TREE_CODE(decl) == VAR_DECL);
- tree type = TREE_TYPE(decl);
- gcc_assert(POINTER_TYPE_P(type));
- tree size = TYPE_SIZE_UNIT(TREE_TYPE(type));
- tree space = context->gogo()->allocate_memory(variable->type(), size,
- this->location());
- space = fold_convert(TREE_TYPE(decl), space);
- DECL_INITIAL(decl) = space;
- return build2(COMPOUND_EXPR, void_type_node,
- this->build_stmt_1(DECL_EXPR, decl),
- build2(MODIFY_EXPR, void_type_node, val, init));
+ ast_dump_context->ostream() << "= ";
+ ast_dump_context->dump_expression(var->init());
}
+ ast_dump_context->ostream() << std::endl;
}
// Make a variable declaration.
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
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)
}
if (this->type_ == NULL)
- this->type_ = this->init_->type();
-
- if (this->type_->is_abstract())
- this->type_ = this->type_->make_non_abstract_type();
+ {
+ this->type_ = this->init_->type();
+ go_assert(!this->type_->is_abstract());
+ }
}
// Check types.
Temporary_statement::do_check_types(Gogo*)
{
if (this->type_ != NULL && this->init_ != NULL)
- gcc_assert(Type::are_assignable(this->type_, this->init_->type(), 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();
+ }
+ }
}
-// Return a tree.
+// Convert to backend representation.
-tree
-Temporary_statement::do_get_tree(Translate_context* context)
+Bstatement*
+Temporary_statement::do_get_backend(Translate_context* context)
{
- gcc_assert(this->decl_ == NULL_TREE);
- tree type_tree = this->type()->get_tree(context->gogo());
- if (type_tree == error_mark_node)
+ 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
{
- this->decl_ = error_mark_node;
- return error_mark_node;
+ 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));
}
- // We can only use create_tmp_var if the type is not addressable.
- if (!TREE_ADDRESSABLE(type_tree))
+
+ 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)
{
- this->decl_ = create_tmp_var(type_tree, "GOTMP");
- DECL_SOURCE_LOCATION(this->decl_) = this->location();
+ go_assert(saw_errors());
+ return context->backend()->error_variable();
}
- else
+ 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)
{
- gcc_assert(context->function() != NULL && context->block() != NULL);
- tree decl = build_decl(this->location(), VAR_DECL,
- create_tmp_var_name("GOTMP"),
- type_tree);
- DECL_ARTIFICIAL(decl) = 1;
- DECL_IGNORED_P(decl) = 1;
- TREE_USED(decl) = 1;
- gcc_assert(current_function_decl != NULL_TREE);
- DECL_CONTEXT(decl) = current_function_decl;
-
- // We have to add this variable to the block so that it winds up
- // in a BIND_EXPR.
- tree block_tree = context->block_tree();
- gcc_assert(block_tree != NULL_TREE);
- DECL_CHAIN(decl) = BLOCK_VARS(block_tree);
- BLOCK_VARS(block_tree) = decl;
-
- this->decl_ = decl;
+ ast_dump_context->ostream() << " ";
+ ast_dump_context->dump_type(this->type_);
}
if (this->init_ != NULL)
- DECL_INITIAL(this->decl_) =
- Expression::convert_for_assignment(context, this->type(),
- this->init_->type(),
- this->init_->get_tree(context),
- this->location());
- if (this->is_address_taken_)
- TREE_ADDRESSABLE(this->decl_) = 1;
- return this->build_stmt_1(DECL_EXPR, this->decl_);
+ {
+ 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.
Assignment_statement(Expression* lhs, Expression* rhs,
source_location location)
: Statement(STATEMENT_ASSIGNMENT, location),
- lhs_(lhs), rhs_(rhs)
+ 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);
void
do_check_types(Gogo*);
- tree
- do_get_tree(Translate_context*);
+ 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.
&& this->lhs_->map_index_expression() == NULL
&& !this->lhs_->is_sink_expression())
{
- if (!this->lhs_->type()->is_error_type())
+ 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;
- if (!Type::are_assignable(lhs_type, rhs_type, &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");
this->set_is_error();
}
- if (lhs_type->is_error_type()
- || rhs_type->is_error_type()
- || lhs_type->is_undefined()
- || rhs_type->is_undefined())
- {
- // Make sure we get the error for an undefined type.
- lhs_type->base();
- rhs_type->base();
- this->set_is_error();
- }
+ if (lhs_type->is_error() || rhs_type->is_error())
+ this->set_is_error();
}
-// Build a tree for an assignment statement.
+// Convert an assignment statement to the backend representation.
-tree
-Assignment_statement::do_get_tree(Translate_context* context)
+Bstatement*
+Assignment_statement::do_get_backend(Translate_context* context)
{
tree rhs_tree = this->rhs_->get_tree(context);
-
if (this->lhs_->is_sink_expression())
- return rhs_tree;
-
+ return context->backend()->expression_statement(tree_to_expr(rhs_tree));
tree lhs_tree = this->lhs_->get_tree(context);
-
- if (lhs_tree == error_mark_node || rhs_tree == error_mark_node)
- return error_mark_node;
-
rhs_tree = Expression::convert_for_assignment(context, this->lhs_->type(),
this->rhs_->type(), rhs_tree,
this->location());
- if (rhs_tree == error_mark_node)
- return error_mark_node;
+ return context->backend()->assignment_statement(tree_to_expr(lhs_tree),
+ tree_to_expr(rhs_tree),
+ this->location());
+}
- return fold_build2_loc(this->location(), MODIFY_EXPR, void_type_node,
- lhs_tree, rhs_tree);
+// 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.
bool
do_traverse_assignments(Traverse_assignments*)
- { gcc_unreachable(); }
+ { go_unreachable(); }
Statement*
- do_lower(Gogo*, Block*);
+ do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
+
+ Bstatement*
+ do_get_backend(Translate_context*)
+ { go_unreachable(); }
- tree
- do_get_tree(Translate_context*)
- { gcc_unreachable(); }
+ void
+ do_dump_statement(Ast_dump_context*) const;
private:
// The operator (OPERATOR_PLUSEQ, etc.).
// statement.
Statement*
-Assignment_operation_statement::do_lower(Gogo*, Block* enclosing)
+Assignment_operation_statement::do_lower(Gogo*, Named_object*,
+ Block* enclosing, Statement_inserter*)
{
source_location loc = this->location();
op = OPERATOR_BITCLEAR;
break;
default:
- gcc_unreachable();
+ go_unreachable();
}
Expression* binop = Expression::make_binary(op, lval, this->rhs_, 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*
Tuple_assignment_statement(Expression_list* lhs, Expression_list* rhs,
source_location location)
: Statement(STATEMENT_TUPLE_ASSIGNMENT, location),
- lhs_(lhs), rhs_(rhs)
+ 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*)
- { gcc_unreachable(); }
+ { go_unreachable(); }
Statement*
- do_lower(Gogo*, Block*);
+ do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
- tree
- do_get_tree(Translate_context*)
- { gcc_unreachable(); }
+ 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.
// up into a set of single assignments.
Statement*
-Tuple_assignment_statement::do_lower(Gogo*, Block* enclosing)
+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);
plhs != this->lhs_->end();
++plhs, ++prhs)
{
- gcc_assert(prhs != this->rhs_->end());
+ 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())
{
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);
}
- gcc_assert(prhs == this->rhs_->end());
+ go_assert(prhs == this->rhs_->end());
prhs = this->rhs_->begin();
std::vector<Temporary_statement*>::const_iterator ptemp = temps.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;
}
- gcc_assert(ptemp == temps.end());
+ 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*
bool
do_traverse_assignments(Traverse_assignments*)
- { gcc_unreachable(); }
+ { go_unreachable(); }
Statement*
- do_lower(Gogo*, Block*);
+ do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
- tree
- do_get_tree(Translate_context*)
- { gcc_unreachable(); }
+ 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.
// Lower a tuple map assignment.
Statement*
-Tuple_map_assignment_statement::do_lower(Gogo*, Block* enclosing)
+Tuple_map_assignment_statement::do_lower(Gogo*, Named_object*,
+ Block* enclosing, Statement_inserter*)
{
source_location loc = this->location();
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);
Statement::make_temporary(Type::lookup_bool_type(), NULL, loc);
b->add_statement(present_temp);
- // func mapaccess2(hmap map[k]v, key *k, val *v) bool
- source_location bloc = BUILTINS_LOCATION;
- Typed_identifier_list* param_types = new Typed_identifier_list();
- param_types->push_back(Typed_identifier("hmap", map_type, bloc));
- Type* pkey_type = Type::make_pointer_type(map_type->key_type());
- param_types->push_back(Typed_identifier("key", pkey_type, bloc));
- Type* pval_type = Type::make_pointer_type(map_type->val_type());
- param_types->push_back(Typed_identifier("val", pval_type, bloc));
-
- Typed_identifier_list* ret_types = new Typed_identifier_list();
- ret_types->push_back(Typed_identifier("", Type::make_boolean_type(), bloc));
-
- Function_type* fntype = Type::make_function_type(NULL, param_types,
- ret_types, bloc);
- Named_object* mapaccess2 =
- Named_object::make_function_declaration("mapaccess2", NULL, fntype, bloc);
- mapaccess2->func_declaration_value()->set_asm_name("runtime.mapaccess2");
-
// present_temp = mapaccess2(MAP, &key_temp, &val_temp)
- Expression* func = Expression::make_func_reference(mapaccess2, NULL, loc);
- Expression_list* params = new Expression_list();
- params->push_back(map_index->map());
- Expression* ref = Expression::make_temporary_reference(key_temp, loc);
- params->push_back(Expression::make_unary(OPERATOR_AND, ref, loc));
+ Temporary_reference_expression* ref =
+ Expression::make_temporary_reference(key_temp, loc);
+ Expression* a1 = Expression::make_unary(OPERATOR_AND, ref, loc);
ref = Expression::make_temporary_reference(val_temp, loc);
- params->push_back(Expression::make_unary(OPERATOR_AND, ref, loc));
- Expression* call = Expression::make_call(func, params, false, loc);
+ Expression* a2 = Expression::make_unary(OPERATOR_AND, ref, loc);
+ Expression* call = Runtime::make_call(Runtime::MAPACCESS2, loc, 3,
+ map_index->map(), a1, a2);
ref = Expression::make_temporary_reference(present_temp, loc);
+ ref->set_is_lvalue();
Statement* s = Statement::make_assignment(ref, call, 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*
bool
do_traverse_assignments(Traverse_assignments*)
- { gcc_unreachable(); }
+ { go_unreachable(); }
Statement*
- do_lower(Gogo*, Block*);
+ do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
- tree
- do_get_tree(Translate_context*)
- { gcc_unreachable(); }
+ 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.
// Lower a map assignment to a function call.
Statement*
-Map_assignment_statement::do_lower(Gogo*, Block* enclosing)
+Map_assignment_statement::do_lower(Gogo*, Named_object*, Block* enclosing,
+ Statement_inserter*)
{
source_location loc = this->location();
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);
Statement::make_temporary(map_type->val_type(), this->val_, loc);
b->add_statement(val_temp);
- // func mapassign2(hmap map[k]v, key *k, val *v, p)
- source_location bloc = BUILTINS_LOCATION;
- Typed_identifier_list* param_types = new Typed_identifier_list();
- param_types->push_back(Typed_identifier("hmap", map_type, bloc));
- Type* pkey_type = Type::make_pointer_type(map_type->key_type());
- param_types->push_back(Typed_identifier("key", pkey_type, bloc));
- Type* pval_type = Type::make_pointer_type(map_type->val_type());
- param_types->push_back(Typed_identifier("val", pval_type, bloc));
- param_types->push_back(Typed_identifier("p", Type::lookup_bool_type(), bloc));
- Function_type* fntype = Type::make_function_type(NULL, param_types,
- NULL, bloc);
- Named_object* mapassign2 =
- Named_object::make_function_declaration("mapassign2", NULL, fntype, bloc);
- mapassign2->func_declaration_value()->set_asm_name("runtime.mapassign2");
+ // 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* func = Expression::make_func_reference(mapassign2, NULL, loc);
- Expression_list* params = new Expression_list();
- params->push_back(Expression::make_temporary_reference(map_temp, loc));
+ Expression* p1 = Expression::make_temporary_reference(map_temp, loc);
Expression* ref = Expression::make_temporary_reference(key_temp, loc);
- params->push_back(Expression::make_unary(OPERATOR_AND, ref, loc));
+ Expression* p2 = Expression::make_unary(OPERATOR_AND, ref, loc);
ref = Expression::make_temporary_reference(val_temp, loc);
- params->push_back(Expression::make_unary(OPERATOR_AND, ref, loc));
- params->push_back(this->should_set_);
- Expression* call = Expression::make_call(func, params, false, 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);
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*
class Tuple_receive_assignment_statement : public Statement
{
public:
- Tuple_receive_assignment_statement(Expression* val, Expression* success,
- Expression* channel,
+ Tuple_receive_assignment_statement(Expression* val, Expression* closed,
+ Expression* channel, bool for_select,
source_location location)
: Statement(STATEMENT_TUPLE_RECEIVE_ASSIGNMENT, location),
- val_(val), success_(success), channel_(channel)
+ val_(val), closed_(closed), channel_(channel), for_select_(for_select)
{ }
protected:
bool
do_traverse_assignments(Traverse_assignments*)
- { gcc_unreachable(); }
+ { go_unreachable(); }
Statement*
- do_lower(Gogo*, Block*);
+ do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
- tree
- do_get_tree(Translate_context*)
- { gcc_unreachable(); }
+ 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 read succeeded or failed.
- Expression* success_;
+ // 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.
Tuple_receive_assignment_statement::do_traverse(Traverse* traverse)
{
if (this->traverse_expression(traverse, &this->val_) == TRAVERSE_EXIT
- || this->traverse_expression(traverse, &this->success_) == 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*, Block* enclosing)
+Tuple_receive_assignment_statement::do_lower(Gogo*, Named_object*,
+ Block* enclosing,
+ Statement_inserter*)
{
source_location loc = this->location();
// evaluated in the right order.
Move_ordered_evals moe(b);
this->val_->traverse_subexpressions(&moe);
- this->success_->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 success_temp bool
- Temporary_statement* success_temp =
+ // var closed_temp bool
+ Temporary_statement* closed_temp =
Statement::make_temporary(Type::lookup_bool_type(), NULL, loc);
- b->add_statement(success_temp);
-
- // func chanrecv2(c chan T, val *T) bool
- source_location bloc = BUILTINS_LOCATION;
- Typed_identifier_list* param_types = new Typed_identifier_list();
- param_types->push_back(Typed_identifier("c", channel_type, bloc));
- Type* pelement_type = Type::make_pointer_type(channel_type->element_type());
- param_types->push_back(Typed_identifier("val", pelement_type, bloc));
-
- Typed_identifier_list* ret_types = new Typed_identifier_list();
- ret_types->push_back(Typed_identifier("", Type::lookup_bool_type(), bloc));
-
- Function_type* fntype = Type::make_function_type(NULL, param_types,
- ret_types, bloc);
- Named_object* chanrecv2 =
- Named_object::make_function_declaration("chanrecv2", NULL, fntype, bloc);
- chanrecv2->func_declaration_value()->set_asm_name("runtime.chanrecv2");
-
- // success_temp = chanrecv2(channel, &val_temp)
- Expression* func = Expression::make_func_reference(chanrecv2, NULL, loc);
- Expression_list* params = new Expression_list();
- params->push_back(this->channel_);
- Expression* ref = Expression::make_temporary_reference(val_temp, loc);
- params->push_back(Expression::make_unary(OPERATOR_AND, ref, loc));
- Expression* call = Expression::make_call(func, params, false, loc);
- ref = Expression::make_temporary_reference(success_temp, 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);
s = Statement::make_assignment(this->val_, ref, loc);
b->add_statement(s);
- // success = success_temp
- ref = Expression::make_temporary_reference(success_temp, loc);
- s = Statement::make_assignment(this->success_, ref, loc);
+ // 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* success,
+Statement::make_tuple_receive_assignment(Expression* val, Expression* closed,
Expression* channel,
+ bool for_select,
source_location location)
{
- return new Tuple_receive_assignment_statement(val, success, channel,
- 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
bool
do_traverse_assignments(Traverse_assignments*)
- { gcc_unreachable(); }
+ { go_unreachable(); }
Statement*
- do_lower(Gogo*, Block*);
+ do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
- tree
- do_get_tree(Translate_context*)
- { gcc_unreachable(); }
+ Bstatement*
+ do_get_backend(Translate_context*)
+ { go_unreachable(); }
- private:
- Call_expression*
- lower_to_empty_interface(const char*);
+ void
+ do_dump_statement(Ast_dump_context*) const;
+ private:
Call_expression*
- lower_to_type(const char*);
+ lower_to_type(Runtime::Function);
void
- lower_to_object_type(Block*, const char*);
+ lower_to_object_type(Block*, Runtime::Function);
// The variable which recieves the converted value.
Expression* val_;
// Lower to a function call.
Statement*
-Tuple_type_guard_assignment_statement::do_lower(Gogo*, Block* enclosing)
+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_type() && !this->type_->is_error_type())
+ 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);
}
if (this->type_->interface_type() != NULL)
{
if (this->type_->interface_type()->is_empty())
- call = this->lower_to_empty_interface(expr_is_empty
- ? "ifaceE2E2"
- : "ifaceI2E2");
+ call = Runtime::make_call((expr_is_empty
+ ? Runtime::IFACEE2E2
+ : Runtime::IFACEI2E2),
+ loc, 1, this->expr_);
else
- call = this->lower_to_type(expr_is_empty ? "ifaceE2I2" : "ifaceI2I2");
+ 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 ? "ifaceE2T2P" : "ifaceI2T2P");
+ call = this->lower_to_type(expr_is_empty
+ ? Runtime::IFACEE2T2P
+ : Runtime::IFACEI2T2P);
else
{
- this->lower_to_object_type(b, expr_is_empty ? "ifaceE2T2" : "ifaceI2T2");
+ 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);
return Statement::make_block_statement(b, loc);
}
-// Lower a conversion to an empty interface type.
-
-Call_expression*
-Tuple_type_guard_assignment_statement::lower_to_empty_interface(
- const char *fnname)
-{
- source_location loc = this->location();
-
- // func FNNAME(interface) (empty, bool)
- source_location bloc = BUILTINS_LOCATION;
- Typed_identifier_list* param_types = new Typed_identifier_list();
- param_types->push_back(Typed_identifier("i", this->expr_->type(), bloc));
- Typed_identifier_list* ret_types = new Typed_identifier_list();
- ret_types->push_back(Typed_identifier("ret", this->type_, bloc));
- ret_types->push_back(Typed_identifier("ok", Type::lookup_bool_type(), bloc));
- Function_type* fntype = Type::make_function_type(NULL, param_types,
- ret_types, bloc);
- Named_object* fn =
- Named_object::make_function_declaration(fnname, NULL, fntype, bloc);
- std::string asm_name = "runtime.";
- asm_name += fnname;
- fn->func_declaration_value()->set_asm_name(asm_name);
-
- // val, ok = FNNAME(expr)
- Expression* func = Expression::make_func_reference(fn, NULL, loc);
- Expression_list* params = new Expression_list();
- params->push_back(this->expr_);
- return Expression::make_call(func, params, false, loc);
-}
-
// Lower a conversion to a non-empty interface type or a pointer type.
Call_expression*
-Tuple_type_guard_assignment_statement::lower_to_type(const char* fnname)
+Tuple_type_guard_assignment_statement::lower_to_type(Runtime::Function code)
{
source_location loc = this->location();
-
- // func FNNAME(*descriptor, interface) (interface, bool)
- source_location bloc = BUILTINS_LOCATION;
- Typed_identifier_list* param_types = new Typed_identifier_list();
- param_types->push_back(Typed_identifier("inter",
- Type::make_type_descriptor_ptr_type(),
- bloc));
- param_types->push_back(Typed_identifier("i", this->expr_->type(), bloc));
- Typed_identifier_list* ret_types = new Typed_identifier_list();
- ret_types->push_back(Typed_identifier("ret", this->type_, bloc));
- ret_types->push_back(Typed_identifier("ok", Type::lookup_bool_type(), bloc));
- Function_type* fntype = Type::make_function_type(NULL, param_types,
- ret_types, bloc);
- Named_object* fn =
- Named_object::make_function_declaration(fnname, NULL, fntype, bloc);
- std::string asm_name = "runtime.";
- asm_name += fnname;
- fn->func_declaration_value()->set_asm_name(asm_name);
-
- // val, ok = FNNAME(type_descriptor, expr)
- Expression* func = Expression::make_func_reference(fn, NULL, loc);
- Expression_list* params = new Expression_list();
- params->push_back(Expression::make_type_descriptor(this->type_, loc));
- params->push_back(this->expr_);
- return Expression::make_call(func, params, false, loc);
+ 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,
- const char *fnname)
+Tuple_type_guard_assignment_statement::lower_to_object_type(
+ Block* b,
+ Runtime::Function code)
{
source_location loc = this->location();
NULL, loc);
b->add_statement(val_temp);
- // func FNNAME(*descriptor, interface, *T) bool
- source_location bloc = BUILTINS_LOCATION;
- Typed_identifier_list* param_types = new Typed_identifier_list();
- param_types->push_back(Typed_identifier("inter",
- Type::make_type_descriptor_ptr_type(),
- bloc));
- param_types->push_back(Typed_identifier("i", this->expr_->type(), bloc));
- Type* ptype = Type::make_pointer_type(this->type_);
- param_types->push_back(Typed_identifier("v", ptype, bloc));
- Typed_identifier_list* ret_types = new Typed_identifier_list();
- ret_types->push_back(Typed_identifier("ok", Type::lookup_bool_type(), bloc));
- Function_type* fntype = Type::make_function_type(NULL, param_types,
- ret_types, bloc);
- Named_object* fn =
- Named_object::make_function_declaration(fnname, NULL, fntype, bloc);
- std::string asm_name = "runtime.";
- asm_name += fnname;
- fn->func_declaration_value()->set_asm_name(asm_name);
-
- // ok = FNNAME(type_descriptor, expr, &val_temp)
- Expression* func = Expression::make_func_reference(fn, NULL, loc);
- Expression_list* params = new Expression_list();
- params->push_back(Expression::make_type_descriptor(this->type_, loc));
- params->push_back(this->expr_);
+ // 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);
- params->push_back(Expression::make_unary(OPERATOR_AND, ref, loc));
- Expression* call = Expression::make_call(func, params, false, 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);
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*
expr_(expr)
{ }
+ Expression*
+ expr()
+ { return this->expr_; }
+
protected:
int
do_traverse(Traverse* traverse)
bool
do_may_fall_through() const;
- tree
- do_get_tree(Translate_context* context)
- { return this->expr_->get_tree(context); }
+ Bstatement*
+ do_get_backend(Translate_context* context);
+
+ void
+ do_dump_statement(Ast_dump_context*) const;
private:
Expression* expr_;
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*
do_may_fall_through() const
{ return this->block_->may_fall_through(); }
- tree
- do_get_tree(Translate_context* context)
- { return this->block_->get_tree(context); }
+ 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*
bool
do_traverse_assignments(Traverse_assignments*)
- { gcc_unreachable(); }
+ { go_unreachable(); }
Statement*
- do_lower(Gogo*, Block*);
+ do_lower(Gogo*, Named_object*, Block*, Statement_inserter*);
+
+ Bstatement*
+ do_get_backend(Translate_context*)
+ { go_unreachable(); }
- tree
- do_get_tree(Translate_context*)
- { gcc_unreachable(); }
+ void
+ do_dump_statement(Ast_dump_context*) const;
private:
// The l-value to increment or decrement.
// Lower to += or -=.
Statement*
-Inc_dec_statement::do_lower(Gogo*, Block*)
+Inc_dec_statement::do_lower(Gogo*, Named_object*, Block*, Statement_inserter*)
{
source_location loc = this->location();
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*
// Class Thunk_statement. This is the base class for go and defer
// statements.
-const char* const Thunk_statement::thunk_field_fn = "fn";
-
-const char* const Thunk_statement::thunk_field_receiver = "receiver";
-
// Constructor.
Thunk_statement::Thunk_statement(Statement_classification classification,
// If this calls something which is not a simple function, then we
// need a thunk.
Expression* fn = this->call_->call_expression()->fn();
- if (fn->bound_method_expression() != NULL
- || fn->interface_field_reference_expression() != NULL)
+ if (fn->interface_field_reference_expression() != NULL)
return false;
return true;
// Now that we know the types of the call, build the struct used to
// pass parameters.
- Function_type* fntype =
- this->call_->call_expression()->get_function_type();
+ 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);
}
Thunk_statement::do_check_types(Gogo*)
{
Call_expression* ce = this->call_->call_expression();
- Function_type* fntype = ce->get_function_type();
- if (fntype != NULL && fntype->is_method())
+ if (ce == NULL)
{
- Expression* fn = ce->fn();
- if (fn->bound_method_expression() == NULL
- && fn->interface_field_reference_expression() == NULL)
- this->report_error(_("no object for method call"));
+ if (!this->call_->is_error_expression())
+ this->report_error("expected call expression");
+ return;
}
}
{
public:
Simplify_thunk_traverse(Gogo* gogo)
- : Traverse(traverse_blocks),
- 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)
{
Thunk_statement* stat = b->statements()->back()->thunk_statement();
if (stat == NULL)
return TRAVERSE_CONTINUE;
- if (stat->simplify_statement(this->gogo_, b))
+ if (stat->simplify_statement(this->gogo_, this->function_, b))
return TRAVERSE_SKIP_COMPONENTS;
return TRAVERSE_CONTINUE;
}
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
// struct to a thunk. The thunk does the real call.
bool
-Thunk_statement::simplify_statement(Gogo* gogo, Block* block)
+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 || this->is_simple(fntype))
+ if (fntype == NULL)
+ {
+ go_assert(saw_errors());
+ this->set_is_error();
+ return false;
+ }
+ if (this->is_simple(fntype))
return false;
Expression* fn = ce->fn();
- Bound_method_expression* bound_method = fn->bound_method_expression();
Interface_field_reference_expression* interface_method =
fn->interface_field_reference_expression();
- const bool is_method = bound_method != NULL || interface_method != NULL;
source_location location = this->location();
std::string thunk_name = Gogo::thunk_name();
// Build the thunk.
- this->build_thunk(gogo, thunk_name, fntype);
+ this->build_thunk(gogo, thunk_name);
// Generate code to call the thunk.
// argument to the thunk.
Expression_list* vals = new Expression_list();
- if (fntype->is_builtin())
- ;
- else if (!is_method)
+ if (!this->is_constant_function())
vals->push_back(fn);
- else if (interface_method != NULL)
- vals->push_back(interface_method->expr());
- else if (bound_method != NULL)
- {
- vals->push_back(bound_method->method());
- Expression* first_arg = bound_method->first_argument();
-
- // We always pass a pointer when calling a method.
- if (first_arg->type()->points_to() == NULL)
- first_arg = Expression::make_unary(OPERATOR_AND, first_arg, location);
-
- // If we are calling a method which was inherited from an
- // embedded struct, and the method did not get a stub, then the
- // first type may be wrong.
- Type* fatype = bound_method->first_argument_type();
- if (fatype != NULL)
- {
- if (fatype->points_to() == NULL)
- fatype = Type::make_pointer_type(fatype);
- Type* unsafe = Type::make_pointer_type(Type::make_void_type());
- first_arg = Expression::make_cast(unsafe, first_arg, location);
- first_arg = Expression::make_cast(fatype, first_arg, location);
- }
- vals->push_back(first_arg);
- }
- else
- gcc_unreachable();
+ if (interface_method != NULL)
+ vals->push_back(interface_method->expr());
if (ce->args() != NULL)
{
// Look up the thunk.
Named_object* named_thunk = gogo->lookup(thunk_name, NULL);
- gcc_assert(named_thunk != NULL && named_thunk->is_function());
+ go_assert(named_thunk != NULL && named_thunk->is_function());
// Build the call.
Expression* func = Expression::make_func_reference(named_thunk, NULL,
else if (this->classification() == STATEMENT_DEFER)
s = Statement::make_defer_statement(call, location);
else
- gcc_unreachable();
+ go_unreachable();
// The current block should end with the go statement.
- gcc_assert(block->statements()->size() >= 1);
- gcc_assert(block->statements()->back() == this);
+ 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
Call_expression* ce = this->call_->call_expression();
Expression* fn = ce->fn();
- Interface_field_reference_expression* interface_method =
- fn->interface_field_reference_expression();
- if (interface_method != NULL)
- {
- // If this thunk statement calls a method on an interface, we
- // pass the interface object to the thunk.
- Typed_identifier tid(Thunk_statement::thunk_field_fn,
- interface_method->expr()->type(),
- location);
- fields->push_back(Struct_field(tid));
- }
- else if (!fntype->is_builtin())
+ if (!this->is_constant_function())
{
// The function to call.
- Typed_identifier tid(Go_statement::thunk_field_fn, fntype, location);
- fields->push_back(Struct_field(tid));
- }
- else if (ce->is_recover_call())
- {
- // The predeclared recover function has no argument. However,
- // we add an argument when building recover thunks. Handle that
- // here.
- fields->push_back(Struct_field(Typed_identifier("can_recover",
- Type::make_boolean_type(),
+ fields->push_back(Struct_field(Typed_identifier("fn", fntype,
location)));
}
- if (fn->bound_method_expression() != NULL)
+ // 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)
{
- gcc_assert(fntype->is_method());
- Type* rtype = fntype->receiver()->type();
- // We always pass the receiver as a pointer.
- if (rtype->points_to() == NULL)
- rtype = Type::make_pointer_type(rtype);
- Typed_identifier tid(Thunk_statement::thunk_field_receiver, rtype,
+ 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)
{
// artificial, function.
void
-Thunk_statement::build_thunk(Gogo* gogo, const std::string& thunk_name,
- Function_type* fntype)
+Thunk_statement::build_thunk(Gogo* gogo, const std::string& thunk_name)
{
source_location location = this->location();
// 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::make_boolean_type(),
+ thunk_results->push_back(Typed_identifier("", Type::lookup_bool_type(),
location));
}
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;
+ Label* retaddr_label = NULL;
if (may_call_recover)
{
retaddr_label = gogo->add_label_reference("retaddr");
Expression* arg = Expression::make_label_addr(retaddr_label, location);
- Expression_list* args = new Expression_list();
- args->push_back(arg);
-
- static Named_object* set_defer_retaddr;
- if (set_defer_retaddr == NULL)
- {
- const source_location bloc = BUILTINS_LOCATION;
- Typed_identifier_list* param_types = new Typed_identifier_list();
- Type *voidptr_type = Type::make_pointer_type(Type::make_void_type());
- param_types->push_back(Typed_identifier("r", voidptr_type, bloc));
-
- Typed_identifier_list* result_types = new Typed_identifier_list();
- result_types->push_back(Typed_identifier("",
- Type::make_boolean_type(),
- bloc));
-
- Function_type* t = Type::make_function_type(NULL, param_types,
- result_types, bloc);
- set_defer_retaddr =
- Named_object::make_function_declaration("__go_set_defer_retaddr",
- NULL, t, bloc);
- const char* n = "__go_set_defer_retaddr";
- set_defer_retaddr->func_declaration_value()->set_asm_name(n);
- }
-
- Expression* fn = Expression::make_func_reference(set_defer_retaddr,
- NULL, location);
- Expression* call = Expression::make_call(fn, args, false, 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.
// Get a reference to the parameter.
Named_object* named_parameter = gogo->lookup(parameter_name, NULL);
- gcc_assert(named_parameter != NULL && named_parameter->is_variable());
+ 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.
thunk_parameter = Expression::make_unary(OPERATOR_MULT, thunk_parameter,
location);
- Bound_method_expression* bound_method = ce->fn()->bound_method_expression();
Interface_field_reference_expression* interface_method =
ce->fn()->interface_field_reference_expression();
Expression* func_to_call;
unsigned int next_index;
- if (!fntype->is_builtin())
- {
- func_to_call = Expression::make_field_reference(thunk_parameter,
- 0, location);
- next_index = 1;
- }
- else
+ if (this->is_constant_function())
{
- gcc_assert(bound_method == NULL && interface_method == NULL);
func_to_call = ce->fn();
next_index = 0;
}
-
- if (bound_method != NULL)
+ else
{
- Expression* r = Expression::make_field_reference(thunk_parameter, 1,
- location);
- // The main program passes in a function pointer from the
- // interface expression, so here we can make a bound method in
- // all cases.
- func_to_call = Expression::make_bound_method(r, func_to_call,
- location);
- next_index = 2;
+ func_to_call = Expression::make_field_reference(thunk_parameter,
+ 0, location);
+ next_index = 1;
}
- else if (interface_method != NULL)
+
+ 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(func_to_call,
- name,
+ func_to_call = Expression::make_interface_field_reference(r, name,
location);
+ next_index = 1;
}
Expression_list* call_params = new Expression_list();
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,
Expression* param = Expression::make_field_reference(thunk_param,
next_index,
location);
- call_params->push_back(param);
+ if (!is_recover_call)
+ call_params->push_back(param);
+ else
+ {
+ go_assert(call_params->empty());
+ recover_arg = param;
+ }
}
- Expression* call = Expression::make_call(func_to_call, call_params, false,
- location);
- // We need to lower in case this is a builtin function.
- call = call->lower(gogo, function, -1);
- if (may_call_recover)
+ if (call_params->empty())
{
- Call_expression* ce = call->call_expression();
- if (ce != NULL)
- ce->set_is_deferred();
+ delete call_params;
+ call_params = NULL;
}
- Statement* call_statement = Statement::make_statement(call);
+ Call_expression* call = Expression::make_call(func_to_call, call_params,
+ false, location);
- // We already ran the determine_types pass, so we need to run it
- // just for this statement now.
- call_statement->determine_types();
+ // 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);
gogo->add_statement(call_statement);
Expression_list* vals = new Expression_list();
vals->push_back(Expression::make_boolean(false, location));
- const Typed_identifier_list* results =
- function->func_value()->type()->results();
- gogo->add_statement(Statement::make_return_statement(results, vals,
- 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 trees.
+// Get the function and argument expressions.
-void
-Thunk_statement::get_fn_and_arg(Translate_context* context, tree* pfn,
- tree* parg)
+bool
+Thunk_statement::get_fn_and_arg(Expression** pfn, Expression** parg)
{
if (this->call_->is_error_expression())
- {
- *pfn = error_mark_node;
- *parg = error_mark_node;
- return;
- }
+ return false;
Call_expression* ce = this->call_->call_expression();
- Expression* fn = ce->fn();
- *pfn = fn->get_tree(context);
+ *pfn = ce->fn();
const Expression_list* args = ce->args();
if (args == NULL || args->empty())
- *parg = null_pointer_node;
+ *parg = Expression::make_nil(this->location());
else
{
- gcc_assert(args->size() == 1);
- *parg = args->front()->get_tree(context);
+ go_assert(args->size() == 1);
+ *parg = args->front();
}
+
+ return true;
}
// Class Go_statement.
-tree
-Go_statement::do_get_tree(Translate_context* context)
+Bstatement*
+Go_statement::do_get_backend(Translate_context* context)
{
- tree fn_tree;
- tree arg_tree;
- this->get_fn_and_arg(context, &fn_tree, &arg_tree);
+ Expression* fn;
+ Expression* arg;
+ if (!this->get_fn_and_arg(&fn, &arg))
+ return context->backend()->error_statement();
- static tree go_fndecl;
+ 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);
+}
- tree fn_arg_type = NULL_TREE;
- if (go_fndecl == NULL_TREE)
- {
- // Only build FN_ARG_TYPE if we need it.
- tree subargtypes = tree_cons(NULL_TREE, ptr_type_node, void_list_node);
- tree subfntype = build_function_type(ptr_type_node, subargtypes);
- fn_arg_type = build_pointer_type(subfntype);
- }
+// Dump the AST representation for go statement.
- return Gogo::call_builtin(&go_fndecl,
- this->location(),
- "__go_go",
- 2,
- void_type_node,
- fn_arg_type,
- fn_tree,
- ptr_type_node,
- arg_tree);
+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.
// Class Defer_statement.
-tree
-Defer_statement::do_get_tree(Translate_context* context)
+Bstatement*
+Defer_statement::do_get_backend(Translate_context* context)
{
- source_location loc = this->location();
-
- tree fn_tree;
- tree arg_tree;
- this->get_fn_and_arg(context, &fn_tree, &arg_tree);
- if (fn_tree == error_mark_node || arg_tree == error_mark_node)
- return error_mark_node;
+ Expression* fn;
+ Expression* arg;
+ if (!this->get_fn_and_arg(&fn, &arg))
+ return context->backend()->error_statement();
- static tree defer_fndecl;
+ source_location loc = this->location();
+ Expression* ds = context->function()->func_value()->defer_stack(loc);
- tree fn_arg_type = NULL_TREE;
- if (defer_fndecl == NULL_TREE)
- {
- // Only build FN_ARG_TYPE if we need it.
- tree subargtypes = tree_cons(NULL_TREE, ptr_type_node, void_list_node);
- tree subfntype = build_function_type(ptr_type_node, subargtypes);
- fn_arg_type = build_pointer_type(subfntype);
- }
+ 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);
+}
- tree defer_stack = context->function()->func_value()->defer_stack(loc);
+// Dump the AST representation for defer statement.
- return Gogo::call_builtin(&defer_fndecl,
- loc,
- "__go_defer",
- 3,
- void_type_node,
- ptr_type_node,
- defer_stack,
- fn_arg_type,
- fn_tree,
- ptr_type_node,
- arg_tree);
+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.
// 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 function has named result
-// variables, and the return statement lists explicit values, then
-// implement it by assigning the values to the result variables and
-// changing the statement to not list any values. This lets
-// panic/recover work correctly.
+// 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*, Block* enclosing)
+Return_statement::do_lower(Gogo*, Named_object* function, Block* enclosing,
+ Statement_inserter*)
{
- if (this->vals_ == NULL)
+ if (this->is_lowered_)
return this;
- const Typed_identifier_list* results = this->results_;
- if (results == NULL || results->empty())
- 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.
- size_t results_count = results->size();
if (results_count > 1
- && this->vals_->size() == 1
- && this->vals_->front()->call_expression() != NULL)
+ && vals->size() == 1
+ && vals->front()->call_expression() != NULL)
{
- Call_expression* call = this->vals_->front()->call_expression();
- size_t count = results->size();
- Expression_list* vals = new Expression_list;
- for (size_t i = 0; i < count; ++i)
+ 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));
- delete this->vals_;
- this->vals_ = vals;
+ vals_count = results_count;
}
- if (results->front().name().empty())
- return this;
-
- if (results_count != this->vals_->size())
+ if (vals_count < results_count)
{
- // Presumably an error which will be reported in check_types.
+ this->report_error(_("not enough arguments to return"));
return this;
}
- // Assign to named return values and then return them.
-
- source_location loc = this->location();
- const Block* top = enclosing;
- while (top->enclosing() != NULL)
- top = top->enclosing();
+ if (vals_count > results_count)
+ {
+ this->report_error(_("too many values in return statement"));
+ return this;
+ }
- const Bindings *bindings = top->bindings();
Block* b = new Block(enclosing, loc);
Expression_list* lhs = new Expression_list();
Expression_list* rhs = new Expression_list();
- Expression_list::const_iterator pe = this->vals_->begin();
+ Expression_list::const_iterator pe = vals->begin();
int i = 1;
- for (Typed_identifier_list::const_iterator pr = results->begin();
+ for (Function::Results::const_iterator pr = results->begin();
pr != results->end();
++pr, ++pe, ++i)
{
- Named_object* rv = bindings->lookup_local(pr->name());
- if (rv == NULL || !rv->is_result_variable())
- {
- // Presumably an error.
- delete b;
- delete lhs;
- delete rhs;
- return this;
- }
-
+ Named_object* rv = *pr;
Expression* e = *pe;
// Check types now so that we give a good error message. The
e->determine_type(&type_context);
std::string reason;
- if (Type::are_assignable(rvtype, e->type(), &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);
i, reason.c_str());
}
}
- gcc_assert(lhs->size() == rhs->size());
+ go_assert(lhs->size() == rhs->size());
if (lhs->empty())
;
else if (lhs->size() == 1)
{
- b->add_statement(Statement::make_assignment(lhs->front(), rhs->front(),
- loc));
+ 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
- b->add_statement(Statement::make_tuple_assignment(lhs, rhs, loc));
-
- b->add_statement(Statement::make_return_statement(this->results_, NULL,
- loc));
-
- return Statement::make_block_statement(b, loc);
-}
-
-// Determine types.
-
-void
-Return_statement::do_determine_types()
-{
- if (this->vals_ == NULL)
- return;
- const Typed_identifier_list* results = this->results_;
-
- Typed_identifier_list::const_iterator pt;
- if (results != NULL)
- pt = results->begin();
- for (Expression_list::iterator pe = this->vals_->begin();
- pe != this->vals_->end();
- ++pe)
{
- if (results == NULL || pt == results->end())
- (*pe)->determine_type_no_context();
- else
+ Statement* s = Statement::make_tuple_assignment(lhs, rhs, loc);
+ if (this->are_hidden_fields_ok_)
{
- Type_context context(pt->type(), false);
- (*pe)->determine_type(&context);
- ++pt;
+ 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);
}
-// Check types.
+// Convert a return statement to the backend representation.
-void
-Return_statement::do_check_types(Gogo*)
+Bstatement*
+Return_statement::do_get_backend(Translate_context* context)
{
- if (this->vals_ == NULL)
- return;
+ source_location loc = this->location();
- const Typed_identifier_list* results = this->results_;
- if (results == NULL)
- {
- this->report_error(_("return with value in function "
- "with no return type"));
- return;
- }
+ Function* function = context->function()->func_value();
+ tree fndecl = function->get_decl();
- int i = 1;
- Typed_identifier_list::const_iterator pt = results->begin();
- for (Expression_list::const_iterator pe = this->vals_->begin();
- pe != this->vals_->end();
- ++pe, ++pt, ++i)
+ Function::Results* results = function->result_variables();
+ std::vector<Bexpression*> retvals;
+ if (results != NULL && !results->empty())
{
- if (pt == results->end())
- {
- this->report_error(_("too many values in return statement"));
- return;
- }
- std::string reason;
- if (!Type::are_assignable(pt->type(), (*pe)->type(), &reason))
+ retvals.reserve(results->size());
+ for (Function::Results::const_iterator p = results->begin();
+ p != results->end();
+ p++)
{
- if (reason.empty())
- error_at(this->location(),
- "incompatible type for return value %d",
- i);
- else
- error_at(this->location(),
- "incompatible type for return value %d (%s)",
- i, reason.c_str());
- this->set_is_error();
- }
- else if (pt->type()->is_error_type()
- || (*pe)->type()->is_error_type()
- || pt->type()->is_undefined()
- || (*pe)->type()->is_undefined())
- {
- // Make sure we get the error for an undefined type.
- pt->type()->base();
- (*pe)->type()->base();
- this->set_is_error();
+ Expression* vr = Expression::make_var_reference(*p, loc);
+ retvals.push_back(tree_to_expr(vr->get_tree(context)));
}
}
- if (pt != results->end())
- this->report_error(_("not enough values in return statement"));
+ return context->backend()->return_statement(tree_to_function(fndecl),
+ retvals, loc);
}
-// Build a RETURN_EXPR tree.
+// Dump the AST representation for a return statement.
-tree
-Return_statement::do_get_tree(Translate_context* context)
+void
+Return_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
{
- Function* function = context->function()->func_value();
- tree fndecl = function->get_decl();
-
- const Typed_identifier_list* results = this->results_;
-
- if (this->vals_ == NULL)
- {
- tree stmt_list = NULL_TREE;
- tree retval = function->return_value(context->gogo(),
- context->function(),
- this->location(),
- &stmt_list);
- tree set;
- if (retval == NULL_TREE)
- set = NULL_TREE;
- else
- set = fold_build2_loc(this->location(), MODIFY_EXPR, void_type_node,
- DECL_RESULT(fndecl), retval);
- append_to_statement_list(this->build_stmt_1(RETURN_EXPR, set),
- &stmt_list);
- return stmt_list;
- }
- else if (this->vals_->size() == 1)
- {
- gcc_assert(!VOID_TYPE_P(TREE_TYPE(TREE_TYPE(fndecl))));
- tree val = (*this->vals_->begin())->get_tree(context);
- if (val == error_mark_node)
- return error_mark_node;
- gcc_assert(results != NULL && results->size() == 1);
- val = Expression::convert_for_assignment(context,
- results->begin()->type(),
- (*this->vals_->begin())->type(),
- val, this->location());
- tree set = build2(MODIFY_EXPR, void_type_node,
- DECL_RESULT(fndecl), val);
- SET_EXPR_LOCATION(set, this->location());
- return this->build_stmt_1(RETURN_EXPR, set);
- }
- else
- {
- gcc_assert(!VOID_TYPE_P(TREE_TYPE(TREE_TYPE(fndecl))));
- tree stmt_list = NULL_TREE;
- tree rettype = TREE_TYPE(DECL_RESULT(fndecl));
- tree retvar = create_tmp_var(rettype, "RESULT");
- gcc_assert(results != NULL && results->size() == this->vals_->size());
- Expression_list::const_iterator pv = this->vals_->begin();
- Typed_identifier_list::const_iterator pr = results->begin();
- for (tree field = TYPE_FIELDS(rettype);
- field != NULL_TREE;
- ++pv, ++pr, field = DECL_CHAIN(field))
- {
- gcc_assert(pv != this->vals_->end());
- tree val = (*pv)->get_tree(context);
- if (val == error_mark_node)
- return error_mark_node;
- val = Expression::convert_for_assignment(context, pr->type(),
- (*pv)->type(), val,
- this->location());
- tree set = build2(MODIFY_EXPR, void_type_node,
- build3(COMPONENT_REF, TREE_TYPE(field),
- retvar, field, NULL_TREE),
- val);
- SET_EXPR_LOCATION(set, this->location());
- append_to_statement_list(set, &stmt_list);
- }
- tree set = build2(MODIFY_EXPR, void_type_node, DECL_RESULT(fndecl),
- retvar);
- append_to_statement_list(this->build_stmt_1(RETURN_EXPR, set),
- &stmt_list);
- return stmt_list;
- }
+ 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.
-Statement*
-Statement::make_return_statement(const Typed_identifier_list* results,
- Expression_list* vals,
+Return_statement*
+Statement::make_return_statement(Expression_list* vals,
source_location location)
{
- return new Return_statement(results, vals, location);
+ return new Return_statement(vals, location);
}
// A break or continue statement.
do_may_fall_through() const
{ return false; }
- tree
- do_get_tree(Translate_context*)
- { return this->label_->get_goto(this->location()); }
+ 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.
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*
do_may_fall_through() const
{ return false; }
- tree
- do_get_tree(Translate_context*);
+ Bstatement*
+ do_get_backend(Translate_context*);
+
+ void
+ do_dump_statement(Ast_dump_context*) const;
private:
Label* label_;
}
}
-// Return the tree for the goto statement.
+// 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.
-tree
-Goto_statement::do_get_tree(Translate_context*)
+void
+Goto_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
{
- return this->build_stmt_1(GOTO_EXPR, this->label_->get_decl());
+ ast_dump_context->print_indent();
+ ast_dump_context->ostream() << "goto " << this->label_->name() << std::endl;
}
// Make a goto statement.
do_may_fall_through() const
{ return false; }
- tree
- do_get_tree(Translate_context*)
- { return this->label_->get_goto(this->location()); }
+ 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*
return TRAVERSE_CONTINUE;
}
-// Return a tree defining this label.
+// Return the backend representation of the statement defining this
+// label.
-tree
-Label_statement::do_get_tree(Translate_context*)
+Bstatement*
+Label_statement::do_get_backend(Translate_context* context)
{
- return this->build_stmt_1(LABEL_EXPR, this->label_->get_decl());
+ 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.
do_traverse(Traverse*)
{ return TRAVERSE_CONTINUE; }
- tree
- do_get_tree(Translate_context*)
- { return this->label_->get_definition(); }
+ 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*
bool
do_may_fall_through() const;
- tree
- do_get_tree(Translate_context*);
+ Bstatement*
+ do_get_backend(Translate_context*);
+
+ void
+ do_dump_statement(Ast_dump_context*) const;
private:
Expression* cond_;
int
If_statement::do_traverse(Traverse* traverse)
{
- if (this->cond_ != NULL)
- {
- if (this->traverse_expression(traverse, &this->cond_) == TRAVERSE_EXIT)
- return TRAVERSE_EXIT;
- }
- if (this->then_block_->traverse(traverse) == TRAVERSE_EXIT)
+ if (this->traverse_expression(traverse, &this->cond_) == TRAVERSE_EXIT
+ || this->then_block_->traverse(traverse) == TRAVERSE_EXIT)
return TRAVERSE_EXIT;
if (this->else_block_ != NULL)
{
void
If_statement::do_determine_types()
{
- if (this->cond_ != NULL)
- {
- Type_context context(Type::lookup_bool_type(), false);
- this->cond_->determine_type(&context);
- }
+ 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();
void
If_statement::do_check_types(Gogo*)
{
- if (this->cond_ != NULL)
- {
- Type* type = this->cond_->type();
- if (type->is_error_type())
- this->set_is_error();
- else if (!type->is_boolean_type())
- this->report_error(_("expected boolean expression"));
- }
+ 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.
|| this->else_block_->may_fall_through());
}
-// Get tree.
+// Get the backend representation.
-tree
-If_statement::do_get_tree(Translate_context* context)
+Bstatement*
+If_statement::do_get_backend(Translate_context* context)
{
- gcc_assert(this->cond_ == NULL || this->cond_->type()->is_boolean_type());
- tree ret = build3(COND_EXPR, void_type_node,
- (this->cond_ == NULL
- ? boolean_true_node
- : this->cond_->get_tree(context)),
- this->then_block_->get_tree(context),
- (this->else_block_ == NULL
- ? NULL_TREE
- : this->else_block_->get_tree(context)));
- SET_EXPR_LOCATION(ret, this->location());
- return ret;
+ 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.
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()(Expression*) const;
+};
+
+size_t
+Case_clauses::Hash_integer_value::operator()(Expression* pe) const
+{
+ Type* itype;
+ mpz_t ival;
+ mpz_init(ival);
+ if (!pe->integer_constant_value(true, ival, &itype))
+ go_unreachable();
+ size_t ret = mpz_get_ui(ival);
+ mpz_clear(ival);
+ return ret;
+}
+
+// Class Case_clauses::Eq_integer_value.
+
+class Case_clauses::Eq_integer_value
+{
+ public:
+ bool
+ operator()(Expression*, Expression*) const;
+};
+
+bool
+Case_clauses::Eq_integer_value::operator()(Expression* a, Expression* b) const
+{
+ Type* atype;
+ Type* btype;
+ mpz_t aval;
+ mpz_t bval;
+ mpz_init(aval);
+ mpz_init(bval);
+ if (!a->integer_constant_value(true, aval, &atype)
+ || !b->integer_constant_value(true, bval, &btype))
+ go_unreachable();
+ bool ret = mpz_cmp(aval, bval) == 0;
+ mpz_clear(aval);
+ mpz_clear(bval);
+ return ret;
+}
+
// Class Case_clauses::Case_clause.
// Traversal.
Case_clauses::Case_clause::traverse(Traverse* traverse)
{
if (this->cases_ != NULL
- && (traverse->traverse_mask() & Traverse::traverse_expressions) != 0)
+ && (traverse->traverse_mask()
+ & (Traverse::traverse_types | Traverse::traverse_expressions)) != 0)
{
if (this->cases_->traverse(traverse) == TRAVERSE_EXIT)
return TRAVERSE_EXIT;
Unnamed_label* next_case_label;
if (this->cases_ == NULL || this->cases_->empty())
{
- gcc_assert(this->is_default_);
+ go_assert(this->is_default_);
next_case_label = NULL;
}
else
return this->statements_->may_fall_through();
}
-// Build up the body of a SWITCH_EXPR.
+// Convert the case values and statements to the backend
+// representation. BREAK_LABEL is the label which break statements
+// should branch to. CASE_CONSTANTS is used to detect duplicate
+// constants. *CASES should be passed as an empty vector; the values
+// for this case will be added to it. If this is the default case,
+// *CASES will remain empty. This returns the statement to execute if
+// one of these cases is selected.
-void
-Case_clauses::Case_clause::get_constant_tree(Translate_context* context,
- Unnamed_label* break_label,
- Case_constants* case_constants,
- tree* stmt_list) const
+Bstatement*
+Case_clauses::Case_clause::get_backend(Translate_context* context,
+ Unnamed_label* break_label,
+ Case_constants* case_constants,
+ std::vector<Bexpression*>* cases) const
{
if (this->cases_ != NULL)
{
+ go_assert(!this->is_default_);
for (Expression_list::const_iterator p = this->cases_->begin();
p != this->cases_->end();
++p)
{
- Type* itype;
- mpz_t ival;
- mpz_init(ival);
- if (!(*p)->integer_constant_value(true, ival, &itype))
- gcc_unreachable();
- gcc_assert(itype != NULL);
- tree type_tree = itype->get_tree(context->gogo());
- tree val = Expression::integer_constant_tree(ival, type_tree);
- mpz_clear(ival);
-
- if (val != error_mark_node)
+ Expression* e = *p;
+ if (e->classification() != Expression::EXPRESSION_INTEGER)
{
- gcc_assert(TREE_CODE(val) == INTEGER_CST);
-
- std::pair<Case_constants::iterator, bool> ins =
- case_constants->insert(val);
- if (!ins.second)
+ Type* itype;
+ mpz_t ival;
+ mpz_init(ival);
+ if (!(*p)->integer_constant_value(true, ival, &itype))
{
- // Value was already present.
- warning_at(this->location_, 0,
- "duplicate case value will never match");
+ // Something went wrong. This can happen with a
+ // negative constant and an unsigned switch value.
+ go_assert(saw_errors());
continue;
}
+ go_assert(itype != NULL);
+ e = Expression::make_integer(&ival, itype, e->location());
+ mpz_clear(ival);
+ }
- tree label = create_artificial_label(this->location_);
- append_to_statement_list(build3(CASE_LABEL_EXPR, void_type_node,
- val, NULL_TREE, label),
- stmt_list);
+ std::pair<Case_constants::iterator, bool> ins =
+ case_constants->insert(e);
+ if (!ins.second)
+ {
+ // Value was already present.
+ error_at(this->location_, "duplicate case in switch");
+ continue;
}
+
+ tree case_tree = e->get_tree(context);
+ Bexpression* case_expr = tree_to_expr(case_tree);
+ cases->push_back(case_expr);
}
}
- if (this->is_default_)
+ Bstatement* statements;
+ if (this->statements_ == NULL)
+ statements = NULL;
+ else
{
- tree label = create_artificial_label(this->location_);
- append_to_statement_list(build3(CASE_LABEL_EXPR, void_type_node,
- NULL_TREE, NULL_TREE, label),
- stmt_list);
+ Bblock* bblock = this->statements_->get_backend(context);
+ statements = context->backend()->block_statement(bblock);
}
- if (this->statements_ != NULL)
+ Bstatement* break_stat;
+ if (this->is_fallthrough_)
+ break_stat = NULL;
+ else
+ break_stat = break_label->get_goto(context, this->location_);
+
+ if (statements == NULL)
+ return break_stat;
+ else if (break_stat == NULL)
+ return statements;
+ else
+ return context->backend()->compound_statement(statements, break_stat);
+}
+
+// Dump the AST representation for a case clause
+
+void
+Case_clauses::Case_clause::dump_clause(Ast_dump_context* ast_dump_context)
+ const
+{
+ ast_dump_context->print_indent();
+ if (this->is_default_)
{
- tree block_tree = this->statements_->get_tree(context);
- if (block_tree != error_mark_node)
- append_to_statement_list(block_tree, stmt_list);
+ ast_dump_context->ostream() << "default:";
+ }
+ else
+ {
+ ast_dump_context->ostream() << "case ";
+ ast_dump_context->dump_expression_list(this->cases_);
+ ast_dump_context->ostream() << ":" ;
+ }
+ ast_dump_context->dump_block(this->statements_);
+ if (this->is_fallthrough_)
+ {
+ ast_dump_context->print_indent();
+ ast_dump_context->ostream() << " (fallthrough)" << std::endl;
}
-
- if (!this->is_fallthrough_)
- append_to_statement_list(break_label->get_goto(this->location_), stmt_list);
}
// Class Case_clauses.
if (default_case != NULL)
default_case->lower(b, val_temp, default_start_label,
default_finish_label);
-
}
// Determine types.
return !found_default;
}
-// Return a tree when all case expressions are constants.
+// Convert the cases to the backend representation. This sets
+// *ALL_CASES and *ALL_STATEMENTS.
-tree
-Case_clauses::get_constant_tree(Translate_context* context,
- Unnamed_label* break_label) const
+void
+Case_clauses::get_backend(Translate_context* context,
+ Unnamed_label* break_label,
+ std::vector<std::vector<Bexpression*> >* all_cases,
+ std::vector<Bstatement*>* all_statements) const
{
Case_constants case_constants;
- tree stmt_list = NULL_TREE;
+
+ size_t c = this->clauses_.size();
+ all_cases->resize(c);
+ all_statements->resize(c);
+
+ size_t i = 0;
+ for (Clauses::const_iterator p = this->clauses_.begin();
+ p != this->clauses_.end();
+ ++p, ++i)
+ {
+ std::vector<Bexpression*> cases;
+ Bstatement* stat = p->get_backend(context, break_label, &case_constants,
+ &cases);
+ (*all_cases)[i].swap(cases);
+ (*all_statements)[i] = stat;
+ }
+}
+
+// Dump the AST representation for case clauses (from a switch statement)
+
+void
+Case_clauses::dump_clauses(Ast_dump_context* ast_dump_context) const
+{
for (Clauses::const_iterator p = this->clauses_.begin();
p != this->clauses_.end();
++p)
- p->get_constant_tree(context, break_label, &case_constants,
- &stmt_list);
- return stmt_list;
+ p->dump_clause(ast_dump_context);
}
// A constant switch statement. A Switch_statement is lowered to this
bool
do_may_fall_through() const;
- tree
- do_get_tree(Translate_context*);
+ Bstatement*
+ do_get_backend(Translate_context*);
+
+ void
+ do_dump_statement(Ast_dump_context*) const;
private:
// The value to switch on.
// Convert to GENERIC.
-tree
-Constant_switch_statement::do_get_tree(Translate_context* context)
+Bstatement*
+Constant_switch_statement::do_get_backend(Translate_context* context)
{
tree switch_val_tree = this->val_->get_tree(context);
+ Bexpression* switch_val_expr = tree_to_expr(switch_val_tree);
Unnamed_label* break_label = this->break_label_;
if (break_label == NULL)
break_label = new Unnamed_label(this->location());
- tree stmt_list = NULL_TREE;
- tree s = build3(SWITCH_EXPR, void_type_node, switch_val_tree,
- this->clauses_->get_constant_tree(context, break_label),
- NULL_TREE);
- SET_EXPR_LOCATION(s, this->location());
- append_to_statement_list(s, &stmt_list);
+ std::vector<std::vector<Bexpression*> > all_cases;
+ std::vector<Bstatement*> all_statements;
+ this->clauses_->get_backend(context, break_label, &all_cases,
+ &all_statements);
- append_to_statement_list(break_label->get_definition(), &stmt_list);
+ Bstatement* switch_statement;
+ switch_statement = context->backend()->switch_statement(switch_val_expr,
+ all_cases,
+ all_statements,
+ this->location());
+ Bstatement* ldef = break_label->get_definition(context);
+ return context->backend()->compound_statement(switch_statement, ldef);
+}
- return stmt_list;
+// Dump the AST representation for a constant switch statement.
+
+void
+Constant_switch_statement::do_dump_statement(Ast_dump_context* ast_dump_context)
+ const
+{
+ ast_dump_context->print_indent();
+ ast_dump_context->ostream() << "switch ";
+ ast_dump_context->dump_expression(this->val_);
+
+ if (ast_dump_context->dump_subblocks())
+ {
+ ast_dump_context->ostream() << " {" << std::endl;
+ this->clauses_->dump_clauses(ast_dump_context);
+ ast_dump_context->ostream() << "}";
+ }
+
+ ast_dump_context->ostream() << std::endl;
}
// Class Switch_statement.
// of if statements.
Statement*
-Switch_statement::do_lower(Gogo*, Block* enclosing)
+Switch_statement::do_lower(Gogo*, Named_object*, Block* enclosing,
+ Statement_inserter*)
{
source_location loc = this->location();
if (this->val_ != NULL
&& (this->val_->is_error_expression()
- || this->val_->type()->is_error_type()))
+ || this->val_->type()->is_error()))
return Statement::make_error_statement(loc);
if (this->val_ != NULL
return this->break_label_;
}
+// Dump the AST representation for a switch statement.
+
+void
+Switch_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
+{
+ ast_dump_context->print_indent();
+ ast_dump_context->ostream() << "switch ";
+ if (this->val_ != NULL)
+ {
+ ast_dump_context->dump_expression(this->val_);
+ }
+ if (ast_dump_context->dump_subblocks())
+ {
+ ast_dump_context->ostream() << " {" << std::endl;
+ this->clauses_->dump_clauses(ast_dump_context);
+ ast_dump_context->print_indent();
+ ast_dump_context->ostream() << "}";
+ }
+ ast_dump_context->ostream() << std::endl;
+}
+
// Make a switch statement.
Switch_statement*
{
Type* type = this->type_;
+ Expression* ref = Expression::make_temporary_reference(descriptor_temp,
+ loc);
+
Expression* cond;
// The language permits case nil, which is of course a constant
// rather than a type. It will appear here as an invalid
// forwarding type.
if (type->is_nil_constant_as_type())
- {
- Expression* ref =
- Expression::make_temporary_reference(descriptor_temp, loc);
- cond = Expression::make_binary(OPERATOR_EQEQ, ref,
- Expression::make_nil(loc),
- loc);
- }
+ cond = Expression::make_binary(OPERATOR_EQEQ, ref,
+ Expression::make_nil(loc),
+ loc);
else
- {
- Expression* func;
- if (type->interface_type() == NULL)
- {
- // func ifacetypeeq(*descriptor, *descriptor) bool
- static Named_object* ifacetypeeq;
- if (ifacetypeeq == NULL)
- {
- const source_location bloc = BUILTINS_LOCATION;
- Typed_identifier_list* param_types =
- new Typed_identifier_list();
- Type* descriptor_type = Type::make_type_descriptor_ptr_type();
- param_types->push_back(Typed_identifier("a", descriptor_type,
- bloc));
- param_types->push_back(Typed_identifier("b", descriptor_type,
- bloc));
- Typed_identifier_list* ret_types =
- new Typed_identifier_list();
- Type* bool_type = Type::lookup_bool_type();
- ret_types->push_back(Typed_identifier("", bool_type, bloc));
- Function_type* fntype = Type::make_function_type(NULL,
- param_types,
- ret_types,
- bloc);
- ifacetypeeq =
- Named_object::make_function_declaration("ifacetypeeq", NULL,
- fntype, bloc);
- const char* n = "runtime.ifacetypeeq";
- ifacetypeeq->func_declaration_value()->set_asm_name(n);
- }
-
- // ifacetypeeq(descriptor_temp, DESCRIPTOR)
- func = Expression::make_func_reference(ifacetypeeq, NULL, loc);
- }
- else
- {
- // func ifaceI2Tp(*descriptor, *descriptor) bool
- static Named_object* ifaceI2Tp;
- if (ifaceI2Tp == NULL)
- {
- const source_location bloc = BUILTINS_LOCATION;
- Typed_identifier_list* param_types =
- new Typed_identifier_list();
- Type* descriptor_type = Type::make_type_descriptor_ptr_type();
- param_types->push_back(Typed_identifier("a", descriptor_type,
- bloc));
- param_types->push_back(Typed_identifier("b", descriptor_type,
- bloc));
- Typed_identifier_list* ret_types =
- new Typed_identifier_list();
- Type* bool_type = Type::lookup_bool_type();
- ret_types->push_back(Typed_identifier("", bool_type, bloc));
- Function_type* fntype = Type::make_function_type(NULL,
- param_types,
- ret_types,
- bloc);
- ifaceI2Tp =
- Named_object::make_function_declaration("ifaceI2Tp", NULL,
- fntype, bloc);
- const char* n = "runtime.ifaceI2Tp";
- ifaceI2Tp->func_declaration_value()->set_asm_name(n);
- }
-
- // ifaceI2Tp(descriptor_temp, DESCRIPTOR)
- func = Expression::make_func_reference(ifaceI2Tp, NULL, loc);
- }
- Expression_list* params = new Expression_list();
- params->push_back(Expression::make_type_descriptor(type, loc));
- Expression* ref =
- Expression::make_temporary_reference(descriptor_temp, loc);
- params->push_back(ref);
- cond = Expression::make_call(func, params, false, loc);
- }
+ cond = Runtime::make_call((type->interface_type() == NULL
+ ? Runtime::IFACETYPEEQ
+ : Runtime::IFACEI2TP),
+ loc, 2,
+ Expression::make_type_descriptor(type, loc),
+ ref);
Unnamed_label* dest;
if (!this->is_fallthrough_)
else
{
// if COND { goto STMTS_LABEL }
- gcc_assert(stmts_label != NULL);
+ go_assert(stmts_label != NULL);
if (*stmts_label == NULL)
*stmts_label = new Unnamed_label(UNKNOWN_LOCATION);
dest = *stmts_label;
&& stmts_label != NULL
&& *stmts_label != NULL))
{
- gcc_assert(!this->is_fallthrough_);
+ go_assert(!this->is_fallthrough_);
if (stmts_label != NULL && *stmts_label != NULL)
{
- gcc_assert(!this->is_default_);
+ go_assert(!this->is_default_);
if (this->statements_ != NULL)
(*stmts_label)->set_location(this->statements_->start_location());
Statement* s = Statement::make_unnamed_label_statement(*stmts_label);
}
if (this->is_fallthrough_)
- gcc_assert(next_case_label == NULL);
+ go_assert(next_case_label == NULL);
else
{
source_location gloc = (this->statements_ == NULL
}
}
+// Dump the AST representation for a type case clause
+
+void
+Type_case_clauses::Type_case_clause::dump_clause(
+ Ast_dump_context* ast_dump_context) const
+{
+ ast_dump_context->print_indent();
+ if (this->is_default_)
+ {
+ ast_dump_context->ostream() << "default:";
+ }
+ else
+ {
+ ast_dump_context->ostream() << "case ";
+ ast_dump_context->dump_type(this->type_);
+ ast_dump_context->ostream() << ":" ;
+ }
+ ast_dump_context->dump_block(this->statements_);
+ if (this->is_fallthrough_)
+ {
+ ast_dump_context->print_indent();
+ ast_dump_context->ostream() << " (fallthrough)" << std::endl;
+ }
+}
+
// Class Type_case_clauses.
// Traversal.
default_case = &*p;
}
}
- gcc_assert(stmts_label == NULL);
+ go_assert(stmts_label == NULL);
if (default_case != NULL)
default_case->lower(b, descriptor_temp, break_label, NULL);
}
+// Dump the AST representation for case clauses (from a switch statement)
+
+void
+Type_case_clauses::dump_clauses(Ast_dump_context* ast_dump_context) const
+{
+ for (Type_clauses::const_iterator p = this->clauses_.begin();
+ p != this->clauses_.end();
+ ++p)
+ p->dump_clause(ast_dump_context);
+}
+
// Class Type_switch_statement.
// Traversal.
// equality testing.
Statement*
-Type_switch_statement::do_lower(Gogo*, Block* enclosing)
+Type_switch_statement::do_lower(Gogo*, Named_object*, Block* enclosing,
+ Statement_inserter*)
{
const source_location loc = this->location();
{
// Doing a type switch on a non-interface type. Should we issue
// a warning for this case?
- // descriptor_temp = DESCRIPTOR
Expression* lhs = Expression::make_temporary_reference(descriptor_temp,
loc);
- Expression* rhs = Expression::make_type_descriptor(val_type, loc);
+ Expression* rhs;
+ if (val_type->is_nil_type())
+ rhs = Expression::make_nil(loc);
+ else
+ {
+ if (val_type->is_abstract())
+ val_type = val_type->make_non_abstract_type();
+ rhs = Expression::make_type_descriptor(val_type, loc);
+ }
Statement* s = Statement::make_assignment(lhs, rhs, loc);
b->add_statement(s);
}
else
{
- const source_location bloc = BUILTINS_LOCATION;
-
- // func {efacetype,ifacetype}(*interface) *descriptor
+ // descriptor_temp = ifacetype(val_temp)
// FIXME: This should be inlined.
- Typed_identifier_list* param_types = new Typed_identifier_list();
- param_types->push_back(Typed_identifier("i", val_type, bloc));
- Typed_identifier_list* ret_types = new Typed_identifier_list();
- ret_types->push_back(Typed_identifier("", descriptor_type, bloc));
- Function_type* fntype = Type::make_function_type(NULL, param_types,
- ret_types, bloc);
bool is_empty = val_type->interface_type()->is_empty();
- const char* fnname = is_empty ? "efacetype" : "ifacetype";
- Named_object* fn =
- Named_object::make_function_declaration(fnname, NULL, fntype, bloc);
- const char* asm_name = (is_empty
- ? "runtime.efacetype"
- : "runtime.ifacetype");
- fn->func_declaration_value()->set_asm_name(asm_name);
-
- // descriptor_temp = ifacetype(val_temp)
- Expression* func = Expression::make_func_reference(fn, NULL, loc);
- Expression_list* params = new Expression_list();
Expression* ref;
if (this->var_ == NULL)
ref = this->expr_;
else
ref = Expression::make_var_reference(this->var_, loc);
- params->push_back(ref);
- Expression* call = Expression::make_call(func, params, false, loc);
- Expression* lhs = Expression::make_temporary_reference(descriptor_temp,
- loc);
+ Expression* call = Runtime::make_call((is_empty
+ ? Runtime::EFACETYPE
+ : Runtime::IFACETYPE),
+ loc, 1, ref);
+ Temporary_reference_expression* lhs =
+ Expression::make_temporary_reference(descriptor_temp, loc);
+ lhs->set_is_lvalue();
Statement* s = Statement::make_assignment(lhs, call, loc);
b->add_statement(s);
}
return this->break_label_;
}
+// Dump the AST representation for a type switch statement
+
+void
+Type_switch_statement::do_dump_statement(Ast_dump_context* ast_dump_context)
+ const
+{
+ ast_dump_context->print_indent();
+ ast_dump_context->ostream() << "switch " << this->var_->name() << " = ";
+ ast_dump_context->dump_expression(this->expr_);
+ ast_dump_context->ostream() << " .(type)";
+ if (ast_dump_context->dump_subblocks())
+ {
+ ast_dump_context->ostream() << " {" << std::endl;
+ this->clauses_->dump_clauses(ast_dump_context);
+ ast_dump_context->ostream() << "}";
+ }
+ ast_dump_context->ostream() << std::endl;
+}
+
// Make a type switch statement.
Type_switch_statement*
return new Type_switch_statement(var, expr, location);
}
+// Class Send_statement.
+
+// Traversal.
+
+int
+Send_statement::do_traverse(Traverse* traverse)
+{
+ if (this->traverse_expression(traverse, &this->channel_) == TRAVERSE_EXIT)
+ return TRAVERSE_EXIT;
+ return this->traverse_expression(traverse, &this->val_);
+}
+
+// Determine types.
+
+void
+Send_statement::do_determine_types()
+{
+ this->channel_->determine_type_no_context();
+ Type* type = this->channel_->type();
+ Type_context context;
+ if (type->channel_type() != NULL)
+ context.type = type->channel_type()->element_type();
+ this->val_->determine_type(&context);
+}
+
+// Check types.
+
+void
+Send_statement::do_check_types(Gogo*)
+{
+ Type* type = this->channel_->type();
+ if (type->is_error())
+ {
+ this->set_is_error();
+ return;
+ }
+ Channel_type* channel_type = type->channel_type();
+ if (channel_type == NULL)
+ {
+ error_at(this->location(), "left operand of %<<-%> must be channel");
+ this->set_is_error();
+ return;
+ }
+ Type* element_type = channel_type->element_type();
+ if (!Type::are_assignable(element_type, this->val_->type(), NULL))
+ {
+ this->report_error(_("incompatible types in send"));
+ return;
+ }
+ if (!channel_type->may_send())
+ {
+ this->report_error(_("invalid send on receive-only channel"));
+ return;
+ }
+}
+
+// Convert a send statement to the backend representation.
+
+Bstatement*
+Send_statement::do_get_backend(Translate_context* context)
+{
+ source_location loc = this->location();
+
+ Channel_type* channel_type = this->channel_->type()->channel_type();
+ Type* element_type = channel_type->element_type();
+ Expression* val = Expression::make_cast(element_type, this->val_, loc);
+
+ bool is_small;
+ bool can_take_address;
+ switch (element_type->base()->classification())
+ {
+ case Type::TYPE_BOOLEAN:
+ case Type::TYPE_INTEGER:
+ case Type::TYPE_FUNCTION:
+ case Type::TYPE_POINTER:
+ case Type::TYPE_MAP:
+ case Type::TYPE_CHANNEL:
+ is_small = true;
+ can_take_address = false;
+ break;
+
+ case Type::TYPE_FLOAT:
+ case Type::TYPE_COMPLEX:
+ case Type::TYPE_STRING:
+ case Type::TYPE_INTERFACE:
+ is_small = false;
+ can_take_address = false;
+ break;
+
+ case Type::TYPE_STRUCT:
+ is_small = false;
+ can_take_address = true;
+ break;
+
+ case Type::TYPE_ARRAY:
+ is_small = false;
+ can_take_address = !element_type->is_open_array_type();
+ break;
+
+ default:
+ case Type::TYPE_ERROR:
+ case Type::TYPE_VOID:
+ case Type::TYPE_SINK:
+ case Type::TYPE_NIL:
+ case Type::TYPE_NAMED:
+ case Type::TYPE_FORWARD:
+ go_assert(saw_errors());
+ return context->backend()->error_statement();
+ }
+
+ // Only try to take the address of a variable. We have already
+ // moved variables to the heap, so this should not cause that to
+ // happen unnecessarily.
+ if (can_take_address
+ && val->var_expression() == NULL
+ && val->temporary_reference_expression() == NULL)
+ can_take_address = false;
+
+ Runtime::Function code;
+ Bstatement* btemp = NULL;
+ Expression* call;
+ if (is_small)
+ {
+ // Type is small enough to handle as uint64.
+ code = Runtime::SEND_SMALL;
+ val = Expression::make_unsafe_cast(Type::lookup_integer_type("uint64"),
+ val, loc);
+ }
+ else if (can_take_address)
+ {
+ // Must pass address of value. The function doesn't change the
+ // value, so just take its address directly.
+ code = Runtime::SEND_BIG;
+ val = Expression::make_unary(OPERATOR_AND, val, loc);
+ }
+ else
+ {
+ // Must pass address of value, but the value is small enough
+ // that it might be in registers. Copy value into temporary
+ // variable to take address.
+ code = Runtime::SEND_BIG;
+ Temporary_statement* temp = Statement::make_temporary(element_type,
+ val, loc);
+ Expression* ref = Expression::make_temporary_reference(temp, loc);
+ val = Expression::make_unary(OPERATOR_AND, ref, loc);
+ btemp = temp->get_backend(context);
+ }
+
+ call = Runtime::make_call(code, loc, 3, this->channel_, val,
+ Expression::make_boolean(this->for_select_, loc));
+
+ context->gogo()->lower_expression(context->function(), NULL, &call);
+ Bexpression* bcall = tree_to_expr(call->get_tree(context));
+ Bstatement* s = context->backend()->expression_statement(bcall);
+
+ if (btemp == NULL)
+ return s;
+ else
+ return context->backend()->compound_statement(btemp, s);
+}
+
+// Dump the AST representation for a send statement
+
+void
+Send_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
+{
+ ast_dump_context->print_indent();
+ ast_dump_context->dump_expression(this->channel_);
+ ast_dump_context->ostream() << " <- ";
+ ast_dump_context->dump_expression(this->val_);
+ ast_dump_context->ostream() << std::endl;
+}
+
+// Make a send statement.
+
+Send_statement*
+Statement::make_send_statement(Expression* channel, Expression* val,
+ source_location location)
+{
+ return new Send_statement(channel, val, location);
+}
+
// Class Select_clauses::Select_clause.
// Traversal.
Select_clauses::Select_clause::traverse(Traverse* traverse)
{
if (!this->is_lowered_
- && (traverse->traverse_mask() & Traverse::traverse_expressions) != 0)
+ && (traverse->traverse_mask()
+ & (Traverse::traverse_types | Traverse::traverse_expressions)) != 0)
{
if (this->channel_ != NULL)
{
if (Expression::traverse(&this->val_, traverse) == TRAVERSE_EXIT)
return TRAVERSE_EXIT;
}
+ if (this->closed_ != NULL)
+ {
+ if (Expression::traverse(&this->closed_, traverse) == TRAVERSE_EXIT)
+ return TRAVERSE_EXIT;
+ }
}
if (this->statements_ != NULL)
{
// receive statements to the clauses.
void
-Select_clauses::Select_clause::lower(Block* b)
+Select_clauses::Select_clause::lower(Gogo* gogo, Named_object* function,
+ Block* b)
{
if (this->is_default_)
{
- gcc_assert(this->channel_ == NULL && this->val_ == NULL);
+ go_assert(this->channel_ == NULL && this->val_ == NULL);
this->is_lowered_ = true;
return;
}
// If this is a send clause, evaluate the value to send before the
// select statement.
Temporary_statement* val_temp = NULL;
- if (this->is_send_)
+ if (this->is_send_ && !this->val_->is_constant())
{
val_temp = Statement::make_temporary(NULL, this->val_, loc);
b->add_statement(val_temp);
Expression* ref = Expression::make_temporary_reference(channel_temp, loc);
if (this->is_send_)
{
- Expression* ref2 = Expression::make_temporary_reference(val_temp, loc);
- Send_expression* send = Expression::make_send(ref, ref2, loc);
- send->discarding_value();
+ Expression* ref2;
+ if (val_temp == NULL)
+ ref2 = this->val_;
+ else
+ ref2 = Expression::make_temporary_reference(val_temp, loc);
+ Send_statement* send = Statement::make_send_statement(ref, ref2, loc);
send->set_for_select();
- init->add_statement(Statement::make_statement(send));
+ init->add_statement(send);
+ }
+ else if (this->closed_ != NULL && !this->closed_->is_sink_expression())
+ {
+ go_assert(this->var_ == NULL && this->closedvar_ == NULL);
+ if (this->val_ == NULL)
+ this->val_ = Expression::make_sink(loc);
+ Statement* s = Statement::make_tuple_receive_assignment(this->val_,
+ this->closed_,
+ ref, true, loc);
+ init->add_statement(s);
+ }
+ else if (this->closedvar_ != NULL)
+ {
+ go_assert(this->val_ == NULL);
+ Expression* val;
+ if (this->var_ == NULL)
+ val = Expression::make_sink(loc);
+ else
+ val = Expression::make_var_reference(this->var_, loc);
+ Expression* closed = Expression::make_var_reference(this->closedvar_,
+ loc);
+ Statement* s = Statement::make_tuple_receive_assignment(val, closed, ref,
+ true, loc);
+ // We have to put S in STATEMENTS_, because that is where the
+ // variables are declared.
+ go_assert(this->statements_ != NULL);
+ this->statements_->add_statement_at_front(s);
+ // We have to lower STATEMENTS_ again, to lower the tuple
+ // receive assignment we just added.
+ gogo->lower_block(function, this->statements_);
}
else
{
recv->set_for_select();
if (this->val_ != NULL)
{
- gcc_assert(this->var_ == NULL);
+ go_assert(this->var_ == NULL);
init->add_statement(Statement::make_assignment(this->val_, recv,
loc));
}
}
else
{
- recv->discarding_value();
init->add_statement(Statement::make_statement(recv));
}
}
+ // Lower any statements we just created.
+ gogo->lower_block(function, init);
+
if (this->statements_ != NULL)
init->add_statement(Statement::make_block_statement(this->statements_,
loc));
void
Select_clauses::Select_clause::determine_types()
{
- gcc_assert(this->is_lowered_);
+ go_assert(this->is_lowered_);
if (this->statements_ != NULL)
this->statements_->determine_types();
}
return this->statements_->may_fall_through();
}
-// Return a tree for the statements to execute.
+// Return the backend representation for the statements to execute.
-tree
-Select_clauses::Select_clause::get_statements_tree(Translate_context* context)
+Bstatement*
+Select_clauses::Select_clause::get_statements_backend(
+ Translate_context* context)
{
if (this->statements_ == NULL)
- return NULL_TREE;
- return this->statements_->get_tree(context);
+ return NULL;
+ Bblock* bblock = this->statements_->get_backend(context);
+ return context->backend()->block_statement(bblock);
+}
+
+// Dump the AST representation for a select case clause
+
+void
+Select_clauses::Select_clause::dump_clause(
+ Ast_dump_context* ast_dump_context) const
+{
+ ast_dump_context->print_indent();
+ if (this->is_default_)
+ {
+ ast_dump_context->ostream() << "default:";
+ }
+ else
+ {
+ ast_dump_context->ostream() << "case " ;
+ if (this->is_send_)
+ {
+ ast_dump_context->dump_expression(this->channel_);
+ ast_dump_context->ostream() << " <- " ;
+ ast_dump_context->dump_expression(this->val_);
+ }
+ else
+ {
+ if (this->val_ != NULL)
+ ast_dump_context->dump_expression(this->val_);
+ if (this->closed_ != NULL)
+ {
+ // FIXME: can val_ == NULL and closed_ ! = NULL?
+ ast_dump_context->ostream() << " , " ;
+ ast_dump_context->dump_expression(this->closed_);
+ }
+ if (this->closedvar_ != NULL ||
+ this->var_ != NULL)
+ ast_dump_context->ostream() << " := " ;
+
+ ast_dump_context->ostream() << " <- " ;
+ ast_dump_context->dump_expression(this->channel_);
+ }
+ ast_dump_context->ostream() << ":" ;
+ }
+ ast_dump_context->dump_block(this->statements_);
}
// Class Select_clauses.
// receive statements to the clauses.
void
-Select_clauses::lower(Block* b)
+Select_clauses::lower(Gogo* gogo, Named_object* function, Block* b)
{
for (Clauses::iterator p = this->clauses_.begin();
p != this->clauses_.end();
++p)
- p->lower(b);
+ p->lower(gogo, function, b);
}
// Determine types.
return false;
}
-// Return a tree. We build a call to
+// Convert to the backend representation. We build a call to
// size_t __go_select(size_t count, _Bool has_default,
// channel* channels, _Bool* is_send)
//
// FIXME: This doesn't handle channels which send interface types
// where the receiver has a static type which matches that interface.
-tree
-Select_clauses::get_tree(Translate_context* context,
- Unnamed_label *break_label,
- source_location location)
+Bstatement*
+Select_clauses::get_backend(Translate_context* context,
+ Unnamed_label *break_label,
+ source_location location)
{
size_t count = this->clauses_.size();
- VEC(constructor_elt, gc)* chan_init = VEC_alloc(constructor_elt, gc, count);
- VEC(constructor_elt, gc)* is_send_init = VEC_alloc(constructor_elt, gc,
- count);
- Select_clause* default_clause = NULL;
- tree final_stmt_list = NULL_TREE;
- tree channel_type_tree = NULL_TREE;
- size_t i = 0;
+ Expression_list* chan_init = new Expression_list();
+ chan_init->reserve(count);
+
+ Expression_list* is_send_init = new Expression_list();
+ is_send_init->reserve(count);
+
+ Select_clause *default_clause = NULL;
+
+ Type* runtime_chanptr_type = Runtime::chanptr_type();
+ Type* runtime_chan_type = runtime_chanptr_type->points_to();
+
for (Clauses::iterator p = this->clauses_.begin();
p != this->clauses_.end();
++p)
continue;
}
- tree channel_tree = p->channel()->get_tree(context);
- if (channel_tree == error_mark_node)
- return error_mark_node;
- channel_type_tree = TREE_TYPE(channel_tree);
-
- constructor_elt* elt = VEC_quick_push(constructor_elt, chan_init, NULL);
- elt->index = build_int_cstu(sizetype, i);
- elt->value = channel_tree;
+ if (p->channel()->type()->channel_type() == NULL)
+ {
+ // We should have given an error in the send or receive
+ // statement we created via lowering.
+ go_assert(saw_errors());
+ return context->backend()->error_statement();
+ }
- elt = VEC_quick_push(constructor_elt, is_send_init, NULL);
- elt->index = build_int_cstu(sizetype, i);
- elt->value = p->is_send() ? boolean_true_node : boolean_false_node;
+ Expression* c = p->channel();
+ c = Expression::make_unsafe_cast(runtime_chan_type, c, p->location());
+ chan_init->push_back(c);
- ++i;
+ is_send_init->push_back(Expression::make_boolean(p->is_send(),
+ p->location()));
}
- gcc_assert(i == count);
- if (i == 0 && default_clause != NULL)
+ if (chan_init->empty())
{
- // There is only a default clause.
- gcc_assert(final_stmt_list == NULL_TREE);
- tree stmt_list = NULL_TREE;
- append_to_statement_list(default_clause->get_statements_tree(context),
- &stmt_list);
- append_to_statement_list(break_label->get_definition(), &stmt_list);
- return stmt_list;
+ go_assert(count == 0);
+ Bstatement* s;
+ Bstatement* ldef = break_label->get_definition(context);
+ if (default_clause != NULL)
+ {
+ // There is a default clause and no cases. Just execute the
+ // default clause.
+ s = default_clause->get_statements_backend(context);
+ }
+ else
+ {
+ // There isn't even a default clause. In this case select
+ // pauses forever. Call the runtime function with nils.
+ mpz_t zval;
+ mpz_init_set_ui(zval, 0);
+ Expression* zero = Expression::make_integer(&zval, NULL, location);
+ mpz_clear(zval);
+ Expression* default_arg = Expression::make_boolean(false, location);
+ Expression* nil1 = Expression::make_nil(location);
+ Expression* nil2 = nil1->copy();
+ Expression* call = Runtime::make_call(Runtime::SELECT, location, 4,
+ zero, default_arg, nil1, nil2);
+ context->gogo()->lower_expression(context->function(), NULL, &call);
+ Bexpression* bcall = tree_to_expr(call->get_tree(context));
+ s = context->backend()->expression_statement(bcall);
+ }
+ if (s == NULL)
+ return ldef;
+ return context->backend()->compound_statement(s, ldef);
}
+ go_assert(count > 0);
- tree pointer_chan_type_tree = (channel_type_tree == NULL_TREE
- ? ptr_type_node
- : build_pointer_type(channel_type_tree));
- tree chans_arg;
- tree pointer_boolean_type_tree = build_pointer_type(boolean_type_node);
- tree is_sends_arg;
+ std::vector<Bstatement*> statements;
- if (i == 0)
- {
- chans_arg = fold_convert_loc(location, pointer_chan_type_tree,
- null_pointer_node);
- is_sends_arg = fold_convert_loc(location, pointer_boolean_type_tree,
- null_pointer_node);
- }
- else
- {
- tree index_type_tree = build_index_type(size_int(count - 1));
- tree chan_array_type_tree = build_array_type(channel_type_tree,
- index_type_tree);
- tree chan_constructor = build_constructor(chan_array_type_tree,
- chan_init);
- tree chan_var = create_tmp_var(chan_array_type_tree, "CHAN");
- DECL_IGNORED_P(chan_var) = 0;
- DECL_INITIAL(chan_var) = chan_constructor;
- DECL_SOURCE_LOCATION(chan_var) = location;
- TREE_ADDRESSABLE(chan_var) = 1;
- tree decl_expr = build1(DECL_EXPR, void_type_node, chan_var);
- SET_EXPR_LOCATION(decl_expr, location);
- append_to_statement_list(decl_expr, &final_stmt_list);
-
- tree is_send_array_type_tree = build_array_type(boolean_type_node,
- index_type_tree);
- tree is_send_constructor = build_constructor(is_send_array_type_tree,
- is_send_init);
- tree is_send_var = create_tmp_var(is_send_array_type_tree, "ISSEND");
- DECL_IGNORED_P(is_send_var) = 0;
- DECL_INITIAL(is_send_var) = is_send_constructor;
- DECL_SOURCE_LOCATION(is_send_var) = location;
- TREE_ADDRESSABLE(is_send_var) = 1;
- decl_expr = build1(DECL_EXPR, void_type_node, is_send_var);
- SET_EXPR_LOCATION(decl_expr, location);
- append_to_statement_list(decl_expr, &final_stmt_list);
-
- chans_arg = fold_convert_loc(location, pointer_chan_type_tree,
- build_fold_addr_expr_loc(location,
- chan_var));
- is_sends_arg = fold_convert_loc(location, pointer_boolean_type_tree,
- build_fold_addr_expr_loc(location,
- is_send_var));
- }
-
- static tree select_fndecl;
- tree call = Gogo::call_builtin(&select_fndecl,
- location,
- "__go_select",
- 4,
- sizetype,
- sizetype,
- size_int(count),
- boolean_type_node,
- (default_clause == NULL
- ? boolean_false_node
- : boolean_true_node),
- pointer_chan_type_tree,
- chans_arg,
- pointer_boolean_type_tree,
- is_sends_arg);
-
- tree stmt_list = NULL_TREE;
+ mpz_t ival;
+ mpz_init_set_ui(ival, count);
+ Expression* ecount = Expression::make_integer(&ival, NULL, location);
+ mpz_clear(ival);
+
+ Type* chan_array_type = Type::make_array_type(runtime_chan_type, ecount);
+ Expression* chans = Expression::make_composite_literal(chan_array_type, 0,
+ false, chan_init,
+ location);
+ context->gogo()->lower_expression(context->function(), NULL, &chans);
+ Temporary_statement* chan_temp = Statement::make_temporary(chan_array_type,
+ chans,
+ location);
+ statements.push_back(chan_temp->get_backend(context));
+
+ Type* is_send_array_type = Type::make_array_type(Type::lookup_bool_type(),
+ ecount->copy());
+ Expression* is_sends = Expression::make_composite_literal(is_send_array_type,
+ 0, false,
+ is_send_init,
+ location);
+ context->gogo()->lower_expression(context->function(), NULL, &is_sends);
+ Temporary_statement* is_send_temp =
+ Statement::make_temporary(is_send_array_type, is_sends, location);
+ statements.push_back(is_send_temp->get_backend(context));
+
+ mpz_init_set_ui(ival, 0);
+ Expression* zero = Expression::make_integer(&ival, NULL, location);
+ mpz_clear(ival);
+
+ Expression* ref = Expression::make_temporary_reference(chan_temp, location);
+ Expression* chan_arg = Expression::make_array_index(ref, zero, NULL,
+ location);
+ chan_arg = Expression::make_unary(OPERATOR_AND, chan_arg, location);
+ chan_arg = Expression::make_unsafe_cast(runtime_chanptr_type, chan_arg,
+ location);
+
+ ref = Expression::make_temporary_reference(is_send_temp, location);
+ Expression* is_send_arg = Expression::make_array_index(ref, zero->copy(),
+ NULL, location);
+ is_send_arg = Expression::make_unary(OPERATOR_AND, is_send_arg, location);
+
+ Expression* default_arg = Expression::make_boolean(default_clause != NULL,
+ location);
+ Expression* call = Runtime::make_call(Runtime::SELECT, location, 4,
+ ecount->copy(), default_arg,
+ chan_arg, is_send_arg);
+ context->gogo()->lower_expression(context->function(), NULL, &call);
+ Bexpression* bcall = tree_to_expr(call->get_tree(context));
+
+ std::vector<std::vector<Bexpression*> > cases;
+ std::vector<Bstatement*> clauses;
+
+ cases.resize(count + (default_clause != NULL ? 1 : 0));
+ clauses.resize(count + (default_clause != NULL ? 1 : 0));
+
+ int index = 0;
if (default_clause != NULL)
- this->add_clause_tree(context, 0, default_clause, break_label, &stmt_list);
+ {
+ this->add_clause_backend(context, location, index, 0, default_clause,
+ break_label, &cases, &clauses);
+ ++index;
+ }
- i = 1;
+ int i = 1;
for (Clauses::iterator p = this->clauses_.begin();
p != this->clauses_.end();
++p)
{
if (!p->is_default())
{
- this->add_clause_tree(context, i, &*p, break_label, &stmt_list);
+ this->add_clause_backend(context, location, index, i, &*p,
+ break_label, &cases, &clauses);
++i;
+ ++index;
}
}
- append_to_statement_list(break_label->get_definition(), &stmt_list);
+ Bstatement* switch_stmt = context->backend()->switch_statement(bcall,
+ cases,
+ clauses,
+ location);
+ statements.push_back(switch_stmt);
+
+ Bstatement* ldef = break_label->get_definition(context);
+ statements.push_back(ldef);
+
+ return context->backend()->statement_list(statements);
+}
+
+// Add CLAUSE to CASES/CLAUSES at INDEX.
+
+void
+Select_clauses::add_clause_backend(
+ Translate_context* context,
+ source_location location,
+ int index,
+ int case_value,
+ Select_clause* clause,
+ Unnamed_label* bottom_label,
+ std::vector<std::vector<Bexpression*> > *cases,
+ std::vector<Bstatement*>* clauses)
+{
+ mpz_t ival;
+ mpz_init_set_ui(ival, case_value);
+ Expression* e = Expression::make_integer(&ival, NULL, location);
+ mpz_clear(ival);
+ (*cases)[index].push_back(tree_to_expr(e->get_tree(context)));
+
+ Bstatement* s = clause->get_statements_backend(context);
- tree switch_stmt = build3(SWITCH_EXPR, sizetype, call, stmt_list, NULL_TREE);
- SET_EXPR_LOCATION(switch_stmt, location);
- append_to_statement_list(switch_stmt, &final_stmt_list);
+ source_location gloc = (clause->statements() == NULL
+ ? clause->location()
+ : clause->statements()->end_location());
+ Bstatement* g = bottom_label->get_goto(context, gloc);
- return final_stmt_list;
+ if (s == NULL)
+ (*clauses)[index] = g;
+ else
+ (*clauses)[index] = context->backend()->compound_statement(s, g);
}
-// Add the tree for CLAUSE to STMT_LIST.
+// Dump the AST representation for select clauses.
void
-Select_clauses::add_clause_tree(Translate_context* context, int case_index,
- Select_clause* clause,
- Unnamed_label* bottom_label, tree* stmt_list)
-{
- tree label = create_artificial_label(clause->location());
- append_to_statement_list(build3(CASE_LABEL_EXPR, void_type_node,
- build_int_cst(sizetype, case_index),
- NULL_TREE, label),
- stmt_list);
- append_to_statement_list(clause->get_statements_tree(context), stmt_list);
- tree g = bottom_label->get_goto(clause->statements() == NULL
- ? clause->location()
- : clause->statements()->end_location());
- append_to_statement_list(g, stmt_list);
+Select_clauses::dump_clauses(Ast_dump_context* ast_dump_context) const
+{
+ for (Clauses::const_iterator p = this->clauses_.begin();
+ p != this->clauses_.end();
+ ++p)
+ p->dump_clause(ast_dump_context);
}
// Class Select_statement.
// explicit statements in the clauses.
Statement*
-Select_statement::do_lower(Gogo*, Block* enclosing)
+Select_statement::do_lower(Gogo* gogo, Named_object* function,
+ Block* enclosing, Statement_inserter*)
{
if (this->is_lowered_)
return this;
Block* b = new Block(enclosing, this->location());
- this->clauses_->lower(b);
+ this->clauses_->lower(gogo, function, b);
this->is_lowered_ = true;
b->add_statement(this);
return Statement::make_block_statement(b, this->location());
}
-// Return the tree for a select statement.
+// Return the backend representation for a select statement.
+
+Bstatement*
+Select_statement::do_get_backend(Translate_context* context)
+{
+ return this->clauses_->get_backend(context, this->break_label(),
+ this->location());
+}
+
+// Dump the AST representation for a select statement.
-tree
-Select_statement::do_get_tree(Translate_context* context)
+void
+Select_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
{
- return this->clauses_->get_tree(context, this->break_label(),
- this->location());
+ ast_dump_context->print_indent();
+ ast_dump_context->ostream() << "select";
+ if (ast_dump_context->dump_subblocks())
+ {
+ ast_dump_context->ostream() << " {" << std::endl;
+ this->clauses_->dump_clauses(ast_dump_context);
+ ast_dump_context->ostream() << "}";
+ }
+ ast_dump_context->ostream() << std::endl;
}
// Make a select statement.
// complex statements make it easier to handle garbage collection.
Statement*
-For_statement::do_lower(Gogo*, Block* enclosing)
+For_statement::do_lower(Gogo*, Named_object*, Block* enclosing,
+ Statement_inserter*)
{
Statement* s;
source_location loc = this->location();
For_statement::set_break_continue_labels(Unnamed_label* break_label,
Unnamed_label* continue_label)
{
- gcc_assert(this->break_label_ == NULL && this->continue_label_ == NULL);
+ go_assert(this->break_label_ == NULL && this->continue_label_ == NULL);
this->break_label_ = break_label;
this->continue_label_ = continue_label;
}
+// Dump the AST representation for a for statement.
+
+void
+For_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
+{
+ if (this->init_ != NULL && ast_dump_context->dump_subblocks())
+ {
+ ast_dump_context->print_indent();
+ ast_dump_context->indent();
+ ast_dump_context->ostream() << "// INIT " << std::endl;
+ ast_dump_context->dump_block(this->init_);
+ ast_dump_context->unindent();
+ }
+ ast_dump_context->print_indent();
+ ast_dump_context->ostream() << "for ";
+ if (this->cond_ != NULL)
+ ast_dump_context->dump_expression(this->cond_);
+
+ if (ast_dump_context->dump_subblocks())
+ {
+ ast_dump_context->ostream() << " {" << std::endl;
+ ast_dump_context->dump_block(this->statements_);
+ if (this->init_ != NULL)
+ {
+ ast_dump_context->print_indent();
+ ast_dump_context->ostream() << "// POST " << std::endl;
+ ast_dump_context->dump_block(this->post_);
+ }
+ ast_dump_context->unindent();
+
+ ast_dump_context->print_indent();
+ ast_dump_context->ostream() << "}";
+ }
+
+ ast_dump_context->ostream() << std::endl;
+}
+
// Make a for statement.
For_statement*
// statements.
Statement*
-For_range_statement::do_lower(Gogo* gogo, Block* enclosing)
+For_range_statement::do_lower(Gogo* gogo, Named_object*, Block* enclosing,
+ Statement_inserter*)
{
Type* range_type = this->range_->type();
if (range_type->points_to() != NULL
index_type = range_type->channel_type()->element_type();
if (this->value_var_ != NULL)
{
- if (!this->value_var_->type()->is_error_type())
+ if (!this->value_var_->type()->is_error())
this->report_error(_("too many variables for range clause "
"with channel"));
return Statement::make_error_statement(this->location());
else
{
this->report_error(_("range clause must have "
- "array, slice, setring, map, or channel type"));
+ "array, slice, string, map, or channel type"));
return Statement::make_error_statement(this->location());
}
{
range_temp = Statement::make_temporary(NULL, this->range_, loc);
temp_block->add_statement(range_temp);
+ this->range_ = NULL;
}
Temporary_statement* index_temp = Statement::make_temporary(index_type,
index_temp, value_temp, &init, &cond, &iter_init,
&post);
else
- gcc_unreachable();
+ go_unreachable();
if (iter_init != NULL)
body->add_statement(Statement::make_block_statement(iter_init, loc));
source_location loc)
{
Named_object* no = gogo->lookup_global(funcname);
- gcc_assert(no != NULL && no->is_function_declaration());
+ go_assert(no != NULL && no->is_function_declaration());
Expression* func = Expression::make_func_reference(no, NULL, loc);
Expression_list* params = new Expression_list();
params->push_back(arg);
Expression* zexpr = Expression::make_integer(&zval, NULL, loc);
mpz_clear(zval);
- ref = Expression::make_temporary_reference(index_temp, loc);
- Statement* s = Statement::make_assignment(ref, zexpr, loc);
+ Temporary_reference_expression* tref =
+ Expression::make_temporary_reference(index_temp, loc);
+ tref->set_is_lvalue();
+ Statement* s = Statement::make_assignment(tref, zexpr, loc);
init->add_statement(s);
*pinit = init;
Expression* ref2 = Expression::make_temporary_reference(index_temp, loc);
Expression* index = Expression::make_index(ref, ref2, NULL, loc);
- ref = Expression::make_temporary_reference(value_temp, loc);
- s = Statement::make_assignment(ref, index, loc);
+ tref = Expression::make_temporary_reference(value_temp, loc);
+ tref->set_is_lvalue();
+ s = Statement::make_assignment(tref, index, loc);
iter_init->add_statement(s);
}
// index_temp++
Block* post = new Block(enclosing, loc);
- ref = Expression::make_temporary_reference(index_temp, loc);
- s = Statement::make_inc_statement(ref);
+ tref = Expression::make_temporary_reference(index_temp, loc);
+ tref->set_is_lvalue();
+ s = Statement::make_inc_statement(tref);
post->add_statement(s);
*ppost = post;
}
// Lower a for range over a string.
void
-For_range_statement::lower_range_string(Gogo* gogo,
+For_range_statement::lower_range_string(Gogo*,
Block* enclosing,
Block* body_block,
Named_object* range_object,
mpz_init_set_ui(zval, 0UL);
Expression* zexpr = Expression::make_integer(&zval, NULL, loc);
- Expression* ref = Expression::make_temporary_reference(index_temp, loc);
+ Temporary_reference_expression* ref =
+ Expression::make_temporary_reference(index_temp, loc);
+ ref->set_is_lvalue();
Statement* s = Statement::make_assignment(ref, zexpr, loc);
init->add_statement(s);
Block* iter_init = new Block(body_block, loc);
- Named_object* no;
- if (value_temp == NULL)
- {
- static Named_object* stringiter;
- if (stringiter == NULL)
- {
- source_location bloc = BUILTINS_LOCATION;
- Type* int_type = gogo->lookup_global("int")->type_value();
-
- Typed_identifier_list* params = new Typed_identifier_list();
- params->push_back(Typed_identifier("s", Type::make_string_type(),
- bloc));
- params->push_back(Typed_identifier("k", int_type, bloc));
-
- Typed_identifier_list* results = new Typed_identifier_list();
- results->push_back(Typed_identifier("", int_type, bloc));
-
- Function_type* fntype = Type::make_function_type(NULL, params,
- results, bloc);
- stringiter = Named_object::make_function_declaration("stringiter",
- NULL, fntype,
- bloc);
- const char* n = "runtime.stringiter";
- stringiter->func_declaration_value()->set_asm_name(n);
- }
- no = stringiter;
- }
- else
- {
- static Named_object* stringiter2;
- if (stringiter2 == NULL)
- {
- source_location bloc = BUILTINS_LOCATION;
- Type* int_type = gogo->lookup_global("int")->type_value();
-
- Typed_identifier_list* params = new Typed_identifier_list();
- params->push_back(Typed_identifier("s", Type::make_string_type(),
- bloc));
- params->push_back(Typed_identifier("k", int_type, bloc));
-
- Typed_identifier_list* results = new Typed_identifier_list();
- results->push_back(Typed_identifier("", int_type, bloc));
- results->push_back(Typed_identifier("", int_type, bloc));
-
- Function_type* fntype = Type::make_function_type(NULL, params,
- results, bloc);
- stringiter2 = Named_object::make_function_declaration("stringiter",
- NULL, fntype,
- bloc);
- const char* n = "runtime.stringiter2";
- stringiter2->func_declaration_value()->set_asm_name(n);
- }
- no = stringiter2;
- }
-
- Expression* func = Expression::make_func_reference(no, NULL, loc);
- Expression_list* params = new Expression_list();
- params->push_back(this->make_range_ref(range_object, range_temp, loc));
- params->push_back(Expression::make_temporary_reference(index_temp, loc));
- Call_expression* call = Expression::make_call(func, params, false, loc);
+ Expression* p1 = this->make_range_ref(range_object, range_temp, loc);
+ Expression* p2 = Expression::make_temporary_reference(index_temp, loc);
+ Call_expression* call = Runtime::make_call((value_temp == NULL
+ ? Runtime::STRINGITER
+ : Runtime::STRINGITER2),
+ loc, 2, p1, p2);
if (value_temp == NULL)
{
ref = Expression::make_temporary_reference(next_index_temp, loc);
+ ref->set_is_lvalue();
s = Statement::make_assignment(ref, call, loc);
}
else
{
Expression_list* lhs = new Expression_list();
- lhs->push_back(Expression::make_temporary_reference(next_index_temp,
- loc));
- lhs->push_back(Expression::make_temporary_reference(value_temp, loc));
+
+ ref = Expression::make_temporary_reference(next_index_temp, loc);
+ ref->set_is_lvalue();
+ lhs->push_back(ref);
+
+ ref = Expression::make_temporary_reference(value_temp, loc);
+ ref->set_is_lvalue();
+ lhs->push_back(ref);
Expression_list* rhs = new Expression_list();
rhs->push_back(Expression::make_call_result(call, 0));
Block* post = new Block(enclosing, loc);
- Expression* lhs = Expression::make_temporary_reference(index_temp, loc);
+ Temporary_reference_expression* lhs =
+ Expression::make_temporary_reference(index_temp, loc);
+ lhs->set_is_lvalue();
Expression* rhs = Expression::make_temporary_reference(next_index_temp, loc);
s = Statement::make_assignment(lhs, rhs, loc);
// Lower a for range over a map.
void
-For_range_statement::lower_range_map(Gogo* gogo,
+For_range_statement::lower_range_map(Gogo*,
Block* enclosing,
Block* body_block,
Named_object* range_object,
Block* init = new Block(enclosing, loc);
- const unsigned long map_iteration_size = 4;
-
- mpz_t ival;
- mpz_init_set_ui(ival, map_iteration_size);
- Expression* iexpr = Expression::make_integer(&ival, NULL, loc);
- mpz_clear(ival);
-
- Type* byte_type = gogo->lookup_global("byte")->type_value();
- Type* ptr_type = Type::make_pointer_type(byte_type);
-
- Type* map_iteration_type = Type::make_array_type(ptr_type, iexpr);
- Type* map_iteration_ptr = Type::make_pointer_type(map_iteration_type);
-
+ Type* map_iteration_type = Runtime::map_iteration_type();
Temporary_statement* hiter = Statement::make_temporary(map_iteration_type,
NULL, loc);
init->add_statement(hiter);
- source_location bloc = BUILTINS_LOCATION;
- Typed_identifier_list* param_types = new Typed_identifier_list();
- param_types->push_back(Typed_identifier("map", this->range_->type(), bloc));
- param_types->push_back(Typed_identifier("it", map_iteration_ptr, bloc));
- Function_type* fntype = Type::make_function_type(NULL, param_types, NULL,
- bloc);
-
- Named_object* mapiterinit =
- Named_object::make_function_declaration("mapiterinit", NULL, fntype, bloc);
- const char* n = "runtime.mapiterinit";
- mapiterinit->func_declaration_value()->set_asm_name(n);
-
- Expression* func = Expression::make_func_reference(mapiterinit, NULL, loc);
- Expression_list* params = new Expression_list();
- params->push_back(this->make_range_ref(range_object, range_temp, loc));
+ Expression* p1 = this->make_range_ref(range_object, range_temp, loc);
Expression* ref = Expression::make_temporary_reference(hiter, loc);
- params->push_back(Expression::make_unary(OPERATOR_AND, ref, loc));
- Expression* call = Expression::make_call(func, params, false, loc);
+ Expression* p2 = Expression::make_unary(OPERATOR_AND, ref, loc);
+ Expression* call = Runtime::make_call(Runtime::MAPITERINIT, loc, 2, p1, p2);
init->add_statement(Statement::make_statement(call));
*pinit = init;
Block* iter_init = new Block(body_block, loc);
- param_types = new Typed_identifier_list();
- param_types->push_back(Typed_identifier("hiter", map_iteration_ptr, bloc));
- Type* pkey_type = Type::make_pointer_type(index_temp->type());
- param_types->push_back(Typed_identifier("key", pkey_type, bloc));
- if (value_temp != NULL)
- {
- Type* pval_type = Type::make_pointer_type(value_temp->type());
- param_types->push_back(Typed_identifier("val", pval_type, bloc));
- }
- fntype = Type::make_function_type(NULL, param_types, NULL, bloc);
- n = value_temp == NULL ? "mapiter1" : "mapiter2";
- Named_object* mapiter = Named_object::make_function_declaration(n, NULL,
- fntype, bloc);
- n = value_temp == NULL ? "runtime.mapiter1" : "runtime.mapiter2";
- mapiter->func_declaration_value()->set_asm_name(n);
-
- func = Expression::make_func_reference(mapiter, NULL, loc);
- params = new Expression_list();
ref = Expression::make_temporary_reference(hiter, loc);
- params->push_back(Expression::make_unary(OPERATOR_AND, ref, loc));
+ p1 = Expression::make_unary(OPERATOR_AND, ref, loc);
ref = Expression::make_temporary_reference(index_temp, loc);
- params->push_back(Expression::make_unary(OPERATOR_AND, ref, loc));
- if (value_temp != NULL)
+ p2 = Expression::make_unary(OPERATOR_AND, ref, loc);
+ if (value_temp == NULL)
+ call = Runtime::make_call(Runtime::MAPITER1, loc, 2, p1, p2);
+ else
{
ref = Expression::make_temporary_reference(value_temp, loc);
- params->push_back(Expression::make_unary(OPERATOR_AND, ref, loc));
+ Expression* p3 = Expression::make_unary(OPERATOR_AND, ref, loc);
+ call = Runtime::make_call(Runtime::MAPITER2, loc, 3, p1, p2, p3);
}
- call = Expression::make_call(func, params, false, loc);
iter_init->add_statement(Statement::make_statement(call));
*piter_init = iter_init;
Block* post = new Block(enclosing, loc);
- static Named_object* mapiternext;
- if (mapiternext == NULL)
- {
- param_types = new Typed_identifier_list();
- param_types->push_back(Typed_identifier("it", map_iteration_ptr, bloc));
- fntype = Type::make_function_type(NULL, param_types, NULL, bloc);
- mapiternext = Named_object::make_function_declaration("mapiternext",
- NULL, fntype,
- bloc);
- const char* n = "runtime.mapiternext";
- mapiternext->func_declaration_value()->set_asm_name(n);
- }
-
- func = Expression::make_func_reference(mapiternext, NULL, loc);
- params = new Expression_list();
ref = Expression::make_temporary_reference(hiter, loc);
- params->push_back(Expression::make_unary(OPERATOR_AND, ref, loc));
- call = Expression::make_call(func, params, false, loc);
+ p1 = Expression::make_unary(OPERATOR_AND, ref, loc);
+ call = Runtime::make_call(Runtime::MAPITERNEXT, loc, 1, p1);
post->add_statement(Statement::make_statement(call));
*ppost = post;
// Lower a for range over a channel.
void
-For_range_statement::lower_range_channel(Gogo* gogo,
+For_range_statement::lower_range_channel(Gogo*,
Block*,
Block* body_block,
Named_object* range_object,
Block** piter_init,
Block** ppost)
{
- gcc_assert(value_temp == NULL);
+ go_assert(value_temp == NULL);
source_location loc = this->location();
// The loop we generate:
// for {
- // index_temp = <-range
- // if closed(range) {
+ // index_temp, ok_temp = <-range
+ // if !ok_temp {
// break
// }
// index = index_temp
- // value = value_temp
// original body
// }
*ppost = NULL;
// Set *PITER_INIT to
- // index_temp = <-range
- // if closed(range) {
+ // index_temp, ok_temp = <-range
+ // if !ok_temp {
// break
// }
Block* iter_init = new Block(body_block, loc);
- Expression* ref = this->make_range_ref(range_object, range_temp, loc);
- Expression* cond = this->call_builtin(gogo, "closed", ref, loc);
-
- ref = this->make_range_ref(range_object, range_temp, loc);
- Expression* recv = Expression::make_receive(ref, loc);
- ref = Expression::make_temporary_reference(index_temp, loc);
- Statement* s = Statement::make_assignment(ref, recv, loc);
+ Temporary_statement* ok_temp =
+ Statement::make_temporary(Type::lookup_bool_type(), NULL, loc);
+ iter_init->add_statement(ok_temp);
+
+ Expression* cref = this->make_range_ref(range_object, range_temp, loc);
+ Temporary_reference_expression* iref =
+ Expression::make_temporary_reference(index_temp, loc);
+ iref->set_is_lvalue();
+ Temporary_reference_expression* oref =
+ Expression::make_temporary_reference(ok_temp, loc);
+ oref->set_is_lvalue();
+ Statement* s = Statement::make_tuple_receive_assignment(iref, oref, cref,
+ false, loc);
iter_init->add_statement(s);
Block* then_block = new Block(iter_init, loc);
s = Statement::make_break_statement(this->break_label(), loc);
then_block->add_statement(s);
+ oref = Expression::make_temporary_reference(ok_temp, loc);
+ Expression* cond = Expression::make_unary(OPERATOR_NOT, oref, loc);
s = Statement::make_if_statement(cond, then_block, NULL, loc);
iter_init->add_statement(s);
return this->continue_label_;
}
+// Dump the AST representation for a for range statement.
+
+void
+For_range_statement::do_dump_statement(Ast_dump_context* ast_dump_context) const
+{
+
+ ast_dump_context->print_indent();
+ ast_dump_context->ostream() << "for ";
+ ast_dump_context->dump_expression(this->index_var_);
+ if (this->value_var_ != NULL)
+ {
+ ast_dump_context->ostream() << ", ";
+ ast_dump_context->dump_expression(this->value_var_);
+ }
+
+ ast_dump_context->ostream() << " = range ";
+ ast_dump_context->dump_expression(this->range_);
+ if (ast_dump_context->dump_subblocks())
+ {
+ ast_dump_context->ostream() << " {" << std::endl;
+
+ ast_dump_context->indent();
+
+ ast_dump_context->dump_block(this->statements_);
+
+ ast_dump_context->unindent();
+ ast_dump_context->print_indent();
+ ast_dump_context->ostream() << "}";
+ }
+ ast_dump_context->ostream() << std::endl;
+}
+
// Make a for statement with a range clause.
For_range_statement*