1 // gogo.cc -- Go frontend parsed representation.
3 // Copyright 2009 The Go Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
13 #include "statements.h"
14 #include "expressions.h"
24 Gogo::Gogo(Backend* backend, int int_type_size, int pointer_size)
28 globals_(new Bindings(NULL)),
30 imported_unsafe_(false),
37 unique_prefix_specified_(false),
39 named_types_are_converted_(false)
41 const source_location loc = BUILTINS_LOCATION;
43 Named_type* uint8_type = Type::make_integer_type("uint8", true, 8,
44 RUNTIME_TYPE_KIND_UINT8);
45 this->add_named_type(uint8_type);
46 this->add_named_type(Type::make_integer_type("uint16", true, 16,
47 RUNTIME_TYPE_KIND_UINT16));
48 this->add_named_type(Type::make_integer_type("uint32", true, 32,
49 RUNTIME_TYPE_KIND_UINT32));
50 this->add_named_type(Type::make_integer_type("uint64", true, 64,
51 RUNTIME_TYPE_KIND_UINT64));
53 this->add_named_type(Type::make_integer_type("int8", false, 8,
54 RUNTIME_TYPE_KIND_INT8));
55 this->add_named_type(Type::make_integer_type("int16", false, 16,
56 RUNTIME_TYPE_KIND_INT16));
57 this->add_named_type(Type::make_integer_type("int32", false, 32,
58 RUNTIME_TYPE_KIND_INT32));
59 this->add_named_type(Type::make_integer_type("int64", false, 64,
60 RUNTIME_TYPE_KIND_INT64));
62 this->add_named_type(Type::make_float_type("float32", 32,
63 RUNTIME_TYPE_KIND_FLOAT32));
64 this->add_named_type(Type::make_float_type("float64", 64,
65 RUNTIME_TYPE_KIND_FLOAT64));
67 this->add_named_type(Type::make_complex_type("complex64", 64,
68 RUNTIME_TYPE_KIND_COMPLEX64));
69 this->add_named_type(Type::make_complex_type("complex128", 128,
70 RUNTIME_TYPE_KIND_COMPLEX128));
72 if (int_type_size < 32)
74 this->add_named_type(Type::make_integer_type("uint", true,
76 RUNTIME_TYPE_KIND_UINT));
77 Named_type* int_type = Type::make_integer_type("int", false, int_type_size,
78 RUNTIME_TYPE_KIND_INT);
79 this->add_named_type(int_type);
81 // "byte" is an alias for "uint8". Construct a Named_object which
82 // points to UINT8_TYPE. Note that this breaks the normal pairing
83 // in which a Named_object points to a Named_type which points back
84 // to the same Named_object.
85 Named_object* byte_type = this->declare_type("byte", loc);
86 byte_type->set_type_value(uint8_type);
88 this->add_named_type(Type::make_integer_type("uintptr", true,
90 RUNTIME_TYPE_KIND_UINTPTR));
92 this->add_named_type(Type::make_named_bool_type());
94 this->add_named_type(Type::make_named_string_type());
96 this->globals_->add_constant(Typed_identifier("true",
97 Type::make_boolean_type(),
100 Expression::make_boolean(true, loc),
102 this->globals_->add_constant(Typed_identifier("false",
103 Type::make_boolean_type(),
106 Expression::make_boolean(false, loc),
109 this->globals_->add_constant(Typed_identifier("nil", Type::make_nil_type(),
112 Expression::make_nil(loc),
115 Type* abstract_int_type = Type::make_abstract_integer_type();
116 this->globals_->add_constant(Typed_identifier("iota", abstract_int_type,
119 Expression::make_iota(),
122 Function_type* new_type = Type::make_function_type(NULL, NULL, NULL, loc);
123 new_type->set_is_varargs();
124 new_type->set_is_builtin();
125 this->globals_->add_function_declaration("new", NULL, new_type, loc);
127 Function_type* make_type = Type::make_function_type(NULL, NULL, NULL, loc);
128 make_type->set_is_varargs();
129 make_type->set_is_builtin();
130 this->globals_->add_function_declaration("make", NULL, make_type, loc);
132 Typed_identifier_list* len_result = new Typed_identifier_list();
133 len_result->push_back(Typed_identifier("", int_type, loc));
134 Function_type* len_type = Type::make_function_type(NULL, NULL, len_result,
136 len_type->set_is_builtin();
137 this->globals_->add_function_declaration("len", NULL, len_type, loc);
139 Typed_identifier_list* cap_result = new Typed_identifier_list();
140 cap_result->push_back(Typed_identifier("", int_type, loc));
141 Function_type* cap_type = Type::make_function_type(NULL, NULL, len_result,
143 cap_type->set_is_builtin();
144 this->globals_->add_function_declaration("cap", NULL, cap_type, loc);
146 Function_type* print_type = Type::make_function_type(NULL, NULL, NULL, loc);
147 print_type->set_is_varargs();
148 print_type->set_is_builtin();
149 this->globals_->add_function_declaration("print", NULL, print_type, loc);
151 print_type = Type::make_function_type(NULL, NULL, NULL, loc);
152 print_type->set_is_varargs();
153 print_type->set_is_builtin();
154 this->globals_->add_function_declaration("println", NULL, print_type, loc);
156 Type *empty = Type::make_interface_type(NULL, loc);
157 Typed_identifier_list* panic_parms = new Typed_identifier_list();
158 panic_parms->push_back(Typed_identifier("e", empty, loc));
159 Function_type *panic_type = Type::make_function_type(NULL, panic_parms,
161 panic_type->set_is_builtin();
162 this->globals_->add_function_declaration("panic", NULL, panic_type, loc);
164 Typed_identifier_list* recover_result = new Typed_identifier_list();
165 recover_result->push_back(Typed_identifier("", empty, loc));
166 Function_type* recover_type = Type::make_function_type(NULL, NULL,
169 recover_type->set_is_builtin();
170 this->globals_->add_function_declaration("recover", NULL, recover_type, loc);
172 Function_type* close_type = Type::make_function_type(NULL, NULL, NULL, loc);
173 close_type->set_is_varargs();
174 close_type->set_is_builtin();
175 this->globals_->add_function_declaration("close", NULL, close_type, loc);
177 Typed_identifier_list* copy_result = new Typed_identifier_list();
178 copy_result->push_back(Typed_identifier("", int_type, loc));
179 Function_type* copy_type = Type::make_function_type(NULL, NULL,
181 copy_type->set_is_varargs();
182 copy_type->set_is_builtin();
183 this->globals_->add_function_declaration("copy", NULL, copy_type, loc);
185 Function_type* append_type = Type::make_function_type(NULL, NULL, NULL, loc);
186 append_type->set_is_varargs();
187 append_type->set_is_builtin();
188 this->globals_->add_function_declaration("append", NULL, append_type, loc);
190 Function_type* complex_type = Type::make_function_type(NULL, NULL, NULL, loc);
191 complex_type->set_is_varargs();
192 complex_type->set_is_builtin();
193 this->globals_->add_function_declaration("complex", NULL, complex_type, loc);
195 Function_type* real_type = Type::make_function_type(NULL, NULL, NULL, loc);
196 real_type->set_is_varargs();
197 real_type->set_is_builtin();
198 this->globals_->add_function_declaration("real", NULL, real_type, loc);
200 Function_type* imag_type = Type::make_function_type(NULL, NULL, NULL, loc);
201 imag_type->set_is_varargs();
202 imag_type->set_is_builtin();
203 this->globals_->add_function_declaration("imag", NULL, imag_type, loc);
206 // Munge name for use in an error message.
209 Gogo::message_name(const std::string& name)
211 return go_localize_identifier(Gogo::unpack_hidden_name(name).c_str());
214 // Get the package name.
217 Gogo::package_name() const
219 go_assert(this->package_ != NULL);
220 return this->package_->name();
223 // Set the package name.
226 Gogo::set_package_name(const std::string& package_name,
227 source_location location)
229 if (this->package_ != NULL && this->package_->name() != package_name)
231 error_at(location, "expected package %<%s%>",
232 Gogo::message_name(this->package_->name()).c_str());
236 // If the user did not specify a unique prefix, we always use "go".
237 // This in effect requires that the package name be unique.
238 if (this->unique_prefix_.empty())
239 this->unique_prefix_ = "go";
241 this->package_ = this->register_package(package_name, this->unique_prefix_,
244 // We used to permit people to qualify symbols with the current
245 // package name (e.g., P.x), but we no longer do.
246 // this->globals_->add_package(package_name, this->package_);
248 if (this->is_main_package())
250 // Declare "main" as a function which takes no parameters and
252 this->declare_function("main",
253 Type::make_function_type(NULL, NULL, NULL,
259 // Return whether this is the "main" package. This is not true if
260 // -fgo-prefix was used.
263 Gogo::is_main_package() const
265 return this->package_name() == "main" && !this->unique_prefix_specified_;
271 Gogo::import_package(const std::string& filename,
272 const std::string& local_name,
273 bool is_local_name_exported,
274 source_location location)
276 if (filename == "unsafe")
278 this->import_unsafe(local_name, is_local_name_exported, location);
282 Imports::const_iterator p = this->imports_.find(filename);
283 if (p != this->imports_.end())
285 Package* package = p->second;
286 package->set_location(location);
287 package->set_is_imported();
288 std::string ln = local_name;
289 bool is_ln_exported = is_local_name_exported;
292 ln = package->name();
293 is_ln_exported = Lex::is_exported_name(ln);
297 Bindings* bindings = package->bindings();
298 for (Bindings::const_declarations_iterator p =
299 bindings->begin_declarations();
300 p != bindings->end_declarations();
302 this->add_named_object(p->second);
305 package->set_uses_sink_alias();
308 ln = this->pack_hidden_name(ln, is_ln_exported);
309 this->package_->bindings()->add_package(ln, package);
314 Import::Stream* stream = Import::open_package(filename, location);
317 error_at(location, "import file %qs not found", filename.c_str());
321 Import imp(stream, location);
322 imp.register_builtin_types(this);
323 Package* package = imp.import(this, local_name, is_local_name_exported);
326 if (package->name() == this->package_name()
327 && package->unique_prefix() == this->unique_prefix())
329 ("imported package uses same package name and prefix "
330 "as package being compiled (see -fgo-prefix option)"));
332 this->imports_.insert(std::make_pair(filename, package));
333 package->set_is_imported();
339 // Add an import control function for an imported package to the list.
342 Gogo::add_import_init_fn(const std::string& package_name,
343 const std::string& init_name, int prio)
345 for (std::set<Import_init>::const_iterator p =
346 this->imported_init_fns_.begin();
347 p != this->imported_init_fns_.end();
350 if (p->init_name() == init_name
351 && (p->package_name() != package_name || p->priority() != prio))
353 error("duplicate package initialization name %qs",
354 Gogo::message_name(init_name).c_str());
355 inform(UNKNOWN_LOCATION, "used by package %qs at priority %d",
356 Gogo::message_name(p->package_name()).c_str(),
358 inform(UNKNOWN_LOCATION, " and by package %qs at priority %d",
359 Gogo::message_name(package_name).c_str(), prio);
364 this->imported_init_fns_.insert(Import_init(package_name, init_name,
368 // Return whether we are at the global binding level.
371 Gogo::in_global_scope() const
373 return this->functions_.empty();
376 // Return the current binding contour.
379 Gogo::current_bindings()
381 if (!this->functions_.empty())
382 return this->functions_.back().blocks.back()->bindings();
383 else if (this->package_ != NULL)
384 return this->package_->bindings();
386 return this->globals_;
390 Gogo::current_bindings() const
392 if (!this->functions_.empty())
393 return this->functions_.back().blocks.back()->bindings();
394 else if (this->package_ != NULL)
395 return this->package_->bindings();
397 return this->globals_;
400 // Return the current block.
403 Gogo::current_block()
405 if (this->functions_.empty())
408 return this->functions_.back().blocks.back();
411 // Look up a name in the current binding contour. If PFUNCTION is not
412 // NULL, set it to the function in which the name is defined, or NULL
413 // if the name is defined in global scope.
416 Gogo::lookup(const std::string& name, Named_object** pfunction) const
418 if (pfunction != NULL)
421 if (Gogo::is_sink_name(name))
422 return Named_object::make_sink();
424 for (Open_functions::const_reverse_iterator p = this->functions_.rbegin();
425 p != this->functions_.rend();
428 Named_object* ret = p->blocks.back()->bindings()->lookup(name);
431 if (pfunction != NULL)
432 *pfunction = p->function;
437 if (this->package_ != NULL)
439 Named_object* ret = this->package_->bindings()->lookup(name);
442 if (ret->package() != NULL)
443 ret->package()->set_used();
448 // We do not look in the global namespace. If we did, the global
449 // namespace would effectively hide names which were defined in
450 // package scope which we have not yet seen. Instead,
451 // define_global_names is called after parsing is over to connect
452 // undefined names at package scope with names defined at global
458 // Look up a name in the current block, without searching enclosing
462 Gogo::lookup_in_block(const std::string& name) const
464 go_assert(!this->functions_.empty());
465 go_assert(!this->functions_.back().blocks.empty());
466 return this->functions_.back().blocks.back()->bindings()->lookup_local(name);
469 // Look up a name in the global namespace.
472 Gogo::lookup_global(const char* name) const
474 return this->globals_->lookup(name);
477 // Add an imported package.
480 Gogo::add_imported_package(const std::string& real_name,
481 const std::string& alias_arg,
482 bool is_alias_exported,
483 const std::string& unique_prefix,
484 source_location location,
485 bool* padd_to_globals)
487 // FIXME: Now that we compile packages as a whole, should we permit
488 // importing the current package?
489 if (this->package_name() == real_name
490 && this->unique_prefix() == unique_prefix)
492 *padd_to_globals = false;
493 if (!alias_arg.empty() && alias_arg != ".")
495 std::string alias = this->pack_hidden_name(alias_arg,
497 this->package_->bindings()->add_package(alias, this->package_);
499 return this->package_;
501 else if (alias_arg == ".")
503 *padd_to_globals = true;
504 return this->register_package(real_name, unique_prefix, location);
506 else if (alias_arg == "_")
508 Package* ret = this->register_package(real_name, unique_prefix, location);
509 ret->set_uses_sink_alias();
514 *padd_to_globals = false;
515 std::string alias = alias_arg;
519 is_alias_exported = Lex::is_exported_name(alias);
521 alias = this->pack_hidden_name(alias, is_alias_exported);
522 Named_object* no = this->add_package(real_name, alias, unique_prefix,
524 if (!no->is_package())
526 return no->package_value();
533 Gogo::add_package(const std::string& real_name, const std::string& alias,
534 const std::string& unique_prefix, source_location location)
536 go_assert(this->in_global_scope());
538 // Register the package. Note that we might have already seen it in
539 // an earlier import.
540 Package* package = this->register_package(real_name, unique_prefix, location);
542 return this->package_->bindings()->add_package(alias, package);
545 // Register a package. This package may or may not be imported. This
546 // returns the Package structure for the package, creating if it
550 Gogo::register_package(const std::string& package_name,
551 const std::string& unique_prefix,
552 source_location location)
554 go_assert(!unique_prefix.empty() && !package_name.empty());
555 std::string name = unique_prefix + '.' + package_name;
556 Package* package = NULL;
557 std::pair<Packages::iterator, bool> ins =
558 this->packages_.insert(std::make_pair(name, package));
561 // We have seen this package name before.
562 package = ins.first->second;
563 go_assert(package != NULL);
564 go_assert(package->name() == package_name
565 && package->unique_prefix() == unique_prefix);
566 if (package->location() == UNKNOWN_LOCATION)
567 package->set_location(location);
571 // First time we have seen this package name.
572 package = new Package(package_name, unique_prefix, location);
573 go_assert(ins.first->second == NULL);
574 ins.first->second = package;
580 // Start compiling a function.
583 Gogo::start_function(const std::string& name, Function_type* type,
584 bool add_method_to_type, source_location location)
586 bool at_top_level = this->functions_.empty();
588 Block* block = new Block(NULL, location);
590 Function* enclosing = (at_top_level
592 : this->functions_.back().function->func_value());
594 Function* function = new Function(type, enclosing, block, location);
596 if (type->is_method())
598 const Typed_identifier* receiver = type->receiver();
599 Variable* this_param = new Variable(receiver->type(), NULL, false,
600 true, true, location);
601 std::string name = receiver->name();
604 // We need to give receivers a name since they wind up in
605 // DECL_ARGUMENTS. FIXME.
606 static unsigned int count;
608 snprintf(buf, sizeof buf, "r.%u", count);
612 block->bindings()->add_variable(name, NULL, this_param);
615 const Typed_identifier_list* parameters = type->parameters();
616 bool is_varargs = type->is_varargs();
617 if (parameters != NULL)
619 for (Typed_identifier_list::const_iterator p = parameters->begin();
620 p != parameters->end();
623 Variable* param = new Variable(p->type(), NULL, false, true, false,
625 if (is_varargs && p + 1 == parameters->end())
626 param->set_is_varargs_parameter();
628 std::string name = p->name();
629 if (name.empty() || Gogo::is_sink_name(name))
631 // We need to give parameters a name since they wind up
632 // in DECL_ARGUMENTS. FIXME.
633 static unsigned int count;
635 snprintf(buf, sizeof buf, "p.%u", count);
639 block->bindings()->add_variable(name, NULL, param);
643 function->create_result_variables(this);
645 const std::string* pname;
646 std::string nested_name;
647 bool is_init = false;
648 if (Gogo::unpack_hidden_name(name) == "init" && !type->is_method())
650 if ((type->parameters() != NULL && !type->parameters()->empty())
651 || (type->results() != NULL && !type->results()->empty()))
653 "func init must have no arguments and no return values");
654 // There can be multiple "init" functions, so give them each a
656 static int init_count;
658 snprintf(buf, sizeof buf, ".$init%d", init_count);
661 pname = &nested_name;
664 else if (!name.empty())
668 // Invent a name for a nested function.
669 static int nested_count;
671 snprintf(buf, sizeof buf, ".$nested%d", nested_count);
674 pname = &nested_name;
678 if (Gogo::is_sink_name(*pname))
680 static int sink_count;
682 snprintf(buf, sizeof buf, ".$sink%d", sink_count);
684 ret = Named_object::make_function(buf, NULL, function);
686 else if (!type->is_method())
688 ret = this->package_->bindings()->add_function(*pname, NULL, function);
689 if (!ret->is_function() || ret->func_value() != function)
691 // Redefinition error. Invent a name to avoid knockon
693 static int redefinition_count;
695 snprintf(buf, sizeof buf, ".$redefined%d", redefinition_count);
696 ++redefinition_count;
697 ret = this->package_->bindings()->add_function(buf, NULL, function);
702 if (!add_method_to_type)
703 ret = Named_object::make_function(name, NULL, function);
706 go_assert(at_top_level);
707 Type* rtype = type->receiver()->type();
709 // We want to look through the pointer created by the
710 // parser, without getting an error if the type is not yet
712 if (rtype->classification() == Type::TYPE_POINTER)
713 rtype = rtype->points_to();
715 if (rtype->is_error_type())
716 ret = Named_object::make_function(name, NULL, function);
717 else if (rtype->named_type() != NULL)
719 ret = rtype->named_type()->add_method(name, function);
720 if (!ret->is_function())
722 // Redefinition error.
723 ret = Named_object::make_function(name, NULL, function);
726 else if (rtype->forward_declaration_type() != NULL)
728 Named_object* type_no =
729 rtype->forward_declaration_type()->named_object();
730 if (type_no->is_unknown())
732 // If we are seeing methods it really must be a
733 // type. Declare it as such. An alternative would
734 // be to support lists of methods for unknown
735 // expressions. Either way the error messages if
736 // this is not a type are going to get confusing.
737 Named_object* declared =
738 this->declare_package_type(type_no->name(),
739 type_no->location());
741 == type_no->unknown_value()->real_named_object());
743 ret = rtype->forward_declaration_type()->add_method(name,
749 this->package_->bindings()->add_method(ret);
752 this->functions_.resize(this->functions_.size() + 1);
753 Open_function& of(this->functions_.back());
755 of.blocks.push_back(block);
759 this->init_functions_.push_back(ret);
760 this->need_init_fn_ = true;
766 // Finish compiling a function.
769 Gogo::finish_function(source_location location)
771 this->finish_block(location);
772 go_assert(this->functions_.back().blocks.empty());
773 this->functions_.pop_back();
776 // Return the current function.
779 Gogo::current_function() const
781 go_assert(!this->functions_.empty());
782 return this->functions_.back().function;
785 // Start a new block.
788 Gogo::start_block(source_location location)
790 go_assert(!this->functions_.empty());
791 Block* block = new Block(this->current_block(), location);
792 this->functions_.back().blocks.push_back(block);
798 Gogo::finish_block(source_location location)
800 go_assert(!this->functions_.empty());
801 go_assert(!this->functions_.back().blocks.empty());
802 Block* block = this->functions_.back().blocks.back();
803 this->functions_.back().blocks.pop_back();
804 block->set_end_location(location);
808 // Add an unknown name.
811 Gogo::add_unknown_name(const std::string& name, source_location location)
813 return this->package_->bindings()->add_unknown_name(name, location);
816 // Declare a function.
819 Gogo::declare_function(const std::string& name, Function_type* type,
820 source_location location)
822 if (!type->is_method())
823 return this->current_bindings()->add_function_declaration(name, NULL, type,
827 // We don't bother to add this to the list of global
829 Type* rtype = type->receiver()->type();
831 // We want to look through the pointer created by the
832 // parser, without getting an error if the type is not yet
834 if (rtype->classification() == Type::TYPE_POINTER)
835 rtype = rtype->points_to();
837 if (rtype->is_error_type())
839 else if (rtype->named_type() != NULL)
840 return rtype->named_type()->add_method_declaration(name, NULL, type,
842 else if (rtype->forward_declaration_type() != NULL)
844 Forward_declaration_type* ftype = rtype->forward_declaration_type();
845 return ftype->add_method_declaration(name, type, location);
852 // Add a label definition.
855 Gogo::add_label_definition(const std::string& label_name,
856 source_location location)
858 go_assert(!this->functions_.empty());
859 Function* func = this->functions_.back().function->func_value();
860 Label* label = func->add_label_definition(this, label_name, location);
861 this->add_statement(Statement::make_label_statement(label, location));
865 // Add a label reference.
868 Gogo::add_label_reference(const std::string& label_name,
869 source_location location, bool issue_goto_errors)
871 go_assert(!this->functions_.empty());
872 Function* func = this->functions_.back().function->func_value();
873 return func->add_label_reference(this, label_name, location,
877 // Return the current binding state.
880 Gogo::bindings_snapshot(source_location location)
882 return new Bindings_snapshot(this->current_block(), location);
888 Gogo::add_statement(Statement* statement)
890 go_assert(!this->functions_.empty()
891 && !this->functions_.back().blocks.empty());
892 this->functions_.back().blocks.back()->add_statement(statement);
898 Gogo::add_block(Block* block, source_location location)
900 go_assert(!this->functions_.empty()
901 && !this->functions_.back().blocks.empty());
902 Statement* statement = Statement::make_block_statement(block, location);
903 this->functions_.back().blocks.back()->add_statement(statement);
909 Gogo::add_constant(const Typed_identifier& tid, Expression* expr,
912 return this->current_bindings()->add_constant(tid, NULL, expr, iota_value);
918 Gogo::add_type(const std::string& name, Type* type, source_location location)
920 Named_object* no = this->current_bindings()->add_type(name, NULL, type,
922 if (!this->in_global_scope() && no->is_type())
923 no->type_value()->set_in_function(this->functions_.back().function);
929 Gogo::add_named_type(Named_type* type)
931 go_assert(this->in_global_scope());
932 this->current_bindings()->add_named_type(type);
938 Gogo::declare_type(const std::string& name, source_location location)
940 Bindings* bindings = this->current_bindings();
941 Named_object* no = bindings->add_type_declaration(name, NULL, location);
942 if (!this->in_global_scope() && no->is_type_declaration())
944 Named_object* f = this->functions_.back().function;
945 no->type_declaration_value()->set_in_function(f);
950 // Declare a type at the package level.
953 Gogo::declare_package_type(const std::string& name, source_location location)
955 return this->package_->bindings()->add_type_declaration(name, NULL, location);
958 // Define a type which was already declared.
961 Gogo::define_type(Named_object* no, Named_type* type)
963 this->current_bindings()->define_type(no, type);
969 Gogo::add_variable(const std::string& name, Variable* variable)
971 Named_object* no = this->current_bindings()->add_variable(name, NULL,
974 // In a function the middle-end wants to see a DECL_EXPR node.
977 && !no->var_value()->is_parameter()
978 && !this->functions_.empty())
979 this->add_statement(Statement::make_variable_declaration(no));
984 // Add a sink--a reference to the blank identifier _.
989 return Named_object::make_sink();
992 // Add a named object.
995 Gogo::add_named_object(Named_object* no)
997 this->current_bindings()->add_named_object(no);
1000 // Record that we've seen an interface type.
1003 Gogo::record_interface_type(Interface_type* itype)
1005 this->interface_types_.push_back(itype);
1008 // Return a name for a thunk object.
1013 static int thunk_count;
1014 char thunk_name[50];
1015 snprintf(thunk_name, sizeof thunk_name, "$thunk%d", thunk_count);
1020 // Return whether a function is a thunk.
1023 Gogo::is_thunk(const Named_object* no)
1025 return no->name().compare(0, 6, "$thunk") == 0;
1028 // Define the global names. We do this only after parsing all the
1029 // input files, because the program might define the global names
1033 Gogo::define_global_names()
1035 for (Bindings::const_declarations_iterator p =
1036 this->globals_->begin_declarations();
1037 p != this->globals_->end_declarations();
1040 Named_object* global_no = p->second;
1041 std::string name(Gogo::pack_hidden_name(global_no->name(), false));
1042 Named_object* no = this->package_->bindings()->lookup(name);
1046 if (no->is_type_declaration())
1048 if (global_no->is_type())
1050 if (no->type_declaration_value()->has_methods())
1051 error_at(no->location(),
1052 "may not define methods for global type");
1053 no->set_type_value(global_no->type_value());
1057 error_at(no->location(), "expected type");
1058 Type* errtype = Type::make_error_type();
1059 Named_object* err = Named_object::make_type("error", NULL,
1062 no->set_type_value(err->type_value());
1065 else if (no->is_unknown())
1066 no->unknown_value()->set_real_named_object(global_no);
1070 // Clear out names in file scope.
1073 Gogo::clear_file_scope()
1075 this->package_->bindings()->clear_file_scope();
1077 // Warn about packages which were imported but not used.
1078 for (Packages::iterator p = this->packages_.begin();
1079 p != this->packages_.end();
1082 Package* package = p->second;
1083 if (package != this->package_
1084 && package->is_imported()
1086 && !package->uses_sink_alias()
1088 error_at(package->location(), "imported and not used: %s",
1089 Gogo::message_name(package->name()).c_str());
1090 package->clear_is_imported();
1091 package->clear_uses_sink_alias();
1092 package->clear_used();
1096 // Traverse the tree.
1099 Gogo::traverse(Traverse* traverse)
1101 // Traverse the current package first for consistency. The other
1102 // packages will only contain imported types, constants, and
1104 if (this->package_->bindings()->traverse(traverse, true) == TRAVERSE_EXIT)
1106 for (Packages::const_iterator p = this->packages_.begin();
1107 p != this->packages_.end();
1110 if (p->second != this->package_)
1112 if (p->second->bindings()->traverse(traverse, true) == TRAVERSE_EXIT)
1118 // Traversal class used to verify types.
1120 class Verify_types : public Traverse
1124 : Traverse(traverse_types)
1131 // Verify that a type is correct.
1134 Verify_types::type(Type* t)
1137 return TRAVERSE_SKIP_COMPONENTS;
1138 return TRAVERSE_CONTINUE;
1141 // Verify that all types are correct.
1144 Gogo::verify_types()
1146 Verify_types traverse;
1147 this->traverse(&traverse);
1150 // Traversal class used to lower parse tree.
1152 class Lower_parse_tree : public Traverse
1155 Lower_parse_tree(Gogo* gogo, Named_object* function)
1156 : Traverse(traverse_variables
1157 | traverse_constants
1158 | traverse_functions
1159 | traverse_statements
1160 | traverse_expressions),
1161 gogo_(gogo), function_(function), iota_value_(-1), inserter_()
1165 set_inserter(const Statement_inserter* inserter)
1166 { this->inserter_ = *inserter; }
1169 variable(Named_object*);
1172 constant(Named_object*, bool);
1175 function(Named_object*);
1178 statement(Block*, size_t* pindex, Statement*);
1181 expression(Expression**);
1186 // The function we are traversing.
1187 Named_object* function_;
1188 // Value to use for the predeclared constant iota.
1190 // Current statement inserter for use by expressions.
1191 Statement_inserter inserter_;
1197 Lower_parse_tree::variable(Named_object* no)
1199 if (!no->is_variable())
1200 return TRAVERSE_CONTINUE;
1202 if (no->is_variable() && no->var_value()->is_global())
1204 // Global variables can have loops in their initialization
1205 // expressions. This is handled in lower_init_expression.
1206 no->var_value()->lower_init_expression(this->gogo_, this->function_,
1208 return TRAVERSE_CONTINUE;
1211 // This is a local variable. We are going to return
1212 // TRAVERSE_SKIP_COMPONENTS here because we want to traverse the
1213 // initialization expression when we reach the variable declaration
1214 // statement. However, that means that we need to traverse the type
1216 if (no->var_value()->has_type())
1218 Type* type = no->var_value()->type();
1221 if (Type::traverse(type, this) == TRAVERSE_EXIT)
1222 return TRAVERSE_EXIT;
1225 go_assert(!no->var_value()->has_pre_init());
1227 return TRAVERSE_SKIP_COMPONENTS;
1230 // Lower constants. We handle constants specially so that we can set
1231 // the right value for the predeclared constant iota. This works in
1232 // conjunction with the way we lower Const_expression objects.
1235 Lower_parse_tree::constant(Named_object* no, bool)
1237 Named_constant* nc = no->const_value();
1239 // Don't get into trouble if the constant's initializer expression
1240 // refers to the constant itself.
1242 return TRAVERSE_CONTINUE;
1245 go_assert(this->iota_value_ == -1);
1246 this->iota_value_ = nc->iota_value();
1247 nc->traverse_expression(this);
1248 this->iota_value_ = -1;
1250 nc->clear_lowering();
1252 // We will traverse the expression a second time, but that will be
1255 return TRAVERSE_CONTINUE;
1258 // Lower function closure types. Record the function while lowering
1259 // it, so that we can pass it down when lowering an expression.
1262 Lower_parse_tree::function(Named_object* no)
1264 no->func_value()->set_closure_type();
1266 go_assert(this->function_ == NULL);
1267 this->function_ = no;
1268 int t = no->func_value()->traverse(this);
1269 this->function_ = NULL;
1271 if (t == TRAVERSE_EXIT)
1273 return TRAVERSE_SKIP_COMPONENTS;
1276 // Lower statement parse trees.
1279 Lower_parse_tree::statement(Block* block, size_t* pindex, Statement* sorig)
1281 // Because we explicitly traverse the statement's contents
1282 // ourselves, we want to skip block statements here. There is
1283 // nothing to lower in a block statement.
1284 if (sorig->is_block_statement())
1285 return TRAVERSE_CONTINUE;
1287 Statement_inserter hold_inserter(this->inserter_);
1288 this->inserter_ = Statement_inserter(block, pindex);
1290 // Lower the expressions first.
1291 int t = sorig->traverse_contents(this);
1292 if (t == TRAVERSE_EXIT)
1294 this->inserter_ = hold_inserter;
1298 // Keep lowering until nothing changes.
1299 Statement* s = sorig;
1302 Statement* snew = s->lower(this->gogo_, this->function_, block,
1307 t = s->traverse_contents(this);
1308 if (t == TRAVERSE_EXIT)
1310 this->inserter_ = hold_inserter;
1316 block->replace_statement(*pindex, s);
1318 this->inserter_ = hold_inserter;
1319 return TRAVERSE_SKIP_COMPONENTS;
1322 // Lower expression parse trees.
1325 Lower_parse_tree::expression(Expression** pexpr)
1327 // We have to lower all subexpressions first, so that we can get
1328 // their type if necessary. This is awkward, because we don't have
1329 // a postorder traversal pass.
1330 if ((*pexpr)->traverse_subexpressions(this) == TRAVERSE_EXIT)
1331 return TRAVERSE_EXIT;
1332 // Keep lowering until nothing changes.
1335 Expression* e = *pexpr;
1336 Expression* enew = e->lower(this->gogo_, this->function_,
1337 &this->inserter_, this->iota_value_);
1342 return TRAVERSE_SKIP_COMPONENTS;
1345 // Lower the parse tree. This is called after the parse is complete,
1346 // when all names should be resolved.
1349 Gogo::lower_parse_tree()
1351 Lower_parse_tree lower_parse_tree(this, NULL);
1352 this->traverse(&lower_parse_tree);
1358 Gogo::lower_block(Named_object* function, Block* block)
1360 Lower_parse_tree lower_parse_tree(this, function);
1361 block->traverse(&lower_parse_tree);
1364 // Lower an expression. INSERTER may be NULL, in which case the
1365 // expression had better not need to create any temporaries.
1368 Gogo::lower_expression(Named_object* function, Statement_inserter* inserter,
1371 Lower_parse_tree lower_parse_tree(this, function);
1372 if (inserter != NULL)
1373 lower_parse_tree.set_inserter(inserter);
1374 lower_parse_tree.expression(pexpr);
1377 // Lower a constant. This is called when lowering a reference to a
1378 // constant. We have to make sure that the constant has already been
1382 Gogo::lower_constant(Named_object* no)
1384 go_assert(no->is_const());
1385 Lower_parse_tree lower(this, NULL);
1386 lower.constant(no, false);
1389 // Look for interface types to finalize methods of inherited
1392 class Finalize_methods : public Traverse
1395 Finalize_methods(Gogo* gogo)
1396 : Traverse(traverse_types),
1407 // Finalize the methods of an interface type.
1410 Finalize_methods::type(Type* t)
1412 // Check the classification so that we don't finalize the methods
1413 // twice for a named interface type.
1414 switch (t->classification())
1416 case Type::TYPE_INTERFACE:
1417 t->interface_type()->finalize_methods();
1420 case Type::TYPE_NAMED:
1422 // We have to finalize the methods of the real type first.
1423 // But if the real type is a struct type, then we only want to
1424 // finalize the methods of the field types, not of the struct
1425 // type itself. We don't want to add methods to the struct,
1426 // since it has a name.
1427 Type* rt = t->named_type()->real_type();
1428 if (rt->classification() != Type::TYPE_STRUCT)
1430 if (Type::traverse(rt, this) == TRAVERSE_EXIT)
1431 return TRAVERSE_EXIT;
1435 if (rt->struct_type()->traverse_field_types(this) == TRAVERSE_EXIT)
1436 return TRAVERSE_EXIT;
1439 t->named_type()->finalize_methods(this->gogo_);
1441 return TRAVERSE_SKIP_COMPONENTS;
1444 case Type::TYPE_STRUCT:
1445 t->struct_type()->finalize_methods(this->gogo_);
1452 return TRAVERSE_CONTINUE;
1455 // Finalize method lists and build stub methods for types.
1458 Gogo::finalize_methods()
1460 Finalize_methods finalize(this);
1461 this->traverse(&finalize);
1464 // Set types for unspecified variables and constants.
1467 Gogo::determine_types()
1469 Bindings* bindings = this->current_bindings();
1470 for (Bindings::const_definitions_iterator p = bindings->begin_definitions();
1471 p != bindings->end_definitions();
1474 if ((*p)->is_function())
1475 (*p)->func_value()->determine_types();
1476 else if ((*p)->is_variable())
1477 (*p)->var_value()->determine_type();
1478 else if ((*p)->is_const())
1479 (*p)->const_value()->determine_type();
1481 // See if a variable requires us to build an initialization
1482 // function. We know that we will see all global variables
1484 if (!this->need_init_fn_ && (*p)->is_variable())
1486 Variable* variable = (*p)->var_value();
1488 // If this is a global variable which requires runtime
1489 // initialization, we need an initialization function.
1490 if (!variable->is_global())
1492 else if (variable->init() == NULL)
1494 else if (variable->type()->interface_type() != NULL)
1495 this->need_init_fn_ = true;
1496 else if (variable->init()->is_constant())
1498 else if (!variable->init()->is_composite_literal())
1499 this->need_init_fn_ = true;
1500 else if (variable->init()->is_nonconstant_composite_literal())
1501 this->need_init_fn_ = true;
1503 // If this is a global variable which holds a pointer value,
1504 // then we need an initialization function to register it as a
1506 if (variable->is_global() && variable->type()->has_pointer())
1507 this->need_init_fn_ = true;
1511 // Determine the types of constants in packages.
1512 for (Packages::const_iterator p = this->packages_.begin();
1513 p != this->packages_.end();
1515 p->second->determine_types();
1518 // Traversal class used for type checking.
1520 class Check_types_traverse : public Traverse
1523 Check_types_traverse(Gogo* gogo)
1524 : Traverse(traverse_variables
1525 | traverse_constants
1526 | traverse_functions
1527 | traverse_statements
1528 | traverse_expressions),
1533 variable(Named_object*);
1536 constant(Named_object*, bool);
1539 function(Named_object*);
1542 statement(Block*, size_t* pindex, Statement*);
1545 expression(Expression**);
1552 // Check that a variable initializer has the right type.
1555 Check_types_traverse::variable(Named_object* named_object)
1557 if (named_object->is_variable())
1559 Variable* var = named_object->var_value();
1561 // Give error if variable type is not defined.
1562 var->type()->base();
1564 Expression* init = var->init();
1567 && !Type::are_assignable(var->type(), init->type(), &reason))
1570 error_at(var->location(), "incompatible type in initialization");
1572 error_at(var->location(),
1573 "incompatible type in initialization (%s)",
1578 return TRAVERSE_CONTINUE;
1581 // Check that a constant initializer has the right type.
1584 Check_types_traverse::constant(Named_object* named_object, bool)
1586 Named_constant* constant = named_object->const_value();
1587 Type* ctype = constant->type();
1588 if (ctype->integer_type() == NULL
1589 && ctype->float_type() == NULL
1590 && ctype->complex_type() == NULL
1591 && !ctype->is_boolean_type()
1592 && !ctype->is_string_type())
1594 if (ctype->is_nil_type())
1595 error_at(constant->location(), "const initializer cannot be nil");
1596 else if (!ctype->is_error())
1597 error_at(constant->location(), "invalid constant type");
1598 constant->set_error();
1600 else if (!constant->expr()->is_constant())
1602 error_at(constant->expr()->location(), "expression is not constant");
1603 constant->set_error();
1605 else if (!Type::are_assignable(constant->type(), constant->expr()->type(),
1608 error_at(constant->location(),
1609 "initialization expression has wrong type");
1610 constant->set_error();
1612 return TRAVERSE_CONTINUE;
1615 // There are no types to check in a function, but this is where we
1616 // issue warnings about labels which are defined but not referenced.
1619 Check_types_traverse::function(Named_object* no)
1621 no->func_value()->check_labels();
1622 return TRAVERSE_CONTINUE;
1625 // Check that types are valid in a statement.
1628 Check_types_traverse::statement(Block*, size_t*, Statement* s)
1630 s->check_types(this->gogo_);
1631 return TRAVERSE_CONTINUE;
1634 // Check that types are valid in an expression.
1637 Check_types_traverse::expression(Expression** expr)
1639 (*expr)->check_types(this->gogo_);
1640 return TRAVERSE_CONTINUE;
1643 // Check that types are valid.
1648 Check_types_traverse traverse(this);
1649 this->traverse(&traverse);
1652 // Check the types in a single block.
1655 Gogo::check_types_in_block(Block* block)
1657 Check_types_traverse traverse(this);
1658 block->traverse(&traverse);
1661 // A traversal class used to find a single shortcut operator within an
1664 class Find_shortcut : public Traverse
1668 : Traverse(traverse_blocks
1669 | traverse_statements
1670 | traverse_expressions),
1674 // A pointer to the expression which was found, or NULL if none was
1678 { return this->found_; }
1683 { return TRAVERSE_SKIP_COMPONENTS; }
1686 statement(Block*, size_t*, Statement*)
1687 { return TRAVERSE_SKIP_COMPONENTS; }
1690 expression(Expression**);
1693 Expression** found_;
1696 // Find a shortcut expression.
1699 Find_shortcut::expression(Expression** pexpr)
1701 Expression* expr = *pexpr;
1702 Binary_expression* be = expr->binary_expression();
1704 return TRAVERSE_CONTINUE;
1705 Operator op = be->op();
1706 if (op != OPERATOR_OROR && op != OPERATOR_ANDAND)
1707 return TRAVERSE_CONTINUE;
1708 go_assert(this->found_ == NULL);
1709 this->found_ = pexpr;
1710 return TRAVERSE_EXIT;
1713 // A traversal class used to turn shortcut operators into explicit if
1716 class Shortcuts : public Traverse
1719 Shortcuts(Gogo* gogo)
1720 : Traverse(traverse_variables
1721 | traverse_statements),
1727 variable(Named_object*);
1730 statement(Block*, size_t*, Statement*);
1733 // Convert a shortcut operator.
1735 convert_shortcut(Block* enclosing, Expression** pshortcut);
1741 // Remove shortcut operators in a single statement.
1744 Shortcuts::statement(Block* block, size_t* pindex, Statement* s)
1746 // FIXME: This approach doesn't work for switch statements, because
1747 // we add the new statements before the whole switch when we need to
1748 // instead add them just before the switch expression. The right
1749 // fix is probably to lower switch statements with nonconstant cases
1750 // to a series of conditionals.
1751 if (s->switch_statement() != NULL)
1752 return TRAVERSE_CONTINUE;
1756 Find_shortcut find_shortcut;
1758 // If S is a variable declaration, then ordinary traversal won't
1759 // do anything. We want to explicitly traverse the
1760 // initialization expression if there is one.
1761 Variable_declaration_statement* vds = s->variable_declaration_statement();
1762 Expression* init = NULL;
1764 s->traverse_contents(&find_shortcut);
1767 init = vds->var()->var_value()->init();
1769 return TRAVERSE_CONTINUE;
1770 init->traverse(&init, &find_shortcut);
1772 Expression** pshortcut = find_shortcut.found();
1773 if (pshortcut == NULL)
1774 return TRAVERSE_CONTINUE;
1776 Statement* snew = this->convert_shortcut(block, pshortcut);
1777 block->insert_statement_before(*pindex, snew);
1780 if (pshortcut == &init)
1781 vds->var()->var_value()->set_init(init);
1785 // Remove shortcut operators in the initializer of a global variable.
1788 Shortcuts::variable(Named_object* no)
1790 if (no->is_result_variable())
1791 return TRAVERSE_CONTINUE;
1792 Variable* var = no->var_value();
1793 Expression* init = var->init();
1794 if (!var->is_global() || init == NULL)
1795 return TRAVERSE_CONTINUE;
1799 Find_shortcut find_shortcut;
1800 init->traverse(&init, &find_shortcut);
1801 Expression** pshortcut = find_shortcut.found();
1802 if (pshortcut == NULL)
1803 return TRAVERSE_CONTINUE;
1805 Statement* snew = this->convert_shortcut(NULL, pshortcut);
1806 var->add_preinit_statement(this->gogo_, snew);
1807 if (pshortcut == &init)
1808 var->set_init(init);
1812 // Given an expression which uses a shortcut operator, return a
1813 // statement which implements it, and update *PSHORTCUT accordingly.
1816 Shortcuts::convert_shortcut(Block* enclosing, Expression** pshortcut)
1818 Binary_expression* shortcut = (*pshortcut)->binary_expression();
1819 Expression* left = shortcut->left();
1820 Expression* right = shortcut->right();
1821 source_location loc = shortcut->location();
1823 Block* retblock = new Block(enclosing, loc);
1824 retblock->set_end_location(loc);
1826 Temporary_statement* ts = Statement::make_temporary(Type::lookup_bool_type(),
1828 retblock->add_statement(ts);
1830 Block* block = new Block(retblock, loc);
1831 block->set_end_location(loc);
1832 Expression* tmpref = Expression::make_temporary_reference(ts, loc);
1833 Statement* assign = Statement::make_assignment(tmpref, right, loc);
1834 block->add_statement(assign);
1836 Expression* cond = Expression::make_temporary_reference(ts, loc);
1837 if (shortcut->binary_expression()->op() == OPERATOR_OROR)
1838 cond = Expression::make_unary(OPERATOR_NOT, cond, loc);
1840 Statement* if_statement = Statement::make_if_statement(cond, block, NULL,
1842 retblock->add_statement(if_statement);
1844 *pshortcut = Expression::make_temporary_reference(ts, loc);
1848 // Now convert any shortcut operators in LEFT and RIGHT.
1849 Shortcuts shortcuts(this->gogo_);
1850 retblock->traverse(&shortcuts);
1852 return Statement::make_block_statement(retblock, loc);
1855 // Turn shortcut operators into explicit if statements. Doing this
1856 // considerably simplifies the order of evaluation rules.
1859 Gogo::remove_shortcuts()
1861 Shortcuts shortcuts(this);
1862 this->traverse(&shortcuts);
1865 // A traversal class which finds all the expressions which must be
1866 // evaluated in order within a statement or larger expression. This
1867 // is used to implement the rules about order of evaluation.
1869 class Find_eval_ordering : public Traverse
1872 typedef std::vector<Expression**> Expression_pointers;
1875 Find_eval_ordering()
1876 : Traverse(traverse_blocks
1877 | traverse_statements
1878 | traverse_expressions),
1884 { return this->exprs_.size(); }
1886 typedef Expression_pointers::const_iterator const_iterator;
1890 { return this->exprs_.begin(); }
1894 { return this->exprs_.end(); }
1899 { return TRAVERSE_SKIP_COMPONENTS; }
1902 statement(Block*, size_t*, Statement*)
1903 { return TRAVERSE_SKIP_COMPONENTS; }
1906 expression(Expression**);
1909 // A list of pointers to expressions with side-effects.
1910 Expression_pointers exprs_;
1913 // If an expression must be evaluated in order, put it on the list.
1916 Find_eval_ordering::expression(Expression** expression_pointer)
1918 // We have to look at subexpressions before this one.
1919 if ((*expression_pointer)->traverse_subexpressions(this) == TRAVERSE_EXIT)
1920 return TRAVERSE_EXIT;
1921 if ((*expression_pointer)->must_eval_in_order())
1922 this->exprs_.push_back(expression_pointer);
1923 return TRAVERSE_SKIP_COMPONENTS;
1926 // A traversal class for ordering evaluations.
1928 class Order_eval : public Traverse
1931 Order_eval(Gogo* gogo)
1932 : Traverse(traverse_variables
1933 | traverse_statements),
1938 variable(Named_object*);
1941 statement(Block*, size_t*, Statement*);
1948 // Implement the order of evaluation rules for a statement.
1951 Order_eval::statement(Block* block, size_t* pindex, Statement* s)
1953 // FIXME: This approach doesn't work for switch statements, because
1954 // we add the new statements before the whole switch when we need to
1955 // instead add them just before the switch expression. The right
1956 // fix is probably to lower switch statements with nonconstant cases
1957 // to a series of conditionals.
1958 if (s->switch_statement() != NULL)
1959 return TRAVERSE_CONTINUE;
1961 Find_eval_ordering find_eval_ordering;
1963 // If S is a variable declaration, then ordinary traversal won't do
1964 // anything. We want to explicitly traverse the initialization
1965 // expression if there is one.
1966 Variable_declaration_statement* vds = s->variable_declaration_statement();
1967 Expression* init = NULL;
1968 Expression* orig_init = NULL;
1970 s->traverse_contents(&find_eval_ordering);
1973 init = vds->var()->var_value()->init();
1975 return TRAVERSE_CONTINUE;
1978 // It might seem that this could be
1979 // init->traverse_subexpressions. Unfortunately that can fail
1982 // newvar, err := call(arg())
1983 // Here newvar will have an init of call result 0 of
1984 // call(arg()). If we only traverse subexpressions, we will
1985 // only find arg(), and we won't bother to move anything out.
1986 // Then we get to the assignment to err, we will traverse the
1987 // whole statement, and this time we will find both call() and
1988 // arg(), and so we will move them out. This will cause them to
1989 // be put into temporary variables before the assignment to err
1990 // but after the declaration of newvar. To avoid that problem,
1991 // we traverse the entire expression here.
1992 Expression::traverse(&init, &find_eval_ordering);
1995 if (find_eval_ordering.size() <= 1)
1997 // If there is only one expression with a side-effect, we can
1998 // leave it in place.
1999 return TRAVERSE_CONTINUE;
2002 bool is_thunk = s->thunk_statement() != NULL;
2003 for (Find_eval_ordering::const_iterator p = find_eval_ordering.begin();
2004 p != find_eval_ordering.end();
2007 Expression** pexpr = *p;
2009 // The last expression in a thunk will be the call passed to go
2010 // or defer, which we must not evaluate early.
2011 if (is_thunk && p + 1 == find_eval_ordering.end())
2014 source_location loc = (*pexpr)->location();
2016 if ((*pexpr)->call_expression() == NULL
2017 || (*pexpr)->call_expression()->result_count() < 2)
2019 Temporary_statement* ts = Statement::make_temporary(NULL, *pexpr,
2022 *pexpr = Expression::make_temporary_reference(ts, loc);
2026 // A call expression which returns multiple results needs to
2027 // be handled specially. We can't create a temporary
2028 // because there is no type to give it. Any actual uses of
2029 // the values will be done via Call_result_expressions.
2030 s = Statement::make_statement(*pexpr, true);
2033 block->insert_statement_before(*pindex, s);
2037 if (init != orig_init)
2038 vds->var()->var_value()->set_init(init);
2040 return TRAVERSE_CONTINUE;
2043 // Implement the order of evaluation rules for the initializer of a
2047 Order_eval::variable(Named_object* no)
2049 if (no->is_result_variable())
2050 return TRAVERSE_CONTINUE;
2051 Variable* var = no->var_value();
2052 Expression* init = var->init();
2053 if (!var->is_global() || init == NULL)
2054 return TRAVERSE_CONTINUE;
2056 Find_eval_ordering find_eval_ordering;
2057 Expression::traverse(&init, &find_eval_ordering);
2059 if (find_eval_ordering.size() <= 1)
2061 // If there is only one expression with a side-effect, we can
2062 // leave it in place.
2063 return TRAVERSE_SKIP_COMPONENTS;
2066 Expression* orig_init = init;
2068 for (Find_eval_ordering::const_iterator p = find_eval_ordering.begin();
2069 p != find_eval_ordering.end();
2072 Expression** pexpr = *p;
2073 source_location loc = (*pexpr)->location();
2075 if ((*pexpr)->call_expression() == NULL
2076 || (*pexpr)->call_expression()->result_count() < 2)
2078 Temporary_statement* ts = Statement::make_temporary(NULL, *pexpr,
2081 *pexpr = Expression::make_temporary_reference(ts, loc);
2085 // A call expression which returns multiple results needs to
2086 // be handled specially.
2087 s = Statement::make_statement(*pexpr, true);
2089 var->add_preinit_statement(this->gogo_, s);
2092 if (init != orig_init)
2093 var->set_init(init);
2095 return TRAVERSE_SKIP_COMPONENTS;
2098 // Use temporary variables to implement the order of evaluation rules.
2101 Gogo::order_evaluations()
2103 Order_eval order_eval(this);
2104 this->traverse(&order_eval);
2107 // Traversal to convert calls to the predeclared recover function to
2108 // pass in an argument indicating whether it can recover from a panic
2111 class Convert_recover : public Traverse
2114 Convert_recover(Named_object* arg)
2115 : Traverse(traverse_expressions),
2121 expression(Expression**);
2124 // The argument to pass to the function.
2128 // Convert calls to recover.
2131 Convert_recover::expression(Expression** pp)
2133 Call_expression* ce = (*pp)->call_expression();
2134 if (ce != NULL && ce->is_recover_call())
2135 ce->set_recover_arg(Expression::make_var_reference(this->arg_,
2137 return TRAVERSE_CONTINUE;
2140 // Traversal for build_recover_thunks.
2142 class Build_recover_thunks : public Traverse
2145 Build_recover_thunks(Gogo* gogo)
2146 : Traverse(traverse_functions),
2151 function(Named_object*);
2155 can_recover_arg(source_location);
2161 // If this function calls recover, turn it into a thunk.
2164 Build_recover_thunks::function(Named_object* orig_no)
2166 Function* orig_func = orig_no->func_value();
2167 if (!orig_func->calls_recover()
2168 || orig_func->is_recover_thunk()
2169 || orig_func->has_recover_thunk())
2170 return TRAVERSE_CONTINUE;
2172 Gogo* gogo = this->gogo_;
2173 source_location location = orig_func->location();
2178 Function_type* orig_fntype = orig_func->type();
2179 Typed_identifier_list* new_params = new Typed_identifier_list();
2180 std::string receiver_name;
2181 if (orig_fntype->is_method())
2183 const Typed_identifier* receiver = orig_fntype->receiver();
2184 snprintf(buf, sizeof buf, "rt.%u", count);
2186 receiver_name = buf;
2187 new_params->push_back(Typed_identifier(receiver_name, receiver->type(),
2188 receiver->location()));
2190 const Typed_identifier_list* orig_params = orig_fntype->parameters();
2191 if (orig_params != NULL && !orig_params->empty())
2193 for (Typed_identifier_list::const_iterator p = orig_params->begin();
2194 p != orig_params->end();
2197 snprintf(buf, sizeof buf, "pt.%u", count);
2199 new_params->push_back(Typed_identifier(buf, p->type(),
2203 snprintf(buf, sizeof buf, "pr.%u", count);
2205 std::string can_recover_name = buf;
2206 new_params->push_back(Typed_identifier(can_recover_name,
2207 Type::lookup_bool_type(),
2208 orig_fntype->location()));
2210 const Typed_identifier_list* orig_results = orig_fntype->results();
2211 Typed_identifier_list* new_results;
2212 if (orig_results == NULL || orig_results->empty())
2216 new_results = new Typed_identifier_list();
2217 for (Typed_identifier_list::const_iterator p = orig_results->begin();
2218 p != orig_results->end();
2220 new_results->push_back(Typed_identifier("", p->type(), p->location()));
2223 Function_type *new_fntype = Type::make_function_type(NULL, new_params,
2225 orig_fntype->location());
2226 if (orig_fntype->is_varargs())
2227 new_fntype->set_is_varargs();
2229 std::string name = orig_no->name() + "$recover";
2230 Named_object *new_no = gogo->start_function(name, new_fntype, false,
2232 Function *new_func = new_no->func_value();
2233 if (orig_func->enclosing() != NULL)
2234 new_func->set_enclosing(orig_func->enclosing());
2236 // We build the code for the original function attached to the new
2237 // function, and then swap the original and new function bodies.
2238 // This means that existing references to the original function will
2239 // then refer to the new function. That makes this code a little
2240 // confusing, in that the reference to NEW_NO really refers to the
2241 // other function, not the one we are building.
2243 Expression* closure = NULL;
2244 if (orig_func->needs_closure())
2246 Named_object* orig_closure_no = orig_func->closure_var();
2247 Variable* orig_closure_var = orig_closure_no->var_value();
2248 Variable* new_var = new Variable(orig_closure_var->type(), NULL, false,
2249 true, false, location);
2250 snprintf(buf, sizeof buf, "closure.%u", count);
2252 Named_object* new_closure_no = Named_object::make_variable(buf, NULL,
2254 new_func->set_closure_var(new_closure_no);
2255 closure = Expression::make_var_reference(new_closure_no, location);
2258 Expression* fn = Expression::make_func_reference(new_no, closure, location);
2260 Expression_list* args = new Expression_list();
2261 if (new_params != NULL)
2263 // Note that we skip the last parameter, which is the boolean
2264 // indicating whether recover can succed.
2265 for (Typed_identifier_list::const_iterator p = new_params->begin();
2266 p + 1 != new_params->end();
2269 Named_object* p_no = gogo->lookup(p->name(), NULL);
2270 go_assert(p_no != NULL
2271 && p_no->is_variable()
2272 && p_no->var_value()->is_parameter());
2273 args->push_back(Expression::make_var_reference(p_no, location));
2276 args->push_back(this->can_recover_arg(location));
2278 gogo->start_block(location);
2280 Call_expression* call = Expression::make_call(fn, args, false, location);
2283 if (orig_fntype->results() == NULL || orig_fntype->results()->empty())
2284 s = Statement::make_statement(call, true);
2287 Expression_list* vals = new Expression_list();
2288 size_t rc = orig_fntype->results()->size();
2290 vals->push_back(call);
2293 for (size_t i = 0; i < rc; ++i)
2294 vals->push_back(Expression::make_call_result(call, i));
2296 s = Statement::make_return_statement(vals, location);
2298 s->determine_types();
2299 gogo->add_statement(s);
2301 Block* b = gogo->finish_block(location);
2303 gogo->add_block(b, location);
2305 // Lower the call in case it returns multiple results.
2306 gogo->lower_block(new_no, b);
2308 gogo->finish_function(location);
2310 // Swap the function bodies and types.
2311 new_func->swap_for_recover(orig_func);
2312 orig_func->set_is_recover_thunk();
2313 new_func->set_calls_recover();
2314 new_func->set_has_recover_thunk();
2316 Bindings* orig_bindings = orig_func->block()->bindings();
2317 Bindings* new_bindings = new_func->block()->bindings();
2318 if (orig_fntype->is_method())
2320 // We changed the receiver to be a regular parameter. We have
2321 // to update the binding accordingly in both functions.
2322 Named_object* orig_rec_no = orig_bindings->lookup_local(receiver_name);
2323 go_assert(orig_rec_no != NULL
2324 && orig_rec_no->is_variable()
2325 && !orig_rec_no->var_value()->is_receiver());
2326 orig_rec_no->var_value()->set_is_receiver();
2328 const std::string& new_receiver_name(orig_fntype->receiver()->name());
2329 Named_object* new_rec_no = new_bindings->lookup_local(new_receiver_name);
2330 if (new_rec_no == NULL)
2331 go_assert(saw_errors());
2334 go_assert(new_rec_no->is_variable()
2335 && new_rec_no->var_value()->is_receiver());
2336 new_rec_no->var_value()->set_is_not_receiver();
2340 // Because we flipped blocks but not types, the can_recover
2341 // parameter appears in the (now) old bindings as a parameter.
2342 // Change it to a local variable, whereupon it will be discarded.
2343 Named_object* can_recover_no = orig_bindings->lookup_local(can_recover_name);
2344 go_assert(can_recover_no != NULL
2345 && can_recover_no->is_variable()
2346 && can_recover_no->var_value()->is_parameter());
2347 orig_bindings->remove_binding(can_recover_no);
2349 // Add the can_recover argument to the (now) new bindings, and
2350 // attach it to any recover statements.
2351 Variable* can_recover_var = new Variable(Type::lookup_bool_type(), NULL,
2352 false, true, false, location);
2353 can_recover_no = new_bindings->add_variable(can_recover_name, NULL,
2355 Convert_recover convert_recover(can_recover_no);
2356 new_func->traverse(&convert_recover);
2358 // Update the function pointers in any named results.
2359 new_func->update_result_variables();
2360 orig_func->update_result_variables();
2362 return TRAVERSE_CONTINUE;
2365 // Return the expression to pass for the .can_recover parameter to the
2366 // new function. This indicates whether a call to recover may return
2367 // non-nil. The expression is
2368 // __go_can_recover(__builtin_return_address()).
2371 Build_recover_thunks::can_recover_arg(source_location location)
2373 static Named_object* builtin_return_address;
2374 if (builtin_return_address == NULL)
2376 const source_location bloc = BUILTINS_LOCATION;
2378 Typed_identifier_list* param_types = new Typed_identifier_list();
2379 Type* uint_type = Type::lookup_integer_type("uint");
2380 param_types->push_back(Typed_identifier("l", uint_type, bloc));
2382 Typed_identifier_list* return_types = new Typed_identifier_list();
2383 Type* voidptr_type = Type::make_pointer_type(Type::make_void_type());
2384 return_types->push_back(Typed_identifier("", voidptr_type, bloc));
2386 Function_type* fntype = Type::make_function_type(NULL, param_types,
2387 return_types, bloc);
2388 builtin_return_address =
2389 Named_object::make_function_declaration("__builtin_return_address",
2390 NULL, fntype, bloc);
2391 const char* n = "__builtin_return_address";
2392 builtin_return_address->func_declaration_value()->set_asm_name(n);
2395 static Named_object* can_recover;
2396 if (can_recover == NULL)
2398 const source_location bloc = BUILTINS_LOCATION;
2399 Typed_identifier_list* param_types = new Typed_identifier_list();
2400 Type* voidptr_type = Type::make_pointer_type(Type::make_void_type());
2401 param_types->push_back(Typed_identifier("a", voidptr_type, bloc));
2402 Type* boolean_type = Type::lookup_bool_type();
2403 Typed_identifier_list* results = new Typed_identifier_list();
2404 results->push_back(Typed_identifier("", boolean_type, bloc));
2405 Function_type* fntype = Type::make_function_type(NULL, param_types,
2407 can_recover = Named_object::make_function_declaration("__go_can_recover",
2410 can_recover->func_declaration_value()->set_asm_name("__go_can_recover");
2413 Expression* fn = Expression::make_func_reference(builtin_return_address,
2417 mpz_init_set_ui(zval, 0UL);
2418 Expression* zexpr = Expression::make_integer(&zval, NULL, location);
2420 Expression_list *args = new Expression_list();
2421 args->push_back(zexpr);
2423 Expression* call = Expression::make_call(fn, args, false, location);
2425 args = new Expression_list();
2426 args->push_back(call);
2428 fn = Expression::make_func_reference(can_recover, NULL, location);
2429 return Expression::make_call(fn, args, false, location);
2432 // Build thunks for functions which call recover. We build a new
2433 // function with an extra parameter, which is whether a call to
2434 // recover can succeed. We then move the body of this function to
2435 // that one. We then turn this function into a thunk which calls the
2436 // new one, passing the value of
2437 // __go_can_recover(__builtin_return_address()). The function will be
2438 // marked as not splitting the stack. This will cooperate with the
2439 // implementation of defer to make recover do the right thing.
2442 Gogo::build_recover_thunks()
2444 Build_recover_thunks build_recover_thunks(this);
2445 this->traverse(&build_recover_thunks);
2448 // Look for named types to see whether we need to create an interface
2451 class Build_method_tables : public Traverse
2454 Build_method_tables(Gogo* gogo,
2455 const std::vector<Interface_type*>& interfaces)
2456 : Traverse(traverse_types),
2457 gogo_(gogo), interfaces_(interfaces)
2466 // A list of locally defined interfaces which have hidden methods.
2467 const std::vector<Interface_type*>& interfaces_;
2470 // Build all required interface method tables for types. We need to
2471 // ensure that we have an interface method table for every interface
2472 // which has a hidden method, for every named type which implements
2473 // that interface. Normally we can just build interface method tables
2474 // as we need them. However, in some cases we can require an
2475 // interface method table for an interface defined in a different
2476 // package for a type defined in that package. If that interface and
2477 // type both use a hidden method, that is OK. However, we will not be
2478 // able to build that interface method table when we need it, because
2479 // the type's hidden method will be static. So we have to build it
2480 // here, and just refer it from other packages as needed.
2483 Gogo::build_interface_method_tables()
2485 std::vector<Interface_type*> hidden_interfaces;
2486 hidden_interfaces.reserve(this->interface_types_.size());
2487 for (std::vector<Interface_type*>::const_iterator pi =
2488 this->interface_types_.begin();
2489 pi != this->interface_types_.end();
2492 const Typed_identifier_list* methods = (*pi)->methods();
2493 if (methods == NULL)
2495 for (Typed_identifier_list::const_iterator pm = methods->begin();
2496 pm != methods->end();
2499 if (Gogo::is_hidden_name(pm->name()))
2501 hidden_interfaces.push_back(*pi);
2507 if (!hidden_interfaces.empty())
2509 // Now traverse the tree looking for all named types.
2510 Build_method_tables bmt(this, hidden_interfaces);
2511 this->traverse(&bmt);
2514 // We no longer need the list of interfaces.
2516 this->interface_types_.clear();
2519 // This is called for each type. For a named type, for each of the
2520 // interfaces with hidden methods that it implements, create the
2524 Build_method_tables::type(Type* type)
2526 Named_type* nt = type->named_type();
2529 for (std::vector<Interface_type*>::const_iterator p =
2530 this->interfaces_.begin();
2531 p != this->interfaces_.end();
2534 // We ask whether a pointer to the named type implements the
2535 // interface, because a pointer can implement more methods
2537 if ((*p)->implements_interface(Type::make_pointer_type(nt), NULL))
2539 nt->interface_method_table(this->gogo_, *p, false);
2540 nt->interface_method_table(this->gogo_, *p, true);
2544 return TRAVERSE_CONTINUE;
2547 // Traversal class used to check for return statements.
2549 class Check_return_statements_traverse : public Traverse
2552 Check_return_statements_traverse()
2553 : Traverse(traverse_functions)
2557 function(Named_object*);
2560 // Check that a function has a return statement if it needs one.
2563 Check_return_statements_traverse::function(Named_object* no)
2565 Function* func = no->func_value();
2566 const Function_type* fntype = func->type();
2567 const Typed_identifier_list* results = fntype->results();
2569 // We only need a return statement if there is a return value.
2570 if (results == NULL || results->empty())
2571 return TRAVERSE_CONTINUE;
2573 if (func->block()->may_fall_through())
2574 error_at(func->location(), "control reaches end of non-void function");
2576 return TRAVERSE_CONTINUE;
2579 // Check return statements.
2582 Gogo::check_return_statements()
2584 Check_return_statements_traverse traverse;
2585 this->traverse(&traverse);
2588 // Get the unique prefix to use before all exported symbols. This
2589 // must be unique across the entire link.
2592 Gogo::unique_prefix() const
2594 go_assert(!this->unique_prefix_.empty());
2595 return this->unique_prefix_;
2598 // Set the unique prefix to use before all exported symbols. This
2599 // comes from the command line option -fgo-prefix=XXX.
2602 Gogo::set_unique_prefix(const std::string& arg)
2604 go_assert(this->unique_prefix_.empty());
2605 this->unique_prefix_ = arg;
2606 this->unique_prefix_specified_ = true;
2609 // Work out the package priority. It is one more than the maximum
2610 // priority of an imported package.
2613 Gogo::package_priority() const
2616 for (Packages::const_iterator p = this->packages_.begin();
2617 p != this->packages_.end();
2619 if (p->second->priority() > priority)
2620 priority = p->second->priority();
2621 return priority + 1;
2624 // Export identifiers as requested.
2629 // For now we always stream to a section. Later we may want to
2630 // support streaming to a separate file.
2631 Stream_to_section stream;
2633 Export exp(&stream);
2634 exp.register_builtin_types(this);
2635 exp.export_globals(this->package_name(),
2636 this->unique_prefix(),
2637 this->package_priority(),
2638 (this->need_init_fn_ && !this->is_main_package()
2639 ? this->get_init_fn_name()
2641 this->imported_init_fns_,
2642 this->package_->bindings());
2645 // Find the blocks in order to convert named types defined in blocks.
2647 class Convert_named_types : public Traverse
2650 Convert_named_types(Gogo* gogo)
2651 : Traverse(traverse_blocks),
2657 block(Block* block);
2664 Convert_named_types::block(Block* block)
2666 this->gogo_->convert_named_types_in_bindings(block->bindings());
2667 return TRAVERSE_CONTINUE;
2670 // Convert all named types to the backend representation. Since named
2671 // types can refer to other types, this needs to be done in the right
2672 // sequence, which is handled by Named_type::convert. Here we arrange
2673 // to call that for each named type.
2676 Gogo::convert_named_types()
2678 this->convert_named_types_in_bindings(this->globals_);
2679 for (Packages::iterator p = this->packages_.begin();
2680 p != this->packages_.end();
2683 Package* package = p->second;
2684 this->convert_named_types_in_bindings(package->bindings());
2687 Convert_named_types cnt(this);
2688 this->traverse(&cnt);
2690 // Make all the builtin named types used for type descriptors, and
2691 // then convert them. They will only be written out if they are
2693 Type::make_type_descriptor_type();
2694 Type::make_type_descriptor_ptr_type();
2695 Function_type::make_function_type_descriptor_type();
2696 Pointer_type::make_pointer_type_descriptor_type();
2697 Struct_type::make_struct_type_descriptor_type();
2698 Array_type::make_array_type_descriptor_type();
2699 Array_type::make_slice_type_descriptor_type();
2700 Map_type::make_map_type_descriptor_type();
2701 Map_type::make_map_descriptor_type();
2702 Channel_type::make_chan_type_descriptor_type();
2703 Interface_type::make_interface_type_descriptor_type();
2704 Type::convert_builtin_named_types(this);
2706 Runtime::convert_types(this);
2708 Function_type::convert_types(this);
2710 this->named_types_are_converted_ = true;
2713 // Convert all names types in a set of bindings.
2716 Gogo::convert_named_types_in_bindings(Bindings* bindings)
2718 for (Bindings::const_definitions_iterator p = bindings->begin_definitions();
2719 p != bindings->end_definitions();
2722 if ((*p)->is_type())
2723 (*p)->type_value()->convert(this);
2729 Function::Function(Function_type* type, Function* enclosing, Block* block,
2730 source_location location)
2731 : type_(type), enclosing_(enclosing), results_(NULL),
2732 closure_var_(NULL), block_(block), location_(location), fndecl_(NULL),
2733 defer_stack_(NULL), results_are_named_(false), calls_recover_(false),
2734 is_recover_thunk_(false), has_recover_thunk_(false)
2738 // Create the named result variables.
2741 Function::create_result_variables(Gogo* gogo)
2743 const Typed_identifier_list* results = this->type_->results();
2744 if (results == NULL || results->empty())
2747 if (!results->front().name().empty())
2748 this->results_are_named_ = true;
2750 this->results_ = new Results();
2751 this->results_->reserve(results->size());
2753 Block* block = this->block_;
2755 for (Typed_identifier_list::const_iterator p = results->begin();
2756 p != results->end();
2759 std::string name = p->name();
2760 if (name.empty() || Gogo::is_sink_name(name))
2762 static int result_counter;
2764 snprintf(buf, sizeof buf, "$ret%d", result_counter);
2766 name = gogo->pack_hidden_name(buf, false);
2768 Result_variable* result = new Result_variable(p->type(), this, index,
2770 Named_object* no = block->bindings()->add_result_variable(name, result);
2771 if (no->is_result_variable())
2772 this->results_->push_back(no);
2775 static int dummy_result_count;
2777 snprintf(buf, sizeof buf, "$dret%d", dummy_result_count);
2778 ++dummy_result_count;
2779 name = gogo->pack_hidden_name(buf, false);
2780 no = block->bindings()->add_result_variable(name, result);
2781 go_assert(no->is_result_variable());
2782 this->results_->push_back(no);
2787 // Update the named result variables when cloning a function which
2791 Function::update_result_variables()
2793 if (this->results_ == NULL)
2796 for (Results::iterator p = this->results_->begin();
2797 p != this->results_->end();
2799 (*p)->result_var_value()->set_function(this);
2802 // Return the closure variable, creating it if necessary.
2805 Function::closure_var()
2807 if (this->closure_var_ == NULL)
2809 // We don't know the type of the variable yet. We add fields as
2811 source_location loc = this->type_->location();
2812 Struct_field_list* sfl = new Struct_field_list;
2813 Type* struct_type = Type::make_struct_type(sfl, loc);
2814 Variable* var = new Variable(Type::make_pointer_type(struct_type),
2815 NULL, false, true, false, loc);
2816 this->closure_var_ = Named_object::make_variable("closure", NULL, var);
2817 // Note that the new variable is not in any binding contour.
2819 return this->closure_var_;
2822 // Set the type of the closure variable.
2825 Function::set_closure_type()
2827 if (this->closure_var_ == NULL)
2829 Named_object* closure = this->closure_var_;
2830 Struct_type* st = closure->var_value()->type()->deref()->struct_type();
2831 unsigned int index = 0;
2832 for (Closure_fields::const_iterator p = this->closure_fields_.begin();
2833 p != this->closure_fields_.end();
2836 Named_object* no = p->first;
2838 snprintf(buf, sizeof buf, "%u", index);
2839 std::string n = no->name() + buf;
2841 if (no->is_variable())
2842 var_type = no->var_value()->type();
2844 var_type = no->result_var_value()->type();
2845 Type* field_type = Type::make_pointer_type(var_type);
2846 st->push_field(Struct_field(Typed_identifier(n, field_type, p->second)));
2850 // Return whether this function is a method.
2853 Function::is_method() const
2855 return this->type_->is_method();
2858 // Add a label definition.
2861 Function::add_label_definition(Gogo* gogo, const std::string& label_name,
2862 source_location location)
2864 Label* lnull = NULL;
2865 std::pair<Labels::iterator, bool> ins =
2866 this->labels_.insert(std::make_pair(label_name, lnull));
2870 // This is a new label.
2871 label = new Label(label_name);
2872 ins.first->second = label;
2876 // The label was already in the hash table.
2877 label = ins.first->second;
2878 if (label->is_defined())
2880 error_at(location, "label %qs already defined",
2881 Gogo::message_name(label_name).c_str());
2882 inform(label->location(), "previous definition of %qs was here",
2883 Gogo::message_name(label_name).c_str());
2884 return new Label(label_name);
2888 label->define(location, gogo->bindings_snapshot(location));
2890 // Issue any errors appropriate for any previous goto's to this
2892 const std::vector<Bindings_snapshot*>& refs(label->refs());
2893 for (std::vector<Bindings_snapshot*>::const_iterator p = refs.begin();
2896 (*p)->check_goto_to(gogo->current_block());
2897 label->clear_refs();
2902 // Add a reference to a label.
2905 Function::add_label_reference(Gogo* gogo, const std::string& label_name,
2906 source_location location, bool issue_goto_errors)
2908 Label* lnull = NULL;
2909 std::pair<Labels::iterator, bool> ins =
2910 this->labels_.insert(std::make_pair(label_name, lnull));
2914 // The label was already in the hash table.
2915 label = ins.first->second;
2919 go_assert(ins.first->second == NULL);
2920 label = new Label(label_name);
2921 ins.first->second = label;
2924 label->set_is_used();
2926 if (issue_goto_errors)
2928 Bindings_snapshot* snapshot = label->snapshot();
2929 if (snapshot != NULL)
2930 snapshot->check_goto_from(gogo->current_block(), location);
2932 label->add_snapshot_ref(gogo->bindings_snapshot(location));
2938 // Warn about labels that are defined but not used.
2941 Function::check_labels() const
2943 for (Labels::const_iterator p = this->labels_.begin();
2944 p != this->labels_.end();
2947 Label* label = p->second;
2948 if (!label->is_used())
2949 error_at(label->location(), "label %qs defined and not used",
2950 Gogo::message_name(label->name()).c_str());
2954 // Swap one function with another. This is used when building the
2955 // thunk we use to call a function which calls recover. It may not
2956 // work for any other case.
2959 Function::swap_for_recover(Function *x)
2961 go_assert(this->enclosing_ == x->enclosing_);
2962 std::swap(this->results_, x->results_);
2963 std::swap(this->closure_var_, x->closure_var_);
2964 std::swap(this->block_, x->block_);
2965 go_assert(this->location_ == x->location_);
2966 go_assert(this->fndecl_ == NULL && x->fndecl_ == NULL);
2967 go_assert(this->defer_stack_ == NULL && x->defer_stack_ == NULL);
2970 // Traverse the tree.
2973 Function::traverse(Traverse* traverse)
2975 unsigned int traverse_mask = traverse->traverse_mask();
2978 & (Traverse::traverse_types | Traverse::traverse_expressions))
2981 if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
2982 return TRAVERSE_EXIT;
2985 // FIXME: We should check traverse_functions here if nested
2986 // functions are stored in block bindings.
2987 if (this->block_ != NULL
2989 & (Traverse::traverse_variables
2990 | Traverse::traverse_constants
2991 | Traverse::traverse_blocks
2992 | Traverse::traverse_statements
2993 | Traverse::traverse_expressions
2994 | Traverse::traverse_types)) != 0)
2996 if (this->block_->traverse(traverse) == TRAVERSE_EXIT)
2997 return TRAVERSE_EXIT;
3000 return TRAVERSE_CONTINUE;
3003 // Work out types for unspecified variables and constants.
3006 Function::determine_types()
3008 if (this->block_ != NULL)
3009 this->block_->determine_types();
3012 // Get a pointer to the variable representing the defer stack for this
3013 // function, making it if necessary. The value of the variable is set
3014 // by the runtime routines to true if the function is returning,
3015 // rather than panicing through. A pointer to this variable is used
3016 // as a marker for the functions on the defer stack associated with
3017 // this function. A function-specific variable permits inlining a
3018 // function which uses defer.
3021 Function::defer_stack(source_location location)
3023 if (this->defer_stack_ == NULL)
3025 Type* t = Type::lookup_bool_type();
3026 Expression* n = Expression::make_boolean(false, location);
3027 this->defer_stack_ = Statement::make_temporary(t, n, location);
3028 this->defer_stack_->set_is_address_taken();
3030 Expression* ref = Expression::make_temporary_reference(this->defer_stack_,
3032 return Expression::make_unary(OPERATOR_AND, ref, location);
3035 // Export the function.
3038 Function::export_func(Export* exp, const std::string& name) const
3040 Function::export_func_with_type(exp, name, this->type_);
3043 // Export a function with a type.
3046 Function::export_func_with_type(Export* exp, const std::string& name,
3047 const Function_type* fntype)
3049 exp->write_c_string("func ");
3051 if (fntype->is_method())
3053 exp->write_c_string("(");
3054 exp->write_type(fntype->receiver()->type());
3055 exp->write_c_string(") ");
3058 exp->write_string(name);
3060 exp->write_c_string(" (");
3061 const Typed_identifier_list* parameters = fntype->parameters();
3062 if (parameters != NULL)
3064 bool is_varargs = fntype->is_varargs();
3066 for (Typed_identifier_list::const_iterator p = parameters->begin();
3067 p != parameters->end();
3073 exp->write_c_string(", ");
3074 if (!is_varargs || p + 1 != parameters->end())
3075 exp->write_type(p->type());
3078 exp->write_c_string("...");
3079 exp->write_type(p->type()->array_type()->element_type());
3083 exp->write_c_string(")");
3085 const Typed_identifier_list* results = fntype->results();
3086 if (results != NULL)
3088 if (results->size() == 1)
3090 exp->write_c_string(" ");
3091 exp->write_type(results->begin()->type());
3095 exp->write_c_string(" (");
3097 for (Typed_identifier_list::const_iterator p = results->begin();
3098 p != results->end();
3104 exp->write_c_string(", ");
3105 exp->write_type(p->type());
3107 exp->write_c_string(")");
3110 exp->write_c_string(";\n");
3113 // Import a function.
3116 Function::import_func(Import* imp, std::string* pname,
3117 Typed_identifier** preceiver,
3118 Typed_identifier_list** pparameters,
3119 Typed_identifier_list** presults,
3122 imp->require_c_string("func ");
3125 if (imp->peek_char() == '(')
3127 imp->require_c_string("(");
3128 Type* rtype = imp->read_type();
3129 *preceiver = new Typed_identifier(Import::import_marker, rtype,
3131 imp->require_c_string(") ");
3134 *pname = imp->read_identifier();
3136 Typed_identifier_list* parameters;
3137 *is_varargs = false;
3138 imp->require_c_string(" (");
3139 if (imp->peek_char() == ')')
3143 parameters = new Typed_identifier_list();
3146 if (imp->match_c_string("..."))
3152 Type* ptype = imp->read_type();
3154 ptype = Type::make_array_type(ptype, NULL);
3155 parameters->push_back(Typed_identifier(Import::import_marker,
3156 ptype, imp->location()));
3157 if (imp->peek_char() != ',')
3159 go_assert(!*is_varargs);
3160 imp->require_c_string(", ");
3163 imp->require_c_string(")");
3164 *pparameters = parameters;
3166 Typed_identifier_list* results;
3167 if (imp->peek_char() != ' ')
3171 results = new Typed_identifier_list();
3172 imp->require_c_string(" ");
3173 if (imp->peek_char() != '(')
3175 Type* rtype = imp->read_type();
3176 results->push_back(Typed_identifier(Import::import_marker, rtype,
3181 imp->require_c_string("(");
3184 Type* rtype = imp->read_type();
3185 results->push_back(Typed_identifier(Import::import_marker,
3186 rtype, imp->location()));
3187 if (imp->peek_char() != ',')
3189 imp->require_c_string(", ");
3191 imp->require_c_string(")");
3194 imp->require_c_string(";\n");
3195 *presults = results;
3200 Block::Block(Block* enclosing, source_location location)
3201 : enclosing_(enclosing), statements_(),
3202 bindings_(new Bindings(enclosing == NULL
3204 : enclosing->bindings())),
3205 start_location_(location),
3206 end_location_(UNKNOWN_LOCATION)
3210 // Add a statement to a block.
3213 Block::add_statement(Statement* statement)
3215 this->statements_.push_back(statement);
3218 // Add a statement to the front of a block. This is slow but is only
3219 // used for reference counts of parameters.
3222 Block::add_statement_at_front(Statement* statement)
3224 this->statements_.insert(this->statements_.begin(), statement);
3227 // Replace a statement in a block.
3230 Block::replace_statement(size_t index, Statement* s)
3232 go_assert(index < this->statements_.size());
3233 this->statements_[index] = s;
3236 // Add a statement before another statement.
3239 Block::insert_statement_before(size_t index, Statement* s)
3241 go_assert(index < this->statements_.size());
3242 this->statements_.insert(this->statements_.begin() + index, s);
3245 // Add a statement after another statement.
3248 Block::insert_statement_after(size_t index, Statement* s)
3250 go_assert(index < this->statements_.size());
3251 this->statements_.insert(this->statements_.begin() + index + 1, s);
3254 // Traverse the tree.
3257 Block::traverse(Traverse* traverse)
3259 unsigned int traverse_mask = traverse->traverse_mask();
3261 if ((traverse_mask & Traverse::traverse_blocks) != 0)
3263 int t = traverse->block(this);
3264 if (t == TRAVERSE_EXIT)
3265 return TRAVERSE_EXIT;
3266 else if (t == TRAVERSE_SKIP_COMPONENTS)
3267 return TRAVERSE_CONTINUE;
3271 & (Traverse::traverse_variables
3272 | Traverse::traverse_constants
3273 | Traverse::traverse_expressions
3274 | Traverse::traverse_types)) != 0)
3276 const unsigned int e_or_t = (Traverse::traverse_expressions
3277 | Traverse::traverse_types);
3278 const unsigned int e_or_t_or_s = (e_or_t
3279 | Traverse::traverse_statements);
3280 for (Bindings::const_definitions_iterator pb =
3281 this->bindings_->begin_definitions();
3282 pb != this->bindings_->end_definitions();
3285 int t = TRAVERSE_CONTINUE;
3286 switch ((*pb)->classification())
3288 case Named_object::NAMED_OBJECT_CONST:
3289 if ((traverse_mask & Traverse::traverse_constants) != 0)
3290 t = traverse->constant(*pb, false);
3291 if (t == TRAVERSE_CONTINUE
3292 && (traverse_mask & e_or_t) != 0)
3294 Type* tc = (*pb)->const_value()->type();
3296 && Type::traverse(tc, traverse) == TRAVERSE_EXIT)
3297 return TRAVERSE_EXIT;
3298 t = (*pb)->const_value()->traverse_expression(traverse);
3302 case Named_object::NAMED_OBJECT_VAR:
3303 case Named_object::NAMED_OBJECT_RESULT_VAR:
3304 if ((traverse_mask & Traverse::traverse_variables) != 0)
3305 t = traverse->variable(*pb);
3306 if (t == TRAVERSE_CONTINUE
3307 && (traverse_mask & e_or_t) != 0)
3309 if ((*pb)->is_result_variable()
3310 || (*pb)->var_value()->has_type())
3312 Type* tv = ((*pb)->is_variable()
3313 ? (*pb)->var_value()->type()
3314 : (*pb)->result_var_value()->type());
3316 && Type::traverse(tv, traverse) == TRAVERSE_EXIT)
3317 return TRAVERSE_EXIT;
3320 if (t == TRAVERSE_CONTINUE
3321 && (traverse_mask & e_or_t_or_s) != 0
3322 && (*pb)->is_variable())
3323 t = (*pb)->var_value()->traverse_expression(traverse,
3327 case Named_object::NAMED_OBJECT_FUNC:
3328 case Named_object::NAMED_OBJECT_FUNC_DECLARATION:
3331 case Named_object::NAMED_OBJECT_TYPE:
3332 if ((traverse_mask & e_or_t) != 0)
3333 t = Type::traverse((*pb)->type_value(), traverse);
3336 case Named_object::NAMED_OBJECT_TYPE_DECLARATION:
3337 case Named_object::NAMED_OBJECT_UNKNOWN:
3340 case Named_object::NAMED_OBJECT_PACKAGE:
3341 case Named_object::NAMED_OBJECT_SINK:
3348 if (t == TRAVERSE_EXIT)
3349 return TRAVERSE_EXIT;
3353 // No point in checking traverse_mask here--if we got here we always
3354 // want to walk the statements. The traversal can insert new
3355 // statements before or after the current statement. Inserting
3356 // statements before the current statement requires updating I via
3357 // the pointer; those statements will not be traversed. Any new
3358 // statements inserted after the current statement will be traversed
3360 for (size_t i = 0; i < this->statements_.size(); ++i)
3362 if (this->statements_[i]->traverse(this, &i, traverse) == TRAVERSE_EXIT)
3363 return TRAVERSE_EXIT;
3366 return TRAVERSE_CONTINUE;
3369 // Work out types for unspecified variables and constants.
3372 Block::determine_types()
3374 for (Bindings::const_definitions_iterator pb =
3375 this->bindings_->begin_definitions();
3376 pb != this->bindings_->end_definitions();
3379 if ((*pb)->is_variable())
3380 (*pb)->var_value()->determine_type();
3381 else if ((*pb)->is_const())
3382 (*pb)->const_value()->determine_type();
3385 for (std::vector<Statement*>::const_iterator ps = this->statements_.begin();
3386 ps != this->statements_.end();
3388 (*ps)->determine_types();
3391 // Return true if the statements in this block may fall through.
3394 Block::may_fall_through() const
3396 if (this->statements_.empty())
3398 return this->statements_.back()->may_fall_through();
3401 // Convert a block to the backend representation.
3404 Block::get_backend(Translate_context* context)
3406 Gogo* gogo = context->gogo();
3407 Named_object* function = context->function();
3408 std::vector<Bvariable*> vars;
3409 vars.reserve(this->bindings_->size_definitions());
3410 for (Bindings::const_definitions_iterator pv =
3411 this->bindings_->begin_definitions();
3412 pv != this->bindings_->end_definitions();
3415 if ((*pv)->is_variable() && !(*pv)->var_value()->is_parameter())
3416 vars.push_back((*pv)->get_backend_variable(gogo, function));
3419 // FIXME: Permitting FUNCTION to be NULL here is a temporary measure
3420 // until we have a proper representation of the init function.
3421 Bfunction* bfunction;
3422 if (function == NULL)
3425 bfunction = tree_to_function(function->func_value()->get_decl());
3426 Bblock* ret = context->backend()->block(bfunction, context->bblock(),
3427 vars, this->start_location_,
3428 this->end_location_);
3430 Translate_context subcontext(gogo, function, this, ret);
3431 std::vector<Bstatement*> bstatements;
3432 bstatements.reserve(this->statements_.size());
3433 for (std::vector<Statement*>::const_iterator p = this->statements_.begin();
3434 p != this->statements_.end();
3436 bstatements.push_back((*p)->get_backend(&subcontext));
3438 context->backend()->block_add_statements(ret, bstatements);
3443 // Class Bindings_snapshot.
3445 Bindings_snapshot::Bindings_snapshot(const Block* b, source_location location)
3446 : block_(b), counts_(), location_(location)
3450 this->counts_.push_back(b->bindings()->size_definitions());
3455 // Report errors appropriate for a goto from B to this.
3458 Bindings_snapshot::check_goto_from(const Block* b, source_location loc)
3461 if (!this->check_goto_block(loc, b, this->block_, &dummy))
3463 this->check_goto_defs(loc, this->block_,
3464 this->block_->bindings()->size_definitions(),
3468 // Report errors appropriate for a goto from this to B.
3471 Bindings_snapshot::check_goto_to(const Block* b)
3474 if (!this->check_goto_block(this->location_, this->block_, b, &index))
3476 this->check_goto_defs(this->location_, b, this->counts_[index],
3477 b->bindings()->size_definitions());
3480 // Report errors appropriate for a goto at LOC from BFROM to BTO.
3481 // Return true if all is well, false if we reported an error. If this
3482 // returns true, it sets *PINDEX to the number of blocks BTO is above
3486 Bindings_snapshot::check_goto_block(source_location loc, const Block* bfrom,
3487 const Block* bto, size_t* pindex)
3489 // It is an error if BTO is not either BFROM or above BFROM.
3491 for (const Block* pb = bfrom; pb != bto; pb = pb->enclosing(), ++index)
3495 error_at(loc, "goto jumps into block");
3496 inform(bto->start_location(), "goto target block starts here");
3504 // Report errors appropriate for a goto at LOC ending at BLOCK, where
3505 // CFROM is the number of names defined at the point of the goto and
3506 // CTO is the number of names defined at the point of the label.
3509 Bindings_snapshot::check_goto_defs(source_location loc, const Block* block,
3510 size_t cfrom, size_t cto)
3514 Bindings::const_definitions_iterator p =
3515 block->bindings()->begin_definitions();
3516 for (size_t i = 0; i < cfrom; ++i)
3518 go_assert(p != block->bindings()->end_definitions());
3521 go_assert(p != block->bindings()->end_definitions());
3523 std::string n = (*p)->message_name();
3524 error_at(loc, "goto jumps over declaration of %qs", n.c_str());
3525 inform((*p)->location(), "%qs defined here", n.c_str());
3531 Variable::Variable(Type* type, Expression* init, bool is_global,
3532 bool is_parameter, bool is_receiver,
3533 source_location location)
3534 : type_(type), init_(init), preinit_(NULL), location_(location),
3535 backend_(NULL), is_global_(is_global), is_parameter_(is_parameter),
3536 is_receiver_(is_receiver), is_varargs_parameter_(false),
3537 is_address_taken_(false), is_non_escaping_address_taken_(false),
3538 seen_(false), init_is_lowered_(false), type_from_init_tuple_(false),
3539 type_from_range_index_(false), type_from_range_value_(false),
3540 type_from_chan_element_(false), is_type_switch_var_(false),
3541 determined_type_(false)
3543 go_assert(type != NULL || init != NULL);
3544 go_assert(!is_parameter || init == NULL);
3547 // Traverse the initializer expression.
3550 Variable::traverse_expression(Traverse* traverse, unsigned int traverse_mask)
3552 if (this->preinit_ != NULL)
3554 if (this->preinit_->traverse(traverse) == TRAVERSE_EXIT)
3555 return TRAVERSE_EXIT;
3557 if (this->init_ != NULL
3559 & (Traverse::traverse_expressions | Traverse::traverse_types))
3562 if (Expression::traverse(&this->init_, traverse) == TRAVERSE_EXIT)
3563 return TRAVERSE_EXIT;
3565 return TRAVERSE_CONTINUE;
3568 // Lower the initialization expression after parsing is complete.
3571 Variable::lower_init_expression(Gogo* gogo, Named_object* function,
3572 Statement_inserter* inserter)
3574 if (this->init_ != NULL && !this->init_is_lowered_)
3578 // We will give an error elsewhere, this is just to prevent
3579 // an infinite loop.
3584 Statement_inserter global_inserter;
3585 if (this->is_global_)
3587 global_inserter = Statement_inserter(gogo, this);
3588 inserter = &global_inserter;
3591 gogo->lower_expression(function, inserter, &this->init_);
3593 this->seen_ = false;
3595 this->init_is_lowered_ = true;
3599 // Get the preinit block.
3602 Variable::preinit_block(Gogo* gogo)
3604 go_assert(this->is_global_);
3605 if (this->preinit_ == NULL)
3606 this->preinit_ = new Block(NULL, this->location());
3608 // If a global variable has a preinitialization statement, then we
3609 // need to have an initialization function.
3610 gogo->set_need_init_fn();
3612 return this->preinit_;
3615 // Add a statement to be run before the initialization expression.
3618 Variable::add_preinit_statement(Gogo* gogo, Statement* s)
3620 Block* b = this->preinit_block(gogo);
3621 b->add_statement(s);
3622 b->set_end_location(s->location());
3625 // In an assignment which sets a variable to a tuple of EXPR, return
3626 // the type of the first element of the tuple.
3629 Variable::type_from_tuple(Expression* expr, bool report_error) const
3631 if (expr->map_index_expression() != NULL)
3633 Map_type* mt = expr->map_index_expression()->get_map_type();
3635 return Type::make_error_type();
3636 return mt->val_type();
3638 else if (expr->receive_expression() != NULL)
3640 Expression* channel = expr->receive_expression()->channel();
3641 Type* channel_type = channel->type();
3642 if (channel_type->channel_type() == NULL)
3643 return Type::make_error_type();
3644 return channel_type->channel_type()->element_type();
3649 error_at(this->location(), "invalid tuple definition");
3650 return Type::make_error_type();
3654 // Given EXPR used in a range clause, return either the index type or
3655 // the value type of the range, depending upon GET_INDEX_TYPE.
3658 Variable::type_from_range(Expression* expr, bool get_index_type,
3659 bool report_error) const
3661 Type* t = expr->type();
3662 if (t->array_type() != NULL
3663 || (t->points_to() != NULL
3664 && t->points_to()->array_type() != NULL
3665 && !t->points_to()->is_slice_type()))
3668 return Type::lookup_integer_type("int");
3670 return t->deref()->array_type()->element_type();
3672 else if (t->is_string_type())
3673 return Type::lookup_integer_type("int");
3674 else if (t->map_type() != NULL)
3677 return t->map_type()->key_type();
3679 return t->map_type()->val_type();
3681 else if (t->channel_type() != NULL)
3684 return t->channel_type()->element_type();
3688 error_at(this->location(),
3689 "invalid definition of value variable for channel range");
3690 return Type::make_error_type();
3696 error_at(this->location(), "invalid type for range clause");
3697 return Type::make_error_type();
3701 // EXPR should be a channel. Return the channel's element type.
3704 Variable::type_from_chan_element(Expression* expr, bool report_error) const
3706 Type* t = expr->type();
3707 if (t->channel_type() != NULL)
3708 return t->channel_type()->element_type();
3712 error_at(this->location(), "expected channel");
3713 return Type::make_error_type();
3717 // Return the type of the Variable. This may be called before
3718 // Variable::determine_type is called, which means that we may need to
3719 // get the type from the initializer. FIXME: If we combine lowering
3720 // with type determination, then this should be unnecessary.
3725 // A variable in a type switch with a nil case will have the wrong
3726 // type here. This gets fixed up in determine_type, below.
3727 Type* type = this->type_;
3728 Expression* init = this->init_;
3729 if (this->is_type_switch_var_
3730 && this->type_->is_nil_constant_as_type())
3732 Type_guard_expression* tge = this->init_->type_guard_expression();
3733 go_assert(tge != NULL);
3740 if (this->type_ == NULL || !this->type_->is_error_type())
3742 error_at(this->location_, "variable initializer refers to itself");
3743 this->type_ = Type::make_error_type();
3752 else if (this->type_from_init_tuple_)
3753 type = this->type_from_tuple(init, false);
3754 else if (this->type_from_range_index_ || this->type_from_range_value_)
3755 type = this->type_from_range(init, this->type_from_range_index_, false);
3756 else if (this->type_from_chan_element_)
3757 type = this->type_from_chan_element(init, false);
3760 go_assert(init != NULL);
3761 type = init->type();
3762 go_assert(type != NULL);
3764 // Variables should not have abstract types.
3765 if (type->is_abstract())
3766 type = type->make_non_abstract_type();
3768 if (type->is_void_type())
3769 type = Type::make_error_type();
3772 this->seen_ = false;
3777 // Fetch the type from a const pointer, in which case it should have
3778 // been set already.
3781 Variable::type() const
3783 go_assert(this->type_ != NULL);
3787 // Set the type if necessary.
3790 Variable::determine_type()
3792 if (this->determined_type_)
3794 this->determined_type_ = true;
3796 if (this->preinit_ != NULL)
3797 this->preinit_->determine_types();
3799 // A variable in a type switch with a nil case will have the wrong
3800 // type here. It will have an initializer which is a type guard.
3801 // We want to initialize it to the value without the type guard, and
3802 // use the type of that value as well.
3803 if (this->is_type_switch_var_ && this->type_->is_nil_constant_as_type())
3805 Type_guard_expression* tge = this->init_->type_guard_expression();
3806 go_assert(tge != NULL);
3808 this->init_ = tge->expr();
3811 if (this->init_ == NULL)
3812 go_assert(this->type_ != NULL && !this->type_->is_abstract());
3813 else if (this->type_from_init_tuple_)
3815 Expression *init = this->init_;
3816 init->determine_type_no_context();
3817 this->type_ = this->type_from_tuple(init, true);
3820 else if (this->type_from_range_index_ || this->type_from_range_value_)
3822 Expression* init = this->init_;
3823 init->determine_type_no_context();
3824 this->type_ = this->type_from_range(init, this->type_from_range_index_,
3828 else if (this->type_from_chan_element_)
3830 Expression* init = this->init_;
3831 init->determine_type_no_context();
3832 this->type_ = this->type_from_chan_element(init, true);
3837 Type_context context(this->type_, false);
3838 this->init_->determine_type(&context);
3839 if (this->type_ == NULL)
3841 Type* type = this->init_->type();
3842 go_assert(type != NULL);
3843 if (type->is_abstract())
3844 type = type->make_non_abstract_type();
3846 if (type->is_void_type())
3848 error_at(this->location_, "variable has no type");
3849 type = Type::make_error_type();
3851 else if (type->is_nil_type())
3853 error_at(this->location_, "variable defined to nil type");
3854 type = Type::make_error_type();
3856 else if (type->is_call_multiple_result_type())
3858 error_at(this->location_,
3859 "single variable set to multiple value function call");
3860 type = Type::make_error_type();
3868 // Export the variable
3871 Variable::export_var(Export* exp, const std::string& name) const
3873 go_assert(this->is_global_);
3874 exp->write_c_string("var ");
3875 exp->write_string(name);
3876 exp->write_c_string(" ");
3877 exp->write_type(this->type());
3878 exp->write_c_string(";\n");
3881 // Import a variable.
3884 Variable::import_var(Import* imp, std::string* pname, Type** ptype)
3886 imp->require_c_string("var ");
3887 *pname = imp->read_identifier();
3888 imp->require_c_string(" ");
3889 *ptype = imp->read_type();
3890 imp->require_c_string(";\n");
3893 // Convert a variable to the backend representation.
3896 Variable::get_backend_variable(Gogo* gogo, Named_object* function,
3897 const Package* package, const std::string& name)
3899 if (this->backend_ == NULL)
3901 Backend* backend = gogo->backend();
3902 Type* type = this->type_;
3903 if (type->is_error_type()
3904 || (type->is_undefined()
3905 && (!this->is_global_ || package == NULL)))
3906 this->backend_ = backend->error_variable();
3909 bool is_parameter = this->is_parameter_;
3910 if (this->is_receiver_ && type->points_to() == NULL)
3911 is_parameter = false;
3912 if (this->is_in_heap())
3914 is_parameter = false;
3915 type = Type::make_pointer_type(type);
3918 std::string n = Gogo::unpack_hidden_name(name);
3919 Btype* btype = type->get_backend(gogo);
3922 if (this->is_global_)
3923 bvar = backend->global_variable((package == NULL
3924 ? gogo->package_name()
3927 ? gogo->unique_prefix()
3928 : package->unique_prefix()),
3932 Gogo::is_hidden_name(name),
3936 tree fndecl = function->func_value()->get_decl();
3937 Bfunction* bfunction = tree_to_function(fndecl);
3938 bool is_address_taken = (this->is_non_escaping_address_taken_
3939 && !this->is_in_heap());
3941 bvar = backend->parameter_variable(bfunction, n, btype,
3945 bvar = backend->local_variable(bfunction, n, btype,
3949 this->backend_ = bvar;
3952 return this->backend_;
3955 // Class Result_variable.
3957 // Convert a result variable to the backend representation.
3960 Result_variable::get_backend_variable(Gogo* gogo, Named_object* function,
3961 const std::string& name)
3963 if (this->backend_ == NULL)
3965 Backend* backend = gogo->backend();
3966 Type* type = this->type_;
3967 if (type->is_error())
3968 this->backend_ = backend->error_variable();
3971 if (this->is_in_heap())
3972 type = Type::make_pointer_type(type);
3973 Btype* btype = type->get_backend(gogo);
3974 tree fndecl = function->func_value()->get_decl();
3975 Bfunction* bfunction = tree_to_function(fndecl);
3976 std::string n = Gogo::unpack_hidden_name(name);
3977 bool is_address_taken = (this->is_non_escaping_address_taken_
3978 && !this->is_in_heap());
3979 this->backend_ = backend->local_variable(bfunction, n, btype,
3984 return this->backend_;
3987 // Class Named_constant.
3989 // Traverse the initializer expression.
3992 Named_constant::traverse_expression(Traverse* traverse)
3994 return Expression::traverse(&this->expr_, traverse);
3997 // Determine the type of the constant.
4000 Named_constant::determine_type()
4002 if (this->type_ != NULL)
4004 Type_context context(this->type_, false);
4005 this->expr_->determine_type(&context);
4009 // A constant may have an abstract type.
4010 Type_context context(NULL, true);
4011 this->expr_->determine_type(&context);
4012 this->type_ = this->expr_->type();
4013 go_assert(this->type_ != NULL);
4017 // Indicate that we found and reported an error for this constant.
4020 Named_constant::set_error()
4022 this->type_ = Type::make_error_type();
4023 this->expr_ = Expression::make_error(this->location_);
4026 // Export a constant.
4029 Named_constant::export_const(Export* exp, const std::string& name) const
4031 exp->write_c_string("const ");
4032 exp->write_string(name);
4033 exp->write_c_string(" ");
4034 if (!this->type_->is_abstract())
4036 exp->write_type(this->type_);
4037 exp->write_c_string(" ");
4039 exp->write_c_string("= ");
4040 this->expr()->export_expression(exp);
4041 exp->write_c_string(";\n");
4044 // Import a constant.
4047 Named_constant::import_const(Import* imp, std::string* pname, Type** ptype,
4050 imp->require_c_string("const ");
4051 *pname = imp->read_identifier();
4052 imp->require_c_string(" ");
4053 if (imp->peek_char() == '=')
4057 *ptype = imp->read_type();
4058 imp->require_c_string(" ");
4060 imp->require_c_string("= ");
4061 *pexpr = Expression::import_expression(imp);
4062 imp->require_c_string(";\n");
4068 Type_declaration::add_method(const std::string& name, Function* function)
4070 Named_object* ret = Named_object::make_function(name, NULL, function);
4071 this->methods_.push_back(ret);
4075 // Add a method declaration.
4078 Type_declaration::add_method_declaration(const std::string& name,
4079 Function_type* type,
4080 source_location location)
4082 Named_object* ret = Named_object::make_function_declaration(name, NULL, type,
4084 this->methods_.push_back(ret);
4088 // Return whether any methods ere defined.
4091 Type_declaration::has_methods() const
4093 return !this->methods_.empty();
4096 // Define methods for the real type.
4099 Type_declaration::define_methods(Named_type* nt)
4101 for (Methods::const_iterator p = this->methods_.begin();
4102 p != this->methods_.end();
4104 nt->add_existing_method(*p);
4107 // We are using the type. Return true if we should issue a warning.
4110 Type_declaration::using_type()
4112 bool ret = !this->issued_warning_;
4113 this->issued_warning_ = true;
4117 // Class Unknown_name.
4119 // Set the real named object.
4122 Unknown_name::set_real_named_object(Named_object* no)
4124 go_assert(this->real_named_object_ == NULL);
4125 go_assert(!no->is_unknown());
4126 this->real_named_object_ = no;
4129 // Class Named_object.
4131 Named_object::Named_object(const std::string& name,
4132 const Package* package,
4133 Classification classification)
4134 : name_(name), package_(package), classification_(classification),
4137 if (Gogo::is_sink_name(name))
4138 go_assert(classification == NAMED_OBJECT_SINK);
4141 // Make an unknown name. This is used by the parser. The name must
4142 // be resolved later. Unknown names are only added in the current
4146 Named_object::make_unknown_name(const std::string& name,
4147 source_location location)
4149 Named_object* named_object = new Named_object(name, NULL,
4150 NAMED_OBJECT_UNKNOWN);
4151 Unknown_name* value = new Unknown_name(location);
4152 named_object->u_.unknown_value = value;
4153 return named_object;
4159 Named_object::make_constant(const Typed_identifier& tid,
4160 const Package* package, Expression* expr,
4163 Named_object* named_object = new Named_object(tid.name(), package,
4164 NAMED_OBJECT_CONST);
4165 Named_constant* named_constant = new Named_constant(tid.type(), expr,
4168 named_object->u_.const_value = named_constant;
4169 return named_object;
4172 // Make a named type.
4175 Named_object::make_type(const std::string& name, const Package* package,
4176 Type* type, source_location location)
4178 Named_object* named_object = new Named_object(name, package,
4180 Named_type* named_type = Type::make_named_type(named_object, type, location);
4181 named_object->u_.type_value = named_type;
4182 return named_object;
4185 // Make a type declaration.
4188 Named_object::make_type_declaration(const std::string& name,
4189 const Package* package,
4190 source_location location)
4192 Named_object* named_object = new Named_object(name, package,
4193 NAMED_OBJECT_TYPE_DECLARATION);
4194 Type_declaration* type_declaration = new Type_declaration(location);
4195 named_object->u_.type_declaration = type_declaration;
4196 return named_object;
4202 Named_object::make_variable(const std::string& name, const Package* package,
4205 Named_object* named_object = new Named_object(name, package,
4207 named_object->u_.var_value = variable;
4208 return named_object;
4211 // Make a result variable.
4214 Named_object::make_result_variable(const std::string& name,
4215 Result_variable* result)
4217 Named_object* named_object = new Named_object(name, NULL,
4218 NAMED_OBJECT_RESULT_VAR);
4219 named_object->u_.result_var_value = result;
4220 return named_object;
4223 // Make a sink. This is used for the special blank identifier _.
4226 Named_object::make_sink()
4228 return new Named_object("_", NULL, NAMED_OBJECT_SINK);
4231 // Make a named function.
4234 Named_object::make_function(const std::string& name, const Package* package,
4237 Named_object* named_object = new Named_object(name, package,
4239 named_object->u_.func_value = function;
4240 return named_object;
4243 // Make a function declaration.
4246 Named_object::make_function_declaration(const std::string& name,
4247 const Package* package,
4248 Function_type* fntype,
4249 source_location location)
4251 Named_object* named_object = new Named_object(name, package,
4252 NAMED_OBJECT_FUNC_DECLARATION);
4253 Function_declaration *func_decl = new Function_declaration(fntype, location);
4254 named_object->u_.func_declaration_value = func_decl;
4255 return named_object;
4261 Named_object::make_package(const std::string& alias, Package* package)
4263 Named_object* named_object = new Named_object(alias, NULL,
4264 NAMED_OBJECT_PACKAGE);
4265 named_object->u_.package_value = package;
4266 return named_object;
4269 // Return the name to use in an error message.
4272 Named_object::message_name() const
4274 if (this->package_ == NULL)
4275 return Gogo::message_name(this->name_);
4276 std::string ret = Gogo::message_name(this->package_->name());
4278 ret += Gogo::message_name(this->name_);
4282 // Set the type when a declaration is defined.
4285 Named_object::set_type_value(Named_type* named_type)
4287 go_assert(this->classification_ == NAMED_OBJECT_TYPE_DECLARATION);
4288 Type_declaration* td = this->u_.type_declaration;
4289 td->define_methods(named_type);
4290 Named_object* in_function = td->in_function();
4291 if (in_function != NULL)
4292 named_type->set_in_function(in_function);
4294 this->classification_ = NAMED_OBJECT_TYPE;
4295 this->u_.type_value = named_type;
4298 // Define a function which was previously declared.
4301 Named_object::set_function_value(Function* function)
4303 go_assert(this->classification_ == NAMED_OBJECT_FUNC_DECLARATION);
4304 this->classification_ = NAMED_OBJECT_FUNC;
4305 // FIXME: We should free the old value.
4306 this->u_.func_value = function;
4309 // Declare an unknown object as a type declaration.
4312 Named_object::declare_as_type()
4314 go_assert(this->classification_ == NAMED_OBJECT_UNKNOWN);
4315 Unknown_name* unk = this->u_.unknown_value;
4316 this->classification_ = NAMED_OBJECT_TYPE_DECLARATION;
4317 this->u_.type_declaration = new Type_declaration(unk->location());
4321 // Return the location of a named object.
4324 Named_object::location() const
4326 switch (this->classification_)
4329 case NAMED_OBJECT_UNINITIALIZED:
4332 case NAMED_OBJECT_UNKNOWN:
4333 return this->unknown_value()->location();
4335 case NAMED_OBJECT_CONST:
4336 return this->const_value()->location();
4338 case NAMED_OBJECT_TYPE:
4339 return this->type_value()->location();
4341 case NAMED_OBJECT_TYPE_DECLARATION:
4342 return this->type_declaration_value()->location();
4344 case NAMED_OBJECT_VAR:
4345 return this->var_value()->location();
4347 case NAMED_OBJECT_RESULT_VAR:
4348 return this->result_var_value()->location();
4350 case NAMED_OBJECT_SINK:
4353 case NAMED_OBJECT_FUNC:
4354 return this->func_value()->location();
4356 case NAMED_OBJECT_FUNC_DECLARATION:
4357 return this->func_declaration_value()->location();
4359 case NAMED_OBJECT_PACKAGE:
4360 return this->package_value()->location();
4364 // Export a named object.
4367 Named_object::export_named_object(Export* exp) const
4369 switch (this->classification_)
4372 case NAMED_OBJECT_UNINITIALIZED:
4373 case NAMED_OBJECT_UNKNOWN:
4376 case NAMED_OBJECT_CONST:
4377 this->const_value()->export_const(exp, this->name_);
4380 case NAMED_OBJECT_TYPE:
4381 this->type_value()->export_named_type(exp, this->name_);
4384 case NAMED_OBJECT_TYPE_DECLARATION:
4385 error_at(this->type_declaration_value()->location(),
4386 "attempt to export %<%s%> which was declared but not defined",
4387 this->message_name().c_str());
4390 case NAMED_OBJECT_FUNC_DECLARATION:
4391 this->func_declaration_value()->export_func(exp, this->name_);
4394 case NAMED_OBJECT_VAR:
4395 this->var_value()->export_var(exp, this->name_);
4398 case NAMED_OBJECT_RESULT_VAR:
4399 case NAMED_OBJECT_SINK:
4402 case NAMED_OBJECT_FUNC:
4403 this->func_value()->export_func(exp, this->name_);
4408 // Convert a variable to the backend representation.
4411 Named_object::get_backend_variable(Gogo* gogo, Named_object* function)
4413 if (this->classification_ == NAMED_OBJECT_VAR)
4414 return this->var_value()->get_backend_variable(gogo, function,
4415 this->package_, this->name_);
4416 else if (this->classification_ == NAMED_OBJECT_RESULT_VAR)
4417 return this->result_var_value()->get_backend_variable(gogo, function,
4425 Bindings::Bindings(Bindings* enclosing)
4426 : enclosing_(enclosing), named_objects_(), bindings_()
4433 Bindings::clear_file_scope()
4435 Contour::iterator p = this->bindings_.begin();
4436 while (p != this->bindings_.end())
4439 if (p->second->package() != NULL)
4441 else if (p->second->is_package())
4443 else if (p->second->is_function()
4444 && !p->second->func_value()->type()->is_method()
4445 && Gogo::unpack_hidden_name(p->second->name()) == "init")
4453 p = this->bindings_.erase(p);
4457 // Look up a symbol.
4460 Bindings::lookup(const std::string& name) const
4462 Contour::const_iterator p = this->bindings_.find(name);
4463 if (p != this->bindings_.end())
4464 return p->second->resolve();
4465 else if (this->enclosing_ != NULL)
4466 return this->enclosing_->lookup(name);
4471 // Look up a symbol locally.
4474 Bindings::lookup_local(const std::string& name) const
4476 Contour::const_iterator p = this->bindings_.find(name);
4477 if (p == this->bindings_.end())
4482 // Remove an object from a set of bindings. This is used for a
4483 // special case in thunks for functions which call recover.
4486 Bindings::remove_binding(Named_object* no)
4488 Contour::iterator pb = this->bindings_.find(no->name());
4489 go_assert(pb != this->bindings_.end());
4490 this->bindings_.erase(pb);
4491 for (std::vector<Named_object*>::iterator pn = this->named_objects_.begin();
4492 pn != this->named_objects_.end();
4497 this->named_objects_.erase(pn);
4504 // Add a method to the list of objects. This is not added to the
4505 // lookup table. This is so that we have a single list of objects
4506 // declared at the top level, which we walk through when it's time to
4507 // convert to trees.
4510 Bindings::add_method(Named_object* method)
4512 this->named_objects_.push_back(method);
4515 // Add a generic Named_object to a Contour.
4518 Bindings::add_named_object_to_contour(Contour* contour,
4519 Named_object* named_object)
4521 go_assert(named_object == named_object->resolve());
4522 const std::string& name(named_object->name());
4523 go_assert(!Gogo::is_sink_name(name));
4525 std::pair<Contour::iterator, bool> ins =
4526 contour->insert(std::make_pair(name, named_object));
4529 // The name was already there.
4530 if (named_object->package() != NULL
4531 && ins.first->second->package() == named_object->package()
4532 && (ins.first->second->classification()
4533 == named_object->classification()))
4535 // This is a second import of the same object.
4536 return ins.first->second;
4538 ins.first->second = this->new_definition(ins.first->second,
4540 return ins.first->second;
4544 // Don't push declarations on the list. We push them on when
4545 // and if we find the definitions. That way we genericize the
4546 // functions in order.
4547 if (!named_object->is_type_declaration()
4548 && !named_object->is_function_declaration()
4549 && !named_object->is_unknown())
4550 this->named_objects_.push_back(named_object);
4551 return named_object;
4555 // We had an existing named object OLD_OBJECT, and we've seen a new
4556 // one NEW_OBJECT with the same name. FIXME: This does not free the
4557 // new object when we don't need it.
4560 Bindings::new_definition(Named_object* old_object, Named_object* new_object)
4563 switch (old_object->classification())
4566 case Named_object::NAMED_OBJECT_UNINITIALIZED:
4569 case Named_object::NAMED_OBJECT_UNKNOWN:
4571 Named_object* real = old_object->unknown_value()->real_named_object();
4573 return this->new_definition(real, new_object);
4574 go_assert(!new_object->is_unknown());
4575 old_object->unknown_value()->set_real_named_object(new_object);
4576 if (!new_object->is_type_declaration()
4577 && !new_object->is_function_declaration())
4578 this->named_objects_.push_back(new_object);
4582 case Named_object::NAMED_OBJECT_CONST:
4585 case Named_object::NAMED_OBJECT_TYPE:
4586 if (new_object->is_type_declaration())
4590 case Named_object::NAMED_OBJECT_TYPE_DECLARATION:
4591 if (new_object->is_type_declaration())
4593 if (new_object->is_type())
4595 old_object->set_type_value(new_object->type_value());
4596 new_object->type_value()->set_named_object(old_object);
4597 this->named_objects_.push_back(old_object);
4602 case Named_object::NAMED_OBJECT_VAR:
4603 case Named_object::NAMED_OBJECT_RESULT_VAR:
4604 // We have already given an error in the parser for cases where
4605 // one parameter or result variable redeclares another one.
4606 if ((new_object->is_variable()
4607 && new_object->var_value()->is_parameter())
4608 || new_object->is_result_variable())
4612 case Named_object::NAMED_OBJECT_SINK:
4615 case Named_object::NAMED_OBJECT_FUNC:
4616 if (new_object->is_function_declaration())
4618 if (!new_object->func_declaration_value()->asm_name().empty())
4619 sorry("__asm__ for function definitions");
4620 Function_type* old_type = old_object->func_value()->type();
4621 Function_type* new_type =
4622 new_object->func_declaration_value()->type();
4623 if (old_type->is_valid_redeclaration(new_type, &reason))
4628 case Named_object::NAMED_OBJECT_FUNC_DECLARATION:
4630 Function_type* old_type = old_object->func_declaration_value()->type();
4631 if (new_object->is_function_declaration())
4633 Function_type* new_type =
4634 new_object->func_declaration_value()->type();
4635 if (old_type->is_valid_redeclaration(new_type, &reason))
4638 if (new_object->is_function())
4640 Function_type* new_type = new_object->func_value()->type();
4641 if (old_type->is_valid_redeclaration(new_type, &reason))
4643 if (!old_object->func_declaration_value()->asm_name().empty())
4644 sorry("__asm__ for function definitions");
4645 old_object->set_function_value(new_object->func_value());
4646 this->named_objects_.push_back(old_object);
4653 case Named_object::NAMED_OBJECT_PACKAGE:
4654 if (new_object->is_package()
4655 && (old_object->package_value()->name()
4656 == new_object->package_value()->name()))
4662 std::string n = old_object->message_name();
4664 error_at(new_object->location(), "redefinition of %qs", n.c_str());
4666 error_at(new_object->location(), "redefinition of %qs: %s", n.c_str(),
4669 inform(old_object->location(), "previous definition of %qs was here",
4675 // Add a named type.
4678 Bindings::add_named_type(Named_type* named_type)
4680 return this->add_named_object(named_type->named_object());
4686 Bindings::add_function(const std::string& name, const Package* package,
4689 return this->add_named_object(Named_object::make_function(name, package,
4693 // Add a function declaration.
4696 Bindings::add_function_declaration(const std::string& name,
4697 const Package* package,
4698 Function_type* type,
4699 source_location location)
4701 Named_object* no = Named_object::make_function_declaration(name, package,
4703 return this->add_named_object(no);
4706 // Define a type which was previously declared.
4709 Bindings::define_type(Named_object* no, Named_type* type)
4711 no->set_type_value(type);
4712 this->named_objects_.push_back(no);
4715 // Traverse bindings.
4718 Bindings::traverse(Traverse* traverse, bool is_global)
4720 unsigned int traverse_mask = traverse->traverse_mask();
4722 // We don't use an iterator because we permit the traversal to add
4723 // new global objects.
4724 const unsigned int e_or_t = (Traverse::traverse_expressions
4725 | Traverse::traverse_types);
4726 const unsigned int e_or_t_or_s = (e_or_t
4727 | Traverse::traverse_statements);
4728 for (size_t i = 0; i < this->named_objects_.size(); ++i)
4730 Named_object* p = this->named_objects_[i];
4731 int t = TRAVERSE_CONTINUE;
4732 switch (p->classification())
4734 case Named_object::NAMED_OBJECT_CONST:
4735 if ((traverse_mask & Traverse::traverse_constants) != 0)
4736 t = traverse->constant(p, is_global);
4737 if (t == TRAVERSE_CONTINUE
4738 && (traverse_mask & e_or_t) != 0)
4740 Type* tc = p->const_value()->type();
4742 && Type::traverse(tc, traverse) == TRAVERSE_EXIT)
4743 return TRAVERSE_EXIT;
4744 t = p->const_value()->traverse_expression(traverse);
4748 case Named_object::NAMED_OBJECT_VAR:
4749 case Named_object::NAMED_OBJECT_RESULT_VAR:
4750 if ((traverse_mask & Traverse::traverse_variables) != 0)
4751 t = traverse->variable(p);
4752 if (t == TRAVERSE_CONTINUE
4753 && (traverse_mask & e_or_t) != 0)
4755 if (p->is_result_variable()
4756 || p->var_value()->has_type())
4758 Type* tv = (p->is_variable()
4759 ? p->var_value()->type()
4760 : p->result_var_value()->type());
4762 && Type::traverse(tv, traverse) == TRAVERSE_EXIT)
4763 return TRAVERSE_EXIT;
4766 if (t == TRAVERSE_CONTINUE
4767 && (traverse_mask & e_or_t_or_s) != 0
4768 && p->is_variable())
4769 t = p->var_value()->traverse_expression(traverse, traverse_mask);
4772 case Named_object::NAMED_OBJECT_FUNC:
4773 if ((traverse_mask & Traverse::traverse_functions) != 0)
4774 t = traverse->function(p);
4776 if (t == TRAVERSE_CONTINUE
4778 & (Traverse::traverse_variables
4779 | Traverse::traverse_constants
4780 | Traverse::traverse_functions
4781 | Traverse::traverse_blocks
4782 | Traverse::traverse_statements
4783 | Traverse::traverse_expressions
4784 | Traverse::traverse_types)) != 0)
4786 if (p->func_value()->traverse(traverse) == TRAVERSE_EXIT)
4787 return TRAVERSE_EXIT;
4791 case Named_object::NAMED_OBJECT_PACKAGE:
4792 // These are traversed in Gogo::traverse.
4793 go_assert(is_global);
4796 case Named_object::NAMED_OBJECT_TYPE:
4797 if ((traverse_mask & e_or_t) != 0)
4798 t = Type::traverse(p->type_value(), traverse);
4801 case Named_object::NAMED_OBJECT_TYPE_DECLARATION:
4802 case Named_object::NAMED_OBJECT_FUNC_DECLARATION:
4803 case Named_object::NAMED_OBJECT_UNKNOWN:
4806 case Named_object::NAMED_OBJECT_SINK:
4811 if (t == TRAVERSE_EXIT)
4812 return TRAVERSE_EXIT;
4815 return TRAVERSE_CONTINUE;
4820 // Clear any references to this label.
4825 for (std::vector<Bindings_snapshot*>::iterator p = this->refs_.begin();
4826 p != this->refs_.end();
4829 this->refs_.clear();
4832 // Get the backend representation for a label.
4835 Label::get_backend_label(Translate_context* context)
4837 if (this->blabel_ == NULL)
4839 Function* function = context->function()->func_value();
4840 tree fndecl = function->get_decl();
4841 Bfunction* bfunction = tree_to_function(fndecl);
4842 this->blabel_ = context->backend()->label(bfunction, this->name_,
4845 return this->blabel_;
4848 // Return an expression for the address of this label.
4851 Label::get_addr(Translate_context* context, source_location location)
4853 Blabel* label = this->get_backend_label(context);
4854 return context->backend()->label_address(label, location);
4857 // Class Unnamed_label.
4859 // Get the backend representation for an unnamed label.
4862 Unnamed_label::get_blabel(Translate_context* context)
4864 if (this->blabel_ == NULL)
4866 Function* function = context->function()->func_value();
4867 tree fndecl = function->get_decl();
4868 Bfunction* bfunction = tree_to_function(fndecl);
4869 this->blabel_ = context->backend()->label(bfunction, "",
4872 return this->blabel_;
4875 // Return a statement which defines this unnamed label.
4878 Unnamed_label::get_definition(Translate_context* context)
4880 Blabel* blabel = this->get_blabel(context);
4881 return context->backend()->label_definition_statement(blabel);
4884 // Return a goto statement to this unnamed label.
4887 Unnamed_label::get_goto(Translate_context* context, source_location location)
4889 Blabel* blabel = this->get_blabel(context);
4890 return context->backend()->goto_statement(blabel, location);
4895 Package::Package(const std::string& name, const std::string& unique_prefix,
4896 source_location location)
4897 : name_(name), unique_prefix_(unique_prefix), bindings_(new Bindings(NULL)),
4898 priority_(0), location_(location), used_(false), is_imported_(false),
4899 uses_sink_alias_(false)
4901 go_assert(!name.empty() && !unique_prefix.empty());
4904 // Set the priority. We may see multiple priorities for an imported
4905 // package; we want to use the largest one.
4908 Package::set_priority(int priority)
4910 if (priority > this->priority_)
4911 this->priority_ = priority;
4914 // Determine types of constants. Everything else in a package
4915 // (variables, function declarations) should already have a fixed
4916 // type. Constants may have abstract types.
4919 Package::determine_types()
4921 Bindings* bindings = this->bindings_;
4922 for (Bindings::const_definitions_iterator p = bindings->begin_definitions();
4923 p != bindings->end_definitions();
4926 if ((*p)->is_const())
4927 (*p)->const_value()->determine_type();
4935 Traverse::~Traverse()
4937 if (this->types_seen_ != NULL)
4938 delete this->types_seen_;
4939 if (this->expressions_seen_ != NULL)
4940 delete this->expressions_seen_;
4943 // Record that we are looking at a type, and return true if we have
4947 Traverse::remember_type(const Type* type)
4949 if (type->is_error_type())
4951 go_assert((this->traverse_mask() & traverse_types) != 0
4952 || (this->traverse_mask() & traverse_expressions) != 0);
4953 // We only have to remember named types, as they are the only ones
4954 // we can see multiple times in a traversal.
4955 if (type->classification() != Type::TYPE_NAMED)
4957 if (this->types_seen_ == NULL)
4958 this->types_seen_ = new Types_seen();
4959 std::pair<Types_seen::iterator, bool> ins = this->types_seen_->insert(type);
4963 // Record that we are looking at an expression, and return true if we
4964 // have already seen it.
4967 Traverse::remember_expression(const Expression* expression)
4969 go_assert((this->traverse_mask() & traverse_types) != 0
4970 || (this->traverse_mask() & traverse_expressions) != 0);
4971 if (this->expressions_seen_ == NULL)
4972 this->expressions_seen_ = new Expressions_seen();
4973 std::pair<Expressions_seen::iterator, bool> ins =
4974 this->expressions_seen_->insert(expression);
4978 // The default versions of these functions should never be called: the
4979 // traversal mask indicates which functions may be called.
4982 Traverse::variable(Named_object*)
4988 Traverse::constant(Named_object*, bool)
4994 Traverse::function(Named_object*)
5000 Traverse::block(Block*)
5006 Traverse::statement(Block*, size_t*, Statement*)
5012 Traverse::expression(Expression**)
5018 Traverse::type(Type*)
5023 // Class Statement_inserter.
5026 Statement_inserter::insert(Statement* s)
5028 if (this->block_ != NULL)
5030 go_assert(this->pindex_ != NULL);
5031 this->block_->insert_statement_before(*this->pindex_, s);
5034 else if (this->var_ != NULL)
5035 this->var_->add_preinit_statement(this->gogo_, s);