LCOV - code coverage report
Current view: top level - gcc/go/gofrontend - gogo.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 91.4 % 4678 4274
Test Date: 2026-02-28 14:20:25 Functions: 92.2 % 308 284
Legend: Lines:     hit not hit

            Line data    Source code
       1              : // gogo.cc -- Go frontend parsed representation.
       2              : 
       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.
       6              : 
       7              : #include "go-system.h"
       8              : 
       9              : #include <fstream>
      10              : 
      11              : #include "filenames.h"
      12              : 
      13              : #include "go-c.h"
      14              : #include "go-diagnostics.h"
      15              : #include "go-encode-id.h"
      16              : #include "go-dump.h"
      17              : #include "go-optimize.h"
      18              : #include "lex.h"
      19              : #include "types.h"
      20              : #include "statements.h"
      21              : #include "expressions.h"
      22              : #include "runtime.h"
      23              : #include "import.h"
      24              : #include "export.h"
      25              : #include "backend.h"
      26              : #include "gogo.h"
      27              : 
      28              : // Class Gogo.
      29              : 
      30         4646 : Gogo::Gogo(Backend* backend, Linemap* linemap, int, int pointer_size)
      31         4646 :   : backend_(backend),
      32         4646 :     linemap_(linemap),
      33         4646 :     package_(NULL),
      34         4646 :     functions_(),
      35         4646 :     globals_(new Bindings(NULL)),
      36         4646 :     file_block_names_(),
      37         4646 :     imports_(),
      38         4646 :     imported_unsafe_(false),
      39         4646 :     current_file_imported_unsafe_(false),
      40         4646 :     current_file_imported_embed_(false),
      41         4646 :     packages_(),
      42         4646 :     init_functions_(),
      43         4646 :     var_deps_(),
      44         4646 :     need_init_fn_(false),
      45         4646 :     init_fn_name_(),
      46         4646 :     imported_init_fns_(),
      47         4646 :     pkgpath_(),
      48         4646 :     pkgpath_symbol_(),
      49         4646 :     prefix_(),
      50         4646 :     pkgpath_set_(false),
      51         4646 :     pkgpath_from_option_(false),
      52         4646 :     prefix_from_option_(false),
      53         4646 :     relative_import_path_(),
      54         4646 :     c_header_(),
      55         4646 :     import_map_(),
      56         4646 :     package_file_(),
      57         4646 :     embed_patterns_(),
      58         4646 :     embed_files_(),
      59         4646 :     check_divide_by_zero_(true),
      60         4646 :     check_divide_overflow_(true),
      61         4646 :     compiling_runtime_(false),
      62         4646 :     debug_escape_level_(0),
      63         4646 :     debug_optimization_(false),
      64         4646 :     nil_check_size_threshold_(4096),
      65         4646 :     need_eqtype_(false),
      66         4646 :     verify_types_(),
      67         4646 :     interface_types_(),
      68         4646 :     specific_type_functions_(),
      69         4646 :     specific_type_functions_are_written_(false),
      70         4646 :     named_types_are_converted_(false),
      71         4646 :     analysis_sets_(),
      72         4646 :     gc_roots_(),
      73         4646 :     type_descriptors_(),
      74         4646 :     imported_inlinable_functions_(),
      75         4646 :     imported_inline_functions_()
      76              : {
      77         4646 :   const Location loc = Linemap::predeclared_location();
      78              : 
      79         4646 :   Named_type* uint8_type = Type::make_integer_type("uint8", true, 8,
      80              :                                                    RUNTIME_TYPE_KIND_UINT8);
      81         4646 :   this->add_named_type(uint8_type);
      82         4646 :   this->add_named_type(Type::make_integer_type("uint16", true,  16,
      83              :                                                RUNTIME_TYPE_KIND_UINT16));
      84         4646 :   this->add_named_type(Type::make_integer_type("uint32", true,  32,
      85              :                                                RUNTIME_TYPE_KIND_UINT32));
      86         4646 :   this->add_named_type(Type::make_integer_type("uint64", true,  64,
      87              :                                                RUNTIME_TYPE_KIND_UINT64));
      88              : 
      89         4646 :   this->add_named_type(Type::make_integer_type("int8",  false,   8,
      90              :                                                RUNTIME_TYPE_KIND_INT8));
      91         4646 :   this->add_named_type(Type::make_integer_type("int16", false,  16,
      92              :                                                RUNTIME_TYPE_KIND_INT16));
      93         4646 :   Named_type* int32_type = Type::make_integer_type("int32", false,  32,
      94              :                                                    RUNTIME_TYPE_KIND_INT32);
      95         4646 :   this->add_named_type(int32_type);
      96         4646 :   this->add_named_type(Type::make_integer_type("int64", false,  64,
      97              :                                                RUNTIME_TYPE_KIND_INT64));
      98              : 
      99         4646 :   this->add_named_type(Type::make_float_type("float32", 32,
     100              :                                              RUNTIME_TYPE_KIND_FLOAT32));
     101         4646 :   this->add_named_type(Type::make_float_type("float64", 64,
     102              :                                              RUNTIME_TYPE_KIND_FLOAT64));
     103              : 
     104         4646 :   this->add_named_type(Type::make_complex_type("complex64", 64,
     105              :                                                RUNTIME_TYPE_KIND_COMPLEX64));
     106         4646 :   this->add_named_type(Type::make_complex_type("complex128", 128,
     107              :                                                RUNTIME_TYPE_KIND_COMPLEX128));
     108              : 
     109         4646 :   int int_type_size = pointer_size;
     110         4646 :   if (int_type_size < 32)
     111              :     int_type_size = 32;
     112         4646 :   this->add_named_type(Type::make_integer_type("uint", true,
     113              :                                                int_type_size,
     114              :                                                RUNTIME_TYPE_KIND_UINT));
     115         4646 :   Named_type* int_type = Type::make_integer_type("int", false, int_type_size,
     116              :                                                  RUNTIME_TYPE_KIND_INT);
     117         4646 :   this->add_named_type(int_type);
     118              : 
     119         4646 :   this->add_named_type(Type::make_integer_type("uintptr", true,
     120              :                                                pointer_size,
     121              :                                                RUNTIME_TYPE_KIND_UINTPTR));
     122              : 
     123              :   // "byte" is an alias for "uint8".
     124         9292 :   uint8_type->integer_type()->set_is_byte();
     125         4646 :   this->add_named_type(Type::make_integer_type_alias("byte", uint8_type));
     126              : 
     127              :   // "rune" is an alias for "int32".
     128         9292 :   int32_type->integer_type()->set_is_rune();
     129         4646 :   this->add_named_type(Type::make_integer_type_alias("rune", int32_type));
     130              : 
     131         4646 :   this->add_named_type(Type::make_named_bool_type());
     132              : 
     133         4646 :   this->add_named_type(Type::make_named_string_type());
     134              : 
     135              :   // "error" is interface { Error() string }.
     136         4646 :   {
     137         4646 :     Typed_identifier_list *methods = new Typed_identifier_list;
     138         4646 :     Typed_identifier_list *results = new Typed_identifier_list;
     139         9292 :     results->push_back(Typed_identifier("", Type::lookup_string_type(), loc));
     140         4646 :     Type *method_type = Type::make_function_type(NULL, NULL, results, loc);
     141         9292 :     methods->push_back(Typed_identifier("Error", method_type, loc));
     142         4646 :     Interface_type *error_iface = Type::make_interface_type(methods, loc);
     143         4646 :     error_iface->finalize_methods();
     144         4646 :     Named_type *error_type = Named_object::make_type("error", NULL, error_iface, loc)->type_value();
     145         4646 :     this->add_named_type(error_type);
     146              :   }
     147              : 
     148              :   // "any" is an alias for the empty interface type.
     149         4646 :   {
     150         4646 :     Type* empty = Type::make_empty_interface_type(loc);
     151         4646 :     Named_object* no = Named_object::make_type("any", NULL, empty, loc);
     152         4646 :     Named_type* nt = no->type_value();
     153         4646 :     nt->set_is_alias();
     154         4646 :     this->add_named_type(nt);
     155              :   }
     156              : 
     157        13938 :   this->globals_->add_constant(Typed_identifier("true",
     158              :                                                 Type::make_boolean_type(),
     159         9292 :                                                 loc),
     160              :                                NULL,
     161              :                                Expression::make_boolean(true, loc),
     162              :                                0);
     163        13938 :   this->globals_->add_constant(Typed_identifier("false",
     164              :                                                 Type::make_boolean_type(),
     165         9292 :                                                 loc),
     166              :                                NULL,
     167              :                                Expression::make_boolean(false, loc),
     168              :                                0);
     169              : 
     170        13938 :   this->globals_->add_constant(Typed_identifier("nil", Type::make_nil_type(),
     171         9292 :                                                 loc),
     172              :                                NULL,
     173              :                                Expression::make_nil(loc),
     174              :                                0);
     175              : 
     176         4646 :   Type* abstract_int_type = Type::make_abstract_integer_type();
     177         9292 :   this->globals_->add_constant(Typed_identifier("iota", abstract_int_type,
     178         4646 :                                                 loc),
     179              :                                NULL,
     180              :                                Expression::make_iota(),
     181              :                                0);
     182              : 
     183         4646 :   Function_type* new_type = Type::make_function_type(NULL, NULL, NULL, loc);
     184         4646 :   new_type->set_is_varargs();
     185         4646 :   new_type->set_is_builtin();
     186         4646 :   this->globals_->add_function_declaration("new", NULL, new_type, loc);
     187              : 
     188         4646 :   Function_type* make_type = Type::make_function_type(NULL, NULL, NULL, loc);
     189         4646 :   make_type->set_is_varargs();
     190         4646 :   make_type->set_is_builtin();
     191         4646 :   this->globals_->add_function_declaration("make", NULL, make_type, loc);
     192              : 
     193         4646 :   Typed_identifier_list* len_result = new Typed_identifier_list();
     194         9292 :   len_result->push_back(Typed_identifier("", int_type, loc));
     195         4646 :   Function_type* len_type = Type::make_function_type(NULL, NULL, len_result,
     196              :                                                      loc);
     197         4646 :   len_type->set_is_builtin();
     198         4646 :   this->globals_->add_function_declaration("len", NULL, len_type, loc);
     199              : 
     200         4646 :   Typed_identifier_list* cap_result = new Typed_identifier_list();
     201         9292 :   cap_result->push_back(Typed_identifier("", int_type, loc));
     202         4646 :   Function_type* cap_type = Type::make_function_type(NULL, NULL, len_result,
     203              :                                                      loc);
     204         4646 :   cap_type->set_is_builtin();
     205         4646 :   this->globals_->add_function_declaration("cap", NULL, cap_type, loc);
     206              : 
     207         4646 :   Function_type* print_type = Type::make_function_type(NULL, NULL, NULL, loc);
     208         4646 :   print_type->set_is_varargs();
     209         4646 :   print_type->set_is_builtin();
     210         4646 :   this->globals_->add_function_declaration("print", NULL, print_type, loc);
     211              : 
     212         4646 :   print_type = Type::make_function_type(NULL, NULL, NULL, loc);
     213         4646 :   print_type->set_is_varargs();
     214         4646 :   print_type->set_is_builtin();
     215         4646 :   this->globals_->add_function_declaration("println", NULL, print_type, loc);
     216              : 
     217         4646 :   Type *empty = Type::make_empty_interface_type(loc);
     218         4646 :   Typed_identifier_list* panic_parms = new Typed_identifier_list();
     219         9292 :   panic_parms->push_back(Typed_identifier("e", empty, loc));
     220         4646 :   Function_type *panic_type = Type::make_function_type(NULL, panic_parms,
     221              :                                                        NULL, loc);
     222         4646 :   panic_type->set_is_builtin();
     223         4646 :   this->globals_->add_function_declaration("panic", NULL, panic_type, loc);
     224              : 
     225         4646 :   Typed_identifier_list* recover_result = new Typed_identifier_list();
     226         9292 :   recover_result->push_back(Typed_identifier("", empty, loc));
     227         4646 :   Function_type* recover_type = Type::make_function_type(NULL, NULL,
     228              :                                                          recover_result,
     229              :                                                          loc);
     230         4646 :   recover_type->set_is_builtin();
     231         4646 :   this->globals_->add_function_declaration("recover", NULL, recover_type, loc);
     232              : 
     233         4646 :   Function_type* close_type = Type::make_function_type(NULL, NULL, NULL, loc);
     234         4646 :   close_type->set_is_varargs();
     235         4646 :   close_type->set_is_builtin();
     236         4646 :   this->globals_->add_function_declaration("close", NULL, close_type, loc);
     237              : 
     238         4646 :   Typed_identifier_list* copy_result = new Typed_identifier_list();
     239         9292 :   copy_result->push_back(Typed_identifier("", int_type, loc));
     240         4646 :   Function_type* copy_type = Type::make_function_type(NULL, NULL,
     241              :                                                       copy_result, loc);
     242         4646 :   copy_type->set_is_varargs();
     243         4646 :   copy_type->set_is_builtin();
     244         4646 :   this->globals_->add_function_declaration("copy", NULL, copy_type, loc);
     245              : 
     246         4646 :   Function_type* append_type = Type::make_function_type(NULL, NULL, NULL, loc);
     247         4646 :   append_type->set_is_varargs();
     248         4646 :   append_type->set_is_builtin();
     249         4646 :   this->globals_->add_function_declaration("append", NULL, append_type, loc);
     250              : 
     251         4646 :   Function_type* complex_type = Type::make_function_type(NULL, NULL, NULL, loc);
     252         4646 :   complex_type->set_is_varargs();
     253         4646 :   complex_type->set_is_builtin();
     254         4646 :   this->globals_->add_function_declaration("complex", NULL, complex_type, loc);
     255              : 
     256         4646 :   Function_type* real_type = Type::make_function_type(NULL, NULL, NULL, loc);
     257         4646 :   real_type->set_is_varargs();
     258         4646 :   real_type->set_is_builtin();
     259         4646 :   this->globals_->add_function_declaration("real", NULL, real_type, loc);
     260              : 
     261         4646 :   Function_type* imag_type = Type::make_function_type(NULL, NULL, NULL, loc);
     262         4646 :   imag_type->set_is_varargs();
     263         4646 :   imag_type->set_is_builtin();
     264         4646 :   this->globals_->add_function_declaration("imag", NULL, imag_type, loc);
     265              : 
     266         4646 :   Function_type* delete_type = Type::make_function_type(NULL, NULL, NULL, loc);
     267         4646 :   delete_type->set_is_varargs();
     268         4646 :   delete_type->set_is_builtin();
     269         4646 :   this->globals_->add_function_declaration("delete", NULL, delete_type, loc);
     270         4646 : }
     271              : 
     272              : std::string
     273       241607 : Gogo::pkgpath_for_symbol(const std::string& pkgpath)
     274              : {
     275       241607 :   go_assert(!pkgpath.empty());
     276       241607 :   return go_encode_id(pkgpath);
     277              : }
     278              : 
     279              : // Return a hash code for a string, given a starting hash.
     280              : 
     281              : unsigned int
     282     13119351 : Gogo::hash_string(const std::string& s, unsigned int h)
     283              : {
     284     13119351 :   const char* p = s.data();
     285     13119351 :   size_t len = s.length();
     286    116448893 :   for (; len > 0; --len)
     287              :     {
     288    103329542 :       h ^= *p++;
     289    103329542 :       h*= 16777619;
     290              :     }
     291     13119351 :   return h;
     292              : }
     293              : 
     294              : // Get the package path to use for type reflection data.  This should
     295              : // ideally be unique across the entire link.
     296              : 
     297              : const std::string&
     298      7563715 : Gogo::pkgpath() const
     299              : {
     300      7563715 :   go_assert(this->pkgpath_set_);
     301      7563715 :   return this->pkgpath_;
     302              : }
     303              : 
     304              : // Set the package path from the -fgo-pkgpath command line option.
     305              : 
     306              : void
     307         2014 : Gogo::set_pkgpath(const std::string& arg)
     308              : {
     309         2014 :   go_assert(!this->pkgpath_set_);
     310         2014 :   this->pkgpath_ = arg;
     311         2014 :   this->pkgpath_set_ = true;
     312         2014 :   this->pkgpath_from_option_ = true;
     313         2014 : }
     314              : 
     315              : // Get the package path to use for symbol names.
     316              : 
     317              : const std::string&
     318       199256 : Gogo::pkgpath_symbol() const
     319              : {
     320       199256 :   go_assert(this->pkgpath_set_);
     321       199256 :   return this->pkgpath_symbol_;
     322              : }
     323              : 
     324              : // Set the unique prefix to use to determine the package path, from
     325              : // the -fgo-prefix command line option.
     326              : 
     327              : void
     328            0 : Gogo::set_prefix(const std::string& arg)
     329              : {
     330            0 :   go_assert(!this->prefix_from_option_);
     331            0 :   this->prefix_ = arg;
     332            0 :   this->prefix_from_option_ = true;
     333            0 : }
     334              : 
     335              : // Munge name for use in an error message.
     336              : 
     337              : std::string
     338       275708 : Gogo::message_name(const std::string& name)
     339              : {
     340       275708 :   return go_localize_identifier(Gogo::unpack_hidden_name(name).c_str());
     341              : }
     342              : 
     343              : // Get the package name.
     344              : 
     345              : const std::string&
     346       349540 : Gogo::package_name() const
     347              : {
     348       349540 :   go_assert(this->package_ != NULL);
     349       349540 :   return this->package_->package_name();
     350              : }
     351              : 
     352              : // Set the package name.
     353              : 
     354              : void
     355        12707 : Gogo::set_package_name(const std::string& package_name,
     356              :                        Location location)
     357              : {
     358        12707 :   if (this->package_ != NULL)
     359              :     {
     360         8061 :       if (this->package_->package_name() != package_name)
     361            0 :         go_error_at(location, "expected package %qs",
     362            0 :                     Gogo::message_name(this->package_->package_name()).c_str());
     363         8061 :       return;
     364              :     }
     365              : 
     366              :   // Now that we know the name of the package we are compiling, set
     367              :   // the package path to use for reflect.Type.PkgPath and global
     368              :   // symbol names.
     369         4646 :   if (this->pkgpath_set_)
     370         2014 :     this->pkgpath_symbol_ = Gogo::pkgpath_for_symbol(this->pkgpath_);
     371              :   else
     372              :     {
     373         2632 :       if (!this->prefix_from_option_ && package_name == "main")
     374              :         {
     375         1889 :           this->pkgpath_ = package_name;
     376         1889 :           this->pkgpath_symbol_ = Gogo::pkgpath_for_symbol(package_name);
     377              :         }
     378              :       else
     379              :         {
     380          743 :           if (!this->prefix_from_option_)
     381          743 :             this->prefix_ = "go";
     382          743 :           this->pkgpath_ = this->prefix_ + '.' + package_name;
     383         1486 :           this->pkgpath_symbol_ = (Gogo::pkgpath_for_symbol(this->prefix_) + '.'
     384         2229 :                                    + Gogo::pkgpath_for_symbol(package_name));
     385              :         }
     386         2632 :       this->pkgpath_set_ = true;
     387              :     }
     388              : 
     389         9292 :   this->package_ = this->register_package(this->pkgpath_,
     390         4646 :                                           this->pkgpath_symbol_, location);
     391         4646 :   this->package_->set_package_name(package_name, location);
     392              : 
     393         4646 :   if (this->is_main_package())
     394              :     {
     395              :       // Declare "main" as a function which takes no parameters and
     396              :       // returns no value.
     397         1889 :       Location uloc = Linemap::unknown_location();
     398         1889 :       this->declare_function(Gogo::pack_hidden_name("main", false),
     399              :                              Type::make_function_type (NULL, NULL, NULL, uloc),
     400              :                              uloc);
     401              :     }
     402              : }
     403              : 
     404              : // Return whether this is the "main" package.  This is not true if
     405              : // -fgo-pkgpath or -fgo-prefix was used.
     406              : 
     407              : bool
     408        39039 : Gogo::is_main_package() const
     409              : {
     410        39039 :   return (this->package_name() == "main"
     411        19480 :           && !this->pkgpath_from_option_
     412        58467 :           && !this->prefix_from_option_);
     413              : }
     414              : 
     415              : // Import a package.
     416              : 
     417              : void
     418        42242 : Gogo::import_package(const std::string& filename,
     419              :                      const std::string& local_name,
     420              :                      bool is_local_name_exported,
     421              :                      bool must_exist,
     422              :                      Location location)
     423              : {
     424        42242 :   if (filename.empty())
     425              :     {
     426            1 :       go_error_at(location, "import path is empty");
     427        20435 :       return;
     428              :     }
     429              : 
     430        42241 :   const char *pf = filename.data();
     431        42241 :   const char *pend = pf + filename.length();
     432       406477 :   while (pf < pend)
     433              :     {
     434       364237 :       unsigned int c;
     435       364237 :       int adv = Lex::fetch_char(pf, &c);
     436       364237 :       if (adv == 0)
     437              :         {
     438            0 :           go_error_at(location, "import path contains invalid UTF-8 sequence");
     439            1 :           return;
     440              :         }
     441       364237 :       if (c == '\0')
     442              :         {
     443            0 :           go_error_at(location, "import path contains NUL");
     444            0 :           return;
     445              :         }
     446       364237 :       if (c < 0x20 || c == 0x7f)
     447              :         {
     448            1 :           go_error_at(location, "import path contains control character");
     449            1 :           return;
     450              :         }
     451       364236 :       if (c == '\\')
     452              :         {
     453            0 :           go_error_at(location, "import path contains backslash; use slash");
     454            0 :           return;
     455              :         }
     456       364236 :       if (Lex::is_unicode_space(c))
     457              :         {
     458            0 :           go_error_at(location, "import path contains space character");
     459            0 :           return;
     460              :         }
     461       364236 :       if (c < 0x7f && strchr("!\"#$%&'()*,:;<=>?[]^`{|}", c) != NULL)
     462              :         {
     463            0 :           go_error_at(location,
     464              :                       "import path contains invalid character '%c'", c);
     465            0 :           return;
     466              :         }
     467       364236 :       pf += adv;
     468              :     }
     469              : 
     470        42240 :   if (IS_ABSOLUTE_PATH(filename.c_str()))
     471              :     {
     472            0 :       go_error_at(location, "import path cannot be absolute path");
     473            0 :       return;
     474              :     }
     475              : 
     476        42240 :   if (local_name == "init")
     477            1 :     go_error_at(location, "cannot import package as init");
     478              : 
     479        42240 :   if (filename == "unsafe")
     480              :     {
     481         1471 :       this->import_unsafe(local_name, is_local_name_exported, location);
     482         1471 :       this->current_file_imported_unsafe_ = true;
     483         1471 :       return;
     484              :     }
     485              : 
     486        40769 :   if (filename == "embed")
     487            8 :     this->current_file_imported_embed_ = true;
     488              : 
     489        40769 :   Imports::const_iterator p = this->imports_.find(filename);
     490        40769 :   if (p != this->imports_.end())
     491              :     {
     492        18951 :       Package* package = p->second;
     493        18951 :       package->set_location(location);
     494        18951 :       std::string ln = local_name;
     495        18951 :       bool is_ln_exported = is_local_name_exported;
     496        18951 :       if (ln.empty())
     497              :         {
     498        18415 :           ln = package->package_name();
     499        18415 :           go_assert(!ln.empty());
     500        18415 :           is_ln_exported = Lex::is_exported_name(ln);
     501              :         }
     502        18951 :       if (ln == "_")
     503              :         ;
     504        18842 :       else if (ln == ".")
     505              :         {
     506          329 :           Bindings* bindings = package->bindings();
     507        62651 :           for (Bindings::const_declarations_iterator pd =
     508          329 :                  bindings->begin_declarations();
     509        62651 :                pd != bindings->end_declarations();
     510        62322 :                ++pd)
     511        62322 :             this->add_dot_import_object(pd->second);
     512          329 :           std::string dot_alias = "." + package->package_name();
     513          329 :           package->add_alias(dot_alias, location);
     514          329 :         }
     515              :       else
     516              :         {
     517        18513 :           package->add_alias(ln, location);
     518        18513 :           ln = this->pack_hidden_name(ln, is_ln_exported);
     519        18513 :           this->package_->bindings()->add_package(ln, package);
     520              :         }
     521        18951 :       return;
     522        18951 :     }
     523              : 
     524              :   // If we are using an importcfg file we have to check two mappings.
     525              :   // IMPORT_MAP_ is a mapping from package path to real package path,
     526              :   // for vendoring.  PACKAGE_FILE_ is a mapping from package path to
     527              :   // file name, to find the file in the build cache.
     528        21818 :   std::string path = filename;
     529        21818 :   Unordered_map(std::string, std::string)::const_iterator pi;
     530        21818 :   pi = this->import_map_.find(filename);
     531        21818 :   if (pi != this->import_map_.end())
     532           19 :     path = pi->second;
     533        21818 :   pi = this->package_file_.find(path);
     534        21818 :   if (pi != this->package_file_.end())
     535          702 :     path = pi->second;
     536              : 
     537        43636 :   Import::Stream* stream = Import::open_package(path, location,
     538        21818 :                                                 this->relative_import_path_);
     539        21818 :   if (stream == NULL)
     540              :     {
     541           10 :       if (must_exist)
     542            0 :         go_error_at(location, "import file %qs not found", filename.c_str());
     543           10 :       return;
     544              :     }
     545              : 
     546        21808 :   Import* imp = new Import(stream, location);
     547        21808 :   imp->register_builtin_types(this);
     548        21808 :   Package* package = imp->import(this, local_name, is_local_name_exported);
     549        21808 :   if (package != NULL)
     550              :     {
     551        21808 :       if (package->pkgpath() == this->pkgpath())
     552            0 :         go_error_at(location,
     553              :                     ("imported package uses same package path as package "
     554              :                      "being compiled (see %<-fgo-pkgpath%> option)"));
     555              : 
     556        43616 :       this->imports_.insert(std::make_pair(filename, package));
     557              :     }
     558              : 
     559        21808 :   imp->clear_stream();
     560        21808 :   delete stream;
     561              : 
     562              :   // FIXME: we never delete imp; we may need it for inlinable functions.
     563        21818 : }
     564              : 
     565              : Import_init *
     566      1549811 : Gogo::lookup_init(const std::string& init_name)
     567              : {
     568      1549811 :   Import_init tmp("", init_name, -1);
     569      1549811 :   Import_init_set::iterator it = this->imported_init_fns_.find(&tmp);
     570      1549811 :   return (it != this->imported_init_fns_.end()) ? *it : NULL;
     571      1549811 : }
     572              : 
     573              : // Add an import control function for an imported package to the list.
     574              : 
     575              : void
     576       660202 : Gogo::add_import_init_fn(const std::string& package_name,
     577              :                          const std::string& init_name, int prio)
     578              : {
     579     30250330 :   for (Import_init_set::iterator p =
     580       660202 :          this->imported_init_fns_.begin();
     581     30910532 :        p != this->imported_init_fns_.end();
     582     30250330 :        ++p)
     583              :     {
     584     30748345 :       Import_init *ii = (*p);
     585     30748345 :       if (ii->init_name() == init_name)
     586              :         {
     587              :           // If a test of package P1, built as part of package P1,
     588              :           // imports package P2, and P2 imports P1 (perhaps
     589              :           // indirectly), then we will see the same import name with
     590              :           // different import priorities.  That is OK, so don't give
     591              :           // an error about it.
     592       498015 :           if (ii->package_name() != package_name)
     593              :             {
     594            0 :               go_error_at(Linemap::unknown_location(),
     595              :                        "duplicate package initialization name %qs",
     596            0 :                        Gogo::message_name(init_name).c_str());
     597            0 :               go_inform(Linemap::unknown_location(), "used by package %qs",
     598            0 :                         Gogo::message_name(ii->package_name()).c_str());
     599            0 :               go_inform(Linemap::unknown_location(), " and by package %qs",
     600            0 :                         Gogo::message_name(package_name).c_str());
     601              :             }
     602       498015 :           ii->set_priority(prio);
     603       498015 :           return;
     604              :         }
     605              :     }
     606              : 
     607       162187 :   Import_init* nii = new Import_init(package_name, init_name, prio);
     608       162187 :   this->imported_init_fns_.insert(nii);
     609              : }
     610              : 
     611              : // Return whether we are at the global binding level.
     612              : 
     613              : bool
     614       711022 : Gogo::in_global_scope() const
     615              : {
     616       711022 :   return this->functions_.empty();
     617              : }
     618              : 
     619              : // Return the current binding contour.
     620              : 
     621              : Bindings*
     622       809029 : Gogo::current_bindings()
     623              : {
     624       809029 :   if (!this->functions_.empty())
     625       388528 :     return this->functions_.back().blocks.back()->bindings();
     626       420501 :   else if (this->package_ != NULL)
     627       322935 :     return this->package_->bindings();
     628              :   else
     629        97566 :     return this->globals_;
     630              : }
     631              : 
     632              : const Bindings*
     633            0 : Gogo::current_bindings() const
     634              : {
     635            0 :   if (!this->functions_.empty())
     636            0 :     return this->functions_.back().blocks.back()->bindings();
     637            0 :   else if (this->package_ != NULL)
     638            0 :     return this->package_->bindings();
     639              :   else
     640            0 :     return this->globals_;
     641              : }
     642              : 
     643              : void
     644        62428 : Gogo::update_init_priority(Import_init* ii,
     645              :                            std::set<const Import_init *>* visited)
     646              : {
     647        62428 :   visited->insert(ii);
     648        62428 :   int succ_prior = -1;
     649              : 
     650       445578 :   for (std::set<std::string>::const_iterator pci =
     651        62428 :            ii->precursors().begin();
     652       508006 :        pci != ii->precursors().end();
     653       445578 :        ++pci)
     654              :     {
     655       445578 :       Import_init* succ = this->lookup_init(*pci);
     656       445578 :       if (visited->find(succ) == visited->end())
     657        32671 :         update_init_priority(succ, visited);
     658       445578 :       succ_prior = std::max(succ_prior, succ->priority());
     659              :     }
     660        62428 :   if (ii->priority() <= succ_prior)
     661        62428 :     ii->set_priority(succ_prior + 1);
     662        62428 : }
     663              : 
     664              : void
     665         1879 : Gogo::recompute_init_priorities()
     666              : {
     667         1879 :   std::set<Import_init *> nonroots;
     668              : 
     669        62428 :   for (Import_init_set::const_iterator p =
     670         1879 :            this->imported_init_fns_.begin();
     671        64307 :        p != this->imported_init_fns_.end();
     672        62428 :        ++p)
     673              :     {
     674        62428 :       const Import_init *ii = *p;
     675       508006 :       for (std::set<std::string>::const_iterator pci =
     676        62428 :                ii->precursors().begin();
     677       508006 :            pci != ii->precursors().end();
     678       445578 :            ++pci)
     679              :         {
     680       445578 :           Import_init* ii_init = this->lookup_init(*pci);
     681       445578 :           nonroots.insert(ii_init);
     682              :         }
     683              :     }
     684              : 
     685              :   // Recursively update priorities starting at roots.
     686         1879 :   std::set<const Import_init*> visited;
     687        62428 :   for (Import_init_set::iterator p =
     688         1879 :            this->imported_init_fns_.begin();
     689        64307 :        p != this->imported_init_fns_.end();
     690        62428 :        ++p)
     691              :     {
     692        62428 :       Import_init* ii = *p;
     693        62428 :       if (nonroots.find(ii) != nonroots.end())
     694        32671 :         continue;
     695        29757 :       update_init_priority(ii, &visited);
     696              :     }
     697         1879 : }
     698              : 
     699              : // Add statements to INIT_STMTS which run the initialization
     700              : // functions for imported packages.  This is only used for the "main"
     701              : // package.
     702              : 
     703              : void
     704         1889 : Gogo::init_imports(std::vector<Bstatement*>& init_stmts, Bfunction *bfunction)
     705              : {
     706         1889 :   go_assert(this->is_main_package());
     707              : 
     708         1889 :   if (this->imported_init_fns_.empty())
     709           10 :     return;
     710              : 
     711         1879 :   Location unknown_loc = Linemap::unknown_location();
     712         1879 :   Function_type* func_type =
     713         1879 :       Type::make_function_type(NULL, NULL, NULL, unknown_loc);
     714         1879 :   Btype* fntype = func_type->get_backend_fntype(this);
     715              : 
     716              :   // Recompute init priorities based on a walk of the init graph.
     717         1879 :   recompute_init_priorities();
     718              : 
     719              :   // We must call them in increasing priority order.
     720         1879 :   std::vector<const Import_init*> v;
     721        62428 :   for (Import_init_set::const_iterator p =
     722         1879 :          this->imported_init_fns_.begin();
     723        64307 :        p != this->imported_init_fns_.end();
     724        62428 :        ++p)
     725              :     {
     726              :       // Don't include dummy inits. They are not real functions.
     727        62428 :       if ((*p)->is_dummy())
     728        27184 :         continue;
     729        35244 :       if ((*p)->priority() < 0)
     730            0 :         go_error_at(Linemap::unknown_location(),
     731              :                     "internal error: failed to set init priority for %s",
     732            0 :                     (*p)->package_name().c_str());
     733        35244 :       v.push_back(*p);
     734              :     }
     735         1879 :   std::sort(v.begin(), v.end(), priority_compare);
     736              : 
     737              :   // We build calls to the init functions, which take no arguments.
     738         1879 :   std::vector<Bexpression*> empty_args;
     739         1879 :   for (std::vector<const Import_init*>::const_iterator p = v.begin();
     740        37123 :        p != v.end();
     741        35244 :        ++p)
     742              :     {
     743        35244 :       const Import_init* ii = *p;
     744        35244 :       std::string user_name = ii->package_name() + ".init";
     745        35244 :       const std::string& init_name(ii->init_name());
     746        35244 :       const unsigned int flags =
     747              :         (Backend::function_is_visible
     748              :          | Backend::function_is_declaration
     749              :          | Backend::function_is_inlinable);
     750        35244 :       Bfunction* pfunc = this->backend()->function(fntype, user_name, init_name,
     751              :                                                    flags, unknown_loc);
     752        35244 :       Bexpression* pfunc_code =
     753        35244 :           this->backend()->function_code_expression(pfunc, unknown_loc);
     754        35244 :       Bexpression* pfunc_call =
     755        35244 :           this->backend()->call_expression(bfunction, pfunc_code, empty_args,
     756              :                                            NULL, unknown_loc);
     757        35244 :       init_stmts.push_back(this->backend()->expression_statement(bfunction,
     758              :                                                                  pfunc_call));
     759        35244 :     }
     760         1879 : }
     761              : 
     762              : // Register global variables with the garbage collector.  We need to
     763              : // register all variables which can hold a pointer value.  They become
     764              : // roots during the mark phase.  We build a struct that is easy to
     765              : // hook into a list of roots.
     766              : 
     767              : // type gcRoot struct {
     768              : //      decl    unsafe.Pointer // Pointer to variable.
     769              : //      size    uintptr        // Total size of variable.
     770              : //      ptrdata uintptr        // Length of variable's gcdata.
     771              : //      gcdata  *byte          // Pointer mask.
     772              : // }
     773              : //
     774              : // type gcRootList struct {
     775              : //      next  *gcRootList
     776              : //      count int
     777              : //      roots [...]gcRoot
     778              : // }
     779              : 
     780              : // The last entry in the roots array has a NULL decl field.
     781              : 
     782              : void
     783         4646 : Gogo::register_gc_vars(const std::vector<Named_object*>& var_gc,
     784              :                        std::vector<Bstatement*>& init_stmts,
     785              :                        Bfunction* init_bfn)
     786              : {
     787         4646 :   if (var_gc.empty() && this->gc_roots_.empty())
     788         2535 :     return;
     789              : 
     790         2111 :   Type* pvt = Type::make_pointer_type(Type::make_void_type());
     791         2111 :   Type* uintptr_type = Type::lookup_integer_type("uintptr");
     792         2111 :   Type* byte_type = Type::lookup_integer_type("byte");
     793         2111 :   Type* pointer_byte_type = Type::make_pointer_type(byte_type);
     794         2111 :   Struct_type* root_type =
     795         2111 :     Type::make_builtin_struct_type(4,
     796              :                                    "decl", pvt,
     797              :                                    "size", uintptr_type,
     798              :                                    "ptrdata", uintptr_type,
     799              :                                    "gcdata", pointer_byte_type);
     800              : 
     801         2111 :   Location builtin_loc = Linemap::predeclared_location();
     802         2111 :   unsigned long roots_len = var_gc.size() + this->gc_roots_.size();
     803         2111 :   Expression* length = Expression::make_integer_ul(roots_len, NULL,
     804              :                                                    builtin_loc);
     805         2111 :   Array_type* root_array_type = Type::make_array_type(root_type, length);
     806         2111 :   root_array_type->set_is_array_incomparable();
     807              : 
     808         2111 :   Type* int_type = Type::lookup_integer_type("int");
     809         2111 :   Struct_type* root_list_type =
     810         2111 :       Type::make_builtin_struct_type(3,
     811              :                                      "next", pvt,
     812              :                                      "count", int_type,
     813              :                                      "roots", root_array_type);
     814              : 
     815              :   // Build an initializer for the roots array.
     816              : 
     817         2111 :   Expression_list* roots_init = new Expression_list();
     818              : 
     819         2111 :   for (std::vector<Named_object*>::const_iterator p = var_gc.begin();
     820        21500 :        p != var_gc.end();
     821        19389 :        ++p)
     822              :     {
     823        19389 :       Expression_list* init = new Expression_list();
     824              : 
     825        19389 :       Location no_loc = (*p)->location();
     826        19389 :       Expression* decl = Expression::make_var_reference(*p, no_loc);
     827        19389 :       Expression* decl_addr =
     828        19389 :           Expression::make_unary(OPERATOR_AND, decl, no_loc);
     829        19389 :       decl_addr->unary_expression()->set_does_not_escape();
     830        19389 :       decl_addr = Expression::make_cast(pvt, decl_addr, no_loc);
     831        19389 :       init->push_back(decl_addr);
     832              : 
     833        19389 :       Expression* size =
     834        19389 :         Expression::make_type_info(decl->type(),
     835              :                                    Expression::TYPE_INFO_SIZE);
     836        19389 :       init->push_back(size);
     837              : 
     838        19389 :       Expression* ptrdata =
     839        19389 :         Expression::make_type_info(decl->type(),
     840              :                                    Expression::TYPE_INFO_BACKEND_PTRDATA);
     841        19389 :       init->push_back(ptrdata);
     842              : 
     843        19389 :       Expression* gcdata = Expression::make_ptrmask_symbol(decl->type());
     844        19389 :       init->push_back(gcdata);
     845              : 
     846        19389 :       Expression* root_ctor =
     847        19389 :           Expression::make_struct_composite_literal(root_type, init, no_loc);
     848        19389 :       roots_init->push_back(root_ctor);
     849              :     }
     850              : 
     851         2111 :   for (std::vector<Expression*>::const_iterator p = this->gc_roots_.begin();
     852         4771 :        p != this->gc_roots_.end();
     853         2660 :        ++p)
     854              :     {
     855         2660 :       Expression_list *init = new Expression_list();
     856              : 
     857         2660 :       Expression* expr = *p;
     858         2660 :       Location eloc = expr->location();
     859         2660 :       init->push_back(Expression::make_cast(pvt, expr, eloc));
     860              : 
     861         2660 :       Type* type = expr->type()->points_to();
     862         2660 :       go_assert(type != NULL);
     863              : 
     864         2660 :       Expression* size =
     865         2660 :         Expression::make_type_info(type,
     866              :                                    Expression::TYPE_INFO_SIZE);
     867         2660 :       init->push_back(size);
     868              : 
     869         2660 :       Expression* ptrdata =
     870         2660 :         Expression::make_type_info(type,
     871              :                                    Expression::TYPE_INFO_BACKEND_PTRDATA);
     872         2660 :       init->push_back(ptrdata);
     873              : 
     874         2660 :       Expression* gcdata = Expression::make_ptrmask_symbol(type);
     875         2660 :       init->push_back(gcdata);
     876              : 
     877         2660 :       Expression* root_ctor =
     878         2660 :         Expression::make_struct_composite_literal(root_type, init, eloc);
     879         2660 :       roots_init->push_back(root_ctor);
     880              :     }
     881              : 
     882              :   // Build a constructor for the struct.
     883              : 
     884         2111 :   Expression_list* root_list_init = new Expression_list();
     885         2111 :   root_list_init->push_back(Expression::make_nil(builtin_loc));
     886         2111 :   root_list_init->push_back(Expression::make_integer_ul(roots_len, int_type,
     887              :                                                         builtin_loc));
     888              : 
     889         2111 :   Expression* roots_ctor =
     890         2111 :       Expression::make_array_composite_literal(root_array_type, roots_init,
     891              :                                                builtin_loc);
     892         2111 :   root_list_init->push_back(roots_ctor);
     893              : 
     894         2111 :   Expression* root_list_ctor =
     895         2111 :       Expression::make_struct_composite_literal(root_list_type, root_list_init,
     896              :                                                 builtin_loc);
     897              : 
     898         2111 :   Expression* root_addr = Expression::make_unary(OPERATOR_AND, root_list_ctor,
     899              :                                                  builtin_loc);
     900         2111 :   root_addr->unary_expression()->set_is_gc_root();
     901         2111 :   Expression* register_roots = Runtime::make_call(this,
     902              :                                                   Runtime::REGISTER_GC_ROOTS,
     903              :                                                   builtin_loc, 1, root_addr);
     904              : 
     905         2111 :   Translate_context context(this, NULL, NULL, NULL);
     906         2111 :   Bexpression* bcall = register_roots->get_backend(&context);
     907         2111 :   init_stmts.push_back(this->backend()->expression_statement(init_bfn, bcall));
     908              : }
     909              : 
     910              : // Build the list of type descriptors defined in this package. This is to help
     911              : // the reflect package to find compiler-generated types.
     912              : 
     913              : // type typeDescriptorList struct {
     914              : //       count int
     915              : //       types [...]unsafe.Pointer
     916              : // }
     917              : 
     918              : static Struct_type*
     919         6535 : type_descriptor_list_type(unsigned long len)
     920              : {
     921         6535 :   Location builtin_loc = Linemap::predeclared_location();
     922         6535 :   Type* int_type = Type::lookup_integer_type("int");
     923         6535 :   Type* ptr_type = Type::make_pointer_type(Type::make_void_type());
     924              :   // Avoid creating zero-length type.
     925         6535 :   unsigned long nelems = (len != 0 ? len : 1);
     926         6535 :   Expression* len_expr = Expression::make_integer_ul(nelems, NULL,
     927              :                                                      builtin_loc);
     928         6535 :   Array_type* array_type = Type::make_array_type(ptr_type, len_expr);
     929         6535 :   array_type->set_is_array_incomparable();
     930         6535 :   Struct_type* list_type =
     931         6535 :     Type::make_builtin_struct_type(2, "count", int_type,
     932              :                                    "types", array_type);
     933         6535 :   return list_type;
     934              : }
     935              : 
     936              : void
     937         4646 : Gogo::build_type_descriptor_list()
     938              : {
     939              :   // Create the list type
     940         4646 :   Location builtin_loc = Linemap::predeclared_location();
     941         4646 :   unsigned long len = this->type_descriptors_.size();
     942         4646 :   Struct_type* list_type = type_descriptor_list_type(len);
     943         4646 :   Btype* bt = list_type->get_backend(this);
     944         4646 :   Btype* bat = list_type->field(1)->type()->get_backend(this);
     945              : 
     946              :   // Create the variable
     947         4646 :   std::string name = this->type_descriptor_list_symbol(this->pkgpath_symbol());
     948         4646 :   unsigned int flags = Backend::variable_is_constant;
     949         4646 :   Bvariable* bv = this->backend()->implicit_variable(name, name, bt, flags, 0);
     950              : 
     951              :   // Build the initializer
     952         4646 :   std::vector<unsigned long> indexes;
     953         4646 :   std::vector<Bexpression*> vals;
     954         4646 :   std::vector<Type*>::iterator p = this->type_descriptors_.begin();
     955       180909 :   for (unsigned long i = 0; i < len; ++i, ++p)
     956              :     {
     957       176263 :       Bexpression* bexpr = (*p)->type_descriptor_pointer(this,
     958       176263 :                                                          builtin_loc);
     959       176263 :       indexes.push_back(i);
     960       176263 :       vals.push_back(bexpr);
     961              :     }
     962         4646 :   Bexpression* barray =
     963         4646 :     this->backend()->array_constructor_expression(bat, indexes, vals,
     964         4646 :                                                   builtin_loc);
     965              : 
     966         4646 :   Translate_context context(this, NULL, NULL, NULL);
     967         4646 :   std::vector<Bexpression*> fields;
     968         4646 :   Expression* len_expr = Expression::make_integer_ul(len, NULL,
     969              :                                                      builtin_loc);
     970         4646 :   fields.push_back(len_expr->get_backend(&context));
     971         4646 :   fields.push_back(barray);
     972         4646 :   Bexpression* binit =
     973         4646 :     this->backend()->constructor_expression(bt, fields, builtin_loc);
     974              : 
     975         4646 :   this->backend()->implicit_variable_set_init(bv, name, bt, flags, binit);
     976         4646 : }
     977              : 
     978              : // Register the type descriptors with the runtime.  This is to help
     979              : // the reflect package to find compiler-generated types.
     980              : 
     981              : void
     982         1889 : Gogo::register_type_descriptors(std::vector<Bstatement*>& init_stmts,
     983              :                                 Bfunction* init_bfn)
     984              : {
     985              :   // Create the list type
     986         1889 :   Location builtin_loc = Linemap::predeclared_location();
     987         1889 :   Struct_type* list_type = type_descriptor_list_type(1);
     988         1889 :   Btype* bt = list_type->get_backend(this);
     989              : 
     990              :   // Collect type lists from transitive imports.
     991         1889 :   std::vector<std::string> list_names;
     992         1889 :   for (Import_init_set::iterator it = this->imported_init_fns_.begin();
     993        64317 :        it != this->imported_init_fns_.end();
     994        62428 :        ++it)
     995              :     {
     996        62428 :       std::string pkgpath_symbol =
     997       124856 :         this->pkgpath_symbol_from_init_fn_name((*it)->init_name());
     998        62428 :       list_names.push_back(this->type_descriptor_list_symbol(pkgpath_symbol));
     999        62428 :     }
    1000              :   // Add the main package itself.
    1001         1889 :   list_names.push_back(this->type_descriptor_list_symbol("main"));
    1002              : 
    1003              :   // Build a list of lists.
    1004         1889 :   std::vector<unsigned long> indexes;
    1005         1889 :   std::vector<Bexpression*> vals;
    1006         1889 :   unsigned long i = 0;
    1007         1889 :   for (std::vector<std::string>::iterator p = list_names.begin();
    1008        66206 :        p != list_names.end();
    1009        64317 :        ++p)
    1010              :     {
    1011        64317 :       Bvariable* bv =
    1012        64317 :         this->backend()->implicit_variable_reference(*p, *p, bt);
    1013        64317 :       Bexpression* bexpr = this->backend()->var_expression(bv, builtin_loc);
    1014        64317 :       bexpr = this->backend()->address_expression(bexpr, builtin_loc);
    1015              : 
    1016        64317 :       indexes.push_back(i);
    1017        64317 :       vals.push_back(bexpr);
    1018        64317 :       i++;
    1019              :     }
    1020         1889 :   Expression* len_expr = Expression::make_integer_ul(i, NULL, builtin_loc);
    1021         1889 :   Type* list_ptr_type = Type::make_pointer_type(list_type);
    1022         1889 :   Type* list_array_type = Type::make_array_type(list_ptr_type, len_expr);
    1023         1889 :   Btype* bat = list_array_type->get_backend(this);
    1024         1889 :   Bexpression* barray =
    1025         1889 :     this->backend()->array_constructor_expression(bat, indexes, vals,
    1026              :                                                   builtin_loc);
    1027              : 
    1028              :   // Create a variable holding the list.
    1029         1889 :   std::string name = this->typelists_symbol();
    1030         1889 :   unsigned int flags = (Backend::variable_is_hidden
    1031              :                         | Backend::variable_is_constant);
    1032         1889 :   Bvariable* bv = this->backend()->implicit_variable(name, name, bat, flags,
    1033              :                                                      0);
    1034         1889 :   this->backend()->implicit_variable_set_init(bv, name, bat, flags, barray);
    1035              : 
    1036              :   // Build the call in main package's init function.
    1037         1889 :   Translate_context context(this, NULL, NULL, NULL);
    1038         1889 :   Bexpression* bexpr = this->backend()->var_expression(bv, builtin_loc);
    1039         1889 :   bexpr = this->backend()->address_expression(bexpr, builtin_loc);
    1040         1889 :   Type* array_ptr_type = Type::make_pointer_type(list_array_type);
    1041         1889 :   Expression* expr = Expression::make_backend(bexpr, array_ptr_type,
    1042              :                                               builtin_loc);
    1043         1889 :   expr = Runtime::make_call(this, Runtime::REGISTER_TYPE_DESCRIPTORS,
    1044              :                             builtin_loc, 2, len_expr->copy(), expr);
    1045         1889 :   Bexpression* bcall = expr->get_backend(&context);
    1046         1889 :   init_stmts.push_back(this->backend()->expression_statement(init_bfn,
    1047              :                                                              bcall));
    1048         1889 : }
    1049              : 
    1050              : // Build the decl for the initialization function.
    1051              : 
    1052              : Named_object*
    1053         3453 : Gogo::initialization_function_decl()
    1054              : {
    1055         3453 :   std::string name = this->get_init_fn_name();
    1056         3453 :   Location loc = this->package_->location();
    1057              : 
    1058         3453 :   Function_type* fntype = Type::make_function_type(NULL, NULL, NULL, loc);
    1059         3453 :   Function* initfn = new Function(fntype, NULL, NULL, loc);
    1060         3453 :   return Named_object::make_function(name, NULL, initfn);
    1061         3453 : }
    1062              : 
    1063              : // Create the magic initialization function.  CODE_STMT is the
    1064              : // code that it needs to run.
    1065              : 
    1066              : Named_object*
    1067         3449 : Gogo::create_initialization_function(Named_object* initfn,
    1068              :                                      Bstatement* code_stmt)
    1069              : {
    1070              :   // Make sure that we thought we needed an initialization function,
    1071              :   // as otherwise we will not have reported it in the export data.
    1072         3449 :   go_assert(this->is_main_package() || this->need_init_fn_);
    1073              : 
    1074         3449 :   if (initfn == NULL)
    1075          278 :     initfn = this->initialization_function_decl();
    1076              : 
    1077              :   // Bind the initialization function code to a block.
    1078         3449 :   Bfunction* fndecl = initfn->func_value()->get_or_make_decl(this, initfn);
    1079         3449 :   Location pkg_loc = this->package_->location();
    1080         3449 :   std::vector<Bvariable*> vars;
    1081         3449 :   this->backend()->block(fndecl, NULL, vars, pkg_loc, pkg_loc);
    1082              : 
    1083         3449 :   if (!this->backend()->function_set_body(fndecl, code_stmt))
    1084              :     {
    1085           41 :       go_assert(saw_errors());
    1086              :       return NULL;
    1087              :     }
    1088              :   return initfn;
    1089         3449 : }
    1090              : 
    1091              : // Given an expression, collect all the global variables defined in
    1092              : // this package that it references.
    1093              : 
    1094              : class Find_vars : public Traverse
    1095              : {
    1096              :  private:
    1097              :   // The list of variables we accumulate.
    1098              :   typedef Unordered_set(Named_object*) Vars;
    1099              : 
    1100              :   // A hash table we use to avoid looping.  The index is a
    1101              :   // Named_object* or a Temporary_statement*.  We only look through
    1102              :   // objects defined in this package.
    1103              :   typedef Unordered_set(const void*) Seen_objects;
    1104              : 
    1105              :  public:
    1106        55915 :   Find_vars()
    1107        55915 :     : Traverse(traverse_expressions | traverse_statements),
    1108        55915 :       vars_(), seen_objects_(), lhs_is_ref_(false)
    1109        55915 :   { }
    1110              : 
    1111              :   // An iterator through the variables found, after the traversal.
    1112              :   typedef Vars::const_iterator const_iterator;
    1113              : 
    1114              :   const_iterator
    1115        55915 :   begin() const
    1116        55915 :   { return this->vars_.begin(); }
    1117              : 
    1118              :   const_iterator
    1119        77447 :   end() const
    1120        77447 :   { return this->vars_.end(); }
    1121              : 
    1122              :   int
    1123              :   expression(Expression**);
    1124              : 
    1125              :   int
    1126              :   statement(Block*, size_t* index, Statement*);
    1127              : 
    1128              :  private:
    1129              :   // Accumulated variables.
    1130              :   Vars vars_;
    1131              :   // Objects we have already seen.
    1132              :   Seen_objects seen_objects_;
    1133              :   // Whether an assignment to a variable counts as a reference.
    1134              :   bool lhs_is_ref_;
    1135              : };
    1136              : 
    1137              : // Collect global variables referenced by EXPR.  Look through function
    1138              : // calls and variable initializations.
    1139              : 
    1140              : int
    1141      7219006 : Find_vars::expression(Expression** pexpr)
    1142              : {
    1143      7219006 :   Expression* e = *pexpr;
    1144              : 
    1145      7219006 :   Var_expression* ve = e->var_expression();
    1146       824867 :   if (ve != NULL)
    1147              :     {
    1148       824867 :       Named_object* v = ve->named_object();
    1149       824867 :       if (!v->is_variable() || v->package() != NULL)
    1150              :         {
    1151              :           // This is a result parameter or a variable defined in a
    1152              :           // different package.  Either way we don't care about it.
    1153       636338 :           return TRAVERSE_CONTINUE;
    1154              :         }
    1155              : 
    1156       734392 :       std::pair<Seen_objects::iterator, bool> ins =
    1157       734392 :         this->seen_objects_.insert(v);
    1158       734392 :       if (!ins.second)
    1159              :         {
    1160              :           // We've seen this variable before.
    1161              :           return TRAVERSE_CONTINUE;
    1162              :         }
    1163              : 
    1164       188529 :       if (v->var_value()->is_global())
    1165        21549 :         this->vars_.insert(v);
    1166              : 
    1167       188529 :       Expression* init = v->var_value()->init();
    1168       188529 :       if (init != NULL)
    1169              :         {
    1170        82447 :           if (Expression::traverse(&init, this) == TRAVERSE_EXIT)
    1171              :             return TRAVERSE_EXIT;
    1172              :         }
    1173              :     }
    1174              : 
    1175              :   // We traverse the code of any function or bound method we see.  Note that
    1176              :   // this means that we will traverse the code of a function or bound method
    1177              :   // whose address is taken even if it is not called.
    1178      6582668 :   Func_expression* fe = e->func_expression();
    1179      6582668 :   Bound_method_expression* bme = e->bound_method_expression();
    1180      6582668 :   if (fe != NULL || bme != NULL)
    1181              :     {
    1182       335257 :       const Named_object* f = fe != NULL ? fe->named_object() : bme->function();
    1183       335257 :       if (f->is_function() && f->package() == NULL)
    1184              :         {
    1185       129265 :           std::pair<Seen_objects::iterator, bool> ins =
    1186       129265 :             this->seen_objects_.insert(f);
    1187       129265 :           if (ins.second)
    1188              :             {
    1189              :               // This is the first time we have seen this name.
    1190        41342 :               bool hold = this->lhs_is_ref_;
    1191        41342 :               this->lhs_is_ref_ = true;
    1192        41342 :               int r = f->func_value()->block()->traverse(this);
    1193        41342 :               this->lhs_is_ref_ = hold;
    1194        41342 :               if (r == TRAVERSE_EXIT)
    1195            0 :                 return TRAVERSE_EXIT;
    1196              :             }
    1197              :         }
    1198              :     }
    1199              : 
    1200      6582668 :   Temporary_reference_expression* tre = e->temporary_reference_expression();
    1201       310565 :   if (tre != NULL)
    1202              :     {
    1203       310565 :       Temporary_statement* ts = tre->statement();
    1204       310565 :       Expression* init = ts->init();
    1205       310565 :       if (init != NULL)
    1206              :         {
    1207       260150 :           std::pair<Seen_objects::iterator, bool> ins =
    1208       260150 :             this->seen_objects_.insert(ts);
    1209       260150 :           if (ins.second)
    1210              :             {
    1211              :               // This is the first time we have seen this temporary
    1212              :               // statement.
    1213       167876 :               if (Expression::traverse(&init, this) == TRAVERSE_EXIT)
    1214            0 :                 return TRAVERSE_EXIT;
    1215              :             }
    1216              :         }
    1217              :     }
    1218              : 
    1219              :   return TRAVERSE_CONTINUE;
    1220              : }
    1221              : 
    1222              : // Check a statement while searching for variables.  This is where we
    1223              : // skip variables on the left hand side of assigments if appropriate.
    1224              : 
    1225              : int
    1226      1047161 : Find_vars::statement(Block*, size_t*, Statement* s)
    1227              : {
    1228      1047161 :   if (this->lhs_is_ref_)
    1229              :     return TRAVERSE_CONTINUE;
    1230         9126 :   Assignment_statement* as = s->assignment_statement();
    1231           28 :   if (as == NULL)
    1232              :     return TRAVERSE_CONTINUE;
    1233              : 
    1234              :   // Only traverse subexpressions of the LHS.
    1235           28 :   if (as->lhs()->traverse_subexpressions(this) == TRAVERSE_EXIT)
    1236              :     return TRAVERSE_EXIT;
    1237              : 
    1238           28 :   Expression* rhs = as->rhs();
    1239           28 :   if (Expression::traverse(&rhs, this) == TRAVERSE_EXIT)
    1240              :     return TRAVERSE_EXIT;
    1241              : 
    1242              :   return TRAVERSE_SKIP_COMPONENTS;
    1243              : }
    1244              : 
    1245              : // Return true if EXPR, PREINIT, or DEP refers to VAR.
    1246              : 
    1247              : static bool
    1248        19214 : expression_requires(Expression* expr, Block* preinit, Named_object* dep,
    1249              :                     Named_object* var)
    1250              : {
    1251        19214 :   Find_vars find_vars;
    1252        19214 :   if (expr != NULL)
    1253        19214 :     Expression::traverse(&expr, &find_vars);
    1254        19214 :   if (preinit != NULL)
    1255          909 :     preinit->traverse(&find_vars);
    1256        19214 :   if (dep != NULL)
    1257              :     {
    1258            0 :       Expression* init = dep->var_value()->init();
    1259            0 :       if (init != NULL)
    1260            0 :         Expression::traverse(&init, &find_vars);
    1261            0 :       if (dep->var_value()->has_pre_init())
    1262            0 :         dep->var_value()->preinit()->traverse(&find_vars);
    1263              :     }
    1264              : 
    1265        29096 :   for (Find_vars::const_iterator p = find_vars.begin();
    1266        29096 :        p != find_vars.end();
    1267         9882 :        ++p)
    1268              :     {
    1269         9899 :       if (*p == var)
    1270        19214 :         return true;
    1271              :     }
    1272              :   return false;
    1273        19214 : }
    1274              : 
    1275              : // Sort variable initializations.  If the initialization expression
    1276              : // for variable A refers directly or indirectly to the initialization
    1277              : // expression for variable B, then we must initialize B before A.
    1278              : 
    1279              : class Var_init
    1280              : {
    1281              :  public:
    1282              :   Var_init()
    1283              :     : var_(NULL), init_(NULL), dep_count_(0)
    1284              :   { }
    1285              : 
    1286        13014 :   Var_init(Named_object* var, Bstatement* init)
    1287        13014 :     : var_(var), init_(init), dep_count_(0)
    1288              :   { }
    1289              : 
    1290              :   // Return the variable.
    1291              :   Named_object*
    1292       121648 :   var() const
    1293       121648 :   { return this->var_; }
    1294              : 
    1295              :   // Return the initialization expression.
    1296              :   Bstatement*
    1297        13014 :   init() const
    1298        13014 :   { return this->init_; }
    1299              : 
    1300              :   // Return the number of remaining dependencies.
    1301              :   size_t
    1302        17340 :   dep_count() const
    1303        17340 :   { return this->dep_count_; }
    1304              : 
    1305              :   // Increment the number of dependencies.
    1306              :   void
    1307         7044 :   add_dependency()
    1308         7044 :   { ++this->dep_count_; }
    1309              : 
    1310              :   // Decrement the number of dependencies.
    1311              :   void
    1312         7040 :   remove_dependency()
    1313         7040 :   { --this->dep_count_; }
    1314              : 
    1315              :  private:
    1316              :   // The variable being initialized.
    1317              :   Named_object* var_;
    1318              :   // The backend initialization statement.
    1319              :   Bstatement* init_;
    1320              :   // The number of initializations this is dependent on.  A variable
    1321              :   // initialization should not be emitted if any of its dependencies
    1322              :   // have not yet been resolved.
    1323              :   size_t dep_count_;
    1324              : };
    1325              : 
    1326              : // For comparing Var_init keys in a map.
    1327              : 
    1328              : inline bool
    1329        88576 : operator<(const Var_init& v1, const Var_init& v2)
    1330        88576 : { return v1.var()->name() < v2.var()->name(); }
    1331              : 
    1332              : typedef std::list<Var_init> Var_inits;
    1333              : 
    1334              : // Sort the variable initializations.  The rule we follow is that we
    1335              : // emit them in the order they appear in the array, except that if the
    1336              : // initialization expression for a variable V1 depends upon another
    1337              : // variable V2 then we initialize V1 after V2.
    1338              : 
    1339              : static void
    1340         1857 : sort_var_inits(Var_inits* var_inits)
    1341              : {
    1342         1857 :   if (var_inits->empty())
    1343            0 :     return;
    1344              : 
    1345         1857 :   std::map<Named_object*, Var_init*> var_to_init;
    1346              : 
    1347              :   // A mapping from a variable initialization to a set of
    1348              :   // variable initializations that depend on it.
    1349         1857 :   typedef std::map<Var_init, std::set<Var_init*> > Init_deps;
    1350         1857 :   Init_deps init_deps;
    1351         1857 :   bool init_loop = false;
    1352              : 
    1353              :   // Map from variable to Var_init.
    1354         1857 :   for (Var_inits::iterator pvar = var_inits->begin();
    1355        14871 :        pvar != var_inits->end();
    1356        13014 :        ++pvar)
    1357              :     {
    1358        13014 :       Named_object* var = pvar->var();
    1359        13014 :       var_to_init[var] = &*pvar;
    1360              :     }
    1361              : 
    1362              :   // Add dependencies to init_deps, and check for cycles.
    1363        14871 :   for (Var_inits::iterator pvar = var_inits->begin();
    1364        14871 :        pvar != var_inits->end();
    1365        13014 :        ++pvar)
    1366              :     {
    1367        13014 :       Named_object* var = pvar->var();
    1368              : 
    1369        13014 :       const std::vector<Named_object*>* refs =
    1370        13014 :         pvar->var()->var_value()->init_refs();
    1371        13014 :       if (refs == NULL)
    1372         9502 :         continue;
    1373        14985 :       for (std::vector<Named_object*>::const_iterator pdep = refs->begin();
    1374        14985 :            pdep != refs->end();
    1375        11473 :            ++pdep)
    1376              :         {
    1377        11473 :           Named_object* dep = *pdep;
    1378        11473 :           if (var == dep)
    1379              :             {
    1380              :               // This is a reference from a variable to itself.
    1381           33 :               go_error_at(var->location(),
    1382              :                           ("initialization expression for %qs "
    1383              :                            "depends upon itself"),
    1384           33 :                           var->message_name().c_str());
    1385         9584 :               continue;
    1386              :             }
    1387              : 
    1388        11440 :           Var_init* dep_init = var_to_init[dep];
    1389        11440 :           if (dep_init == NULL)
    1390              :             {
    1391              :               // This is a dependency on some variable that doesn't
    1392              :               // have an initializer, so for purposes of
    1393              :               // initialization ordering this is irrelevant.
    1394         4396 :               continue;
    1395              :             }
    1396              : 
    1397         7044 :           init_deps[*dep_init].insert(&(*pvar));
    1398         7044 :           pvar->add_dependency();
    1399              : 
    1400              :           // Check for cycles.
    1401         7044 :           const std::vector<Named_object*>* deprefs =
    1402         7044 :             dep_init->var()->var_value()->init_refs();
    1403         7044 :           if (deprefs == NULL)
    1404         5155 :             continue;
    1405         5108 :           for (std::vector<Named_object*>::const_iterator pdepdep =
    1406         1889 :                  deprefs->begin();
    1407         5108 :                pdepdep != deprefs->end();
    1408         3219 :                ++pdepdep)
    1409              :             {
    1410         3221 :               if (*pdepdep == var)
    1411              :                 {
    1412            2 :                   go_error_at(var->location(),
    1413              :                               ("initialization expressions for %qs and "
    1414              :                                "%qs depend upon each other"),
    1415            4 :                               var->message_name().c_str(),
    1416            2 :                               dep->message_name().c_str());
    1417            2 :                   go_inform(dep->location(), "%qs defined here",
    1418            2 :                             dep->message_name().c_str());
    1419            2 :                   init_loop = true;
    1420            2 :                   break;
    1421              :                 }
    1422              :             }
    1423              :         }
    1424              :     }
    1425              : 
    1426         1857 :   var_to_init.clear();
    1427              : 
    1428              :   // If there are no dependencies then the declaration order is sorted.
    1429         1857 :   if (!init_deps.empty() && !init_loop)
    1430              :     {
    1431              :       // Otherwise, sort variable initializations by emitting all variables with
    1432              :       // no dependencies in declaration order. VAR_INITS is already in
    1433              :       // declaration order.
    1434          329 :       Var_inits ready;
    1435         8713 :       while (!var_inits->empty())
    1436              :         {
    1437              :           Var_inits::iterator v1;;
    1438        17340 :           for (v1 = var_inits->begin(); v1 != var_inits->end(); ++v1)
    1439              :             {
    1440        17340 :               if (v1->dep_count() == 0)
    1441              :                 break;
    1442              :             }
    1443         8055 :           go_assert(v1 != var_inits->end());
    1444              : 
    1445              :           // V1 either has no dependencies or its dependencies have already
    1446              :           // been emitted, add it to READY next.  When V1 is emitted, remove
    1447              :           // a dependency from each V that depends on V1.
    1448         8055 :           ready.splice(ready.end(), *var_inits, v1);
    1449              : 
    1450         8055 :           Init_deps::iterator p1 = init_deps.find(*v1);
    1451         8055 :           if (p1 != init_deps.end())
    1452              :             {
    1453         3945 :               std::set<Var_init*> resolved = p1->second;
    1454        10985 :               for (std::set<Var_init*>::iterator pv = resolved.begin();
    1455        10985 :                    pv != resolved.end();
    1456         7040 :                    ++pv)
    1457         7040 :                 (*pv)->remove_dependency();
    1458         3945 :               init_deps.erase(p1);
    1459         3945 :             }
    1460              :         }
    1461          329 :       var_inits->swap(ready);
    1462          329 :       go_assert(init_deps.empty());
    1463          329 :     }
    1464         1857 : }
    1465              : 
    1466              : // Give an error if the initialization expression for VAR depends on
    1467              : // itself.  We only check if INIT is not NULL and there is no
    1468              : // dependency; when INIT is NULL, it means that PREINIT sets VAR,
    1469              : // which we will interpret as a loop.
    1470              : 
    1471              : void
    1472        11992 : Gogo::check_self_dep(Named_object* var)
    1473              : {
    1474        11992 :   Expression* init = var->var_value()->init();
    1475        11992 :   Block* preinit = var->var_value()->preinit();
    1476        11992 :   Named_object* dep = this->var_depends_on(var->var_value());
    1477        11992 :   if (init != NULL
    1478        11992 :       && dep == NULL
    1479        11992 :       && expression_requires(init, preinit, NULL, var))
    1480           17 :     go_error_at(var->location(),
    1481              :                 "initialization expression for %qs depends upon itself",
    1482           34 :                 var->message_name().c_str());
    1483        11992 : }
    1484              : 
    1485              : // Write out the global definitions.
    1486              : 
    1487              : void
    1488         4646 : Gogo::write_globals()
    1489              : {
    1490         4646 :   this->build_interface_method_tables();
    1491              : 
    1492         4646 :   Bindings* bindings = this->current_bindings();
    1493              : 
    1494       450028 :   for (Bindings::const_declarations_iterator p = bindings->begin_declarations();
    1495       450028 :        p != bindings->end_declarations();
    1496       445382 :        ++p)
    1497              :     {
    1498              :       // If any function declarations needed a descriptor, make sure
    1499              :       // we build it.
    1500       445382 :       Named_object* no = p->second;
    1501       445382 :       if (no->is_function_declaration())
    1502         3904 :         no->func_declaration_value()->build_backend_descriptor(this);
    1503              :     }
    1504              : 
    1505              :   // Lists of globally declared types, variables, constants, and functions
    1506              :   // that must be defined.
    1507         4646 :   std::vector<Btype*> type_decls;
    1508         4646 :   std::vector<Bvariable*> var_decls;
    1509         4646 :   std::vector<Bexpression*> const_decls;
    1510         4646 :   std::vector<Bfunction*> func_decls;
    1511              : 
    1512              :   // The init function declaration and associated Bfunction, if necessary.
    1513         4646 :   Named_object* init_fndecl = NULL;
    1514         4646 :   Bfunction* init_bfn = NULL;
    1515              : 
    1516         4646 :   std::vector<Bstatement*> init_stmts;
    1517         4646 :   std::vector<Bstatement*> var_init_stmts;
    1518              : 
    1519         4646 :   if (this->is_main_package())
    1520              :     {
    1521         1889 :       init_fndecl = this->initialization_function_decl();
    1522         1889 :       init_bfn = init_fndecl->func_value()->get_or_make_decl(this, init_fndecl);
    1523              :     }
    1524              : 
    1525              :   // A list of variable initializations.
    1526         4646 :   Var_inits var_inits;
    1527              : 
    1528              :   // A list of variables which need to be registered with the garbage
    1529              :   // collector.
    1530         4646 :   size_t count_definitions = bindings->size_definitions();
    1531         4646 :   std::vector<Named_object*> var_gc;
    1532         4646 :   var_gc.reserve(count_definitions);
    1533              : 
    1534       565904 :   for (Bindings::const_definitions_iterator p = bindings->begin_definitions();
    1535       565904 :        p != bindings->end_definitions();
    1536       561258 :        ++p)
    1537              :     {
    1538       561258 :       Named_object* no = *p;
    1539       561258 :       go_assert(!no->is_type_declaration() && !no->is_function_declaration());
    1540              : 
    1541              :       // There is nothing to do for a package.
    1542       561258 :       if (no->is_package())
    1543       198487 :         continue;
    1544              : 
    1545              :       // There is nothing to do for an object which was imported from
    1546              :       // a different package into the global scope.
    1547       521843 :       if (no->package() != NULL)
    1548        32785 :         continue;
    1549              : 
    1550              :       // Skip blank named functions and constants.
    1551       283565 :       if ((no->is_function() && no->func_value()->is_sink())
    1552       772323 :           || (no->is_const() && no->const_value()->is_sink()))
    1553         1477 :         continue;
    1554              : 
    1555              :       // Skip global sink variables with static initializers.  With
    1556              :       // non-static initializers we have to evaluate for side effects,
    1557              :       // and we wind up initializing a dummy variable.  That is not
    1558              :       // ideal but it works and it's a rare case.
    1559       487653 :       if (no->is_variable()
    1560        25471 :           && no->var_value()->is_global_sink()
    1561         1114 :           && !no->var_value()->has_pre_init()
    1562       488010 :           && (no->var_value()->init() == NULL
    1563          417 :               || no->var_value()->init()->is_static_initializer()))
    1564           72 :         continue;
    1565              : 
    1566              :       // There is nothing useful we can output for constants which
    1567              :       // have ideal or non-integral type.
    1568       487509 :       if (no->is_const())
    1569              :         {
    1570       148137 :           Type* type = no->const_value()->type();
    1571       148137 :           if (type == NULL)
    1572            0 :             type = no->const_value()->expr()->type();
    1573       171745 :           if (type->is_abstract() || !type->is_numeric_type())
    1574       124738 :             continue;
    1575              :         }
    1576              : 
    1577       362771 :       if (!no->is_variable())
    1578       337372 :         no->get_backend(this, const_decls, type_decls, func_decls);
    1579              :       else
    1580              :         {
    1581        25399 :           Variable* var = no->var_value();
    1582        25399 :           Bvariable* bvar = no->get_backend_variable(this, NULL);
    1583        25399 :           var_decls.push_back(bvar);
    1584              : 
    1585              :           // Check for a sink variable, which may be used to run an
    1586              :           // initializer purely for its side effects.
    1587        25399 :           bool is_sink = no->name()[0] == '_' && no->name()[1] == '.';
    1588              : 
    1589        25399 :           Bstatement* var_init_stmt = NULL;
    1590        25399 :           if (!var->has_pre_init())
    1591              :             {
    1592              :               // If the backend representation of the variable initializer is
    1593              :               // constant, we can just set the initial value using
    1594              :               // global_var_set_init instead of during the init() function.
    1595              :               // The initializer is constant if it is the zero-value of the
    1596              :               // variable's type or if the initial value is an immutable value
    1597              :               // that is not copied to the heap.
    1598        13191 :               bool is_static_initializer = false;
    1599        13191 :               if (var->init() == NULL)
    1600              :                 is_static_initializer = true;
    1601              :               else
    1602              :                 {
    1603         8092 :                   Type* var_type = var->type();
    1604         8092 :                   Expression* init = var->init();
    1605         8092 :                   Expression* init_cast =
    1606         8092 :                       Expression::make_cast(var_type, init, var->location());
    1607         8092 :                   is_static_initializer = init_cast->is_static_initializer();
    1608              :                 }
    1609              : 
    1610              :               // Non-constant variable initializations might need to create
    1611              :               // temporary variables, which will need the initialization
    1612              :               // function as context.
    1613         8092 :               Named_object* var_init_fn;
    1614         8092 :               if (is_static_initializer)
    1615              :                 var_init_fn = NULL;
    1616              :               else
    1617              :                 {
    1618          802 :                   if (init_fndecl == NULL)
    1619              :                     {
    1620           51 :                       init_fndecl = this->initialization_function_decl();
    1621           51 :                       Function* func = init_fndecl->func_value();
    1622           51 :                       init_bfn = func->get_or_make_decl(this, init_fndecl);
    1623              :                     }
    1624              :                   var_init_fn = init_fndecl;
    1625              :                 }
    1626        13191 :               Bexpression* var_binit = var->get_init(this, var_init_fn);
    1627              : 
    1628        13191 :               if (var_binit == NULL)
    1629              :                 ;
    1630         8092 :               else if (is_static_initializer)
    1631              :                 {
    1632         7290 :                   if (expression_requires(var->init(), NULL,
    1633              :                                           this->var_depends_on(var), no))
    1634            0 :                     go_error_at(no->location(),
    1635              :                                 "initialization expression for %qs depends "
    1636              :                                 "upon itself",
    1637            0 :                                 no->message_name().c_str());
    1638         7290 :                   this->backend()->global_variable_set_init(bvar, var_binit);
    1639              :                 }
    1640          802 :               else if (is_sink)
    1641          357 :                 var_init_stmt =
    1642          357 :                     this->backend()->expression_statement(init_bfn, var_binit);
    1643              :               else
    1644              :                 {
    1645          445 :                   Location loc = var->location();
    1646          445 :                   Bexpression* var_expr =
    1647          445 :                       this->backend()->var_expression(bvar, loc);
    1648          445 :                   var_init_stmt =
    1649          445 :                       this->backend()->assignment_statement(init_bfn, var_expr,
    1650              :                                                             var_binit, loc);
    1651              :                 }
    1652              :             }
    1653              :           else
    1654              :             {
    1655              :               // We are going to create temporary variables which
    1656              :               // means that we need an fndecl.
    1657        12208 :               if (init_fndecl == NULL)
    1658         1235 :                 init_fndecl = this->initialization_function_decl();
    1659              : 
    1660        12208 :               Bvariable* var_decl = is_sink ? NULL : bvar;
    1661        12208 :               var_init_stmt = var->get_init_block(this, init_fndecl, var_decl);
    1662              :             }
    1663              : 
    1664        25399 :           if (var_init_stmt != NULL)
    1665              :             {
    1666        13010 :               if (var->init() == NULL && !var->has_pre_init())
    1667            0 :                 var_init_stmts.push_back(var_init_stmt);
    1668              :               else
    1669        13010 :                 var_inits.push_back(Var_init(no, var_init_stmt));
    1670              :             }
    1671        12389 :           else if (this->var_depends_on(var) != NULL)
    1672              :             {
    1673              :               // This variable is initialized from something that is
    1674              :               // not in its init or preinit.  This variable needs to
    1675              :               // participate in dependency analysis sorting, in case
    1676              :               // some other variable depends on this one.
    1677            4 :               Btype* btype = no->var_value()->type()->get_backend(this);
    1678            4 :               Bexpression* zero = this->backend()->zero_expression(btype);
    1679            4 :               Bstatement* zero_stmt =
    1680            4 :                   this->backend()->expression_statement(init_bfn, zero);
    1681            4 :               var_inits.push_back(Var_init(no, zero_stmt));
    1682              :             }
    1683              : 
    1684              :           // Collect a list of all global variables with pointers,
    1685              :           // to register them for the garbage collector.
    1686        25399 :           if (!is_sink && var->type()->has_pointer())
    1687              :             {
    1688              :               // Avoid putting runtime.gcRoots itself on the list.
    1689        38792 :               if (this->compiling_runtime()
    1690          832 :                   && this->package_name() == "runtime"
    1691        39435 :                   && (Gogo::unpack_hidden_name(no->name()) == "gcRoots"
    1692        19396 :                    || Gogo::unpack_hidden_name(no->name()) == "gcRootsIndex"))
    1693              :                 ;
    1694              :               else
    1695        19389 :                 var_gc.push_back(no);
    1696              :             }
    1697              :         }
    1698              :     }
    1699              : 
    1700              :   // Output inline functions, which are in different packages.
    1701        12247 :   for (std::vector<Named_object*>::const_iterator p =
    1702         4646 :          this->imported_inline_functions_.begin();
    1703        16893 :        p != this->imported_inline_functions_.end();
    1704        12247 :        ++p)
    1705        12247 :     (*p)->get_backend(this, const_decls, type_decls, func_decls);
    1706              : 
    1707              :   // Build the list of type descriptors.
    1708         4646 :   this->build_type_descriptor_list();
    1709              : 
    1710         4646 :   if (this->is_main_package())
    1711              :     {
    1712              :       // Register the type descriptor lists, so that at run time
    1713              :       // the reflect package can find compiler-created types, and
    1714              :       // deduplicate if the same type is created with reflection.
    1715              :       // This needs to be done before calling any package's init
    1716              :       // function, as it may create type through reflection.
    1717         1889 :       this->register_type_descriptors(init_stmts, init_bfn);
    1718              : 
    1719              :       // Initialize imported packages.
    1720         1889 :       this->init_imports(init_stmts, init_bfn);
    1721              :     }
    1722              : 
    1723              :   // Register global variables with the garbage collector.
    1724         4646 :   this->register_gc_vars(var_gc, init_stmts, init_bfn);
    1725              : 
    1726              :   // Simple variable initializations, after all variables are
    1727              :   // registered.
    1728         4646 :   init_stmts.push_back(this->backend()->statement_list(var_init_stmts));
    1729              : 
    1730              :   // Complete variable initializations, first sorting them into a
    1731              :   // workable order.
    1732         4646 :   if (!var_inits.empty())
    1733              :     {
    1734         1857 :       sort_var_inits(&var_inits);
    1735         1857 :       for (Var_inits::const_iterator p = var_inits.begin();
    1736        14871 :            p != var_inits.end();
    1737        13014 :            ++p)
    1738        13014 :         init_stmts.push_back(p->init());
    1739              :     }
    1740              : 
    1741              :   // After all the variables are initialized, call the init
    1742              :   // functions if there are any.  Init functions take no arguments, so
    1743              :   // we pass in EMPTY_ARGS to call them.
    1744         4646 :   std::vector<Bexpression*> empty_args;
    1745         1884 :   for (std::vector<Named_object*>::const_iterator p =
    1746         4646 :            this->init_functions_.begin();
    1747         6530 :        p != this->init_functions_.end();
    1748         1884 :        ++p)
    1749              :     {
    1750         1884 :       Location func_loc = (*p)->location();
    1751         1884 :       Function* func = (*p)->func_value();
    1752         1884 :       Bfunction* initfn = func->get_or_make_decl(this, *p);
    1753         1884 :       Bexpression* func_code =
    1754         1884 :           this->backend()->function_code_expression(initfn, func_loc);
    1755         1884 :       Bexpression* call = this->backend()->call_expression(init_bfn, func_code,
    1756              :                                                            empty_args,
    1757              :                                                            NULL, func_loc);
    1758         1884 :       Bstatement* ist = this->backend()->expression_statement(init_bfn, call);
    1759         1884 :       init_stmts.push_back(ist);
    1760              :     }
    1761              : 
    1762              :   // Set up a magic function to do all the initialization actions.
    1763              :   // This will be called if this package is imported.
    1764         4646 :   Bstatement* init_fncode = this->backend()->statement_list(init_stmts);
    1765         4646 :   if (this->need_init_fn_ || this->is_main_package())
    1766              :     {
    1767         3449 :       init_fndecl =
    1768         3449 :         this->create_initialization_function(init_fndecl, init_fncode);
    1769         3449 :       if (init_fndecl != NULL)
    1770         3408 :         func_decls.push_back(init_fndecl->func_value()->get_decl());
    1771              :     }
    1772              : 
    1773              :   // We should not have seen any new bindings created during the conversion.
    1774         4646 :   go_assert(count_definitions == this->current_bindings()->size_definitions());
    1775              : 
    1776              :   // Define all globally declared values.
    1777         4646 :   if (!saw_errors())
    1778         4217 :     this->backend()->write_global_definitions(type_decls, const_decls,
    1779              :                                               func_decls, var_decls);
    1780         4646 : }
    1781              : 
    1782              : // Return the current block.
    1783              : 
    1784              : Block*
    1785       927284 : Gogo::current_block()
    1786              : {
    1787       927284 :   if (this->functions_.empty())
    1788              :     return NULL;
    1789              :   else
    1790       927283 :     return this->functions_.back().blocks.back();
    1791              : }
    1792              : 
    1793              : // Look up a name in the current binding contour.  If PFUNCTION is not
    1794              : // NULL, set it to the function in which the name is defined, or NULL
    1795              : // if the name is defined in global scope.
    1796              : 
    1797              : Named_object*
    1798      4072605 : Gogo::lookup(const std::string& name, Named_object** pfunction) const
    1799              : {
    1800      4072605 :   if (pfunction != NULL)
    1801      3214520 :     *pfunction = NULL;
    1802              : 
    1803      4072605 :   if (Gogo::is_sink_name(name))
    1804         8421 :     return Named_object::make_sink();
    1805              : 
    1806      5432137 :   for (Open_functions::const_reverse_iterator p = this->functions_.rbegin();
    1807      5432137 :        p != this->functions_.rend();
    1808      1367953 :        ++p)
    1809              :     {
    1810      3485807 :       Named_object* ret = p->blocks.back()->bindings()->lookup(name);
    1811      3485807 :       if (ret != NULL)
    1812              :         {
    1813      2117854 :           if (pfunction != NULL)
    1814      1860217 :             *pfunction = p->function;
    1815      2117854 :           return ret;
    1816              :         }
    1817              :     }
    1818              : 
    1819      1946330 :   if (this->package_ != NULL)
    1820              :     {
    1821      1946330 :       Named_object* ret = this->package_->bindings()->lookup(name);
    1822      1946330 :       if (ret != NULL)
    1823              :         {
    1824      1745186 :           if (ret->package() != NULL)
    1825              :             {
    1826        26027 :               std::string dot_alias = "." + ret->package()->package_name();
    1827        26027 :               ret->package()->note_usage(dot_alias);
    1828        26027 :             }
    1829      1745186 :           return ret;
    1830              :         }
    1831              :     }
    1832              : 
    1833              :   // We do not look in the global namespace.  If we did, the global
    1834              :   // namespace would effectively hide names which were defined in
    1835              :   // package scope which we have not yet seen.  Instead,
    1836              :   // define_global_names is called after parsing is over to connect
    1837              :   // undefined names at package scope with names defined at global
    1838              :   // scope.
    1839              : 
    1840              :   return NULL;
    1841              : }
    1842              : 
    1843              : // Look up a name in the current block, without searching enclosing
    1844              : // blocks.
    1845              : 
    1846              : Named_object*
    1847       306705 : Gogo::lookup_in_block(const std::string& name) const
    1848              : {
    1849       306705 :   go_assert(!this->functions_.empty());
    1850       306705 :   go_assert(!this->functions_.back().blocks.empty());
    1851       306705 :   return this->functions_.back().blocks.back()->bindings()->lookup_local(name);
    1852              : }
    1853              : 
    1854              : // Look up a name in the global namespace.
    1855              : 
    1856              : Named_object*
    1857       639622 : Gogo::lookup_global(const char* name) const
    1858              : {
    1859       639622 :   return this->globals_->lookup(name);
    1860              : }
    1861              : 
    1862              : // Add an imported package.
    1863              : 
    1864              : Package*
    1865        25881 : Gogo::add_imported_package(const std::string& real_name,
    1866              :                            const std::string& alias_arg,
    1867              :                            bool is_alias_exported,
    1868              :                            const std::string& pkgpath,
    1869              :                            const std::string& pkgpath_symbol,
    1870              :                            Location location,
    1871              :                            bool* padd_to_globals)
    1872              : {
    1873        25881 :   Package* ret = this->register_package(pkgpath, pkgpath_symbol, location);
    1874        25881 :   ret->set_package_name(real_name, location);
    1875              : 
    1876        25881 :   *padd_to_globals = false;
    1877              : 
    1878        25881 :   if (alias_arg == "_")
    1879              :     ;
    1880        20914 :   else if (alias_arg == ".")
    1881              :     {
    1882           10 :       *padd_to_globals = true;
    1883           10 :       std::string dot_alias = "." + real_name;
    1884           10 :       ret->add_alias(dot_alias, location);
    1885           10 :     }
    1886              :   else
    1887              :     {
    1888        20904 :       std::string alias = alias_arg;
    1889        20904 :       if (alias.empty())
    1890              :         {
    1891        20308 :           alias = real_name;
    1892        20308 :           is_alias_exported = Lex::is_exported_name(alias);
    1893              :         }
    1894        20904 :       ret->add_alias(alias, location);
    1895        20904 :       alias = this->pack_hidden_name(alias, is_alias_exported);
    1896        20904 :       Named_object* no = this->package_->bindings()->add_package(alias, ret);
    1897        20904 :       if (!no->is_package())
    1898            0 :         return NULL;
    1899        20904 :     }
    1900              : 
    1901              :   return ret;
    1902              : }
    1903              : 
    1904              : // Register a package.  This package may or may not be imported.  This
    1905              : // returns the Package structure for the package, creating if it
    1906              : // necessary.  LOCATION is the location of the import statement that
    1907              : // led us to see this package.  PKGPATH_SYMBOL is the symbol to use
    1908              : // for names in the package; it may be the empty string, in which case
    1909              : // we either get it later or make a guess when we need it.
    1910              : 
    1911              : Package*
    1912       734293 : Gogo::register_package(const std::string& pkgpath,
    1913              :                        const std::string& pkgpath_symbol, Location location)
    1914              : {
    1915       734293 :   Package* package = NULL;
    1916       734293 :   std::pair<Packages::iterator, bool> ins =
    1917      1468586 :     this->packages_.insert(std::make_pair(pkgpath, package));
    1918       734293 :   if (!ins.second)
    1919              :     {
    1920              :       // We have seen this package name before.
    1921       617651 :       package = ins.first->second;
    1922       617651 :       go_assert(package != NULL && package->pkgpath() == pkgpath);
    1923       617651 :       if (!pkgpath_symbol.empty())
    1924        14338 :         package->set_pkgpath_symbol(pkgpath_symbol);
    1925       617651 :       if (Linemap::is_unknown_location(package->location()))
    1926       318737 :         package->set_location(location);
    1927              :     }
    1928              :   else
    1929              :     {
    1930              :       // First time we have seen this package name.
    1931       116642 :       package = new Package(pkgpath, pkgpath_symbol, location);
    1932       116642 :       go_assert(ins.first->second == NULL);
    1933       116642 :       ins.first->second = package;
    1934              :     }
    1935              : 
    1936       734293 :   return package;
    1937              : }
    1938              : 
    1939              : // Return the pkgpath symbol for a package, given the pkgpath.
    1940              : 
    1941              : std::string
    1942            0 : Gogo::pkgpath_symbol_for_package(const std::string& pkgpath)
    1943              : {
    1944            0 :   Packages::iterator p = this->packages_.find(pkgpath);
    1945            0 :   go_assert(p != this->packages_.end());
    1946            0 :   return p->second->pkgpath_symbol();
    1947              : }
    1948              : 
    1949              : // Start compiling a function.
    1950              : 
    1951              : Named_object*
    1952       283570 : Gogo::start_function(const std::string& name, Function_type* type,
    1953              :                      bool add_method_to_type, Location location)
    1954              : {
    1955       283570 :   bool at_top_level = this->functions_.empty();
    1956              : 
    1957       283570 :   Block* block = new Block(NULL, location);
    1958              : 
    1959       283570 :   Named_object* enclosing = (at_top_level
    1960       283570 :                          ? NULL
    1961        24308 :                          : this->functions_.back().function);
    1962              : 
    1963       283570 :   Function* function = new Function(type, enclosing, block, location);
    1964              : 
    1965       283570 :   if (type->is_method())
    1966              :     {
    1967        77457 :       const Typed_identifier* receiver = type->receiver();
    1968        77457 :       Variable* this_param = new Variable(receiver->type(), NULL, false,
    1969        77457 :                                           true, true, location);
    1970        77457 :       std::string rname = receiver->name();
    1971        77457 :       unsigned rcounter = 0;
    1972              : 
    1973              :       // We need to give a nameless receiver parameter a synthesized name to
    1974              :       // avoid having it clash with some other nameless param. FIXME.
    1975        77457 :       Gogo::rename_if_empty(&rname, "r", &rcounter);
    1976              : 
    1977        77457 :       block->bindings()->add_variable(rname, NULL, this_param);
    1978        77457 :     }
    1979              : 
    1980       283570 :   const Typed_identifier_list* parameters = type->parameters();
    1981       283570 :   bool is_varargs = type->is_varargs();
    1982       283570 :   unsigned pcounter = 0;
    1983       283570 :   if (parameters != NULL)
    1984              :     {
    1985       218169 :       for (Typed_identifier_list::const_iterator p = parameters->begin();
    1986       597335 :            p != parameters->end();
    1987       379166 :            ++p)
    1988              :         {
    1989       379166 :           Variable* param = new Variable(p->type(), NULL, false, true, false,
    1990       379166 :                                          p->location());
    1991       379166 :           if (is_varargs && p + 1 == parameters->end())
    1992         2018 :             param->set_is_varargs_parameter();
    1993              : 
    1994       379166 :           std::string pname = p->name();
    1995              : 
    1996              :           // We need to give each nameless parameter a non-empty name to avoid
    1997              :           // having it clash with some other nameless param. FIXME.
    1998       379166 :           Gogo::rename_if_empty(&pname, "p", &pcounter);
    1999              : 
    2000       379166 :           block->bindings()->add_variable(pname, NULL, param);
    2001       379166 :         }
    2002              :     }
    2003              : 
    2004       283570 :   function->create_result_variables(this);
    2005              : 
    2006       283570 :   const std::string* pname;
    2007       283570 :   std::string nested_name;
    2008       283570 :   bool is_init = false;
    2009       567140 :   if (Gogo::unpack_hidden_name(name) == "init" && !type->is_method())
    2010              :     {
    2011         1884 :       if ((type->parameters() != NULL && !type->parameters()->empty())
    2012         1884 :           || (type->results() != NULL && !type->results()->empty()))
    2013            0 :         go_error_at(location,
    2014              :                     "func init must have no arguments and no return values");
    2015              :       // There can be multiple "init" functions, so give them each a
    2016              :       // different name.
    2017         1884 :       nested_name = this->init_function_name();
    2018         1884 :       pname = &nested_name;
    2019         1884 :       is_init = true;
    2020              :     }
    2021       281686 :   else if (!name.empty())
    2022              :     pname = &name;
    2023              :   else
    2024              :     {
    2025              :       // Invent a name for a nested function.
    2026        25609 :       nested_name = this->nested_function_name(enclosing);
    2027        25609 :       pname = &nested_name;
    2028              :     }
    2029              : 
    2030       283570 :   Named_object* ret;
    2031       283570 :   if (Gogo::is_sink_name(*pname))
    2032              :     {
    2033          305 :       std::string sname(this->sink_function_name());
    2034          305 :       ret = Named_object::make_function(sname, NULL, function);
    2035          305 :       ret->func_value()->set_is_sink();
    2036              : 
    2037          305 :       if (!type->is_method())
    2038          300 :         ret = this->package_->bindings()->add_named_object(ret);
    2039            5 :       else if (add_method_to_type)
    2040              :         {
    2041              :           // We should report errors even for sink methods.
    2042            5 :           Type* rtype = type->receiver()->type();
    2043              :           // Avoid points_to and deref to avoid getting an error if
    2044              :           // the type is not yet defined.
    2045            5 :           if (rtype->classification() == Type::TYPE_POINTER)
    2046            2 :             rtype = rtype->points_to();
    2047            5 :           while (rtype->named_type() != NULL
    2048            5 :                  && rtype->named_type()->is_alias())
    2049            0 :             rtype = rtype->named_type()->real_type()->forwarded();
    2050            5 :           if (rtype->is_error_type())
    2051              :             ;
    2052            5 :           else if (rtype->named_type() != NULL)
    2053              :             {
    2054            4 :               if (rtype->named_type()->named_object()->package() != NULL)
    2055            0 :                 go_error_at(type->receiver()->location(),
    2056              :                             "may not define methods on non-local type");
    2057              :             }
    2058            1 :           else if (rtype->forward_declaration_type() != NULL)
    2059              :             {
    2060              :               // Go ahead and add the method in case we need to report
    2061              :               // an error when we see the definition.
    2062            1 :               rtype->forward_declaration_type()->add_existing_method(ret);
    2063              :             }
    2064              :           else
    2065            0 :             go_error_at(type->receiver()->location(),
    2066              :                         ("invalid receiver type "
    2067              :                          "(receiver must be a named type)"));
    2068              :         }
    2069          305 :     }
    2070       283265 :   else if (!type->is_method())
    2071              :     {
    2072       205813 :       ret = this->package_->bindings()->add_function(*pname, NULL, function);
    2073       205813 :       if (!ret->is_function() || ret->func_value() != function)
    2074              :         {
    2075              :           // Redefinition error.  Invent a name to avoid knockon
    2076              :           // errors.
    2077            2 :           std::string rname(this->redefined_function_name());
    2078            2 :           ret = this->package_->bindings()->add_function(rname, NULL, function);
    2079            2 :         }
    2080              :     }
    2081              :   else
    2082              :     {
    2083        77452 :       if (!add_method_to_type)
    2084        24262 :         ret = Named_object::make_function(name, NULL, function);
    2085              :       else
    2086              :         {
    2087        53190 :           go_assert(at_top_level);
    2088        53190 :           Type* rtype = type->receiver()->type();
    2089              : 
    2090        53190 :           while (rtype->named_type() != NULL
    2091        53202 :                  && rtype->named_type()->is_alias())
    2092           12 :             rtype = rtype->named_type()->real_type()->forwarded();
    2093              : 
    2094              :           // We want to look through the pointer created by the
    2095              :           // parser, without getting an error if the type is not yet
    2096              :           // defined.
    2097        53190 :           if (rtype->classification() == Type::TYPE_POINTER)
    2098        43456 :             rtype = rtype->points_to();
    2099              : 
    2100        53192 :           while (rtype->named_type() != NULL
    2101        53192 :                  && rtype->named_type()->is_alias())
    2102            2 :             rtype = rtype->named_type()->real_type()->forwarded();
    2103              : 
    2104        53190 :           if (rtype->is_error_type())
    2105            0 :             ret = Named_object::make_function(name, NULL, function);
    2106        53190 :           else if (rtype->named_type() != NULL)
    2107              :             {
    2108        50650 :               if (rtype->named_type()->named_object()->package() != NULL)
    2109              :                 {
    2110            3 :                   go_error_at(type->receiver()->location(),
    2111              :                               "may not define methods on non-local type");
    2112            3 :                   ret = Named_object::make_function(name, NULL, function);
    2113              :                 }
    2114              :               else
    2115              :                 {
    2116        50647 :                   ret = rtype->named_type()->add_method(name, function);
    2117        50647 :                   if (!ret->is_function())
    2118              :                     {
    2119              :                       // Redefinition error.
    2120            2 :                       ret = Named_object::make_function(name, NULL, function);
    2121              :                     }
    2122              :                 }
    2123              :             }
    2124         2540 :           else if (rtype->forward_declaration_type() != NULL)
    2125              :             {
    2126         2538 :               Named_object* type_no =
    2127         2538 :                 rtype->forward_declaration_type()->named_object();
    2128         2538 :               if (type_no->is_unknown())
    2129              :                 {
    2130              :                   // If we are seeing methods it really must be a
    2131              :                   // type.  Declare it as such.  An alternative would
    2132              :                   // be to support lists of methods for unknown
    2133              :                   // expressions.  Either way the error messages if
    2134              :                   // this is not a type are going to get confusing.
    2135          519 :                   Named_object* declared =
    2136          519 :                     this->declare_package_type(type_no->name(),
    2137              :                                                type_no->location());
    2138          519 :                   go_assert(declared
    2139              :                              == type_no->unknown_value()->real_named_object());
    2140              :                 }
    2141         2538 :               ret = rtype->forward_declaration_type()->add_method(name,
    2142              :                                                                   function);
    2143              :             }
    2144              :           else
    2145              :             {
    2146            2 :               go_error_at(type->receiver()->location(),
    2147              :                           ("invalid receiver type (receiver must "
    2148              :                            "be a named type)"));
    2149            2 :               ret = Named_object::make_function(name, NULL, function);
    2150              :             }
    2151              :         }
    2152        77452 :       this->package_->bindings()->add_method(ret);
    2153              :     }
    2154              : 
    2155       283570 :   this->functions_.resize(this->functions_.size() + 1);
    2156       283570 :   Open_function& of(this->functions_.back());
    2157       283570 :   of.function = ret;
    2158       283570 :   of.blocks.push_back(block);
    2159              : 
    2160       283570 :   if (is_init)
    2161              :     {
    2162         1884 :       this->init_functions_.push_back(ret);
    2163         1884 :       this->need_init_fn_ = true;
    2164              :     }
    2165              : 
    2166       283570 :   return ret;
    2167       283570 : }
    2168              : 
    2169              : // Finish compiling a function.
    2170              : 
    2171              : void
    2172       283570 : Gogo::finish_function(Location location)
    2173              : {
    2174       283570 :   this->finish_block(location);
    2175       283570 :   go_assert(this->functions_.back().blocks.empty());
    2176       283570 :   this->functions_.pop_back();
    2177       283570 : }
    2178              : 
    2179              : // Return the current function.
    2180              : 
    2181              : Named_object*
    2182      2109334 : Gogo::current_function() const
    2183              : {
    2184      2109334 :   go_assert(!this->functions_.empty());
    2185      2109334 :   return this->functions_.back().function;
    2186              : }
    2187              : 
    2188              : // Start a new block.
    2189              : 
    2190              : void
    2191       913094 : Gogo::start_block(Location location)
    2192              : {
    2193       913094 :   go_assert(!this->functions_.empty());
    2194       913094 :   Block* block = new Block(this->current_block(), location);
    2195       913094 :   this->functions_.back().blocks.push_back(block);
    2196       913094 : }
    2197              : 
    2198              : // Finish a block.
    2199              : 
    2200              : Block*
    2201      1196664 : Gogo::finish_block(Location location)
    2202              : {
    2203      1196664 :   go_assert(!this->functions_.empty());
    2204      1196664 :   go_assert(!this->functions_.back().blocks.empty());
    2205      1196664 :   Block* block = this->functions_.back().blocks.back();
    2206      1196664 :   this->functions_.back().blocks.pop_back();
    2207      1196664 :   block->set_end_location(location);
    2208      1196664 :   return block;
    2209              : }
    2210              : 
    2211              : // Add an erroneous name.
    2212              : 
    2213              : Named_object*
    2214           14 : Gogo::add_erroneous_name(const std::string& name)
    2215              : {
    2216           14 :   return this->package_->bindings()->add_erroneous_name(name);
    2217              : }
    2218              : 
    2219              : // Add an unknown name.
    2220              : 
    2221              : Named_object*
    2222        85593 : Gogo::add_unknown_name(const std::string& name, Location location)
    2223              : {
    2224        85593 :   return this->package_->bindings()->add_unknown_name(name, location);
    2225              : }
    2226              : 
    2227              : // Declare a function.
    2228              : 
    2229              : Named_object*
    2230         5732 : Gogo::declare_function(const std::string& name, Function_type* type,
    2231              :                        Location location)
    2232              : {
    2233         5732 :   if (!type->is_method())
    2234         5669 :     return this->current_bindings()->add_function_declaration(name, NULL, type,
    2235         5669 :                                                               location);
    2236              :   else
    2237              :     {
    2238              :       // We don't bother to add this to the list of global
    2239              :       // declarations.
    2240           63 :       Type* rtype = type->receiver()->type();
    2241              : 
    2242           63 :       while (rtype->named_type() != NULL
    2243           63 :           && rtype->named_type()->is_alias())
    2244            0 :         rtype = rtype->named_type()->real_type()->forwarded();
    2245              : 
    2246              :       // We want to look through the pointer created by the
    2247              :       // parser, without getting an error if the type is not yet
    2248              :       // defined.
    2249           63 :       if (rtype->classification() == Type::TYPE_POINTER)
    2250            5 :         rtype = rtype->points_to();
    2251              : 
    2252           63 :       while (rtype->named_type() != NULL
    2253           63 :           && rtype->named_type()->is_alias())
    2254            0 :         rtype = rtype->named_type()->real_type()->forwarded();
    2255              : 
    2256           63 :       if (rtype->is_error_type())
    2257              :         return NULL;
    2258           63 :       else if (rtype->named_type() != NULL)
    2259           63 :         return rtype->named_type()->add_method_declaration(name, NULL, type,
    2260           63 :                                                            location);
    2261            0 :       else if (rtype->forward_declaration_type() != NULL)
    2262              :         {
    2263            0 :           Forward_declaration_type* ftype = rtype->forward_declaration_type();
    2264            0 :           return ftype->add_method_declaration(name, NULL, type, location);
    2265              :         }
    2266              :       else
    2267              :         {
    2268            0 :           go_error_at(type->receiver()->location(),
    2269              :                       "invalid receiver type (receiver must be a named type)");
    2270            0 :           return Named_object::make_erroneous_name(name);
    2271              :         }
    2272              :     }
    2273              : }
    2274              : 
    2275              : // Add a label definition.
    2276              : 
    2277              : Label*
    2278        10525 : Gogo::add_label_definition(const std::string& label_name,
    2279              :                            Location location)
    2280              : {
    2281        10525 :   go_assert(!this->functions_.empty());
    2282        10525 :   Function* func = this->functions_.back().function->func_value();
    2283        10525 :   Label* label = func->add_label_definition(this, label_name, location);
    2284        10525 :   this->add_statement(Statement::make_label_statement(label, location));
    2285        10525 :   return label;
    2286              : }
    2287              : 
    2288              : // Add a label reference.
    2289              : 
    2290              : Label*
    2291        10958 : Gogo::add_label_reference(const std::string& label_name,
    2292              :                           Location location, bool issue_goto_errors)
    2293              : {
    2294        10958 :   go_assert(!this->functions_.empty());
    2295        10958 :   Function* func = this->functions_.back().function->func_value();
    2296        10958 :   return func->add_label_reference(this, label_name, location,
    2297        10958 :                                    issue_goto_errors);
    2298              : }
    2299              : 
    2300              : // Return the current binding state.
    2301              : 
    2302              : Bindings_snapshot*
    2303        12092 : Gogo::bindings_snapshot(Location location)
    2304              : {
    2305        12092 :   return new Bindings_snapshot(this->current_block(), location);
    2306              : }
    2307              : 
    2308              : // Add a statement.
    2309              : 
    2310              : void
    2311      1798489 : Gogo::add_statement(Statement* statement)
    2312              : {
    2313      1798489 :   go_assert(!this->functions_.empty()
    2314              :              && !this->functions_.back().blocks.empty());
    2315      1798489 :   this->functions_.back().blocks.back()->add_statement(statement);
    2316      1798489 : }
    2317              : 
    2318              : // Add a block.
    2319              : 
    2320              : void
    2321       444343 : Gogo::add_block(Block* block, Location location)
    2322              : {
    2323       444343 :   go_assert(!this->functions_.empty()
    2324              :              && !this->functions_.back().blocks.empty());
    2325       444343 :   Statement* statement = Statement::make_block_statement(block, location);
    2326       444343 :   this->functions_.back().blocks.back()->add_statement(statement);
    2327       444343 : }
    2328              : 
    2329              : // Add a constant.
    2330              : 
    2331              : Named_object*
    2332       154777 : Gogo::add_constant(const Typed_identifier& tid, Expression* expr,
    2333              :                    int iota_value)
    2334              : {
    2335       154777 :   return this->current_bindings()->add_constant(tid, NULL, expr, iota_value);
    2336              : }
    2337              : 
    2338              : // Add a type.
    2339              : 
    2340              : void
    2341            0 : Gogo::add_type(const std::string& name, Type* type, Location location)
    2342              : {
    2343            0 :   Named_object* no = this->current_bindings()->add_type(name, NULL, type,
    2344              :                                                         location);
    2345            0 :   if (!this->in_global_scope() && no->is_type())
    2346              :     {
    2347            0 :       Named_object* f = this->functions_.back().function;
    2348            0 :       unsigned int index;
    2349            0 :       if (f->is_function())
    2350            0 :         index = f->func_value()->new_local_type_index();
    2351              :       else
    2352              :         index = 0;
    2353            0 :       no->type_value()->set_in_function(f, index);
    2354              :     }
    2355            0 : }
    2356              : 
    2357              : // Add a named type.
    2358              : 
    2359              : void
    2360        97599 : Gogo::add_named_type(Named_type* type)
    2361              : {
    2362        97599 :   go_assert(this->in_global_scope());
    2363        97599 :   this->current_bindings()->add_named_type(type);
    2364        97599 : }
    2365              : 
    2366              : // Declare a type.
    2367              : 
    2368              : Named_object*
    2369        31990 : Gogo::declare_type(const std::string& name, Location location)
    2370              : {
    2371        31990 :   Bindings* bindings = this->current_bindings();
    2372        31990 :   Named_object* no = bindings->add_type_declaration(name, NULL, location);
    2373        31990 :   if (!this->in_global_scope() && no->is_type_declaration())
    2374              :     {
    2375         1295 :       Named_object* f = this->functions_.back().function;
    2376         1295 :       unsigned int index;
    2377         1295 :       if (f->is_function())
    2378         1295 :         index = f->func_value()->new_local_type_index();
    2379              :       else
    2380              :         index = 0;
    2381         1295 :       no->type_declaration_value()->set_in_function(f, index);
    2382              :     }
    2383        31990 :   return no;
    2384              : }
    2385              : 
    2386              : // Declare a type at the package level.
    2387              : 
    2388              : Named_object*
    2389          519 : Gogo::declare_package_type(const std::string& name, Location location)
    2390              : {
    2391          519 :   return this->package_->bindings()->add_type_declaration(name, NULL, location);
    2392              : }
    2393              : 
    2394              : // Declare a function at the package level.
    2395              : 
    2396              : Named_object*
    2397        92810 : Gogo::declare_package_function(const std::string& name, Function_type* type,
    2398              :                                Location location)
    2399              : {
    2400        92810 :   return this->package_->bindings()->add_function_declaration(name, NULL, type,
    2401        92810 :                                                               location);
    2402              : }
    2403              : 
    2404              : // Add a function declaration to the list of functions we may want to
    2405              : // inline.
    2406              : 
    2407              : void
    2408        82289 : Gogo::add_imported_inlinable_function(Named_object* no)
    2409              : {
    2410        82289 :   go_assert(no->is_function_declaration());
    2411        82289 :   Function_declaration* fd = no->func_declaration_value();
    2412        82289 :   if (fd->is_on_inlinable_list())
    2413              :     return;
    2414        12247 :   this->imported_inlinable_functions_.push_back(no);
    2415        12247 :   fd->set_is_on_inlinable_list();
    2416              : }
    2417              : 
    2418              : // Define a type which was already declared.
    2419              : 
    2420              : void
    2421        31990 : Gogo::define_type(Named_object* no, Named_type* type)
    2422              : {
    2423        31990 :   this->current_bindings()->define_type(no, type);
    2424        31990 : }
    2425              : 
    2426              : // Add a variable.
    2427              : 
    2428              : Named_object*
    2429       405952 : Gogo::add_variable(const std::string& name, Variable* variable)
    2430              : {
    2431       405952 :   Named_object* no = this->current_bindings()->add_variable(name, NULL,
    2432              :                                                             variable);
    2433              : 
    2434              :   // In a function the middle-end wants to see a DECL_EXPR node.
    2435       405952 :   if (no != NULL
    2436       405952 :       && no->is_variable()
    2437       405950 :       && !no->var_value()->is_parameter()
    2438       811901 :       && !this->functions_.empty())
    2439       380475 :     this->add_statement(Statement::make_variable_declaration(no));
    2440              : 
    2441       405952 :   return no;
    2442              : }
    2443              : 
    2444              : void
    2445       474197 : Gogo::rename_if_empty(std::string* pname, const char* tag, unsigned* count)
    2446              : {
    2447       474197 :   if (pname->empty() || Gogo::is_sink_name(*pname))
    2448              :     {
    2449         3395 :       char buf[50];
    2450         3395 :       go_assert(strlen(tag) < 10);
    2451         3395 :       snprintf(buf, sizeof buf, "%s.%u", tag, *count);
    2452         3395 :       ++(*count);
    2453         3395 :       *pname = buf;
    2454              :     }
    2455       474197 : }
    2456              : 
    2457              : 
    2458              : // Add a sink--a reference to the blank identifier _.
    2459              : 
    2460              : Named_object*
    2461        19303 : Gogo::add_sink()
    2462              : {
    2463        19303 :   return Named_object::make_sink();
    2464              : }
    2465              : 
    2466              : // Add a named object for a dot import.
    2467              : 
    2468              : void
    2469        62469 : Gogo::add_dot_import_object(Named_object* no)
    2470              : {
    2471              :   // If the name already exists, then it was defined in some file seen
    2472              :   // earlier.  If the earlier name is just a declaration, don't add
    2473              :   // this name, because that will cause the previous declaration to
    2474              :   // merge to this imported name, which should not happen.  Just add
    2475              :   // this name to the list of file block names to get appropriate
    2476              :   // errors if we see a later definition.
    2477        62469 :   Named_object* e = this->package_->bindings()->lookup(no->name());
    2478        62469 :   if (e != NULL && e->package() == NULL)
    2479              :     {
    2480            1 :       if (e->is_unknown())
    2481            1 :         e = e->resolve();
    2482            1 :       if (e->package() == NULL
    2483            1 :           && (e->is_type_declaration()
    2484              :               || e->is_function_declaration()
    2485              :               || e->is_unknown()))
    2486              :         {
    2487            1 :           this->add_file_block_name(no->name(), no->location());
    2488            1 :           return;
    2489              :         }
    2490              :     }
    2491              : 
    2492        62468 :   this->current_bindings()->add_named_object(no);
    2493              : }
    2494              : 
    2495              : // Add a linkname.  This implements the go:linkname compiler directive.
    2496              : // We only support this for functions and function declarations.
    2497              : 
    2498              : void
    2499         3913 : Gogo::add_linkname(const std::string& go_name, bool is_exported,
    2500              :                    const std::string& ext_name, Location loc)
    2501              : {
    2502         3913 :   Named_object* no =
    2503         3913 :     this->package_->bindings()->lookup(this->pack_hidden_name(go_name,
    2504              :                                                               is_exported));
    2505         3913 :   if (no == NULL)
    2506            0 :     go_error_at(loc, "%s is not defined", go_name.c_str());
    2507         3913 :   else if (no->is_function())
    2508              :     {
    2509         2641 :       if (ext_name.empty())
    2510         1673 :         no->func_value()->set_is_exported_by_linkname();
    2511              :       else
    2512          968 :         no->func_value()->set_asm_name(ext_name);
    2513              :     }
    2514         1272 :   else if (no->is_function_declaration())
    2515              :     {
    2516         1272 :       if (ext_name.empty())
    2517            0 :         go_error_at(loc,
    2518              :                     ("%<//go:linkname%> missing external name "
    2519              :                      "for declaration of %s"),
    2520              :                     go_name.c_str());
    2521              :       else
    2522         1272 :         no->func_declaration_value()->set_asm_name(ext_name);
    2523              :     }
    2524              :   else
    2525            0 :     go_error_at(loc,
    2526              :                 ("%s is not a function; "
    2527              :                  "%<//go:linkname%> is only supported for functions"),
    2528              :                 go_name.c_str());
    2529         3913 : }
    2530              : 
    2531              : // Mark all local variables used.  This is used when some types of
    2532              : // parse error occur.
    2533              : 
    2534              : void
    2535           79 : Gogo::mark_locals_used()
    2536              : {
    2537          119 :   for (Open_functions::iterator pf = this->functions_.begin();
    2538          119 :        pf != this->functions_.end();
    2539           40 :        ++pf)
    2540              :     {
    2541           96 :       for (std::vector<Block*>::iterator pb = pf->blocks.begin();
    2542           96 :            pb != pf->blocks.end();
    2543           56 :            ++pb)
    2544           56 :         (*pb)->bindings()->mark_locals_used();
    2545              :     }
    2546           79 : }
    2547              : 
    2548              : // Record that we've seen an interface type.
    2549              : 
    2550              : void
    2551        71624 : Gogo::record_interface_type(Interface_type* itype)
    2552              : {
    2553        71624 :   this->interface_types_.push_back(itype);
    2554        71624 : }
    2555              : 
    2556              : // Define the global names.  We do this only after parsing all the
    2557              : // input files, because the program might define the global names
    2558              : // itself.
    2559              : 
    2560              : void
    2561         4646 : Gogo::define_global_names()
    2562              : {
    2563         4646 :   if (this->is_main_package())
    2564              :     {
    2565              :       // Every Go program has to import the runtime package, so that
    2566              :       // it is properly initialized.  We can't use
    2567              :       // predeclared_location here as it will cause runtime functions
    2568              :       // to appear to be builtin functions.
    2569         1889 :       this->import_package("runtime", "_", false, false,
    2570         1889 :                            this->package_->location());
    2571              :     }
    2572              : 
    2573       185840 :   for (Bindings::const_declarations_iterator p =
    2574         4646 :          this->globals_->begin_declarations();
    2575       190486 :        p != this->globals_->end_declarations();
    2576       185840 :        ++p)
    2577              :     {
    2578       185840 :       Named_object* global_no = p->second;
    2579       185840 :       std::string name(Gogo::pack_hidden_name(global_no->name(), false));
    2580       185840 :       Named_object* no = this->package_->bindings()->lookup(name);
    2581       185840 :       if (no == NULL)
    2582       155926 :         continue;
    2583        29914 :       no = no->resolve();
    2584        29914 :       if (no->is_type_declaration())
    2585              :         {
    2586            2 :           if (global_no->is_type())
    2587              :             {
    2588            2 :               if (no->type_declaration_value()->has_methods())
    2589              :                 {
    2590            4 :                   for (std::vector<Named_object*>::const_iterator pm =
    2591            2 :                          no->type_declaration_value()->methods()->begin();
    2592            4 :                        pm != no->type_declaration_value()->methods()->end();
    2593            2 :                        pm++)
    2594            2 :                     go_error_at((*pm)->location(),
    2595              :                                 "may not define methods on non-local type");
    2596              :                 }
    2597            2 :               no->set_type_value(global_no->type_value());
    2598              :             }
    2599              :           else
    2600              :             {
    2601            0 :               go_error_at(no->location(), "expected type");
    2602            0 :               Type* errtype = Type::make_error_type();
    2603            0 :               Named_object* err =
    2604            0 :                 Named_object::make_type("erroneous_type", NULL, errtype,
    2605              :                                         Linemap::predeclared_location());
    2606            0 :               no->set_type_value(err->type_value());
    2607              :             }
    2608              :         }
    2609        29912 :       else if (no->is_unknown())
    2610        29815 :         no->unknown_value()->set_real_named_object(global_no);
    2611       185840 :     }
    2612              : 
    2613              :   // Give an error if any name is defined in both the package block
    2614              :   // and the file block.  For example, this can happen if one file
    2615              :   // imports "fmt" and another file defines a global variable fmt.
    2616       336794 :   for (Bindings::const_declarations_iterator p =
    2617         4646 :          this->package_->bindings()->begin_declarations();
    2618       341440 :        p != this->package_->bindings()->end_declarations();
    2619       336794 :        ++p)
    2620              :     {
    2621       336794 :       if (p->second->is_unknown()
    2622       336794 :           && p->second->unknown_value()->real_named_object() == NULL)
    2623              :         {
    2624              :           // No point in warning about an undefined name, as we will
    2625              :           // get other errors later anyhow.
    2626           53 :           continue;
    2627              :         }
    2628       336741 :       File_block_names::const_iterator pf =
    2629       336741 :         this->file_block_names_.find(p->second->name());
    2630       336741 :       if (pf != this->file_block_names_.end())
    2631              :         {
    2632            1 :           std::string n = p->second->message_name();
    2633            1 :           go_error_at(p->second->location(),
    2634              :                       "%qs defined as both imported name and global name",
    2635              :                       n.c_str());
    2636            1 :           go_inform(pf->second, "%qs imported here", n.c_str());
    2637            1 :         }
    2638              : 
    2639              :       // No package scope identifier may be named "init".
    2640       673482 :       if (!p->second->is_function()
    2641       336741 :           && Gogo::unpack_hidden_name(p->second->name()) == "init")
    2642              :         {
    2643            4 :           go_error_at(p->second->location(),
    2644              :                       "cannot declare init - must be func");
    2645              :         }
    2646              :     }
    2647         4646 : }
    2648              : 
    2649              : // Clear out names in file scope.
    2650              : 
    2651              : void
    2652        12707 : Gogo::clear_file_scope()
    2653              : {
    2654        12707 :   this->package_->bindings()->clear_file_scope(this);
    2655              : 
    2656              :   // Warn about packages which were imported but not used.
    2657        12707 :   bool quiet = saw_errors();
    2658       375205 :   for (Packages::iterator p = this->packages_.begin();
    2659       375205 :        p != this->packages_.end();
    2660       362498 :        ++p)
    2661              :     {
    2662       362498 :       Package* package = p->second;
    2663       362498 :       if (package != this->package_ && !quiet)
    2664              :         {
    2665       389161 :           for (Package::Aliases::const_iterator p1 = package->aliases().begin();
    2666       389161 :                p1 != package->aliases().end();
    2667        39719 :                ++p1)
    2668              :             {
    2669        39719 :               if (!p1->second->used())
    2670              :                 {
    2671              :                   // Give a more refined error message if the alias name is known.
    2672           13 :                   std::string pkg_name = package->package_name();
    2673           13 :                   if (p1->first != pkg_name && p1->first[0] != '.')
    2674              :                     {
    2675            8 :                       go_error_at(p1->second->location(),
    2676              :                                   "imported and not used: %s as %s",
    2677            8 :                                   Gogo::message_name(pkg_name).c_str(),
    2678            8 :                                   Gogo::message_name(p1->first).c_str());
    2679              :                     }
    2680              :                   else
    2681            9 :                     go_error_at(p1->second->location(),
    2682              :                                 "imported and not used: %s",
    2683           18 :                                 Gogo::message_name(pkg_name).c_str());
    2684           13 :                 }
    2685              :             }
    2686              :         }
    2687       362498 :       package->clear_used();
    2688              :     }
    2689              : 
    2690        12707 :   this->current_file_imported_unsafe_ = false;
    2691        12707 :   this->current_file_imported_embed_ = false;
    2692        12707 : }
    2693              : 
    2694              : // Queue up a type-specific hash function for later writing.  These
    2695              : // are written out in write_specific_type_functions, called after the
    2696              : // parse tree is lowered.
    2697              : 
    2698              : void
    2699            1 : Gogo::queue_hash_function(Type* type, int64_t size, Backend_name* bname,
    2700              :                           Function_type* hash_fntype)
    2701              : {
    2702            1 :   go_assert(!this->specific_type_functions_are_written_);
    2703            1 :   go_assert(!this->in_global_scope());
    2704            1 :   Specific_type_function::Specific_type_function_kind kind =
    2705              :     Specific_type_function::SPECIFIC_HASH;
    2706            1 :   Specific_type_function* tsf = new Specific_type_function(type, NULL, size,
    2707              :                                                            kind, bname,
    2708            1 :                                                            hash_fntype);
    2709            1 :   this->specific_type_functions_.push_back(tsf);
    2710            1 : }
    2711              : 
    2712              : // Queue up a type-specific equal function for later writing.  These
    2713              : // are written out in write_specific_type_functions, called after the
    2714              : // parse tree is lowered.
    2715              : 
    2716              : void
    2717           75 : Gogo::queue_equal_function(Type* type, Named_type* name, int64_t size,
    2718              :                            Backend_name* bname, Function_type* equal_fntype)
    2719              : {
    2720           75 :   go_assert(!this->specific_type_functions_are_written_);
    2721           75 :   go_assert(!this->in_global_scope());
    2722           75 :   Specific_type_function::Specific_type_function_kind kind =
    2723              :     Specific_type_function::SPECIFIC_EQUAL;
    2724           75 :   Specific_type_function* tsf = new Specific_type_function(type, name, size,
    2725              :                                                            kind, bname,
    2726           75 :                                                            equal_fntype);
    2727           75 :   this->specific_type_functions_.push_back(tsf);
    2728           75 : }
    2729              : 
    2730              : // Look for types which need specific hash or equality functions.
    2731              : 
    2732         9292 : class Specific_type_functions : public Traverse
    2733              : {
    2734              :  public:
    2735         4646 :   Specific_type_functions(Gogo* gogo)
    2736         4646 :     : Traverse(traverse_types),
    2737         4646 :       gogo_(gogo)
    2738              :   { }
    2739              : 
    2740              :   int
    2741              :   type(Type*);
    2742              : 
    2743              :  private:
    2744              :   Gogo* gogo_;
    2745              : };
    2746              : 
    2747              : int
    2748     11352289 : Specific_type_functions::type(Type* t)
    2749              : {
    2750     11352289 :   switch (t->classification())
    2751              :     {
    2752       688700 :     case Type::TYPE_NAMED:
    2753       688700 :       {
    2754       688700 :         Named_type* nt = t->named_type();
    2755       688700 :         if (nt->is_alias())
    2756              :           return TRAVERSE_CONTINUE;
    2757       665511 :         if (t->needs_specific_type_functions(this->gogo_))
    2758       360328 :           t->equal_function(this->gogo_, nt, NULL);
    2759              : 
    2760              :         // If this is a struct type, we don't want to make functions
    2761              :         // for the unnamed struct.
    2762       665511 :         Type* rt = nt->real_type();
    2763       665511 :         if (rt->struct_type() == NULL)
    2764              :           {
    2765       256727 :             if (Type::traverse(rt, this) == TRAVERSE_EXIT)
    2766              :               return TRAVERSE_EXIT;
    2767              :           }
    2768              :         else
    2769              :           {
    2770              :             // If this type is defined in another package, then we don't
    2771              :             // need to worry about the unexported fields.
    2772       408784 :             bool is_defined_elsewhere = nt->named_object()->package() != NULL;
    2773       817568 :             const Struct_field_list* fields = rt->struct_type()->fields();
    2774      2687947 :             for (Struct_field_list::const_iterator p = fields->begin();
    2775      2687947 :                  p != fields->end();
    2776      2279163 :                  ++p)
    2777              :               {
    2778      3850234 :                 if (is_defined_elsewhere
    2779      2279163 :                     && Gogo::is_hidden_name(p->field_name()))
    2780      1571071 :                   continue;
    2781       708092 :                 if (Type::traverse(p->type(), this) == TRAVERSE_EXIT)
    2782            0 :                   return TRAVERSE_EXIT;
    2783              :               }
    2784              :           }
    2785              : 
    2786              :         return TRAVERSE_SKIP_COMPONENTS;
    2787              :       }
    2788              : 
    2789       680219 :     case Type::TYPE_STRUCT:
    2790       680219 :     case Type::TYPE_ARRAY:
    2791       680219 :       if (t->needs_specific_type_functions(this->gogo_))
    2792       165530 :         t->equal_function(this->gogo_, NULL, NULL);
    2793              :       break;
    2794              : 
    2795        77572 :     case Type::TYPE_MAP:
    2796        77572 :       {
    2797       155144 :         Type* key_type = t->map_type()->key_type()->unalias();
    2798        77572 :         if (key_type->needs_specific_type_functions(this->gogo_))
    2799        37920 :           key_type->hash_function(this->gogo_, NULL);
    2800              :       }
    2801              :       break;
    2802              : 
    2803              :     default:
    2804              :       break;
    2805              :     }
    2806              : 
    2807              :   return TRAVERSE_CONTINUE;
    2808              : }
    2809              : 
    2810              : // Write out type specific functions.
    2811              : 
    2812              : void
    2813         4646 : Gogo::write_specific_type_functions()
    2814              : {
    2815         4646 :   Specific_type_functions stf(this);
    2816         4646 :   this->traverse(&stf);
    2817              : 
    2818         9368 :   while (!this->specific_type_functions_.empty())
    2819              :     {
    2820           76 :       Specific_type_function* tsf = this->specific_type_functions_.back();
    2821           76 :       this->specific_type_functions_.pop_back();
    2822           76 :       if (tsf->kind == Specific_type_function::SPECIFIC_HASH)
    2823            1 :         tsf->type->write_hash_function(this, tsf->size, &tsf->bname,
    2824              :                                        tsf->fntype);
    2825              :       else
    2826           75 :         tsf->type->write_equal_function(this, tsf->name, tsf->size,
    2827           75 :                                         &tsf->bname, tsf->fntype);
    2828          152 :       delete tsf;
    2829              :     }
    2830         4646 :   this->specific_type_functions_are_written_ = true;
    2831         4646 : }
    2832              : 
    2833              : // Traverse the tree.
    2834              : 
    2835              : void
    2836        93447 : Gogo::traverse(Traverse* traverse)
    2837              : {
    2838              :   // Traverse the current package first for consistency.  The other
    2839              :   // packages will only contain imported types, constants, and
    2840              :   // declarations.
    2841        93447 :   if (this->package_->bindings()->traverse(traverse, true) == TRAVERSE_EXIT)
    2842              :     return;
    2843      2497779 :   for (Packages::const_iterator p = this->packages_.begin();
    2844      2497779 :        p != this->packages_.end();
    2845      2404332 :        ++p)
    2846              :     {
    2847      2404332 :       if (p->second != this->package_)
    2848              :         {
    2849      2310885 :           if (p->second->bindings()->traverse(traverse, true) == TRAVERSE_EXIT)
    2850              :             break;
    2851              :         }
    2852              :     }
    2853              : }
    2854              : 
    2855              : // Add a type to verify.  This is used for types of sink variables, in
    2856              : // order to give appropriate error messages.
    2857              : 
    2858              : void
    2859         2352 : Gogo::add_type_to_verify(Type* type)
    2860              : {
    2861         2352 :   this->verify_types_.push_back(type);
    2862         2352 : }
    2863              : 
    2864              : // Traversal class used to verify types.
    2865              : 
    2866         9292 : class Verify_types : public Traverse
    2867              : {
    2868              :  public:
    2869         4646 :   Verify_types(Gogo* gogo)
    2870         4646 :     : Traverse(traverse_types),
    2871         4646 :       gogo_(gogo)
    2872              :   { }
    2873              : 
    2874              :   int
    2875              :   type(Type*);
    2876              : 
    2877              :  private:
    2878              :   Gogo* gogo_;
    2879              : };
    2880              : 
    2881              : // Verify that a type is correct.
    2882              : 
    2883              : int
    2884      9991308 : Verify_types::type(Type* t)
    2885              : {
    2886      9991308 :   if (!t->verify(this->gogo_))
    2887           46 :     return TRAVERSE_SKIP_COMPONENTS;
    2888              :   return TRAVERSE_CONTINUE;
    2889              : }
    2890              : 
    2891              : // Verify that all types are correct.
    2892              : 
    2893              : void
    2894         4646 : Gogo::verify_types()
    2895              : {
    2896         4646 :   Verify_types traverse(this);
    2897         4646 :   this->traverse(&traverse);
    2898              : 
    2899         4646 :   for (std::vector<Type*>::iterator p = this->verify_types_.begin();
    2900         6998 :        p != this->verify_types_.end();
    2901         2352 :        ++p)
    2902         2352 :     (*p)->verify(this);
    2903         5046 :   this->verify_types_.clear();
    2904         4646 : }
    2905              : 
    2906              : // Traversal class used to lower parse tree.
    2907              : 
    2908         4646 : class Lower_parse_tree : public Traverse
    2909              : {
    2910              :  public:
    2911      1845831 :   Lower_parse_tree(Gogo* gogo, Named_object* function)
    2912              :     : Traverse(traverse_variables
    2913              :                | traverse_constants
    2914              :                | traverse_functions
    2915              :                | traverse_statements
    2916              :                | traverse_expressions),
    2917      1845831 :       gogo_(gogo), function_(function), inserter_()
    2918              :   { }
    2919              : 
    2920              :   void
    2921       509729 :   set_inserter(const Statement_inserter* inserter)
    2922       509729 :   { this->inserter_ = *inserter; }
    2923              : 
    2924              :   int
    2925              :   variable(Named_object*);
    2926              : 
    2927              :   int
    2928              :   constant(Named_object*, bool);
    2929              : 
    2930              :   int
    2931              :   function(Named_object*);
    2932              : 
    2933              :   int
    2934              :   statement(Block*, size_t* pindex, Statement*);
    2935              : 
    2936              :   int
    2937              :   expression(Expression**);
    2938              : 
    2939              :  private:
    2940              :   // General IR.
    2941              :   Gogo* gogo_;
    2942              :   // The function we are traversing.
    2943              :   Named_object* function_;
    2944              :   // Current statement inserter for use by expressions.
    2945              :   Statement_inserter inserter_;
    2946              : };
    2947              : 
    2948              : // Lower variables.
    2949              : 
    2950              : int
    2951      1303459 : Lower_parse_tree::variable(Named_object* no)
    2952              : {
    2953      1303459 :   if (!no->is_variable())
    2954              :     return TRAVERSE_CONTINUE;
    2955              : 
    2956      1160523 :   if (no->is_variable() && no->var_value()->is_global())
    2957              :     {
    2958              :       // Global variables can have loops in their initialization
    2959              :       // expressions.  This is handled in lower_init_expression.
    2960       348764 :       no->var_value()->lower_init_expression(this->gogo_, this->function_,
    2961              :                                              &this->inserter_);
    2962       348764 :       return TRAVERSE_CONTINUE;
    2963              :     }
    2964              : 
    2965              :   // This is a local variable.  We are going to return
    2966              :   // TRAVERSE_SKIP_COMPONENTS here because we want to traverse the
    2967              :   // initialization expression when we reach the variable declaration
    2968              :   // statement.  However, that means that we need to traverse the type
    2969              :   // ourselves.
    2970       811759 :   if (no->var_value()->has_type())
    2971              :     {
    2972       811759 :       Type* type = no->var_value()->type();
    2973       811759 :       if (type != NULL)
    2974              :         {
    2975       811759 :           if (Type::traverse(type, this) == TRAVERSE_EXIT)
    2976              :             return TRAVERSE_EXIT;
    2977              :         }
    2978              :     }
    2979       811759 :   go_assert(!no->var_value()->has_pre_init());
    2980              : 
    2981              :   return TRAVERSE_SKIP_COMPONENTS;
    2982              : }
    2983              : 
    2984              : // Lower constants.  We handle constants specially so that we can set
    2985              : // the right value for the predeclared constant iota.  This works in
    2986              : // conjunction with the way we lower Const_expression objects.
    2987              : 
    2988              : int
    2989      2207813 : Lower_parse_tree::constant(Named_object* no, bool)
    2990              : {
    2991      2207813 :   Named_constant* nc = no->const_value();
    2992              : 
    2993              :   // Don't get into trouble if the constant's initializer expression
    2994              :   // refers to the constant itself.
    2995      2207813 :   if (nc->lowering())
    2996              :     return TRAVERSE_CONTINUE;
    2997      2207813 :   nc->set_lowering();
    2998              : 
    2999      2207813 :   nc->traverse_expression(this);
    3000              : 
    3001      2207813 :   nc->clear_lowering();
    3002              : 
    3003              :   // We will traverse the expression a second time, but that will be
    3004              :   // fast.
    3005              : 
    3006      2207813 :   return TRAVERSE_CONTINUE;
    3007              : }
    3008              : 
    3009              : // Lower the body of a function, and set the closure type.  Record the
    3010              : // function while lowering it, so that we can pass it down when
    3011              : // lowering an expression.
    3012              : 
    3013              : int
    3014       175648 : Lower_parse_tree::function(Named_object* no)
    3015              : {
    3016       175648 :   go_assert(this->function_ == NULL);
    3017       175648 :   this->function_ = no;
    3018       175648 :   int t = no->func_value()->traverse(this);
    3019       175648 :   this->function_ = NULL;
    3020              : 
    3021       175648 :   if (t == TRAVERSE_EXIT)
    3022            0 :     return t;
    3023              :   return TRAVERSE_SKIP_COMPONENTS;
    3024              : }
    3025              : 
    3026              : // Lower statement parse trees.
    3027              : 
    3028              : int
    3029      6344381 : Lower_parse_tree::statement(Block* block, size_t* pindex, Statement* sorig)
    3030              : {
    3031              :   // Because we explicitly traverse the statement's contents
    3032              :   // ourselves, we want to skip block statements here.  There is
    3033              :   // nothing to lower in a block statement.
    3034      6344381 :   if (sorig->is_block_statement())
    3035              :     return TRAVERSE_CONTINUE;
    3036              : 
    3037      5282412 :   Statement_inserter hold_inserter(this->inserter_);
    3038      5282412 :   this->inserter_ = Statement_inserter(block, pindex);
    3039              : 
    3040              :   // Lower the expressions first.
    3041      5282412 :   int t = sorig->traverse_contents(this);
    3042      5282412 :   if (t == TRAVERSE_EXIT)
    3043              :     {
    3044            0 :       this->inserter_ = hold_inserter;
    3045            0 :       return t;
    3046              :     }
    3047              : 
    3048              :   // Keep lowering until nothing changes.
    3049              :   Statement* s = sorig;
    3050      5998487 :   while (true)
    3051              :     {
    3052      5998487 :       Statement* snew = s->lower(this->gogo_, this->function_, block,
    3053              :                                  &this->inserter_);
    3054      5998487 :       if (snew == s)
    3055              :         break;
    3056       716075 :       s = snew;
    3057       716075 :       t = s->traverse_contents(this);
    3058       716075 :       if (t == TRAVERSE_EXIT)
    3059              :         {
    3060            0 :           this->inserter_ = hold_inserter;
    3061            0 :           return t;
    3062              :         }
    3063              :     }
    3064              : 
    3065      5282412 :   if (s != sorig)
    3066       657256 :     block->replace_statement(*pindex, s);
    3067              : 
    3068      5282412 :   this->inserter_ = hold_inserter;
    3069      5282412 :   return TRAVERSE_SKIP_COMPONENTS;
    3070              : }
    3071              : 
    3072              : // Lower expression parse trees.
    3073              : 
    3074              : int
    3075     29688962 : Lower_parse_tree::expression(Expression** pexpr)
    3076              : {
    3077              :   // We have to lower all subexpressions first, so that we can get
    3078              :   // their type if necessary.  This is awkward, because we don't have
    3079              :   // a postorder traversal pass.
    3080     29688962 :   if ((*pexpr)->traverse_subexpressions(this) == TRAVERSE_EXIT)
    3081              :     return TRAVERSE_EXIT;
    3082              :   // Keep lowering until nothing changes.
    3083     33718790 :   while (true)
    3084              :     {
    3085     31703876 :       Expression* e = *pexpr;
    3086     31703876 :       Expression* enew = e->lower(this->gogo_, this->function_,
    3087              :                                   &this->inserter_);
    3088     31703876 :       if (enew == e)
    3089              :         break;
    3090      2014914 :       if (enew->traverse_subexpressions(this) == TRAVERSE_EXIT)
    3091              :         return TRAVERSE_EXIT;
    3092      2014914 :       *pexpr = enew;
    3093      2014914 :     }
    3094              : 
    3095              :   // Lower the type of this expression before the parent looks at it,
    3096              :   // in case the type contains an array that has expressions in its
    3097              :   // length.  Skip an Unknown_expression, as at this point that means
    3098              :   // a composite literal key that does not have a type.
    3099     29688962 :   if ((*pexpr)->unknown_expression() == NULL)
    3100     29688962 :     Type::traverse((*pexpr)->type(), this);
    3101              : 
    3102              :   return TRAVERSE_SKIP_COMPONENTS;
    3103              : }
    3104              : 
    3105              : // Lower the parse tree.  This is called after the parse is complete,
    3106              : // when all names should be resolved.
    3107              : 
    3108              : void
    3109         4646 : Gogo::lower_parse_tree()
    3110              : {
    3111         4646 :   Lower_parse_tree lower_parse_tree(this, NULL);
    3112         4646 :   this->traverse(&lower_parse_tree);
    3113              : 
    3114              :   // If we found any functions defined in other packages that are
    3115              :   // inlinables, import their bodies and turn them into functions.
    3116              :   //
    3117              :   // Note that as we import inlinable functions we may find more
    3118              :   // inlinable functions, so don't use an iterator.
    3119        16893 :   for (size_t i = 0; i < this->imported_inlinable_functions_.size(); i++)
    3120              :     {
    3121        12247 :       Named_object* no = this->imported_inlinable_functions_[i];
    3122        12247 :       no->func_declaration_value()->import_function_body(this, no);
    3123              :     }
    3124              : 
    3125              :   // There might be type definitions that involve expressions such as the
    3126              :   // array length.  Make sure to lower these expressions as well.  Otherwise,
    3127              :   // errors hidden within a type can introduce unexpected errors into later
    3128              :   // passes.
    3129         4646 :   for (std::vector<Type*>::iterator p = this->verify_types_.begin();
    3130         4646 :        p != this->verify_types_.end();
    3131            0 :        ++p)
    3132            0 :     Type::traverse(*p, &lower_parse_tree);
    3133         4646 : }
    3134              : 
    3135              : // Lower a block.
    3136              : 
    3137              : void
    3138       121652 : Gogo::lower_block(Named_object* function, Block* block)
    3139              : {
    3140       121652 :   Lower_parse_tree lower_parse_tree(this, function);
    3141       121652 :   block->traverse(&lower_parse_tree);
    3142       121652 : }
    3143              : 
    3144              : // Lower an expression.  INSERTER may be NULL, in which case the
    3145              : // expression had better not need to create any temporaries.
    3146              : 
    3147              : void
    3148       526274 : Gogo::lower_expression(Named_object* function, Statement_inserter* inserter,
    3149              :                        Expression** pexpr)
    3150              : {
    3151       526274 :   Lower_parse_tree lower_parse_tree(this, function);
    3152       526274 :   if (inserter != NULL)
    3153       509729 :     lower_parse_tree.set_inserter(inserter);
    3154       526274 :   lower_parse_tree.expression(pexpr);
    3155       526274 : }
    3156              : 
    3157              : // Lower a constant.  This is called when lowering a reference to a
    3158              : // constant.  We have to make sure that the constant has already been
    3159              : // lowered.
    3160              : 
    3161              : void
    3162      1193259 : Gogo::lower_constant(Named_object* no)
    3163              : {
    3164      1193259 :   go_assert(no->is_const());
    3165      1193259 :   Lower_parse_tree lower(this, NULL);
    3166      1193259 :   lower.constant(no, false);
    3167      1193259 : }
    3168              : 
    3169              : // Make implicit type conversions explicit.  Currently only does for
    3170              : // interface conversions, so the escape analysis can see them and
    3171              : // optimize.
    3172              : 
    3173        36574 : class Add_conversions : public Traverse
    3174              : {
    3175              :  public:
    3176        18287 :   Add_conversions()
    3177              :     : Traverse(traverse_statements
    3178        18287 :                | traverse_expressions)
    3179              :   { }
    3180              : 
    3181              :   int
    3182              :   statement(Block*, size_t* pindex, Statement*);
    3183              : 
    3184              :   int
    3185              :   expression(Expression**);
    3186              : };
    3187              : 
    3188              : // Add explicit conversions in a statement.
    3189              : 
    3190              : int
    3191      3845075 : Add_conversions::statement(Block*, size_t*, Statement* sorig)
    3192              : {
    3193      3845075 :   sorig->add_conversions();
    3194      3845075 :   return TRAVERSE_CONTINUE;
    3195              : }
    3196              : 
    3197              : // Add explicit conversions in an expression.
    3198              : 
    3199              : int
    3200     11386106 : Add_conversions::expression(Expression** pexpr)
    3201              : {
    3202     11386106 :   (*pexpr)->add_conversions();
    3203     11386106 :   return TRAVERSE_CONTINUE;
    3204              : }
    3205              : 
    3206              : void
    3207         4646 : Gogo::add_conversions()
    3208              : {
    3209         4646 :   Add_conversions add_conversions;
    3210         4646 :   this->traverse(&add_conversions);
    3211         4646 : }
    3212              : 
    3213              : void
    3214        13641 : Gogo::add_conversions_in_block(Block *b)
    3215              : {
    3216        13641 :   Add_conversions add_conversions;
    3217        13641 :   b->traverse(&add_conversions);
    3218        13641 : }
    3219              : 
    3220              : // Traversal class for simple deadcode elimination.
    3221              : 
    3222         9292 : class Remove_deadcode : public Traverse
    3223              : {
    3224              :  public:
    3225         4646 :   Remove_deadcode(Gogo* gogo)
    3226         4646 :     : Traverse(traverse_statements
    3227              :                | traverse_expressions),
    3228         4646 :       gogo_(gogo)
    3229              :   { }
    3230              : 
    3231              :   int
    3232              :   statement(Block*, size_t* pindex, Statement*);
    3233              : 
    3234              :   int
    3235              :   expression(Expression**);
    3236              : 
    3237              :  private:
    3238              :   Gogo* gogo_;
    3239              : };
    3240              : 
    3241              : // Remove deadcode in a statement.
    3242              : 
    3243              : int
    3244      3792796 : Remove_deadcode::statement(Block* block, size_t* pindex, Statement* sorig)
    3245              : {
    3246      3792796 :   Location loc = sorig->location();
    3247      3792796 :   If_statement* ifs = sorig->if_statement();
    3248       336470 :   if (ifs != NULL)
    3249              :     {
    3250              :       // Remove the dead branch of an if statement.
    3251       336470 :       bool bval;
    3252       336470 :       if (ifs->condition()->boolean_constant_value(&bval))
    3253              :         {
    3254         4381 :           Statement* s;
    3255         4381 :           if (bval)
    3256          861 :             s = Statement::make_block_statement(ifs->then_block(),
    3257              :                                                 loc);
    3258              :           else
    3259         3520 :             if (ifs->else_block() != NULL)
    3260          273 :               s = Statement::make_block_statement(ifs->else_block(),
    3261              :                                                   loc);
    3262              :             else
    3263              :               // Make a dummy statement.
    3264         3247 :               s = Statement::make_statement(Expression::make_boolean(false, loc),
    3265              :                                             true);
    3266              : 
    3267         4381 :           block->replace_statement(*pindex, s);
    3268              :         }
    3269              :     }
    3270      3792796 :   return TRAVERSE_CONTINUE;
    3271              : }
    3272              : 
    3273              : // Remove deadcode in an expression.
    3274              : 
    3275              : int
    3276     11084394 : Remove_deadcode::expression(Expression** pexpr)
    3277              : {
    3278              :   // Discard the right arm of a shortcut expression of constant value.
    3279     11084394 :   Binary_expression* be = (*pexpr)->binary_expression();
    3280       651226 :   bool bval;
    3281       651226 :   if (be != NULL
    3282       651226 :       && be->boolean_constant_value(&bval)
    3283         1310 :       && (be->op() == OPERATOR_ANDAND
    3284          385 :           || be->op() == OPERATOR_OROR))
    3285              :     {
    3286         1308 :       *pexpr = Expression::make_boolean(bval, be->location());
    3287         1308 :       Type_context context(NULL, false);
    3288         1308 :       (*pexpr)->determine_type(this->gogo_, &context);
    3289              :     }
    3290     11084394 :   return TRAVERSE_CONTINUE;
    3291              : }
    3292              : 
    3293              : // Remove deadcode.
    3294              : 
    3295              : void
    3296         4646 : Gogo::remove_deadcode()
    3297              : {
    3298         4646 :   Remove_deadcode remove_deadcode(this);
    3299         4646 :   this->traverse(&remove_deadcode);
    3300         4646 : }
    3301              : 
    3302              : // Traverse the tree to create function descriptors as needed.
    3303              : 
    3304         9292 : class Create_function_descriptors : public Traverse
    3305              : {
    3306              :  public:
    3307         4646 :   Create_function_descriptors(Gogo* gogo)
    3308         4646 :     : Traverse(traverse_functions | traverse_expressions),
    3309         4646 :       gogo_(gogo)
    3310              :   { }
    3311              : 
    3312              :   int
    3313              :   function(Named_object*);
    3314              : 
    3315              :   int
    3316              :   expression(Expression**);
    3317              : 
    3318              :   static bool
    3319              :   skip_descriptor(Gogo* gogo, const Named_object*);
    3320              : 
    3321              :  private:
    3322              :   Gogo* gogo_;
    3323              : };
    3324              : 
    3325              : // Create a descriptor for every top-level exported function and every
    3326              : // function referenced by an inline function.
    3327              : 
    3328              : int
    3329       188603 : Create_function_descriptors::function(Named_object* no)
    3330              : {
    3331       188603 :   if (Create_function_descriptors::skip_descriptor(this->gogo_, no))
    3332              :     return TRAVERSE_CONTINUE;
    3333              : 
    3334       188603 :   if (no->is_function()
    3335       188603 :       && no->func_value()->enclosing() == NULL
    3336       164295 :       && !no->func_value()->is_method()
    3337       274853 :       && ((!Gogo::is_hidden_name(no->name())
    3338        44009 :            && !Gogo::is_thunk(no))
    3339        43547 :           || no->func_value()->is_referenced_by_inline()))
    3340        42703 :     no->func_value()->descriptor(this->gogo_, no);
    3341              : 
    3342              :   return TRAVERSE_CONTINUE;
    3343              : }
    3344              : 
    3345              : // If we see a function referenced in any way other than calling it,
    3346              : // create a descriptor for it.
    3347              : 
    3348              : int
    3349     10286949 : Create_function_descriptors::expression(Expression** pexpr)
    3350              : {
    3351     10286949 :   Expression* expr = *pexpr;
    3352              : 
    3353     10286949 :   Func_expression* fe = expr->func_expression();
    3354     10286949 :   if (fe != NULL)
    3355              :     {
    3356              :       // We would not get here for a call to this function, so this is
    3357              :       // a reference to a function other than calling it.  We need a
    3358              :       // descriptor.
    3359        46977 :       if (fe->closure() != NULL)
    3360              :         return TRAVERSE_CONTINUE;
    3361        37237 :       Named_object* no = fe->named_object();
    3362        37237 :       if (no->is_function() && !no->func_value()->is_method())
    3363        21527 :         no->func_value()->descriptor(this->gogo_, no);
    3364        15710 :       else if (no->is_function_declaration()
    3365        15710 :                && !no->func_declaration_value()->type()->is_method()
    3366        31420 :                && !Linemap::is_predeclared_location(no->location()))
    3367        14737 :         no->func_declaration_value()->descriptor(this->gogo_, no);
    3368        37237 :       return TRAVERSE_CONTINUE;
    3369              :     }
    3370              : 
    3371     10239972 :   Bound_method_expression* bme = expr->bound_method_expression();
    3372     10239972 :   if (bme != NULL)
    3373              :     {
    3374              :       // We would not get here for a call to this method, so this is a
    3375              :       // method value.  We need to create a thunk.
    3376          934 :       Bound_method_expression::create_thunk(this->gogo_, bme->method(),
    3377              :                                             bme->function());
    3378          934 :       return TRAVERSE_CONTINUE;
    3379              :     }
    3380              : 
    3381     10239038 :   Interface_field_reference_expression* ifre =
    3382     10239038 :     expr->interface_field_reference_expression();
    3383     10239038 :   if (ifre != NULL)
    3384              :     {
    3385              :       // We would not get here for a call to this interface method, so
    3386              :       // this is a method value.  We need to create a thunk.
    3387          105 :       Interface_type* type = ifre->expr()->type()->interface_type();
    3388          105 :       if (type != NULL)
    3389          105 :         Interface_field_reference_expression::create_thunk(this->gogo_, type,
    3390              :                                                            ifre->name());
    3391          105 :       return TRAVERSE_CONTINUE;
    3392              :     }
    3393              : 
    3394     10238933 :   Call_expression* ce = expr->call_expression();
    3395       816921 :   if (ce != NULL)
    3396              :     {
    3397       816921 :       Expression* fn = ce->fn();
    3398       816921 :       if (fn->func_expression() != NULL
    3399              :           || fn->bound_method_expression() != NULL
    3400              :           || fn->interface_field_reference_expression() != NULL)
    3401              :         {
    3402              :           // Traverse the arguments but not the function.
    3403       797443 :           Expression_list* args = ce->args();
    3404       797443 :           if (args != NULL)
    3405              :             {
    3406       740673 :               if (args->traverse(this) == TRAVERSE_EXIT)
    3407              :                 return TRAVERSE_EXIT;
    3408              :             }
    3409              : 
    3410              :           // Traverse the subexpressions of the function, if any.
    3411       797443 :           if (fn->traverse_subexpressions(this) == TRAVERSE_EXIT)
    3412              :             return TRAVERSE_EXIT;
    3413              : 
    3414              :           return TRAVERSE_SKIP_COMPONENTS;
    3415              :         }
    3416              :     }
    3417              : 
    3418              :   return TRAVERSE_CONTINUE;
    3419              : }
    3420              : 
    3421              : // The gc compiler has some special cases that it always compiles as
    3422              : // intrinsics.  For those we don't want to generate a function
    3423              : // descriptor, as there will be no code for it to refer to.
    3424              : 
    3425              : bool
    3426       189200 : Create_function_descriptors::skip_descriptor(Gogo* gogo,
    3427              :                                              const Named_object* no)
    3428              : {
    3429       189200 :   const std::string& pkgpath(no->package() == NULL
    3430       189200 :                              ? gogo->pkgpath()
    3431        12247 :                              : no->package()->pkgpath());
    3432              : 
    3433              :   // internal/abi is the standard library package,
    3434              :   // bootstrap/internal/abi is the name used when bootstrapping the gc
    3435              :   // compiler.
    3436              : 
    3437       189200 :   return ((pkgpath == "internal/abi"
    3438       189192 :            || pkgpath == "bootstrap/internal/abi")
    3439       189200 :           && (no->name() == "FuncPCABI0"
    3440            4 :               || no->name() == "FuncPCABIInternal"));
    3441              : }
    3442              : 
    3443              : // Create function descriptors as needed.  We need a function
    3444              : // descriptor for all exported functions and for all functions that
    3445              : // are referenced without being called.
    3446              : 
    3447              : void
    3448         4646 : Gogo::create_function_descriptors()
    3449              : {
    3450              :   // Create a function descriptor for any exported function that is
    3451              :   // declared in this package.  This is so that we have a descriptor
    3452              :   // for functions written in assembly.  Gather the descriptors first
    3453              :   // so that we don't add declarations while looping over them.
    3454         4646 :   std::vector<Named_object*> fndecls;
    3455         4646 :   Bindings* b = this->package_->bindings();
    3456       342111 :   for (Bindings::const_declarations_iterator p = b->begin_declarations();
    3457       342111 :        p != b->end_declarations();
    3458       337465 :        ++p)
    3459              :     {
    3460       337465 :       Named_object* no = p->second;
    3461       337465 :       if (no->is_function_declaration()
    3462         3904 :           && !no->func_declaration_value()->type()->is_method()
    3463         3904 :           && !Linemap::is_predeclared_location(no->location())
    3464         3904 :           && !Gogo::is_hidden_name(no->name())
    3465       338062 :           && !Create_function_descriptors::skip_descriptor(this, no))
    3466          589 :         fndecls.push_back(no);
    3467              :     }
    3468         5235 :   for (std::vector<Named_object*>::const_iterator p = fndecls.begin();
    3469         5235 :        p != fndecls.end();
    3470          589 :        ++p)
    3471          589 :     (*p)->func_declaration_value()->descriptor(this, *p);
    3472         4646 :   fndecls.clear();
    3473              : 
    3474         4646 :   Create_function_descriptors cfd(this);
    3475         4646 :   this->traverse(&cfd);
    3476         4646 : }
    3477              : 
    3478              : // Lower calls to builtin functions.  We need to do this early because
    3479              : // some builtin calls are constant expressions.  In particular we need
    3480              : // to do this before finalize_methods, because finalize_methods calls
    3481              : // is_direct_iface_type, which needs to know whether something like
    3482              : // [unsafe.Sizeof(byte(0))]*byte is a direct-iface type.
    3483              : 
    3484         9292 : class Lower_builtin_calls : public Traverse
    3485              : {
    3486              :  public:
    3487         4646 :   Lower_builtin_calls(Gogo* gogo)
    3488         4646 :     : Traverse(traverse_expressions),
    3489         4646 :       gogo_(gogo)
    3490              :   { }
    3491              : 
    3492              :   int
    3493              :   expression(Expression**);
    3494              : 
    3495              :  private:
    3496              :   Gogo* gogo_;
    3497              : };
    3498              : 
    3499              : int
    3500      9009908 : Lower_builtin_calls::expression(Expression** pexpr)
    3501              : {
    3502      9009908 :   Call_expression* ce = (*pexpr)->call_expression();
    3503       836475 :   if (ce != NULL)
    3504       836475 :     *pexpr = ce->lower_builtin(this->gogo_);
    3505      9009908 :   return TRAVERSE_CONTINUE;
    3506              : }
    3507              : 
    3508              : void
    3509         4646 : Gogo::lower_builtin_calls()
    3510              : {
    3511         4646 :   Lower_builtin_calls lbc(this);
    3512         4646 :   this->traverse(&lbc);
    3513         4646 : }
    3514              : 
    3515              : // Finalize the methods of an interface type.
    3516              : 
    3517              : int
    3518     25418891 : Finalize_methods::type(Type* t)
    3519              : {
    3520              :   // Check the classification so that we don't finalize the methods
    3521              :   // twice for a named interface type.
    3522     25418891 :   switch (t->classification())
    3523              :     {
    3524       272376 :     case Type::TYPE_INTERFACE:
    3525       272376 :       t->interface_type()->finalize_methods();
    3526       272376 :       break;
    3527              : 
    3528      2286587 :     case Type::TYPE_NAMED:
    3529      2286587 :       {
    3530      2286587 :         Named_type* nt = t->named_type();
    3531              : 
    3532      2286587 :         if (nt->is_alias())
    3533              :           return TRAVERSE_CONTINUE;
    3534              : 
    3535      2192698 :         Type* rt = nt->real_type();
    3536      2192698 :         if (rt->classification() != Type::TYPE_STRUCT)
    3537              :           {
    3538              :             // Finalize the methods of the real type first.
    3539       930790 :             if (Type::traverse(rt, this) == TRAVERSE_EXIT)
    3540              :               return TRAVERSE_EXIT;
    3541              : 
    3542              :             // Finalize the methods of this type.
    3543       930790 :             nt->finalize_methods(this->gogo_);
    3544              :           }
    3545              :         else
    3546              :           {
    3547              :             // We don't want to finalize the methods of a named struct
    3548              :             // type, as the methods should be attached to the named
    3549              :             // type, not the struct type.  We just want to finalize
    3550              :             // the field types.
    3551              :             //
    3552              :             // It is possible that a field type refers indirectly to
    3553              :             // this type, such as via a field with function type with
    3554              :             // an argument or result whose type is this type.  To
    3555              :             // avoid the cycle, first finalize the methods of any
    3556              :             // embedded types, which are the only types we need to
    3557              :             // know to finalize the methods of this type.
    3558      2523816 :             const Struct_field_list* fields = rt->struct_type()->fields();
    3559      1261908 :             if (fields != NULL)
    3560              :               {
    3561      8240766 :                 for (Struct_field_list::const_iterator pf = fields->begin();
    3562      8240766 :                      pf != fields->end();
    3563      6978858 :                      ++pf)
    3564              :                   {
    3565      6978858 :                     if (pf->is_anonymous())
    3566              :                       {
    3567       124573 :                         if (Type::traverse(pf->type(), this) == TRAVERSE_EXIT)
    3568            0 :                           return TRAVERSE_EXIT;
    3569              :                       }
    3570              :                   }
    3571              :               }
    3572              : 
    3573              :             // Finalize the methods of this type.
    3574      1261908 :             nt->finalize_methods(this->gogo_);
    3575              : 
    3576              :             // Finalize all the struct fields.
    3577      2523816 :             if (rt->struct_type()->traverse_field_types(this) == TRAVERSE_EXIT)
    3578              :               return TRAVERSE_EXIT;
    3579              :           }
    3580              : 
    3581              :         // If this type is defined in a different package, then finalize the
    3582              :         // types of all the methods, since we won't see them otherwise.
    3583      2192698 :         if (nt->named_object()->package() != NULL && nt->has_any_methods())
    3584              :           {
    3585      1041198 :             const Methods* methods = nt->methods();
    3586      8545555 :             for (Methods::const_iterator p = methods->begin();
    3587      8545555 :                  p != methods->end();
    3588      7504357 :                  ++p)
    3589              :               {
    3590      7504357 :                 if (Type::traverse(p->second->type(), this) == TRAVERSE_EXIT)
    3591            0 :                   return TRAVERSE_EXIT;
    3592              :               }
    3593              :           }
    3594              : 
    3595              :         return TRAVERSE_SKIP_COMPONENTS;
    3596              :       }
    3597              : 
    3598       119380 :     case Type::TYPE_STRUCT:
    3599              :       // Traverse the field types first in case there is an embedded
    3600              :       // field with methods that the struct should inherit.
    3601       238760 :       if (t->struct_type()->traverse_field_types(this) == TRAVERSE_EXIT)
    3602              :           return TRAVERSE_EXIT;
    3603       119380 :       t->struct_type()->finalize_methods(this->gogo_);
    3604       119380 :       return TRAVERSE_SKIP_COMPONENTS;
    3605              : 
    3606              :     default:
    3607              :       break;
    3608              :     }
    3609              : 
    3610              :   return TRAVERSE_CONTINUE;
    3611              : }
    3612              : 
    3613              : // Finalize method lists and build stub methods for types.
    3614              : 
    3615              : void
    3616         4646 : Gogo::finalize_methods()
    3617              : {
    3618         4646 :   Finalize_methods finalize(this);
    3619         4646 :   this->traverse(&finalize);
    3620         4646 : }
    3621              : 
    3622              : // Finalize the method list for a type.  This is called when a type is
    3623              : // parsed for an inlined function body, which happens after the
    3624              : // finalize_methods pass.
    3625              : 
    3626              : void
    3627          563 : Gogo::finalize_methods_for_type(Type* type)
    3628              : {
    3629          563 :   Finalize_methods finalize(this);
    3630          563 :   Type::traverse(type, &finalize);
    3631          563 : }
    3632              : 
    3633              : // Set types for unspecified variables and constants.
    3634              : 
    3635              : void
    3636         4646 : Gogo::determine_types()
    3637              : {
    3638         4646 :   this->current_bindings()->determine_types(this);
    3639              : 
    3640              :   // Determine the types of constants in packages.
    3641       120891 :   for (Packages::const_iterator p = this->packages_.begin();
    3642       120891 :        p != this->packages_.end();
    3643       116245 :        ++p)
    3644       116245 :     p->second->determine_types(this);
    3645         4646 : }
    3646              : 
    3647              : // Traversal class used for type checking.
    3648              : 
    3649         4646 : class Check_types_traverse : public Traverse
    3650              : {
    3651              :  public:
    3652        18287 :   Check_types_traverse(Gogo* gogo)
    3653        18287 :     : Traverse(traverse_variables
    3654              :                | traverse_constants
    3655              :                | traverse_functions
    3656              :                | traverse_statements
    3657              :                | traverse_expressions),
    3658        18287 :       gogo_(gogo)
    3659              :   { }
    3660              : 
    3661              :   int
    3662              :   variable(Named_object*);
    3663              : 
    3664              :   int
    3665              :   constant(Named_object*, bool);
    3666              : 
    3667              :   int
    3668              :   function(Named_object*);
    3669              : 
    3670              :   int
    3671              :   statement(Block*, size_t* pindex, Statement*);
    3672              : 
    3673              :   int
    3674              :   expression(Expression**);
    3675              : 
    3676              :  private:
    3677              :   // General IR.
    3678              :   Gogo* gogo_;
    3679              : };
    3680              : 
    3681              : // Check that a variable initializer has the right type.
    3682              : 
    3683              : int
    3684      1116170 : Check_types_traverse::variable(Named_object* named_object)
    3685              : {
    3686      1116170 :   if (named_object->is_variable())
    3687              :     {
    3688       984741 :       Variable* var = named_object->var_value();
    3689              : 
    3690              :       // Give error if variable type is not defined.
    3691       984741 :       var->type()->base();
    3692              : 
    3693       984741 :       Expression* init = var->init();
    3694       984741 :       std::string reason;
    3695       984741 :       if (init != NULL
    3696      1283553 :           && !Type::are_assignable(var->type(), init->type(), &reason))
    3697              :         {
    3698           45 :           if (reason.empty())
    3699            0 :             go_error_at(var->location(), "incompatible type in initialization");
    3700              :           else
    3701           45 :             go_error_at(var->location(),
    3702              :                         "incompatible type in initialization (%s)",
    3703              :                         reason.c_str());
    3704           45 :           init = Expression::make_error(named_object->location());
    3705           45 :           var->clear_init();
    3706              :         }
    3707       984696 :       else if (init != NULL
    3708       984696 :                && init->func_expression() != NULL)
    3709              :         {
    3710         3219 :           Named_object* no = init->func_expression()->named_object();
    3711         3219 :           Function_type* fntype;
    3712         3219 :           if (no->is_function())
    3713         3047 :             fntype = no->func_value()->type();
    3714          172 :           else if (no->is_function_declaration())
    3715          172 :             fntype = no->func_declaration_value()->type();
    3716              :           else
    3717            0 :             go_unreachable();
    3718              : 
    3719              :           // Builtin functions cannot be used as function values for variable
    3720              :           // initialization.
    3721         3219 :           if (fntype->is_builtin())
    3722              :             {
    3723            1 :               go_error_at(init->location(),
    3724              :                           "invalid use of special built-in function %qs; "
    3725              :                           "must be called",
    3726            2 :                           no->message_name().c_str());
    3727              :             }
    3728              :         }
    3729              : 
    3730       984741 :       if (!var->is_used()
    3731       368942 :           && !var->is_global()
    3732        51096 :           && !var->is_parameter()
    3733           35 :           && !var->is_receiver()
    3734           35 :           && !var->type()->is_error()
    3735           13 :           && (init == NULL || !init->is_error_expression())
    3736       984749 :           && !Lex::is_invalid_identifier(named_object->name()))
    3737              :         {
    3738              :           // Avoid giving an error if the initializer is invalid.
    3739            7 :           if (init != NULL)
    3740            6 :             init->check_types(this->gogo_);
    3741              : 
    3742            6 :           if (init == NULL || !init->is_error_expression())
    3743            6 :             go_error_at(var->location(), "%qs declared but not used",
    3744           12 :                         named_object->message_name().c_str());
    3745              :         }
    3746       984741 :     }
    3747      1116170 :   return TRAVERSE_CONTINUE;
    3748              : }
    3749              : 
    3750              : // Check that a constant initializer has the right type.
    3751              : 
    3752              : int
    3753      1014050 : Check_types_traverse::constant(Named_object* named_object, bool)
    3754              : {
    3755      1014050 :   Named_constant* constant = named_object->const_value();
    3756      1014050 :   Type* ctype = constant->type();
    3757      1047673 :   if (ctype->integer_type() == NULL
    3758      1041246 :       && ctype->float_type() == NULL
    3759      1041202 :       && ctype->complex_type() == NULL
    3760        27177 :       && !ctype->is_boolean_type()
    3761        25650 :       && !ctype->is_string_type())
    3762              :     {
    3763           25 :       if (ctype->is_nil_type())
    3764            1 :         go_error_at(constant->location(), "const initializer cannot be nil");
    3765           24 :       else if (!ctype->is_error())
    3766           15 :         go_error_at(constant->location(), "invalid constant type");
    3767           25 :       constant->set_error();
    3768              :     }
    3769      1014025 :   else if (constant->expr()->is_error_expression())
    3770              :     {
    3771            3 :       go_assert(saw_errors());
    3772            3 :       constant->set_error();
    3773              :     }
    3774      1014022 :   else if (!constant->expr()->is_constant())
    3775              :     {
    3776           12 :       go_error_at(constant->expr()->location(), "expression is not constant");
    3777           12 :       constant->set_error();
    3778              :     }
    3779      1014010 :   else if (!Type::are_assignable(constant->type(), constant->expr()->type(),
    3780              :                                  NULL))
    3781              :     {
    3782            1 :       go_error_at(constant->location(),
    3783              :                   "initialization expression has wrong type");
    3784            1 :       constant->set_error();
    3785              :     }
    3786      1014050 :   return TRAVERSE_CONTINUE;
    3787              : }
    3788              : 
    3789              : // There are no types to check in a function, but this is where we
    3790              : // issue warnings about labels which are defined but not referenced.
    3791              : 
    3792              : int
    3793       175575 : Check_types_traverse::function(Named_object* no)
    3794              : {
    3795       175575 :   no->func_value()->check_labels();
    3796       175575 :   return TRAVERSE_CONTINUE;
    3797              : }
    3798              : 
    3799              : // Check that types are valid in a statement.
    3800              : 
    3801              : int
    3802      1852286 : Check_types_traverse::statement(Block*, size_t*, Statement* s)
    3803              : {
    3804      1852286 :   s->check_types(this->gogo_);
    3805      1852286 :   return TRAVERSE_CONTINUE;
    3806              : }
    3807              : 
    3808              : // Check that types are valid in an expression.
    3809              : 
    3810              : int
    3811      9641328 : Check_types_traverse::expression(Expression** expr)
    3812              : {
    3813      9641328 :   (*expr)->check_types(this->gogo_);
    3814      9641328 :   return TRAVERSE_CONTINUE;
    3815              : }
    3816              : 
    3817              : // Check that types are valid.
    3818              : 
    3819              : void
    3820         4646 : Gogo::check_types()
    3821              : {
    3822         4646 :   Check_types_traverse traverse(this);
    3823         4646 :   this->traverse(&traverse);
    3824              : 
    3825         4646 :   Bindings* bindings = this->current_bindings();
    3826       342038 :   for (Bindings::const_declarations_iterator p = bindings->begin_declarations();
    3827       342038 :        p != bindings->end_declarations();
    3828       337392 :        ++p)
    3829              :     {
    3830              :       // Also check the types in a function declaration's signature.
    3831       337392 :       Named_object* no = p->second;
    3832       337392 :       if (no->is_function_declaration())
    3833         3904 :         no->func_declaration_value()->check_types();
    3834              :     }
    3835         4646 : }
    3836              : 
    3837              : // Check the types in a single block.
    3838              : 
    3839              : void
    3840        13641 : Gogo::check_types_in_block(Block* block)
    3841              : {
    3842        13641 :   Check_types_traverse traverse(this);
    3843        13641 :   block->traverse(&traverse);
    3844        13641 : }
    3845              : 
    3846              : // For each global variable defined in the current package, record the
    3847              : // set of variables that its initializer depends on.  We do this after
    3848              : // lowering so that all unknown names are resolved to their final
    3849              : // locations.  We do this before write barrier insertion because that
    3850              : // makes it harder to distinguish references from assignments in
    3851              : // preinit blocks.
    3852              : 
    3853              : void
    3854         4646 : Gogo::record_global_init_refs()
    3855              : {
    3856         4646 :   Bindings* bindings = this->package_->bindings();
    3857       458695 :   for (Bindings::const_definitions_iterator pb = bindings->begin_definitions();
    3858       458695 :        pb != bindings->end_definitions();
    3859       454049 :        pb++)
    3860              :     {
    3861       454049 :       Named_object* no = *pb;
    3862       454049 :       if (!no->is_variable())
    3863       417348 :         continue;
    3864              : 
    3865        36701 :       Variable* var = no->var_value();
    3866        36701 :       go_assert(var->is_global());
    3867              : 
    3868        36701 :       Find_vars find_vars;
    3869        36701 :       Expression* init = var->init();
    3870        36701 :       if (init != NULL)
    3871        20356 :         Expression::traverse(&init, &find_vars);
    3872        36701 :       if (var->has_pre_init())
    3873          121 :         var->preinit()->traverse(&find_vars);
    3874        36701 :       Named_object* dep = this->var_depends_on(var);
    3875        36701 :       if (dep != NULL)
    3876              :         {
    3877           99 :           Expression* dinit = dep->var_value()->init();
    3878           99 :           if (dinit != NULL)
    3879           95 :             Expression::traverse(&dinit, &find_vars);
    3880           99 :           if (dep->var_value()->has_pre_init())
    3881           99 :             dep->var_value()->preinit()->traverse(&find_vars);
    3882              :         }
    3883              : 
    3884        48351 :       for (Find_vars::const_iterator pv = find_vars.begin();
    3885        48351 :            pv != find_vars.end();
    3886        11650 :            ++pv)
    3887        11650 :         var->add_init_ref(*pv);
    3888        36701 :     }
    3889         4646 : }
    3890              : 
    3891              : // A traversal class which finds all the expressions which must be
    3892              : // evaluated in order within a statement or larger expression.  This
    3893              : // is used to implement the rules about order of evaluation.
    3894              : 
    3895              : class Find_eval_ordering : public Traverse
    3896              : {
    3897              :  private:
    3898              :   typedef std::vector<Expression**> Expression_pointers;
    3899              : 
    3900              :  public:
    3901      4734399 :   Find_eval_ordering()
    3902              :     : Traverse(traverse_blocks
    3903              :                | traverse_statements
    3904              :                | traverse_expressions),
    3905      4734399 :       exprs_()
    3906              :   { }
    3907              : 
    3908              :   size_t
    3909      4632515 :   size() const
    3910      9265030 :   { return this->exprs_.size(); }
    3911              : 
    3912              :   typedef Expression_pointers::const_iterator const_iterator;
    3913              : 
    3914              :   const_iterator
    3915       427177 :   begin() const
    3916       427177 :   { return this->exprs_.begin(); }
    3917              : 
    3918              :   const_iterator
    3919      1102848 :   end() const
    3920          711 :   { return this->exprs_.end(); }
    3921              : 
    3922              :  protected:
    3923              :   int
    3924      1683357 :   block(Block*)
    3925      1683357 :   { return TRAVERSE_SKIP_COMPONENTS; }
    3926              : 
    3927              :   int
    3928            0 :   statement(Block*, size_t*, Statement*)
    3929            0 :   { return TRAVERSE_SKIP_COMPONENTS; }
    3930              : 
    3931              :   int
    3932              :   expression(Expression**);
    3933              : 
    3934              :  private:
    3935              :   // A list of pointers to expressions with side-effects.
    3936              :   Expression_pointers exprs_;
    3937              : };
    3938              : 
    3939              : // If an expression must be evaluated in order, put it on the list.
    3940              : 
    3941              : int
    3942     12441152 : Find_eval_ordering::expression(Expression** expression_pointer)
    3943              : {
    3944     12441152 :   Binary_expression* binexp = (*expression_pointer)->binary_expression();
    3945       740142 :   if (binexp != NULL
    3946       740142 :       && (binexp->op() == OPERATOR_ANDAND || binexp->op() == OPERATOR_OROR))
    3947              :     {
    3948              :       // Shortcut expressions may potentially have side effects which need
    3949              :       // to be ordered, so add them to the list.
    3950              :       // We don't order its subexpressions here since they may be evaluated
    3951              :       // conditionally. This is handled in remove_shortcuts.
    3952        86545 :       this->exprs_.push_back(expression_pointer);
    3953        86545 :       return TRAVERSE_SKIP_COMPONENTS;
    3954              :     }
    3955              : 
    3956              :   // We have to look at subexpressions before this one.
    3957     12354607 :   if ((*expression_pointer)->traverse_subexpressions(this) == TRAVERSE_EXIT)
    3958              :     return TRAVERSE_EXIT;
    3959     12354607 :   if ((*expression_pointer)->must_eval_in_order())
    3960       899159 :     this->exprs_.push_back(expression_pointer);
    3961              :   return TRAVERSE_SKIP_COMPONENTS;
    3962              : }
    3963              : 
    3964              : // A traversal class for ordering evaluations.
    3965              : 
    3966       368002 : class Order_eval : public Traverse
    3967              : {
    3968              :  public:
    3969       184001 :   Order_eval(Gogo* gogo)
    3970       184001 :     : Traverse(traverse_variables
    3971              :                | traverse_statements),
    3972       184001 :       gogo_(gogo)
    3973              :   { }
    3974              : 
    3975              :   int
    3976              :   variable(Named_object*);
    3977              : 
    3978              :   int
    3979              :   statement(Block*, size_t*, Statement*);
    3980              : 
    3981              :  private:
    3982              :   // The IR.
    3983              :   Gogo* gogo_;
    3984              : };
    3985              : 
    3986              : // Implement the order of evaluation rules for a statement.
    3987              : 
    3988              : int
    3989      4714043 : Order_eval::statement(Block* block, size_t* pindex, Statement* stmt)
    3990              : {
    3991              :   // FIXME: This approach doesn't work for switch statements, because
    3992              :   // we add the new statements before the whole switch when we need to
    3993              :   // instead add them just before the switch expression.  The right
    3994              :   // fix is probably to lower switch statements with nonconstant cases
    3995              :   // to a series of conditionals.
    3996      4714043 :   if (stmt->switch_statement() != NULL)
    3997              :     return TRAVERSE_CONTINUE;
    3998              : 
    3999      4714043 :   Find_eval_ordering find_eval_ordering;
    4000              : 
    4001              :   // If S is a variable declaration, then ordinary traversal won't do
    4002              :   // anything.  We want to explicitly traverse the initialization
    4003              :   // expression if there is one.
    4004      4714043 :   Variable_declaration_statement* vds = stmt->variable_declaration_statement();
    4005      4714043 :   Expression* init = NULL;
    4006      4714043 :   Expression* orig_init = NULL;
    4007      4714043 :   if (vds == NULL)
    4008      4331413 :     stmt->traverse_contents(&find_eval_ordering);
    4009              :   else
    4010              :     {
    4011       382630 :       init = vds->var()->var_value()->init();
    4012       382630 :       if (init == NULL)
    4013              :         return TRAVERSE_CONTINUE;
    4014       280746 :       orig_init = init;
    4015              : 
    4016              :       // It might seem that this could be
    4017              :       // init->traverse_subexpressions.  Unfortunately that can fail
    4018              :       // in a case like
    4019              :       //   var err os.Error
    4020              :       //   newvar, err := call(arg())
    4021              :       // Here newvar will have an init of call result 0 of
    4022              :       // call(arg()).  If we only traverse subexpressions, we will
    4023              :       // only find arg(), and we won't bother to move anything out.
    4024              :       // Then we get to the assignment to err, we will traverse the
    4025              :       // whole statement, and this time we will find both call() and
    4026              :       // arg(), and so we will move them out.  This will cause them to
    4027              :       // be put into temporary variables before the assignment to err
    4028              :       // but after the declaration of newvar.  To avoid that problem,
    4029              :       // we traverse the entire expression here.
    4030       280746 :       Expression::traverse(&init, &find_eval_ordering);
    4031              :     }
    4032              : 
    4033      4612159 :   size_t c = find_eval_ordering.size();
    4034      4612159 :   if (c == 0)
    4035              :     return TRAVERSE_CONTINUE;
    4036              : 
    4037              :   // If there is only one expression with a side-effect, we can
    4038              :   // usually leave it in place.
    4039       732622 :   if (c == 1)
    4040              :     {
    4041       516834 :       switch (stmt->classification())
    4042              :         {
    4043              :         case Statement::STATEMENT_ASSIGNMENT:
    4044              :           // For an assignment statement, we need to evaluate an
    4045              :           // expression on the right hand side before we evaluate any
    4046              :           // index expression on the left hand side, so for that case
    4047              :           // we always move the expression.  Otherwise we mishandle
    4048              :           // m[0] = len(m) where m is a map.
    4049              :           break;
    4050              : 
    4051        49625 :         case Statement::STATEMENT_EXPRESSION:
    4052        49625 :           {
    4053              :             // If this is a call statement that doesn't return any
    4054              :             // values, it will not have been counted as a value to
    4055              :             // move.  We need to move any subexpressions in case they
    4056              :             // are themselves call statements that require passing a
    4057              :             // closure.
    4058        49625 :             Expression* expr = stmt->expression_statement()->expr();
    4059        49625 :             if (expr->call_expression() != NULL
    4060        48371 :                 && expr->call_expression()->result_count() == 0)
    4061              :               break;
    4062        37239 :             return TRAVERSE_CONTINUE;
    4063              :           }
    4064              : 
    4065              :         default:
    4066              :           // We can leave the expression in place.
    4067              :           return TRAVERSE_CONTINUE;
    4068              :         }
    4069              :     }
    4070              : 
    4071       426116 :   bool is_thunk = stmt->thunk_statement() != NULL;
    4072       426116 :   Expression_statement* es = stmt->expression_statement();
    4073      1091959 :   for (Find_eval_ordering::const_iterator p = find_eval_ordering.begin();
    4074      1091959 :        p != find_eval_ordering.end();
    4075       665843 :        ++p)
    4076              :     {
    4077       666177 :       Expression** pexpr = *p;
    4078              : 
    4079              :       // The last expression in a thunk will be the call passed to go
    4080              :       // or defer, which we must not evaluate early.
    4081       666177 :       if (is_thunk && p + 1 == find_eval_ordering.end())
    4082              :         break;
    4083              : 
    4084       665843 :       Location loc = (*pexpr)->location();
    4085       665843 :       Statement* s;
    4086       665843 :       if ((*pexpr)->call_expression() == NULL
    4087       470782 :           || (*pexpr)->call_expression()->result_count() < 2)
    4088              :         {
    4089       495596 :           Temporary_statement* ts = Statement::make_temporary(NULL, *pexpr,
    4090              :                                                               loc);
    4091       495596 :           s = ts;
    4092       495596 :           *pexpr = Expression::make_temporary_reference(ts, loc);
    4093              :         }
    4094              :       else
    4095              :         {
    4096              :           // A call expression which returns multiple results needs to
    4097              :           // be handled specially.  We can't create a temporary
    4098              :           // because there is no type to give it.  Any actual uses of
    4099              :           // the values will be done via Call_result_expressions.
    4100              :           //
    4101              :           // Since a given call expression can be shared by multiple
    4102              :           // Call_result_expressions, avoid hoisting the call the
    4103              :           // second time we see it here. In addition, don't try to
    4104              :           // hoist the top-level multi-return call in the statement,
    4105              :           // since doing this would result a tree with more than one copy
    4106              :           // of the call.
    4107       170247 :           if (this->remember_expression(*pexpr))
    4108              :             s = NULL;
    4109        81997 :           else if (es != NULL && *pexpr == es->expr())
    4110              :             s = NULL;
    4111              :           else
    4112        78880 :             s = Statement::make_statement(*pexpr, true);
    4113              :         }
    4114              : 
    4115       574476 :       if (s != NULL)
    4116              :         {
    4117       574476 :           block->insert_statement_before(*pindex, s);
    4118       574476 :           ++*pindex;
    4119              :         }
    4120              :     }
    4121              : 
    4122       426116 :   if (init != orig_init)
    4123        81272 :     vds->var()->var_value()->set_init(init);
    4124              : 
    4125              :   return TRAVERSE_CONTINUE;
    4126      4714043 : }
    4127              : 
    4128              : // Implement the order of evaluation rules for the initializer of a
    4129              : // global variable.
    4130              : 
    4131              : int
    4132      1148735 : Order_eval::variable(Named_object* no)
    4133              : {
    4134      1148735 :   if (no->is_result_variable())
    4135              :     return TRAVERSE_CONTINUE;
    4136      1005419 :   Variable* var = no->var_value();
    4137      1005419 :   Expression* init = var->init();
    4138      1005419 :   if (!var->is_global() || init == NULL)
    4139              :     return TRAVERSE_CONTINUE;
    4140              : 
    4141        20356 :   Find_eval_ordering find_eval_ordering;
    4142        20356 :   Expression::traverse(&init, &find_eval_ordering);
    4143              : 
    4144        20356 :   if (find_eval_ordering.size() <= 1)
    4145              :     {
    4146              :       // If there is only one expression with a side-effect, we can
    4147              :       // leave it in place.
    4148              :       return TRAVERSE_SKIP_COMPONENTS;
    4149              :     }
    4150              : 
    4151         1061 :   Expression* orig_init = init;
    4152              : 
    4153        10178 :   for (Find_eval_ordering::const_iterator p = find_eval_ordering.begin();
    4154        10178 :        p != find_eval_ordering.end();
    4155         9117 :        ++p)
    4156              :     {
    4157         9117 :       Expression** pexpr = *p;
    4158         9117 :       Location loc = (*pexpr)->location();
    4159         9117 :       Statement* s;
    4160         9117 :       if ((*pexpr)->call_expression() == NULL
    4161         8906 :           || (*pexpr)->call_expression()->result_count() < 2)
    4162              :         {
    4163         8923 :           Temporary_statement* ts = Statement::make_temporary(NULL, *pexpr,
    4164              :                                                               loc);
    4165         8923 :           s = ts;
    4166         8923 :           *pexpr = Expression::make_temporary_reference(ts, loc);
    4167              :         }
    4168              :       else
    4169              :         {
    4170              :           // A call expression which returns multiple results needs to
    4171              :           // be handled specially.
    4172          194 :           s = Statement::make_statement(*pexpr, true);
    4173              :         }
    4174         9117 :       var->add_preinit_statement(this->gogo_, s);
    4175              :     }
    4176              : 
    4177         1061 :   if (init != orig_init)
    4178          448 :     var->set_init(init);
    4179              : 
    4180              :   return TRAVERSE_SKIP_COMPONENTS;
    4181        20356 : }
    4182              : 
    4183              : // Use temporary variables to implement the order of evaluation rules.
    4184              : 
    4185              : void
    4186         4646 : Gogo::order_evaluations()
    4187              : {
    4188         4646 :   Order_eval order_eval(this);
    4189         4646 :   this->traverse(&order_eval);
    4190         4646 : }
    4191              : 
    4192              : // Order evaluations in a block.
    4193              : 
    4194              : void
    4195        92810 : Gogo::order_block(Block* block)
    4196              : {
    4197        92810 :   Order_eval order_eval(this);
    4198        92810 :   block->traverse(&order_eval);
    4199        92810 : }
    4200              : 
    4201              : // A traversal class used to find a single shortcut operator within an
    4202              : // expression.
    4203              : 
    4204      5384618 : class Find_shortcut : public Traverse
    4205              : {
    4206              :  public:
    4207      5404974 :   Find_shortcut()
    4208      5404974 :     : Traverse(traverse_blocks
    4209              :                | traverse_statements
    4210              :                | traverse_expressions),
    4211      5404974 :       found_(NULL)
    4212              :   { }
    4213              : 
    4214              :   // A pointer to the expression which was found, or NULL if none was
    4215              :   // found.
    4216              :   Expression**
    4217      5303090 :   found() const
    4218      5303090 :   { return this->found_; }
    4219              : 
    4220              :  protected:
    4221              :   int
    4222      1683503 :   block(Block*)
    4223      1683503 :   { return TRAVERSE_SKIP_COMPONENTS; }
    4224              : 
    4225              :   int
    4226            0 :   statement(Block*, size_t*, Statement*)
    4227            0 :   { return TRAVERSE_SKIP_COMPONENTS; }
    4228              : 
    4229              :   int
    4230              :   expression(Expression**);
    4231              : 
    4232              :  private:
    4233              :   Expression** found_;
    4234              : };
    4235              : 
    4236              : // Find a shortcut expression.
    4237              : 
    4238              : int
    4239     13602502 : Find_shortcut::expression(Expression** pexpr)
    4240              : {
    4241     13602502 :   Expression* expr = *pexpr;
    4242     13602502 :   Binary_expression* be = expr->binary_expression();
    4243       744759 :   if (be == NULL)
    4244              :     return TRAVERSE_CONTINUE;
    4245       744759 :   Operator op = be->op();
    4246       744759 :   if (op != OPERATOR_OROR && op != OPERATOR_ANDAND)
    4247              :     return TRAVERSE_CONTINUE;
    4248        86545 :   go_assert(this->found_ == NULL);
    4249        86545 :   this->found_ = pexpr;
    4250        86545 :   return TRAVERSE_EXIT;
    4251              : }
    4252              : 
    4253              : // A traversal class used to turn shortcut operators into explicit if
    4254              : // statements.
    4255              : 
    4256       368002 : class Shortcuts : public Traverse
    4257              : {
    4258              :  public:
    4259       184001 :   Shortcuts(Gogo* gogo)
    4260       184001 :     : Traverse(traverse_variables
    4261              :                | traverse_statements),
    4262       184001 :       gogo_(gogo)
    4263              :   { }
    4264              : 
    4265              :  protected:
    4266              :   int
    4267              :   variable(Named_object*);
    4268              : 
    4269              :   int
    4270              :   statement(Block*, size_t*, Statement*);
    4271              : 
    4272              :  private:
    4273              :   // Convert a shortcut operator.
    4274              :   Statement*
    4275              :   convert_shortcut(Block* enclosing, Expression** pshortcut);
    4276              : 
    4277              :   // The IR.
    4278              :   Gogo* gogo_;
    4279              : };
    4280              : 
    4281              : // Remove shortcut operators in a single statement.
    4282              : 
    4283              : int
    4284      5298073 : Shortcuts::statement(Block* block, size_t* pindex, Statement* s)
    4285              : {
    4286              :   // FIXME: This approach doesn't work for switch statements, because
    4287              :   // we add the new statements before the whole switch when we need to
    4288              :   // instead add them just before the switch expression.  The right
    4289              :   // fix is probably to lower switch statements with nonconstant cases
    4290              :   // to a series of conditionals.
    4291      5298073 :   if (s->switch_statement() != NULL)
    4292              :     return TRAVERSE_CONTINUE;
    4293              : 
    4294      5471065 :   while (true)
    4295              :     {
    4296      5384569 :       Find_shortcut find_shortcut;
    4297              : 
    4298              :       // If S is a variable declaration, then ordinary traversal won't
    4299              :       // do anything.  We want to explicitly traverse the
    4300              :       // initialization expression if there is one.
    4301      5384569 :       Variable_declaration_statement* vds = s->variable_declaration_statement();
    4302      5384569 :       Expression* init = NULL;
    4303      5384569 :       if (vds == NULL)
    4304      5001306 :         s->traverse_contents(&find_shortcut);
    4305              :       else
    4306              :         {
    4307       383263 :           init = vds->var()->var_value()->init();
    4308       383263 :           if (init == NULL)
    4309              :             return TRAVERSE_CONTINUE;
    4310       281379 :           init->traverse(&init, &find_shortcut);
    4311              :         }
    4312      5282685 :       Expression** pshortcut = find_shortcut.found();
    4313      5282685 :       if (pshortcut == NULL)
    4314              :         return TRAVERSE_CONTINUE;
    4315              : 
    4316        86496 :       Statement* snew = this->convert_shortcut(block, pshortcut);
    4317        86496 :       block->insert_statement_before(*pindex, snew);
    4318        86496 :       ++*pindex;
    4319              : 
    4320        86496 :       if (pshortcut == &init)
    4321          612 :         vds->var()->var_value()->set_init(init);
    4322      5384569 :     }
    4323              : }
    4324              : 
    4325              : // Remove shortcut operators in the initializer of a global variable.
    4326              : 
    4327              : int
    4328      1148735 : Shortcuts::variable(Named_object* no)
    4329              : {
    4330      1148735 :   if (no->is_result_variable())
    4331              :     return TRAVERSE_CONTINUE;
    4332      1005419 :   Variable* var = no->var_value();
    4333      1005419 :   Expression* init = var->init();
    4334      1005419 :   if (!var->is_global() || init == NULL)
    4335              :     return TRAVERSE_CONTINUE;
    4336              : 
    4337        20454 :   while (true)
    4338              :     {
    4339        20405 :       Find_shortcut find_shortcut;
    4340        20405 :       init->traverse(&init, &find_shortcut);
    4341        20405 :       Expression** pshortcut = find_shortcut.found();
    4342        20405 :       if (pshortcut == NULL)
    4343        20356 :         return TRAVERSE_CONTINUE;
    4344              : 
    4345           49 :       Statement* snew = this->convert_shortcut(NULL, pshortcut);
    4346           49 :       var->add_preinit_statement(this->gogo_, snew);
    4347           49 :       if (pshortcut == &init)
    4348           49 :         var->set_init(init);
    4349        20405 :     }
    4350              : }
    4351              : 
    4352              : // Given an expression which uses a shortcut operator, return a
    4353              : // statement which implements it, and update *PSHORTCUT accordingly.
    4354              : 
    4355              : Statement*
    4356        86545 : Shortcuts::convert_shortcut(Block* enclosing, Expression** pshortcut)
    4357              : {
    4358        86545 :   Binary_expression* shortcut = (*pshortcut)->binary_expression();
    4359        86545 :   Expression* left = shortcut->left();
    4360        86545 :   Expression* right = shortcut->right();
    4361        86545 :   Location loc = shortcut->location();
    4362              : 
    4363        86545 :   Block* retblock = new Block(enclosing, loc);
    4364        86545 :   retblock->set_end_location(loc);
    4365              : 
    4366        86545 :   Temporary_statement* ts = Statement::make_temporary(shortcut->type(),
    4367              :                                                       left, loc);
    4368        86545 :   retblock->add_statement(ts);
    4369              : 
    4370        86545 :   Block* block = new Block(retblock, loc);
    4371        86545 :   block->set_end_location(loc);
    4372        86545 :   Expression* tmpref = Expression::make_temporary_reference(ts, loc);
    4373        86545 :   Statement* assign = Statement::make_assignment(tmpref, right, loc);
    4374        86545 :   block->add_statement(assign);
    4375              : 
    4376        86545 :   Expression* cond = Expression::make_temporary_reference(ts, loc);
    4377        86545 :   if (shortcut->binary_expression()->op() == OPERATOR_OROR)
    4378        30643 :     cond = Expression::make_unary(OPERATOR_NOT, cond, loc);
    4379              : 
    4380        86545 :   Statement* if_statement = Statement::make_if_statement(cond, block, NULL,
    4381              :                                                          loc);
    4382        86545 :   if_statement->determine_types(this->gogo_);
    4383        86545 :   retblock->add_statement(if_statement);
    4384              : 
    4385        86545 :   *pshortcut = Expression::make_temporary_reference(ts, loc);
    4386              : 
    4387        86545 :   delete shortcut;
    4388              : 
    4389              :   // Now convert any shortcut operators in LEFT and RIGHT.
    4390              :   // LEFT and RIGHT were skipped in the top level
    4391              :   // Gogo::order_evaluations. We need to order their
    4392              :   // components first.
    4393        86545 :   Order_eval order_eval(this->gogo_);
    4394        86545 :   retblock->traverse(&order_eval);
    4395        86545 :   Shortcuts shortcuts(this->gogo_);
    4396        86545 :   retblock->traverse(&shortcuts);
    4397              : 
    4398        86545 :   return Statement::make_block_statement(retblock, loc);
    4399        86545 : }
    4400              : 
    4401              : // Turn shortcut operators into explicit if statements.  Doing this
    4402              : // considerably simplifies the order of evaluation rules.
    4403              : 
    4404              : void
    4405         4646 : Gogo::remove_shortcuts()
    4406              : {
    4407         4646 :   Shortcuts shortcuts(this);
    4408         4646 :   this->traverse(&shortcuts);
    4409         4646 : }
    4410              : 
    4411              : // Turn shortcut operators into explicit if statements in a block.
    4412              : 
    4413              : void
    4414        92810 : Gogo::remove_shortcuts_in_block(Block* block)
    4415              : {
    4416        92810 :   Shortcuts shortcuts(this);
    4417        92810 :   block->traverse(&shortcuts);
    4418        92810 : }
    4419              : 
    4420              : // Traversal to flatten parse tree after order of evaluation rules are applied.
    4421              : 
    4422       927220 : class Flatten : public Traverse
    4423              : {
    4424              :  public:
    4425       463610 :   Flatten(Gogo* gogo, Named_object* function)
    4426              :     : Traverse(traverse_variables
    4427              :                | traverse_functions
    4428              :                | traverse_statements
    4429              :                | traverse_expressions),
    4430       463610 :       gogo_(gogo), function_(function), inserter_()
    4431              :   { }
    4432              : 
    4433              :   void
    4434       445323 :   set_inserter(const Statement_inserter* inserter)
    4435       445323 :   { this->inserter_ = *inserter; }
    4436              : 
    4437              :   int
    4438              :   variable(Named_object*);
    4439              : 
    4440              :   int
    4441              :   function(Named_object*);
    4442              : 
    4443              :   int
    4444              :   statement(Block*, size_t* pindex, Statement*);
    4445              : 
    4446              :   int
    4447              :   expression(Expression**);
    4448              : 
    4449              :  private:
    4450              :   // General IR.
    4451              :   Gogo* gogo_;
    4452              :   // The function we are traversing.
    4453              :   Named_object* function_;
    4454              :   // Current statement inserter for use by expressions.
    4455              :   Statement_inserter inserter_;
    4456              : };
    4457              : 
    4458              : // Flatten variables.
    4459              : 
    4460              : int
    4461      1469839 : Flatten::variable(Named_object* no)
    4462              : {
    4463      1469839 :   if (!no->is_variable())
    4464              :     return TRAVERSE_CONTINUE;
    4465              : 
    4466      1224943 :   if (no->is_variable() && no->var_value()->is_global())
    4467              :     {
    4468              :       // Global variables can have loops in their initialization
    4469              :       // expressions.  This is handled in flatten_init_expression.
    4470       348764 :       no->var_value()->flatten_init_expression(this->gogo_, this->function_,
    4471              :                                                &this->inserter_);
    4472       348764 :       return TRAVERSE_CONTINUE;
    4473              :     }
    4474              : 
    4475       876179 :   if (!no->var_value()->is_parameter()
    4476       401990 :       && !no->var_value()->is_receiver()
    4477       401990 :       && !no->var_value()->is_closure()
    4478       401990 :       && no->var_value()->is_non_escaping_address_taken()
    4479        31906 :       && !no->var_value()->is_in_heap()
    4480       902882 :       && no->var_value()->toplevel_decl() == NULL)
    4481              :     {
    4482              :       // Local variable that has address taken but not escape.
    4483              :       // It needs to be live beyond its lexical scope. So we
    4484              :       // create a top-level declaration for it.
    4485              :       // No need to do it if it is already in the top level.
    4486        21694 :       Block* top_block = function_->func_value()->block();
    4487        21694 :       if (top_block->bindings()->lookup_local(no->name()) != no)
    4488              :         {
    4489         4935 :           Variable* var = no->var_value();
    4490         4935 :           Temporary_statement* ts =
    4491         4935 :             Statement::make_temporary(var->type(), NULL, var->location());
    4492         4935 :           ts->set_is_address_taken();
    4493         4935 :           top_block->add_statement_at_front(ts);
    4494         4935 :           var->set_toplevel_decl(ts);
    4495              :         }
    4496              :     }
    4497              : 
    4498       876179 :   go_assert(!no->var_value()->has_pre_init());
    4499              : 
    4500              :   return TRAVERSE_SKIP_COMPONENTS;
    4501              : }
    4502              : 
    4503              : // Flatten the body of a function.  Record the function while flattening it,
    4504              : // so that we can pass it down when flattening an expression.
    4505              : 
    4506              : int
    4507       295812 : Flatten::function(Named_object* no)
    4508              : {
    4509       295812 :   go_assert(this->function_ == NULL);
    4510       295812 :   this->function_ = no;
    4511       295812 :   int t = no->func_value()->traverse(this);
    4512       295812 :   this->function_ = NULL;
    4513              : 
    4514       295812 :   if (t == TRAVERSE_EXIT)
    4515            0 :     return t;
    4516              :   return TRAVERSE_SKIP_COMPONENTS;
    4517              : }
    4518              : 
    4519              : // Flatten statement parse trees.
    4520              : 
    4521              : int
    4522      6383028 : Flatten::statement(Block* block, size_t* pindex, Statement* sorig)
    4523              : {
    4524              :   // Because we explicitly traverse the statement's contents
    4525              :   // ourselves, we want to skip block statements here.  There is
    4526              :   // nothing to flatten in a block statement.
    4527      6383028 :   if (sorig->is_block_statement())
    4528              :     return TRAVERSE_CONTINUE;
    4529              : 
    4530      4958515 :   Statement_inserter hold_inserter(this->inserter_);
    4531      4958515 :   this->inserter_ = Statement_inserter(block, pindex);
    4532              : 
    4533              :   // Flatten the expressions first.
    4534      4958515 :   int t = sorig->traverse_contents(this);
    4535      4958515 :   if (t == TRAVERSE_EXIT)
    4536              :     {
    4537            0 :       this->inserter_ = hold_inserter;
    4538            0 :       return t;
    4539              :     }
    4540              : 
    4541              :   // Keep flattening until nothing changes.
    4542              :   Statement* s = sorig;
    4543      4958876 :   while (true)
    4544              :     {
    4545      4958876 :       Statement* snew = s->flatten(this->gogo_, this->function_, block,
    4546              :                                    &this->inserter_);
    4547      4958876 :       if (snew == s)
    4548              :         break;
    4549          361 :       s = snew;
    4550          361 :       t = s->traverse_contents(this);
    4551          361 :       if (t == TRAVERSE_EXIT)
    4552              :         {
    4553            0 :           this->inserter_ = hold_inserter;
    4554            0 :           return t;
    4555              :         }
    4556              :     }
    4557              : 
    4558      4958515 :   if (s != sorig)
    4559          361 :     block->replace_statement(*pindex, s);
    4560              : 
    4561      4958515 :   this->inserter_ = hold_inserter;
    4562      4958515 :   return TRAVERSE_SKIP_COMPONENTS;
    4563              : }
    4564              : 
    4565              : // Flatten expression parse trees.
    4566              : 
    4567              : int
    4568     18318593 : Flatten::expression(Expression** pexpr)
    4569              : {
    4570              :   // Keep flattening until nothing changes.
    4571     18379093 :   while (true)
    4572              :     {
    4573     18348843 :       Expression* e = *pexpr;
    4574     18348843 :       if (e->traverse_subexpressions(this) == TRAVERSE_EXIT)
    4575              :         return TRAVERSE_EXIT;
    4576              : 
    4577     18348843 :       Expression* enew = e->flatten(this->gogo_, this->function_,
    4578              :                                     &this->inserter_);
    4579     18348843 :       if (enew == e)
    4580              :         break;
    4581        30250 :       *pexpr = enew;
    4582        30250 :     }
    4583              :   return TRAVERSE_SKIP_COMPONENTS;
    4584              : }
    4585              : 
    4586              : // Flatten a block.
    4587              : 
    4588              : void
    4589        13641 : Gogo::flatten_block(Named_object* function, Block* block)
    4590              : {
    4591        13641 :   Flatten flatten(this, function);
    4592        13641 :   block->traverse(&flatten);
    4593        13641 : }
    4594              : 
    4595              : // Flatten an expression.  INSERTER may be NULL, in which case the
    4596              : // expression had better not need to create any temporaries.
    4597              : 
    4598              : void
    4599       445323 : Gogo::flatten_expression(Named_object* function, Statement_inserter* inserter,
    4600              :                          Expression** pexpr)
    4601              : {
    4602       445323 :   Flatten flatten(this, function);
    4603       445323 :   if (inserter != NULL)
    4604       445323 :     flatten.set_inserter(inserter);
    4605       445323 :   flatten.expression(pexpr);
    4606       445323 : }
    4607              : 
    4608              : void
    4609         4646 : Gogo::flatten()
    4610              : {
    4611         4646 :   Flatten flatten(this, NULL);
    4612         4646 :   this->traverse(&flatten);
    4613         4646 : }
    4614              : 
    4615              : // Traversal to convert calls to the predeclared recover function to
    4616              : // pass in an argument indicating whether it can recover from a panic
    4617              : // or not.
    4618              : 
    4619         1662 : class Convert_recover : public Traverse
    4620              : {
    4621              :  public:
    4622          831 :   Convert_recover(Named_object* arg)
    4623          831 :     : Traverse(traverse_expressions),
    4624          831 :       arg_(arg)
    4625              :   { }
    4626              : 
    4627              :  protected:
    4628              :   int
    4629              :   expression(Expression**);
    4630              : 
    4631              :  private:
    4632              :   // The argument to pass to the function.
    4633              :   Named_object* arg_;
    4634              : };
    4635              : 
    4636              : // Convert calls to recover.
    4637              : 
    4638              : int
    4639        22685 : Convert_recover::expression(Expression** pp)
    4640              : {
    4641        22685 :   Call_expression* ce = (*pp)->call_expression();
    4642         2849 :   if (ce != NULL && ce->is_recover_call())
    4643          847 :     ce->set_recover_arg(Expression::make_var_reference(this->arg_,
    4644              :                                                        ce->location()));
    4645        22685 :   return TRAVERSE_CONTINUE;
    4646              : }
    4647              : 
    4648              : // Traversal for build_recover_thunks.
    4649              : 
    4650         9292 : class Build_recover_thunks : public Traverse
    4651              : {
    4652              :  public:
    4653         4646 :   Build_recover_thunks(Gogo* gogo)
    4654         4646 :     : Traverse(traverse_functions),
    4655         4646 :       gogo_(gogo)
    4656              :   { }
    4657              : 
    4658              :   int
    4659              :   function(Named_object*);
    4660              : 
    4661              :  private:
    4662              :   Expression*
    4663              :   can_recover_arg(Location);
    4664              : 
    4665              :   // General IR.
    4666              :   Gogo* gogo_;
    4667              : };
    4668              : 
    4669              : // If this function calls recover, turn it into a thunk.
    4670              : 
    4671              : int
    4672       189434 : Build_recover_thunks::function(Named_object* orig_no)
    4673              : {
    4674       189434 :   Function* orig_func = orig_no->func_value();
    4675       189434 :   if (!orig_func->calls_recover()
    4676         1662 :       || orig_func->is_recover_thunk()
    4677       191096 :       || orig_func->has_recover_thunk())
    4678              :     return TRAVERSE_CONTINUE;
    4679              : 
    4680          831 :   Gogo* gogo = this->gogo_;
    4681          831 :   Location location = orig_func->location();
    4682              : 
    4683          831 :   static int count;
    4684          831 :   char buf[50];
    4685              : 
    4686          831 :   Function_type* orig_fntype = orig_func->type();
    4687          831 :   Typed_identifier_list* new_params = new Typed_identifier_list();
    4688          831 :   std::string receiver_name;
    4689          831 :   if (orig_fntype->is_method())
    4690              :     {
    4691           42 :       const Typed_identifier* receiver = orig_fntype->receiver();
    4692           42 :       snprintf(buf, sizeof buf, "rt.%u", count);
    4693           42 :       ++count;
    4694           42 :       receiver_name = buf;
    4695           84 :       new_params->push_back(Typed_identifier(receiver_name, receiver->type(),
    4696           84 :                                              receiver->location()));
    4697              :     }
    4698          831 :   const Typed_identifier_list* orig_params = orig_fntype->parameters();
    4699          831 :   if (orig_params != NULL && !orig_params->empty())
    4700              :     {
    4701          175 :       for (Typed_identifier_list::const_iterator p = orig_params->begin();
    4702          272 :            p != orig_params->end();
    4703          175 :            ++p)
    4704              :         {
    4705          175 :           snprintf(buf, sizeof buf, "pt.%u", count);
    4706          175 :           ++count;
    4707          350 :           new_params->push_back(Typed_identifier(buf, p->type(),
    4708          350 :                                                  p->location()));
    4709              :         }
    4710              :     }
    4711          831 :   snprintf(buf, sizeof buf, "pr.%u", count);
    4712          831 :   ++count;
    4713          831 :   std::string can_recover_name = buf;
    4714         1662 :   new_params->push_back(Typed_identifier(can_recover_name,
    4715          831 :                                          Type::lookup_bool_type(),
    4716         1662 :                                          orig_fntype->location()));
    4717              : 
    4718          831 :   const Typed_identifier_list* orig_results = orig_fntype->results();
    4719          831 :   Typed_identifier_list* new_results;
    4720          831 :   if (orig_results == NULL || orig_results->empty())
    4721              :     new_results = NULL;
    4722              :   else
    4723              :     {
    4724            3 :       new_results = new Typed_identifier_list();
    4725            3 :       for (Typed_identifier_list::const_iterator p = orig_results->begin();
    4726            6 :            p != orig_results->end();
    4727            3 :            ++p)
    4728            6 :         new_results->push_back(Typed_identifier("", p->type(), p->location()));
    4729              :     }
    4730              : 
    4731          831 :   Function_type *new_fntype = Type::make_function_type(NULL, new_params,
    4732              :                                                        new_results,
    4733              :                                                        orig_fntype->location());
    4734          831 :   if (orig_fntype->is_varargs())
    4735            1 :     new_fntype->set_is_varargs();
    4736              : 
    4737          831 :   Type* rtype = NULL;
    4738          831 :   if (orig_fntype->is_method())
    4739           42 :     rtype = orig_fntype->receiver()->type();
    4740          831 :   std::string name(gogo->recover_thunk_name(orig_no->name(), rtype));
    4741          831 :   Named_object *new_no = gogo->start_function(name, new_fntype, false,
    4742              :                                               location);
    4743          831 :   Function *new_func = new_no->func_value();
    4744          831 :   if (orig_func->enclosing() != NULL)
    4745          737 :     new_func->set_enclosing(orig_func->enclosing());
    4746              : 
    4747              :   // We build the code for the original function attached to the new
    4748              :   // function, and then swap the original and new function bodies.
    4749              :   // This means that existing references to the original function will
    4750              :   // then refer to the new function.  That makes this code a little
    4751              :   // confusing, in that the reference to NEW_NO really refers to the
    4752              :   // other function, not the one we are building.
    4753              : 
    4754          831 :   Expression* closure = NULL;
    4755          831 :   if (orig_func->needs_closure())
    4756              :     {
    4757              :       // For the new function we are creating, declare a new parameter
    4758              :       // variable NEW_CLOSURE_NO and set it to be the closure variable
    4759              :       // of the function.  This will be set to the closure value
    4760              :       // passed in by the caller.  Then pass a reference to this
    4761              :       // variable as the closure value when calling the original
    4762              :       // function.  In other words, simply pass the closure value
    4763              :       // through the thunk we are creating.
    4764          526 :       Named_object* orig_closure_no = orig_func->closure_var();
    4765          526 :       Variable* orig_closure_var = orig_closure_no->var_value();
    4766          526 :       Variable* new_var = new Variable(orig_closure_var->type(), NULL, false,
    4767          526 :                                        false, false, location);
    4768          526 :       new_var->set_is_closure();
    4769          526 :       snprintf(buf, sizeof buf, "closure.%u", count);
    4770          526 :       ++count;
    4771          526 :       Named_object* new_closure_no = Named_object::make_variable(buf, NULL,
    4772              :                                                                  new_var);
    4773          526 :       new_func->set_closure_var(new_closure_no);
    4774          526 :       closure = Expression::make_var_reference(new_closure_no, location);
    4775              :     }
    4776              : 
    4777          831 :   Expression* fn = Expression::make_func_reference(new_no, closure, location);
    4778              : 
    4779          831 :   Expression_list* args = new Expression_list();
    4780          831 :   if (new_params != NULL)
    4781              :     {
    4782              :       // Note that we skip the last parameter, which is the boolean
    4783              :       // indicating whether recover can succed.
    4784          831 :       for (Typed_identifier_list::const_iterator p = new_params->begin();
    4785         1048 :            p + 1 != new_params->end();
    4786          217 :            ++p)
    4787              :         {
    4788          217 :           Named_object* p_no = gogo->lookup(p->name(), NULL);
    4789          217 :           go_assert(p_no != NULL
    4790              :                      && p_no->is_variable()
    4791              :                      && p_no->var_value()->is_parameter());
    4792          217 :           args->push_back(Expression::make_var_reference(p_no, location));
    4793              :         }
    4794              :     }
    4795          831 :   args->push_back(this->can_recover_arg(location));
    4796              : 
    4797          831 :   gogo->start_block(location);
    4798              : 
    4799          831 :   Call_expression* call = Expression::make_call(fn, args, false, location);
    4800              : 
    4801              :   // Any varargs call has already been lowered.
    4802          831 :   call->set_varargs_are_lowered();
    4803              : 
    4804          831 :   Statement* s = Statement::make_return_from_call(new_no, call, location);
    4805          831 :   s->determine_types(this->gogo_);
    4806          831 :   gogo->add_statement(s);
    4807              : 
    4808          831 :   Block* b = gogo->finish_block(location);
    4809              : 
    4810          831 :   gogo->add_block(b, location);
    4811              : 
    4812              :   // Lower the call in case it returns multiple results.
    4813          831 :   gogo->lower_block(new_no, b);
    4814              : 
    4815          831 :   gogo->finish_function(location);
    4816              : 
    4817              :   // Swap the function bodies and types.
    4818          831 :   new_func->swap_for_recover(orig_func);
    4819          831 :   orig_func->set_is_recover_thunk();
    4820          831 :   new_func->set_calls_recover();
    4821          831 :   new_func->set_has_recover_thunk();
    4822              : 
    4823          831 :   Bindings* orig_bindings = orig_func->block()->bindings();
    4824          831 :   Bindings* new_bindings = new_func->block()->bindings();
    4825          831 :   if (orig_fntype->is_method())
    4826              :     {
    4827              :       // We changed the receiver to be a regular parameter.  We have
    4828              :       // to update the binding accordingly in both functions.
    4829           42 :       Named_object* orig_rec_no = orig_bindings->lookup_local(receiver_name);
    4830           42 :       go_assert(orig_rec_no != NULL
    4831              :                  && orig_rec_no->is_variable()
    4832              :                  && !orig_rec_no->var_value()->is_receiver());
    4833           42 :       orig_rec_no->var_value()->set_is_receiver();
    4834              : 
    4835           42 :       std::string new_receiver_name(orig_fntype->receiver()->name());
    4836           42 :       if (new_receiver_name.empty())
    4837              :         {
    4838              :           // Find the receiver.  It was named "r.NNN" in
    4839              :           // Gogo::start_function.
    4840            7 :           for (Bindings::const_definitions_iterator p =
    4841            7 :                  new_bindings->begin_definitions();
    4842            7 :                p != new_bindings->end_definitions();
    4843            0 :                ++p)
    4844              :             {
    4845            7 :               const std::string& pname((*p)->name());
    4846            7 :               if (pname[0] == 'r' && pname[1] == '.')
    4847              :                 {
    4848            7 :                   new_receiver_name = pname;
    4849            7 :                   break;
    4850              :                 }
    4851              :             }
    4852            7 :           go_assert(!new_receiver_name.empty());
    4853              :         }
    4854           42 :       Named_object* new_rec_no = new_bindings->lookup_local(new_receiver_name);
    4855           42 :       if (new_rec_no == NULL)
    4856            0 :         go_assert(saw_errors());
    4857              :       else
    4858              :         {
    4859           42 :           go_assert(new_rec_no->is_variable()
    4860              :                      && new_rec_no->var_value()->is_receiver());
    4861           42 :           new_rec_no->var_value()->set_is_not_receiver();
    4862              :         }
    4863           42 :     }
    4864              : 
    4865              :   // Because we flipped blocks but not types, the can_recover
    4866              :   // parameter appears in the (now) old bindings as a parameter.
    4867              :   // Change it to a local variable, whereupon it will be discarded.
    4868          831 :   Named_object* can_recover_no = orig_bindings->lookup_local(can_recover_name);
    4869          831 :   go_assert(can_recover_no != NULL
    4870              :              && can_recover_no->is_variable()
    4871              :              && can_recover_no->var_value()->is_parameter());
    4872          831 :   orig_bindings->remove_binding(can_recover_no);
    4873              : 
    4874              :   // Add the can_recover argument to the (now) new bindings, and
    4875              :   // attach it to any recover statements.
    4876          831 :   Variable* can_recover_var = new Variable(Type::lookup_bool_type(), NULL,
    4877         1662 :                                            false, true, false, location);
    4878          831 :   can_recover_no = new_bindings->add_variable(can_recover_name, NULL,
    4879              :                                               can_recover_var);
    4880          831 :   Convert_recover convert_recover(can_recover_no);
    4881          831 :   new_func->traverse(&convert_recover);
    4882              : 
    4883              :   // Update the function pointers in any named results.
    4884          831 :   new_func->update_result_variables();
    4885          831 :   orig_func->update_result_variables();
    4886              : 
    4887          831 :   return TRAVERSE_CONTINUE;
    4888          831 : }
    4889              : 
    4890              : // Return the expression to pass for the .can_recover parameter to the
    4891              : // new function.  This indicates whether a call to recover may return
    4892              : // non-nil.  The expression is runtime.canrecover(__builtin_return_address()).
    4893              : 
    4894              : Expression*
    4895          831 : Build_recover_thunks::can_recover_arg(Location location)
    4896              : {
    4897          831 :   Type* uintptr_type = Type::lookup_integer_type("uintptr");
    4898          831 :   static Named_object* can_recover;
    4899          831 :   if (can_recover == NULL)
    4900              :     {
    4901          319 :       const Location bloc = Linemap::predeclared_location();
    4902          319 :       Typed_identifier_list* param_types = new Typed_identifier_list();
    4903          638 :       param_types->push_back(Typed_identifier("a", uintptr_type, bloc));
    4904          319 :       Type* boolean_type = Type::lookup_bool_type();
    4905          319 :       Typed_identifier_list* results = new Typed_identifier_list();
    4906          638 :       results->push_back(Typed_identifier("", boolean_type, bloc));
    4907          319 :       Function_type* fntype = Type::make_function_type(NULL, param_types,
    4908              :                                                        results, bloc);
    4909          638 :       can_recover =
    4910          319 :         Named_object::make_function_declaration("runtime_canrecover",
    4911              :                                                 NULL, fntype, bloc);
    4912          319 :       can_recover->func_declaration_value()->set_asm_name("runtime.canrecover");
    4913              :     }
    4914              : 
    4915          831 :   Expression* zexpr = Expression::make_integer_ul(0, NULL, location);
    4916          831 :   Expression* call = Runtime::make_call(this->gogo_,
    4917              :                                         Runtime::BUILTIN_RETURN_ADDRESS,
    4918              :                                         location, 1, zexpr);
    4919          831 :   call = Expression::make_unsafe_cast(uintptr_type, call, location);
    4920              : 
    4921          831 :   Expression_list* args = new Expression_list();
    4922          831 :   args->push_back(call);
    4923              : 
    4924          831 :   Expression* fn = Expression::make_func_reference(can_recover, NULL, location);
    4925          831 :   return Expression::make_call(fn, args, false, location);
    4926              : }
    4927              : 
    4928              : // Build thunks for functions which call recover.  We build a new
    4929              : // function with an extra parameter, which is whether a call to
    4930              : // recover can succeed.  We then move the body of this function to
    4931              : // that one.  We then turn this function into a thunk which calls the
    4932              : // new one, passing the value of runtime.canrecover(__builtin_return_address()).
    4933              : // The function will be marked as not splitting the stack.  This will
    4934              : // cooperate with the implementation of defer to make recover do the
    4935              : // right thing.
    4936              : 
    4937              : void
    4938         4646 : Gogo::build_recover_thunks()
    4939              : {
    4940         4646 :   Build_recover_thunks build_recover_thunks(this);
    4941         4646 :   this->traverse(&build_recover_thunks);
    4942         4646 : }
    4943              : 
    4944              : // Look for named types to see whether we need to create an interface
    4945              : // method table.
    4946              : 
    4947         4178 : class Build_method_tables : public Traverse
    4948              : {
    4949              :  public:
    4950         2089 :   Build_method_tables(Gogo* gogo,
    4951              :                       const std::vector<Interface_type*>& interfaces)
    4952         2089 :     : Traverse(traverse_types),
    4953         2089 :       gogo_(gogo), interfaces_(interfaces)
    4954              :   { }
    4955              : 
    4956              :   int
    4957              :   type(Type*);
    4958              : 
    4959              :  private:
    4960              :   // The IR.
    4961              :   Gogo* gogo_;
    4962              :   // A list of locally defined interfaces which have hidden methods.
    4963              :   const std::vector<Interface_type*>& interfaces_;
    4964              : };
    4965              : 
    4966              : // Build all required interface method tables for types.  We need to
    4967              : // ensure that we have an interface method table for every interface
    4968              : // which has a hidden method, for every named type which implements
    4969              : // that interface.  Normally we can just build interface method tables
    4970              : // as we need them.  However, in some cases we can require an
    4971              : // interface method table for an interface defined in a different
    4972              : // package for a type defined in that package.  If that interface and
    4973              : // type both use a hidden method, that is OK.  However, we will not be
    4974              : // able to build that interface method table when we need it, because
    4975              : // the type's hidden method will be static.  So we have to build it
    4976              : // here, and just refer it from other packages as needed.
    4977              : 
    4978              : void
    4979         4646 : Gogo::build_interface_method_tables()
    4980              : {
    4981         4646 :   if (saw_errors())
    4982          411 :     return;
    4983              : 
    4984         4235 :   std::vector<Interface_type*> hidden_interfaces;
    4985         4235 :   hidden_interfaces.reserve(this->interface_types_.size());
    4986        70825 :   for (std::vector<Interface_type*>::const_iterator pi =
    4987         4235 :          this->interface_types_.begin();
    4988        75060 :        pi != this->interface_types_.end();
    4989        70825 :        ++pi)
    4990              :     {
    4991        70825 :       const Typed_identifier_list* methods = (*pi)->methods();
    4992        70825 :       if (methods == NULL)
    4993         1486 :         continue;
    4994       293117 :       for (Typed_identifier_list::const_iterator pm = methods->begin();
    4995       293117 :            pm != methods->end();
    4996       223778 :            ++pm)
    4997              :         {
    4998       232173 :           if (Gogo::is_hidden_name(pm->name()))
    4999              :             {
    5000         8395 :               hidden_interfaces.push_back(*pi);
    5001         8395 :               break;
    5002              :             }
    5003              :         }
    5004              :     }
    5005              : 
    5006         4235 :   if (!hidden_interfaces.empty())
    5007              :     {
    5008              :       // Now traverse the tree looking for all named types.
    5009         2089 :       Build_method_tables bmt(this, hidden_interfaces);
    5010         2089 :       this->traverse(&bmt);
    5011         2089 :     }
    5012              : 
    5013              :   // We no longer need the list of interfaces.
    5014              : 
    5015         7749 :   this->interface_types_.clear();
    5016         4235 : }
    5017              : 
    5018              : // This is called for each type.  For a named type, for each of the
    5019              : // interfaces with hidden methods that it implements, create the
    5020              : // method table.
    5021              : 
    5022              : int
    5023     11457799 : Build_method_tables::type(Type* type)
    5024              : {
    5025     11457799 :   Named_type* nt = type->named_type();
    5026     11457799 :   Struct_type* st = type->struct_type();
    5027     11457799 :   if (nt != NULL || st != NULL)
    5028              :     {
    5029      3642914 :       Translate_context context(this->gogo_, NULL, NULL, NULL);
    5030     19163114 :       for (std::vector<Interface_type*>::const_iterator p =
    5031      3642914 :              this->interfaces_.begin();
    5032     22806028 :            p != this->interfaces_.end();
    5033     19163114 :            ++p)
    5034              :         {
    5035              :           // We ask whether a pointer to the named type implements the
    5036              :           // interface, because a pointer can implement more methods
    5037              :           // than a value.
    5038     19163114 :           if (nt != NULL)
    5039              :             {
    5040     15767481 :               if ((*p)->implements_interface(Type::make_pointer_type(nt),
    5041              :                                              NULL))
    5042              :                 {
    5043        69546 :                   nt->interface_method_table(*p, false)->get_backend(&context);
    5044        69546 :                   nt->interface_method_table(*p, true)->get_backend(&context);
    5045              :                 }
    5046              :             }
    5047              :           else
    5048              :             {
    5049      3395633 :               if ((*p)->implements_interface(Type::make_pointer_type(st),
    5050              :                                              NULL))
    5051              :                 {
    5052          249 :                   st->interface_method_table(*p, false)->get_backend(&context);
    5053          249 :                   st->interface_method_table(*p, true)->get_backend(&context);
    5054              :                 }
    5055              :             }
    5056              :         }
    5057              :     }
    5058     11457799 :   return TRAVERSE_CONTINUE;
    5059              : }
    5060              : 
    5061              : // Return an expression which allocates memory to hold values of type TYPE.
    5062              : 
    5063              : Expression*
    5064       204381 : Gogo::allocate_memory(Type* type, Location location)
    5065              : {
    5066       204381 :   Expression* td = Expression::make_type_descriptor(type, location);
    5067       204381 :   return Runtime::make_call(this, Runtime::NEW, location, 1, td);
    5068              : }
    5069              : 
    5070              : // Traversal class used to check for return statements.
    5071              : 
    5072         9292 : class Check_return_statements_traverse : public Traverse
    5073              : {
    5074              :  public:
    5075         4646 :   Check_return_statements_traverse()
    5076         4646 :     : Traverse(traverse_functions)
    5077              :   { }
    5078              : 
    5079              :   int
    5080              :   function(Named_object*);
    5081              : };
    5082              : 
    5083              : // Check that a function has a return statement if it needs one.
    5084              : 
    5085              : int
    5086       174977 : Check_return_statements_traverse::function(Named_object* no)
    5087              : {
    5088       174977 :   Function* func = no->func_value();
    5089       174977 :   const Function_type* fntype = func->type();
    5090       174977 :   const Typed_identifier_list* results = fntype->results();
    5091              : 
    5092              :   // We only need a return statement if there is a return value.
    5093       174977 :   if (results == NULL || results->empty())
    5094              :     return TRAVERSE_CONTINUE;
    5095              : 
    5096       104631 :   if (func->block()->may_fall_through())
    5097          226 :     go_error_at(func->block()->end_location(),
    5098              :                 "missing return at end of function");
    5099              : 
    5100              :   return TRAVERSE_CONTINUE;
    5101              : }
    5102              : 
    5103              : // Check return statements.
    5104              : 
    5105              : void
    5106         4646 : Gogo::check_return_statements()
    5107              : {
    5108         4646 :   Check_return_statements_traverse traverse;
    5109         4646 :   this->traverse(&traverse);
    5110         4646 : }
    5111              : 
    5112              : // Traversal class to decide whether a function body is less than the
    5113              : // inlining budget.  This adjusts *available as it goes, and stops the
    5114              : // traversal if it goes negative.
    5115              : 
    5116      1636631 : class Inline_within_budget : public Traverse
    5117              : {
    5118              :  public:
    5119      1822972 :   Inline_within_budget(int* available)
    5120      1822972 :     : Traverse(traverse_statements
    5121              :                | traverse_expressions),
    5122      1822972 :       available_(available)
    5123              :   { }
    5124              : 
    5125              :   int
    5126              :   statement(Block*, size_t*, Statement*);
    5127              : 
    5128              :   int
    5129              :   expression(Expression**);
    5130              : 
    5131              :  private:
    5132              :   // Pointer to remaining budget.
    5133              :   int* available_;
    5134              : };
    5135              : 
    5136              : // Adjust the budget for the inlining cost of a statement.
    5137              : 
    5138              : int
    5139      4945654 : Inline_within_budget::statement(Block*, size_t*, Statement* s)
    5140              : {
    5141      4945654 :   if (*this->available_ < 0)
    5142              :     return TRAVERSE_EXIT;
    5143      4926467 :   *this->available_ -= s->inlining_cost();
    5144      4926467 :   return TRAVERSE_CONTINUE;
    5145              : }
    5146              : 
    5147              : // Adjust the budget for the inlining cost of an expression.
    5148              : 
    5149              : int
    5150     10891690 : Inline_within_budget::expression(Expression** pexpr)
    5151              : {
    5152     10891690 :   if (*this->available_ < 0)
    5153              :     return TRAVERSE_EXIT;
    5154      9245900 :   *this->available_ -= (*pexpr)->inlining_cost();
    5155      9245900 :   return TRAVERSE_CONTINUE;
    5156              : }
    5157              : 
    5158              : // Traversal class to find functions whose body should be exported for
    5159              : // inlining by other packages.
    5160              : 
    5161         8504 : class Mark_inline_candidates : public Traverse
    5162              : {
    5163              :  public:
    5164         4252 :   Mark_inline_candidates(Unordered_set(Named_object*)* marked)
    5165         4252 :     : Traverse(traverse_functions
    5166              :                | traverse_types),
    5167         4252 :       marked_functions_(marked)
    5168              :   { }
    5169              : 
    5170              :   int
    5171              :   function(Named_object*);
    5172              : 
    5173              :   int
    5174              :   type(Type*);
    5175              : 
    5176              :  private:
    5177              :   // We traverse the function body trying to determine how expensive
    5178              :   // it is for inlining.  We start with a budget, and decrease that
    5179              :   // budget for each statement and expression.  If the budget goes
    5180              :   // negative, we do not export the function body.  The value of this
    5181              :   // budget is a heuristic.  In the usual GCC spirit, we could
    5182              :   // consider setting this via a command line option.
    5183              :   const int budget_heuristic = 80;
    5184              : 
    5185              :   // Set of named objects that are marked as inline candidates.
    5186              :   Unordered_set(Named_object*)* marked_functions_;
    5187              : };
    5188              : 
    5189              : // Mark a function if it is an inline candidate.
    5190              : 
    5191              : int
    5192       186701 : Mark_inline_candidates::function(Named_object* no)
    5193              : {
    5194       186701 :   Function* func = no->func_value();
    5195       186701 :   if ((func->pragmas() & GOPRAGMA_NOINLINE) != 0)
    5196              :     return TRAVERSE_CONTINUE;
    5197       186341 :   int budget = budget_heuristic;
    5198       186341 :   Inline_within_budget iwb(&budget);
    5199       186341 :   func->block()->traverse(&iwb);
    5200       186341 :   if (budget >= 0)
    5201              :     {
    5202        41215 :       func->set_export_for_inlining();
    5203        41215 :       this->marked_functions_->insert(no);
    5204              :     }
    5205       186341 :   return TRAVERSE_CONTINUE;
    5206       186341 : }
    5207              : 
    5208              : // Mark methods if they are inline candidates.
    5209              : 
    5210              : int
    5211     11069029 : Mark_inline_candidates::type(Type* t)
    5212              : {
    5213     11069029 :   Named_type* nt = t->named_type();
    5214     11069029 :   if (nt == NULL || nt->is_alias())
    5215              :     return TRAVERSE_CONTINUE;
    5216      3156920 :   const Bindings* methods = nt->local_methods();
    5217      3156920 :   if (methods == NULL)
    5218              :     return TRAVERSE_CONTINUE;
    5219      2115020 :   for (Bindings::const_definitions_iterator p = methods->begin_definitions();
    5220      2115020 :        p != methods->end_definitions();
    5221      1637896 :        ++p)
    5222              :     {
    5223      1637896 :       Named_object* no = *p;
    5224      1637896 :       go_assert(no->is_function());
    5225      1637896 :       Function *func = no->func_value();
    5226      1637896 :       if ((func->pragmas() & GOPRAGMA_NOINLINE) != 0)
    5227         1265 :         continue;
    5228      1636631 :       int budget = budget_heuristic;
    5229      1636631 :       Inline_within_budget iwb(&budget);
    5230      1636631 :       func->block()->traverse(&iwb);
    5231      1636631 :       if (budget >= 0)
    5232              :         {
    5233       116157 :           func->set_export_for_inlining();
    5234       116157 :           this->marked_functions_->insert(no);
    5235              :         }
    5236      1636631 :     }
    5237              :   return TRAVERSE_CONTINUE;
    5238              : }
    5239              : 
    5240              : // Export identifiers as requested.
    5241              : 
    5242              : void
    5243         4646 : Gogo::do_exports()
    5244              : {
    5245         4646 :   if (saw_errors())
    5246          394 :     return;
    5247              : 
    5248              :   // Mark any functions whose body should be exported for inlining by
    5249              :   // other packages.
    5250         4252 :   Unordered_set(Named_object*) marked_functions;
    5251         4252 :   Mark_inline_candidates mic(&marked_functions);
    5252         4252 :   this->traverse(&mic);
    5253              : 
    5254              :   // For now we always stream to a section.  Later we may want to
    5255              :   // support streaming to a separate file.
    5256         4252 :   Stream_to_section stream(this->backend());
    5257              : 
    5258              :   // Write out either the prefix or pkgpath depending on how we were
    5259              :   // invoked.
    5260         4252 :   std::string prefix;
    5261         4252 :   std::string pkgpath;
    5262         4252 :   if (this->pkgpath_from_option_)
    5263         2006 :     pkgpath = this->pkgpath_;
    5264         2246 :   else if (this->prefix_from_option_)
    5265            0 :     prefix = this->prefix_;
    5266         2246 :   else if (this->is_main_package())
    5267         1636 :     pkgpath = "main";
    5268              :   else
    5269          610 :     prefix = "go";
    5270              : 
    5271         4252 :   std::string init_fn_name;
    5272         4252 :   if (this->is_main_package())
    5273         1636 :     init_fn_name = "";
    5274         2616 :   else if (this->need_init_fn_)
    5275         1525 :     init_fn_name = this->get_init_fn_name();
    5276              :   else
    5277         1091 :     init_fn_name = this->dummy_init_fn_name();
    5278              : 
    5279         4252 :   Export exp(&stream);
    5280         4252 :   exp.register_builtin_types(this);
    5281         4252 :   exp.export_globals(this->package_name(),
    5282              :                      prefix,
    5283              :                      pkgpath,
    5284         4252 :                      this->packages_,
    5285         4252 :                      this->imports_,
    5286              :                      init_fn_name,
    5287         4252 :                      this->imported_init_fns_,
    5288         4252 :                      this->package_->bindings(),
    5289              :                      &marked_functions);
    5290              : 
    5291         4252 :   if (!this->c_header_.empty() && !saw_errors())
    5292            4 :     this->write_c_header();
    5293         4252 : }
    5294              : 
    5295              : // Write the top level named struct types in C format to a C header
    5296              : // file.  This is used when building the runtime package, to share
    5297              : // struct definitions between C and Go.
    5298              : 
    5299              : void
    5300            4 : Gogo::write_c_header()
    5301              : {
    5302            4 :   std::ofstream out;
    5303            4 :   out.open(this->c_header_.c_str());
    5304            4 :   if (out.fail())
    5305              :     {
    5306            0 :       go_error_at(Linemap::unknown_location(),
    5307              :                   "cannot open %s: %m", this->c_header_.c_str());
    5308            0 :       return;
    5309              :     }
    5310              : 
    5311            4 :   std::list<Named_object*> types;
    5312            4 :   Bindings* top = this->package_->bindings();
    5313        41142 :   for (Bindings::const_definitions_iterator p = top->begin_definitions();
    5314        41142 :        p != top->end_definitions();
    5315        41138 :        ++p)
    5316              :     {
    5317        41138 :       Named_object* no = *p;
    5318              : 
    5319              :       // Skip names that start with underscore followed by something
    5320              :       // other than an uppercase letter, as when compiling the runtime
    5321              :       // package they are mostly types defined by mkrsysinfo.sh based
    5322              :       // on the C system header files.  We don't need to translate
    5323              :       // types to C and back to Go.  But do accept the special cases
    5324              :       // _defer, _panic, and _type.
    5325        41138 :       std::string name = Gogo::unpack_hidden_name(no->name());
    5326        41138 :       if (name[0] == '_'
    5327        29508 :           && (name[1] < 'A' || name[1] > 'Z')
    5328        51208 :           && (name != "_defer" && name != "_panic" && name != "_type"))
    5329        10054 :         continue;
    5330              : 
    5331        31084 :       if (no->is_type() && no->type_value()->struct_type() != NULL)
    5332          656 :         types.push_back(no);
    5333        31084 :       if (no->is_const()
    5334        52372 :           && no->const_value()->type()->integer_type() != NULL
    5335        51836 :           && !no->const_value()->is_sink())
    5336              :         {
    5337        20748 :           Numeric_constant nc;
    5338        20748 :           unsigned long val;
    5339        20748 :           if (no->const_value()->expr()->numeric_constant_value(&nc)
    5340        20748 :               && nc.to_unsigned_long(&val) == Numeric_constant::NC_UL_VALID)
    5341              :             {
    5342        82080 :               out << "#define " << no->message_name() << ' ' << val
    5343        41040 :                   << std::endl;
    5344              :             }
    5345        20748 :         }
    5346        41138 :     }
    5347              : 
    5348            4 :   std::vector<const Named_object*> written;
    5349            4 :   int loop = 0;
    5350          772 :   while (!types.empty())
    5351              :     {
    5352          768 :       Named_object* no = types.front();
    5353          768 :       types.pop_front();
    5354              : 
    5355          768 :       std::vector<const Named_object*> needs;
    5356          768 :       std::vector<const Named_object*> declare;
    5357         1536 :       if (!no->type_value()->struct_type()->can_write_to_c_header(&needs,
    5358              :                                                                   &declare))
    5359           60 :         continue;
    5360              : 
    5361          708 :       bool ok = true;
    5362         1172 :       for (std::vector<const Named_object*>::const_iterator pr
    5363          708 :              = needs.begin();
    5364         1172 :            pr != needs.end() && ok;
    5365          464 :            ++pr)
    5366              :         {
    5367        17612 :           for (std::list<Named_object*>::const_iterator pt = types.begin();
    5368        17612 :                pt != types.end() && ok;
    5369        17148 :                ++pt)
    5370        17148 :             if (*pr == *pt)
    5371          112 :               ok = false;
    5372              :         }
    5373          708 :       if (!ok)
    5374              :         {
    5375          112 :           ++loop;
    5376          112 :           if (loop > 10000)
    5377              :             {
    5378              :               // This should be impossible since the code parsed and
    5379              :               // type checked.
    5380            0 :               go_unreachable();
    5381              :             }
    5382              : 
    5383          112 :           types.push_back(no);
    5384          112 :           continue;
    5385              :         }
    5386              : 
    5387         1136 :       for (std::vector<const Named_object*>::const_iterator pd
    5388          596 :              = declare.begin();
    5389         1136 :            pd != declare.end();
    5390          540 :            ++pd)
    5391              :         {
    5392          540 :           if (*pd == no)
    5393          112 :             continue;
    5394              : 
    5395          456 :           std::vector<const Named_object*> dneeds;
    5396          456 :           std::vector<const Named_object*> ddeclare;
    5397          912 :           if (!(*pd)->type_value()->struct_type()->
    5398          912 :               can_write_to_c_header(&dneeds, &ddeclare))
    5399           28 :             continue;
    5400              : 
    5401          428 :           bool done = false;
    5402        18764 :           for (std::vector<const Named_object*>::const_iterator pw
    5403          428 :                  = written.begin();
    5404        18764 :                pw != written.end();
    5405        18336 :                ++pw)
    5406              :             {
    5407        18668 :               if (*pw == *pd)
    5408              :                 {
    5409              :                   done = true;
    5410              :                   break;
    5411              :                 }
    5412              :             }
    5413          428 :           if (!done)
    5414              :             {
    5415           96 :               out << std::endl;
    5416          192 :               out << "struct " << (*pd)->message_name() << ";" << std::endl;
    5417           96 :               written.push_back(*pd);
    5418              :             }
    5419          456 :         }
    5420              : 
    5421          596 :       out << std::endl;
    5422         1192 :       out << "struct " << no->message_name() << " {" << std::endl;
    5423         1192 :       no->type_value()->struct_type()->write_to_c_header(out);
    5424          596 :       out << "};" << std::endl;
    5425          596 :       written.push_back(no);
    5426          768 :     }
    5427              : 
    5428            4 :   out.close();
    5429            4 :   if (out.fail())
    5430            0 :     go_error_at(Linemap::unknown_location(),
    5431              :                 "error writing to %s: %m", this->c_header_.c_str());
    5432            4 : }
    5433              : 
    5434              : // Find the blocks in order to convert named types defined in blocks.
    5435              : 
    5436         9292 : class Convert_named_types : public Traverse
    5437              : {
    5438              :  public:
    5439         4646 :   Convert_named_types(Gogo* gogo)
    5440         4646 :     : Traverse(traverse_blocks),
    5441         4646 :       gogo_(gogo)
    5442              :   { }
    5443              : 
    5444              :  protected:
    5445              :   int
    5446              :   block(Block* block);
    5447              : 
    5448              :  private:
    5449              :   Gogo* gogo_;
    5450              : };
    5451              : 
    5452              : int
    5453      1676057 : Convert_named_types::block(Block* block)
    5454              : {
    5455      1676057 :   this->gogo_->convert_named_types_in_bindings(block->bindings());
    5456      1676057 :   return TRAVERSE_CONTINUE;
    5457              : }
    5458              : 
    5459              : // Convert all named types to the backend representation.  Since named
    5460              : // types can refer to other types, this needs to be done in the right
    5461              : // sequence, which is handled by Named_type::convert.  Here we arrange
    5462              : // to call that for each named type.
    5463              : 
    5464              : void
    5465         4646 : Gogo::convert_named_types()
    5466              : {
    5467         4646 :   this->convert_named_types_in_bindings(this->globals_);
    5468       120891 :   for (Packages::iterator p = this->packages_.begin();
    5469       120891 :        p != this->packages_.end();
    5470       116245 :        ++p)
    5471              :     {
    5472       116245 :       Package* package = p->second;
    5473       116245 :       this->convert_named_types_in_bindings(package->bindings());
    5474              :     }
    5475              : 
    5476         4646 :   Convert_named_types cnt(this);
    5477         4646 :   this->traverse(&cnt);
    5478              : 
    5479              :   // Make all the builtin named types used for type descriptors, and
    5480              :   // then convert them.  They will only be written out if they are
    5481              :   // needed.
    5482         4646 :   Type::make_type_descriptor_type();
    5483         4646 :   Type::make_type_descriptor_ptr_type();
    5484         4646 :   Function_type::make_function_type_descriptor_type();
    5485         4646 :   Pointer_type::make_pointer_type_descriptor_type();
    5486         4646 :   Struct_type::make_struct_type_descriptor_type();
    5487         4646 :   Array_type::make_array_type_descriptor_type();
    5488         4646 :   Array_type::make_slice_type_descriptor_type();
    5489         4646 :   Map_type::make_map_type_descriptor_type();
    5490         4646 :   Channel_type::make_chan_type_descriptor_type();
    5491         4646 :   Interface_type::make_interface_type_descriptor_type();
    5492         4646 :   Expression::make_func_descriptor_type();
    5493         4646 :   Type::convert_builtin_named_types(this);
    5494              : 
    5495         4646 :   Runtime::convert_types(this);
    5496              : 
    5497         4646 :   this->named_types_are_converted_ = true;
    5498              : 
    5499         4646 :   Type::finish_pointer_types(this);
    5500         4646 : }
    5501              : 
    5502              : // Convert all names types in a set of bindings.
    5503              : 
    5504              : void
    5505      1796948 : Gogo::convert_named_types_in_bindings(Bindings* bindings)
    5506              : {
    5507      4939636 :   for (Bindings::const_definitions_iterator p = bindings->begin_definitions();
    5508      4939636 :        p != bindings->end_definitions();
    5509      3142688 :        ++p)
    5510              :     {
    5511      3142688 :       if ((*p)->is_type())
    5512       733326 :         (*p)->type_value()->convert(this);
    5513              :     }
    5514      1796948 : }
    5515              : 
    5516              : void
    5517            0 : debug_go_gogo(Gogo* gogo)
    5518              : {
    5519            0 :   if (gogo != NULL)
    5520            0 :     gogo->debug_dump();
    5521            0 : }
    5522              : 
    5523              : void
    5524            0 : Gogo::debug_dump()
    5525              : {
    5526            0 :   std::cerr << "Packages:\n";
    5527            0 :   for (Packages::const_iterator p = this->packages_.begin();
    5528            0 :        p != this->packages_.end();
    5529            0 :        ++p)
    5530              :     {
    5531            0 :       const char *tag = "  ";
    5532            0 :       if (p->second == this->package_)
    5533            0 :         tag = "* ";
    5534            0 :       std::cerr << tag << "'" << p->first << "' "
    5535            0 :                 << p->second->pkgpath() << " " << ((void*)p->second) << "\n";
    5536              :     }
    5537            0 : }
    5538              : 
    5539              : // Class Function.
    5540              : 
    5541       299270 : Function::Function(Function_type* type, Named_object* enclosing, Block* block,
    5542       299270 :                    Location location)
    5543       299270 :   : type_(type), enclosing_(enclosing), results_(NULL),
    5544       299270 :     closure_var_(NULL), block_(block), location_(location), labels_(),
    5545       299270 :     local_type_count_(0), descriptor_(NULL), fndecl_(NULL), defer_stack_(NULL),
    5546       299270 :     pragmas_(0), nested_functions_(0), is_sink_(false),
    5547       299270 :     results_are_named_(false), is_unnamed_type_stub_method_(false),
    5548       299270 :     calls_recover_(false), is_recover_thunk_(false), has_recover_thunk_(false),
    5549       299270 :     calls_defer_retaddr_(false), is_type_specific_function_(false),
    5550       299270 :     in_unique_section_(false), export_for_inlining_(false),
    5551       299270 :     is_inline_only_(false), is_referenced_by_inline_(false),
    5552       299270 :     is_exported_by_linkname_(false)
    5553              : {
    5554       299270 : }
    5555              : 
    5556              : // Create the named result variables.
    5557              : 
    5558              : void
    5559       295817 : Function::create_result_variables(Gogo* gogo)
    5560              : {
    5561       295817 :   const Typed_identifier_list* results = this->type_->results();
    5562       295817 :   if (results == NULL || results->empty())
    5563              :     return;
    5564              : 
    5565       217670 :   if (!results->front().name().empty())
    5566        21590 :     this->results_are_named_ = true;
    5567              : 
    5568       217670 :   this->results_ = new Results();
    5569       217670 :   this->results_->reserve(results->size());
    5570              : 
    5571       217670 :   Block* block = this->block_;
    5572       217670 :   int index = 0;
    5573       217670 :   for (Typed_identifier_list::const_iterator p = results->begin();
    5574       462567 :        p != results->end();
    5575       244897 :        ++p, ++index)
    5576              :     {
    5577       244897 :       std::string name = p->name();
    5578       244897 :       if (name.empty() || Gogo::is_sink_name(name))
    5579              :         {
    5580       212090 :           static int result_counter;
    5581       212090 :           char buf[100];
    5582       212090 :           snprintf(buf, sizeof buf, "$ret%d", result_counter);
    5583       212090 :           ++result_counter;
    5584       212090 :           name = gogo->pack_hidden_name(buf, false);
    5585              :         }
    5586       244897 :       Result_variable* result = new Result_variable(p->type(), this, index,
    5587       244897 :                                                     p->location());
    5588       244897 :       Named_object* no = block->bindings()->add_result_variable(name, result);
    5589       244897 :       if (no->is_result_variable())
    5590       244893 :         this->results_->push_back(no);
    5591              :       else
    5592              :         {
    5593            4 :           static int dummy_result_count;
    5594            4 :           char buf[100];
    5595            4 :           snprintf(buf, sizeof buf, "$dret%d", dummy_result_count);
    5596            4 :           ++dummy_result_count;
    5597            4 :           name = gogo->pack_hidden_name(buf, false);
    5598            4 :           no = block->bindings()->add_result_variable(name, result);
    5599            4 :           go_assert(no->is_result_variable());
    5600            4 :           this->results_->push_back(no);
    5601              :         }
    5602       244897 :     }
    5603              : }
    5604              : 
    5605              : // Update the named result variables when cloning a function which
    5606              : // calls recover.
    5607              : 
    5608              : void
    5609         1662 : Function::update_result_variables()
    5610              : {
    5611         1662 :   if (this->results_ == NULL)
    5612              :     return;
    5613              : 
    5614           12 :   for (Results::iterator p = this->results_->begin();
    5615           12 :        p != this->results_->end();
    5616            6 :        ++p)
    5617            6 :     (*p)->result_var_value()->set_function(this);
    5618              : }
    5619              : 
    5620              : // Whether this method should not be included in the type descriptor.
    5621              : 
    5622              : bool
    5623       572082 : Function::nointerface() const
    5624              : {
    5625       572082 :   go_assert(this->is_method());
    5626       572082 :   return (this->pragmas_ & GOPRAGMA_NOINTERFACE) != 0;
    5627              : }
    5628              : 
    5629              : // Record that this method should not be included in the type
    5630              : // descriptor.
    5631              : 
    5632              : void
    5633            2 : Function::set_nointerface()
    5634              : {
    5635            2 :   this->pragmas_ |= GOPRAGMA_NOINTERFACE;
    5636            2 : }
    5637              : 
    5638              : // Return the closure variable, creating it if necessary.
    5639              : 
    5640              : Named_object*
    5641        56715 : Function::closure_var()
    5642              : {
    5643        56715 :   if (this->closure_var_ == NULL)
    5644              :     {
    5645        13057 :       go_assert(this->descriptor_ == NULL);
    5646              :       // We don't know the type of the variable yet.  We add fields as
    5647              :       // we find them.
    5648        13057 :       Location loc = this->type_->location();
    5649        13057 :       Struct_field_list* sfl = new Struct_field_list;
    5650        13057 :       Struct_type* struct_type = Type::make_struct_type(sfl, loc);
    5651        13057 :       struct_type->set_is_struct_incomparable();
    5652        13057 :       Variable* var = new Variable(Type::make_pointer_type(struct_type),
    5653        26114 :                                    NULL, false, false, false, loc);
    5654        13057 :       var->set_is_used();
    5655        13057 :       var->set_is_closure();
    5656        13057 :       this->closure_var_ = Named_object::make_variable("$closure", NULL, var);
    5657              :       // Note that the new variable is not in any binding contour.
    5658              :     }
    5659        56715 :   return this->closure_var_;
    5660              : }
    5661              : 
    5662              : // Set the type of the closure variable.
    5663              : 
    5664              : void
    5665       175575 : Function::set_closure_type()
    5666              : {
    5667       175575 :   if (this->closure_var_ == NULL)
    5668              :     return;
    5669        13057 :   Named_object* closure = this->closure_var_;
    5670        26114 :   Struct_type* st = closure->var_value()->type()->deref()->struct_type();
    5671              : 
    5672              :   // The first field of a closure is always a pointer to the function
    5673              :   // code.
    5674        13057 :   Type* voidptr_type = Type::make_pointer_type(Type::make_void_type());
    5675        26114 :   st->push_field(Struct_field(Typed_identifier(".f", voidptr_type,
    5676        13057 :                                                this->location_)));
    5677              : 
    5678        13057 :   unsigned int index = 1;
    5679        13057 :   for (Closure_fields::const_iterator p = this->closure_fields_.begin();
    5680        38530 :        p != this->closure_fields_.end();
    5681        25473 :        ++p, ++index)
    5682              :     {
    5683        25473 :       Named_object* no = p->first;
    5684        25473 :       char buf[20];
    5685        25473 :       snprintf(buf, sizeof buf, "%u", index);
    5686        25473 :       std::string n = no->name() + buf;
    5687        25473 :       Type* var_type;
    5688        25473 :       if (no->is_variable())
    5689        24646 :         var_type = no->var_value()->type();
    5690              :       else
    5691          827 :         var_type = no->result_var_value()->type();
    5692        25473 :       Type* field_type = Type::make_pointer_type(var_type);
    5693        50946 :       st->push_field(Struct_field(Typed_identifier(n, field_type, p->second)));
    5694        25473 :     }
    5695              : }
    5696              : 
    5697              : // Return whether this function is a method.
    5698              : 
    5699              : bool
    5700      1802988 : Function::is_method() const
    5701              : {
    5702      1802988 :   return this->type_->is_method();
    5703              : }
    5704              : 
    5705              : // Add a label definition.
    5706              : 
    5707              : Label*
    5708        10526 : Function::add_label_definition(Gogo* gogo, const std::string& label_name,
    5709              :                                Location location)
    5710              : {
    5711        10526 :   Label* lnull = NULL;
    5712        10526 :   std::pair<Labels::iterator, bool> ins =
    5713        21052 :     this->labels_.insert(std::make_pair(label_name, lnull));
    5714        10526 :   Label* label;
    5715        10526 :   if (label_name == "_")
    5716              :     {
    5717            3 :       label = Label::create_dummy_label();
    5718            3 :       if (ins.second)
    5719            2 :         ins.first->second = label;
    5720              :     }
    5721        10523 :   else if (ins.second)
    5722              :     {
    5723              :       // This is a new label.
    5724         1126 :       label = new Label(label_name);
    5725         1126 :       ins.first->second = label;
    5726              :     }
    5727              :   else
    5728              :     {
    5729              :       // The label was already in the hash table.
    5730         9397 :       label = ins.first->second;
    5731         9397 :       if (label->is_defined())
    5732              :         {
    5733            2 :           go_error_at(location, "label %qs already defined",
    5734            2 :                       Gogo::message_name(label_name).c_str());
    5735            2 :           go_inform(label->location(), "previous definition of %qs was here",
    5736            2 :                     Gogo::message_name(label_name).c_str());
    5737            2 :           return new Label(label_name);
    5738              :         }
    5739              :     }
    5740              : 
    5741        10524 :   label->define(location, gogo->bindings_snapshot(location));
    5742              : 
    5743              :   // Issue any errors appropriate for any previous goto's to this
    5744              :   // label.
    5745        10524 :   const std::vector<Bindings_snapshot*>& refs(label->refs());
    5746        12089 :   for (std::vector<Bindings_snapshot*>::const_iterator p = refs.begin();
    5747        12089 :        p != refs.end();
    5748         1565 :        ++p)
    5749         1565 :     (*p)->check_goto_to(gogo->current_block());
    5750        10524 :   label->clear_refs();
    5751              : 
    5752        10524 :   return label;
    5753              : }
    5754              : 
    5755              : // Add a reference to a label.
    5756              : 
    5757              : Label*
    5758        10959 : Function::add_label_reference(Gogo* gogo, const std::string& label_name,
    5759              :                               Location location, bool issue_goto_errors)
    5760              : {
    5761        10959 :   Label* lnull = NULL;
    5762        10959 :   std::pair<Labels::iterator, bool> ins =
    5763        21918 :     this->labels_.insert(std::make_pair(label_name, lnull));
    5764        10959 :   Label* label;
    5765        10959 :   if (!ins.second)
    5766              :     {
    5767              :       // The label was already in the hash table.
    5768         1559 :       label = ins.first->second;
    5769              :     }
    5770              :   else
    5771              :     {
    5772         9400 :       go_assert(ins.first->second == NULL);
    5773         9400 :       label = new Label(label_name);
    5774         9400 :       ins.first->second = label;
    5775              :     }
    5776              : 
    5777        10959 :   label->set_is_used();
    5778              : 
    5779        10959 :   if (issue_goto_errors)
    5780              :     {
    5781         2101 :       Bindings_snapshot* snapshot = label->snapshot();
    5782         2101 :       if (snapshot != NULL)
    5783          533 :         snapshot->check_goto_from(gogo->current_block(), location);
    5784              :       else
    5785         1568 :         label->add_snapshot_ref(gogo->bindings_snapshot(location));
    5786              :     }
    5787              : 
    5788        10959 :   return label;
    5789              : }
    5790              : 
    5791              : // Warn about labels that are defined but not used.
    5792              : 
    5793              : void
    5794       175575 : Function::check_labels() const
    5795              : {
    5796       177262 :   for (Labels::const_iterator p = this->labels_.begin();
    5797       177262 :        p != this->labels_.end();
    5798         1687 :        p++)
    5799              :     {
    5800         1687 :       Label* label = p->second;
    5801         1687 :       if (!label->is_used())
    5802            7 :         go_error_at(label->location(), "label %qs defined and not used",
    5803           14 :                     Gogo::message_name(label->name()).c_str());
    5804              :     }
    5805       175575 : }
    5806              : 
    5807              : // Set the receiver type.  This is used to remove aliases.
    5808              : 
    5809              : void
    5810            2 : Function::set_receiver_type(Type* rtype)
    5811              : {
    5812            2 :   Function_type* oft = this->type_;
    5813            2 :   Typed_identifier* rec = new Typed_identifier(oft->receiver()->name(),
    5814              :                                                rtype,
    5815            2 :                                                oft->receiver()->location());
    5816            2 :   Typed_identifier_list* parameters = NULL;
    5817            2 :   if (oft->parameters() != NULL)
    5818            0 :     parameters = oft->parameters()->copy();
    5819            2 :   Typed_identifier_list* results = NULL;
    5820            2 :   if (oft->results() != NULL)
    5821            0 :     results = oft->results()->copy();
    5822            2 :   Function_type* nft = Type::make_function_type(rec, parameters, results,
    5823              :                                                 oft->location());
    5824            2 :   this->type_ = nft;
    5825            2 : }
    5826              : 
    5827              : // Swap one function with another.  This is used when building the
    5828              : // thunk we use to call a function which calls recover.  It may not
    5829              : // work for any other case.
    5830              : 
    5831              : void
    5832          831 : Function::swap_for_recover(Function *x)
    5833              : {
    5834          831 :   go_assert(this->enclosing_ == x->enclosing_);
    5835          831 :   std::swap(this->results_, x->results_);
    5836          831 :   std::swap(this->closure_var_, x->closure_var_);
    5837          831 :   std::swap(this->block_, x->block_);
    5838          831 :   go_assert(this->location_ == x->location_);
    5839          831 :   go_assert(this->fndecl_ == NULL && x->fndecl_ == NULL);
    5840          831 :   go_assert(this->defer_stack_ == NULL && x->defer_stack_ == NULL);
    5841          831 : }
    5842              : 
    5843              : // Traverse the tree.
    5844              : 
    5845              : int
    5846      4843658 : Function::traverse(Traverse* traverse)
    5847              : {
    5848      4843658 :   unsigned int traverse_mask = traverse->traverse_mask();
    5849              : 
    5850      4843658 :   if ((traverse_mask
    5851      4843658 :        & (Traverse::traverse_types | Traverse::traverse_expressions))
    5852              :       != 0)
    5853              :     {
    5854      3032181 :       if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
    5855              :         return TRAVERSE_EXIT;
    5856              :     }
    5857              : 
    5858              :   // FIXME: We should check traverse_functions here if nested
    5859              :   // functions are stored in block bindings.
    5860      4843658 :   if (this->block_ != NULL
    5861      4843658 :       && (traverse_mask
    5862      4843658 :           & (Traverse::traverse_variables
    5863              :              | Traverse::traverse_constants
    5864              :              | Traverse::traverse_blocks
    5865              :              | Traverse::traverse_statements
    5866              :              | Traverse::traverse_expressions
    5867              :              | Traverse::traverse_types)) != 0)
    5868              :     {
    5869      4275326 :       if (this->block_->traverse(traverse) == TRAVERSE_EXIT)
    5870              :         return TRAVERSE_EXIT;
    5871              :     }
    5872              : 
    5873              :   return TRAVERSE_CONTINUE;
    5874              : }
    5875              : 
    5876              : // Work out types for unspecified variables and constants.
    5877              : 
    5878              : void
    5879       175575 : Function::determine_types(Gogo* gogo)
    5880              : {
    5881       175575 :   this->set_closure_type();
    5882       175575 :   if (this->block_ != NULL)
    5883       175575 :     this->block_->determine_types(gogo);
    5884       175575 : }
    5885              : 
    5886              : // Return the function descriptor, the value you get when you refer to
    5887              : // the function in Go code without calling it.
    5888              : 
    5889              : Expression*
    5890       194963 : Function::descriptor(Gogo*, Named_object* no)
    5891              : {
    5892       194963 :   go_assert(!this->is_method());
    5893       194963 :   go_assert(this->closure_var_ == NULL);
    5894       194963 :   if (this->descriptor_ == NULL)
    5895       154640 :     this->descriptor_ = Expression::make_func_descriptor(no);
    5896       194963 :   return this->descriptor_;
    5897              : }
    5898              : 
    5899              : // Get a pointer to the variable representing the defer stack for this
    5900              : // function, making it if necessary.  The value of the variable is set
    5901              : // by the runtime routines to true if the function is returning,
    5902              : // rather than panicing through.  A pointer to this variable is used
    5903              : // as a marker for the functions on the defer stack associated with
    5904              : // this function.  A function-specific variable permits inlining a
    5905              : // function which uses defer.
    5906              : 
    5907              : Expression*
    5908        47518 : Function::defer_stack(Location location)
    5909              : {
    5910        47518 :   if (this->defer_stack_ == NULL)
    5911              :     {
    5912         8354 :       Type* t = Type::lookup_bool_type();
    5913         8354 :       Expression* n = Expression::make_boolean(false, location);
    5914         8354 :       this->defer_stack_ = Statement::make_temporary(t, n, location);
    5915         8354 :       this->defer_stack_->set_is_address_taken();
    5916              :     }
    5917        47518 :   Expression* ref = Expression::make_temporary_reference(this->defer_stack_,
    5918              :                                                          location);
    5919        47518 :   return Expression::make_unary(OPERATOR_AND, ref, location);
    5920              : }
    5921              : 
    5922              : // Export the function.
    5923              : 
    5924              : void
    5925        79191 : Function::export_func(Export* exp, const Named_object* no) const
    5926              : {
    5927        79191 :   Block* block = NULL;
    5928        79191 :   if (this->export_for_inlining())
    5929        16707 :     block = this->block_;
    5930       158382 :   Function::export_func_with_type(exp, no, this->type_, this->results_,
    5931        79191 :                                   this->is_method() && this->nointerface(),
    5932              :                                   this->asm_name(), block, this->location_);
    5933        79191 : }
    5934              : 
    5935              : // Export a function with a type.
    5936              : 
    5937              : void
    5938       380525 : Function::export_func_with_type(Export* exp, const Named_object* no,
    5939              :                                 const Function_type* fntype,
    5940              :                                 Function::Results* result_vars,
    5941              :                                 bool nointerface, const std::string& asm_name,
    5942              :                                 Block* block, Location loc)
    5943              : {
    5944       380525 :   exp->write_c_string("func ");
    5945              : 
    5946       380525 :   if (nointerface)
    5947              :     {
    5948            2 :       go_assert(fntype->is_method());
    5949            2 :       exp->write_c_string("/*nointerface*/ ");
    5950              :     }
    5951              : 
    5952       380525 :   if (!asm_name.empty())
    5953              :     {
    5954         1943 :       exp->write_c_string("/*asm ");
    5955         1943 :       exp->write_string(asm_name);
    5956         1943 :       exp->write_c_string(" */ ");
    5957              :     }
    5958              : 
    5959       380525 :   if (fntype->is_method())
    5960              :     {
    5961       332259 :       exp->write_c_string("(");
    5962       332259 :       const Typed_identifier* receiver = fntype->receiver();
    5963       332259 :       exp->write_name(receiver->name());
    5964       332259 :       exp->write_escape(receiver->note());
    5965       332259 :       exp->write_c_string(" ");
    5966       332259 :       exp->write_type(receiver->type()->unalias());
    5967       332259 :       exp->write_c_string(") ");
    5968              :     }
    5969              : 
    5970       380525 :   if (no->package() != NULL && !fntype->is_method())
    5971              :     {
    5972        12519 :       char buf[50];
    5973        12519 :       snprintf(buf, sizeof buf, "<p%d>", exp->package_index(no->package()));
    5974        12519 :       exp->write_c_string(buf);
    5975              :     }
    5976              : 
    5977       380525 :   const std::string& name(no->name());
    5978       380525 :   if (!Gogo::is_hidden_name(name))
    5979       266401 :     exp->write_string(name);
    5980              :   else
    5981              :     {
    5982       114124 :       exp->write_c_string(".");
    5983       114124 :       exp->write_string(Gogo::unpack_hidden_name(name));
    5984              :     }
    5985              : 
    5986       380525 :   exp->write_c_string(" (");
    5987       380525 :   const Typed_identifier_list* parameters = fntype->parameters();
    5988       380525 :   if (parameters != NULL)
    5989              :     {
    5990       196976 :       bool is_varargs = fntype->is_varargs();
    5991       196976 :       bool first = true;
    5992       502780 :       for (Typed_identifier_list::const_iterator p = parameters->begin();
    5993       502780 :            p != parameters->end();
    5994       305804 :            ++p)
    5995              :         {
    5996       305804 :           if (first)
    5997              :             first = false;
    5998              :           else
    5999       108828 :             exp->write_c_string(", ");
    6000       305804 :           exp->write_name(p->name());
    6001       305804 :           exp->write_escape(p->note());
    6002       305804 :           exp->write_c_string(" ");
    6003       305804 :           if (!is_varargs || p + 1 != parameters->end())
    6004       297156 :             exp->write_type(p->type());
    6005              :           else
    6006              :             {
    6007         8648 :               exp->write_c_string("...");
    6008        17296 :               exp->write_type(p->type()->array_type()->element_type());
    6009              :             }
    6010              :         }
    6011              :     }
    6012       380525 :   exp->write_c_string(")");
    6013              : 
    6014       380525 :   const Typed_identifier_list* result_decls = fntype->results();
    6015       380525 :   if (result_decls != NULL)
    6016              :     {
    6017       285501 :       if (result_decls->size() == 1
    6018       238548 :           && result_decls->begin()->name().empty()
    6019       495866 :           && block == NULL)
    6020              :         {
    6021       202162 :           exp->write_c_string(" ");
    6022       202162 :           exp->write_type(result_decls->begin()->type());
    6023              :         }
    6024              :       else
    6025              :         {
    6026        83339 :           exp->write_c_string(" (");
    6027        83339 :           bool first = true;
    6028        83339 :           Results::const_iterator pr;
    6029        83339 :           if (result_vars != NULL)
    6030        23063 :             pr = result_vars->begin();
    6031       228546 :           for (Typed_identifier_list::const_iterator pd = result_decls->begin();
    6032       228546 :                pd != result_decls->end();
    6033       145207 :                ++pd)
    6034              :             {
    6035       145207 :               if (first)
    6036              :                 first = false;
    6037              :               else
    6038        61868 :                 exp->write_c_string(", ");
    6039              :               // We only use pr->name, which may be artificial, if
    6040              :               // need it for inlining.
    6041       145207 :               if (block == NULL || result_vars == NULL)
    6042       131610 :                 exp->write_name(pd->name());
    6043              :               else
    6044        13597 :                 exp->write_name((*pr)->name());
    6045       145207 :               exp->write_escape(pd->note());
    6046       145207 :               exp->write_c_string(" ");
    6047       145207 :               exp->write_type(pd->type());
    6048       145207 :               if (result_vars != NULL)
    6049        34890 :                 ++pr;
    6050              :             }
    6051        83339 :           if (result_vars != NULL)
    6052        23063 :             go_assert(pr == result_vars->end());
    6053        83339 :           exp->write_c_string(")");
    6054              :         }
    6055              :     }
    6056              : 
    6057       380525 :   if (block == NULL)
    6058       363818 :     exp->write_c_string("\n");
    6059              :   else
    6060              :     {
    6061        16707 :       int indent = 1;
    6062        16707 :       if (fntype->is_method())
    6063         7254 :         indent++;
    6064              : 
    6065        16707 :       Export_function_body efb(exp, indent);
    6066              : 
    6067        16707 :       efb.indent();
    6068        16707 :       efb.write_c_string("// ");
    6069        33414 :       efb.write_string(Linemap::location_to_file(block->start_location()));
    6070        16707 :       efb.write_char(':');
    6071        16707 :       char buf[100];
    6072        16707 :       snprintf(buf, sizeof buf, "%d", Linemap::location_to_line(loc));
    6073        16707 :       efb.write_c_string(buf);
    6074        16707 :       efb.write_char('\n');
    6075        16707 :       block->export_block(&efb);
    6076              : 
    6077        16707 :       const std::string& body(efb.body());
    6078              : 
    6079        16707 :       snprintf(buf, sizeof buf, " <inl:%lu>\n",
    6080        16707 :                static_cast<unsigned long>(body.length()));
    6081        16707 :       exp->write_c_string(buf);
    6082              : 
    6083        16707 :       exp->write_string(body);
    6084        16707 :     }
    6085       380525 : }
    6086              : 
    6087              : // Import a function.
    6088              : 
    6089              : bool
    6090      3055849 : Function::import_func(Import* imp, std::string* pname,
    6091              :                       Package** ppkg, bool* pis_exported,
    6092              :                       Typed_identifier** preceiver,
    6093              :                       Typed_identifier_list** pparameters,
    6094              :                       Typed_identifier_list** presults,
    6095              :                       bool* is_varargs,
    6096              :                       bool* nointerface,
    6097              :                       std::string* asm_name,
    6098              :                       std::string* body)
    6099              : {
    6100      3055849 :   imp->require_c_string("func ");
    6101              : 
    6102      3055849 :   *nointerface = false;
    6103      3168045 :   while (imp->match_c_string("/*"))
    6104              :     {
    6105       112196 :       imp->advance(2);
    6106       112196 :       if (imp->match_c_string("nointerface"))
    6107              :         {
    6108            2 :           imp->require_c_string("nointerface*/ ");
    6109            2 :           *nointerface = true;
    6110              :         }
    6111       112194 :       else if (imp->match_c_string("asm"))
    6112              :         {
    6113       112194 :           imp->require_c_string("asm ");
    6114       112194 :           *asm_name = imp->read_identifier();
    6115       112194 :           imp->require_c_string(" */ ");
    6116              :         }
    6117              :       else
    6118              :         {
    6119            0 :           go_error_at(imp->location(),
    6120              :                       "import error at %d: unrecognized function comment",
    6121              :                       imp->pos());
    6122            0 :           return false;
    6123              :         }
    6124              :     }
    6125              : 
    6126      3055849 :   if (*nointerface)
    6127              :     {
    6128              :       // Only a method can be nointerface.
    6129            2 :       go_assert(imp->peek_char() == '(');
    6130              :     }
    6131              : 
    6132      3055849 :   *preceiver = NULL;
    6133      3055849 :   if (imp->peek_char() == '(')
    6134              :     {
    6135      1784464 :       imp->require_c_string("(");
    6136      1784464 :       std::string name = imp->read_name();
    6137      1784464 :       std::string escape_note = imp->read_escape();
    6138      1784464 :       imp->require_c_string(" ");
    6139      1784464 :       Type* rtype = imp->read_type();
    6140      1784464 :       *preceiver = new Typed_identifier(name, rtype, imp->location());
    6141      1784464 :       (*preceiver)->set_note(escape_note);
    6142      1784464 :       imp->require_c_string(") ");
    6143      1784464 :     }
    6144              : 
    6145      3055849 :   if (!Import::read_qualified_identifier(imp, pname, ppkg, pis_exported))
    6146              :     {
    6147            0 :       go_error_at(imp->location(),
    6148              :                   "import error at %d: bad function name in export data",
    6149              :                   imp->pos());
    6150            0 :       return false;
    6151              :     }
    6152              : 
    6153      3055849 :   Typed_identifier_list* parameters;
    6154      3055849 :   *is_varargs = false;
    6155      3055849 :   imp->require_c_string(" (");
    6156      3055849 :   if (imp->peek_char() == ')')
    6157              :     parameters = NULL;
    6158              :   else
    6159              :     {
    6160      1898752 :       parameters = new Typed_identifier_list();
    6161      4524174 :       while (true)
    6162              :         {
    6163      3211463 :           std::string name = imp->read_name();
    6164      3211463 :           std::string escape_note = imp->read_escape();
    6165      3211463 :           imp->require_c_string(" ");
    6166              : 
    6167      3211463 :           if (imp->match_c_string("..."))
    6168              :             {
    6169        52697 :               imp->advance(3);
    6170        52697 :               *is_varargs = true;
    6171              :             }
    6172              : 
    6173      3211463 :           Type* ptype = imp->read_type();
    6174      3211463 :           if (*is_varargs)
    6175        52697 :             ptype = Type::make_array_type(ptype, NULL);
    6176      3211463 :           Typed_identifier t = Typed_identifier(name, ptype, imp->location());
    6177      3211463 :           t.set_note(escape_note);
    6178      3211463 :           parameters->push_back(t);
    6179      3211463 :           if (imp->peek_char() != ',')
    6180              :             break;
    6181      1312711 :           go_assert(!*is_varargs);
    6182      1312711 :           imp->require_c_string(", ");
    6183      3211463 :         }
    6184              :     }
    6185      3055849 :   imp->require_c_string(")");
    6186      3055849 :   *pparameters = parameters;
    6187              : 
    6188      3055849 :   Typed_identifier_list* results;
    6189      3055849 :   if (imp->peek_char() != ' ' || imp->match_c_string(" <inl"))
    6190              :     results = NULL;
    6191              :   else
    6192              :     {
    6193      2247984 :       results = new Typed_identifier_list();
    6194      2247984 :       imp->require_c_string(" ");
    6195      2247984 :       if (imp->peek_char() != '(')
    6196              :         {
    6197      1422320 :           Type* rtype = imp->read_type();
    6198      2844640 :           results->push_back(Typed_identifier("", rtype, imp->location()));
    6199              :         }
    6200              :       else
    6201              :         {
    6202       825664 :           imp->require_c_string("(");
    6203      1762970 :           while (true)
    6204              :             {
    6205      1294317 :               std::string name = imp->read_name();
    6206      1294317 :               std::string note = imp->read_escape();
    6207      1294317 :               imp->require_c_string(" ");
    6208      1294317 :               Type* rtype = imp->read_type();
    6209      1294317 :               Typed_identifier t = Typed_identifier(name, rtype,
    6210      1294317 :                                                     imp->location());
    6211      1294317 :               t.set_note(note);
    6212      1294317 :               results->push_back(t);
    6213      1294317 :               if (imp->peek_char() != ',')
    6214              :                 break;
    6215       468653 :               imp->require_c_string(", ");
    6216      1294317 :             }
    6217       825664 :           imp->require_c_string(")");
    6218              :         }
    6219              :     }
    6220      3055849 :   *presults = results;
    6221              : 
    6222      3055849 :   if (!imp->match_c_string(" <inl:"))
    6223              :     {
    6224      2549164 :       imp->require_semicolon_if_old_version();
    6225      2549164 :       imp->require_c_string("\n");
    6226      2549164 :       body->clear();
    6227              :     }
    6228              :   else
    6229              :     {
    6230       506685 :       imp->require_c_string(" <inl:");
    6231       506685 :       std::string lenstr;
    6232      1989446 :       int c;
    6233      3472207 :       while (true)
    6234              :         {
    6235      1989446 :           c = imp->peek_char();
    6236      1989446 :           if (c < '0' || c > '9')
    6237              :             break;
    6238      1482761 :           lenstr += c;
    6239      1482761 :           imp->get_char();
    6240              :         }
    6241       506685 :       imp->require_c_string(">\n");
    6242              : 
    6243       506685 :       errno = 0;
    6244       506685 :       char* end;
    6245       506685 :       long llen = strtol(lenstr.c_str(), &end, 10);
    6246       506685 :       if (*end != '\0'
    6247       506685 :           || llen < 0
    6248       506685 :           || (llen == LONG_MAX && errno == ERANGE))
    6249              :         {
    6250            0 :           go_error_at(imp->location(), "invalid inline function length %s",
    6251              :                       lenstr.c_str());
    6252            0 :           return false;
    6253              :         }
    6254              : 
    6255       506685 :       imp->read(static_cast<size_t>(llen), body);
    6256       506685 :     }
    6257              : 
    6258              :   return true;
    6259              : }
    6260              : 
    6261              : // Get the backend name.
    6262              : 
    6263              : void
    6264       453583 : Function::backend_name(Gogo* gogo, Named_object* no, Backend_name *bname)
    6265              : {
    6266       453583 :   if (!this->asm_name_.empty())
    6267       186630 :     bname->set_asm_name(this->asm_name_);
    6268       266953 :   else if (no->package() == NULL && no->name() == gogo->get_init_fn_name())
    6269              :     {
    6270              :       // These names appear in the export data and are used
    6271              :       // directly in the assembler code.  If we change this here
    6272              :       // we need to change Gogo::init_imports.
    6273         3453 :       bname->set_asm_name(no->name());
    6274              :     }
    6275       263500 :   else if (this->enclosing_ != NULL)
    6276              :     {
    6277              :       // Rewrite the nested name to use the enclosing function name.
    6278              :       // We don't do this earlier because we just store simple names
    6279              :       // in a Named_object, not Backend_names.
    6280              : 
    6281              :       // The name was set by nested_function_name, which always
    6282              :       // appends ..funcNNN.  We want that to be our suffix.
    6283        35810 :       size_t pos = no->name().find("..func");
    6284        35810 :       go_assert(pos != std::string::npos);
    6285              : 
    6286        35810 :       Named_object* enclosing = this->enclosing_;
    6287        39375 :       while (true)
    6288              :         {
    6289        39375 :           Named_object* parent = enclosing->func_value()->enclosing();
    6290        39375 :           if (parent == NULL)
    6291              :             break;
    6292              :           enclosing = parent;
    6293              :         }
    6294              : 
    6295        35810 :       Type* rtype = NULL;
    6296        35810 :       if (enclosing->func_value()->type()->is_method())
    6297         4021 :         rtype = enclosing->func_value()->type()->receiver()->type();
    6298        35810 :       gogo->function_backend_name(enclosing->name(), enclosing->package(),
    6299              :                                   rtype, bname);
    6300        35810 :       bname->append_suffix(no->name().substr(pos));
    6301              :     }
    6302              :   else
    6303              :     {
    6304       227690 :       Type* rtype = NULL;
    6305       227690 :       if (this->type_->is_method())
    6306        78028 :         rtype = this->type_->receiver()->type();
    6307       227690 :       gogo->function_backend_name(no->name(), no->package(), rtype, bname);
    6308              :     }
    6309       453583 : }
    6310              : 
    6311              : // Get the backend representation.
    6312              : 
    6313              : Bfunction*
    6314      3815493 : Function::get_or_make_decl(Gogo* gogo, Named_object* no)
    6315              : {
    6316      3815493 :   if (this->fndecl_ == NULL)
    6317              :     {
    6318       298947 :       unsigned int flags = 0;
    6319       298947 :       if (no->package() != NULL)
    6320              :         {
    6321              :           // Functions defined in other packages must be visible.
    6322              :           flags |= Backend::function_is_visible;
    6323              :         }
    6324       286700 :       else if (this->enclosing_ != NULL || Gogo::is_thunk(no))
    6325              :         ;
    6326       246708 :       else if (Gogo::unpack_hidden_name(no->name()) == "init"
    6327       246708 :                && !this->type_->is_method())
    6328              :         ;
    6329       246708 :       else if (no->name() == gogo->get_init_fn_name())
    6330              :         flags |= Backend::function_is_visible;
    6331       243255 :       else if (Gogo::unpack_hidden_name(no->name()) == "main"
    6332       243255 :                && gogo->is_main_package())
    6333              :         flags |= Backend::function_is_visible;
    6334              :       // Methods have to be public even if they are hidden because
    6335              :       // they can be pulled into type descriptors when using
    6336              :       // anonymous fields.
    6337       241491 :       else if (!Gogo::is_hidden_name(no->name())
    6338       241491 :                || this->type_->is_method())
    6339              :         {
    6340       201716 :           if (!this->is_unnamed_type_stub_method_)
    6341       209116 :             flags |= Backend::function_is_visible;
    6342              :         }
    6343              : 
    6344       298947 :       if (!this->asm_name_.empty())
    6345              :         {
    6346              :           // If an assembler name is explicitly specified, there must
    6347              :           // be some reason to refer to the symbol from a different
    6348              :           // object file.
    6349        93778 :           flags |= Backend::function_is_visible;
    6350              :         }
    6351              : 
    6352              :       // If an inline body refers to this function, then it
    6353              :       // needs to be visible in the symbol table.
    6354       298947 :       if (this->is_referenced_by_inline_)
    6355         8157 :         flags |= Backend::function_is_visible;
    6356              : 
    6357              :       // A go:linkname directive can be used to force a function to be
    6358              :       // visible.
    6359       298947 :       if (this->is_exported_by_linkname_)
    6360         1673 :         flags |= Backend::function_is_visible;
    6361              : 
    6362              :       // If a function calls the predeclared recover function, we
    6363              :       // can't inline it, because recover behaves differently in a
    6364              :       // function passed directly to defer.  If this is a recover
    6365              :       // thunk that we built to test whether a function can be
    6366              :       // recovered, we can't inline it, because that will mess up
    6367              :       // our return address comparison.
    6368       298947 :       bool is_inlinable = !(this->calls_recover_ || this->is_recover_thunk_);
    6369              : 
    6370              :       // If a function calls __go_set_defer_retaddr, then mark it as
    6371              :       // uninlinable.  This prevents the GCC backend from splitting
    6372              :       // the function; splitting the function is a bad idea because we
    6373              :       // want the return address label to be in the same function as
    6374              :       // the call.
    6375       298947 :       if (this->calls_defer_retaddr_)
    6376         8840 :         is_inlinable = false;
    6377              : 
    6378              :       // Check the //go:noinline compiler directive.
    6379       298947 :       if ((this->pragmas_ & GOPRAGMA_NOINLINE) != 0)
    6380              :         is_inlinable = false;
    6381              : 
    6382       298587 :       if (is_inlinable)
    6383       288084 :         flags |= Backend::function_is_inlinable;
    6384              : 
    6385              :       // If this is a thunk created to call a function which calls
    6386              :       // the predeclared recover function, we need to disable
    6387              :       // stack splitting for the thunk.
    6388       298947 :       bool disable_split_stack = this->is_recover_thunk_;
    6389              : 
    6390              :       // Check the //go:nosplit compiler directive.
    6391       298947 :       if ((this->pragmas_ & GOPRAGMA_NOSPLIT) != 0)
    6392              :         disable_split_stack = true;
    6393              : 
    6394       297513 :       if (disable_split_stack)
    6395         2264 :         flags |= Backend::function_no_split_stack;
    6396              : 
    6397              :       // This should go into a unique section if that has been
    6398              :       // requested elsewhere, or if this is a nointerface function.
    6399              :       // We want to put a nointerface function into a unique section
    6400              :       // because there is a good chance that the linker garbage
    6401              :       // collection can discard it.
    6402       298947 :       if (this->in_unique_section_
    6403       298947 :           || (this->is_method() && this->nointerface()))
    6404            3 :         flags |= Backend::function_in_unique_section;
    6405              : 
    6406       298947 :       if (this->is_inline_only_)
    6407        12247 :         flags |= Backend::function_only_inline;
    6408              : 
    6409       298947 :       Btype* functype = this->type_->get_backend_fntype(gogo);
    6410              : 
    6411       298947 :       Backend_name bname;
    6412       298947 :       this->backend_name(gogo, no, &bname);
    6413              : 
    6414       597894 :       this->fndecl_ = gogo->backend()->function(functype,
    6415       597894 :                                                 bname.name(),
    6416       298947 :                                                 bname.optional_asm_name(),
    6417              :                                                 flags,
    6418              :                                                 this->location());
    6419       298947 :     }
    6420      3815493 :   return this->fndecl_;
    6421              : }
    6422              : 
    6423              : // Get the backend name.
    6424              : 
    6425              : void
    6426       382393 : Function_declaration::backend_name(Gogo* gogo, Named_object* no,
    6427              :                                    Backend_name* bname)
    6428              : {
    6429       382393 :   if (!this->asm_name_.empty())
    6430       214540 :     bname->set_asm_name(this->asm_name_);
    6431              :   else
    6432              :     {
    6433       167853 :       Type* rtype = NULL;
    6434       167853 :       if (this->fntype_->is_method())
    6435       120044 :         rtype = this->fntype_->receiver()->type();
    6436       167853 :       gogo->function_backend_name(no->name(), no->package(), rtype, bname);
    6437              :     }
    6438       382393 : }
    6439              : 
    6440              : // Get the backend representation.
    6441              : 
    6442              : Bfunction*
    6443      1525106 : Function_declaration::get_or_make_decl(Gogo* gogo, Named_object* no)
    6444              : {
    6445      1525106 :   if (this->fndecl_ == NULL)
    6446              :     {
    6447       283089 :       unsigned int flags =
    6448              :         (Backend::function_is_visible
    6449              :          | Backend::function_is_declaration
    6450              :          | Backend::function_is_inlinable);
    6451              : 
    6452              :       // Let Go code use an asm declaration to pick up a builtin
    6453              :       // function.
    6454       283089 :       if (!this->asm_name_.empty())
    6455              :         {
    6456       130264 :           Bfunction* builtin_decl =
    6457       130264 :             gogo->backend()->lookup_builtin(this->asm_name_);
    6458       130264 :           if (builtin_decl != NULL)
    6459              :             {
    6460         5835 :               this->fndecl_ = builtin_decl;
    6461         5835 :               return this->fndecl_;
    6462              :             }
    6463              : 
    6464       124429 :           if (this->asm_name_ == "runtime.gopanic"
    6465       122279 :               || this->asm_name_.compare(0, 13, "runtime.panic") == 0
    6466       117175 :               || this->asm_name_.compare(0, 15, "runtime.goPanic") == 0
    6467       232787 :               || this->asm_name_ == "runtime.block")
    6468              :             flags |= Backend::function_does_not_return;
    6469              :         }
    6470              : 
    6471       277254 :       Btype* functype = this->fntype_->get_backend_fntype(gogo);
    6472              : 
    6473       277254 :       Backend_name bname;
    6474       277254 :       this->backend_name(gogo, no, &bname);
    6475              : 
    6476       554508 :       this->fndecl_ = gogo->backend()->function(functype,
    6477       554508 :                                                 bname.name(),
    6478       277254 :                                                 bname.optional_asm_name(),
    6479              :                                                 flags,
    6480              :                                                 this->location());
    6481       277254 :     }
    6482              : 
    6483      1519271 :   return this->fndecl_;
    6484              : }
    6485              : 
    6486              : // Build the descriptor for a function declaration.  This won't
    6487              : // necessarily happen if the package has just a declaration for the
    6488              : // function and no other reference to it, but we may still need the
    6489              : // descriptor for references from other packages.
    6490              : void
    6491         3904 : Function_declaration::build_backend_descriptor(Gogo* gogo)
    6492              : {
    6493         3904 :   if (this->descriptor_ != NULL)
    6494              :     {
    6495          650 :       Translate_context context(gogo, NULL, NULL, NULL);
    6496          650 :       this->descriptor_->get_backend(&context);
    6497              :     }
    6498         3904 : }
    6499              : 
    6500              : // Check that the types used in this declaration's signature are defined.
    6501              : // Reports errors for any undefined type.
    6502              : 
    6503              : void
    6504         3904 : Function_declaration::check_types() const
    6505              : {
    6506              :   // Calling Type::base will give errors for any undefined types.
    6507         3904 :   Function_type* fntype = this->type();
    6508         3904 :   if (fntype->receiver() != NULL)
    6509            0 :     fntype->receiver()->type()->base();
    6510         3904 :   if (fntype->parameters() != NULL)
    6511              :     {
    6512         3035 :       const Typed_identifier_list* params = fntype->parameters();
    6513         9609 :       for (Typed_identifier_list::const_iterator p = params->begin();
    6514         9609 :            p != params->end();
    6515         6574 :            ++p)
    6516         6574 :         p->type()->base();
    6517              :     }
    6518         3904 : }
    6519              : 
    6520              : // Return the function's decl after it has been built.
    6521              : 
    6522              : Bfunction*
    6523      9319223 : Function::get_decl() const
    6524              : {
    6525      9319223 :   go_assert(this->fndecl_ != NULL);
    6526      9319223 :   return this->fndecl_;
    6527              : }
    6528              : 
    6529              : // Build the backend representation for the function code.
    6530              : 
    6531              : void
    6532       295494 : Function::build(Gogo* gogo, Named_object* named_function)
    6533              : {
    6534       295494 :   Translate_context context(gogo, named_function, NULL, NULL);
    6535              : 
    6536              :   // A list of parameter variables for this function.
    6537       295494 :   std::vector<Bvariable*> param_vars;
    6538              : 
    6539              :   // Variables that need to be declared for this function and their
    6540              :   // initial values.
    6541       295494 :   std::vector<Bvariable*> vars;
    6542       295494 :   std::vector<Expression*> var_inits;
    6543       295494 :   std::vector<Statement*> var_decls_stmts;
    6544      1216747 :   for (Bindings::const_definitions_iterator p =
    6545       295494 :          this->block_->bindings()->begin_definitions();
    6546      1216747 :        p != this->block_->bindings()->end_definitions();
    6547       921253 :        ++p)
    6548              :     {
    6549       921253 :       Location loc = (*p)->location();
    6550       921253 :       if ((*p)->is_variable() && (*p)->var_value()->is_parameter())
    6551              :         {
    6552       474166 :           Bvariable* bvar = (*p)->get_backend_variable(gogo, named_function);
    6553       474166 :           Bvariable* parm_bvar = bvar;
    6554              : 
    6555              :           // We always pass the receiver to a method as a pointer.  If
    6556              :           // the receiver is declared as a non-pointer type, then we
    6557              :           // copy the value into a local variable.  For direct interface
    6558              :           // type we pack the pointer into the type.
    6559       474166 :           if ((*p)->var_value()->is_receiver()
    6560       474166 :               && (*p)->var_value()->type()->points_to() == NULL)
    6561              :             {
    6562        15151 :               std::string name = (*p)->name() + ".pointer";
    6563        15151 :               Type* var_type = (*p)->var_value()->type();
    6564        15151 :               Variable* parm_var =
    6565        15151 :                   new Variable(Type::make_pointer_type(var_type), NULL, false,
    6566        30302 :                                true, false, loc);
    6567        15151 :               Named_object* parm_no =
    6568        15151 :                   Named_object::make_variable(name, NULL, parm_var);
    6569        15151 :               parm_bvar = parm_no->get_backend_variable(gogo, named_function);
    6570              : 
    6571        15151 :               vars.push_back(bvar);
    6572              : 
    6573        15151 :               Expression* parm_ref =
    6574        15151 :                   Expression::make_var_reference(parm_no, loc);
    6575        15151 :               Type* recv_type = (*p)->var_value()->type();
    6576        15151 :               if (recv_type->is_direct_iface_type())
    6577         2016 :                 parm_ref = Expression::pack_direct_iface(recv_type, parm_ref, loc);
    6578              :               else
    6579        13135 :                 parm_ref =
    6580        13135 :                     Expression::make_dereference(parm_ref,
    6581              :                                                  Expression::NIL_CHECK_NEEDED,
    6582              :                                                  loc);
    6583        15151 :               if ((*p)->var_value()->is_in_heap())
    6584         1222 :                 parm_ref = Expression::make_heap_expression(parm_ref, loc);
    6585        15151 :               var_inits.push_back(parm_ref);
    6586        15151 :             }
    6587       459015 :           else if ((*p)->var_value()->is_in_heap())
    6588              :             {
    6589              :               // If we take the address of a parameter, then we need
    6590              :               // to copy it into the heap.
    6591         3001 :               std::string parm_name = (*p)->name() + ".param";
    6592         6002 :               Variable* parm_var = new Variable((*p)->var_value()->type(), NULL,
    6593         3001 :                                                 false, true, false, loc);
    6594         3001 :               Named_object* parm_no =
    6595         3001 :                   Named_object::make_variable(parm_name, NULL, parm_var);
    6596         3001 :               parm_bvar = parm_no->get_backend_variable(gogo, named_function);
    6597              : 
    6598         3001 :               vars.push_back(bvar);
    6599         3001 :               Expression* var_ref =
    6600         3001 :                   Expression::make_var_reference(parm_no, loc);
    6601         3001 :               var_ref = Expression::make_heap_expression(var_ref, loc);
    6602         3001 :               var_inits.push_back(var_ref);
    6603         3001 :             }
    6604       474166 :           param_vars.push_back(parm_bvar);
    6605              :         }
    6606       447087 :       else if ((*p)->is_result_variable())
    6607              :         {
    6608       244740 :           Bvariable* bvar = (*p)->get_backend_variable(gogo, named_function);
    6609              : 
    6610       244740 :           Type* type = (*p)->result_var_value()->type();
    6611       244740 :           Expression* init;
    6612       244740 :           if (!(*p)->result_var_value()->is_in_heap())
    6613              :             {
    6614       244618 :               Btype* btype = type->get_backend(gogo);
    6615       244618 :               Bexpression* binit = gogo->backend()->zero_expression(btype);
    6616       244618 :               init = Expression::make_backend(binit, type, loc);
    6617              :             }
    6618              :           else
    6619          122 :             init = Expression::make_allocation(type, loc);
    6620              : 
    6621       244740 :           vars.push_back(bvar);
    6622       244740 :           var_inits.push_back(init);
    6623              :         }
    6624       202347 :       else if (this->defer_stack_ != NULL
    6625        23528 :                && (*p)->is_variable()
    6626        22838 :                && (*p)->var_value()->is_non_escaping_address_taken()
    6627       947277 :                && !(*p)->var_value()->is_in_heap())
    6628              :         {
    6629              :           // Local variable captured by deferred closure needs to be live
    6630              :           // until the end of the function. We create a top-level
    6631              :           // declaration for it.
    6632              :           // TODO: we don't need to do this if the variable is not captured
    6633              :           // by the defer closure. There is no easy way to check it here,
    6634              :           // so we do this for all address-taken variables for now.
    6635         1585 :           Variable* var = (*p)->var_value();
    6636         1585 :           Temporary_statement* ts =
    6637         1585 :             Statement::make_temporary(var->type(), NULL, var->location());
    6638         1585 :           ts->set_is_address_taken();
    6639         1585 :           var->set_toplevel_decl(ts);
    6640         1585 :           var_decls_stmts.push_back(ts);
    6641              :         }
    6642              :     }
    6643       295494 :   if (!gogo->backend()->function_set_parameters(this->fndecl_, param_vars))
    6644              :     {
    6645           14 :       go_assert(saw_errors());
    6646              :       return;
    6647              :     }
    6648              : 
    6649              :   // If we need a closure variable, make sure to create it.
    6650              :   // It gets installed in the function as a side effect of creation.
    6651       295480 :   if (this->closure_var_ != NULL)
    6652              :     {
    6653        14291 :       go_assert(this->closure_var_->var_value()->is_closure());
    6654        14291 :       this->closure_var_->get_backend_variable(gogo, named_function);
    6655              :     }
    6656              : 
    6657       295480 :   if (this->block_ != NULL)
    6658              :     {
    6659              :       // Declare variables if necessary.
    6660       295480 :       Bblock* var_decls = NULL;
    6661       295480 :       std::vector<Bstatement*> var_decls_bstmt_list;
    6662       295480 :       Bstatement* defer_init = NULL;
    6663       295480 :       if (!vars.empty() || this->defer_stack_ != NULL)
    6664              :         {
    6665       224897 :           var_decls =
    6666       449794 :               gogo->backend()->block(this->fndecl_, NULL, vars,
    6667       224897 :                                      this->block_->start_location(),
    6668       224897 :                                      this->block_->end_location());
    6669              : 
    6670       224897 :           if (this->defer_stack_ != NULL)
    6671              :             {
    6672         8353 :               Translate_context dcontext(gogo, named_function, this->block_,
    6673         8353 :                                          var_decls);
    6674         8353 :               defer_init = this->defer_stack_->get_backend(&dcontext);
    6675         8353 :               var_decls_bstmt_list.push_back(defer_init);
    6676         9938 :               for (std::vector<Statement*>::iterator p = var_decls_stmts.begin();
    6677         9938 :                    p != var_decls_stmts.end();
    6678         1585 :                    ++p)
    6679              :                 {
    6680         1585 :                   Bstatement* bstmt = (*p)->get_backend(&dcontext);
    6681         1585 :                   var_decls_bstmt_list.push_back(bstmt);
    6682              :                 }
    6683              :             }
    6684              :         }
    6685              : 
    6686              :       // Build the backend representation for all the statements in the
    6687              :       // function.
    6688       295480 :       Translate_context bcontext(gogo, named_function, NULL, NULL);
    6689       295480 :       Bblock* code_block = this->block_->get_backend(&bcontext);
    6690              : 
    6691              :       // Initialize variables if necessary.
    6692       295480 :       Translate_context icontext(gogo, named_function, this->block_,
    6693       295480 :                                  var_decls);
    6694       295480 :       std::vector<Bstatement*> init;
    6695       295480 :       go_assert(vars.size() == var_inits.size());
    6696       558362 :       for (size_t i = 0; i < vars.size(); ++i)
    6697              :         {
    6698       262882 :           Bexpression* binit = var_inits[i]->get_backend(&icontext);
    6699       262882 :           Bstatement* init_stmt =
    6700       262882 :               gogo->backend()->init_statement(this->fndecl_, vars[i],
    6701       262882 :                                               binit);
    6702       262882 :           init.push_back(init_stmt);
    6703              :         }
    6704       295480 :       Bstatement* var_init = gogo->backend()->statement_list(init);
    6705              : 
    6706              :       // Initialize all variables before executing this code block.
    6707       295480 :       Bstatement* code_stmt = gogo->backend()->block_statement(code_block);
    6708       295480 :       code_stmt = gogo->backend()->compound_statement(var_init, code_stmt);
    6709              : 
    6710              :       // If we have a defer stack, initialize it at the start of a
    6711              :       // function.
    6712       295480 :       Bstatement* except = NULL;
    6713       295480 :       Bstatement* fini = NULL;
    6714       295480 :       if (defer_init != NULL)
    6715              :         {
    6716              :           // Clean up the defer stack when we leave the function.
    6717         8353 :           this->build_defer_wrapper(gogo, named_function, &except, &fini);
    6718              : 
    6719              :           // Wrap the code for this function in an exception handler to handle
    6720              :           // defer calls.
    6721         8353 :           code_stmt =
    6722         8353 :               gogo->backend()->exception_handler_statement(code_stmt,
    6723              :                                                            except, fini,
    6724              :                                                            this->location_);
    6725              :         }
    6726              : 
    6727              :       // Stick the code into the block we built for the receiver, if
    6728              :       // we built one.
    6729       295480 :       if (var_decls != NULL)
    6730              :         {
    6731       224897 :           var_decls_bstmt_list.push_back(code_stmt);
    6732       224897 :           gogo->backend()->block_add_statements(var_decls, var_decls_bstmt_list);
    6733       224897 :           code_stmt = gogo->backend()->block_statement(var_decls);
    6734              :         }
    6735              : 
    6736       295480 :       if (!gogo->backend()->function_set_body(this->fndecl_, code_stmt))
    6737              :         {
    6738            0 :           go_assert(saw_errors());
    6739            0 :           return;
    6740              :         }
    6741       295480 :     }
    6742              : 
    6743              :   // If we created a descriptor for the function, make sure we emit it.
    6744       295480 :   if (this->descriptor_ != NULL)
    6745              :     {
    6746       154636 :       Translate_context dcontext(gogo, NULL, NULL, NULL);
    6747       154636 :       this->descriptor_->get_backend(&dcontext);
    6748              :     }
    6749       295494 : }
    6750              : 
    6751              : // Build the wrappers around function code needed if the function has
    6752              : // any defer statements.  This sets *EXCEPT to an exception handler
    6753              : // and *FINI to a finally handler.
    6754              : 
    6755              : void
    6756         8353 : Function::build_defer_wrapper(Gogo* gogo, Named_object* named_function,
    6757              :                               Bstatement** except, Bstatement** fini)
    6758              : {
    6759         8353 :   Location end_loc = this->block_->end_location();
    6760              : 
    6761              :   // Add an exception handler.  This is used if a panic occurs.  Its
    6762              :   // purpose is to stop the stack unwinding if a deferred function
    6763              :   // calls recover.  There are more details in
    6764              :   // libgo/runtime/go-unwind.c.
    6765              : 
    6766         8353 :   std::vector<Bstatement*> stmts;
    6767         8353 :   Expression* call = Runtime::make_call(gogo, Runtime::CHECKDEFER, end_loc, 1,
    6768              :                                         this->defer_stack(end_loc));
    6769         8353 :   Translate_context context(gogo, named_function, NULL, NULL);
    6770         8353 :   Bexpression* defer = call->get_backend(&context);
    6771         8353 :   stmts.push_back(gogo->backend()->expression_statement(this->fndecl_, defer));
    6772              : 
    6773         8353 :   Bstatement* ret_bstmt = this->return_value(gogo, named_function, end_loc);
    6774         8353 :   if (ret_bstmt != NULL)
    6775         3600 :     stmts.push_back(ret_bstmt);
    6776              : 
    6777         8353 :   go_assert(*except == NULL);
    6778         8353 :   *except = gogo->backend()->statement_list(stmts);
    6779              : 
    6780         8353 :   call = Runtime::make_call(gogo, Runtime::CHECKDEFER, end_loc, 1,
    6781              :                             this->defer_stack(end_loc));
    6782         8353 :   defer = call->get_backend(&context);
    6783              : 
    6784         8353 :   call = Runtime::make_call(gogo, Runtime::DEFERRETURN, end_loc, 1,
    6785              :                             this->defer_stack(end_loc));
    6786         8353 :   Bexpression* undefer = call->get_backend(&context);
    6787         8353 :   Bstatement* function_defer =
    6788         8353 :       gogo->backend()->function_defer_statement(this->fndecl_, undefer, defer,
    6789         8353 :                                                 end_loc);
    6790         8353 :   stmts = std::vector<Bstatement*>(1, function_defer);
    6791         8353 :   if (this->type_->results() != NULL
    6792         3600 :       && !this->type_->results()->empty()
    6793        11953 :       && !this->type_->results()->front().name().empty())
    6794              :     {
    6795              :       // If the result variables are named, and we are returning from
    6796              :       // this function rather than panicing through it, we need to
    6797              :       // return them again, because they might have been changed by a
    6798              :       // defer function.  The runtime routines set the defer_stack
    6799              :       // variable to true if we are returning from this function.
    6800              : 
    6801         1020 :       ret_bstmt = this->return_value(gogo, named_function, end_loc);
    6802         1020 :       Bexpression* nil = Expression::make_nil(end_loc)->get_backend(&context);
    6803         1020 :       Bexpression* ret =
    6804         1020 :           gogo->backend()->compound_expression(ret_bstmt, nil, end_loc);
    6805         1020 :       Expression* ref =
    6806         1020 :         Expression::make_temporary_reference(this->defer_stack_, end_loc);
    6807         1020 :       Bexpression* bref = ref->get_backend(&context);
    6808         1020 :       ret = gogo->backend()->conditional_expression(this->fndecl_,
    6809              :                                                     NULL, bref, ret, NULL,
    6810              :                                                     end_loc);
    6811         1020 :       stmts.push_back(gogo->backend()->expression_statement(this->fndecl_, ret));
    6812              :     }
    6813              : 
    6814         8353 :   go_assert(*fini == NULL);
    6815         8353 :   *fini = gogo->backend()->statement_list(stmts);
    6816         8353 : }
    6817              : 
    6818              : // Return the statement that assigns values to this function's result struct.
    6819              : 
    6820              : Bstatement*
    6821         9373 : Function::return_value(Gogo* gogo, Named_object* named_function,
    6822              :                        Location location) const
    6823              : {
    6824         9373 :   const Typed_identifier_list* results = this->type_->results();
    6825         9373 :   if (results == NULL || results->empty())
    6826              :     return NULL;
    6827              : 
    6828         4620 :   go_assert(this->results_ != NULL);
    6829         4620 :   if (this->results_->size() != results->size())
    6830              :     {
    6831            0 :       go_assert(saw_errors());
    6832            0 :       return gogo->backend()->error_statement();
    6833              :     }
    6834              : 
    6835         4620 :   std::vector<Bexpression*> vals(results->size());
    6836        11463 :   for (size_t i = 0; i < vals.size(); ++i)
    6837              :     {
    6838         6843 :       Named_object* no = (*this->results_)[i];
    6839         6843 :       Bvariable* bvar = no->get_backend_variable(gogo, named_function);
    6840         6843 :       Bexpression* val = gogo->backend()->var_expression(bvar, location);
    6841         6843 :       if (no->result_var_value()->is_in_heap())
    6842              :         {
    6843          112 :           Btype* bt = no->result_var_value()->type()->get_backend(gogo);
    6844          112 :           val = gogo->backend()->indirect_expression(bt, val, true, location);
    6845              :         }
    6846         6843 :       vals[i] = val;
    6847              :     }
    6848         4620 :   return gogo->backend()->return_statement(this->fndecl_, vals, location);
    6849         4620 : }
    6850              : 
    6851              : // Class Block.
    6852              : 
    6853      2715775 : Block::Block(Block* enclosing, Location location)
    6854      2715775 :   : enclosing_(enclosing), statements_(),
    6855      2715775 :     bindings_(new Bindings(enclosing == NULL
    6856              :                            ? NULL
    6857      2715775 :                            : enclosing->bindings())),
    6858      2715775 :     start_location_(location),
    6859      2715775 :     end_location_(Linemap::unknown_location())
    6860              : {
    6861      2715775 : }
    6862              : 
    6863              : // Add a statement to a block.
    6864              : 
    6865              : void
    6866      5240279 : Block::add_statement(Statement* statement)
    6867              : {
    6868      5240279 :   this->statements_.push_back(statement);
    6869      5240279 : }
    6870              : 
    6871              : // Add a statement to the front of a block.  This is slow but is only
    6872              : // used for reference counts of parameters.
    6873              : 
    6874              : void
    6875         4935 : Block::add_statement_at_front(Statement* statement)
    6876              : {
    6877         4935 :   this->statements_.insert(this->statements_.begin(), statement);
    6878         4935 : }
    6879              : 
    6880              : // Replace a statement in a block.
    6881              : 
    6882              : void
    6883       747805 : Block::replace_statement(size_t index, Statement* s)
    6884              : {
    6885       747805 :   go_assert(index < this->statements_.size());
    6886       747805 :   this->statements_[index] = s;
    6887       747805 : }
    6888              : 
    6889              : // Add a statement before another statement.
    6890              : 
    6891              : void
    6892      1792861 : Block::insert_statement_before(size_t index, Statement* s)
    6893              : {
    6894      1792861 :   go_assert(index < this->statements_.size());
    6895      1792861 :   this->statements_.insert(this->statements_.begin() + index, s);
    6896      1792861 : }
    6897              : 
    6898              : // Add a statement after another statement.
    6899              : 
    6900              : void
    6901            0 : Block::insert_statement_after(size_t index, Statement* s)
    6902              : {
    6903            0 :   go_assert(index < this->statements_.size());
    6904            0 :   this->statements_.insert(this->statements_.begin() + index + 1, s);
    6905            0 : }
    6906              : 
    6907              : // Traverse the tree.
    6908              : 
    6909              : int
    6910     43295454 : Block::traverse(Traverse* traverse)
    6911              : {
    6912     43295454 :   unsigned int traverse_mask = traverse->traverse_mask();
    6913              : 
    6914     43295454 :   if ((traverse_mask & Traverse::traverse_blocks) != 0)
    6915              :     {
    6916      8958855 :       int t = traverse->block(this);
    6917      8958855 :       if (t == TRAVERSE_EXIT)
    6918              :         return TRAVERSE_EXIT;
    6919      8958855 :       else if (t == TRAVERSE_SKIP_COMPONENTS)
    6920              :         return TRAVERSE_CONTINUE;
    6921              :     }
    6922              : 
    6923     39914953 :   if ((traverse_mask
    6924     39914953 :        & (Traverse::traverse_variables
    6925              :           | Traverse::traverse_constants
    6926              :           | Traverse::traverse_expressions
    6927              :           | Traverse::traverse_types)) != 0)
    6928              :     {
    6929     22170262 :       for (Bindings::const_definitions_iterator pb =
    6930     34999445 :              this->bindings_->begin_definitions();
    6931     57169707 :            pb != this->bindings_->end_definitions();
    6932     22170262 :            ++pb)
    6933              :         {
    6934     22770074 :           if ((*pb)->traverse(traverse, false) == TRAVERSE_EXIT)
    6935      3016289 :             return TRAVERSE_EXIT;
    6936              :         }
    6937              :     }
    6938              : 
    6939              :   // No point in checking traverse_mask here--if we got here we always
    6940              :   // want to walk the statements.  The traversal can insert new
    6941              :   // statements before or after the current statement.  Inserting
    6942              :   // statements before the current statement requires updating I via
    6943              :   // the pointer; those statements will not be traversed.  Any new
    6944              :   // statements inserted after the current statement will be traversed
    6945              :   // in their turn.
    6946    130621692 :   for (size_t i = 0; i < this->statements_.size(); ++i)
    6947              :     {
    6948     93723028 :       if (this->statements_[i]->traverse(this, &i, traverse) == TRAVERSE_EXIT)
    6949      2416477 :         return TRAVERSE_EXIT;
    6950              :     }
    6951              : 
    6952     36898664 :   return TRAVERSE_CONTINUE;
    6953              : }
    6954              : 
    6955              : // Work out types for unspecified variables and constants.
    6956              : 
    6957              : void
    6958      2102767 : Block::determine_types(Gogo* gogo)
    6959              : {
    6960       842541 :   for (Bindings::const_definitions_iterator pb =
    6961      2102767 :          this->bindings_->begin_definitions();
    6962      2945308 :        pb != this->bindings_->end_definitions();
    6963       842541 :        ++pb)
    6964              :     {
    6965       842541 :       if ((*pb)->is_variable())
    6966       692848 :         (*pb)->var_value()->determine_type(gogo);
    6967       149693 :       else if ((*pb)->is_const())
    6968         5531 :         (*pb)->const_value()->determine_type(gogo);
    6969              :     }
    6970              : 
    6971      5569777 :   for (std::vector<Statement*>::const_iterator ps = this->statements_.begin();
    6972      5569777 :        ps != this->statements_.end();
    6973      3467010 :        ++ps)
    6974      3467010 :     (*ps)->determine_types(gogo);
    6975      2102767 : }
    6976              : 
    6977              : // Return true if the statements in this block may fall through.
    6978              : 
    6979              : bool
    6980       113775 : Block::may_fall_through() const
    6981              : {
    6982       113775 :   if (this->statements_.empty())
    6983              :     return true;
    6984       113732 :   return this->statements_.back()->may_fall_through();
    6985              : }
    6986              : 
    6987              : // Write export data for a block.
    6988              : 
    6989              : void
    6990        44123 : Block::export_block(Export_function_body* efb)
    6991              : {
    6992       122751 :   for (Block::iterator p = this->begin();
    6993       122751 :        p != this->end();
    6994        78628 :        ++p)
    6995              :     {
    6996        78628 :       efb->indent();
    6997              : 
    6998        78628 :       efb->increment_indent();
    6999        78628 :       (*p)->export_statement(efb);
    7000        78628 :       efb->decrement_indent();
    7001              : 
    7002        78628 :       Location loc = (*p)->location();
    7003        78628 :       if ((*p)->is_block_statement())
    7004              :         {
    7005              :           // For a block we put the start location on the first brace
    7006              :           // in Block_statement::do_export_statement.  Here we put the
    7007              :           // end location on the final brace.
    7008        22817 :           loc = (*p)->block_statement()->block()->end_location();
    7009              :         }
    7010        78628 :       char buf[50];
    7011        78628 :       snprintf(buf, sizeof buf, " //%d\n", Linemap::location_to_line(loc));
    7012        78628 :       efb->write_c_string(buf);
    7013              :     }
    7014        44123 : }
    7015              : 
    7016              : // Add exported block data to SET, reading from BODY starting at OFF.
    7017              : // Returns whether the import succeeded.
    7018              : 
    7019              : bool
    7020        38869 : Block::import_block(Block* set, Import_function_body *ifb, Location loc)
    7021              : {
    7022        38869 :   Location eloc = ifb->location();
    7023        38869 :   Location sloc = loc;
    7024        38869 :   const std::string& body(ifb->body());
    7025        38869 :   size_t off = ifb->off();
    7026       108195 :   while (off < body.length())
    7027              :     {
    7028        95948 :       int indent = ifb->indent();
    7029        95948 :       if (off + indent >= body.length())
    7030              :         {
    7031            0 :           go_error_at(eloc,
    7032              :                       "invalid export data for %qs: insufficient indentation",
    7033            0 :                       ifb->name().c_str());
    7034            0 :           return false;
    7035              :         }
    7036       245244 :       for (int i = 0; i < indent - 1; i++)
    7037              :         {
    7038       149296 :           if (body[off + i] != ' ')
    7039              :             {
    7040            0 :               go_error_at(eloc,
    7041              :                           "invalid export data for %qs: bad indentation",
    7042            0 :                           ifb->name().c_str());
    7043            0 :               return false;
    7044              :             }
    7045              :         }
    7046              : 
    7047        95948 :       bool at_end = false;
    7048        95948 :       if (body[off + indent - 1] == '}')
    7049              :         at_end = true;
    7050        69326 :       else if (body[off + indent - 1] != ' ')
    7051              :         {
    7052            0 :           go_error_at(eloc,
    7053              :                       "invalid export data for %qs: bad indentation",
    7054            0 :                       ifb->name().c_str());
    7055            0 :           return false;
    7056              :         }
    7057              : 
    7058        95948 :       off += indent;
    7059              : 
    7060        95948 :       size_t nl = body.find('\n', off);
    7061        95948 :       if (nl == std::string::npos)
    7062              :         {
    7063            0 :           go_error_at(eloc, "invalid export data for %qs: missing newline",
    7064            0 :                       ifb->name().c_str());
    7065            0 :           return false;
    7066              :         }
    7067              : 
    7068        95948 :       size_t lineno_pos = body.find(" //", off);
    7069        95948 :       if (lineno_pos == std::string::npos || lineno_pos >= nl)
    7070              :         {
    7071            0 :           go_error_at(eloc, "invalid export data for %qs: missing line number",
    7072            0 :                       ifb->name().c_str());
    7073            0 :           return false;
    7074              :         }
    7075              : 
    7076        95948 :       unsigned int lineno = 0;
    7077       333045 :       for (size_t i = lineno_pos + 3; i < nl; ++i)
    7078              :         {
    7079       237097 :           char c = body[i];
    7080       237097 :           if (c < '0' || c > '9')
    7081              :             {
    7082            0 :               go_error_at(loc,
    7083              :                           "invalid export data for %qs: invalid line number",
    7084            0 :                           ifb->name().c_str());
    7085            0 :               return false;
    7086              :             }
    7087       237097 :           lineno = lineno * 10 + c - '0';
    7088              :         }
    7089              : 
    7090        95948 :       ifb->gogo()->linemap()->start_line(lineno, 1);
    7091        95948 :       sloc = ifb->gogo()->linemap()->get_location(0);
    7092              : 
    7093        95948 :       if (at_end)
    7094              :         {
    7095              :           // An if statement can have an "else" following the "}", in
    7096              :           // which case we want to leave the offset where it is, just
    7097              :           // after the "}".  We don't get the block ending location
    7098              :           // quite right for if statements.
    7099        26622 :           if (body.compare(off, 6, " else ") != 0)
    7100        26486 :             off = nl + 1;
    7101              :           break;
    7102              :         }
    7103              : 
    7104        69326 :       ifb->set_off(off);
    7105        69326 :       Statement* s = Statement::import_statement(ifb, sloc);
    7106        69326 :       if (s == NULL)
    7107              :         return false;
    7108              : 
    7109        69326 :       set->add_statement(s);
    7110              : 
    7111        69326 :       size_t at = ifb->off();
    7112        69326 :       if (at < nl + 1)
    7113              :         off = nl + 1;
    7114              :       else
    7115              :         off = at;
    7116              :     }
    7117              : 
    7118        38869 :   ifb->set_off(off);
    7119        38869 :   set->set_end_location(sloc);
    7120        38869 :   return true;
    7121              : }
    7122              : 
    7123              : // Convert a block to the backend representation.
    7124              : 
    7125              : Bblock*
    7126      2631804 : Block::get_backend(Translate_context* context)
    7127              : {
    7128      2631804 :   Gogo* gogo = context->gogo();
    7129      2631804 :   Named_object* function = context->function();
    7130      2631804 :   std::vector<Bvariable*> vars;
    7131      2631804 :   vars.reserve(this->bindings_->size_definitions());
    7132      1108071 :   for (Bindings::const_definitions_iterator pv =
    7133      2631804 :          this->bindings_->begin_definitions();
    7134      3739875 :        pv != this->bindings_->end_definitions();
    7135      1108071 :        ++pv)
    7136              :     {
    7137      1108071 :       if ((*pv)->is_variable() && !(*pv)->var_value()->is_parameter())
    7138       382504 :         vars.push_back((*pv)->get_backend_variable(gogo, function));
    7139              :     }
    7140              : 
    7141      2631804 :   go_assert(function != NULL);
    7142      2631804 :   Bfunction* bfunction =
    7143      2631804 :     function->func_value()->get_or_make_decl(gogo, function);
    7144      2631804 :   Bblock* ret = context->backend()->block(bfunction, context->bblock(),
    7145              :                                           vars, this->start_location_,
    7146              :                                           this->end_location_);
    7147              : 
    7148      2631804 :   Translate_context subcontext(gogo, function, this, ret);
    7149      2631804 :   std::vector<Bstatement*> bstatements;
    7150      2631804 :   bstatements.reserve(this->statements_.size());
    7151      2631804 :   for (std::vector<Statement*>::const_iterator p = this->statements_.begin();
    7152      9648688 :        p != this->statements_.end();
    7153      7016884 :        ++p)
    7154      7016884 :     bstatements.push_back((*p)->get_backend(&subcontext));
    7155              : 
    7156      2631804 :   context->backend()->block_add_statements(ret, bstatements);
    7157              : 
    7158      2631804 :   return ret;
    7159      2631804 : }
    7160              : 
    7161              : // Class Bindings_snapshot.
    7162              : 
    7163        12092 : Bindings_snapshot::Bindings_snapshot(const Block* b, Location location)
    7164        12092 :   : block_(b), counts_(), location_(location)
    7165              : {
    7166        40730 :   while (b != NULL)
    7167              :     {
    7168        28638 :       this->counts_.push_back(b->bindings()->size_definitions());
    7169        28638 :       b = b->enclosing();
    7170              :     }
    7171        12092 : }
    7172              : 
    7173              : // Report errors appropriate for a goto from B to this.
    7174              : 
    7175              : void
    7176          533 : Bindings_snapshot::check_goto_from(const Block* b, Location loc)
    7177              : {
    7178          533 :   size_t dummy;
    7179          533 :   if (!this->check_goto_block(loc, b, this->block_, &dummy))
    7180           12 :     return;
    7181          521 :   this->check_goto_defs(loc, this->block_,
    7182          521 :                         this->block_->bindings()->size_definitions(),
    7183          521 :                         this->counts_[0]);
    7184              : }
    7185              : 
    7186              : // Report errors appropriate for a goto from this to B.
    7187              : 
    7188              : void
    7189         1565 : Bindings_snapshot::check_goto_to(const Block* b)
    7190              : {
    7191         1565 :   size_t index;
    7192         1565 :   if (!this->check_goto_block(this->location_, this->block_, b, &index))
    7193           19 :     return;
    7194         1546 :   this->check_goto_defs(this->location_, b, this->counts_[index],
    7195              :                         b->bindings()->size_definitions());
    7196              : }
    7197              : 
    7198              : // Report errors appropriate for a goto at LOC from BFROM to BTO.
    7199              : // Return true if all is well, false if we reported an error.  If this
    7200              : // returns true, it sets *PINDEX to the number of blocks BTO is above
    7201              : // BFROM.
    7202              : 
    7203              : bool
    7204         2098 : Bindings_snapshot::check_goto_block(Location loc, const Block* bfrom,
    7205              :                                     const Block* bto, size_t* pindex)
    7206              : {
    7207              :   // It is an error if BTO is not either BFROM or above BFROM.
    7208         2098 :   size_t index = 0;
    7209         9845 :   for (const Block* pb = bfrom; pb != bto; pb = pb->enclosing(), ++index)
    7210              :     {
    7211         7778 :       if (pb == NULL)
    7212              :         {
    7213           31 :           go_error_at(loc, "goto jumps into block");
    7214           31 :           go_inform(bto->start_location(), "goto target block starts here");
    7215           31 :           return false;
    7216              :         }
    7217              :     }
    7218         2067 :   *pindex = index;
    7219         2067 :   return true;
    7220              : }
    7221              : 
    7222              : // Report errors appropriate for a goto at LOC ending at BLOCK, where
    7223              : // CFROM is the number of names defined at the point of the goto and
    7224              : // CTO is the number of names defined at the point of the label.
    7225              : 
    7226              : void
    7227         2067 : Bindings_snapshot::check_goto_defs(Location loc, const Block* block,
    7228              :                                    size_t cfrom, size_t cto)
    7229              : {
    7230         2067 :   if (cfrom < cto)
    7231              :     {
    7232           14 :       Bindings::const_definitions_iterator p =
    7233           14 :         block->bindings()->begin_definitions();
    7234           31 :       for (size_t i = 0; i < cfrom; ++i)
    7235              :         {
    7236           17 :           go_assert(p != block->bindings()->end_definitions());
    7237           17 :           ++p;
    7238              :         }
    7239           14 :       go_assert(p != block->bindings()->end_definitions());
    7240              : 
    7241           34 :       for (; p != block->bindings()->end_definitions(); ++p)
    7242              :         {
    7243           20 :           if ((*p)->is_variable())
    7244              :             {
    7245            6 :               std::string n = (*p)->message_name();
    7246            6 :               go_error_at(loc, "goto jumps over declaration of %qs", n.c_str());
    7247            6 :               go_inform((*p)->location(), "%qs defined here", n.c_str());
    7248            6 :             }
    7249              :         }
    7250              :     }
    7251         2067 : }
    7252              : 
    7253              : // Class Function_declaration.
    7254              : 
    7255              : // Whether this declares a method.
    7256              : 
    7257              : bool
    7258      2119468 : Function_declaration::is_method() const
    7259              : {
    7260      2119468 :   return this->fntype_->is_method();
    7261              : }
    7262              : 
    7263              : // Whether this method should not be included in the type descriptor.
    7264              : 
    7265              : bool
    7266      1330920 : Function_declaration::nointerface() const
    7267              : {
    7268      1330920 :   go_assert(this->is_method());
    7269      1330920 :   return (this->pragmas_ & GOPRAGMA_NOINTERFACE) != 0;
    7270              : }
    7271              : 
    7272              : // Record that this method should not be included in the type
    7273              : // descriptor.
    7274              : 
    7275              : void
    7276            2 : Function_declaration::set_nointerface()
    7277              : {
    7278            2 :   this->pragmas_ |= GOPRAGMA_NOINTERFACE;
    7279            2 : }
    7280              : 
    7281              : // Set the receiver type.  This is used to remove aliases.
    7282              : 
    7283              : void
    7284            0 : Function_declaration::set_receiver_type(Type* rtype)
    7285              : {
    7286            0 :   Function_type* oft = this->fntype_;
    7287            0 :   Typed_identifier* rec = new Typed_identifier(oft->receiver()->name(),
    7288              :                                                rtype,
    7289            0 :                                                oft->receiver()->location());
    7290            0 :   Typed_identifier_list* parameters = NULL;
    7291            0 :   if (oft->parameters() != NULL)
    7292            0 :     parameters = oft->parameters()->copy();
    7293            0 :   Typed_identifier_list* results = NULL;
    7294            0 :   if (oft->results() != NULL)
    7295            0 :     results = oft->results()->copy();
    7296            0 :   Function_type* nft = Type::make_function_type(rec, parameters, results,
    7297              :                                                 oft->location());
    7298            0 :   this->fntype_ = nft;
    7299            0 : }
    7300              : 
    7301              : // Import an inlinable function.  This is used for an inlinable
    7302              : // function whose body is recorded in the export data.  Parse the
    7303              : // export data into a Block and create a regular function using that
    7304              : // Block as its body.  Redeclare this function declaration as the
    7305              : // function.
    7306              : 
    7307              : void
    7308        12247 : Function_declaration::import_function_body(Gogo* gogo, Named_object* no)
    7309              : {
    7310        12247 :   go_assert(no->func_declaration_value() == this);
    7311        12247 :   go_assert(no->package() != NULL);
    7312        12247 :   const std::string& body(this->imported_body_);
    7313        12247 :   go_assert(!body.empty());
    7314              : 
    7315              :   // Read the "//FILE:LINE" comment starts the export data.
    7316              : 
    7317        12247 :   size_t indent = 1;
    7318        12247 :   if (this->is_method())
    7319          593 :     indent = 2;
    7320        12247 :   size_t i = 0;
    7321        25087 :   for (; i < indent; i++)
    7322              :     {
    7323        12840 :       if (body.at(i) != ' ')
    7324              :         {
    7325            0 :           go_error_at(this->location_,
    7326              :                       "invalid export body for %qs: bad initial indentation",
    7327            0 :                       no->message_name().c_str());
    7328            0 :           return;
    7329              :         }
    7330              :     }
    7331              : 
    7332        12247 :   if (body.substr(i, 2) != "//")
    7333              :     {
    7334            0 :       go_error_at(this->location_,
    7335              :                   "invalid export body for %qs: missing file comment",
    7336            0 :                   no->message_name().c_str());
    7337            0 :       return;
    7338              :     }
    7339              : 
    7340        12247 :   size_t colon = body.find(':', i + 2);
    7341        12247 :   size_t nl = body.find('\n', i + 2);
    7342        12247 :   if (nl == std::string::npos)
    7343              :     {
    7344            0 :       go_error_at(this->location_,
    7345              :                   "invalid export body for %qs: missing file name",
    7346            0 :                   no->message_name().c_str());
    7347            0 :       return;
    7348              :     }
    7349        12247 :   if (colon == std::string::npos || nl < colon)
    7350              :     {
    7351            0 :       go_error_at(this->location_,
    7352              :                   "invalid export body for %qs: missing initial line number",
    7353            0 :                   no->message_name().c_str());
    7354            0 :       return;
    7355              :     }
    7356              : 
    7357        12247 :   std::string file = body.substr(i + 2, colon - (i + 2));
    7358        12247 :   std::string linestr = body.substr(colon + 1, nl - (colon + 1));
    7359        12247 :   char* end;
    7360        12247 :   long linenol = strtol(linestr.c_str(), &end, 10);
    7361        12247 :   if (*end != '\0')
    7362              :     {
    7363            0 :       go_error_at(this->location_,
    7364              :                   "invalid export body for %qs: invalid initial line number",
    7365            0 :                   no->message_name().c_str());
    7366            0 :       return;
    7367              :     }
    7368        12247 :   unsigned int lineno = static_cast<unsigned int>(linenol);
    7369              : 
    7370              :   // Turn the file/line into a location.
    7371              : 
    7372        12247 :   char* alc = new char[file.length() + 1];
    7373        12247 :   memcpy(alc, file.data(), file.length());
    7374        12247 :   alc[file.length()] = '\0';
    7375        12247 :   gogo->linemap()->start_file(alc, lineno);
    7376        12247 :   gogo->linemap()->start_line(lineno, 1);
    7377        12247 :   Location start_loc = gogo->linemap()->get_location(0);
    7378              : 
    7379              :   // Define the function with an outer block that declares the
    7380              :   // parameters.
    7381              : 
    7382        12247 :   Function_type* fntype = this->fntype_;
    7383              : 
    7384        12247 :   Block* outer = new Block(NULL, start_loc);
    7385              : 
    7386        12247 :   Function* fn = new Function(fntype, NULL, outer, start_loc);
    7387        12247 :   fn->set_is_inline_only();
    7388              : 
    7389        12247 :   if (fntype->is_method())
    7390              :     {
    7391          593 :       if (this->nointerface())
    7392            1 :         fn->set_nointerface();
    7393          593 :       const Typed_identifier* receiver = fntype->receiver();
    7394          593 :       Variable* recv_param = new Variable(receiver->type(), NULL, false,
    7395          593 :                                           true, true, start_loc);
    7396              : 
    7397          593 :       std::string rname = receiver->name();
    7398          593 :       unsigned rcounter = 0;
    7399              : 
    7400              :       // We need to give a nameless receiver a name to avoid having it
    7401              :       // clash with some other nameless param. FIXME.
    7402          593 :       Gogo::rename_if_empty(&rname, "r", &rcounter);
    7403              : 
    7404          593 :       outer->bindings()->add_variable(rname, NULL, recv_param);
    7405          593 :     }
    7406              : 
    7407        12247 :   const Typed_identifier_list* params = fntype->parameters();
    7408        12247 :   bool is_varargs = fntype->is_varargs();
    7409        12247 :   unsigned pcounter = 0;
    7410        12247 :   if (params != NULL)
    7411              :     {
    7412        10560 :       for (Typed_identifier_list::const_iterator p = params->begin();
    7413        27541 :            p != params->end();
    7414        16981 :            ++p)
    7415              :         {
    7416        16981 :           Variable* param = new Variable(p->type(), NULL, false, true, false,
    7417        16981 :                                          start_loc);
    7418        16981 :           if (is_varargs && p + 1 == params->end())
    7419          581 :             param->set_is_varargs_parameter();
    7420              : 
    7421        16981 :           std::string pname = p->name();
    7422              : 
    7423              :           // We need to give each nameless parameter a non-empty name to avoid
    7424              :           // having it clash with some other nameless param. FIXME.
    7425        16981 :           Gogo::rename_if_empty(&pname, "p", &pcounter);
    7426              : 
    7427        16981 :           outer->bindings()->add_variable(pname, NULL, param);
    7428        16981 :         }
    7429              :     }
    7430              : 
    7431        12247 :   fn->create_result_variables(gogo);
    7432              : 
    7433        12247 :   if (!fntype->is_method())
    7434              :     {
    7435        11654 :       const Package* package = no->package();
    7436        11654 :       no = package->bindings()->add_function(no->name(), package, fn);
    7437              :     }
    7438              :   else
    7439              :     {
    7440         1186 :       Named_type* rtype = fntype->receiver()->type()->deref()->named_type();
    7441          593 :       go_assert(rtype != NULL);
    7442          593 :       no = rtype->add_method(no->name(), fn);
    7443          593 :       const Package* package = rtype->named_object()->package();
    7444          593 :       package->bindings()->add_method(no);
    7445              :     }
    7446              : 
    7447        12247 :   Import_function_body ifb(gogo, this->imp_, no, body, nl + 1, outer, indent);
    7448              : 
    7449        12247 :   if (!Block::import_block(outer, &ifb, start_loc))
    7450            0 :     return;
    7451              : 
    7452        12247 :   outer->determine_types(gogo);
    7453        12247 :   gogo->lower_block(no, outer);
    7454              : 
    7455        12247 :   gogo->add_imported_inline_function(no);
    7456        12247 : }
    7457              : 
    7458              : // Return the function descriptor.
    7459              : 
    7460              : Expression*
    7461       120120 : Function_declaration::descriptor(Gogo*, Named_object* no)
    7462              : {
    7463       120120 :   go_assert(!this->fntype_->is_method());
    7464       120120 :   if (this->descriptor_ == NULL)
    7465       105144 :     this->descriptor_ = Expression::make_func_descriptor(no);
    7466       120120 :   return this->descriptor_;
    7467              : }
    7468              : 
    7469              : // Class Variable.
    7470              : 
    7471      1743043 : Variable::Variable(Type* type, Expression* init, bool is_global,
    7472              :                    bool is_parameter, bool is_receiver,
    7473      1743043 :                    Location location)
    7474      1743043 :   : type_(type), init_(init), preinit_(NULL), location_(location),
    7475      1743043 :     toplevel_decl_(NULL), init_refs_(NULL), embeds_(NULL), backend_(NULL),
    7476      1743043 :     is_global_(is_global), is_parameter_(is_parameter), is_closure_(false),
    7477      1743043 :     is_receiver_(is_receiver), is_varargs_parameter_(false),
    7478      1743043 :     is_global_sink_(false), is_used_(false), is_address_taken_(false),
    7479      1743043 :     is_non_escaping_address_taken_(false), seen_(false),
    7480      1743043 :     init_is_lowered_(false), init_is_flattened_(false),
    7481      1743043 :     type_from_init_tuple_(false), type_from_range_index_(false),
    7482      1743043 :     type_from_range_value_(false), type_from_chan_element_(false),
    7483      1743043 :     is_type_switch_var_(false), determined_type_(false),
    7484      1743043 :     in_unique_section_(false), is_referenced_by_inline_(false)
    7485              : {
    7486      1743043 :   go_assert(type != NULL || init != NULL);
    7487      1743043 :   go_assert(!is_parameter || init == NULL);
    7488      1743043 : }
    7489              : 
    7490              : // Traverse the initializer expression.
    7491              : 
    7492              : int
    7493     21645010 : Variable::traverse_expression(Traverse* traverse, unsigned int traverse_mask)
    7494              : {
    7495     21645010 :   if (this->preinit_ != NULL)
    7496              :     {
    7497        36173 :       if (this->preinit_->traverse(traverse) == TRAVERSE_EXIT)
    7498              :         return TRAVERSE_EXIT;
    7499              :     }
    7500     21645010 :   if (this->init_ != NULL
    7501      5848926 :       && ((traverse_mask
    7502      5848926 :            & (Traverse::traverse_expressions | Traverse::traverse_types))
    7503              :           != 0))
    7504              :     {
    7505      4978871 :       if (Expression::traverse(&this->init_, traverse) == TRAVERSE_EXIT)
    7506              :         return TRAVERSE_EXIT;
    7507              :     }
    7508              :   return TRAVERSE_CONTINUE;
    7509              : }
    7510              : 
    7511              : // Lower the initialization expression after parsing is complete.
    7512              : 
    7513              : void
    7514      6555653 : Variable::lower_init_expression(Gogo* gogo, Named_object* function,
    7515              :                                 Statement_inserter* inserter)
    7516              : {
    7517      6555653 :   Named_object* dep = gogo->var_depends_on(this);
    7518      6555653 :   if (dep != NULL && dep->is_variable())
    7519          194 :     dep->var_value()->lower_init_expression(gogo, function, inserter);
    7520              : 
    7521      6555653 :   if (this->embeds_ != NULL)
    7522              :     {
    7523              :       // Now that we have seen any possible type aliases, convert the
    7524              :       // go:embed directives into an initializer.
    7525           22 :       go_assert(this->init_ == NULL && this->type_ != NULL);
    7526           22 :       this->init_ = gogo->initializer_for_embeds(this->type_, this->embeds_,
    7527              :                                                  this->location_);
    7528           22 :       delete this->embeds_;
    7529           22 :       this->embeds_ = NULL;
    7530              :     }
    7531              : 
    7532      6555653 :   if (this->init_ != NULL && !this->init_is_lowered_)
    7533              :     {
    7534       302033 :       if (this->seen_)
    7535              :         {
    7536              :           // We will give an error elsewhere, this is just to prevent
    7537              :           // an infinite loop.
    7538            0 :           return;
    7539              :         }
    7540       302033 :       this->seen_ = true;
    7541              : 
    7542       302033 :       Statement_inserter global_inserter;
    7543       302033 :       if (this->is_global_)
    7544              :         {
    7545        20356 :           global_inserter = Statement_inserter(gogo, this);
    7546        20356 :           inserter = &global_inserter;
    7547              :         }
    7548              : 
    7549       302033 :       gogo->lower_expression(function, inserter, &this->init_);
    7550              : 
    7551       302033 :       this->seen_ = false;
    7552              : 
    7553       302033 :       this->init_is_lowered_ = true;
    7554              :     }
    7555              : }
    7556              : 
    7557              : // Flatten the initialization expression after ordering evaluations.
    7558              : 
    7559              : void
    7560       751215 : Variable::flatten_init_expression(Gogo* gogo, Named_object* function,
    7561              :                                   Statement_inserter* inserter)
    7562              : {
    7563       751215 :   Named_object* dep = gogo->var_depends_on(this);
    7564       751215 :   if (dep != NULL && dep->is_variable())
    7565           99 :     dep->var_value()->flatten_init_expression(gogo, function, inserter);
    7566              : 
    7567       751215 :   if (this->init_ != NULL && !this->init_is_flattened_)
    7568              :     {
    7569       282858 :       if (this->seen_)
    7570              :         {
    7571              :           // We will give an error elsewhere, this is just to prevent
    7572              :           // an infinite loop.
    7573            0 :           return;
    7574              :         }
    7575       282858 :       this->seen_ = true;
    7576              : 
    7577       282858 :       Statement_inserter global_inserter;
    7578       282858 :       if (this->is_global_)
    7579              :         {
    7580         8364 :           global_inserter = Statement_inserter(gogo, this);
    7581         8364 :           inserter = &global_inserter;
    7582              :         }
    7583              : 
    7584       282858 :       gogo->flatten_expression(function, inserter, &this->init_);
    7585              : 
    7586              :       // If an interface conversion is needed, we need a temporary
    7587              :       // variable.
    7588       282858 :       if (this->type_ != NULL
    7589       282858 :           && !Type::are_identical(this->type_, this->init_->type(),
    7590              :                                   Type::COMPARE_ERRORS | Type::COMPARE_TAGS,
    7591              :                                   NULL)
    7592          898 :           && this->init_->type()->interface_type() != NULL
    7593       282859 :           && !this->init_->is_multi_eval_safe())
    7594              :         {
    7595            0 :           Temporary_statement* temp =
    7596            0 :             Statement::make_temporary(NULL, this->init_, this->location_);
    7597            0 :           inserter->insert(temp);
    7598            0 :           this->init_ = Expression::make_temporary_reference(temp,
    7599              :                                                              this->location_);
    7600              :         }
    7601              : 
    7602       282858 :       this->seen_ = false;
    7603       282858 :       this->init_is_flattened_ = true;
    7604              :     }
    7605              : }
    7606              : 
    7607              : // Get the preinit block.
    7608              : 
    7609              : Block*
    7610        52980 : Variable::preinit_block(Gogo* gogo)
    7611              : {
    7612        52980 :   go_assert(this->is_global_);
    7613        52980 :   if (this->preinit_ == NULL)
    7614        12208 :     this->preinit_ = new Block(NULL, this->location());
    7615              : 
    7616              :   // If a global variable has a preinitialization statement, then we
    7617              :   // need to have an initialization function.
    7618        52980 :   gogo->set_need_init_fn();
    7619              : 
    7620        52980 :   return this->preinit_;
    7621              : }
    7622              : 
    7623              : // Add a statement to be run before the initialization expression.
    7624              : 
    7625              : void
    7626        52980 : Variable::add_preinit_statement(Gogo* gogo, Statement* s)
    7627              : {
    7628        52980 :   Block* b = this->preinit_block(gogo);
    7629        52980 :   b->add_statement(s);
    7630        52980 :   b->set_end_location(s->location());
    7631        52980 : }
    7632              : 
    7633              : // Whether this variable has a type.
    7634              : 
    7635              : bool
    7636     19294975 : Variable::has_type() const
    7637              : {
    7638     19294975 :   if (this->type_ == NULL)
    7639              :     return false;
    7640              : 
    7641              :   // A variable created in a type switch case nil does not actually
    7642              :   // have a type yet.  It will be changed to use the initializer's
    7643              :   // type in determine_type.
    7644     18653027 :   if (this->is_type_switch_var_
    7645     18653027 :       && this->type_->is_nil_constant_as_type())
    7646              :     return false;
    7647              : 
    7648              :   return true;
    7649              : }
    7650              : 
    7651              : // In an assignment which sets a variable to a tuple of EXPR, return
    7652              : // the type of the first element of the tuple.
    7653              : 
    7654              : Type*
    7655         1594 : Variable::type_from_tuple(Expression* expr, bool report_error) const
    7656              : {
    7657         1594 :   if (Index_expression::is_map_index(expr))
    7658              :     {
    7659         1553 :       Map_type* mt;
    7660         1553 :       if (expr->map_index_expression() != NULL)
    7661            0 :         mt = expr->map_index_expression()->get_map_type();
    7662              :       else
    7663         1553 :         mt = expr->index_expression()->left()->type()->map_type();
    7664            0 :       if (mt == NULL)
    7665            0 :         return Type::make_error_type();
    7666         1553 :       return mt->val_type();
    7667              :     }
    7668           41 :   else if (expr->receive_expression() != NULL)
    7669              :     {
    7670           41 :       Expression* channel = expr->receive_expression()->channel();
    7671           41 :       Type* channel_type = channel->type();
    7672           41 :       if (channel_type->channel_type() == NULL)
    7673            0 :         return Type::make_error_type();
    7674           82 :       return channel_type->channel_type()->element_type();
    7675              :     }
    7676              :   else
    7677              :     {
    7678            0 :       if (report_error)
    7679            0 :         go_error_at(this->location(), "invalid tuple definition");
    7680            0 :       return Type::make_error_type();
    7681              :     }
    7682              : }
    7683              : 
    7684              : // Given EXPR used in a range clause, return either the index type or
    7685              : // the value type of the range, depending upon GET_INDEX_TYPE.
    7686              : 
    7687              : Type*
    7688        31409 : Variable::type_from_range(Expression* expr, bool get_index_type,
    7689              :                           bool report_error) const
    7690              : {
    7691        31409 :   Type* t = expr->type();
    7692        31409 :   if (t->is_error_type())
    7693              :     return t;
    7694        31409 :   else if (t->array_type() != NULL
    7695         4113 :       || (t->points_to() != NULL
    7696          222 :           && t->points_to()->array_type() != NULL
    7697          222 :           && !t->points_to()->is_slice_type()))
    7698              :     {
    7699        27518 :       if (get_index_type)
    7700         7286 :         return Type::lookup_integer_type("int");
    7701              :       else
    7702        60696 :         return t->deref()->array_type()->element_type();
    7703              :     }
    7704         3891 :   else if (t->is_string_type())
    7705              :     {
    7706          732 :       if (get_index_type)
    7707          190 :         return Type::lookup_integer_type("int");
    7708              :       else
    7709          542 :         return Type::lookup_integer_type("int32");
    7710              :     }
    7711         3159 :   else if (t->map_type() != NULL)
    7712              :     {
    7713         3084 :       if (get_index_type)
    7714         3378 :         return t->map_type()->key_type();
    7715              :       else
    7716         2790 :         return t->map_type()->val_type();
    7717              :     }
    7718           75 :   else if (t->channel_type() != NULL)
    7719              :     {
    7720           75 :       if (get_index_type)
    7721          150 :         return t->channel_type()->element_type();
    7722              :       else
    7723              :         {
    7724            0 :           if (report_error)
    7725            0 :             go_error_at(this->location(),
    7726              :                         ("invalid definition of value variable "
    7727              :                          "for channel range"));
    7728            0 :           return Type::make_error_type();
    7729              :         }
    7730              :     }
    7731              :   else
    7732              :     {
    7733            0 :       if (report_error)
    7734            0 :         go_error_at(this->location(), "invalid type for range clause");
    7735            0 :       return Type::make_error_type();
    7736              :     }
    7737              : }
    7738              : 
    7739              : // EXPR should be a channel.  Return the channel's element type.
    7740              : 
    7741              : Type*
    7742          524 : Variable::type_from_chan_element(Expression* expr, bool report_error) const
    7743              : {
    7744          524 :   Type* t = expr->type();
    7745          524 :   if (t->channel_type() != NULL)
    7746         1048 :     return t->channel_type()->element_type();
    7747              :   else
    7748              :     {
    7749            0 :       if (report_error)
    7750            0 :         go_error_at(this->location(), "expected channel");
    7751            0 :       return Type::make_error_type();
    7752              :     }
    7753              : }
    7754              : 
    7755              : // Return the type of the Variable.  This may be called before
    7756              : // Variable::determine_type is called, which means that we may need to
    7757              : // get the type from the initializer.  FIXME: If we combine lowering
    7758              : // with type determination, then this should be unnecessary.
    7759              : 
    7760              : Type*
    7761     71405498 : Variable::type()
    7762              : {
    7763              :   // A variable in a type switch with a nil case will have the wrong
    7764              :   // type here.  This gets fixed up in determine_type, below.
    7765     71405498 :   Type* type = this->type_;
    7766     71405498 :   Expression* init = this->init_;
    7767     71405498 :   if (this->is_type_switch_var_
    7768      1010459 :       && type != NULL
    7769     72415957 :       && this->type_->is_nil_constant_as_type())
    7770              :     {
    7771            0 :       Type_guard_expression* tge = this->init_->type_guard_expression();
    7772            0 :       go_assert(tge != NULL);
    7773            0 :       init = tge->expr();
    7774            0 :       type = NULL;
    7775              :     }
    7776              : 
    7777     71405498 :   if (this->seen_)
    7778              :     {
    7779            0 :       if (this->type_ == NULL || !this->type_->is_error_type())
    7780              :         {
    7781            0 :           go_error_at(this->location_, "variable initializer refers to itself");
    7782            0 :           this->type_ = Type::make_error_type();
    7783              :         }
    7784            0 :       return this->type_;
    7785              :     }
    7786              : 
    7787     71405498 :   this->seen_ = true;
    7788              : 
    7789     71405498 :   if (type != NULL)
    7790              :     ;
    7791            0 :   else if (this->type_from_init_tuple_)
    7792            0 :     type = this->type_from_tuple(init, false);
    7793            0 :   else if (this->type_from_range_index_ || this->type_from_range_value_)
    7794            0 :     type = this->type_from_range(init, this->type_from_range_index_, false);
    7795            0 :   else if (this->type_from_chan_element_)
    7796            0 :     type = this->type_from_chan_element(init, false);
    7797              :   else
    7798              :     {
    7799            0 :       go_assert(init != NULL);
    7800            0 :       type = init->type();
    7801            0 :       go_assert(type != NULL);
    7802              : 
    7803              :       // Variables should not have abstract types.
    7804            0 :       if (type->is_abstract())
    7805            0 :         type = type->make_non_abstract_type();
    7806              : 
    7807            0 :       if (type->is_void_type())
    7808            0 :         type = Type::make_error_type();
    7809              :     }
    7810              : 
    7811     71405498 :   this->seen_ = false;
    7812              : 
    7813     71405498 :   return type;
    7814              : }
    7815              : 
    7816              : // Fetch the type from a const pointer, in which case it should have
    7817              : // been set already.
    7818              : 
    7819              : Type*
    7820         8440 : Variable::type() const
    7821              : {
    7822         8440 :   go_assert(this->type_ != NULL);
    7823         8440 :   return this->type_;
    7824              : }
    7825              : 
    7826              : // Set the type if necessary.
    7827              : 
    7828              : void
    7829      6759215 : Variable::determine_type(Gogo* gogo)
    7830              : {
    7831      6759215 :   if (this->determined_type_)
    7832              :     return;
    7833       906794 :   this->determined_type_ = true;
    7834              : 
    7835       906794 :   if (this->preinit_ != NULL)
    7836            4 :     this->preinit_->determine_types(gogo);
    7837              : 
    7838              :   // A variable in a type switch with a nil case will have the wrong
    7839              :   // type here.  It will have an initializer which is a type guard.
    7840              :   // We want to initialize it to the value without the type guard, and
    7841              :   // use the type of that value as well.
    7842       906794 :   if (this->is_type_switch_var_
    7843         9915 :       && this->type_ != NULL
    7844       915568 :       && this->type_->is_nil_constant_as_type())
    7845              :     {
    7846          122 :       Type_guard_expression* tge = this->init_->type_guard_expression();
    7847            0 :       go_assert(tge != NULL);
    7848          122 :       this->type_ = NULL;
    7849          122 :       this->init_ = tge->expr();
    7850              :     }
    7851              : 
    7852       906794 :   if (this->init_ == NULL)
    7853       571773 :     go_assert(this->type_ != NULL && !this->type_->is_abstract());
    7854       335021 :   else if (this->type_from_init_tuple_)
    7855              :     {
    7856         1594 :       Expression *init = this->init_;
    7857         1594 :       init->determine_type_no_context(gogo);
    7858         1594 :       this->type_ = this->type_from_tuple(init, true);
    7859         1594 :       this->init_ = NULL;
    7860              :     }
    7861       333427 :   else if (this->type_from_range_index_ || this->type_from_range_value_)
    7862              :     {
    7863        31409 :       Expression* init = this->init_;
    7864        31409 :       init->determine_type_no_context(gogo);
    7865        31409 :       this->type_ = this->type_from_range(init, this->type_from_range_index_,
    7866              :                                           true);
    7867        31409 :       this->init_ = NULL;
    7868        31409 :     }
    7869       302018 :   else if (this->type_from_chan_element_)
    7870              :     {
    7871          524 :       Expression* init = this->init_;
    7872          524 :       init->determine_type_no_context(gogo);
    7873          524 :       this->type_ = this->type_from_chan_element(init, true);
    7874          524 :       this->init_ = NULL;
    7875              :     }
    7876              :   else
    7877              :     {
    7878       301494 :       Type_context context(this->type_, false);
    7879       301494 :       this->init_->determine_type(gogo, &context);
    7880       301494 :       if (this->type_ == NULL)
    7881              :         {
    7882       287569 :           Type* type = this->init_->type();
    7883       287569 :           go_assert(type != NULL);
    7884       287569 :           if (type->is_abstract())
    7885          601 :             type = type->make_non_abstract_type();
    7886              : 
    7887       287569 :           if (type->is_void_type())
    7888              :             {
    7889            1 :               go_error_at(this->location_, "variable has no type");
    7890            1 :               type = Type::make_error_type();
    7891              :             }
    7892       287568 :           else if (type->is_nil_type())
    7893              :             {
    7894            2 :               go_error_at(this->location_, "variable defined to nil type");
    7895            2 :               type = Type::make_error_type();
    7896              :             }
    7897       287566 :           else if (type->is_call_multiple_result_type())
    7898              :             {
    7899            2 :               go_error_at(this->location_,
    7900              :                        "single variable set to multiple-value function call");
    7901            2 :               type = Type::make_error_type();
    7902              :             }
    7903              : 
    7904       287569 :           this->type_ = type;
    7905              :         }
    7906              :     }
    7907              : }
    7908              : 
    7909              : // Get the initial value of a variable.  This does not
    7910              : // consider whether the variable is in the heap--it returns the
    7911              : // initial value as though it were always stored in the stack.
    7912              : 
    7913              : Bexpression*
    7914       395645 : Variable::get_init(Gogo* gogo, Named_object* function)
    7915              : {
    7916       395645 :   go_assert(this->preinit_ == NULL);
    7917       395645 :   Location loc = this->location();
    7918       395645 :   if (this->init_ == NULL)
    7919              :     {
    7920       113105 :       go_assert(!this->is_parameter_);
    7921       113105 :       if (this->is_global_ || this->is_in_heap())
    7922              :         return NULL;
    7923        95247 :       Btype* btype = this->type()->get_backend(gogo);
    7924        95247 :       return gogo->backend()->zero_expression(btype);
    7925              :     }
    7926              :   else
    7927              :     {
    7928       282540 :       Translate_context context(gogo, function, NULL, NULL);
    7929       282540 :       Expression* init = Expression::make_cast(this->type(), this->init_, loc);
    7930       282540 :       return init->get_backend(&context);
    7931              :     }
    7932              : }
    7933              : 
    7934              : // Get the initial value of a variable when a block is required.
    7935              : // VAR_DECL is the decl to set; it may be NULL for a sink variable.
    7936              : 
    7937              : Bstatement*
    7938        12208 : Variable::get_init_block(Gogo* gogo, Named_object* function,
    7939              :                          Bvariable* var_decl)
    7940              : {
    7941        12208 :   go_assert(this->preinit_ != NULL);
    7942              : 
    7943              :   // We want to add the variable assignment to the end of the preinit
    7944              :   // block.
    7945              : 
    7946        12208 :   Translate_context context(gogo, function, NULL, NULL);
    7947        12208 :   Bblock* bblock = this->preinit_->get_backend(&context);
    7948        12208 :   Bfunction* bfunction =
    7949        12208 :       function->func_value()->get_or_make_decl(gogo, function);
    7950              : 
    7951              :   // It's possible to have pre-init statements without an initializer
    7952              :   // if the pre-init statements set the variable.
    7953        12208 :   Bstatement* decl_init = NULL;
    7954        12208 :   if (this->init_ != NULL)
    7955              :     {
    7956          212 :       if (var_decl == NULL)
    7957              :         {
    7958           29 :           Bexpression* init_bexpr = this->init_->get_backend(&context);
    7959           29 :           decl_init = gogo->backend()->expression_statement(bfunction,
    7960              :                                                             init_bexpr);
    7961              :         }
    7962              :       else
    7963              :         {
    7964          183 :           Location loc = this->location();
    7965          183 :           Expression* val_expr =
    7966          183 :               Expression::make_cast(this->type(), this->init_, loc);
    7967          183 :           Bexpression* val = val_expr->get_backend(&context);
    7968          183 :           Bexpression* var_ref =
    7969          183 :               gogo->backend()->var_expression(var_decl, loc);
    7970          183 :           decl_init = gogo->backend()->assignment_statement(bfunction, var_ref,
    7971              :                                                             val, loc);
    7972              :         }
    7973              :     }
    7974        12208 :   Bstatement* block_stmt = gogo->backend()->block_statement(bblock);
    7975        12208 :   if (decl_init != NULL)
    7976          212 :     block_stmt = gogo->backend()->compound_statement(block_stmt, decl_init);
    7977        12208 :   return block_stmt;
    7978              : }
    7979              : 
    7980              : // Add an initializer reference.
    7981              : 
    7982              : void
    7983        11650 : Variable::add_init_ref(Named_object* var)
    7984              : {
    7985        11650 :   if (this->init_refs_ == NULL)
    7986         3632 :     this->init_refs_ = new std::vector<Named_object*>;
    7987        11650 :   this->init_refs_->push_back(var);
    7988        11650 : }
    7989              : 
    7990              : // Export the variable
    7991              : 
    7992              : void
    7993         8440 : Variable::export_var(Export* exp, const Named_object* no) const
    7994              : {
    7995         8440 :   go_assert(this->is_global_);
    7996         8440 :   exp->write_c_string("var ");
    7997         8440 :   if (no->package() != NULL)
    7998              :     {
    7999         2056 :       char buf[50];
    8000         2056 :       snprintf(buf, sizeof buf, "<p%d>", exp->package_index(no->package()));
    8001         2056 :       exp->write_c_string(buf);
    8002              :     }
    8003              : 
    8004         8440 :   if (!Gogo::is_hidden_name(no->name()))
    8005         5354 :     exp->write_string(no->name());
    8006              :   else
    8007              :     {
    8008         3086 :       exp->write_c_string(".");
    8009         3086 :       exp->write_string(Gogo::unpack_hidden_name(no->name()));
    8010              :     }
    8011              : 
    8012         8440 :   exp->write_c_string(" ");
    8013         8440 :   exp->write_type(this->type());
    8014         8440 :   exp->write_c_string("\n");
    8015         8440 : }
    8016              : 
    8017              : // Import a variable.
    8018              : 
    8019              : bool
    8020       326311 : Variable::import_var(Import* imp, std::string* pname, Package** ppkg,
    8021              :                      bool* pis_exported, Type** ptype)
    8022              : {
    8023       326311 :   imp->require_c_string("var ");
    8024       326311 :   if (!Import::read_qualified_identifier(imp, pname, ppkg, pis_exported))
    8025              :     {
    8026            0 :       go_error_at(imp->location(),
    8027              :                   "import error at %d: bad variable name in export data",
    8028              :                   imp->pos());
    8029            0 :       return false;
    8030              :     }
    8031       326311 :   imp->require_c_string(" ");
    8032       326311 :   *ptype = imp->read_type();
    8033       326311 :   imp->require_semicolon_if_old_version();
    8034       326311 :   imp->require_c_string("\n");
    8035       326311 :   return true;
    8036              : }
    8037              : 
    8038              : // Convert a variable to the backend representation.
    8039              : 
    8040              : Bvariable*
    8041      4441091 : Variable::get_backend_variable(Gogo* gogo, Named_object* function,
    8042              :                                const Package* package, const std::string& name)
    8043              : {
    8044      4441091 :   if (this->backend_ == NULL)
    8045              :     {
    8046       923560 :       Backend* backend = gogo->backend();
    8047       923560 :       Type* type = this->type_;
    8048       923560 :       if (type->is_error_type()
    8049       923560 :           || (type->is_undefined()
    8050            5 :               && (!this->is_global_ || package == NULL)))
    8051          149 :         this->backend_ = backend->error_variable();
    8052              :       else
    8053              :         {
    8054       923411 :           bool is_parameter = this->is_parameter_;
    8055       923411 :           if (this->is_receiver_ && type->points_to() == NULL)
    8056              :             is_parameter = false;
    8057       923411 :           if (this->is_in_heap())
    8058              :             {
    8059        18171 :               is_parameter = false;
    8060        18171 :               type = Type::make_pointer_type(type);
    8061              :             }
    8062              : 
    8063       923411 :           Btype* btype = type->get_backend(gogo);
    8064              : 
    8065       923411 :           Bvariable* bvar;
    8066       923411 :           if (Map_type::is_zero_value(this))
    8067            3 :             bvar = Map_type::backend_zero_value(gogo);
    8068       923408 :           else if (this->is_global_)
    8069              :             {
    8070        34342 :               Backend_name bname;
    8071        34342 :               gogo->global_var_backend_name(name, package, &bname);
    8072              : 
    8073        34342 :               bool is_hidden = Gogo::is_hidden_name(name);
    8074              :               // Hack to export runtime.writeBarrier.  FIXME.
    8075              :               // This is because go:linkname doesn't work on variables.
    8076        68684 :               if (gogo->compiling_runtime()
    8077        34342 :                   && bname.name() == "runtime.writeBarrier")
    8078           20 :                 is_hidden = false;
    8079              : 
    8080              :               // If an inline body refers to this variable, then it
    8081              :               // needs to be visible in the symbol table.
    8082        34342 :               if (this->is_referenced_by_inline_)
    8083         4329 :                 is_hidden = false;
    8084              : 
    8085              :               // If this variable is in a different package, then it
    8086              :               // can't be treated as a hidden symbol.  This case can
    8087              :               // arise when an inlined function refers to a
    8088              :               // package-scope unexported variable.
    8089        34342 :               if (package != NULL)
    8090         9045 :                 is_hidden = false;
    8091              : 
    8092        34342 :               unsigned int flags = 0;
    8093        34342 :               if (this->is_address_taken_
    8094        32083 :                   || this->is_non_escaping_address_taken_)
    8095         3542 :                 flags |= Backend::variable_address_is_taken;
    8096        34342 :               if (package != NULL)
    8097         9045 :                 flags |= Backend::variable_is_external;
    8098        34342 :               if (is_hidden)
    8099        17925 :                 flags |= Backend::variable_is_hidden;
    8100        34342 :               if (this->in_unique_section_)
    8101            0 :                 flags |= Backend::variable_in_unique_section;
    8102              : 
    8103              :               // For some reason asm_name can't be the empty string
    8104              :               // for global_variable, so we call asm_name rather than
    8105              :               // optional_asm_name here.  FIXME.
    8106              : 
    8107        34342 :               bvar = backend->global_variable(bname.name(),
    8108        34342 :                                               bname.asm_name(),
    8109              :                                               btype, flags,
    8110              :                                               this->location_);
    8111        34342 :             }
    8112       889066 :           else if (function == NULL)
    8113              :             {
    8114            0 :               go_assert(saw_errors());
    8115            0 :               bvar = backend->error_variable();
    8116              :             }
    8117              :           else
    8118              :             {
    8119       889066 :               const std::string n = Gogo::unpack_hidden_name(name);
    8120       889066 :               Bfunction* bfunction = function->func_value()->get_decl();
    8121       889066 :               unsigned int flags = 0;
    8122       889066 :               if (this->is_non_escaping_address_taken_
    8123       889066 :                   && !this->is_in_heap())
    8124              :                 flags |= Backend::variable_address_is_taken;
    8125       889066 :               if (this->is_closure())
    8126        14291 :                 bvar = backend->static_chain_variable(bfunction, n, btype,
    8127              :                                                       flags, this->location_);
    8128       874775 :               else if (is_parameter)
    8129       474160 :                 bvar = backend->parameter_variable(bfunction, n, btype,
    8130              :                                                    flags, this->location_);
    8131              :               else
    8132              :                 {
    8133       400615 :                   Bvariable* bvar_decl = NULL;
    8134       400615 :                   if (this->toplevel_decl_ != NULL)
    8135              :                     {
    8136         6520 :                       Translate_context context(gogo, NULL, NULL, NULL);
    8137         6520 :                       bvar_decl = this->toplevel_decl_->temporary_statement()
    8138         6520 :                         ->get_backend_variable(&context);
    8139              :                     }
    8140       400615 :                   bvar = backend->local_variable(bfunction, n, btype,
    8141              :                                                  bvar_decl, flags,
    8142              :                                                  this->location_);
    8143              :                 }
    8144       889066 :             }
    8145       923411 :           this->backend_ = bvar;
    8146              :         }
    8147              :     }
    8148      4441091 :   return this->backend_;
    8149              : }
    8150              : 
    8151              : // Class Result_variable.
    8152              : 
    8153              : // Convert a result variable to the backend representation.
    8154              : 
    8155              : Bvariable*
    8156      1161879 : Result_variable::get_backend_variable(Gogo* gogo, Named_object* function,
    8157              :                                       const std::string& name)
    8158              : {
    8159      1161879 :   if (this->backend_ == NULL)
    8160              :     {
    8161       244740 :       Backend* backend = gogo->backend();
    8162       244740 :       Type* type = this->type_;
    8163       244740 :       if (type->is_error())
    8164            3 :         this->backend_ = backend->error_variable();
    8165              :       else
    8166              :         {
    8167       244737 :           if (this->is_in_heap())
    8168          122 :             type = Type::make_pointer_type(type);
    8169       244737 :           Btype* btype = type->get_backend(gogo);
    8170       244737 :           Bfunction* bfunction = function->func_value()->get_decl();
    8171       244737 :           std::string n = Gogo::unpack_hidden_name(name);
    8172       244737 :           unsigned int flags = 0;
    8173       244737 :           if (this->is_non_escaping_address_taken_
    8174       244737 :               && !this->is_in_heap())
    8175              :             flags |= Backend::variable_address_is_taken;
    8176       244737 :           this->backend_ = backend->local_variable(bfunction, n, btype,
    8177              :                                                    NULL, flags,
    8178              :                                                    this->location_);
    8179       244737 :         }
    8180              :     }
    8181      1161879 :   return this->backend_;
    8182              : }
    8183              : 
    8184              : // Class Named_constant.
    8185              : 
    8186              : // Set the type of a named constant.  This is only used to set the
    8187              : // type to an error type.
    8188              : 
    8189              : void
    8190            0 : Named_constant::set_type(Type* t)
    8191              : {
    8192            0 :   go_assert(this->type_ == NULL || t->is_error_type());
    8193            0 :   this->type_ = t;
    8194            0 : }
    8195              : 
    8196              : // Traverse the initializer expression.
    8197              : 
    8198              : int
    8199     16412124 : Named_constant::traverse_expression(Traverse* traverse)
    8200              : {
    8201     16412124 :   return Expression::traverse(&this->expr_, traverse);
    8202              : }
    8203              : 
    8204              : // Set the iota value in a constant expression.
    8205              : 
    8206      1011563 : class Set_iota_value : public Traverse
    8207              : {
    8208              :  public:
    8209      1011563 :   Set_iota_value(int iota_value)
    8210      1011563 :     : Traverse(traverse_expressions),
    8211      1011563 :       iota_value_(iota_value)
    8212              :   { }
    8213              : 
    8214              :   int
    8215              :   expression(Expression**);
    8216              : 
    8217              :  private:
    8218              :   int iota_value_;
    8219              : };
    8220              : 
    8221              : int
    8222      1137409 : Set_iota_value::expression(Expression** pexpr)
    8223              : {
    8224      1137409 :   Expression* expr = *pexpr;
    8225      1137409 :   if (expr->const_expression() != NULL)
    8226        46831 :     expr->const_expression()->set_iota_value(this->iota_value_);
    8227      1090578 :   else if (expr->unknown_expression() != NULL)
    8228              :     {
    8229              :       // This case can happen for an array length that is not set in
    8230              :       // the determine types pass.
    8231        17225 :       expr->unknown_expression()->set_iota_value(this->iota_value_);
    8232              :     }
    8233      1137409 :   return TRAVERSE_CONTINUE;
    8234              : }
    8235              : 
    8236              : // Determine the type of the constant.
    8237              : 
    8238              : void
    8239      1598644 : Named_constant::determine_type(Gogo* gogo)
    8240              : {
    8241      1598644 :   if (this->type_is_determined_)
    8242       587081 :     return;
    8243      1011563 :   this->type_is_determined_ = true;
    8244              : 
    8245      1011563 :   if (this->type_ != NULL)
    8246              :     {
    8247       222604 :       Type_context context(this->type_, this->type_->is_abstract());
    8248       222604 :       this->expr_->determine_type(gogo, &context);
    8249              :     }
    8250              :   else
    8251              :     {
    8252              :       // A constant may have an abstract type.
    8253       788959 :       Type_context context(NULL, true);
    8254       788959 :       this->expr_->determine_type(gogo, &context);
    8255       788959 :       this->type_ = this->expr_->type();
    8256       788959 :       go_assert(this->type_ != NULL);
    8257              :     }
    8258              : 
    8259      1011563 :   Set_iota_value siv(this->iota_value_);
    8260      1011563 :   this->traverse_expression(&siv);
    8261      1011563 : }
    8262              : 
    8263              : // Indicate that we found and reported an error for this constant.
    8264              : 
    8265              : void
    8266           41 : Named_constant::set_error()
    8267              : {
    8268           41 :   this->type_ = Type::make_error_type();
    8269           41 :   this->expr_ = Expression::make_error(this->location_);
    8270           41 : }
    8271              : 
    8272              : // Export a constant.
    8273              : 
    8274              : void
    8275        39984 : Named_constant::export_const(Export* exp, const std::string& name) const
    8276              : {
    8277        39984 :   exp->write_c_string("const ");
    8278        39984 :   exp->write_string(name);
    8279        39984 :   exp->write_c_string(" ");
    8280        39984 :   if (!this->type_->is_abstract())
    8281              :     {
    8282        15584 :       exp->write_type(this->type_);
    8283        15584 :       exp->write_c_string(" ");
    8284              :     }
    8285        39984 :   exp->write_c_string("= ");
    8286              : 
    8287        39984 :   Export_function_body efb(exp, 0);
    8288        39984 :   if (!this->type_->is_abstract())
    8289        15584 :     efb.set_type_context(this->type_);
    8290        39984 :   this->expr()->export_expression(&efb);
    8291        39984 :   exp->write_string(efb.body());
    8292              : 
    8293        39984 :   exp->write_c_string("\n");
    8294        39984 : }
    8295              : 
    8296              : // Import a constant.
    8297              : 
    8298              : void
    8299       851537 : Named_constant::import_const(Import* imp, std::string* pname, Type** ptype,
    8300              :                              Expression** pexpr)
    8301              : {
    8302       851537 :   imp->require_c_string("const ");
    8303       851537 :   *pname = imp->read_identifier();
    8304       851537 :   imp->require_c_string(" ");
    8305       851537 :   if (imp->peek_char() == '=')
    8306       656190 :     *ptype = NULL;
    8307              :   else
    8308              :     {
    8309       195347 :       *ptype = imp->read_type();
    8310       195347 :       imp->require_c_string(" ");
    8311              :     }
    8312       851537 :   imp->require_c_string("= ");
    8313       851537 :   *pexpr = Expression::import_expression(imp, imp->location());
    8314       851537 :   imp->require_semicolon_if_old_version();
    8315       851537 :   imp->require_c_string("\n");
    8316       851537 : }
    8317              : 
    8318              : // Get the backend representation.
    8319              : 
    8320              : Bexpression*
    8321        23399 : Named_constant::get_backend(Gogo* gogo, Named_object* const_no)
    8322              : {
    8323        23399 :   if (this->bconst_ == NULL)
    8324              :     {
    8325        23399 :       Translate_context subcontext(gogo, NULL, NULL, NULL);
    8326        23399 :       Type* type = this->type();
    8327        23399 :       Location loc = this->location();
    8328              : 
    8329        23399 :       Expression* const_ref = Expression::make_const_reference(const_no, loc);
    8330        23399 :       Bexpression* const_decl = const_ref->get_backend(&subcontext);
    8331        46798 :       if (type != NULL && type->is_numeric_type())
    8332              :         {
    8333        23399 :           Btype* btype = type->get_backend(gogo);
    8334        23399 :           std::string name;
    8335        23399 :           if (const_no->package() == NULL)
    8336        23399 :             name = gogo->pkgpath();
    8337              :           else
    8338            0 :             name = const_no->package()->pkgpath();
    8339        23399 :           name.push_back('.');
    8340        46798 :           name.append(Gogo::unpack_hidden_name(const_no->name()));
    8341        23399 :           const_decl =
    8342        23399 :             gogo->backend()->named_constant_expression(btype, name,
    8343              :                                                        const_decl, loc);
    8344        23399 :         }
    8345        23399 :       this->bconst_ = const_decl;
    8346              :     }
    8347        23399 :   return this->bconst_;
    8348              : }
    8349              : 
    8350              : // Add a method.
    8351              : 
    8352              : Named_object*
    8353         2538 : Type_declaration::add_method(const std::string& name, Function* function)
    8354              : {
    8355         2538 :   Named_object* ret = Named_object::make_function(name, NULL, function);
    8356         2538 :   this->methods_.push_back(ret);
    8357         2538 :   return ret;
    8358              : }
    8359              : 
    8360              : // Add a method declaration.
    8361              : 
    8362              : Named_object*
    8363            0 : Type_declaration::add_method_declaration(const std::string&  name,
    8364              :                                          Package* package,
    8365              :                                          Function_type* type,
    8366              :                                          Location location)
    8367              : {
    8368            0 :   Named_object* ret = Named_object::make_function_declaration(name, package,
    8369            0 :                                                               type, location);
    8370            0 :   this->methods_.push_back(ret);
    8371            0 :   return ret;
    8372              : }
    8373              : 
    8374              : // Return whether any methods are defined.
    8375              : 
    8376              : bool
    8377            2 : Type_declaration::has_methods() const
    8378              : {
    8379            2 :   return !this->methods_.empty();
    8380              : }
    8381              : 
    8382              : // Define methods for the real type.
    8383              : 
    8384              : void
    8385       622967 : Type_declaration::define_methods(Named_type* nt)
    8386              : {
    8387       622967 :   if (this->methods_.empty())
    8388              :     return;
    8389              : 
    8390          518 :   while (nt->is_alias())
    8391              :     {
    8392            0 :       Type *t = nt->real_type()->forwarded();
    8393            0 :       if (t->named_type() != NULL)
    8394            0 :         nt = t->named_type();
    8395            0 :       else if (t->forward_declaration_type() != NULL)
    8396              :         {
    8397            0 :           Named_object* no = t->forward_declaration_type()->named_object();
    8398            0 :           Type_declaration* td = no->type_declaration_value();
    8399            0 :           td->methods_.insert(td->methods_.end(), this->methods_.begin(),
    8400              :                               this->methods_.end());
    8401            0 :           this->methods_.clear();
    8402            0 :           return;
    8403              :         }
    8404              :       else
    8405              :         {
    8406            0 :           for (std::vector<Named_object*>::const_iterator p =
    8407            0 :                  this->methods_.begin();
    8408            0 :                p != this->methods_.end();
    8409            0 :                ++p)
    8410            0 :             go_error_at((*p)->location(),
    8411              :                         ("invalid receiver type "
    8412              :                          "(receiver must be a named type)"));
    8413              :           return;
    8414              :         }
    8415              :     }
    8416              : 
    8417         3055 :   for (std::vector<Named_object*>::const_iterator p = this->methods_.begin();
    8418         3055 :        p != this->methods_.end();
    8419         2537 :        ++p)
    8420              :     {
    8421         2537 :       if ((*p)->is_function_declaration()
    8422         2537 :           || !(*p)->func_value()->is_sink())
    8423         2536 :         nt->add_existing_method(*p);
    8424              :     }
    8425              : }
    8426              : 
    8427              : // We are using the type.  Return true if we should issue a warning.
    8428              : 
    8429              : bool
    8430            8 : Type_declaration::using_type()
    8431              : {
    8432            8 :   bool ret = !this->issued_warning_;
    8433            8 :   this->issued_warning_ = true;
    8434            8 :   return ret;
    8435              : }
    8436              : 
    8437              : // Class Unknown_name.
    8438              : 
    8439              : // Set the real named object.
    8440              : 
    8441              : void
    8442        85539 : Unknown_name::set_real_named_object(Named_object* no)
    8443              : {
    8444        85539 :   go_assert(this->real_named_object_ == NULL);
    8445        85539 :   go_assert(!no->is_unknown());
    8446        85539 :   this->real_named_object_ = no;
    8447        85539 : }
    8448              : 
    8449              : // Class Named_object.
    8450              : 
    8451     10352768 : Named_object::Named_object(const std::string& name,
    8452              :                            const Package* package,
    8453     10352768 :                            Classification classification)
    8454     10352768 :   : name_(name), package_(package), classification_(classification),
    8455     10352768 :     is_redefinition_(false)
    8456              : {
    8457     10352768 :   if (Gogo::is_sink_name(name))
    8458      1385474 :     go_assert(classification == NAMED_OBJECT_SINK);
    8459     10352768 : }
    8460              : 
    8461              : // Make an unknown name.  This is used by the parser.  The name must
    8462              : // be resolved later.  Unknown names are only added in the current
    8463              : // package.
    8464              : 
    8465              : Named_object*
    8466        85593 : Named_object::make_unknown_name(const std::string& name,
    8467              :                                 Location location)
    8468              : {
    8469        85593 :   Named_object* named_object = new Named_object(name, NULL,
    8470        85593 :                                                 NAMED_OBJECT_UNKNOWN);
    8471        85593 :   Unknown_name* value = new Unknown_name(location);
    8472        85593 :   named_object->u_.unknown_value = value;
    8473        85593 :   return named_object;
    8474              : }
    8475              : 
    8476              : // Make a constant.
    8477              : 
    8478              : Named_object*
    8479      1024898 : Named_object::make_constant(const Typed_identifier& tid,
    8480              :                             const Package* package, Expression* expr,
    8481              :                             int iota_value)
    8482              : {
    8483      1024898 :   Named_object* named_object = new Named_object(tid.name(), package,
    8484      1024898 :                                                 NAMED_OBJECT_CONST);
    8485      1024898 :   Named_constant* named_constant = new Named_constant(tid.type(), expr,
    8486              :                                                       iota_value,
    8487      1024898 :                                                       tid.location());
    8488      1024898 :   named_object->u_.const_value = named_constant;
    8489      1024898 :   return named_object;
    8490              : }
    8491              : 
    8492              : // Make a named type.
    8493              : 
    8494              : Named_object*
    8495       752562 : Named_object::make_type(const std::string& name, const Package* package,
    8496              :                         Type* type, Location location)
    8497              : {
    8498       752562 :   Named_object* named_object = new Named_object(name, package,
    8499       752562 :                                                 NAMED_OBJECT_TYPE);
    8500       752562 :   Named_type* named_type = Type::make_named_type(named_object, type, location);
    8501       752562 :   named_object->u_.type_value = named_type;
    8502       752562 :   return named_object;
    8503              : }
    8504              : 
    8505              : // Make a type declaration.
    8506              : 
    8507              : Named_object*
    8508       623484 : Named_object::make_type_declaration(const std::string& name,
    8509              :                                     const Package* package,
    8510              :                                     Location location)
    8511              : {
    8512       623484 :   Named_object* named_object = new Named_object(name, package,
    8513       623484 :                                                 NAMED_OBJECT_TYPE_DECLARATION);
    8514       623484 :   Type_declaration* type_declaration = new Type_declaration(location);
    8515       623484 :   named_object->u_.type_declaration = type_declaration;
    8516       623484 :   return named_object;
    8517              : }
    8518              : 
    8519              : // Make a variable.
    8520              : 
    8521              : Named_object*
    8522      1743043 : Named_object::make_variable(const std::string& name, const Package* package,
    8523              :                             Variable* variable)
    8524              : {
    8525      1743043 :   Named_object* named_object = new Named_object(name, package,
    8526      1743043 :                                                 NAMED_OBJECT_VAR);
    8527      1743043 :   named_object->u_.var_value = variable;
    8528      1743043 :   return named_object;
    8529              : }
    8530              : 
    8531              : // Make a result variable.
    8532              : 
    8533              : Named_object*
    8534       244901 : Named_object::make_result_variable(const std::string& name,
    8535              :                                    Result_variable* result)
    8536              : {
    8537       244901 :   Named_object* named_object = new Named_object(name, NULL,
    8538       244901 :                                                 NAMED_OBJECT_RESULT_VAR);
    8539       244901 :   named_object->u_.result_var_value = result;
    8540       244901 :   return named_object;
    8541              : }
    8542              : 
    8543              : // Make a sink.  This is used for the special blank identifier _.
    8544              : 
    8545              : Named_object*
    8546      1385474 : Named_object::make_sink()
    8547              : {
    8548      1385474 :   return new Named_object("_", NULL, NAMED_OBJECT_SINK);
    8549              : }
    8550              : 
    8551              : // Make a named function.
    8552              : 
    8553              : Named_object*
    8554       299274 : Named_object::make_function(const std::string& name, const Package* package,
    8555              :                             Function* function)
    8556              : {
    8557       299274 :   Named_object* named_object = new Named_object(name, package,
    8558       299274 :                                                 NAMED_OBJECT_FUNC);
    8559       299274 :   named_object->u_.func_value = function;
    8560       299274 :   return named_object;
    8561              : }
    8562              : 
    8563              : // Make a function declaration.
    8564              : 
    8565              : Named_object*
    8566      4154108 : Named_object::make_function_declaration(const std::string& name,
    8567              :                                         const Package* package,
    8568              :                                         Function_type* fntype,
    8569              :                                         Location location)
    8570              : {
    8571      4154108 :   Named_object* named_object = new Named_object(name, package,
    8572      4154108 :                                                 NAMED_OBJECT_FUNC_DECLARATION);
    8573      4154108 :   Function_declaration *func_decl = new Function_declaration(fntype, location);
    8574      4154108 :   named_object->u_.func_declaration_value = func_decl;
    8575      4154108 :   return named_object;
    8576              : }
    8577              : 
    8578              : // Make a package.
    8579              : 
    8580              : Named_object*
    8581        39417 : Named_object::make_package(const std::string& alias, Package* package)
    8582              : {
    8583        39417 :   Named_object* named_object = new Named_object(alias, NULL,
    8584        39417 :                                                 NAMED_OBJECT_PACKAGE);
    8585        39417 :   named_object->u_.package_value = package;
    8586        39417 :   return named_object;
    8587              : }
    8588              : 
    8589              : // Return the name to use in an error message.
    8590              : 
    8591              : std::string
    8592       233515 : Named_object::message_name() const
    8593              : {
    8594       233515 :   if (this->package_ == NULL)
    8595       215411 :     return Gogo::message_name(this->name_);
    8596        18104 :   std::string ret;
    8597        18104 :   if (this->package_->has_package_name())
    8598        18104 :     ret = this->package_->package_name();
    8599              :   else
    8600            0 :     ret = this->package_->pkgpath();
    8601        18104 :   ret = Gogo::message_name(ret);
    8602        18104 :   ret += '.';
    8603        36208 :   ret += Gogo::message_name(this->name_);
    8604        18104 :   return ret;
    8605        18104 : }
    8606              : 
    8607              : // Set the type when a declaration is defined.
    8608              : 
    8609              : void
    8610       622967 : Named_object::set_type_value(Named_type* named_type)
    8611              : {
    8612       622967 :   go_assert(this->classification_ == NAMED_OBJECT_TYPE_DECLARATION);
    8613       622967 :   Type_declaration* td = this->u_.type_declaration;
    8614       622967 :   td->define_methods(named_type);
    8615       622967 :   unsigned int index;
    8616       622967 :   Named_object* in_function = td->in_function(&index);
    8617       622967 :   if (in_function != NULL)
    8618         1295 :     named_type->set_in_function(in_function, index);
    8619       624262 :   delete td;
    8620       622967 :   this->classification_ = NAMED_OBJECT_TYPE;
    8621       622967 :   this->u_.type_value = named_type;
    8622       622967 : }
    8623              : 
    8624              : // Define a function which was previously declared.
    8625              : 
    8626              : void
    8627       106821 : Named_object::set_function_value(Function* function)
    8628              : {
    8629       106821 :   go_assert(this->classification_ == NAMED_OBJECT_FUNC_DECLARATION);
    8630       106821 :   if (this->func_declaration_value()->has_descriptor())
    8631              :     {
    8632            0 :       Expression* descriptor =
    8633            0 :         this->func_declaration_value()->descriptor(NULL, NULL);
    8634            0 :       function->set_descriptor(descriptor);
    8635              :     }
    8636       106821 :   this->classification_ = NAMED_OBJECT_FUNC;
    8637              :   // FIXME: We should free the old value.
    8638       106821 :   this->u_.func_value = function;
    8639       106821 : }
    8640              : 
    8641              : // Declare an unknown object as a type declaration.
    8642              : 
    8643              : void
    8644            1 : Named_object::declare_as_type()
    8645              : {
    8646            1 :   go_assert(this->classification_ == NAMED_OBJECT_UNKNOWN);
    8647            1 :   Unknown_name* unk = this->u_.unknown_value;
    8648            1 :   this->classification_ = NAMED_OBJECT_TYPE_DECLARATION;
    8649            1 :   this->u_.type_declaration = new Type_declaration(unk->location());
    8650            1 :   delete unk;
    8651            1 : }
    8652              : 
    8653              : // Return the location of a named object.
    8654              : 
    8655              : Location
    8656      1914762 : Named_object::location() const
    8657              : {
    8658      1914762 :   switch (this->classification_)
    8659              :     {
    8660            0 :     default:
    8661            0 :     case NAMED_OBJECT_UNINITIALIZED:
    8662            0 :       go_unreachable();
    8663              : 
    8664            0 :     case NAMED_OBJECT_ERRONEOUS:
    8665            0 :       return Linemap::unknown_location();
    8666              : 
    8667          532 :     case NAMED_OBJECT_UNKNOWN:
    8668          532 :       return this->unknown_value()->location();
    8669              : 
    8670        12553 :     case NAMED_OBJECT_CONST:
    8671        12553 :       return this->const_value()->location();
    8672              : 
    8673        15018 :     case NAMED_OBJECT_TYPE:
    8674        15018 :       return this->type_value()->location();
    8675              : 
    8676            0 :     case NAMED_OBJECT_TYPE_DECLARATION:
    8677            0 :       return this->type_declaration_value()->location();
    8678              : 
    8679       701250 :     case NAMED_OBJECT_VAR:
    8680       701250 :       return this->var_value()->location();
    8681              : 
    8682       244740 :     case NAMED_OBJECT_RESULT_VAR:
    8683       244740 :       return this->result_var_value()->location();
    8684              : 
    8685            0 :     case NAMED_OBJECT_SINK:
    8686            0 :       go_unreachable();
    8687              : 
    8688       442504 :     case NAMED_OBJECT_FUNC:
    8689       442504 :       return this->func_value()->location();
    8690              : 
    8691       458739 :     case NAMED_OBJECT_FUNC_DECLARATION:
    8692       458739 :       return this->func_declaration_value()->location();
    8693              : 
    8694        39426 :     case NAMED_OBJECT_PACKAGE:
    8695        39426 :       return this->package_value()->location();
    8696              :     }
    8697              : }
    8698              : 
    8699              : // Traverse a Named_object.
    8700              : 
    8701              : int
    8702     69260768 : Named_object::traverse(Traverse* traverse, bool is_global)
    8703              : {
    8704     69260768 :   const unsigned int traverse_mask = traverse->traverse_mask();
    8705     69260768 :   const unsigned int e_or_t = (Traverse::traverse_expressions
    8706              :                                | Traverse::traverse_types);
    8707     69260768 :   const unsigned int e_or_t_or_s = (e_or_t
    8708              :                                     | Traverse::traverse_statements);
    8709              : 
    8710     69260768 :   int t = TRAVERSE_CONTINUE;
    8711     69260768 :   switch (this->classification_)
    8712              :     {
    8713     21316345 :     case Named_object::NAMED_OBJECT_CONST:
    8714     21316345 :       if ((traverse_mask & Traverse::traverse_constants) != 0)
    8715      2028604 :         t = traverse->constant(this, is_global);
    8716      2028604 :       if (t == TRAVERSE_CONTINUE
    8717     21316345 :           && (traverse_mask & e_or_t) != 0)
    8718              :         {
    8719     13192748 :           Type* tc = this->const_value()->type();
    8720     13192748 :           if (tc != NULL)
    8721              :             {
    8722     11609046 :               if (Type::traverse(tc, traverse) == TRAVERSE_EXIT)
    8723              :                 return TRAVERSE_EXIT;
    8724              :             }
    8725     13192748 :           t = this->const_value()->traverse_expression(traverse);
    8726              :         }
    8727              :       break;
    8728              : 
    8729     29747323 :     case Named_object::NAMED_OBJECT_VAR:
    8730     29747323 :     case Named_object::NAMED_OBJECT_RESULT_VAR:
    8731     29747323 :       if ((traverse_mask & Traverse::traverse_variables) != 0)
    8732      7660321 :         t = traverse->variable(this);
    8733      7660321 :       if (t == TRAVERSE_CONTINUE
    8734     28039029 :           && (traverse_mask & e_or_t) != 0)
    8735              :         {
    8736     22626466 :           if (this->is_result_variable() || this->var_value()->has_type())
    8737              :             {
    8738     21984274 :               Type* tv = (this->is_variable()
    8739     21984274 :                           ? this->var_value()->type()
    8740      4143250 :                           : this->result_var_value()->type());
    8741     21984274 :               if (tv != NULL)
    8742              :                 {
    8743     21984274 :                   if (Type::traverse(tv, traverse) == TRAVERSE_EXIT)
    8744              :                     return TRAVERSE_EXIT;
    8745              :                 }
    8746              :             }
    8747              :         }
    8748     28038873 :       if (t == TRAVERSE_CONTINUE
    8749     28038873 :           && (traverse_mask & e_or_t_or_s) != 0
    8750     26312579 :           && this->is_variable())
    8751     21645010 :         t = this->var_value()->traverse_expression(traverse,
    8752              :                                                    traverse_mask);
    8753              :       break;
    8754              : 
    8755      4279462 :     case Named_object::NAMED_OBJECT_FUNC:
    8756      4279462 :       if ((traverse_mask & Traverse::traverse_functions) != 0)
    8757      2369044 :         t = traverse->function(this);
    8758      2369044 :       if (t == TRAVERSE_CONTINUE
    8759      3029629 :           && (traverse_mask
    8760      3029629 :               & (Traverse::traverse_variables
    8761              :                  | Traverse::traverse_constants
    8762              :                  | Traverse::traverse_functions
    8763              :                  | Traverse::traverse_blocks
    8764              :                  | Traverse::traverse_statements
    8765              :                  | Traverse::traverse_expressions
    8766              :                  | Traverse::traverse_types)) != 0)
    8767      3029629 :         t = this->func_value()->traverse(traverse);
    8768              :       break;
    8769              : 
    8770     13091317 :     case Named_object::NAMED_OBJECT_TYPE:
    8771     13091317 :       if ((traverse_mask & e_or_t) != 0)
    8772              :         {
    8773      8062261 :           t = Type::traverse(this->type_value(), traverse);
    8774      8062261 :           if (t == TRAVERSE_EXIT)
    8775              :             return TRAVERSE_EXIT;
    8776              : 
    8777              :           // Traverse the types of any local methods that are declared
    8778              :           // but not defined.  We will see defined methods as
    8779              :           // NAMED_OBJECT_FUNC, but we won't see methods that are only
    8780              :           // declared.
    8781      8062261 :           if (this->package_ == NULL
    8782      8062261 :               && this->type_value()->named_type()->local_methods() != NULL)
    8783              :             {
    8784       151118 :               const Bindings* methods =
    8785       151118 :                 this->type_value()->named_type()->local_methods();
    8786       844855 :               for (Bindings::const_declarations_iterator p =
    8787       151118 :                      methods->begin_declarations();
    8788       844855 :                    p != methods->end_declarations();
    8789       693737 :                    ++p)
    8790              :                 {
    8791       693737 :                   if (p->second->is_function_declaration())
    8792              :                     {
    8793         9224 :                       Type* mt = p->second->func_declaration_value()->type();
    8794         9224 :                       if (Type::traverse(mt, traverse) == TRAVERSE_EXIT)
    8795          156 :                         return TRAVERSE_EXIT;
    8796              :                     }
    8797              :                 }
    8798              :             }
    8799              :         }
    8800              : 
    8801              :       break;
    8802              : 
    8803              :     case Named_object::NAMED_OBJECT_PACKAGE:
    8804              :     case Named_object::NAMED_OBJECT_FUNC_DECLARATION:
    8805              :     case Named_object::NAMED_OBJECT_TYPE_DECLARATION:
    8806              :     case Named_object::NAMED_OBJECT_UNKNOWN:
    8807              :     case Named_object::NAMED_OBJECT_ERRONEOUS:
    8808              :       break;
    8809              : 
    8810            0 :     case Named_object::NAMED_OBJECT_SINK:
    8811            0 :       go_unreachable();
    8812              : 
    8813            0 :     default:
    8814            0 :       go_unreachable();
    8815              :     }
    8816              : 
    8817              :   return t;
    8818              : }
    8819              : 
    8820              : // Export a named object.
    8821              : 
    8822              : void
    8823       428949 : Named_object::export_named_object(Export* exp) const
    8824              : {
    8825       428949 :   switch (this->classification_)
    8826              :     {
    8827            0 :     default:
    8828            0 :     case NAMED_OBJECT_UNINITIALIZED:
    8829            0 :     case NAMED_OBJECT_UNKNOWN:
    8830            0 :       go_unreachable();
    8831              : 
    8832              :     case NAMED_OBJECT_ERRONEOUS:
    8833              :       break;
    8834              : 
    8835        39984 :     case NAMED_OBJECT_CONST:
    8836        39984 :       this->const_value()->export_const(exp, this->name_);
    8837        39984 :       break;
    8838              : 
    8839            0 :     case NAMED_OBJECT_TYPE:
    8840              :       // Types are handled by export::write_types.
    8841            0 :       go_unreachable();
    8842              : 
    8843            0 :     case NAMED_OBJECT_TYPE_DECLARATION:
    8844            0 :       go_error_at(this->type_declaration_value()->location(),
    8845              :                   "attempt to export %qs which was declared but not defined",
    8846            0 :                   this->message_name().c_str());
    8847            0 :       break;
    8848              : 
    8849       301334 :     case NAMED_OBJECT_FUNC_DECLARATION:
    8850       301334 :       this->func_declaration_value()->export_func(exp, this);
    8851       301334 :       break;
    8852              : 
    8853         8440 :     case NAMED_OBJECT_VAR:
    8854         8440 :       this->var_value()->export_var(exp, this);
    8855         8440 :       break;
    8856              : 
    8857            0 :     case NAMED_OBJECT_RESULT_VAR:
    8858            0 :     case NAMED_OBJECT_SINK:
    8859            0 :       go_unreachable();
    8860              : 
    8861        79191 :     case NAMED_OBJECT_FUNC:
    8862        79191 :       this->func_value()->export_func(exp, this);
    8863        79191 :       break;
    8864              :     }
    8865       428949 : }
    8866              : 
    8867              : // Convert a variable to the backend representation.
    8868              : 
    8869              : Bvariable*
    8870      5602970 : Named_object::get_backend_variable(Gogo* gogo, Named_object* function)
    8871              : {
    8872      5602970 :   if (this->classification_ == NAMED_OBJECT_VAR)
    8873      4441091 :     return this->var_value()->get_backend_variable(gogo, function,
    8874      4441091 :                                                    this->package_, this->name_);
    8875      1161879 :   else if (this->classification_ == NAMED_OBJECT_RESULT_VAR)
    8876      1161879 :     return this->result_var_value()->get_backend_variable(gogo, function,
    8877      1161879 :                                                           this->name_);
    8878              :   else
    8879            0 :     go_unreachable();
    8880              : }
    8881              : 
    8882              : void
    8883            0 : debug_go_named_object(Named_object* no)
    8884              : {
    8885            0 :   if (no == NULL)
    8886              :     {
    8887            0 :       std::cerr << "<null>";
    8888            0 :       return;
    8889              :     }
    8890            0 :   std::cerr << "'" << no->name() << "': ";
    8891            0 :   const char *tag;
    8892            0 :   switch (no->classification())
    8893              :     {
    8894              :       case Named_object::NAMED_OBJECT_UNINITIALIZED:
    8895              :         tag = "uninitialized";
    8896              :         break;
    8897            0 :       case Named_object::NAMED_OBJECT_ERRONEOUS:
    8898            0 :         tag = "<error>";
    8899            0 :         break;
    8900            0 :       case Named_object::NAMED_OBJECT_UNKNOWN:
    8901            0 :         tag = "<unknown>";
    8902            0 :         break;
    8903            0 :       case Named_object::NAMED_OBJECT_CONST:
    8904            0 :         tag = "constant";
    8905            0 :         break;
    8906            0 :       case Named_object::NAMED_OBJECT_TYPE:
    8907            0 :         tag = "type";
    8908            0 :         break;
    8909            0 :       case Named_object::NAMED_OBJECT_TYPE_DECLARATION:
    8910            0 :         tag = "type_decl";
    8911            0 :         break;
    8912            0 :       case Named_object::NAMED_OBJECT_VAR:
    8913            0 :         tag = "var";
    8914            0 :         break;
    8915            0 :       case Named_object::NAMED_OBJECT_RESULT_VAR:
    8916            0 :         tag = "result_var";
    8917            0 :         break;
    8918            0 :       case Named_object::NAMED_OBJECT_SINK:
    8919            0 :         tag = "<sink>";
    8920            0 :         break;
    8921            0 :       case Named_object::NAMED_OBJECT_FUNC:
    8922            0 :         tag = "func";
    8923            0 :         break;
    8924            0 :       case Named_object::NAMED_OBJECT_FUNC_DECLARATION:
    8925            0 :         tag = "func_decl";
    8926            0 :         break;
    8927            0 :       case Named_object::NAMED_OBJECT_PACKAGE:
    8928            0 :         tag = "package";
    8929            0 :         break;
    8930            0 :       default:
    8931            0 :         tag = "<unknown named object classification>";
    8932            0 :         break;
    8933            0 :   };
    8934            0 :   std::cerr << tag << "\n";
    8935              : }
    8936              : 
    8937              : // Get the backend representation for this named object.
    8938              : 
    8939              : void
    8940       349619 : Named_object::get_backend(Gogo* gogo, std::vector<Bexpression*>& const_decls,
    8941              :                           std::vector<Btype*>& type_decls,
    8942              :                           std::vector<Bfunction*>& func_decls)
    8943              : {
    8944              :   // If this is a definition, avoid trying to get the backend
    8945              :   // representation, as that can crash.
    8946       349619 :   if (this->is_redefinition_)
    8947              :     {
    8948           18 :       go_assert(saw_errors());
    8949              :       return;
    8950              :     }
    8951              : 
    8952       349601 :   switch (this->classification_)
    8953              :     {
    8954        23399 :     case NAMED_OBJECT_CONST:
    8955        23399 :       if (!Gogo::is_erroneous_name(this->name_))
    8956        23399 :         const_decls.push_back(this->u_.const_value->get_backend(gogo, this));
    8957              :       break;
    8958              : 
    8959        30695 :     case NAMED_OBJECT_TYPE:
    8960        30695 :       {
    8961        30695 :         Named_type* named_type = this->u_.type_value;
    8962              : 
    8963              :         // No need to do anything for aliases-- whatever has to be done
    8964              :         // can be done for the alias target.
    8965        30695 :         if (named_type->is_alias())
    8966              :           break;
    8967              : 
    8968        30401 :         if (!Gogo::is_erroneous_name(this->name_))
    8969        30401 :           type_decls.push_back(named_type->get_backend(gogo));
    8970              : 
    8971              :         // We need to produce a type descriptor for every named
    8972              :         // type, and for a pointer to every named type, since
    8973              :         // other files or packages might refer to them.  We need
    8974              :         // to do this even for hidden types, because they might
    8975              :         // still be returned by some function.  Simply calling the
    8976              :         // type_descriptor method is enough to create the type
    8977              :         // descriptor, even though we don't do anything with it.
    8978        30401 :         if (this->package_ == NULL && !saw_errors())
    8979              :           {
    8980        29583 :             named_type->
    8981        29583 :                 type_descriptor_pointer(gogo, Linemap::predeclared_location());
    8982        29583 :             Type* pn = Type::make_pointer_type(named_type);
    8983        29583 :             pn->type_descriptor_pointer(gogo, Linemap::predeclared_location());
    8984        29583 :             if (named_type->in_heap())
    8985              :               {
    8986        29358 :                 named_type->gc_symbol_pointer(gogo);
    8987        29358 :                 pn->gc_symbol_pointer(gogo);
    8988              :               }
    8989              :           }
    8990              :       }
    8991              :       break;
    8992              : 
    8993            0 :     case NAMED_OBJECT_TYPE_DECLARATION:
    8994            0 :       go_error_at(Linemap::unknown_location(),
    8995              :                   "reference to undefined type %qs",
    8996            0 :                   this->message_name().c_str());
    8997            0 :       return;
    8998              : 
    8999            0 :     case NAMED_OBJECT_VAR:
    9000            0 :     case NAMED_OBJECT_RESULT_VAR:
    9001            0 :     case NAMED_OBJECT_SINK:
    9002            0 :       go_unreachable();
    9003              : 
    9004       295494 :     case NAMED_OBJECT_FUNC:
    9005       295494 :       {
    9006       295494 :         Function* func = this->u_.func_value;
    9007       295494 :         if (!Gogo::is_erroneous_name(this->name_))
    9008       295494 :           func_decls.push_back(func->get_or_make_decl(gogo, this));
    9009              : 
    9010       295494 :         if (func->block() != NULL)
    9011       295494 :           func->build(gogo, this);
    9012              :       }
    9013              :       break;
    9014              : 
    9015              :     case NAMED_OBJECT_ERRONEOUS:
    9016              :       break;
    9017              : 
    9018            0 :     default:
    9019            0 :       go_unreachable();
    9020              :     }
    9021              : }
    9022              : 
    9023              : // Class Bindings.
    9024              : 
    9025      3142387 : Bindings::Bindings(Bindings* enclosing)
    9026      3142387 :   : enclosing_(enclosing), named_objects_(), bindings_()
    9027              : {
    9028      3142387 : }
    9029              : 
    9030              : // Clear imports.
    9031              : 
    9032              : void
    9033        12707 : Bindings::clear_file_scope(Gogo* gogo)
    9034              : {
    9035        12707 :   Contour::iterator p = this->bindings_.begin();
    9036      3427599 :   while (p != this->bindings_.end())
    9037              :     {
    9038      3414892 :       bool keep;
    9039      3414892 :       if (p->second->package() != NULL)
    9040              :         keep = false;
    9041      3352391 :       else if (p->second->is_package())
    9042              :         keep = false;
    9043      6625952 :       else if (p->second->is_function()
    9044      1308250 :                && !p->second->func_value()->type()->is_method()
    9045      4621226 :                && Gogo::unpack_hidden_name(p->second->name()) == "init")
    9046              :         keep = false;
    9047              :       else
    9048      3312976 :         keep = true;
    9049              : 
    9050      3312976 :       if (keep)
    9051      3312976 :         ++p;
    9052              :       else
    9053              :         {
    9054       101916 :           gogo->add_file_block_name(p->second->name(), p->second->location());
    9055       101916 :           p = this->bindings_.erase(p);
    9056              :         }
    9057              :     }
    9058        12707 : }
    9059              : 
    9060              : // Look up a symbol.
    9061              : 
    9062              : Named_object*
    9063      8266853 : Bindings::lookup(const std::string& name) const
    9064              : {
    9065     13217175 :   Contour::const_iterator p = this->bindings_.find(name);
    9066     13217175 :   if (p != this->bindings_.end())
    9067      5475853 :     return p->second->resolve();
    9068      7741322 :   else if (this->enclosing_ != NULL)
    9069              :     return this->enclosing_->lookup(name);
    9070              :   else
    9071              :     return NULL;
    9072              : }
    9073              : 
    9074              : // Look up a symbol locally.
    9075              : 
    9076              : Named_object*
    9077       643809 : Bindings::lookup_local(const std::string& name) const
    9078              : {
    9079       643809 :   Contour::const_iterator p = this->bindings_.find(name);
    9080       643809 :   if (p == this->bindings_.end())
    9081              :     return NULL;
    9082       334424 :   return p->second;
    9083              : }
    9084              : 
    9085              : // Remove an object from a set of bindings.  This is used for a
    9086              : // special case in thunks for functions which call recover.
    9087              : 
    9088              : void
    9089          831 : Bindings::remove_binding(Named_object* no)
    9090              : {
    9091          831 :   Contour::iterator pb = this->bindings_.find(no->name());
    9092          831 :   go_assert(pb != this->bindings_.end());
    9093          831 :   this->bindings_.erase(pb);
    9094         1048 :   for (std::vector<Named_object*>::iterator pn = this->named_objects_.begin();
    9095         1048 :        pn != this->named_objects_.end();
    9096          217 :        ++pn)
    9097              :     {
    9098         1048 :       if (*pn == no)
    9099              :         {
    9100          831 :           this->named_objects_.erase(pn);
    9101          831 :           return;
    9102              :         }
    9103              :     }
    9104            0 :   go_unreachable();
    9105              : }
    9106              : 
    9107              : // Add a method to the list of objects.  This is not added to the
    9108              : // lookup table.  This is so that we have a single list of objects
    9109              : // declared at the top level, which we walk through when it's time to
    9110              : // convert to trees.
    9111              : 
    9112              : void
    9113        78045 : Bindings::add_method(Named_object* method)
    9114              : {
    9115        78045 :   this->named_objects_.push_back(method);
    9116        78045 : }
    9117              : 
    9118              : // Add a generic Named_object to a Contour.
    9119              : 
    9120              : Named_object*
    9121      7487992 : Bindings::add_named_object_to_contour(Contour* contour,
    9122              :                                       Named_object* named_object)
    9123              : {
    9124      7487992 :   go_assert(named_object == named_object->resolve());
    9125      7487992 :   const std::string& name(named_object->name());
    9126      7487992 :   go_assert(!Gogo::is_sink_name(name));
    9127              : 
    9128      7487992 :   std::pair<Contour::iterator, bool> ins =
    9129     14975984 :     contour->insert(std::make_pair(name, named_object));
    9130      7487992 :   if (!ins.second)
    9131              :     {
    9132              :       // The name was already there.
    9133       825981 :       if (named_object->package() != NULL
    9134       675130 :           && ins.first->second->package() == named_object->package()
    9135      1501111 :           && (ins.first->second->classification()
    9136       675130 :               == named_object->classification()))
    9137              :         {
    9138              :           // This is a second import of the same object.
    9139              :           return ins.first->second;
    9140              :         }
    9141       749427 :       ins.first->second = this->new_definition(ins.first->second,
    9142              :                                                named_object);
    9143       749427 :       return ins.first->second;
    9144              :     }
    9145              :   else
    9146              :     {
    9147              :       // Don't push declarations on the list.  We push them on when
    9148              :       // and if we find the definitions.  That way we genericize the
    9149              :       // functions in order.
    9150      6662011 :       if (!named_object->is_type_declaration()
    9151              :           && !named_object->is_function_declaration()
    9152              :           && !named_object->is_unknown())
    9153      2759825 :         this->named_objects_.push_back(named_object);
    9154      6662011 :       return named_object;
    9155              :     }
    9156              : }
    9157              : 
    9158              : // We had an existing named object OLD_OBJECT, and we've seen a new
    9159              : // one NEW_OBJECT with the same name.  FIXME: This does not free the
    9160              : // new object when we don't need it.
    9161              : 
    9162              : Named_object*
    9163       749427 : Bindings::new_definition(Named_object* old_object, Named_object* new_object)
    9164              : {
    9165       749427 :   if (new_object->is_erroneous() && !old_object->is_erroneous())
    9166              :     return new_object;
    9167              : 
    9168       749427 :   std::string reason;
    9169       749427 :   switch (old_object->classification())
    9170              :     {
    9171            0 :     default:
    9172            0 :     case Named_object::NAMED_OBJECT_UNINITIALIZED:
    9173            0 :       go_unreachable();
    9174              : 
    9175              :     case Named_object::NAMED_OBJECT_ERRONEOUS:
    9176              :       return old_object;
    9177              : 
    9178        55724 :     case Named_object::NAMED_OBJECT_UNKNOWN:
    9179        55724 :       {
    9180        55724 :         Named_object* real = old_object->unknown_value()->real_named_object();
    9181        55724 :         if (real != NULL)
    9182            0 :           return this->new_definition(real, new_object);
    9183        55724 :         go_assert(!new_object->is_unknown());
    9184        55724 :         old_object->unknown_value()->set_real_named_object(new_object);
    9185        55724 :         if (!new_object->is_type_declaration()
    9186        55724 :             && !new_object->is_function_declaration())
    9187        44390 :           this->named_objects_.push_back(new_object);
    9188              :         return new_object;
    9189              :       }
    9190              : 
    9191              :     case Named_object::NAMED_OBJECT_CONST:
    9192              :       break;
    9193              : 
    9194            0 :     case Named_object::NAMED_OBJECT_TYPE:
    9195            0 :       if (new_object->is_type_declaration())
    9196              :         return old_object;
    9197              :       break;
    9198              : 
    9199       586845 :     case Named_object::NAMED_OBJECT_TYPE_DECLARATION:
    9200       586845 :       if (new_object->is_type_declaration())
    9201              :         return old_object;
    9202       586329 :       if (new_object->is_type())
    9203              :         {
    9204       586329 :           old_object->set_type_value(new_object->type_value());
    9205       586329 :           new_object->type_value()->set_named_object(old_object);
    9206       586329 :           this->named_objects_.push_back(old_object);
    9207       586329 :           return old_object;
    9208              :         }
    9209              :       break;
    9210              : 
    9211           13 :     case Named_object::NAMED_OBJECT_VAR:
    9212           13 :     case Named_object::NAMED_OBJECT_RESULT_VAR:
    9213              :       // We have already given an error in the parser for cases where
    9214              :       // one parameter or result variable redeclares another one.
    9215           13 :       if ((new_object->is_variable()
    9216            8 :            && new_object->var_value()->is_parameter())
    9217           18 :           || new_object->is_result_variable())
    9218              :         return old_object;
    9219              :       break;
    9220              : 
    9221            0 :     case Named_object::NAMED_OBJECT_SINK:
    9222            0 :       go_unreachable();
    9223              : 
    9224              :     case Named_object::NAMED_OBJECT_FUNC:
    9225              :       break;
    9226              : 
    9227       106826 :     case Named_object::NAMED_OBJECT_FUNC_DECLARATION:
    9228       106826 :       {
    9229              :         // We declare the hash and equality functions before defining
    9230              :         // them, because we sometimes see that we need the declaration
    9231              :         // while we are in the middle of a different function.
    9232              :         //
    9233              :         // We declare the main function before the user defines it, to
    9234              :         // give better error messages.
    9235              :         //
    9236              :         // We declare inline functions before we define them, as we
    9237              :         // only define them if we need them.
    9238       213652 :         if (new_object->is_function()
    9239       106826 :             && ((Linemap::is_predeclared_location(old_object->location())
    9240        92810 :                  && Linemap::is_predeclared_location(new_object->location()))
    9241       120840 :                 || (Gogo::unpack_hidden_name(old_object->name()) == "main"
    9242         1764 :                     && Linemap::is_unknown_location(old_object->location()))
    9243        12250 :                 || (new_object->package() != NULL
    9244        12247 :                     && old_object->func_declaration_value()->has_imported_body()
    9245        12247 :                     && new_object->func_value()->is_inline_only())))
    9246              :           {
    9247       106821 :             Function_type* old_type =
    9248       106821 :                 old_object->func_declaration_value()->type();
    9249       106821 :             Function_type* new_type = new_object->func_value()->type();
    9250       106821 :             if (old_type->is_valid_redeclaration(new_type, &reason))
    9251              :               {
    9252       106821 :                 Function_declaration* fd =
    9253       106821 :                   old_object->func_declaration_value();
    9254       106821 :                 go_assert(fd->asm_name().empty());
    9255       106821 :                 old_object->set_function_value(new_object->func_value());
    9256       106821 :                 this->named_objects_.push_back(old_object);
    9257       106821 :                 return old_object;
    9258              :               }
    9259              :           }
    9260              :       }
    9261              :       break;
    9262              : 
    9263              :     case Named_object::NAMED_OBJECT_PACKAGE:
    9264              :       break;
    9265              :     }
    9266              : 
    9267           56 :   std::string n = old_object->message_name();
    9268           28 :   if (reason.empty())
    9269           28 :     go_error_at(new_object->location(), "redefinition of %qs", n.c_str());
    9270              :   else
    9271            0 :     go_error_at(new_object->location(), "redefinition of %qs: %s", n.c_str(),
    9272              :                 reason.c_str());
    9273           28 :   old_object->set_is_redefinition();
    9274           28 :   new_object->set_is_redefinition();
    9275              : 
    9276           28 :   if (!Linemap::is_unknown_location(old_object->location())
    9277           28 :       && !Linemap::is_predeclared_location(old_object->location()))
    9278           27 :     go_inform(old_object->location(), "previous definition of %qs was here",
    9279              :               n.c_str());
    9280              : 
    9281           28 :   return old_object;
    9282       749427 : }
    9283              : 
    9284              : // Add a named type.
    9285              : 
    9286              : Named_object*
    9287        97599 : Bindings::add_named_type(Named_type* named_type)
    9288              : {
    9289        97599 :   return this->add_named_object(named_type->named_object());
    9290              : }
    9291              : 
    9292              : // Add a function.
    9293              : 
    9294              : Named_object*
    9295       268709 : Bindings::add_function(const std::string& name, const Package* package,
    9296              :                        Function* function)
    9297              : {
    9298       268709 :   return this->add_named_object(Named_object::make_function(name, package,
    9299       268709 :                                                             function));
    9300              : }
    9301              : 
    9302              : // Add a function declaration.
    9303              : 
    9304              : Named_object*
    9305      3242196 : Bindings::add_function_declaration(const std::string& name,
    9306              :                                    const Package* package,
    9307              :                                    Function_type* type,
    9308              :                                    Location location)
    9309              : {
    9310      3242196 :   Named_object* no = Named_object::make_function_declaration(name, package,
    9311              :                                                              type, location);
    9312      3242196 :   return this->add_named_object(no);
    9313              : }
    9314              : 
    9315              : // Define a type which was previously declared.
    9316              : 
    9317              : void
    9318        31990 : Bindings::define_type(Named_object* no, Named_type* type)
    9319              : {
    9320        31990 :   no->set_type_value(type);
    9321        31990 :   this->named_objects_.push_back(no);
    9322        31990 : }
    9323              : 
    9324              : // Mark all local variables as used.  This is used for some types of
    9325              : // parse error.
    9326              : 
    9327              : void
    9328           56 : Bindings::mark_locals_used()
    9329              : {
    9330           72 :   for (std::vector<Named_object*>::iterator p = this->named_objects_.begin();
    9331           72 :        p != this->named_objects_.end();
    9332           16 :        ++p)
    9333           16 :     if ((*p)->is_variable())
    9334           12 :       (*p)->var_value()->set_is_used();
    9335           56 : }
    9336              : 
    9337              : // Traverse bindings.
    9338              : 
    9339              : int
    9340      2404332 : Bindings::traverse(Traverse* traverse, bool is_global)
    9341              : {
    9342      2404332 :   unsigned int traverse_mask = traverse->traverse_mask();
    9343              : 
    9344              :   // We don't use an iterator because we permit the traversal to add
    9345              :   // new global objects.
    9346      2404332 :   const unsigned int e_or_t = (Traverse::traverse_expressions
    9347              :                                | Traverse::traverse_types);
    9348     48895026 :   for (size_t i = 0; i < this->named_objects_.size(); ++i)
    9349              :     {
    9350     46490694 :       Named_object* p = this->named_objects_[i];
    9351     46490694 :       if (p->traverse(traverse, is_global) == TRAVERSE_EXIT)
    9352              :         return TRAVERSE_EXIT;
    9353              :     }
    9354              : 
    9355              :   // If we need to traverse types, check the function declarations,
    9356              :   // which have types.  Also check any methods of a type declaration.
    9357      2404332 :   if ((traverse_mask & e_or_t) != 0)
    9358              :     {
    9359     43991414 :       for (Bindings::const_declarations_iterator p =
    9360      1481305 :              this->begin_declarations();
    9361     43991414 :            p != this->end_declarations();
    9362     42510109 :            ++p)
    9363              :         {
    9364     42510109 :           if (p->second->is_function_declaration())
    9365              :             {
    9366     15386027 :               if (Type::traverse(p->second->func_declaration_value()->type(),
    9367              :                                  traverse)
    9368              :                   == TRAVERSE_EXIT)
    9369      2404332 :                 return TRAVERSE_EXIT;
    9370              :             }
    9371     27124082 :           else if (p->second->is_type_declaration())
    9372              :             {
    9373           20 :               const std::vector<Named_object*>* methods =
    9374           20 :                 p->second->type_declaration_value()->methods();
    9375           40 :               for (std::vector<Named_object*>::const_iterator pm =
    9376           20 :                      methods->begin();
    9377           40 :                    pm != methods->end();
    9378           20 :                    pm++)
    9379              :                 {
    9380           20 :                   Named_object* no = *pm;
    9381           20 :                   Type *t;
    9382           20 :                   if (no->is_function())
    9383           20 :                     t = no->func_value()->type();
    9384            0 :                   else if (no->is_function_declaration())
    9385            0 :                     t = no->func_declaration_value()->type();
    9386              :                   else
    9387            0 :                     continue;
    9388           20 :                   if (Type::traverse(t, traverse) == TRAVERSE_EXIT)
    9389            0 :                     return TRAVERSE_EXIT;
    9390              :                 }
    9391              :             }
    9392              :         }
    9393              :     }
    9394              : 
    9395              :   // Traverse function declarations when needed.
    9396      2404332 :   if ((traverse_mask & Traverse::traverse_func_declarations) != 0)
    9397              :     {
    9398      3305840 :       for (Bindings::const_declarations_iterator p = this->begin_declarations();
    9399      3305840 :            p != this->end_declarations();
    9400      3193277 :            ++p)
    9401              :         {
    9402      3193277 :           if (p->second->is_function_declaration())
    9403              :             {
    9404      1149080 :               if (traverse->function_declaration(p->second) == TRAVERSE_EXIT)
    9405            0 :                 return TRAVERSE_EXIT;
    9406              :             }
    9407              :         }
    9408              :     }
    9409              : 
    9410              :   return TRAVERSE_CONTINUE;
    9411              : }
    9412              : 
    9413              : // Determine types for the objects.
    9414              : 
    9415              : void
    9416         4646 : Bindings::determine_types(Gogo* gogo)
    9417              : {
    9418              :   // We don't use an iterator because the traversal can add new
    9419              :   // bindings.
    9420       457914 :   for (size_t i = 0; i < this->named_objects_.size(); ++i)
    9421              :     {
    9422       453268 :       Named_object* no = this->named_objects_[i];
    9423       453268 :       if (no->is_function())
    9424       175575 :         no->func_value()->determine_types(gogo);
    9425       277693 :       else if (no->is_variable())
    9426        36701 :         no->var_value()->determine_type(gogo);
    9427       240992 :       else if (no->is_const())
    9428       157051 :         no->const_value()->determine_type(gogo);
    9429              : 
    9430              :       // See if a variable requires us to build an initialization
    9431              :       // function.  We know that we will see all global variables
    9432              :       // here.
    9433       453268 :       if (!gogo->need_init_fn() && no->is_variable())
    9434              :         {
    9435         2937 :           Variable* variable = no->var_value();
    9436              : 
    9437              :           // If this is a global variable which requires runtime
    9438              :           // initialization, we need an initialization function.
    9439              : 
    9440         2937 :           if (!variable->is_global())
    9441            0 :             continue;
    9442              : 
    9443         2937 :           if (variable->init() == NULL)
    9444              :             ;
    9445         2258 :           else if (variable->type()->interface_type() != NULL)
    9446          253 :             gogo->set_need_init_fn();
    9447         2005 :           else if (variable->init()->is_constant())
    9448              :             ;
    9449         1573 :           else if (!variable->init()->is_composite_literal())
    9450          494 :             gogo->set_need_init_fn();
    9451         1079 :           else if (variable->init()->is_nonconstant_composite_literal())
    9452            0 :             gogo->set_need_init_fn();
    9453              : 
    9454              :           // If this global variable holds a pointer value, we need an
    9455              :           // initialization function to register it as a GC root.
    9456         2937 :           if (variable->type()->has_pointer())
    9457         1716 :             gogo->set_need_init_fn();
    9458              :         }
    9459              :     }
    9460         4646 : }
    9461              : 
    9462              : void
    9463            0 : Bindings::debug_dump()
    9464              : {
    9465            0 :   std::set<Named_object*> defs;
    9466            0 :   for (size_t i = 0; i < this->named_objects_.size(); ++i)
    9467            0 :     defs.insert(this->named_objects_[i]);
    9468            0 :   for (Contour::iterator p = this->bindings_.begin();
    9469            0 :        p != this->bindings_.end();
    9470            0 :        ++p)
    9471              :     {
    9472            0 :       const char* tag = "  ";
    9473            0 :       if (defs.find(p->second) != defs.end())
    9474            0 :         tag = "* ";
    9475            0 :       std::cerr << tag;
    9476            0 :       debug_go_named_object(p->second);
    9477              :     }
    9478            0 : }
    9479              : 
    9480              : void
    9481            0 : debug_go_bindings(Bindings* bindings)
    9482              : {
    9483            0 :   if (bindings != NULL)
    9484            0 :     bindings->debug_dump();
    9485            0 : }
    9486              : 
    9487              : // Class Label.
    9488              : 
    9489              : // Clear any references to this label.
    9490              : 
    9491              : void
    9492        10524 : Label::clear_refs()
    9493              : {
    9494        12089 :   for (std::vector<Bindings_snapshot*>::iterator p = this->refs_.begin();
    9495        12089 :        p != this->refs_.end();
    9496         1565 :        ++p)
    9497         3130 :     delete *p;
    9498        10524 :   this->refs_.clear();
    9499        10524 : }
    9500              : 
    9501              : // Get the backend representation for a label.
    9502              : 
    9503              : Blabel*
    9504        30094 : Label::get_backend_label(Translate_context* context)
    9505              : {
    9506        30094 :   if (this->blabel_ == NULL)
    9507              :     {
    9508        10412 :       Function* function = context->function()->func_value();
    9509        10412 :       Bfunction* bfunction = function->get_decl();
    9510        10412 :       this->blabel_ = context->backend()->label(bfunction, this->name_,
    9511              :                                                 this->location_);
    9512              :     }
    9513        30094 :   return this->blabel_;
    9514              : }
    9515              : 
    9516              : // Return an expression for the address of this label.
    9517              : 
    9518              : Bexpression*
    9519         8840 : Label::get_addr(Translate_context* context, Location location)
    9520              : {
    9521         8840 :   Blabel* label = this->get_backend_label(context);
    9522         8840 :   return context->backend()->label_address(label, location);
    9523              : }
    9524              : 
    9525              : // Return the dummy label that represents any instance of the blank label.
    9526              : 
    9527              : Label*
    9528            3 : Label::create_dummy_label()
    9529              : {
    9530            3 :   static Label* dummy_label;
    9531            3 :   if (dummy_label == NULL)
    9532              :     {
    9533            2 :       dummy_label = new Label("_");
    9534            2 :       dummy_label->set_is_used();
    9535              :     }
    9536            3 :   return dummy_label;
    9537              : }
    9538              : 
    9539              : // Class Unnamed_label.
    9540              : 
    9541              : // Get the backend representation for an unnamed label.
    9542              : 
    9543              : Blabel*
    9544       398195 : Unnamed_label::get_blabel(Translate_context* context)
    9545              : {
    9546       398195 :   if (this->blabel_ == NULL)
    9547              :     {
    9548       174399 :       Function* function = context->function()->func_value();
    9549       174399 :       Bfunction* bfunction = function->get_decl();
    9550       174399 :       this->blabel_ = context->backend()->label(bfunction, "",
    9551              :                                                 this->location_);
    9552              :     }
    9553       398195 :   return this->blabel_;
    9554              : }
    9555              : 
    9556              : // Return a statement which defines this unnamed label.
    9557              : 
    9558              : Bstatement*
    9559       174399 : Unnamed_label::get_definition(Translate_context* context)
    9560              : {
    9561       174399 :   Blabel* blabel = this->get_blabel(context);
    9562       174399 :   return context->backend()->label_definition_statement(blabel);
    9563              : }
    9564              : 
    9565              : // Return a goto statement to this unnamed label.
    9566              : 
    9567              : Bstatement*
    9568       223796 : Unnamed_label::get_goto(Translate_context* context, Location location)
    9569              : {
    9570       223796 :   Blabel* blabel = this->get_blabel(context);
    9571       223796 :   return context->backend()->goto_statement(blabel, location);
    9572              : }
    9573              : 
    9574              : // Class Package.
    9575              : 
    9576       116642 : Package::Package(const std::string& pkgpath,
    9577       116642 :                  const std::string& pkgpath_symbol, Location location)
    9578       233284 :   : pkgpath_(pkgpath), pkgpath_symbol_(pkgpath_symbol),
    9579       116642 :     package_name_(), bindings_(new Bindings(NULL)),
    9580       116642 :     location_(location)
    9581              : {
    9582       116642 :   go_assert(!pkgpath.empty());
    9583       116642 : }
    9584              : 
    9585              : // Set the package name.
    9586              : 
    9587              : void
    9588       244825 : Package::set_package_name(const std::string& package_name, Location location)
    9589              : {
    9590       244825 :   go_assert(!package_name.empty());
    9591       244825 :   if (this->package_name_.empty())
    9592       116642 :     this->package_name_ = package_name;
    9593       128183 :   else if (this->package_name_ != package_name)
    9594            0 :     go_error_at(location,
    9595              :                 ("saw two different packages with "
    9596              :                  "the same package path %s: %s, %s"),
    9597              :                 this->pkgpath_.c_str(), this->package_name_.c_str(),
    9598              :                 package_name.c_str());
    9599       244825 : }
    9600              : 
    9601              : // Return the pkgpath symbol, which is a prefix for symbols defined in
    9602              : // this package.
    9603              : 
    9604              : std::string
    9605       194269 : Package::pkgpath_symbol() const
    9606              : {
    9607       194269 :   if (this->pkgpath_symbol_.empty())
    9608       101672 :     return Gogo::pkgpath_for_symbol(this->pkgpath_);
    9609        92597 :   return this->pkgpath_symbol_;
    9610              : }
    9611              : 
    9612              : // Set the package path symbol.
    9613              : 
    9614              : void
    9615        14338 : Package::set_pkgpath_symbol(const std::string& pkgpath_symbol)
    9616              : {
    9617        14338 :   go_assert(!pkgpath_symbol.empty());
    9618        14338 :   if (this->pkgpath_symbol_.empty())
    9619        11763 :     this->pkgpath_symbol_ = pkgpath_symbol;
    9620              :   else
    9621         2575 :     go_assert(this->pkgpath_symbol_ == pkgpath_symbol);
    9622        14338 : }
    9623              : 
    9624              : // Note that symbol from this package was and qualified by ALIAS.
    9625              : 
    9626              : void
    9627       322202 : Package::note_usage(const std::string& alias) const
    9628              : {
    9629       322202 :   Aliases::const_iterator p = this->aliases_.find(alias);
    9630       322202 :   go_assert(p != this->aliases_.end());
    9631       322202 :   p->second->note_usage();
    9632       322202 : }
    9633              : 
    9634              : // Forget a given usage.  If forgetting this usage means this package becomes
    9635              : // unused, report that error.
    9636              : 
    9637              : void
    9638           39 : Package::forget_usage(Expression* usage) const
    9639              : {
    9640           39 :   if (this->fake_uses_.empty())
    9641           39 :     return;
    9642              : 
    9643            0 :   std::set<Expression*>::iterator p = this->fake_uses_.find(usage);
    9644            0 :   go_assert(p != this->fake_uses_.end());
    9645            0 :   this->fake_uses_.erase(p);
    9646              : 
    9647            0 :   if (this->fake_uses_.empty())
    9648            0 :     go_error_at(this->location(), "imported and not used: %s",
    9649            0 :                 Gogo::message_name(this->package_name()).c_str());
    9650              : }
    9651              : 
    9652              : // Clear the used field for the next file.  If the only usages of this package
    9653              : // are possibly fake, keep the fake usages for lowering.
    9654              : 
    9655              : void
    9656       362498 : Package::clear_used()
    9657              : {
    9658       362498 :   std::string dot_alias = "." + this->package_name();
    9659       362498 :   Aliases::const_iterator p = this->aliases_.find(dot_alias);
    9660       362498 :   if (p != this->aliases_.end() && p->second->used() > this->fake_uses_.size())
    9661          335 :     this->fake_uses_.clear();
    9662              : 
    9663       362498 :   this->aliases_.clear();
    9664       362498 : }
    9665              : 
    9666              : Package_alias*
    9667        39756 : Package::add_alias(const std::string& alias, Location location)
    9668              : {
    9669        39756 :   Aliases::const_iterator p = this->aliases_.find(alias);
    9670        39756 :   if (p == this->aliases_.end())
    9671              :     {
    9672        39756 :       std::pair<Aliases::iterator, bool> ret;
    9673        39756 :       ret = this->aliases_.insert(std::make_pair(alias,
    9674        79512 :                                                  new Package_alias(location)));
    9675        39756 :       p = ret.first;
    9676              :     }
    9677        39756 :   return p->second;
    9678              : }
    9679              : 
    9680              : // Determine types of constants.  Everything else in a package
    9681              : // (variables, function declarations) should already have a fixed
    9682              : // type.  Constants may have abstract types.
    9683              : 
    9684              : void
    9685       116245 : Package::determine_types(Gogo* gogo)
    9686              : {
    9687       116245 :   Bindings* bindings = this->bindings_;
    9688      2322947 :   for (Bindings::const_definitions_iterator p = bindings->begin_definitions();
    9689      2322947 :        p != bindings->end_definitions();
    9690      2206702 :        ++p)
    9691              :     {
    9692      2206702 :       if ((*p)->is_const())
    9693      1008588 :         (*p)->const_value()->determine_type(gogo);
    9694              :     }
    9695       116245 : }
    9696              : 
    9697              : // Class Traverse.
    9698              : 
    9699              : // Destructor.
    9700              : 
    9701     17888553 : Traverse::~Traverse()
    9702              : {
    9703     17888553 :   if (this->types_seen_ != NULL)
    9704      8403595 :     delete this->types_seen_;
    9705     17888553 :   if (this->expressions_seen_ != NULL)
    9706       671165 :     delete this->expressions_seen_;
    9707     17888553 : }
    9708              : 
    9709              : // Record that we are looking at a type, and return true if we have
    9710              : // already seen it.
    9711              : 
    9712              : bool
    9713   1186508470 : Traverse::remember_type(const Type* type)
    9714              : {
    9715   1186508470 :   if (type->is_error_type())
    9716              :     return true;
    9717   1186498359 :   go_assert((this->traverse_mask() & traverse_types) != 0
    9718              :              || (this->traverse_mask() & traverse_expressions) != 0);
    9719              :   // We mostly only have to remember named types.  But it turns out
    9720              :   // that an interface type can refer to itself without using a name
    9721              :   // by relying on interface inheritance, as in
    9722              :   //
    9723              :   //         type I interface { F() interface{I} }
    9724              :   //
    9725              :   // Similarly it is possible for array types to refer to themselves
    9726              :   // without a name, e.g.
    9727              :   //
    9728              :   //         var x [uintptr(unsafe.Sizeof(&x))]byte
    9729              :   //
    9730   1186498359 :   if (type->classification() != Type::TYPE_NAMED
    9731    602269142 :       && type->classification() != Type::TYPE_ARRAY
    9732   1709022487 :       && type->classification() != Type::TYPE_INTERFACE)
    9733              :     return false;
    9734    677827132 :   if (this->types_seen_ == NULL)
    9735      8403595 :     this->types_seen_ = new Types_seen();
    9736    677827132 :   std::pair<Types_seen::iterator, bool> ins = this->types_seen_->insert(type);
    9737    677827132 :   return !ins.second;
    9738              : }
    9739              : 
    9740              : // Record that we are looking at an expression, and return true if we
    9741              : // have already seen it. NB: this routine used to assert if the traverse
    9742              : // mask did not include expressions/types -- this is no longer the case,
    9743              : // since it can be useful to remember specific expressions during
    9744              : // walks that only cover statements.
    9745              : 
    9746              : bool
    9747      3237533 : Traverse::remember_expression(const Expression* expression)
    9748              : {
    9749      3237533 :   if (this->expressions_seen_ == NULL)
    9750       671165 :     this->expressions_seen_ = new Expressions_seen();
    9751      3237533 :   std::pair<Expressions_seen::iterator, bool> ins =
    9752      3237533 :     this->expressions_seen_->insert(expression);
    9753      3237533 :   return !ins.second;
    9754              : }
    9755              : 
    9756              : // The default versions of these functions should never be called: the
    9757              : // traversal mask indicates which functions may be called.
    9758              : 
    9759              : int
    9760            0 : Traverse::variable(Named_object*)
    9761              : {
    9762            0 :   go_unreachable();
    9763              : }
    9764              : 
    9765              : int
    9766            0 : Traverse::constant(Named_object*, bool)
    9767              : {
    9768            0 :   go_unreachable();
    9769              : }
    9770              : 
    9771              : int
    9772            0 : Traverse::function(Named_object*)
    9773              : {
    9774            0 :   go_unreachable();
    9775              : }
    9776              : 
    9777              : int
    9778            0 : Traverse::block(Block*)
    9779              : {
    9780            0 :   go_unreachable();
    9781              : }
    9782              : 
    9783              : int
    9784            0 : Traverse::statement(Block*, size_t*, Statement*)
    9785              : {
    9786            0 :   go_unreachable();
    9787              : }
    9788              : 
    9789              : int
    9790            0 : Traverse::expression(Expression**)
    9791              : {
    9792            0 :   go_unreachable();
    9793              : }
    9794              : 
    9795              : int
    9796            0 : Traverse::type(Type*)
    9797              : {
    9798            0 :   go_unreachable();
    9799              : }
    9800              : 
    9801              : int
    9802            0 : Traverse::function_declaration(Named_object*)
    9803              : {
    9804            0 :   go_unreachable();
    9805              : }
    9806              : 
    9807              : // Class Statement_inserter.
    9808              : 
    9809              : void
    9810      1159926 : Statement_inserter::insert(Statement* s)
    9811              : {
    9812      1159926 :   if (this->statements_added_ != NULL)
    9813       177892 :     this->statements_added_->insert(s);
    9814              : 
    9815      1159926 :   if (this->block_ != NULL)
    9816              :     {
    9817      1128108 :       go_assert(this->pindex_ != NULL);
    9818      1128108 :       this->block_->insert_statement_before(*this->pindex_, s);
    9819      1128108 :       ++*this->pindex_;
    9820              :     }
    9821        31818 :   else if (this->var_ != NULL)
    9822        31818 :     this->var_->add_preinit_statement(this->gogo_, s);
    9823              :   else
    9824            0 :     go_assert(saw_errors());
    9825      1159926 : }
        

Generated by: LCOV version 2.4-beta

LCOV profile is generated on x86_64 machine using following configure options: configure --disable-bootstrap --enable-coverage=opt --enable-languages=c,c++,fortran,go,jit,lto,rust,m2 --enable-host-shared. GCC test suite is run with the built compiler.