LCOV - code coverage report
Current view: top level - gcc/go/gofrontend - expressions.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 85.3 % 11157 9517
Test Date: 2024-04-13 14:00:49 Functions: 82.7 % 728 602
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: - 0 0

             Branch data     Line data    Source code
       1                 :             : // expressions.cc -- Go frontend expression handling.
       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 <algorithm>
      10                 :             : 
      11                 :             : #include "go-c.h"
      12                 :             : #include "gogo.h"
      13                 :             : #include "go-diagnostics.h"
      14                 :             : #include "go-encode-id.h"
      15                 :             : #include "types.h"
      16                 :             : #include "export.h"
      17                 :             : #include "import.h"
      18                 :             : #include "statements.h"
      19                 :             : #include "lex.h"
      20                 :             : #include "runtime.h"
      21                 :             : #include "backend.h"
      22                 :             : #include "expressions.h"
      23                 :             : #include "ast-dump.h"
      24                 :             : 
      25                 :             : // Class Expression.
      26                 :             : 
      27                 :    51506814 : Expression::Expression(Expression_classification classification,
      28                 :    51506814 :                        Location location)
      29                 :    51506814 :   : classification_(classification), location_(location)
      30                 :             : {
      31                 :    51506814 : }
      32                 :             : 
      33                 :      200637 : Expression::~Expression()
      34                 :             : {
      35                 :      200637 : }
      36                 :             : 
      37                 :             : // Traverse the expressions.
      38                 :             : 
      39                 :             : int
      40                 :   236647316 : Expression::traverse(Expression** pexpr, Traverse* traverse)
      41                 :             : {
      42                 :   236647316 :   Expression* expr = *pexpr;
      43                 :   236647316 :   if ((traverse->traverse_mask() & Traverse::traverse_expressions) != 0)
      44                 :             :     {
      45                 :   178092232 :       int t = traverse->expression(pexpr);
      46                 :   178092232 :       if (t == TRAVERSE_EXIT)
      47                 :             :         return TRAVERSE_EXIT;
      48                 :   176359929 :       else if (t == TRAVERSE_SKIP_COMPONENTS)
      49                 :             :         return TRAVERSE_CONTINUE;
      50                 :             :     }
      51                 :   165358909 :   return expr->do_traverse(traverse);
      52                 :             : }
      53                 :             : 
      54                 :             : // Traverse subexpressions of this expression.
      55                 :             : 
      56                 :             : int
      57                 :    71854486 : Expression::traverse_subexpressions(Traverse* traverse)
      58                 :             : {
      59                 :    71854486 :   return this->do_traverse(traverse);
      60                 :             : }
      61                 :             : 
      62                 :             : // A traversal used to set the location of subexpressions.
      63                 :             : 
      64                 :        9085 : class Set_location : public Traverse
      65                 :             : {
      66                 :             :  public:
      67                 :        9085 :   Set_location(Location loc)
      68                 :        9085 :     : Traverse(traverse_expressions),
      69                 :        9085 :       loc_(loc)
      70                 :             :   { }
      71                 :             : 
      72                 :             :   int
      73                 :             :   expression(Expression** pexpr);
      74                 :             : 
      75                 :             :  private:
      76                 :             :   Location loc_;
      77                 :             : };
      78                 :             : 
      79                 :             : // Set the location of an expression.
      80                 :             : 
      81                 :             : int
      82                 :        5614 : Set_location::expression(Expression** pexpr)
      83                 :             : {
      84                 :             :   // Some expressions are shared or don't have an independent
      85                 :             :   // location, so we shouldn't change their location.  This is the set
      86                 :             :   // of expressions for which do_copy is just "return this" or
      87                 :             :   // otherwise does not pass down the location.
      88                 :        5614 :   switch ((*pexpr)->classification())
      89                 :             :     {
      90                 :             :     case Expression::EXPRESSION_ERROR:
      91                 :             :     case Expression::EXPRESSION_VAR_REFERENCE:
      92                 :             :     case Expression::EXPRESSION_ENCLOSED_VAR_REFERENCE:
      93                 :             :     case Expression::EXPRESSION_STRING:
      94                 :             :     case Expression::EXPRESSION_FUNC_DESCRIPTOR:
      95                 :             :     case Expression::EXPRESSION_TYPE:
      96                 :             :     case Expression::EXPRESSION_BOOLEAN:
      97                 :             :     case Expression::EXPRESSION_CONST_REFERENCE:
      98                 :             :     case Expression::EXPRESSION_NIL:
      99                 :             :     case Expression::EXPRESSION_TYPE_DESCRIPTOR:
     100                 :             :     case Expression::EXPRESSION_GC_SYMBOL:
     101                 :             :     case Expression::EXPRESSION_PTRMASK_SYMBOL:
     102                 :             :     case Expression::EXPRESSION_TYPE_INFO:
     103                 :             :     case Expression::EXPRESSION_STRUCT_FIELD_OFFSET:
     104                 :             :       return TRAVERSE_CONTINUE;
     105                 :        5607 :     default:
     106                 :        5607 :       break;
     107                 :             :     }
     108                 :             : 
     109                 :        5607 :   (*pexpr)->location_ = this->loc_;
     110                 :        5607 :   return TRAVERSE_CONTINUE;
     111                 :             : }
     112                 :             : 
     113                 :             : // Set the location of an expression and its subexpressions.
     114                 :             : 
     115                 :             : void
     116                 :        9085 : Expression::set_location(Location loc)
     117                 :             : {
     118                 :        9085 :   this->location_ = loc;
     119                 :        9085 :   Set_location sl(loc);
     120                 :        9085 :   this->traverse_subexpressions(&sl);
     121                 :        9085 : }
     122                 :             : 
     123                 :             : // Default implementation for do_traverse for child classes.
     124                 :             : 
     125                 :             : int
     126                 :    69446510 : Expression::do_traverse(Traverse*)
     127                 :             : {
     128                 :    69446510 :   return TRAVERSE_CONTINUE;
     129                 :             : }
     130                 :             : 
     131                 :             : // This virtual function is called by the parser if the value of this
     132                 :             : // expression is being discarded.  By default, we give an error.
     133                 :             : // Expressions with side effects override.
     134                 :             : 
     135                 :             : bool
     136                 :          18 : Expression::do_discarding_value()
     137                 :             : {
     138                 :          18 :   this->unused_value_error();
     139                 :          18 :   return false;
     140                 :             : }
     141                 :             : 
     142                 :             : // This virtual function is called to export expressions.  This will
     143                 :             : // only be used by expressions which may be constant.
     144                 :             : 
     145                 :             : void
     146                 :           0 : Expression::do_export(Export_function_body*) const
     147                 :             : {
     148                 :           0 :   go_unreachable();
     149                 :             : }
     150                 :             : 
     151                 :             : // Write a name to the export data.
     152                 :             : 
     153                 :             : void
     154                 :       13427 : Expression::export_name(Export_function_body* efb, const Named_object* no)
     155                 :             : {
     156                 :       13427 :   if (no->package() != NULL)
     157                 :             :     {
     158                 :        4700 :       char buf[50];
     159                 :        4700 :       snprintf(buf, sizeof buf, "<p%d>", efb->package_index(no->package()));
     160                 :        4700 :       efb->write_c_string(buf);
     161                 :             :     }
     162                 :             : 
     163                 :       13427 :   if (!Gogo::is_hidden_name(no->name()))
     164                 :        5679 :     efb->write_string(no->name());
     165                 :             :   else
     166                 :             :     {
     167                 :        7748 :       efb->write_c_string(".");
     168                 :        7748 :       efb->write_string(Gogo::unpack_hidden_name(no->name()));
     169                 :             :     }
     170                 :       13427 : }
     171                 :             : 
     172                 :             : // Give an error saying that the value of the expression is not used.
     173                 :             : 
     174                 :             : void
     175                 :          95 : Expression::unused_value_error()
     176                 :             : {
     177                 :          95 :   if (this->type()->is_error())
     178                 :             :     {
     179                 :          13 :       go_assert(saw_errors());
     180                 :          13 :       this->set_is_error();
     181                 :             :     }
     182                 :             :   else
     183                 :          82 :     this->report_error(_("value computed is not used"));
     184                 :          95 : }
     185                 :             : 
     186                 :             : // Note that this expression is an error.  This is called by children
     187                 :             : // when they discover an error.
     188                 :             : 
     189                 :             : void
     190                 :        5305 : Expression::set_is_error()
     191                 :             : {
     192                 :        5305 :   this->classification_ = EXPRESSION_ERROR;
     193                 :        5305 : }
     194                 :             : 
     195                 :             : // For children to call to report an error conveniently.
     196                 :             : 
     197                 :             : void
     198                 :        1291 : Expression::report_error(const char* msg)
     199                 :             : {
     200                 :        1291 :   go_error_at(this->location_, "%s", msg);
     201                 :        1291 :   this->set_is_error();
     202                 :        1291 : }
     203                 :             : 
     204                 :             : // A convenience function for handling a type in do_is_untyped.  If
     205                 :             : // TYPE is not abstract, return false.  Otherwise set *PTYPE to TYPE
     206                 :             : // and return true.
     207                 :             : 
     208                 :             : bool
     209                 :     1389067 : Expression::is_untyped_type(Type* type, Type** ptype)
     210                 :             : {
     211                 :     1389067 :   if (!type->is_abstract())
     212                 :             :     return false;
     213                 :       53297 :   *ptype = type;
     214                 :       53297 :   return true;
     215                 :             : }
     216                 :             : 
     217                 :             : // Report whether this is a type expression.
     218                 :             : 
     219                 :             : bool
     220                 :    26632041 : Expression::is_type_expression() const
     221                 :             : {
     222                 :    26632041 :   if (this->classification_ == EXPRESSION_TYPE)
     223                 :             :     return true;
     224                 :    26442389 :   if (this->unknown_expression() != NULL)
     225                 :             :     {
     226                 :      833605 :       Named_object* no = this->unknown_expression()->named_object();
     227                 :      833605 :       if (no->is_unknown())
     228                 :             :         {
     229                 :      634280 :           no = no->unknown_value()->real_named_object();
     230                 :      634280 :           if (no == NULL)
     231                 :             :             return false;
     232                 :             :         }
     233                 :      250021 :       return no->is_type();
     234                 :             :     }
     235                 :    25608784 :   if (this->unary_expression() != NULL
     236                 :      149929 :       && this->unary_expression()->op() == OPERATOR_MULT
     237                 :       35160 :       && this->unary_expression()->operand()->is_type_expression())
     238                 :             :     return true;
     239                 :             :   return false;
     240                 :             : }
     241                 :             : 
     242                 :             : // Set types of variables and constants.  This is implemented by the
     243                 :             : // child class.
     244                 :             : 
     245                 :             : void
     246                 :    38397501 : Expression::determine_type(Gogo* gogo, const Type_context* context)
     247                 :             : {
     248                 :    38397501 :   this->do_determine_type(gogo, context);
     249                 :    38397501 : }
     250                 :             : 
     251                 :             : // Set types when there is no context.
     252                 :             : 
     253                 :             : void
     254                 :    14366022 : Expression::determine_type_no_context(Gogo* gogo)
     255                 :             : {
     256                 :    14366022 :   Type_context context;
     257                 :    14366022 :   this->do_determine_type(gogo, &context);
     258                 :    14366022 : }
     259                 :             : 
     260                 :             : // Return true if two expressions refer to the same variable or struct
     261                 :             : // field.  This can only be true when there are no side effects.
     262                 :             : 
     263                 :             : bool
     264                 :       47768 : Expression::is_same_variable(Expression* a, Expression* b)
     265                 :             : {
     266                 :       47768 :   if (a->classification() != b->classification())
     267                 :             :     return false;
     268                 :             : 
     269                 :       43769 :   Var_expression* av = a->var_expression();
     270                 :       43769 :   if (av != NULL)
     271                 :       30528 :     return av->named_object() == b->var_expression()->named_object();
     272                 :             : 
     273                 :       13241 :   Field_reference_expression* af = a->field_reference_expression();
     274                 :       13241 :   if (af != NULL)
     275                 :             :     {
     276                 :        6441 :       Field_reference_expression* bf = b->field_reference_expression();
     277                 :        6441 :       return (af->field_index() == bf->field_index()
     278                 :        6441 :               && Expression::is_same_variable(af->expr(), bf->expr()));
     279                 :             :     }
     280                 :             : 
     281                 :        6800 :   Unary_expression* au = a->unary_expression();
     282                 :        6800 :   if (au != NULL)
     283                 :             :     {
     284                 :        6083 :       Unary_expression* bu = b->unary_expression();
     285                 :        6083 :       return (au->op() == OPERATOR_MULT
     286                 :        6083 :               && bu->op() == OPERATOR_MULT
     287                 :       12166 :               && Expression::is_same_variable(au->operand(),
     288                 :             :                                               bu->operand()));
     289                 :             :     }
     290                 :             : 
     291                 :         717 :   Array_index_expression* aie = a->array_index_expression();
     292                 :         717 :   if (aie != NULL)
     293                 :             :     {
     294                 :          86 :       Array_index_expression* bie = b->array_index_expression();
     295                 :          86 :       return (aie->end() == NULL
     296                 :          86 :               && bie->end() == NULL
     297                 :          86 :               && Expression::is_same_variable(aie->array(), bie->array())
     298                 :         172 :               && Expression::is_same_variable(aie->start(), bie->start()));
     299                 :             :     }
     300                 :             : 
     301                 :         631 :   Numeric_constant aval;
     302                 :         631 :   if (a->numeric_constant_value(&aval))
     303                 :             :     {
     304                 :          28 :       Numeric_constant bval;
     305                 :          28 :       if (b->numeric_constant_value(&bval))
     306                 :          28 :         return aval.equals(bval);
     307                 :          28 :     }
     308                 :             : 
     309                 :             :   return false;
     310                 :         631 : }
     311                 :             : 
     312                 :             : // Return an expression handling any conversions which must be done during
     313                 :             : // assignment.
     314                 :             : 
     315                 :             : Expression*
     316                 :    12775507 : Expression::convert_for_assignment(Gogo* gogo, Type* lhs_type,
     317                 :             :                                    Expression* rhs, Location location)
     318                 :             : {
     319                 :    12775507 :   Type* rhs_type = rhs->type();
     320                 :    12775507 :   if (lhs_type->is_error()
     321                 :    12775502 :       || rhs_type->is_error()
     322                 :    25551002 :       || rhs->is_error_expression())
     323                 :          13 :     return Expression::make_error(location);
     324                 :             : 
     325                 :    12775494 :   bool are_identical = Type::are_identical(lhs_type, rhs_type,
     326                 :             :                                            (Type::COMPARE_ERRORS
     327                 :             :                                             | Type::COMPARE_TAGS),
     328                 :             :                                            NULL);
     329                 :    12775494 :   Expression* ret;
     330                 :    12775494 :   if (!are_identical && lhs_type->interface_type() != NULL)
     331                 :             :     {
     332                 :             :       // Type to interface conversions have been made explicit early.
     333                 :       26024 :       go_assert(rhs_type->interface_type() != NULL);
     334                 :       26024 :       ret = Expression::convert_interface_to_interface(gogo, lhs_type, rhs,
     335                 :             :                                                        false, location);
     336                 :             :     }
     337                 :    12749470 :   else if (!are_identical && rhs_type->interface_type() != NULL)
     338                 :       12525 :     ret = Expression::convert_interface_to_type(gogo, lhs_type, rhs, location);
     339                 :    12736945 :   else if (lhs_type->is_slice_type() && rhs_type->is_nil_type())
     340                 :             :     {
     341                 :             :       // Assigning nil to a slice.
     342                 :       19166 :       Expression* nil = Expression::make_nil(location);
     343                 :       19166 :       Expression* zero = Expression::make_integer_ul(0, NULL, location);
     344                 :       19166 :       ret = Expression::make_slice_value(lhs_type, nil, zero, zero, location);
     345                 :             :     }
     346                 :    12717779 :   else if (rhs_type->is_nil_type())
     347                 :      851619 :     ret = Expression::make_nil(location);
     348                 :    11866160 :   else if (are_identical)
     349                 :             :     {
     350                 :    11663783 :       if (lhs_type->forwarded() != rhs_type->forwarded())
     351                 :             :         {
     352                 :             :           // Different but identical types require an explicit
     353                 :             :           // conversion.  This happens with type aliases.
     354                 :      473959 :           return Expression::make_cast(lhs_type, rhs, location);
     355                 :             :         }
     356                 :             : 
     357                 :             :       // No conversion is needed.
     358                 :             :       return rhs;
     359                 :             :     }
     360                 :      202377 :   else if (lhs_type->points_to() != NULL)
     361                 :       13359 :     ret = Expression::make_unsafe_cast(lhs_type, rhs, location);
     362                 :      189018 :   else if (lhs_type->is_numeric_type())
     363                 :      143858 :     ret = Expression::make_cast(lhs_type, rhs, location);
     364                 :       45196 :   else if ((lhs_type->struct_type() != NULL
     365                 :       45124 :             && rhs_type->struct_type() != NULL)
     366                 :        3434 :            || (lhs_type->array_type() != NULL
     367                 :        3470 :                && rhs_type->array_type() != NULL))
     368                 :             :     {
     369                 :             :       // This conversion must be permitted by Go, or we wouldn't have
     370                 :             :       // gotten here.
     371                 :        3470 :       ret = Expression::make_unsafe_cast(lhs_type, rhs, location);
     372                 :             :     }
     373                 :             :   else
     374                 :       41690 :     return rhs;
     375                 :             : 
     376                 :     1070021 :   Type_context context(lhs_type, false);
     377                 :     1070021 :   ret->determine_type(gogo, &context);
     378                 :     1070021 :   return ret;
     379                 :             : }
     380                 :             : 
     381                 :             : // Return an expression for a conversion from a non-interface type to an
     382                 :             : // interface type.  If ON_STACK is true, it can allocate the storage on
     383                 :             : // stack.
     384                 :             : 
     385                 :             : Expression*
     386                 :      201200 : Expression::convert_type_to_interface(Type* lhs_type, Expression* rhs,
     387                 :             :                                       bool on_stack, Location location)
     388                 :             : {
     389                 :      201200 :   Interface_type* lhs_interface_type = lhs_type->interface_type();
     390                 :      201200 :   bool lhs_is_empty = lhs_interface_type->is_empty();
     391                 :             : 
     392                 :             :   // Since RHS_TYPE is a static type, we can create the interface
     393                 :             :   // method table at compile time.
     394                 :             : 
     395                 :             :   // When setting an interface to nil, we just set both fields to
     396                 :             :   // NULL.
     397                 :      201200 :   Type* rhs_type = rhs->type();
     398                 :      201200 :   if (rhs_type->is_nil_type())
     399                 :             :     {
     400                 :       28459 :       Expression* nil = Expression::make_nil(location);
     401                 :       28459 :       return Expression::make_interface_value(lhs_type, nil, nil, location);
     402                 :             :     }
     403                 :             : 
     404                 :             :   // This should have been checked already.
     405                 :      172741 :   if (!lhs_interface_type->implements_interface(rhs_type, NULL))
     406                 :             :     {
     407                 :           0 :       go_assert(saw_errors());
     408                 :           0 :       return Expression::make_error(location);
     409                 :             :     }
     410                 :             : 
     411                 :             :   // An interface is a tuple.  If LHS_TYPE is an empty interface type,
     412                 :             :   // then the first field is the type descriptor for RHS_TYPE.
     413                 :             :   // Otherwise it is the interface method table for RHS_TYPE.
     414                 :      172741 :   Expression* first_field;
     415                 :      172741 :   if (lhs_is_empty)
     416                 :      129332 :     first_field = Expression::make_type_descriptor(rhs_type, location);
     417                 :             :   else
     418                 :             :     {
     419                 :             :       // Build the interface method table for this interface and this
     420                 :             :       // object type: a list of function pointers for each interface
     421                 :             :       // method.
     422                 :       43409 :       Named_type* rhs_named_type = rhs_type->named_type();
     423                 :       43409 :       Struct_type* rhs_struct_type = rhs_type->struct_type();
     424                 :       43409 :       bool is_pointer = false;
     425                 :       43409 :       if (rhs_named_type == NULL && rhs_struct_type == NULL)
     426                 :             :         {
     427                 :       59638 :           rhs_named_type = rhs_type->deref()->named_type();
     428                 :       59638 :           rhs_struct_type = rhs_type->deref()->struct_type();
     429                 :             :           is_pointer = true;
     430                 :             :         }
     431                 :       43409 :       if (rhs_named_type != NULL)
     432                 :       43285 :         first_field =
     433                 :       43285 :           rhs_named_type->interface_method_table(lhs_interface_type,
     434                 :             :                                                  is_pointer);
     435                 :         124 :       else if (rhs_struct_type != NULL)
     436                 :         124 :         first_field =
     437                 :         124 :           rhs_struct_type->interface_method_table(lhs_interface_type,
     438                 :             :                                                   is_pointer);
     439                 :             :       else
     440                 :           0 :         first_field = Expression::make_nil(location);
     441                 :             :     }
     442                 :             : 
     443                 :      172741 :   Expression* obj;
     444                 :      172741 :   if (rhs_type->is_direct_iface_type())
     445                 :             :     {
     446                 :             :       // We are assigning a pointer to the interface; the interface
     447                 :             :       // holds the pointer itself.
     448                 :       47262 :       obj = unpack_direct_iface(rhs, location);
     449                 :             :     }
     450                 :             :   else
     451                 :             :     {
     452                 :             :       // We are assigning a non-pointer value to the interface; the
     453                 :             :       // interface gets a copy of the value in the heap if it escapes.
     454                 :             : 
     455                 :             :       // An exception is &global if global is notinheap, which is a
     456                 :             :       // pointer value but not a direct-iface type and we can't simply
     457                 :             :       // take its address.
     458                 :      125479 :       bool is_address = (rhs->unary_expression() != NULL
     459                 :         735 :                          && rhs->unary_expression()->op() == OPERATOR_AND);
     460                 :             : 
     461                 :      125479 :       if (rhs->is_constant() && !is_address)
     462                 :       31942 :         obj = Expression::make_unary(OPERATOR_AND, rhs, location);
     463                 :             :       else
     464                 :             :         {
     465                 :       93537 :           obj = Expression::make_heap_expression(rhs, location);
     466                 :       93537 :           if (on_stack)
     467                 :         597 :             obj->heap_expression()->set_allocate_on_stack();
     468                 :             :         }
     469                 :             :     }
     470                 :             : 
     471                 :      172741 :   return Expression::make_interface_value(lhs_type, first_field, obj, location);
     472                 :             : }
     473                 :             : 
     474                 :             : // Return an expression for the pointer-typed value of a direct interface
     475                 :             : // type.  Specifically, for single field struct or array, get the single
     476                 :             : // field, and do this recursively.  The reason for this is that we don't
     477                 :             : // want to assign a struct or an array to a pointer-typed field.  The
     478                 :             : // backend may not like that.
     479                 :             : 
     480                 :             : Expression*
     481                 :       57906 : Expression::unpack_direct_iface(Expression* rhs, Location loc)
     482                 :             : {
     483                 :       62190 :   Struct_type* st = rhs->type()->struct_type();
     484                 :       62190 :   if (st != NULL)
     485                 :             :     {
     486                 :        4284 :       go_assert(st->field_count() == 1);
     487                 :        4284 :       Expression* field = Expression::make_field_reference(rhs, 0, loc);
     488                 :        4284 :       return unpack_direct_iface(field, loc);
     489                 :             :     }
     490                 :       57906 :   Array_type* at = rhs->type()->array_type();
     491                 :         814 :   if (at != NULL)
     492                 :             :     {
     493                 :         814 :       int64_t len;
     494                 :         814 :       bool ok = at->int_length(&len);
     495                 :         814 :       go_assert(ok && len == 1);
     496                 :         814 :       Type* int_type = Type::lookup_integer_type("int");
     497                 :         814 :       Expression* index = Expression::make_integer_ul(0, int_type, loc);
     498                 :         814 :       Expression* elem = Expression::make_array_index(rhs, index, NULL, NULL, loc);
     499                 :         814 :       return unpack_direct_iface(elem, loc);
     500                 :             :     }
     501                 :             :   return rhs;
     502                 :             : }
     503                 :             : 
     504                 :             : // The opposite of unpack_direct_iface.
     505                 :             : 
     506                 :             : Expression*
     507                 :       13513 : Expression::pack_direct_iface(Type* t, Expression* rhs, Location loc)
     508                 :             : {
     509                 :       13513 :   if (rhs->type() == t)
     510                 :             :     return rhs;
     511                 :       13513 :   Struct_type* st = t->struct_type();
     512                 :       13513 :   if (st != NULL)
     513                 :             :     {
     514                 :        1665 :       Expression_list* vals = new Expression_list();
     515                 :        1665 :       vals->push_back(pack_direct_iface(st->field(0)->type(), rhs, loc));
     516                 :        1665 :       return Expression::make_struct_composite_literal(t, vals, loc);
     517                 :             :     }
     518                 :       11848 :   Array_type* at = t->array_type();
     519                 :       11848 :   if (at != NULL)
     520                 :             :     {
     521                 :           4 :       Expression_list* vals = new Expression_list();
     522                 :           4 :       vals->push_back(pack_direct_iface(at->element_type(), rhs, loc));
     523                 :           4 :       return Expression::make_array_composite_literal(t, vals, loc);
     524                 :             :     }
     525                 :       11844 :   return Expression::make_unsafe_cast(t, rhs, loc);
     526                 :             : }
     527                 :             : 
     528                 :             : // Return an expression for the type descriptor of RHS.
     529                 :             : 
     530                 :             : Expression*
     531                 :       54471 : Expression::get_interface_type_descriptor(Expression* rhs)
     532                 :             : {
     533                 :       54471 :   go_assert(rhs->type()->interface_type() != NULL);
     534                 :       54471 :   Location location = rhs->location();
     535                 :             : 
     536                 :             :   // The type descriptor is the first field of an empty interface.
     537                 :      108942 :   if (rhs->type()->interface_type()->is_empty())
     538                 :        8989 :     return Expression::make_interface_info(rhs, INTERFACE_INFO_TYPE_DESCRIPTOR,
     539                 :        8989 :                                            location);
     540                 :             : 
     541                 :       45482 :   Expression* mtable =
     542                 :       45482 :       Expression::make_interface_info(rhs, INTERFACE_INFO_METHODS, location);
     543                 :             : 
     544                 :       45482 :   Expression* descriptor =
     545                 :       45482 :       Expression::make_dereference(mtable, NIL_CHECK_NOT_NEEDED, location);
     546                 :       45482 :   descriptor = Expression::make_field_reference(descriptor, 0, location);
     547                 :       45482 :   Expression* nil = Expression::make_nil(location);
     548                 :             : 
     549                 :       45482 :   Expression* eq =
     550                 :       45482 :       Expression::make_binary(OPERATOR_EQEQ, mtable, nil, location);
     551                 :       45482 :   return Expression::make_conditional(eq, nil, descriptor, location);
     552                 :             : }
     553                 :             : 
     554                 :             : // Return an expression for the conversion of an interface type to an
     555                 :             : // interface type.
     556                 :             : 
     557                 :             : Expression*
     558                 :       27040 : Expression::convert_interface_to_interface(Gogo* gogo, Type *lhs_type,
     559                 :             :                                            Expression* rhs,
     560                 :             :                                            bool for_type_guard,
     561                 :             :                                            Location location)
     562                 :             : {
     563                 :       27040 :   if (Type::are_identical(lhs_type, rhs->type(),
     564                 :             :                           Type::COMPARE_ERRORS | Type::COMPARE_TAGS,
     565                 :             :                           NULL))
     566                 :             :     return rhs;
     567                 :             : 
     568                 :       27035 :   Interface_type* lhs_interface_type = lhs_type->interface_type();
     569                 :       27035 :   bool lhs_is_empty = lhs_interface_type->is_empty();
     570                 :             : 
     571                 :             :   // In the general case this requires runtime examination of the type
     572                 :             :   // method table to match it up with the interface methods.
     573                 :             : 
     574                 :             :   // FIXME: If all of the methods in the right hand side interface
     575                 :             :   // also appear in the left hand side interface, then we don't need
     576                 :             :   // to do a runtime check, although we still need to build a new
     577                 :             :   // method table.
     578                 :             : 
     579                 :             :   // We are going to evaluate RHS multiple times.
     580                 :       27035 :   go_assert(rhs->is_multi_eval_safe());
     581                 :             : 
     582                 :             :   // Get the type descriptor for the right hand side.  This will be
     583                 :             :   // NULL for a nil interface.
     584                 :       27035 :   Expression* rhs_type_expr = Expression::get_interface_type_descriptor(rhs);
     585                 :       27035 :   Expression* lhs_type_expr =
     586                 :       27035 :       Expression::make_type_descriptor(lhs_type, location);
     587                 :             : 
     588                 :       27035 :   Expression* first_field;
     589                 :       27035 :   if (for_type_guard)
     590                 :             :     {
     591                 :             :       // A type assertion fails when converting a nil interface.
     592                 :        1011 :       first_field = Runtime::make_call(gogo, Runtime::ASSERTITAB, location, 2,
     593                 :             :                                        lhs_type_expr, rhs_type_expr);
     594                 :             :     }
     595                 :       26024 :   else if (lhs_is_empty)
     596                 :             :     {
     597                 :             :       // A conversion to an empty interface always succeeds, and the
     598                 :             :       // first field is just the type descriptor of the object.
     599                 :             :       first_field = rhs_type_expr;
     600                 :             :     }
     601                 :             :   else
     602                 :             :     {
     603                 :             :       // A conversion to a non-empty interface may fail, but unlike a
     604                 :             :       // type assertion converting nil will always succeed.
     605                 :        4304 :       first_field = Runtime::make_call(gogo, Runtime::REQUIREITAB, location, 2,
     606                 :             :                                        lhs_type_expr, rhs_type_expr);
     607                 :             :     }
     608                 :             : 
     609                 :             :   // The second field is simply the object pointer.
     610                 :       27035 :   Expression* obj =
     611                 :       27035 :       Expression::make_interface_info(rhs, INTERFACE_INFO_OBJECT, location);
     612                 :       27035 :   Expression* ret = Expression::make_interface_value(lhs_type, first_field,
     613                 :             :                                                      obj, location);
     614                 :       27035 :   Type_context context(lhs_type, false);
     615                 :       27035 :   ret->determine_type(gogo, &context);
     616                 :       27035 :   return ret;
     617                 :             : }
     618                 :             : 
     619                 :             : // Return an expression for the conversion of an interface type to a
     620                 :             : // non-interface type.
     621                 :             : 
     622                 :             : Expression*
     623                 :       12525 : Expression::convert_interface_to_type(Gogo* gogo, Type *lhs_type, Expression* rhs,
     624                 :             :                                       Location location)
     625                 :             : {
     626                 :             :   // We are going to evaluate RHS multiple times.
     627                 :       12525 :   go_assert(rhs->is_multi_eval_safe());
     628                 :             : 
     629                 :             :   // Build an expression to check that the type is valid.  It will
     630                 :             :   // panic with an appropriate runtime type error if the type is not
     631                 :             :   // valid.
     632                 :             :   // (lhs_type == rhs_type ? nil /*dummy*/ :
     633                 :             :   //    panicdottype(lhs_type, rhs_type, inter_type))
     634                 :             :   // For some Oses, we need to call runtime.eqtype instead of
     635                 :             :   // lhs_type == rhs_type, as we may have unmerged type descriptors
     636                 :             :   // from shared libraries.
     637                 :       12525 :   Expression* lhs_type_expr = Expression::make_type_descriptor(lhs_type,
     638                 :             :                                                                 location);
     639                 :       12525 :   Expression* rhs_descriptor =
     640                 :       12525 :       Expression::get_interface_type_descriptor(rhs);
     641                 :             : 
     642                 :       12525 :   Type* rhs_type = rhs->type();
     643                 :       12525 :   Expression* rhs_inter_expr = Expression::make_type_descriptor(rhs_type,
     644                 :             :                                                                 location);
     645                 :             : 
     646                 :       12525 :   Expression* cond;
     647                 :       12525 :   if (gogo->need_eqtype()) {
     648                 :           0 :     cond = Runtime::make_call(gogo, Runtime::EQTYPE, location,
     649                 :             :                               2, lhs_type_expr,
     650                 :             :                               rhs_descriptor);
     651                 :             :   } else {
     652                 :       12525 :     cond = Expression::make_binary(OPERATOR_EQEQ, lhs_type_expr,
     653                 :             :                                    rhs_descriptor, location);
     654                 :             :   }
     655                 :             : 
     656                 :       12525 :   rhs_descriptor = Expression::get_interface_type_descriptor(rhs);
     657                 :       12525 :   Expression* panic = Runtime::make_call(gogo, Runtime::PANICDOTTYPE, location,
     658                 :             :                                          3, lhs_type_expr->copy(),
     659                 :             :                                          rhs_descriptor,
     660                 :             :                                          rhs_inter_expr);
     661                 :       12525 :   Expression* nil = Expression::make_nil(location);
     662                 :       12525 :   Expression* check = Expression::make_conditional(cond, nil, panic,
     663                 :             :                                                    location);
     664                 :             : 
     665                 :             :   // If the conversion succeeds, pull out the value.
     666                 :       12525 :   Expression* obj = Expression::make_interface_info(rhs, INTERFACE_INFO_OBJECT,
     667                 :             :                                                     location);
     668                 :             : 
     669                 :             :   // If the value is a direct interface, then it is the value we want.
     670                 :             :   // Otherwise it points to the value.
     671                 :       12525 :   if (lhs_type->is_direct_iface_type())
     672                 :        9828 :     obj = Expression::pack_direct_iface(lhs_type, obj, location);
     673                 :             :   else
     674                 :             :     {
     675                 :        2697 :       obj = Expression::make_unsafe_cast(Type::make_pointer_type(lhs_type), obj,
     676                 :             :                                          location);
     677                 :        2697 :       obj = Expression::make_dereference(obj, NIL_CHECK_NOT_NEEDED,
     678                 :             :                                          location);
     679                 :             :     }
     680                 :       12525 :   return Expression::make_compound(check, obj, location);
     681                 :             : }
     682                 :             : 
     683                 :             : // Convert an expression to its backend representation.  This is implemented by
     684                 :             : // the child class.  Not that it is not in general safe to call this multiple
     685                 :             : // times for a single expression, but that we don't catch such errors.
     686                 :             : 
     687                 :             : Bexpression*
     688                 :    40324950 : Expression::get_backend(Translate_context* context)
     689                 :             : {
     690                 :             :   // The child may have marked this expression as having an error.
     691                 :    40324950 :   if (this->classification_ == EXPRESSION_ERROR)
     692                 :             :     {
     693                 :        3629 :       go_assert(saw_errors());
     694                 :        3629 :       return context->backend()->error_expression();
     695                 :             :     }
     696                 :             : 
     697                 :    40321321 :   return this->do_get_backend(context);
     698                 :             : }
     699                 :             : 
     700                 :             : // Return a backend expression for VAL.
     701                 :             : Bexpression*
     702                 :     6048331 : Expression::backend_numeric_constant_expression(Translate_context* context,
     703                 :             :                                                 Numeric_constant* val)
     704                 :             : {
     705                 :     6048331 :   Gogo* gogo = context->gogo();
     706                 :     6048331 :   Type* type = val->type();
     707                 :     6048331 :   if (type == NULL)
     708                 :           0 :     return gogo->backend()->error_expression();
     709                 :             : 
     710                 :     6048331 :   Btype* btype = type->get_backend(gogo);
     711                 :     6048331 :   Bexpression* ret;
     712                 :     6048331 :   if (type->integer_type() != NULL)
     713                 :             :     {
     714                 :     6011963 :       mpz_t ival;
     715                 :     6011963 :       if (!val->to_int(&ival))
     716                 :             :         {
     717                 :           0 :           go_assert(saw_errors());
     718                 :           0 :           return gogo->backend()->error_expression();
     719                 :             :         }
     720                 :     6011963 :       ret = gogo->backend()->integer_constant_expression(btype, ival);
     721                 :     6011963 :       mpz_clear(ival);
     722                 :             :     }
     723                 :       36368 :   else if (type->float_type() != NULL)
     724                 :             :     {
     725                 :       33424 :       mpfr_t fval;
     726                 :       33424 :       if (!val->to_float(&fval))
     727                 :             :         {
     728                 :           0 :           go_assert(saw_errors());
     729                 :           0 :           return gogo->backend()->error_expression();
     730                 :             :         }
     731                 :       33424 :       ret = gogo->backend()->float_constant_expression(btype, fval);
     732                 :       33424 :       mpfr_clear(fval);
     733                 :             :     }
     734                 :        2944 :   else if (type->complex_type() != NULL)
     735                 :             :     {
     736                 :        2944 :       mpc_t cval;
     737                 :        2944 :       if (!val->to_complex(&cval))
     738                 :             :         {
     739                 :           0 :           go_assert(saw_errors());
     740                 :           0 :           return gogo->backend()->error_expression();
     741                 :             :         }
     742                 :        2944 :       ret = gogo->backend()->complex_constant_expression(btype, cval);
     743                 :        2944 :       mpc_clear(cval);
     744                 :             :     }
     745                 :             :   else
     746                 :           0 :     go_unreachable();
     747                 :             : 
     748                 :             :   return ret;
     749                 :             : }
     750                 :             : 
     751                 :             : // Insert bounds checks for an index expression.  Check that that VAL
     752                 :             : // >= 0 and that it fits in an int.  Then check that VAL OP BOUND is
     753                 :             : // true.  If any condition is false, call one of the CODE runtime
     754                 :             : // functions, which will panic.
     755                 :             : 
     756                 :             : void
     757                 :      224469 : Expression::check_bounds(Gogo* gogo, Expression* val, Operator op,
     758                 :             :                          Expression* bound,
     759                 :             :                          Runtime::Function code,
     760                 :             :                          Runtime::Function code_u,
     761                 :             :                          Runtime::Function code_extend,
     762                 :             :                          Runtime::Function code_extend_u,
     763                 :             :                          Statement_inserter* inserter,
     764                 :             :                          Location loc)
     765                 :             : {
     766                 :      224469 :   go_assert(val->is_multi_eval_safe());
     767                 :      224469 :   go_assert(bound->is_multi_eval_safe());
     768                 :             : 
     769                 :      224469 :   Type* int_type = Type::lookup_integer_type("int");
     770                 :      448938 :   int int_type_size = int_type->integer_type()->bits();
     771                 :             : 
     772                 :      224469 :   Type* val_type = val->type();
     773                 :      224469 :   if (val_type->integer_type() == NULL)
     774                 :             :     {
     775                 :           0 :       go_assert(saw_errors());
     776                 :             :       return;
     777                 :             :     }
     778                 :      448938 :   int val_type_size = val_type->integer_type()->bits();
     779                 :      448938 :   bool val_is_unsigned = val_type->integer_type()->is_unsigned();
     780                 :             : 
     781                 :             :   // Check that VAL >= 0.
     782                 :      224469 :   Expression* check = NULL;
     783                 :      224469 :   if (!val_is_unsigned)
     784                 :             :     {
     785                 :      213388 :       Expression* zero = Expression::make_integer_ul(0, val_type, loc);
     786                 :      213388 :       check = Expression::make_binary(OPERATOR_GE, val->copy(), zero, loc);
     787                 :             :     }
     788                 :             : 
     789                 :             :   // If VAL's type is larger than int, check that VAL fits in an int.
     790                 :      224469 :   if (val_type_size > int_type_size
     791                 :      223774 :       || (val_type_size == int_type_size
     792                 :      223774 :           && val_is_unsigned))
     793                 :             :     {
     794                 :        5511 :       mpz_t one;
     795                 :        5511 :       mpz_init_set_ui(one, 1UL);
     796                 :             : 
     797                 :             :       // maxval = 2^(int_type_size - 1) - 1
     798                 :        5511 :       mpz_t maxval;
     799                 :        5511 :       mpz_init(maxval);
     800                 :        5511 :       mpz_mul_2exp(maxval, one, int_type_size - 1);
     801                 :        5511 :       mpz_sub_ui(maxval, maxval, 1);
     802                 :        5511 :       Expression* max = Expression::make_integer_z(&maxval, val_type, loc);
     803                 :        5511 :       mpz_clear(one);
     804                 :        5511 :       mpz_clear(maxval);
     805                 :             : 
     806                 :        5511 :       Expression* cmp = Expression::make_binary(OPERATOR_LE, val->copy(),
     807                 :             :                                                 max, loc);
     808                 :        5511 :       if (check == NULL)
     809                 :             :         check = cmp;
     810                 :             :       else
     811                 :         311 :         check = Expression::make_binary(OPERATOR_ANDAND, check, cmp, loc);
     812                 :             :     }
     813                 :             : 
     814                 :             :   // For the final check we can assume that VAL fits in an int.
     815                 :      224469 :   Expression* ival;
     816                 :      224469 :   if (val_type == int_type)
     817                 :      188561 :     ival = val->copy();
     818                 :             :   else
     819                 :       35908 :     ival = Expression::make_cast(int_type, val->copy(), loc);
     820                 :             : 
     821                 :             :   // BOUND is assumed to fit in an int.  Either it comes from len or
     822                 :             :   // cap, or it was checked by an earlier call.
     823                 :      224469 :   Expression* ibound;
     824                 :      224469 :   if (bound->type() == int_type)
     825                 :      205354 :     ibound = bound->copy();
     826                 :             :   else
     827                 :       19115 :     ibound = Expression::make_cast(int_type, bound->copy(), loc);
     828                 :             : 
     829                 :      224469 :   Expression* cmp = Expression::make_binary(op, ival, ibound, loc);
     830                 :      224469 :   if (check == NULL)
     831                 :             :     check = cmp;
     832                 :             :   else
     833                 :      218588 :     check = Expression::make_binary(OPERATOR_ANDAND, check, cmp, loc);
     834                 :             : 
     835                 :      224469 :   Runtime::Function c;
     836                 :      224469 :   if (val_type_size > int_type_size)
     837                 :             :     {
     838                 :         695 :       if (val_is_unsigned)
     839                 :             :         c = code_extend_u;
     840                 :             :       else
     841                 :         311 :         c = code_extend;
     842                 :             :     }
     843                 :             :   else
     844                 :             :     {
     845                 :      223774 :       if (val_is_unsigned)
     846                 :             :         c = code_u;
     847                 :             :       else
     848                 :      213077 :         c = code;
     849                 :             :     }
     850                 :             : 
     851                 :      224469 :   Expression* ignore = Expression::make_boolean(true, loc);
     852                 :      224469 :   Expression* crash = Runtime::make_call(gogo, c, loc, 2,
     853                 :             :                                          val->copy(), bound->copy());
     854                 :      224469 :   Expression* cond = Expression::make_conditional(check, ignore, crash, loc);
     855                 :      224469 :   Statement* s = Statement::make_statement(cond, true);
     856                 :      224469 :   s->determine_types(gogo);
     857                 :      224469 :   inserter->insert(s);
     858                 :             : }
     859                 :             : 
     860                 :             : void
     861                 :           0 : Expression::dump_expression(Ast_dump_context* ast_dump_context) const
     862                 :             : {
     863                 :           0 :   this->do_dump_expression(ast_dump_context);
     864                 :           0 : }
     865                 :             : 
     866                 :             : // Error expressions.  This are used to avoid cascading errors.
     867                 :             : 
     868                 :             : class Error_expression : public Expression
     869                 :             : {
     870                 :             :  public:
     871                 :        9856 :   Error_expression(Location location)
     872                 :       19712 :     : Expression(EXPRESSION_ERROR, location)
     873                 :             :   { }
     874                 :             : 
     875                 :             :  protected:
     876                 :             :   bool
     877                 :          62 :   do_is_constant() const
     878                 :          62 :   { return true; }
     879                 :             : 
     880                 :             :   bool
     881                 :          25 :   do_is_untyped(Type**) const
     882                 :          25 :   { return false; }
     883                 :             : 
     884                 :             :   bool
     885                 :          30 :   do_numeric_constant_value(Numeric_constant*)
     886                 :          30 :   { return false; }
     887                 :             : 
     888                 :             :   bool
     889                 :           6 :   do_discarding_value()
     890                 :           6 :   { return true; }
     891                 :             : 
     892                 :             :   Type*
     893                 :       12537 :   do_type()
     894                 :       12537 :   { return Type::make_error_type(); }
     895                 :             : 
     896                 :             :   void
     897                 :         160 :   do_determine_type(Gogo*, const Type_context*)
     898                 :         160 :   { }
     899                 :             : 
     900                 :             :   Expression*
     901                 :           0 :   do_copy()
     902                 :           0 :   { return this; }
     903                 :             : 
     904                 :             :   bool
     905                 :           9 :   do_is_addressable() const
     906                 :           9 :   { return true; }
     907                 :             : 
     908                 :             :   Bexpression*
     909                 :           0 :   do_get_backend(Translate_context* context)
     910                 :           0 :   { return context->backend()->error_expression(); }
     911                 :             : 
     912                 :             :   void
     913                 :             :   do_dump_expression(Ast_dump_context*) const;
     914                 :             : };
     915                 :             : 
     916                 :             : // Dump the ast representation for an error expression to a dump context.
     917                 :             : 
     918                 :             : void
     919                 :           0 : Error_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
     920                 :             : {
     921                 :           0 :   ast_dump_context->ostream() << "_Error_" ;
     922                 :           0 : }
     923                 :             : 
     924                 :             : Expression*
     925                 :        9856 : Expression::make_error(Location location)
     926                 :             : {
     927                 :        9856 :   return new Error_expression(location);
     928                 :             : }
     929                 :             : 
     930                 :             : // An expression which is really a type.  This is used during parsing.
     931                 :             : // It is an error if these survive after lowering.
     932                 :             : 
     933                 :             : class Type_expression : public Expression
     934                 :             : {
     935                 :             :  public:
     936                 :      254309 :   Type_expression(Type* type, Location location)
     937                 :      254309 :     : Expression(EXPRESSION_TYPE, location),
     938                 :      508618 :       type_(type)
     939                 :             :   { }
     940                 :             : 
     941                 :             :  protected:
     942                 :             :   int
     943                 :      143439 :   do_traverse(Traverse* traverse)
     944                 :      143439 :   { return Type::traverse(this->type_, traverse); }
     945                 :             : 
     946                 :             :   Type*
     947                 :      476280 :   do_type()
     948                 :      476280 :   { return this->type_; }
     949                 :             : 
     950                 :             :   void
     951                 :       21109 :   do_determine_type(Gogo*, const Type_context*)
     952                 :       21109 :   { }
     953                 :             : 
     954                 :             :   void
     955                 :             :   do_check_types(Gogo*);
     956                 :             : 
     957                 :             :   Expression*
     958                 :           0 :   do_copy()
     959                 :           0 :   { return this; }
     960                 :             : 
     961                 :             :   Bexpression*
     962                 :             :   do_get_backend(Translate_context*);
     963                 :             : 
     964                 :             :   void do_dump_expression(Ast_dump_context*) const;
     965                 :             : 
     966                 :             :  private:
     967                 :             :   // The type which we are representing as an expression.
     968                 :             :   Type* type_;
     969                 :             : };
     970                 :             : 
     971                 :             : void
     972                 :       23066 : Type_expression::do_check_types(Gogo*)
     973                 :             : {
     974                 :       23066 :   if (this->type_->is_error())
     975                 :             :     {
     976                 :           2 :       go_assert(saw_errors());
     977                 :           2 :       this->set_is_error();
     978                 :             :     }
     979                 :       23066 : }
     980                 :             : 
     981                 :             : Bexpression*
     982                 :           2 : Type_expression::do_get_backend(Translate_context* context)
     983                 :             : {
     984                 :           2 :   if (!this->is_error_expression())
     985                 :           2 :     this->report_error("invalid use of type");
     986                 :           2 :   return context->backend()->error_expression();
     987                 :             : }
     988                 :             : 
     989                 :             : void
     990                 :           0 : Type_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
     991                 :             : {
     992                 :           0 :   ast_dump_context->dump_type(this->type_);
     993                 :           0 : }
     994                 :             : 
     995                 :             : Expression*
     996                 :      254309 : Expression::make_type(Type* type, Location location)
     997                 :             : {
     998                 :      254309 :   return new Type_expression(type, location);
     999                 :             : }
    1000                 :             : 
    1001                 :             : // Class Var_expression.
    1002                 :             : 
    1003                 :             : // Lower a variable expression.  Here we just make sure that the
    1004                 :             : // initialization expression of the variable has been lowered.  This
    1005                 :             : // ensures that we will be able to determine the type of the variable
    1006                 :             : // if necessary.
    1007                 :             : 
    1008                 :             : Expression*
    1009                 :     6401445 : Var_expression::do_lower(Gogo* gogo, Named_object* function,
    1010                 :             :                          Statement_inserter* inserter)
    1011                 :             : {
    1012                 :     6401445 :   if (this->variable_->is_variable())
    1013                 :             :     {
    1014                 :     5667209 :       Variable* var = this->variable_->var_value();
    1015                 :             :       // This is either a local variable or a global variable.  A
    1016                 :             :       // reference to a variable which is local to an enclosing
    1017                 :             :       // function will be a reference to a field in a closure.
    1018                 :     5667209 :       if (var->is_global())
    1019                 :             :         {
    1020                 :      446198 :           function = NULL;
    1021                 :      446198 :           inserter = NULL;
    1022                 :             :         }
    1023                 :     5667209 :       var->lower_init_expression(gogo, function, inserter);
    1024                 :             :     }
    1025                 :     6401445 :   return this;
    1026                 :             : }
    1027                 :             : 
    1028                 :             : // Return the type of a reference to a variable.
    1029                 :             : 
    1030                 :             : Type*
    1031                 :    51507985 : Var_expression::do_type()
    1032                 :             : {
    1033                 :    51507985 :   if (this->variable_->is_variable())
    1034                 :    45970621 :     return this->variable_->var_value()->type();
    1035                 :     5537364 :   else if (this->variable_->is_result_variable())
    1036                 :     5537364 :     return this->variable_->result_var_value()->type();
    1037                 :             :   else
    1038                 :           0 :     go_unreachable();
    1039                 :             : }
    1040                 :             : 
    1041                 :             : // Determine the type of a reference to a variable.
    1042                 :             : 
    1043                 :             : void
    1044                 :     6260556 : Var_expression::do_determine_type(Gogo* gogo, const Type_context*)
    1045                 :             : {
    1046                 :     6260556 :   if (this->variable_->is_variable())
    1047                 :     5608897 :     this->variable_->var_value()->determine_type(gogo);
    1048                 :     6260556 : }
    1049                 :             : 
    1050                 :             : // Something takes the address of this variable.  This means that we
    1051                 :             : // may want to move the variable onto the heap.
    1052                 :             : 
    1053                 :             : void
    1054                 :     1429439 : Var_expression::do_address_taken(bool escapes)
    1055                 :             : {
    1056                 :     1429439 :   if (!escapes)
    1057                 :             :     {
    1058                 :     1080289 :       if (this->variable_->is_variable())
    1059                 :     1077272 :         this->variable_->var_value()->set_non_escaping_address_taken();
    1060                 :        3017 :       else if (this->variable_->is_result_variable())
    1061                 :        3017 :         this->variable_->result_var_value()->set_non_escaping_address_taken();
    1062                 :             :       else
    1063                 :           0 :         go_unreachable();
    1064                 :             :     }
    1065                 :             :   else
    1066                 :             :     {
    1067                 :      349150 :       if (this->variable_->is_variable())
    1068                 :      348813 :         this->variable_->var_value()->set_address_taken();
    1069                 :         337 :       else if (this->variable_->is_result_variable())
    1070                 :         337 :         this->variable_->result_var_value()->set_address_taken();
    1071                 :             :       else
    1072                 :           0 :         go_unreachable();
    1073                 :             :     }
    1074                 :             : 
    1075                 :     1429439 :   if (this->variable_->is_variable()
    1076                 :     1429439 :       && this->variable_->var_value()->is_in_heap())
    1077                 :             :     {
    1078                 :     1176461 :       Node::make_node(this)->set_encoding(Node::ESCAPE_HEAP);
    1079                 :     1176461 :       Node::make_node(this->variable_)->set_encoding(Node::ESCAPE_HEAP);
    1080                 :             :     }
    1081                 :     1429439 : }
    1082                 :             : 
    1083                 :             : // Export a reference to a variable.
    1084                 :             : 
    1085                 :             : void
    1086                 :       54718 : Var_expression::do_export(Export_function_body* efb) const
    1087                 :             : {
    1088                 :       54718 :   Named_object* no = this->variable_;
    1089                 :       54718 :   if (no->is_result_variable() || !no->var_value()->is_global())
    1090                 :       52209 :     efb->write_string(Gogo::unpack_hidden_name(no->name()));
    1091                 :             :   else
    1092                 :        2509 :     Expression::export_name(efb, no);
    1093                 :       54718 : }
    1094                 :             : 
    1095                 :             : // Get the backend representation for a reference to a variable.
    1096                 :             : 
    1097                 :             : Bexpression*
    1098                 :     4053455 : Var_expression::do_get_backend(Translate_context* context)
    1099                 :             : {
    1100                 :     4053455 :   Bvariable* bvar = this->variable_->get_backend_variable(context->gogo(),
    1101                 :             :                                                           context->function());
    1102                 :     4053455 :   bool is_in_heap;
    1103                 :     4053455 :   Location loc = this->location();
    1104                 :     4053455 :   Btype* btype;
    1105                 :     4053455 :   Gogo* gogo = context->gogo();
    1106                 :     4053455 :   if (this->variable_->is_variable())
    1107                 :             :     {
    1108                 :     3143673 :       is_in_heap = this->variable_->var_value()->is_in_heap();
    1109                 :     3143673 :       btype = this->variable_->var_value()->type()->get_backend(gogo);
    1110                 :             :     }
    1111                 :      909782 :   else if (this->variable_->is_result_variable())
    1112                 :             :     {
    1113                 :      909782 :       is_in_heap = this->variable_->result_var_value()->is_in_heap();
    1114                 :      909782 :       btype = this->variable_->result_var_value()->type()->get_backend(gogo);
    1115                 :             :     }
    1116                 :             :   else
    1117                 :           0 :     go_unreachable();
    1118                 :             : 
    1119                 :     4053455 :   Bexpression* ret =
    1120                 :     4053455 :       context->backend()->var_expression(bvar, loc);
    1121                 :     4053455 :   if (is_in_heap)
    1122                 :       66935 :     ret = context->backend()->indirect_expression(btype, ret, true, loc);
    1123                 :     4053455 :   return ret;
    1124                 :             : }
    1125                 :             : 
    1126                 :             : // Ast dump for variable expression.
    1127                 :             : 
    1128                 :             : void
    1129                 :           0 : Var_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
    1130                 :             : {
    1131                 :           0 :   ast_dump_context->ostream() << this->variable_->message_name() ;
    1132                 :           0 : }
    1133                 :             : 
    1134                 :             : // Make a reference to a variable in an expression.
    1135                 :             : 
    1136                 :             : Expression*
    1137                 :     3520359 : Expression::make_var_reference(Named_object* var, Location location)
    1138                 :             : {
    1139                 :     3520359 :   if (var->is_sink())
    1140                 :       19221 :     return Expression::make_sink(location);
    1141                 :     3501138 :   if (var->is_redefinition())
    1142                 :           0 :     return Expression::make_error(location);
    1143                 :             : 
    1144                 :             :   // FIXME: Creating a new object for each reference to a variable is
    1145                 :             :   // wasteful.
    1146                 :     3501138 :   return new Var_expression(var, location);
    1147                 :             : }
    1148                 :             : 
    1149                 :             : // Class Enclosed_var_expression.
    1150                 :             : 
    1151                 :             : int
    1152                 :      822743 : Enclosed_var_expression::do_traverse(Traverse*)
    1153                 :             : {
    1154                 :      822743 :   return TRAVERSE_CONTINUE;
    1155                 :             : }
    1156                 :             : 
    1157                 :             : // Lower the reference to the enclosed variable.
    1158                 :             : 
    1159                 :             : Expression*
    1160                 :       92072 : Enclosed_var_expression::do_lower(Gogo* gogo, Named_object* function,
    1161                 :             :                                   Statement_inserter* inserter)
    1162                 :             : {
    1163                 :       92072 :   gogo->lower_expression(function, inserter, &this->reference_);
    1164                 :       92072 :   return this;
    1165                 :             : }
    1166                 :             : 
    1167                 :             : // Flatten the reference to the enclosed variable.
    1168                 :             : 
    1169                 :             : Expression*
    1170                 :       46839 : Enclosed_var_expression::do_flatten(Gogo* gogo, Named_object* function,
    1171                 :             :                                     Statement_inserter* inserter)
    1172                 :             : {
    1173                 :       46839 :   gogo->flatten_expression(function, inserter, &this->reference_);
    1174                 :       46839 :   return this;
    1175                 :             : }
    1176                 :             : 
    1177                 :             : void
    1178                 :       28424 : Enclosed_var_expression::do_address_taken(bool escapes)
    1179                 :             : {
    1180                 :       28424 :   if (!escapes)
    1181                 :             :     {
    1182                 :       23149 :       if (this->variable_->is_variable())
    1183                 :       23080 :         this->variable_->var_value()->set_non_escaping_address_taken();
    1184                 :          69 :       else if (this->variable_->is_result_variable())
    1185                 :          69 :         this->variable_->result_var_value()->set_non_escaping_address_taken();
    1186                 :             :       else
    1187                 :           0 :         go_unreachable();
    1188                 :             :     }
    1189                 :             :   else
    1190                 :             :     {
    1191                 :        5275 :       if (this->variable_->is_variable())
    1192                 :        5266 :         this->variable_->var_value()->set_address_taken();
    1193                 :           9 :       else if (this->variable_->is_result_variable())
    1194                 :           9 :         this->variable_->result_var_value()->set_address_taken();
    1195                 :             :       else
    1196                 :           0 :         go_unreachable();
    1197                 :             :     }
    1198                 :             : 
    1199                 :       28424 :   if (this->variable_->is_variable()
    1200                 :       28424 :       && this->variable_->var_value()->is_in_heap())
    1201                 :       21942 :     Node::make_node(this->variable_)->set_encoding(Node::ESCAPE_HEAP);
    1202                 :       28424 : }
    1203                 :             : 
    1204                 :             : // Ast dump for enclosed variable expression.
    1205                 :             : 
    1206                 :             : void
    1207                 :           0 : Enclosed_var_expression::do_dump_expression(Ast_dump_context* adc) const
    1208                 :             : {
    1209                 :           0 :   adc->ostream() << this->variable_->message_name();
    1210                 :           0 : }
    1211                 :             : 
    1212                 :             : // Make a reference to a variable within an enclosing function.
    1213                 :             : 
    1214                 :             : Expression*
    1215                 :       43136 : Expression::make_enclosing_var_reference(Expression* reference,
    1216                 :             :                                          Named_object* var, Location location)
    1217                 :             : {
    1218                 :       43136 :   return new Enclosed_var_expression(reference, var, location);
    1219                 :             : }
    1220                 :             : 
    1221                 :             : // Class Temporary_reference_expression.
    1222                 :             : 
    1223                 :             : // The type.
    1224                 :             : 
    1225                 :             : Type*
    1226                 :    18805052 : Temporary_reference_expression::do_type()
    1227                 :             : {
    1228                 :    18805052 :   return this->statement_->type();
    1229                 :             : }
    1230                 :             : 
    1231                 :             : // Called if something takes the address of this temporary variable.
    1232                 :             : // We never have to move temporary variables to the heap, but we do
    1233                 :             : // need to know that they must live in the stack rather than in a
    1234                 :             : // register.
    1235                 :             : 
    1236                 :             : void
    1237                 :       33858 : Temporary_reference_expression::do_address_taken(bool)
    1238                 :             : {
    1239                 :       33858 :   this->statement_->set_is_address_taken();
    1240                 :       33858 : }
    1241                 :             : 
    1242                 :             : // Export a reference to a temporary.
    1243                 :             : 
    1244                 :             : void
    1245                 :        6731 : Temporary_reference_expression::do_export(Export_function_body* efb) const
    1246                 :             : {
    1247                 :        6731 :   unsigned int idx = efb->temporary_index(this->statement_);
    1248                 :        6731 :   char buf[50];
    1249                 :        6731 :   snprintf(buf, sizeof buf, "$t%u", idx);
    1250                 :        6731 :   efb->write_c_string(buf);
    1251                 :        6731 : }
    1252                 :             : 
    1253                 :             : // Import a reference to a temporary.
    1254                 :             : 
    1255                 :             : Expression*
    1256                 :        3923 : Temporary_reference_expression::do_import(Import_function_body* ifb,
    1257                 :             :                                           Location loc)
    1258                 :             : {
    1259                 :        3923 :   std::string id = ifb->read_identifier();
    1260                 :        3923 :   go_assert(id[0] == '$' && id[1] == 't');
    1261                 :        3923 :   const char *p = id.c_str();
    1262                 :        3923 :   char *end;
    1263                 :        3923 :   long idx = strtol(p + 2, &end, 10);
    1264                 :        3923 :   if (*end != '\0' || idx > 0x7fffffff)
    1265                 :             :     {
    1266                 :           0 :       if (!ifb->saw_error())
    1267                 :           0 :         go_error_at(loc,
    1268                 :             :                     ("invalid export data for %qs: "
    1269                 :             :                      "invalid temporary reference index at %lu"),
    1270                 :           0 :                     ifb->name().c_str(),
    1271                 :           0 :                     static_cast<unsigned long>(ifb->off()));
    1272                 :           0 :       ifb->set_saw_error();
    1273                 :           0 :       return Expression::make_error(loc);
    1274                 :             :     }
    1275                 :             : 
    1276                 :        3923 :   Temporary_statement* temp =
    1277                 :        3923 :     ifb->temporary_statement(static_cast<unsigned int>(idx));
    1278                 :        3923 :   if (temp == NULL)
    1279                 :             :     {
    1280                 :           0 :       if (!ifb->saw_error())
    1281                 :           0 :         go_error_at(loc,
    1282                 :             :                     ("invalid export data for %qs: "
    1283                 :             :                      "undefined temporary reference index at %lu"),
    1284                 :           0 :                     ifb->name().c_str(),
    1285                 :           0 :                     static_cast<unsigned long>(ifb->off()));
    1286                 :           0 :       ifb->set_saw_error();
    1287                 :           0 :       return Expression::make_error(loc);
    1288                 :             :     }
    1289                 :             : 
    1290                 :        3923 :   return Expression::make_temporary_reference(temp, loc);
    1291                 :        3923 : }
    1292                 :             : 
    1293                 :             : // Get a backend expression referring to the variable.
    1294                 :             : 
    1295                 :             : Bexpression*
    1296                 :     3792946 : Temporary_reference_expression::do_get_backend(Translate_context* context)
    1297                 :             : {
    1298                 :     3792946 :   Gogo* gogo = context->gogo();
    1299                 :     3792946 :   Bvariable* bvar = this->statement_->get_backend_variable(context);
    1300                 :     3792946 :   Bexpression* ret = gogo->backend()->var_expression(bvar, this->location());
    1301                 :             : 
    1302                 :             :   // The backend can't always represent the same set of recursive types
    1303                 :             :   // that the Go frontend can.  In some cases this means that a
    1304                 :             :   // temporary variable won't have the right backend type.  Correct
    1305                 :             :   // that here by adding a type cast.  We need to use base() to push
    1306                 :             :   // the circularity down one level.
    1307                 :     3792946 :   Type* stype = this->statement_->type();
    1308                 :     3792946 :   if (!this->is_lvalue_
    1309                 :     3698578 :       && stype->points_to() != NULL
    1310                 :     4263465 :       && stype->points_to()->is_void_type())
    1311                 :             :     {
    1312                 :       18132 :       Btype* btype = this->type()->base()->get_backend(gogo);
    1313                 :       18132 :       ret = gogo->backend()->convert_expression(btype, ret, this->location());
    1314                 :             :     }
    1315                 :     3792946 :   return ret;
    1316                 :             : }
    1317                 :             : 
    1318                 :             : // Ast dump for temporary reference.
    1319                 :             : 
    1320                 :             : void
    1321                 :           0 : Temporary_reference_expression::do_dump_expression(
    1322                 :             :                                 Ast_dump_context* ast_dump_context) const
    1323                 :             : {
    1324                 :           0 :   ast_dump_context->dump_temp_variable_name(this->statement_);
    1325                 :           0 : }
    1326                 :             : 
    1327                 :             : // Make a reference to a temporary variable.
    1328                 :             : 
    1329                 :             : Temporary_reference_expression*
    1330                 :     3859268 : Expression::make_temporary_reference(Temporary_statement* statement,
    1331                 :             :                                      Location location)
    1332                 :             : {
    1333                 :     3859268 :   statement->add_use();
    1334                 :     3859268 :   return new Temporary_reference_expression(statement, location);
    1335                 :             : }
    1336                 :             : 
    1337                 :             : // Class Set_and_use_temporary_expression.
    1338                 :             : 
    1339                 :             : // Return the type.
    1340                 :             : 
    1341                 :             : Type*
    1342                 :      325969 : Set_and_use_temporary_expression::do_type()
    1343                 :             : {
    1344                 :      325969 :   return this->statement_->type();
    1345                 :             : }
    1346                 :             : 
    1347                 :             : // Determine the type of the expression.
    1348                 :             : 
    1349                 :             : void
    1350                 :       20879 : Set_and_use_temporary_expression::do_determine_type(
    1351                 :             :     Gogo* gogo,
    1352                 :             :     const Type_context* context)
    1353                 :             : {
    1354                 :       20879 :   this->expr_->determine_type(gogo, context);
    1355                 :       20879 : }
    1356                 :             : 
    1357                 :             : // Take the address.
    1358                 :             : 
    1359                 :             : void
    1360                 :           0 : Set_and_use_temporary_expression::do_address_taken(bool)
    1361                 :             : {
    1362                 :           0 :   this->statement_->set_is_address_taken();
    1363                 :           0 : }
    1364                 :             : 
    1365                 :             : // Return the backend representation.
    1366                 :             : 
    1367                 :             : Bexpression*
    1368                 :       16122 : Set_and_use_temporary_expression::do_get_backend(Translate_context* context)
    1369                 :             : {
    1370                 :       16122 :   Location loc = this->location();
    1371                 :       16122 :   Gogo* gogo = context->gogo();
    1372                 :       16122 :   Bvariable* bvar = this->statement_->get_backend_variable(context);
    1373                 :       16122 :   Bexpression* lvar_ref = gogo->backend()->var_expression(bvar, loc);
    1374                 :             : 
    1375                 :       16122 :   Named_object* fn = context->function();
    1376                 :       16122 :   go_assert(fn != NULL);
    1377                 :       16122 :   Bfunction* bfn = fn->func_value()->get_or_make_decl(gogo, fn);
    1378                 :       16122 :   Bexpression* bexpr = this->expr_->get_backend(context);
    1379                 :       16122 :   Bstatement* set = gogo->backend()->assignment_statement(bfn, lvar_ref,
    1380                 :             :                                                           bexpr, loc);
    1381                 :       16122 :   Bexpression* var_ref = gogo->backend()->var_expression(bvar, loc);
    1382                 :       16122 :   Bexpression* ret = gogo->backend()->compound_expression(set, var_ref, loc);
    1383                 :       16122 :   return ret;
    1384                 :             : }
    1385                 :             : 
    1386                 :             : // Dump.
    1387                 :             : 
    1388                 :             : void
    1389                 :           0 : Set_and_use_temporary_expression::do_dump_expression(
    1390                 :             :     Ast_dump_context* ast_dump_context) const
    1391                 :             : {
    1392                 :           0 :   ast_dump_context->ostream() << '(';
    1393                 :           0 :   ast_dump_context->dump_temp_variable_name(this->statement_);
    1394                 :           0 :   ast_dump_context->ostream() << " = ";
    1395                 :           0 :   this->expr_->dump_expression(ast_dump_context);
    1396                 :           0 :   ast_dump_context->ostream() << ')';
    1397                 :           0 : }
    1398                 :             : 
    1399                 :             : // Make a set-and-use temporary.
    1400                 :             : 
    1401                 :             : Set_and_use_temporary_expression*
    1402                 :       19056 : Expression::make_set_and_use_temporary(Temporary_statement* statement,
    1403                 :             :                                        Expression* expr, Location location)
    1404                 :             : {
    1405                 :       19056 :   return new Set_and_use_temporary_expression(statement, expr, location);
    1406                 :             : }
    1407                 :             : 
    1408                 :             : // A sink expression--a use of the blank identifier _.
    1409                 :             : 
    1410                 :             : class Sink_expression : public Expression
    1411                 :             : {
    1412                 :             :  public:
    1413                 :       27627 :   Sink_expression(Location location)
    1414                 :       27627 :     : Expression(EXPRESSION_SINK, location),
    1415                 :           0 :       type_(NULL), bvar_(NULL)
    1416                 :             :   { }
    1417                 :             : 
    1418                 :             :  protected:
    1419                 :             :   bool
    1420                 :           0 :   do_discarding_value()
    1421                 :           0 :   { return true; }
    1422                 :             : 
    1423                 :             :   Type*
    1424                 :             :   do_type();
    1425                 :             : 
    1426                 :             :   void
    1427                 :             :   do_determine_type(Gogo*, const Type_context*);
    1428                 :             : 
    1429                 :             :   Expression*
    1430                 :           0 :   do_copy()
    1431                 :           0 :   { return new Sink_expression(this->location()); }
    1432                 :             : 
    1433                 :             :   Bexpression*
    1434                 :             :   do_get_backend(Translate_context*);
    1435                 :             : 
    1436                 :             :   void
    1437                 :             :   do_dump_expression(Ast_dump_context*) const;
    1438                 :             : 
    1439                 :             :  private:
    1440                 :             :   // The type of this sink variable.
    1441                 :             :   Type* type_;
    1442                 :             :   // The temporary variable we generate.
    1443                 :             :   Bvariable* bvar_;
    1444                 :             : };
    1445                 :             : 
    1446                 :             : // Return the type of a sink expression.
    1447                 :             : 
    1448                 :             : Type*
    1449                 :      189204 : Sink_expression::do_type()
    1450                 :             : {
    1451                 :      189204 :   if (this->type_ == NULL)
    1452                 :      189204 :     return Type::make_sink_type();
    1453                 :             :   return this->type_;
    1454                 :             : }
    1455                 :             : 
    1456                 :             : // Determine the type of a sink expression.
    1457                 :             : 
    1458                 :             : void
    1459                 :       63873 : Sink_expression::do_determine_type(Gogo*, const Type_context* context)
    1460                 :             : {
    1461                 :       63873 :   if (context->type != NULL)
    1462                 :           0 :     this->type_ = context->type;
    1463                 :       63873 : }
    1464                 :             : 
    1465                 :             : // Return a temporary variable for a sink expression.  This will
    1466                 :             : // presumably be a write-only variable which the middle-end will drop.
    1467                 :             : 
    1468                 :             : Bexpression*
    1469                 :           0 : Sink_expression::do_get_backend(Translate_context* context)
    1470                 :             : {
    1471                 :           0 :   Location loc = this->location();
    1472                 :           0 :   Gogo* gogo = context->gogo();
    1473                 :           0 :   if (this->bvar_ == NULL)
    1474                 :             :     {
    1475                 :           0 :       if (this->type_ == NULL || this->type_->is_sink_type())
    1476                 :             :         {
    1477                 :           0 :           go_assert(saw_errors());
    1478                 :           0 :           return gogo->backend()->error_expression();
    1479                 :             :         }
    1480                 :             : 
    1481                 :           0 :       Named_object* fn = context->function();
    1482                 :           0 :       go_assert(fn != NULL);
    1483                 :           0 :       Bfunction* fn_ctx = fn->func_value()->get_or_make_decl(gogo, fn);
    1484                 :           0 :       Btype* bt = this->type_->get_backend(context->gogo());
    1485                 :           0 :       Bstatement* decl;
    1486                 :           0 :       this->bvar_ =
    1487                 :           0 :         gogo->backend()->temporary_variable(fn_ctx, context->bblock(), bt, NULL,
    1488                 :             :                                             0, loc, &decl);
    1489                 :           0 :       Bexpression* var_ref =
    1490                 :           0 :           gogo->backend()->var_expression(this->bvar_, loc);
    1491                 :           0 :       var_ref = gogo->backend()->compound_expression(decl, var_ref, loc);
    1492                 :           0 :       return var_ref;
    1493                 :             :     }
    1494                 :           0 :   return gogo->backend()->var_expression(this->bvar_, loc);
    1495                 :             : }
    1496                 :             : 
    1497                 :             : // Ast dump for sink expression.
    1498                 :             : 
    1499                 :             : void
    1500                 :           0 : Sink_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
    1501                 :             : {
    1502                 :           0 :   ast_dump_context->ostream() << "_" ;
    1503                 :           0 : }
    1504                 :             : 
    1505                 :             : // Make a sink expression.
    1506                 :             : 
    1507                 :             : Expression*
    1508                 :       27627 : Expression::make_sink(Location location)
    1509                 :             : {
    1510                 :       27627 :   return new Sink_expression(location);
    1511                 :             : }
    1512                 :             : 
    1513                 :             : // Class Func_expression.
    1514                 :             : 
    1515                 :             : // FIXME: Can a function expression appear in a constant expression?
    1516                 :             : // The value is unchanging.  Initializing a constant to the address of
    1517                 :             : // a function seems like it could work, though there might be little
    1518                 :             : // point to it.
    1519                 :             : 
    1520                 :             : // Traversal.
    1521                 :             : 
    1522                 :             : int
    1523                 :    14462028 : Func_expression::do_traverse(Traverse* traverse)
    1524                 :             : {
    1525                 :    14462028 :   return (this->closure_ == NULL
    1526                 :    14462028 :           ? TRAVERSE_CONTINUE
    1527                 :      232320 :           : Expression::traverse(&this->closure_, traverse));
    1528                 :             : }
    1529                 :             : 
    1530                 :             : // Return the type of a function expression.
    1531                 :             : 
    1532                 :             : Type*
    1533                 :    24385138 : Func_expression::do_type()
    1534                 :             : {
    1535                 :    24385138 :   if (this->function_->is_function())
    1536                 :    11179678 :     return this->function_->func_value()->type();
    1537                 :    13205460 :   else if (this->function_->is_function_declaration())
    1538                 :    13205460 :     return this->function_->func_declaration_value()->type();
    1539                 :             :   else
    1540                 :           0 :     go_unreachable();
    1541                 :             : }
    1542                 :             : 
    1543                 :             : // Get the backend representation for the code of a function expression.
    1544                 :             : 
    1545                 :             : Bexpression*
    1546                 :     2137390 : Func_expression::get_code_pointer(Gogo* gogo, Named_object* no, Location loc)
    1547                 :             : {
    1548                 :     2137390 :   Function_type* fntype;
    1549                 :     2137390 :   if (no->is_function())
    1550                 :      612558 :     fntype = no->func_value()->type();
    1551                 :     1524832 :   else if (no->is_function_declaration())
    1552                 :     1524832 :     fntype = no->func_declaration_value()->type();
    1553                 :             :   else
    1554                 :           0 :     go_unreachable();
    1555                 :             : 
    1556                 :             :   // Builtin functions are handled specially by Call_expression.  We
    1557                 :             :   // can't take their address.
    1558                 :     2137390 :   if (fntype->is_builtin())
    1559                 :             :     {
    1560                 :           0 :       go_error_at(loc,
    1561                 :             :                   ("invalid use of special built-in function %qs; "
    1562                 :             :                    "must be called"),
    1563                 :           0 :                   no->message_name().c_str());
    1564                 :           0 :       return gogo->backend()->error_expression();
    1565                 :             :     }
    1566                 :             : 
    1567                 :     2137390 :   Bfunction* fndecl;
    1568                 :     2137390 :   if (no->is_function())
    1569                 :      612558 :     fndecl = no->func_value()->get_or_make_decl(gogo, no);
    1570                 :     1524832 :   else if (no->is_function_declaration())
    1571                 :     1524832 :     fndecl = no->func_declaration_value()->get_or_make_decl(gogo, no);
    1572                 :             :   else
    1573                 :             :     go_unreachable();
    1574                 :             : 
    1575                 :     2137390 :   return gogo->backend()->function_code_expression(fndecl, loc);
    1576                 :             : }
    1577                 :             : 
    1578                 :             : // Get the backend representation for a function expression.  This is used when
    1579                 :             : // we take the address of a function rather than simply calling it.  A func
    1580                 :             : // value is represented as a pointer to a block of memory.  The first
    1581                 :             : // word of that memory is a pointer to the function code.  The
    1582                 :             : // remaining parts of that memory are the addresses of variables that
    1583                 :             : // the function closes over.
    1584                 :             : 
    1585                 :             : Bexpression*
    1586                 :      155415 : Func_expression::do_get_backend(Translate_context* context)
    1587                 :             : {
    1588                 :             :   // If there is no closure, just use the function descriptor.
    1589                 :      155415 :   if (this->closure_ == NULL)
    1590                 :             :     {
    1591                 :      142613 :       Gogo* gogo = context->gogo();
    1592                 :      142613 :       Named_object* no = this->function_;
    1593                 :      142613 :       Expression* descriptor;
    1594                 :      142613 :       if (no->is_function())
    1595                 :       37921 :         descriptor = no->func_value()->descriptor(gogo, no);
    1596                 :      104692 :       else if (no->is_function_declaration())
    1597                 :             :         {
    1598                 :      104692 :           if (no->func_declaration_value()->type()->is_builtin())
    1599                 :             :             {
    1600                 :           1 :               go_error_at(this->location(),
    1601                 :             :                           ("invalid use of special built-in function %qs; "
    1602                 :             :                            "must be called"),
    1603                 :           1 :                           no->message_name().c_str());
    1604                 :           1 :               return gogo->backend()->error_expression();
    1605                 :             :             }
    1606                 :      104691 :           descriptor = no->func_declaration_value()->descriptor(gogo, no);
    1607                 :             :         }
    1608                 :             :       else
    1609                 :           0 :         go_unreachable();
    1610                 :             : 
    1611                 :      142612 :       Bexpression* bdesc = descriptor->get_backend(context);
    1612                 :      142612 :       return gogo->backend()->address_expression(bdesc, this->location());
    1613                 :             :     }
    1614                 :             : 
    1615                 :       12802 :   go_assert(this->function_->func_value()->enclosing() != NULL);
    1616                 :             : 
    1617                 :             :   // If there is a closure, then the closure is itself the function
    1618                 :             :   // expression.  It is a pointer to a struct whose first field points
    1619                 :             :   // to the function code and whose remaining fields are the addresses
    1620                 :             :   // of the closed-over variables.
    1621                 :       12802 :   Bexpression *bexpr = this->closure_->get_backend(context);
    1622                 :             : 
    1623                 :             :   // Introduce a backend type conversion, to account for any differences
    1624                 :             :   // between the argument type (function descriptor, struct with a
    1625                 :             :   // single field) and the closure (struct with multiple fields).
    1626                 :       12802 :   Gogo* gogo = context->gogo();
    1627                 :       12802 :   Btype *btype = this->type()->get_backend(gogo);
    1628                 :       12802 :   return gogo->backend()->convert_expression(btype, bexpr, this->location());
    1629                 :             : }
    1630                 :             : 
    1631                 :             : // The cost of inlining a function reference.
    1632                 :             : 
    1633                 :             : int
    1634                 :      570550 : Func_expression::do_inlining_cost() const
    1635                 :             : {
    1636                 :             :   // FIXME: We don't inline references to nested functions.
    1637                 :      570550 :   if (this->closure_ != NULL)
    1638                 :             :     return 0x100000;
    1639                 :      560406 :   if (this->function_->is_function()
    1640                 :      560406 :       && this->function_->func_value()->enclosing() != NULL)
    1641                 :        2780 :     return 0x100000;
    1642                 :             : 
    1643                 :             :   return 1;
    1644                 :             : }
    1645                 :             : 
    1646                 :             : // Export a reference to a function.
    1647                 :             : 
    1648                 :             : void
    1649                 :       10918 : Func_expression::do_export(Export_function_body* efb) const
    1650                 :             : {
    1651                 :       10918 :   Expression::export_name(efb, this->function_);
    1652                 :       10918 : }
    1653                 :             : 
    1654                 :             : // Ast dump for function.
    1655                 :             : 
    1656                 :             : void
    1657                 :           0 : Func_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
    1658                 :             : {
    1659                 :           0 :   ast_dump_context->ostream() << this->function_->name();
    1660                 :           0 :   if (this->closure_ != NULL)
    1661                 :             :     {
    1662                 :           0 :       ast_dump_context->ostream() << " {closure =  ";
    1663                 :           0 :       this->closure_->dump_expression(ast_dump_context);
    1664                 :           0 :       ast_dump_context->ostream() << "}";
    1665                 :             :     }
    1666                 :           0 : }
    1667                 :             : 
    1668                 :             : // Make a reference to a function in an expression.
    1669                 :             : 
    1670                 :             : Expression*
    1671                 :     2155702 : Expression::make_func_reference(Named_object* function, Expression* closure,
    1672                 :             :                                 Location location)
    1673                 :             : {
    1674                 :     2155702 :   Func_expression* fe = new Func_expression(function, closure, location);
    1675                 :             : 
    1676                 :             :   // Detect references to builtin functions and set the runtime code if
    1677                 :             :   // appropriate.
    1678                 :     2155702 :   if (function->is_function_declaration())
    1679                 :     1798407 :     fe->set_runtime_code(Runtime::name_to_code(function->name()));
    1680                 :     2155702 :   return fe;
    1681                 :             : }
    1682                 :             : 
    1683                 :             : // Class Func_descriptor_expression.
    1684                 :             : 
    1685                 :             : // Constructor.
    1686                 :             : 
    1687                 :      259481 : Func_descriptor_expression::Func_descriptor_expression(Named_object* fn)
    1688                 :             :   : Expression(EXPRESSION_FUNC_DESCRIPTOR, fn->location()),
    1689                 :      259481 :     fn_(fn), dvar_(NULL)
    1690                 :             : {
    1691                 :      259481 :   go_assert(!fn->is_function() || !fn->func_value()->needs_closure());
    1692                 :      259481 : }
    1693                 :             : 
    1694                 :             : // Traversal.
    1695                 :             : 
    1696                 :             : int
    1697                 :           0 : Func_descriptor_expression::do_traverse(Traverse*)
    1698                 :             : {
    1699                 :           0 :   return TRAVERSE_CONTINUE;
    1700                 :             : }
    1701                 :             : 
    1702                 :             : // All function descriptors have the same type.
    1703                 :             : 
    1704                 :             : Type* Func_descriptor_expression::descriptor_type;
    1705                 :             : 
    1706                 :             : void
    1707                 :      407919 : Func_descriptor_expression::make_func_descriptor_type()
    1708                 :             : {
    1709                 :      407919 :   if (Func_descriptor_expression::descriptor_type != NULL)
    1710                 :             :     return;
    1711                 :        4642 :   Type* uintptr_type = Type::lookup_integer_type("uintptr");
    1712                 :        4642 :   Type* struct_type = Type::make_builtin_struct_type(1, "fn", uintptr_type);
    1713                 :        4642 :   Func_descriptor_expression::descriptor_type =
    1714                 :        4642 :     Type::make_builtin_named_type("functionDescriptor", struct_type);
    1715                 :             : }
    1716                 :             : 
    1717                 :             : Type*
    1718                 :      403277 : Func_descriptor_expression::do_type()
    1719                 :             : {
    1720                 :      403277 :   Func_descriptor_expression::make_func_descriptor_type();
    1721                 :      403277 :   return Func_descriptor_expression::descriptor_type;
    1722                 :             : }
    1723                 :             : 
    1724                 :             : // The backend representation for a function descriptor.
    1725                 :             : 
    1726                 :             : Bexpression*
    1727                 :      297698 : Func_descriptor_expression::do_get_backend(Translate_context* context)
    1728                 :             : {
    1729                 :      297698 :   Named_object* no = this->fn_;
    1730                 :      297698 :   Location loc = no->location();
    1731                 :      297698 :   if (this->dvar_ != NULL)
    1732                 :       38226 :     return context->backend()->var_expression(this->dvar_, loc);
    1733                 :             : 
    1734                 :      259472 :   Gogo* gogo = context->gogo();
    1735                 :      259472 :   Backend_name bname;
    1736                 :      259472 :   gogo->function_descriptor_backend_name(no, &bname);
    1737                 :      259472 :   bool is_descriptor = false;
    1738                 :      259472 :   if (no->is_function_declaration()
    1739                 :      105036 :       && !no->func_declaration_value()->asm_name().empty()
    1740                 :      349480 :       && Linemap::is_predeclared_location(no->location()))
    1741                 :             :     is_descriptor = true;
    1742                 :             : 
    1743                 :             :   // The runtime package implements some functions defined in the
    1744                 :             :   // syscall package.  Let the syscall package define the descriptor
    1745                 :             :   // in this case.
    1746                 :      259472 :   if (gogo->compiling_runtime()
    1747                 :       21429 :       && gogo->package_name() == "runtime"
    1748                 :       14731 :       && no->is_function()
    1749                 :        5497 :       && !no->func_value()->asm_name().empty()
    1750                 :      263318 :       && no->func_value()->asm_name().compare(0, 8, "syscall.") == 0)
    1751                 :             :     is_descriptor = true;
    1752                 :             : 
    1753                 :      259472 :   Btype* btype = this->type()->get_backend(gogo);
    1754                 :             : 
    1755                 :      259472 :   Bvariable* bvar;
    1756                 :      259472 :   if (no->package() != NULL || is_descriptor)
    1757                 :      115667 :     bvar =
    1758                 :      115667 :       context->backend()->immutable_struct_reference(bname.name(),
    1759                 :      231334 :                                                      bname.optional_asm_name(),
    1760                 :             :                                                      btype, loc);
    1761                 :             :   else
    1762                 :             :     {
    1763                 :      143805 :       Location bloc = Linemap::predeclared_location();
    1764                 :             : 
    1765                 :             :       // The runtime package has hash/equality functions that are
    1766                 :             :       // referenced by type descriptors outside of the runtime, so the
    1767                 :             :       // function descriptors must be visible even though they are not
    1768                 :             :       // exported.
    1769                 :      143805 :       bool is_exported_runtime = false;
    1770                 :      143805 :       if (gogo->compiling_runtime()
    1771                 :        8867 :           && gogo->package_name() == "runtime"
    1772                 :      149266 :           && (no->name().find("hash") != std::string::npos
    1773                 :        5356 :               || no->name().find("equal") != std::string::npos))
    1774                 :             :         is_exported_runtime = true;
    1775                 :             : 
    1776                 :      143805 :       bool is_hidden = ((no->is_function()
    1777                 :      143155 :                          && no->func_value()->enclosing() != NULL)
    1778                 :      133034 :                         || (Gogo::is_hidden_name(no->name())
    1779                 :        7955 :                             && !is_exported_runtime)
    1780                 :      269087 :                         || Gogo::is_thunk(no));
    1781                 :             : 
    1782                 :      143805 :       if (no->is_function() && no->func_value()->is_referenced_by_inline())
    1783                 :             :         is_hidden = false;
    1784                 :             : 
    1785                 :      142209 :       unsigned int flags = 0;
    1786                 :      142209 :       if (is_hidden)
    1787                 :      143805 :         flags |= Backend::variable_is_hidden;
    1788                 :      143805 :       bvar = context->backend()->immutable_struct(bname.name(),
    1789                 :      143805 :                                                   bname.optional_asm_name(),
    1790                 :             :                                                   flags, btype, bloc);
    1791                 :      143805 :       Expression_list* vals = new Expression_list();
    1792                 :      143805 :       vals->push_back(Expression::make_func_code_reference(this->fn_, bloc));
    1793                 :      143805 :       Expression* init =
    1794                 :      143805 :         Expression::make_struct_composite_literal(this->type(), vals, bloc);
    1795                 :      143805 :       Translate_context bcontext(gogo, NULL, NULL, NULL);
    1796                 :      143805 :       bcontext.set_is_const();
    1797                 :      143805 :       Bexpression* binit = init->get_backend(&bcontext);
    1798                 :      143805 :       context->backend()->immutable_struct_set_init(bvar, bname.name(),
    1799                 :             :                                                     flags, btype, bloc, binit);
    1800                 :             :     }
    1801                 :             : 
    1802                 :      259472 :   this->dvar_ = bvar;
    1803                 :      259472 :   return gogo->backend()->var_expression(bvar, loc);
    1804                 :      259472 : }
    1805                 :             : 
    1806                 :             : // Print a function descriptor expression.
    1807                 :             : 
    1808                 :             : void
    1809                 :           0 : Func_descriptor_expression::do_dump_expression(Ast_dump_context* context) const
    1810                 :             : {
    1811                 :           0 :   context->ostream() << "[descriptor " << this->fn_->name() << "]";
    1812                 :           0 : }
    1813                 :             : 
    1814                 :             : // Make a function descriptor expression.
    1815                 :             : 
    1816                 :             : Func_descriptor_expression*
    1817                 :      259481 : Expression::make_func_descriptor(Named_object* fn)
    1818                 :             : {
    1819                 :      259481 :   return new Func_descriptor_expression(fn);
    1820                 :             : }
    1821                 :             : 
    1822                 :             : // Make the function descriptor type, so that it can be converted.
    1823                 :             : 
    1824                 :             : void
    1825                 :        4642 : Expression::make_func_descriptor_type()
    1826                 :             : {
    1827                 :        4642 :   Func_descriptor_expression::make_func_descriptor_type();
    1828                 :        4642 : }
    1829                 :             : 
    1830                 :             : // A reference to just the code of a function.
    1831                 :             : 
    1832                 :             : class Func_code_reference_expression : public Expression
    1833                 :             : {
    1834                 :             :  public:
    1835                 :     2357360 :   Func_code_reference_expression(Named_object* function, Location location)
    1836                 :     2357360 :     : Expression(EXPRESSION_FUNC_CODE_REFERENCE, location),
    1837                 :     4714720 :       function_(function)
    1838                 :             :   { }
    1839                 :             : 
    1840                 :             :  protected:
    1841                 :             :   int
    1842                 :      221665 :   do_traverse(Traverse*)
    1843                 :      221665 :   { return TRAVERSE_CONTINUE; }
    1844                 :             : 
    1845                 :             :   bool
    1846                 :      318868 :   do_is_static_initializer() const
    1847                 :      318868 :   { return true; }
    1848                 :             : 
    1849                 :             :   Type*
    1850                 :      456875 :   do_type()
    1851                 :      456875 :   { return Type::make_pointer_type(Type::make_void_type()); }
    1852                 :             : 
    1853                 :             :   void
    1854                 :      548802 :   do_determine_type(Gogo*, const Type_context*)
    1855                 :      548802 :   { }
    1856                 :             : 
    1857                 :             :   Expression*
    1858                 :           0 :   do_copy()
    1859                 :             :   {
    1860                 :           0 :     return Expression::make_func_code_reference(this->function_,
    1861                 :           0 :                                                 this->location());
    1862                 :             :   }
    1863                 :             : 
    1864                 :             :   Bexpression*
    1865                 :             :   do_get_backend(Translate_context*);
    1866                 :             : 
    1867                 :             :   void
    1868                 :           0 :   do_dump_expression(Ast_dump_context* context) const
    1869                 :           0 :   { context->ostream() << "[raw " << this->function_->name() << "]" ; }
    1870                 :             : 
    1871                 :             :  private:
    1872                 :             :   // The function.
    1873                 :             :   Named_object* function_;
    1874                 :             : };
    1875                 :             : 
    1876                 :             : // Get the backend representation for a reference to function code.
    1877                 :             : 
    1878                 :             : Bexpression*
    1879                 :     2137390 : Func_code_reference_expression::do_get_backend(Translate_context* context)
    1880                 :             : {
    1881                 :     2137390 :   return Func_expression::get_code_pointer(context->gogo(), this->function_,
    1882                 :     2137390 :                                            this->location());
    1883                 :             : }
    1884                 :             : 
    1885                 :             : // Make a reference to the code of a function.
    1886                 :             : 
    1887                 :             : Expression*
    1888                 :     2357360 : Expression::make_func_code_reference(Named_object* function, Location location)
    1889                 :             : {
    1890                 :     2357360 :   return new Func_code_reference_expression(function, location);
    1891                 :             : }
    1892                 :             : 
    1893                 :             : // Class Unknown_expression.
    1894                 :             : 
    1895                 :             : // Return the name of an unknown expression.
    1896                 :             : 
    1897                 :             : const std::string&
    1898                 :        5232 : Unknown_expression::name() const
    1899                 :             : {
    1900                 :        5232 :   return this->named_object_->name();
    1901                 :             : }
    1902                 :             : 
    1903                 :             : // Set the iota value if this could be a reference to iota.
    1904                 :             : 
    1905                 :             : void
    1906                 :       17241 : Unknown_expression::set_iota_value(int iota_value)
    1907                 :             : {
    1908                 :       17241 :   this->iota_value_ = iota_value;
    1909                 :       17241 :   this->is_iota_ = true;
    1910                 :       17241 : }
    1911                 :             : 
    1912                 :             : // Traversal.
    1913                 :             : 
    1914                 :             : int
    1915                 :     2179096 : Unknown_expression::do_traverse(Traverse* traverse)
    1916                 :             : {
    1917                 :     2179096 :   if (this->lowered_ != NULL)
    1918                 :             :     {
    1919                 :     1131135 :       if (Expression::traverse(&this->lowered_, traverse) == TRAVERSE_EXIT)
    1920                 :             :         return TRAVERSE_EXIT;
    1921                 :             :     }
    1922                 :             :   return TRAVERSE_CONTINUE;
    1923                 :             : }
    1924                 :             : 
    1925                 :             : // Determine the type of a reference to an unknown name.  At this
    1926                 :             : // point we have to figure out what the name refers to.
    1927                 :             : 
    1928                 :             : void
    1929                 :      451096 : Unknown_expression::do_determine_type(Gogo* gogo, const Type_context* context)
    1930                 :             : {
    1931                 :      451096 :   if (this->is_error_expression())
    1932                 :       11143 :     return;
    1933                 :             : 
    1934                 :      451096 :   if (this->lowered_ != NULL)
    1935                 :             :     {
    1936                 :       11097 :       this->lowered_->determine_type(gogo, context);
    1937                 :       11097 :       return;
    1938                 :             :     }
    1939                 :             : 
    1940                 :      439999 :   Location loc = this->location();
    1941                 :             : 
    1942                 :      439999 :   Named_object* no = this->named_object_;
    1943                 :      439999 :   if (no->is_unknown())
    1944                 :             :     {
    1945                 :      439471 :       Named_object* real = no->unknown_value()->real_named_object();
    1946                 :      439471 :       if (real == NULL)
    1947                 :             :         {
    1948                 :          46 :           if (!this->no_error_message_)
    1949                 :          45 :             go_error_at(loc, "reference to undefined name %qs",
    1950                 :          90 :                         no->message_name().c_str());
    1951                 :          46 :           this->set_is_error();
    1952                 :          46 :           return;
    1953                 :             :         }
    1954                 :      439425 :       no = real;
    1955                 :      439425 :       this->named_object_ = real;
    1956                 :             :     }
    1957                 :             : 
    1958                 :      439953 :   switch (no->classification())
    1959                 :             :     {
    1960                 :       91849 :     case Named_object::NAMED_OBJECT_TYPE:
    1961                 :       91849 :       this->lowered_ = Expression::make_type(no->type_value(), loc);
    1962                 :       91849 :       break;
    1963                 :       70720 :     case Named_object::NAMED_OBJECT_FUNC:
    1964                 :       70720 :     case Named_object::NAMED_OBJECT_FUNC_DECLARATION:
    1965                 :       70720 :       this->lowered_ = Expression::make_func_reference(no, NULL, loc);
    1966                 :       70720 :       break;
    1967                 :      256297 :     case Named_object::NAMED_OBJECT_CONST:
    1968                 :      256297 :       this->lowered_ = Expression::make_const_reference(no, loc);
    1969                 :      256297 :       this->lowered_->determine_type(gogo, context);
    1970                 :      256297 :       if (this->is_iota_)
    1971                 :          11 :         this->lowered_->const_expression()->set_iota_value(this->iota_value_);
    1972                 :             :       break;
    1973                 :       21087 :     case Named_object::NAMED_OBJECT_VAR:
    1974                 :       21087 :       this->lowered_ = Expression::make_var_reference(no, loc);
    1975                 :       21087 :       no->var_value()->set_is_used();
    1976                 :       21087 :       this->lowered_->determine_type(gogo, context);
    1977                 :       21087 :       break;
    1978                 :           0 :     case Named_object::NAMED_OBJECT_TYPE_DECLARATION:
    1979                 :           0 :       if (!this->no_error_message_)
    1980                 :           0 :         go_error_at(this->location(), "reference to undefined type %qs",
    1981                 :           0 :                     no->message_name().c_str());
    1982                 :           0 :       this->set_is_error();
    1983                 :           0 :       break;
    1984                 :           0 :     case Named_object::NAMED_OBJECT_PACKAGE:
    1985                 :           0 :       if (!this->no_error_message_)
    1986                 :           0 :         this->report_error(_("unexpected reference to package"));
    1987                 :           0 :       this->set_is_error();
    1988                 :           0 :       break;
    1989                 :           0 :     default:
    1990                 :           0 :       go_unreachable();
    1991                 :             :     }
    1992                 :             : }
    1993                 :             : 
    1994                 :             : Type*
    1995                 :     1083742 : Unknown_expression::do_type()
    1996                 :             : {
    1997                 :     1083742 :   if (this->is_error_expression())
    1998                 :          55 :     return Type::make_error_type();
    1999                 :     1083687 :   go_assert(this->lowered_ != NULL);
    2000                 :     1083687 :   return this->lowered_->type();
    2001                 :             : }
    2002                 :             : 
    2003                 :             : bool
    2004                 :      111373 : Unknown_expression::do_is_constant() const
    2005                 :             : {
    2006                 :      111373 :   if (this->is_error_expression())
    2007                 :             :     return true;
    2008                 :      111367 :   if (this->lowered_ != NULL)
    2009                 :       17930 :     return this->lowered_->is_constant();
    2010                 :             : 
    2011                 :             :   // This can be called before do_determine_types by
    2012                 :             :   // Binary_expression::do_determine_type, which needs to know which
    2013                 :             :   // values are constant before it works out the appropriate
    2014                 :             :   // Type_context to pass down.
    2015                 :       93437 :   Named_object* no = this->named_object_;
    2016                 :       93437 :   if (no->is_unknown())
    2017                 :             :     {
    2018                 :       93437 :       no = no->unknown_value()->real_named_object();
    2019                 :       93437 :       if (no == NULL)
    2020                 :             :         return true;
    2021                 :             :     }
    2022                 :       93432 :   return no->is_const();
    2023                 :             : }
    2024                 :             : 
    2025                 :             : bool
    2026                 :       93851 : Unknown_expression::do_is_untyped(Type** ptype) const
    2027                 :             : {
    2028                 :       93851 :   if (this->is_error_expression())
    2029                 :             :     return false;
    2030                 :       93851 :   if (this->lowered_ != NULL)
    2031                 :           0 :     return this->lowered_->is_untyped(ptype);
    2032                 :             : 
    2033                 :       93851 :   Named_object* no = this->named_object_;
    2034                 :       93851 :   if (no->is_unknown())
    2035                 :             :     {
    2036                 :       93851 :       no = no->unknown_value()->real_named_object();
    2037                 :       93851 :       if (no == NULL)
    2038                 :             :         return false;
    2039                 :             :     }
    2040                 :             : 
    2041                 :       93846 :   if (!no->is_const())
    2042                 :             :     return false;
    2043                 :       92551 :   Type* t = no->const_value()->type();
    2044                 :       92551 :   if (t != NULL)
    2045                 :       87593 :     return Expression::is_untyped_type(t, ptype);
    2046                 :        4958 :   return no->const_value()->expr()->is_untyped(ptype);
    2047                 :             : }
    2048                 :             : 
    2049                 :             : bool
    2050                 :       69041 : Unknown_expression::do_numeric_constant_value(Numeric_constant* nc)
    2051                 :             : {
    2052                 :       69041 :   if (this->is_error_expression())
    2053                 :             :     return false;
    2054                 :       69041 :   if (this->lowered_ != NULL)
    2055                 :       68966 :     return this->lowered_->numeric_constant_value(nc);
    2056                 :             : 
    2057                 :             :   // This can be called before the determine_types pass.
    2058                 :          75 :   Named_object* no = this->named_object_;
    2059                 :          75 :   if (no->is_unknown())
    2060                 :             :     {
    2061                 :          75 :       no = no->unknown_value()->real_named_object();
    2062                 :          75 :       if (no == NULL)
    2063                 :             :         return false;
    2064                 :             :     }
    2065                 :          75 :   if (!no->is_const())
    2066                 :             :     return false;
    2067                 :          75 :   return no->const_value()->expr()->numeric_constant_value(nc);
    2068                 :             : }
    2069                 :             : 
    2070                 :             : bool
    2071                 :         259 : Unknown_expression::do_string_constant_value(std::string* val)
    2072                 :             : {
    2073                 :         259 :   if (this->is_error_expression())
    2074                 :             :     return false;
    2075                 :         259 :   go_assert(this->lowered_ != NULL);
    2076                 :         259 :   return this->lowered_->string_constant_value(val);
    2077                 :             : }
    2078                 :             : 
    2079                 :             : bool
    2080                 :           0 : Unknown_expression::do_boolean_constant_value(bool* val)
    2081                 :             : {
    2082                 :           0 :   if (this->is_error_expression())
    2083                 :             :     return false;
    2084                 :           0 :   go_assert(this->lowered_ != NULL);
    2085                 :           0 :   return this->lowered_->boolean_constant_value(val);
    2086                 :             : }
    2087                 :             : 
    2088                 :             : bool
    2089                 :       12895 : Unknown_expression::do_is_addressable() const
    2090                 :             : {
    2091                 :       12895 :   if (this->is_error_expression())
    2092                 :             :     return true;
    2093                 :       12893 :   go_assert(this->lowered_ != NULL);
    2094                 :       12893 :   return this->lowered_->is_addressable();
    2095                 :             : }
    2096                 :             : 
    2097                 :             : // Lower a reference to an unknown name.
    2098                 :             : 
    2099                 :             : Expression*
    2100                 :      358095 : Unknown_expression::do_lower(Gogo*, Named_object*, Statement_inserter*)
    2101                 :             : {
    2102                 :      358095 :   if (this->is_error_expression())
    2103                 :          29 :     return Expression::make_error(this->location());
    2104                 :      358066 :   go_assert(this->lowered_ != NULL);
    2105                 :             :   return this->lowered_;
    2106                 :             : }
    2107                 :             : 
    2108                 :             : // Dump the ast representation for an unknown expression to a dump context.
    2109                 :             : 
    2110                 :             : void
    2111                 :           0 : Unknown_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
    2112                 :             : {
    2113                 :           0 :   if (this->lowered_ != NULL)
    2114                 :           0 :     this->lowered_->dump_expression(ast_dump_context);
    2115                 :             :   else
    2116                 :           0 :     ast_dump_context->ostream() << "_Unknown_(" << this->named_object_->name()
    2117                 :           0 :                                 << ")";
    2118                 :           0 : }
    2119                 :             : 
    2120                 :             : // Make a reference to an unknown name.
    2121                 :             : 
    2122                 :             : Unknown_expression*
    2123                 :      592238 : Expression::make_unknown_reference(Named_object* no, Location location)
    2124                 :             : {
    2125                 :      592238 :   return new Unknown_expression(no, location);
    2126                 :             : }
    2127                 :             : 
    2128                 :             : // Start exporting a type conversion for a constant, if needed.  This
    2129                 :             : // returns whether we need to export a closing parenthesis.
    2130                 :             : 
    2131                 :             : bool
    2132                 :       65044 : Expression::export_constant_type(Export_function_body* efb, Type* type)
    2133                 :             : {
    2134                 :       65044 :   if (type == NULL
    2135                 :       61637 :       || type->is_abstract()
    2136                 :       95371 :       || type == efb->type_context())
    2137                 :       51539 :     return false;
    2138                 :       13505 :   efb->write_c_string("$convert(");
    2139                 :       13505 :   efb->write_type(type);
    2140                 :       13505 :   efb->write_c_string(", ");
    2141                 :       13505 :   return true;
    2142                 :             : }
    2143                 :             : 
    2144                 :             : // Finish a type conversion for a constant.
    2145                 :             : 
    2146                 :             : void
    2147                 :       65044 : Expression::finish_export_constant_type(Export_function_body* efb, bool needed)
    2148                 :             : {
    2149                 :       65044 :   if (needed)
    2150                 :       13505 :     efb->write_c_string(")");
    2151                 :       65044 : }
    2152                 :             : 
    2153                 :             : // A boolean expression.
    2154                 :             : 
    2155                 :             : class Boolean_expression : public Expression
    2156                 :             : {
    2157                 :             :  public:
    2158                 :      594670 :   Boolean_expression(bool val, Location location)
    2159                 :      594670 :     : Expression(EXPRESSION_BOOLEAN, location),
    2160                 :     1189340 :       val_(val), type_(NULL)
    2161                 :             :   { }
    2162                 :             : 
    2163                 :             :   static Expression*
    2164                 :             :   do_import(Import_expression*, Location);
    2165                 :             : 
    2166                 :             :  protected:
    2167                 :             :   int
    2168                 :             :   do_traverse(Traverse*);
    2169                 :             : 
    2170                 :             :   bool
    2171                 :       24832 :   do_is_constant() const
    2172                 :       24832 :   { return true; }
    2173                 :             : 
    2174                 :             :   bool
    2175                 :             :   do_is_untyped(Type**) const;
    2176                 :             : 
    2177                 :             :   bool
    2178                 :           0 :   do_is_zero_value() const
    2179                 :           0 :   { return this->val_ == false; }
    2180                 :             : 
    2181                 :             :   bool
    2182                 :        9479 :   do_boolean_constant_value(bool* val)
    2183                 :             :   {
    2184                 :        9479 :     *val = this->val_;
    2185                 :        9479 :     return true;
    2186                 :             :   }
    2187                 :             : 
    2188                 :             :   bool
    2189                 :          11 :   do_is_static_initializer() const
    2190                 :          11 :   { return true; }
    2191                 :             : 
    2192                 :             :   Type*
    2193                 :             :   do_type();
    2194                 :             : 
    2195                 :             :   void
    2196                 :             :   do_determine_type(Gogo*, const Type_context*);
    2197                 :             : 
    2198                 :             :   Expression*
    2199                 :           0 :   do_copy()
    2200                 :           0 :   { return this; }
    2201                 :             : 
    2202                 :             :   Bexpression*
    2203                 :      652774 :   do_get_backend(Translate_context* context)
    2204                 :      652774 :   { return context->backend()->boolean_constant_expression(this->val_); }
    2205                 :             : 
    2206                 :             :   int
    2207                 :       47347 :   do_inlining_cost() const
    2208                 :       47347 :   { return 1; }
    2209                 :             : 
    2210                 :             :   void
    2211                 :             :   do_export(Export_function_body* efb) const;
    2212                 :             : 
    2213                 :             :   void
    2214                 :           0 :   do_dump_expression(Ast_dump_context* ast_dump_context) const
    2215                 :           0 :   { ast_dump_context->ostream() << (this->val_ ? "true" : "false"); }
    2216                 :             : 
    2217                 :             :  private:
    2218                 :             :   // The constant.
    2219                 :             :   bool val_;
    2220                 :             :   // The type as determined by context.
    2221                 :             :   Type* type_;
    2222                 :             : };
    2223                 :             : 
    2224                 :             : // Traverse a boolean expression.  We just need to traverse the type
    2225                 :             : // if there is one.
    2226                 :             : 
    2227                 :             : int
    2228                 :     1399072 : Boolean_expression::do_traverse(Traverse* traverse)
    2229                 :             : {
    2230                 :     1399072 :   if (this->type_ != NULL)
    2231                 :     1364110 :     return Type::traverse(this->type_, traverse);
    2232                 :             :   return TRAVERSE_CONTINUE;
    2233                 :             : }
    2234                 :             : 
    2235                 :             : bool
    2236                 :       24047 : Boolean_expression::do_is_untyped(Type** ptype) const
    2237                 :             : {
    2238                 :       24047 :   if (this->type_ != NULL)
    2239                 :          44 :     return Expression::is_untyped_type(this->type_, ptype);
    2240                 :       24003 :   *ptype = Type::make_boolean_type();
    2241                 :       24003 :   return true;
    2242                 :             : }
    2243                 :             : 
    2244                 :             : // Get the type.
    2245                 :             : 
    2246                 :             : Type*
    2247                 :     1545511 : Boolean_expression::do_type()
    2248                 :             : {
    2249                 :     1545511 :   if (this->type_ == NULL)
    2250                 :       15257 :     this->type_ = Type::make_boolean_type();
    2251                 :     1545511 :   return this->type_;
    2252                 :             : }
    2253                 :             : 
    2254                 :             : // Set the type from the context.
    2255                 :             : 
    2256                 :             : void
    2257                 :      656089 : Boolean_expression::do_determine_type(Gogo*, const Type_context* context)
    2258                 :             : {
    2259                 :      656089 :   if (this->type_ != NULL && !this->type_->is_abstract())
    2260                 :             :     ;
    2261                 :      566805 :   else if (context->type != NULL && context->type->is_boolean_type())
    2262                 :      188992 :     this->type_ = context->type;
    2263                 :      377813 :   else if (!context->may_be_abstract)
    2264                 :      377170 :     this->type_ = Type::lookup_bool_type();
    2265                 :      656089 : }
    2266                 :             : 
    2267                 :             : // Export a boolean constant.
    2268                 :             : 
    2269                 :             : void
    2270                 :        3836 : Boolean_expression::do_export(Export_function_body* efb) const
    2271                 :             : {
    2272                 :        3836 :   bool exported_type = Expression::export_constant_type(efb, this->type_);
    2273                 :        5839 :   efb->write_c_string(this->val_ ? "$true" : "$false");
    2274                 :        3836 :   Expression::finish_export_constant_type(efb, exported_type);
    2275                 :        3836 : }
    2276                 :             : 
    2277                 :             : // Import a boolean constant.
    2278                 :             : 
    2279                 :             : Expression*
    2280                 :        2859 : Boolean_expression::do_import(Import_expression* imp, Location loc)
    2281                 :             : {
    2282                 :        2859 :   if (imp->version() >= EXPORT_FORMAT_V3)
    2283                 :        2859 :     imp->require_c_string("$");
    2284                 :        2859 :   if (imp->peek_char() == 't')
    2285                 :             :     {
    2286                 :        1370 :       imp->require_c_string("true");
    2287                 :        1370 :       return Expression::make_boolean(true, loc);
    2288                 :             :     }
    2289                 :             :   else
    2290                 :             :     {
    2291                 :        1489 :       imp->require_c_string("false");
    2292                 :        1489 :       return Expression::make_boolean(false, loc);
    2293                 :             :     }
    2294                 :             : }
    2295                 :             : 
    2296                 :             : // Make a boolean expression.
    2297                 :             : 
    2298                 :             : Expression*
    2299                 :      594670 : Expression::make_boolean(bool val, Location location)
    2300                 :             : {
    2301                 :      594670 :   return new Boolean_expression(val, location);
    2302                 :             : }
    2303                 :             : 
    2304                 :             : // Class String_expression.
    2305                 :             : 
    2306                 :             : // Traverse a string expression.  We just need to traverse the type
    2307                 :             : // if there is one.
    2308                 :             : 
    2309                 :             : int
    2310                 :     7998378 : String_expression::do_traverse(Traverse* traverse)
    2311                 :             : {
    2312                 :     7998378 :   if (this->type_ != NULL)
    2313                 :     7103441 :     return Type::traverse(this->type_, traverse);
    2314                 :             :   return TRAVERSE_CONTINUE;
    2315                 :             : }
    2316                 :             : 
    2317                 :             : bool
    2318                 :   102798683 : String_expression::do_is_untyped(Type** ptype) const
    2319                 :             : {
    2320                 :   102798683 :   if (this->type_ != NULL)
    2321                 :        8255 :     return Expression::is_untyped_type(this->type_, ptype);
    2322                 :   102790428 :   *ptype = Type::make_string_type();
    2323                 :   102790428 :   return true;
    2324                 :             : }
    2325                 :             : 
    2326                 :             : // Get the type.
    2327                 :             : 
    2328                 :             : Type*
    2329                 :     5381971 : String_expression::do_type()
    2330                 :             : {
    2331                 :     5381971 :   if (this->type_ == NULL)
    2332                 :       17814 :     this->type_ = Type::make_string_type();
    2333                 :     5381971 :   return this->type_;
    2334                 :             : }
    2335                 :             : 
    2336                 :             : // Set the type from the context.
    2337                 :             : 
    2338                 :             : void
    2339                 :     2322867 : String_expression::do_determine_type(Gogo*, const Type_context* context)
    2340                 :             : {
    2341                 :     2322867 :   if (this->type_ != NULL && !this->type_->is_abstract())
    2342                 :             :     ;
    2343                 :     1586150 :   else if (context->type != NULL && context->type->is_string_type())
    2344                 :     1539569 :     this->type_ = context->type;
    2345                 :       46581 :   else if (!context->may_be_abstract)
    2346                 :       28914 :     this->type_ = Type::lookup_string_type();
    2347                 :     2322867 : }
    2348                 :             : 
    2349                 :             : // Build a string constant.
    2350                 :             : 
    2351                 :             : Bexpression*
    2352                 :     1010969 : String_expression::do_get_backend(Translate_context* context)
    2353                 :             : {
    2354                 :     1010969 :   Gogo* gogo = context->gogo();
    2355                 :     1010969 :   Btype* btype = Type::make_string_type()->get_backend(gogo);
    2356                 :             : 
    2357                 :     1010969 :   Location loc = this->location();
    2358                 :     1010969 :   std::vector<Bexpression*> init(2);
    2359                 :             : 
    2360                 :     1010969 :   if (this->val_.size() == 0)
    2361                 :       25731 :     init[0] = gogo->backend()->nil_pointer_expression();
    2362                 :             :   else
    2363                 :             :     {
    2364                 :      985238 :       Bexpression* str_cst =
    2365                 :      985238 :         gogo->backend()->string_constant_expression(this->val_);
    2366                 :      985238 :       init[0] = gogo->backend()->address_expression(str_cst, loc);
    2367                 :             :     }
    2368                 :             : 
    2369                 :     1010969 :   Btype* int_btype = Type::lookup_integer_type("int")->get_backend(gogo);
    2370                 :     1010969 :   mpz_t lenval;
    2371                 :     1010969 :   mpz_init_set_ui(lenval, this->val_.length());
    2372                 :     1010969 :   init[1] = gogo->backend()->integer_constant_expression(int_btype, lenval);
    2373                 :     1010969 :   mpz_clear(lenval);
    2374                 :             : 
    2375                 :     1010969 :   return gogo->backend()->constructor_expression(btype, init, loc);
    2376                 :     1010969 : }
    2377                 :             : 
    2378                 :             :  // Write string literal to string dump.
    2379                 :             : 
    2380                 :             : void
    2381                 :        6474 : String_expression::export_string(String_dump* exp,
    2382                 :             :                                  const String_expression* str)
    2383                 :             : {
    2384                 :        6474 :   std::string s;
    2385                 :        6474 :   s.reserve(str->val_.length() * 4 + 2);
    2386                 :        6474 :   s += '"';
    2387                 :      175078 :   for (std::string::const_iterator p = str->val_.begin();
    2388                 :      175078 :        p != str->val_.end();
    2389                 :      168604 :        ++p)
    2390                 :             :     {
    2391                 :      168604 :       if (*p == '\\' || *p == '"')
    2392                 :             :         {
    2393                 :        6998 :           s += '\\';
    2394                 :        6998 :           s += *p;
    2395                 :             :         }
    2396                 :      161606 :       else if (*p >= 0x20 && *p < 0x7f)
    2397                 :      120717 :         s += *p;
    2398                 :       40889 :       else if (*p == '\n')
    2399                 :        1036 :         s += "\\n";
    2400                 :       39853 :       else if (*p == '\t')
    2401                 :         360 :         s += "\\t";
    2402                 :             :       else
    2403                 :             :         {
    2404                 :       39493 :           s += "\\x";
    2405                 :       39493 :           unsigned char c = *p;
    2406                 :       39493 :           unsigned int dig = c >> 4;
    2407                 :       39493 :           s += dig < 10 ? '0' + dig : 'A' + dig - 10;
    2408                 :       39493 :           dig = c & 0xf;
    2409                 :       39493 :           s += dig < 10 ? '0' + dig : 'A' + dig - 10;
    2410                 :             :         }
    2411                 :             :     }
    2412                 :        6474 :   s += '"';
    2413                 :        6474 :   exp->write_string(s);
    2414                 :        6474 : }
    2415                 :             : 
    2416                 :             : // Export a string expression.
    2417                 :             : 
    2418                 :             : void
    2419                 :        6474 : String_expression::do_export(Export_function_body* efb) const
    2420                 :             : {
    2421                 :        6474 :   bool exported_type = Expression::export_constant_type(efb, this->type_);
    2422                 :        6474 :   String_expression::export_string(efb, this);
    2423                 :        6474 :   Expression::finish_export_constant_type(efb, exported_type);
    2424                 :        6474 : }
    2425                 :             : 
    2426                 :             : // Import a string expression.
    2427                 :             : 
    2428                 :             : Expression*
    2429                 :       31858 : String_expression::do_import(Import_expression* imp, Location loc)
    2430                 :             : {
    2431                 :       31858 :   imp->require_c_string("\"");
    2432                 :       31858 :   std::string val;
    2433                 :      633823 :   while (true)
    2434                 :             :     {
    2435                 :      633823 :       int c = imp->get_char();
    2436                 :      633823 :       if (c == '"' || c == -1)
    2437                 :             :         break;
    2438                 :      601965 :       if (c != '\\')
    2439                 :      555609 :         val += static_cast<char>(c);
    2440                 :             :       else
    2441                 :             :         {
    2442                 :       46356 :           c = imp->get_char();
    2443                 :       46356 :           if (c == '\\' || c == '"')
    2444                 :       19650 :             val += static_cast<char>(c);
    2445                 :       26706 :           else if (c == 'n')
    2446                 :        1074 :             val += '\n';
    2447                 :       25632 :           else if (c == 't')
    2448                 :         517 :             val += '\t';
    2449                 :       25115 :           else if (c == 'x')
    2450                 :             :             {
    2451                 :       25115 :               c = imp->get_char();
    2452                 :       25115 :               unsigned int vh = c >= '0' && c <= '9' ? c - '0' : c - 'A' + 10;
    2453                 :       25115 :               c = imp->get_char();
    2454                 :       25115 :               unsigned int vl = c >= '0' && c <= '9' ? c - '0' : c - 'A' + 10;
    2455                 :       25115 :               char v = (vh << 4) | vl;
    2456                 :       25115 :               val += v;
    2457                 :             :             }
    2458                 :             :           else
    2459                 :             :             {
    2460                 :           0 :               go_error_at(imp->location(), "bad string constant");
    2461                 :           0 :               return Expression::make_error(loc);
    2462                 :             :             }
    2463                 :             :         }
    2464                 :             :     }
    2465                 :       31858 :   return Expression::make_string(val, loc);
    2466                 :       31858 : }
    2467                 :             : 
    2468                 :             : // Ast dump for string expression.
    2469                 :             : 
    2470                 :             : void
    2471                 :           0 : String_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
    2472                 :             : {
    2473                 :           0 :   String_expression::export_string(ast_dump_context, this);
    2474                 :           0 : }
    2475                 :             : 
    2476                 :             : // Make a string expression with abstract string type (common case).
    2477                 :             : 
    2478                 :             : Expression*
    2479                 :     1559044 : Expression::make_string(const std::string& val, Location location)
    2480                 :             : {
    2481                 :     1559044 :   return new String_expression(val, NULL, location);
    2482                 :             : }
    2483                 :             : 
    2484                 :             : // Make a string expression with a specific string type.
    2485                 :             : 
    2486                 :             : Expression*
    2487                 :       38286 : Expression::make_string_typed(const std::string& val, Type* type, Location location)
    2488                 :             : {
    2489                 :       38286 :   return new String_expression(val, type, location);
    2490                 :             : }
    2491                 :             : 
    2492                 :             : // An expression that evaluates to some characteristic of a string.
    2493                 :             : // This is used when indexing, bound-checking, or nil checking a string.
    2494                 :             : 
    2495                 :             : class String_info_expression : public Expression
    2496                 :             : {
    2497                 :             :  public:
    2498                 :      441684 :   String_info_expression(Expression* string, String_info string_info,
    2499                 :             :                         Location location)
    2500                 :      441684 :     : Expression(EXPRESSION_STRING_INFO, location),
    2501                 :      883368 :       string_(string), string_info_(string_info)
    2502                 :             :   { }
    2503                 :             : 
    2504                 :             :  protected:
    2505                 :             :   Type*
    2506                 :             :   do_type();
    2507                 :             : 
    2508                 :             :   void
    2509                 :      755924 :   do_determine_type(Gogo*, const Type_context*)
    2510                 :      755924 :   { }
    2511                 :             : 
    2512                 :             :   Expression*
    2513                 :      140057 :   do_copy()
    2514                 :             :   {
    2515                 :      140057 :     return new String_info_expression(this->string_->copy(), this->string_info_,
    2516                 :      140057 :                                       this->location());
    2517                 :             :   }
    2518                 :             : 
    2519                 :             :   Bexpression*
    2520                 :             :   do_get_backend(Translate_context* context);
    2521                 :             : 
    2522                 :             :   void
    2523                 :             :   do_dump_expression(Ast_dump_context*) const;
    2524                 :             : 
    2525                 :             :   void
    2526                 :           0 :   do_issue_nil_check()
    2527                 :           0 :   { this->string_->issue_nil_check(); }
    2528                 :             : 
    2529                 :             :  private:
    2530                 :             :   // The string for which we are getting information.
    2531                 :             :   Expression* string_;
    2532                 :             :   // What information we want.
    2533                 :             :   String_info string_info_;
    2534                 :             : };
    2535                 :             : 
    2536                 :             : // Return the type of the string info.
    2537                 :             : 
    2538                 :             : Type*
    2539                 :     1103123 : String_info_expression::do_type()
    2540                 :             : {
    2541                 :     1103123 :   switch (this->string_info_)
    2542                 :             :     {
    2543                 :      610392 :     case STRING_INFO_DATA:
    2544                 :      610392 :       {
    2545                 :      610392 :         Type* byte_type = Type::lookup_integer_type("uint8");
    2546                 :      610392 :         return Type::make_pointer_type(byte_type);
    2547                 :             :       }
    2548                 :      492731 :     case STRING_INFO_LENGTH:
    2549                 :      492731 :         return Type::lookup_integer_type("int");
    2550                 :           0 :     default:
    2551                 :           0 :       go_unreachable();
    2552                 :             :     }
    2553                 :             : }
    2554                 :             : 
    2555                 :             : // Return string information in GENERIC.
    2556                 :             : 
    2557                 :             : Bexpression*
    2558                 :      443333 : String_info_expression::do_get_backend(Translate_context* context)
    2559                 :             : {
    2560                 :      443333 :   Gogo* gogo = context->gogo();
    2561                 :             : 
    2562                 :      443333 :   Bexpression* bstring = this->string_->get_backend(context);
    2563                 :      443333 :   switch (this->string_info_)
    2564                 :             :     {
    2565                 :      443333 :     case STRING_INFO_DATA:
    2566                 :      443333 :     case STRING_INFO_LENGTH:
    2567                 :      443333 :       return gogo->backend()->struct_field_expression(bstring,
    2568                 :             :                                                       this->string_info_,
    2569                 :      443333 :                                                       this->location());
    2570                 :           0 :       break;
    2571                 :           0 :     default:
    2572                 :           0 :       go_unreachable();
    2573                 :             :     }
    2574                 :             : }
    2575                 :             : 
    2576                 :             : // Dump ast representation for a type info expression.
    2577                 :             : 
    2578                 :             : void
    2579                 :           0 : String_info_expression::do_dump_expression(
    2580                 :             :     Ast_dump_context* ast_dump_context) const
    2581                 :             : {
    2582                 :           0 :   ast_dump_context->ostream() << "stringinfo(";
    2583                 :           0 :   this->string_->dump_expression(ast_dump_context);
    2584                 :           0 :   ast_dump_context->ostream() << ",";
    2585                 :           0 :   ast_dump_context->ostream() <<
    2586                 :           0 :       (this->string_info_ == STRING_INFO_DATA ? "data"
    2587                 :           0 :     : this->string_info_ == STRING_INFO_LENGTH ? "length"
    2588                 :           0 :     : "unknown");
    2589                 :           0 :   ast_dump_context->ostream() << ")";
    2590                 :           0 : }
    2591                 :             : 
    2592                 :             : // Make a string info expression.
    2593                 :             : 
    2594                 :             : Expression*
    2595                 :      301627 : Expression::make_string_info(Expression* string, String_info string_info,
    2596                 :             :                             Location location)
    2597                 :             : {
    2598                 :      301627 :   return new String_info_expression(string, string_info, location);
    2599                 :             : }
    2600                 :             : 
    2601                 :             : // An expression that represents an string value: a struct with value pointer
    2602                 :             : // and length fields.
    2603                 :             : 
    2604                 :             : class String_value_expression : public Expression
    2605                 :             : {
    2606                 :             :  public:
    2607                 :        1689 :   String_value_expression(Expression* valptr, Expression* len, Location location)
    2608                 :        1689 :       : Expression(EXPRESSION_STRING_VALUE, location),
    2609                 :        3378 :         valptr_(valptr), len_(len)
    2610                 :             :   { }
    2611                 :             : 
    2612                 :             :  protected:
    2613                 :             :   int
    2614                 :             :   do_traverse(Traverse*);
    2615                 :             : 
    2616                 :             :   Type*
    2617                 :           0 :   do_type()
    2618                 :           0 :   { return Type::make_string_type(); }
    2619                 :             : 
    2620                 :             :   void
    2621                 :           0 :   do_determine_type(Gogo*, const Type_context*)
    2622                 :           0 :   { go_unreachable(); }
    2623                 :             : 
    2624                 :             :   Expression*
    2625                 :           0 :   do_copy()
    2626                 :             :   {
    2627                 :           0 :     return new String_value_expression(this->valptr_->copy(),
    2628                 :           0 :                                        this->len_->copy(),
    2629                 :           0 :                                        this->location());
    2630                 :             :   }
    2631                 :             : 
    2632                 :             :   Bexpression*
    2633                 :             :   do_get_backend(Translate_context* context);
    2634                 :             : 
    2635                 :             :   void
    2636                 :             :   do_dump_expression(Ast_dump_context*) const;
    2637                 :             : 
    2638                 :             :  private:
    2639                 :             :   // The value pointer.
    2640                 :             :   Expression* valptr_;
    2641                 :             :   // The length.
    2642                 :             :   Expression* len_;
    2643                 :             : };
    2644                 :             : 
    2645                 :             : int
    2646                 :           0 : String_value_expression::do_traverse(Traverse* traverse)
    2647                 :             : {
    2648                 :           0 :   if (Expression::traverse(&this->valptr_, traverse) == TRAVERSE_EXIT
    2649                 :           0 :       || Expression::traverse(&this->len_, traverse) == TRAVERSE_EXIT)
    2650                 :           0 :     return TRAVERSE_EXIT;
    2651                 :             :   return TRAVERSE_CONTINUE;
    2652                 :             : }
    2653                 :             : 
    2654                 :             : Bexpression*
    2655                 :        1689 : String_value_expression::do_get_backend(Translate_context* context)
    2656                 :             : {
    2657                 :        1689 :   std::vector<Bexpression*> vals(2);
    2658                 :        1689 :   vals[0] = this->valptr_->get_backend(context);
    2659                 :        1689 :   vals[1] = this->len_->get_backend(context);
    2660                 :             : 
    2661                 :        1689 :   Gogo* gogo = context->gogo();
    2662                 :        1689 :   Btype* btype = Type::make_string_type()->get_backend(gogo);
    2663                 :        1689 :   return gogo->backend()->constructor_expression(btype, vals, this->location());
    2664                 :        1689 : }
    2665                 :             : 
    2666                 :             : void
    2667                 :           0 : String_value_expression::do_dump_expression(
    2668                 :             :     Ast_dump_context* ast_dump_context) const
    2669                 :             : {
    2670                 :           0 :   ast_dump_context->ostream() << "stringvalue(";
    2671                 :           0 :   ast_dump_context->ostream() << "value: ";
    2672                 :           0 :   this->valptr_->dump_expression(ast_dump_context);
    2673                 :           0 :   ast_dump_context->ostream() << ", length: ";
    2674                 :           0 :   this->len_->dump_expression(ast_dump_context);
    2675                 :           0 :   ast_dump_context->ostream() << ")";
    2676                 :           0 : }
    2677                 :             : 
    2678                 :             : Expression*
    2679                 :        1689 : Expression::make_string_value(Expression* valptr, Expression* len,
    2680                 :             :                               Location location)
    2681                 :             : {
    2682                 :        1689 :   return new String_value_expression(valptr, len, location);
    2683                 :             : }
    2684                 :             : 
    2685                 :             : // Make an integer expression.
    2686                 :             : 
    2687                 :             : class Integer_expression : public Expression
    2688                 :             : {
    2689                 :             :  public:
    2690                 :     8434163 :   Integer_expression(const mpz_t* val, Type* type, bool is_character_constant,
    2691                 :             :                      Location location)
    2692                 :     8434163 :     : Expression(EXPRESSION_INTEGER, location),
    2693                 :     8434163 :       type_(type), is_character_constant_(is_character_constant)
    2694                 :     8434163 :   { mpz_init_set(this->val_, *val); }
    2695                 :             : 
    2696                 :             :   static Expression*
    2697                 :             :   do_import(Import_expression*, Location);
    2698                 :             : 
    2699                 :             :   // Write VAL to string dump.
    2700                 :             :   static void
    2701                 :             :   export_integer(String_dump* exp, const mpz_t val);
    2702                 :             : 
    2703                 :             :   // Write VAL to dump context.
    2704                 :             :   static void
    2705                 :             :   dump_integer(Ast_dump_context* ast_dump_context, const mpz_t val);
    2706                 :             : 
    2707                 :             :  protected:
    2708                 :             :   int
    2709                 :             :   do_traverse(Traverse*);
    2710                 :             : 
    2711                 :             :   bool
    2712                 :     3453035 :   do_is_constant() const
    2713                 :     3453035 :   { return true; }
    2714                 :             : 
    2715                 :             :   bool
    2716                 :             :   do_is_untyped(Type**) const;
    2717                 :             : 
    2718                 :             :   bool
    2719                 :         448 :   do_is_zero_value() const
    2720                 :         448 :   { return mpz_sgn(this->val_) == 0; }
    2721                 :             : 
    2722                 :             :   bool
    2723                 :     1537209 :   do_is_static_initializer() const
    2724                 :     1537209 :   { return true; }
    2725                 :             : 
    2726                 :             :   bool
    2727                 :             :   do_numeric_constant_value(Numeric_constant* nc);
    2728                 :             : 
    2729                 :             :   Type*
    2730                 :             :   do_type();
    2731                 :             : 
    2732                 :             :   void
    2733                 :             :   do_determine_type(Gogo*, const Type_context* context);
    2734                 :             : 
    2735                 :             :   void
    2736                 :             :   do_check_types(Gogo*);
    2737                 :             : 
    2738                 :             :   Bexpression*
    2739                 :             :   do_get_backend(Translate_context*);
    2740                 :             : 
    2741                 :             :   Expression*
    2742                 :      478146 :   do_copy()
    2743                 :             :   {
    2744                 :      478146 :     if (this->is_character_constant_)
    2745                 :         241 :       return Expression::make_character(&this->val_,
    2746                 :         121 :                                         (this->type_ == NULL
    2747                 :             :                                          ? NULL
    2748                 :         120 :                                          : this->type_->copy_expressions()),
    2749                 :         121 :                                         this->location());
    2750                 :             :     else
    2751                 :      941062 :       return Expression::make_integer_z(&this->val_,
    2752                 :      478025 :                                         (this->type_ == NULL
    2753                 :             :                                          ? NULL
    2754                 :      463037 :                                          : this->type_->copy_expressions()),
    2755                 :      478025 :                                         this->location());
    2756                 :             :   }
    2757                 :             : 
    2758                 :             :   int
    2759                 :     1685981 :   do_inlining_cost() const
    2760                 :     1685981 :   { return 1; }
    2761                 :             : 
    2762                 :             :   void
    2763                 :             :   do_export(Export_function_body*) const;
    2764                 :             : 
    2765                 :             :   void
    2766                 :             :   do_dump_expression(Ast_dump_context*) const;
    2767                 :             : 
    2768                 :             :  private:
    2769                 :             :   // The integer value.
    2770                 :             :   mpz_t val_;
    2771                 :             :   // The type so far.
    2772                 :             :   Type* type_;
    2773                 :             :   // Whether this is a character constant.
    2774                 :             :   bool is_character_constant_;
    2775                 :             : };
    2776                 :             : 
    2777                 :             : // Traverse an integer expression.  We just need to traverse the type
    2778                 :             : // if there is one.
    2779                 :             : 
    2780                 :             : int
    2781                 :    50479054 : Integer_expression::do_traverse(Traverse* traverse)
    2782                 :             : {
    2783                 :    50479054 :   if (this->type_ != NULL)
    2784                 :    45693896 :     return Type::traverse(this->type_, traverse);
    2785                 :             :   return TRAVERSE_CONTINUE;
    2786                 :             : }
    2787                 :             : 
    2788                 :             : // Return a numeric constant for this expression.  We have to mark
    2789                 :             : // this as a character when appropriate.
    2790                 :             : 
    2791                 :             : bool
    2792                 :    26510459 : Integer_expression::do_numeric_constant_value(Numeric_constant* nc)
    2793                 :             : {
    2794                 :    26510459 :   if (this->is_character_constant_)
    2795                 :       37209 :     nc->set_rune(this->type_, this->val_);
    2796                 :             :   else
    2797                 :    26473250 :     nc->set_int(this->type_, this->val_);
    2798                 :    26510459 :   return true;
    2799                 :             : }
    2800                 :             : 
    2801                 :             : bool
    2802                 :     1504440 : Integer_expression::do_is_untyped(Type** ptype) const
    2803                 :             : {
    2804                 :     1504440 :   if (this->type_ != NULL)
    2805                 :     1194841 :     return Expression::is_untyped_type(this->type_, ptype);
    2806                 :      309599 :   if (this->is_character_constant_)
    2807                 :       16451 :     *ptype = Type::make_abstract_character_type();
    2808                 :             :   else
    2809                 :      293148 :     *ptype = Type::make_abstract_integer_type();
    2810                 :             :   return true;
    2811                 :             : }
    2812                 :             : 
    2813                 :             : // Return the current type.  If we haven't set the type yet, we return
    2814                 :             : // an abstract integer type.
    2815                 :             : 
    2816                 :             : Type*
    2817                 :    20199543 : Integer_expression::do_type()
    2818                 :             : {
    2819                 :    20199543 :   if (this->type_ == NULL)
    2820                 :             :     {
    2821                 :      787514 :       if (this->is_character_constant_)
    2822                 :        5997 :         this->type_ = Type::make_abstract_character_type();
    2823                 :             :       else
    2824                 :      781517 :         this->type_ = Type::make_abstract_integer_type();
    2825                 :             :     }
    2826                 :    20199543 :   return this->type_;
    2827                 :             : }
    2828                 :             : 
    2829                 :             : // Set the type of the integer value.  Here we may switch from an
    2830                 :             : // abstract type to a real type.
    2831                 :             : 
    2832                 :             : void
    2833                 :     6681864 : Integer_expression::do_determine_type(Gogo*, const Type_context* context)
    2834                 :             : {
    2835                 :     6681864 :   if (this->type_ != NULL && !this->type_->is_abstract())
    2836                 :             :     ;
    2837                 :     2460719 :   else if (context->type != NULL && context->type->is_numeric_type())
    2838                 :     1646879 :     this->type_ = context->type;
    2839                 :      813840 :   else if (!context->may_be_abstract)
    2840                 :             :     {
    2841                 :       41913 :       if (this->is_character_constant_)
    2842                 :         361 :         this->type_ = Type::lookup_integer_type("int32");
    2843                 :             :       else
    2844                 :       41552 :         this->type_ = Type::lookup_integer_type("int");
    2845                 :             :     }
    2846                 :     6681864 : }
    2847                 :             : 
    2848                 :             : // Check the type of an integer constant.
    2849                 :             : 
    2850                 :             : void
    2851                 :     2438643 : Integer_expression::do_check_types(Gogo*)
    2852                 :             : {
    2853                 :     2438643 :   Type* type = this->type_;
    2854                 :     2438643 :   if (type == NULL)
    2855                 :           1 :     return;
    2856                 :     2438642 :   Numeric_constant nc;
    2857                 :     2438642 :   if (this->is_character_constant_)
    2858                 :       54823 :     nc.set_rune(NULL, this->val_);
    2859                 :             :   else
    2860                 :     2383819 :     nc.set_int(NULL, this->val_);
    2861                 :     2438642 :   if (!nc.set_type(type, true, this->location()))
    2862                 :          28 :     this->set_is_error();
    2863                 :     2438642 : }
    2864                 :             : 
    2865                 :             : // Get the backend representation for an integer constant.
    2866                 :             : 
    2867                 :             : Bexpression*
    2868                 :     6025272 : Integer_expression::do_get_backend(Translate_context* context)
    2869                 :             : {
    2870                 :     6025272 :   if (this->is_error_expression()
    2871                 :     6025272 :       || (this->type_ != NULL && this->type_->is_error_type()))
    2872                 :             :     {
    2873                 :           0 :       go_assert(saw_errors());
    2874                 :           0 :       return context->gogo()->backend()->error_expression();
    2875                 :             :     }
    2876                 :             : 
    2877                 :     6025272 :   Type* resolved_type = NULL;
    2878                 :     6025272 :   if (this->type_ != NULL && !this->type_->is_abstract())
    2879                 :     5979764 :     resolved_type = this->type_;
    2880                 :       45508 :   else if (this->type_ != NULL && this->type_->float_type() != NULL)
    2881                 :             :     {
    2882                 :             :       // We are converting to an abstract floating point type.
    2883                 :           0 :       resolved_type = Type::lookup_float_type("float64");
    2884                 :             :     }
    2885                 :       45508 :   else if (this->type_ != NULL && this->type_->complex_type() != NULL)
    2886                 :             :     {
    2887                 :             :       // We are converting to an abstract complex type.
    2888                 :           0 :       resolved_type = Type::lookup_complex_type("complex128");
    2889                 :             :     }
    2890                 :             :   else
    2891                 :             :     {
    2892                 :             :       // If we still have an abstract type here, then this is being
    2893                 :             :       // used in a constant expression which didn't get reduced for
    2894                 :             :       // some reason.  Use a type which will fit the value.  We use <,
    2895                 :             :       // not <=, because we need an extra bit for the sign bit.
    2896                 :       45508 :       int bits = mpz_sizeinbase(this->val_, 2);
    2897                 :       45508 :       Type* int_type = Type::lookup_integer_type("int");
    2898                 :       91016 :       if (bits < int_type->integer_type()->bits())
    2899                 :             :         resolved_type = int_type;
    2900                 :           1 :       else if (bits < 64)
    2901                 :           0 :         resolved_type = Type::lookup_integer_type("int64");
    2902                 :             :       else
    2903                 :             :         {
    2904                 :           1 :           if (!saw_errors())
    2905                 :           1 :             go_error_at(this->location(), "integer constant overflow");
    2906                 :           1 :           return context->gogo()->backend()->error_expression();
    2907                 :             :         }
    2908                 :             :     }
    2909                 :     6025271 :   Numeric_constant nc;
    2910                 :     6025271 :   nc.set_int(resolved_type, this->val_);
    2911                 :     6025271 :   return Expression::backend_numeric_constant_expression(context, &nc);
    2912                 :     6025271 : }
    2913                 :             : 
    2914                 :             : // Write VAL to export data.
    2915                 :             : 
    2916                 :             : void
    2917                 :       54160 : Integer_expression::export_integer(String_dump* exp, const mpz_t val)
    2918                 :             : {
    2919                 :       54160 :   char* s = mpz_get_str(NULL, 10, val);
    2920                 :       54160 :   exp->write_c_string(s);
    2921                 :       54160 :   free(s);
    2922                 :       54160 : }
    2923                 :             : 
    2924                 :             : // Export an integer in a constant expression.
    2925                 :             : 
    2926                 :             : void
    2927                 :       54160 : Integer_expression::do_export(Export_function_body* efb) const
    2928                 :             : {
    2929                 :       54160 :   bool exported_type = Expression::export_constant_type(efb, this->type_);
    2930                 :             : 
    2931                 :       54160 :   Integer_expression::export_integer(efb, this->val_);
    2932                 :       54160 :   if (this->is_character_constant_)
    2933                 :        1421 :     efb->write_c_string("'");
    2934                 :             :   // A trailing space lets us reliably identify the end of the number.
    2935                 :       54160 :   efb->write_c_string(" ");
    2936                 :             : 
    2937                 :       54160 :   Expression::finish_export_constant_type(efb, exported_type);
    2938                 :       54160 : }
    2939                 :             : 
    2940                 :             : // Import an integer, floating point, or complex value.  This handles
    2941                 :             : // all these types because they all start with digits.
    2942                 :             : 
    2943                 :             : Expression*
    2944                 :      953779 : Integer_expression::do_import(Import_expression* imp, Location loc)
    2945                 :             : {
    2946                 :      953779 :   std::string num = imp->read_identifier();
    2947                 :      953779 :   imp->require_c_string(" ");
    2948                 :      953779 :   if (!num.empty() && num[num.length() - 1] == 'i')
    2949                 :             :     {
    2950                 :           3 :       mpfr_t real;
    2951                 :           3 :       size_t plus_pos = num.find('+', 1);
    2952                 :           3 :       size_t minus_pos = num.find('-', 1);
    2953                 :           3 :       size_t pos;
    2954                 :           3 :       if (plus_pos == std::string::npos)
    2955                 :             :         pos = minus_pos;
    2956                 :           2 :       else if (minus_pos == std::string::npos)
    2957                 :             :         pos = plus_pos;
    2958                 :             :       else
    2959                 :             :         {
    2960                 :           0 :           go_error_at(imp->location(), "bad number in import data: %qs",
    2961                 :             :                       num.c_str());
    2962                 :           0 :           return Expression::make_error(loc);
    2963                 :             :         }
    2964                 :           3 :       if (pos == std::string::npos)
    2965                 :           0 :         mpfr_init_set_ui(real, 0, MPFR_RNDN);
    2966                 :             :       else
    2967                 :             :         {
    2968                 :           3 :           std::string real_str = num.substr(0, pos);
    2969                 :           3 :           if (mpfr_init_set_str(real, real_str.c_str(), 10, MPFR_RNDN) != 0)
    2970                 :             :             {
    2971                 :           0 :               go_error_at(imp->location(), "bad number in import data: %qs",
    2972                 :             :                           real_str.c_str());
    2973                 :           0 :               return Expression::make_error(loc);
    2974                 :             :             }
    2975                 :           3 :         }
    2976                 :             : 
    2977                 :           3 :       std::string imag_str;
    2978                 :           3 :       if (pos == std::string::npos)
    2979                 :           0 :         imag_str = num;
    2980                 :             :       else
    2981                 :           3 :         imag_str = num.substr(pos);
    2982                 :           3 :       imag_str = imag_str.substr(0, imag_str.size() - 1);
    2983                 :           3 :       mpfr_t imag;
    2984                 :           3 :       if (mpfr_init_set_str(imag, imag_str.c_str(), 10, MPFR_RNDN) != 0)
    2985                 :             :         {
    2986                 :           0 :           go_error_at(imp->location(), "bad number in import data: %qs",
    2987                 :             :                       imag_str.c_str());
    2988                 :           0 :           return Expression::make_error(loc);
    2989                 :             :         }
    2990                 :           3 :       mpc_t cval;
    2991                 :           3 :       mpc_init2(cval, mpc_precision);
    2992                 :           3 :       mpc_set_fr_fr(cval, real, imag, MPC_RNDNN);
    2993                 :           3 :       mpfr_clear(real);
    2994                 :           3 :       mpfr_clear(imag);
    2995                 :           3 :       Expression* ret = Expression::make_complex(&cval, NULL, loc);
    2996                 :           3 :       mpc_clear(cval);
    2997                 :           3 :       return ret;
    2998                 :           3 :     }
    2999                 :      953776 :   else if (num.find('.') == std::string::npos
    3000                 :      953776 :            && num.find('E') == std::string::npos)
    3001                 :             :     {
    3002                 :      949523 :       bool is_character_constant = (!num.empty()
    3003                 :      949523 :                                     && num[num.length() - 1] == '\'');
    3004                 :        6646 :       if (is_character_constant)
    3005                 :        6646 :         num = num.substr(0, num.length() - 1);
    3006                 :      949523 :       mpz_t val;
    3007                 :      949523 :       if (mpz_init_set_str(val, num.c_str(), 10) != 0)
    3008                 :             :         {
    3009                 :           0 :           go_error_at(imp->location(), "bad number in import data: %qs",
    3010                 :             :                       num.c_str());
    3011                 :           0 :           return Expression::make_error(loc);
    3012                 :             :         }
    3013                 :      949523 :       Expression* ret;
    3014                 :      949523 :       if (is_character_constant)
    3015                 :        6646 :         ret = Expression::make_character(&val, NULL, loc);
    3016                 :             :       else
    3017                 :      942877 :         ret = Expression::make_integer_z(&val, NULL, loc);
    3018                 :      949523 :       mpz_clear(val);
    3019                 :      949523 :       return ret;
    3020                 :             :     }
    3021                 :             :   else
    3022                 :             :     {
    3023                 :        4253 :       mpfr_t val;
    3024                 :        4253 :       if (mpfr_init_set_str(val, num.c_str(), 10, MPFR_RNDN) != 0)
    3025                 :             :         {
    3026                 :           0 :           go_error_at(imp->location(), "bad number in import data: %qs",
    3027                 :             :                       num.c_str());
    3028                 :           0 :           return Expression::make_error(loc);
    3029                 :             :         }
    3030                 :        4253 :       Expression* ret = Expression::make_float(&val, NULL, loc);
    3031                 :        4253 :       mpfr_clear(val);
    3032                 :        4253 :       return ret;
    3033                 :             :     }
    3034                 :      953779 : }
    3035                 :             : // Ast dump for integer expression.
    3036                 :             : 
    3037                 :             : void
    3038                 :           0 : Integer_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
    3039                 :             : {
    3040                 :           0 :   if (this->is_character_constant_)
    3041                 :           0 :     ast_dump_context->ostream() << '\'';
    3042                 :           0 :   Integer_expression::export_integer(ast_dump_context, this->val_);
    3043                 :           0 :   if (this->is_character_constant_)
    3044                 :           0 :     ast_dump_context->ostream() << '\'';
    3045                 :           0 : }
    3046                 :             : 
    3047                 :             : // Build a new integer value from a multi-precision integer.
    3048                 :             : 
    3049                 :             : Expression*
    3050                 :     8373979 : Expression::make_integer_z(const mpz_t* val, Type* type, Location location)
    3051                 :             : {
    3052                 :     8373979 :   return new Integer_expression(val, type, false, location);
    3053                 :             : }
    3054                 :             : 
    3055                 :             : // Build a new integer value from an unsigned long.
    3056                 :             : 
    3057                 :             : Expression*
    3058                 :     4212971 : Expression::make_integer_ul(unsigned long val, Type *type, Location location)
    3059                 :             : {
    3060                 :     4212971 :   mpz_t zval;
    3061                 :     4212971 :   mpz_init_set_ui(zval, val);
    3062                 :     4212971 :   Expression* ret = Expression::make_integer_z(&zval, type, location);
    3063                 :     4212971 :   mpz_clear(zval);
    3064                 :     4212971 :   return ret;
    3065                 :             : }
    3066                 :             : 
    3067                 :             : // Build a new integer value from a signed long.
    3068                 :             : 
    3069                 :             : Expression*
    3070                 :       16515 : Expression::make_integer_sl(long val, Type *type, Location location)
    3071                 :             : {
    3072                 :       16515 :   mpz_t zval;
    3073                 :       16515 :   mpz_init_set_si(zval, val);
    3074                 :       16515 :   Expression* ret = Expression::make_integer_z(&zval, type, location);
    3075                 :       16515 :   mpz_clear(zval);
    3076                 :       16515 :   return ret;
    3077                 :             : }
    3078                 :             : 
    3079                 :             : // Store an int64_t in an uninitialized mpz_t.
    3080                 :             : 
    3081                 :             : static void
    3082                 :     1218892 : set_mpz_from_int64(mpz_t* zval, int64_t val)
    3083                 :             : {
    3084                 :     1218892 :   if (val >= 0)
    3085                 :             :     {
    3086                 :     1218892 :       unsigned long ul = static_cast<unsigned long>(val);
    3087                 :     1218892 :       if (static_cast<int64_t>(ul) == val)
    3088                 :             :         {
    3089                 :     1218892 :           mpz_init_set_ui(*zval, ul);
    3090                 :     1218892 :           return;
    3091                 :             :         }
    3092                 :             :     }
    3093                 :           0 :   uint64_t uv;
    3094                 :           0 :   if (val >= 0)
    3095                 :             :     uv = static_cast<uint64_t>(val);
    3096                 :             :   else
    3097                 :           0 :     uv = static_cast<uint64_t>(- val);
    3098                 :           0 :   unsigned long ul = uv & 0xffffffffUL;
    3099                 :           0 :   mpz_init_set_ui(*zval, ul);
    3100                 :           0 :   mpz_t hval;
    3101                 :           0 :   mpz_init_set_ui(hval, static_cast<unsigned long>(uv >> 32));
    3102                 :           0 :   mpz_mul_2exp(hval, hval, 32);
    3103                 :           0 :   mpz_add(*zval, *zval, hval);
    3104                 :           0 :   mpz_clear(hval);
    3105                 :           0 :   if (val < 0)
    3106                 :           0 :     mpz_neg(*zval, *zval);
    3107                 :             : }
    3108                 :             : 
    3109                 :             : // Build a new integer value from an int64_t.
    3110                 :             : 
    3111                 :             : Expression*
    3112                 :     1216766 : Expression::make_integer_int64(int64_t val, Type* type, Location location)
    3113                 :             : {
    3114                 :     1216766 :   mpz_t zval;
    3115                 :     1216766 :   set_mpz_from_int64(&zval, val);
    3116                 :     1216766 :   Expression* ret = Expression::make_integer_z(&zval, type, location);
    3117                 :     1216766 :   mpz_clear(zval);
    3118                 :     1216766 :   return ret;
    3119                 :             : }
    3120                 :             : 
    3121                 :             : // Build a new character constant value.
    3122                 :             : 
    3123                 :             : Expression*
    3124                 :       60184 : Expression::make_character(const mpz_t* val, Type* type, Location location)
    3125                 :             : {
    3126                 :       60184 :   return new Integer_expression(val, type, true, location);
    3127                 :             : }
    3128                 :             : 
    3129                 :             : // Floats.
    3130                 :             : 
    3131                 :             : class Float_expression : public Expression
    3132                 :             : {
    3133                 :             :  public:
    3134                 :       32243 :   Float_expression(const mpfr_t* val, Type* type, Location location)
    3135                 :       32243 :     : Expression(EXPRESSION_FLOAT, location),
    3136                 :       32243 :       type_(type)
    3137                 :             :   {
    3138                 :       32243 :     mpfr_init_set(this->val_, *val, MPFR_RNDN);
    3139                 :       32243 :   }
    3140                 :             : 
    3141                 :             :   // Write VAL to export data.
    3142                 :             :   static void
    3143                 :             :   export_float(String_dump* exp, const mpfr_t val);
    3144                 :             : 
    3145                 :             :   // Write VAL to dump file.
    3146                 :             :   static void
    3147                 :             :   dump_float(Ast_dump_context* ast_dump_context, const mpfr_t val);
    3148                 :             : 
    3149                 :             :  protected:
    3150                 :             :   int
    3151                 :             :   do_traverse(Traverse*);
    3152                 :             : 
    3153                 :             :   bool
    3154                 :       17418 :   do_is_constant() const
    3155                 :       17418 :   { return true; }
    3156                 :             : 
    3157                 :             :   bool
    3158                 :             :   do_is_untyped(Type**) const;
    3159                 :             : 
    3160                 :             :   bool
    3161                 :           4 :   do_is_zero_value() const
    3162                 :             :   {
    3163                 :           4 :     return mpfr_zero_p(this->val_) != 0
    3164                 :           4 :            && mpfr_signbit(this->val_) == 0;
    3165                 :             :   }
    3166                 :             : 
    3167                 :             :   bool
    3168                 :       13963 :   do_is_static_initializer() const
    3169                 :       13963 :   { return true; }
    3170                 :             : 
    3171                 :             :   bool
    3172                 :       20066 :   do_numeric_constant_value(Numeric_constant* nc)
    3173                 :             :   {
    3174                 :       20066 :     nc->set_float(this->type_, this->val_);
    3175                 :       20066 :     return true;
    3176                 :             :   }
    3177                 :             : 
    3178                 :             :   Type*
    3179                 :             :   do_type();
    3180                 :             : 
    3181                 :             :   void
    3182                 :             :   do_determine_type(Gogo*, const Type_context*);
    3183                 :             : 
    3184                 :             :   void
    3185                 :             :   do_check_types(Gogo*);
    3186                 :             : 
    3187                 :             :   Expression*
    3188                 :          73 :   do_copy()
    3189                 :         144 :   { return Expression::make_float(&this->val_,
    3190                 :          73 :                                   (this->type_ == NULL
    3191                 :             :                                    ? NULL
    3192                 :          71 :                                    : this->type_->copy_expressions()),
    3193                 :          73 :                                   this->location()); }
    3194                 :             : 
    3195                 :             :   Bexpression*
    3196                 :             :   do_get_backend(Translate_context*);
    3197                 :             : 
    3198                 :             :   int
    3199                 :        4765 :   do_inlining_cost() const
    3200                 :        4765 :   { return 1; }
    3201                 :             : 
    3202                 :             :   void
    3203                 :             :   do_export(Export_function_body*) const;
    3204                 :             : 
    3205                 :             :   void
    3206                 :             :   do_dump_expression(Ast_dump_context*) const;
    3207                 :             : 
    3208                 :             :  private:
    3209                 :             :   // The floating point value.
    3210                 :             :   mpfr_t val_;
    3211                 :             :   // The type so far.
    3212                 :             :   Type* type_;
    3213                 :             : };
    3214                 :             : 
    3215                 :             : // Traverse a float expression.  We just need to traverse the type if
    3216                 :             : // there is one.
    3217                 :             : 
    3218                 :             : int
    3219                 :      431792 : Float_expression::do_traverse(Traverse* traverse)
    3220                 :             : {
    3221                 :      431792 :   if (this->type_ != NULL)
    3222                 :      385194 :     return Type::traverse(this->type_, traverse);
    3223                 :             :   return TRAVERSE_CONTINUE;
    3224                 :             : }
    3225                 :             : 
    3226                 :             : bool
    3227                 :        7828 : Float_expression::do_is_untyped(Type** ptype) const
    3228                 :             : {
    3229                 :        7828 :   if (this->type_ != NULL)
    3230                 :         130 :     return Expression::is_untyped_type(this->type_, ptype);
    3231                 :        7698 :   *ptype = Type::make_abstract_float_type();
    3232                 :        7698 :   return true;
    3233                 :             : }
    3234                 :             : 
    3235                 :             : // Return the current type.  If we haven't set the type yet, we return
    3236                 :             : // an abstract float type.
    3237                 :             : 
    3238                 :             : Type*
    3239                 :      149905 : Float_expression::do_type()
    3240                 :             : {
    3241                 :      149905 :   if (this->type_ == NULL)
    3242                 :        8272 :     this->type_ = Type::make_abstract_float_type();
    3243                 :      149905 :   return this->type_;
    3244                 :             : }
    3245                 :             : 
    3246                 :             : // Set the type of the float value.  Here we may switch from an
    3247                 :             : // abstract type to a real type.
    3248                 :             : 
    3249                 :             : void
    3250                 :       31815 : Float_expression::do_determine_type(Gogo*, const Type_context* context)
    3251                 :             : {
    3252                 :       31815 :   if (this->type_ != NULL && !this->type_->is_abstract())
    3253                 :             :     ;
    3254                 :       26734 :   else if (context->type != NULL
    3255                 :       26734 :            && (context->type->integer_type() != NULL
    3256                 :       33750 :                || context->type->float_type() != NULL
    3257                 :       18531 :                || context->type->complex_type() != NULL))
    3258                 :       17567 :     this->type_ = context->type;
    3259                 :        9167 :   else if (!context->may_be_abstract)
    3260                 :         895 :     this->type_ = Type::lookup_float_type("float64");
    3261                 :       31815 : }
    3262                 :             : 
    3263                 :             : // Check the type of a float value.
    3264                 :             : 
    3265                 :             : void
    3266                 :       26516 : Float_expression::do_check_types(Gogo*)
    3267                 :             : {
    3268                 :       26516 :   Type* type = this->type_;
    3269                 :       26516 :   if (type == NULL)
    3270                 :           1 :     return;
    3271                 :       26515 :   Numeric_constant nc;
    3272                 :       26515 :   nc.set_float(NULL, this->val_);
    3273                 :       26515 :   if (!nc.set_type(this->type_, true, this->location()))
    3274                 :          55 :     this->set_is_error();
    3275                 :       26515 : }
    3276                 :             : 
    3277                 :             : // Get the backend representation for a float constant.
    3278                 :             : 
    3279                 :             : Bexpression*
    3280                 :       20531 : Float_expression::do_get_backend(Translate_context* context)
    3281                 :             : {
    3282                 :       20531 :   if (this->is_error_expression()
    3283                 :       20531 :       || (this->type_ != NULL && this->type_->is_error_type()))
    3284                 :             :     {
    3285                 :           0 :       go_assert(saw_errors());
    3286                 :           0 :       return context->gogo()->backend()->error_expression();
    3287                 :             :     }
    3288                 :             : 
    3289                 :       20531 :   Type* resolved_type;
    3290                 :       20531 :   if (this->type_ != NULL && !this->type_->is_abstract())
    3291                 :       20512 :     resolved_type = this->type_;
    3292                 :          19 :   else if (this->type_ != NULL && this->type_->integer_type() != NULL)
    3293                 :             :     {
    3294                 :             :       // We have an abstract integer type.  We just hope for the best.
    3295                 :           0 :       resolved_type = Type::lookup_integer_type("int");
    3296                 :             :     }
    3297                 :          19 :   else if (this->type_ != NULL && this->type_->complex_type() != NULL)
    3298                 :             :     {
    3299                 :             :       // We are converting to an abstract complex type.
    3300                 :           0 :       resolved_type = Type::lookup_complex_type("complex128");
    3301                 :             :     }
    3302                 :             :   else
    3303                 :             :     {
    3304                 :             :       // If we still have an abstract type here, then this is being
    3305                 :             :       // used in a constant expression which didn't get reduced.  We
    3306                 :             :       // just use float64 and hope for the best.
    3307                 :          19 :       resolved_type = Type::lookup_float_type("float64");
    3308                 :             :     }
    3309                 :             : 
    3310                 :       20531 :   Numeric_constant nc;
    3311                 :       20531 :   nc.set_float(resolved_type, this->val_);
    3312                 :       20531 :   return Expression::backend_numeric_constant_expression(context, &nc);
    3313                 :       20531 : }
    3314                 :             : 
    3315                 :             : // Write a floating point number to a string dump.
    3316                 :             : 
    3317                 :             : void
    3318                 :         593 : Float_expression::export_float(String_dump *exp, const mpfr_t val)
    3319                 :             : {
    3320                 :         593 :   mpfr_exp_t exponent;
    3321                 :         593 :   char* s = mpfr_get_str(NULL, &exponent, 10, 0, val, MPFR_RNDN);
    3322                 :         593 :   if (*s == '-')
    3323                 :          53 :     exp->write_c_string("-");
    3324                 :         593 :   exp->write_c_string("0.");
    3325                 :         593 :   exp->write_c_string(*s == '-' ? s + 1 : s);
    3326                 :         593 :   mpfr_free_str(s);
    3327                 :         593 :   char buf[30];
    3328                 :         593 :   snprintf(buf, sizeof buf, "E%ld", exponent);
    3329                 :         593 :   exp->write_c_string(buf);
    3330                 :         593 : }
    3331                 :             : 
    3332                 :             : // Export a floating point number in a constant expression.
    3333                 :             : 
    3334                 :             : void
    3335                 :         549 : Float_expression::do_export(Export_function_body* efb) const
    3336                 :             : {
    3337                 :         549 :   bool exported_type = Expression::export_constant_type(efb, this->type_);
    3338                 :             : 
    3339                 :         549 :   Float_expression::export_float(efb, this->val_);
    3340                 :             :   // A trailing space lets us reliably identify the end of the number.
    3341                 :         549 :   efb->write_c_string(" ");
    3342                 :             : 
    3343                 :         549 :   Expression::finish_export_constant_type(efb, exported_type);
    3344                 :         549 : }
    3345                 :             : 
    3346                 :             : // Dump a floating point number to the dump file.
    3347                 :             : 
    3348                 :             : void
    3349                 :           0 : Float_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
    3350                 :             : {
    3351                 :           0 :   Float_expression::export_float(ast_dump_context, this->val_);
    3352                 :           0 : }
    3353                 :             : 
    3354                 :             : // Make a float expression.
    3355                 :             : 
    3356                 :             : Expression*
    3357                 :       32243 : Expression::make_float(const mpfr_t* val, Type* type, Location location)
    3358                 :             : {
    3359                 :       32243 :   return new Float_expression(val, type, location);
    3360                 :             : }
    3361                 :             : 
    3362                 :             : // Complex numbers.
    3363                 :             : 
    3364                 :             : class Complex_expression : public Expression
    3365                 :             : {
    3366                 :             :  public:
    3367                 :        3600 :   Complex_expression(const mpc_t* val, Type* type, Location location)
    3368                 :        3600 :     : Expression(EXPRESSION_COMPLEX, location),
    3369                 :        3600 :       type_(type)
    3370                 :             :   {
    3371                 :        3600 :     mpc_init2(this->val_, mpc_precision);
    3372                 :        3600 :     mpc_set(this->val_, *val, MPC_RNDNN);
    3373                 :        3600 :   }
    3374                 :             : 
    3375                 :             :   // Write VAL to string dump.
    3376                 :             :   static void
    3377                 :             :   export_complex(String_dump* exp, const mpc_t val);
    3378                 :             : 
    3379                 :             :   // Write REAL/IMAG to dump context.
    3380                 :             :   static void
    3381                 :             :   dump_complex(Ast_dump_context* ast_dump_context, const mpc_t val);
    3382                 :             : 
    3383                 :             :  protected:
    3384                 :             :   int
    3385                 :             :   do_traverse(Traverse*);
    3386                 :             : 
    3387                 :             :   bool
    3388                 :        1594 :   do_is_constant() const
    3389                 :        1594 :   { return true; }
    3390                 :             : 
    3391                 :             :   bool
    3392                 :             :   do_is_untyped(Type**) const;
    3393                 :             : 
    3394                 :             :   bool
    3395                 :           4 :   do_is_zero_value() const
    3396                 :             :   {
    3397                 :           4 :     return mpfr_zero_p(mpc_realref(this->val_)) != 0
    3398                 :           0 :            && mpfr_signbit(mpc_realref(this->val_)) == 0
    3399                 :           0 :            && mpfr_zero_p(mpc_imagref(this->val_)) != 0
    3400                 :           4 :            && mpfr_signbit(mpc_imagref(this->val_)) == 0;
    3401                 :             :   }
    3402                 :             : 
    3403                 :             :   bool
    3404                 :        1688 :   do_is_static_initializer() const
    3405                 :        1688 :   { return true; }
    3406                 :             : 
    3407                 :             :   bool
    3408                 :        1251 :   do_numeric_constant_value(Numeric_constant* nc)
    3409                 :             :   {
    3410                 :        1251 :     nc->set_complex(this->type_, this->val_);
    3411                 :        1251 :     return true;
    3412                 :             :   }
    3413                 :             : 
    3414                 :             :   Type*
    3415                 :             :   do_type();
    3416                 :             : 
    3417                 :             :   void
    3418                 :             :   do_determine_type(Gogo*, const Type_context*);
    3419                 :             : 
    3420                 :             :   void
    3421                 :             :   do_check_types(Gogo*);
    3422                 :             : 
    3423                 :             :   Expression*
    3424                 :          12 :   do_copy()
    3425                 :             :   {
    3426                 :          24 :     return Expression::make_complex(&this->val_,
    3427                 :          12 :                                     (this->type_ == NULL
    3428                 :             :                                      ? NULL
    3429                 :          12 :                                      : this->type_->copy_expressions()),
    3430                 :          12 :                                     this->location());
    3431                 :             :   }
    3432                 :             : 
    3433                 :             :   Bexpression*
    3434                 :             :   do_get_backend(Translate_context*);
    3435                 :             : 
    3436                 :             :   int
    3437                 :         125 :   do_inlining_cost() const
    3438                 :         125 :   { return 2; }
    3439                 :             : 
    3440                 :             :   void
    3441                 :             :   do_export(Export_function_body*) const;
    3442                 :             : 
    3443                 :             :   void
    3444                 :             :   do_dump_expression(Ast_dump_context*) const;
    3445                 :             : 
    3446                 :             :  private:
    3447                 :             :   // The complex value.
    3448                 :             :   mpc_t val_;
    3449                 :             :   // The type if known.
    3450                 :             :   Type* type_;
    3451                 :             : };
    3452                 :             : 
    3453                 :             : // Traverse a complex expression.  We just need to traverse the type
    3454                 :             : // if there is one.
    3455                 :             : 
    3456                 :             : int
    3457                 :       43990 : Complex_expression::do_traverse(Traverse* traverse)
    3458                 :             : {
    3459                 :       43990 :   if (this->type_ != NULL)
    3460                 :       42004 :     return Type::traverse(this->type_, traverse);
    3461                 :             :   return TRAVERSE_CONTINUE;
    3462                 :             : }
    3463                 :             : 
    3464                 :             : bool
    3465                 :         945 : Complex_expression::do_is_untyped(Type** ptype) const
    3466                 :             : {
    3467                 :         945 :   if (this->type_ != NULL)
    3468                 :          25 :     return Expression::is_untyped_type(this->type_, ptype);
    3469                 :         920 :   *ptype = Type::make_abstract_complex_type();
    3470                 :         920 :   return true;
    3471                 :             : }
    3472                 :             : 
    3473                 :             : // Return the current type.  If we haven't set the type yet, we return
    3474                 :             : // an abstract complex type.
    3475                 :             : 
    3476                 :             : Type*
    3477                 :       18583 : Complex_expression::do_type()
    3478                 :             : {
    3479                 :       18583 :   if (this->type_ == NULL)
    3480                 :          21 :     this->type_ = Type::make_abstract_complex_type();
    3481                 :       18583 :   return this->type_;
    3482                 :             : }
    3483                 :             : 
    3484                 :             : // Set the type of the complex value.  Here we may switch from an
    3485                 :             : // abstract type to a real type.
    3486                 :             : 
    3487                 :             : void
    3488                 :        5406 : Complex_expression::do_determine_type(Gogo*, const Type_context* context)
    3489                 :             : {
    3490                 :        5406 :   if (this->type_ != NULL && !this->type_->is_abstract())
    3491                 :             :     ;
    3492                 :        1837 :   else if (context->type != NULL && context->type->is_numeric_type())
    3493                 :        1768 :     this->type_ = context->type;
    3494                 :          69 :   else if (!context->may_be_abstract)
    3495                 :          48 :     this->type_ = Type::lookup_complex_type("complex128");
    3496                 :        5406 : }
    3497                 :             : 
    3498                 :             : // Check the type of a complex value.
    3499                 :             : 
    3500                 :             : void
    3501                 :        1854 : Complex_expression::do_check_types(Gogo*)
    3502                 :             : {
    3503                 :        1854 :   Type* type = this->type_;
    3504                 :        1854 :   if (type == NULL)
    3505                 :           0 :     return;
    3506                 :        1854 :   Numeric_constant nc;
    3507                 :        1854 :   nc.set_complex(NULL, this->val_);
    3508                 :        1854 :   if (!nc.set_type(this->type_, true, this->location()))
    3509                 :           4 :     this->set_is_error();
    3510                 :        1854 : }
    3511                 :             : 
    3512                 :             : // Get the backend representation for a complex constant.
    3513                 :             : 
    3514                 :             : Bexpression*
    3515                 :        2529 : Complex_expression::do_get_backend(Translate_context* context)
    3516                 :             : {
    3517                 :        2529 :   if (this->is_error_expression()
    3518                 :        2529 :       || (this->type_ != NULL && this->type_->is_error_type()))
    3519                 :             :     {
    3520                 :           0 :       go_assert(saw_errors());
    3521                 :           0 :       return context->gogo()->backend()->error_expression();
    3522                 :             :     }
    3523                 :             : 
    3524                 :        2529 :   Type* resolved_type;
    3525                 :        2529 :   if (this->type_ != NULL && !this->type_->is_abstract())
    3526                 :        2507 :     resolved_type = this->type_;
    3527                 :          22 :   else if (this->type_ != NULL && this->type_->integer_type() != NULL)
    3528                 :             :     {
    3529                 :             :       // We are converting to an abstract integer type.
    3530                 :           0 :       resolved_type = Type::lookup_integer_type("int");
    3531                 :             :     }
    3532                 :          22 :   else if (this->type_ != NULL && this->type_->float_type() != NULL)
    3533                 :             :     {
    3534                 :             :       // We are converting to an abstract float type.
    3535                 :           0 :       resolved_type = Type::lookup_float_type("float64");
    3536                 :             :     }
    3537                 :             :   else
    3538                 :             :     {
    3539                 :             :       // If we still have an abstract type here, this is being
    3540                 :             :       // used in a constant expression which didn't get reduced.  We
    3541                 :             :       // just use complex128 and hope for the best.
    3542                 :          22 :       resolved_type = Type::lookup_complex_type("complex128");
    3543                 :             :     }
    3544                 :             : 
    3545                 :        2529 :   Numeric_constant nc;
    3546                 :        2529 :   nc.set_complex(resolved_type, this->val_);
    3547                 :        2529 :   return Expression::backend_numeric_constant_expression(context, &nc);
    3548                 :        2529 : }
    3549                 :             : 
    3550                 :             : // Write REAL/IMAG to export data.
    3551                 :             : 
    3552                 :             : void
    3553                 :          25 : Complex_expression::export_complex(String_dump* exp, const mpc_t val)
    3554                 :             : {
    3555                 :          25 :   if (!mpfr_zero_p(mpc_realref(val)))
    3556                 :             :     {
    3557                 :          19 :       Float_expression::export_float(exp, mpc_realref(val));
    3558                 :          19 :       if (mpfr_sgn(mpc_imagref(val)) >= 0)
    3559                 :          14 :         exp->write_c_string("+");
    3560                 :             :     }
    3561                 :          25 :   Float_expression::export_float(exp, mpc_imagref(val));
    3562                 :          25 :   exp->write_c_string("i");
    3563                 :          25 : }
    3564                 :             : 
    3565                 :             : // Export a complex number in a constant expression.
    3566                 :             : 
    3567                 :             : void
    3568                 :          25 : Complex_expression::do_export(Export_function_body* efb) const
    3569                 :             : {
    3570                 :          25 :   bool exported_type = Expression::export_constant_type(efb, this->type_);
    3571                 :             : 
    3572                 :          25 :   Complex_expression::export_complex(efb, this->val_);
    3573                 :             :   // A trailing space lets us reliably identify the end of the number.
    3574                 :          25 :   efb->write_c_string(" ");
    3575                 :             : 
    3576                 :          25 :   Expression::finish_export_constant_type(efb, exported_type);
    3577                 :          25 : }
    3578                 :             : 
    3579                 :             : // Dump a complex expression to the dump file.
    3580                 :             : 
    3581                 :             : void
    3582                 :           0 : Complex_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
    3583                 :             : {
    3584                 :           0 :   Complex_expression::export_complex(ast_dump_context, this->val_);
    3585                 :           0 : }
    3586                 :             : 
    3587                 :             : // Make a complex expression.
    3588                 :             : 
    3589                 :             : Expression*
    3590                 :        3600 : Expression::make_complex(const mpc_t* val, Type* type, Location location)
    3591                 :             : {
    3592                 :        3600 :   return new Complex_expression(val, type, location);
    3593                 :             : }
    3594                 :             : 
    3595                 :             : // Find a named object in an expression.
    3596                 :             : 
    3597                 :      415472 : class Find_named_object : public Traverse
    3598                 :             : {
    3599                 :             :  public:
    3600                 :      415472 :   Find_named_object(Named_object* no)
    3601                 :      415472 :     : Traverse(traverse_expressions),
    3602                 :      415472 :       no_(no), found_(false)
    3603                 :             :   { }
    3604                 :             : 
    3605                 :             :   // Whether we found the object.
    3606                 :             :   bool
    3607                 :      415472 :   found() const
    3608                 :      415472 :   { return this->found_; }
    3609                 :             : 
    3610                 :             :  protected:
    3611                 :             :   int
    3612                 :             :   expression(Expression**);
    3613                 :             : 
    3614                 :             :  private:
    3615                 :             :   // The object we are looking for.
    3616                 :             :   Named_object* no_;
    3617                 :             :   // Whether we found it.
    3618                 :             :   bool found_;
    3619                 :             : };
    3620                 :             : 
    3621                 :             : // Class Const_expression.
    3622                 :             : 
    3623                 :             : // Traversal.
    3624                 :             : 
    3625                 :             : int
    3626                 :     7778532 : Const_expression::do_traverse(Traverse* traverse)
    3627                 :             : {
    3628                 :     7778532 :   if (this->type_ != NULL)
    3629                 :     7436253 :     return Type::traverse(this->type_, traverse);
    3630                 :             :   return TRAVERSE_CONTINUE;
    3631                 :             : }
    3632                 :             : 
    3633                 :             : // Whether this is the zero value.
    3634                 :             : 
    3635                 :             : bool
    3636                 :          32 : Const_expression::do_is_zero_value() const
    3637                 :             : {
    3638                 :          32 :   return this->constant_->const_value()->expr()->is_zero_value();
    3639                 :             : }
    3640                 :             : 
    3641                 :             : // Lower a constant expression.  This is where we convert the
    3642                 :             : // predeclared constant iota into an integer value.
    3643                 :             : 
    3644                 :             : Expression*
    3645                 :     1201990 : Const_expression::do_lower(Gogo* gogo, Named_object*, Statement_inserter*)
    3646                 :             : {
    3647                 :     1201990 :   Location loc = this->location();
    3648                 :             : 
    3649                 :     1201990 :   if (this->is_error_expression())
    3650                 :         874 :     return Expression::make_error(loc);
    3651                 :     1201116 :   if (this->constant_->const_value()->expr()->is_error_expression())
    3652                 :         191 :     return Expression::make_error(loc);
    3653                 :             : 
    3654                 :     1200925 :   if (this->is_iota_)
    3655                 :       10365 :     return Expression::make_integer_ul(this->iota_value_, NULL, loc);
    3656                 :             : 
    3657                 :             :   // Make sure that the constant itself has been lowered.
    3658                 :     1190560 :   gogo->lower_constant(this->constant_);
    3659                 :             : 
    3660                 :     1190560 :   return this;
    3661                 :             : }
    3662                 :             : 
    3663                 :             : // Return a numeric constant value.
    3664                 :             : 
    3665                 :             : bool
    3666                 :      548657 : Const_expression::do_numeric_constant_value(Numeric_constant* nc)
    3667                 :             : {
    3668                 :      548657 :   if (this->seen_)
    3669                 :             :     return false;
    3670                 :             : 
    3671                 :      548657 :   Type* ctype;
    3672                 :      548657 :   if (this->type_ != NULL)
    3673                 :             :     ctype = this->type_;
    3674                 :             :   else
    3675                 :         231 :     ctype = this->constant_->const_value()->type();
    3676                 :             : 
    3677                 :      548657 :   if (this->is_iota_)
    3678                 :             :     {
    3679                 :       54473 :       nc->set_unsigned_long(ctype,
    3680                 :       54473 :                             static_cast<unsigned long>(this->iota_value_));
    3681                 :       54473 :       return true;
    3682                 :             :     }
    3683                 :             : 
    3684                 :      494184 :   Expression* e = this->constant_->const_value()->expr();
    3685                 :             : 
    3686                 :      494184 :   this->seen_ = true;
    3687                 :             : 
    3688                 :      494184 :   bool r = e->numeric_constant_value(nc);
    3689                 :             : 
    3690                 :      494184 :   this->seen_ = false;
    3691                 :             : 
    3692                 :      494184 :   if (r && ctype != NULL)
    3693                 :             :     {
    3694                 :      480908 :       if (!nc->set_type(ctype, false, this->location()))
    3695                 :             :         return false;
    3696                 :             :     }
    3697                 :             : 
    3698                 :             :   return r;
    3699                 :             : }
    3700                 :             : 
    3701                 :             : bool
    3702                 :       10533 : Const_expression::do_string_constant_value(std::string* val)
    3703                 :             : {
    3704                 :       10533 :   if (this->seen_)
    3705                 :             :     return false;
    3706                 :       10533 :   if (this->is_iota_)
    3707                 :             :     return false;
    3708                 :             : 
    3709                 :       10533 :   Expression* e = this->constant_->const_value()->expr();
    3710                 :             : 
    3711                 :       10533 :   this->seen_ = true;
    3712                 :       10533 :   bool ok = e->string_constant_value(val);
    3713                 :       10533 :   this->seen_ = false;
    3714                 :             : 
    3715                 :       10533 :   return ok;
    3716                 :             : }
    3717                 :             : 
    3718                 :             : bool
    3719                 :        6822 : Const_expression::do_boolean_constant_value(bool* val)
    3720                 :             : {
    3721                 :        6822 :   if (this->seen_)
    3722                 :             :     return false;
    3723                 :        6822 :   if (this->is_iota_)
    3724                 :             :     return false;
    3725                 :             : 
    3726                 :        6822 :   Expression* e = this->constant_->const_value()->expr();
    3727                 :             : 
    3728                 :        6822 :   this->seen_ = true;
    3729                 :        6822 :   bool ok = e->boolean_constant_value(val);
    3730                 :        6822 :   this->seen_ = false;
    3731                 :             : 
    3732                 :        6822 :   return ok;
    3733                 :             : }
    3734                 :             : 
    3735                 :             : // Whether this is untyped.
    3736                 :             : 
    3737                 :             : bool
    3738                 :       87834 : Const_expression::do_is_untyped(Type** ptype) const
    3739                 :             : {
    3740                 :       87834 :   if (this->type_ != NULL)
    3741                 :       16888 :     return Expression::is_untyped_type(this->type_, ptype);
    3742                 :             : 
    3743                 :       70946 :   Named_constant* nc = this->constant_->const_value();
    3744                 :       70946 :   if (nc->type() != NULL)
    3745                 :       67877 :     return Expression::is_untyped_type(nc->type(), ptype);
    3746                 :             : 
    3747                 :        3069 :   return nc->expr()->is_untyped(ptype);
    3748                 :             : }
    3749                 :             : 
    3750                 :             : // Return the type of the const reference.
    3751                 :             : 
    3752                 :             : Type*
    3753                 :     3896486 : Const_expression::do_type()
    3754                 :             : {
    3755                 :     3896486 :   if (this->type_ == NULL)
    3756                 :             :     {
    3757                 :           0 :       go_assert(saw_errors());
    3758                 :           0 :       return Type::make_error_type();
    3759                 :             :     }
    3760                 :             : 
    3761                 :             :   return this->type_;
    3762                 :             : }
    3763                 :             : 
    3764                 :             : // Set the type of the const reference.
    3765                 :             : 
    3766                 :             : void
    3767                 :      644458 : Const_expression::do_determine_type(Gogo* gogo, const Type_context* context)
    3768                 :             : {
    3769                 :      644458 :   if (this->type_ != NULL)
    3770                 :             :     return;
    3771                 :             : 
    3772                 :             :   // The type may depend on the type of other constants.  Avoid an
    3773                 :             :   // endless loop.
    3774                 :      426095 :   if (this->seen_)
    3775                 :             :     {
    3776                 :           0 :       if (!saw_errors())
    3777                 :           0 :         go_error_at(this->location(), "constant refers to itself");
    3778                 :           0 :       this->set_is_error();
    3779                 :           0 :       this->type_ = Type::make_error_type();
    3780                 :           0 :       return;
    3781                 :             :     }
    3782                 :             : 
    3783                 :      426095 :   this->seen_ = true;
    3784                 :             : 
    3785                 :      426095 :   Named_constant* nc = this->constant_->const_value();
    3786                 :      426095 :   nc->determine_type(gogo);
    3787                 :             : 
    3788                 :      426095 :   Type* ctype = nc->type();
    3789                 :             : 
    3790                 :      426095 :   this->seen_ = false;
    3791                 :             : 
    3792                 :      426095 :   if (ctype == NULL)
    3793                 :             :     {
    3794                 :           2 :       go_error_at(nc->expr()->location(), "constant refers to itself");
    3795                 :           2 :       this->set_is_error();
    3796                 :           2 :       this->type_ = Type::make_error_type();
    3797                 :             :     }
    3798                 :      426093 :   else if (!ctype->is_abstract())
    3799                 :      224088 :     this->type_ = ctype;
    3800                 :      202005 :   else if (context->type != NULL
    3801                 :      148876 :            && context->type->is_numeric_type()
    3802                 :      270003 :            && ctype->is_numeric_type())
    3803                 :       67983 :     this->type_ = context->type;
    3804                 :      134022 :   else if (context->type != NULL
    3805                 :       80893 :            && context->type->is_string_type()
    3806                 :      141560 :            && ctype->is_string_type())
    3807                 :        7283 :     this->type_ = context->type;
    3808                 :      126739 :   else if (context->type != NULL
    3809                 :       73610 :            && context->type->is_boolean_type()
    3810                 :      198385 :            && ctype->is_boolean_type())
    3811                 :       71646 :     this->type_ = context->type;
    3812                 :       55093 :   else if (!context->may_be_abstract)
    3813                 :             :     {
    3814                 :        9022 :       if (ctype->is_abstract())
    3815                 :        9022 :         ctype = ctype->make_non_abstract_type();
    3816                 :        9022 :       this->type_ = ctype;
    3817                 :             :     }
    3818                 :             :   else
    3819                 :       46071 :     this->type_ = ctype;
    3820                 :             : }
    3821                 :             : 
    3822                 :             : // Check for a loop in which the initializer of a constant refers to
    3823                 :             : // the constant itself.
    3824                 :             : 
    3825                 :             : void
    3826                 :      415472 : Const_expression::check_for_init_loop()
    3827                 :             : {
    3828                 :      415472 :   if (this->is_error_expression())
    3829                 :           0 :     return;
    3830                 :      415472 :   if (this->type_ != NULL && this->type_->is_error())
    3831                 :             :     return;
    3832                 :      415472 :   if (this->constant_->const_value()->expr()->is_error_expression())
    3833                 :             :     {
    3834                 :           0 :       this->set_is_error();
    3835                 :           0 :       return;
    3836                 :             :     }
    3837                 :             : 
    3838                 :      415472 :   if (this->seen_)
    3839                 :             :     {
    3840                 :           0 :       this->report_error(_("constant refers to itself"));
    3841                 :           0 :       this->type_ = Type::make_error_type();
    3842                 :           0 :       return;
    3843                 :             :     }
    3844                 :             : 
    3845                 :      415472 :   Expression* init = this->constant_->const_value()->expr();
    3846                 :      415472 :   Find_named_object find_named_object(this->constant_);
    3847                 :             : 
    3848                 :      415472 :   this->seen_ = true;
    3849                 :      415472 :   Expression::traverse(&init, &find_named_object);
    3850                 :      415472 :   this->seen_ = false;
    3851                 :             : 
    3852                 :      415472 :   if (find_named_object.found())
    3853                 :             :     {
    3854                 :           0 :       if (this->type_ == NULL || !this->type_->is_error())
    3855                 :             :         {
    3856                 :           0 :           this->report_error(_("constant refers to itself"));
    3857                 :           0 :           this->type_ = Type::make_error_type();
    3858                 :             :         }
    3859                 :           0 :       return;
    3860                 :             :     }
    3861                 :      415472 : }
    3862                 :             : 
    3863                 :             : // Set the iota value if this is a reference to iota.
    3864                 :             : 
    3865                 :             : void
    3866                 :       45463 : Const_expression::set_iota_value(int iota_value)
    3867                 :             : {
    3868                 :       45463 :   Named_constant* nc = this->constant_->const_value();
    3869                 :       45463 :   if (nc->expr()->classification() == EXPRESSION_IOTA)
    3870                 :             :     {
    3871                 :       10363 :       this->is_iota_ = true;
    3872                 :       10363 :       this->iota_value_ = iota_value;
    3873                 :             :     }
    3874                 :       45463 : }
    3875                 :             : 
    3876                 :             : // Check types of a const reference.
    3877                 :             : 
    3878                 :             : void
    3879                 :      425840 : Const_expression::do_check_types(Gogo*)
    3880                 :             : {
    3881                 :      425840 :   if (this->is_error_expression())
    3882                 :             :     return;
    3883                 :      425840 :   if (this->type_ != NULL && this->type_->is_error())
    3884                 :             :     return;
    3885                 :      425838 :   if (this->constant_->const_value()->expr()->is_error_expression())
    3886                 :             :     {
    3887                 :           1 :       this->set_is_error();
    3888                 :           1 :       return;
    3889                 :             :     }
    3890                 :             : 
    3891                 :      425837 :   Expression* expr = this->constant_->const_value()->expr();
    3892                 :      425837 :   if (expr->classification() == EXPRESSION_IOTA && !this->is_iota_)
    3893                 :             :     {
    3894                 :           4 :       go_error_at(this->location(),
    3895                 :             :                   "iota is only defined in const declarations");
    3896                 :             :       // Avoid knock-on errors.
    3897                 :           4 :       this->is_iota_ = true;
    3898                 :           4 :       this->iota_value_ = 0;
    3899                 :             :     }
    3900                 :             : 
    3901                 :      425837 :   if (this->is_iota_ && this->type_->is_numeric_type())
    3902                 :             :     {
    3903                 :       10365 :       Numeric_constant nc;
    3904                 :       10365 :       nc.set_unsigned_long(Type::make_abstract_integer_type(),
    3905                 :       10365 :                            static_cast<unsigned long>(this->iota_value_));
    3906                 :       10365 :       if (!nc.set_type(this->type_, true, this->location()))
    3907                 :           0 :         this->set_is_error();
    3908                 :       10365 :       return;
    3909                 :       10365 :     }
    3910                 :             : 
    3911                 :      415472 :   this->check_for_init_loop();
    3912                 :             : 
    3913                 :             :   // Check that numeric constant fits in type.
    3914                 :      415472 :   if (this->type_->is_numeric_type())
    3915                 :             :     {
    3916                 :      189245 :       Numeric_constant nc;
    3917                 :      189245 :       if (expr->numeric_constant_value(&nc))
    3918                 :             :         {
    3919                 :      188519 :           if (!nc.set_type(this->type_, true, this->location()))
    3920                 :         873 :             this->set_is_error();
    3921                 :             :         }
    3922                 :      189245 :     }
    3923                 :             : }
    3924                 :             : 
    3925                 :             : // Return the backend representation for a const reference.
    3926                 :             : 
    3927                 :             : Bexpression*
    3928                 :      259212 : Const_expression::do_get_backend(Translate_context* context)
    3929                 :             : {
    3930                 :      259212 :   if (this->is_error_expression()
    3931                 :      259212 :       || (this->type_ != NULL && this->type_->is_error()))
    3932                 :             :     {
    3933                 :           0 :       go_assert(saw_errors());
    3934                 :           0 :       return context->backend()->error_expression();
    3935                 :             :     }
    3936                 :             : 
    3937                 :      259212 :   go_assert(!this->is_iota_);
    3938                 :             : 
    3939                 :             :   // If the type has been set for this expression, but the underlying
    3940                 :             :   // object is an abstract int or float, we try to get the abstract
    3941                 :             :   // value.  Otherwise we may lose something in the conversion.
    3942                 :      259212 :   Expression* expr = this->constant_->const_value()->expr();
    3943                 :      259212 :   if (this->type_ != NULL
    3944                 :      235813 :       && this->type_->is_numeric_type()
    3945                 :      403440 :       && (this->constant_->const_value()->type() == NULL
    3946                 :      144228 :           || this->constant_->const_value()->type()->is_abstract()))
    3947                 :             :     {
    3948                 :       82314 :       Numeric_constant nc;
    3949                 :       82314 :       if (expr->numeric_constant_value(&nc)
    3950                 :       82314 :           && nc.set_type(this->type_, false, this->location()))
    3951                 :             :         {
    3952                 :       82314 :           Expression* e = nc.expression(this->location());
    3953                 :       82314 :           return e->get_backend(context);
    3954                 :             :         }
    3955                 :       82314 :     }
    3956                 :             : 
    3957                 :      176898 :   if (this->type_ != NULL)
    3958                 :      153499 :     expr = Expression::make_cast(this->type_, expr, this->location());
    3959                 :      176898 :   return expr->get_backend(context);
    3960                 :             : }
    3961                 :             : 
    3962                 :             : // When exporting a reference to a const as part of a const
    3963                 :             : // expression, we export the value.  We ignore the fact that it has
    3964                 :             : // a name.
    3965                 :             : 
    3966                 :             : void
    3967                 :       29410 : Const_expression::do_export(Export_function_body* efb) const
    3968                 :             : {
    3969                 :       29410 :   this->constant_->const_value()->expr()->export_expression(efb);
    3970                 :       29410 : }
    3971                 :             : 
    3972                 :             : // Dump ast representation for constant expression.
    3973                 :             : 
    3974                 :             : void
    3975                 :           0 : Const_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
    3976                 :             : {
    3977                 :           0 :   ast_dump_context->ostream() << this->constant_->name();
    3978                 :           0 : }
    3979                 :             : 
    3980                 :             : // Make a reference to a constant in an expression.
    3981                 :             : 
    3982                 :             : Expression*
    3983                 :      449605 : Expression::make_const_reference(Named_object* constant,
    3984                 :             :                                  Location location)
    3985                 :             : {
    3986                 :      449605 :   return new Const_expression(constant, location);
    3987                 :             : }
    3988                 :             : 
    3989                 :             : // Find a named object in an expression.
    3990                 :             : 
    3991                 :             : int
    3992                 :      661415 : Find_named_object::expression(Expression** pexpr)
    3993                 :             : {
    3994                 :      661415 :   switch ((*pexpr)->classification())
    3995                 :             :     {
    3996                 :       74842 :     case Expression::EXPRESSION_CONST_REFERENCE:
    3997                 :       74842 :       {
    3998                 :       74842 :         Const_expression* ce = static_cast<Const_expression*>(*pexpr);
    3999                 :       74842 :         if (ce->named_object() == this->no_)
    4000                 :             :           break;
    4001                 :             :         return TRAVERSE_CONTINUE;
    4002                 :             :       }
    4003                 :             : 
    4004                 :         173 :     case Expression::EXPRESSION_VAR_REFERENCE:
    4005                 :         173 :       if ((*pexpr)->var_expression()->named_object() == this->no_)
    4006                 :             :         break;
    4007                 :             :       return TRAVERSE_CONTINUE;
    4008                 :        1414 :     case Expression::EXPRESSION_FUNC_REFERENCE:
    4009                 :        1414 :       if ((*pexpr)->func_expression()->named_object() == this->no_)
    4010                 :             :         break;
    4011                 :             :       return TRAVERSE_CONTINUE;
    4012                 :             :     case Expression::EXPRESSION_ERROR:
    4013                 :             :       return TRAVERSE_EXIT;
    4014                 :             :     default:
    4015                 :             :       return TRAVERSE_CONTINUE;
    4016                 :             :     }
    4017                 :           0 :   this->found_ = true;
    4018                 :           0 :   return TRAVERSE_EXIT;
    4019                 :             : }
    4020                 :             : 
    4021                 :             : // The nil value.
    4022                 :             : 
    4023                 :             : class Nil_expression : public Expression
    4024                 :             : {
    4025                 :             :  public:
    4026                 :     2347819 :   Nil_expression(Location location)
    4027                 :     4695638 :     : Expression(EXPRESSION_NIL, location)
    4028                 :             :   { }
    4029                 :             : 
    4030                 :             :   static Expression*
    4031                 :             :   do_import(Import_expression*, Location);
    4032                 :             : 
    4033                 :             :  protected:
    4034                 :             :   bool
    4035                 :       59824 :   do_is_constant() const
    4036                 :       59824 :   { return true; }
    4037                 :             : 
    4038                 :             :   bool
    4039                 :             :   do_untyped_type(Type** ptype) const
    4040                 :             :   {
    4041                 :             :     *ptype = Type::make_nil_type();
    4042                 :             :     return true;
    4043                 :             :   }
    4044                 :             : 
    4045                 :             :   bool
    4046                 :          18 :   do_is_zero_value() const
    4047                 :          18 :   { return true; }
    4048                 :             : 
    4049                 :             :   bool
    4050                 :      592317 :   do_is_static_initializer() const
    4051                 :      592317 :   { return true; }
    4052                 :             : 
    4053                 :             :   Type*
    4054                 :     2018406 :   do_type()
    4055                 :     2018406 :   { return Type::make_nil_type(); }
    4056                 :             : 
    4057                 :             :   void
    4058                 :     2945880 :   do_determine_type(Gogo*, const Type_context*)
    4059                 :     2945880 :   { }
    4060                 :             : 
    4061                 :             :   Expression*
    4062                 :           0 :   do_copy()
    4063                 :           0 :   { return this; }
    4064                 :             : 
    4065                 :             :   Bexpression*
    4066                 :     1202197 :   do_get_backend(Translate_context* context)
    4067                 :     1202197 :   { return context->backend()->nil_pointer_expression(); }
    4068                 :             : 
    4069                 :             :   int
    4070                 :       17529 :   do_inlining_cost() const
    4071                 :       17529 :   { return 1; }
    4072                 :             : 
    4073                 :             :   void
    4074                 :        1456 :   do_export(Export_function_body* efb) const
    4075                 :        1456 :   { efb->write_c_string("$nil"); }
    4076                 :             : 
    4077                 :             :   void
    4078                 :           0 :   do_dump_expression(Ast_dump_context* ast_dump_context) const
    4079                 :           0 :   { ast_dump_context->ostream() << "nil"; }
    4080                 :             : };
    4081                 :             : 
    4082                 :             : // Import a nil expression.
    4083                 :             : 
    4084                 :             : Expression*
    4085                 :         621 : Nil_expression::do_import(Import_expression* imp, Location loc)
    4086                 :             : {
    4087                 :         621 :   if (imp->version() >= EXPORT_FORMAT_V3)
    4088                 :         621 :     imp->require_c_string("$");
    4089                 :         621 :   imp->require_c_string("nil");
    4090                 :         621 :   return Expression::make_nil(loc);
    4091                 :             : }
    4092                 :             : 
    4093                 :             : // Make a nil expression.
    4094                 :             : 
    4095                 :             : Expression*
    4096                 :     2347819 : Expression::make_nil(Location location)
    4097                 :             : {
    4098                 :     2347819 :   return new Nil_expression(location);
    4099                 :             : }
    4100                 :             : 
    4101                 :             : // The value of the predeclared constant iota.  This is little more
    4102                 :             : // than a marker.  This will be lowered to an integer in
    4103                 :             : // Const_expression::do_lower, which is where we know the value that
    4104                 :             : // it should have.
    4105                 :             : 
    4106                 :             : class Iota_expression : public Parser_expression
    4107                 :             : {
    4108                 :             :  public:
    4109                 :        4642 :   Iota_expression(Location location)
    4110                 :        4642 :     : Parser_expression(EXPRESSION_IOTA, location)
    4111                 :        4642 :   { }
    4112                 :             : 
    4113                 :             :  protected:
    4114                 :             :   Type*
    4115                 :           0 :   do_type()
    4116                 :           0 :   { return Type::make_abstract_integer_type(); }
    4117                 :             : 
    4118                 :             :   void
    4119                 :         471 :   do_determine_type(Gogo*, const Type_context*)
    4120                 :         471 :   { }
    4121                 :             : 
    4122                 :             :   Expression*
    4123                 :           0 :   do_lower(Gogo*, Named_object*, Statement_inserter*)
    4124                 :           0 :   { go_unreachable(); }
    4125                 :             : 
    4126                 :             :   // There should only ever be one of these.
    4127                 :             :   Expression*
    4128                 :           0 :   do_copy()
    4129                 :           0 :   { go_unreachable(); }
    4130                 :             : 
    4131                 :             :   void
    4132                 :           0 :   do_dump_expression(Ast_dump_context* ast_dump_context) const
    4133                 :           0 :   { ast_dump_context->ostream() << "iota"; }
    4134                 :             : };
    4135                 :             : 
    4136                 :             : // Make an iota expression.  This is only called for one case: the
    4137                 :             : // value of the predeclared constant iota.
    4138                 :             : 
    4139                 :             : Expression*
    4140                 :        4642 : Expression::make_iota()
    4141                 :             : {
    4142                 :        4642 :   static Iota_expression iota_expression(Linemap::unknown_location());
    4143                 :        4642 :   return &iota_expression;
    4144                 :             : }
    4145                 :             : 
    4146                 :             : // Class Type_conversion_expression.
    4147                 :             : 
    4148                 :             : // Traversal.
    4149                 :             : 
    4150                 :             : int
    4151                 :     6426934 : Type_conversion_expression::do_traverse(Traverse* traverse)
    4152                 :             : {
    4153                 :     6426934 :   if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT
    4154                 :     6426934 :       || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
    4155                 :       75690 :     return TRAVERSE_EXIT;
    4156                 :             :   return TRAVERSE_CONTINUE;
    4157                 :             : }
    4158                 :             : 
    4159                 :             : // Return the type of the expression.
    4160                 :             : 
    4161                 :             : Type*
    4162                 :     6938753 : Type_conversion_expression::do_type()
    4163                 :             : {
    4164                 :     6938753 :   if (this->is_error_expression() || this->expr_->is_error_expression())
    4165                 :         259 :     return Type::make_error_type();
    4166                 :     6938494 :   return this->type_;
    4167                 :             : }
    4168                 :             : 
    4169                 :             : // Convert to a constant at lowering time.  Also lower conversions
    4170                 :             : // from slice to pointer-to-array, as they can panic.
    4171                 :             : 
    4172                 :             : Expression*
    4173                 :      463859 : Type_conversion_expression::do_lower(Gogo* gogo, Named_object*,
    4174                 :             :                                      Statement_inserter* inserter)
    4175                 :             : {
    4176                 :      463859 :   Type* type = this->type_;
    4177                 :      463859 :   Expression* val = this->expr_;
    4178                 :      463859 :   Location location = this->location();
    4179                 :             : 
    4180                 :      463859 :   if (type->is_numeric_type())
    4181                 :             :     {
    4182                 :      293860 :       Numeric_constant nc;
    4183                 :      293860 :       if (val->numeric_constant_value(&nc))
    4184                 :             :         {
    4185                 :       31348 :           if (!nc.set_type(type, true, location))
    4186                 :          19 :             return Expression::make_error(location);
    4187                 :       31329 :           return nc.expression(location);
    4188                 :             :         }
    4189                 :      293860 :     }
    4190                 :             : 
    4191                 :             :   // According to the language specification on string conversions
    4192                 :             :   // (http://golang.org/ref/spec#Conversions_to_and_from_a_string_type):
    4193                 :             :   // When converting an integer into a string, the string will be a UTF-8
    4194                 :             :   // representation of the integer and integers "outside the range of valid
    4195                 :             :   // Unicode code points are converted to '\uFFFD'."
    4196                 :      432511 :   if (type->is_string_type())
    4197                 :             :     {
    4198                 :       27230 :       Numeric_constant nc;
    4199                 :       27230 :       if (val->numeric_constant_value(&nc) && nc.is_int())
    4200                 :             :         {
    4201                 :             :           // An integer value doesn't fit in the Unicode code point range if it
    4202                 :             :           // overflows the Go "int" type or is negative.
    4203                 :         418 :           unsigned long ul;
    4204                 :         418 :           if (!nc.set_type(Type::lookup_integer_type("int"), false, location)
    4205                 :         418 :               || nc.to_unsigned_long(&ul) == Numeric_constant::NC_UL_NEGATIVE)
    4206                 :           6 :             return Expression::make_string("\ufffd", location);
    4207                 :             :         }
    4208                 :       27230 :     }
    4209                 :             : 
    4210                 :      432505 :   if (type->is_slice_type())
    4211                 :             :     {
    4212                 :       25664 :       Type* element_type = type->array_type()->element_type()->forwarded();
    4213                 :       24656 :       bool is_byte = (element_type->integer_type() != NULL
    4214                 :       11824 :                       && element_type->integer_type()->is_byte());
    4215                 :       24656 :       bool is_rune = (element_type->integer_type() != NULL
    4216                 :       11824 :                       && element_type->integer_type()->is_rune());
    4217                 :       12832 :       if (is_byte || is_rune)
    4218                 :             :         {
    4219                 :        9918 :           std::string s;
    4220                 :        9918 :           if (val->string_constant_value(&s))
    4221                 :             :             {
    4222                 :        4887 :               Expression_list* vals = new Expression_list();
    4223                 :        4887 :               if (is_byte)
    4224                 :             :                 {
    4225                 :        4831 :                   for (std::string::const_iterator p = s.begin();
    4226                 :      164092 :                        p != s.end();
    4227                 :      159261 :                        p++)
    4228                 :             :                     {
    4229                 :      159261 :                       unsigned char c = static_cast<unsigned char>(*p);
    4230                 :      159261 :                       vals->push_back(Expression::make_integer_ul(c,
    4231                 :             :                                                                   element_type,
    4232                 :             :                                                                   location));
    4233                 :             :                     }
    4234                 :             :                 }
    4235                 :             :               else
    4236                 :             :                 {
    4237                 :          56 :                   const char *p = s.data();
    4238                 :          56 :                   const char *pend = s.data() + s.length();
    4239                 :         609 :                   while (p < pend)
    4240                 :             :                     {
    4241                 :         553 :                       unsigned int c;
    4242                 :         553 :                       int adv = Lex::fetch_char(p, &c);
    4243                 :         553 :                       if (adv == 0)
    4244                 :             :                         {
    4245                 :           4 :                           go_warning_at(this->location(), 0,
    4246                 :             :                                      "invalid UTF-8 encoding");
    4247                 :           4 :                           adv = 1;
    4248                 :             :                         }
    4249                 :         553 :                       p += adv;
    4250                 :         553 :                       vals->push_back(Expression::make_integer_ul(c,
    4251                 :             :                                                                   element_type,
    4252                 :             :                                                                   location));
    4253                 :             :                     }
    4254                 :             :                 }
    4255                 :             : 
    4256                 :        4887 :               return Expression::make_slice_composite_literal(type, vals,
    4257                 :             :                                                               location);
    4258                 :             :             }
    4259                 :        9918 :         }
    4260                 :             :     }
    4261                 :             : 
    4262                 :      427618 :   if (type->points_to() != NULL
    4263                 :      120945 :       && type->points_to()->array_type() != NULL
    4264                 :        3503 :       && !type->points_to()->is_slice_type()
    4265                 :        1545 :       && val->type()->is_slice_type()
    4266                 :      427710 :       && Type::are_identical(type->points_to()->array_type()->element_type(),
    4267                 :          92 :                              val->type()->array_type()->element_type(),
    4268                 :             :                              0, NULL))
    4269                 :             :     {
    4270                 :          46 :       Temporary_statement* val_temp = NULL;
    4271                 :          46 :       if (!val->is_multi_eval_safe())
    4272                 :             :         {
    4273                 :          25 :           val_temp = Statement::make_temporary(val->type(), NULL, location);
    4274                 :          25 :           inserter->insert(val_temp);
    4275                 :          25 :           val = Expression::make_set_and_use_temporary(val_temp, val,
    4276                 :             :                                                        location);
    4277                 :             :         }
    4278                 :             : 
    4279                 :          46 :       Type* int_type = Type::lookup_integer_type("int");
    4280                 :          46 :       Temporary_statement* vallen_temp =
    4281                 :          46 :         Statement::make_temporary(int_type, NULL, location);
    4282                 :          46 :       inserter->insert(vallen_temp);
    4283                 :             : 
    4284                 :          92 :       Expression* arrlen = type->points_to()->array_type()->length();
    4285                 :          46 :       Expression* vallen =
    4286                 :          46 :         Expression::make_slice_info(val, Expression::SLICE_INFO_LENGTH,
    4287                 :             :                                     location);
    4288                 :          46 :       vallen = Expression::make_set_and_use_temporary(vallen_temp, vallen,
    4289                 :             :                                                       location);
    4290                 :          46 :       Expression* cond = Expression::make_binary(OPERATOR_GT, arrlen, vallen,
    4291                 :             :                                                  location);
    4292                 :             : 
    4293                 :          46 :       vallen = Expression::make_temporary_reference(vallen_temp, location);
    4294                 :          46 :       Expression* panic = Runtime::make_call(gogo,
    4295                 :             :                                              Runtime::PANIC_SLICE_CONVERT,
    4296                 :             :                                              location, 2, arrlen, vallen);
    4297                 :             : 
    4298                 :          46 :       Expression* nil = Expression::make_nil(location);
    4299                 :          46 :       Expression* check = Expression::make_conditional(cond, panic, nil,
    4300                 :             :                                                        location);
    4301                 :             : 
    4302                 :          46 :       if (val_temp == NULL)
    4303                 :          21 :         val = val->copy();
    4304                 :             :       else
    4305                 :          25 :         val = Expression::make_temporary_reference(val_temp, location);
    4306                 :          46 :       Expression* ptr =
    4307                 :          46 :         Expression::make_slice_info(val, Expression::SLICE_INFO_VALUE_POINTER,
    4308                 :             :                                     location);
    4309                 :          46 :       ptr = Expression::make_unsafe_cast(type, ptr, location);
    4310                 :             : 
    4311                 :          46 :       Expression* ret = Expression::make_compound(check, ptr, location);
    4312                 :          46 :       ret->determine_type_no_context(gogo);
    4313                 :          46 :       return ret;
    4314                 :             :     }
    4315                 :             : 
    4316                 :      427572 :   return this;
    4317                 :             : }
    4318                 :             : 
    4319                 :             : // Flatten a type conversion by using a temporary variable for the slice
    4320                 :             : // in slice to string conversions.
    4321                 :             : 
    4322                 :             : Expression*
    4323                 :      846696 : Type_conversion_expression::do_flatten(Gogo*, Named_object*,
    4324                 :             :                                        Statement_inserter* inserter)
    4325                 :             : {
    4326                 :      846696 :   if (this->type()->is_error_type() || this->expr_->is_error_expression())
    4327                 :             :     {
    4328                 :         723 :       go_assert(saw_errors());
    4329                 :         723 :       return Expression::make_error(this->location());
    4330                 :             :     }
    4331                 :             : 
    4332                 :      845973 :   if (((this->type()->is_string_type()
    4333                 :       22867 :         && this->expr_->type()->is_slice_type())
    4334                 :      840222 :        || this->expr_->type()->interface_type() != NULL)
    4335                 :      882966 :       && !this->expr_->is_multi_eval_safe())
    4336                 :             :     {
    4337                 :        7268 :       Temporary_statement* temp =
    4338                 :        7268 :           Statement::make_temporary(NULL, this->expr_, this->location());
    4339                 :        7268 :       inserter->insert(temp);
    4340                 :        7268 :       this->expr_ = Expression::make_temporary_reference(temp, this->location());
    4341                 :             :     }
    4342                 :             : 
    4343                 :             :   // For interface conversion and string to/from slice conversions,
    4344                 :             :   // decide if we can allocate on stack.
    4345                 :     1438158 :   if (this->type()->interface_type() != NULL
    4346                 :      592185 :       || this->type()->is_string_type()
    4347                 :      569318 :       || this->expr_->type()->is_string_type())
    4348                 :             :     {
    4349                 :      278526 :       Node* n = Node::make_node(this);
    4350                 :      278526 :       if ((n->encoding() & ESCAPE_MASK) == Node::ESCAPE_NONE)
    4351                 :       10302 :         this->no_escape_ = true;
    4352                 :             :     }
    4353                 :      845973 :   return this;
    4354                 :             : }
    4355                 :             : 
    4356                 :             : // Return whether a type conversion is a constant.
    4357                 :             : 
    4358                 :             : bool
    4359                 :      380574 : Type_conversion_expression::do_is_constant() const
    4360                 :             : {
    4361                 :      380574 :   if (!this->expr_->is_constant())
    4362                 :             :     return false;
    4363                 :             : 
    4364                 :             :   // A conversion to a type that may not be used as a constant is not
    4365                 :             :   // a constant.  For example, []byte(nil).
    4366                 :       63191 :   Type* type = this->type_;
    4367                 :       85345 :   if (type->integer_type() == NULL
    4368                 :       21995 :       && type->float_type() == NULL
    4369                 :       21982 :       && type->complex_type() == NULL
    4370                 :       21982 :       && !type->is_boolean_type()
    4371                 :       21885 :       && !type->is_string_type())
    4372                 :             :     return false;
    4373                 :             : 
    4374                 :             :   return true;
    4375                 :             : }
    4376                 :             : 
    4377                 :             : // Return whether a type conversion is a zero value.
    4378                 :             : 
    4379                 :             : bool
    4380                 :         183 : Type_conversion_expression::do_is_zero_value() const
    4381                 :             : {
    4382                 :         183 :   if (!this->expr_->is_zero_value())
    4383                 :             :     return false;
    4384                 :             : 
    4385                 :             :   // Some type conversion from zero value is still not zero value.
    4386                 :             :   // For example, []byte("") or interface{}(0).
    4387                 :             :   // Conservatively, only report true if the RHS is nil.
    4388                 :           0 :   Type* type = this->type_;
    4389                 :           0 :   if (type->integer_type() == NULL
    4390                 :           0 :       && type->float_type() == NULL
    4391                 :           0 :       && type->complex_type() == NULL
    4392                 :           0 :       && !type->is_boolean_type()
    4393                 :           0 :       && !type->is_string_type())
    4394                 :           0 :     return this->expr_->is_nil_expression();
    4395                 :             : 
    4396                 :             :   return true;
    4397                 :             : }
    4398                 :             : 
    4399                 :             : // Return whether a type conversion can be used in a constant
    4400                 :             : // initializer.
    4401                 :             : 
    4402                 :             : bool
    4403                 :       47532 : Type_conversion_expression::do_is_static_initializer() const
    4404                 :             : {
    4405                 :       47532 :   Type* type = this->type_;
    4406                 :       47532 :   Type* expr_type = this->expr_->type();
    4407                 :             : 
    4408                 :       91776 :   if (type->interface_type() != NULL
    4409                 :       47532 :       || expr_type->interface_type() != NULL)
    4410                 :        3288 :     return false;
    4411                 :             : 
    4412                 :       44244 :   if (!this->expr_->is_static_initializer())
    4413                 :             :     return false;
    4414                 :             : 
    4415                 :       34845 :   if (Type::are_identical(type, expr_type,
    4416                 :             :                           Type::COMPARE_ERRORS | Type::COMPARE_TAGS,
    4417                 :             :                           NULL))
    4418                 :             :     return true;
    4419                 :             : 
    4420                 :       22310 :   if (type->is_string_type() && expr_type->is_string_type())
    4421                 :             :     return true;
    4422                 :             : 
    4423                 :       22239 :   if ((type->is_numeric_type()
    4424                 :       22210 :        || type->is_boolean_type()
    4425                 :       22210 :        || type->points_to() != NULL)
    4426                 :       44367 :       && (expr_type->is_numeric_type()
    4427                 :       22092 :           || expr_type->is_boolean_type()
    4428                 :       22092 :           || expr_type->points_to() != NULL))
    4429                 :       22115 :     return true;
    4430                 :             : 
    4431                 :             :   return false;
    4432                 :             : }
    4433                 :             : 
    4434                 :             : // Return the constant numeric value if there is one.
    4435                 :             : 
    4436                 :             : bool
    4437                 :      105449 : Type_conversion_expression::do_numeric_constant_value(
    4438                 :             :     Numeric_constant* nc)
    4439                 :             : {
    4440                 :      105449 :   if (!this->type_->is_numeric_type())
    4441                 :             :     return false;
    4442                 :       96627 :   if (!this->expr_->numeric_constant_value(nc))
    4443                 :             :     return false;
    4444                 :       10669 :   return nc->set_type(this->type_, false, this->location());
    4445                 :             : }
    4446                 :             : 
    4447                 :             : // Return the constant string value if there is one.
    4448                 :             : 
    4449                 :             : bool
    4450                 :        7974 : Type_conversion_expression::do_string_constant_value(std::string* val)
    4451                 :             : {
    4452                 :        7974 :   if (this->type_->is_string_type() && this->expr_->type()->is_string_type())
    4453                 :         126 :     return this->expr_->string_constant_value(val);
    4454                 :             : 
    4455                 :        7848 :   if (this->type_->is_string_type()
    4456                 :        7848 :       && this->expr_->type()->integer_type() != NULL)
    4457                 :             :     {
    4458                 :         207 :       Numeric_constant nc;
    4459                 :         207 :       if (this->expr_->numeric_constant_value(&nc))
    4460                 :             :         {
    4461                 :         122 :           unsigned long ival;
    4462                 :         122 :           if (nc.to_unsigned_long(&ival) == Numeric_constant::NC_UL_VALID)
    4463                 :             :             {
    4464                 :         122 :               unsigned int cval = static_cast<unsigned int>(ival);
    4465                 :         122 :               if (static_cast<unsigned long>(cval) != ival)
    4466                 :             :                 {
    4467                 :           1 :                   go_warning_at(this->location(), 0,
    4468                 :             :                                 "unicode code point 0x%lx out of range",
    4469                 :             :                                 ival);
    4470                 :           1 :                   cval = 0xfffd; // Unicode "replacement character."
    4471                 :             :                 }
    4472                 :         122 :               val->clear();
    4473                 :         122 :               Lex::append_char(cval, true, val, this->location());
    4474                 :         122 :               return true;
    4475                 :             :             }
    4476                 :             :         }
    4477                 :         207 :     }
    4478                 :             : 
    4479                 :             :   // FIXME: Could handle conversion from const []int here.
    4480                 :             : 
    4481                 :             :   return false;
    4482                 :             : }
    4483                 :             : 
    4484                 :             : // Return the constant boolean value if there is one.
    4485                 :             : 
    4486                 :             : bool
    4487                 :        6048 : Type_conversion_expression::do_boolean_constant_value(bool* val)
    4488                 :             : {
    4489                 :        6048 :   if (!this->type_->is_boolean_type())
    4490                 :             :     return false;
    4491                 :          12 :   return this->expr_->boolean_constant_value(val);
    4492                 :             : }
    4493                 :             : 
    4494                 :             : // Determine the resulting type of the conversion.
    4495                 :             : 
    4496                 :             : void
    4497                 :     2505102 : Type_conversion_expression::do_determine_type(Gogo* gogo, const Type_context*)
    4498                 :             : {
    4499                 :     2505102 :   Type_context subcontext(this->type_, false);
    4500                 :     2505102 :   this->expr_->determine_type(gogo, &subcontext);
    4501                 :     2505102 : }
    4502                 :             : 
    4503                 :             : // Check that types are convertible.
    4504                 :             : 
    4505                 :             : void
    4506                 :      126355 : Type_conversion_expression::do_check_types(Gogo*)
    4507                 :             : {
    4508                 :      126355 :   Type* type = this->type_;
    4509                 :      126355 :   Type* expr_type = this->expr_->type();
    4510                 :      126355 :   std::string reason;
    4511                 :             : 
    4512                 :      126355 :   if (type->is_error() || expr_type->is_error())
    4513                 :             :     {
    4514                 :           2 :       this->set_is_error();
    4515                 :           2 :       return;
    4516                 :             :     }
    4517                 :             : 
    4518                 :      126353 :   if (this->may_convert_function_types_
    4519                 :           0 :       && type->function_type() != NULL
    4520                 :      252611 :       && expr_type->function_type() != NULL)
    4521                 :             :     return;
    4522                 :             : 
    4523                 :      126353 :   if (Type::are_convertible(type, expr_type, &reason))
    4524                 :             :     return;
    4525                 :             : 
    4526                 :             :   // We can convert all numeric types if the value is a constant.
    4527                 :         102 :   if (type->is_numeric_type()
    4528                 :          17 :       && expr_type->is_numeric_type()
    4529                 :         107 :       && this->expr_->is_constant())
    4530                 :             :     return;
    4531                 :             : 
    4532                 :          97 :   go_error_at(this->location(), "%s", reason.c_str());
    4533                 :          97 :   this->set_is_error();
    4534                 :      126355 : }
    4535                 :             : 
    4536                 :             : // Copy.
    4537                 :             : 
    4538                 :             : Expression*
    4539                 :        5949 : Type_conversion_expression::do_copy()
    4540                 :             : {
    4541                 :       11898 :   Expression* ret = new Type_conversion_expression(this->type_->copy_expressions(),
    4542                 :        5949 :                                                    this->expr_->copy(),
    4543                 :        5949 :                                                    this->location());
    4544                 :        5949 :   ret->conversion_expression()->set_no_copy(this->no_copy_);
    4545                 :        5949 :   return ret;
    4546                 :             : }
    4547                 :             : 
    4548                 :             : // Get the backend representation for a type conversion.
    4549                 :             : 
    4550                 :             : Bexpression*
    4551                 :     2943228 : Type_conversion_expression::do_get_backend(Translate_context* context)
    4552                 :             : {
    4553                 :     2943228 :   Type* type = this->type_;
    4554                 :     2943228 :   Type* expr_type = this->expr_->type();
    4555                 :     2943228 :   Type_context tcontext(type, false);
    4556                 :             : 
    4557                 :     2943228 :   Gogo* gogo = context->gogo();
    4558                 :     2943228 :   Btype* btype = type->get_backend(gogo);
    4559                 :     2943228 :   Location loc = this->location();
    4560                 :             : 
    4561                 :     2943228 :   if (Type::are_identical(type, expr_type,
    4562                 :             :                           Type::COMPARE_ERRORS | Type::COMPARE_TAGS,
    4563                 :             :                           NULL))
    4564                 :             :     {
    4565                 :     1104769 :       Bexpression* bexpr = this->expr_->get_backend(context);
    4566                 :     1104769 :       return gogo->backend()->convert_expression(btype, bexpr, loc);
    4567                 :             :     }
    4568                 :     2065683 :   else if (type->interface_type() != NULL
    4569                 :     1838459 :            && expr_type->interface_type() == NULL)
    4570                 :             :     {
    4571                 :      201200 :       Expression* conversion =
    4572                 :      402400 :           Expression::convert_type_to_interface(type, this->expr_,
    4573                 :      201200 :                                                 this->no_escape_, loc);
    4574                 :      201200 :       conversion->determine_type(gogo, &tcontext);
    4575                 :      201200 :       return conversion->get_backend(context);
    4576                 :             :     }
    4577                 :     3248494 :   else if (type->interface_type() != NULL
    4578                 :     1637259 :            || expr_type->interface_type() != NULL)
    4579                 :             :     {
    4580                 :       26024 :       Expression* conversion =
    4581                 :       26024 :           Expression::convert_for_assignment(gogo, type, this->expr_,
    4582                 :             :                                              loc);
    4583                 :       26024 :       conversion->determine_type(gogo, &tcontext);
    4584                 :       26024 :       return conversion->get_backend(context);
    4585                 :             :     }
    4586                 :     1611235 :   else if (type->is_string_type()
    4587                 :     1611235 :            && expr_type->integer_type() != NULL)
    4588                 :             :     {
    4589                 :         648 :       mpz_t intval;
    4590                 :         648 :       Numeric_constant nc;
    4591                 :         648 :       if (this->expr_->numeric_constant_value(&nc)
    4592                 :         648 :           && nc.to_int(&intval))
    4593                 :             :         {
    4594                 :         380 :           std::string s;
    4595                 :         380 :           unsigned int x;
    4596                 :         380 :           if (mpz_fits_uint_p(intval))
    4597                 :         381 :             x = mpz_get_ui(intval);
    4598                 :             :           else
    4599                 :             :             {
    4600                 :           1 :               char* ms = mpz_get_str(NULL, 16, intval);
    4601                 :           1 :               go_warning_at(loc, 0,
    4602                 :             :                             "unicode code point 0x%s out of range in string",
    4603                 :             :                             ms);
    4604                 :           1 :               free(ms);
    4605                 :           1 :               x = 0xfffd;
    4606                 :             :             }
    4607                 :         380 :           Lex::append_char(x, true, &s, loc);
    4608                 :         380 :           mpz_clear(intval);
    4609                 :         380 :           Expression* se = Expression::make_string(s, loc);
    4610                 :         380 :           se->determine_type(gogo, &tcontext);
    4611                 :         380 :           return se->get_backend(context);
    4612                 :         380 :         }
    4613                 :             : 
    4614                 :         268 :       Expression* buf;
    4615                 :         268 :       if (this->no_escape_)
    4616                 :             :         {
    4617                 :         200 :           Type* byte_type = Type::lookup_integer_type("uint8");
    4618                 :         200 :           Expression* buflen =
    4619                 :         200 :             Expression::make_integer_ul(4, NULL, loc);
    4620                 :         200 :           Type* array_type = Type::make_array_type(byte_type, buflen);
    4621                 :         200 :           buf = Expression::make_allocation(array_type, loc);
    4622                 :         200 :           buf->allocation_expression()->set_allocate_on_stack();
    4623                 :         200 :           buf->allocation_expression()->set_no_zero();
    4624                 :             :         }
    4625                 :             :       else
    4626                 :          68 :         buf = Expression::make_nil(loc);
    4627                 :         268 :       Expression* i2s_expr =
    4628                 :         268 :         Runtime::make_call(gogo, Runtime::INTSTRING, loc, 2, buf, this->expr_);
    4629                 :         268 :       Expression* ret = Expression::make_cast(type, i2s_expr, loc);
    4630                 :         268 :       Type_context tcontext(type, false);
    4631                 :         268 :       ret->determine_type(gogo, &tcontext);
    4632                 :         268 :       return ret->get_backend(context);
    4633                 :         648 :     }
    4634                 :     1610587 :   else if (type->is_string_type() && expr_type->is_slice_type())
    4635                 :             :     {
    4636                 :        5289 :       Array_type* a = expr_type->array_type();
    4637                 :        5289 :       Type* e = a->element_type()->forwarded();
    4638                 :        5289 :       go_assert(e->integer_type() != NULL);
    4639                 :        5289 :       go_assert(this->expr_->is_multi_eval_safe());
    4640                 :             : 
    4641                 :        5289 :       Expression* buf;
    4642                 :        5289 :       if (this->no_escape_ && !this->no_copy_)
    4643                 :             :         {
    4644                 :         408 :           Type* byte_type = Type::lookup_integer_type("uint8");
    4645                 :         408 :           Expression* buflen =
    4646                 :         408 :             Expression::make_integer_ul(tmp_string_buf_size, NULL, loc);
    4647                 :         408 :           Type* array_type = Type::make_array_type(byte_type, buflen);
    4648                 :         408 :           buf = Expression::make_allocation(array_type, loc);
    4649                 :         408 :           buf->allocation_expression()->set_allocate_on_stack();
    4650                 :         408 :           buf->allocation_expression()->set_no_zero();
    4651                 :             :         }
    4652                 :             :       else
    4653                 :        4881 :         buf = Expression::make_nil(loc);
    4654                 :             : 
    4655                 :       10578 :       if (e->integer_type()->is_byte())
    4656                 :             :         {
    4657                 :        5155 :           Expression* ptr =
    4658                 :        5155 :             Expression::make_slice_info(this->expr_, SLICE_INFO_VALUE_POINTER,
    4659                 :             :                                         loc);
    4660                 :        5155 :           Expression* len =
    4661                 :        5155 :             Expression::make_slice_info(this->expr_, SLICE_INFO_LENGTH, loc);
    4662                 :        5155 :           if (this->no_copy_)
    4663                 :             :             {
    4664                 :        1689 :               if (gogo->debug_optimization())
    4665                 :           6 :                 go_debug(loc, "no copy string([]byte)");
    4666                 :        1689 :               Expression* str = Expression::make_string_value(ptr, len, loc);
    4667                 :        1689 :               return str->get_backend(context);
    4668                 :             :             }
    4669                 :        3466 :           Expression* ret = Runtime::make_call(gogo, Runtime::SLICEBYTETOSTRING,
    4670                 :             :                                                loc, 3, buf, ptr, len);
    4671                 :        3466 :           Type_context tcontext(type, false);
    4672                 :        3466 :           ret->determine_type(gogo, &tcontext);
    4673                 :        3466 :           return ret->get_backend(context);
    4674                 :             :         }
    4675                 :             :       else
    4676                 :             :         {
    4677                 :         268 :           go_assert(e->integer_type()->is_rune());
    4678                 :         134 :           Expression* ret = Runtime::make_call(gogo, Runtime::SLICERUNETOSTRING,
    4679                 :             :                                                loc, 2, buf, this->expr_);
    4680                 :         134 :           Type_context tcontext(type, false);
    4681                 :         134 :           ret->determine_type(gogo, &tcontext);
    4682                 :         134 :           return ret->get_backend(context);
    4683                 :             :         }
    4684                 :             :     }
    4685                 :     1608931 :   else if (type->is_slice_type() && expr_type->is_string_type())
    4686                 :             :     {
    4687                 :        3128 :       Type* e = type->array_type()->element_type()->forwarded();
    4688                 :        1564 :       go_assert(e->integer_type() != NULL);
    4689                 :             : 
    4690                 :        1564 :       Runtime::Function code;
    4691                 :        3128 :       if (e->integer_type()->is_byte())
    4692                 :             :         code = Runtime::STRINGTOSLICEBYTE;
    4693                 :             :       else
    4694                 :             :         {
    4695                 :          90 :           go_assert(e->integer_type()->is_rune());
    4696                 :             :           code = Runtime::STRINGTOSLICERUNE;
    4697                 :             :         }
    4698                 :             : 
    4699                 :        1564 :       Expression* buf;
    4700                 :        1564 :       if (this->no_escape_)
    4701                 :             :         {
    4702                 :         524 :           Expression* buflen =
    4703                 :         524 :             Expression::make_integer_ul(tmp_string_buf_size, NULL, loc);
    4704                 :         524 :           Type* array_type = Type::make_array_type(e, buflen);
    4705                 :         524 :           buf = Expression::make_allocation(array_type, loc);
    4706                 :         524 :           buf->allocation_expression()->set_allocate_on_stack();
    4707                 :         524 :           buf->allocation_expression()->set_no_zero();
    4708                 :             :         }
    4709                 :             :       else
    4710                 :        1040 :         buf = Expression::make_nil(loc);
    4711                 :        1564 :       Expression* s2a = Runtime::make_call(gogo, code, loc, 2, buf,
    4712                 :             :                                            this->expr_);
    4713                 :        1564 :       Expression* ret = Expression::make_unsafe_cast(type, s2a, loc);
    4714                 :        1564 :       Type_context tcontext(type, false);
    4715                 :        1564 :       ret->determine_type(gogo, &tcontext);
    4716                 :        1564 :       return ret->get_backend(context);
    4717                 :             :     }
    4718                 :     1603734 :   else if (type->is_numeric_type())
    4719                 :             :     {
    4720                 :      412030 :       go_assert(Type::are_convertible(type, expr_type, NULL));
    4721                 :      412030 :       Bexpression* bexpr = this->expr_->get_backend(context);
    4722                 :      412030 :       return gogo->backend()->convert_expression(btype, bexpr, loc);
    4723                 :             :     }
    4724                 :     1191704 :   else if ((type->is_unsafe_pointer_type()
    4725                 :      769905 :             && (expr_type->points_to() != NULL
    4726                 :     1406138 :                 || expr_type->integer_type()))
    4727                 :      436852 :            || (expr_type->is_unsafe_pointer_type()
    4728                 :      214434 :                && type->points_to() != NULL)
    4729                 :     1414122 :            || (this->may_convert_function_types_
    4730                 :           0 :                && type->function_type() != NULL
    4731                 :      969286 :                && expr_type->function_type() != NULL))
    4732                 :             :     {
    4733                 :      969286 :       Bexpression* bexpr = this->expr_->get_backend(context);
    4734                 :      969286 :       return gogo->backend()->convert_expression(btype, bexpr, loc);
    4735                 :             :     }
    4736                 :             :   else
    4737                 :             :     {
    4738                 :      222418 :       Expression* conversion =
    4739                 :      222418 :           Expression::convert_for_assignment(gogo, type, this->expr_, loc);
    4740                 :      222418 :       conversion->determine_type(gogo, &tcontext);
    4741                 :      222418 :       return conversion->get_backend(context);
    4742                 :             :     }
    4743                 :             : }
    4744                 :             : 
    4745                 :             : // Cost of inlining a type conversion.
    4746                 :             : 
    4747                 :             : int
    4748                 :      289413 : Type_conversion_expression::do_inlining_cost() const
    4749                 :             : {
    4750                 :      289413 :   Type* type = this->type_;
    4751                 :      289413 :   Type* expr_type = this->expr_->type();
    4752                 :      491515 :   if (type->interface_type() != NULL || expr_type->interface_type() != NULL)
    4753                 :       87311 :     return 10;
    4754                 :      298377 :   else if (type->is_string_type() && expr_type->integer_type() != NULL)
    4755                 :             :     return 10;
    4756                 :      201934 :   else if (type->is_string_type() && expr_type->is_slice_type())
    4757                 :             :     return 10;
    4758                 :      199427 :   else if (type->is_slice_type() && expr_type->is_string_type())
    4759                 :             :     return 10;
    4760                 :             :   else
    4761                 :      193138 :     return 1;
    4762                 :             : }
    4763                 :             : 
    4764                 :             : // Output a type conversion in a constant expression.
    4765                 :             : 
    4766                 :             : void
    4767                 :       10254 : Type_conversion_expression::do_export(Export_function_body* efb) const
    4768                 :             : {
    4769                 :       10254 :   efb->write_c_string("$convert(");
    4770                 :       10254 :   efb->write_type(this->type_);
    4771                 :       10254 :   efb->write_c_string(", ");
    4772                 :             : 
    4773                 :       10254 :   Type* old_context = efb->type_context();
    4774                 :       10254 :   efb->set_type_context(this->type_);
    4775                 :             : 
    4776                 :       10254 :   this->expr_->export_expression(efb);
    4777                 :             : 
    4778                 :       10254 :   efb->set_type_context(old_context);
    4779                 :             : 
    4780                 :       10254 :   efb->write_c_string(")");
    4781                 :       10254 : }
    4782                 :             : 
    4783                 :             : // Import a type conversion or a struct construction.
    4784                 :             : 
    4785                 :             : Expression*
    4786                 :       18058 : Type_conversion_expression::do_import(Import_expression* imp, Location loc)
    4787                 :             : {
    4788                 :       18058 :   imp->require_c_string("$convert(");
    4789                 :       18058 :   Type* type = imp->read_type();
    4790                 :       18058 :   imp->require_c_string(", ");
    4791                 :       18058 :   Expression* val = Expression::import_expression(imp, loc);
    4792                 :       18058 :   imp->require_c_string(")");
    4793                 :       18058 :   return Expression::make_cast(type, val, loc);
    4794                 :             : }
    4795                 :             : 
    4796                 :             : // Dump ast representation for a type conversion expression.
    4797                 :             : 
    4798                 :             : void
    4799                 :           0 : Type_conversion_expression::do_dump_expression(
    4800                 :             :     Ast_dump_context* ast_dump_context) const
    4801                 :             : {
    4802                 :           0 :   ast_dump_context->dump_type(this->type_);
    4803                 :           0 :   ast_dump_context->ostream() << "(";
    4804                 :           0 :   ast_dump_context->dump_expression(this->expr_);
    4805                 :           0 :   ast_dump_context->ostream() << ") ";
    4806                 :           0 : }
    4807                 :             : 
    4808                 :             : // Make a type cast expression.
    4809                 :             : 
    4810                 :             : Expression*
    4811                 :     3012187 : Expression::make_cast(Type* type, Expression* val, Location location)
    4812                 :             : {
    4813                 :     3012187 :   if (type->is_error_type() || val->is_error_expression())
    4814                 :         512 :     return Expression::make_error(location);
    4815                 :     3011675 :   return new Type_conversion_expression(type, val, location);
    4816                 :             : }
    4817                 :             : 
    4818                 :             : // Class Unsafe_type_conversion_expression.
    4819                 :             : 
    4820                 :             : // Traversal.
    4821                 :             : 
    4822                 :             : int
    4823                 :     1435964 : Unsafe_type_conversion_expression::do_traverse(Traverse* traverse)
    4824                 :             : {
    4825                 :     1435964 :   if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT
    4826                 :     1435964 :       || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
    4827                 :       70884 :     return TRAVERSE_EXIT;
    4828                 :             :   return TRAVERSE_CONTINUE;
    4829                 :             : }
    4830                 :             : 
    4831                 :             : // Return whether an unsafe type conversion can be used as a constant
    4832                 :             : // initializer.
    4833                 :             : 
    4834                 :             : bool
    4835                 :         184 : Unsafe_type_conversion_expression::do_is_static_initializer() const
    4836                 :             : {
    4837                 :         184 :   Type* type = this->type_;
    4838                 :         184 :   Type* expr_type = this->expr_->type();
    4839                 :             : 
    4840                 :         368 :   if (type->interface_type() != NULL
    4841                 :         184 :       || expr_type->interface_type() != NULL)
    4842                 :           0 :     return false;
    4843                 :             : 
    4844                 :         184 :   if (!this->expr_->is_static_initializer())
    4845                 :             :     return false;
    4846                 :             : 
    4847                 :           0 :   if (Type::are_convertible(type, expr_type, NULL))
    4848                 :             :     return true;
    4849                 :             : 
    4850                 :           0 :   if (type->is_string_type() && expr_type->is_string_type())
    4851                 :             :     return true;
    4852                 :             : 
    4853                 :           0 :   if ((type->is_numeric_type()
    4854                 :           0 :        || type->is_boolean_type()
    4855                 :           0 :        || type->points_to() != NULL)
    4856                 :           0 :       && (expr_type->is_numeric_type()
    4857                 :           0 :           || expr_type->is_boolean_type()
    4858                 :           0 :           || expr_type->points_to() != NULL))
    4859                 :           0 :     return true;
    4860                 :             : 
    4861                 :             :   return false;
    4862                 :             : }
    4863                 :             : 
    4864                 :             : // Copy.
    4865                 :             : 
    4866                 :             : Expression*
    4867                 :           0 : Unsafe_type_conversion_expression::do_copy()
    4868                 :             : {
    4869                 :           0 :   return new Unsafe_type_conversion_expression(this->type_->copy_expressions(),
    4870                 :           0 :                                                this->expr_->copy(),
    4871                 :           0 :                                                this->location());
    4872                 :             : }
    4873                 :             : 
    4874                 :             : // Convert to backend representation.
    4875                 :             : 
    4876                 :             : Bexpression*
    4877                 :      741050 : Unsafe_type_conversion_expression::do_get_backend(Translate_context* context)
    4878                 :             : {
    4879                 :             :   // We are only called for a limited number of cases.
    4880                 :             : 
    4881                 :      741050 :   Type* t = this->type_;
    4882                 :      741050 :   Type* et = this->expr_->type();
    4883                 :             : 
    4884                 :      741050 :   if (t->is_error_type()
    4885                 :      741050 :       || this->expr_->is_error_expression()
    4886                 :     1482100 :       || et->is_error_type())
    4887                 :             :     {
    4888                 :           0 :       go_assert(saw_errors());
    4889                 :           0 :       return context->backend()->error_expression();
    4890                 :             :     }
    4891                 :             : 
    4892                 :      741050 :   if (t->array_type() != NULL)
    4893                 :       58490 :     go_assert(et->array_type() != NULL
    4894                 :             :               && t->is_slice_type() == et->is_slice_type());
    4895                 :      711805 :   else if (t->struct_type() != NULL)
    4896                 :             :     {
    4897                 :          36 :       if (t->named_type() != NULL
    4898                 :          30 :           && et->named_type() != NULL
    4899                 :          58 :           && !Type::are_convertible(t, et, NULL))
    4900                 :             :         {
    4901                 :           0 :           go_assert(saw_errors());
    4902                 :           0 :           return context->backend()->error_expression();
    4903                 :             :         }
    4904                 :             : 
    4905                 :          72 :       go_assert(et->struct_type() != NULL
    4906                 :             :                 && Type::are_convertible(t, et, NULL));
    4907                 :             :     }
    4908                 :      711769 :   else if (t->map_type() != NULL)
    4909                 :       21419 :     go_assert(et->map_type() != NULL || et->points_to() != NULL);
    4910                 :      690748 :   else if (t->channel_type() != NULL)
    4911                 :        9220 :     go_assert(et->channel_type() != NULL || et->points_to() != NULL);
    4912                 :      681641 :   else if (t->points_to() != NULL)
    4913                 :      615193 :     go_assert(et->points_to() != NULL
    4914                 :             :               || et->channel_type() != NULL
    4915                 :             :               || et->map_type() != NULL
    4916                 :             :               || et->function_type() != NULL
    4917                 :             :               || et->integer_type() != NULL
    4918                 :             :               || et->is_nil_type());
    4919                 :      114544 :   else if (t->function_type() != NULL)
    4920                 :        1343 :     go_assert(et->points_to() != NULL);
    4921                 :      113201 :   else if (et->is_unsafe_pointer_type())
    4922                 :       44766 :     go_assert(t->points_to() != NULL
    4923                 :             :               || (t->integer_type() != NULL
    4924                 :             :                   && t->integer_type() == Type::lookup_integer_type("uintptr")->real_type()));
    4925                 :       98279 :   else if (t->interface_type() != NULL)
    4926                 :             :     {
    4927                 :       79972 :       bool empty_iface = t->interface_type()->is_empty();
    4928                 :      119958 :       go_assert(et->interface_type() != NULL
    4929                 :             :                 && et->interface_type()->is_empty() == empty_iface);
    4930                 :             :     }
    4931                 :       58293 :   else if (t->integer_type() != NULL)
    4932                 :      866462 :     go_assert(et->is_boolean_type()
    4933                 :             :               || et->integer_type() != NULL
    4934                 :             :               || et->function_type() != NULL
    4935                 :             :               || et->points_to() != NULL
    4936                 :             :               || et->map_type() != NULL
    4937                 :             :               || et->channel_type() != NULL
    4938                 :             :               || et->is_nil_type());
    4939                 :             :   else
    4940                 :           0 :     go_unreachable();
    4941                 :             : 
    4942                 :      741050 :   Gogo* gogo = context->gogo();
    4943                 :      741050 :   Btype* btype = t->get_backend(gogo);
    4944                 :      741050 :   Bexpression* bexpr = this->expr_->get_backend(context);
    4945                 :      741050 :   Location loc = this->location();
    4946                 :      741050 :   return gogo->backend()->convert_expression(btype, bexpr, loc);
    4947                 :             : }
    4948                 :             : 
    4949                 :             : // Dump ast representation for an unsafe type conversion expression.
    4950                 :             : 
    4951                 :             : void
    4952                 :           0 : Unsafe_type_conversion_expression::do_dump_expression(
    4953                 :             :     Ast_dump_context* ast_dump_context) const
    4954                 :             : {
    4955                 :           0 :   ast_dump_context->dump_type(this->type_);
    4956                 :           0 :   ast_dump_context->ostream() << "(";
    4957                 :           0 :   ast_dump_context->dump_expression(this->expr_);
    4958                 :           0 :   ast_dump_context->ostream() << ") ";
    4959                 :           0 : }
    4960                 :             : 
    4961                 :             : // Make an unsafe type conversion expression.
    4962                 :             : 
    4963                 :             : Expression*
    4964                 :      780253 : Expression::make_unsafe_cast(Type* type, Expression* expr,
    4965                 :             :                              Location location)
    4966                 :             : {
    4967                 :      780253 :   return new Unsafe_type_conversion_expression(type, expr, location);
    4968                 :             : }
    4969                 :             : 
    4970                 :             : // Class Unary_expression.
    4971                 :             : 
    4972                 :             : // Call the address_taken method of the operand if needed.  This is
    4973                 :             : // called after escape analysis but before inserting write barriers.
    4974                 :             : 
    4975                 :             : void
    4976                 :      826416 : Unary_expression::check_operand_address_taken(Gogo*)
    4977                 :             : {
    4978                 :      826416 :   if (this->op_ != OPERATOR_AND)
    4979                 :             :     return;
    4980                 :             : 
    4981                 :             :   // If this->escapes_ is false at this point, then it was set to
    4982                 :             :   // false by an explicit call to set_does_not_escape, and the value
    4983                 :             :   // does not escape.  If this->escapes_ is true, we may be able to
    4984                 :             :   // set it to false based on the escape analysis pass.
    4985                 :      205228 :   if (this->escapes_)
    4986                 :             :     {
    4987                 :      176937 :       Node* n = Node::make_node(this);
    4988                 :      176937 :       if ((n->encoding() & ESCAPE_MASK) == int(Node::ESCAPE_NONE))
    4989                 :      129245 :         this->escapes_ = false;
    4990                 :             :     }
    4991                 :             : 
    4992                 :      205228 :   this->expr_->address_taken(this->escapes_);
    4993                 :             : }
    4994                 :             : 
    4995                 :             : // If we are taking the address of a composite literal, and the
    4996                 :             : // contents are not constant, then we want to make a heap expression
    4997                 :             : // instead.
    4998                 :             : 
    4999                 :             : Expression*
    5000                 :     1636853 : Unary_expression::do_lower(Gogo* gogo, Named_object*, Statement_inserter*)
    5001                 :             : {
    5002                 :     1636853 :   Location loc = this->location();
    5003                 :             : 
    5004                 :     1636853 :   if (this->is_error_expression())
    5005                 :           7 :     return Expression::make_error(loc);
    5006                 :             : 
    5007                 :     1636846 :   Operator op = this->op_;
    5008                 :     1636846 :   Expression* expr = this->expr_;
    5009                 :             : 
    5010                 :     1636846 :   if (expr->is_error_expression())
    5011                 :           2 :     return Expression::make_error(loc);
    5012                 :             : 
    5013                 :     1636844 :   if (op == OPERATOR_MULT && expr->is_type_expression())
    5014                 :             :     {
    5015                 :         163 :       Expression* ret =
    5016                 :         163 :         Expression::make_type(Type::make_pointer_type(expr->type()), loc);
    5017                 :         163 :       ret->determine_type_no_context(gogo);
    5018                 :         163 :       return ret;
    5019                 :             :     }
    5020                 :             : 
    5021                 :             :   // *&x simplifies to x.  *(*T)(unsafe.Pointer)(&x) does not require
    5022                 :             :   // moving x to the heap.  FIXME: Is it worth doing a real escape
    5023                 :             :   // analysis here?  This case is found in math/unsafe.go and is
    5024                 :             :   // therefore worth special casing.
    5025                 :     1636681 :   if (op == OPERATOR_MULT)
    5026                 :             :     {
    5027                 :             :       Expression* e = expr;
    5028                 :     1187135 :       while (e->classification() == EXPRESSION_CONVERSION)
    5029                 :             :         {
    5030                 :       20844 :           Type_conversion_expression* te
    5031                 :             :             = static_cast<Type_conversion_expression*>(e);
    5032                 :       20844 :           e = te->expr();
    5033                 :             :         }
    5034                 :             : 
    5035                 :     1166291 :       if (e->classification() == EXPRESSION_UNARY)
    5036                 :             :         {
    5037                 :        2957 :           Unary_expression* ue = static_cast<Unary_expression*>(e);
    5038                 :        2957 :           if (ue->op_ == OPERATOR_AND)
    5039                 :             :             {
    5040                 :        1069 :               if (e == expr)
    5041                 :             :                 {
    5042                 :             :                   // *&x == x.
    5043                 :          24 :                   if (!ue->expr_->is_addressable() && !ue->create_temp_)
    5044                 :             :                     {
    5045                 :           0 :                       go_error_at(ue->location(),
    5046                 :             :                                   "invalid operand for unary %<&%>");
    5047                 :           0 :                       this->set_is_error();
    5048                 :             :                     }
    5049                 :          24 :                   return ue->expr_;
    5050                 :             :                 }
    5051                 :        1045 :               ue->set_does_not_escape();
    5052                 :             :             }
    5053                 :             :         }
    5054                 :             :     }
    5055                 :             : 
    5056                 :             :   // Check for an invalid pointer dereference.  We need to do this
    5057                 :             :   // here because Unary_expression::do_type will return an error type
    5058                 :             :   // in this case.  That can cause code to appear erroneous, and
    5059                 :             :   // therefore disappear at lowering time, without any error message.
    5060                 :     1166267 :   if (op == OPERATOR_MULT && expr->type()->points_to() == NULL)
    5061                 :             :     {
    5062                 :           0 :       this->report_error(_("expected pointer"));
    5063                 :           0 :       return Expression::make_error(this->location());
    5064                 :             :     }
    5065                 :             : 
    5066                 :     1636657 :   if (op == OPERATOR_PLUS || op == OPERATOR_MINUS || op == OPERATOR_XOR)
    5067                 :             :     {
    5068                 :       88050 :       Numeric_constant nc;
    5069                 :       88050 :       if (expr->numeric_constant_value(&nc))
    5070                 :             :         {
    5071                 :       64324 :           Numeric_constant result;
    5072                 :       64324 :           bool issued_error;
    5073                 :       64324 :           if (Unary_expression::eval_constant(this->type_, op, &nc, loc,
    5074                 :             :                                               &result, &issued_error))
    5075                 :             :             {
    5076                 :       64324 :               Expression* ret = result.expression(loc);
    5077                 :       64324 :               Type_context subcontext(this->type_, this->type_->is_abstract());
    5078                 :       64324 :               ret->determine_type(gogo, &subcontext);
    5079                 :       64324 :               ret->check_types(gogo);
    5080                 :       64324 :               return ret;
    5081                 :             :             }
    5082                 :           0 :           else if (issued_error)
    5083                 :           0 :             return Expression::make_error(this->location());
    5084                 :       64324 :         }
    5085                 :       88050 :     }
    5086                 :             : 
    5087                 :     1572333 :   return this;
    5088                 :             : }
    5089                 :             : 
    5090                 :             : // Flatten expression if a nil check must be performed and create temporary
    5091                 :             : // variables if necessary.
    5092                 :             : 
    5093                 :             : Expression*
    5094                 :     1372577 : Unary_expression::do_flatten(Gogo* gogo, Named_object*,
    5095                 :             :                              Statement_inserter* inserter)
    5096                 :             : {
    5097                 :     1372577 :   if (this->is_error_expression()
    5098                 :     1372577 :       || this->expr_->is_error_expression()
    5099                 :     2745154 :       || this->expr_->type()->is_error_type())
    5100                 :             :     {
    5101                 :           0 :       go_assert(saw_errors());
    5102                 :           0 :       return Expression::make_error(this->location());
    5103                 :             :     }
    5104                 :             : 
    5105                 :     1372577 :   Location location = this->location();
    5106                 :     1372577 :   if (this->op_ == OPERATOR_MULT
    5107                 :     1372577 :       && !this->expr_->is_multi_eval_safe())
    5108                 :             :     {
    5109                 :      176468 :       go_assert(this->expr_->type()->points_to() != NULL);
    5110                 :      176468 :       switch (this->requires_nil_check(gogo))
    5111                 :             :         {
    5112                 :           0 :           case NIL_CHECK_ERROR_ENCOUNTERED:
    5113                 :           0 :             {
    5114                 :           0 :               go_assert(saw_errors());
    5115                 :           0 :               return Expression::make_error(this->location());
    5116                 :             :             }
    5117                 :             :           case NIL_CHECK_NOT_NEEDED:
    5118                 :             :             break;
    5119                 :        4142 :           case NIL_CHECK_NEEDED:
    5120                 :        4142 :             this->create_temp_ = true;
    5121                 :        4142 :             break;
    5122                 :           0 :           case NIL_CHECK_DEFAULT:
    5123                 :           0 :             go_unreachable();
    5124                 :             :         }
    5125                 :             :     }
    5126                 :             : 
    5127                 :     1372577 :   if (this->create_temp_ && !this->expr_->is_multi_eval_safe())
    5128                 :             :     {
    5129                 :       12729 :       Temporary_statement* temp =
    5130                 :       12729 :           Statement::make_temporary(NULL, this->expr_, location);
    5131                 :       12729 :       inserter->insert(temp);
    5132                 :       12729 :       this->expr_ = Expression::make_temporary_reference(temp, location);
    5133                 :             :     }
    5134                 :             : 
    5135                 :     1372577 :   return this;
    5136                 :             : }
    5137                 :             : 
    5138                 :             : // Return whether a unary expression is a constant.
    5139                 :             : 
    5140                 :             : bool
    5141                 :      334199 : Unary_expression::do_is_constant() const
    5142                 :             : {
    5143                 :      334199 :   if (this->op_ == OPERATOR_MULT || this->op_ == OPERATOR_AND)
    5144                 :             :     {
    5145                 :             :       // These are not constant by Go language rules.
    5146                 :             :       return false;
    5147                 :             :     }
    5148                 :             :   else
    5149                 :       39460 :     return this->expr_->is_constant();
    5150                 :             : }
    5151                 :             : 
    5152                 :             : bool
    5153                 :      234006 : Unary_expression::do_is_untyped(Type** ptype) const
    5154                 :             : {
    5155                 :      234006 :   if (this->type_ != NULL)
    5156                 :         144 :     return Expression::is_untyped_type(this->type_, ptype);
    5157                 :             : 
    5158                 :      233862 :   if (this->op_ == OPERATOR_MULT || this->op_ == OPERATOR_AND)
    5159                 :             :     return false;
    5160                 :       28276 :   return this->expr_->is_untyped(ptype);
    5161                 :             : }
    5162                 :             : 
    5163                 :             : // Return whether a unary expression can be used as a constant
    5164                 :             : // initializer.
    5165                 :             : 
    5166                 :             : bool
    5167                 :     1020898 : Unary_expression::do_is_static_initializer() const
    5168                 :             : {
    5169                 :     1020898 :   if (this->op_ == OPERATOR_MULT)
    5170                 :             :     return false;
    5171                 :     1020883 :   else if (this->op_ == OPERATOR_AND)
    5172                 :     1020877 :     return Unary_expression::base_is_static_initializer(this->expr_);
    5173                 :             :   else
    5174                 :           6 :     return this->expr_->is_static_initializer();
    5175                 :             : }
    5176                 :             : 
    5177                 :             : // Return whether the address of EXPR can be used as a static
    5178                 :             : // initializer.
    5179                 :             : 
    5180                 :             : bool
    5181                 :     1020929 : Unary_expression::base_is_static_initializer(Expression* expr)
    5182                 :             : {
    5183                 :             :   // The address of a field reference can be a static initializer if
    5184                 :             :   // the base can be a static initializer.
    5185                 :     1021749 :   Field_reference_expression* fre = expr->field_reference_expression();
    5186                 :     1021749 :   if (fre != NULL)
    5187                 :         820 :     return Unary_expression::base_is_static_initializer(fre->expr());
    5188                 :             : 
    5189                 :             :   // The address of an index expression can be a static initializer if
    5190                 :             :   // the base can be a static initializer and the index is constant.
    5191                 :     1020929 :   Array_index_expression* aind = expr->array_index_expression();
    5192                 :     1020929 :   if (aind != NULL)
    5193                 :          52 :     return (aind->end() == NULL
    5194                 :          52 :             && aind->start()->is_constant()
    5195                 :         104 :             && Unary_expression::base_is_static_initializer(aind->array()));
    5196                 :             : 
    5197                 :             :   // The address of a global variable can be a static initializer.
    5198                 :     1020877 :   Var_expression* ve = expr->var_expression();
    5199                 :     1020877 :   if (ve != NULL)
    5200                 :             :     {
    5201                 :       20904 :       Named_object* no = ve->named_object();
    5202                 :       20904 :       return no->is_variable() && no->var_value()->is_global();
    5203                 :             :     }
    5204                 :             : 
    5205                 :             :   // The address of a composite literal can be used as a static
    5206                 :             :   // initializer if the composite literal is itself usable as a
    5207                 :             :   // static initializer.
    5208                 :      999973 :   if (expr->is_composite_literal() && expr->is_static_initializer())
    5209                 :             :     return true;
    5210                 :             : 
    5211                 :             :   // The address of a string constant can be used as a static
    5212                 :             :   // initializer.  This can not be written in Go itself but this is
    5213                 :             :   // used when building a type descriptor.
    5214                 :     1020552 :   if (expr->string_expression() != NULL)
    5215                 :             :     return true;
    5216                 :             : 
    5217                 :             :   return false;
    5218                 :             : }
    5219                 :             : 
    5220                 :             : // Return whether this dereference expression requires an explicit nil
    5221                 :             : // check. If we are dereferencing the pointer to a large struct
    5222                 :             : // (greater than the specified size threshold), we need to check for
    5223                 :             : // nil. We don't bother to check for small structs because we expect
    5224                 :             : // the system to crash on a nil pointer dereference. However, if we
    5225                 :             : // know the address of this expression is being taken, we must always
    5226                 :             : // check for nil.
    5227                 :             : Unary_expression::Nil_check_classification
    5228                 :     1277229 : Unary_expression::requires_nil_check(Gogo* gogo)
    5229                 :             : {
    5230                 :     1277229 :   go_assert(this->op_ == OPERATOR_MULT);
    5231                 :     1277229 :   go_assert(this->expr_->type()->points_to() != NULL);
    5232                 :             : 
    5233                 :     1277229 :   if (this->issue_nil_check_ == NIL_CHECK_NEEDED)
    5234                 :             :     return NIL_CHECK_NEEDED;
    5235                 :     1208603 :   else if (this->issue_nil_check_ == NIL_CHECK_NOT_NEEDED)
    5236                 :             :     return NIL_CHECK_NOT_NEEDED;
    5237                 :             : 
    5238                 :      560820 :   Type* ptype = this->expr_->type()->points_to();
    5239                 :      560820 :   int64_t type_size = -1;
    5240                 :      560820 :   if (!ptype->is_void_type())
    5241                 :             :     {
    5242                 :      560820 :       bool ok = ptype->backend_type_size(gogo, &type_size);
    5243                 :      560820 :       if (!ok)
    5244                 :             :         return NIL_CHECK_ERROR_ENCOUNTERED;
    5245                 :             :     }
    5246                 :             : 
    5247                 :      560820 :   int64_t size_cutoff = gogo->nil_check_size_threshold();
    5248                 :      560820 :   if (size_cutoff == -1 || (type_size != -1 && type_size >= size_cutoff))
    5249                 :       15662 :     this->issue_nil_check_ = NIL_CHECK_NEEDED;
    5250                 :             :   else
    5251                 :      545158 :     this->issue_nil_check_ = NIL_CHECK_NOT_NEEDED;
    5252                 :      560820 :   return this->issue_nil_check_;
    5253                 :             : }
    5254                 :             : 
    5255                 :             : // Apply unary opcode OP to UNC, setting NC.  Return true if this
    5256                 :             : // could be done, false if not.  On overflow, issues an error and sets
    5257                 :             : // *ISSUED_ERROR.
    5258                 :             : 
    5259                 :             : bool
    5260                 :       81777 : Unary_expression::eval_constant(Type* type, Operator op,
    5261                 :             :                                 const Numeric_constant* unc,
    5262                 :             :                                 Location location, Numeric_constant* nc,
    5263                 :             :                                 bool* issued_error)
    5264                 :             : {
    5265                 :       81777 :   *issued_error = false;
    5266                 :       81777 :   switch (op)
    5267                 :             :     {
    5268                 :        1029 :     case OPERATOR_PLUS:
    5269                 :        1029 :       *nc = *unc;
    5270                 :        1029 :       return true;
    5271                 :             : 
    5272                 :       71686 :     case OPERATOR_MINUS:
    5273                 :       71686 :       if (unc->is_int() || unc->is_rune())
    5274                 :             :         break;
    5275                 :        4061 :       else if (unc->is_float())
    5276                 :             :         {
    5277                 :        4051 :           mpfr_t uval;
    5278                 :        4051 :           unc->get_float(&uval);
    5279                 :        4051 :           mpfr_t val;
    5280                 :        4051 :           mpfr_init(val);
    5281                 :        4051 :           mpfr_neg(val, uval, MPFR_RNDN);
    5282                 :        4051 :           Type* utype = unc->type();
    5283                 :        4051 :           if (type != NULL
    5284                 :        4051 :               && type->is_abstract()
    5285                 :        6024 :               && type->is_numeric_type())
    5286                 :             :             utype = type;
    5287                 :        4051 :           nc->set_float(utype, val);
    5288                 :        4051 :           mpfr_clear(uval);
    5289                 :        4051 :           mpfr_clear(val);
    5290                 :        4051 :           return true;
    5291                 :             :         }
    5292                 :          10 :       else if (unc->is_complex())
    5293                 :             :         {
    5294                 :          10 :           mpc_t uval;
    5295                 :          10 :           unc->get_complex(&uval);
    5296                 :          10 :           mpc_t val;
    5297                 :          10 :           mpc_init2(val, mpc_precision);
    5298                 :          10 :           mpc_neg(val, uval, MPC_RNDNN);
    5299                 :          10 :           Type* utype = unc->type();
    5300                 :          10 :           if (type != NULL
    5301                 :          10 :               && type->is_abstract()
    5302                 :          16 :               && type->is_numeric_type())
    5303                 :             :             utype = type;
    5304                 :          10 :           nc->set_complex(utype, val);
    5305                 :          10 :           mpc_clear(uval);
    5306                 :          10 :           mpc_clear(val);
    5307                 :          10 :           return true;
    5308                 :             :         }
    5309                 :             :       else
    5310                 :           0 :         go_unreachable();
    5311                 :             : 
    5312                 :             :     case OPERATOR_XOR:
    5313                 :             :       break;
    5314                 :             : 
    5315                 :             :     case OPERATOR_NOT:
    5316                 :             :     case OPERATOR_AND:
    5317                 :             :     case OPERATOR_MULT:
    5318                 :             :       return false;
    5319                 :             : 
    5320                 :           0 :     default:
    5321                 :           0 :       go_unreachable();
    5322                 :             :     }
    5323                 :             : 
    5324                 :       76687 :   if (!unc->is_int() && !unc->is_rune())
    5325                 :             :     return false;
    5326                 :             : 
    5327                 :       76687 :   mpz_t uval;
    5328                 :       76687 :   if (unc->is_rune())
    5329                 :           0 :     unc->get_rune(&uval);
    5330                 :             :   else
    5331                 :       76687 :     unc->get_int(&uval);
    5332                 :       76687 :   mpz_t val;
    5333                 :       76687 :   mpz_init(val);
    5334                 :             : 
    5335                 :       76687 :   switch (op)
    5336                 :             :     {
    5337                 :       67625 :     case OPERATOR_MINUS:
    5338                 :       67625 :       mpz_neg(val, uval);
    5339                 :       67625 :       break;
    5340                 :             : 
    5341                 :             :     case OPERATOR_NOT:
    5342                 :             :       mpz_set_ui(val, mpz_cmp_si(uval, 0) == 0 ? 1 : 0);
    5343                 :             :       break;
    5344                 :             : 
    5345                 :        9062 :     case OPERATOR_XOR:
    5346                 :        9062 :       {
    5347                 :        9062 :         Type* utype = unc->type();
    5348                 :       18124 :         if (utype->integer_type() == NULL
    5349                 :        9062 :             || utype->integer_type()->is_abstract())
    5350                 :         124 :           mpz_com(val, uval);
    5351                 :             :         else
    5352                 :             :           {
    5353                 :             :             // The number of HOST_WIDE_INTs that it takes to represent
    5354                 :             :             // UVAL.
    5355                 :        8938 :             size_t count = ((mpz_sizeinbase(uval, 2)
    5356                 :             :                              + HOST_BITS_PER_WIDE_INT
    5357                 :        8938 :                              - 1)
    5358                 :             :                             / HOST_BITS_PER_WIDE_INT);
    5359                 :             : 
    5360                 :        8938 :             unsigned HOST_WIDE_INT* phwi = new unsigned HOST_WIDE_INT[count];
    5361                 :        8938 :             memset(phwi, 0, count * sizeof(HOST_WIDE_INT));
    5362                 :             : 
    5363                 :       17876 :             size_t obits = utype->integer_type()->bits();
    5364                 :             : 
    5365                 :       17876 :             if (!utype->integer_type()->is_unsigned() && mpz_sgn(uval) < 0)
    5366                 :             :               {
    5367                 :           0 :                 mpz_t adj;
    5368                 :           0 :                 mpz_init_set_ui(adj, 1);
    5369                 :           0 :                 mpz_mul_2exp(adj, adj, obits);
    5370                 :           0 :                 mpz_add(uval, uval, adj);
    5371                 :           0 :                 mpz_clear(adj);
    5372                 :             :               }
    5373                 :             : 
    5374                 :        8938 :             size_t ecount;
    5375                 :        8938 :             mpz_export(phwi, &ecount, -1, sizeof(HOST_WIDE_INT), 0, 0, uval);
    5376                 :        8938 :             go_assert(ecount <= count);
    5377                 :             : 
    5378                 :             :             // Trim down to the number of words required by the type.
    5379                 :        8938 :             size_t ocount = ((obits + HOST_BITS_PER_WIDE_INT - 1)
    5380                 :             :                              / HOST_BITS_PER_WIDE_INT);
    5381                 :        8938 :             go_assert(ocount <= count);
    5382                 :             : 
    5383                 :       17876 :             for (size_t i = 0; i < ocount; ++i)
    5384                 :        8938 :               phwi[i] = ~phwi[i];
    5385                 :             : 
    5386                 :        8938 :             size_t clearbits = ocount * HOST_BITS_PER_WIDE_INT - obits;
    5387                 :        8938 :             if (clearbits != 0)
    5388                 :        4176 :               phwi[ocount - 1] &= (((unsigned HOST_WIDE_INT) (HOST_WIDE_INT) -1)
    5389                 :        4176 :                                    >> clearbits);
    5390                 :             : 
    5391                 :        8938 :             mpz_import(val, ocount, -1, sizeof(HOST_WIDE_INT), 0, 0, phwi);
    5392                 :             : 
    5393                 :       17876 :             if (!utype->integer_type()->is_unsigned()
    5394                 :        8938 :                 && mpz_tstbit(val, obits - 1))
    5395                 :             :               {
    5396                 :           7 :                 mpz_t adj;
    5397                 :           7 :                 mpz_init_set_ui(adj, 1);
    5398                 :           7 :                 mpz_mul_2exp(adj, adj, obits);
    5399                 :           7 :                 mpz_sub(val, val, adj);
    5400                 :           7 :                 mpz_clear(adj);
    5401                 :             :               }
    5402                 :             : 
    5403                 :        8938 :             delete[] phwi;
    5404                 :             :           }
    5405                 :             :       }
    5406                 :             :       break;
    5407                 :             : 
    5408                 :           0 :     default:
    5409                 :           0 :       go_unreachable();
    5410                 :             :     }
    5411                 :             : 
    5412                 :       76687 :   if (unc->is_rune())
    5413                 :           0 :     nc->set_rune(NULL, val);
    5414                 :             :   else
    5415                 :       76687 :     nc->set_int(NULL, val);
    5416                 :             : 
    5417                 :       76687 :   mpz_clear(uval);
    5418                 :       76687 :   mpz_clear(val);
    5419                 :             : 
    5420                 :       76687 :   if (!nc->set_type(unc->type(), true, location))
    5421                 :             :     {
    5422                 :           0 :       *issued_error = true;
    5423                 :           0 :       return false;
    5424                 :             :     }
    5425                 :             :   return true;
    5426                 :             : }
    5427                 :             : 
    5428                 :             : // Return the integral constant value of a unary expression, if it has one.
    5429                 :             : 
    5430                 :             : bool
    5431                 :       38883 : Unary_expression::do_numeric_constant_value(Numeric_constant* nc)
    5432                 :             : {
    5433                 :       38883 :   if (this->is_error_expression())
    5434                 :             :     return false;
    5435                 :             : 
    5436                 :       38883 :   Numeric_constant unc;
    5437                 :       38883 :   if (!this->expr_->numeric_constant_value(&unc))
    5438                 :             :     return false;
    5439                 :       17453 :   bool issued_error;
    5440                 :       17453 :   bool r = Unary_expression::eval_constant(this->type_, this->op_, &unc,
    5441                 :             :                                            this->location(), nc,
    5442                 :             :                                            &issued_error);
    5443                 :       17453 :   if (issued_error)
    5444                 :           0 :     this->set_is_error();
    5445                 :             :   return r;
    5446                 :       38883 : }
    5447                 :             : 
    5448                 :             : // Return the boolean constant value of a unary expression, if it has one.
    5449                 :             : 
    5450                 :             : bool
    5451                 :       57888 : Unary_expression::do_boolean_constant_value(bool* val)
    5452                 :             : {
    5453                 :       57888 :   if (this->op_ == OPERATOR_NOT
    5454                 :       57888 :       && this->expr_->boolean_constant_value(val))
    5455                 :             :     {
    5456                 :         295 :       *val = !*val;
    5457                 :         295 :       return true;
    5458                 :             :     }
    5459                 :             :   return false;
    5460                 :             : }
    5461                 :             : 
    5462                 :             : // Return the type of a unary expression.
    5463                 :             : 
    5464                 :             : Type*
    5465                 :    20084251 : Unary_expression::do_type()
    5466                 :             : {
    5467                 :    20084251 :   if (this->type_ == NULL)
    5468                 :             :     {
    5469                 :    19585588 :       switch (this->op_)
    5470                 :             :         {
    5471                 :     3928254 :         case OPERATOR_AND:
    5472                 :     3928254 :           return Type::make_pointer_type(this->expr_->type());
    5473                 :             : 
    5474                 :    15657334 :         case OPERATOR_MULT:
    5475                 :    15657334 :           {
    5476                 :    15657334 :             if (this->expr_->is_type_expression())
    5477                 :       10468 :               return Type::make_pointer_type(this->expr_->type());
    5478                 :             : 
    5479                 :    15646866 :             Type* subtype = this->expr_->type();
    5480                 :    15646866 :             Type* points_to = subtype->points_to();
    5481                 :    15646866 :             if (points_to == NULL)
    5482                 :             :               {
    5483                 :           1 :                 this->report_error(_("expected pointer"));
    5484                 :           1 :                 this->type_ = Type::make_error_type();
    5485                 :           1 :                 return this->type_;
    5486                 :             :               }
    5487                 :             :             return points_to;
    5488                 :             :           }
    5489                 :             : 
    5490                 :           0 :         default:
    5491                 :           0 :           go_assert(saw_errors());
    5492                 :           0 :           return Type::make_error_type();
    5493                 :             :         }
    5494                 :             :     }
    5495                 :             : 
    5496                 :             :   return this->type_;
    5497                 :             : }
    5498                 :             : 
    5499                 :             : // Determine abstract types for a unary expression.
    5500                 :             : 
    5501                 :             : void
    5502                 :     4812545 : Unary_expression::do_determine_type(Gogo* gogo, const Type_context* context)
    5503                 :             : {
    5504                 :     4812545 :   switch (this->op_)
    5505                 :             :     {
    5506                 :      187849 :     case OPERATOR_PLUS:
    5507                 :      187849 :     case OPERATOR_MINUS:
    5508                 :      187849 :     case OPERATOR_NOT:
    5509                 :      187849 :     case OPERATOR_XOR:
    5510                 :      187849 :       {
    5511                 :      187849 :         if (this->type_ != NULL)
    5512                 :       20903 :           return;
    5513                 :             : 
    5514                 :      166946 :         Type* dummy;
    5515                 :      166946 :         Type_context subcontext(*context);
    5516                 :      166946 :         if (this->expr_->is_untyped(&dummy) && this->expr_->is_constant())
    5517                 :             :           {
    5518                 :             :             // We evaluate an untyped operator as untyped.  Then we
    5519                 :             :             // convert it to the desired type.  Otherwise we may, for
    5520                 :             :             // example, give a useless error for one more than the
    5521                 :             :             // most positive integer when it is the operand of a unary
    5522                 :             :             // minus.
    5523                 :       63103 :             subcontext.type = NULL;
    5524                 :       63103 :             subcontext.may_be_abstract = true;
    5525                 :             :           }
    5526                 :      166946 :         this->expr_->determine_type(gogo, &subcontext);
    5527                 :             : 
    5528                 :      166946 :         this->type_ = this->expr_->type();
    5529                 :             : 
    5530                 :             :         // If this is an untyped expression in a typed context, use
    5531                 :             :         // the context type.  If this doesn't work we'll report an
    5532                 :             :         // error later.
    5533                 :      166946 :         if (this->type_->is_abstract()
    5534                 :       63368 :             && !context->may_be_abstract
    5535                 :      220947 :             && context->type != NULL)
    5536                 :             :           {
    5537                 :       53521 :             if (context->type->interface_type() == NULL)
    5538                 :       53296 :               this->type_ = context->type;
    5539                 :             :             else
    5540                 :         225 :               this->type_ = this->type_->make_non_abstract_type();
    5541                 :             :           }
    5542                 :             :       }
    5543                 :      166946 :       break;
    5544                 :             : 
    5545                 :     2925912 :     case OPERATOR_AND:
    5546                 :             :       // Taking the address of something.
    5547                 :     2925912 :       {
    5548                 :     2925912 :         Type* subtype = (context->type == NULL
    5549                 :     2925912 :                          ? NULL
    5550                 :     2626403 :                          : context->type->points_to());
    5551                 :     2925912 :         Type_context subcontext(subtype, false);
    5552                 :     2925912 :         this->expr_->determine_type(gogo, &subcontext);
    5553                 :             :       }
    5554                 :     2925912 :       break;
    5555                 :             : 
    5556                 :     1698784 :     case OPERATOR_MULT:
    5557                 :     1698784 :       {
    5558                 :     1698784 :         if (this->expr_->is_type_expression())
    5559                 :             :           {
    5560                 :        4891 :             this->expr_->determine_type_no_context(gogo);
    5561                 :        4891 :             return;
    5562                 :             :           }
    5563                 :             : 
    5564                 :             :         // Indirecting through a pointer.
    5565                 :     1693893 :         Type* subtype = (context->type == NULL
    5566                 :     1693893 :                          ? NULL
    5567                 :       53135 :                          : Type::make_pointer_type(context->type));
    5568                 :     1693893 :         Type_context subcontext(subtype, false);
    5569                 :     1693893 :         this->expr_->determine_type(gogo, &subcontext);
    5570                 :             :       }
    5571                 :     1693893 :       break;
    5572                 :             : 
    5573                 :           0 :     default:
    5574                 :           0 :       go_unreachable();
    5575                 :             :     }
    5576                 :             : }
    5577                 :             : 
    5578                 :             : // Check types for a unary expression.
    5579                 :             : 
    5580                 :             : void
    5581                 :      323014 : Unary_expression::do_check_types(Gogo*)
    5582                 :             : {
    5583                 :      323014 :   if (this->is_error_expression())
    5584                 :             :     return;
    5585                 :             : 
    5586                 :      323012 :   Type* type = this->expr_->type();
    5587                 :      323012 :   if (type->is_error())
    5588                 :             :     {
    5589                 :           0 :       this->set_is_error();
    5590                 :           0 :       return;
    5591                 :             :     }
    5592                 :             : 
    5593                 :      323012 :   switch (this->op_)
    5594                 :             :     {
    5595                 :       68819 :     case OPERATOR_PLUS:
    5596                 :       68819 :     case OPERATOR_MINUS:
    5597                 :       76674 :       if (type->integer_type() == NULL
    5598                 :         103 :           && type->float_type() == NULL
    5599                 :      323016 :           && type->complex_type() == NULL)
    5600                 :           2 :         this->report_error(_("expected numeric type"));
    5601                 :             :       break;
    5602                 :             : 
    5603                 :       32185 :     case OPERATOR_NOT:
    5604                 :       32185 :       if (!type->is_boolean_type())
    5605                 :           0 :         this->report_error(_("expected boolean type"));
    5606                 :             :       break;
    5607                 :             : 
    5608                 :        2582 :     case OPERATOR_XOR:
    5609                 :        2582 :       if (type->integer_type() == NULL)
    5610                 :           2 :         this->report_error(_("expected integer"));
    5611                 :             :       break;
    5612                 :             : 
    5613                 :      181058 :     case OPERATOR_AND:
    5614                 :      181058 :       if (!this->expr_->is_addressable())
    5615                 :             :         {
    5616                 :        4826 :           if (!this->create_temp_)
    5617                 :             :             {
    5618                 :           1 :               go_error_at(this->location(), "invalid operand for unary %<&%>");
    5619                 :           1 :               this->set_is_error();
    5620                 :             :             }
    5621                 :             :         }
    5622                 :             :       else
    5623                 :      176232 :         this->expr_->issue_nil_check();
    5624                 :             :       break;
    5625                 :             : 
    5626                 :       38368 :     case OPERATOR_MULT:
    5627                 :       38368 :       if (this->expr_->is_type_expression())
    5628                 :             :         break;
    5629                 :             : 
    5630                 :             :       // Catching an invalid indirection of unsafe.Pointer here avoid
    5631                 :             :       // having to deal with TYPE_VOID in other places.
    5632                 :       38205 :       if (this->expr_->type()->is_unsafe_pointer_type())
    5633                 :             :         {
    5634                 :           1 :           go_error_at(this->location(),
    5635                 :             :                       "invalid indirect of %<unsafe.Pointer%>");
    5636                 :           1 :           this->set_is_error();
    5637                 :           1 :           return;
    5638                 :             :         }
    5639                 :             : 
    5640                 :             :       // Indirecting through a pointer.
    5641                 :       38204 :       if (type->points_to() == NULL)
    5642                 :           0 :         this->report_error(_("expected pointer"));
    5643                 :       38204 :       if (type->points_to()->is_error())
    5644                 :           1 :         this->set_is_error();
    5645                 :             :       break;
    5646                 :             : 
    5647                 :           0 :     default:
    5648                 :           0 :       go_unreachable();
    5649                 :             :     }
    5650                 :             : }
    5651                 :             : 
    5652                 :             : // Get the backend representation for a unary expression.
    5653                 :             : 
    5654                 :             : Bexpression*
    5655                 :     3038877 : Unary_expression::do_get_backend(Translate_context* context)
    5656                 :             : {
    5657                 :     3038877 :   Gogo* gogo = context->gogo();
    5658                 :     3038877 :   Location loc = this->location();
    5659                 :             : 
    5660                 :             :   // Taking the address of a set-and-use-temporary expression requires
    5661                 :             :   // setting the temporary and then taking the address.
    5662                 :     3038877 :   if (this->op_ == OPERATOR_AND)
    5663                 :             :     {
    5664                 :     1829716 :       Set_and_use_temporary_expression* sut =
    5665                 :     1829716 :         this->expr_->set_and_use_temporary_expression();
    5666                 :         619 :       if (sut != NULL)
    5667                 :             :         {
    5668                 :         619 :           Temporary_statement* temp = sut->temporary();
    5669                 :         619 :           Bvariable* bvar = temp->get_backend_variable(context);
    5670                 :         619 :           Bexpression* bvar_expr =
    5671                 :         619 :               gogo->backend()->var_expression(bvar, loc);
    5672                 :         619 :           Bexpression* bval = sut->expression()->get_backend(context);
    5673                 :             : 
    5674                 :         619 :           Named_object* fn = context->function();
    5675                 :         619 :           go_assert(fn != NULL);
    5676                 :         619 :           Bfunction* bfn =
    5677                 :         619 :               fn->func_value()->get_or_make_decl(gogo, fn);
    5678                 :         619 :           Bstatement* bassign =
    5679                 :         619 :               gogo->backend()->assignment_statement(bfn, bvar_expr, bval, loc);
    5680                 :         619 :           Bexpression* bvar_addr =
    5681                 :         619 :               gogo->backend()->address_expression(bvar_expr, loc);
    5682                 :         619 :           return gogo->backend()->compound_expression(bassign, bvar_addr, loc);
    5683                 :             :         }
    5684                 :             :     }
    5685                 :             : 
    5686                 :     3038258 :   Bexpression* ret;
    5687                 :     3038258 :   Bexpression* bexpr = this->expr_->get_backend(context);
    5688                 :     3038258 :   Btype* btype = (this->type_ == NULL
    5689                 :     3038258 :                   ? this->expr_->type()->get_backend(gogo)
    5690                 :      102248 :                   : this->type_->get_backend(gogo));
    5691                 :     3038258 :   switch (this->op_)
    5692                 :             :     {
    5693                 :         116 :     case OPERATOR_PLUS:
    5694                 :         116 :       ret = gogo->backend()->convert_expression(btype, bexpr, loc);
    5695                 :         116 :       break;
    5696                 :             : 
    5697                 :       10894 :     case OPERATOR_MINUS:
    5698                 :       10894 :       ret = gogo->backend()->unary_expression(this->op_, bexpr, loc);
    5699                 :       10894 :       ret = gogo->backend()->convert_expression(btype, ret, loc);
    5700                 :       10894 :       break;
    5701                 :             : 
    5702                 :       97390 :     case OPERATOR_NOT:
    5703                 :       97390 :     case OPERATOR_XOR:
    5704                 :       97390 :       ret = gogo->backend()->unary_expression(this->op_, bexpr, loc);
    5705                 :       97390 :       ret = gogo->backend()->convert_expression(btype, ret, loc);
    5706                 :       97390 :       break;
    5707                 :             : 
    5708                 :     1829097 :     case OPERATOR_AND:
    5709                 :     1829097 :       if (!this->create_temp_)
    5710                 :             :         {
    5711                 :             :           // We should not see a non-constant constructor here; cases
    5712                 :             :           // where we would see one should have been moved onto the
    5713                 :             :           // heap at parse time.  Taking the address of a nonconstant
    5714                 :             :           // constructor will not do what the programmer expects.
    5715                 :             : 
    5716                 :     1793758 :           go_assert(!this->expr_->is_composite_literal()
    5717                 :             :                     || this->expr_->is_static_initializer());
    5718                 :     1793758 :           if (this->expr_->classification() == EXPRESSION_UNARY)
    5719                 :             :             {
    5720                 :        3790 :               Unary_expression* ue =
    5721                 :             :                 static_cast<Unary_expression*>(this->expr_);
    5722                 :        3790 :               go_assert(ue->op() != OPERATOR_AND);
    5723                 :             :             }
    5724                 :             :         }
    5725                 :             : 
    5726                 :     1829097 :       if (this->is_gc_root_ || this->is_slice_init_)
    5727                 :             :         {
    5728                 :      289190 :           std::string var_name;
    5729                 :      289190 :           bool copy_to_heap = false;
    5730                 :      289190 :           if (this->is_gc_root_)
    5731                 :             :             {
    5732                 :             :               // Build a decl for a GC root variable.  GC roots are mutable, so
    5733                 :             :               // they cannot be represented as an immutable_struct in the
    5734                 :             :               // backend.
    5735                 :        2111 :               var_name = gogo->gc_root_name();
    5736                 :             :             }
    5737                 :             :           else
    5738                 :             :             {
    5739                 :             :               // Build a decl for a slice value initializer.  An immutable slice
    5740                 :             :               // value initializer may have to be copied to the heap if it
    5741                 :             :               // contains pointers in a non-constant context.
    5742                 :      287079 :               var_name = gogo->initializer_name();
    5743                 :             : 
    5744                 :      287079 :               Array_type* at = this->expr_->type()->array_type();
    5745                 :           0 :               go_assert(at != NULL);
    5746                 :             : 
    5747                 :             :               // If we are not copying the value to the heap, we will only
    5748                 :             :               // initialize the value once, so we can use this directly
    5749                 :             :               // rather than copying it.  In that case we can't make it
    5750                 :             :               // read-only, because the program is permitted to change it.
    5751                 :      287079 :               copy_to_heap = (context->function() != NULL
    5752                 :      287079 :                               || context->is_const());
    5753                 :             :             }
    5754                 :        2111 :           unsigned int flags = (Backend::variable_is_hidden
    5755                 :             :                                 | Backend::variable_address_is_taken);
    5756                 :        2111 :           if (copy_to_heap)
    5757                 :             :             flags |= Backend::variable_is_constant;
    5758                 :      289190 :           Bvariable* implicit =
    5759                 :      289190 :             gogo->backend()->implicit_variable(var_name, "", btype, flags, 0);
    5760                 :      289190 :           gogo->backend()->implicit_variable_set_init(implicit, var_name, btype,
    5761                 :             :                                                       flags, bexpr);
    5762                 :      289190 :           bexpr = gogo->backend()->var_expression(implicit, loc);
    5763                 :             : 
    5764                 :             :           // If we are not copying a slice initializer to the heap,
    5765                 :             :           // then it can be changed by the program, so if it can
    5766                 :             :           // contain pointers we must register it as a GC root.
    5767                 :      289190 :           if (this->is_slice_init_
    5768                 :      287079 :               && !copy_to_heap
    5769                 :      298053 :               && this->expr_->type()->has_pointer())
    5770                 :             :             {
    5771                 :        2660 :               Bexpression* root =
    5772                 :        2660 :                   gogo->backend()->var_expression(implicit, loc);
    5773                 :        2660 :               root = gogo->backend()->address_expression(root, loc);
    5774                 :        2660 :               Type* type = Type::make_pointer_type(this->expr_->type());
    5775                 :        2660 :               gogo->add_gc_root(Expression::make_backend(root, type, loc));
    5776                 :             :             }
    5777                 :      289190 :         }
    5778                 :     1539907 :       else if ((this->expr_->is_composite_literal()
    5779                 :     1484508 :                 || this->expr_->string_expression() != NULL)
    5780                 :     2190666 :                && this->expr_->is_static_initializer())
    5781                 :             :         {
    5782                 :      706158 :           std::string var_name(gogo->initializer_name());
    5783                 :      706158 :           unsigned int flags = (Backend::variable_is_hidden
    5784                 :             :                                 | Backend::variable_address_is_taken);
    5785                 :      706158 :           Bvariable* decl =
    5786                 :      706158 :             gogo->backend()->immutable_struct(var_name, "", flags, btype, loc);
    5787                 :      706158 :           gogo->backend()->immutable_struct_set_init(decl, var_name, flags,
    5788                 :             :                                                      btype, loc, bexpr);
    5789                 :      706158 :           bexpr = gogo->backend()->var_expression(decl, loc);
    5790                 :      706158 :         }
    5791                 :      833749 :       else if (this->expr_->is_constant())
    5792                 :             :         {
    5793                 :       14716 :           std::string var_name(gogo->initializer_name());
    5794                 :       14716 :           unsigned int flags = (Backend::variable_is_hidden
    5795                 :             :                                 | Backend::variable_is_constant
    5796                 :             :                                 | Backend::variable_address_is_taken);
    5797                 :       14716 :           Bvariable* decl =
    5798                 :       14716 :             gogo->backend()->implicit_variable(var_name, "", btype, flags, 0);
    5799                 :       14716 :           gogo->backend()->implicit_variable_set_init(decl, var_name, btype,
    5800                 :             :                                                       flags, bexpr);
    5801                 :       14716 :           bexpr = gogo->backend()->var_expression(decl, loc);
    5802                 :       14716 :         }
    5803                 :             : 
    5804                 :     1829097 :       go_assert(!this->create_temp_ || this->expr_->is_multi_eval_safe());
    5805                 :     1829097 :       ret = gogo->backend()->address_expression(bexpr, loc);
    5806                 :     1829097 :       break;
    5807                 :             : 
    5808                 :     1100761 :     case OPERATOR_MULT:
    5809                 :     1100761 :       {
    5810                 :     1100761 :         go_assert(this->expr_->type()->points_to() != NULL);
    5811                 :             : 
    5812                 :     1100761 :         Type* ptype = this->expr_->type()->points_to();
    5813                 :     1100761 :         Btype* pbtype = ptype->get_backend(gogo);
    5814                 :     1100761 :         switch (this->requires_nil_check(gogo))
    5815                 :             :           {
    5816                 :             :             case NIL_CHECK_NOT_NEEDED:
    5817                 :             :               break;
    5818                 :           0 :             case NIL_CHECK_ERROR_ENCOUNTERED:
    5819                 :           0 :               {
    5820                 :           0 :                 go_assert(saw_errors());
    5821                 :           0 :                 return gogo->backend()->error_expression();
    5822                 :             :               }
    5823                 :       80146 :             case NIL_CHECK_NEEDED:
    5824                 :       80146 :               {
    5825                 :       80146 :                 go_assert(this->expr_->is_multi_eval_safe());
    5826                 :             : 
    5827                 :             :                 // If we're nil-checking the result of a set-and-use-temporary
    5828                 :             :                 // expression, then pick out the target temp and use that
    5829                 :             :                 // for the final result of the conditional.
    5830                 :       80146 :                 Bexpression* tbexpr = bexpr;
    5831                 :       80146 :                 Bexpression* ubexpr = bexpr;
    5832                 :       80146 :                 Set_and_use_temporary_expression* sut =
    5833                 :       80146 :                     this->expr_->set_and_use_temporary_expression();
    5834                 :           0 :                 if (sut != NULL) {
    5835                 :           0 :                   Temporary_statement* temp = sut->temporary();
    5836                 :           0 :                   Bvariable* bvar = temp->get_backend_variable(context);
    5837                 :           0 :                   ubexpr = gogo->backend()->var_expression(bvar, loc);
    5838                 :             :                 }
    5839                 :       80146 :                 Bexpression* nil =
    5840                 :       80146 :                     Expression::make_nil(loc)->get_backend(context);
    5841                 :       80146 :                 Bexpression* compare =
    5842                 :       80146 :                     gogo->backend()->binary_expression(OPERATOR_EQEQ, tbexpr,
    5843                 :             :                                                        nil, loc);
    5844                 :       80146 :                 Expression* crash = Runtime::make_call(gogo, Runtime::PANIC_MEM,
    5845                 :             :                                                        loc, 0);
    5846                 :       80146 :                 crash->determine_type_no_context(gogo);
    5847                 :       80146 :                 Bexpression* bcrash = crash->get_backend(context);
    5848                 :       80146 :                 Bfunction* bfn = context->function()->func_value()->get_decl();
    5849                 :       80146 :                 bexpr = gogo->backend()->conditional_expression(bfn, btype,
    5850                 :             :                                                                 compare,
    5851                 :             :                                                                 bcrash, ubexpr,
    5852                 :             :                                                                 loc);
    5853                 :       80146 :                 break;
    5854                 :             :               }
    5855                 :           0 :             case NIL_CHECK_DEFAULT:
    5856                 :           0 :               go_unreachable();
    5857                 :             :           }
    5858                 :     1100761 :         ret = gogo->backend()->indirect_expression(pbtype, bexpr, false, loc);
    5859                 :             :       }
    5860                 :     1100761 :       break;
    5861                 :             : 
    5862                 :           0 :     default:
    5863                 :           0 :       go_unreachable();
    5864                 :             :     }
    5865                 :             : 
    5866                 :             :   return ret;
    5867                 :             : }
    5868                 :             : 
    5869                 :             : // Export a unary expression.
    5870                 :             : 
    5871                 :             : void
    5872                 :        3143 : Unary_expression::do_export(Export_function_body* efb) const
    5873                 :             : {
    5874                 :        3143 :   switch (this->op_)
    5875                 :             :     {
    5876                 :           0 :     case OPERATOR_PLUS:
    5877                 :           0 :       efb->write_c_string("+");
    5878                 :           0 :       break;
    5879                 :         112 :     case OPERATOR_MINUS:
    5880                 :         112 :       efb->write_c_string("-");
    5881                 :         112 :       break;
    5882                 :         304 :     case OPERATOR_NOT:
    5883                 :         304 :       efb->write_c_string("!");
    5884                 :         304 :       break;
    5885                 :          44 :     case OPERATOR_XOR:
    5886                 :          44 :       efb->write_c_string("^");
    5887                 :          44 :       break;
    5888                 :        1003 :     case OPERATOR_AND:
    5889                 :        1003 :       efb->write_c_string("&");
    5890                 :        1003 :       break;
    5891                 :        1680 :     case OPERATOR_MULT:
    5892                 :        1680 :       efb->write_c_string("*");
    5893                 :        1680 :       break;
    5894                 :           0 :     default:
    5895                 :           0 :       go_unreachable();
    5896                 :             :     }
    5897                 :        3143 :   this->expr_->export_expression(efb);
    5898                 :        3143 : }
    5899                 :             : 
    5900                 :             : // Import a unary expression.
    5901                 :             : 
    5902                 :             : Expression*
    5903                 :        9633 : Unary_expression::do_import(Import_expression* imp, Location loc)
    5904                 :             : {
    5905                 :        9633 :   Operator op;
    5906                 :        9633 :   switch (imp->get_char())
    5907                 :             :     {
    5908                 :             :     case '+':
    5909                 :             :       op = OPERATOR_PLUS;
    5910                 :             :       break;
    5911                 :        7888 :     case '-':
    5912                 :        7888 :       op = OPERATOR_MINUS;
    5913                 :        7888 :       break;
    5914                 :         537 :     case '!':
    5915                 :         537 :       op = OPERATOR_NOT;
    5916                 :         537 :       break;
    5917                 :          74 :     case '^':
    5918                 :          74 :       op = OPERATOR_XOR;
    5919                 :          74 :       break;
    5920                 :         640 :     case '&':
    5921                 :         640 :       op = OPERATOR_AND;
    5922                 :         640 :       break;
    5923                 :         494 :     case '*':
    5924                 :         494 :       op = OPERATOR_MULT;
    5925                 :         494 :       break;
    5926                 :           0 :     default:
    5927                 :           0 :       go_unreachable();
    5928                 :             :     }
    5929                 :        9633 :   if (imp->version() < EXPORT_FORMAT_V3)
    5930                 :           0 :     imp->require_c_string(" ");
    5931                 :        9633 :   Expression* expr = Expression::import_expression(imp, loc);
    5932                 :        9633 :   return Expression::make_unary(op, expr, loc);
    5933                 :             : }
    5934                 :             : 
    5935                 :             : // Dump ast representation of an unary expression.
    5936                 :             : 
    5937                 :             : void
    5938                 :           0 : Unary_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
    5939                 :             : {
    5940                 :           0 :   ast_dump_context->dump_operator(this->op_);
    5941                 :           0 :   ast_dump_context->ostream() << "(";
    5942                 :           0 :   ast_dump_context->dump_expression(this->expr_);
    5943                 :           0 :   ast_dump_context->ostream() << ") ";
    5944                 :           0 : }
    5945                 :             : 
    5946                 :             : // Make a unary expression.
    5947                 :             : 
    5948                 :             : Expression*
    5949                 :     3784310 : Expression::make_unary(Operator op, Expression* expr, Location location)
    5950                 :             : {
    5951                 :     3784310 :   return new Unary_expression(op, expr, location);
    5952                 :             : }
    5953                 :             : 
    5954                 :             : Expression*
    5955                 :      998757 : Expression::make_dereference(Expression* ptr,
    5956                 :             :                              Nil_check_classification docheck,
    5957                 :             :                              Location location)
    5958                 :             : {
    5959                 :      998757 :   Expression* deref = Expression::make_unary(OPERATOR_MULT, ptr, location);
    5960                 :      998757 :   if (docheck == NIL_CHECK_NEEDED)
    5961                 :       13135 :     deref->unary_expression()->set_requires_nil_check(true);
    5962                 :      985622 :   else if (docheck == NIL_CHECK_NOT_NEEDED)
    5963                 :      434313 :     deref->unary_expression()->set_requires_nil_check(false);
    5964                 :      998757 :   return deref;
    5965                 :             : }
    5966                 :             : 
    5967                 :             : // If this is an indirection through a pointer, return the expression
    5968                 :             : // being pointed through.  Otherwise return this.
    5969                 :             : 
    5970                 :             : Expression*
    5971                 :        9703 : Expression::deref()
    5972                 :             : {
    5973                 :        9703 :   if (this->classification_ == EXPRESSION_UNARY)
    5974                 :             :     {
    5975                 :        4510 :       Unary_expression* ue = static_cast<Unary_expression*>(this);
    5976                 :        4510 :       if (ue->op() == OPERATOR_MULT)
    5977                 :        4510 :         return ue->operand();
    5978                 :             :     }
    5979                 :             :   return this;
    5980                 :             : }
    5981                 :             : 
    5982                 :             : // Class Binary_expression.
    5983                 :             : 
    5984                 :             : // Traversal.
    5985                 :             : 
    5986                 :             : int
    5987                 :    13841033 : Binary_expression::do_traverse(Traverse* traverse)
    5988                 :             : {
    5989                 :    13841033 :   int t = Expression::traverse(&this->left_, traverse);
    5990                 :    13841033 :   if (t == TRAVERSE_EXIT)
    5991                 :             :     return TRAVERSE_EXIT;
    5992                 :    13459541 :   return Expression::traverse(&this->right_, traverse);
    5993                 :             : }
    5994                 :             : 
    5995                 :             : // Return whether a binary expression is untyped.
    5996                 :             : 
    5997                 :             : bool
    5998                 :   103757272 : Binary_expression::do_is_untyped(Type** ptype) const
    5999                 :             : {
    6000                 :   103757272 :   if (this->type_ != NULL)
    6001                 :       13270 :     return Expression::is_untyped_type(this->type_, ptype);
    6002                 :             : 
    6003                 :   103744002 :   switch (this->op_)
    6004                 :             :     {
    6005                 :      668400 :     case OPERATOR_EQEQ:
    6006                 :      668400 :     case OPERATOR_NOTEQ:
    6007                 :      668400 :     case OPERATOR_LT:
    6008                 :      668400 :     case OPERATOR_LE:
    6009                 :      668400 :     case OPERATOR_GT:
    6010                 :      668400 :     case OPERATOR_GE:
    6011                 :             :       // Comparisons are untyped by default.
    6012                 :      668400 :       *ptype = Type::make_boolean_type();
    6013                 :      668400 :       return true;
    6014                 :             : 
    6015                 :      204795 :     case OPERATOR_LSHIFT:
    6016                 :      204795 :     case OPERATOR_RSHIFT:
    6017                 :             :       // A shift operation is untyped if the left hand expression is
    6018                 :             :       // untyped.  The right hand expression is irrelevant.
    6019                 :      204795 :       return this->left_->is_untyped(ptype);
    6020                 :             : 
    6021                 :   102870807 :     default:
    6022                 :   102870807 :       break;
    6023                 :             :     }
    6024                 :             : 
    6025                 :   102870807 :   Type* tleft;
    6026                 :   102870807 :   Type* tright;
    6027                 :   102870807 :   if (!this->left_->is_untyped(&tleft)
    6028                 :   102870807 :       || !this->right_->is_untyped(&tright))
    6029                 :      114846 :     return false;
    6030                 :             : 
    6031                 :             :   // If both sides are numeric, pick a type based on the kind.
    6032                 :   102755961 :   enum kind { INT, RUNE, FLOAT, COMPLEX };
    6033                 :   102755961 :   enum kind kleft, kright;
    6034                 :             : 
    6035                 :   102755961 :   if (tleft->integer_type() != NULL)
    6036                 :       34974 :     kleft = tleft->integer_type()->is_rune() ? RUNE : INT;
    6037                 :   102738474 :   else if (tleft->float_type() != NULL)
    6038                 :             :     kleft = FLOAT;
    6039                 :   102755962 :   else if (tleft->complex_type() != NULL)
    6040                 :             :     kleft = COMPLEX;
    6041                 :             :   else
    6042                 :             :     {
    6043                 :             :       // Not numeric.  If the types are different, we will report an
    6044                 :             :       // error later.
    6045                 :   102738126 :       *ptype = tleft;
    6046                 :   102738126 :       return true;
    6047                 :             :     }
    6048                 :             : 
    6049                 :       17835 :   if (tright->integer_type() != NULL)
    6050                 :       34566 :     kright = tright->integer_type()->is_rune() ? RUNE : INT;
    6051                 :         552 :   else if (tright->float_type() != NULL)
    6052                 :             :     kright = FLOAT;
    6053                 :       17897 :   else if (tright->complex_type() != NULL)
    6054                 :             :     kright = COMPLEX;
    6055                 :             :   else
    6056                 :             :     {
    6057                 :             :       // Types are different.  We will report an error later.
    6058                 :           0 :       *ptype = tleft;
    6059                 :           0 :       return true;
    6060                 :             :     }
    6061                 :             : 
    6062                 :       17835 :   if (kleft > kright)
    6063                 :         303 :     *ptype = tleft;
    6064                 :             :   else
    6065                 :       17532 :     *ptype = tright;
    6066                 :             : 
    6067                 :             :   return true;
    6068                 :             : }
    6069                 :             : 
    6070                 :             : // Return whether this expression may be used as a static initializer.
    6071                 :             : 
    6072                 :             : bool
    6073                 :      471511 : Binary_expression::do_is_static_initializer() const
    6074                 :             : {
    6075                 :      471511 :   if (!this->left_->is_static_initializer()
    6076                 :      471511 :       || !this->right_->is_static_initializer())
    6077                 :         351 :     return false;
    6078                 :             : 
    6079                 :             :   // Addresses can be static initializers, but we can't implement
    6080                 :             :   // arbitray binary expressions of them.
    6081                 :      471160 :   Unary_expression* lu = this->left_->unary_expression();
    6082                 :      471160 :   Unary_expression* ru = this->right_->unary_expression();
    6083                 :      471160 :   if (lu != NULL && lu->op() == OPERATOR_AND)
    6084                 :             :     {
    6085                 :           0 :       if (ru != NULL && ru->op() == OPERATOR_AND)
    6086                 :           0 :         return this->op_ == OPERATOR_MINUS;
    6087                 :             :       else
    6088                 :           0 :         return this->op_ == OPERATOR_PLUS || this->op_ == OPERATOR_MINUS;
    6089                 :             :     }
    6090                 :      471160 :   else if (ru != NULL && ru->op() == OPERATOR_AND)
    6091                 :           0 :     return this->op_ == OPERATOR_PLUS || this->op_ == OPERATOR_MINUS;
    6092                 :             : 
    6093                 :             :   // Other cases should resolve in the backend.
    6094                 :             :   return true;
    6095                 :             : }
    6096                 :             : 
    6097                 :             : // Return the type to use for a binary operation on operands of
    6098                 :             : // LEFT_TYPE and RIGHT_TYPE.  These are the types of constants and as
    6099                 :             : // such may be NULL or abstract.
    6100                 :             : 
    6101                 :             : bool
    6102                 :     1302967 : Binary_expression::operation_type(Operator op, Type* left_type,
    6103                 :             :                                   Type* right_type, Type** result_type)
    6104                 :             : {
    6105                 :     1302967 :   if (left_type != right_type
    6106                 :      185212 :       && !left_type->is_abstract()
    6107                 :       81373 :       && !right_type->is_abstract()
    6108                 :       81090 :       && left_type->base() != right_type->base()
    6109                 :             :       && op != OPERATOR_LSHIFT
    6110                 :     1309611 :       && op != OPERATOR_RSHIFT)
    6111                 :             :     {
    6112                 :             :       // May be a type error--let it be diagnosed elsewhere.
    6113                 :             :       return false;
    6114                 :             :     }
    6115                 :             : 
    6116                 :     1302956 :   if (op == OPERATOR_LSHIFT || op == OPERATOR_RSHIFT)
    6117                 :             :     {
    6118                 :      110374 :       if (left_type->integer_type() != NULL)
    6119                 :      110365 :         *result_type = left_type;
    6120                 :             :       else
    6121                 :           9 :         *result_type = Type::make_abstract_integer_type();
    6122                 :             :     }
    6123                 :     1192582 :   else if (!left_type->is_abstract() && left_type->named_type() != NULL)
    6124                 :      911450 :     *result_type = left_type;
    6125                 :      281132 :   else if (!right_type->is_abstract() && right_type->named_type() != NULL)
    6126                 :         306 :     *result_type = right_type;
    6127                 :      280826 :   else if (!left_type->is_abstract())
    6128                 :           0 :     *result_type = left_type;
    6129                 :      280826 :   else if (!right_type->is_abstract())
    6130                 :           0 :     *result_type = right_type;
    6131                 :      280826 :   else if (left_type->complex_type() != NULL)
    6132                 :        1431 :     *result_type = left_type;
    6133                 :      279395 :   else if (right_type->complex_type() != NULL)
    6134                 :         400 :     *result_type = right_type;
    6135                 :      278995 :   else if (left_type->float_type() != NULL)
    6136                 :        2613 :     *result_type = left_type;
    6137                 :      276382 :   else if (right_type->float_type() != NULL)
    6138                 :         148 :     *result_type = right_type;
    6139                 :      463360 :   else if (left_type->integer_type() != NULL
    6140                 :      187126 :            && left_type->integer_type()->is_rune())
    6141                 :        2104 :     *result_type = left_type;
    6142                 :      459152 :   else if (right_type->integer_type() != NULL
    6143                 :      185022 :            && right_type->integer_type()->is_rune())
    6144                 :          65 :     *result_type = right_type;
    6145                 :             :   else
    6146                 :      274065 :     *result_type = left_type;
    6147                 :             : 
    6148                 :             :   return true;
    6149                 :             : }
    6150                 :             : 
    6151                 :             : // Convert an integer comparison code and an operator to a boolean
    6152                 :             : // value.
    6153                 :             : 
    6154                 :             : bool
    6155                 :        2996 : Binary_expression::cmp_to_bool(Operator op, int cmp)
    6156                 :             : {
    6157                 :        2996 :   switch (op)
    6158                 :             :     {
    6159                 :        2268 :     case OPERATOR_EQEQ:
    6160                 :        2268 :       return cmp == 0;
    6161                 :         696 :       break;
    6162                 :         696 :     case OPERATOR_NOTEQ:
    6163                 :         696 :       return cmp != 0;
    6164                 :          14 :       break;
    6165                 :          14 :     case OPERATOR_LT:
    6166                 :          14 :       return cmp < 0;
    6167                 :           0 :       break;
    6168                 :           0 :     case OPERATOR_LE:
    6169                 :           0 :       return cmp <= 0;
    6170                 :           8 :     case OPERATOR_GT:
    6171                 :           8 :       return cmp > 0;
    6172                 :          10 :     case OPERATOR_GE:
    6173                 :          10 :       return cmp >= 0;
    6174                 :           0 :     default:
    6175                 :           0 :       go_unreachable();
    6176                 :             :     }
    6177                 :             : }
    6178                 :             : 
    6179                 :             : // Compare constants according to OP.
    6180                 :             : 
    6181                 :             : bool
    6182                 :         936 : Binary_expression::compare_constant(Operator op, Numeric_constant* left_nc,
    6183                 :             :                                     Numeric_constant* right_nc,
    6184                 :             :                                     Location location, bool* result)
    6185                 :             : {
    6186                 :         936 :   Type* left_type = left_nc->type();
    6187                 :         936 :   Type* right_type = right_nc->type();
    6188                 :             : 
    6189                 :         936 :   Type* type;
    6190                 :         936 :   if (!Binary_expression::operation_type(op, left_type, right_type, &type))
    6191                 :             :     return false;
    6192                 :             : 
    6193                 :             :   // When comparing an untyped operand to a typed operand, we are
    6194                 :             :   // effectively coercing the untyped operand to the other operand's
    6195                 :             :   // type, so make sure that is valid.
    6196                 :         936 :   if (!left_nc->set_type(type, true, location)
    6197                 :         936 :       || !right_nc->set_type(type, true, location))
    6198                 :           0 :     return false;
    6199                 :             : 
    6200                 :         936 :   bool ret;
    6201                 :         936 :   int cmp;
    6202                 :         936 :   if (type->complex_type() != NULL)
    6203                 :             :     {
    6204                 :           7 :       if (op != OPERATOR_EQEQ && op != OPERATOR_NOTEQ)
    6205                 :             :         return false;
    6206                 :           7 :       ret = Binary_expression::compare_complex(left_nc, right_nc, &cmp);
    6207                 :             :     }
    6208                 :         929 :   else if (type->float_type() != NULL)
    6209                 :          28 :     ret = Binary_expression::compare_float(left_nc, right_nc, &cmp);
    6210                 :             :   else
    6211                 :         901 :     ret = Binary_expression::compare_integer(left_nc, right_nc, &cmp);
    6212                 :             : 
    6213                 :         936 :   if (ret)
    6214                 :         936 :     *result = Binary_expression::cmp_to_bool(op, cmp);
    6215                 :             : 
    6216                 :             :   return ret;
    6217                 :             : }
    6218                 :             : 
    6219                 :             : // Compare integer constants.
    6220                 :             : 
    6221                 :             : bool
    6222                 :         901 : Binary_expression::compare_integer(const Numeric_constant* left_nc,
    6223                 :             :                                    const Numeric_constant* right_nc,
    6224                 :             :                                    int* cmp)
    6225                 :             : {
    6226                 :         901 :   mpz_t left_val;
    6227                 :         901 :   if (!left_nc->to_int(&left_val))
    6228                 :             :     return false;
    6229                 :         901 :   mpz_t right_val;
    6230                 :         901 :   if (!right_nc->to_int(&right_val))
    6231                 :             :     {
    6232                 :           0 :       mpz_clear(left_val);
    6233                 :           0 :       return false;
    6234                 :             :     }
    6235                 :             : 
    6236                 :         901 :   *cmp = mpz_cmp(left_val, right_val);
    6237                 :             : 
    6238                 :         901 :   mpz_clear(left_val);
    6239                 :         901 :   mpz_clear(right_val);
    6240                 :             : 
    6241                 :         901 :   return true;
    6242                 :             : }
    6243                 :             : 
    6244                 :             : // Compare floating point constants.
    6245                 :             : 
    6246                 :             : bool
    6247                 :          28 : Binary_expression::compare_float(const Numeric_constant* left_nc,
    6248                 :             :                                  const Numeric_constant* right_nc,
    6249                 :             :                                  int* cmp)
    6250                 :             : {
    6251                 :          28 :   mpfr_t left_val;
    6252                 :          28 :   if (!left_nc->to_float(&left_val))
    6253                 :             :     return false;
    6254                 :          28 :   mpfr_t right_val;
    6255                 :          28 :   if (!right_nc->to_float(&right_val))
    6256                 :             :     {
    6257                 :           0 :       mpfr_clear(left_val);
    6258                 :           0 :       return false;
    6259                 :             :     }
    6260                 :             : 
    6261                 :             :   // We already coerced both operands to the same type.  If that type
    6262                 :             :   // is not an abstract type, we need to round the values accordingly.
    6263                 :          28 :   Type* type = left_nc->type();
    6264                 :          28 :   if (!type->is_abstract() && type->float_type() != NULL)
    6265                 :             :     {
    6266                 :          30 :       int bits = type->float_type()->bits();
    6267                 :          15 :       mpfr_prec_round(left_val, bits, MPFR_RNDN);
    6268                 :          15 :       mpfr_prec_round(right_val, bits, MPFR_RNDN);
    6269                 :             :     }
    6270                 :             : 
    6271                 :          28 :   *cmp = mpfr_cmp(left_val, right_val);
    6272                 :             : 
    6273                 :          28 :   mpfr_clear(left_val);
    6274                 :          28 :   mpfr_clear(right_val);
    6275                 :             : 
    6276                 :          28 :   return true;
    6277                 :             : }
    6278                 :             : 
    6279                 :             : // Compare complex constants.  Complex numbers may only be compared
    6280                 :             : // for equality.
    6281                 :             : 
    6282                 :             : bool
    6283                 :           7 : Binary_expression::compare_complex(const Numeric_constant* left_nc,
    6284                 :             :                                    const Numeric_constant* right_nc,
    6285                 :             :                                    int* cmp)
    6286                 :             : {
    6287                 :           7 :   mpc_t left_val;
    6288                 :           7 :   if (!left_nc->to_complex(&left_val))
    6289                 :             :     return false;
    6290                 :           7 :   mpc_t right_val;
    6291                 :           7 :   if (!right_nc->to_complex(&right_val))
    6292                 :             :     {
    6293                 :           0 :       mpc_clear(left_val);
    6294                 :           0 :       return false;
    6295                 :             :     }
    6296                 :             : 
    6297                 :             :   // We already coerced both operands to the same type.  If that type
    6298                 :             :   // is not an abstract type, we need to round the values accordingly.
    6299                 :           7 :   Type* type = left_nc->type();
    6300                 :           7 :   if (!type->is_abstract() && type->complex_type() != NULL)
    6301                 :             :     {
    6302                 :           4 :       int bits = type->complex_type()->bits();
    6303                 :           2 :       mpfr_prec_round(mpc_realref(left_val), bits / 2, MPFR_RNDN);
    6304                 :           2 :       mpfr_prec_round(mpc_imagref(left_val), bits / 2, MPFR_RNDN);
    6305                 :           2 :       mpfr_prec_round(mpc_realref(right_val), bits / 2, MPFR_RNDN);
    6306                 :           2 :       mpfr_prec_round(mpc_imagref(right_val), bits / 2, MPFR_RNDN);
    6307                 :             :     }
    6308                 :             : 
    6309                 :           7 :   *cmp = mpc_cmp(left_val, right_val) != 0;
    6310                 :             : 
    6311                 :           7 :   mpc_clear(left_val);
    6312                 :           7 :   mpc_clear(right_val);
    6313                 :             : 
    6314                 :           7 :   return true;
    6315                 :             : }
    6316                 :             : 
    6317                 :             : // Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC.  Return
    6318                 :             : // true if this could be done, false if not.  Issue errors at LOCATION
    6319                 :             : // as appropriate, and sets *ISSUED_ERROR if it did.
    6320                 :             : 
    6321                 :             : bool
    6322                 :      293503 : Binary_expression::eval_constant(Operator op, Numeric_constant* left_nc,
    6323                 :             :                                  Numeric_constant* right_nc,
    6324                 :             :                                  Location location, Numeric_constant* nc,
    6325                 :             :                                  bool* issued_error)
    6326                 :             : {
    6327                 :      293503 :   *issued_error = false;
    6328                 :      293503 :   switch (op)
    6329                 :             :     {
    6330                 :             :     case OPERATOR_OROR:
    6331                 :             :     case OPERATOR_ANDAND:
    6332                 :             :     case OPERATOR_EQEQ:
    6333                 :             :     case OPERATOR_NOTEQ:
    6334                 :             :     case OPERATOR_LT:
    6335                 :             :     case OPERATOR_LE:
    6336                 :             :     case OPERATOR_GT:
    6337                 :             :     case OPERATOR_GE:
    6338                 :             :       // These return boolean values, not numeric.
    6339                 :             :       return false;
    6340                 :      293503 :     default:
    6341                 :      293503 :       break;
    6342                 :             :     }
    6343                 :             : 
    6344                 :      293503 :   Type* left_type = left_nc->type();
    6345                 :      293503 :   Type* right_type = right_nc->type();
    6346                 :             : 
    6347                 :      293503 :   Type* type;
    6348                 :      293503 :   if (!Binary_expression::operation_type(op, left_type, right_type, &type))
    6349                 :             :     return false;
    6350                 :             : 
    6351                 :      293503 :   bool is_shift = op == OPERATOR_LSHIFT || op == OPERATOR_RSHIFT;
    6352                 :             : 
    6353                 :             :   // When combining an untyped operand with a typed operand, we are
    6354                 :             :   // effectively coercing the untyped operand to the other operand's
    6355                 :             :   // type, so make sure that is valid.
    6356                 :      293503 :   if (!left_nc->set_type(type, true, location))
    6357                 :             :     return false;
    6358                 :      293503 :   if (!is_shift && !right_nc->set_type(type, true, location))
    6359                 :             :     return false;
    6360                 :      293503 :   if (is_shift
    6361                 :      293503 :       && right_type->integer_type() == NULL
    6362                 :      293503 :       && !right_type->is_abstract())
    6363                 :             :     return false;
    6364                 :             : 
    6365                 :      293503 :   bool r;
    6366                 :      293503 :   if (type->complex_type() != NULL)
    6367                 :        1017 :     r = Binary_expression::eval_complex(op, left_nc, right_nc, location, nc,
    6368                 :             :                                         issued_error);
    6369                 :      292486 :   else if (type->float_type() != NULL)
    6370                 :        1832 :     r = Binary_expression::eval_float(op, left_nc, right_nc, location, nc,
    6371                 :             :                                       issued_error);
    6372                 :             :   else
    6373                 :      290654 :     r = Binary_expression::eval_integer(op, left_nc, right_nc, location, nc,
    6374                 :             :                                         issued_error);
    6375                 :             : 
    6376                 :      293503 :   if (r)
    6377                 :             :     {
    6378                 :      293503 :       r = nc->set_type(type, true, location);
    6379                 :      293503 :       if (!r)
    6380                 :          12 :         *issued_error = true;
    6381                 :             :     }
    6382                 :             : 
    6383                 :             :   return r;
    6384                 :             : }
    6385                 :             : 
    6386                 :             : // Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC, using
    6387                 :             : // integer operations.  Return true if this could be done, false if
    6388                 :             : // not.
    6389                 :             : 
    6390                 :             : bool
    6391                 :      290654 : Binary_expression::eval_integer(Operator op, const Numeric_constant* left_nc,
    6392                 :             :                                 const Numeric_constant* right_nc,
    6393                 :             :                                 Location location, Numeric_constant* nc,
    6394                 :             :                                 bool* issued_error)
    6395                 :             : {
    6396                 :      290654 :   mpz_t left_val;
    6397                 :      290654 :   if (!left_nc->to_int(&left_val))
    6398                 :             :     return false;
    6399                 :      290654 :   mpz_t right_val;
    6400                 :      290654 :   if (!right_nc->to_int(&right_val))
    6401                 :             :     {
    6402                 :           0 :       mpz_clear(left_val);
    6403                 :           0 :       return false;
    6404                 :             :     }
    6405                 :             : 
    6406                 :      290654 :   mpz_t val;
    6407                 :      290654 :   mpz_init(val);
    6408                 :             : 
    6409                 :      290654 :   switch (op)
    6410                 :             :     {
    6411                 :       37546 :     case OPERATOR_PLUS:
    6412                 :       37546 :       mpz_add(val, left_val, right_val);
    6413                 :       37546 :       if (mpz_sizeinbase(val, 2) > 0x100000)
    6414                 :             :         {
    6415                 :           0 :           go_error_at(location, "constant addition overflow");
    6416                 :           0 :           nc->set_invalid();
    6417                 :           0 :           mpz_set_ui(val, 1);
    6418                 :           0 :           *issued_error = true;
    6419                 :             :         }
    6420                 :             :       break;
    6421                 :       27168 :     case OPERATOR_MINUS:
    6422                 :       27168 :       mpz_sub(val, left_val, right_val);
    6423                 :       27168 :       if (mpz_sizeinbase(val, 2) > 0x100000)
    6424                 :             :         {
    6425                 :           0 :           go_error_at(location, "constant subtraction overflow");
    6426                 :           0 :           nc->set_invalid();
    6427                 :           0 :           mpz_set_ui(val, 1);
    6428                 :           0 :           *issued_error = true;
    6429                 :             :         }
    6430                 :             :       break;
    6431                 :        6872 :     case OPERATOR_OR:
    6432                 :        6872 :       mpz_ior(val, left_val, right_val);
    6433                 :        6872 :       break;
    6434                 :          22 :     case OPERATOR_XOR:
    6435                 :          22 :       mpz_xor(val, left_val, right_val);
    6436                 :          22 :       break;
    6437                 :       95429 :     case OPERATOR_MULT:
    6438                 :       95429 :       mpz_mul(val, left_val, right_val);
    6439                 :       95429 :       if (mpz_sizeinbase(val, 2) > 0x100000)
    6440                 :             :         {
    6441                 :           1 :           go_error_at(location, "constant multiplication overflow");
    6442                 :           1 :           nc->set_invalid();
    6443                 :           1 :           mpz_set_ui(val, 1);
    6444                 :           1 :           *issued_error = true;
    6445                 :             :         }
    6446                 :             :       break;
    6447                 :       12095 :     case OPERATOR_DIV:
    6448                 :       12095 :       if (mpz_sgn(right_val) != 0)
    6449                 :       12095 :         mpz_tdiv_q(val, left_val, right_val);
    6450                 :             :       else
    6451                 :             :         {
    6452                 :           0 :           go_error_at(location, "division by zero");
    6453                 :           0 :           nc->set_invalid();
    6454                 :           0 :           mpz_set_ui(val, 0);
    6455                 :           0 :           *issued_error = true;
    6456                 :             :         }
    6457                 :             :       break;
    6458                 :         118 :     case OPERATOR_MOD:
    6459                 :         118 :       if (mpz_sgn(right_val) != 0)
    6460                 :         118 :         mpz_tdiv_r(val, left_val, right_val);
    6461                 :             :       else
    6462                 :             :         {
    6463                 :           0 :           go_error_at(location, "division by zero");
    6464                 :           0 :           nc->set_invalid();
    6465                 :           0 :           mpz_set_ui(val, 0);
    6466                 :           0 :           *issued_error = true;
    6467                 :             :         }
    6468                 :             :       break;
    6469                 :      102805 :     case OPERATOR_LSHIFT:
    6470                 :      102805 :       {
    6471                 :      102805 :         unsigned long shift = mpz_get_ui(right_val);
    6472                 :      102805 :         if (mpz_cmp_ui(right_val, shift) == 0 && shift <= 0x100000)
    6473                 :      102805 :           mpz_mul_2exp(val, left_val, shift);
    6474                 :             :         else
    6475                 :             :           {
    6476                 :           0 :             go_error_at(location, "shift count overflow");
    6477                 :           0 :             nc->set_invalid();
    6478                 :           0 :             mpz_set_ui(val, 1);
    6479                 :           0 :             *issued_error = true;
    6480                 :             :           }
    6481                 :             :         break;
    6482                 :             :       }
    6483                 :             :       break;
    6484                 :        7569 :     case OPERATOR_RSHIFT:
    6485                 :        7569 :       {
    6486                 :        7569 :         unsigned long shift = mpz_get_ui(right_val);
    6487                 :        7569 :         if (mpz_cmp_ui(right_val, shift) != 0)
    6488                 :             :           {
    6489                 :           0 :             go_error_at(location, "shift count overflow");
    6490                 :           0 :             nc->set_invalid();
    6491                 :           0 :             mpz_set_ui(val, 1);
    6492                 :           0 :             *issued_error = true;
    6493                 :             :           }
    6494                 :             :         else
    6495                 :             :           {
    6496                 :        7569 :             if (mpz_cmp_ui(left_val, 0) >= 0)
    6497                 :        7557 :               mpz_tdiv_q_2exp(val, left_val, shift);
    6498                 :             :             else
    6499                 :          12 :               mpz_fdiv_q_2exp(val, left_val, shift);
    6500                 :             :           }
    6501                 :             :         break;
    6502                 :             :       }
    6503                 :             :       break;
    6504                 :         964 :     case OPERATOR_AND:
    6505                 :         964 :       mpz_and(val, left_val, right_val);
    6506                 :         964 :       break;
    6507                 :          66 :     case OPERATOR_BITCLEAR:
    6508                 :          66 :       {
    6509                 :          66 :         mpz_t tval;
    6510                 :          66 :         mpz_init(tval);
    6511                 :          66 :         mpz_com(tval, right_val);
    6512                 :          66 :         mpz_and(val, left_val, tval);
    6513                 :          66 :         mpz_clear(tval);
    6514                 :             :       }
    6515                 :          66 :       break;
    6516                 :           0 :     default:
    6517                 :           0 :       go_unreachable();
    6518                 :             :     }
    6519                 :             : 
    6520                 :      290654 :   mpz_clear(left_val);
    6521                 :      290654 :   mpz_clear(right_val);
    6522                 :             : 
    6523                 :      290654 :   if (left_nc->is_rune()
    6524                 :      290654 :       || (op != OPERATOR_LSHIFT
    6525                 :      289000 :           && op != OPERATOR_RSHIFT
    6526                 :      178628 :           && right_nc->is_rune()))
    6527                 :        1703 :     nc->set_rune(NULL, val);
    6528                 :             :   else
    6529                 :      288951 :     nc->set_int(NULL, val);
    6530                 :             : 
    6531                 :      290654 :   mpz_clear(val);
    6532                 :             : 
    6533                 :      290654 :   return true;
    6534                 :             : }
    6535                 :             : 
    6536                 :             : // Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC, using
    6537                 :             : // floating point operations.  Return true if this could be done,
    6538                 :             : // false if not.
    6539                 :             : 
    6540                 :             : bool
    6541                 :        1832 : Binary_expression::eval_float(Operator op, const Numeric_constant* left_nc,
    6542                 :             :                               const Numeric_constant* right_nc,
    6543                 :             :                               Location location, Numeric_constant* nc,
    6544                 :             :                               bool* issued_error)
    6545                 :             : {
    6546                 :        1832 :   mpfr_t left_val;
    6547                 :        1832 :   if (!left_nc->to_float(&left_val))
    6548                 :             :     return false;
    6549                 :        1832 :   mpfr_t right_val;
    6550                 :        1832 :   if (!right_nc->to_float(&right_val))
    6551                 :             :     {
    6552                 :           0 :       mpfr_clear(left_val);
    6553                 :           0 :       return false;
    6554                 :             :     }
    6555                 :             : 
    6556                 :        1832 :   mpfr_t val;
    6557                 :        1832 :   mpfr_init(val);
    6558                 :             : 
    6559                 :        1832 :   bool ret = true;
    6560                 :        1832 :   switch (op)
    6561                 :             :     {
    6562                 :         261 :     case OPERATOR_PLUS:
    6563                 :         261 :       mpfr_add(val, left_val, right_val, MPFR_RNDN);
    6564                 :         261 :       break;
    6565                 :         179 :     case OPERATOR_MINUS:
    6566                 :         179 :       mpfr_sub(val, left_val, right_val, MPFR_RNDN);
    6567                 :         179 :       break;
    6568                 :           0 :     case OPERATOR_OR:
    6569                 :           0 :     case OPERATOR_XOR:
    6570                 :           0 :     case OPERATOR_AND:
    6571                 :           0 :     case OPERATOR_BITCLEAR:
    6572                 :           0 :     case OPERATOR_MOD:
    6573                 :           0 :     case OPERATOR_LSHIFT:
    6574                 :           0 :     case OPERATOR_RSHIFT:
    6575                 :           0 :       mpfr_set_ui(val, 0, MPFR_RNDN);
    6576                 :           0 :       ret = false;
    6577                 :           0 :       break;
    6578                 :         480 :     case OPERATOR_MULT:
    6579                 :         480 :       mpfr_mul(val, left_val, right_val, MPFR_RNDN);
    6580                 :         480 :       break;
    6581                 :         912 :     case OPERATOR_DIV:
    6582                 :         912 :       if (!mpfr_zero_p(right_val))
    6583                 :         911 :         mpfr_div(val, left_val, right_val, MPFR_RNDN);
    6584                 :             :       else
    6585                 :             :         {
    6586                 :           1 :           go_error_at(location, "division by zero");
    6587                 :           1 :           nc->set_invalid();
    6588                 :           1 :           mpfr_set_ui(val, 0, MPFR_RNDN);
    6589                 :           1 :           *issued_error = true;
    6590                 :             :         }
    6591                 :             :       break;
    6592                 :           0 :     default:
    6593                 :           0 :       go_unreachable();
    6594                 :             :     }
    6595                 :             : 
    6596                 :        1832 :   mpfr_clear(left_val);
    6597                 :        1832 :   mpfr_clear(right_val);
    6598                 :             : 
    6599                 :        1832 :   nc->set_float(NULL, val);
    6600                 :        1832 :   mpfr_clear(val);
    6601                 :             : 
    6602                 :        1832 :   return ret;
    6603                 :             : }
    6604                 :             : 
    6605                 :             : // Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC, using
    6606                 :             : // complex operations.  Return true if this could be done, false if
    6607                 :             : // not.
    6608                 :             : 
    6609                 :             : bool
    6610                 :        1017 : Binary_expression::eval_complex(Operator op, const Numeric_constant* left_nc,
    6611                 :             :                                 const Numeric_constant* right_nc,
    6612                 :             :                                 Location location, Numeric_constant* nc,
    6613                 :             :                                 bool* issued_error)
    6614                 :             : {
    6615                 :        1017 :   mpc_t left_val;
    6616                 :        1017 :   if (!left_nc->to_complex(&left_val))
    6617                 :             :     return false;
    6618                 :        1017 :   mpc_t right_val;
    6619                 :        1017 :   if (!right_nc->to_complex(&right_val))
    6620                 :             :     {
    6621                 :           0 :       mpc_clear(left_val);
    6622                 :           0 :       return false;
    6623                 :             :     }
    6624                 :             : 
    6625                 :        1017 :   mpc_t val;
    6626                 :        1017 :   mpc_init2(val, mpc_precision);
    6627                 :             : 
    6628                 :        1017 :   bool ret = true;
    6629                 :        1017 :   switch (op)
    6630                 :             :     {
    6631                 :         702 :     case OPERATOR_PLUS:
    6632                 :         702 :       mpc_add(val, left_val, right_val, MPC_RNDNN);
    6633                 :         702 :       break;
    6634                 :         266 :     case OPERATOR_MINUS:
    6635                 :         266 :       mpc_sub(val, left_val, right_val, MPC_RNDNN);
    6636                 :         266 :       break;
    6637                 :           0 :     case OPERATOR_OR:
    6638                 :           0 :     case OPERATOR_XOR:
    6639                 :           0 :     case OPERATOR_AND:
    6640                 :           0 :     case OPERATOR_BITCLEAR:
    6641                 :           0 :     case OPERATOR_MOD:
    6642                 :           0 :     case OPERATOR_LSHIFT:
    6643                 :           0 :     case OPERATOR_RSHIFT:
    6644                 :           0 :       mpc_set_ui(val, 0, MPC_RNDNN);
    6645                 :           0 :       ret = false;
    6646                 :           0 :       break;
    6647                 :          13 :     case OPERATOR_MULT:
    6648                 :          13 :       mpc_mul(val, left_val, right_val, MPC_RNDNN);
    6649                 :          13 :       break;
    6650                 :          36 :     case OPERATOR_DIV:
    6651                 :          36 :       if (mpc_cmp_si(right_val, 0) == 0)
    6652                 :             :         {
    6653                 :           5 :           go_error_at(location, "division by zero");
    6654                 :           5 :           nc->set_invalid();
    6655                 :           5 :           mpc_set_ui(val, 0, MPC_RNDNN);
    6656                 :           5 :           *issued_error = true;
    6657                 :           5 :           break;
    6658                 :             :         }
    6659                 :          31 :       mpc_div(val, left_val, right_val, MPC_RNDNN);
    6660                 :          31 :       break;
    6661                 :           0 :     default:
    6662                 :           0 :       go_unreachable();
    6663                 :             :     }
    6664                 :             : 
    6665                 :        1017 :   mpc_clear(left_val);
    6666                 :        1017 :   mpc_clear(right_val);
    6667                 :             : 
    6668                 :        1017 :   nc->set_complex(NULL, val);
    6669                 :        1017 :   mpc_clear(val);
    6670                 :             : 
    6671                 :        1017 :   return ret;
    6672                 :             : }
    6673                 :             : 
    6674                 :             : // Lower a binary expression.  We have to evaluate constant
    6675                 :             : // expressions now, in order to implement Go's unlimited precision
    6676                 :             : // constants.
    6677                 :             : 
    6678                 :             : Expression*
    6679                 :     1495691 : Binary_expression::do_lower(Gogo* gogo, Named_object*,
    6680                 :             :                             Statement_inserter* inserter)
    6681                 :             : {
    6682                 :     1495691 :   Location location = this->location();
    6683                 :             : 
    6684                 :     1495691 :   if (this->is_error_expression())
    6685                 :         360 :     return Expression::make_error(location);
    6686                 :             : 
    6687                 :     1495331 :   Operator op = this->op_;
    6688                 :     1495331 :   Expression* left = this->left_;
    6689                 :     1495331 :   Expression* right = this->right_;
    6690                 :             : 
    6691                 :     1495331 :   if (left->is_error_expression() || right->is_error_expression())
    6692                 :         186 :     return Expression::make_error(location);
    6693                 :             : 
    6694                 :     1495145 :   const bool is_comparison = (op == OPERATOR_EQEQ
    6695                 :             :                               || op == OPERATOR_NOTEQ
    6696                 :             :                               || op == OPERATOR_LT
    6697                 :             :                               || op == OPERATOR_LE
    6698                 :             :                               || op == OPERATOR_GT
    6699                 :     1495145 :                               || op == OPERATOR_GE);
    6700                 :             : 
    6701                 :             :   // Numeric constant expressions.
    6702                 :     1495145 :   {
    6703                 :     1495145 :     Numeric_constant left_nc;
    6704                 :     1495145 :     Numeric_constant right_nc;
    6705                 :     1495145 :     if (left->numeric_constant_value(&left_nc)
    6706                 :     1572092 :         && right->numeric_constant_value(&right_nc))
    6707                 :             :       {
    6708                 :       42366 :         Expression* ret;
    6709                 :       42366 :         if (is_comparison)
    6710                 :             :           {
    6711                 :         936 :             bool result;
    6712                 :         936 :             if (!Binary_expression::compare_constant(op, &left_nc,
    6713                 :             :                                                      &right_nc, location,
    6714                 :             :                                                      &result))
    6715                 :           0 :               return this;
    6716                 :         936 :             ret = Expression::make_boolean(result, location);
    6717                 :             :           }
    6718                 :             :         else
    6719                 :             :           {
    6720                 :       41430 :             Numeric_constant nc;
    6721                 :       41430 :             bool issued_error;
    6722                 :       41430 :             if (!Binary_expression::eval_constant(op, &left_nc, &right_nc,
    6723                 :             :                                                   location, &nc,
    6724                 :             :                                                   &issued_error))
    6725                 :             :               {
    6726                 :          12 :                 if (issued_error)
    6727                 :          12 :                   return Expression::make_error(location);
    6728                 :           0 :                 return this;
    6729                 :             :               }
    6730                 :       41418 :             ret = nc.expression(location);
    6731                 :       41430 :           }
    6732                 :             : 
    6733                 :       42354 :         Type_context subcontext(this->type_, this->type_->is_abstract());
    6734                 :       42354 :         ret->determine_type(gogo, &subcontext);
    6735                 :       42354 :         ret->check_types(gogo);
    6736                 :       42354 :         return ret;
    6737                 :             :       }
    6738                 :     1495145 :   }
    6739                 :             : 
    6740                 :             :   // String constant expressions.
    6741                 :             :   //
    6742                 :             :   // Avoid constant folding here if the left and right types are incompatible
    6743                 :             :   // (leave the operation intact so that the type checker can complain about it
    6744                 :             :   // later on). If concatenating an abstract string with a named string type,
    6745                 :             :   // result type needs to be of the named type (see issue 31412).
    6746                 :     1452779 :   if (left->type()->is_string_type()
    6747                 :      131950 :       && right->type()->is_string_type()
    6748                 :     1584721 :       && (left->type()->named_type() == NULL
    6749                 :       93520 :           || right->type()->named_type() == NULL
    6750                 :       93520 :           || left->type()->named_type() == right->type()->named_type()))
    6751                 :             :     {
    6752                 :      131942 :       std::string left_string;
    6753                 :      131942 :       std::string right_string;
    6754                 :      131942 :       if (left->string_constant_value(&left_string)
    6755                 :      178143 :           && right->string_constant_value(&right_string))
    6756                 :             :         {
    6757                 :       40346 :           Expression* ret = NULL;
    6758                 :       40346 :           if (op == OPERATOR_PLUS)
    6759                 :             :             {
    6760                 :       38286 :               delete left;
    6761                 :       38286 :               delete right;
    6762                 :       38286 :               ret = Expression::make_string_typed(left_string + right_string,
    6763                 :             :                                                   this->type_, location);
    6764                 :             :             }
    6765                 :        2060 :           else if (is_comparison)
    6766                 :             :             {
    6767                 :        2060 :               int cmp = left_string.compare(right_string);
    6768                 :        2060 :               bool r = Binary_expression::cmp_to_bool(op, cmp);
    6769                 :        2060 :               delete left;
    6770                 :        2060 :               delete right;
    6771                 :        2060 :               ret = Expression::make_boolean(r, location);
    6772                 :             :             }
    6773                 :             : 
    6774                 :       40346 :           if (ret != NULL)
    6775                 :             :             {
    6776                 :       40346 :               Type_context subcontext(this->type_, this->type_->is_abstract());
    6777                 :       40346 :               ret->determine_type(gogo, &subcontext);
    6778                 :       40346 :               ret->check_types(gogo);
    6779                 :       40346 :               return ret;
    6780                 :             :             }
    6781                 :             :         }
    6782                 :      131942 :     }
    6783                 :             : 
    6784                 :             :   // Lower struct, array, and some interface comparisons.
    6785                 :     1412433 :   if (op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ)
    6786                 :             :     {
    6787                 :      493351 :       if (left->type()->struct_type() != NULL
    6788                 :        8051 :           && right->type()->struct_type() != NULL)
    6789                 :        8036 :         return this->lower_struct_comparison(gogo, inserter);
    6790                 :      491583 :       else if (left->type()->array_type() != NULL
    6791                 :       14319 :                && !left->type()->is_slice_type()
    6792                 :       17684 :                && right->type()->array_type() != NULL
    6793                 :        8835 :                && !right->type()->is_slice_type())
    6794                 :        8835 :         return this->lower_array_comparison(gogo, inserter);
    6795                 :      557715 :       else if ((left->type()->interface_type() != NULL
    6796                 :      478858 :                 && right->type()->interface_type() == NULL)
    6797                 :      768715 :                || (left->type()->interface_type() == NULL
    6798                 :      379143 :                    && right->type()->interface_type() != NULL))
    6799                 :       79140 :         return this->lower_interface_value_comparison(gogo, inserter);
    6800                 :             :     }
    6801                 :             : 
    6802                 :             :   // Lower string concatenation to String_concat_expression, so that
    6803                 :             :   // we can group sequences of string additions.
    6804                 :     1316422 :   if (this->left_->type()->is_string_type() && this->op_ == OPERATOR_PLUS)
    6805                 :             :     {
    6806                 :       16280 :       Expression_list* exprs;
    6807                 :       16280 :       String_concat_expression* left_sce =
    6808                 :       16280 :         this->left_->string_concat_expression();
    6809                 :       16280 :       if (left_sce != NULL)
    6810                 :        5359 :         exprs = left_sce->exprs();
    6811                 :             :       else
    6812                 :             :         {
    6813                 :       10921 :           exprs = new Expression_list();
    6814                 :       10921 :           exprs->push_back(this->left_);
    6815                 :             :         }
    6816                 :             : 
    6817                 :       16280 :       String_concat_expression* right_sce =
    6818                 :       16280 :         this->right_->string_concat_expression();
    6819                 :       16280 :       if (right_sce != NULL)
    6820                 :         401 :         exprs->append(right_sce->exprs());
    6821                 :             :       else
    6822                 :       15879 :         exprs->push_back(this->right_);
    6823                 :             : 
    6824                 :       16280 :       return Expression::make_string_concat(exprs);
    6825                 :             :     }
    6826                 :             : 
    6827                 :     1300142 :   return this;
    6828                 :             : }
    6829                 :             : 
    6830                 :             : // Lower a struct comparison.
    6831                 :             : 
    6832                 :             : Expression*
    6833                 :        8036 : Binary_expression::lower_struct_comparison(Gogo* gogo,
    6834                 :             :                                            Statement_inserter* inserter)
    6835                 :             : {
    6836                 :        8036 :   Struct_type* st = this->left_->type()->struct_type();
    6837                 :        8036 :   Struct_type* st2 = this->right_->type()->struct_type();
    6838                 :        8036 :   if (st2 == NULL)
    6839                 :           0 :     return this;
    6840                 :        8036 :   if (st != st2
    6841                 :        8036 :       && !Type::are_identical(st, st2,
    6842                 :             :                               Type::COMPARE_ERRORS | Type::COMPARE_TAGS,
    6843                 :             :                               NULL))
    6844                 :           0 :     return this;
    6845                 :        8036 :   if (!Type::are_compatible_for_comparison(true, this->left_->type(),
    6846                 :        8036 :                                            this->right_->type(), NULL))
    6847                 :           0 :     return this;
    6848                 :             : 
    6849                 :             :   // See if we can compare using memcmp.  As a heuristic, we use
    6850                 :             :   // memcmp rather than field references and comparisons if there are
    6851                 :             :   // more than two fields.
    6852                 :        8036 :   if (st->compare_is_identity(gogo) && st->total_field_count() > 2)
    6853                 :         549 :     return this->lower_compare_to_memcmp(gogo, inserter);
    6854                 :             : 
    6855                 :        7487 :   Location loc = this->location();
    6856                 :             : 
    6857                 :        7487 :   Expression* left = this->left_;
    6858                 :        7487 :   Temporary_statement* left_temp = NULL;
    6859                 :        7487 :   if (left->var_expression() == NULL
    6860                 :        7487 :       && left->temporary_reference_expression() == NULL)
    6861                 :             :     {
    6862                 :        7168 :       left_temp = Statement::make_temporary(left->type(), NULL, loc);
    6863                 :        7168 :       inserter->insert(left_temp);
    6864                 :        7168 :       left = Expression::make_set_and_use_temporary(left_temp, left, loc);
    6865                 :             :     }
    6866                 :             : 
    6867                 :        7487 :   Expression* right = this->right_;
    6868                 :        7487 :   Temporary_statement* right_temp = NULL;
    6869                 :        7487 :   if (right->var_expression() == NULL
    6870                 :        7487 :       && right->temporary_reference_expression() == NULL)
    6871                 :             :     {
    6872                 :        7302 :       right_temp = Statement::make_temporary(right->type(), NULL, loc);
    6873                 :        7302 :       inserter->insert(right_temp);
    6874                 :        7302 :       right = Expression::make_set_and_use_temporary(right_temp, right, loc);
    6875                 :             :     }
    6876                 :             : 
    6877                 :        7487 :   Expression* ret = Expression::make_boolean(true, loc);
    6878                 :        7487 :   const Struct_field_list* fields = st->fields();
    6879                 :        7487 :   unsigned int field_index = 0;
    6880                 :        7487 :   for (Struct_field_list::const_iterator pf = fields->begin();
    6881                 :       25191 :        pf != fields->end();
    6882                 :       17704 :        ++pf, ++field_index)
    6883                 :             :     {
    6884                 :       17704 :       if (Gogo::is_sink_name(pf->field_name()))
    6885                 :         128 :         continue;
    6886                 :             : 
    6887                 :       17576 :       if (field_index > 0)
    6888                 :             :         {
    6889                 :       10406 :           if (left_temp == NULL)
    6890                 :         554 :             left = left->copy();
    6891                 :             :           else
    6892                 :        9852 :             left = Expression::make_temporary_reference(left_temp, loc);
    6893                 :       10406 :           if (right_temp == NULL)
    6894                 :         365 :             right = right->copy();
    6895                 :             :           else
    6896                 :       10041 :             right = Expression::make_temporary_reference(right_temp, loc);
    6897                 :             :         }
    6898                 :       17576 :       Expression* f1 = Expression::make_field_reference(left, field_index,
    6899                 :             :                                                         loc);
    6900                 :       17576 :       Expression* f2 = Expression::make_field_reference(right, field_index,
    6901                 :             :                                                         loc);
    6902                 :       17576 :       Expression* cond = Expression::make_binary(OPERATOR_EQEQ, f1, f2, loc);
    6903                 :       17576 :       ret = Expression::make_binary(OPERATOR_ANDAND, ret, cond, loc);
    6904                 :             :     }
    6905                 :             : 
    6906                 :        7487 :   if (this->op_ == OPERATOR_NOTEQ)
    6907                 :        6401 :     ret = Expression::make_unary(OPERATOR_NOT, ret, loc);
    6908                 :             : 
    6909                 :        7487 :   ret->determine_type_no_context(gogo);
    6910                 :             : 
    6911                 :        7487 :   return ret;
    6912                 :             : }
    6913                 :             : 
    6914                 :             : // Lower an array comparison.
    6915                 :             : 
    6916                 :             : Expression*
    6917                 :        8835 : Binary_expression::lower_array_comparison(Gogo* gogo,
    6918                 :             :                                           Statement_inserter* inserter)
    6919                 :             : {
    6920                 :        8835 :   Array_type* at = this->left_->type()->array_type();
    6921                 :        8835 :   Array_type* at2 = this->right_->type()->array_type();
    6922                 :        8835 :   if (at2 == NULL)
    6923                 :           0 :     return this;
    6924                 :        8835 :   if (at != at2
    6925                 :        8835 :       && !Type::are_identical(at, at2,
    6926                 :             :                               Type::COMPARE_ERRORS | Type::COMPARE_TAGS,
    6927                 :             :                               NULL))
    6928                 :           0 :     return this;
    6929                 :        8835 :   if (!Type::are_compatible_for_comparison(true, this->left_->type(),
    6930                 :        8835 :                                            this->right_->type(), NULL))
    6931                 :           0 :     return this;
    6932                 :             : 
    6933                 :             :   // Call memcmp directly if possible.  This may let the middle-end
    6934                 :             :   // optimize the call.
    6935                 :        8835 :   if (at->compare_is_identity(gogo))
    6936                 :        7874 :     return this->lower_compare_to_memcmp(gogo, inserter);
    6937                 :             : 
    6938                 :             :   // Call the array comparison function.
    6939                 :         961 :   Named_object* equal_fn =
    6940                 :         961 :     at->equal_function(gogo, this->left_->type()->named_type(), NULL);
    6941                 :             : 
    6942                 :         961 :   Location loc = this->location();
    6943                 :             : 
    6944                 :         961 :   Expression* func = Expression::make_func_reference(equal_fn, NULL, loc);
    6945                 :             : 
    6946                 :         961 :   Expression_list* args = new Expression_list();
    6947                 :         961 :   args->push_back(this->operand_address(inserter, this->left_));
    6948                 :         961 :   args->push_back(this->operand_address(inserter, this->right_));
    6949                 :             : 
    6950                 :         961 :   Call_expression* ce = Expression::make_call(func, args, false, loc);
    6951                 :             : 
    6952                 :             :   // Record that this is a call to a generated equality function.  We
    6953                 :             :   // need to do this because a comparison returns an abstract boolean
    6954                 :             :   // type, but the function necessarily returns "bool".  The
    6955                 :             :   // difference shows up in code like
    6956                 :             :   //     type mybool bool
    6957                 :             :   //     var b mybool = [10]string{} == [10]string{}
    6958                 :             :   // The comparison function returns "bool", but since a comparison
    6959                 :             :   // has an abstract boolean type we need an implicit conversion to
    6960                 :             :   // "mybool".  The implicit conversion is inserted in
    6961                 :             :   // Call_expression::do_flatten.
    6962                 :         961 :   ce->set_is_equal_function();
    6963                 :             : 
    6964                 :         961 :   Expression* ret = ce;
    6965                 :         961 :   if (this->op_ == OPERATOR_NOTEQ)
    6966                 :         485 :     ret = Expression::make_unary(OPERATOR_NOT, ret, loc);
    6967                 :             : 
    6968                 :         961 :   ret->determine_type_no_context(gogo);
    6969                 :             : 
    6970                 :         961 :   return ret;
    6971                 :             : }
    6972                 :             : 
    6973                 :             : // Lower an interface to value comparison.
    6974                 :             : 
    6975                 :             : Expression*
    6976                 :       79140 : Binary_expression::lower_interface_value_comparison(Gogo*,
    6977                 :             :                                                     Statement_inserter* inserter)
    6978                 :             : {
    6979                 :       79140 :   Type* left_type = this->left_->type();
    6980                 :       79140 :   Type* right_type = this->right_->type();
    6981                 :       79140 :   Interface_type* ift;
    6982                 :       79140 :   if (left_type->interface_type() != NULL)
    6983                 :             :     {
    6984                 :       78857 :       ift = left_type->interface_type();
    6985                 :       78857 :       if (!ift->implements_interface(right_type, NULL))
    6986                 :       74700 :         return this;
    6987                 :             :     }
    6988                 :             :   else
    6989                 :             :     {
    6990                 :         283 :       ift = right_type->interface_type();
    6991                 :         283 :       if (!ift->implements_interface(left_type, NULL))
    6992                 :           0 :         return this;
    6993                 :             :     }
    6994                 :        4440 :   if (!Type::are_compatible_for_comparison(true, left_type, right_type, NULL))
    6995                 :           0 :     return this;
    6996                 :             : 
    6997                 :        4440 :   Location loc = this->location();
    6998                 :             : 
    6999                 :        4440 :   if (left_type->interface_type() == NULL
    7000                 :         283 :       && left_type->points_to() == NULL
    7001                 :          89 :       && !this->left_->is_addressable())
    7002                 :             :     {
    7003                 :           4 :       Temporary_statement* temp =
    7004                 :           4 :           Statement::make_temporary(left_type, NULL, loc);
    7005                 :           4 :       inserter->insert(temp);
    7006                 :           4 :       this->left_ =
    7007                 :           4 :           Expression::make_set_and_use_temporary(temp, this->left_, loc);
    7008                 :             :     }
    7009                 :             : 
    7010                 :        4440 :   if (right_type->interface_type() == NULL
    7011                 :        4157 :       && right_type->points_to() == NULL
    7012                 :        3326 :       && !this->right_->is_addressable())
    7013                 :             :     {
    7014                 :        2317 :       Temporary_statement* temp =
    7015                 :        2317 :           Statement::make_temporary(right_type, NULL, loc);
    7016                 :        2317 :       inserter->insert(temp);
    7017                 :        2317 :       this->right_ =
    7018                 :        2317 :           Expression::make_set_and_use_temporary(temp, this->right_, loc);
    7019                 :             :     }
    7020                 :             : 
    7021                 :        4440 :   return this;
    7022                 :             : }
    7023                 :             : 
    7024                 :             : // Lower a struct or array comparison to a call to memcmp.
    7025                 :             : 
    7026                 :             : Expression*
    7027                 :        8423 : Binary_expression::lower_compare_to_memcmp(Gogo* gogo,
    7028                 :             :                                            Statement_inserter* inserter)
    7029                 :             : {
    7030                 :        8423 :   Location loc = this->location();
    7031                 :             : 
    7032                 :        8423 :   Expression* a1 = this->operand_address(inserter, this->left_);
    7033                 :        8423 :   Expression* a2 = this->operand_address(inserter, this->right_);
    7034                 :        8423 :   Expression* len = Expression::make_type_info(this->left_->type(),
    7035                 :             :                                                TYPE_INFO_SIZE);
    7036                 :             : 
    7037                 :        8423 :   Expression* call = Runtime::make_call(gogo, Runtime::MEMCMP, loc, 3,
    7038                 :             :                                         a1, a2, len);
    7039                 :        8423 :   Type* int32_type = Type::lookup_integer_type("int32");
    7040                 :        8423 :   Expression* zero = Expression::make_integer_ul(0, int32_type, loc);
    7041                 :        8423 :   Expression* ret = Expression::make_binary(this->op_, call, zero, loc);
    7042                 :        8423 :   Type_context context(this->type_, this->type_->is_abstract());
    7043                 :        8423 :   ret->determine_type(gogo, &context);
    7044                 :        8423 :   return ret;
    7045                 :             : }
    7046                 :             : 
    7047                 :             : Expression*
    7048                 :      868730 : Binary_expression::do_flatten(Gogo* gogo, Named_object*,
    7049                 :             :                               Statement_inserter* inserter)
    7050                 :             : {
    7051                 :      868730 :   Location loc = this->location();
    7052                 :      868730 :   if (this->left_->type()->is_error_type()
    7053                 :      868730 :       || this->right_->type()->is_error_type()
    7054                 :      868730 :       || this->left_->is_error_expression()
    7055                 :     1737460 :       || this->right_->is_error_expression())
    7056                 :             :     {
    7057                 :           0 :       go_assert(saw_errors());
    7058                 :           0 :       return Expression::make_error(loc);
    7059                 :             :     }
    7060                 :             : 
    7061                 :      868730 :   Temporary_statement* temp;
    7062                 :             : 
    7063                 :      868730 :   Type* left_type = this->left_->type();
    7064                 :      868730 :   bool is_shift_op = (this->op_ == OPERATOR_LSHIFT
    7065                 :      868730 :                       || this->op_ == OPERATOR_RSHIFT);
    7066                 :      868730 :   bool is_idiv_op = ((this->op_ == OPERATOR_DIV &&
    7067                 :     1729886 :                       left_type->integer_type() != NULL)
    7068                 :      870331 :                      || this->op_ == OPERATOR_MOD);
    7069                 :      868730 :   bool is_string_op = (left_type->is_string_type()
    7070                 :     1737460 :                        && this->right_->type()->is_string_type());
    7071                 :             : 
    7072                 :       46741 :   if (is_string_op)
    7073                 :             :     {
    7074                 :             :       // Mark string([]byte) operands to reuse the backing store.
    7075                 :             :       // String comparison does not keep the reference, so it is safe.
    7076                 :       46741 :       Type_conversion_expression* lce =
    7077                 :       46741 :         this->left_->conversion_expression();
    7078                 :        1100 :       if (lce != NULL && lce->expr()->type()->is_slice_type())
    7079                 :        1080 :         lce->set_no_copy(true);
    7080                 :       46741 :       Type_conversion_expression* rce =
    7081                 :       46741 :         this->right_->conversion_expression();
    7082                 :         885 :       if (rce != NULL && rce->expr()->type()->is_slice_type())
    7083                 :         395 :         rce->set_no_copy(true);
    7084                 :             :     }
    7085                 :             : 
    7086                 :      868730 :   if (is_shift_op
    7087                 :      842992 :       || (is_idiv_op
    7088                 :       11890 :           && (gogo->check_divide_by_zero() || gogo->check_divide_overflow()))
    7089                 :     1699832 :       || is_string_op)
    7090                 :             :     {
    7091                 :       84369 :       if (!this->left_->is_multi_eval_safe())
    7092                 :             :         {
    7093                 :       38134 :           temp = Statement::make_temporary(NULL, this->left_, loc);
    7094                 :       38134 :           inserter->insert(temp);
    7095                 :       38134 :           this->left_ = Expression::make_temporary_reference(temp, loc);
    7096                 :             :         }
    7097                 :       84369 :       if (!this->right_->is_multi_eval_safe())
    7098                 :             :         {
    7099                 :       43288 :           temp =
    7100                 :       43288 :               Statement::make_temporary(NULL, this->right_, loc);
    7101                 :       43288 :           this->right_ = Expression::make_temporary_reference(temp, loc);
    7102                 :       43288 :           inserter->insert(temp);
    7103                 :             :         }
    7104                 :             :     }
    7105                 :      868730 :   return this;
    7106                 :             : }
    7107                 :             : 
    7108                 :             : 
    7109                 :             : // Return the address of EXPR, cast to unsafe.Pointer.
    7110                 :             : 
    7111                 :             : Expression*
    7112                 :       18768 : Binary_expression::operand_address(Statement_inserter* inserter,
    7113                 :             :                                    Expression* expr)
    7114                 :             : {
    7115                 :       18768 :   Location loc = this->location();
    7116                 :             : 
    7117                 :       18768 :   if (!expr->is_addressable())
    7118                 :             :     {
    7119                 :        2194 :       Temporary_statement* temp = Statement::make_temporary(expr->type(), NULL,
    7120                 :             :                                                             loc);
    7121                 :        2194 :       inserter->insert(temp);
    7122                 :        2194 :       expr = Expression::make_set_and_use_temporary(temp, expr, loc);
    7123                 :             :     }
    7124                 :       18768 :   expr = Expression::make_unary(OPERATOR_AND, expr, loc);
    7125                 :       18768 :   static_cast<Unary_expression*>(expr)->set_does_not_escape();
    7126                 :       18768 :   Type* void_type = Type::make_void_type();
    7127                 :       18768 :   Type* unsafe_pointer_type = Type::make_pointer_type(void_type);
    7128                 :       18768 :   return Expression::make_cast(unsafe_pointer_type, expr, loc);
    7129                 :             : }
    7130                 :             : 
    7131                 :             : // Return the numeric constant value, if it has one.
    7132                 :             : 
    7133                 :             : bool
    7134                 :      726640 : Binary_expression::do_numeric_constant_value(Numeric_constant* nc)
    7135                 :             : {
    7136                 :      726640 :   if (this->is_error_expression())
    7137                 :             :     return false;
    7138                 :             : 
    7139                 :      726443 :   Numeric_constant left_nc;
    7140                 :      726443 :   if (!this->left_->numeric_constant_value(&left_nc))
    7141                 :             :     return false;
    7142                 :      276274 :   Numeric_constant right_nc;
    7143                 :      276274 :   if (!this->right_->numeric_constant_value(&right_nc))
    7144                 :             :     return false;
    7145                 :      252073 :   bool issued_error;
    7146                 :      252073 :   bool r = Binary_expression::eval_constant(this->op_, &left_nc, &right_nc,
    7147                 :             :                                             this->location(), nc,
    7148                 :             :                                             &issued_error);
    7149                 :      252073 :   if (issued_error)
    7150                 :           1 :     this->set_is_error();
    7151                 :             :   return r;
    7152                 :      726443 : }
    7153                 :             : 
    7154                 :             : // Return the boolean constant value, if it has one.
    7155                 :             : 
    7156                 :             : bool
    7157                 :     1103404 : Binary_expression::do_boolean_constant_value(bool* val)
    7158                 :             : {
    7159                 :     1103404 :   if (this->is_error_expression())
    7160                 :             :     return false;
    7161                 :             : 
    7162                 :     1103404 :   bool is_comparison = false;
    7163                 :     1103404 :   switch (this->op_)
    7164                 :             :     {
    7165                 :      672060 :     case OPERATOR_EQEQ:
    7166                 :      672060 :     case OPERATOR_NOTEQ:
    7167                 :      672060 :     case OPERATOR_LT:
    7168                 :      672060 :     case OPERATOR_LE:
    7169                 :      672060 :     case OPERATOR_GT:
    7170                 :      672060 :     case OPERATOR_GE:
    7171                 :      672060 :       is_comparison = true;
    7172                 :      672060 :       break;
    7173                 :             :     case OPERATOR_ANDAND:
    7174                 :             :     case OPERATOR_OROR:
    7175                 :             :       break;
    7176                 :             :     default:
    7177                 :             :       return false;
    7178                 :             :     }
    7179                 :             : 
    7180                 :     1675802 :   Numeric_constant left_nc, right_nc;
    7181                 :      837901 :   if (is_comparison
    7182                 :      672060 :       && this->left_->numeric_constant_value(&left_nc)
    7183                 :      846499 :       && this->right_->numeric_constant_value(&right_nc))
    7184                 :           0 :     return Binary_expression::compare_constant(this->op_, &left_nc,
    7185                 :             :                                                &right_nc,
    7186                 :             :                                                this->location(),
    7187                 :           0 :                                                val);
    7188                 :             : 
    7189                 :     1675802 :   std::string left_str, right_str;
    7190                 :      837901 :   if (is_comparison
    7191                 :      672060 :       && this->left_->string_constant_value(&left_str)
    7192                 :      837965 :       && this->right_->string_constant_value(&right_str))
    7193                 :             :     {
    7194                 :           0 :       *val = Binary_expression::cmp_to_bool(this->op_,
    7195                 :             :                                             left_str.compare(right_str));
    7196                 :           0 :       return true;
    7197                 :             :     }
    7198                 :             : 
    7199                 :      837901 :   bool left_bval;
    7200                 :      837901 :   if (this->left_->boolean_constant_value(&left_bval))
    7201                 :             :     {
    7202                 :        5727 :       if (this->op_ == OPERATOR_ANDAND && !left_bval)
    7203                 :             :         {
    7204                 :        1789 :           *val = false;
    7205                 :        2797 :           return true;
    7206                 :             :         }
    7207                 :        3938 :       else if (this->op_ == OPERATOR_OROR && left_bval)
    7208                 :             :         {
    7209                 :         167 :           *val = true;
    7210                 :         167 :           return true;
    7211                 :             :         }
    7212                 :             : 
    7213                 :        3771 :       bool right_bval;
    7214                 :        3771 :       if (this->right_->boolean_constant_value(&right_bval))
    7215                 :             :         {
    7216                 :         841 :           switch (this->op_)
    7217                 :             :             {
    7218                 :           2 :             case OPERATOR_EQEQ:
    7219                 :           2 :               *val = (left_bval == right_bval);
    7220                 :           2 :               return true;
    7221                 :           0 :             case OPERATOR_NOTEQ:
    7222                 :           0 :               *val = (left_bval != right_bval);
    7223                 :           0 :               return true;
    7224                 :         839 :             case OPERATOR_ANDAND:
    7225                 :         839 :             case OPERATOR_OROR:
    7226                 :         839 :               *val = right_bval;
    7227                 :         839 :               return true;
    7228                 :           0 :             default:
    7229                 :           0 :               go_unreachable();
    7230                 :             :             }
    7231                 :             :         }
    7232                 :             :     }
    7233                 :             : 
    7234                 :             :   return false;
    7235                 :             : }
    7236                 :             : 
    7237                 :             : // Note that the value is being discarded.
    7238                 :             : 
    7239                 :             : bool
    7240                 :           2 : Binary_expression::do_discarding_value()
    7241                 :             : {
    7242                 :           2 :   if (this->op_ == OPERATOR_OROR || this->op_ == OPERATOR_ANDAND)
    7243                 :           0 :     return this->right_->discarding_value();
    7244                 :             :   else
    7245                 :             :     {
    7246                 :           2 :       this->unused_value_error();
    7247                 :           2 :       return false;
    7248                 :             :     }
    7249                 :             : }
    7250                 :             : 
    7251                 :             : // Get type.
    7252                 :             : 
    7253                 :             : Type*
    7254                 :     7249739 : Binary_expression::do_type()
    7255                 :             : {
    7256                 :     7249739 :   if (this->type_ == NULL)
    7257                 :             :     {
    7258                 :         157 :       go_assert(saw_errors());
    7259                 :         157 :       return Type::make_error_type();
    7260                 :             :     }
    7261                 :             : 
    7262                 :             :   return this->type_;
    7263                 :             : }
    7264                 :             : 
    7265                 :             : // Set type for a binary expression.
    7266                 :             : 
    7267                 :             : void
    7268                 :     2878455 : Binary_expression::do_determine_type(Gogo* gogo, const Type_context* context)
    7269                 :             : {
    7270                 :     2878455 :   if (this->type_ != NULL)
    7271                 :      538304 :     return;
    7272                 :             : 
    7273                 :             :   // For a shift operation, the type of the binary expression is the
    7274                 :             :   // type of the left operand.  If the left operand is a constant,
    7275                 :             :   // then it gets its type from the context.
    7276                 :     2340269 :   bool is_shift_op = (this->op_ == OPERATOR_LSHIFT
    7277                 :     2340269 :                       || this->op_ == OPERATOR_RSHIFT);
    7278                 :             : 
    7279                 :             :   // For a comparison operation, the type of the binary expression is
    7280                 :             :   // a boolean type.
    7281                 :     2340269 :   bool is_comparison = (this->op_ == OPERATOR_EQEQ
    7282                 :             :                         || this->op_ == OPERATOR_NOTEQ
    7283                 :             :                         || this->op_ == OPERATOR_LT
    7284                 :             :                         || this->op_ == OPERATOR_LE
    7285                 :             :                         || this->op_ == OPERATOR_GT
    7286                 :     2340269 :                         || this->op_ == OPERATOR_GE);
    7287                 :             : 
    7288                 :             :   // For constant expressions, the context of the result is not useful in
    7289                 :             :   // determining the types of the operands.  It is only legal to use abstract
    7290                 :             :   // boolean, numeric, and string constants as operands where it is legal to
    7291                 :             :   // use non-abstract boolean, numeric, and string constants, respectively.
    7292                 :             :   // Any issues with the operation will be resolved in the check_types pass.
    7293                 :     2340269 :   bool left_is_constant = this->left_->is_constant();
    7294                 :     2340269 :   bool right_is_constant = this->right_->is_constant();
    7295                 :     2340269 :   bool is_constant_expr = left_is_constant && right_is_constant;
    7296                 :             : 
    7297                 :     2340269 :   Type_context subcontext(*context);
    7298                 :     2340269 :   if (is_comparison)
    7299                 :     1291564 :     subcontext.type = NULL;
    7300                 :             : 
    7301                 :     2340269 :   Type* tleft;
    7302                 :     2340269 :   bool left_is_untyped = this->left_->is_untyped(&tleft);
    7303                 :     2340269 :   if (!left_is_untyped)
    7304                 :             :     {
    7305                 :     1954003 :       this->left_->determine_type(gogo, &subcontext);
    7306                 :     1954003 :       tleft = this->left_->type();
    7307                 :             :     }
    7308                 :             : 
    7309                 :     2340269 :   Type* tright;
    7310                 :     2340269 :   bool right_is_untyped = this->right_->is_untyped(&tright);
    7311                 :     2340269 :   if (!right_is_untyped)
    7312                 :             :     {
    7313                 :             :       // For a shift operation, the right operand should always be an
    7314                 :             :       // integer.
    7315                 :     1767510 :       if (is_shift_op)
    7316                 :             :         {
    7317                 :      191688 :           subcontext.type = Type::lookup_integer_type("uint");
    7318                 :      191688 :           subcontext.may_be_abstract = false;
    7319                 :             :         }
    7320                 :             : 
    7321                 :     1767510 :       this->right_->determine_type(gogo, &subcontext);
    7322                 :     1767510 :       tright = this->right_->type();
    7323                 :             :     }
    7324                 :             : 
    7325                 :             :   // For each operand we have the real type or, if the operand is a
    7326                 :             :   // untyped, a guess at the type.  Use this to determine the types of
    7327                 :             :   // untyped operands.
    7328                 :             : 
    7329                 :     2340269 :   subcontext = *context;
    7330                 :     2340269 :   if (left_is_untyped && (right_is_untyped || is_shift_op) && is_constant_expr)
    7331                 :             :     {
    7332                 :             :       // We evaluate the operands of an untyped expression as untyped
    7333                 :             :       // values.  Then we convert to the desired type.  Otherwise we
    7334                 :             :       // may, for example, mishandle a floating-point constant
    7335                 :             :       // division as an integer division.
    7336                 :      123273 :       subcontext.type = NULL;
    7337                 :      123273 :       subcontext.may_be_abstract = true;
    7338                 :             :     }
    7339                 :     2216996 :   else if (is_comparison)
    7340                 :             :     {
    7341                 :             :       // In a comparison, the context does not determine the types of
    7342                 :             :       // the operands.
    7343                 :     1290591 :       subcontext.type = NULL;
    7344                 :             :     }
    7345                 :             : 
    7346                 :             :   // Set the context for the left hand operand.
    7347                 :             : 
    7348                 :     2340269 :   if (is_shift_op)
    7349                 :             :     {
    7350                 :             :       // The right hand operand of a shift plays no role in
    7351                 :             :       // determining the type of the left hand operand.
    7352                 :      221604 :       if (subcontext.type == NULL
    7353                 :       16914 :           && right_is_constant
    7354                 :       15864 :           && context->may_be_abstract)
    7355                 :        5512 :         subcontext.type = Type::make_abstract_integer_type();
    7356                 :             :     }
    7357                 :     2118665 :   else if (!tleft->is_abstract())
    7358                 :     1744999 :     subcontext.type = tleft;
    7359                 :      373666 :   else if (!tright->is_abstract())
    7360                 :       30913 :     subcontext.type = tright;
    7361                 :      342753 :   else if (subcontext.type == NULL)
    7362                 :             :     {
    7363                 :      324537 :       if ((tleft->integer_type() != NULL && tright->integer_type() != NULL)
    7364                 :     2623767 :           || (tleft->float_type() != NULL && tright->float_type() != NULL)
    7365                 :     2622250 :           || (tleft->complex_type() != NULL && tright->complex_type() != NULL)
    7366                 :      281929 :           || (tleft->is_boolean_type() && tright->is_boolean_type()))
    7367                 :             :         {
    7368                 :             :           // Both sides have an abstract integer, abstract float,
    7369                 :             :           // abstract complex, or abstract boolean type.  Just let
    7370                 :             :           // CONTEXT determine whether they may remain abstract or not.
    7371                 :             :         }
    7372                 :       40093 :       else if (tleft->complex_type() != NULL)
    7373                 :           8 :         subcontext.type = tleft;
    7374                 :       40085 :       else if (tright->complex_type() != NULL)
    7375                 :         818 :         subcontext.type = tright;
    7376                 :       39267 :       else if (tleft->float_type() != NULL)
    7377                 :         478 :         subcontext.type = tleft;
    7378                 :       38789 :       else if (tright->float_type() != NULL)
    7379                 :         360 :         subcontext.type = tright;
    7380                 :             :       else
    7381                 :       38429 :         subcontext.type = tleft;
    7382                 :             :     }
    7383                 :             : 
    7384                 :     2340269 :   if (left_is_untyped)
    7385                 :             :     {
    7386                 :      386266 :       this->left_->determine_type(gogo, &subcontext);
    7387                 :      386266 :       tleft = this->left_->type();
    7388                 :             :     }
    7389                 :             : 
    7390                 :     2340269 :   if (is_shift_op)
    7391                 :             :     {
    7392                 :             :       // We may have inherited an unusable type for the shift operand.
    7393                 :             :       // Give a useful error if that happened.
    7394                 :      221604 :       if (left_is_untyped
    7395                 :      221604 :           && !is_constant_expr
    7396                 :        1497 :           && subcontext.type != NULL
    7397                 :        1402 :           && !subcontext.may_be_abstract
    7398                 :        1402 :           && subcontext.type->interface_type() == NULL
    7399                 :      222994 :           && subcontext.type->integer_type() == NULL
    7400                 :         123 :           && !tleft->is_error()
    7401                 :      221727 :           && !tright->is_error())
    7402                 :         123 :         this->report_error(("invalid context-determined non-integer type "
    7403                 :             :                             "for left operand of shift"));
    7404                 :             : 
    7405                 :             :       // The context for the right hand operand is the same as for the
    7406                 :             :       // left hand operand, except for a shift operator.
    7407                 :      221604 :       subcontext.type = Type::lookup_integer_type("uint");
    7408                 :      221604 :       subcontext.may_be_abstract = false;
    7409                 :             :     }
    7410                 :             : 
    7411                 :     2340269 :   if (right_is_untyped)
    7412                 :             :     {
    7413                 :      572759 :       this->right_->determine_type(gogo, &subcontext);
    7414                 :      572759 :       tright = this->right_->type();
    7415                 :             :     }
    7416                 :             : 
    7417                 :     2340269 :   if (this->left_->is_error_expression()
    7418                 :     2340180 :       || tleft->is_error()
    7419                 :     2340179 :       || this->right_->is_error_expression()
    7420                 :     4680431 :       || tright->is_error())
    7421                 :             :     {
    7422                 :         107 :       this->set_is_error();
    7423                 :         107 :       return;
    7424                 :             :     }
    7425                 :             : 
    7426                 :     2340162 :   if (is_comparison)
    7427                 :             :     {
    7428                 :     1291513 :       if (context->type != NULL && context->type->is_boolean_type())
    7429                 :      543263 :         this->type_ = context->type;
    7430                 :      748250 :       else if (!context->may_be_abstract)
    7431                 :      646293 :         this->type_ = Type::lookup_bool_type();
    7432                 :             :       else
    7433                 :      101957 :         this->type_ = Type::make_boolean_type();
    7434                 :             :     }
    7435                 :             :   else
    7436                 :             :     {
    7437                 :     1048649 :       if (is_shift_op)
    7438                 :             :         {
    7439                 :             :           // Shifts only work with integers, so force an abstract
    7440                 :             :           // floating-point type (such as 1.0 << 1) into an integer.
    7441                 :      221597 :           if (tleft->is_abstract()
    7442                 :       11181 :               && tleft->integer_type() == NULL
    7443                 :      221685 :               && context->type == NULL)
    7444                 :             :             {
    7445                 :           2 :               this->type_ = Type::make_abstract_integer_type();
    7446                 :           2 :               if (!context->may_be_abstract)
    7447                 :           2 :                 this->type_ = this->type_->make_non_abstract_type();
    7448                 :             :             }
    7449                 :             :           else
    7450                 :      221595 :             this->type_ = tleft;
    7451                 :             :         }
    7452                 :             :       else
    7453                 :             :         {
    7454                 :      827052 :           if (!Binary_expression::operation_type(this->op_, tleft, tright,
    7455                 :             :                                                  &this->type_))
    7456                 :             :             {
    7457                 :          11 :               this->report_error("incompatible types in binary expression");
    7458                 :          11 :               this->type_ = Type::make_error_type();
    7459                 :          11 :               return;
    7460                 :             :             }
    7461                 :             :         }
    7462                 :             : 
    7463                 :             :       // If this is an untyped expression in a typed context, use the
    7464                 :             :       // context type.  If this doesn't work we'll report an error
    7465                 :             :       // later.
    7466                 :     1048638 :       if (this->type_->is_abstract()
    7467                 :      122376 :           && !context->may_be_abstract
    7468                 :     1120580 :           && context->type != NULL)
    7469                 :             :         {
    7470                 :       21134 :           if (context->type->interface_type() == NULL
    7471                 :       20897 :               && ((this->type_->is_numeric_type()
    7472                 :       19307 :                    && context->type->is_numeric_type())
    7473                 :        1602 :                   || (this->type_->is_string_type()
    7474                 :        1253 :                       && context->type->is_string_type())
    7475                 :         381 :                   || (this->type_->is_boolean_type()
    7476                 :         337 :                       && context->type->is_boolean_type())))
    7477                 :       20853 :             this->type_ = context->type;
    7478                 :     2340432 :           else if (context->type->interface_type() != NULL)
    7479                 :         237 :             this->type_ = this->type_->make_non_abstract_type();
    7480                 :             :         }
    7481                 :             :     }
    7482                 :             : }
    7483                 :             : 
    7484                 :             : // Report an error if the binary operator OP does not support TYPE.
    7485                 :             : // OTYPE is the type of the other operand.  Return whether the
    7486                 :             : // operation is OK.  This should not be used for shift.
    7487                 :             : 
    7488                 :             : bool
    7489                 :      869113 : Binary_expression::check_operator_type(Operator op, Type* type, Type* otype,
    7490                 :             :                                        Location location)
    7491                 :             : {
    7492                 :      869113 :   switch (op)
    7493                 :             :     {
    7494                 :       64063 :     case OPERATOR_OROR:
    7495                 :       64063 :     case OPERATOR_ANDAND:
    7496                 :       64063 :       if (!type->is_boolean_type()
    7497                 :      128126 :           || !otype->is_boolean_type())
    7498                 :             :         {
    7499                 :           0 :           go_error_at(location, "expected boolean type");
    7500                 :           0 :           return false;
    7501                 :             :         }
    7502                 :             :       break;
    7503                 :             : 
    7504                 :      417882 :     case OPERATOR_EQEQ:
    7505                 :      417882 :     case OPERATOR_NOTEQ:
    7506                 :      417882 :       {
    7507                 :      417882 :         std::string reason;
    7508                 :      417882 :         if (!Type::are_compatible_for_comparison(true, type, otype, &reason))
    7509                 :             :           {
    7510                 :          32 :             go_error_at(location, "%s", reason.c_str());
    7511                 :          32 :             return false;
    7512                 :             :           }
    7513                 :          32 :       }
    7514                 :      417850 :       break;
    7515                 :             : 
    7516                 :      152884 :     case OPERATOR_LT:
    7517                 :      152884 :     case OPERATOR_LE:
    7518                 :      152884 :     case OPERATOR_GT:
    7519                 :      152884 :     case OPERATOR_GE:
    7520                 :      152884 :       {
    7521                 :      152884 :         std::string reason;
    7522                 :      152884 :         if (!Type::are_compatible_for_comparison(false, type, otype, &reason))
    7523                 :             :           {
    7524                 :          20 :             go_error_at(location, "%s", reason.c_str());
    7525                 :          20 :             return false;
    7526                 :             :           }
    7527                 :          20 :       }
    7528                 :      152864 :       break;
    7529                 :             : 
    7530                 :      118426 :     case OPERATOR_PLUS:
    7531                 :      118426 :     case OPERATOR_PLUSEQ:
    7532                 :      173019 :       if ((!type->is_numeric_type() && !type->is_string_type())
    7533                 :      291445 :           || (!otype->is_numeric_type() && !otype->is_string_type()))
    7534                 :             :         {
    7535                 :           0 :           go_error_at(location,
    7536                 :             :                    "expected integer, floating, complex, or string type");
    7537                 :           0 :           return false;
    7538                 :             :         }
    7539                 :             :       break;
    7540                 :             : 
    7541                 :       76619 :     case OPERATOR_MINUS:
    7542                 :       76619 :     case OPERATOR_MINUSEQ:
    7543                 :       76619 :     case OPERATOR_MULT:
    7544                 :       76619 :     case OPERATOR_MULTEQ:
    7545                 :       76619 :     case OPERATOR_DIV:
    7546                 :       76619 :     case OPERATOR_DIVEQ:
    7547                 :      153237 :       if (!type->is_numeric_type() || !otype->is_numeric_type())
    7548                 :             :         {
    7549                 :           1 :           go_error_at(location, "expected integer, floating, or complex type");
    7550                 :           1 :           return false;
    7551                 :             :         }
    7552                 :             :       break;
    7553                 :             : 
    7554                 :       39239 :     case OPERATOR_MOD:
    7555                 :       39239 :     case OPERATOR_MODEQ:
    7556                 :       39239 :     case OPERATOR_OR:
    7557                 :       39239 :     case OPERATOR_OREQ:
    7558                 :       39239 :     case OPERATOR_AND:
    7559                 :       39239 :     case OPERATOR_ANDEQ:
    7560                 :       39239 :     case OPERATOR_XOR:
    7561                 :       39239 :     case OPERATOR_XOREQ:
    7562                 :       39239 :     case OPERATOR_BITCLEAR:
    7563                 :       39239 :     case OPERATOR_BITCLEAREQ:
    7564                 :       78466 :       if (type->integer_type() == NULL || otype->integer_type() == NULL)
    7565                 :             :         {
    7566                 :          12 :           go_error_at(location, "expected integer type");
    7567                 :          12 :           return false;
    7568                 :             :         }
    7569                 :             :       break;
    7570                 :             : 
    7571                 :           0 :     default:
    7572                 :           0 :       go_unreachable();
    7573                 :             :     }
    7574                 :             : 
    7575                 :             :   return true;
    7576                 :             : }
    7577                 :             : 
    7578                 :             : // Check types.
    7579                 :             : 
    7580                 :             : void
    7581                 :      594580 : Binary_expression::do_check_types(Gogo*)
    7582                 :             : {
    7583                 :      594580 :   if (this->classification() == EXPRESSION_ERROR)
    7584                 :             :     return;
    7585                 :             : 
    7586                 :      594348 :   Type* left_type = this->left_->type();
    7587                 :      594348 :   Type* right_type = this->right_->type();
    7588                 :      594348 :   if (left_type->is_error() || right_type->is_error())
    7589                 :             :     {
    7590                 :           1 :       this->set_is_error();
    7591                 :           1 :       return;
    7592                 :             :     }
    7593                 :             : 
    7594                 :      594347 :   if (this->op_ == OPERATOR_EQEQ
    7595                 :             :       || this->op_ == OPERATOR_NOTEQ
    7596                 :             :       || this->op_ == OPERATOR_LT
    7597                 :             :       || this->op_ == OPERATOR_LE
    7598                 :             :       || this->op_ == OPERATOR_GT
    7599                 :      594347 :       || this->op_ == OPERATOR_GE)
    7600                 :             :     {
    7601                 :      285441 :       if (left_type->is_nil_type() && right_type->is_nil_type())
    7602                 :             :         {
    7603                 :           2 :           this->report_error(_("invalid comparison of nil with nil"));
    7604                 :           2 :           return;
    7605                 :             :         }
    7606                 :      285439 :       if (!Type::are_assignable(left_type, right_type, NULL)
    7607                 :      285439 :           && !Type::are_assignable(right_type, left_type, NULL))
    7608                 :             :         {
    7609                 :          32 :           this->report_error(_("incompatible types in binary expression"));
    7610                 :          32 :           return;
    7611                 :             :         }
    7612                 :      285407 :       if (!Binary_expression::check_operator_type(this->op_, left_type,
    7613                 :             :                                                   right_type,
    7614                 :             :                                                   this->location())
    7615                 :      285407 :           || !Binary_expression::check_operator_type(this->op_, right_type,
    7616                 :             :                                                      left_type,
    7617                 :             :                                                      this->location()))
    7618                 :             :         {
    7619                 :          52 :           this->set_is_error();
    7620                 :          52 :           return;
    7621                 :             :         }
    7622                 :             :     }
    7623                 :      308906 :   else if (this->op_ != OPERATOR_LSHIFT && this->op_ != OPERATOR_RSHIFT)
    7624                 :             :     {
    7625                 :      274755 :       if (!Type::are_compatible_for_binop(left_type, right_type))
    7626                 :             :         {
    7627                 :          16 :           this->report_error(_("incompatible types in binary expression"));
    7628                 :          16 :           return;
    7629                 :             :         }
    7630                 :      274739 :       if (!Binary_expression::check_operator_type(this->op_, left_type,
    7631                 :             :                                                   right_type,
    7632                 :             :                                                   this->location()))
    7633                 :             :         {
    7634                 :          12 :           this->set_is_error();
    7635                 :          12 :           return;
    7636                 :             :         }
    7637                 :      274727 :       if (this->op_ == OPERATOR_DIV || this->op_ == OPERATOR_MOD)
    7638                 :             :         {
    7639                 :             :           // Division by a zero integer constant is an error.
    7640                 :       15069 :           Numeric_constant rconst;
    7641                 :       15069 :           unsigned long rval;
    7642                 :       15069 :           if (left_type->integer_type() != NULL
    7643                 :       12915 :               && this->right_->numeric_constant_value(&rconst)
    7644                 :        9862 :               && rconst.to_unsigned_long(&rval) == Numeric_constant::NC_UL_VALID
    7645                 :        9673 :               && rval == 0)
    7646                 :             :             {
    7647                 :           2 :               this->report_error(_("integer division by zero"));
    7648                 :           2 :               return;
    7649                 :             :             }
    7650                 :       15069 :         }
    7651                 :             :     }
    7652                 :             :   else
    7653                 :             :     {
    7654                 :       34151 :       if (left_type->integer_type() == NULL
    7655                 :          41 :           && !left_type->is_abstract()
    7656                 :          32 :           && !this->is_constant())
    7657                 :          32 :         this->report_error(_("shift of non-integer operand"));
    7658                 :             : 
    7659                 :       34151 :       if (right_type->is_string_type())
    7660                 :           0 :         this->report_error(_("shift count not integer"));
    7661                 :       34151 :       else if (!right_type->is_abstract()
    7662                 :       34151 :                && right_type->integer_type() == NULL)
    7663                 :           0 :         this->report_error(_("shift count not integer"));
    7664                 :             :       else
    7665                 :             :         {
    7666                 :       34151 :           Numeric_constant nc;
    7667                 :       34151 :           if (this->right_->numeric_constant_value(&nc))
    7668                 :             :             {
    7669                 :       30409 :               mpz_t val;
    7670                 :       30409 :               if (!nc.to_int(&val))
    7671                 :           0 :                 this->report_error(_("shift count not integer"));
    7672                 :             :               else
    7673                 :             :                 {
    7674                 :       30409 :                   if (mpz_sgn(val) < 0)
    7675                 :             :                     {
    7676                 :           1 :                       this->report_error(_("negative shift count"));
    7677                 :           1 :                       Location rloc = this->right_->location();
    7678                 :           1 :                       this->right_ = Expression::make_integer_ul(0, right_type,
    7679                 :             :                                                                  rloc);
    7680                 :             :                     }
    7681                 :       30409 :                   mpz_clear(val);
    7682                 :             :                 }
    7683                 :             :             }
    7684                 :       34151 :         }
    7685                 :             :     }
    7686                 :             : }
    7687                 :             : 
    7688                 :             : // Get the backend representation for a binary expression.
    7689                 :             : 
    7690                 :             : Bexpression*
    7691                 :     1994966 : Binary_expression::do_get_backend(Translate_context* context)
    7692                 :             : {
    7693                 :     1994966 :   Gogo* gogo = context->gogo();
    7694                 :     1994966 :   Location loc = this->location();
    7695                 :     1994966 :   Type* left_type = this->left_->type();
    7696                 :     1994966 :   Type* right_type = this->right_->type();
    7697                 :             : 
    7698                 :     1994966 :   bool use_left_type = true;
    7699                 :     1994966 :   bool is_shift_op = false;
    7700                 :     1994966 :   bool is_idiv_op = false;
    7701                 :     1994966 :   switch (this->op_)
    7702                 :             :     {
    7703                 :     1266836 :     case OPERATOR_EQEQ:
    7704                 :     1266836 :     case OPERATOR_NOTEQ:
    7705                 :     1266836 :     case OPERATOR_LT:
    7706                 :     1266836 :     case OPERATOR_LE:
    7707                 :     1266836 :     case OPERATOR_GT:
    7708                 :     1266836 :     case OPERATOR_GE:
    7709                 :     1266836 :       return Expression::comparison(context, this->type_, this->op_,
    7710                 :     1266836 :                                     this->left_, this->right_, loc);
    7711                 :             : 
    7712                 :             :     case OPERATOR_OROR:
    7713                 :             :     case OPERATOR_ANDAND:
    7714                 :      728130 :       use_left_type = false;
    7715                 :             :       break;
    7716                 :             :     case OPERATOR_PLUS:
    7717                 :             :     case OPERATOR_MINUS:
    7718                 :             :     case OPERATOR_OR:
    7719                 :             :     case OPERATOR_XOR:
    7720                 :             :     case OPERATOR_MULT:
    7721                 :             :       break;
    7722                 :        8928 :     case OPERATOR_DIV:
    7723                 :      744428 :       if (left_type->float_type() != NULL || left_type->complex_type() != NULL)
    7724                 :             :         break;
    7725                 :             :       // Fall through.
    7726                 :             :     case OPERATOR_MOD:
    7727                 :             :       is_idiv_op = true;
    7728                 :             :       break;
    7729                 :      143057 :     case OPERATOR_LSHIFT:
    7730                 :      143057 :     case OPERATOR_RSHIFT:
    7731                 :      143057 :       is_shift_op = true;
    7732                 :      143057 :       break;
    7733                 :        2018 :     case OPERATOR_BITCLEAR:
    7734                 :        2018 :       this->right_ = Expression::make_unary(OPERATOR_XOR, this->right_, loc);
    7735                 :             :     case OPERATOR_AND:
    7736                 :             :       break;
    7737                 :           0 :     default:
    7738                 :           0 :       go_unreachable();
    7739                 :             :     }
    7740                 :             : 
    7741                 :             :   // The only binary operation for string is +, and that should have
    7742                 :             :   // been converted to a String_concat_expression in do_lower.
    7743                 :      728130 :   go_assert(!left_type->is_string_type());
    7744                 :             : 
    7745                 :      728130 :   Bexpression* left = this->left_->get_backend(context);
    7746                 :      728130 :   Bexpression* right = this->right_->get_backend(context);
    7747                 :             : 
    7748                 :      728130 :   Type* type = use_left_type ? left_type : right_type;
    7749                 :      728130 :   Btype* btype = type->get_backend(gogo);
    7750                 :             : 
    7751                 :      728130 :   Bexpression* ret =
    7752                 :      728130 :       gogo->backend()->binary_expression(this->op_, left, right, loc);
    7753                 :      728130 :   ret = gogo->backend()->convert_expression(btype, ret, loc);
    7754                 :             : 
    7755                 :             :   // Initialize overflow constants.
    7756                 :      728130 :   Bexpression* overflow;
    7757                 :      728130 :   mpz_t zero;
    7758                 :      728130 :   mpz_init_set_ui(zero, 0UL);
    7759                 :      728130 :   mpz_t one;
    7760                 :      728130 :   mpz_init_set_ui(one, 1UL);
    7761                 :      728130 :   mpz_t neg_one;
    7762                 :      728130 :   mpz_init_set_si(neg_one, -1);
    7763                 :             : 
    7764                 :      728130 :   Btype* left_btype = left_type->get_backend(gogo);
    7765                 :      728130 :   Btype* right_btype = right_type->get_backend(gogo);
    7766                 :             : 
    7767                 :             :   // In Go, a shift larger than the size of the type is well-defined.
    7768                 :             :   // This is not true in C, so we need to insert a conditional.
    7769                 :             :   // We also need to check for a negative shift count.
    7770                 :      728130 :   if (is_shift_op)
    7771                 :             :     {
    7772                 :      143057 :       go_assert(left_type->integer_type() != NULL);
    7773                 :      143057 :       go_assert(right_type->integer_type() != NULL);
    7774                 :             : 
    7775                 :      286114 :       int bits = left_type->integer_type()->bits();
    7776                 :             : 
    7777                 :      143057 :       Numeric_constant nc;
    7778                 :      143057 :       unsigned long ul;
    7779                 :      143057 :       if (!this->right_->numeric_constant_value(&nc)
    7780                 :      139107 :           || nc.to_unsigned_long(&ul) != Numeric_constant::NC_UL_VALID
    7781                 :      282164 :           || ul >= static_cast<unsigned long>(bits))
    7782                 :             :         {
    7783                 :        4368 :           mpz_t bitsval;
    7784                 :        4368 :           mpz_init_set_ui(bitsval, bits);
    7785                 :        4368 :           Bexpression* bits_expr =
    7786                 :        4368 :             gogo->backend()->integer_constant_expression(right_btype, bitsval);
    7787                 :        4368 :           Bexpression* compare =
    7788                 :        4368 :             gogo->backend()->binary_expression(OPERATOR_LT,
    7789                 :             :                                                right, bits_expr, loc);
    7790                 :             : 
    7791                 :        4368 :           Bexpression* zero_expr =
    7792                 :        4368 :             gogo->backend()->integer_constant_expression(left_btype, zero);
    7793                 :        4368 :           overflow = zero_expr;
    7794                 :        4368 :           Bfunction* bfn = context->function()->func_value()->get_decl();
    7795                 :        4368 :           if (this->op_ == OPERATOR_RSHIFT
    7796                 :        5848 :               && !left_type->integer_type()->is_unsigned())
    7797                 :             :             {
    7798                 :         344 :               Bexpression* neg_expr =
    7799                 :         344 :                 gogo->backend()->binary_expression(OPERATOR_LT, left,
    7800                 :             :                                                    zero_expr, loc);
    7801                 :         344 :               Bexpression* neg_one_expr =
    7802                 :         344 :                 gogo->backend()->integer_constant_expression(left_btype,
    7803                 :             :                                                              neg_one);
    7804                 :         344 :               overflow = gogo->backend()->conditional_expression(bfn,
    7805                 :             :                                                                  btype,
    7806                 :             :                                                                  neg_expr,
    7807                 :             :                                                                  neg_one_expr,
    7808                 :             :                                                                  zero_expr,
    7809                 :             :                                                                  loc);
    7810                 :             :             }
    7811                 :        4368 :           ret = gogo->backend()->conditional_expression(bfn, btype, compare,
    7812                 :             :                                                         ret, overflow, loc);
    7813                 :        4368 :           mpz_clear(bitsval);
    7814                 :             :         }
    7815                 :             : 
    7816                 :      286114 :       if (!right_type->integer_type()->is_unsigned()
    7817                 :      143057 :           && (!this->right_->numeric_constant_value(&nc)
    7818                 :         425 :               || nc.to_unsigned_long(&ul) != Numeric_constant::NC_UL_VALID))
    7819                 :             :         {
    7820                 :         133 :           Bexpression* zero_expr =
    7821                 :         133 :             gogo->backend()->integer_constant_expression(right_btype, zero);
    7822                 :         133 :           Bexpression* compare =
    7823                 :         133 :             gogo->backend()->binary_expression(OPERATOR_LT, right, zero_expr,
    7824                 :             :                                                loc);
    7825                 :         133 :           Expression* crash = Runtime::make_call(gogo, Runtime::PANIC_SHIFT,
    7826                 :             :                                                  loc, 0);
    7827                 :         133 :           crash->determine_type_no_context(gogo);
    7828                 :         133 :           Bexpression* bcrash = crash->get_backend(context);
    7829                 :         133 :           Bfunction* bfn = context->function()->func_value()->get_decl();
    7830                 :         133 :           ret = gogo->backend()->conditional_expression(bfn, btype, compare,
    7831                 :             :                                                         bcrash, ret, loc);
    7832                 :             :         }
    7833                 :      143057 :     }
    7834                 :             : 
    7835                 :             :   // Add checks for division by zero and division overflow as needed.
    7836                 :      728130 :   if (is_idiv_op)
    7837                 :             :     {
    7838                 :       11583 :       if (gogo->check_divide_by_zero())
    7839                 :             :         {
    7840                 :             :           // right == 0
    7841                 :       11583 :           Bexpression* zero_expr =
    7842                 :       11583 :               gogo->backend()->integer_constant_expression(right_btype, zero);
    7843                 :       11583 :           Bexpression* check =
    7844                 :       11583 :               gogo->backend()->binary_expression(OPERATOR_EQEQ,
    7845                 :             :                                                  right, zero_expr, loc);
    7846                 :             : 
    7847                 :       11583 :           Expression* crash = Runtime::make_call(gogo, Runtime::PANIC_DIVIDE,
    7848                 :             :                                                  loc, 0);
    7849                 :       11583 :           crash->determine_type_no_context(gogo);
    7850                 :       11583 :           Bexpression* bcrash = crash->get_backend(context);
    7851                 :             : 
    7852                 :             :           // right == 0 ? (panicdivide(), 0) : ret
    7853                 :       11583 :           Bfunction* bfn = context->function()->func_value()->get_decl();
    7854                 :       11583 :           ret = gogo->backend()->conditional_expression(bfn, btype,
    7855                 :             :                                                         check, bcrash,
    7856                 :             :                                                         ret, loc);
    7857                 :             :         }
    7858                 :             : 
    7859                 :       11583 :       if (gogo->check_divide_overflow())
    7860                 :             :         {
    7861                 :             :           // right == -1
    7862                 :             :           // FIXME: It would be nice to say that this test is expected
    7863                 :             :           // to return false.
    7864                 :             : 
    7865                 :       11583 :           Bexpression* neg_one_expr =
    7866                 :       11583 :               gogo->backend()->integer_constant_expression(right_btype, neg_one);
    7867                 :       11583 :           Bexpression* check =
    7868                 :       11583 :               gogo->backend()->binary_expression(OPERATOR_EQEQ,
    7869                 :             :                                                  right, neg_one_expr, loc);
    7870                 :             : 
    7871                 :       11583 :           Bexpression* zero_expr =
    7872                 :       11583 :               gogo->backend()->integer_constant_expression(btype, zero);
    7873                 :       11583 :           Bexpression* one_expr =
    7874                 :       11583 :               gogo->backend()->integer_constant_expression(btype, one);
    7875                 :       11583 :           Bfunction* bfn = context->function()->func_value()->get_decl();
    7876                 :             : 
    7877                 :       23166 :           if (type->integer_type()->is_unsigned())
    7878                 :             :             {
    7879                 :             :               // An unsigned -1 is the largest possible number, so
    7880                 :             :               // dividing is always 1 or 0.
    7881                 :             : 
    7882                 :        5288 :               Bexpression* cmp =
    7883                 :        5288 :                   gogo->backend()->binary_expression(OPERATOR_EQEQ,
    7884                 :             :                                                      left, right, loc);
    7885                 :        5288 :               if (this->op_ == OPERATOR_DIV)
    7886                 :        3207 :                 overflow =
    7887                 :        3207 :                     gogo->backend()->conditional_expression(bfn, btype, cmp,
    7888                 :             :                                                             one_expr, zero_expr,
    7889                 :             :                                                             loc);
    7890                 :             :               else
    7891                 :        2081 :                 overflow =
    7892                 :        2081 :                     gogo->backend()->conditional_expression(bfn, btype, cmp,
    7893                 :             :                                                             zero_expr, left,
    7894                 :             :                                                             loc);
    7895                 :             :             }
    7896                 :             :           else
    7897                 :             :             {
    7898                 :             :               // Computing left / -1 is the same as computing - left,
    7899                 :             :               // which does not overflow since Go sets -fwrapv.
    7900                 :        6295 :               if (this->op_ == OPERATOR_DIV)
    7901                 :             :                 {
    7902                 :        4134 :                   Expression* negate_expr =
    7903                 :        4134 :                       Expression::make_unary(OPERATOR_MINUS, this->left_, loc);
    7904                 :        4134 :                   overflow = negate_expr->get_backend(context);
    7905                 :             :                 }
    7906                 :             :               else
    7907                 :             :                 overflow = zero_expr;
    7908                 :             :             }
    7909                 :       11583 :           overflow = gogo->backend()->convert_expression(btype, overflow, loc);
    7910                 :             : 
    7911                 :             :           // right == -1 ? - left : ret
    7912                 :       11583 :           ret = gogo->backend()->conditional_expression(bfn, btype,
    7913                 :             :                                                         check, overflow,
    7914                 :             :                                                         ret, loc);
    7915                 :             :         }
    7916                 :             :     }
    7917                 :             : 
    7918                 :      728130 :   mpz_clear(zero);
    7919                 :      728130 :   mpz_clear(one);
    7920                 :      728130 :   mpz_clear(neg_one);
    7921                 :      728130 :   return ret;
    7922                 :             : }
    7923                 :             : 
    7924                 :             : // Export a binary expression.
    7925                 :             : 
    7926                 :             : void
    7927                 :       16026 : Binary_expression::do_export(Export_function_body* efb) const
    7928                 :             : {
    7929                 :       16026 :   efb->write_c_string("(");
    7930                 :       16026 :   this->left_->export_expression(efb);
    7931                 :       16026 :   switch (this->op_)
    7932                 :             :     {
    7933                 :         607 :     case OPERATOR_OROR:
    7934                 :         607 :       efb->write_c_string(" || ");
    7935                 :         607 :       break;
    7936                 :        1193 :     case OPERATOR_ANDAND:
    7937                 :        1193 :       efb->write_c_string(" && ");
    7938                 :        1193 :       break;
    7939                 :        2323 :     case OPERATOR_EQEQ:
    7940                 :        2323 :       efb->write_c_string(" == ");
    7941                 :        2323 :       break;
    7942                 :         872 :     case OPERATOR_NOTEQ:
    7943                 :         872 :       efb->write_c_string(" != ");
    7944                 :         872 :       break;
    7945                 :        1526 :     case OPERATOR_LT:
    7946                 :        1526 :       efb->write_c_string(" < ");
    7947                 :        1526 :       break;
    7948                 :         785 :     case OPERATOR_LE:
    7949                 :         785 :       efb->write_c_string(" <= ");
    7950                 :         785 :       break;
    7951                 :         355 :     case OPERATOR_GT:
    7952                 :         355 :       efb->write_c_string(" > ");
    7953                 :         355 :       break;
    7954                 :        1052 :     case OPERATOR_GE:
    7955                 :        1052 :       efb->write_c_string(" >= ");
    7956                 :        1052 :       break;
    7957                 :        1746 :     case OPERATOR_PLUS:
    7958                 :        1746 :       efb->write_c_string(" + ");
    7959                 :        1746 :       break;
    7960                 :        1185 :     case OPERATOR_MINUS:
    7961                 :        1185 :       efb->write_c_string(" - ");
    7962                 :        1185 :       break;
    7963                 :         430 :     case OPERATOR_OR:
    7964                 :         430 :       efb->write_c_string(" | ");
    7965                 :         430 :       break;
    7966                 :         163 :     case OPERATOR_XOR:
    7967                 :         163 :       efb->write_c_string(" ^ ");
    7968                 :         163 :       break;
    7969                 :         534 :     case OPERATOR_MULT:
    7970                 :         534 :       efb->write_c_string(" * ");
    7971                 :         534 :       break;
    7972                 :         430 :     case OPERATOR_DIV:
    7973                 :         430 :       efb->write_c_string(" / ");
    7974                 :         430 :       break;
    7975                 :         187 :     case OPERATOR_MOD:
    7976                 :         187 :       efb->write_c_string(" % ");
    7977                 :         187 :       break;
    7978                 :         386 :     case OPERATOR_LSHIFT:
    7979                 :         386 :       efb->write_c_string(" << ");
    7980                 :         386 :       break;
    7981                 :        1125 :     case OPERATOR_RSHIFT:
    7982                 :        1125 :       efb->write_c_string(" >> ");
    7983                 :        1125 :       break;
    7984                 :        1046 :     case OPERATOR_AND:
    7985                 :        1046 :       efb->write_c_string(" & ");
    7986                 :        1046 :       break;
    7987                 :          81 :     case OPERATOR_BITCLEAR:
    7988                 :          81 :       efb->write_c_string(" &^ ");
    7989                 :          81 :       break;
    7990                 :           0 :     default:
    7991                 :           0 :       go_unreachable();
    7992                 :             :     }
    7993                 :       16026 :   this->right_->export_expression(efb);
    7994                 :       16026 :   efb->write_c_string(")");
    7995                 :       16026 : }
    7996                 :             : 
    7997                 :             : // Import a binary expression.
    7998                 :             : 
    7999                 :             : Expression*
    8000                 :       22026 : Binary_expression::do_import(Import_expression* imp, Location loc)
    8001                 :             : {
    8002                 :       22026 :   imp->require_c_string("(");
    8003                 :             : 
    8004                 :       22026 :   Expression* left = Expression::import_expression(imp, loc);
    8005                 :             : 
    8006                 :       22026 :   Operator op;
    8007                 :       22026 :   if (imp->match_c_string(" || "))
    8008                 :             :     {
    8009                 :         652 :       op = OPERATOR_OROR;
    8010                 :         652 :       imp->advance(4);
    8011                 :             :     }
    8012                 :       21374 :   else if (imp->match_c_string(" && "))
    8013                 :             :     {
    8014                 :        1823 :       op = OPERATOR_ANDAND;
    8015                 :        1823 :       imp->advance(4);
    8016                 :             :     }
    8017                 :       19551 :   else if (imp->match_c_string(" == "))
    8018                 :             :     {
    8019                 :        3241 :       op = OPERATOR_EQEQ;
    8020                 :        3241 :       imp->advance(4);
    8021                 :             :     }
    8022                 :       16310 :   else if (imp->match_c_string(" != "))
    8023                 :             :     {
    8024                 :         843 :       op = OPERATOR_NOTEQ;
    8025                 :         843 :       imp->advance(4);
    8026                 :             :     }
    8027                 :       15467 :   else if (imp->match_c_string(" < "))
    8028                 :             :     {
    8029                 :        1154 :       op = OPERATOR_LT;
    8030                 :        1154 :       imp->advance(3);
    8031                 :             :     }
    8032                 :       14313 :   else if (imp->match_c_string(" <= "))
    8033                 :             :     {
    8034                 :         988 :       op = OPERATOR_LE;
    8035                 :         988 :       imp->advance(4);
    8036                 :             :     }
    8037                 :       13325 :   else if (imp->match_c_string(" > "))
    8038                 :             :     {
    8039                 :         226 :       op = OPERATOR_GT;
    8040                 :         226 :       imp->advance(3);
    8041                 :             :     }
    8042                 :       13099 :   else if (imp->match_c_string(" >= "))
    8043                 :             :     {
    8044                 :        2482 :       op = OPERATOR_GE;
    8045                 :        2482 :       imp->advance(4);
    8046                 :             :     }
    8047                 :       10617 :   else if (imp->match_c_string(" + "))
    8048                 :             :     {
    8049                 :        1789 :       op = OPERATOR_PLUS;
    8050                 :        1789 :       imp->advance(3);
    8051                 :             :     }
    8052                 :        8828 :   else if (imp->match_c_string(" - "))
    8053                 :             :     {
    8054                 :        1554 :       op = OPERATOR_MINUS;
    8055                 :        1554 :       imp->advance(3);
    8056                 :             :     }
    8057                 :        7274 :   else if (imp->match_c_string(" | "))
    8058                 :             :     {
    8059                 :         347 :       op = OPERATOR_OR;
    8060                 :         347 :       imp->advance(3);
    8061                 :             :     }
    8062                 :        6927 :   else if (imp->match_c_string(" ^ "))
    8063                 :             :     {
    8064                 :         152 :       op = OPERATOR_XOR;
    8065                 :         152 :       imp->advance(3);
    8066                 :             :     }
    8067                 :        6775 :   else if (imp->match_c_string(" * "))
    8068                 :             :     {
    8069                 :         573 :       op = OPERATOR_MULT;
    8070                 :         573 :       imp->advance(3);
    8071                 :             :     }
    8072                 :        6202 :   else if (imp->match_c_string(" / "))
    8073                 :             :     {
    8074                 :         223 :       op = OPERATOR_DIV;
    8075                 :         223 :       imp->advance(3);
    8076                 :             :     }
    8077                 :        5979 :   else if (imp->match_c_string(" % "))
    8078                 :             :     {
    8079                 :          40 :       op = OPERATOR_MOD;
    8080                 :          40 :       imp->advance(3);
    8081                 :             :     }
    8082                 :        5939 :   else if (imp->match_c_string(" << "))
    8083                 :             :     {
    8084                 :         120 :       op = OPERATOR_LSHIFT;
    8085                 :         120 :       imp->advance(4);
    8086                 :             :     }
    8087                 :        5819 :   else if (imp->match_c_string(" >> "))
    8088                 :             :     {
    8089                 :         941 :       op = OPERATOR_RSHIFT;
    8090                 :         941 :       imp->advance(4);
    8091                 :             :     }
    8092                 :        4878 :   else if (imp->match_c_string(" & "))
    8093                 :             :     {
    8094                 :         940 :       op = OPERATOR_AND;
    8095                 :         940 :       imp->advance(3);
    8096                 :             :     }
    8097                 :        3938 :   else if (imp->match_c_string(" &^ "))
    8098                 :             :     {
    8099                 :          74 :       op = OPERATOR_BITCLEAR;
    8100                 :          74 :       imp->advance(4);
    8101                 :             :     }
    8102                 :        3864 :   else if (imp->match_c_string(")"))
    8103                 :             :     {
    8104                 :             :       // Not a binary operator after all.
    8105                 :        3864 :       imp->advance(1);
    8106                 :        3864 :       return left;
    8107                 :             :     }
    8108                 :             :   else
    8109                 :             :     {
    8110                 :           0 :       go_error_at(imp->location(), "unrecognized binary operator");
    8111                 :           0 :       return Expression::make_error(loc);
    8112                 :             :     }
    8113                 :             : 
    8114                 :       18162 :   Expression* right = Expression::import_expression(imp, loc);
    8115                 :             : 
    8116                 :       18162 :   imp->require_c_string(")");
    8117                 :             : 
    8118                 :       18162 :   return Expression::make_binary(op, left, right, loc);
    8119                 :             : }
    8120                 :             : 
    8121                 :             : // Dump ast representation of a binary expression.
    8122                 :             : 
    8123                 :             : void
    8124                 :           0 : Binary_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
    8125                 :             : {
    8126                 :           0 :   ast_dump_context->ostream() << "(";
    8127                 :           0 :   ast_dump_context->dump_expression(this->left_);
    8128                 :           0 :   ast_dump_context->ostream() << " ";
    8129                 :           0 :   ast_dump_context->dump_operator(this->op_);
    8130                 :           0 :   ast_dump_context->ostream() << " ";
    8131                 :           0 :   ast_dump_context->dump_expression(this->right_);
    8132                 :           0 :   ast_dump_context->ostream() << ") ";
    8133                 :           0 : }
    8134                 :             : 
    8135                 :             : // Make a binary expression.
    8136                 :             : 
    8137                 :             : Expression*
    8138                 :     2340375 : Expression::make_binary(Operator op, Expression* left, Expression* right,
    8139                 :             :                         Location location)
    8140                 :             : {
    8141                 :     2340375 :   return new Binary_expression(op, left, right, location);
    8142                 :             : }
    8143                 :             : 
    8144                 :             : // Implement a comparison.
    8145                 :             : 
    8146                 :             : Bexpression*
    8147                 :     1266836 : Expression::comparison(Translate_context* context, Type* result_type,
    8148                 :             :                        Operator op, Expression* left, Expression* right,
    8149                 :             :                        Location location)
    8150                 :             : {
    8151                 :     1266836 :   Gogo* gogo = context->gogo();
    8152                 :     1266836 :   Type* left_type = left->type();
    8153                 :     1266836 :   Type* right_type = right->type();
    8154                 :             : 
    8155                 :     1266836 :   Expression* zexpr = Expression::make_integer_ul(0, NULL, location);
    8156                 :             : 
    8157                 :     1266836 :   if (left_type->is_string_type() && right_type->is_string_type())
    8158                 :             :     {
    8159                 :       45349 :       go_assert(left->is_multi_eval_safe());
    8160                 :       45349 :       go_assert(right->is_multi_eval_safe());
    8161                 :             : 
    8162                 :       45349 :       if (op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ)
    8163                 :             :         {
    8164                 :             :           // (l.len == r.len
    8165                 :             :           //  ? (l.ptr == r.ptr ? true : memcmp(l.ptr, r.ptr, r.len) == 0)
    8166                 :             :           //  : false)
    8167                 :       44857 :           Expression* llen = Expression::make_string_info(left,
    8168                 :             :                                                           STRING_INFO_LENGTH,
    8169                 :             :                                                           location);
    8170                 :       44857 :           Expression* rlen = Expression::make_string_info(right,
    8171                 :             :                                                           STRING_INFO_LENGTH,
    8172                 :             :                                                           location);
    8173                 :       44857 :           Expression* leneq = Expression::make_binary(OPERATOR_EQEQ, llen, rlen,
    8174                 :             :                                                       location);
    8175                 :       44857 :           Expression* lptr = Expression::make_string_info(left->copy(),
    8176                 :             :                                                           STRING_INFO_DATA,
    8177                 :             :                                                           location);
    8178                 :       44857 :           Expression* rptr = Expression::make_string_info(right->copy(),
    8179                 :             :                                                           STRING_INFO_DATA,
    8180                 :             :                                                           location);
    8181                 :       44857 :           Expression* ptreq = Expression::make_binary(OPERATOR_EQEQ, lptr, rptr,
    8182                 :             :                                                       location);
    8183                 :       44857 :           Expression* btrue = Expression::make_boolean(true, location);
    8184                 :       44857 :           Expression* call = Runtime::make_call(gogo, Runtime::MEMCMP,
    8185                 :             :                                                 location, 3,
    8186                 :             :                                                 lptr->copy(), rptr->copy(),
    8187                 :             :                                                 rlen->copy());
    8188                 :       44857 :           Type* int32_type = Type::lookup_integer_type("int32");
    8189                 :       44857 :           Expression* zero = Expression::make_integer_ul(0, int32_type, location);
    8190                 :       44857 :           Expression* cmp = Expression::make_binary(OPERATOR_EQEQ, call, zero,
    8191                 :             :                                                     location);
    8192                 :       44857 :           Expression* cond = Expression::make_conditional(ptreq, btrue, cmp,
    8193                 :             :                                                           location);
    8194                 :       44857 :           Expression* bfalse = Expression::make_boolean(false, location);
    8195                 :       44857 :           left = Expression::make_conditional(leneq, cond, bfalse, location);
    8196                 :       44857 :           right = Expression::make_boolean(true, location);
    8197                 :             :         }
    8198                 :             :       else
    8199                 :             :         {
    8200                 :         492 :           left = Runtime::make_call(gogo, Runtime::CMPSTRING, location, 2,
    8201                 :             :                                     left, right);
    8202                 :         492 :           right = zexpr;
    8203                 :             :         }
    8204                 :             :     }
    8205                 :     1221487 :   else if ((left_type->interface_type() != NULL
    8206                 :       60273 :             && right_type->interface_type() == NULL
    8207                 :       52571 :             && !right_type->is_nil_type())
    8208                 :     1219943 :            || (left_type->interface_type() == NULL
    8209                 :     1161214 :                && !left_type->is_nil_type()
    8210                 :     1161210 :                && right_type->interface_type() != NULL))
    8211                 :             :     {
    8212                 :             :       // Comparing an interface value to a non-interface value.
    8213                 :        3236 :       if (left_type->interface_type() == NULL)
    8214                 :             :         {
    8215                 :             :           std::swap(left_type, right_type);
    8216                 :             :           std::swap(left, right);
    8217                 :             :         }
    8218                 :             : 
    8219                 :             :       // The right operand is not an interface.  We need to take its
    8220                 :             :       // address if it is not a direct interface type.
    8221                 :        1692 :       Expression* pointer_arg = NULL;
    8222                 :        1692 :       if (right_type->is_direct_iface_type())
    8223                 :         565 :         pointer_arg = Expression::unpack_direct_iface(right, location);
    8224                 :             :       else
    8225                 :             :         {
    8226                 :        1127 :           go_assert(right->is_addressable());
    8227                 :        1127 :           pointer_arg = Expression::make_unary(OPERATOR_AND, right,
    8228                 :             :                                                location);
    8229                 :             :         }
    8230                 :             : 
    8231                 :        1692 :       Expression* descriptor =
    8232                 :        1692 :           Expression::make_type_descriptor(right_type, location);
    8233                 :        2990 :       left = Runtime::make_call(gogo,
    8234                 :        3384 :                                 (left_type->interface_type()->is_empty()
    8235                 :             :                                  ? Runtime::EFACEVALEQ
    8236                 :             :                                  : Runtime::IFACEVALEQ),
    8237                 :             :                                 location, 3, left, descriptor,
    8238                 :             :                                 pointer_arg);
    8239                 :        1692 :       go_assert(op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ);
    8240                 :        1692 :       right = Expression::make_boolean(true, location);
    8241                 :             :     }
    8242                 :     1219795 :   else if (left_type->interface_type() != NULL
    8243                 :       58729 :            && right_type->interface_type() != NULL)
    8244                 :             :     {
    8245                 :        7702 :       Runtime::Function compare_function;
    8246                 :       15404 :       if (left_type->interface_type()->is_empty()
    8247                 :        8571 :           && right_type->interface_type()->is_empty())
    8248                 :             :         compare_function = Runtime::EFACEEQ;
    8249                 :       13766 :       else if (!left_type->interface_type()->is_empty()
    8250                 :       13716 :                && !right_type->interface_type()->is_empty())
    8251                 :             :         compare_function = Runtime::IFACEEQ;
    8252                 :             :       else
    8253                 :             :         {
    8254                 :         104 :           if (left_type->interface_type()->is_empty())
    8255                 :             :             {
    8256                 :          50 :               std::swap(left_type, right_type);
    8257                 :          50 :               std::swap(left, right);
    8258                 :             :             }
    8259                 :         104 :           go_assert(!left_type->interface_type()->is_empty());
    8260                 :         104 :           go_assert(right_type->interface_type()->is_empty());
    8261                 :             :           compare_function = Runtime::IFACEEFACEEQ;
    8262                 :             :         }
    8263                 :             : 
    8264                 :        7702 :       left = Runtime::make_call(gogo, compare_function, location, 2,
    8265                 :             :                                 left, right);
    8266                 :        7702 :       go_assert(op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ);
    8267                 :        7702 :       right = Expression::make_boolean(true, location);
    8268                 :             :     }
    8269                 :             : 
    8270                 :     1266836 :   if (left_type->is_nil_type()
    8271                 :     1266836 :       && (op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ))
    8272                 :             :     {
    8273                 :             :       std::swap(left_type, right_type);
    8274                 :             :       std::swap(left, right);
    8275                 :             :     }
    8276                 :             : 
    8277                 :     1266836 :   if (right_type->is_nil_type())
    8278                 :             :     {
    8279                 :      124823 :       right = Expression::make_nil(location);
    8280                 :      124823 :       if (left_type->array_type() != NULL
    8281                 :        6666 :           && left_type->array_type()->length() == NULL)
    8282                 :             :         {
    8283                 :        3333 :           Array_type* at = left_type->array_type();
    8284                 :        3333 :           left = at->get_value_pointer(context->gogo(), left);
    8285                 :             :         }
    8286                 :      121490 :       else if (left_type->interface_type() != NULL)
    8287                 :             :         {
    8288                 :             :           // An interface is nil if the first field is nil.
    8289                 :       51028 :           left = Expression::make_field_reference(left, 0, location);
    8290                 :             :         }
    8291                 :             :     }
    8292                 :             : 
    8293                 :     1266836 :   left->determine_type_no_context(gogo);
    8294                 :     1266836 :   right->determine_type_no_context(gogo);
    8295                 :             : 
    8296                 :     1266836 :   Bexpression* left_bexpr = left->get_backend(context);
    8297                 :     1266836 :   Bexpression* right_bexpr = right->get_backend(context);
    8298                 :             : 
    8299                 :     1266836 :   Bexpression* ret = gogo->backend()->binary_expression(op, left_bexpr,
    8300                 :             :                                                         right_bexpr, location);
    8301                 :     1266836 :   if (result_type != NULL)
    8302                 :     1266731 :     ret = gogo->backend()->convert_expression(result_type->get_backend(gogo),
    8303                 :             :                                               ret, location);
    8304                 :     1266836 :   return ret;
    8305                 :             : }
    8306                 :             : 
    8307                 :             : // Class String_concat_expression.
    8308                 :             : 
    8309                 :             : bool
    8310                 :        1461 : String_concat_expression::do_is_constant() const
    8311                 :             : {
    8312                 :        2295 :   for (Expression_list::const_iterator pe = this->exprs_->begin();
    8313                 :        2295 :        pe != this->exprs_->end();
    8314                 :         834 :        ++pe)
    8315                 :             :     {
    8316                 :        2295 :       if (!(*pe)->is_constant())
    8317                 :        1461 :         return false;
    8318                 :             :     }
    8319                 :             :   return true;
    8320                 :             : }
    8321                 :             : 
    8322                 :             : bool
    8323                 :         403 : String_concat_expression::do_is_untyped(Type** ptype) const
    8324                 :             : {
    8325                 :         403 :   for (Expression_list::iterator pe = this->exprs_->begin();
    8326                 :         403 :        pe != this->exprs_->end();
    8327                 :           0 :        ++pe)
    8328                 :             :     {
    8329                 :         403 :       if (!(*pe)->is_untyped(ptype))
    8330                 :         403 :         return false;
    8331                 :             :     }
    8332                 :             : 
    8333                 :           0 :   *ptype = Type::make_string_type();
    8334                 :           0 :   return true;
    8335                 :             : }
    8336                 :             : 
    8337                 :             : bool
    8338                 :           6 : String_concat_expression::do_is_zero_value() const
    8339                 :             : {
    8340                 :           6 :   for (Expression_list::const_iterator pe = this->exprs_->begin();
    8341                 :           6 :        pe != this->exprs_->end();
    8342                 :           0 :        ++pe)
    8343                 :             :     {
    8344                 :           6 :       if (!(*pe)->is_zero_value())
    8345                 :           6 :         return false;
    8346                 :             :     }
    8347                 :             :   return true;
    8348                 :             : }
    8349                 :             : 
    8350                 :             : bool
    8351                 :          39 : String_concat_expression::do_is_static_initializer() const
    8352                 :             : {
    8353                 :          52 :   for (Expression_list::const_iterator pe = this->exprs_->begin();
    8354                 :          52 :        pe != this->exprs_->end();
    8355                 :          13 :        ++pe)
    8356                 :             :     {
    8357                 :          52 :       if (!(*pe)->is_static_initializer())
    8358                 :          39 :         return false;
    8359                 :             :     }
    8360                 :             :   return true;
    8361                 :             : }
    8362                 :             : 
    8363                 :             : Type*
    8364                 :      109310 : String_concat_expression::do_type()
    8365                 :             : {
    8366                 :      109310 :   Type* t = this->exprs_->front()->type();
    8367                 :      109310 :   Expression_list::iterator pe = this->exprs_->begin();
    8368                 :      109310 :   ++pe;
    8369                 :      290786 :   for (; pe != this->exprs_->end(); ++pe)
    8370                 :             :     {
    8371                 :      181476 :       Type* t1;
    8372                 :      181476 :       if (!Binary_expression::operation_type(OPERATOR_PLUS, t,
    8373                 :      181476 :                                              (*pe)->type(),
    8374                 :             :                                              &t1))
    8375                 :           0 :         return Type::make_error_type();
    8376                 :      181476 :       t = t1;
    8377                 :             :     }
    8378                 :             :   return t;
    8379                 :             : }
    8380                 :             : 
    8381                 :             : void
    8382                 :        3795 : String_concat_expression::do_determine_type(Gogo* gogo,
    8383                 :             :                                             const Type_context* context)
    8384                 :             : {
    8385                 :        3795 :   Type_context subcontext(*context);
    8386                 :        3795 :   for (Expression_list::iterator pe = this->exprs_->begin();
    8387                 :        3795 :        pe != this->exprs_->end();
    8388                 :           0 :        ++pe)
    8389                 :             :     {
    8390                 :        3795 :       Type* t = (*pe)->type();
    8391                 :        3795 :       if (!t->is_abstract())
    8392                 :             :         {
    8393                 :        3795 :           subcontext.type = t;
    8394                 :        3795 :           break;
    8395                 :             :         }
    8396                 :             :     }
    8397                 :        3795 :   if (subcontext.type == NULL)
    8398                 :           0 :     subcontext.type = this->exprs_->front()->type();
    8399                 :       14101 :   for (Expression_list::iterator pe = this->exprs_->begin();
    8400                 :       14101 :        pe != this->exprs_->end();
    8401                 :       10306 :        ++pe)
    8402                 :       10306 :     (*pe)->determine_type(gogo, &subcontext);
    8403                 :        3795 : }
    8404                 :             : 
    8405                 :             : void
    8406                 :          21 : String_concat_expression::do_check_types(Gogo*)
    8407                 :             : {
    8408                 :          21 :   if (this->is_error_expression())
    8409                 :             :     return;
    8410                 :          21 :   Type* t = this->exprs_->front()->type();
    8411                 :          21 :   if (t->is_error())
    8412                 :             :     {
    8413                 :           0 :       this->set_is_error();
    8414                 :           0 :       return;
    8415                 :             :     }
    8416                 :          21 :   Expression_list::iterator pe = this->exprs_->begin();
    8417                 :          21 :   ++pe;
    8418                 :          48 :   for (; pe != this->exprs_->end(); ++pe)
    8419                 :             :     {
    8420                 :          27 :       Type* t1 = (*pe)->type();
    8421                 :          27 :       if (!Type::are_compatible_for_binop(t, t1))
    8422                 :             :         {
    8423                 :           0 :           this->report_error("incompatible types in binary expression");
    8424                 :           0 :           return;
    8425                 :             :         }
    8426                 :          27 :       if (!Binary_expression::check_operator_type(OPERATOR_PLUS, t, t1,
    8427                 :             :                                                   this->location()))
    8428                 :             :         {
    8429                 :           0 :           this->set_is_error();
    8430                 :           0 :           return;
    8431                 :             :         }
    8432                 :             :     }
    8433                 :             : }
    8434                 :             : 
    8435                 :             : Expression*
    8436                 :       10472 : String_concat_expression::do_flatten(Gogo* gogo, Named_object*,
    8437                 :             :                                      Statement_inserter* inserter)
    8438                 :             : {
    8439                 :       10472 :   if (this->is_error_expression())
    8440                 :           0 :     return this;
    8441                 :       10472 :   Location loc = this->location();
    8442                 :       10472 :   Type* type = this->type();
    8443                 :             : 
    8444                 :             :   // Mark string([]byte) operands to reuse the backing store.
    8445                 :             :   // runtime.concatstrings does not keep the reference.
    8446                 :             :   //
    8447                 :             :   // Note: in the gc runtime, if all but one inputs are empty,
    8448                 :             :   // concatstrings returns the only nonempty input without copy.
    8449                 :             :   // So it is not safe to reuse the backing store if it is a
    8450                 :             :   // string([]byte) conversion. So the gc compiler does the
    8451                 :             :   // no-copy optimization only when there is at least one
    8452                 :             :   // constant nonempty input. Currently the gccgo runtime
    8453                 :             :   // doesn't do this, so we don't do the check.
    8454                 :       10472 :   for (Expression_list::iterator p = this->exprs_->begin();
    8455                 :       37161 :        p != this->exprs_->end();
    8456                 :       26689 :        ++p)
    8457                 :             :     {
    8458                 :       27106 :       Type_conversion_expression* tce = (*p)->conversion_expression();
    8459                 :         417 :       if (tce != NULL)
    8460                 :         417 :         tce->set_no_copy(true);
    8461                 :             :     }
    8462                 :             : 
    8463                 :       10472 :   Expression* buf = NULL;
    8464                 :       10472 :   Node* n = Node::make_node(this);
    8465                 :       10472 :   if ((n->encoding() & ESCAPE_MASK) == Node::ESCAPE_NONE)
    8466                 :             :     {
    8467                 :        1205 :       size_t size = 0;
    8468                 :        1205 :       for (Expression_list::iterator p = this->exprs_->begin();
    8469                 :        4128 :            p != this->exprs_->end();
    8470                 :        2923 :            ++p)
    8471                 :             :         {
    8472                 :        2923 :           std::string s;
    8473                 :        2923 :           if ((*p)->string_constant_value(&s))
    8474                 :        1342 :             size += s.length();
    8475                 :        2923 :         }
    8476                 :             :       // Make a buffer on stack if the result does not escape.
    8477                 :             :       // But don't do this if we know it won't fit.
    8478                 :        1205 :       if (size < (size_t)tmp_string_buf_size)
    8479                 :             :         {
    8480                 :        1129 :           Type* byte_type = Type::lookup_integer_type("uint8");
    8481                 :        1129 :           Expression* buflen =
    8482                 :        1129 :             Expression::make_integer_ul(tmp_string_buf_size, NULL, loc);
    8483                 :        1129 :           Expression::make_integer_ul(tmp_string_buf_size, NULL, loc);
    8484                 :        1129 :           Type* array_type = Type::make_array_type(byte_type, buflen);
    8485                 :        1129 :           buf = Expression::make_allocation(array_type, loc);
    8486                 :        1129 :           buf->allocation_expression()->set_allocate_on_stack();
    8487                 :        1129 :           buf->allocation_expression()->set_no_zero();
    8488                 :             :         }
    8489                 :             :     }
    8490                 :        1129 :   if (buf == NULL)
    8491                 :        9343 :     buf = Expression::make_nil(loc);
    8492                 :       10472 :   go_assert(this->exprs_->size() > 1);
    8493                 :       10472 :   Expression* len =
    8494                 :       10472 :     Expression::make_integer_ul(this->exprs_->size(), NULL, loc);
    8495                 :       10472 :   Array_type* array_type = Type::make_array_type(type, len);
    8496                 :       10472 :   array_type->set_is_array_incomparable();
    8497                 :       10472 :   Expression* array =
    8498                 :       10472 :     Expression::make_array_composite_literal(array_type, this->exprs_,
    8499                 :             :                                              loc);
    8500                 :       10472 :   Temporary_statement* ts =
    8501                 :       10472 :     Statement::make_temporary(array_type, array, loc);
    8502                 :       10472 :   ts->determine_types(gogo);
    8503                 :       10472 :   inserter->insert(ts);
    8504                 :       10472 :   Expression* ref = Expression::make_temporary_reference(ts, loc);
    8505                 :       10472 :   ref = Expression::make_unary(OPERATOR_AND, ref, loc);
    8506                 :       10472 :   Expression* call =
    8507                 :       10472 :     Runtime::make_call(gogo, Runtime::CONCATSTRINGS, loc, 3, buf,
    8508                 :             :                        ref, len->copy());
    8509                 :       10472 :   Expression* ret = Expression::make_cast(type, call, loc);
    8510                 :       10472 :   Type_context context(type, false);
    8511                 :       10472 :   ret->determine_type(gogo, &context);
    8512                 :       10472 :   return ret;
    8513                 :             : }
    8514                 :             : 
    8515                 :             : void
    8516                 :           0 : String_concat_expression::do_dump_expression(
    8517                 :             :     Ast_dump_context* ast_dump_context) const
    8518                 :             : {
    8519                 :           0 :   ast_dump_context->ostream() << "concat(";
    8520                 :           0 :   ast_dump_context->dump_expression_list(this->exprs_, false);
    8521                 :           0 :   ast_dump_context->ostream() << ")";
    8522                 :           0 : }
    8523                 :             : 
    8524                 :             : Expression*
    8525                 :       16280 : Expression::make_string_concat(Expression_list* exprs)
    8526                 :             : {
    8527                 :       16280 :   return new String_concat_expression(exprs);
    8528                 :             : }
    8529                 :             : 
    8530                 :             : // Class Bound_method_expression.
    8531                 :             : 
    8532                 :             : // Traversal.
    8533                 :             : 
    8534                 :             : int
    8535                 :      401458 : Bound_method_expression::do_traverse(Traverse* traverse)
    8536                 :             : {
    8537                 :      401458 :   return Expression::traverse(&this->expr_, traverse);
    8538                 :             : }
    8539                 :             : 
    8540                 :             : // Return the type of a bound method expression.  The type of this
    8541                 :             : // object is simply the type of the method with no receiver.
    8542                 :             : 
    8543                 :             : Type*
    8544                 :     2418476 : Bound_method_expression::do_type()
    8545                 :             : {
    8546                 :     2418476 :   Named_object* fn = this->method_->named_object();
    8547                 :     2418476 :   Function_type* fntype;
    8548                 :     2418476 :   if (fn->is_function())
    8549                 :     1355971 :     fntype = fn->func_value()->type();
    8550                 :     1062505 :   else if (fn->is_function_declaration())
    8551                 :     1062505 :     fntype = fn->func_declaration_value()->type();
    8552                 :             :   else
    8553                 :           0 :     return Type::make_error_type();
    8554                 :     2418476 :   return fntype->copy_without_receiver();
    8555                 :             : }
    8556                 :             : 
    8557                 :             : // Determine the types of a method expression.
    8558                 :             : 
    8559                 :             : void
    8560                 :      264967 : Bound_method_expression::do_determine_type(Gogo* gogo, const Type_context*)
    8561                 :             : {
    8562                 :      264967 :   Named_object* fn = this->method_->named_object();
    8563                 :      264967 :   Function_type* fntype;
    8564                 :      264967 :   if (fn->is_function())
    8565                 :      137245 :     fntype = fn->func_value()->type();
    8566                 :      127722 :   else if (fn->is_function_declaration())
    8567                 :      127722 :     fntype = fn->func_declaration_value()->type();
    8568                 :             :   else
    8569                 :             :     fntype = NULL;
    8570                 :      264967 :   if (fntype == NULL || !fntype->is_method())
    8571                 :           0 :     this->expr_->determine_type_no_context(gogo);
    8572                 :             :   else
    8573                 :             :     {
    8574                 :      264967 :       Type_context subcontext(fntype->receiver()->type(), false);
    8575                 :      264967 :       this->expr_->determine_type(gogo, &subcontext);
    8576                 :             :     }
    8577                 :      264967 : }
    8578                 :             : 
    8579                 :             : // Check the types of a method expression.
    8580                 :             : 
    8581                 :             : void
    8582                 :       23667 : Bound_method_expression::do_check_types(Gogo*)
    8583                 :             : {
    8584                 :       23667 :   Named_object* fn = this->function();
    8585                 :       23667 :   if (!fn->is_function() && !fn->is_function_declaration())
    8586                 :             :     {
    8587                 :           0 :       this->report_error(_("object is not a method"));
    8588                 :           0 :       return;
    8589                 :             :     }
    8590                 :             : 
    8591                 :       23667 :   Function_type* fntype;
    8592                 :       23667 :   if (fn->is_function())
    8593                 :       10989 :     fntype = fn->func_value()->type();
    8594                 :       12678 :   else if (fn->is_function_declaration())
    8595                 :       12678 :     fntype = fn->func_declaration_value()->type();
    8596                 :             :   else
    8597                 :             :     go_unreachable();
    8598                 :       23667 :   Type* rtype = fntype->receiver()->type()->deref();
    8599                 :       23667 :   Type* etype = (this->expr_type_ != NULL
    8600                 :       23667 :                  ? this->expr_type_
    8601                 :       23667 :                  : this->expr_->type());
    8602                 :       23667 :   etype = etype->deref();
    8603                 :       23667 :   if (!Type::are_identical(rtype, etype, Type::COMPARE_TAGS, NULL))
    8604                 :           0 :     this->report_error(_("method type does not match object type"));
    8605                 :             : }
    8606                 :             : 
    8607                 :             : // If a bound method expression is not simply called, then it is
    8608                 :             : // represented as a closure.  The closure will hold a single variable,
    8609                 :             : // the receiver to pass to the method.  The function will be a simple
    8610                 :             : // thunk that pulls that value from the closure and calls the method
    8611                 :             : // with the remaining arguments.
    8612                 :             : //
    8613                 :             : // Because method values are not common, we don't build all thunks for
    8614                 :             : // every methods, but instead only build them as we need them.  In
    8615                 :             : // particular, we even build them on demand for methods defined in
    8616                 :             : // other packages.
    8617                 :             : 
    8618                 :             : Bound_method_expression::Method_value_thunks
    8619                 :             :   Bound_method_expression::method_value_thunks;
    8620                 :             : 
    8621                 :             : // Find or create the thunk for FN.
    8622                 :             : 
    8623                 :             : Named_object*
    8624                 :         934 : Bound_method_expression::create_thunk(Gogo* gogo, const Method* method,
    8625                 :             :                                       Named_object* fn)
    8626                 :             : {
    8627                 :         934 :   std::pair<Named_object*, Named_object*> val(fn, NULL);
    8628                 :         934 :   std::pair<Method_value_thunks::iterator, bool> ins =
    8629                 :         934 :     Bound_method_expression::method_value_thunks.insert(val);
    8630                 :         934 :   if (!ins.second)
    8631                 :             :     {
    8632                 :             :       // We have seen this method before.
    8633                 :         287 :       go_assert(ins.first->second != NULL);
    8634                 :         287 :       return ins.first->second;
    8635                 :             :     }
    8636                 :             : 
    8637                 :         647 :   Location loc = fn->location();
    8638                 :             : 
    8639                 :         647 :   Function_type* orig_fntype;
    8640                 :         647 :   if (fn->is_function())
    8641                 :         541 :     orig_fntype = fn->func_value()->type();
    8642                 :         106 :   else if (fn->is_function_declaration())
    8643                 :         106 :     orig_fntype = fn->func_declaration_value()->type();
    8644                 :             :   else
    8645                 :             :     orig_fntype = NULL;
    8646                 :             : 
    8647                 :         647 :   if (orig_fntype == NULL || !orig_fntype->is_method())
    8648                 :             :     {
    8649                 :           0 :       ins.first->second =
    8650                 :           0 :         Named_object::make_erroneous_name(gogo->thunk_name());
    8651                 :           0 :       return ins.first->second;
    8652                 :             :     }
    8653                 :             : 
    8654                 :         647 :   Struct_field_list* sfl = new Struct_field_list();
    8655                 :             :   // The type here is wrong--it should be the C function type.  But it
    8656                 :             :   // doesn't really matter.
    8657                 :         647 :   Type* vt = Type::make_pointer_type(Type::make_void_type());
    8658                 :         647 :   sfl->push_back(Struct_field(Typed_identifier("fn", vt, loc)));
    8659                 :         647 :   sfl->push_back(Struct_field(Typed_identifier("val",
    8660                 :             :                                                orig_fntype->receiver()->type(),
    8661                 :             :                                                loc)));
    8662                 :         647 :   Struct_type* st = Type::make_struct_type(sfl, loc);
    8663                 :         647 :   st->set_is_struct_incomparable();
    8664                 :         647 :   Type* closure_type = Type::make_pointer_type(st);
    8665                 :             : 
    8666                 :         647 :   Function_type* new_fntype = orig_fntype->copy_with_names();
    8667                 :             : 
    8668                 :         647 :   std::string thunk_name = gogo->thunk_name();
    8669                 :         647 :   Named_object* new_no = gogo->start_function(thunk_name, new_fntype,
    8670                 :             :                                               false, loc);
    8671                 :             : 
    8672                 :         647 :   Variable* cvar = new Variable(closure_type, NULL, false, false, false, loc);
    8673                 :         647 :   cvar->set_is_used();
    8674                 :         647 :   cvar->set_is_closure();
    8675                 :         647 :   Named_object* cp = Named_object::make_variable("$closure" + thunk_name,
    8676                 :             :                                                  NULL, cvar);
    8677                 :         647 :   new_no->func_value()->set_closure_var(cp);
    8678                 :             : 
    8679                 :         647 :   gogo->start_block(loc);
    8680                 :             : 
    8681                 :             :   // Field 0 of the closure is the function code pointer, field 1 is
    8682                 :             :   // the value on which to invoke the method.
    8683                 :         647 :   Expression* arg = Expression::make_var_reference(cp, loc);
    8684                 :         647 :   arg = Expression::make_dereference(arg, NIL_CHECK_NOT_NEEDED, loc);
    8685                 :         647 :   arg = Expression::make_field_reference(arg, 1, loc);
    8686                 :             : 
    8687                 :         647 :   Expression* bme = Expression::make_bound_method(arg, method, fn, loc);
    8688                 :             : 
    8689                 :         647 :   const Typed_identifier_list* orig_params = orig_fntype->parameters();
    8690                 :         647 :   Expression_list* args;
    8691                 :         647 :   if (orig_params == NULL || orig_params->empty())
    8692                 :             :     args = NULL;
    8693                 :             :   else
    8694                 :             :     {
    8695                 :         386 :       const Typed_identifier_list* new_params = new_fntype->parameters();
    8696                 :         386 :       args = new Expression_list();
    8697                 :         386 :       for (Typed_identifier_list::const_iterator p = new_params->begin();
    8698                 :        1135 :            p != new_params->end();
    8699                 :         749 :            ++p)
    8700                 :             :         {
    8701                 :         749 :           Named_object* p_no = gogo->lookup(p->name(), NULL);
    8702                 :         749 :           go_assert(p_no != NULL
    8703                 :             :                     && p_no->is_variable()
    8704                 :             :                     && p_no->var_value()->is_parameter());
    8705                 :         749 :           args->push_back(Expression::make_var_reference(p_no, loc));
    8706                 :             :         }
    8707                 :             :     }
    8708                 :             : 
    8709                 :         647 :   Call_expression* call = Expression::make_call(bme, args,
    8710                 :         647 :                                                 orig_fntype->is_varargs(),
    8711                 :             :                                                 loc);
    8712                 :         647 :   call->set_varargs_are_lowered();
    8713                 :             : 
    8714                 :         647 :   Statement* s = Statement::make_return_from_call(new_no, call, loc);
    8715                 :         647 :   s->determine_types(gogo);
    8716                 :         647 :   gogo->add_statement(s);
    8717                 :         647 :   Block* b = gogo->finish_block(loc);
    8718                 :         647 :   gogo->add_block(b, loc);
    8719                 :             : 
    8720                 :             :   // This is called after lowering.
    8721                 :         647 :   gogo->lower_block(new_no, b);
    8722                 :             : 
    8723                 :         647 :   gogo->finish_function(loc);
    8724                 :             : 
    8725                 :         647 :   ins.first->second = new_no;
    8726                 :         647 :   return new_no;
    8727                 :         647 : }
    8728                 :             : 
    8729                 :             : // Look up a thunk for FN.
    8730                 :             : 
    8731                 :             : Named_object*
    8732                 :         934 : Bound_method_expression::lookup_thunk(Named_object* fn)
    8733                 :             : {
    8734                 :         934 :   Method_value_thunks::const_iterator p =
    8735                 :         934 :     Bound_method_expression::method_value_thunks.find(fn);
    8736                 :         934 :   if (p == Bound_method_expression::method_value_thunks.end())
    8737                 :             :     return NULL;
    8738                 :         934 :   return p->second;
    8739                 :             : }
    8740                 :             : 
    8741                 :             : // Return an expression to check *REF for nil while dereferencing
    8742                 :             : // according to FIELD_INDEXES.  Update *REF to build up the field
    8743                 :             : // reference.  This is a static function so that we don't have to
    8744                 :             : // worry about declaring Field_indexes in expressions.h.
    8745                 :             : 
    8746                 :             : static Expression*
    8747                 :          31 : bme_check_nil(const Method::Field_indexes* field_indexes, Location loc,
    8748                 :             :               Expression** ref)
    8749                 :             : {
    8750                 :          31 :   if (field_indexes == NULL)
    8751                 :          14 :     return Expression::make_boolean(false, loc);
    8752                 :          17 :   Expression* cond = bme_check_nil(field_indexes->next, loc, ref);
    8753                 :          34 :   Struct_type* stype = (*ref)->type()->deref()->struct_type();
    8754                 :          17 :   go_assert(stype != NULL
    8755                 :             :             && field_indexes->field_index < stype->field_count());
    8756                 :          17 :   if ((*ref)->type()->struct_type() == NULL)
    8757                 :             :     {
    8758                 :          10 :       go_assert((*ref)->type()->points_to() != NULL);
    8759                 :          10 :       Expression* n = Expression::make_binary(OPERATOR_EQEQ, *ref,
    8760                 :             :                                               Expression::make_nil(loc),
    8761                 :             :                                               loc);
    8762                 :          10 :       cond = Expression::make_binary(OPERATOR_OROR, cond, n, loc);
    8763                 :          10 :       *ref = Expression::make_dereference(*ref, Expression::NIL_CHECK_DEFAULT,
    8764                 :             :                                           loc);
    8765                 :          20 :       go_assert((*ref)->type()->struct_type() == stype);
    8766                 :             :     }
    8767                 :          17 :   *ref = Expression::make_field_reference(*ref, field_indexes->field_index,
    8768                 :             :                                           loc);
    8769                 :          17 :   return cond;
    8770                 :             : }
    8771                 :             : 
    8772                 :             : // Flatten a method value into a struct with nil checks.  We can't do
    8773                 :             : // this in the lowering phase, because if the method value is called
    8774                 :             : // directly we don't need a thunk.  That case will have been handled
    8775                 :             : // by Call_expression::do_lower, so if we get here then we do need a
    8776                 :             : // thunk.
    8777                 :             : 
    8778                 :             : Expression*
    8779                 :         934 : Bound_method_expression::do_flatten(Gogo* gogo, Named_object*,
    8780                 :             :                                     Statement_inserter* inserter)
    8781                 :             : {
    8782                 :         934 :   Location loc = this->location();
    8783                 :             : 
    8784                 :         934 :   Named_object* thunk = Bound_method_expression::lookup_thunk(this->function_);
    8785                 :             : 
    8786                 :             :   // The thunk should have been created during the
    8787                 :             :   // create_function_descriptors pass.
    8788                 :         934 :   if (thunk == NULL || thunk->is_erroneous())
    8789                 :             :     {
    8790                 :           0 :       go_assert(saw_errors());
    8791                 :           0 :       return Expression::make_error(loc);
    8792                 :             :     }
    8793                 :             : 
    8794                 :             :   // Force the expression into a variable.  This is only necessary if
    8795                 :             :   // we are going to do nil checks below, but it's easy enough to
    8796                 :             :   // always do it.
    8797                 :         934 :   Expression* expr = this->expr_;
    8798                 :         934 :   if (!expr->is_multi_eval_safe())
    8799                 :             :     {
    8800                 :         292 :       Temporary_statement* etemp = Statement::make_temporary(NULL, expr, loc);
    8801                 :         292 :       inserter->insert(etemp);
    8802                 :         292 :       expr = Expression::make_temporary_reference(etemp, loc);
    8803                 :             :     }
    8804                 :             : 
    8805                 :             :   // If the method expects a value, and we have a pointer, we need to
    8806                 :             :   // dereference the pointer.
    8807                 :             : 
    8808                 :         934 :   Named_object* fn = this->method_->named_object();
    8809                 :         934 :   Function_type *fntype;
    8810                 :         934 :   if (fn->is_function())
    8811                 :         814 :     fntype = fn->func_value()->type();
    8812                 :         120 :   else if (fn->is_function_declaration())
    8813                 :         120 :     fntype = fn->func_declaration_value()->type();
    8814                 :             :   else
    8815                 :           0 :     go_unreachable();
    8816                 :             : 
    8817                 :         934 :   Expression* val = expr;
    8818                 :         934 :   if (fntype->receiver()->type()->points_to() == NULL
    8819                 :        1135 :       && val->type()->points_to() != NULL)
    8820                 :          25 :     val = Expression::make_dereference(val, NIL_CHECK_DEFAULT, loc);
    8821                 :             : 
    8822                 :             :   // Note that we are ignoring this->expr_type_ here.  The thunk will
    8823                 :             :   // expect a closure whose second field has type this->expr_type_ (if
    8824                 :             :   // that is not NULL).  We are going to pass it a closure whose
    8825                 :             :   // second field has type this->expr_->type().  Since
    8826                 :             :   // this->expr_type_ is only not-NULL for pointer types, we can get
    8827                 :             :   // away with this.
    8828                 :             : 
    8829                 :         934 :   Struct_field_list* fields = new Struct_field_list();
    8830                 :         934 :   fields->push_back(Struct_field(Typed_identifier("fn",
    8831                 :         934 :                                                   thunk->func_value()->type(),
    8832                 :             :                                                   loc)));
    8833                 :         934 :   fields->push_back(Struct_field(Typed_identifier("val", val->type(), loc)));
    8834                 :         934 :   Struct_type* st = Type::make_struct_type(fields, loc);
    8835                 :         934 :   st->set_is_struct_incomparable();
    8836                 :             : 
    8837                 :         934 :   Expression_list* vals = new Expression_list();
    8838                 :         934 :   vals->push_back(Expression::make_func_code_reference(thunk, loc));
    8839                 :         934 :   vals->push_back(val);
    8840                 :             : 
    8841                 :         934 :   Expression* ret = Expression::make_struct_composite_literal(st, vals, loc);
    8842                 :         934 :   ret = Expression::make_heap_expression(ret, loc);
    8843                 :             : 
    8844                 :         934 :   Node* node = Node::make_node(this);
    8845                 :         934 :   if ((node->encoding() & ESCAPE_MASK) == Node::ESCAPE_NONE)
    8846                 :         417 :     ret->heap_expression()->set_allocate_on_stack();
    8847                 :         517 :   else if (gogo->compiling_runtime()
    8848                 :           0 :            && gogo->package_name() == "runtime"
    8849                 :         517 :            && !saw_errors())
    8850                 :           0 :     go_error_at(loc, "%s escapes to heap, not allowed in runtime",
    8851                 :           0 :                 node->ast_format(gogo).c_str());
    8852                 :             : 
    8853                 :             :   // If necessary, check whether the expression or any embedded
    8854                 :             :   // pointers are nil.
    8855                 :             : 
    8856                 :         934 :   Expression* nil_check = NULL;
    8857                 :         934 :   if (this->method_->field_indexes() != NULL)
    8858                 :             :     {
    8859                 :          14 :       Expression* ref = expr;
    8860                 :          14 :       nil_check = bme_check_nil(this->method_->field_indexes(), loc, &ref);
    8861                 :          14 :       expr = ref;
    8862                 :             :     }
    8863                 :             : 
    8864                 :        1135 :   if (this->method_->is_value_method() && expr->type()->points_to() != NULL)
    8865                 :             :     {
    8866                 :          27 :       Expression* n = Expression::make_binary(OPERATOR_EQEQ, expr,
    8867                 :             :                                               Expression::make_nil(loc),
    8868                 :             :                                               loc);
    8869                 :          27 :       if (nil_check == NULL)
    8870                 :             :         nil_check = n;
    8871                 :             :       else
    8872                 :           6 :         nil_check = Expression::make_binary(OPERATOR_OROR, nil_check, n, loc);
    8873                 :             :     }
    8874                 :             : 
    8875                 :         934 :   if (nil_check != NULL)
    8876                 :             :     {
    8877                 :          35 :       Expression* crash = Runtime::make_call(gogo, Runtime::PANIC_MEM, loc, 0);
    8878                 :             :       // Fix the type of the conditional expression by pretending to
    8879                 :             :       // evaluate to RET either way through the conditional.
    8880                 :          35 :       crash->determine_type_no_context(gogo);
    8881                 :          35 :       crash = Expression::make_compound(crash, ret, loc);
    8882                 :          35 :       ret = Expression::make_conditional(nil_check, crash, ret, loc);
    8883                 :             :     }
    8884                 :             : 
    8885                 :             :   // RET is a pointer to a struct, but we want a function type.
    8886                 :         934 :   ret = Expression::make_unsafe_cast(this->type(), ret, loc);
    8887                 :             : 
    8888                 :         934 :   ret->determine_type_no_context(gogo);
    8889                 :             : 
    8890                 :         934 :   return ret;
    8891                 :             : }
    8892                 :             : 
    8893                 :             : // Dump ast representation of a bound method expression.
    8894                 :             : 
    8895                 :             : void
    8896                 :           0 : Bound_method_expression::do_dump_expression(Ast_dump_context* ast_dump_context)
    8897                 :             :     const
    8898                 :             : {
    8899                 :           0 :   if (this->expr_type_ != NULL)
    8900                 :           0 :     ast_dump_context->ostream() << "(";
    8901                 :           0 :   ast_dump_context->dump_expression(this->expr_);
    8902                 :           0 :   if (this->expr_type_ != NULL)
    8903                 :             :     {
    8904                 :           0 :       ast_dump_context->ostream() << ":";
    8905                 :           0 :       ast_dump_context->dump_type(this->expr_type_);
    8906                 :           0 :       ast_dump_context->ostream() << ")";
    8907                 :             :     }
    8908                 :             : 
    8909                 :           0 :   ast_dump_context->ostream() << "." << this->function_->name();
    8910                 :           0 : }
    8911                 :             : 
    8912                 :             : // Make a method expression.
    8913                 :             : 
    8914                 :             : Bound_method_expression*
    8915                 :      264726 : Expression::make_bound_method(Expression* expr, const Method* method,
    8916                 :             :                               Named_object* function, Location location)
    8917                 :             : {
    8918                 :      264726 :   return new Bound_method_expression(expr, method, function, location);
    8919                 :             : }
    8920                 :             : 
    8921                 :             : // A general selector.  This is a Parser_expression for LEFT.NAME.  It
    8922                 :             : // is lowered after we know the type of the left hand side.
    8923                 :             : 
    8924                 :             : class Selector_expression : public Parser_expression
    8925                 :             : {
    8926                 :             :  public:
    8927                 :      700127 :   Selector_expression(Expression* left, const std::string& name,
    8928                 :             :                       Location location)
    8929                 :      700127 :     : Parser_expression(EXPRESSION_SELECTOR, location),
    8930                 :      700127 :       left_(left), name_(name), resolved_(NULL)
    8931                 :      700127 :   { }
    8932                 :             : 
    8933                 :             :   // Return the resolved selector.  This will typically be a
    8934                 :             :   // Field_reference_expression or a Bound_method_expression or an
    8935                 :             :   // Interface_field_reference_expression.
    8936                 :             :   Expression*
    8937                 :        1156 :   resolved()
    8938                 :        1156 :   { return this->resolved_; }
    8939                 :             : 
    8940                 :             :  protected:
    8941                 :             :   int
    8942                 :     3601519 :   do_traverse(Traverse* traverse)
    8943                 :     3601519 :   { return Expression::traverse(&this->left_, traverse); }
    8944                 :             : 
    8945                 :             :   Type*
    8946                 :             :   do_type();
    8947                 :             : 
    8948                 :             :   void
    8949                 :             :   do_determine_type(Gogo*, const Type_context*);
    8950                 :             : 
    8951                 :             :   bool
    8952                 :             :   do_is_addressable() const;
    8953                 :             : 
    8954                 :             :   void
    8955                 :             :   do_issue_nil_check();
    8956                 :             : 
    8957                 :             :   Expression*
    8958                 :             :   do_lower(Gogo*, Named_object*, Statement_inserter*);
    8959                 :             : 
    8960                 :             :   Expression*
    8961                 :           0 :   do_copy()
    8962                 :             :   {
    8963                 :           0 :     return new Selector_expression(this->left_->copy(), this->name_,
    8964                 :           0 :                                    this->location());
    8965                 :             :   }
    8966                 :             : 
    8967                 :             :   void
    8968                 :             :   do_dump_expression(Ast_dump_context* ast_dump_context) const;
    8969                 :             : 
    8970                 :             :  private:
    8971                 :             :   Expression*
    8972                 :             :   lower_method_expression(Gogo*);
    8973                 :             : 
    8974                 :             :   // The expression on the left hand side.
    8975                 :             :   Expression* left_;
    8976                 :             :   // The name on the right hand side.
    8977                 :             :   std::string name_;
    8978                 :             :   // The resolved expression.
    8979                 :             :   Expression* resolved_;
    8980                 :             : };
    8981                 :             : 
    8982                 :             : void
    8983                 :      807096 : Selector_expression::do_determine_type(Gogo* gogo, const Type_context* context)
    8984                 :             : {
    8985                 :      807096 :   if (this->is_error_expression() || this->resolved_ != NULL)
    8986                 :             :     return;
    8987                 :      700125 :   Expression* left = this->left_;
    8988                 :      700125 :   left->determine_type_no_context(gogo);
    8989                 :      700125 :   if (left->is_error_expression())
    8990                 :           7 :     this->set_is_error();
    8991                 :             :   else
    8992                 :             :     {
    8993                 :      700118 :       if (left->is_type_expression())
    8994                 :         602 :         this->resolved_ = this->lower_method_expression(gogo);
    8995                 :             :       else
    8996                 :      699516 :         this->resolved_ = Type::bind_field_or_method(gogo, left->type(), left,
    8997                 :      699516 :                                                      this->name_,
    8998                 :             :                                                      this->location());
    8999                 :      700118 :       this->resolved_->determine_type(gogo, context);
    9000                 :             :     }
    9001                 :             : }
    9002                 :             : 
    9003                 :             : Type*
    9004                 :     2874842 : Selector_expression::do_type()
    9005                 :             : {
    9006                 :     2874842 :   if (this->is_error_expression())
    9007                 :          12 :     return Type::make_error_type();
    9008                 :     2874830 :   go_assert(this->resolved_ != NULL);
    9009                 :     2874830 :   return this->resolved_->type();
    9010                 :             : }
    9011                 :             : 
    9012                 :             : bool
    9013                 :      141725 : Selector_expression::do_is_addressable() const
    9014                 :             : {
    9015                 :      141725 :   if (this->is_error_expression())
    9016                 :             :     return true;
    9017                 :      141724 :   go_assert(this->resolved_ != NULL);
    9018                 :      141724 :   return this->resolved_->is_addressable();
    9019                 :             : }
    9020                 :             : 
    9021                 :             : void
    9022                 :       16436 : Selector_expression::do_issue_nil_check()
    9023                 :             : {
    9024                 :       16436 :   if (this->is_error_expression())
    9025                 :             :     return;
    9026                 :       16436 :   go_assert(this->resolved_ != NULL);
    9027                 :       16436 :   this->resolved_->issue_nil_check();
    9028                 :             : }
    9029                 :             : 
    9030                 :             : // Lower a selector expression to the resolved value.
    9031                 :             : 
    9032                 :             : Expression*
    9033                 :      771980 : Selector_expression::do_lower(Gogo*, Named_object*, Statement_inserter*)
    9034                 :             : {
    9035                 :      771980 :   if (this->is_error_expression() || this->resolved_ == NULL)
    9036                 :           5 :     return Expression::make_error(this->location());
    9037                 :             :   return this->resolved_;
    9038                 :             : }
    9039                 :             : 
    9040                 :             : // Lower a method expression T.M or (*T).M.  We turn this into a
    9041                 :             : // function literal.
    9042                 :             : 
    9043                 :             : Expression*
    9044                 :         602 : Selector_expression::lower_method_expression(Gogo* gogo)
    9045                 :             : {
    9046                 :         602 :   Location location = this->location();
    9047                 :         602 :   Type* left_type = this->left_->type();
    9048                 :         602 :   Type* type = left_type;
    9049                 :         602 :   const std::string& name(this->name_);
    9050                 :             : 
    9051                 :         602 :   bool is_pointer;
    9052                 :         602 :   if (type->points_to() == NULL)
    9053                 :             :     is_pointer = false;
    9054                 :             :   else
    9055                 :             :     {
    9056                 :         407 :       is_pointer = true;
    9057                 :         407 :       type = type->points_to();
    9058                 :             :     }
    9059                 :             : 
    9060                 :         602 :   Named_type* nt = type->named_type();
    9061                 :         602 :   Struct_type* st = type->struct_type();
    9062                 :         602 :   bool is_ambiguous = false;
    9063                 :         602 :   Method* method = NULL;
    9064                 :         602 :   if (nt != NULL)
    9065                 :         599 :     method = nt->method_function(name, &is_ambiguous);
    9066                 :           3 :   else if (st != NULL)
    9067                 :           1 :     method = st->method_function(name, &is_ambiguous);
    9068                 :         602 :   const Typed_identifier* imethod = NULL;
    9069                 :         602 :   if (method == NULL && !is_pointer)
    9070                 :             :     {
    9071                 :          28 :       Interface_type* it = type->interface_type();
    9072                 :          28 :       if (it != NULL)
    9073                 :          28 :         imethod = it->find_method(name);
    9074                 :             :     }
    9075                 :             : 
    9076                 :         602 :   if ((method == NULL && imethod == NULL)
    9077                 :         602 :       || (left_type->named_type() != NULL && left_type->points_to() != NULL))
    9078                 :             :     {
    9079                 :           3 :       if (nt != NULL)
    9080                 :             :         {
    9081                 :           2 :           if (!is_ambiguous)
    9082                 :           2 :             go_error_at(location, "type %<%s%s%> has no method %<%s%>",
    9083                 :             :                         is_pointer ? "*" : "",
    9084                 :           4 :                         nt->message_name().c_str(),
    9085                 :           4 :                         Gogo::message_name(name).c_str());
    9086                 :             :           else
    9087                 :           0 :             go_error_at(location, "method %<%s%s%> is ambiguous in type %<%s%>",
    9088                 :           0 :                         Gogo::message_name(name).c_str(),
    9089                 :             :                         is_pointer ? "*" : "",
    9090                 :           0 :                         nt->message_name().c_str());
    9091                 :             :         }
    9092                 :             :       else
    9093                 :             :         {
    9094                 :           1 :           if (!is_ambiguous)
    9095                 :           1 :             go_error_at(location, "type has no method %<%s%>",
    9096                 :           2 :                         Gogo::message_name(name).c_str());
    9097                 :             :           else
    9098                 :           0 :             go_error_at(location, "method %<%s%> is ambiguous",
    9099                 :           0 :                         Gogo::message_name(name).c_str());
    9100                 :             :         }
    9101                 :           3 :       return Expression::make_error(location);
    9102                 :             :     }
    9103                 :             : 
    9104                 :         599 :   if (method != NULL && !is_pointer && !method->is_value_method())
    9105                 :             :     {
    9106                 :           1 :       go_error_at(location, "method requires pointer (use %<(*%s).%s%>)",
    9107                 :           2 :                   nt->message_name().c_str(),
    9108                 :           1 :                   Gogo::message_name(name).c_str());
    9109                 :           1 :       return Expression::make_error(location);
    9110                 :             :     }
    9111                 :             : 
    9112                 :             :   // Build a new function type in which the receiver becomes the first
    9113                 :             :   // argument.
    9114                 :         598 :   Function_type* method_type;
    9115                 :         598 :   if (method != NULL)
    9116                 :             :     {
    9117                 :         570 :       method_type = method->type();
    9118                 :         570 :       go_assert(method_type->is_method());
    9119                 :             :     }
    9120                 :             :   else
    9121                 :             :     {
    9122                 :          28 :       method_type = imethod->type()->function_type();
    9123                 :          28 :       go_assert(method_type != NULL && !method_type->is_method());
    9124                 :             :     }
    9125                 :             : 
    9126                 :         598 :   const char* const receiver_name = "$this";
    9127                 :         598 :   Typed_identifier_list* parameters = new Typed_identifier_list();
    9128                 :         598 :   parameters->push_back(Typed_identifier(receiver_name, this->left_->type(),
    9129                 :             :                                          location));
    9130                 :             : 
    9131                 :         598 :   const Typed_identifier_list* method_parameters = method_type->parameters();
    9132                 :         598 :   if (method_parameters != NULL)
    9133                 :             :     {
    9134                 :         272 :       int i = 0;
    9135                 :         272 :       for (Typed_identifier_list::const_iterator p = method_parameters->begin();
    9136                 :         730 :            p != method_parameters->end();
    9137                 :         458 :            ++p, ++i)
    9138                 :             :         {
    9139                 :         458 :           if (!p->name().empty() && !Gogo::is_sink_name(p->name()))
    9140                 :         439 :             parameters->push_back(*p);
    9141                 :             :           else
    9142                 :             :             {
    9143                 :          19 :               char buf[20];
    9144                 :          19 :               snprintf(buf, sizeof buf, "$param%d", i);
    9145                 :          19 :               parameters->push_back(Typed_identifier(buf, p->type(),
    9146                 :             :                                                      p->location()));
    9147                 :             :             }
    9148                 :             :         }
    9149                 :             :     }
    9150                 :             : 
    9151                 :         598 :   const Typed_identifier_list* method_results = method_type->results();
    9152                 :         598 :   Typed_identifier_list* results;
    9153                 :         598 :   if (method_results == NULL)
    9154                 :             :     results = NULL;
    9155                 :             :   else
    9156                 :             :     {
    9157                 :         477 :       results = new Typed_identifier_list();
    9158                 :         477 :       for (Typed_identifier_list::const_iterator p = method_results->begin();
    9159                 :        1004 :            p != method_results->end();
    9160                 :         527 :            ++p)
    9161                 :         527 :         results->push_back(*p);
    9162                 :             :     }
    9163                 :             : 
    9164                 :         598 :   Function_type* fntype = Type::make_function_type(NULL, parameters, results,
    9165                 :             :                                                    location);
    9166                 :         598 :   if (method_type->is_varargs())
    9167                 :           3 :     fntype->set_is_varargs();
    9168                 :             : 
    9169                 :             :   // We generate methods which always takes a pointer to the receiver
    9170                 :             :   // as their first argument.  If this is for a pointer type, we can
    9171                 :             :   // simply reuse the existing function.  We use an internal hack to
    9172                 :             :   // get the right type.
    9173                 :             :   // FIXME: This optimization is disabled because it doesn't yet work
    9174                 :             :   // with function descriptors when the method expression is not
    9175                 :             :   // directly called.
    9176                 :         598 :   if (method != NULL && is_pointer && false)
    9177                 :             :     {
    9178                 :             :       Named_object* mno = (method->needs_stub_method()
    9179                 :             :                            ? method->stub_object()
    9180                 :             :                            : method->named_object());
    9181                 :             :       Expression* f = Expression::make_func_reference(mno, NULL, location);
    9182                 :             :       f = Expression::make_cast(fntype, f, location);
    9183                 :             :       Type_conversion_expression* tce =
    9184                 :             :         static_cast<Type_conversion_expression*>(f);
    9185                 :             :       tce->set_may_convert_function_types();
    9186                 :             :       return f;
    9187                 :             :     }
    9188                 :             : 
    9189                 :         598 :   Named_object* no = gogo->start_function(gogo->thunk_name(), fntype, false,
    9190                 :             :                                           location);
    9191                 :             : 
    9192                 :         598 :   Named_object* vno = gogo->lookup(receiver_name, NULL);
    9193                 :         598 :   go_assert(vno != NULL);
    9194                 :         598 :   Expression* ve = Expression::make_var_reference(vno, location);
    9195                 :         598 :   Expression* bm;
    9196                 :         598 :   if (method != NULL)
    9197                 :         570 :     bm = Type::bind_field_or_method(gogo, type, ve, name, location);
    9198                 :             :   else
    9199                 :          28 :     bm = Expression::make_interface_field_reference(ve, name, location);
    9200                 :             : 
    9201                 :             :   // Even though we found the method above, if it has an error type we
    9202                 :             :   // may see an error here.
    9203                 :         598 :   if (bm->is_error_expression())
    9204                 :             :     {
    9205                 :           0 :       gogo->finish_function(location);
    9206                 :           0 :       return bm;
    9207                 :             :     }
    9208                 :             : 
    9209                 :         598 :   Expression_list* args;
    9210                 :         598 :   if (parameters->size() <= 1)
    9211                 :             :     args = NULL;
    9212                 :             :   else
    9213                 :             :     {
    9214                 :         272 :       args = new Expression_list();
    9215                 :         272 :       Typed_identifier_list::const_iterator p = parameters->begin();
    9216                 :         272 :       ++p;
    9217                 :         730 :       for (; p != parameters->end(); ++p)
    9218                 :             :         {
    9219                 :         458 :           vno = gogo->lookup(p->name(), NULL);
    9220                 :         458 :           go_assert(vno != NULL);
    9221                 :         458 :           args->push_back(Expression::make_var_reference(vno, location));
    9222                 :             :         }
    9223                 :             :     }
    9224                 :             : 
    9225                 :         598 :   gogo->start_block(location);
    9226                 :             : 
    9227                 :         598 :   Call_expression* call = Expression::make_call(bm, args,
    9228                 :         598 :                                                 method_type->is_varargs(),
    9229                 :             :                                                 location);
    9230                 :             : 
    9231                 :         598 :   Statement* s = Statement::make_return_from_call(no, call, location);
    9232                 :         598 :   s->determine_types(gogo);
    9233                 :         598 :   gogo->add_statement(s);
    9234                 :             : 
    9235                 :         598 :   Block* b = gogo->finish_block(location);
    9236                 :             : 
    9237                 :         598 :   gogo->add_block(b, location);
    9238                 :             : 
    9239                 :         598 :   gogo->finish_function(location);
    9240                 :             : 
    9241                 :         598 :   return Expression::make_func_reference(no, NULL, location);
    9242                 :             : }
    9243                 :             : 
    9244                 :             : // Dump the ast for a selector expression.
    9245                 :             : 
    9246                 :             : void
    9247                 :           0 : Selector_expression::do_dump_expression(Ast_dump_context* ast_dump_context)
    9248                 :             :     const
    9249                 :             : {
    9250                 :           0 :   ast_dump_context->dump_expression(this->left_);
    9251                 :           0 :   ast_dump_context->ostream() << ".";
    9252                 :           0 :   ast_dump_context->ostream() << this->name_;
    9253                 :           0 : }
    9254                 :             : 
    9255                 :             : // Make a selector expression.
    9256                 :             : 
    9257                 :             : Expression*
    9258                 :      700127 : Expression::make_selector(Expression* left, const std::string& name,
    9259                 :             :                           Location location)
    9260                 :             : {
    9261                 :      700127 :   return new Selector_expression(left, name, location);
    9262                 :             : }
    9263                 :             : 
    9264                 :             : // Class Builtin_call_expression.  This is used for a call to a
    9265                 :             : // builtin function.
    9266                 :             : 
    9267                 :      240767 : Builtin_call_expression::Builtin_call_expression(Gogo* gogo,
    9268                 :             :                                                  Expression* fn,
    9269                 :             :                                                  Expression_list* args,
    9270                 :             :                                                  bool is_varargs,
    9271                 :      240767 :                                                  Location location)
    9272                 :             :   : Call_expression(fn, args, is_varargs, location),
    9273                 :      240767 :     gogo_(gogo), code_(BUILTIN_INVALID), seen_(false),
    9274                 :      240767 :     recover_arg_is_set_(false)
    9275                 :             : {
    9276                 :      240767 :   const Named_object* no;
    9277                 :      240767 :   if (fn->is_error_expression())
    9278                 :             :     {
    9279                 :             :       this->code_ = BUILTIN_INVALID;
    9280                 :             :       return;
    9281                 :             :     }
    9282                 :      240767 :   else if (fn->func_expression() != NULL)
    9283                 :      240767 :     no = fn->func_expression()->named_object();
    9284                 :           0 :   else if (fn->unknown_expression() != NULL)
    9285                 :           0 :     no = fn->unknown_expression()->named_object();
    9286                 :             :   else
    9287                 :           0 :     go_unreachable();
    9288                 :             : 
    9289                 :      240767 :   const std::string& name(no->name());
    9290                 :      240767 :   if (name == "append")
    9291                 :       15751 :     this->code_ = BUILTIN_APPEND;
    9292                 :      225016 :   else if (name == "cap")
    9293                 :       16623 :     this->code_ = BUILTIN_CAP;
    9294                 :      208393 :   else if (name == "close")
    9295                 :        1094 :     this->code_ = BUILTIN_CLOSE;
    9296                 :      207299 :   else if (name == "complex")
    9297                 :       13722 :     this->code_ = BUILTIN_COMPLEX;
    9298                 :      193577 :   else if (name == "copy")
    9299                 :        4493 :     this->code_ = BUILTIN_COPY;
    9300                 :      189084 :   else if (name == "delete")
    9301                 :         943 :     this->code_ = BUILTIN_DELETE;
    9302                 :      188141 :   else if (name == "imag")
    9303                 :         555 :     this->code_ = BUILTIN_IMAG;
    9304                 :      187586 :   else if (name == "len")
    9305                 :      116377 :     this->code_ = BUILTIN_LEN;
    9306                 :       71209 :   else if (name == "make")
    9307                 :       15137 :     this->code_ = BUILTIN_MAKE;
    9308                 :       56072 :   else if (name == "new")
    9309                 :       21308 :     this->code_ = BUILTIN_NEW;
    9310                 :       34764 :   else if (name == "panic")
    9311                 :       15153 :     this->code_ = BUILTIN_PANIC;
    9312                 :       19611 :   else if (name == "print")
    9313                 :        2727 :     this->code_ = BUILTIN_PRINT;
    9314                 :       16884 :   else if (name == "println")
    9315                 :       13459 :     this->code_ = BUILTIN_PRINTLN;
    9316                 :        3425 :   else if (name == "real")
    9317                 :         493 :     this->code_ = BUILTIN_REAL;
    9318                 :        2932 :   else if (name == "recover")
    9319                 :         857 :     this->code_ = BUILTIN_RECOVER;
    9320                 :        2075 :   else if (name == "Add")
    9321                 :           9 :     this->code_ = BUILTIN_ADD;
    9322                 :        2066 :   else if (name == "Alignof")
    9323                 :          17 :     this->code_ = BUILTIN_ALIGNOF;
    9324                 :        2049 :   else if (name == "Offsetof")
    9325                 :        1161 :     this->code_ = BUILTIN_OFFSETOF;
    9326                 :         888 :   else if (name == "Sizeof")
    9327                 :         879 :     this->code_ = BUILTIN_SIZEOF;
    9328                 :           9 :   else if (name == "Slice")
    9329                 :           9 :     this->code_ = BUILTIN_SLICE;
    9330                 :             :   else
    9331                 :           0 :     go_unreachable();
    9332                 :             : }
    9333                 :             : 
    9334                 :             : // Return whether this is a call to recover.  This is a virtual
    9335                 :             : // function called from the parent class.
    9336                 :             : 
    9337                 :             : bool
    9338                 :        1580 : Builtin_call_expression::do_is_recover_call() const
    9339                 :             : {
    9340                 :        1580 :   if (this->classification() == EXPRESSION_ERROR)
    9341                 :             :     return false;
    9342                 :        1580 :   return this->code_ == BUILTIN_RECOVER;
    9343                 :             : }
    9344                 :             : 
    9345                 :             : // Set the argument for a call to recover.
    9346                 :             : 
    9347                 :             : void
    9348                 :         857 : Builtin_call_expression::do_set_recover_arg(Expression* arg)
    9349                 :             : {
    9350                 :         857 :   const Expression_list* args = this->args();
    9351                 :         857 :   go_assert(args == NULL || args->empty());
    9352                 :         857 :   Expression_list* new_args = new Expression_list();
    9353                 :         857 :   new_args->push_back(arg);
    9354                 :         857 :   this->set_args(new_args);
    9355                 :         857 :   this->recover_arg_is_set_ = true;
    9356                 :         857 : }
    9357                 :             : 
    9358                 :             : // Lower a builtin call expression.  This turns new and make into
    9359                 :             : // specific expressions.  We also convert to a constant if we can.
    9360                 :             : 
    9361                 :             : Expression*
    9362                 :      449704 : Builtin_call_expression::do_lower(Gogo* gogo, Named_object* function,
    9363                 :             :                                   Statement_inserter* inserter)
    9364                 :             : {
    9365                 :      449704 :   if (this->is_error_expression())
    9366                 :         995 :     return this;
    9367                 :             : 
    9368                 :      448709 :   Location loc = this->location();
    9369                 :             : 
    9370                 :      448709 :   if (this->code_ == BUILTIN_OFFSETOF)
    9371                 :             :     {
    9372                 :        1154 :       Expression* arg = this->one_arg();
    9373                 :        1154 :       Field_reference_expression* farg = arg->field_reference_expression();
    9374                 :        2097 :       while (farg != NULL)
    9375                 :             :         {
    9376                 :        2097 :           if (!farg->implicit())
    9377                 :             :             break;
    9378                 :             :           // When the selector refers to an embedded field,
    9379                 :             :           // it must not be reached through pointer indirections.
    9380                 :        1841 :           if (farg->expr()->deref() != farg->expr())
    9381                 :             :             {
    9382                 :         898 :               this->report_error(_("argument of Offsetof implies "
    9383                 :             :                                    "indirection of an embedded field"));
    9384                 :         898 :               return this;
    9385                 :             :             }
    9386                 :             :           // Go up until we reach the original base.
    9387                 :        3040 :           farg = farg->expr()->field_reference_expression();
    9388                 :             :         }
    9389                 :             :     }
    9390                 :             : 
    9391                 :      447811 :   if (this->is_constant())
    9392                 :             :     {
    9393                 :       15723 :       Numeric_constant nc;
    9394                 :       15723 :       if (this->numeric_constant_value(&nc))
    9395                 :             :         {
    9396                 :       15723 :           Expression* ret = nc.expression(loc);
    9397                 :       15723 :           Type_context subcontext;
    9398                 :       15723 :           if (this->type() != NULL)
    9399                 :        1526 :             subcontext = Type_context(this->type(),
    9400                 :        1526 :                                       this->type()->is_abstract());
    9401                 :       15723 :           ret->determine_type(gogo, &subcontext);
    9402                 :       15723 :           return ret;
    9403                 :             :         }
    9404                 :       15723 :     }
    9405                 :             : 
    9406                 :      432088 :   switch (this->code_)
    9407                 :             :     {
    9408                 :             :     default:
    9409                 :             :       break;
    9410                 :             : 
    9411                 :       23188 :     case BUILTIN_NEW:
    9412                 :       23188 :       return Expression::make_allocation(this->one_arg()->type(), loc);
    9413                 :             : 
    9414                 :       15099 :     case BUILTIN_MAKE:
    9415                 :       15099 :       return this->lower_make(gogo, inserter);
    9416                 :             : 
    9417                 :         874 :     case BUILTIN_RECOVER:
    9418                 :         874 :       if (function != NULL)
    9419                 :         874 :         function->func_value()->set_calls_recover();
    9420                 :             :       else
    9421                 :             :         {
    9422                 :             :           // Calling recover outside of a function always returns the
    9423                 :             :           // nil empty interface.
    9424                 :           0 :           Type* eface = Type::make_empty_interface_type(loc);
    9425                 :           0 :           return Expression::make_cast(eface, Expression::make_nil(loc), loc);
    9426                 :             :         }
    9427                 :         874 :       break;
    9428                 :             : 
    9429                 :        1559 :     case BUILTIN_DELETE:
    9430                 :        1559 :       {
    9431                 :        1559 :         const Expression_list* args = this->args();
    9432                 :        1559 :         Type* key_type =
    9433                 :        3118 :           args->front()->type()->map_type()->key_type();
    9434                 :        1559 :         Expression_list::iterator pa = this->args()->begin();
    9435                 :        1559 :         pa++;
    9436                 :        1559 :         Type* arg_type = (*pa)->type();
    9437                 :        1559 :         if (!Type::are_identical(key_type, arg_type, 0, NULL))
    9438                 :          16 :           *pa = Expression::make_cast(key_type, *pa, loc);
    9439                 :             :       }
    9440                 :             :       break;
    9441                 :             : 
    9442                 :       17885 :     case BUILTIN_PRINT:
    9443                 :       17885 :     case BUILTIN_PRINTLN:
    9444                 :             :       // Force all the arguments into temporary variables, so that we
    9445                 :             :       // don't try to evaluate something while holding the print lock.
    9446                 :       17885 :       if (this->args() == NULL)
    9447                 :             :         break;
    9448                 :       61881 :       for (Expression_list::iterator pa = this->args()->begin();
    9449                 :       61881 :            pa != this->args()->end();
    9450                 :       44065 :            ++pa)
    9451                 :             :         {
    9452                 :       44065 :           if (!(*pa)->is_multi_eval_safe())
    9453                 :             :             {
    9454                 :       27576 :               Temporary_statement* temp =
    9455                 :       27576 :                 Statement::make_temporary(NULL, *pa, loc);
    9456                 :       27576 :               inserter->insert(temp);
    9457                 :       27576 :               *pa = Expression::make_temporary_reference(temp, loc);
    9458                 :             :             }
    9459                 :             :         }
    9460                 :             :       break;
    9461                 :             :     }
    9462                 :             : 
    9463                 :             :   return this;
    9464                 :             : }
    9465                 :             : 
    9466                 :             : // Flatten a builtin call expression.  This turns the arguments of some
    9467                 :             : // builtin calls into temporary expressions.  Also expand copy and append
    9468                 :             : // to runtime calls.
    9469                 :             : 
    9470                 :             : Expression*
    9471                 :      210171 : Builtin_call_expression::do_flatten(Gogo* gogo, Named_object* function,
    9472                 :             :                                     Statement_inserter* inserter)
    9473                 :             : {
    9474                 :      210171 :   if (this->is_error_expression())
    9475                 :             :     {
    9476                 :         978 :       go_assert(saw_errors());
    9477                 :         978 :       return this;
    9478                 :             :     }
    9479                 :             : 
    9480                 :      209193 :   Location loc = this->location();
    9481                 :             : 
    9482                 :      209193 :   switch (this->code_)
    9483                 :             :     {
    9484                 :             :     default:
    9485                 :             :       break;
    9486                 :             : 
    9487                 :        2654 :     case BUILTIN_APPEND:
    9488                 :        2654 :       return this->flatten_append(gogo, function, inserter, NULL, NULL);
    9489                 :             : 
    9490                 :        4478 :     case BUILTIN_COPY:
    9491                 :        4478 :       {
    9492                 :        4478 :         Type* at = this->args()->front()->type();
    9493                 :       13434 :         for (Expression_list::iterator pa = this->args()->begin();
    9494                 :       13434 :              pa != this->args()->end();
    9495                 :        8956 :              ++pa)
    9496                 :             :           {
    9497                 :        8956 :             if ((*pa)->is_error_expression())
    9498                 :             :               {
    9499                 :           0 :                 go_assert(saw_errors());
    9500                 :           0 :                 return Expression::make_error(loc);
    9501                 :             :               }
    9502                 :        8956 :             if ((*pa)->is_nil_expression())
    9503                 :             :               {
    9504                 :           0 :                 Expression* nil = Expression::make_nil(loc);
    9505                 :           0 :                 Expression* zero = Expression::make_integer_ul(0, NULL, loc);
    9506                 :           0 :                 *pa = Expression::make_slice_value(at, nil, zero, zero, loc);
    9507                 :             :               }
    9508                 :        8956 :             if (!(*pa)->is_multi_eval_safe())
    9509                 :             :               {
    9510                 :        5001 :                 Temporary_statement* temp =
    9511                 :        5001 :                   Statement::make_temporary(NULL, *pa, loc);
    9512                 :        5001 :                 inserter->insert(temp);
    9513                 :        5001 :                 *pa = Expression::make_temporary_reference(temp, loc);
    9514                 :             :               }
    9515                 :             :           }
    9516                 :             : 
    9517                 :             :         // Lower to runtime call.
    9518                 :        4478 :         const Expression_list* args = this->args();
    9519                 :        4478 :         go_assert(args != NULL && args->size() == 2);
    9520                 :        4478 :         Expression* arg1 = args->front();
    9521                 :        4478 :         Expression* arg2 = args->back();
    9522                 :        4478 :         go_assert(arg1->is_multi_eval_safe());
    9523                 :        4478 :         go_assert(arg2->is_multi_eval_safe());
    9524                 :        4478 :         bool arg2_is_string = arg2->type()->is_string_type();
    9525                 :             : 
    9526                 :        4478 :         Expression* ret;
    9527                 :        8956 :         Type* et = at->array_type()->element_type();
    9528                 :        4478 :         if (et->has_pointer())
    9529                 :             :           {
    9530                 :        1649 :             Expression* td = Expression::make_type_descriptor(et, loc);
    9531                 :        1649 :             Expression* pd =
    9532                 :        1649 :               Expression::make_slice_info(arg1, SLICE_INFO_VALUE_POINTER, loc);
    9533                 :        1649 :             Expression* ld =
    9534                 :        1649 :               Expression::make_slice_info(arg1, SLICE_INFO_LENGTH, loc);
    9535                 :        1649 :             Expression* ps =
    9536                 :        1649 :               Expression::make_slice_info(arg2, SLICE_INFO_VALUE_POINTER, loc);
    9537                 :        1649 :             Expression* ls =
    9538                 :        1649 :               Expression::make_slice_info(arg2, SLICE_INFO_LENGTH, loc);
    9539                 :        1649 :             ret = Runtime::make_call(gogo, Runtime::TYPEDSLICECOPY, loc,
    9540                 :             :                                      5, td, pd, ld, ps, ls);
    9541                 :             :           }
    9542                 :             :         else
    9543                 :             :           {
    9544                 :        2829 :             Type* int_type = Type::lookup_integer_type("int");
    9545                 :        2829 :             Type* uintptr_type = Type::lookup_integer_type("uintptr");
    9546                 :             : 
    9547                 :             :             // l1 = len(arg1)
    9548                 :        2829 :             Named_object* lenfn = gogo->lookup_global("len");
    9549                 :        2829 :             Expression* lenref = Expression::make_func_reference(lenfn, NULL, loc);
    9550                 :        2829 :             Expression_list* len_args = new Expression_list();
    9551                 :        2829 :             len_args->push_back(arg1->copy());
    9552                 :        2829 :             Expression* len1 = Expression::make_call(lenref, len_args, false, loc);
    9553                 :        2829 :             gogo->lower_expression(function, inserter, &len1);
    9554                 :        2829 :             gogo->flatten_expression(function, inserter, &len1);
    9555                 :        2829 :             Temporary_statement* l1tmp = Statement::make_temporary(int_type, len1, loc);
    9556                 :        2829 :             l1tmp->determine_types(gogo);
    9557                 :        2829 :             inserter->insert(l1tmp);
    9558                 :             : 
    9559                 :             :             // l2 = len(arg2)
    9560                 :        2829 :             len_args = new Expression_list();
    9561                 :        2829 :             len_args->push_back(arg2->copy());
    9562                 :        2829 :             Expression* len2 = Expression::make_call(lenref, len_args, false, loc);
    9563                 :        2829 :             gogo->lower_expression(function, inserter, &len2);
    9564                 :        2829 :             gogo->flatten_expression(function, inserter, &len2);
    9565                 :        2829 :             Temporary_statement* l2tmp = Statement::make_temporary(int_type, len2, loc);
    9566                 :        2829 :             l2tmp->determine_types(gogo);
    9567                 :        2829 :             inserter->insert(l2tmp);
    9568                 :             : 
    9569                 :             :             // n = (l1 < l2 ? l1 : l2)
    9570                 :        2829 :             Expression* l1ref = Expression::make_temporary_reference(l1tmp, loc);
    9571                 :        2829 :             Expression* l2ref = Expression::make_temporary_reference(l2tmp, loc);
    9572                 :        2829 :             Expression* cond = Expression::make_binary(OPERATOR_LT, l1ref, l2ref, loc);
    9573                 :        2829 :             Expression* n = Expression::make_conditional(cond,
    9574                 :             :                                                          l1ref->copy(),
    9575                 :             :                                                          l2ref->copy(),
    9576                 :             :                                                          loc);
    9577                 :        2829 :             Temporary_statement* ntmp = Statement::make_temporary(NULL, n, loc);
    9578                 :        2829 :             ntmp->determine_types(gogo);
    9579                 :        2829 :             inserter->insert(ntmp);
    9580                 :             : 
    9581                 :             :             // sz = n * sizeof(elem_type)
    9582                 :        2829 :             Expression* nref = Expression::make_temporary_reference(ntmp, loc);
    9583                 :        2829 :             nref = Expression::make_cast(uintptr_type, nref, loc);
    9584                 :        2829 :             Expression* sz = Expression::make_type_info(et, TYPE_INFO_SIZE);
    9585                 :        2829 :             sz = Expression::make_binary(OPERATOR_MULT, sz, nref, loc);
    9586                 :             : 
    9587                 :             :             // memmove(arg1.ptr, arg2.ptr, sz)
    9588                 :        2829 :             Expression* p1 = Expression::make_slice_info(arg1,
    9589                 :             :                                                          SLICE_INFO_VALUE_POINTER,
    9590                 :             :                                                          loc);
    9591                 :        2829 :             Expression* p2 = (arg2_is_string
    9592                 :        2829 :                               ? Expression::make_string_info(arg2,
    9593                 :             :                                                              STRING_INFO_DATA,
    9594                 :             :                                                              loc)
    9595                 :        2452 :                               : Expression::make_slice_info(arg2,
    9596                 :             :                                                             SLICE_INFO_VALUE_POINTER,
    9597                 :        2829 :                                                             loc));
    9598                 :        2829 :             Expression* call = Runtime::make_call(gogo,
    9599                 :             :                                                   Runtime::BUILTIN_MEMMOVE,
    9600                 :             :                                                   loc, 3,
    9601                 :             :                                                   p1, p2, sz);
    9602                 :             : 
    9603                 :             :             // n is the return value of copy
    9604                 :        2829 :             nref = Expression::make_temporary_reference(ntmp, loc);
    9605                 :        2829 :             ret = Expression::make_compound(call, nref, loc);
    9606                 :             :           }
    9607                 :        4478 :         ret->determine_type_no_context(gogo);
    9608                 :        4478 :         return ret;
    9609                 :             :       }
    9610                 :       15370 :       break;
    9611                 :             : 
    9612                 :       15370 :     case BUILTIN_PANIC:
    9613                 :       15370 :       for (Expression_list::iterator pa = this->args()->begin();
    9614                 :       30740 :            pa != this->args()->end();
    9615                 :       15370 :            ++pa)
    9616                 :             :         {
    9617                 :       15370 :           if (!(*pa)->is_multi_eval_safe()
    9618                 :       30138 :               && (*pa)->type()->interface_type() != NULL)
    9619                 :             :             {
    9620                 :       14768 :               Temporary_statement* temp =
    9621                 :       14768 :                 Statement::make_temporary(NULL, *pa, loc);
    9622                 :       14768 :               inserter->insert(temp);
    9623                 :       14768 :               *pa = Expression::make_temporary_reference(temp, loc);
    9624                 :             :             }
    9625                 :             :         }
    9626                 :             :       break;
    9627                 :             : 
    9628                 :      154386 :     case BUILTIN_LEN:
    9629                 :      154386 :     case BUILTIN_CAP:
    9630                 :      154386 :       {
    9631                 :      154386 :         Expression_list::iterator pa = this->args()->begin();
    9632                 :      154386 :         if (!(*pa)->is_multi_eval_safe()
    9633                 :      154386 :             && ((*pa)->type()->map_type() != NULL
    9634                 :       15197 :                 || (*pa)->type()->channel_type() != NULL))
    9635                 :             :           {
    9636                 :         692 :             Temporary_statement* temp =
    9637                 :         692 :               Statement::make_temporary(NULL, *pa, loc);
    9638                 :         692 :             inserter->insert(temp);
    9639                 :         692 :             *pa = Expression::make_temporary_reference(temp, loc);
    9640                 :             :           }
    9641                 :             :       }
    9642                 :             :       break;
    9643                 :             : 
    9644                 :         867 :     case BUILTIN_DELETE:
    9645                 :         867 :       {
    9646                 :             :         // Lower to a runtime function call.
    9647                 :         867 :         const Expression_list* args = this->args();
    9648                 :             : 
    9649                 :             :         // Since this function returns no value it must appear in
    9650                 :             :         // a statement by itself, so we don't have to worry about
    9651                 :             :         // order of evaluation of values around it.  Evaluate the
    9652                 :             :         // map first to get order of evaluation right.
    9653                 :         867 :         Map_type* mt = args->front()->type()->map_type();
    9654                 :         867 :         Temporary_statement* map_temp =
    9655                 :         867 :           Statement::make_temporary(mt, args->front(), loc);
    9656                 :         867 :         inserter->insert(map_temp);
    9657                 :             : 
    9658                 :         867 :         Temporary_statement* key_temp =
    9659                 :         867 :           Statement::make_temporary(mt->key_type(), args->back(), loc);
    9660                 :         867 :         inserter->insert(key_temp);
    9661                 :             : 
    9662                 :         867 :         Expression* e1 = Expression::make_type_descriptor(mt, loc);
    9663                 :         867 :         Expression* e2 = Expression::make_temporary_reference(map_temp,
    9664                 :             :                                                               loc);
    9665                 :         867 :         Expression* e3 = Expression::make_temporary_reference(key_temp,
    9666                 :             :                                                               loc);
    9667                 :             : 
    9668                 :         867 :         Runtime::Function code;
    9669                 :         867 :         switch (mt->algorithm(gogo))
    9670                 :             :           {
    9671                 :         139 :             case Map_type::MAP_ALG_FAST32:
    9672                 :         139 :             case Map_type::MAP_ALG_FAST32PTR:
    9673                 :         139 :               {
    9674                 :         139 :                 code = Runtime::MAPDELETE_FAST32;
    9675                 :         139 :                 Type* uint32_type = Type::lookup_integer_type("uint32");
    9676                 :         139 :                 Type* uint32_ptr_type = Type::make_pointer_type(uint32_type);
    9677                 :         139 :                 e3 = Expression::make_unary(OPERATOR_AND, e3, loc);
    9678                 :         139 :                 e3 = Expression::make_unsafe_cast(uint32_ptr_type, e3,
    9679                 :             :                                                   loc);
    9680                 :         139 :                 e3 = Expression::make_dereference(e3,
    9681                 :             :                                                   Expression::NIL_CHECK_NOT_NEEDED,
    9682                 :             :                                                   loc);
    9683                 :         139 :                 break;
    9684                 :             :               }
    9685                 :         211 :             case Map_type::MAP_ALG_FAST64:
    9686                 :         211 :             case Map_type::MAP_ALG_FAST64PTR:
    9687                 :         211 :               {
    9688                 :         211 :                 code = Runtime::MAPDELETE_FAST64;
    9689                 :         211 :                 Type* uint64_type = Type::lookup_integer_type("uint64");
    9690                 :         211 :                 Type* uint64_ptr_type = Type::make_pointer_type(uint64_type);
    9691                 :         211 :                 e3 = Expression::make_unary(OPERATOR_AND, e3, loc);
    9692                 :         211 :                 e3 = Expression::make_unsafe_cast(uint64_ptr_type, e3,
    9693                 :             :                                                   loc);
    9694                 :         211 :                 e3 = Expression::make_dereference(e3,
    9695                 :             :                                                   Expression::NIL_CHECK_NOT_NEEDED,
    9696                 :             :                                                   loc);
    9697                 :         211 :                 break;
    9698                 :             :               }
    9699                 :             :             case Map_type::MAP_ALG_FASTSTR:
    9700                 :             :               code = Runtime::MAPDELETE_FASTSTR;
    9701                 :             :               break;
    9702                 :         177 :             default:
    9703                 :         177 :               code = Runtime::MAPDELETE;
    9704                 :             : 
    9705                 :             :               // If the call to delete is deferred, and is in a loop,
    9706                 :             :               // then the loop will only have a single instance of the
    9707                 :             :               // temporary variable.  Passing the address of the
    9708                 :             :               // temporary variable here means that the deferred call
    9709                 :             :               // will see the last value in the loop, not the current
    9710                 :             :               // value.  So for this unusual case copy the value into
    9711                 :             :               // the heap.
    9712                 :         177 :               if (!this->is_deferred())
    9713                 :         144 :                 e3 = Expression::make_unary(OPERATOR_AND, e3, loc);
    9714                 :             :               else
    9715                 :             :                 {
    9716                 :          33 :                   Expression* a = Expression::make_allocation(mt->key_type(),
    9717                 :             :                                                               loc);
    9718                 :          33 :                   Temporary_statement* atemp =
    9719                 :          33 :                     Statement::make_temporary(NULL, a, loc);
    9720                 :          33 :                   atemp->determine_types(gogo);
    9721                 :          33 :                   inserter->insert(atemp);
    9722                 :             : 
    9723                 :          33 :                   a = Expression::make_temporary_reference(atemp, loc);
    9724                 :          33 :                   a = Expression::make_dereference(a, NIL_CHECK_NOT_NEEDED, loc);
    9725                 :          33 :                   Statement* s = Statement::make_assignment(a, e3, loc);
    9726                 :          33 :                   s->determine_types(gogo);
    9727                 :          33 :                   inserter->insert(s);
    9728                 :             : 
    9729                 :          33 :                   e3 = Expression::make_temporary_reference(atemp, loc);
    9730                 :             :                 }
    9731                 :             :           }
    9732                 :             : 
    9733                 :         867 :         Expression* ret = Runtime::make_call(gogo, code, loc, 3, e1, e2, e3);
    9734                 :         867 :         ret->determine_type_no_context(gogo);
    9735                 :         867 :         return ret;
    9736                 :             :       }
    9737                 :             : 
    9738                 :           2 :     case BUILTIN_ADD:
    9739                 :           2 :       {
    9740                 :           2 :         Expression* ptr = this->args()->front();
    9741                 :           2 :         Type* uintptr_type = Type::lookup_integer_type("uintptr");
    9742                 :           2 :         ptr = Expression::make_cast(uintptr_type, ptr, loc);
    9743                 :           2 :         Expression* len = this->args()->back();
    9744                 :           2 :         len = Expression::make_cast(uintptr_type, len, loc);
    9745                 :           2 :         Expression* add = Expression::make_binary(OPERATOR_PLUS, ptr, len,
    9746                 :             :                                                   loc);
    9747                 :           2 :         Expression* ret = Expression::make_cast(this->args()->front()->type(),
    9748                 :             :                                                 add, loc);
    9749                 :           2 :         ret->determine_type_no_context(gogo);
    9750                 :           2 :         return ret;
    9751                 :             :       }
    9752                 :             : 
    9753                 :           9 :     case BUILTIN_SLICE:
    9754                 :           9 :       {
    9755                 :           9 :         Expression* ptr = this->args()->front();
    9756                 :           9 :         Temporary_statement* ptr_temp = NULL;
    9757                 :           9 :         if (!ptr->is_multi_eval_safe())
    9758                 :             :           {
    9759                 :           8 :             ptr_temp = Statement::make_temporary(NULL, ptr, loc);
    9760                 :           8 :             inserter->insert(ptr_temp);
    9761                 :           8 :             ptr = Expression::make_temporary_reference(ptr_temp, loc);
    9762                 :             :           }
    9763                 :             : 
    9764                 :           9 :         Expression* len = this->args()->back();
    9765                 :           9 :         Temporary_statement* len_temp = NULL;
    9766                 :           9 :         if (!len->is_multi_eval_safe())
    9767                 :             :           {
    9768                 :           2 :             len_temp = Statement::make_temporary(NULL, len, loc);
    9769                 :           2 :             inserter->insert(len_temp);
    9770                 :           2 :             len = Expression::make_temporary_reference(len_temp, loc);
    9771                 :             :           }
    9772                 :             : 
    9773                 :           9 :         bool fits_in_int;
    9774                 :           9 :         Numeric_constant nc;
    9775                 :           9 :         if (this->args()->back()->numeric_constant_value(&nc))
    9776                 :             :           {
    9777                 :             :             // We gave an error for constants that don't fit in int in
    9778                 :             :             // check_types.
    9779                 :             :             fits_in_int = true;
    9780                 :             :           }
    9781                 :             :         else
    9782                 :             :           {
    9783                 :           2 :             Integer_type* itype = this->args()->back()->type()->integer_type();
    9784                 :           0 :             go_assert(itype != NULL);
    9785                 :           2 :             int ebits = itype->bits();
    9786                 :           2 :             int intbits =
    9787                 :           4 :               Type::lookup_integer_type("int")->integer_type()->bits();
    9788                 :             : 
    9789                 :             :             // We can treat ebits == intbits as small even for an
    9790                 :             :             // unsigned integer type, because we will convert the
    9791                 :             :             // value to int and then reject it in the runtime if it is
    9792                 :             :             // negative.
    9793                 :             : 
    9794                 :           2 :             fits_in_int = ebits <= intbits;
    9795                 :             :           }
    9796                 :             : 
    9797                 :          11 :         Runtime::Function code = (fits_in_int
    9798                 :           2 :                                   ? Runtime::UNSAFESLICE
    9799                 :             :                                   : Runtime::UNSAFESLICE64);
    9800                 :           9 :         Expression* td =
    9801                 :           9 :           Expression::make_type_descriptor(ptr->type()->points_to(), loc);
    9802                 :           9 :         Expression* check = Runtime::make_call(gogo, code, loc, 3,
    9803                 :             :                                                td, ptr, len);
    9804                 :             : 
    9805                 :           9 :         if (ptr_temp == NULL)
    9806                 :           1 :           ptr = ptr->copy();
    9807                 :             :         else
    9808                 :           8 :           ptr = Expression::make_temporary_reference(ptr_temp, loc);
    9809                 :           9 :         Expression* nil = Expression::make_nil(loc);
    9810                 :           9 :         nil = Expression::make_cast(ptr->type(), nil, loc);
    9811                 :           9 :         Expression* is_nil = Expression::make_binary(OPERATOR_EQEQ, ptr, nil,
    9812                 :             :                                                      loc);
    9813                 :             : 
    9814                 :           9 :         if (len_temp == NULL)
    9815                 :           7 :           len = len->copy();
    9816                 :             :         else
    9817                 :           2 :           len = Expression::make_temporary_reference(len_temp, loc);
    9818                 :           9 :         Expression* zero = Expression::make_integer_ul(0, len->type(), loc);
    9819                 :           9 :         Expression* is_zero = Expression::make_binary(OPERATOR_EQEQ, len, zero,
    9820                 :             :                                                       loc);
    9821                 :             : 
    9822                 :           9 :         Expression* cond = Expression::make_binary(OPERATOR_ANDAND, is_nil,
    9823                 :             :                                                    is_zero, loc);
    9824                 :             : 
    9825                 :           9 :         Type* slice_type = Type::make_array_type(ptr->type()->points_to(),
    9826                 :             :                                                  NULL);
    9827                 :           9 :         nil = Expression::make_nil(loc);
    9828                 :           9 :         Expression* nil_slice = Expression::make_cast(slice_type, nil, loc);
    9829                 :             : 
    9830                 :           9 :         if (ptr_temp == NULL)
    9831                 :           1 :           ptr = ptr->copy();
    9832                 :             :         else
    9833                 :           8 :           ptr = Expression::make_temporary_reference(ptr_temp, loc);
    9834                 :             : 
    9835                 :           9 :         if (len_temp == NULL)
    9836                 :           7 :           len = len->copy();
    9837                 :             :         else
    9838                 :           2 :           len = Expression::make_temporary_reference(len_temp, loc);
    9839                 :             : 
    9840                 :           9 :         Expression* cap;
    9841                 :           9 :         if (len_temp == NULL)
    9842                 :           7 :           cap = len->copy();
    9843                 :             :         else
    9844                 :           2 :           cap = Expression::make_temporary_reference(len_temp, loc);
    9845                 :             : 
    9846                 :           9 :         Expression* slice = Expression::make_slice_value(slice_type, ptr,
    9847                 :             :                                                          len, cap, loc);
    9848                 :             : 
    9849                 :           9 :         slice = Expression::make_conditional(cond, nil_slice, slice, loc);
    9850                 :             : 
    9851                 :           9 :         Expression* ret = Expression::make_compound(check, slice, loc);
    9852                 :           9 :         ret->determine_type_no_context(gogo);
    9853                 :           9 :         return ret;
    9854                 :           9 :       }
    9855                 :             :     }
    9856                 :             : 
    9857                 :      201183 :   return this;
    9858                 :             : }
    9859                 :             : 
    9860                 :             : // Lower a make expression.
    9861                 :             : 
    9862                 :             : Expression*
    9863                 :       15099 : Builtin_call_expression::lower_make(Gogo* gogo, Statement_inserter* inserter)
    9864                 :             : {
    9865                 :       15099 :   Location loc = this->location();
    9866                 :             : 
    9867                 :       15099 :   const Expression_list* args = this->args();
    9868                 :             : 
    9869                 :       15099 :   Expression_list::const_iterator parg = args->begin();
    9870                 :             : 
    9871                 :       15099 :   Expression* first_arg = *parg;
    9872                 :       15099 :   go_assert(first_arg->is_type_expression());
    9873                 :       15099 :   Type* type = first_arg->type();
    9874                 :             : 
    9875                 :       15099 :   bool is_slice = false;
    9876                 :       15099 :   bool is_map = false;
    9877                 :       15099 :   bool is_chan = false;
    9878                 :       15099 :   if (type->is_slice_type())
    9879                 :             :     is_slice = true;
    9880                 :        6609 :   else if (type->map_type() != NULL)
    9881                 :             :     is_map = true;
    9882                 :       17862 :   else if (type->channel_type() != NULL)
    9883                 :             :     is_chan = true;
    9884                 :             :   else
    9885                 :           0 :     go_unreachable();
    9886                 :             : 
    9887                 :       15099 :   ++parg;
    9888                 :       15099 :   Expression* len_arg;
    9889                 :       15099 :   bool len_small = false;
    9890                 :       15099 :   if (parg == args->end())
    9891                 :             :     {
    9892                 :        4933 :       go_assert(!is_slice);
    9893                 :        4933 :       len_arg = Expression::make_integer_ul(0, NULL, loc);
    9894                 :        4933 :       len_small = true;
    9895                 :             :     }
    9896                 :             :   else
    9897                 :             :     {
    9898                 :       10166 :       len_arg = *parg;
    9899                 :       10166 :       if (!this->check_int_value(len_arg, true, &len_small))
    9900                 :           8 :         return Expression::make_error(this->location());
    9901                 :       10158 :       ++parg;
    9902                 :             :     }
    9903                 :             : 
    9904                 :       15091 :   Expression* cap_arg = NULL;
    9905                 :       15091 :   bool cap_small = false;
    9906                 :       15091 :   Numeric_constant nclen;
    9907                 :       15091 :   Numeric_constant nccap;
    9908                 :       15091 :   unsigned long vlen;
    9909                 :       15091 :   unsigned long vcap;
    9910                 :       15091 :   if (is_slice && parg != args->end())
    9911                 :             :     {
    9912                 :        1715 :       cap_arg = *parg;
    9913                 :        1715 :       if (!this->check_int_value(cap_arg, false, &cap_small))
    9914                 :           2 :         return Expression::make_error(this->location());
    9915                 :             : 
    9916                 :        1713 :       if (len_arg->numeric_constant_value(&nclen)
    9917                 :        1614 :           && cap_arg->numeric_constant_value(&nccap)
    9918                 :         554 :           && nclen.to_unsigned_long(&vlen) == Numeric_constant::NC_UL_VALID
    9919                 :         554 :           && nccap.to_unsigned_long(&vcap) == Numeric_constant::NC_UL_VALID
    9920                 :        2267 :           && vlen > vcap)
    9921                 :             :         {
    9922                 :           1 :           this->report_error(_("len larger than cap"));
    9923                 :           1 :           return Expression::make_error(this->location());
    9924                 :             :         }
    9925                 :             : 
    9926                 :        1712 :       ++parg;
    9927                 :             :     }
    9928                 :             : 
    9929                 :       15088 :   go_assert(parg == args->end());
    9930                 :             : 
    9931                 :       15088 :   Location type_loc = first_arg->location();
    9932                 :             : 
    9933                 :       15088 :   Expression* call;
    9934                 :       15088 :   if (is_slice)
    9935                 :             :     {
    9936                 :        8479 :       Temporary_statement* len_temp = NULL;
    9937                 :        8479 :       if (!len_arg->is_constant())
    9938                 :             :         {
    9939                 :        4458 :           len_temp = Statement::make_temporary(NULL, len_arg, loc);
    9940                 :        4458 :           inserter->insert(len_temp);
    9941                 :        4458 :           len_arg = Expression::make_temporary_reference(len_temp, loc);
    9942                 :             :         }
    9943                 :             : 
    9944                 :        8479 :       if (cap_arg == NULL)
    9945                 :             :         {
    9946                 :        6767 :           cap_small = len_small;
    9947                 :        6767 :           if (len_temp == NULL)
    9948                 :        2408 :             cap_arg = len_arg->copy();
    9949                 :             :           else
    9950                 :        4359 :             cap_arg = Expression::make_temporary_reference(len_temp, loc);
    9951                 :             :         }
    9952                 :        1712 :       else if (!cap_arg->is_constant())
    9953                 :             :         {
    9954                 :        1138 :           Temporary_statement* cap_temp = Statement::make_temporary(NULL,
    9955                 :             :                                                                     cap_arg,
    9956                 :             :                                                                     loc);
    9957                 :        1138 :           inserter->insert(cap_temp);
    9958                 :        1138 :           cap_arg = Expression::make_temporary_reference(cap_temp, loc);
    9959                 :             :         }
    9960                 :             : 
    9961                 :       16958 :       Type* et = type->array_type()->element_type();
    9962                 :        8479 :       Expression* type_arg = Expression::make_type_descriptor(et, type_loc);
    9963                 :        8479 :       Runtime::Function code = Runtime::MAKESLICE;
    9964                 :        8479 :       if (!len_small || !cap_small)
    9965                 :         120 :         code = Runtime::MAKESLICE64;
    9966                 :        8479 :       Expression* mem = Runtime::make_call(gogo, code, loc, 3,
    9967                 :             :                                            type_arg, len_arg, cap_arg);
    9968                 :        8479 :       mem = Expression::make_unsafe_cast(Type::make_pointer_type(et), mem,
    9969                 :             :                                          loc);
    9970                 :        8479 :       Type* int_type = Type::lookup_integer_type("int");
    9971                 :        8479 :       len_arg = Expression::make_cast(int_type, len_arg->copy(), loc);
    9972                 :        8479 :       cap_arg = Expression::make_cast(int_type, cap_arg->copy(), loc);
    9973                 :        8479 :       call = Expression::make_slice_value(type, mem, len_arg, cap_arg, loc);
    9974                 :             :     }
    9975                 :        6609 :   else if (is_map)
    9976                 :             :     {
    9977                 :        3846 :       Expression* type_arg = Expression::make_type_descriptor(type, type_loc);
    9978                 :        3846 :       if (!len_small)
    9979                 :           2 :         call = Runtime::make_call(gogo, Runtime::MAKEMAP64, loc, 3, type_arg,
    9980                 :             :                                   len_arg,
    9981                 :             :                                   Expression::make_nil(loc));
    9982                 :             :       else
    9983                 :             :         {
    9984                 :        3844 :           if (len_arg->numeric_constant_value(&nclen)
    9985                 :        3485 :               && nclen.to_unsigned_long(&vlen) == Numeric_constant::NC_UL_VALID
    9986                 :        7329 :               && vlen <= Map_type::bucket_size)
    9987                 :        3434 :             call = Runtime::make_call(gogo, Runtime::MAKEMAP_SMALL, loc, 0);
    9988                 :             :           else
    9989                 :         410 :             call = Runtime::make_call(gogo, Runtime::MAKEMAP, loc, 3, type_arg,
    9990                 :             :                                       len_arg,
    9991                 :             :                                       Expression::make_nil(loc));
    9992                 :             :         }
    9993                 :             :     }
    9994                 :        2763 :   else if (is_chan)
    9995                 :             :     {
    9996                 :        2763 :       Expression* type_arg = Expression::make_type_descriptor(type, type_loc);
    9997                 :        2763 :       Runtime::Function code = Runtime::MAKECHAN;
    9998                 :        2763 :       if (!len_small)
    9999                 :           0 :         code = Runtime::MAKECHAN64;
   10000                 :        2763 :       call = Runtime::make_call(gogo, code, loc, 2, type_arg, len_arg);
   10001                 :             :     }
   10002                 :             :   else
   10003                 :           0 :     go_unreachable();
   10004                 :             : 
   10005                 :       15088 :   Expression* ret = Expression::make_unsafe_cast(type, call, loc);
   10006                 :       15088 :   ret->determine_type_no_context(gogo);
   10007                 :       15088 :   return ret;
   10008                 :       15091 : }
   10009                 :             : 
   10010                 :             : // Flatten a call to the predeclared append function.  We do this in
   10011                 :             : // the flatten phase, not the lowering phase, so that we run after
   10012                 :             : // type checking and after order_evaluations.  If ASSIGN_LHS is not
   10013                 :             : // NULL, this append is the right-hand-side of an assignment and
   10014                 :             : // ASSIGN_LHS is the left-hand-side; in that case, set LHS directly
   10015                 :             : // rather than returning a slice.  This lets us omit a write barrier
   10016                 :             : // in common cases like a = append(a, ...) when the slice does not
   10017                 :             : // need to grow.  ENCLOSING is not NULL iff ASSIGN_LHS is not NULL.
   10018                 :             : 
   10019                 :             : Expression*
   10020                 :       15676 : Builtin_call_expression::flatten_append(Gogo* gogo, Named_object* function,
   10021                 :             :                                         Statement_inserter* inserter,
   10022                 :             :                                         Expression* assign_lhs,
   10023                 :             :                                         Block* enclosing)
   10024                 :             : {
   10025                 :       15676 :   if (this->is_error_expression())
   10026                 :           0 :     return this;
   10027                 :             : 
   10028                 :       15676 :   Location loc = this->location();
   10029                 :             : 
   10030                 :       15676 :   const Expression_list* args = this->args();
   10031                 :       15676 :   go_assert(args != NULL && !args->empty());
   10032                 :             : 
   10033                 :       15676 :   Type* slice_type = args->front()->type();
   10034                 :       15676 :   go_assert(slice_type->is_slice_type());
   10035                 :       31352 :   Type* element_type = slice_type->array_type()->element_type();
   10036                 :             : 
   10037                 :       15676 :   if (args->size() == 1)
   10038                 :             :     {
   10039                 :             :       // append(s) evaluates to s.
   10040                 :          23 :       if (assign_lhs != NULL)
   10041                 :             :         return NULL;
   10042                 :          23 :       return args->front();
   10043                 :             :     }
   10044                 :             : 
   10045                 :       15653 :   Type* int_type = Type::lookup_integer_type("int");
   10046                 :       15653 :   Type_context int_context(int_type, false);
   10047                 :       15653 :   Type* uint_type = Type::lookup_integer_type("uint");
   10048                 :             : 
   10049                 :             :   // Implementing
   10050                 :             :   //   append(s1, s2...)
   10051                 :             :   // or
   10052                 :             :   //   append(s1, a1, a2, a3, ...)
   10053                 :             : 
   10054                 :             :   // s1tmp := s1
   10055                 :       15653 :   Temporary_statement* s1tmp = Statement::make_temporary(NULL, args->front(),
   10056                 :             :                                                          loc);
   10057                 :       15653 :   inserter->insert(s1tmp);
   10058                 :             : 
   10059                 :             :   // l1tmp := len(s1tmp)
   10060                 :       15653 :   Named_object* lenfn = gogo->lookup_global("len");
   10061                 :       15653 :   Expression* lenref = Expression::make_func_reference(lenfn, NULL, loc);
   10062                 :       15653 :   Expression_list* call_args = new Expression_list();
   10063                 :       15653 :   call_args->push_back(Expression::make_temporary_reference(s1tmp, loc));
   10064                 :       15653 :   Expression* len = Expression::make_call(lenref, call_args, false, loc);
   10065                 :       15653 :   len->determine_type(gogo, &int_context);
   10066                 :       15653 :   gogo->lower_expression(function, inserter, &len);
   10067                 :       15653 :   gogo->flatten_expression(function, inserter, &len);
   10068                 :       15653 :   Temporary_statement* l1tmp = Statement::make_temporary(int_type, len, loc);
   10069                 :       15653 :   inserter->insert(l1tmp);
   10070                 :             : 
   10071                 :       15653 :   Temporary_statement* s2tmp = NULL;
   10072                 :       15653 :   Temporary_statement* l2tmp = NULL;
   10073                 :       15653 :   Expression_list* add = NULL;
   10074                 :       15653 :   Expression* len2;
   10075                 :       15653 :   Call_expression* makecall = NULL;
   10076                 :       15653 :   if (this->is_varargs())
   10077                 :             :     {
   10078                 :        3556 :       go_assert(args->size() == 2);
   10079                 :             : 
   10080                 :        3556 :       std::pair<Call_expression*, Temporary_statement*> p =
   10081                 :        3556 :         Expression::find_makeslice_call(args->back());
   10082                 :        3556 :       makecall = p.first;
   10083                 :        3556 :       if (makecall != NULL)
   10084                 :             :         {
   10085                 :             :           // We are handling
   10086                 :             :           //    append(s, make([]T, len[, cap])...))
   10087                 :             :           // which has already been lowered to
   10088                 :             :           //    append(s, runtime.makeslice(T, len, cap)).
   10089                 :             :           // We will optimize this to directly zeroing the tail,
   10090                 :             :           // instead of allocating a new slice then copy.
   10091                 :             : 
   10092                 :             :           // Retrieve the length and capacity. Cannot reference s2 as
   10093                 :             :           // we will remove the makeslice call.
   10094                 :          27 :           Expression* len_arg = makecall->args()->at(1);
   10095                 :          27 :           len_arg = Expression::make_cast(int_type, len_arg, loc);
   10096                 :          27 :           l2tmp = Statement::make_temporary(int_type, len_arg, loc);
   10097                 :          27 :           l2tmp->determine_types(gogo);
   10098                 :          27 :           inserter->insert(l2tmp);
   10099                 :             : 
   10100                 :          27 :           Expression* cap_arg = makecall->args()->at(2);
   10101                 :          27 :           cap_arg = Expression::make_cast(int_type, cap_arg, loc);
   10102                 :          27 :           Temporary_statement* c2tmp =
   10103                 :          27 :             Statement::make_temporary(int_type, cap_arg, loc);
   10104                 :          27 :           c2tmp->determine_types(gogo);
   10105                 :          27 :           inserter->insert(c2tmp);
   10106                 :             : 
   10107                 :             :           // Check bad len/cap here.
   10108                 :             :           // checkmakeslice(type, len, cap)
   10109                 :             :           // (Note that if len and cap are constants, we won't see a
   10110                 :             :           // makeslice call here, as it will be rewritten to a stack
   10111                 :             :           // allocated array by Mark_address_taken::expression.)
   10112                 :          27 :           Expression* elem = Expression::make_type_descriptor(element_type,
   10113                 :             :                                                               loc);
   10114                 :          27 :           len2 = Expression::make_temporary_reference(l2tmp, loc);
   10115                 :          27 :           Expression* cap2 = Expression::make_temporary_reference(c2tmp, loc);
   10116                 :          27 :           Expression* check = Runtime::make_call(gogo,
   10117                 :             :                                                  Runtime::CHECK_MAKE_SLICE,
   10118                 :          27 :                                                  loc, 3, elem, len2, cap2);
   10119                 :          27 :           check->determine_type_no_context(gogo);
   10120                 :          27 :           gogo->lower_expression(function, inserter, &check);
   10121                 :          27 :           gogo->flatten_expression(function, inserter, &check);
   10122                 :          27 :           Statement* s = Statement::make_statement(check, false);
   10123                 :          27 :           inserter->insert(s);
   10124                 :             : 
   10125                 :             :           // Remove the original makeslice call.
   10126                 :          27 :           Temporary_statement* ts = p.second;
   10127                 :          27 :           if (ts != NULL && ts->uses() == 1)
   10128                 :          27 :             ts->set_init(Expression::make_nil(loc));
   10129                 :             :         }
   10130                 :             :       else
   10131                 :             :         {
   10132                 :             :           // s2tmp := s2
   10133                 :        3529 :           s2tmp = Statement::make_temporary(NULL, args->back(), loc);
   10134                 :        3529 :           inserter->insert(s2tmp);
   10135                 :             : 
   10136                 :             :           // l2tmp := len(s2tmp)
   10137                 :        3529 :           lenref = Expression::make_func_reference(lenfn, NULL, loc);
   10138                 :        3529 :           call_args = new Expression_list();
   10139                 :        3529 :           call_args->push_back(Expression::make_temporary_reference(s2tmp, loc));
   10140                 :        3529 :           len = Expression::make_call(lenref, call_args, false, loc);
   10141                 :        3529 :           len->determine_type(gogo, &int_context);
   10142                 :        3529 :           gogo->lower_expression(function, inserter, &len);
   10143                 :        3529 :           gogo->flatten_expression(function, inserter, &len);
   10144                 :        3529 :           l2tmp = Statement::make_temporary(int_type, len, loc);
   10145                 :        3529 :           l2tmp->determine_types(gogo);
   10146                 :        3529 :           inserter->insert(l2tmp);
   10147                 :             :         }
   10148                 :             : 
   10149                 :             :       // len2 = l2tmp
   10150                 :        3556 :       len2 = Expression::make_temporary_reference(l2tmp, loc);
   10151                 :             :     }
   10152                 :             :   else
   10153                 :             :     {
   10154                 :             :       // We have to ensure that all the arguments are in variables
   10155                 :             :       // now, because otherwise if one of them is an index expression
   10156                 :             :       // into the current slice we could overwrite it before we fetch
   10157                 :             :       // it.
   10158                 :       12097 :       add = new Expression_list();
   10159                 :       12097 :       Expression_list::const_iterator pa = args->begin();
   10160                 :       26301 :       for (++pa; pa != args->end(); ++pa)
   10161                 :             :         {
   10162                 :       14204 :           if ((*pa)->is_multi_eval_safe())
   10163                 :        7816 :             add->push_back(*pa);
   10164                 :             :           else
   10165                 :             :             {
   10166                 :        6388 :               Temporary_statement* tmp = Statement::make_temporary(NULL, *pa,
   10167                 :             :                                                                    loc);
   10168                 :        6388 :               inserter->insert(tmp);
   10169                 :        6388 :               add->push_back(Expression::make_temporary_reference(tmp, loc));
   10170                 :             :             }
   10171                 :             :         }
   10172                 :             : 
   10173                 :             :       // len2 = len(add)
   10174                 :       12097 :       len2 = Expression::make_integer_ul(add->size(), int_type, loc);
   10175                 :             :     }
   10176                 :             : 
   10177                 :             :   // ntmp := l1tmp + len2
   10178                 :       15653 :   Expression* ref = Expression::make_temporary_reference(l1tmp, loc);
   10179                 :       15653 :   Expression* sum = Expression::make_binary(OPERATOR_PLUS, ref, len2, loc);
   10180                 :       15653 :   sum->determine_type(gogo, &int_context);
   10181                 :       15653 :   gogo->lower_expression(function, inserter, &sum);
   10182                 :       15653 :   gogo->flatten_expression(function, inserter, &sum);
   10183                 :       15653 :   Temporary_statement* ntmp = Statement::make_temporary(int_type, sum, loc);
   10184                 :       15653 :   ntmp->determine_types(gogo);
   10185                 :       15653 :   inserter->insert(ntmp);
   10186                 :             : 
   10187                 :             :   // s1tmp = uint(ntmp) > uint(cap(s1tmp)) ?
   10188                 :             :   //   growslice(type, s1tmp, ntmp) :
   10189                 :             :   //   s1tmp[:ntmp]
   10190                 :             :   // Using uint here means that if the computation of ntmp overflowed,
   10191                 :             :   // we will call growslice which will panic.
   10192                 :             : 
   10193                 :       15653 :   Named_object* capfn = gogo->lookup_global("cap");
   10194                 :       15653 :   Expression* capref = Expression::make_func_reference(capfn, NULL, loc);
   10195                 :       15653 :   call_args = new Expression_list();
   10196                 :       15653 :   call_args->push_back(Expression::make_temporary_reference(s1tmp, loc));
   10197                 :       15653 :   Expression* cap = Expression::make_call(capref, call_args, false, loc);
   10198                 :       15653 :   cap->determine_type(gogo, &int_context);
   10199                 :       15653 :   gogo->lower_expression(function, inserter, &cap);
   10200                 :       15653 :   gogo->flatten_expression(function, inserter, &cap);
   10201                 :       15653 :   Temporary_statement* c1tmp = Statement::make_temporary(int_type, cap, loc);
   10202                 :       15653 :   c1tmp->determine_types(gogo);
   10203                 :       15653 :   inserter->insert(c1tmp);
   10204                 :             : 
   10205                 :       15653 :   Expression* left = Expression::make_temporary_reference(ntmp, loc);
   10206                 :       15653 :   left = Expression::make_cast(uint_type, left, loc);
   10207                 :       15653 :   Expression* right = Expression::make_temporary_reference(c1tmp, loc);
   10208                 :       15653 :   right = Expression::make_cast(uint_type, right, loc);
   10209                 :             : 
   10210                 :       15653 :   Expression* cond = Expression::make_binary(OPERATOR_GT, left, right, loc);
   10211                 :             : 
   10212                 :       15653 :   Type* unsafe_ptr_type = Type::make_pointer_type(Type::make_void_type());
   10213                 :       15653 :   Expression* a1 = Expression::make_type_descriptor(element_type, loc);
   10214                 :       15653 :   Expression* a2 = Expression::make_temporary_reference(s1tmp, loc);
   10215                 :       31306 :   a2 = slice_type->array_type()->get_value_pointer(gogo, a2);
   10216                 :       15653 :   a2 = Expression::make_cast(unsafe_ptr_type, a2, loc);
   10217                 :       15653 :   Expression* a3 = Expression::make_temporary_reference(l1tmp, loc);
   10218                 :       15653 :   Expression* a4 = Expression::make_temporary_reference(c1tmp, loc);
   10219                 :       15653 :   Expression* a5 = Expression::make_temporary_reference(ntmp, loc);
   10220                 :       15653 :   Expression* call = Runtime::make_call(gogo, Runtime::GROWSLICE, loc, 5,
   10221                 :       15653 :                                         a1, a2, a3, a4, a5);
   10222                 :       15653 :   call = Expression::make_unsafe_cast(slice_type, call, loc);
   10223                 :             : 
   10224                 :       15653 :   ref = Expression::make_temporary_reference(s1tmp, loc);
   10225                 :       15653 :   Expression* zero = Expression::make_integer_ul(0, int_type, loc);
   10226                 :       15653 :   Expression* ref2 = Expression::make_temporary_reference(ntmp, loc);
   10227                 :       15653 :   ref = Expression::make_array_index(ref, zero, ref2, NULL, loc);
   10228                 :       15653 :   ref->array_index_expression()->set_needs_bounds_check(false);
   10229                 :             : 
   10230                 :       15653 :   if (assign_lhs == NULL)
   10231                 :             :     {
   10232                 :        2631 :       Expression* rhs = Expression::make_conditional(cond, call, ref, loc);
   10233                 :             : 
   10234                 :        2631 :       rhs->determine_type_no_context(gogo);
   10235                 :        2631 :       gogo->lower_expression(function, inserter, &rhs);
   10236                 :        2631 :       gogo->flatten_expression(function, inserter, &rhs);
   10237                 :             : 
   10238                 :        2631 :       ref = Expression::make_temporary_reference(s1tmp, loc);
   10239                 :        2631 :       Statement* assign = Statement::make_assignment(ref, rhs, loc);
   10240                 :        2631 :       assign->determine_types(gogo);
   10241                 :        2631 :       inserter->insert(assign);
   10242                 :             :     }
   10243                 :             :   else
   10244                 :             :     {
   10245                 :       13022 :       cond->determine_type_no_context(gogo);
   10246                 :       13022 :       gogo->lower_expression(function, inserter, &cond);
   10247                 :       13022 :       gogo->flatten_expression(function, inserter, &cond);
   10248                 :       13022 :       call->determine_type_no_context(gogo);
   10249                 :       13022 :       gogo->lower_expression(function, inserter, &call);
   10250                 :       13022 :       gogo->flatten_expression(function, inserter, &call);
   10251                 :       13022 :       ref->determine_type_no_context(gogo);
   10252                 :       13022 :       gogo->lower_expression(function, inserter, &ref);
   10253                 :       13022 :       gogo->flatten_expression(function, inserter, &ref);
   10254                 :             : 
   10255                 :       13022 :       Block* then_block = new Block(enclosing, loc);
   10256                 :       13022 :       Assignment_statement* assign =
   10257                 :       13022 :         Statement::make_assignment(assign_lhs, call, loc);
   10258                 :       13022 :       assign->determine_types(gogo);
   10259                 :       13022 :       then_block->add_statement(assign);
   10260                 :             : 
   10261                 :       13022 :       Block* else_block = new Block(enclosing, loc);
   10262                 :       13022 :       assign = Statement::make_assignment(assign_lhs->copy(), ref, loc);
   10263                 :             :       // This assignment will not change the pointer value, so it does
   10264                 :             :       // not need a write barrier.
   10265                 :       13022 :       assign->set_omit_write_barrier();
   10266                 :       13022 :       assign->determine_types(gogo);
   10267                 :       13022 :       else_block->add_statement(assign);
   10268                 :             : 
   10269                 :       13022 :       Statement* s = Statement::make_if_statement(cond, then_block,
   10270                 :             :                                                   else_block, loc);
   10271                 :       13022 :       s->determine_types(gogo);
   10272                 :       13022 :       inserter->insert(s);
   10273                 :             : 
   10274                 :       13022 :       ref = Expression::make_temporary_reference(s1tmp, loc);
   10275                 :       13022 :       assign = Statement::make_assignment(ref, assign_lhs->copy(), loc);
   10276                 :       13022 :       assign->determine_types(gogo);
   10277                 :       13022 :       inserter->insert(assign);
   10278                 :             :     }
   10279                 :             : 
   10280                 :       15653 :   Type* uintptr_type = Type::lookup_integer_type("uintptr");
   10281                 :             : 
   10282                 :       15653 :   if (this->is_varargs())
   10283                 :             :     {
   10284                 :        3556 :       if (makecall != NULL)
   10285                 :             :         {
   10286                 :             :           // memclr(&s1tmp[l1tmp], l2tmp*sizeof(elem))
   10287                 :          27 :           a1 = Expression::make_temporary_reference(s1tmp, loc);
   10288                 :          27 :           ref = Expression::make_temporary_reference(l1tmp, loc);
   10289                 :          27 :           a1 = Expression::make_array_index(a1, ref, NULL, NULL, loc);
   10290                 :          27 :           a1->array_index_expression()->set_needs_bounds_check(false);
   10291                 :          27 :           a1 = Expression::make_unary(OPERATOR_AND, a1, loc);
   10292                 :             : 
   10293                 :          27 :           ref = Expression::make_temporary_reference(l2tmp, loc);
   10294                 :          27 :           ref = Expression::make_cast(uintptr_type, ref, loc);
   10295                 :          27 :           a2 = Expression::make_type_info(element_type, TYPE_INFO_SIZE);
   10296                 :          27 :           a2 = Expression::make_binary(OPERATOR_MULT, a2, ref, loc);
   10297                 :             : 
   10298                 :          27 :           if (element_type->has_pointer())
   10299                 :           7 :             call = Runtime::make_call(gogo, Runtime::MEMCLRHASPTR, loc, 2,
   10300                 :             :                                       a1, a2);
   10301                 :             :           else
   10302                 :             :             {
   10303                 :          20 :               Type* int32_type = Type::lookup_integer_type("int32");
   10304                 :          20 :               zero = Expression::make_integer_ul(0, int32_type, loc);
   10305                 :          20 :               call = Runtime::make_call(gogo, Runtime::BUILTIN_MEMSET, loc, 3,
   10306                 :             :                                         a1, zero, a2);
   10307                 :             :             }
   10308                 :             : 
   10309                 :          27 :           if (element_type->has_pointer())
   10310                 :             :             {
   10311                 :             :               // For a slice containing pointers, growslice already zeroed
   10312                 :             :               // the memory. We only need to zero in non-growing case.
   10313                 :             :               // Note: growslice does not zero the memory in non-pointer case.
   10314                 :           7 :               ref = Expression::make_temporary_reference(ntmp, loc);
   10315                 :           7 :               ref = Expression::make_cast(uint_type, ref, loc);
   10316                 :           7 :               ref2 = Expression::make_temporary_reference(c1tmp, loc);
   10317                 :           7 :               ref2 = Expression::make_cast(uint_type, ref2, loc);
   10318                 :           7 :               cond = Expression::make_binary(OPERATOR_GT, ref, ref2, loc);
   10319                 :           7 :               zero = Expression::make_integer_ul(0, int_type, loc);
   10320                 :           7 :               call = Expression::make_conditional(cond, zero, call, loc);
   10321                 :             :             }
   10322                 :             :         }
   10323                 :             :       else
   10324                 :             :         {
   10325                 :        3529 :           if (element_type->has_pointer())
   10326                 :             :             {
   10327                 :             :               // copy(s1tmp[l1tmp:], s2tmp)
   10328                 :        1251 :               a1 = Expression::make_temporary_reference(s1tmp, loc);
   10329                 :        1251 :               ref = Expression::make_temporary_reference(l1tmp, loc);
   10330                 :        1251 :               Expression* nil = Expression::make_nil(loc);
   10331                 :        1251 :               a1 = Expression::make_array_index(a1, ref, nil, NULL, loc);
   10332                 :        1251 :               a1->array_index_expression()->set_needs_bounds_check(false);
   10333                 :             : 
   10334                 :        1251 :               a2 = Expression::make_temporary_reference(s2tmp, loc);
   10335                 :             : 
   10336                 :        1251 :               Named_object* copyfn = gogo->lookup_global("copy");
   10337                 :        1251 :               Expression* copyref = Expression::make_func_reference(copyfn, NULL, loc);
   10338                 :        1251 :               call_args = new Expression_list();
   10339                 :        1251 :               call_args->push_back(a1);
   10340                 :        1251 :               call_args->push_back(a2);
   10341                 :        1251 :               call = Expression::make_call(copyref, call_args, false, loc);
   10342                 :             :             }
   10343                 :             :           else
   10344                 :             :             {
   10345                 :             :               // memmove(&s1tmp[l1tmp], s2tmp.ptr, l2tmp*sizeof(elem))
   10346                 :        2278 :               a1 = Expression::make_temporary_reference(s1tmp, loc);
   10347                 :        2278 :               ref = Expression::make_temporary_reference(l1tmp, loc);
   10348                 :        2278 :               a1 = Expression::make_array_index(a1, ref, NULL, NULL, loc);
   10349                 :        2278 :               a1->array_index_expression()->set_needs_bounds_check(false);
   10350                 :        2278 :               a1 = Expression::make_unary(OPERATOR_AND, a1, loc);
   10351                 :             : 
   10352                 :        2278 :               a2 = Expression::make_temporary_reference(s2tmp, loc);
   10353                 :        2278 :               a2 = (a2->type()->is_string_type()
   10354                 :        2278 :                     ? Expression::make_string_info(a2,
   10355                 :             :                                                    STRING_INFO_DATA,
   10356                 :             :                                                    loc)
   10357                 :        1314 :                     : Expression::make_slice_info(a2,
   10358                 :             :                                                   SLICE_INFO_VALUE_POINTER,
   10359                 :             :                                                   loc));
   10360                 :             : 
   10361                 :        2278 :               ref = Expression::make_temporary_reference(l2tmp, loc);
   10362                 :        2278 :               ref = Expression::make_cast(uintptr_type, ref, loc);
   10363                 :        2278 :               a3 = Expression::make_type_info(element_type, TYPE_INFO_SIZE);
   10364                 :        2278 :               a3 = Expression::make_binary(OPERATOR_MULT, a3, ref, loc);
   10365                 :             : 
   10366                 :        2278 :               call = Runtime::make_call(gogo, Runtime::BUILTIN_MEMMOVE, loc, 3,
   10367                 :             :                                         a1, a2, a3);
   10368                 :             :             }
   10369                 :             :         }
   10370                 :        3556 :       call->determine_type_no_context(gogo);
   10371                 :        3556 :       gogo->lower_expression(function, inserter, &call);
   10372                 :        3556 :       gogo->flatten_expression(function, inserter, &call);
   10373                 :        3556 :       inserter->insert(Statement::make_statement(call, false));
   10374                 :             :     }
   10375                 :             :   else
   10376                 :             :     {
   10377                 :             :       // For each argument:
   10378                 :             :       //  s1tmp[l1tmp+i] = a
   10379                 :       12097 :       unsigned long i = 0;
   10380                 :       12097 :       for (Expression_list::const_iterator pa = add->begin();
   10381                 :       26301 :            pa != add->end();
   10382                 :       14204 :            ++pa, ++i)
   10383                 :             :         {
   10384                 :       14204 :           ref = Expression::make_temporary_reference(s1tmp, loc);
   10385                 :       14204 :           ref2 = Expression::make_temporary_reference(l1tmp, loc);
   10386                 :       14204 :           Expression* off = Expression::make_integer_ul(i, int_type, loc);
   10387                 :       14204 :           ref2 = Expression::make_binary(OPERATOR_PLUS, ref2, off, loc);
   10388                 :       14204 :           Expression* lhs = Expression::make_array_index(ref, ref2, NULL,
   10389                 :       14204 :                                                          NULL, loc);
   10390                 :       14204 :           lhs->array_index_expression()->set_needs_bounds_check(false);
   10391                 :       14204 :           lhs->determine_type_no_context(gogo);
   10392                 :       14204 :           gogo->lower_expression(function, inserter, &lhs);
   10393                 :       14204 :           gogo->flatten_expression(function, inserter, &lhs);
   10394                 :       14204 :       Expression* elem = *pa;
   10395                 :       14204 :       if (!Type::are_identical(element_type, elem->type(), 0, NULL)
   10396                 :       14204 :           && element_type->interface_type() != NULL)
   10397                 :         504 :         elem = Expression::make_cast(element_type, elem, loc);
   10398                 :             :           // The flatten pass runs after the write barrier pass, so we
   10399                 :             :           // need to insert a write barrier here if necessary.
   10400                 :             :           // However, if ASSIGN_LHS is not NULL, we have been called
   10401                 :             :           // directly before the write barrier pass.
   10402                 :       14204 :           Statement* assign;
   10403                 :       14204 :           if (assign_lhs != NULL
   10404                 :       14204 :               || !gogo->assign_needs_write_barrier(lhs, NULL))
   10405                 :       12988 :             assign = Statement::make_assignment(lhs, elem, loc);
   10406                 :             :           else
   10407                 :             :             {
   10408                 :        1216 :               Function* f = function == NULL ? NULL : function->func_value();
   10409                 :        1216 :               assign = gogo->assign_with_write_barrier(f, NULL, inserter,
   10410                 :             :                                                        lhs, elem, loc);
   10411                 :             :             }
   10412                 :       14204 :           assign->determine_types(gogo);
   10413                 :       14204 :           inserter->insert(assign);
   10414                 :             :         }
   10415                 :             :     }
   10416                 :             : 
   10417                 :       15653 :   if (assign_lhs != NULL)
   10418                 :             :     return NULL;
   10419                 :             : 
   10420                 :        2631 :   return Expression::make_temporary_reference(s1tmp, loc);
   10421                 :             : }
   10422                 :             : 
   10423                 :             : // Return whether an expression has an integer value.  Report an error
   10424                 :             : // if not.  This is used when handling calls to the predeclared make
   10425                 :             : // function.  Set *SMALL if the value is known to fit in type "int".
   10426                 :             : 
   10427                 :             : bool
   10428                 :       11881 : Builtin_call_expression::check_int_value(Expression* e, bool is_length,
   10429                 :             :                                          bool *small)
   10430                 :             : {
   10431                 :       11881 :   *small = false;
   10432                 :             : 
   10433                 :       11881 :   Numeric_constant nc;
   10434                 :       11881 :   if (e->numeric_constant_value(&nc))
   10435                 :             :     {
   10436                 :        5748 :       unsigned long v;
   10437                 :        5748 :       switch (nc.to_unsigned_long(&v))
   10438                 :             :         {
   10439                 :             :         case Numeric_constant::NC_UL_VALID:
   10440                 :             :           break;
   10441                 :           1 :         case Numeric_constant::NC_UL_NOTINT:
   10442                 :           1 :           go_error_at(e->location(), "non-integer %s argument to make",
   10443                 :             :                       is_length ? "len" : "cap");
   10444                 :           1 :           return false;
   10445                 :           2 :         case Numeric_constant::NC_UL_NEGATIVE:
   10446                 :           2 :           go_error_at(e->location(), "negative %s argument to make",
   10447                 :             :                       is_length ? "len" : "cap");
   10448                 :           2 :           return false;
   10449                 :             :         case Numeric_constant::NC_UL_BIG:
   10450                 :             :           // We don't want to give a compile-time error for a 64-bit
   10451                 :             :           // value on a 32-bit target.
   10452                 :             :           break;
   10453                 :             :         }
   10454                 :             : 
   10455                 :        5745 :       mpz_t val;
   10456                 :        5745 :       if (!nc.to_int(&val))
   10457                 :           0 :         go_unreachable();
   10458                 :        5745 :       int bits = mpz_sizeinbase(val, 2);
   10459                 :        5745 :       mpz_clear(val);
   10460                 :        5745 :       Type* int_type = Type::lookup_integer_type("int");
   10461                 :       11490 :       if (bits >= int_type->integer_type()->bits())
   10462                 :             :         {
   10463                 :           1 :           go_error_at(e->location(), "%s argument too large for make",
   10464                 :             :                       is_length ? "len" : "cap");
   10465                 :           1 :           return false;
   10466                 :             :         }
   10467                 :             : 
   10468                 :        5744 :       *small = true;
   10469                 :        5744 :       return true;
   10470                 :             :     }
   10471                 :             : 
   10472                 :        6133 :   if (e->type()->integer_type() != NULL)
   10473                 :             :     {
   10474                 :       12254 :       int ebits = e->type()->integer_type()->bits();
   10475                 :       12254 :       int intbits = Type::lookup_integer_type("int")->integer_type()->bits();
   10476                 :             : 
   10477                 :             :       // We can treat ebits == intbits as small even for an unsigned
   10478                 :             :       // integer type, because we will convert the value to int and
   10479                 :             :       // then reject it in the runtime if it is negative.
   10480                 :        6127 :       *small = ebits <= intbits;
   10481                 :             : 
   10482                 :        6127 :       return true;
   10483                 :             :     }
   10484                 :             : 
   10485                 :           6 :   go_error_at(e->location(), "non-integer %s argument to make",
   10486                 :             :               is_length ? "len" : "cap");
   10487                 :           6 :   return false;
   10488                 :       11881 : }
   10489                 :             : 
   10490                 :             : // Return the type of the real or imag functions, given the type of
   10491                 :             : // the argument.  We need to map complex64 to float32 and complex128
   10492                 :             : // to float64, so it has to be done by name.  This returns NULL if it
   10493                 :             : // can't figure out the type.
   10494                 :             : 
   10495                 :             : Type*
   10496                 :       22174 : Builtin_call_expression::real_imag_type(Type* arg_type)
   10497                 :             : {
   10498                 :       22174 :   if (arg_type == NULL || arg_type->is_abstract())
   10499                 :          95 :     return NULL;
   10500                 :       22079 :   Named_type* nt = arg_type->named_type();
   10501                 :       22079 :   if (nt == NULL)
   10502                 :             :     return NULL;
   10503                 :       22080 :   while (nt->real_type()->named_type() != NULL)
   10504                 :           5 :     nt = nt->real_type()->named_type();
   10505                 :       22075 :   if (nt->name() == "complex64")
   10506                 :         153 :     return Type::lookup_float_type("float32");
   10507                 :       21922 :   else if (nt->name() == "complex128")
   10508                 :       21859 :     return Type::lookup_float_type("float64");
   10509                 :             :   else
   10510                 :             :     return NULL;
   10511                 :             : }
   10512                 :             : 
   10513                 :             : // Return the type of the complex function, given the type of one of the
   10514                 :             : // argments.  Like real_imag_type, we have to map by name.
   10515                 :             : 
   10516                 :             : Type*
   10517                 :       88910 : Builtin_call_expression::complex_type(Type* arg_type)
   10518                 :             : {
   10519                 :       88910 :   if (arg_type == NULL || arg_type->is_abstract())
   10520                 :         305 :     return NULL;
   10521                 :       88605 :   Named_type* nt = arg_type->named_type();
   10522                 :       88605 :   if (nt == NULL)
   10523                 :             :     return NULL;
   10524                 :       88599 :   while (nt->real_type()->named_type() != NULL)
   10525                 :           2 :     nt = nt->real_type()->named_type();
   10526                 :       88597 :   if (nt->name() == "float32")
   10527                 :         117 :     return Type::lookup_complex_type("complex64");
   10528                 :       88480 :   else if (nt->name() == "float64")
   10529                 :       88470 :     return Type::lookup_complex_type("complex128");
   10530                 :             :   else
   10531                 :             :     return NULL;
   10532                 :             : }
   10533                 :             : 
   10534                 :             : // Return a single argument, or NULL if there isn't one.
   10535                 :             : 
   10536                 :             : Expression*
   10537                 :      593021 : Builtin_call_expression::one_arg() const
   10538                 :             : {
   10539                 :      593021 :   const Expression_list* args = this->args();
   10540                 :      593021 :   if (args == NULL || args->size() != 1)
   10541                 :             :     return NULL;
   10542                 :      593021 :   return args->front();
   10543                 :             : }
   10544                 :             : 
   10545                 :             : // A traversal class which looks for a call or receive expression.
   10546                 :             : 
   10547                 :       22825 : class Find_call_expression : public Traverse
   10548                 :             : {
   10549                 :             :  public:
   10550                 :       22825 :   Find_call_expression()
   10551                 :       22825 :     : Traverse(traverse_expressions),
   10552                 :       22825 :       found_(false)
   10553                 :             :   { }
   10554                 :             : 
   10555                 :             :   int
   10556                 :             :   expression(Expression**);
   10557                 :             : 
   10558                 :             :   bool
   10559                 :       22825 :   found()
   10560                 :       22825 :   { return this->found_; }
   10561                 :             : 
   10562                 :             :  private:
   10563                 :             :   bool found_;
   10564                 :             : };
   10565                 :             : 
   10566                 :             : int
   10567                 :       27373 : Find_call_expression::expression(Expression** pexpr)
   10568                 :             : {
   10569                 :       27373 :   Expression* expr = *pexpr;
   10570                 :       27373 :   if (!expr->is_constant()
   10571                 :       54517 :       && (expr->call_expression() != NULL
   10572                 :          52 :           || expr->receive_expression() != NULL))
   10573                 :             :     {
   10574                 :          52 :       this->found_ = true;
   10575                 :          52 :       return TRAVERSE_EXIT;
   10576                 :             :     }
   10577                 :             :   return TRAVERSE_CONTINUE;
   10578                 :             : }
   10579                 :             : 
   10580                 :             : // Return whether calling len or cap on EXPR, of array type, is a
   10581                 :             : // constant.  The language spec says "the expressions len(s) and
   10582                 :             : // cap(s) are constants if the type of s is an array or pointer to an
   10583                 :             : // array and the expression s does not contain channel receives or
   10584                 :             : // (non-constant) function calls."
   10585                 :             : 
   10586                 :             : bool
   10587                 :       22825 : Builtin_call_expression::array_len_is_constant(Expression* expr)
   10588                 :             : {
   10589                 :       91300 :   go_assert(expr->type()->deref()->array_type() != NULL
   10590                 :             :             && !expr->type()->deref()->is_slice_type());
   10591                 :       22825 :   if (expr->is_constant())
   10592                 :             :     return true;
   10593                 :       22825 :   Find_call_expression find_call;
   10594                 :       22825 :   Expression::traverse(&expr, &find_call);
   10595                 :       22825 :   return !find_call.found();
   10596                 :       22825 : }
   10597                 :             : 
   10598                 :             : // Return whether this is constant: len of a string constant, or len
   10599                 :             : // or cap of an array, or unsafe.Sizeof, unsafe.Offsetof,
   10600                 :             : // unsafe.Alignof.
   10601                 :             : 
   10602                 :             : bool
   10603                 :      508656 : Builtin_call_expression::do_is_constant() const
   10604                 :             : {
   10605                 :      508656 :   if (this->is_error_expression())
   10606                 :             :     return true;
   10607                 :      508656 :   switch (this->code_)
   10608                 :             :     {
   10609                 :      327547 :     case BUILTIN_LEN:
   10610                 :      327547 :     case BUILTIN_CAP:
   10611                 :      327547 :       {
   10612                 :      327547 :         if (this->seen_)
   10613                 :             :           return false;
   10614                 :             : 
   10615                 :      327547 :         Expression* arg = this->one_arg();
   10616                 :      327547 :         if (arg == NULL)
   10617                 :             :           return false;
   10618                 :             : 
   10619                 :             :         // We may be called before the determine_types pass.
   10620                 :      327547 :         arg->determine_type_no_context(this->gogo_);
   10621                 :             : 
   10622                 :      327547 :         Type* arg_type = arg->type();
   10623                 :      327547 :         if (arg_type->is_error())
   10624                 :             :           return true;
   10625                 :             : 
   10626                 :      327544 :         if (arg_type->points_to() != NULL
   10627                 :        9303 :             && arg_type->points_to()->array_type() != NULL
   10628                 :      336841 :             && !arg_type->points_to()->is_slice_type())
   10629                 :        9291 :           arg_type = arg_type->points_to();
   10630                 :             : 
   10631                 :      601770 :         if (arg_type->array_type() != NULL
   10632                 :      274226 :             && arg_type->array_type()->length() != NULL)
   10633                 :             :           {
   10634                 :       13524 :             this->seen_ = true;
   10635                 :       13524 :             bool ret = Builtin_call_expression::array_len_is_constant(arg);
   10636                 :       13524 :             this->seen_ = false;
   10637                 :       13524 :             return ret;
   10638                 :             :           }
   10639                 :             : 
   10640                 :      594242 :         if (this->code_ == BUILTIN_LEN && arg_type->is_string_type())
   10641                 :             :           {
   10642                 :       50230 :             this->seen_ = true;
   10643                 :       50230 :             bool ret = arg->is_constant();
   10644                 :       50230 :             this->seen_ = false;
   10645                 :       50230 :             return ret;
   10646                 :             :           }
   10647                 :             :       }
   10648                 :             :       break;
   10649                 :             : 
   10650                 :        1625 :     case BUILTIN_SIZEOF:
   10651                 :        1625 :     case BUILTIN_ALIGNOF:
   10652                 :        1625 :       return this->one_arg() != NULL;
   10653                 :             : 
   10654                 :        1378 :     case BUILTIN_OFFSETOF:
   10655                 :        1378 :       {
   10656                 :        1378 :         Expression* arg = this->one_arg();
   10657                 :        1378 :         if (arg == NULL)
   10658                 :             :           return false;
   10659                 :        1378 :         return (arg->field_reference_expression() != NULL
   10660                 :        1122 :                 || arg->classification() == Expression::EXPRESSION_SELECTOR);
   10661                 :             :       }
   10662                 :             : 
   10663                 :       49388 :     case BUILTIN_COMPLEX:
   10664                 :       49388 :       {
   10665                 :       49388 :         const Expression_list* args = this->args();
   10666                 :       49388 :         if (args != NULL && args->size() == 2)
   10667                 :       49388 :           return args->front()->is_constant() && args->back()->is_constant();
   10668                 :             :       }
   10669                 :             :       break;
   10670                 :             : 
   10671                 :        2195 :     case BUILTIN_REAL:
   10672                 :        2195 :     case BUILTIN_IMAG:
   10673                 :        2195 :       {
   10674                 :        2195 :         Expression* arg = this->one_arg();
   10675                 :        4390 :         return arg != NULL && arg->is_constant();
   10676                 :             :       }
   10677                 :             : 
   10678                 :             :     default:
   10679                 :             :       break;
   10680                 :             :     }
   10681                 :             : 
   10682                 :             :   return false;
   10683                 :             : }
   10684                 :             : 
   10685                 :             : // Return whether a builtin call is untyped.  Most builtin functions
   10686                 :             : // have a known type, but complex, real, and imag can be untyped.
   10687                 :             : 
   10688                 :             : bool
   10689                 :       56988 : Builtin_call_expression::do_is_untyped(Type** ptype) const
   10690                 :             : {
   10691                 :       56988 :   if (this->is_error_expression())
   10692                 :             :     return false;
   10693                 :             : 
   10694                 :       56988 :   switch (this->code_)
   10695                 :             :     {
   10696                 :             :     default:
   10697                 :             :       return false;
   10698                 :             : 
   10699                 :       13556 :     case BUILTIN_COMPLEX:
   10700                 :       13556 :       {
   10701                 :       13556 :         const Expression_list* args = this->args();
   10702                 :       13556 :         if (args == NULL || args->size() != 2)
   10703                 :             :           return false;
   10704                 :       13556 :         Type* dummy;
   10705                 :       13556 :         if (!args->front()->is_untyped(&dummy)
   10706                 :       13556 :             || !args->back()->is_untyped(&dummy))
   10707                 :       12065 :           return false;
   10708                 :        1491 :         *ptype = Type::make_abstract_complex_type();
   10709                 :        1491 :         return true;
   10710                 :             :       }
   10711                 :             : 
   10712                 :        1197 :     case BUILTIN_REAL:
   10713                 :        1197 :     case BUILTIN_IMAG:
   10714                 :        1197 :       {
   10715                 :        1197 :         Expression* arg = this->one_arg();
   10716                 :        1197 :         if (arg == NULL)
   10717                 :             :           return false;
   10718                 :        1197 :         if (!arg->is_untyped(ptype))
   10719                 :             :           return false;
   10720                 :          32 :         *ptype = Type::make_abstract_float_type();
   10721                 :          32 :         return true;
   10722                 :             :       }
   10723                 :             :     }
   10724                 :             : }
   10725                 :             : 
   10726                 :             : // Return a numeric constant if possible.
   10727                 :             : 
   10728                 :             : bool
   10729                 :      148018 : Builtin_call_expression::do_numeric_constant_value(Numeric_constant* nc)
   10730                 :             : {
   10731                 :      148018 :   if (this->code_ == BUILTIN_LEN
   10732                 :      148018 :       || this->code_ == BUILTIN_CAP)
   10733                 :             :     {
   10734                 :      142020 :       Expression* arg = this->one_arg();
   10735                 :      142020 :       if (arg == NULL)
   10736                 :             :         return false;
   10737                 :             : 
   10738                 :             :       // We may be called before the determine_types pass.
   10739                 :      142020 :       arg->determine_type_no_context(this->gogo_);
   10740                 :             : 
   10741                 :      142020 :       Type* arg_type = arg->type();
   10742                 :      142020 :       if (arg_type->is_error())
   10743                 :             :         return false;
   10744                 :             : 
   10745                 :      282615 :       if (this->code_ == BUILTIN_LEN && arg_type->is_string_type())
   10746                 :             :         {
   10747                 :       33992 :           std::string sval;
   10748                 :       33992 :           if (arg->string_constant_value(&sval))
   10749                 :             :             {
   10750                 :        2376 :               nc->set_unsigned_long(Type::lookup_integer_type("int"),
   10751                 :             :                                     sval.length());
   10752                 :        2376 :               return true;
   10753                 :             :             }
   10754                 :       33992 :         }
   10755                 :             : 
   10756                 :      139644 :       if (arg_type->points_to() != NULL
   10757                 :        9186 :           && arg_type->points_to()->array_type() != NULL
   10758                 :      148830 :           && !arg_type->points_to()->is_slice_type())
   10759                 :        9186 :         arg_type = arg_type->points_to();
   10760                 :             : 
   10761                 :      244725 :       if (arg_type->array_type() != NULL
   10762                 :      105081 :           && arg_type->array_type()->length() != NULL)
   10763                 :             :         {
   10764                 :       12299 :           if (this->seen_)
   10765                 :             :             return false;
   10766                 :             : 
   10767                 :       12299 :           if (!arg_type->is_error())
   10768                 :             :             {
   10769                 :       24598 :               Expression* e = arg_type->array_type()->length();
   10770                 :       12299 :               this->seen_ = true;
   10771                 :       12299 :               bool r = e->numeric_constant_value(nc);
   10772                 :       12299 :               this->seen_ = false;
   10773                 :       12299 :               if (r)
   10774                 :             :                 {
   10775                 :       12299 :                   if (!nc->set_type(Type::lookup_integer_type("int"), false,
   10776                 :             :                                     this->location()))
   10777                 :           0 :                     r = false;
   10778                 :             :                 }
   10779                 :       12299 :               return r;
   10780                 :             :             }
   10781                 :             :         }
   10782                 :             :     }
   10783                 :             :   else if (this->code_ == BUILTIN_SIZEOF
   10784                 :             :            || this->code_ == BUILTIN_ALIGNOF)
   10785                 :             :     {
   10786                 :        1870 :       Expression* arg = this->one_arg();
   10787                 :        1870 :       if (arg == NULL)
   10788                 :             :         return false;
   10789                 :             : 
   10790                 :             :       // We may be called before the determine_types pass.
   10791                 :        1870 :       arg->determine_type_no_context(this->gogo_);
   10792                 :             : 
   10793                 :        1870 :       Type* arg_type = arg->type();
   10794                 :        1870 :       if (arg_type->is_error())
   10795                 :             :         return false;
   10796                 :        1870 :       if (arg_type->is_abstract())
   10797                 :           0 :         arg_type = arg_type->make_non_abstract_type();
   10798                 :        1870 :       if (this->seen_)
   10799                 :             :         return false;
   10800                 :             : 
   10801                 :        1870 :       int64_t ret;
   10802                 :        1870 :       if (this->code_ == BUILTIN_SIZEOF)
   10803                 :             :         {
   10804                 :        1859 :           this->seen_ = true;
   10805                 :        1859 :           bool ok = arg_type->backend_type_size(this->gogo_, &ret);
   10806                 :        1859 :           this->seen_ = false;
   10807                 :        1859 :           if (!ok)
   10808                 :             :             return false;
   10809                 :             :         }
   10810                 :          11 :       else if (this->code_ == BUILTIN_ALIGNOF)
   10811                 :             :         {
   10812                 :          11 :           bool ok;
   10813                 :          11 :           this->seen_ = true;
   10814                 :          11 :           if (arg->field_reference_expression() == NULL)
   10815                 :           5 :             ok = arg_type->backend_type_align(this->gogo_, &ret);
   10816                 :             :           else
   10817                 :             :             {
   10818                 :             :               // Calling unsafe.Alignof(s.f) returns the alignment of
   10819                 :             :               // the type of f when it is used as a field in a struct.
   10820                 :           6 :               ok = arg_type->backend_type_field_align(this->gogo_, &ret);
   10821                 :             :             }
   10822                 :          11 :           this->seen_ = false;
   10823                 :          11 :           if (!ok)
   10824                 :             :             return false;
   10825                 :             :         }
   10826                 :             :       else
   10827                 :           0 :         go_unreachable();
   10828                 :             : 
   10829                 :        1870 :       mpz_t zval;
   10830                 :        1870 :       set_mpz_from_int64(&zval, ret);
   10831                 :        1870 :       nc->set_int(Type::lookup_integer_type("uintptr"), zval);
   10832                 :        1870 :       mpz_clear(zval);
   10833                 :        1870 :       return true;
   10834                 :             :     }
   10835                 :             :   else if (this->code_ == BUILTIN_OFFSETOF)
   10836                 :             :     {
   10837                 :         804 :       Expression* arg = this->one_arg();
   10838                 :         804 :       if (arg == NULL)
   10839                 :             :         return false;
   10840                 :             : 
   10841                 :             :       // We may be called before the determine_types pass.
   10842                 :         804 :       arg->determine_type_no_context(this->gogo_);
   10843                 :             : 
   10844                 :         804 :       Field_reference_expression* farg = arg->field_reference_expression();
   10845                 :         256 :       if (farg == NULL)
   10846                 :             :         return false;
   10847                 :         256 :       if (this->seen_)
   10848                 :             :         return false;
   10849                 :             : 
   10850                 :             :       int64_t total_offset = 0;
   10851                 :         966 :       while (true)
   10852                 :             :         {
   10853                 :         611 :           Expression* struct_expr = farg->expr();
   10854                 :         611 :           Type* st = struct_expr->type();
   10855                 :         611 :           if (st->struct_type() == NULL)
   10856                 :           0 :             return false;
   10857                 :         611 :           if (st->named_type() != NULL)
   10858                 :         584 :             st->named_type()->convert(this->gogo_);
   10859                 :         611 :           if (st->is_error_type())
   10860                 :             :             {
   10861                 :           0 :               go_assert(saw_errors());
   10862                 :             :               return false;
   10863                 :             :             }
   10864                 :         611 :           int64_t offset;
   10865                 :         611 :           this->seen_ = true;
   10866                 :        1222 :           bool ok = st->struct_type()->backend_field_offset(this->gogo_,
   10867                 :             :                                                             farg->field_index(),
   10868                 :             :                                                             &offset);
   10869                 :         611 :           this->seen_ = false;
   10870                 :         611 :           if (!ok)
   10871                 :             :             return false;
   10872                 :         611 :           total_offset += offset;
   10873                 :         611 :           if (farg->implicit() && struct_expr->field_reference_expression() != NULL)
   10874                 :             :             {
   10875                 :             :               // Go up until we reach the original base.
   10876                 :         355 :               farg = struct_expr->field_reference_expression();
   10877                 :         355 :               continue;
   10878                 :             :             }
   10879                 :         256 :           break;
   10880                 :             :         }
   10881                 :         256 :       mpz_t zval;
   10882                 :         256 :       set_mpz_from_int64(&zval, total_offset);
   10883                 :         256 :       nc->set_int(Type::lookup_integer_type("uintptr"), zval);
   10884                 :         256 :       mpz_clear(zval);
   10885                 :         256 :       return true;
   10886                 :             :     }
   10887                 :             :   else if (this->code_ == BUILTIN_REAL || this->code_ == BUILTIN_IMAG)
   10888                 :             :     {
   10889                 :         636 :       Expression* arg = this->one_arg();
   10890                 :         636 :       if (arg == NULL)
   10891                 :             :         return false;
   10892                 :             : 
   10893                 :         636 :       Numeric_constant argnc;
   10894                 :         636 :       if (!arg->numeric_constant_value(&argnc))
   10895                 :             :         return false;
   10896                 :             : 
   10897                 :          42 :       mpc_t val;
   10898                 :          42 :       if (!argnc.to_complex(&val))
   10899                 :             :         return false;
   10900                 :             : 
   10901                 :          42 :       Type* type = Builtin_call_expression::real_imag_type(argnc.type());
   10902                 :          42 :       if (this->code_ == BUILTIN_REAL)
   10903                 :          22 :         nc->set_float(type, mpc_realref(val));
   10904                 :             :       else
   10905                 :          20 :         nc->set_float(type, mpc_imagref(val));
   10906                 :          42 :       mpc_clear(val);
   10907                 :          42 :       return true;
   10908                 :         636 :     }
   10909                 :             :   else if (this->code_ == BUILTIN_COMPLEX)
   10910                 :             :     {
   10911                 :        1528 :       const Expression_list* args = this->args();
   10912                 :        1528 :       if (args == NULL || args->size() != 2)
   10913                 :             :         return false;
   10914                 :             : 
   10915                 :        1528 :       Numeric_constant rnc;
   10916                 :        1528 :       if (!args->front()->numeric_constant_value(&rnc))
   10917                 :             :         return false;
   10918                 :        1527 :       Numeric_constant inc;
   10919                 :        1527 :       if (!args->back()->numeric_constant_value(&inc))
   10920                 :             :         return false;
   10921                 :             : 
   10922                 :        1527 :       if (rnc.type() != NULL
   10923                 :        1527 :           && !rnc.type()->is_abstract()
   10924                 :        1506 :           && inc.type() != NULL
   10925                 :        1506 :           && !inc.type()->is_abstract()
   10926                 :        3033 :           && !Type::are_identical(rnc.type(), inc.type(),
   10927                 :             :                                   Type::COMPARE_ERRORS | Type::COMPARE_TAGS,
   10928                 :             :                                   NULL))
   10929                 :             :         return false;
   10930                 :             : 
   10931                 :        1527 :       mpfr_t r;
   10932                 :        1527 :       if (!rnc.to_float(&r))
   10933                 :             :         return false;
   10934                 :        1527 :       mpfr_t i;
   10935                 :        1527 :       if (!inc.to_float(&i))
   10936                 :             :         {
   10937                 :           0 :           mpfr_clear(r);
   10938                 :           0 :           return false;
   10939                 :             :         }
   10940                 :             : 
   10941                 :        1527 :       Type* arg_type = rnc.type();
   10942                 :        1527 :       if (arg_type == NULL || arg_type->is_abstract())
   10943                 :          21 :         arg_type = inc.type();
   10944                 :             : 
   10945                 :        1527 :       mpc_t val;
   10946                 :        1527 :       mpc_init2(val, mpc_precision);
   10947                 :        1527 :       mpc_set_fr_fr(val, r, i, MPC_RNDNN);
   10948                 :        1527 :       mpfr_clear(r);
   10949                 :        1527 :       mpfr_clear(i);
   10950                 :             : 
   10951                 :        1527 :       Type* type = Builtin_call_expression::complex_type(arg_type);
   10952                 :        1527 :       nc->set_complex(type, val);
   10953                 :             : 
   10954                 :        1527 :       mpc_clear(val);
   10955                 :             : 
   10956                 :        1527 :       return true;
   10957                 :        1528 :     }
   10958                 :             : 
   10959                 :             :   return false;
   10960                 :             : }
   10961                 :             : 
   10962                 :             : // Give an error if we are discarding the value of an expression which
   10963                 :             : // should not normally be discarded.  We don't give an error for
   10964                 :             : // discarding the value of an ordinary function call, but we do for
   10965                 :             : // builtin functions, purely for consistency with the gc compiler.
   10966                 :             : 
   10967                 :             : bool
   10968                 :       34613 : Builtin_call_expression::do_discarding_value()
   10969                 :             : {
   10970                 :       34613 :   switch (this->code_)
   10971                 :             :     {
   10972                 :           0 :     case BUILTIN_INVALID:
   10973                 :           0 :     default:
   10974                 :           0 :       go_unreachable();
   10975                 :             : 
   10976                 :          71 :     case BUILTIN_APPEND:
   10977                 :          71 :     case BUILTIN_CAP:
   10978                 :          71 :     case BUILTIN_COMPLEX:
   10979                 :          71 :     case BUILTIN_IMAG:
   10980                 :          71 :     case BUILTIN_LEN:
   10981                 :          71 :     case BUILTIN_MAKE:
   10982                 :          71 :     case BUILTIN_NEW:
   10983                 :          71 :     case BUILTIN_REAL:
   10984                 :          71 :     case BUILTIN_ADD:
   10985                 :          71 :     case BUILTIN_ALIGNOF:
   10986                 :          71 :     case BUILTIN_OFFSETOF:
   10987                 :          71 :     case BUILTIN_SIZEOF:
   10988                 :          71 :     case BUILTIN_SLICE:
   10989                 :          71 :       this->unused_value_error();
   10990                 :          71 :       return false;
   10991                 :             : 
   10992                 :             :     case BUILTIN_CLOSE:
   10993                 :             :     case BUILTIN_COPY:
   10994                 :             :     case BUILTIN_DELETE:
   10995                 :             :     case BUILTIN_PANIC:
   10996                 :             :     case BUILTIN_PRINT:
   10997                 :             :     case BUILTIN_PRINTLN:
   10998                 :             :     case BUILTIN_RECOVER:
   10999                 :             :       return true;
   11000                 :             :     }
   11001                 :             : }
   11002                 :             : 
   11003                 :             : // Return the type.
   11004                 :             : 
   11005                 :             : Type*
   11006                 :     1451500 : Builtin_call_expression::do_type()
   11007                 :             : {
   11008                 :     1451500 :   if (this->is_error_expression())
   11009                 :        1941 :     return Type::make_error_type();
   11010                 :             : 
   11011                 :     1449559 :   Type* type = this->type();
   11012                 :     1449559 :   if (type != NULL)
   11013                 :             :     return type;
   11014                 :             : 
   11015                 :     1448018 :   switch (this->code_)
   11016                 :             :     {
   11017                 :           0 :     case BUILTIN_INVALID:
   11018                 :           0 :     default:
   11019                 :           0 :       return Type::make_error_type();
   11020                 :             : 
   11021                 :       14123 :     case BUILTIN_NEW:
   11022                 :       14123 :       {
   11023                 :       14123 :         const Expression_list* args = this->args();
   11024                 :       14123 :         if (args == NULL || args->empty())
   11025                 :           0 :           return Type::make_error_type();
   11026                 :       14123 :         return Type::make_pointer_type(args->front()->type());
   11027                 :             :       }
   11028                 :             : 
   11029                 :       24732 :     case BUILTIN_MAKE:
   11030                 :       24732 :       {
   11031                 :       24732 :         const Expression_list* args = this->args();
   11032                 :       24732 :         if (args == NULL || args->empty())
   11033                 :           0 :           return Type::make_error_type();
   11034                 :       24732 :         return args->front()->type();
   11035                 :             :       }
   11036                 :             : 
   11037                 :     1142751 :     case BUILTIN_CAP:
   11038                 :     1142751 :     case BUILTIN_COPY:
   11039                 :     1142751 :     case BUILTIN_LEN:
   11040                 :     1142751 :       return Type::lookup_integer_type("int");
   11041                 :             : 
   11042                 :        3750 :     case BUILTIN_ALIGNOF:
   11043                 :        3750 :     case BUILTIN_OFFSETOF:
   11044                 :        3750 :     case BUILTIN_SIZEOF:
   11045                 :        3750 :       return Type::lookup_integer_type("uintptr");
   11046                 :             : 
   11047                 :       75676 :     case BUILTIN_CLOSE:
   11048                 :       75676 :     case BUILTIN_DELETE:
   11049                 :       75676 :     case BUILTIN_PANIC:
   11050                 :       75676 :     case BUILTIN_PRINT:
   11051                 :       75676 :     case BUILTIN_PRINTLN:
   11052                 :       75676 :       return Type::make_void_type();
   11053                 :             : 
   11054                 :        6835 :     case BUILTIN_RECOVER:
   11055                 :        6835 :       return Type::make_empty_interface_type(Linemap::predeclared_location());
   11056                 :             : 
   11057                 :       85298 :     case BUILTIN_APPEND:
   11058                 :       85298 :       {
   11059                 :       85298 :         const Expression_list* args = this->args();
   11060                 :       85298 :         if (args == NULL || args->empty())
   11061                 :           0 :           return Type::make_error_type();
   11062                 :       85298 :         Type *ret = args->front()->type();
   11063                 :       85298 :         if (!ret->is_slice_type())
   11064                 :           2 :           return Type::make_error_type();
   11065                 :             :         return ret;
   11066                 :             :       }
   11067                 :             : 
   11068                 :        8427 :     case BUILTIN_REAL:
   11069                 :        8427 :     case BUILTIN_IMAG:
   11070                 :        8427 :       {
   11071                 :        8427 :         Expression* arg = this->one_arg();
   11072                 :        8427 :         if (arg == NULL)
   11073                 :           0 :           return Type::make_error_type();
   11074                 :        8427 :         Type* t = arg->type();
   11075                 :        8427 :         if (t->is_abstract())
   11076                 :           0 :           t = t->make_non_abstract_type();
   11077                 :        8427 :         t = Builtin_call_expression::real_imag_type(t);
   11078                 :        8427 :         if (t == NULL)
   11079                 :           4 :           t = Type::make_error_type();
   11080                 :             :         return t;
   11081                 :             :       }
   11082                 :             : 
   11083                 :       86335 :     case BUILTIN_COMPLEX:
   11084                 :       86335 :       {
   11085                 :       86335 :         const Expression_list* args = this->args();
   11086                 :       86335 :         if (args == NULL || args->size() != 2)
   11087                 :           0 :           return Type::make_error_type();
   11088                 :       86335 :         Type* t = args->front()->type();
   11089                 :       86335 :         if (t->is_abstract())
   11090                 :             :           {
   11091                 :           0 :             t = args->back()->type();
   11092                 :           0 :             if (t->is_abstract())
   11093                 :           0 :               t = t->make_non_abstract_type();
   11094                 :             :           }
   11095                 :       86335 :         t = Builtin_call_expression::complex_type(t);
   11096                 :       86335 :         if (t == NULL)
   11097                 :           1 :           t = Type::make_error_type();
   11098                 :             :         return t;
   11099                 :             :       }
   11100                 :             : 
   11101                 :          41 :     case BUILTIN_ADD:
   11102                 :          41 :       return Type::make_pointer_type(Type::make_void_type());
   11103                 :             : 
   11104                 :          50 :     case BUILTIN_SLICE:
   11105                 :          50 :       const Expression_list* args = this->args();
   11106                 :          50 :       if (args == NULL || args->size() != 2)
   11107                 :           0 :         return Type::make_error_type();
   11108                 :          50 :       Type* pt = args->front()->type()->points_to();
   11109                 :          50 :       if (pt == NULL)
   11110                 :           0 :         return Type::make_error_type();
   11111                 :          50 :       return Type::make_array_type(pt, NULL);
   11112                 :             :     }
   11113                 :             : }
   11114                 :             : 
   11115                 :             : // Determine the type.
   11116                 :             : 
   11117                 :             : void
   11118                 :      295512 : Builtin_call_expression::do_determine_type(Gogo* gogo,
   11119                 :             :                                            const Type_context* context)
   11120                 :             : {
   11121                 :      295512 :   if (!this->determining_types())
   11122                 :             :     return;
   11123                 :             : 
   11124                 :      226647 :   this->fn()->determine_type_no_context(gogo);
   11125                 :             : 
   11126                 :      226647 :   this->simplify_multiple_results(gogo);
   11127                 :             : 
   11128                 :      226647 :   const Expression_list* args = this->args();
   11129                 :             : 
   11130                 :      226647 :   bool is_print;
   11131                 :      226647 :   Type* arg_type = NULL;
   11132                 :      226647 :   Type* trailing_arg_types = NULL;
   11133                 :      226647 :   switch (this->code_)
   11134                 :             :     {
   11135                 :       15117 :     case BUILTIN_MAKE:
   11136                 :       15117 :       trailing_arg_types = Type::lookup_integer_type("int");
   11137                 :       15117 :       is_print = false;
   11138                 :       15117 :       break;
   11139                 :             : 
   11140                 :             :     case BUILTIN_PRINT:
   11141                 :             :     case BUILTIN_PRINTLN:
   11142                 :             :       // Do not force a large integer constant to "int".
   11143                 :             :       is_print = true;
   11144                 :             :       break;
   11145                 :             : 
   11146                 :        1048 :     case BUILTIN_REAL:
   11147                 :        1048 :     case BUILTIN_IMAG:
   11148                 :        1048 :       {
   11149                 :             :         // We need the argument to determine the type, so check it now
   11150                 :             :         // before any call to the do_type method.
   11151                 :        1048 :         const Expression_list* args = this->args();
   11152                 :        1048 :         if (args == NULL || args->size() < 1)
   11153                 :             :           {
   11154                 :           0 :             this->report_error(_("not enough arguments"));
   11155                 :           0 :             return;
   11156                 :             :           }
   11157                 :        1048 :         else if (args->size() > 1)
   11158                 :             :           {
   11159                 :           0 :             this->report_error(_("too many arguments"));
   11160                 :           0 :             return;
   11161                 :             :           }
   11162                 :             : 
   11163                 :        1048 :         Type* dummy;
   11164                 :        1048 :         if (context->type != NULL
   11165                 :         764 :             && context->type->is_numeric_type()
   11166                 :        1803 :             && this->is_untyped(&dummy))
   11167                 :             :           {
   11168                 :          28 :             Type* type = context->type;
   11169                 :          28 :             if (type->is_abstract() && !context->may_be_abstract)
   11170                 :           0 :               type = type->make_non_abstract_type();
   11171                 :          28 :             this->set_type(type);
   11172                 :             :           }
   11173                 :        1020 :         else if (context->may_be_abstract && this->is_constant())
   11174                 :           5 :           this->set_type(Type::make_abstract_float_type());
   11175                 :             : 
   11176                 :        1048 :         arg_type = Builtin_call_expression::complex_type(context->type);
   11177                 :        1048 :         if (arg_type == NULL)
   11178                 :             :           {
   11179                 :         301 :             if (context->may_be_abstract)
   11180                 :           6 :               arg_type = Type::make_abstract_complex_type();
   11181                 :             :             else
   11182                 :         295 :               arg_type = Type::lookup_complex_type("complex128");
   11183                 :             :           }
   11184                 :             : 
   11185                 :        1048 :         if (!args->front()->is_untyped(&dummy))
   11186                 :             :           {
   11187                 :         994 :             Type_context subcontext(arg_type, context->may_be_abstract);
   11188                 :         994 :             args->front()->determine_type(gogo, &subcontext);
   11189                 :             :           }
   11190                 :             : 
   11191                 :        1048 :         is_print = false;
   11192                 :             :       }
   11193                 :        1048 :       break;
   11194                 :             : 
   11195                 :       13710 :     case BUILTIN_COMPLEX:
   11196                 :       13710 :       {
   11197                 :             :         // We need the arguments to determine the type, so check them
   11198                 :             :         // now before any call to the do_type method.
   11199                 :       13710 :         const Expression_list* args = this->args();
   11200                 :       13710 :         if (args == NULL || args->size() < 2)
   11201                 :             :           {
   11202                 :           4 :             this->report_error(_("not enough arguments"));
   11203                 :           9 :             return;
   11204                 :             :           }
   11205                 :       13706 :         else if (args->size() > 2)
   11206                 :             :           {
   11207                 :           1 :             this->report_error(_("too many arguments"));
   11208                 :           1 :             return;
   11209                 :             :           }
   11210                 :             : 
   11211                 :       13705 :         Type* dummy;
   11212                 :       13705 :         if (context->type != NULL
   11213                 :       13615 :             && context->type->is_numeric_type()
   11214                 :       27259 :             && this->is_untyped(&dummy))
   11215                 :             :           {
   11216                 :        1490 :             Type* type = context->type;
   11217                 :        1490 :             if (type->is_abstract() && !context->may_be_abstract)
   11218                 :           0 :               type = type->make_non_abstract_type();
   11219                 :        1490 :             this->set_type(type);
   11220                 :             :           }
   11221                 :       12215 :         else if (context->may_be_abstract && this->is_constant())
   11222                 :           6 :           this->set_type(Type::make_abstract_complex_type());
   11223                 :             : 
   11224                 :             :         // For the complex function the type of one operand can
   11225                 :             :         // determine the type of the other, as in a binary expression.
   11226                 :       13705 :         arg_type = Builtin_call_expression::real_imag_type(context->type);
   11227                 :       13705 :         if (arg_type == NULL)
   11228                 :             :           {
   11229                 :         153 :             if (context->may_be_abstract)
   11230                 :           6 :               arg_type = Type::make_abstract_float_type();
   11231                 :             :             else
   11232                 :         147 :               arg_type = Type::lookup_float_type("float64");
   11233                 :             :           }
   11234                 :             : 
   11235                 :       13705 :         Type_context subcontext(arg_type, context->may_be_abstract);
   11236                 :       13705 :         if (!args->front()->is_untyped(&dummy))
   11237                 :             :           {
   11238                 :       11127 :             args->front()->determine_type(gogo, &subcontext);
   11239                 :       11127 :             arg_type = args->front()->type();
   11240                 :             :           }
   11241                 :        2578 :         else if (!args->back()->is_untyped(&dummy))
   11242                 :             :           {
   11243                 :        1054 :             args->back()->determine_type(gogo, &subcontext);
   11244                 :        1054 :             arg_type = args->back()->type();
   11245                 :             :           }
   11246                 :             : 
   11247                 :       13705 :         is_print = false;
   11248                 :             :       }
   11249                 :       13705 :       break;
   11250                 :             : 
   11251                 :       15743 :     case BUILTIN_APPEND:
   11252                 :       15743 :       if (!this->is_varargs()
   11253                 :       12154 :           && args != NULL
   11254                 :       27897 :           && !args->empty())
   11255                 :             :         {
   11256                 :       12154 :           args->front()->determine_type_no_context(gogo);
   11257                 :       12154 :           if (args->front()->type()->is_slice_type())
   11258                 :       12152 :             trailing_arg_types =
   11259                 :       24304 :               args->front()->type()->array_type()->element_type();
   11260                 :             :         }
   11261                 :             :       is_print = false;
   11262                 :             :       break;
   11263                 :             : 
   11264                 :          18 :     case BUILTIN_ADD:
   11265                 :          18 :     case BUILTIN_SLICE:
   11266                 :             :       // Both unsafe.Add and unsafe.Slice take two arguments, and the
   11267                 :             :       // second arguments defaults to "int".
   11268                 :          18 :       if (args != NULL && args->size() == 2)
   11269                 :             :         {
   11270                 :          18 :           if (this->code_ == BUILTIN_SLICE)
   11271                 :           9 :             args->front()->determine_type_no_context(gogo);
   11272                 :             :           else
   11273                 :             :             {
   11274                 :           9 :               Type* pointer = Type::make_pointer_type(Type::make_void_type());
   11275                 :           9 :               Type_context subcontext(pointer, false);
   11276                 :           9 :               args->front()->determine_type(gogo, &subcontext);
   11277                 :             :             }
   11278                 :          18 :           Type* int_type = Type::lookup_integer_type("int");
   11279                 :          18 :           Type_context subcontext(int_type, false);
   11280                 :          18 :           args->back()->determine_type(gogo, &subcontext);
   11281                 :          18 :           return;
   11282                 :             :         }
   11283                 :             :       is_print = false;
   11284                 :             :       break;
   11285                 :             : 
   11286                 :         943 :     case BUILTIN_DELETE:
   11287                 :         943 :       if (args != NULL && args->size() == 2)
   11288                 :             :         {
   11289                 :         940 :           args->front()->determine_type_no_context(gogo);
   11290                 :         940 :           Map_type* mt = args->front()->type()->map_type();
   11291                 :         939 :           if (mt != NULL)
   11292                 :         939 :             trailing_arg_types = mt->key_type();
   11293                 :             :         }
   11294                 :             :       is_print = false;
   11295                 :             :       break;
   11296                 :             : 
   11297                 :             :     default:
   11298                 :      167477 :       is_print = false;
   11299                 :             :       break;
   11300                 :             :     }
   11301                 :             : 
   11302                 :      226624 :   if (args != NULL)
   11303                 :             :     {
   11304                 :      522945 :       for (Expression_list::const_iterator pa = args->begin();
   11305                 :      522945 :            pa != args->end();
   11306                 :      297233 :            ++pa)
   11307                 :             :         {
   11308                 :      297233 :           Type_context subcontext;
   11309                 :      297233 :           subcontext.type = arg_type;
   11310                 :             : 
   11311                 :      297233 :           if (is_print && (*pa)->is_constant())
   11312                 :             :             {
   11313                 :             :               // We want to print large constants, we so can't just
   11314                 :             :               // use the appropriate nonabstract type.  Use uint64 for
   11315                 :             :               // an integer if we know it is nonnegative, otherwise
   11316                 :             :               // use int64 for a integer, otherwise use float64 for a
   11317                 :             :               // float or complex128 for a complex.
   11318                 :       26478 :               Type* atype;
   11319                 :       26478 :               if ((*pa)->is_untyped(&atype))
   11320                 :             :                 {
   11321                 :       25189 :                   Type* want_type = NULL;
   11322                 :       25189 :                   if (atype->integer_type() != NULL)
   11323                 :             :                     {
   11324                 :         505 :                       Numeric_constant nc;
   11325                 :         505 :                       if (this->numeric_constant_value(&nc))
   11326                 :             :                         {
   11327                 :           0 :                           mpz_t val;
   11328                 :           0 :                           if (nc.to_int(&val))
   11329                 :             :                             {
   11330                 :           0 :                               if (mpz_sgn(val) >= 0)
   11331                 :           0 :                                 want_type = Type::lookup_integer_type("uint64");
   11332                 :           0 :                               mpz_clear(val);
   11333                 :             :                             }
   11334                 :             :                         }
   11335                 :           0 :                       if (want_type == NULL)
   11336                 :         505 :                         want_type = Type::lookup_integer_type("int64");
   11337                 :         505 :                     }
   11338                 :       24684 :                   else if (atype->float_type() != NULL)
   11339                 :          56 :                     want_type = Type::lookup_float_type("float64");
   11340                 :       24628 :                   else if (atype->complex_type() != NULL)
   11341                 :          14 :                     want_type = Type::lookup_complex_type("complex128");
   11342                 :       24614 :                   else if (atype->is_abstract_string_type())
   11343                 :       24588 :                     want_type = Type::lookup_string_type();
   11344                 :          26 :                   else if (atype->is_abstract_boolean_type())
   11345                 :          26 :                     want_type = Type::lookup_bool_type();
   11346                 :             :                   else
   11347                 :           0 :                     go_unreachable();
   11348                 :       25189 :                   subcontext.type = want_type;
   11349                 :             :                 }
   11350                 :             :             }
   11351                 :             : 
   11352                 :      297233 :           (*pa)->determine_type(gogo, &subcontext);
   11353                 :             : 
   11354                 :      297233 :           if (trailing_arg_types != NULL)
   11355                 :             :             {
   11356                 :       28208 :               arg_type = trailing_arg_types;
   11357                 :       28208 :               trailing_arg_types = NULL;
   11358                 :             :             }
   11359                 :             :         }
   11360                 :             :     }
   11361                 :             : }
   11362                 :             : 
   11363                 :             : // If there is exactly one argument, return true.  Otherwise give an
   11364                 :             : // error message and return false.
   11365                 :             : 
   11366                 :             : bool
   11367                 :       80123 : Builtin_call_expression::check_one_arg()
   11368                 :             : {
   11369                 :       80123 :   const Expression_list* args = this->args();
   11370                 :       80123 :   if (args == NULL || args->size() < 1)
   11371                 :             :     {
   11372                 :           0 :       this->report_error(_("not enough arguments"));
   11373                 :           0 :       return false;
   11374                 :             :     }
   11375                 :       80123 :   else if (args->size() > 1)
   11376                 :             :     {
   11377                 :           0 :       this->report_error(_("too many arguments"));
   11378                 :           0 :       return false;
   11379                 :             :     }
   11380                 :       80123 :   if (args->front()->is_error_expression()
   11381                 :       80123 :       || args->front()->type()->is_error())
   11382                 :             :     {
   11383                 :           5 :       this->set_is_error();
   11384                 :           5 :       return false;
   11385                 :             :     }
   11386                 :             :   return true;
   11387                 :             : }
   11388                 :             : 
   11389                 :             : // Check argument types for a builtin function.
   11390                 :             : 
   11391                 :             : void
   11392                 :      145801 : Builtin_call_expression::do_check_types(Gogo* gogo)
   11393                 :             : {
   11394                 :      145801 :   if (this->is_error_expression())
   11395                 :             :     return;
   11396                 :             : 
   11397                 :      145724 :   if (this->is_varargs() && this->code_ != BUILTIN_APPEND)
   11398                 :             :     {
   11399                 :           8 :       go_error_at(this->location(),
   11400                 :             :                   "invalid use of %<...%> with built-in function");
   11401                 :           8 :       this->set_is_error();
   11402                 :           8 :       return;
   11403                 :             :     }
   11404                 :             : 
   11405                 :      145716 :   switch (this->code_)
   11406                 :             :     {
   11407                 :             :     case BUILTIN_INVALID:
   11408                 :             :       return;
   11409                 :             : 
   11410                 :        7322 :     case BUILTIN_NEW:
   11411                 :        7322 :       if (this->check_one_arg())
   11412                 :             :         {
   11413                 :        7322 :           Expression* arg = this->one_arg();
   11414                 :        7322 :           if (!arg->is_type_expression())
   11415                 :             :             {
   11416                 :           0 :               go_error_at(arg->location(), "expected type");
   11417                 :           0 :               this->set_is_error();
   11418                 :             :             }
   11419                 :             :         }
   11420                 :             :       break;
   11421                 :             : 
   11422                 :       15106 :     case BUILTIN_MAKE:
   11423                 :       15106 :       {
   11424                 :       15106 :         const Expression_list* args = this->args();
   11425                 :       15106 :         if (args == NULL || args->size() < 1)
   11426                 :             :           {
   11427                 :           0 :             this->report_error(_("not enough arguments"));
   11428                 :           0 :             return;
   11429                 :             :           }
   11430                 :             : 
   11431                 :       15106 :         Expression* first_arg = args->front();
   11432                 :       15106 :         if (!first_arg->is_type_expression())
   11433                 :             :           {
   11434                 :           0 :             go_error_at(first_arg->location(), "expected type");
   11435                 :           0 :             this->set_is_error();
   11436                 :           0 :             return;
   11437                 :             :           }
   11438                 :             : 
   11439                 :       15106 :         Type* type = first_arg->type();
   11440                 :       15106 :         if (!type->in_heap())
   11441                 :           0 :           go_error_at(first_arg->location(),
   11442                 :             :                       "cannot make slice of go:notinheap type");
   11443                 :             : 
   11444                 :       15106 :         bool is_slice = type->is_slice_type();
   11445                 :       15106 :         if (!is_slice
   11446                 :        2766 :             && type->map_type() == NULL
   11447                 :       21721 :             && type->channel_type() == NULL)
   11448                 :             :           {
   11449                 :           0 :             this->report_error(_("invalid type for make function"));
   11450                 :           0 :             return;
   11451                 :             :           }
   11452                 :             : 
   11453                 :       15106 :         Expression_list::const_iterator parg = args->begin();
   11454                 :       15106 :         ++parg;
   11455                 :       15106 :         if (parg == args->end())
   11456                 :             :           {
   11457                 :        4935 :             if (is_slice)
   11458                 :             :               {
   11459                 :           0 :                 this->report_error(_("length required when "
   11460                 :             :                                      "allocating a slice"));
   11461                 :           0 :                 return;
   11462                 :             :               }
   11463                 :             :           }
   11464                 :             :         else
   11465                 :             :           {
   11466                 :       10171 :             if ((*parg)->type()->integer_type() == NULL)
   11467                 :             :               {
   11468                 :           8 :                 go_error_at((*parg)->location(),
   11469                 :             :                             "non-integer len argument in make");
   11470                 :           8 :                 return;
   11471                 :             :               }
   11472                 :       10163 :             ++parg;
   11473                 :             : 
   11474                 :       10163 :             if (is_slice && parg != args->end())
   11475                 :             :               {
   11476                 :        1716 :                 if ((*parg)->type()->integer_type() == NULL)
   11477                 :             :                   {
   11478                 :           5 :                     go_error_at((*parg)->location(),
   11479                 :             :                                 "non-integer cap argument in make");
   11480                 :           5 :                     return;
   11481                 :             :                   }
   11482                 :        1711 :                 ++parg;
   11483                 :             :               }
   11484                 :             :           }
   11485                 :             : 
   11486                 :       15093 :         if (parg != args->end())
   11487                 :             :           {
   11488                 :           1 :             this->report_error(_("too many arguments to make"));
   11489                 :           1 :             return;
   11490                 :             :           }
   11491                 :             :       }
   11492                 :             :       break;
   11493                 :             : 
   11494                 :         892 :     case BUILTIN_DELETE:
   11495                 :         892 :       {
   11496                 :         892 :         const Expression_list* args = this->args();
   11497                 :         892 :         if (args == NULL || args->size() < 2)
   11498                 :           2 :           this->report_error(_("not enough arguments"));
   11499                 :         890 :         else if (args->size() > 2)
   11500                 :           1 :           this->report_error(_("too many arguments"));
   11501                 :         889 :         else if (args->front()->type()->map_type() == NULL)
   11502                 :           1 :           this->report_error(_("argument 1 must be a map"));
   11503                 :             :         else
   11504                 :             :           {
   11505                 :         888 :             Type* key_type =
   11506                 :        1776 :               args->front()->type()->map_type()->key_type();
   11507                 :         888 :             Expression_list::iterator pa = this->args()->begin();
   11508                 :         888 :             pa++;
   11509                 :         888 :             Type* arg_type = (*pa)->type();
   11510                 :         888 :             std::string reason;
   11511                 :         888 :             if (!Type::are_assignable(key_type, arg_type, &reason))
   11512                 :             :               {
   11513                 :           0 :                 if (reason.empty())
   11514                 :           0 :                   go_error_at(this->location(),
   11515                 :             :                               "argument 2 has incompatible type");
   11516                 :             :                 else
   11517                 :           0 :                   go_error_at(this->location(),
   11518                 :             :                               "argument 2 has incompatible type (%s)",
   11519                 :             :                               reason.c_str());
   11520                 :           0 :                 this->set_is_error();
   11521                 :             :               }
   11522                 :         888 :           }
   11523                 :             :       }
   11524                 :             :       break;
   11525                 :             : 
   11526                 :       54786 :     case BUILTIN_LEN:
   11527                 :       54786 :     case BUILTIN_CAP:
   11528                 :       54786 :       {
   11529                 :             :         // The single argument may be either a string or an array or a
   11530                 :             :         // map or a channel, or a pointer to a closed array.
   11531                 :       54786 :         if (this->check_one_arg())
   11532                 :             :           {
   11533                 :       54783 :             Type* arg_type = this->one_arg()->type();
   11534                 :       54783 :             if (arg_type->points_to() != NULL
   11535                 :         204 :                 && arg_type->points_to()->array_type() != NULL
   11536                 :       54981 :                 && !arg_type->points_to()->is_slice_type())
   11537                 :         192 :               arg_type = arg_type->points_to();
   11538                 :       54783 :             if (this->code_ == BUILTIN_CAP)
   11539                 :             :               {
   11540                 :         944 :                 if (!arg_type->is_error()
   11541                 :          54 :                     && arg_type->array_type() == NULL
   11542                 :         944 :                     && arg_type->channel_type() == NULL)
   11543                 :           3 :                   this->report_error(_("argument must be array or slice "
   11544                 :             :                                        "or channel"));
   11545                 :             :               }
   11546                 :             :             else
   11547                 :             :               {
   11548                 :       53839 :                 if (!arg_type->is_error()
   11549                 :       53839 :                     && !arg_type->is_string_type()
   11550                 :        1230 :                     && arg_type->array_type() == NULL
   11551                 :          42 :                     && arg_type->map_type() == NULL
   11552                 :       53839 :                     && arg_type->channel_type() == NULL)
   11553                 :           9 :                   this->report_error(_("argument must be string or "
   11554                 :             :                                        "array or slice or map or channel"));
   11555                 :             :               }
   11556                 :             :           }
   11557                 :             :       }
   11558                 :             :       break;
   11559                 :             : 
   11560                 :       16165 :     case BUILTIN_PRINT:
   11561                 :       16165 :     case BUILTIN_PRINTLN:
   11562                 :       16165 :       {
   11563                 :       16165 :         const Expression_list* args = this->args();
   11564                 :       16165 :         if (args != NULL)
   11565                 :             :           {
   11566                 :       16111 :             for (Expression_list::const_iterator p = args->begin();
   11567                 :       54836 :                  p != args->end();
   11568                 :       38725 :                  ++p)
   11569                 :             :               {
   11570                 :       38725 :                 Type* type = (*p)->type();
   11571                 :       38725 :                 if (type->is_error()
   11572                 :       38712 :                     || type->is_string_type()
   11573                 :       39671 :                     || type->integer_type() != NULL
   11574                 :       39442 :                     || type->float_type() != NULL
   11575                 :       39366 :                     || type->complex_type() != NULL
   11576                 :         641 :                     || type->is_boolean_type()
   11577                 :         482 :                     || type->points_to() != NULL
   11578                 :       38752 :                     || type->interface_type() != NULL
   11579                 :       38750 :                     || type->channel_type() != NULL
   11580                 :       38747 :                     || type->map_type() != NULL
   11581                 :       38725 :                     || type->function_type() != NULL
   11582                 :       38745 :                     || type->is_slice_type())
   11583                 :             :                   ;
   11584                 :           1 :                 else if ((*p)->is_type_expression())
   11585                 :             :                   {
   11586                 :             :                     // If this is a type expression it's going to give
   11587                 :             :                     // an error anyhow, so we don't need one here.
   11588                 :             :                   }
   11589                 :             :                 else
   11590                 :             :                   {
   11591                 :             :                     // Report errors in the expression first.
   11592                 :           1 :                     (*p)->check_types(gogo);
   11593                 :           1 :                     if (!(*p)->is_error_expression())
   11594                 :           0 :                       this->report_error(_("unsupported argument type to "
   11595                 :             :                                            "builtin function"));
   11596                 :             :                   }
   11597                 :             :               }
   11598                 :             :           }
   11599                 :             :       }
   11600                 :             :       break;
   11601                 :             : 
   11602                 :         863 :     case BUILTIN_CLOSE:
   11603                 :         863 :       if (this->check_one_arg())
   11604                 :             :         {
   11605                 :         863 :           if (this->one_arg()->type()->channel_type() == NULL)
   11606                 :           1 :             this->report_error(_("argument must be channel"));
   11607                 :        1724 :           else if (!this->one_arg()->type()->channel_type()->may_send())
   11608                 :           1 :             this->report_error(_("cannot close receive-only channel"));
   11609                 :             :         }
   11610                 :             :       break;
   11611                 :             : 
   11612                 :       14972 :     case BUILTIN_PANIC:
   11613                 :       14972 :     case BUILTIN_SIZEOF:
   11614                 :       14972 :     case BUILTIN_ALIGNOF:
   11615                 :       14972 :       if (this->check_one_arg())
   11616                 :             :         {
   11617                 :       14972 :           Expression* arg = this->one_arg();
   11618                 :       14972 :           if (arg->type()->is_void_type())
   11619                 :           0 :             this->report_error(_("argument to builtin has void type"));
   11620                 :             :         }
   11621                 :             :       break;
   11622                 :             : 
   11623                 :         848 :     case BUILTIN_RECOVER:
   11624                 :         848 :       if (this->args() != NULL
   11625                 :           1 :           && !this->args()->empty()
   11626                 :         849 :           && !this->recover_arg_is_set_)
   11627                 :           0 :         this->report_error(_("too many arguments"));
   11628                 :             :       break;
   11629                 :             : 
   11630                 :        1156 :     case BUILTIN_OFFSETOF:
   11631                 :        1156 :       if (this->check_one_arg())
   11632                 :             :         {
   11633                 :        1156 :           Expression* arg = this->one_arg();
   11634                 :        1156 :           if (arg->classification() == Expression::EXPRESSION_SELECTOR)
   11635                 :             :             {
   11636                 :        1156 :               Selector_expression* se = static_cast<Selector_expression*>(arg);
   11637                 :        1156 :               Expression* resolved = se->resolved();
   11638                 :        1156 :               if (resolved != NULL)
   11639                 :        1156 :                 arg = resolved;
   11640                 :             :             }
   11641                 :        1156 :           if (arg->is_error_expression())
   11642                 :             :             ;
   11643                 :        1156 :           else if (arg->bound_method_expression() != NULL
   11644                 :           2 :                    || arg->interface_field_reference_expression() != NULL)
   11645                 :           2 :             this->report_error(_("invalid use of method value as "
   11646                 :             :                                  "argument of Offsetof"));
   11647                 :        1154 :           else if (arg->field_reference_expression() == NULL)
   11648                 :           0 :             this->report_error(_("argument must be a field reference"));
   11649                 :             :         }
   11650                 :             :       break;
   11651                 :             : 
   11652                 :        3229 :     case BUILTIN_COPY:
   11653                 :        3229 :       {
   11654                 :        3229 :         const Expression_list* args = this->args();
   11655                 :        3229 :         if (args == NULL || args->size() < 2)
   11656                 :             :           {
   11657                 :           0 :             this->report_error(_("not enough arguments"));
   11658                 :           0 :             break;
   11659                 :             :           }
   11660                 :        3229 :         else if (args->size() > 2)
   11661                 :             :           {
   11662                 :           0 :             this->report_error(_("too many arguments"));
   11663                 :           0 :             break;
   11664                 :             :           }
   11665                 :        3229 :         Type* arg1_type = args->front()->type();
   11666                 :        3229 :         Type* arg2_type = args->back()->type();
   11667                 :        3229 :         if (arg1_type->is_error() || arg2_type->is_error())
   11668                 :             :           {
   11669                 :           0 :             this->set_is_error();
   11670                 :           0 :             break;
   11671                 :             :           }
   11672                 :             : 
   11673                 :        3229 :         Type* e1;
   11674                 :        3229 :         if (arg1_type->is_slice_type())
   11675                 :        6456 :           e1 = arg1_type->array_type()->element_type();
   11676                 :             :         else
   11677                 :             :           {
   11678                 :           1 :             this->report_error(_("left argument must be a slice"));
   11679                 :           1 :             break;
   11680                 :             :           }
   11681                 :             : 
   11682                 :        3228 :         if (arg2_type->is_slice_type())
   11683                 :             :           {
   11684                 :        5700 :             Type* e2 = arg2_type->array_type()->element_type();
   11685                 :        2850 :             if (!Type::are_identical(e1, e2, Type::COMPARE_TAGS, NULL))
   11686                 :           0 :               this->report_error(_("element types must be the same"));
   11687                 :             :           }
   11688                 :         378 :         else if (arg2_type->is_string_type())
   11689                 :             :           {
   11690                 :        1131 :             if (e1->integer_type() == NULL || !e1->integer_type()->is_byte())
   11691                 :           0 :               this->report_error(_("first argument must be []byte"));
   11692                 :             :           }
   11693                 :             :         else
   11694                 :           1 :             this->report_error(_("second argument must be slice or string"));
   11695                 :             :       }
   11696                 :             :       break;
   11697                 :             : 
   11698                 :       15645 :     case BUILTIN_APPEND:
   11699                 :       15645 :       {
   11700                 :       15645 :         const Expression_list* args = this->args();
   11701                 :       15645 :         if (args == NULL || args->empty())
   11702                 :             :           {
   11703                 :           0 :             this->report_error(_("not enough arguments"));
   11704                 :           0 :             break;
   11705                 :             :           }
   11706                 :             : 
   11707                 :       15645 :         Type* slice_type = args->front()->type();
   11708                 :       15645 :         if (!slice_type->is_slice_type())
   11709                 :             :           {
   11710                 :           2 :             if (slice_type->is_error_type())
   11711                 :             :               break;
   11712                 :           2 :             if (slice_type->is_nil_type())
   11713                 :           1 :               go_error_at(args->front()->location(), "use of untyped nil");
   11714                 :             :             else
   11715                 :           1 :               go_error_at(args->front()->location(),
   11716                 :             :                           "argument 1 must be a slice");
   11717                 :           2 :             this->set_is_error();
   11718                 :           2 :             break;
   11719                 :             :           }
   11720                 :             : 
   11721                 :       31286 :         Type* element_type = slice_type->array_type()->element_type();
   11722                 :       15643 :         if (!element_type->in_heap())
   11723                 :           0 :           go_error_at(args->front()->location(),
   11724                 :             :                       "cannot append to slice of go:notinheap type");
   11725                 :       15643 :         if (this->is_varargs())
   11726                 :             :           {
   11727                 :        3553 :             if (!args->back()->type()->is_slice_type()
   11728                 :        3553 :                 && !args->back()->type()->is_string_type())
   11729                 :             :               {
   11730                 :           0 :                 go_error_at(args->back()->location(),
   11731                 :             :                             "invalid use of %<...%> with non-slice/non-string");
   11732                 :           0 :                 this->set_is_error();
   11733                 :           0 :                 break;
   11734                 :             :               }
   11735                 :             : 
   11736                 :        3553 :             if (args->size() < 2)
   11737                 :             :               {
   11738                 :           0 :                 this->report_error(_("not enough arguments"));
   11739                 :           0 :                 break;
   11740                 :             :               }
   11741                 :        3553 :             if (args->size() > 2)
   11742                 :             :               {
   11743                 :           0 :                 this->report_error(_("too many arguments"));
   11744                 :           0 :                 break;
   11745                 :             :               }
   11746                 :             : 
   11747                 :        3553 :             if (args->back()->type()->is_string_type()
   11748                 :         942 :                 && element_type->integer_type() != NULL
   11749                 :        4495 :                 && element_type->integer_type()->is_byte())
   11750                 :             :               {
   11751                 :             :                 // Permit append(s1, s2...) when s1 is a slice of
   11752                 :             :                 // bytes and s2 is a string type.
   11753                 :             :               }
   11754                 :             :             else
   11755                 :             :               {
   11756                 :             :                 // We have to test for assignment compatibility to a
   11757                 :             :                 // slice of the element type, which is not necessarily
   11758                 :             :                 // the same as the type of the first argument: the
   11759                 :             :                 // first argument might have a named type.
   11760                 :        2611 :                 Type* check_type = Type::make_array_type(element_type, NULL);
   11761                 :        2611 :                 std::string reason;
   11762                 :        2611 :                 if (!Type::are_assignable(check_type, args->back()->type(),
   11763                 :             :                                           &reason))
   11764                 :             :                   {
   11765                 :           0 :                     if (reason.empty())
   11766                 :           0 :                       go_error_at(args->back()->location(),
   11767                 :             :                                   "argument 2 has invalid type");
   11768                 :             :                     else
   11769                 :           0 :                       go_error_at(args->back()->location(),
   11770                 :             :                                   "argument 2 has invalid type (%s)",
   11771                 :             :                                   reason.c_str());
   11772                 :           0 :                     this->set_is_error();
   11773                 :           0 :                     break;
   11774                 :             :                   }
   11775                 :        2611 :               }
   11776                 :             :           }
   11777                 :             :         else
   11778                 :             :           {
   11779                 :       12090 :             Expression_list::const_iterator pa = args->begin();
   11780                 :       12090 :             int i = 2;
   11781                 :       26264 :             for (++pa; pa != args->end(); ++pa, ++i)
   11782                 :             :               {
   11783                 :       14174 :                 std::string reason;
   11784                 :       14174 :                 if (!Type::are_assignable(element_type, (*pa)->type(),
   11785                 :             :                                           &reason))
   11786                 :             :                   {
   11787                 :           2 :                     if (reason.empty())
   11788                 :           0 :                       go_error_at((*pa)->location(),
   11789                 :             :                                   "argument %d has incompatible type", i);
   11790                 :             :                     else
   11791                 :           2 :                       go_error_at((*pa)->location(),
   11792                 :             :                                   "argument %d has incompatible type (%s)",
   11793                 :             :                                   i, reason.c_str());
   11794                 :           2 :                     this->set_is_error();
   11795                 :             :                   }
   11796                 :       14174 :               }
   11797                 :             :           }
   11798                 :             :       }
   11799                 :             :       break;
   11800                 :             : 
   11801                 :        1024 :     case BUILTIN_REAL:
   11802                 :        1024 :     case BUILTIN_IMAG:
   11803                 :        1024 :       if (this->check_one_arg())
   11804                 :             :         {
   11805                 :        1022 :           if (this->one_arg()->type()->complex_type() == NULL)
   11806                 :           2 :             this->report_error(_("argument must have complex type"));
   11807                 :             :         }
   11808                 :             :       break;
   11809                 :             : 
   11810                 :       13690 :     case BUILTIN_COMPLEX:
   11811                 :       13690 :       {
   11812                 :       13690 :         const Expression_list* args = this->args();
   11813                 :       13690 :         go_assert(args != NULL && args->size() == 2);
   11814                 :       13690 :         if (args->front()->is_error_expression()
   11815                 :       13683 :             || args->front()->type()->is_error()
   11816                 :       13683 :             || args->back()->is_error_expression()
   11817                 :       27371 :             || args->back()->type()->is_error())
   11818                 :           9 :           this->set_is_error();
   11819                 :       13681 :         else if (!Type::are_identical(args->front()->type(),
   11820                 :       13681 :                                       args->back()->type(),
   11821                 :             :                                       Type::COMPARE_TAGS, NULL))
   11822                 :           6 :           this->report_error(_("complex arguments must have identical types"));
   11823                 :       13675 :         else if (args->front()->type()->float_type() == NULL)
   11824                 :           1 :           this->report_error(_("complex arguments must have "
   11825                 :             :                                "floating-point type"));
   11826                 :             :       }
   11827                 :             :       break;
   11828                 :             : 
   11829                 :          18 :     case BUILTIN_ADD:
   11830                 :          18 :     case BUILTIN_SLICE:
   11831                 :          18 :       {
   11832                 :          18 :         Numeric_constant nc;
   11833                 :          18 :         unsigned long v;
   11834                 :          18 :         const Expression_list* args = this->args();
   11835                 :          18 :         if (args == NULL || args->size() < 2)
   11836                 :           0 :           this->report_error(_("not enough arguments"));
   11837                 :          18 :         else if (args->size() > 2)
   11838                 :           0 :           this->report_error(_("too many arguments"));
   11839                 :          18 :         else if (args->front()->is_error_expression()
   11840                 :          18 :                  || args->front()->type()->is_error()
   11841                 :          18 :                  || args->back()->is_error_expression()
   11842                 :          36 :                  || args->back()->type()->is_error())
   11843                 :           0 :           this->set_is_error();
   11844                 :          18 :         else if (args->back()->type()->integer_type() == NULL
   11845                 :           0 :                  && (!args->back()->type()->is_abstract()
   11846                 :           0 :                      || !args->back()->numeric_constant_value(&nc)
   11847                 :           0 :                      || (nc.to_unsigned_long(&v)
   11848                 :             :                          == Numeric_constant::NC_UL_NOTINT)))
   11849                 :             :           {
   11850                 :           0 :             if (this->code_ == BUILTIN_ADD)
   11851                 :           0 :               go_error_at(args->back()->location(), "non-integer offset");
   11852                 :             :             else
   11853                 :           0 :               go_error_at(args->back()->location(), "non-integer size");
   11854                 :             :           }
   11855                 :          18 :         else if (this->code_ == BUILTIN_ADD)
   11856                 :             :           {
   11857                 :           9 :             Type* pointer_type =
   11858                 :           9 :               Type::make_pointer_type(Type::make_void_type());
   11859                 :           9 :             std::string reason;
   11860                 :           9 :             if (!Type::are_assignable(pointer_type, args->front()->type(),
   11861                 :             :                                       &reason))
   11862                 :             :               {
   11863                 :           0 :                 if (reason.empty())
   11864                 :           0 :                   go_error_at(args->front()->location(),
   11865                 :             :                               "argument 1 has incompatible type");
   11866                 :             :                 else
   11867                 :           0 :                   go_error_at(args->front()->location(),
   11868                 :             :                               "argument 1 has incompatible type (%s)",
   11869                 :             :                               reason.c_str());
   11870                 :           0 :                 this->set_is_error();
   11871                 :             :               }
   11872                 :           9 :           }
   11873                 :             :         else
   11874                 :             :           {
   11875                 :           9 :             if (args->front()->type()->points_to() == NULL)
   11876                 :             :               {
   11877                 :           0 :                 go_error_at(args->front()->location(),
   11878                 :             :                             "argument 1 must be a pointer");
   11879                 :           0 :                 this->set_is_error();
   11880                 :             :               }
   11881                 :             : 
   11882                 :           9 :             unsigned int int_bits =
   11883                 :          18 :               Type::lookup_integer_type("int")->integer_type()->bits();
   11884                 :             : 
   11885                 :           9 :             mpz_t ival;
   11886                 :           9 :             if (args->back()->numeric_constant_value(&nc) && nc.to_int(&ival))
   11887                 :             :               {
   11888                 :           7 :                 if (mpz_sgn(ival) < 0
   11889                 :           7 :                     || mpz_sizeinbase(ival, 2) >= int_bits)
   11890                 :             :                   {
   11891                 :           0 :                     go_error_at(args->back()->location(),
   11892                 :             :                                 "slice length out of range");
   11893                 :           0 :                     this->set_is_error();
   11894                 :             :                   }
   11895                 :           7 :                 mpz_clear(ival);
   11896                 :             :               }
   11897                 :             :           }
   11898                 :          18 :       }
   11899                 :          18 :       break;
   11900                 :             : 
   11901                 :           0 :     default:
   11902                 :           0 :       go_unreachable();
   11903                 :             :     }
   11904                 :             : }
   11905                 :             : 
   11906                 :             : Expression*
   11907                 :           0 : Builtin_call_expression::do_copy()
   11908                 :             : {
   11909                 :           0 :   Call_expression* bce =
   11910                 :             :     new Builtin_call_expression(this->gogo_, this->fn()->copy(),
   11911                 :           0 :                                 (this->args() == NULL
   11912                 :             :                                  ? NULL
   11913                 :           0 :                                  : this->args()->copy()),
   11914                 :           0 :                                 this->is_varargs(),
   11915                 :           0 :                                 this->location());
   11916                 :             : 
   11917                 :           0 :   if (this->varargs_are_lowered())
   11918                 :           0 :     bce->set_varargs_are_lowered();
   11919                 :           0 :   if (this->is_deferred())
   11920                 :           0 :     bce->set_is_deferred();
   11921                 :           0 :   if (this->is_concurrent())
   11922                 :           0 :     bce->set_is_concurrent();
   11923                 :           0 :   return bce;
   11924                 :             : }
   11925                 :             : 
   11926                 :             : // Return the backend representation for a builtin function.
   11927                 :             : 
   11928                 :             : Bexpression*
   11929                 :      164888 : Builtin_call_expression::do_get_backend(Translate_context* context)
   11930                 :             : {
   11931                 :      164888 :   Gogo* gogo = context->gogo();
   11932                 :      164888 :   Location location = this->location();
   11933                 :             : 
   11934                 :      164888 :   if (this->is_erroneous_call())
   11935                 :             :     {
   11936                 :          29 :       go_assert(saw_errors());
   11937                 :          29 :       return gogo->backend()->error_expression();
   11938                 :             :     }
   11939                 :             : 
   11940                 :      164859 :   switch (this->code_)
   11941                 :             :     {
   11942                 :           0 :     case BUILTIN_INVALID:
   11943                 :           0 :     case BUILTIN_NEW:
   11944                 :           0 :     case BUILTIN_MAKE:
   11945                 :           0 :     case BUILTIN_ADD:
   11946                 :           0 :     case BUILTIN_SLICE:
   11947                 :           0 :       go_unreachable();
   11948                 :             : 
   11949                 :      119501 :     case BUILTIN_LEN:
   11950                 :      119501 :     case BUILTIN_CAP:
   11951                 :      119501 :       {
   11952                 :      119501 :         const Expression_list* args = this->args();
   11953                 :      119501 :         go_assert(args != NULL && args->size() == 1);
   11954                 :      119501 :         Expression* arg = args->front();
   11955                 :      119501 :         Type* arg_type = arg->type();
   11956                 :             : 
   11957                 :      119501 :         if (this->seen_)
   11958                 :             :           {
   11959                 :           0 :             go_assert(saw_errors());
   11960                 :           0 :             return context->backend()->error_expression();
   11961                 :             :           }
   11962                 :      119501 :         this->seen_ = true;
   11963                 :      119501 :         this->seen_ = false;
   11964                 :      119501 :         if (arg_type->points_to() != NULL)
   11965                 :             :           {
   11966                 :           6 :             arg_type = arg_type->points_to();
   11967                 :          12 :             go_assert(arg_type->array_type() != NULL
   11968                 :             :                        && !arg_type->is_slice_type());
   11969                 :           6 :             arg = Expression::make_dereference(arg, NIL_CHECK_DEFAULT,
   11970                 :             :                                                location);
   11971                 :             :           }
   11972                 :             : 
   11973                 :      119501 :         Type* int_type = Type::lookup_integer_type("int");
   11974                 :      119501 :         Expression* val;
   11975                 :      119501 :         if (this->code_ == BUILTIN_LEN)
   11976                 :             :           {
   11977                 :      102918 :             if (arg_type->is_string_type())
   11978                 :       16417 :               val = Expression::make_string_info(arg, STRING_INFO_LENGTH,
   11979                 :             :                                                  location);
   11980                 :       86501 :             else if (arg_type->array_type() != NULL)
   11981                 :             :               {
   11982                 :       85290 :                 if (this->seen_)
   11983                 :             :                   {
   11984                 :           0 :                     go_assert(saw_errors());
   11985                 :           0 :                     return context->backend()->error_expression();
   11986                 :             :                   }
   11987                 :       85290 :                 this->seen_ = true;
   11988                 :      170580 :                 val = arg_type->array_type()->get_length(gogo, arg);
   11989                 :       85290 :                 this->seen_ = false;
   11990                 :             :               }
   11991                 :        1244 :             else if (arg_type->map_type() != NULL
   11992                 :        1211 :                      || arg_type->channel_type() != NULL)
   11993                 :             :               {
   11994                 :             :                 // The first field is the length.  If the pointer is
   11995                 :             :                 // nil, the length is zero.
   11996                 :        1211 :                 Type* pint_type = Type::make_pointer_type(int_type);
   11997                 :        1211 :                 arg = Expression::make_unsafe_cast(pint_type, arg, location);
   11998                 :        1211 :                 Expression* nil = Expression::make_nil(location);
   11999                 :        1211 :                 nil = Expression::make_cast(pint_type, nil, location);
   12000                 :        1211 :                 Expression* cmp = Expression::make_binary(OPERATOR_EQEQ,
   12001                 :             :                                                           arg, nil, location);
   12002                 :        1211 :                 Expression* zero = Expression::make_integer_ul(0, int_type,
   12003                 :             :                                                                location);
   12004                 :        1211 :                 Expression* indir =
   12005                 :        1211 :                     Expression::make_dereference(arg, NIL_CHECK_NOT_NEEDED,
   12006                 :             :                                                  location);
   12007                 :        1211 :                 val = Expression::make_conditional(cmp, zero, indir, location);
   12008                 :             :               }
   12009                 :             :             else
   12010                 :           0 :               go_unreachable();
   12011                 :             :           }
   12012                 :             :         else
   12013                 :             :           {
   12014                 :       16583 :             if (arg_type->array_type() != NULL)
   12015                 :             :               {
   12016                 :       16532 :                 if (this->seen_)
   12017                 :             :                   {
   12018                 :           0 :                     go_assert(saw_errors());
   12019                 :           0 :                     return context->backend()->error_expression();
   12020                 :             :                   }
   12021                 :       16532 :                 this->seen_ = true;
   12022                 :       33064 :                 val = arg_type->array_type()->get_capacity(gogo, arg);
   12023                 :       16532 :                 this->seen_ = false;
   12024                 :             :               }
   12025                 :          51 :             else if (arg_type->channel_type() != NULL)
   12026                 :             :               {
   12027                 :             :                 // The second field is the capacity.  If the pointer
   12028                 :             :                 // is nil, the capacity is zero.
   12029                 :          51 :                 Type* uintptr_type = Type::lookup_integer_type("uintptr");
   12030                 :          51 :                 Type* pint_type = Type::make_pointer_type(int_type);
   12031                 :          51 :                 Expression* parg = Expression::make_unsafe_cast(uintptr_type,
   12032                 :             :                                                                 arg,
   12033                 :             :                                                                 location);
   12034                 :         102 :                 int off = int_type->integer_type()->bits() / 8;
   12035                 :          51 :                 Expression* eoff = Expression::make_integer_ul(off,
   12036                 :             :                                                                uintptr_type,
   12037                 :             :                                                                location);
   12038                 :          51 :                 parg = Expression::make_binary(OPERATOR_PLUS, parg, eoff,
   12039                 :             :                                                location);
   12040                 :          51 :                 parg = Expression::make_unsafe_cast(pint_type, parg, location);
   12041                 :          51 :                 Expression* nil = Expression::make_nil(location);
   12042                 :          51 :                 nil = Expression::make_cast(pint_type, nil, location);
   12043                 :          51 :                 Expression* cmp = Expression::make_binary(OPERATOR_EQEQ,
   12044                 :             :                                                           arg, nil, location);
   12045                 :          51 :                 Expression* zero = Expression::make_integer_ul(0, int_type,
   12046                 :             :                                                                location);
   12047                 :          51 :                 Expression* indir =
   12048                 :          51 :                     Expression::make_dereference(parg, NIL_CHECK_NOT_NEEDED,
   12049                 :             :                                                  location);
   12050                 :          51 :                 val = Expression::make_conditional(cmp, zero, indir, location);
   12051                 :             :               }
   12052                 :             :             else
   12053                 :           0 :               go_unreachable();
   12054                 :             :           }
   12055                 :             : 
   12056                 :      119501 :         Expression* e = Expression::make_cast(int_type, val, location);
   12057                 :      119501 :         e->determine_type_no_context(gogo);
   12058                 :      119501 :         return e->get_backend(context);
   12059                 :             :       }
   12060                 :             : 
   12061                 :       15643 :     case BUILTIN_PRINT:
   12062                 :       15643 :     case BUILTIN_PRINTLN:
   12063                 :       15643 :       {
   12064                 :       15643 :         const bool is_ln = this->code_ == BUILTIN_PRINTLN;
   12065                 :             : 
   12066                 :       15643 :         Expression* print_stmts = Runtime::make_call(gogo, Runtime::PRINTLOCK,
   12067                 :             :                                                      location, 0);
   12068                 :             : 
   12069                 :       15643 :         const Expression_list* call_args = this->args();
   12070                 :       15643 :         if (call_args != NULL)
   12071                 :             :           {
   12072                 :       15596 :             for (Expression_list::const_iterator p = call_args->begin();
   12073                 :       52179 :                  p != call_args->end();
   12074                 :       36583 :                  ++p)
   12075                 :             :               {
   12076                 :       36583 :                 if (is_ln && p != call_args->begin())
   12077                 :             :                   {
   12078                 :       15284 :                     Expression* print_space =
   12079                 :       15284 :                       Runtime::make_call(gogo, Runtime::PRINTSP, location, 0);
   12080                 :             : 
   12081                 :       15284 :                     print_stmts =
   12082                 :       15284 :                         Expression::make_compound(print_stmts, print_space,
   12083                 :             :                                                   location);
   12084                 :             :                   }
   12085                 :             : 
   12086                 :       36583 :                 Expression* arg = *p;
   12087                 :       36583 :                 Type* type = arg->type();
   12088                 :       36583 :                 Runtime::Function code;
   12089                 :       36583 :                 if (type->is_string_type())
   12090                 :             :                   code = Runtime::PRINTSTRING;
   12091                 :       19704 :                 else if (type->integer_type() != NULL
   12092                 :        9466 :                          && type->integer_type()->is_unsigned())
   12093                 :             :                   {
   12094                 :        4454 :                     Type* itype = Type::lookup_integer_type("uint64");
   12095                 :        4454 :                     arg = Expression::make_cast(itype, arg, location);
   12096                 :        8908 :                     if (gogo->compiling_runtime()
   12097                 :        1686 :                         && type->named_type() != NULL
   12098                 :       10594 :                         && gogo->unpack_hidden_name(type->named_type()->name())
   12099                 :        1686 :                            == "hex")
   12100                 :             :                       code = Runtime::PRINTHEX;
   12101                 :             :                     else
   12102                 :        3964 :                       code = Runtime::PRINTUINT;
   12103                 :             :                   }
   12104                 :        5784 :                 else if (type->integer_type() != NULL)
   12105                 :             :                   {
   12106                 :        5012 :                     Type* itype = Type::lookup_integer_type("int64");
   12107                 :        5012 :                     arg = Expression::make_cast(itype, arg, location);
   12108                 :        5012 :                     code = Runtime::PRINTINT;
   12109                 :             :                   }
   12110                 :         772 :                 else if (type->float_type() != NULL)
   12111                 :             :                   {
   12112                 :         220 :                     Type* dtype = Type::lookup_float_type("float64");
   12113                 :         220 :                     arg = Expression::make_cast(dtype, arg, location);
   12114                 :         220 :                     code = Runtime::PRINTFLOAT;
   12115                 :             :                   }
   12116                 :         552 :                 else if (type->complex_type() != NULL)
   12117                 :             :                   {
   12118                 :          76 :                     Type* ctype = Type::lookup_complex_type("complex128");
   12119                 :          76 :                     arg = Expression::make_cast(ctype, arg, location);
   12120                 :          76 :                     code = Runtime::PRINTCOMPLEX;
   12121                 :             :                   }
   12122                 :         476 :                 else if (type->is_boolean_type())
   12123                 :             :                   code = Runtime::PRINTBOOL;
   12124                 :         349 :                 else if (type->points_to() != NULL
   12125                 :          54 :                          || type->channel_type() != NULL
   12126                 :         351 :                          || type->map_type() != NULL
   12127                 :         356 :                          || type->function_type() != NULL)
   12128                 :             :                   {
   12129                 :         300 :                     arg = Expression::make_cast(type, arg, location);
   12130                 :         300 :                     code = Runtime::PRINTPOINTER;
   12131                 :             :                   }
   12132                 :          49 :                 else if (type->interface_type() != NULL)
   12133                 :             :                   {
   12134                 :          60 :                     if (type->interface_type()->is_empty())
   12135                 :             :                       code = Runtime::PRINTEFACE;
   12136                 :             :                     else
   12137                 :           4 :                       code = Runtime::PRINTIFACE;
   12138                 :             :                   }
   12139                 :          19 :                 else if (type->is_slice_type())
   12140                 :             :                   code = Runtime::PRINTSLICE;
   12141                 :             :                 else
   12142                 :             :                   {
   12143                 :           0 :                     go_assert(saw_errors());
   12144                 :           0 :                     return context->backend()->error_expression();
   12145                 :             :                   }
   12146                 :             : 
   12147                 :       36583 :                 Expression* call = Runtime::make_call(gogo, code, location, 1,
   12148                 :             :                                                       arg);
   12149                 :       36583 :                 print_stmts = Expression::make_compound(print_stmts, call,
   12150                 :             :                                                         location);
   12151                 :             :               }
   12152                 :             :           }
   12153                 :             : 
   12154                 :       15643 :         if (is_ln)
   12155                 :             :           {
   12156                 :       13300 :             Expression* print_nl =
   12157                 :       13300 :               Runtime::make_call(gogo, Runtime::PRINTNL, location, 0);
   12158                 :       13300 :             print_stmts = Expression::make_compound(print_stmts, print_nl,
   12159                 :             :                                                     location);
   12160                 :             :           }
   12161                 :             : 
   12162                 :       15643 :         Expression* unlock = Runtime::make_call(gogo, Runtime::PRINTUNLOCK,
   12163                 :             :                                                 location, 0);
   12164                 :       15643 :         print_stmts = Expression::make_compound(print_stmts, unlock, location);
   12165                 :             : 
   12166                 :       15643 :         print_stmts->determine_type_no_context(gogo);
   12167                 :             : 
   12168                 :       15643 :         return print_stmts->get_backend(context);
   12169                 :             :       }
   12170                 :             : 
   12171                 :       14856 :     case BUILTIN_PANIC:
   12172                 :       14856 :       {
   12173                 :       14856 :         const Expression_list* args = this->args();
   12174                 :       14856 :         go_assert(args != NULL && args->size() == 1);
   12175                 :       14856 :         Expression* arg = args->front();
   12176                 :       14856 :         Type *empty =
   12177                 :       14856 :           Type::make_empty_interface_type(Linemap::predeclared_location());
   12178                 :       14856 :         arg = Expression::convert_for_assignment(gogo, empty, arg, location);
   12179                 :             : 
   12180                 :       14856 :         Expression* panic =
   12181                 :       14856 :           Runtime::make_call(gogo, Runtime::GOPANIC, location, 1, arg);
   12182                 :       14856 :         panic->determine_type_no_context(gogo);
   12183                 :       14856 :         return panic->get_backend(context);
   12184                 :             :       }
   12185                 :             : 
   12186                 :         847 :     case BUILTIN_RECOVER:
   12187                 :         847 :       {
   12188                 :             :         // The argument is set when building recover thunks.  It's a
   12189                 :             :         // boolean value which is true if we can recover a value now.
   12190                 :         847 :         const Expression_list* args = this->args();
   12191                 :         847 :         go_assert(args != NULL && args->size() == 1);
   12192                 :         847 :         Expression* arg = args->front();
   12193                 :         847 :         Type *empty =
   12194                 :         847 :           Type::make_empty_interface_type(Linemap::predeclared_location());
   12195                 :             : 
   12196                 :         847 :         Expression* nil = Expression::make_nil(location);
   12197                 :         847 :         nil = Expression::make_interface_value(empty, nil, nil, location);
   12198                 :             : 
   12199                 :             :         // We need to handle a deferred call to recover specially,
   12200                 :             :         // because it changes whether it can recover a panic or not.
   12201                 :             :         // See test7 in test/recover1.go.
   12202                 :         847 :         Expression* recover = Runtime::make_call(gogo,
   12203                 :         847 :                                                  (this->is_deferred()
   12204                 :             :                                                   ? Runtime::DEFERREDRECOVER
   12205                 :             :                                                   : Runtime::GORECOVER),
   12206                 :             :                                                  location, 0);
   12207                 :         847 :         Expression* cond =
   12208                 :         847 :             Expression::make_conditional(arg, recover, nil, location);
   12209                 :         847 :         cond->determine_type_no_context(gogo);
   12210                 :         847 :         return cond->get_backend(context);
   12211                 :             :       }
   12212                 :             : 
   12213                 :         861 :     case BUILTIN_CLOSE:
   12214                 :         861 :       {
   12215                 :         861 :         const Expression_list* args = this->args();
   12216                 :         861 :         go_assert(args != NULL && args->size() == 1);
   12217                 :         861 :         Expression* arg = args->front();
   12218                 :         861 :         Expression* close = Runtime::make_call(gogo, Runtime::CLOSE, location,
   12219                 :             :                                                1, arg);
   12220                 :         861 :         close->determine_type_no_context(gogo);
   12221                 :         861 :         return close->get_backend(context);
   12222                 :             :       }
   12223                 :             : 
   12224                 :           0 :     case BUILTIN_SIZEOF:
   12225                 :           0 :     case BUILTIN_OFFSETOF:
   12226                 :           0 :     case BUILTIN_ALIGNOF:
   12227                 :           0 :       {
   12228                 :           0 :         Numeric_constant nc;
   12229                 :           0 :         unsigned long val;
   12230                 :           0 :         if (!this->numeric_constant_value(&nc)
   12231                 :           0 :             || nc.to_unsigned_long(&val) != Numeric_constant::NC_UL_VALID)
   12232                 :             :           {
   12233                 :           0 :             go_assert(saw_errors());
   12234                 :           0 :             return context->backend()->error_expression();
   12235                 :             :           }
   12236                 :           0 :         Type* uintptr_type = Type::lookup_integer_type("uintptr");
   12237                 :           0 :         mpz_t ival;
   12238                 :           0 :         nc.get_int(&ival);
   12239                 :           0 :         Expression* int_cst =
   12240                 :           0 :             Expression::make_integer_z(&ival, uintptr_type, location);
   12241                 :           0 :         mpz_clear(ival);
   12242                 :           0 :         return int_cst->get_backend(context);
   12243                 :           0 :       }
   12244                 :             : 
   12245                 :           0 :     case BUILTIN_COPY:
   12246                 :             :       // Handled in Builtin_call_expression::do_flatten.
   12247                 :           0 :       go_unreachable();
   12248                 :             : 
   12249                 :           0 :     case BUILTIN_APPEND:
   12250                 :             :       // Handled in Builtin_call_expression::flatten_append.
   12251                 :           0 :       go_unreachable();
   12252                 :             : 
   12253                 :         986 :     case BUILTIN_REAL:
   12254                 :         986 :     case BUILTIN_IMAG:
   12255                 :         986 :       {
   12256                 :         986 :         const Expression_list* args = this->args();
   12257                 :         986 :         go_assert(args != NULL && args->size() == 1);
   12258                 :             : 
   12259                 :         986 :         Bexpression* ret;
   12260                 :         986 :         Bexpression* bcomplex = args->front()->get_backend(context);
   12261                 :         986 :         if (this->code_ == BUILTIN_REAL)
   12262                 :         460 :           ret = gogo->backend()->real_part_expression(bcomplex, location);
   12263                 :             :         else
   12264                 :         526 :           ret = gogo->backend()->imag_part_expression(bcomplex, location);
   12265                 :             :         return ret;
   12266                 :             :       }
   12267                 :             : 
   12268                 :       12165 :     case BUILTIN_COMPLEX:
   12269                 :       12165 :       {
   12270                 :       12165 :         const Expression_list* args = this->args();
   12271                 :       12165 :         go_assert(args != NULL && args->size() == 2);
   12272                 :       12165 :         Bexpression* breal = args->front()->get_backend(context);
   12273                 :       12165 :         Bexpression* bimag = args->back()->get_backend(context);
   12274                 :       12165 :         return gogo->backend()->complex_expression(breal, bimag, location);
   12275                 :             :       }
   12276                 :             : 
   12277                 :           0 :     default:
   12278                 :           0 :       go_unreachable();
   12279                 :             :     }
   12280                 :             : }
   12281                 :             : 
   12282                 :             : // We have to support exporting a builtin call expression, because
   12283                 :             : // code can set a constant to the result of a builtin expression.
   12284                 :             : 
   12285                 :             : void
   12286                 :        4252 : Builtin_call_expression::do_export(Export_function_body* efb) const
   12287                 :             : {
   12288                 :        4252 :   if (this->code_ == BUILTIN_ADD || this->code_ == BUILTIN_SLICE)
   12289                 :             :     {
   12290                 :           0 :       char buf[50];
   12291                 :           0 :       snprintf(buf, sizeof buf, "<p%d>%s", efb->unsafe_package_index(),
   12292                 :             :                (this->code_ == BUILTIN_ADD ? "Add" : "Slice"));
   12293                 :           0 :       efb->write_c_string(buf);
   12294                 :           0 :       this->export_arguments(efb);
   12295                 :             :     }
   12296                 :             :   else
   12297                 :             :     {
   12298                 :        4252 :       const char *s = NULL;
   12299                 :        4252 :       switch (this->code_)
   12300                 :             :         {
   12301                 :           0 :         default:
   12302                 :           0 :           go_unreachable();
   12303                 :             :         case BUILTIN_APPEND:
   12304                 :             :           s = "append";
   12305                 :             :           break;
   12306                 :          54 :         case BUILTIN_COPY:
   12307                 :          54 :           s = "copy";
   12308                 :          54 :           break;
   12309                 :        3177 :         case BUILTIN_LEN:
   12310                 :        3177 :           s = "len";
   12311                 :        3177 :           break;
   12312                 :          14 :         case BUILTIN_CAP:
   12313                 :          14 :           s = "cap";
   12314                 :          14 :           break;
   12315                 :          24 :         case BUILTIN_DELETE:
   12316                 :          24 :           s = "delete";
   12317                 :          24 :           break;
   12318                 :          13 :         case BUILTIN_PRINT:
   12319                 :          13 :           s = "print";
   12320                 :          13 :           break;
   12321                 :          17 :         case BUILTIN_PRINTLN:
   12322                 :          17 :           s = "println";
   12323                 :          17 :           break;
   12324                 :         519 :         case BUILTIN_PANIC:
   12325                 :         519 :           s = "panic";
   12326                 :         519 :           break;
   12327                 :          13 :         case BUILTIN_RECOVER:
   12328                 :          13 :           s = "recover";
   12329                 :          13 :           break;
   12330                 :           9 :         case BUILTIN_CLOSE:
   12331                 :           9 :           s = "close";
   12332                 :           9 :           break;
   12333                 :          64 :         case BUILTIN_REAL:
   12334                 :          64 :           s = "real";
   12335                 :          64 :           break;
   12336                 :          76 :         case BUILTIN_IMAG:
   12337                 :          76 :           s = "imag";
   12338                 :          76 :           break;
   12339                 :          66 :         case BUILTIN_COMPLEX:
   12340                 :          66 :           s = "complex";
   12341                 :          66 :           break;
   12342                 :             :         }
   12343                 :        4252 :       efb->write_c_string(s);
   12344                 :        4252 :       this->export_arguments(efb);
   12345                 :             :     }
   12346                 :        4252 : }
   12347                 :             : 
   12348                 :             : // Class Call_expression.
   12349                 :             : 
   12350                 :             : // A Go function can be viewed in a couple of different ways.  The
   12351                 :             : // code of a Go function becomes a backend function with parameters
   12352                 :             : // whose types are simply the backend representation of the Go types.
   12353                 :             : // If there are multiple results, they are returned as a backend
   12354                 :             : // struct.
   12355                 :             : 
   12356                 :             : // However, when Go code refers to a function other than simply
   12357                 :             : // calling it, the backend type of that function is actually a struct.
   12358                 :             : // The first field of the struct points to the Go function code
   12359                 :             : // (sometimes a wrapper as described below).  The remaining fields
   12360                 :             : // hold addresses of closed-over variables.  This struct is called a
   12361                 :             : // closure.
   12362                 :             : 
   12363                 :             : // There are a few cases to consider.
   12364                 :             : 
   12365                 :             : // A direct function call of a known function in package scope.  In
   12366                 :             : // this case there are no closed-over variables, and we know the name
   12367                 :             : // of the function code.  We can simply produce a backend call to the
   12368                 :             : // function directly, and not worry about the closure.
   12369                 :             : 
   12370                 :             : // A direct function call of a known function literal.  In this case
   12371                 :             : // we know the function code and we know the closure.  We generate the
   12372                 :             : // function code such that it expects an additional final argument of
   12373                 :             : // the closure type.  We pass the closure as the last argument, after
   12374                 :             : // the other arguments.
   12375                 :             : 
   12376                 :             : // An indirect function call.  In this case we have a closure.  We
   12377                 :             : // load the pointer to the function code from the first field of the
   12378                 :             : // closure.  We pass the address of the closure as the last argument.
   12379                 :             : 
   12380                 :             : // A call to a method of an interface.  Type methods are always at
   12381                 :             : // package scope, so we call the function directly, and don't worry
   12382                 :             : // about the closure.
   12383                 :             : 
   12384                 :             : // This means that for a function at package scope we have two cases.
   12385                 :             : // One is the direct call, which has no closure.  The other is the
   12386                 :             : // indirect call, which does have a closure.  We can't simply ignore
   12387                 :             : // the closure, even though it is the last argument, because that will
   12388                 :             : // fail on targets where the function pops its arguments.  So when
   12389                 :             : // generating a closure for a package-scope function we set the
   12390                 :             : // function code pointer in the closure to point to a wrapper
   12391                 :             : // function.  This wrapper function accepts a final argument that
   12392                 :             : // points to the closure, ignores it, and calls the real function as a
   12393                 :             : // direct function call.  This wrapper will normally be efficient, and
   12394                 :             : // can often simply be a tail call to the real function.
   12395                 :             : 
   12396                 :             : // We don't use GCC's static chain pointer because 1) we don't need
   12397                 :             : // it; 2) GCC only permits using a static chain to call a known
   12398                 :             : // function, so we can't use it for an indirect call anyhow.  Since we
   12399                 :             : // can't use it for an indirect call, we may as well not worry about
   12400                 :             : // using it for a direct call either.
   12401                 :             : 
   12402                 :             : // We pass the closure last rather than first because it means that
   12403                 :             : // the function wrapper we put into a closure for a package-scope
   12404                 :             : // function can normally just be a tail call to the real function.
   12405                 :             : 
   12406                 :             : // For method expressions we generate a wrapper that loads the
   12407                 :             : // receiver from the closure and then calls the method.  This
   12408                 :             : // unfortunately forces reshuffling the arguments, since there is a
   12409                 :             : // new first argument, but we can't avoid reshuffling either for
   12410                 :             : // method expressions or for indirect calls of package-scope
   12411                 :             : // functions, and since the latter are more common we reshuffle for
   12412                 :             : // method expressions.
   12413                 :             : 
   12414                 :             : // Note that the Go code retains the Go types.  The extra final
   12415                 :             : // argument only appears when we convert to the backend
   12416                 :             : // representation.
   12417                 :             : 
   12418                 :             : // Traversal.
   12419                 :             : 
   12420                 :             : int
   12421                 :    17227499 : Call_expression::do_traverse(Traverse* traverse)
   12422                 :             : {
   12423                 :    17227499 :   if (this->lowered_ != NULL)
   12424                 :      347102 :     return Expression::traverse(&this->lowered_, traverse);
   12425                 :             : 
   12426                 :             :   // If we are calling a function in a different package that returns
   12427                 :             :   // an unnamed type, this may be the only chance we get to traverse
   12428                 :             :   // that type.  We don't traverse this->type_ because it may be a
   12429                 :             :   // Call_multiple_result_type that will just lead back here.
   12430                 :    16880397 :   if (this->type_ != NULL && !this->type_->is_error_type())
   12431                 :             :     {
   12432                 :    12521815 :       Function_type *fntype = this->get_function_type();
   12433                 :    12521815 :       if (fntype != NULL && Type::traverse(fntype, traverse) == TRAVERSE_EXIT)
   12434                 :             :         return TRAVERSE_EXIT;
   12435                 :             :     }
   12436                 :    16878257 :   if (Expression::traverse(&this->fn_, traverse) == TRAVERSE_EXIT)
   12437                 :             :     return TRAVERSE_EXIT;
   12438                 :    16279479 :   if (this->args_ != NULL)
   12439                 :             :     {
   12440                 :    14757931 :       if (this->args_->traverse(traverse) == TRAVERSE_EXIT)
   12441                 :             :         return TRAVERSE_EXIT;
   12442                 :             :     }
   12443                 :             :   return TRAVERSE_CONTINUE;
   12444                 :             : }
   12445                 :             : 
   12446                 :             : // Lower a Call_expression to a Builtin_call_expression.  This happens
   12447                 :             : // early on, before determine_types.
   12448                 :             : 
   12449                 :             : Expression*
   12450                 :     3894049 : Call_expression::lower_builtin(Gogo* gogo)
   12451                 :             : {
   12452                 :             :   // This is called before determine_types, so we can't call
   12453                 :             :   // this->fn_->type().  Fortunately builtin calls require a direct
   12454                 :             :   // reference to the builtin.
   12455                 :     3894049 :   Expression* fn = this->fn_;
   12456                 :     3894049 :   Named_object* no;
   12457                 :     3894049 :   if (fn->func_expression() != NULL)
   12458                 :     2459104 :     no = fn->func_expression()->named_object();
   12459                 :     1434945 :   else if (fn->unknown_expression() != NULL)
   12460                 :             :     {
   12461                 :      446120 :       no = fn->unknown_expression()->named_object();
   12462                 :      446120 :       if (no->is_unknown())
   12463                 :             :         {
   12464                 :      446120 :           no = no->unknown_value()->real_named_object();
   12465                 :      446120 :           if (no == NULL)
   12466                 :          10 :             return this;
   12467                 :             :         }
   12468                 :             :     }
   12469                 :             :   else
   12470                 :      988825 :     return this;
   12471                 :             : 
   12472                 :     2905214 :   if (!no->is_function_declaration())
   12473                 :      854569 :     return this;
   12474                 :     2050645 :   if (!no->func_declaration_value()->type()->is_builtin())
   12475                 :     1809878 :     return this;
   12476                 :             : 
   12477                 :      240767 :   if (fn->unknown_expression() != NULL)
   12478                 :      143737 :     fn = Expression::make_func_reference(no, NULL, fn->location());
   12479                 :             : 
   12480                 :      240767 :   Builtin_call_expression* bce = new Builtin_call_expression(gogo, fn,
   12481                 :             :                                                              this->args_,
   12482                 :      240767 :                                                              this->is_varargs_,
   12483                 :      240767 :                                                              this->location());
   12484                 :      240767 :   if (this->is_deferred_)
   12485                 :         344 :     bce->set_is_deferred();
   12486                 :      240767 :   if (this->is_concurrent_)
   12487                 :          27 :     bce->set_is_concurrent();
   12488                 :             :   return bce;
   12489                 :             : }
   12490                 :             : 
   12491                 :             : // A type conversion can be a constant.
   12492                 :             : 
   12493                 :             : bool
   12494                 :      177870 : Call_expression::do_is_constant() const
   12495                 :             : {
   12496                 :      177870 :   if (this->lowered_ != NULL)
   12497                 :         635 :     return this->lowered_->is_constant();
   12498                 :      177235 :   if (this->fn_->is_type_expression()
   12499                 :       40307 :       && this->args_ != NULL
   12500                 :      217542 :       && this->args_->size() == 1)
   12501                 :       40307 :     return this->args_->front()->is_constant();
   12502                 :             :   return false;
   12503                 :             : }
   12504                 :             : 
   12505                 :             : bool
   12506                 :      172487 : Call_expression::do_is_untyped(Type** ptype) const
   12507                 :             : {
   12508                 :      172487 :   if (this->lowered_ != NULL)
   12509                 :           2 :     return this->lowered_->is_untyped(ptype);
   12510                 :             :   return false;
   12511                 :             : }
   12512                 :             : 
   12513                 :             : bool
   12514                 :      132121 : Call_expression::do_numeric_constant_value(Numeric_constant* nc)
   12515                 :             : {
   12516                 :      132121 :   if (this->lowered_ != NULL)
   12517                 :       10307 :     return this->lowered_->numeric_constant_value(nc);
   12518                 :      121814 :   if (this->fn_->is_type_expression()
   12519                 :          34 :       && this->args_ != NULL
   12520                 :      121848 :       && this->args_->size() == 1)
   12521                 :             :     {
   12522                 :             :       // If we get here, it's before the determine_types pass, so we
   12523                 :             :       // have to pull apart the type carefully.  This is a hack that
   12524                 :             :       // is needed because the finalize_methods needs to be able to
   12525                 :             :       // determine whether the length of an array is 1.
   12526                 :             : 
   12527                 :          34 :       Type* type;
   12528                 :          34 :       if (this->fn_->classification() == EXPRESSION_TYPE)
   12529                 :           0 :         type = this->fn_->type();
   12530                 :      121815 :       else if (this->fn_->unknown_expression() != NULL)
   12531                 :             :         {
   12532                 :          34 :           Named_object* no = this->fn_->unknown_expression()->named_object();
   12533                 :          34 :           if (no->is_unknown())
   12534                 :             :             {
   12535                 :          34 :               no = no->unknown_value()->real_named_object();
   12536                 :          34 :               go_assert(no != NULL);
   12537                 :             :             }
   12538                 :          34 :           type = no->type_value();
   12539                 :             :         }
   12540                 :             :       else
   12541                 :             :         return false;
   12542                 :             : 
   12543                 :          34 :       if (!type->is_numeric_type())
   12544                 :             :         return false;
   12545                 :          34 :       if (!this->args_->front()->numeric_constant_value(nc))
   12546                 :             :         return false;
   12547                 :          33 :       return nc->set_type(type, false, this->location());
   12548                 :             :     }
   12549                 :             :   return false;
   12550                 :             : }
   12551                 :             : 
   12552                 :             : bool
   12553                 :      243404 : Call_expression::do_discarding_value()
   12554                 :             : {
   12555                 :      243404 :   if (this->fn_->is_type_expression())
   12556                 :             :     {
   12557                 :           4 :       this->unused_value_error();
   12558                 :           4 :       return false;
   12559                 :             :     }
   12560                 :             :   return true;
   12561                 :             : }
   12562                 :             : 
   12563                 :             : // Lower a call statement.
   12564                 :             : 
   12565                 :             : Expression*
   12566                 :     1493103 : Call_expression::do_lower(Gogo* gogo, Named_object*,
   12567                 :             :                           Statement_inserter* inserter)
   12568                 :             : {
   12569                 :     1493103 :   if (this->lowered_ != NULL)
   12570                 :             :     return this->lowered_;
   12571                 :             : 
   12572                 :     1327855 :   Location loc = this->location();
   12573                 :             : 
   12574                 :     1327855 :   if (this->is_error_expression())
   12575                 :          96 :     return Expression::make_error(loc);
   12576                 :             : 
   12577                 :             :   // Although we've already lowered calls to builtin functions, we may
   12578                 :             :   // still see calls generated to builtins elsewhere in the lowering
   12579                 :             :   // pass.  It's simpler to handle them here.
   12580                 :     1327759 :   Expression* builtin = this->lower_builtin(gogo);
   12581                 :     1327759 :   if (builtin != this)
   12582                 :             :     return builtin;
   12583                 :             : 
   12584                 :             :   // If this call returns multiple results, create a temporary
   12585                 :             :   // variable to hold them.
   12586                 :     1308149 :   if (this->result_count() > 1 && this->call_temp_ == NULL)
   12587                 :             :     {
   12588                 :       93382 :       Struct_field_list* sfl = new Struct_field_list();
   12589                 :       93382 :       const Typed_identifier_list* results =
   12590                 :       93382 :         this->get_function_type()->results();
   12591                 :             : 
   12592                 :       93382 :       int i = 0;
   12593                 :       93382 :       char buf[20];
   12594                 :       93382 :       for (Typed_identifier_list::const_iterator p = results->begin();
   12595                 :      290003 :            p != results->end();
   12596                 :      196621 :            ++p, ++i)
   12597                 :             :         {
   12598                 :      196621 :           snprintf(buf, sizeof buf, "res%d", i);
   12599                 :      196621 :           sfl->push_back(Struct_field(Typed_identifier(buf, p->type(), loc)));
   12600                 :             :         }
   12601                 :             : 
   12602                 :       93382 :       Struct_type* st = Type::make_struct_type(sfl, loc);
   12603                 :       93382 :       st->set_is_struct_incomparable();
   12604                 :       93382 :       st->set_is_results_struct();
   12605                 :       93382 :       this->call_temp_ = Statement::make_temporary(st, NULL, loc);
   12606                 :       93382 :       inserter->insert(this->call_temp_);
   12607                 :             :     }
   12608                 :             : 
   12609                 :             :   // If this is call to a method, call the method directly passing the
   12610                 :             :   // object as the first parameter.
   12611                 :     1308149 :   Bound_method_expression* bme = this->fn_->bound_method_expression();
   12612                 :      264230 :   if (bme != NULL && !this->is_deferred_ && !this->is_concurrent_)
   12613                 :             :     {
   12614                 :      258276 :       Named_object* methodfn = bme->function();
   12615                 :      258276 :       Function_type* mft = (methodfn->is_function()
   12616                 :      258276 :                             ? methodfn->func_value()->type()
   12617                 :      123932 :                             : methodfn->func_declaration_value()->type());
   12618                 :      258276 :       Expression* first_arg = bme->first_argument();
   12619                 :             : 
   12620                 :             :       // We always pass a pointer when calling a method, except for
   12621                 :             :       // direct interface types when calling a value method.
   12622                 :      258276 :       if (!first_arg->type()->is_error()
   12623                 :      258275 :           && first_arg->type()->points_to() == NULL
   12624                 :      298186 :           && !first_arg->type()->is_direct_iface_type())
   12625                 :             :         {
   12626                 :       35412 :           first_arg = Expression::make_unary(OPERATOR_AND, first_arg, loc);
   12627                 :             :           // We may need to create a temporary variable so that we can
   12628                 :             :           // take the address.  We can't do that here because it will
   12629                 :             :           // mess up the order of evaluation.
   12630                 :       35412 :           Unary_expression* ue = static_cast<Unary_expression*>(first_arg);
   12631                 :       35412 :           ue->set_create_temp();
   12632                 :             :         }
   12633                 :      222864 :       else if (mft->receiver()->type()->points_to() == NULL
   12634                 :        6612 :                && first_arg->type()->points_to() != NULL
   12635                 :      224977 :                && first_arg->type()->points_to()->is_direct_iface_type())
   12636                 :          45 :         first_arg = Expression::make_dereference(first_arg,
   12637                 :             :                                                  Expression::NIL_CHECK_DEFAULT,
   12638                 :             :                                                  loc);
   12639                 :             : 
   12640                 :             :       // If we are calling a method which was inherited from an
   12641                 :             :       // embedded struct, and the method did not get a stub, then the
   12642                 :             :       // first type may be wrong.
   12643                 :      258276 :       Type* fatype = bme->first_argument_type();
   12644                 :      258276 :       if (fatype != NULL)
   12645                 :             :         {
   12646                 :           0 :           if (fatype->points_to() == NULL)
   12647                 :           0 :             fatype = Type::make_pointer_type(fatype);
   12648                 :           0 :           first_arg = Expression::make_unsafe_cast(fatype, first_arg, loc);
   12649                 :             :         }
   12650                 :             : 
   12651                 :      258276 :       first_arg->determine_type_no_context(gogo);
   12652                 :      258276 :       first_arg->check_types(gogo);
   12653                 :             : 
   12654                 :      258276 :       Expression_list* new_args = new Expression_list();
   12655                 :      258276 :       new_args->push_back(first_arg);
   12656                 :      258276 :       if (this->args_ != NULL)
   12657                 :             :         {
   12658                 :      156430 :           for (Expression_list::const_iterator p = this->args_->begin();
   12659                 :      405775 :                p != this->args_->end();
   12660                 :      249345 :                ++p)
   12661                 :      249345 :             new_args->push_back(*p);
   12662                 :             :         }
   12663                 :             : 
   12664                 :             :       // We have to change in place because this structure may be
   12665                 :             :       // referenced by Call_result_expressions.  We can't delete the
   12666                 :             :       // old arguments, because we may be traversing them up in some
   12667                 :             :       // caller.  FIXME.
   12668                 :      258276 :       this->args_ = new_args;
   12669                 :      258276 :       this->fn_ = Expression::make_func_reference(methodfn, NULL,
   12670                 :             :                                                   bme->location());
   12671                 :             :     }
   12672                 :             : 
   12673                 :             :   // If this is a call to an imported function for which we have an
   12674                 :             :   // inlinable function body, add it to the list of functions to give
   12675                 :             :   // to the backend as inlining opportunities.
   12676                 :     1308149 :   Func_expression* fe = this->fn_->func_expression();
   12677                 :     1199641 :   if (fe != NULL
   12678                 :     1199641 :       && fe->named_object()->is_function_declaration()
   12679                 :      718935 :       && fe->named_object()->func_declaration_value()->has_imported_body())
   12680                 :       82297 :     gogo->add_imported_inlinable_function(fe->named_object());
   12681                 :             : 
   12682                 :             :   return this;
   12683                 :             : }
   12684                 :             : 
   12685                 :             : // Flatten a call with multiple results into a temporary.
   12686                 :             : 
   12687                 :             : Expression*
   12688                 :     1038846 : Call_expression::do_flatten(Gogo* gogo, Named_object*,
   12689                 :             :                             Statement_inserter* inserter)
   12690                 :             : {
   12691                 :     1038846 :   if (this->is_erroneous_call())
   12692                 :             :     {
   12693                 :        3209 :       go_assert(saw_errors());
   12694                 :        3209 :       return Expression::make_error(this->location());
   12695                 :             :     }
   12696                 :             : 
   12697                 :     1035637 :   if (this->is_flattened_)
   12698                 :      153795 :     return this;
   12699                 :      881842 :   this->is_flattened_ = true;
   12700                 :             : 
   12701                 :             :   // Add temporary variables for all arguments that require type
   12702                 :             :   // conversion.
   12703                 :      881842 :   Function_type* fntype = this->get_function_type();
   12704                 :      881842 :   if (fntype == NULL)
   12705                 :             :     {
   12706                 :           0 :       go_assert(saw_errors());
   12707                 :           0 :       return this;
   12708                 :             :     }
   12709                 :      829093 :   if (this->args_ != NULL && !this->args_->empty()
   12710                 :     1702728 :       && fntype->parameters() != NULL && !fntype->parameters()->empty())
   12711                 :             :     {
   12712                 :      719539 :       bool is_interface_method =
   12713                 :      719539 :         this->fn_->interface_field_reference_expression() != NULL;
   12714                 :             : 
   12715                 :      719539 :       Expression_list *args = new Expression_list();
   12716                 :      719539 :       Typed_identifier_list::const_iterator pp = fntype->parameters()->begin();
   12717                 :      719539 :       Expression_list::const_iterator pa = this->args_->begin();
   12718                 :      719539 :       if (!is_interface_method && fntype->is_method())
   12719                 :             :         {
   12720                 :             :           // The receiver argument.
   12721                 :      155664 :           args->push_back(*pa);
   12722                 :      155664 :           ++pa;
   12723                 :             :         }
   12724                 :     2246950 :       for (; pa != this->args_->end(); ++pa, ++pp)
   12725                 :             :         {
   12726                 :     1527411 :           go_assert(pp != fntype->parameters()->end());
   12727                 :     1527411 :           if (Type::are_identical(pp->type(), (*pa)->type(),
   12728                 :             :                                   Type::COMPARE_TAGS, NULL))
   12729                 :     1511287 :             args->push_back(*pa);
   12730                 :             :           else
   12731                 :             :             {
   12732                 :       16124 :               Location loc = (*pa)->location();
   12733                 :       16124 :               Expression* arg = *pa;
   12734                 :       16124 :               if (!arg->is_multi_eval_safe())
   12735                 :             :                 {
   12736                 :       14979 :                   Temporary_statement *temp =
   12737                 :       14979 :                     Statement::make_temporary(NULL, arg, loc);
   12738                 :       14979 :                   inserter->insert(temp);
   12739                 :       14979 :                   arg = Expression::make_temporary_reference(temp, loc);
   12740                 :             :                 }
   12741                 :       16124 :               arg = Expression::convert_for_assignment(gogo, pp->type(), arg,
   12742                 :             :                                                        loc);
   12743                 :       16124 :               args->push_back(arg);
   12744                 :             :             }
   12745                 :             :         }
   12746                 :      719539 :       delete this->args_;
   12747                 :      719539 :       this->args_ = args;
   12748                 :             :     }
   12749                 :             : 
   12750                 :             :   // Lower to compiler intrinsic if possible.
   12751                 :      881842 :   Func_expression* fe = this->fn_->func_expression();
   12752                 :      879345 :   if (!this->is_concurrent_ && !this->is_deferred_
   12753                 :      870820 :       && fe != NULL
   12754                 :     1702185 :       && (fe->named_object()->is_function_declaration()
   12755                 :      345640 :           || fe->named_object()->is_function()))
   12756                 :             :     {
   12757                 :      820343 :       Expression* ret = this->intrinsify(gogo, inserter);
   12758                 :      820343 :       if (ret != NULL)
   12759                 :             :         {
   12760                 :        6161 :           ret->determine_type_no_context(gogo);
   12761                 :        6161 :           return ret;
   12762                 :             :         }
   12763                 :             :     }
   12764                 :             : 
   12765                 :             :   // Add an implicit conversion to a boolean type, if needed.  See the
   12766                 :             :   // comment in Binary_expression::lower_array_comparison.
   12767                 :      875681 :   if (this->is_equal_function_
   12768                 :         961 :       && this->type_ != NULL
   12769                 :      876642 :       && this->type_ != Type::lookup_bool_type())
   12770                 :           0 :     return Expression::make_cast(this->type_, this, this->location());
   12771                 :             : 
   12772                 :      875681 :   return this;
   12773                 :             : }
   12774                 :             : 
   12775                 :             : // Lower a call to a compiler intrinsic if possible.
   12776                 :             : // Returns NULL if it is not an intrinsic.
   12777                 :             : 
   12778                 :             : Expression*
   12779                 :      820343 : Call_expression::intrinsify(Gogo* gogo,
   12780                 :             :                             Statement_inserter* inserter)
   12781                 :             : {
   12782                 :      820343 :   Func_expression* fe = this->fn_->func_expression();
   12783                 :      820343 :   Named_object* no = fe->named_object();
   12784                 :      820343 :   std::string name = Gogo::unpack_hidden_name(no->name());
   12785                 :      820343 :   std::string package = (no->package() != NULL
   12786                 :      264840 :                          ? no->package()->pkgpath()
   12787                 :     1085183 :                          : gogo->pkgpath());
   12788                 :      820343 :   bool is_method = ((no->is_function() && no->func_value()->is_method())
   12789                 :     1030879 :                     || (no->is_function_declaration()
   12790                 :      474703 :                         && no->func_declaration_value()->is_method()));
   12791                 :      820343 :   Location loc = this->location();
   12792                 :             : 
   12793                 :      820343 :   Type* int_type = Type::lookup_integer_type("int");
   12794                 :      820343 :   Type* int32_type = Type::lookup_integer_type("int32");
   12795                 :      820343 :   Type* int64_type = Type::lookup_integer_type("int64");
   12796                 :      820343 :   Type* uint_type = Type::lookup_integer_type("uint");
   12797                 :      820343 :   Type* uint8_type = Type::lookup_integer_type("uint8");
   12798                 :      820343 :   Type* uint32_type = Type::lookup_integer_type("uint32");
   12799                 :      820343 :   Type* uint64_type = Type::lookup_integer_type("uint64");
   12800                 :      820343 :   Type* uintptr_type = Type::lookup_integer_type("uintptr");
   12801                 :      820343 :   Type* pointer_type = Type::make_pointer_type(Type::make_void_type());
   12802                 :             : 
   12803                 :     1640686 :   int int_size = int_type->named_type()->real_type()->integer_type()->bits() / 8;
   12804                 :     1640686 :   int ptr_size = uintptr_type->named_type()->real_type()->integer_type()->bits() / 8;
   12805                 :             : 
   12806                 :      820343 :   if (package == "sync/atomic")
   12807                 :             :     {
   12808                 :        2095 :       if (is_method)
   12809                 :             :         return NULL;
   12810                 :             : 
   12811                 :             :       // sync/atomic functions and runtime/internal/atomic functions
   12812                 :             :       // are very similar. In order not to duplicate code, we just
   12813                 :             :       // redirect to the latter and let the code below to handle them.
   12814                 :             :       // Note: no StorePointer, SwapPointer, and CompareAndSwapPointer,
   12815                 :             :       // as they need write barriers.
   12816                 :        1717 :       if (name == "LoadInt32")
   12817                 :         201 :         name = "Loadint32";
   12818                 :        1516 :       else if (name == "LoadInt64")
   12819                 :          39 :         name = "Loadint64";
   12820                 :        1477 :       else if (name == "LoadUint32")
   12821                 :         147 :         name = "Load";
   12822                 :        1330 :       else if (name == "LoadUint64")
   12823                 :         104 :         name = "Load64";
   12824                 :        1226 :       else if (name == "LoadUintptr")
   12825                 :          16 :         name = "Loaduintptr";
   12826                 :        1210 :       else if (name == "LoadPointer")
   12827                 :         102 :         name = "Loadp";
   12828                 :        1108 :       else if (name == "StoreInt32")
   12829                 :         125 :         name = "Storeint32";
   12830                 :         983 :       else if (name == "StoreInt64")
   12831                 :          20 :         name = "Storeint64";
   12832                 :         963 :       else if (name == "StoreUint32")
   12833                 :         111 :         name = "Store";
   12834                 :         852 :       else if (name == "StoreUint64")
   12835                 :          25 :         name = "Store64";
   12836                 :         827 :       else if (name == "StoreUintptr")
   12837                 :          12 :         name = "Storeuintptr";
   12838                 :         815 :       else if (name == "AddInt32")
   12839                 :         161 :         name = "Xaddint32";
   12840                 :         654 :       else if (name == "AddInt64")
   12841                 :          40 :         name = "Xaddint64";
   12842                 :         614 :       else if (name == "AddUint32")
   12843                 :          52 :         name = "Xadd";
   12844                 :         562 :       else if (name == "AddUint64")
   12845                 :          57 :         name = "Xadd64";
   12846                 :         505 :       else if (name == "AddUintptr")
   12847                 :          14 :         name = "Xadduintptr";
   12848                 :         491 :       else if (name == "SwapInt32")
   12849                 :           6 :         name = "Xchgint32";
   12850                 :         485 :       else if (name == "SwapInt64")
   12851                 :           6 :         name = "Xchgint64";
   12852                 :         479 :       else if (name == "SwapUint32")
   12853                 :           7 :         name = "Xchg";
   12854                 :         472 :       else if (name == "SwapUint64")
   12855                 :           6 :         name = "Xchg64";
   12856                 :         466 :       else if (name == "SwapUintptr")
   12857                 :           7 :         name = "Xchguintptr";
   12858                 :         459 :       else if (name == "CompareAndSwapInt32")
   12859                 :         101 :         name = "Casint32";
   12860                 :         358 :       else if (name == "CompareAndSwapInt64")
   12861                 :           8 :         name = "Casint64";
   12862                 :         350 :       else if (name == "CompareAndSwapUint32")
   12863                 :          21 :         name = "Cas";
   12864                 :         329 :       else if (name == "CompareAndSwapUint64")
   12865                 :          60 :         name = "Cas64";
   12866                 :         269 :       else if (name == "CompareAndSwapUintptr")
   12867                 :          16 :         name = "Casuintptr";
   12868                 :             :       else
   12869                 :             :         return NULL;
   12870                 :             : 
   12871                 :        1464 :       package = "runtime/internal/atomic";
   12872                 :             :     }
   12873                 :             : 
   12874                 :      819712 :   if (package == "runtime/internal/sys")
   12875                 :             :     {
   12876                 :         308 :       if (is_method)
   12877                 :             :         return NULL;
   12878                 :             : 
   12879                 :             :       // runtime/internal/sys functions and math/bits functions
   12880                 :             :       // are very similar. In order not to duplicate code, we just
   12881                 :             :       // redirect to the latter and let the code below to handle them.
   12882                 :         308 :       if (name == "Bswap32")
   12883                 :           2 :         name = "ReverseBytes32";
   12884                 :         306 :       else if (name == "Bswap64")
   12885                 :           2 :         name = "ReverseBytes64";
   12886                 :         304 :       else if (name == "Ctz32")
   12887                 :           5 :         name = "TrailingZeros32";
   12888                 :         299 :       else if (name == "Ctz64")
   12889                 :          27 :         name = "TrailingZeros64";
   12890                 :             :       else
   12891                 :             :         return NULL;
   12892                 :             : 
   12893                 :          36 :       package = "math/bits";
   12894                 :             :     }
   12895                 :             : 
   12896                 :      819440 :   if (package == "runtime")
   12897                 :             :     {
   12898                 :       72980 :       if (is_method)
   12899                 :             :         return NULL;
   12900                 :             : 
   12901                 :             :       // Handle a couple of special runtime functions.  In the runtime
   12902                 :             :       // package, getcallerpc returns the PC of the caller, and
   12903                 :             :       // getcallersp returns the frame pointer of the caller.  Implement
   12904                 :             :       // these by turning them into calls to GCC builtin functions.  We
   12905                 :             :       // could implement them in normal code, but then we would have to
   12906                 :             :       // explicitly unwind the stack.  These functions are intended to be
   12907                 :             :       // efficient.  Note that this technique obviously only works for
   12908                 :             :       // direct calls, but that is the only way they are used.
   12909                 :       50898 :       if (name == "getcallerpc"
   12910                 :       50898 :           && (this->args_ == NULL || this->args_->size() == 0))
   12911                 :             :         {
   12912                 :         216 :           Expression* arg = Expression::make_integer_ul(0, uint32_type, loc);
   12913                 :         216 :           Expression* call =
   12914                 :         216 :             Runtime::make_call(gogo, Runtime::BUILTIN_RETURN_ADDRESS, loc,
   12915                 :             :                                1, arg);
   12916                 :             :           // The builtin functions return void*, but the Go functions return uintptr.
   12917                 :         216 :           return Expression::make_cast(uintptr_type, call, loc);
   12918                 :             :         }
   12919                 :       50682 :       else if (name == "getcallersp"
   12920                 :       50682 :                && (this->args_ == NULL || this->args_->size() == 0))
   12921                 :             : 
   12922                 :             :         {
   12923                 :          28 :           Expression* call =
   12924                 :          28 :             Runtime::make_call(gogo, Runtime::BUILTIN_DWARF_CFA, loc, 0);
   12925                 :             :           // The builtin functions return void*, but the Go functions return uintptr.
   12926                 :          28 :           return Expression::make_cast(uintptr_type, call, loc);
   12927                 :             :         }
   12928                 :             :     }
   12929                 :      746460 :   else if (package == "math/bits")
   12930                 :             :     {
   12931                 :       12928 :       if (is_method)
   12932                 :             :         return NULL;
   12933                 :             : 
   12934                 :       12922 :       if ((name == "ReverseBytes16" || name == "ReverseBytes32"
   12935                 :       12902 :            || name == "ReverseBytes64" || name == "ReverseBytes")
   12936                 :       12988 :           && this->args_ != NULL && this->args_->size() == 1)
   12937                 :             :         {
   12938                 :          66 :           Runtime::Function code;
   12939                 :          66 :           if (name == "ReverseBytes16")
   12940                 :             :             code = Runtime::BUILTIN_BSWAP16;
   12941                 :          60 :           else if (name == "ReverseBytes32")
   12942                 :             :             code = Runtime::BUILTIN_BSWAP32;
   12943                 :          40 :           else if (name == "ReverseBytes64")
   12944                 :             :             code = Runtime::BUILTIN_BSWAP64;
   12945                 :           4 :           else if (name == "ReverseBytes")
   12946                 :           4 :             code = (int_size == 8 ? Runtime::BUILTIN_BSWAP64 : Runtime::BUILTIN_BSWAP32);
   12947                 :             :           else
   12948                 :           0 :             go_unreachable();
   12949                 :          66 :           Expression* arg = this->args_->front();
   12950                 :          66 :           Expression* call = Runtime::make_call(gogo, code, loc, 1, arg);
   12951                 :          66 :           if (name == "ReverseBytes")
   12952                 :           4 :             return Expression::make_cast(uint_type, call, loc);
   12953                 :             :           return call;
   12954                 :             :         }
   12955                 :       12856 :       else if ((name == "TrailingZeros8" || name == "TrailingZeros16")
   12956                 :       12868 :                && this->args_ != NULL && this->args_->size() == 1)
   12957                 :             :         {
   12958                 :             :           // GCC does not have a ctz8 or ctz16 intrinsic. We do
   12959                 :             :           // ctz32(0x100 | arg) or ctz32(0x10000 | arg).
   12960                 :          12 :           Expression* arg = this->args_->front();
   12961                 :          12 :           arg = Expression::make_cast(uint32_type, arg, loc);
   12962                 :          12 :           unsigned long mask = (name == "TrailingZeros8" ? 0x100 : 0x10000);
   12963                 :          12 :           Expression* c = Expression::make_integer_ul(mask, uint32_type, loc);
   12964                 :          12 :           arg = Expression::make_binary(OPERATOR_OR, arg, c, loc);
   12965                 :          12 :           Expression* call = Runtime::make_call(gogo, Runtime::BUILTIN_CTZ,
   12966                 :             :                                                 loc, 1, arg);
   12967                 :          12 :           return Expression::make_cast(int_type, call, loc);
   12968                 :             :         }
   12969                 :       12850 :       else if ((name == "TrailingZeros32"
   12970                 :       12823 :                 || (name == "TrailingZeros" && int_size == 4))
   12971                 :       12861 :                && this->args_ != NULL && this->args_->size() == 1)
   12972                 :             :         {
   12973                 :          38 :           Expression* arg = this->args_->front();
   12974                 :          38 :           if (!arg->is_multi_eval_safe())
   12975                 :             :             {
   12976                 :          28 :               Temporary_statement* ts = Statement::make_temporary(uint32_type, arg, loc);
   12977                 :          28 :               inserter->insert(ts);
   12978                 :          28 :               arg = Expression::make_temporary_reference(ts, loc);
   12979                 :             :             }
   12980                 :             :           // arg == 0 ? 32 : __builtin_ctz(arg)
   12981                 :          38 :           Expression* zero = Expression::make_integer_ul(0, uint32_type, loc);
   12982                 :          38 :           Expression* cmp = Expression::make_binary(OPERATOR_EQEQ, arg, zero, loc);
   12983                 :          38 :           Expression* c32 = Expression::make_integer_ul(32, int_type, loc);
   12984                 :          38 :           Expression* call = Runtime::make_call(gogo, Runtime::BUILTIN_CTZ,
   12985                 :             :                                                 loc, 1, arg->copy());
   12986                 :          38 :           call = Expression::make_cast(int_type, call, loc);
   12987                 :          38 :           return Expression::make_conditional(cmp, c32, call, loc);
   12988                 :             :         }
   12989                 :       12812 :       else if ((name == "TrailingZeros64"
   12990                 :       12747 :                 || (name == "TrailingZeros" && int_size == 8))
   12991                 :       12823 :                && this->args_ != NULL && this->args_->size() == 1)
   12992                 :             :         {
   12993                 :          76 :           Expression* arg = this->args_->front();
   12994                 :          76 :           if (!arg->is_multi_eval_safe())
   12995                 :             :             {
   12996                 :          52 :               Temporary_statement* ts = Statement::make_temporary(uint64_type, arg, loc);
   12997                 :          52 :               inserter->insert(ts);
   12998                 :          52 :               arg = Expression::make_temporary_reference(ts, loc);
   12999                 :             :             }
   13000                 :             :           // arg == 0 ? 64 : __builtin_ctzll(arg)
   13001                 :          76 :           Expression* zero = Expression::make_integer_ul(0, uint64_type, loc);
   13002                 :          76 :           Expression* cmp = Expression::make_binary(OPERATOR_EQEQ, arg, zero, loc);
   13003                 :          76 :           Expression* c64 = Expression::make_integer_ul(64, int_type, loc);
   13004                 :          76 :           Expression* call = Runtime::make_call(gogo, Runtime::BUILTIN_CTZLL,
   13005                 :             :                                                 loc, 1, arg->copy());
   13006                 :          76 :           call = Expression::make_cast(int_type, call, loc);
   13007                 :          76 :           return Expression::make_conditional(cmp, c64, call, loc);
   13008                 :             :         }
   13009                 :       12730 :       else if ((name == "LeadingZeros8" || name == "LeadingZeros16"
   13010                 :       12724 :                 || name == "Len8" || name == "Len16")
   13011                 :       12766 :                && this->args_ != NULL && this->args_->size() == 1)
   13012                 :             :         {
   13013                 :             :           // GCC does not have a clz8 ir clz16 intrinsic. We do
   13014                 :             :           // clz32(arg<<24 | 0xffffff) or clz32(arg<<16 | 0xffff).
   13015                 :          36 :           Expression* arg = this->args_->front();
   13016                 :          36 :           arg = Expression::make_cast(uint32_type, arg, loc);
   13017                 :          36 :           unsigned long shift =
   13018                 :          36 :             ((name == "LeadingZeros8" || name == "Len8") ? 24 : 16);
   13019                 :          36 :           Expression* c = Expression::make_integer_ul(shift, uint32_type, loc);
   13020                 :          36 :           arg = Expression::make_binary(OPERATOR_LSHIFT, arg, c, loc);
   13021                 :          36 :           unsigned long mask =
   13022                 :          36 :             ((name == "LeadingZeros8" || name == "Len8") ? 0xffffff : 0xffff);
   13023                 :          36 :           c = Expression::make_integer_ul(mask, uint32_type, loc);
   13024                 :          36 :           arg = Expression::make_binary(OPERATOR_OR, arg, c, loc);
   13025                 :          36 :           Expression* call = Runtime::make_call(gogo, Runtime::BUILTIN_CLZ,
   13026                 :             :                                                 loc, 1, arg);
   13027                 :          36 :           call = Expression::make_cast(int_type, call, loc);
   13028                 :             :           // len = width - clz
   13029                 :          36 :           if (name == "Len8")
   13030                 :             :             {
   13031                 :          12 :               c = Expression::make_integer_ul(8, int_type, loc);
   13032                 :          12 :               return Expression::make_binary(OPERATOR_MINUS, c, call, loc);
   13033                 :             :             }
   13034                 :          24 :           else if (name == "Len16")
   13035                 :             :             {
   13036                 :          12 :               c = Expression::make_integer_ul(16, int_type, loc);
   13037                 :          12 :               return Expression::make_binary(OPERATOR_MINUS, c, call, loc);
   13038                 :             :             }
   13039                 :             :           return call;
   13040                 :             :         }
   13041                 :       12694 :       else if ((name == "LeadingZeros32" || name == "Len32"
   13042                 :       12669 :                 || ((name == "LeadingZeros" || name == "Len") && int_size == 4))
   13043                 :       12745 :                && this->args_ != NULL && this->args_->size() == 1)
   13044                 :             :         {
   13045                 :          51 :           Expression* arg = this->args_->front();
   13046                 :          51 :           if (!arg->is_multi_eval_safe())
   13047                 :             :             {
   13048                 :          26 :               Temporary_statement* ts = Statement::make_temporary(uint32_type, arg, loc);
   13049                 :          26 :               inserter->insert(ts);
   13050                 :          26 :               arg = Expression::make_temporary_reference(ts, loc);
   13051                 :             :             }
   13052                 :             :           // arg == 0 ? 32 : __builtin_clz(arg)
   13053                 :          51 :           Expression* zero = Expression::make_integer_ul(0, uint32_type, loc);
   13054                 :          51 :           Expression* cmp = Expression::make_binary(OPERATOR_EQEQ, arg, zero, loc);
   13055                 :          51 :           Expression* c32 = Expression::make_integer_ul(32, int_type, loc);
   13056                 :          51 :           Expression* call = Runtime::make_call(gogo, Runtime::BUILTIN_CLZ,
   13057                 :             :                                                 loc, 1, arg->copy());
   13058                 :          51 :           call = Expression::make_cast(int_type, call, loc);
   13059                 :          51 :           Expression* cond = Expression::make_conditional(cmp, c32, call, loc);
   13060                 :             :           // len = 32 - clz
   13061                 :          51 :           if (name == "Len32" || name == "Len")
   13062                 :          40 :             return Expression::make_binary(OPERATOR_MINUS, c32->copy(), cond, loc);
   13063                 :             :           return cond;
   13064                 :             :         }
   13065                 :       12569 :       else if ((name == "LeadingZeros64" || name == "Len64"
   13066                 :       12485 :                 || ((name == "LeadingZeros" || name == "Len") && int_size == 8))
   13067                 :       12753 :                && this->args_ != NULL && this->args_->size() == 1)
   13068                 :             :         {
   13069                 :         184 :           Expression* arg = this->args_->front();
   13070                 :         184 :           if (!arg->is_multi_eval_safe())
   13071                 :             :             {
   13072                 :          51 :               Temporary_statement* ts = Statement::make_temporary(uint64_type, arg, loc);
   13073                 :          51 :               inserter->insert(ts);
   13074                 :          51 :               arg = Expression::make_temporary_reference(ts, loc);
   13075                 :             :             }
   13076                 :             :           // arg == 0 ? 64 : __builtin_clzll(arg)
   13077                 :         184 :           Expression* zero = Expression::make_integer_ul(0, uint64_type, loc);
   13078                 :         184 :           Expression* cmp = Expression::make_binary(OPERATOR_EQEQ, arg, zero, loc);
   13079                 :         184 :           Expression* c64 = Expression::make_integer_ul(64, int_type, loc);
   13080                 :         184 :           Expression* call = Runtime::make_call(gogo, Runtime::BUILTIN_CLZLL,
   13081                 :             :                                                 loc, 1, arg->copy());
   13082                 :         184 :           call = Expression::make_cast(int_type, call, loc);
   13083                 :         184 :           Expression* cond = Expression::make_conditional(cmp, c64, call, loc);
   13084                 :             :           // len = 64 - clz
   13085                 :         184 :           if (name == "Len64" || name == "Len")
   13086                 :          99 :             return Expression::make_binary(OPERATOR_MINUS, c64->copy(), cond, loc);
   13087                 :             :           return cond;
   13088                 :             :         }
   13089                 :       12441 :       else if ((name == "OnesCount8" || name == "OnesCount16"
   13090                 :       12435 :            || name == "OnesCount32" || name == "OnesCount64"
   13091                 :       12411 :            || name == "OnesCount")
   13092                 :       12501 :           && this->args_ != NULL && this->args_->size() == 1)
   13093                 :             :         {
   13094                 :          60 :           Runtime::Function code;
   13095                 :          60 :           if (name == "OnesCount64")
   13096                 :             :             code = Runtime::BUILTIN_POPCOUNTLL;
   13097                 :          46 :           else if (name == "OnesCount")
   13098                 :           6 :             code = (int_size == 8 ? Runtime::BUILTIN_POPCOUNTLL : Runtime::BUILTIN_POPCOUNT);
   13099                 :             :           else
   13100                 :             :             code = Runtime::BUILTIN_POPCOUNT;
   13101                 :          60 :           Expression* arg = this->args_->front();
   13102                 :          60 :           Expression* call = Runtime::make_call(gogo, code, loc, 1, arg);
   13103                 :          60 :           return Expression::make_cast(int_type, call, loc);
   13104                 :             :         }
   13105                 :             :     }
   13106                 :      733532 :   else if (package == "runtime/internal/atomic")
   13107                 :             :     {
   13108                 :        6502 :       int memorder = __ATOMIC_SEQ_CST;
   13109                 :             : 
   13110                 :        6502 :       if (is_method)
   13111                 :             :         {
   13112                 :         580 :           Function_type* ftype = (no->is_function()
   13113                 :         580 :                                   ? no->func_value()->type()
   13114                 :         568 :                                   : no->func_declaration_value()->type());
   13115                 :         580 :           Type* rtype = ftype->receiver()->type()->deref();
   13116                 :         580 :           go_assert(rtype->named_type() != NULL);
   13117                 :         580 :           const std::string& rname(rtype->named_type()->name());
   13118                 :         580 :           if (rname == "Int32")
   13119                 :             :             {
   13120                 :          42 :               if (name == "Load")
   13121                 :          35 :                 name = "LoadInt32";
   13122                 :           7 :               else if (name == "Store")
   13123                 :           7 :                 name = "Storeint32";
   13124                 :           0 :               else if (name == "CompareAndSwap")
   13125                 :           0 :                 name = "Casint32";
   13126                 :           0 :               else if (name == "Swap")
   13127                 :           0 :                 name = "Xchgint32";
   13128                 :           0 :               else if (name == "Add")
   13129                 :           0 :                 name = "Xaddint32";
   13130                 :             :               else
   13131                 :           0 :                 go_unreachable();
   13132                 :             :             }
   13133                 :         538 :           else if (rname == "Int64")
   13134                 :             :             {
   13135                 :         100 :               if (name == "Load")
   13136                 :          42 :                 name = "LoadInt64";
   13137                 :          58 :               else if (name == "Store")
   13138                 :          21 :                 name = "Storeint64";
   13139                 :          37 :               else if (name == "CompareAndSwap")
   13140                 :           0 :                 name = "Casint64";
   13141                 :          37 :               else if (name == "Swap")
   13142                 :           0 :                 name = "Xchgint64";
   13143                 :          37 :               else if (name == "Add")
   13144                 :          37 :                 name = "Xaddint64";
   13145                 :             :               else
   13146                 :           0 :                 go_unreachable();
   13147                 :             :             }
   13148                 :         438 :           else if (rname == "Uint8")
   13149                 :             :             {
   13150                 :           0 :               if (name == "Load")
   13151                 :           0 :                 name = "Load8";
   13152                 :           0 :               else if (name == "Store")
   13153                 :           0 :                 name = "Store8";
   13154                 :           0 :               else if (name == "And")
   13155                 :           0 :                 name = "And8";
   13156                 :           0 :               else if (name == "Or")
   13157                 :           0 :                 name = "Or8";
   13158                 :             :               else
   13159                 :           0 :                 go_unreachable();
   13160                 :             :             }
   13161                 :         438 :           else if (rname == "Uint32")
   13162                 :             :             {
   13163                 :         119 :               if (name == "Load")
   13164                 :          70 :                 name = "Load";
   13165                 :          49 :               else if (name == "LoadAcquire")
   13166                 :           0 :                 name = "LoadAcq";
   13167                 :          49 :               else if (name == "Store")
   13168                 :          14 :                 name = "Store";
   13169                 :          35 :               else if (name == "CompareAndSwap")
   13170                 :          35 :                 name = "Cas";
   13171                 :           0 :               else if (name == "CompareAndSwapRelease")
   13172                 :           0 :                 name = "CasRel";
   13173                 :           0 :               else if (name == "Swap")
   13174                 :           0 :                 name = "Xchg";
   13175                 :           0 :               else if (name == "And")
   13176                 :           0 :                 name = "And";
   13177                 :           0 :               else if (name == "Or")
   13178                 :           0 :                 name = "Or";
   13179                 :           0 :               else if (name == "Add")
   13180                 :           0 :                 name = "Xadd";
   13181                 :             :               else
   13182                 :           0 :                 go_unreachable();
   13183                 :             :             }
   13184                 :         319 :           else if (rname == "Uint64")
   13185                 :             :             {
   13186                 :         120 :               if (name == "Load")
   13187                 :          58 :                 name = "Load64";
   13188                 :          62 :               else if (name == "Store")
   13189                 :          34 :                 name = "Store64";
   13190                 :          28 :               else if (name == "CompareAndSwap")
   13191                 :           0 :                 name = "Cas64";
   13192                 :          28 :               else if (name == "Swap")
   13193                 :           0 :                 name = "Xchgt64";
   13194                 :          28 :               else if (name == "Add")
   13195                 :          28 :                 name = "Xadd64";
   13196                 :             :               else
   13197                 :           0 :                 go_unreachable();
   13198                 :             :             }
   13199                 :         199 :           else if (rname == "Uintptr")
   13200                 :             :             {
   13201                 :         133 :               if (name == "Load")
   13202                 :          49 :                 name = "Loaduintptr";
   13203                 :          84 :               else if (name == "LoadAcquire")
   13204                 :           0 :                 name = "Loadacquintptr";
   13205                 :          84 :               else if (name == "Store")
   13206                 :          35 :                 name = "Storeuintptr";
   13207                 :          49 :               else if (name == "StoreRelease")
   13208                 :           0 :                 name = "StoreReluintptr";
   13209                 :          49 :               else if (name == "CompareAndSwap")
   13210                 :          28 :                 name = "Casuintptr";
   13211                 :          21 :               else if (name == "Swap")
   13212                 :           7 :                 name = "Xchguintptr";
   13213                 :          14 :               else if (name == "Add")
   13214                 :          14 :                 name = "Xadduintptr";
   13215                 :             :               else
   13216                 :           0 :                 go_unreachable();
   13217                 :             :             }
   13218                 :          66 :           else if (rname == "Float64")
   13219                 :             :             {
   13220                 :             :               // Needs unsafe type conversion.  Don't intrinsify for now.
   13221                 :             :               return NULL;
   13222                 :             :             }
   13223                 :           0 :           else if (rname == "UnsafePointer")
   13224                 :             :             {
   13225                 :           0 :               if (name == "Load")
   13226                 :           0 :                 name = "Loadp";
   13227                 :           0 :               else if (name == "StoreNoWB")
   13228                 :           0 :                 name = "StorepoWB";
   13229                 :           0 :               else if (name == "CompareAndSwapNoWB")
   13230                 :           0 :                 name = "Casp1";
   13231                 :             :               else
   13232                 :           0 :                 go_unreachable();
   13233                 :             :             }
   13234                 :             :           else
   13235                 :           0 :             go_unreachable();
   13236                 :             :         }
   13237                 :             : 
   13238                 :        5460 :       if ((name == "Load" || name == "Load64" || name == "Loadint64" || name == "Loadp"
   13239                 :        4594 :            || name == "Loaduint" || name == "Loaduintptr" || name == "LoadAcq"
   13240                 :        4321 :            || name == "Loadint32" || name == "Load8")
   13241                 :        7827 :           && this->args_ != NULL && this->args_->size() == 1)
   13242                 :             :         {
   13243                 :        2367 :           if (int_size < 8 && (name == "Load64" || name == "Loadint64"))
   13244                 :             :             // On 32-bit architectures we need to check alignment.
   13245                 :             :             // Not intrinsify for now.
   13246                 :             :             return NULL;
   13247                 :             : 
   13248                 :        2075 :           Runtime::Function code;
   13249                 :        2075 :           Type* res_type;
   13250                 :        2075 :           if (name == "Load")
   13251                 :             :             {
   13252                 :             :               code = Runtime::ATOMIC_LOAD_4;
   13253                 :             :               res_type = uint32_type;
   13254                 :             :             }
   13255                 :        1099 :           else if (name == "Load64")
   13256                 :             :             {
   13257                 :             :               code = Runtime::ATOMIC_LOAD_8;
   13258                 :             :               res_type = uint64_type;
   13259                 :             :             }
   13260                 :         768 :           else if (name == "Loadint32")
   13261                 :             :             {
   13262                 :             :               code = Runtime::ATOMIC_LOAD_4;
   13263                 :             :               res_type = int32_type;
   13264                 :             :             }
   13265                 :         561 :           else if (name == "Loadint64")
   13266                 :             :             {
   13267                 :             :               code = Runtime::ATOMIC_LOAD_8;
   13268                 :             :               res_type = int64_type;
   13269                 :             :             }
   13270                 :         524 :           else if (name == "Loaduint")
   13271                 :             :             {
   13272                 :           7 :               code = (int_size == 8
   13273                 :           7 :                       ? Runtime::ATOMIC_LOAD_8
   13274                 :             :                       : Runtime::ATOMIC_LOAD_4);
   13275                 :             :               res_type = uint_type;
   13276                 :             :             }
   13277                 :         517 :           else if (name == "Loaduintptr")
   13278                 :             :             {
   13279                 :         211 :               code = (ptr_size == 8
   13280                 :         211 :                       ? Runtime::ATOMIC_LOAD_8
   13281                 :             :                       : Runtime::ATOMIC_LOAD_4);
   13282                 :             :               res_type = uintptr_type;
   13283                 :             :             }
   13284                 :         306 :           else if (name == "Loadp")
   13285                 :             :             {
   13286                 :         206 :               code = (ptr_size == 8
   13287                 :         206 :                       ? Runtime::ATOMIC_LOAD_8
   13288                 :             :                       : Runtime::ATOMIC_LOAD_4);
   13289                 :             :               res_type = pointer_type;
   13290                 :             :             }
   13291                 :         100 :           else if (name == "LoadAcq")
   13292                 :             :             {
   13293                 :             :               code = Runtime::ATOMIC_LOAD_4;
   13294                 :             :               res_type = uint32_type;
   13295                 :             :               memorder = __ATOMIC_ACQUIRE;
   13296                 :             :             }
   13297                 :          45 :           else if (name == "Load8")
   13298                 :             :             {
   13299                 :             :               code = Runtime::ATOMIC_LOAD_1;
   13300                 :             :               res_type = uint8_type;
   13301                 :             :             }
   13302                 :             :           else
   13303                 :           0 :             go_unreachable();
   13304                 :        2075 :           Expression* a1 = this->args_->front();
   13305                 :        2075 :           Expression* a2 = Expression::make_integer_ul(memorder, int32_type, loc);
   13306                 :        2075 :           Expression* call = Runtime::make_call(gogo, code, loc, 2, a1, a2);
   13307                 :        2075 :           return Expression::make_unsafe_cast(res_type, call, loc);
   13308                 :             :         }
   13309                 :             : 
   13310                 :        3586 :       if ((name == "Store" || name == "Store64" || name == "StorepNoWB"
   13311                 :        3226 :            || name == "Storeuintptr" || name == "StoreRel"
   13312                 :        3097 :            || name == "Storeint32" || name == "Storeint64")
   13313                 :        4743 :           && this->args_ != NULL && this->args_->size() == 2)
   13314                 :             :         {
   13315                 :        1157 :           if (int_size < 8 && (name == "Store64" || name == "Storeint64"))
   13316                 :             :             return NULL;
   13317                 :             : 
   13318                 :        1019 :           Runtime::Function code;
   13319                 :        1019 :           Expression* a1 = this->args_->at(0);
   13320                 :        1019 :           Expression* a2 = this->args_->at(1);
   13321                 :        1019 :           if (name == "Store")
   13322                 :             :             code = Runtime::ATOMIC_STORE_4;
   13323                 :         536 :           else if (name == "Store64")
   13324                 :             :             code = Runtime::ATOMIC_STORE_8;
   13325                 :         391 :           else if (name == "Storeint32")
   13326                 :             :             code = Runtime::ATOMIC_STORE_4;
   13327                 :         253 :           else if (name == "Storeint64")
   13328                 :             :             code = Runtime::ATOMIC_STORE_8;
   13329                 :         228 :           else if (name == "Storeuintptr")
   13330                 :         102 :             code = (ptr_size == 8 ? Runtime::ATOMIC_STORE_8 : Runtime::ATOMIC_STORE_4);
   13331                 :         126 :           else if (name == "StorepNoWB")
   13332                 :             :             {
   13333                 :          99 :               code = (ptr_size == 8 ? Runtime::ATOMIC_STORE_8 : Runtime::ATOMIC_STORE_4);
   13334                 :          99 :               a2 = Expression::make_unsafe_cast(uintptr_type, a2, loc);
   13335                 :          99 :               a2 = Expression::make_cast(uint64_type, a2, loc);
   13336                 :             :             }
   13337                 :          27 :           else if (name == "StoreRel")
   13338                 :             :             {
   13339                 :             :               code = Runtime::ATOMIC_STORE_4;
   13340                 :             :               memorder = __ATOMIC_RELEASE;
   13341                 :             :             }
   13342                 :           0 :           else if (name == "Store8")
   13343                 :             :             code = Runtime::ATOMIC_STORE_1;
   13344                 :             :           else
   13345                 :           0 :             go_unreachable();
   13346                 :        1019 :           Expression* a3 = Expression::make_integer_ul(memorder, int32_type, loc);
   13347                 :        1019 :           return Runtime::make_call(gogo, code, loc, 3, a1, a2, a3);
   13348                 :             :         }
   13349                 :             : 
   13350                 :        2848 :       if ((name == "Xchg" || name == "Xchg64" || name == "Xchguintptr"
   13351                 :        2798 :            || name == "Xchgint32" || name == "Xchgint64")
   13352                 :        2986 :           && this->args_ != NULL && this->args_->size() == 2)
   13353                 :             :         {
   13354                 :         138 :           if (int_size < 8 && (name == "Xchg64" || name == "Xchgint64"))
   13355                 :             :             return NULL;
   13356                 :             : 
   13357                 :         118 :           Runtime::Function code;
   13358                 :         118 :           Type* res_type;
   13359                 :         118 :           if (name == "Xchg")
   13360                 :             :             {
   13361                 :             :               code = Runtime::ATOMIC_EXCHANGE_4;
   13362                 :             :               res_type = uint32_type;
   13363                 :             :             }
   13364                 :          54 :           else if (name == "Xchg64")
   13365                 :             :             {
   13366                 :             :               code = Runtime::ATOMIC_EXCHANGE_8;
   13367                 :             :               res_type = uint64_type;
   13368                 :             :             }
   13369                 :          38 :           else if (name == "Xchgint32")
   13370                 :             :             {
   13371                 :             :               code = Runtime::ATOMIC_EXCHANGE_4;
   13372                 :             :               res_type = int32_type;
   13373                 :             :             }
   13374                 :          26 :           else if (name == "Xchgint64")
   13375                 :             :             {
   13376                 :             :               code = Runtime::ATOMIC_EXCHANGE_8;
   13377                 :             :               res_type = int64_type;
   13378                 :             :             }
   13379                 :          20 :           else if (name == "Xchguintptr")
   13380                 :             :             {
   13381                 :          20 :               code = (ptr_size == 8
   13382                 :          20 :                       ? Runtime::ATOMIC_EXCHANGE_8
   13383                 :             :                       : Runtime::ATOMIC_EXCHANGE_4);
   13384                 :             :               res_type = uintptr_type;
   13385                 :             :             }
   13386                 :             :           else
   13387                 :           0 :             go_unreachable();
   13388                 :         118 :           Expression* a1 = this->args_->at(0);
   13389                 :         118 :           Expression* a2 = this->args_->at(1);
   13390                 :         118 :           Expression* a3 = Expression::make_integer_ul(memorder, int32_type, loc);
   13391                 :         118 :           Expression* call = Runtime::make_call(gogo, code, loc, 3, a1, a2, a3);
   13392                 :         118 :           return Expression::make_cast(res_type, call, loc);
   13393                 :             :         }
   13394                 :             : 
   13395                 :        2197 :       if ((name == "Cas" || name == "Cas64" || name == "Casuintptr"
   13396                 :        1963 :            || name == "Casp1" || name == "CasRel"
   13397                 :        1923 :            || name == "Casint32" || name == "Casint64")
   13398                 :        3176 :           && this->args_ != NULL && this->args_->size() == 3)
   13399                 :             :         {
   13400                 :         979 :           if (int_size < 8 && (name == "Cas64" || name == "Casint64"))
   13401                 :             :             return NULL;
   13402                 :             : 
   13403                 :         903 :           Runtime::Function code;
   13404                 :         903 :           Expression* a1 = this->args_->at(0);
   13405                 :             : 
   13406                 :             :           // Builtin cas takes a pointer to the old value.
   13407                 :             :           // Store it in a temporary and take the address.
   13408                 :         903 :           Expression* a2 = this->args_->at(1);
   13409                 :         903 :           Temporary_statement* ts = Statement::make_temporary(NULL, a2, loc);
   13410                 :         903 :           inserter->insert(ts);
   13411                 :         903 :           a2 = Expression::make_temporary_reference(ts, loc);
   13412                 :         903 :           a2 = Expression::make_unary(OPERATOR_AND, a2, loc);
   13413                 :             : 
   13414                 :         903 :           Expression* a3 = this->args_->at(2);
   13415                 :         903 :           if (name == "Cas")
   13416                 :             :             code = Runtime::ATOMIC_COMPARE_EXCHANGE_4;
   13417                 :         326 :           else if (name == "Cas64")
   13418                 :             :             code = Runtime::ATOMIC_COMPARE_EXCHANGE_8;
   13419                 :         250 :           else if (name == "Casint32")
   13420                 :             :             code = Runtime::ATOMIC_COMPARE_EXCHANGE_4;
   13421                 :         143 :           else if (name == "Casint64")
   13422                 :             :             code = Runtime::ATOMIC_COMPARE_EXCHANGE_8;
   13423                 :         132 :           else if (name == "Casuintptr")
   13424                 :          92 :             code = (ptr_size == 8
   13425                 :          92 :                     ? Runtime::ATOMIC_COMPARE_EXCHANGE_8
   13426                 :             :                     : Runtime::ATOMIC_COMPARE_EXCHANGE_4);
   13427                 :          40 :           else if (name == "Casp1")
   13428                 :             :             {
   13429                 :          12 :               code = (ptr_size == 8
   13430                 :           6 :                       ? Runtime::ATOMIC_COMPARE_EXCHANGE_8
   13431                 :             :                       : Runtime::ATOMIC_COMPARE_EXCHANGE_4);
   13432                 :           6 :               a3 = Expression::make_unsafe_cast(uintptr_type, a3, loc);
   13433                 :           6 :               a3 = Expression::make_cast(uint64_type, a3, loc);
   13434                 :             :             }
   13435                 :          34 :           else if (name == "CasRel")
   13436                 :             :             {
   13437                 :             :               code = Runtime::ATOMIC_COMPARE_EXCHANGE_4;
   13438                 :             :               memorder = __ATOMIC_RELEASE;
   13439                 :             :             }
   13440                 :             :           else
   13441                 :           0 :             go_unreachable();
   13442                 :         903 :           Expression* a4 = Expression::make_boolean(false, loc);
   13443                 :         903 :           Expression* a5 = Expression::make_integer_ul(memorder, int32_type, loc);
   13444                 :         903 :           Expression* a6 = Expression::make_integer_ul(__ATOMIC_RELAXED, int32_type, loc);
   13445                 :         903 :           return Runtime::make_call(gogo, code, loc, 6, a1, a2, a3, a4, a5, a6);
   13446                 :             :         }
   13447                 :             : 
   13448                 :        1287 :       if ((name == "Xadd" || name == "Xadd64" || name == "Xaddint64"
   13449                 :         787 :            || name == "Xadduintptr" || name == "Xaddint32")
   13450                 :        2581 :           && this->args_ != NULL && this->args_->size() == 2)
   13451                 :             :         {
   13452                 :        1294 :           if (int_size < 8 && (name == "Xadd64" || name == "Xaddint64"))
   13453                 :             :             return NULL;
   13454                 :             : 
   13455                 :        1079 :           Runtime::Function code;
   13456                 :        1079 :           Type* res_type;
   13457                 :        1079 :           if (name == "Xadd")
   13458                 :             :             {
   13459                 :             :               code = Runtime::ATOMIC_ADD_FETCH_4;
   13460                 :             :               res_type = uint32_type;
   13461                 :             :             }
   13462                 :         571 :           else if (name == "Xadd64")
   13463                 :             :             {
   13464                 :             :               code = Runtime::ATOMIC_ADD_FETCH_8;
   13465                 :             :               res_type = uint64_type;
   13466                 :             :             }
   13467                 :         432 :           else if (name == "Xaddint32")
   13468                 :             :             {
   13469                 :             :               code = Runtime::ATOMIC_ADD_FETCH_4;
   13470                 :             :               res_type = int32_type;
   13471                 :             :             }
   13472                 :         265 :           else if (name == "Xaddint64")
   13473                 :             :             {
   13474                 :             :               code = Runtime::ATOMIC_ADD_FETCH_8;
   13475                 :             :               res_type = int64_type;
   13476                 :             :             }
   13477                 :         119 :           else if (name == "Xadduintptr")
   13478                 :             :             {
   13479                 :         119 :               code = (ptr_size == 8
   13480                 :         119 :                       ? Runtime::ATOMIC_ADD_FETCH_8
   13481                 :             :                       : Runtime::ATOMIC_ADD_FETCH_4);
   13482                 :             :               res_type = uintptr_type;
   13483                 :             :             }
   13484                 :             :           else
   13485                 :           0 :             go_unreachable();
   13486                 :        1079 :           Expression* a1 = this->args_->at(0);
   13487                 :        1079 :           Expression* a2 = this->args_->at(1);
   13488                 :        1079 :           Expression* a3 = Expression::make_integer_ul(memorder, int32_type, loc);
   13489                 :        1079 :           Expression* call = Runtime::make_call(gogo, code, loc, 3, a1, a2, a3);
   13490                 :        1079 :           return Expression::make_cast(res_type, call, loc);
   13491                 :             :         }
   13492                 :             : 
   13493                 :         457 :       if ((name == "And8" || name == "Or8")
   13494                 :         580 :           && this->args_ != NULL && this->args_->size() == 2)
   13495                 :             :         {
   13496                 :         123 :           Runtime::Function code;
   13497                 :         123 :           if (name == "And8")
   13498                 :             :             code = Runtime::ATOMIC_AND_FETCH_1;
   13499                 :          79 :           else if (name == "Or8")
   13500                 :             :             code = Runtime::ATOMIC_OR_FETCH_1;
   13501                 :             :           else
   13502                 :           0 :             go_unreachable();
   13503                 :         123 :           Expression* a1 = this->args_->at(0);
   13504                 :         123 :           Expression* a2 = this->args_->at(1);
   13505                 :         123 :           Expression* a3 = Expression::make_integer_ul(memorder, int32_type, loc);
   13506                 :         123 :           return Runtime::make_call(gogo, code, loc, 3, a1, a2, a3);
   13507                 :             :         }
   13508                 :             :     }
   13509                 :      727030 :   else if (package == "internal/abi"
   13510                 :      727030 :            || package == "bootstrap/internal/abi") // for bootstrapping gc
   13511                 :             :     {
   13512                 :          77 :       if (is_method)
   13513                 :             :         return NULL;
   13514                 :             : 
   13515                 :          71 :       if ((name == "FuncPCABI0" || name == "FuncPCABIInternal")
   13516                 :          77 :           && this->args_ != NULL
   13517                 :         154 :           && this->args_->size() == 1)
   13518                 :             :         {
   13519                 :             :           // We expect to see a conversion from the expression to "any".
   13520                 :          77 :           Expression* expr = this->args_->front();
   13521                 :          77 :           Type_conversion_expression* tce = expr->conversion_expression();
   13522                 :          75 :           if (tce != NULL)
   13523                 :          75 :             expr = tce->expr();
   13524                 :          77 :           Func_expression* fe = expr->func_expression();
   13525                 :          77 :           Interface_field_reference_expression* interface_method =
   13526                 :          77 :             expr->interface_field_reference_expression();
   13527                 :          77 :           if (fe != NULL)
   13528                 :             :             {
   13529                 :          75 :               Named_object* no = fe->named_object();
   13530                 :          75 :               Expression* ref = Expression::make_func_code_reference(no, loc);
   13531                 :          75 :               Type* uintptr_type = Type::lookup_integer_type("uintptr");
   13532                 :          75 :               return Expression::make_cast(uintptr_type, ref, loc);
   13533                 :             :             }
   13534                 :           2 :           else if (interface_method != NULL)
   13535                 :           0 :             return interface_method->get_function();
   13536                 :             :           else
   13537                 :             :             {
   13538                 :           2 :               expr = this->args_->front();
   13539                 :           6 :               go_assert(expr->type()->interface_type() != NULL
   13540                 :             :                         && expr->type()->interface_type()->is_empty());
   13541                 :           2 :               expr = Expression::make_interface_info(expr,
   13542                 :             :                                                      INTERFACE_INFO_OBJECT,
   13543                 :             :                                                      loc);
   13544                 :             :               // Trust that this is a function type, which means that
   13545                 :             :               // it is a direct iface type and we can use EXPR
   13546                 :             :               // directly.  The backend representation of this
   13547                 :             :               // function is a pointer to a struct whose first field
   13548                 :             :               // is the actual function to call.
   13549                 :           2 :               Type* pvoid = Type::make_pointer_type(Type::make_void_type());
   13550                 :           2 :               Type* pfntype = Type::make_pointer_type(pvoid);
   13551                 :           2 :               Expression* ref = make_unsafe_cast(pfntype, expr, loc);
   13552                 :           2 :               return Expression::make_dereference(ref, NIL_CHECK_NOT_NEEDED,
   13553                 :           2 :                                                   loc);
   13554                 :             :             }
   13555                 :             :         }
   13556                 :             :     }
   13557                 :             : 
   13558                 :             :   return NULL;
   13559                 :      820343 : }
   13560                 :             : 
   13561                 :             : // Make implicit type conversions explicit.
   13562                 :             : 
   13563                 :             : void
   13564                 :      834516 : Call_expression::do_add_conversions()
   13565                 :             : {
   13566                 :             :   // Skip call that requires a thunk. We generate conversions inside the thunk.
   13567                 :      834516 :   if (this->is_concurrent_ || this->is_deferred_)
   13568                 :      304606 :     return;
   13569                 :             : 
   13570                 :      820773 :   if (this->args_ == NULL || this->args_->empty())
   13571                 :             :     return;
   13572                 :             : 
   13573                 :      760824 :   Function_type* fntype = this->get_function_type();
   13574                 :      760824 :   if (fntype == NULL)
   13575                 :             :     {
   13576                 :           0 :       go_assert(saw_errors());
   13577                 :             :       return;
   13578                 :             :     }
   13579                 :      760824 :   if (fntype->parameters() == NULL || fntype->parameters()->empty())
   13580                 :             :     return;
   13581                 :             : 
   13582                 :      529910 :   Location loc = this->location();
   13583                 :      529910 :   Expression_list::iterator pa = this->args_->begin();
   13584                 :      529910 :   Typed_identifier_list::const_iterator pp = fntype->parameters()->begin();
   13585                 :      529910 :   bool is_interface_method =
   13586                 :      529910 :     this->fn_->interface_field_reference_expression() != NULL;
   13587                 :      529910 :   size_t argcount = this->args_->size();
   13588                 :      529910 :   if (!is_interface_method && fntype->is_method())
   13589                 :             :     {
   13590                 :             :       // Skip the receiver argument, which cannot be interface.
   13591                 :      155664 :       pa++;
   13592                 :      155664 :       argcount--;
   13593                 :             :     }
   13594                 :      529910 :   if (argcount != fntype->parameters()->size())
   13595                 :             :     {
   13596                 :           0 :       go_assert(saw_errors());
   13597                 :             :       return;
   13598                 :             :     }
   13599                 :     1522988 :   for (; pa != this->args_->end(); ++pa, ++pp)
   13600                 :             :     {
   13601                 :      993078 :       Type* pt = pp->type();
   13602                 :      993078 :       if (!Type::are_identical(pt, (*pa)->type(), 0, NULL)
   13603                 :     1068797 :           && pt->interface_type() != NULL)
   13604                 :       59595 :         *pa = Expression::make_cast(pt, *pa, loc);
   13605                 :             :     }
   13606                 :             : }
   13607                 :             : 
   13608                 :             : // Get the function type.  This can return NULL in error cases.
   13609                 :             : 
   13610                 :             : Function_type*
   13611                 :    24771617 : Call_expression::get_function_type() const
   13612                 :             : {
   13613                 :    24771617 :   return this->fn_->type()->function_type();
   13614                 :             : }
   13615                 :             : 
   13616                 :             : // Return the number of values which this call will return.
   13617                 :             : 
   13618                 :             : size_t
   13619                 :     2919333 : Call_expression::result_count() const
   13620                 :             : {
   13621                 :     2919333 :   const Function_type* fntype = this->get_function_type();
   13622                 :     2919333 :   if (fntype == NULL)
   13623                 :             :     return 0;
   13624                 :     2907269 :   if (fntype->results() == NULL)
   13625                 :             :     return 0;
   13626                 :     2330859 :   return fntype->results()->size();
   13627                 :             : }
   13628                 :             : 
   13629                 :             : // Return the temporary that holds the result for a call with multiple
   13630                 :             : // results.
   13631                 :             : 
   13632                 :             : Temporary_statement*
   13633                 :      167662 : Call_expression::results() const
   13634                 :             : {
   13635                 :      167662 :   if (this->call_temp_ == NULL)
   13636                 :             :     {
   13637                 :           0 :       go_assert(saw_errors());
   13638                 :             :       return NULL;
   13639                 :             :     }
   13640                 :             :   return this->call_temp_;
   13641                 :             : }
   13642                 :             : 
   13643                 :             : // Set the number of results expected from a call expression.
   13644                 :             : 
   13645                 :             : void
   13646                 :       64962 : Call_expression::set_expected_result_count(size_t count)
   13647                 :             : {
   13648                 :       64962 :   go_assert(this->expected_result_count_ == 0);
   13649                 :       64962 :   go_assert(!this->types_are_determined_);
   13650                 :       64962 :   this->expected_result_count_ = count;
   13651                 :       64962 : }
   13652                 :             : 
   13653                 :             : // Return whether this is a call to the predeclared function recover.
   13654                 :             : 
   13655                 :             : bool
   13656                 :       16490 : Call_expression::is_recover_call() const
   13657                 :             : {
   13658                 :       16490 :   return this->do_is_recover_call();
   13659                 :             : }
   13660                 :             : 
   13661                 :             : // Set the argument to the recover function.
   13662                 :             : 
   13663                 :             : void
   13664                 :         857 : Call_expression::set_recover_arg(Expression* arg)
   13665                 :             : {
   13666                 :         857 :   this->do_set_recover_arg(arg);
   13667                 :         857 : }
   13668                 :             : 
   13669                 :             : // Virtual functions also implemented by Builtin_call_expression.
   13670                 :             : 
   13671                 :             : bool
   13672                 :       14910 : Call_expression::do_is_recover_call() const
   13673                 :             : {
   13674                 :       14910 :   return false;
   13675                 :             : }
   13676                 :             : 
   13677                 :             : void
   13678                 :           0 : Call_expression::do_set_recover_arg(Expression*)
   13679                 :             : {
   13680                 :           0 :   go_unreachable();
   13681                 :             : }
   13682                 :             : 
   13683                 :             : // We have found an error with this call expression; return true if
   13684                 :             : // we should report it.
   13685                 :             : 
   13686                 :             : bool
   13687                 :           9 : Call_expression::issue_error()
   13688                 :             : {
   13689                 :           9 :   if (this->issued_error_)
   13690                 :             :     return false;
   13691                 :             :   else
   13692                 :             :     {
   13693                 :           5 :       this->issued_error_ = true;
   13694                 :           5 :       return true;
   13695                 :             :     }
   13696                 :             : }
   13697                 :             : 
   13698                 :             : // Whether or not this call contains errors, either in the call or the
   13699                 :             : // arguments to the call.
   13700                 :             : 
   13701                 :             : bool
   13702                 :     1203734 : Call_expression::is_erroneous_call()
   13703                 :             : {
   13704                 :     1203734 :   if (this->is_error_expression() || this->fn()->is_error_expression())
   13705                 :             :     return true;
   13706                 :             : 
   13707                 :     1203733 :   if (this->args() == NULL)
   13708                 :             :     return false;
   13709                 :     3456389 :   for (Expression_list::iterator pa = this->args()->begin();
   13710                 :     3456389 :        pa != this->args()->end();
   13711                 :     2314738 :        ++pa)
   13712                 :             :     {
   13713                 :     2317975 :       if ((*pa)->type()->is_error_type() || (*pa)->is_error_expression())
   13714                 :        3238 :         return true;
   13715                 :             :     }
   13716                 :             :   return false;
   13717                 :             : }
   13718                 :             : 
   13719                 :             : // Get the type.
   13720                 :             : 
   13721                 :             : Type*
   13722                 :     6210194 : Call_expression::do_type()
   13723                 :             : {
   13724                 :     6210194 :   if (this->is_error_expression())
   13725                 :          36 :     return Type::make_error_type();
   13726                 :     6210158 :   if (this->lowered_ != NULL)
   13727                 :      140189 :     return this->lowered_->type();
   13728                 :     6069969 :   go_assert(this->type_ != NULL);
   13729                 :             :   return this->type_;
   13730                 :             : }
   13731                 :             : 
   13732                 :             : // Determine types for a call expression.  We can use the function
   13733                 :             : // parameter types to set the types of the arguments.  We simplify
   13734                 :             : // some of the call cases here, storing the result in the lowered_
   13735                 :             : // field.
   13736                 :             : 
   13737                 :             : void
   13738                 :     2243447 : Call_expression::do_determine_type(Gogo* gogo, const Type_context* context)
   13739                 :             : {
   13740                 :     2243447 :   if (!this->determining_types())
   13741                 :     2243447 :     return;
   13742                 :             : 
   13743                 :     1729815 :   if (this->lowered_== NULL)
   13744                 :             :     {
   13745                 :     1729815 :       Expression* builtin = this->lower_builtin(gogo);
   13746                 :     1729815 :       if (builtin != this)
   13747                 :       75179 :         this->lowered_ = builtin;
   13748                 :             :     }
   13749                 :             : 
   13750                 :     1729815 :   if (this->lowered_ != NULL)
   13751                 :             :     {
   13752                 :       75179 :       this->lowered_->determine_type(gogo, context);
   13753                 :       75179 :       return;
   13754                 :             :     }
   13755                 :             : 
   13756                 :     1654636 :   this->fn_->determine_type_no_context(gogo);
   13757                 :             : 
   13758                 :             :   // Simplify a type conversion.
   13759                 :             : 
   13760                 :     1654636 :   if (this->fn_->is_type_expression()
   13761                 :       89712 :       && this->args_ != NULL
   13762                 :       89712 :       && this->args_->size() == 1
   13763                 :     1744348 :       && (this->expected_result_count_ == 0
   13764                 :             :           || this->expected_result_count_ == 1))
   13765                 :             :     {
   13766                 :       89712 :       this->lowered_ = Expression::make_cast(this->fn_->type(),
   13767                 :       89712 :                                              this->args_->front(),
   13768                 :             :                                              this->location());
   13769                 :       89712 :       this->lowered_->determine_type(gogo, context);
   13770                 :       89712 :       return;
   13771                 :             :     }
   13772                 :             : 
   13773                 :             :   // Get the type of the function we are calling.
   13774                 :             : 
   13775                 :     1564924 :   Function_type* fntype = this->get_function_type();
   13776                 :     1564924 :   if (fntype == NULL)
   13777                 :             :     {
   13778                 :             :       // We report the error here so that we can reasonably return an
   13779                 :             :       // error type in do_type.
   13780                 :          56 :       if (!this->fn_->type()->is_error())
   13781                 :           5 :         this->report_error(_("expected function"));
   13782                 :             :       else
   13783                 :          51 :         this->set_is_error();
   13784                 :          56 :       if (this->args_ != NULL)
   13785                 :             :         {
   13786                 :          34 :           for (Expression_list::iterator p = this->args_->begin();
   13787                 :          34 :                p != this->args_->end();
   13788                 :          18 :                ++p)
   13789                 :          18 :             (*p)->determine_type_no_context(gogo);
   13790                 :             :         }
   13791                 :          56 :       return;
   13792                 :             :     }
   13793                 :             : 
   13794                 :             :   // Simplify f(g()) where g() returns multiple results.
   13795                 :             : 
   13796                 :     1564868 :   this->simplify_multiple_results(gogo);
   13797                 :             : 
   13798                 :             :   // Set the type of this expression.
   13799                 :             : 
   13800                 :     1564868 :   go_assert(this->type_ == NULL);
   13801                 :     1564868 :   const Typed_identifier_list* results = fntype->results();
   13802                 :     1564868 :   if (results == NULL || results->empty())
   13803                 :      889667 :     this->type_ = Type::make_void_type();
   13804                 :      675201 :   else if (results->size() == 1)
   13805                 :             :     {
   13806                 :             :       // If this is a call to a generated equality function, we
   13807                 :             :       // determine the type based on the context.  See the comment in
   13808                 :             :       // Binary_expression::lower_array_comparison.
   13809                 :      581807 :       if (this->is_equal_function_
   13810                 :         961 :           && !context->may_be_abstract
   13811                 :         961 :           && context->type != NULL
   13812                 :      581807 :           && context->type->is_boolean_type())
   13813                 :           0 :         this->type_ = context->type;
   13814                 :             :       else
   13815                 :      581807 :         this->type_ = results->begin()->type();
   13816                 :             :     }
   13817                 :             :   else
   13818                 :       93394 :     this->type_ = Type::make_call_multiple_result_type();
   13819                 :             : 
   13820                 :             :   // Determine the types of the arguments.
   13821                 :             : 
   13822                 :     1564868 :   if (this->args_ == NULL)
   13823                 :             :     {
   13824                 :      165644 :       if (fntype->is_varargs())
   13825                 :             :         {
   13826                 :         193 :           if (!this->rewrite_varargs())
   13827                 :           0 :             this->set_is_error();
   13828                 :             :         }
   13829                 :      165644 :       return;
   13830                 :             :     }
   13831                 :             : 
   13832                 :     1399224 :   const Typed_identifier_list* parameters = fntype->parameters();
   13833                 :     1399224 :   Typed_identifier_list::const_iterator pt;
   13834                 :     1399224 :   if (parameters != NULL)
   13835                 :     1236386 :     pt = parameters->begin();
   13836                 :     1399224 :   bool first = true;
   13837                 :     4173687 :   for (Expression_list::const_iterator pa = this->args_->begin();
   13838                 :     4173687 :        pa != this->args_->end();
   13839                 :     2774463 :        ++pa)
   13840                 :             :     {
   13841                 :     2774463 :       if (first)
   13842                 :             :         {
   13843                 :     1236386 :           first = false;
   13844                 :             :           // If this is a method, the first argument is the
   13845                 :             :           // receiver.
   13846                 :     1236386 :           if (fntype != NULL && fntype->is_method())
   13847                 :             :             {
   13848                 :           0 :               Type* rtype = fntype->receiver()->type();
   13849                 :             :               // The receiver is always passed as a pointer.
   13850                 :           0 :               if (rtype->points_to() == NULL)
   13851                 :           0 :                 rtype = Type::make_pointer_type(rtype);
   13852                 :           0 :               Type_context subcontext(rtype, false);
   13853                 :           0 :               (*pa)->determine_type(gogo, &subcontext);
   13854                 :           0 :               continue;
   13855                 :           0 :             }
   13856                 :             :         }
   13857                 :             : 
   13858                 :     2769848 :       if ((this->is_varargs_ || this->varargs_are_lowered_)
   13859                 :       27434 :           && fntype->is_varargs()
   13860                 :        4632 :           && pa + 1 == this->args_->end()
   13861                 :        2308 :           && parameters != NULL
   13862                 :     2776771 :           && pt + 1 == parameters->end())
   13863                 :             :         {
   13864                 :        2308 :           Type_context subcontext(pt->type(), false);
   13865                 :        2308 :           (*pa)->determine_type(gogo, &subcontext);
   13866                 :        2308 :           continue;
   13867                 :        2308 :         }
   13868                 :             : 
   13869                 :     2772155 :       if (!this->is_varargs_
   13870                 :     2769832 :           && fntype->is_varargs()
   13871                 :      177215 :           && parameters != NULL
   13872                 :     2949370 :           && pt + 1 == parameters->end())
   13873                 :             :         {
   13874                 :      114906 :           go_assert(pt->type()->is_slice_type());
   13875                 :      114906 :           Type_context subcontext(pt->type()->array_type()->element_type(),
   13876                 :      229812 :                                   false);
   13877                 :      114906 :           (*pa)->determine_type(gogo, &subcontext);
   13878                 :      114906 :           continue;
   13879                 :      114906 :         }
   13880                 :             : 
   13881                 :     2657249 :       if (parameters != NULL && pt != parameters->end())
   13882                 :             :         {
   13883                 :     2657247 :           Type_context subcontext(pt->type(), false);
   13884                 :     2657247 :           (*pa)->determine_type(gogo, &subcontext);
   13885                 :     2657247 :           if (!fntype->is_varargs() || pt + 1 != parameters->end())
   13886                 :     2657244 :             ++pt;
   13887                 :             :         }
   13888                 :             :       else
   13889                 :           2 :         (*pa)->determine_type_no_context(gogo);
   13890                 :             :     }
   13891                 :             : 
   13892                 :     1399224 :   if (fntype->is_varargs())
   13893                 :             :     {
   13894                 :       72617 :       if (!this->rewrite_varargs())
   13895                 :           6 :         this->set_is_error();
   13896                 :             :     }
   13897                 :             : }
   13898                 :             : 
   13899                 :             : // Called when determining types for a Call_expression.  Return true
   13900                 :             : // if we should go ahead, false if they have already been determined.
   13901                 :             : 
   13902                 :             : bool
   13903                 :     2538959 : Call_expression::determining_types()
   13904                 :             : {
   13905                 :     2538959 :   if (this->types_are_determined_)
   13906                 :             :     return false;
   13907                 :             :   else
   13908                 :             :     {
   13909                 :     1956462 :       this->types_are_determined_ = true;
   13910                 :     1956462 :       return true;
   13911                 :             :     }
   13912                 :             : }
   13913                 :             : 
   13914                 :             : // Simplify f(g()) where g() returns multiple results.  Called by
   13915                 :             : // do_determine_types of both Call_expression and
   13916                 :             : // Builtin_call_expression.
   13917                 :             : 
   13918                 :             : void
   13919                 :     1791515 : Call_expression::simplify_multiple_results(Gogo* gogo)
   13920                 :             : {
   13921                 :     1791515 :   if (this->args_ == NULL || this->args_->size() != 1)
   13922                 :             :     return;
   13923                 :             : 
   13924                 :      489700 :   Call_expression* call = this->args_->front()->call_expression();
   13925                 :       23895 :   if (call == NULL || call->is_builtin())
   13926                 :      467165 :     return;
   13927                 :             : 
   13928                 :       22535 :   call->determine_type_no_context(gogo);
   13929                 :       22535 :   size_t rc = call->result_count();
   13930                 :       22535 :   Function_type* fntype = this->get_function_type();
   13931                 :       22535 :   if (rc > 1
   13932                 :       22535 :       && ((fntype->parameters() != NULL
   13933                 :         288 :            && (fntype->parameters()->size() == rc
   13934                 :          16 :                || (fntype->is_varargs()
   13935                 :          16 :                    && fntype->parameters()->size() - 1 <= rc)))
   13936                 :          12 :           || fntype->is_builtin()))
   13937                 :             :     {
   13938                 :         300 :       if (this->is_varargs_)
   13939                 :             :         {
   13940                 :           2 :           go_error_at(call->location(),
   13941                 :             :                       "multiple-value argument in single-value context");
   13942                 :           2 :           this->set_is_error();
   13943                 :             :         }
   13944                 :             : 
   13945                 :         300 :       call->set_is_multi_value_arg();
   13946                 :         300 :       Expression_list* args = new Expression_list;
   13947                 :         987 :       for (size_t i = 0; i < rc; ++i)
   13948                 :         687 :         args->push_back(Expression::make_call_result(call, i));
   13949                 :             :       // We can't create a new Call_expression here because this
   13950                 :             :       // one may be referred to by Call_result expressions.
   13951                 :         300 :       this->args_ = args;
   13952                 :             :     }
   13953                 :             : }
   13954                 :             : 
   13955                 :             : // Lower a call to a varargs function by rewriting the value(s) passed
   13956                 :             : // to the varargs argument into a slice.  Called during the
   13957                 :             : // determine_types pass.
   13958                 :             : 
   13959                 :             : bool
   13960                 :       72810 : Call_expression::rewrite_varargs()
   13961                 :             : {
   13962                 :       72810 :   if (this->varargs_are_lowered_)
   13963                 :             :     return true;
   13964                 :       72491 :   this->varargs_are_lowered_ = true;
   13965                 :             : 
   13966                 :       72491 :   Function_type* fntype = this->get_function_type();
   13967                 :             : 
   13968                 :       72491 :   const Typed_identifier_list* parameters = fntype->parameters();
   13969                 :       72491 :   go_assert(parameters != NULL && !parameters->empty());
   13970                 :       72491 :   size_t param_count = parameters->size();
   13971                 :             : 
   13972                 :       72491 :   Type* varargs_type = parameters->back().type();
   13973                 :       72491 :   go_assert(varargs_type->is_slice_type());
   13974                 :             : 
   13975                 :       72491 :   size_t arg_count = this->args_ == NULL ? 0 : this->args_->size();
   13976                 :       72491 :   if (arg_count < param_count - 1)
   13977                 :             :     {
   13978                 :           0 :       if (!this->is_error_expression())
   13979                 :           0 :         this->report_error(_("not enough arguments"));
   13980                 :           0 :       return false;
   13981                 :             :     }
   13982                 :             : 
   13983                 :       72491 :   bool ret = true;
   13984                 :       72491 :   Expression_list* old_args = this->args_;
   13985                 :       72491 :   Expression_list* new_args = new Expression_list();
   13986                 :       72491 :   bool push_empty_arg = false;
   13987                 :       72491 :   if (old_args == NULL || old_args->empty())
   13988                 :             :     {
   13989                 :         193 :       go_assert(param_count == 1);
   13990                 :             :       push_empty_arg = true;
   13991                 :             :     }
   13992                 :             :   else
   13993                 :             :     {
   13994                 :             :       Expression_list::const_iterator pa;
   13995                 :             :       size_t i = 1;
   13996                 :      136625 :       for (pa = old_args->begin(); pa != old_args->end(); ++pa, ++i)
   13997                 :             :         {
   13998                 :      130282 :           if (i == param_count)
   13999                 :             :             break;
   14000                 :       64327 :           new_args->push_back(*pa);
   14001                 :             :         }
   14002                 :             : 
   14003                 :             :       // We have reached the varargs parameter.
   14004                 :             : 
   14005                 :       72298 :       if (pa == old_args->end())
   14006                 :             :         push_empty_arg = true;
   14007                 :       65955 :       else if (pa + 1 == old_args->end() && this->is_varargs_)
   14008                 :        1987 :         new_args->push_back(*pa);
   14009                 :       63968 :       else if (this->is_varargs_)
   14010                 :             :         {
   14011                 :           2 :           if (!this->is_error_expression())
   14012                 :             :             {
   14013                 :           1 :               if ((*pa)->type()->is_slice_type())
   14014                 :           1 :                 this->report_error(_("too many arguments"));
   14015                 :             :               else
   14016                 :             :                 {
   14017                 :           0 :                   go_error_at(this->location(),
   14018                 :             :                               "invalid use of %<...%> with non-slice");
   14019                 :           0 :                   this->set_is_error();
   14020                 :             :                 }
   14021                 :             :             }
   14022                 :           2 :           return false;
   14023                 :             :         }
   14024                 :             :       else
   14025                 :             :         {
   14026                 :      127932 :           Type* element_type = varargs_type->array_type()->element_type();
   14027                 :       63966 :           Expression_list* vals = new Expression_list;
   14028                 :      178872 :           for (; pa != old_args->end(); ++pa, ++i)
   14029                 :             :             {
   14030                 :      114906 :               Type* patype = (*pa)->type();
   14031                 :      114906 :               Location paloc = (*pa)->location();
   14032                 :      114906 :               if (!this->check_argument_type(i, element_type, patype, paloc))
   14033                 :             :                 {
   14034                 :           4 :                   ret = false;
   14035                 :           4 :                   continue;
   14036                 :             :                 }
   14037                 :      114902 :               vals->push_back(*pa);
   14038                 :             :             }
   14039                 :       63966 :           Expression* val =
   14040                 :       63966 :             Expression::make_slice_composite_literal(varargs_type, vals,
   14041                 :             :                                                      this->location());
   14042                 :       63966 :           new_args->push_back(val);
   14043                 :             :         }
   14044                 :             :     }
   14045                 :             : 
   14046                 :       65953 :   if (push_empty_arg)
   14047                 :        6536 :     new_args->push_back(Expression::make_nil(this->location()));
   14048                 :             : 
   14049                 :             :   // We can't create a new Call_expression here because this
   14050                 :             :   // one may be referred to by Call_result expressions.
   14051                 :       72489 :   this->args_ = new_args;
   14052                 :             : 
   14053                 :       72489 :   return ret;
   14054                 :             : }
   14055                 :             : 
   14056                 :             : // Check types for parameter I.
   14057                 :             : 
   14058                 :             : bool
   14059                 :      975032 : Call_expression::check_argument_type(int i, const Type* parameter_type,
   14060                 :             :                                      const Type* argument_type,
   14061                 :             :                                      Location argument_location)
   14062                 :             : {
   14063                 :      975032 :   std::string reason;
   14064                 :      975032 :   if (!Type::are_assignable(parameter_type, argument_type, &reason))
   14065                 :             :     {
   14066                 :          27 :       if (reason.empty())
   14067                 :           0 :         go_error_at(argument_location, "argument %d has incompatible type", i);
   14068                 :             :       else
   14069                 :          27 :         go_error_at(argument_location,
   14070                 :             :                     "argument %d has incompatible type (%s)",
   14071                 :             :                     i, reason.c_str());
   14072                 :          27 :       this->set_is_error();
   14073                 :          27 :       return false;
   14074                 :             :     }
   14075                 :             :   return true;
   14076                 :      975032 : }
   14077                 :             : 
   14078                 :             : // Check types.
   14079                 :             : 
   14080                 :             : void
   14081                 :      723626 : Call_expression::do_check_types(Gogo*)
   14082                 :             : {
   14083                 :      723626 :   if (this->is_error_expression()
   14084                 :      723561 :       || this->fn_->is_error_expression()
   14085                 :     1447187 :       || this->fn_->type()->is_error())
   14086                 :          66 :     return;
   14087                 :      723560 :   if (this->lowered_ != NULL)
   14088                 :             :     return;
   14089                 :             : 
   14090                 :      633878 :   Function_type* fntype = this->get_function_type();
   14091                 :      633878 :   go_assert(fntype != NULL);
   14092                 :             : 
   14093                 :      633878 :   if (this->expected_result_count_ != 0
   14094                 :      633878 :       && this->expected_result_count_ != this->result_count())
   14095                 :             :     {
   14096                 :           5 :       if (this->issue_error())
   14097                 :           3 :         this->report_error(_("function result count mismatch"));
   14098                 :           5 :       this->set_is_error();
   14099                 :           5 :       return;
   14100                 :             :     }
   14101                 :             : 
   14102                 :      633873 :   if (this->is_varargs_ && !fntype->is_varargs())
   14103                 :             :     {
   14104                 :           1 :       go_error_at(this->location(),
   14105                 :             :                   "invalid use of %<...%> calling non-variadic function");
   14106                 :           1 :       this->set_is_error();
   14107                 :           1 :       return;
   14108                 :             :     }
   14109                 :             : 
   14110                 :      633872 :   bool is_method = fntype->is_method();
   14111                 :      633872 :   if (is_method)
   14112                 :             :     {
   14113                 :        1987 :       go_assert(this->args_ != NULL && !this->args_->empty());
   14114                 :        1987 :       Type* rtype = fntype->receiver()->type();
   14115                 :        1987 :       Expression* first_arg = this->args_->front();
   14116                 :             :       // We dereference the values since receivers are always passed
   14117                 :             :       // as pointers.
   14118                 :        1987 :       std::string reason;
   14119                 :        5961 :       if (!Type::are_assignable(rtype->deref(), first_arg->type()->deref(),
   14120                 :             :                                 &reason))
   14121                 :             :         {
   14122                 :           0 :           if (reason.empty())
   14123                 :           0 :             this->report_error(_("incompatible type for receiver"));
   14124                 :             :           else
   14125                 :             :             {
   14126                 :           0 :               go_error_at(this->location(),
   14127                 :             :                           "incompatible type for receiver (%s)",
   14128                 :             :                           reason.c_str());
   14129                 :           0 :               this->set_is_error();
   14130                 :             :             }
   14131                 :             :         }
   14132                 :        1987 :     }
   14133                 :             : 
   14134                 :      633872 :   const Typed_identifier_list* parameters = fntype->parameters();
   14135                 :      633872 :   if (this->args_ == NULL || this->args_->empty())
   14136                 :             :     {
   14137                 :      158037 :       if (parameters != NULL && !parameters->empty())
   14138                 :           9 :         this->report_error(_("not enough arguments"));
   14139                 :             :     }
   14140                 :      475835 :   else if (parameters == NULL)
   14141                 :             :     {
   14142                 :        1170 :       if (!is_method || this->args_->size() > 1)
   14143                 :           0 :         this->report_error(_("too many arguments"));
   14144                 :             :     }
   14145                 :      474665 :   else if (this->args_->size() == 1
   14146                 :      243596 :            && this->args_->front()->call_expression() != NULL
   14147                 :      494586 :            && this->args_->front()->call_expression()->result_count() > 1)
   14148                 :             :     {
   14149                 :             :       // This is F(G()) when G returns more than one result.  If the
   14150                 :             :       // results can be matched to parameters, it would have been
   14151                 :             :       // rewritten in determine_types.  If we get here we know there
   14152                 :             :       // is a mismatch.
   14153                 :           0 :       if (this->args_->front()->call_expression()->result_count()
   14154                 :           0 :           < parameters->size())
   14155                 :           0 :         this->report_error(_("not enough arguments"));
   14156                 :             :       else
   14157                 :           0 :         this->report_error(_("too many arguments"));
   14158                 :             :     }
   14159                 :             :   else
   14160                 :             :     {
   14161                 :      474665 :       int i = 0;
   14162                 :      474665 :       Expression_list::const_iterator pa = this->args_->begin();
   14163                 :      474665 :       if (is_method)
   14164                 :         817 :         ++pa;
   14165                 :      474665 :       for (Typed_identifier_list::const_iterator pt = parameters->begin();
   14166                 :     1334791 :            pt != parameters->end();
   14167                 :      860126 :            ++pt, ++pa, ++i)
   14168                 :             :         {
   14169                 :      860127 :           if (pa == this->args_->end())
   14170                 :             :             {
   14171                 :           1 :               this->report_error(_("not enough arguments"));
   14172                 :           1 :               return;
   14173                 :             :             }
   14174                 :      860126 :           this->check_argument_type(i + 1, pt->type(), (*pa)->type(),
   14175                 :      860126 :                                     (*pa)->location());
   14176                 :             :         }
   14177                 :      474664 :       if (pa != this->args_->end())
   14178                 :           2 :         this->report_error(_("too many arguments"));
   14179                 :             :     }
   14180                 :             : }
   14181                 :             : 
   14182                 :             : Expression*
   14183                 :          11 : Call_expression::do_copy()
   14184                 :             : {
   14185                 :          11 :   Call_expression* call =
   14186                 :          22 :     Expression::make_call(this->fn_->copy(),
   14187                 :          11 :                           (this->args_ == NULL
   14188                 :             :                            ? NULL
   14189                 :          11 :                            : this->args_->copy()),
   14190                 :          11 :                           this->is_varargs_, this->location());
   14191                 :             : 
   14192                 :          11 :   if (this->varargs_are_lowered_)
   14193                 :           0 :     call->set_varargs_are_lowered();
   14194                 :          11 :   if (this->is_deferred_)
   14195                 :           0 :     call->set_is_deferred();
   14196                 :          11 :   if (this->is_concurrent_)
   14197                 :           0 :     call->set_is_concurrent();
   14198                 :          11 :   return call;
   14199                 :             : }
   14200                 :             : 
   14201                 :             : // Return whether we have to use a temporary variable to ensure that
   14202                 :             : // we evaluate this call expression in order.  If the call returns no
   14203                 :             : // results then it will inevitably be executed last.
   14204                 :             : 
   14205                 :             : bool
   14206                 :      973838 : Call_expression::do_must_eval_in_order() const
   14207                 :             : {
   14208                 :      973838 :   return this->result_count() > 0;
   14209                 :             : }
   14210                 :             : 
   14211                 :             : // Get the function and the first argument to use when calling an
   14212                 :             : // interface method.
   14213                 :             : 
   14214                 :             : Expression*
   14215                 :       31774 : Call_expression::interface_method_function(
   14216                 :             :     Interface_field_reference_expression* interface_method,
   14217                 :             :     Expression** first_arg_ptr,
   14218                 :             :     Location location)
   14219                 :             : {
   14220                 :       31774 :   Expression* object = interface_method->get_underlying_object();
   14221                 :       31774 :   Type* unsafe_ptr_type = Type::make_pointer_type(Type::make_void_type());
   14222                 :       63548 :   *first_arg_ptr =
   14223                 :       31774 :       Expression::make_unsafe_cast(unsafe_ptr_type, object, location);
   14224                 :       31774 :   return interface_method->get_function();
   14225                 :             : }
   14226                 :             : 
   14227                 :             : // Build the call expression.
   14228                 :             : 
   14229                 :             : Bexpression*
   14230                 :     1754314 : Call_expression::do_get_backend(Translate_context* context)
   14231                 :             : {
   14232                 :     1754314 :   Location location = this->location();
   14233                 :             : 
   14234                 :     1754314 :   if (this->call_ != NULL)
   14235                 :             :     {
   14236                 :             :       // If the call returns multiple results, make a new reference to
   14237                 :             :       // the temporary.
   14238                 :          95 :       if (this->call_temp_ != NULL)
   14239                 :             :         {
   14240                 :          95 :           Expression* ref =
   14241                 :          95 :             Expression::make_temporary_reference(this->call_temp_, location);
   14242                 :          95 :           return ref->get_backend(context);
   14243                 :             :         }
   14244                 :             : 
   14245                 :             :       return this->call_;
   14246                 :             :     }
   14247                 :             : 
   14248                 :     1754219 :   Function_type* fntype = this->get_function_type();
   14249                 :     1754219 :   if (fntype == NULL)
   14250                 :           0 :     return context->backend()->error_expression();
   14251                 :             : 
   14252                 :     1754219 :   if (this->fn_->is_error_expression())
   14253                 :           0 :     return context->backend()->error_expression();
   14254                 :             : 
   14255                 :     1754219 :   Gogo* gogo = context->gogo();
   14256                 :             : 
   14257                 :     1754219 :   Func_expression* func = this->fn_->func_expression();
   14258                 :     1754219 :   Interface_field_reference_expression* interface_method =
   14259                 :     1754219 :     this->fn_->interface_field_reference_expression();
   14260                 :     1754219 :   const bool has_closure = func != NULL && func->closure() != NULL;
   14261                 :     3507745 :   const bool is_interface_method = interface_method != NULL;
   14262                 :             : 
   14263                 :     1753526 :   bool has_closure_arg;
   14264                 :     1753526 :   if (has_closure)
   14265                 :             :     has_closure_arg = true;
   14266                 :     1753526 :   else if (func != NULL)
   14267                 :             :     has_closure_arg = false;
   14268                 :       54255 :   else if (is_interface_method)
   14269                 :             :     has_closure_arg = false;
   14270                 :             :   else
   14271                 :     1754219 :     has_closure_arg = true;
   14272                 :             : 
   14273                 :     1754219 :   Expression* first_arg = NULL;
   14274                 :     1754219 :   if (!is_interface_method && fntype->is_method())
   14275                 :             :     {
   14276                 :      256648 :       first_arg = this->args_->front();
   14277                 :      256648 :       if (first_arg->type()->points_to() == NULL
   14278                 :      261191 :           && first_arg->type()->is_direct_iface_type())
   14279                 :        4543 :         first_arg = Expression::unpack_direct_iface(first_arg,
   14280                 :             :                                                     first_arg->location());
   14281                 :             :     }
   14282                 :             : 
   14283                 :     1754219 :   int nargs;
   14284                 :     1754219 :   std::vector<Bexpression*> fn_args;
   14285                 :     1754219 :   if (this->args_ == NULL || this->args_->empty())
   14286                 :             :     {
   14287                 :      215239 :       nargs = is_interface_method ? 1 : 0;
   14288                 :       19932 :       if (nargs > 0)
   14289                 :       19932 :         fn_args.resize(1);
   14290                 :             :     }
   14291                 :     1538980 :   else if (fntype->parameters() == NULL || fntype->parameters()->empty())
   14292                 :             :     {
   14293                 :             :       // Passing a receiver parameter.
   14294                 :      101193 :       go_assert(!is_interface_method
   14295                 :             :                 && fntype->is_method()
   14296                 :             :                 && this->args_->size() == 1);
   14297                 :      101193 :       nargs = 1;
   14298                 :      101193 :       fn_args.resize(1);
   14299                 :      101193 :       fn_args[0] = first_arg->get_backend(context);
   14300                 :             :     }
   14301                 :             :   else
   14302                 :             :     {
   14303                 :     1437787 :       const Typed_identifier_list* params = fntype->parameters();
   14304                 :             : 
   14305                 :     1437787 :       nargs = this->args_->size();
   14306                 :     1437787 :       int i = is_interface_method ? 1 : 0;
   14307                 :     1437787 :       nargs += i;
   14308                 :     1437787 :       fn_args.resize(nargs);
   14309                 :             : 
   14310                 :     1437787 :       Typed_identifier_list::const_iterator pp = params->begin();
   14311                 :     1437787 :       Expression_list::const_iterator pe = this->args_->begin();
   14312                 :     1437787 :       if (!is_interface_method && fntype->is_method())
   14313                 :             :         {
   14314                 :      155455 :           fn_args[i] = first_arg->get_backend(context);
   14315                 :      155455 :           ++pe;
   14316                 :      155455 :           ++i;
   14317                 :             :         }
   14318                 :     4359602 :       for (; pe != this->args_->end(); ++pe, ++pp, ++i)
   14319                 :             :         {
   14320                 :     2921815 :           go_assert(pp != params->end());
   14321                 :     2921815 :           Expression* arg =
   14322                 :     2921815 :               Expression::convert_for_assignment(gogo, pp->type(), *pe,
   14323                 :             :                                                  location);
   14324                 :     2921815 :           fn_args[i] = arg->get_backend(context);
   14325                 :             :         }
   14326                 :     1437787 :       go_assert(pp == params->end());
   14327                 :     1437787 :       go_assert(i == nargs);
   14328                 :             :     }
   14329                 :             : 
   14330                 :     1754219 :   Expression* fn;
   14331                 :     1754219 :   Expression* closure = NULL;
   14332                 :     1754219 :   if (func != NULL)
   14333                 :             :     {
   14334                 :     1699964 :       Named_object* no = func->named_object();
   14335                 :     1699964 :       fn = Expression::make_func_code_reference(no, location);
   14336                 :     1699964 :       if (has_closure)
   14337                 :         693 :         closure = func->closure();
   14338                 :             :     }
   14339                 :       54255 :   else if (!is_interface_method)
   14340                 :             :     {
   14341                 :       22481 :       closure = this->fn_;
   14342                 :             : 
   14343                 :             :       // The backend representation of this function type is a pointer
   14344                 :             :       // to a struct whose first field is the actual function to call.
   14345                 :       22481 :       Type* pfntype =
   14346                 :       22481 :           Type::make_pointer_type(
   14347                 :       22481 :               Type::make_pointer_type(Type::make_void_type()));
   14348                 :       22481 :       fn = Expression::make_unsafe_cast(pfntype, this->fn_, location);
   14349                 :       22481 :       fn = Expression::make_dereference(fn, NIL_CHECK_NOT_NEEDED, location);
   14350                 :             :     }
   14351                 :             :   else
   14352                 :             :     {
   14353                 :       31774 :       Expression* arg0;
   14354                 :       31774 :       fn = this->interface_method_function(interface_method, &arg0,
   14355                 :             :                                            location);
   14356                 :       31774 :       fn_args[0] = arg0->get_backend(context);
   14357                 :             :     }
   14358                 :             : 
   14359                 :     1754219 :   Bexpression* bclosure = NULL;
   14360                 :     1754219 :   if (has_closure_arg)
   14361                 :       23174 :     bclosure = closure->get_backend(context);
   14362                 :             :   else
   14363                 :     1731045 :     go_assert(closure == NULL);
   14364                 :             : 
   14365                 :     1754219 :   Bexpression* bfn = fn->get_backend(context);
   14366                 :             : 
   14367                 :             :   // When not calling a named function directly, use a type conversion
   14368                 :             :   // in case the type of the function is a recursive type which refers
   14369                 :             :   // to itself.  We don't do this for an interface method because 1)
   14370                 :             :   // an interface method never refers to itself, so we always have a
   14371                 :             :   // function type here; 2) we pass an extra first argument to an
   14372                 :             :   // interface method, so fntype is not correct.
   14373                 :     1754219 :   if (func == NULL && !is_interface_method)
   14374                 :             :     {
   14375                 :       22481 :       Btype* bft = fntype->get_backend_fntype(gogo);
   14376                 :       22481 :       bfn = gogo->backend()->convert_expression(bft, bfn, location);
   14377                 :             :     }
   14378                 :             : 
   14379                 :     1754219 :   Bfunction* bfunction = NULL;
   14380                 :     1754219 :   if (context->function())
   14381                 :     1750220 :     bfunction = context->function()->func_value()->get_decl();
   14382                 :     1754219 :   Bexpression* call = gogo->backend()->call_expression(bfunction, bfn,
   14383                 :             :                                                        fn_args, bclosure,
   14384                 :             :                                                        location);
   14385                 :             : 
   14386                 :     1754219 :   if (this->call_temp_ != NULL)
   14387                 :             :     {
   14388                 :             :       // This case occurs when the call returns multiple results.
   14389                 :             : 
   14390                 :       92971 :       Expression* ref = Expression::make_temporary_reference(this->call_temp_,
   14391                 :             :                                                              location);
   14392                 :       92971 :       Bexpression* bref = ref->get_backend(context);
   14393                 :       92971 :       Bstatement* bassn = gogo->backend()->assignment_statement(bfunction,
   14394                 :             :                                                                 bref, call,
   14395                 :             :                                                                 location);
   14396                 :             : 
   14397                 :       92971 :       ref = Expression::make_temporary_reference(this->call_temp_, location);
   14398                 :       92971 :       this->call_ = ref->get_backend(context);
   14399                 :             : 
   14400                 :       92971 :       return gogo->backend()->compound_expression(bassn, this->call_,
   14401                 :       92971 :                                                   location);
   14402                 :             :     }
   14403                 :             : 
   14404                 :     1661248 :   this->call_ = call;
   14405                 :     1661248 :   return this->call_;
   14406                 :     1754219 : }
   14407                 :             : 
   14408                 :             : // The cost of inlining a call expression.
   14409                 :             : 
   14410                 :             : int
   14411                 :      915917 : Call_expression::do_inlining_cost() const
   14412                 :             : {
   14413                 :      915917 :   Func_expression* fn = this->fn_->func_expression();
   14414                 :             : 
   14415                 :             :   // FIXME: We don't yet support all kinds of calls.
   14416                 :      889792 :   if (fn != NULL && fn->closure() != NULL)
   14417                 :             :     return 0x100000;
   14418                 :     1495700 :   if (this->fn_->interface_field_reference_expression())
   14419                 :             :     return 0x100000;
   14420                 :      896164 :   if (this->get_function_type()->is_method())
   14421                 :             :     return 0x100000;
   14422                 :             : 
   14423                 :             :   return 5;
   14424                 :             : }
   14425                 :             : 
   14426                 :             : // Export a call expression.
   14427                 :             : 
   14428                 :             : void
   14429                 :       10850 : Call_expression::do_export(Export_function_body* efb) const
   14430                 :             : {
   14431                 :       10850 :   bool simple_call = (this->fn_->func_expression() != NULL);
   14432                 :       10850 :   if (!simple_call)
   14433                 :         194 :     efb->write_c_string("(");
   14434                 :       10850 :   this->fn_->export_expression(efb);
   14435                 :       10850 :   if (!simple_call)
   14436                 :         194 :     efb->write_c_string(")");
   14437                 :       10850 :   this->export_arguments(efb);
   14438                 :       10850 : }
   14439                 :             : 
   14440                 :             : // Export call expression arguments.
   14441                 :             : 
   14442                 :             : void
   14443                 :       15102 : Call_expression::export_arguments(Export_function_body* efb) const
   14444                 :             : {
   14445                 :       15102 :   efb->write_c_string("(");
   14446                 :       15102 :   if (this->args_ != NULL && !this->args_->empty())
   14447                 :             :     {
   14448                 :       12676 :       Expression_list::const_iterator pa = this->args_->begin();
   14449                 :       12676 :       (*pa)->export_expression(efb);
   14450                 :       22352 :       for (pa++; pa != this->args_->end(); pa++)
   14451                 :             :         {
   14452                 :        9676 :           efb->write_c_string(", ");
   14453                 :        9676 :           (*pa)->export_expression(efb);
   14454                 :             :         }
   14455                 :       12676 :       if (this->is_varargs_)
   14456                 :         199 :         efb->write_c_string("...");
   14457                 :             :     }
   14458                 :       15102 :   efb->write_c_string(")");
   14459                 :       15102 : }
   14460                 :             : 
   14461                 :             : // Dump ast representation for a call expression.
   14462                 :             : 
   14463                 :             : void
   14464                 :           0 : Call_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
   14465                 :             : {
   14466                 :           0 :   this->fn_->dump_expression(ast_dump_context);
   14467                 :           0 :   ast_dump_context->ostream() << "(";
   14468                 :           0 :   if (args_ != NULL)
   14469                 :           0 :     ast_dump_context->dump_expression_list(this->args_);
   14470                 :             : 
   14471                 :           0 :   ast_dump_context->ostream() << ") ";
   14472                 :           0 : }
   14473                 :             : 
   14474                 :             : // Make a call expression.
   14475                 :             : 
   14476                 :             : Call_expression*
   14477                 :     2128674 : Expression::make_call(Expression* fn, Expression_list* args, bool is_varargs,
   14478                 :             :                       Location location)
   14479                 :             : {
   14480                 :     2128674 :   return new Call_expression(fn, args, is_varargs, location);
   14481                 :             : }
   14482                 :             : 
   14483                 :             : // Class Call_result_expression.
   14484                 :             : 
   14485                 :             : // Traverse a call result.
   14486                 :             : 
   14487                 :             : int
   14488                 :     3067286 : Call_result_expression::do_traverse(Traverse* traverse)
   14489                 :             : {
   14490                 :     3067286 :   if (traverse->remember_expression(this->call_))
   14491                 :             :     {
   14492                 :             :       // We have already traversed the call expression.
   14493                 :             :       return TRAVERSE_CONTINUE;
   14494                 :             :     }
   14495                 :     1669260 :   return Expression::traverse(&this->call_, traverse);
   14496                 :             : }
   14497                 :             : 
   14498                 :             : // Get the type.
   14499                 :             : 
   14500                 :             : Type*
   14501                 :     1990403 : Call_result_expression::do_type()
   14502                 :             : {
   14503                 :     1990403 :   if (this->classification() == EXPRESSION_ERROR)
   14504                 :          30 :     return Type::make_error_type();
   14505                 :             : 
   14506                 :             :   // THIS->CALL_ can be replaced with a temporary reference due to
   14507                 :             :   // Call_expression::do_must_eval_in_order when there is an error.
   14508                 :     1990373 :   Call_expression* ce = this->call_->call_expression();
   14509                 :     1990373 :   if (ce == NULL)
   14510                 :             :     {
   14511                 :          12 :       this->set_is_error();
   14512                 :          12 :       return Type::make_error_type();
   14513                 :             :     }
   14514                 :     1990361 :   Function_type* fntype = ce->get_function_type();
   14515                 :     1990361 :   if (fntype == NULL)
   14516                 :             :     {
   14517                 :           0 :       if (ce->issue_error())
   14518                 :             :         {
   14519                 :           0 :           if (!ce->fn()->type()->is_error())
   14520                 :           0 :             this->report_error(_("expected function"));
   14521                 :             :         }
   14522                 :           0 :       this->set_is_error();
   14523                 :           0 :       return Type::make_error_type();
   14524                 :             :     }
   14525                 :     1990361 :   const Typed_identifier_list* results = fntype->results();
   14526                 :     1990361 :   if (results == NULL || results->size() < 2)
   14527                 :             :     {
   14528                 :           4 :       if (ce->issue_error())
   14529                 :           2 :         this->report_error(_("number of results does not match "
   14530                 :             :                              "number of values"));
   14531                 :           4 :       return Type::make_error_type();
   14532                 :             :     }
   14533                 :             :   Typed_identifier_list::const_iterator pr = results->begin();
   14534                 :     3206421 :   for (unsigned int i = 0; i < this->index_; ++i)
   14535                 :             :     {
   14536                 :     1216064 :       if (pr == results->end())
   14537                 :             :         break;
   14538                 :     1216064 :       ++pr;
   14539                 :             :     }
   14540                 :     1990357 :   if (pr == results->end())
   14541                 :             :     {
   14542                 :           0 :       if (ce->issue_error())
   14543                 :           0 :         this->report_error(_("number of results does not match "
   14544                 :             :                              "number of values"));
   14545                 :           0 :       return Type::make_error_type();
   14546                 :             :     }
   14547                 :     1990357 :   return pr->type();
   14548                 :             : }
   14549                 :             : 
   14550                 :             : // Check the type.  Just make sure that we trigger the warning in
   14551                 :             : // do_type.
   14552                 :             : 
   14553                 :             : void
   14554                 :      146270 : Call_result_expression::do_check_types(Gogo*)
   14555                 :             : {
   14556                 :      146270 :   this->type();
   14557                 :      146270 : }
   14558                 :             : 
   14559                 :             : // Determine the type.  We have nothing to do here, but the 0 result
   14560                 :             : // needs to pass down to the caller.
   14561                 :             : 
   14562                 :             : void
   14563                 :      193148 : Call_result_expression::do_determine_type(Gogo* gogo, const Type_context*)
   14564                 :             : {
   14565                 :      193148 :   this->call_->determine_type_no_context(gogo);
   14566                 :      193148 : }
   14567                 :             : 
   14568                 :             : // Return the backend representation.  We just refer to the temporary set by the
   14569                 :             : // call expression.  We don't do this at lowering time because it makes it
   14570                 :             : // hard to evaluate the call at the right time.
   14571                 :             : 
   14572                 :             : Bexpression*
   14573                 :      167662 : Call_result_expression::do_get_backend(Translate_context* context)
   14574                 :             : {
   14575                 :      167662 :   Call_expression* ce = this->call_->call_expression();
   14576                 :      167662 :   if (ce == NULL)
   14577                 :             :     {
   14578                 :           0 :       go_assert(this->call_->is_error_expression());
   14579                 :           0 :       return context->backend()->error_expression();
   14580                 :             :     }
   14581                 :      167662 :   Temporary_statement* ts = ce->results();
   14582                 :      167662 :   if (ts == NULL)
   14583                 :             :     {
   14584                 :           0 :       go_assert(saw_errors());
   14585                 :           0 :       return context->backend()->error_expression();
   14586                 :             :     }
   14587                 :      167662 :   Expression* ref = Expression::make_temporary_reference(ts, this->location());
   14588                 :      167662 :   ref = Expression::make_field_reference(ref, this->index_, this->location());
   14589                 :      167662 :   return ref->get_backend(context);
   14590                 :             : }
   14591                 :             : 
   14592                 :             : // Dump ast representation for a call result expression.
   14593                 :             : 
   14594                 :             : void
   14595                 :           0 : Call_result_expression::do_dump_expression(Ast_dump_context* ast_dump_context)
   14596                 :             :     const
   14597                 :             : {
   14598                 :             :   // FIXME: Wouldn't it be better if the call is assigned to a temporary
   14599                 :             :   // (struct) and the fields are referenced instead.
   14600                 :           0 :   ast_dump_context->ostream() << this->index_ << "@(";
   14601                 :           0 :   ast_dump_context->dump_expression(this->call_);
   14602                 :           0 :   ast_dump_context->ostream() << ")";
   14603                 :           0 : }
   14604                 :             : 
   14605                 :             : // Make a reference to a single result of a call which returns
   14606                 :             : // multiple results.
   14607                 :             : 
   14608                 :             : Expression*
   14609                 :      168017 : Expression::make_call_result(Call_expression* call, unsigned int index)
   14610                 :             : {
   14611                 :      168017 :   return new Call_result_expression(call, index);
   14612                 :             : }
   14613                 :             : 
   14614                 :             : // Class Index_expression.
   14615                 :             : 
   14616                 :             : // Report whether EXPR is a map index expression.  This is called when
   14617                 :             : // types are determined but before lowering.
   14618                 :             : 
   14619                 :             : bool
   14620                 :       34763 : Index_expression::is_map_index(Expression* expr)
   14621                 :             : {
   14622                 :       34763 :   if (expr->map_index_expression() != NULL)
   14623                 :             :     return true;
   14624                 :       34763 :   if (expr->index_expression() != NULL)
   14625                 :       14232 :     return expr->index_expression()->left_->type()->map_type() != NULL;
   14626                 :             :   return false;
   14627                 :             : }
   14628                 :             : 
   14629                 :             : // Traversal.
   14630                 :             : 
   14631                 :             : int
   14632                 :      823151 : Index_expression::do_traverse(Traverse* traverse)
   14633                 :             : {
   14634                 :      823151 :   if (Expression::traverse(&this->left_, traverse) == TRAVERSE_EXIT
   14635                 :      823151 :       || Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT
   14636                 :      823150 :       || (this->end_ != NULL
   14637                 :      238856 :           && Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT)
   14638                 :     1646301 :       || (this->cap_ != NULL
   14639                 :       19045 :           && Expression::traverse(&this->cap_, traverse) == TRAVERSE_EXIT))
   14640                 :           1 :     return TRAVERSE_EXIT;
   14641                 :             :   return TRAVERSE_CONTINUE;
   14642                 :             : }
   14643                 :             : 
   14644                 :             : void
   14645                 :      213061 : Index_expression::do_determine_type(Gogo* gogo, const Type_context*)
   14646                 :             : {
   14647                 :      213061 :   this->left_->determine_type_no_context(gogo);
   14648                 :             : 
   14649                 :      213061 :   Type_context end_context;
   14650                 :      213061 :   Type* type = this->left_->type();
   14651                 :      213061 :   if (type->array_type() != NULL
   14652                 :       51651 :       || (type->points_to() != NULL
   14653                 :       51652 :           && type->points_to()->array_type() != NULL
   14654                 :       15822 :           && !type->points_to()->is_slice_type())
   14655                 :       35829 :       || type->is_string_type())
   14656                 :             :     {
   14657                 :      196990 :       Type_context index_context(Type::lookup_integer_type("int"), false);
   14658                 :      196990 :       this->start_->determine_type(gogo, &index_context);
   14659                 :      196990 :       end_context = index_context;
   14660                 :             :     }
   14661                 :       16071 :   else if (type->map_type() != NULL)
   14662                 :             :     {
   14663                 :       32128 :       Type_context key_context(type->map_type()->key_type(), false);
   14664                 :       16064 :       this->start_->determine_type(gogo, &key_context);
   14665                 :             :     }
   14666                 :           7 :   else if (type->is_error())
   14667                 :           1 :     this->set_is_error();
   14668                 :             :   else
   14669                 :             :     {
   14670                 :           6 :       if (this->cap_ != NULL)
   14671                 :           0 :         this->report_error(_("invalid 3-index slice of object "
   14672                 :             :                              "that is not a slice"));
   14673                 :           6 :       else if (this->end_ != NULL)
   14674                 :           5 :         this->report_error(_("attempt to slice object that is not "
   14675                 :             :                              "array, slice, or string"));
   14676                 :             :       else
   14677                 :           1 :         this->report_error(_("attempt to index object that is not "
   14678                 :             :                              "array, slice, string, or map"));
   14679                 :             : 
   14680                 :           6 :       this->start_->determine_type_no_context(gogo);
   14681                 :             :     }
   14682                 :             : 
   14683                 :      213061 :   if (this->end_ != NULL)
   14684                 :       50072 :     this->end_->determine_type(gogo, &end_context);
   14685                 :      213061 :   if (this->cap_ != NULL)
   14686                 :        3824 :     this->cap_->determine_type(gogo, &end_context);
   14687                 :      213061 : }
   14688                 :             : 
   14689                 :             : Type*
   14690                 :      364616 : Index_expression::do_type()
   14691                 :             : {
   14692                 :      364616 :   if (this->is_error_expression())
   14693                 :          10 :     return Type::make_error_type();
   14694                 :             : 
   14695                 :      364606 :   Type* type = this->left_->type();
   14696                 :      364606 :   if (this->end_ != NULL)
   14697                 :             :     {
   14698                 :             :       // A slice of an array is a slice type.  A slice of a slice or
   14699                 :             :       // string type is that same type.
   14700                 :      112050 :       Array_type* at = type->deref()->array_type();
   14701                 :       43426 :       if (at != NULL && !at->is_slice_type())
   14702                 :       16257 :         return Type::make_array_type(at->element_type(), NULL);
   14703                 :       39768 :       return type;
   14704                 :             :     }
   14705                 :      617162 :   if (type->deref()->array_type() != NULL)
   14706                 :      816789 :     return type->deref()->array_type()->element_type();
   14707                 :       36318 :   else if (type->is_string_type())
   14708                 :       14018 :     return Type::lookup_integer_type("byte");
   14709                 :       22300 :   else if (type->map_type() != NULL)
   14710                 :       44600 :     return type->map_type()->val_type();
   14711                 :             :   else
   14712                 :           0 :     return Type::make_error_type();
   14713                 :             : }
   14714                 :             : 
   14715                 :             : void
   14716                 :      157136 : Index_expression::do_check_types(Gogo*)
   14717                 :             : {
   14718                 :      157136 :   if (this->is_error_expression())
   14719                 :             :     return;
   14720                 :             : 
   14721                 :      157129 :   Type* ltype = this->left_->type();
   14722                 :      157129 :   if (ltype->is_error())
   14723                 :             :     {
   14724                 :           0 :       go_assert(saw_errors());
   14725                 :             :       return;
   14726                 :             :     }
   14727                 :             : 
   14728                 :      157129 :   if (this->left_->is_type_expression())
   14729                 :             :     {
   14730                 :           0 :       this->report_error(_("attempt to index type expression"));
   14731                 :           0 :       return;
   14732                 :             :     }
   14733                 :             : 
   14734                 :      157129 :   if (ltype->array_type() != NULL)
   14735                 :             :     {
   14736                 :      110274 :       if (!Array_index_expression::check_indexes(this->left_, this->start_,
   14737                 :             :                                                  this->end_, this->cap_,
   14738                 :             :                                                  this->location()))
   14739                 :        1535 :         this->set_is_error();
   14740                 :             :     }
   14741                 :       46855 :   else if (ltype->points_to() != NULL
   14742                 :       15637 :            && ltype->points_to()->array_type() != NULL
   14743                 :       62492 :            && !ltype->points_to()->is_slice_type())
   14744                 :             :     {
   14745                 :       15637 :       Expression* deref = Expression::make_dereference(this->left_,
   14746                 :             :                                                        NIL_CHECK_DEFAULT,
   14747                 :             :                                                        this->location());
   14748                 :       15637 :       if (!Array_index_expression::check_indexes(deref, this->start_,
   14749                 :             :                                                  this->end_, this->cap_,
   14750                 :             :                                                  this->location()))
   14751                 :         979 :         this->set_is_error();
   14752                 :       15637 :       delete deref;
   14753                 :             :     }
   14754                 :       31218 :   else if (ltype->is_string_type())
   14755                 :             :     {
   14756                 :       17514 :       if (this->cap_ != NULL)
   14757                 :           8 :         this->report_error(_("invalid 3-index slice of string"));
   14758                 :             :       else
   14759                 :             :         {
   14760                 :       17506 :           if (!String_index_expression::check_indexes(this->left_,
   14761                 :             :                                                       this->start_,
   14762                 :             :                                                       this->end_,
   14763                 :             :                                                       this->location()))
   14764                 :          15 :             this->set_is_error();
   14765                 :             :         }
   14766                 :             :     }
   14767                 :       13704 :   else if (ltype->map_type() != NULL)
   14768                 :             :     {
   14769                 :       13704 :       if (this->end_ != NULL || this->cap_ != NULL)
   14770                 :           0 :         this->report_error(_("invalid slice of map"));
   14771                 :             :       else
   14772                 :             :         {
   14773                 :       13704 :           Map_type* mt = ltype->map_type();
   14774                 :       13704 :           std::string reason;
   14775                 :       13704 :           if (!Type::are_assignable(mt->key_type(), this->start_->type(),
   14776                 :             :                                     &reason))
   14777                 :             :             {
   14778                 :           1 :               if (reason.empty())
   14779                 :           0 :                 this->report_error(_("incompatible type for map index"));
   14780                 :             :               else
   14781                 :             :                 {
   14782                 :           1 :                   go_error_at(this->location(),
   14783                 :             :                               "incompatible type for map index (%s)",
   14784                 :             :                               reason.c_str());
   14785                 :           1 :                   this->set_is_error();
   14786                 :             :                 }
   14787                 :             :             }
   14788                 :       13704 :         }
   14789                 :             :     }
   14790                 :             :   else
   14791                 :           0 :     go_unreachable();
   14792                 :             : }
   14793                 :             : 
   14794                 :             : bool
   14795                 :       40649 : Index_expression::do_is_addressable() const
   14796                 :             : {
   14797                 :       40649 :   if (this->is_error_expression())
   14798                 :             :     return true;
   14799                 :             : 
   14800                 :       40649 :   Type* type = this->left_->type();
   14801                 :       40649 :   if (type->is_error())
   14802                 :             :     return true;
   14803                 :             : 
   14804                 :             :   // A slice index is addressable, and an index of an addressable
   14805                 :             :   // array is addressable.
   14806                 :             : 
   14807                 :       40649 :   bool is_pointer = false;
   14808                 :       40649 :   if (type->points_to() != NULL
   14809                 :        3647 :       && type->points_to()->array_type() != NULL
   14810                 :       44296 :       && !type->points_to()->is_slice_type())
   14811                 :             :     {
   14812                 :        3647 :       type = type->points_to();
   14813                 :        3647 :       is_pointer = true;
   14814                 :             :     }
   14815                 :             : 
   14816                 :       46452 :   if (type->array_type() == NULL)
   14817                 :             :     return false;
   14818                 :             : 
   14819                 :       34904 :   if (this->end_ != NULL)
   14820                 :             :     return false;
   14821                 :       34850 :   if (type->is_slice_type())
   14822                 :             :     return true;
   14823                 :       13523 :   return is_pointer || this->left_->is_addressable();
   14824                 :             : }
   14825                 :             : 
   14826                 :             : // We need to do a nil check of any pointer dereference.
   14827                 :             : 
   14828                 :             : void
   14829                 :        4655 : Index_expression::do_issue_nil_check()
   14830                 :             : {
   14831                 :        4655 :   this->left_->issue_nil_check();
   14832                 :        4655 :   this->needs_nil_check_ = true;
   14833                 :        4655 : }
   14834                 :             : 
   14835                 :             : // Lower an index expression.  This converts the generic index
   14836                 :             : // expression into an array index, a string index, or a map index.
   14837                 :             : 
   14838                 :             : Expression*
   14839                 :      189160 : Index_expression::do_lower(Gogo*, Named_object*, Statement_inserter*)
   14840                 :             : {
   14841                 :      189160 :   if (this->is_error_expression())
   14842                 :        2543 :     return Expression::make_error(this->location());
   14843                 :             : 
   14844                 :      186617 :   Location location = this->location();
   14845                 :      186617 :   Expression* left = this->left_;
   14846                 :      186617 :   Expression* start = this->start_;
   14847                 :      186617 :   Expression* end = this->end_;
   14848                 :      186617 :   Expression* cap = this->cap_;
   14849                 :             : 
   14850                 :      186617 :   Type* type = left->type();
   14851                 :      186617 :   if (type->is_error())
   14852                 :             :     {
   14853                 :           1 :       go_assert(saw_errors());
   14854                 :           1 :       return Expression::make_error(location);
   14855                 :             :     }
   14856                 :      186616 :   else if (type->array_type() != NULL)
   14857                 :      138128 :     return Expression::make_array_index(left, start, end, cap, location);
   14858                 :       48488 :   else if (type->points_to() != NULL
   14859                 :       14748 :            && type->points_to()->array_type() != NULL
   14860                 :       63236 :            && !type->points_to()->is_slice_type())
   14861                 :             :     {
   14862                 :       14748 :       Expression* deref =
   14863                 :       14748 :           Expression::make_dereference(left, NIL_CHECK_DEFAULT, location);
   14864                 :             : 
   14865                 :             :       // For an ordinary index into the array, the pointer will be
   14866                 :             :       // dereferenced.  For a slice it will not--the resulting slice
   14867                 :             :       // will simply reuse the pointer, which is incorrect if that
   14868                 :             :       // pointer is nil.  We may also be in a context that requires a
   14869                 :             :       // nil check.
   14870                 :       14748 :       if (end != NULL || cap != NULL || this->needs_nil_check_)
   14871                 :        3964 :         deref->issue_nil_check();
   14872                 :             : 
   14873                 :       14748 :       return Expression::make_array_index(deref, start, end, cap, location);
   14874                 :             :     }
   14875                 :       33740 :   else if (type->is_string_type())
   14876                 :             :     {
   14877                 :       19681 :       go_assert(cap == NULL);
   14878                 :       19681 :       return Expression::make_string_index(left, start, end, location);
   14879                 :             :     }
   14880                 :       14059 :   else if (type->map_type() != NULL)
   14881                 :             :     {
   14882                 :       14059 :       go_assert(end == NULL && cap == NULL);
   14883                 :       14059 :       return Expression::make_map_index(left, start, location);
   14884                 :             :     }
   14885                 :             :   else
   14886                 :           0 :     go_unreachable();
   14887                 :             : }
   14888                 :             : 
   14889                 :             : // Write an indexed expression
   14890                 :             : // (expr[expr:expr:expr], expr[expr:expr] or expr[expr]) to a dump context.
   14891                 :             : 
   14892                 :             : void
   14893                 :           0 : Index_expression::dump_index_expression(Ast_dump_context* ast_dump_context,
   14894                 :             :                                         const Expression* expr,
   14895                 :             :                                         const Expression* start,
   14896                 :             :                                         const Expression* end,
   14897                 :             :                                         const Expression* cap)
   14898                 :             : {
   14899                 :           0 :   expr->dump_expression(ast_dump_context);
   14900                 :           0 :   ast_dump_context->ostream() << "[";
   14901                 :           0 :   start->dump_expression(ast_dump_context);
   14902                 :           0 :   if (end != NULL)
   14903                 :             :     {
   14904                 :           0 :       ast_dump_context->ostream() << ":";
   14905                 :           0 :       end->dump_expression(ast_dump_context);
   14906                 :             :     }
   14907                 :           0 :   if (cap != NULL)
   14908                 :             :     {
   14909                 :           0 :       ast_dump_context->ostream() << ":";
   14910                 :           0 :       cap->dump_expression(ast_dump_context);
   14911                 :             :     }
   14912                 :           0 :   ast_dump_context->ostream() << "]";
   14913                 :           0 : }
   14914                 :             : 
   14915                 :             : // Dump ast representation for an index expression.
   14916                 :             : 
   14917                 :             : void
   14918                 :           0 : Index_expression::do_dump_expression(Ast_dump_context* ast_dump_context)
   14919                 :             :     const
   14920                 :             : {
   14921                 :           0 :   Index_expression::dump_index_expression(ast_dump_context, this->left_,
   14922                 :           0 :                                           this->start_, this->end_, this->cap_);
   14923                 :           0 : }
   14924                 :             : 
   14925                 :             : // Make an index expression.
   14926                 :             : 
   14927                 :             : Expression*
   14928                 :      181155 : Expression::make_index(Expression* left, Expression* start, Expression* end,
   14929                 :             :                        Expression* cap, Location location)
   14930                 :             : {
   14931                 :      181155 :   return new Index_expression(left, start, end, cap, location);
   14932                 :             : }
   14933                 :             : 
   14934                 :             : // Class Array_index_expression.
   14935                 :             : 
   14936                 :             : // Array index traversal.
   14937                 :             : 
   14938                 :             : int
   14939                 :     2785701 : Array_index_expression::do_traverse(Traverse* traverse)
   14940                 :             : {
   14941                 :     2785701 :   if (Expression::traverse(&this->array_, traverse) == TRAVERSE_EXIT)
   14942                 :             :     return TRAVERSE_EXIT;
   14943                 :     2759729 :   if (Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT)
   14944                 :             :     return TRAVERSE_EXIT;
   14945                 :     2756612 :   if (this->end_ != NULL)
   14946                 :             :     {
   14947                 :      629217 :       if (Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT)
   14948                 :             :         return TRAVERSE_EXIT;
   14949                 :             :     }
   14950                 :     2756518 :   if (this->cap_ != NULL)
   14951                 :             :     {
   14952                 :       58788 :       if (Expression::traverse(&this->cap_, traverse) == TRAVERSE_EXIT)
   14953                 :             :         return TRAVERSE_EXIT;
   14954                 :             :     }
   14955                 :             :   return TRAVERSE_CONTINUE;
   14956                 :             : }
   14957                 :             : 
   14958                 :             : // Return the type of an array index.
   14959                 :             : 
   14960                 :             : Type*
   14961                 :     2602467 : Array_index_expression::do_type()
   14962                 :             : {
   14963                 :     2602467 :   if (this->type_ == NULL)
   14964                 :             :     {
   14965                 :      218688 :      Array_type* type = this->array_->type()->array_type();
   14966                 :      218688 :       if (type == NULL)
   14967                 :           0 :         this->type_ = Type::make_error_type();
   14968                 :      218688 :       else if (this->end_ == NULL)
   14969                 :      166041 :         this->type_ = type->element_type();
   14970                 :       52647 :       else if (type->is_slice_type())
   14971                 :             :         {
   14972                 :             :           // A slice of a slice has the same type as the original
   14973                 :             :           // slice.
   14974                 :       79858 :           this->type_ = this->array_->type()->deref();
   14975                 :             :         }
   14976                 :             :       else
   14977                 :             :         {
   14978                 :             :           // A slice of an array is a slice.
   14979                 :       12718 :           this->type_ = Type::make_array_type(type->element_type(), NULL);
   14980                 :             :         }
   14981                 :             :     }
   14982                 :     2602467 :   return this->type_;
   14983                 :             : }
   14984                 :             : 
   14985                 :             : // Set the type of an array index.
   14986                 :             : 
   14987                 :             : void
   14988                 :      183524 : Array_index_expression::do_determine_type(Gogo* gogo, const Type_context*)
   14989                 :             : {
   14990                 :      183524 :   this->array_->determine_type_no_context(gogo);
   14991                 :             : 
   14992                 :      183524 :   Type_context index_context(Type::lookup_integer_type("int"), false);
   14993                 :      183524 :   this->start_->determine_type(gogo, &index_context);
   14994                 :      183524 :   if (this->end_ != NULL)
   14995                 :       56850 :     this->end_->determine_type(gogo, &index_context);
   14996                 :      183524 :   if (this->cap_ != NULL)
   14997                 :        3342 :     this->cap_->determine_type(gogo, &index_context);
   14998                 :      183524 : }
   14999                 :             : 
   15000                 :             : // Check types of an array index.
   15001                 :             : 
   15002                 :             : void
   15003                 :         551 : Array_index_expression::do_check_types(Gogo*)
   15004                 :             : {
   15005                 :         551 :   if (!Array_index_expression::check_indexes(this->array_, this->start_,
   15006                 :             :                                              this->end_, this->cap_,
   15007                 :             :                                              this->location()))
   15008                 :           0 :     this->set_is_error();
   15009                 :         551 : }
   15010                 :             : 
   15011                 :             : // A static function to check array index types.  This is also called
   15012                 :             : // by Index_expression::do_check_types.  It reports whether type
   15013                 :             : // checking succeeded.
   15014                 :             : 
   15015                 :             : bool
   15016                 :      126462 : Array_index_expression::check_indexes(Expression* array, Expression* start,
   15017                 :             :                                       Expression* end, Expression* cap,
   15018                 :             :                                       Location loc)
   15019                 :             : {
   15020                 :      126462 :   bool ret = true;
   15021                 :             : 
   15022                 :      126462 :   Numeric_constant nc;
   15023                 :      126462 :   unsigned long v;
   15024                 :      126467 :   if (start->type()->integer_type() == NULL
   15025                 :           5 :       && !start->type()->is_error()
   15026                 :           5 :       && (!start->type()->is_abstract()
   15027                 :           0 :           || !start->numeric_constant_value(&nc)
   15028                 :           0 :           || nc.to_unsigned_long(&v) == Numeric_constant::NC_UL_NOTINT))
   15029                 :             :     {
   15030                 :           5 :       go_error_at(loc, "index must be integer");
   15031                 :           5 :       ret = false;
   15032                 :             :     }
   15033                 :             : 
   15034                 :      126462 :   if (end != NULL
   15035                 :       53125 :       && end->type()->integer_type() == NULL
   15036                 :       15763 :       && !end->type()->is_error()
   15037                 :       15754 :       && !end->is_nil_expression()
   15038                 :           0 :       && !end->is_error_expression()
   15039                 :      126462 :       && (!end->type()->is_abstract()
   15040                 :           0 :           || !end->numeric_constant_value(&nc)
   15041                 :           0 :           || nc.to_unsigned_long(&v) == Numeric_constant::NC_UL_NOTINT))
   15042                 :             :     {
   15043                 :           0 :       go_error_at(loc, "slice end must be integer");
   15044                 :           0 :       ret = false;
   15045                 :             :     }
   15046                 :             : 
   15047                 :      126462 :   if (cap != NULL
   15048                 :        3809 :       && cap->type()->integer_type() == NULL
   15049                 :           8 :       && !cap->type()->is_error()
   15050                 :           0 :       && !cap->is_nil_expression()
   15051                 :           0 :       && !cap->is_error_expression()
   15052                 :      126462 :       && (!cap->type()->is_abstract()
   15053                 :           0 :           || !cap->numeric_constant_value(&nc)
   15054                 :           0 :           || nc.to_unsigned_long(&v) == Numeric_constant::NC_UL_NOTINT))
   15055                 :             :     {
   15056                 :           0 :       go_error_at(loc, "slice capacity must be integer");
   15057                 :           0 :       ret = false;
   15058                 :             :     }
   15059                 :             : 
   15060                 :      126462 :   Array_type* array_type = array->type()->array_type();
   15061                 :      126462 :   if (array_type == NULL)
   15062                 :             :     {
   15063                 :           0 :       go_assert(array->type()->is_error());
   15064                 :             :       return false;
   15065                 :             :     }
   15066                 :             : 
   15067                 :      126462 :   unsigned int int_bits =
   15068                 :      252924 :     Type::lookup_integer_type("int")->integer_type()->bits();
   15069                 :             : 
   15070                 :      126462 :   Numeric_constant lvalnc;
   15071                 :      126462 :   mpz_t lval;
   15072                 :      126462 :   bool lval_valid = (array_type->length() != NULL
   15073                 :       51618 :                      && array_type->length()->numeric_constant_value(&lvalnc)
   15074                 :      178080 :                      && lvalnc.to_int(&lval));
   15075                 :      126462 :   Numeric_constant inc;
   15076                 :      126462 :   mpz_t ival;
   15077                 :      126462 :   bool ival_valid = false;
   15078                 :      126462 :   if (start->numeric_constant_value(&inc) && inc.to_int(&ival))
   15079                 :             :     {
   15080                 :       71376 :       ival_valid = true;
   15081                 :      106799 :       if (mpz_sgn(ival) < 0
   15082                 :       70400 :           || mpz_sizeinbase(ival, 2) >= int_bits
   15083                 :       70398 :           || (lval_valid
   15084                 :       24171 :               && (end == NULL
   15085                 :       24171 :                   ? mpz_cmp(ival, lval) >= 0
   15086                 :       11252 :                   : mpz_cmp(ival, lval) > 0)))
   15087                 :             :         {
   15088                 :        1491 :           go_error_at(start->location(), "array index out of bounds");
   15089                 :        1491 :           ret = false;
   15090                 :             :         }
   15091                 :             :     }
   15092                 :             : 
   15093                 :      126462 :   if (end != NULL && !end->is_nil_expression())
   15094                 :             :     {
   15095                 :       21608 :       Numeric_constant enc;
   15096                 :       21608 :       mpz_t eval;
   15097                 :       21608 :       bool eval_valid = false;
   15098                 :       21608 :       if (end->numeric_constant_value(&enc) && enc.to_int(&eval))
   15099                 :             :         {
   15100                 :        7697 :           eval_valid = true;
   15101                 :        7697 :           if (mpz_sgn(eval) < 0
   15102                 :        6723 :               || mpz_sizeinbase(eval, 2) >= int_bits
   15103                 :        6721 :               || (lval_valid && mpz_cmp(eval, lval) > 0))
   15104                 :             :             {
   15105                 :        1489 :               go_error_at(end->location(), "array index out of bounds");
   15106                 :        1489 :               ret = false;
   15107                 :             :             }
   15108                 :        6208 :           else if (ival_valid && mpz_cmp(ival, eval) > 0)
   15109                 :             :             {
   15110                 :          14 :               go_error_at(loc, "inverted slice range");
   15111                 :          14 :               ret = false;
   15112                 :             :             }
   15113                 :             :         }
   15114                 :             : 
   15115                 :       21608 :       Numeric_constant cnc;
   15116                 :       21608 :       mpz_t cval;
   15117                 :       21608 :       if (cap != NULL
   15118                 :       25409 :           && cap->numeric_constant_value(&cnc) && cnc.to_int(&cval))
   15119                 :             :         {
   15120                 :        1201 :           if (mpz_sgn(cval) < 0
   15121                 :        1201 :               || mpz_sizeinbase(cval, 2) >= int_bits
   15122                 :        1201 :               || (lval_valid && mpz_cmp(cval, lval) > 0))
   15123                 :             :             {
   15124                 :           3 :               go_error_at(cap->location(), "array index out of bounds");
   15125                 :           3 :               ret = false;
   15126                 :             :             }
   15127                 :        1198 :           else if (ival_valid && mpz_cmp(ival, cval) > 0)
   15128                 :             :             {
   15129                 :          10 :               go_error_at(cap->location(),
   15130                 :             :                           "invalid slice index: capacity less than start");
   15131                 :          10 :               ret = false;
   15132                 :             :             }
   15133                 :        1188 :           else if (eval_valid && mpz_cmp(eval, cval) > 0)
   15134                 :             :             {
   15135                 :           6 :               go_error_at(cap->location(),
   15136                 :             :                           "invalid slice index: capacity less than length");
   15137                 :           6 :               ret = false;
   15138                 :             :             }
   15139                 :        1201 :           mpz_clear(cval);
   15140                 :             :         }
   15141                 :             : 
   15142                 :       21608 :       if (eval_valid)
   15143                 :        7697 :         mpz_clear(eval);
   15144                 :       21608 :     }
   15145                 :      126462 :   if (ival_valid)
   15146                 :       71376 :     mpz_clear(ival);
   15147                 :      126462 :   if (lval_valid)
   15148                 :       51618 :     mpz_clear(lval);
   15149                 :             : 
   15150                 :             :   // A slice of an array requires an addressable array.  A slice of a
   15151                 :             :   // slice is always possible.
   15152                 :      126462 :   if (end != NULL && !array_type->is_slice_type())
   15153                 :             :     {
   15154                 :       14285 :       if (!array->is_addressable())
   15155                 :             :         {
   15156                 :           4 :           go_error_at(loc, "slice of unaddressable value");
   15157                 :           4 :           ret = false;
   15158                 :             :         }
   15159                 :             :       else
   15160                 :             :         {
   15161                 :             :           // Set the array address taken but not escape. The escape
   15162                 :             :           // analysis will make it escape to heap when needed.
   15163                 :       14281 :           array->address_taken(false);
   15164                 :             :         }
   15165                 :             :     }
   15166                 :             : 
   15167                 :      126462 :   return ret;
   15168                 :      126462 : }
   15169                 :             : 
   15170                 :             : // The subexpressions of an array index must be evaluated in order.
   15171                 :             : // If this is indexing into an array, rather than a slice, then only
   15172                 :             : // the index should be evaluated.  Since this is called for values on
   15173                 :             : // the left hand side of an assigment, evaluating the array, meaning
   15174                 :             : // copying the array, will cause a different array to be modified.
   15175                 :             : 
   15176                 :             : bool
   15177                 :        4166 : Array_index_expression::do_must_eval_subexpressions_in_order(
   15178                 :             :     int* skip) const
   15179                 :             : {
   15180                 :        4166 :   *skip = this->array_->type()->is_slice_type() ? 0 : 1;
   15181                 :        4166 :   return true;
   15182                 :             : }
   15183                 :             : 
   15184                 :             : // Flatten array indexing: add temporary variables and bounds checks.
   15185                 :             : 
   15186                 :             : Expression*
   15187                 :      249456 : Array_index_expression::do_flatten(Gogo* gogo, Named_object*,
   15188                 :             :                                    Statement_inserter* inserter)
   15189                 :             : {
   15190                 :      249456 :   if (this->is_flattened_)
   15191                 :       40350 :     return this;
   15192                 :      209106 :   this->is_flattened_ = true;
   15193                 :             : 
   15194                 :      209106 :   Location loc = this->location();
   15195                 :             : 
   15196                 :      209106 :   if (this->is_error_expression())
   15197                 :           0 :     return Expression::make_error(loc);
   15198                 :             : 
   15199                 :      209106 :   Expression* array = this->array_;
   15200                 :      209106 :   Expression* start = this->start_;
   15201                 :      209106 :   Expression* end = this->end_;
   15202                 :      209106 :   Expression* cap = this->cap_;
   15203                 :      209106 :   if (array->is_error_expression()
   15204                 :      209106 :       || array->type()->is_error_type()
   15205                 :      209106 :       || start->is_error_expression()
   15206                 :      208669 :       || start->type()->is_error_type()
   15207                 :      208669 :       || (end != NULL
   15208                 :      104236 :           && (end->is_error_expression() || end->type()->is_error_type()))
   15209                 :      417479 :       || (cap != NULL
   15210                 :        7536 :           && (cap->is_error_expression() || cap->type()->is_error_type())))
   15211                 :             :     {
   15212                 :         737 :       go_assert(saw_errors());
   15213                 :         737 :       return Expression::make_error(loc);
   15214                 :             :     }
   15215                 :             : 
   15216                 :      208369 :   Array_type* array_type = this->array_->type()->array_type();
   15217                 :      208369 :   if (array_type == NULL)
   15218                 :             :     {
   15219                 :           0 :       go_assert(saw_errors());
   15220                 :           0 :       return Expression::make_error(loc);
   15221                 :             :     }
   15222                 :             : 
   15223                 :      208369 :   Temporary_statement* temp;
   15224                 :      208369 :   if (array_type->is_slice_type() && !array->is_multi_eval_safe())
   15225                 :             :     {
   15226                 :       18925 :       temp = Statement::make_temporary(NULL, array, loc);
   15227                 :       18925 :       inserter->insert(temp);
   15228                 :       18925 :       this->array_ = Expression::make_temporary_reference(temp, loc);
   15229                 :       18925 :       array = this->array_;
   15230                 :             :     }
   15231                 :      208369 :   if (!start->is_multi_eval_safe())
   15232                 :             :     {
   15233                 :       28762 :       temp = Statement::make_temporary(NULL, start, loc);
   15234                 :       28762 :       inserter->insert(temp);
   15235                 :       28762 :       this->start_ = Expression::make_temporary_reference(temp, loc);
   15236                 :       28762 :       start = this->start_;
   15237                 :             :     }
   15238                 :      208369 :   if (end != NULL
   15239                 :       51966 :       && !end->is_nil_expression()
   15240                 :      243887 :       && !end->is_multi_eval_safe())
   15241                 :             :     {
   15242                 :        3946 :       temp = Statement::make_temporary(NULL, end, loc);
   15243                 :        3946 :       inserter->insert(temp);
   15244                 :        3946 :       this->end_ = Expression::make_temporary_reference(temp, loc);
   15245                 :        3946 :       end = this->end_;
   15246                 :             :     }
   15247                 :      208369 :   if (cap != NULL && !cap->is_multi_eval_safe())
   15248                 :             :     {
   15249                 :         328 :       temp = Statement::make_temporary(NULL, cap, loc);
   15250                 :         328 :       inserter->insert(temp);
   15251                 :         328 :       this->cap_ = Expression::make_temporary_reference(temp, loc);
   15252                 :         328 :       cap = this->cap_;
   15253                 :             :     }
   15254                 :             : 
   15255                 :      208369 :   if (!this->needs_bounds_check_)
   15256                 :       33694 :     return this;
   15257                 :             : 
   15258                 :      174675 :   Expression* len;
   15259                 :      174675 :   if (!array_type->is_slice_type())
   15260                 :             :     {
   15261                 :       80215 :       len = array_type->get_length(gogo, this->array_);
   15262                 :       80215 :       go_assert(len->is_constant());
   15263                 :             :     }
   15264                 :             :   else
   15265                 :             :     {
   15266                 :       94460 :       len = array_type->get_length(gogo, this->array_->copy());
   15267                 :       94460 :       temp = Statement::make_temporary(NULL, len, loc);
   15268                 :       94460 :       temp->determine_types(gogo);
   15269                 :       94460 :       inserter->insert(temp);
   15270                 :       94460 :       len = Expression::make_temporary_reference(temp, loc);
   15271                 :             :     }
   15272                 :             : 
   15273                 :      174675 :   Expression* scap = NULL;
   15274                 :      174675 :   if (array_type->is_slice_type())
   15275                 :             :     {
   15276                 :       94460 :       scap = array_type->get_capacity(gogo, this->array_->copy());
   15277                 :       94460 :       temp = Statement::make_temporary(NULL, scap, loc);
   15278                 :       94460 :       temp->determine_types(gogo);
   15279                 :       94460 :       inserter->insert(temp);
   15280                 :       94460 :       scap = Expression::make_temporary_reference(temp, loc);
   15281                 :             :     }
   15282                 :             : 
   15283                 :             :   // The order of bounds checks here matches the order used by the gc
   15284                 :             :   // compiler, as tested by issue30116[u].go.
   15285                 :             : 
   15286                 :      174675 :   if (cap != NULL)
   15287                 :             :     {
   15288                 :        3766 :       if (array_type->is_slice_type())
   15289                 :        2260 :         Expression::check_bounds(gogo, cap, OPERATOR_LE, scap,
   15290                 :             :                                  Runtime::PANIC_SLICE3_ACAP,
   15291                 :             :                                  Runtime::PANIC_SLICE3_ACAP_U,
   15292                 :             :                                  Runtime::PANIC_EXTEND_SLICE3_ACAP,
   15293                 :             :                                  Runtime::PANIC_EXTEND_SLICE3_ACAP_U,
   15294                 :             :                                  inserter, loc);
   15295                 :             :       else
   15296                 :        1506 :         Expression::check_bounds(gogo, cap, OPERATOR_LE, len,
   15297                 :             :                                  Runtime::PANIC_SLICE3_ALEN,
   15298                 :             :                                  Runtime::PANIC_SLICE3_ALEN_U,
   15299                 :             :                                  Runtime::PANIC_EXTEND_SLICE3_ALEN,
   15300                 :             :                                  Runtime::PANIC_EXTEND_SLICE3_ALEN_U,
   15301                 :             :                                  inserter, loc);
   15302                 :             : 
   15303                 :        3766 :       Expression* start_bound = cap;
   15304                 :        3766 :       if (end != NULL && !end->is_nil_expression())
   15305                 :             :         {
   15306                 :        3766 :           Expression::check_bounds(gogo, end, OPERATOR_LE, cap,
   15307                 :             :                                    Runtime::PANIC_SLICE3_B,
   15308                 :             :                                    Runtime::PANIC_SLICE3_B_U,
   15309                 :             :                                    Runtime::PANIC_EXTEND_SLICE3_B,
   15310                 :             :                                    Runtime::PANIC_EXTEND_SLICE3_B_U,
   15311                 :             :                                    inserter, loc);
   15312                 :        3766 :           start_bound = end;
   15313                 :             :         }
   15314                 :             : 
   15315                 :        3766 :       Expression::check_bounds(gogo, start, OPERATOR_LE, start_bound,
   15316                 :             :                                Runtime::PANIC_SLICE3_C,
   15317                 :             :                                Runtime::PANIC_SLICE3_C_U,
   15318                 :             :                                Runtime::PANIC_EXTEND_SLICE3_C,
   15319                 :             :                                Runtime::PANIC_EXTEND_SLICE3_C_U,
   15320                 :             :                                inserter, loc);
   15321                 :             :     }
   15322                 :      170909 :   else if (end != NULL && !end->is_nil_expression())
   15323                 :             :     {
   15324                 :       16099 :       if (array_type->is_slice_type())
   15325                 :       11950 :         Expression::check_bounds(gogo, end, OPERATOR_LE, scap,
   15326                 :             :                                  Runtime::PANIC_SLICE_ACAP,
   15327                 :             :                                  Runtime::PANIC_SLICE_ACAP_U,
   15328                 :             :                                  Runtime::PANIC_EXTEND_SLICE_ACAP,
   15329                 :             :                                  Runtime::PANIC_EXTEND_SLICE_ACAP_U,
   15330                 :             :                                  inserter, loc);
   15331                 :             :       else
   15332                 :        4149 :         Expression::check_bounds(gogo, end, OPERATOR_LE, len,
   15333                 :             :                                  Runtime::PANIC_SLICE_ALEN,
   15334                 :             :                                  Runtime::PANIC_SLICE_ALEN_U,
   15335                 :             :                                  Runtime::PANIC_EXTEND_SLICE_ALEN,
   15336                 :             :                                  Runtime::PANIC_EXTEND_SLICE_ALEN_U,
   15337                 :             :                                  inserter, loc);
   15338                 :             : 
   15339                 :       16099 :       Expression::check_bounds(gogo, start, OPERATOR_LE, end,
   15340                 :             :                                Runtime::PANIC_SLICE_B,
   15341                 :             :                                Runtime::PANIC_SLICE_B_U,
   15342                 :             :                                Runtime::PANIC_EXTEND_SLICE_B,
   15343                 :             :                                Runtime::PANIC_EXTEND_SLICE_B_U,
   15344                 :             :                                inserter, loc);
   15345                 :             :     }
   15346                 :      154810 :   else if (end != NULL)
   15347                 :             :     {
   15348                 :       15197 :       Expression* start_bound;
   15349                 :       15197 :       if (array_type->is_slice_type())
   15350                 :             :         start_bound = scap;
   15351                 :             :       else
   15352                 :        6666 :         start_bound = len;
   15353                 :       15197 :       Expression::check_bounds(gogo, start, OPERATOR_LE, start_bound,
   15354                 :             :                                Runtime::PANIC_SLICE_B,
   15355                 :             :                                Runtime::PANIC_SLICE_B_U,
   15356                 :             :                                Runtime::PANIC_EXTEND_SLICE_B,
   15357                 :             :                                Runtime::PANIC_EXTEND_SLICE_B_U,
   15358                 :             :                                inserter, loc);
   15359                 :             :     }
   15360                 :             :   else
   15361                 :      139613 :     Expression::check_bounds(gogo, start, OPERATOR_LT, len,
   15362                 :             :                              Runtime::PANIC_INDEX,
   15363                 :             :                              Runtime::PANIC_INDEX_U,
   15364                 :             :                              Runtime::PANIC_EXTEND_INDEX,
   15365                 :             :                              Runtime::PANIC_EXTEND_INDEX_U,
   15366                 :             :                              inserter, loc);
   15367                 :             : 
   15368                 :      174675 :   return this;
   15369                 :             : }
   15370                 :             : 
   15371                 :             : // Return whether this expression is addressable.
   15372                 :             : 
   15373                 :             : bool
   15374                 :        1158 : Array_index_expression::do_is_addressable() const
   15375                 :             : {
   15376                 :             :   // A slice expression is not addressable.
   15377                 :        1158 :   if (this->end_ != NULL)
   15378                 :             :     return false;
   15379                 :             : 
   15380                 :             :   // An index into a slice is addressable.
   15381                 :        1104 :   if (this->array_->type()->is_slice_type())
   15382                 :             :     return true;
   15383                 :             : 
   15384                 :             :   // An index into an array is addressable if the array is
   15385                 :             :   // addressable.
   15386                 :         432 :   return this->array_->is_addressable();
   15387                 :             : }
   15388                 :             : 
   15389                 :             : void
   15390                 :       13984 : Array_index_expression::do_address_taken(bool escapes)
   15391                 :             : {
   15392                 :             :   // In &x[0], if x is a slice, then x's address is not taken.
   15393                 :       13984 :   if (!this->array_->type()->is_slice_type())
   15394                 :        4590 :     this->array_->address_taken(escapes);
   15395                 :       13984 : }
   15396                 :             : 
   15397                 :             : // Get the backend representation for an array index.
   15398                 :             : 
   15399                 :             : Bexpression*
   15400                 :      207320 : Array_index_expression::do_get_backend(Translate_context* context)
   15401                 :             : {
   15402                 :      207320 :   Array_type* array_type = this->array_->type()->array_type();
   15403                 :      207320 :   if (array_type == NULL)
   15404                 :             :     {
   15405                 :           0 :       go_assert(this->array_->type()->is_error());
   15406                 :           0 :       return context->backend()->error_expression();
   15407                 :             :     }
   15408                 :      207320 :   go_assert(!array_type->is_slice_type()
   15409                 :             :             || this->array_->is_multi_eval_safe());
   15410                 :             : 
   15411                 :      207320 :   Location loc = this->location();
   15412                 :      207320 :   Gogo* gogo = context->gogo();
   15413                 :             : 
   15414                 :      207320 :   Type* int_type = Type::lookup_integer_type("int");
   15415                 :      207320 :   Btype* int_btype = int_type->get_backend(gogo);
   15416                 :             : 
   15417                 :             :   // Convert the length and capacity to "int".  FIXME: Do we need to
   15418                 :             :   // do this?
   15419                 :      207320 :   Bexpression* length = NULL;
   15420                 :      207320 :   if (this->end_ == NULL || this->end_->is_nil_expression())
   15421                 :             :     {
   15422                 :      171808 :       Expression* len = array_type->get_length(gogo, this->array_);
   15423                 :      171808 :       length = len->get_backend(context);
   15424                 :      171808 :       length = gogo->backend()->convert_expression(int_btype, length, loc);
   15425                 :             :     }
   15426                 :             : 
   15427                 :      207320 :   Bexpression* capacity = NULL;
   15428                 :      207320 :   if (this->end_ != NULL)
   15429                 :             :     {
   15430                 :       51960 :       Expression* cap = array_type->get_capacity(gogo, this->array_);
   15431                 :       51960 :       capacity = cap->get_backend(context);
   15432                 :       51960 :       capacity = gogo->backend()->convert_expression(int_btype, capacity, loc);
   15433                 :             :     }
   15434                 :             : 
   15435                 :      207320 :   Bexpression* cap_arg = capacity;
   15436                 :      207320 :   if (this->cap_ != NULL)
   15437                 :             :     {
   15438                 :        3766 :       cap_arg = this->cap_->get_backend(context);
   15439                 :        3766 :       cap_arg = gogo->backend()->convert_expression(int_btype, cap_arg, loc);
   15440                 :             :     }
   15441                 :             : 
   15442                 :      207320 :   if (length == NULL)
   15443                 :       35512 :     length = cap_arg;
   15444                 :             : 
   15445                 :      207320 :   if (this->start_->type()->integer_type() == NULL
   15446                 :           0 :       && !Type::are_convertible(int_type, this->start_->type(), NULL))
   15447                 :             :     {
   15448                 :           0 :       go_assert(saw_errors());
   15449                 :           0 :       return context->backend()->error_expression();
   15450                 :             :     }
   15451                 :             : 
   15452                 :      207320 :   Bexpression* start = this->start_->get_backend(context);
   15453                 :      207320 :   start = gogo->backend()->convert_expression(int_btype, start, loc);
   15454                 :             : 
   15455                 :      207320 :   Bfunction* bfn = context->function()->func_value()->get_decl();
   15456                 :      207320 :   if (this->end_ == NULL)
   15457                 :             :     {
   15458                 :             :       // Simple array indexing.
   15459                 :      155360 :       Bexpression* ret;
   15460                 :      155360 :       if (!array_type->is_slice_type())
   15461                 :             :         {
   15462                 :       66922 :           Bexpression* array = this->array_->get_backend(context);
   15463                 :       66922 :           ret = gogo->backend()->array_index_expression(array, start, loc);
   15464                 :             :         }
   15465                 :             :       else
   15466                 :             :         {
   15467                 :       88438 :           Expression* valptr = array_type->get_value_pointer(gogo,
   15468                 :             :                                                              this->array_);
   15469                 :       88438 :           Bexpression* ptr = valptr->get_backend(context);
   15470                 :       88438 :           ptr = gogo->backend()->pointer_offset_expression(ptr, start, loc);
   15471                 :             : 
   15472                 :      176876 :           Type* ele_type = this->array_->type()->array_type()->element_type();
   15473                 :       88438 :           Btype* ele_btype = ele_type->get_backend(gogo);
   15474                 :       88438 :           ret = gogo->backend()->indirect_expression(ele_btype, ptr, false,
   15475                 :             :                                                      loc);
   15476                 :             :         }
   15477                 :      155360 :       return ret;
   15478                 :             :     }
   15479                 :             : 
   15480                 :             :   // Slice expression.
   15481                 :             : 
   15482                 :       51960 :   Bexpression* end;
   15483                 :       51960 :   if (this->end_->is_nil_expression())
   15484                 :             :     end = length;
   15485                 :             :   else
   15486                 :             :     {
   15487                 :       35512 :       end = this->end_->get_backend(context);
   15488                 :       35512 :       end = gogo->backend()->convert_expression(int_btype, end, loc);
   15489                 :             :     }
   15490                 :             : 
   15491                 :       51960 :   Bexpression* result_length =
   15492                 :       51960 :     gogo->backend()->binary_expression(OPERATOR_MINUS, end, start, loc);
   15493                 :             : 
   15494                 :       51960 :   Bexpression* result_capacity =
   15495                 :       51960 :     gogo->backend()->binary_expression(OPERATOR_MINUS, cap_arg, start, loc);
   15496                 :             : 
   15497                 :             :   // If the new capacity is zero, don't change val.  Otherwise we can
   15498                 :             :   // get a pointer to the next object in memory, keeping it live
   15499                 :             :   // unnecessarily.  When the capacity is zero, the actual pointer
   15500                 :             :   // value doesn't matter.
   15501                 :       51960 :   Bexpression* zero =
   15502                 :       51960 :     Expression::make_integer_ul(0, int_type, loc)->get_backend(context);
   15503                 :       51960 :   Bexpression* cond =
   15504                 :       51960 :     gogo->backend()->binary_expression(OPERATOR_EQEQ, result_capacity, zero,
   15505                 :             :                                        loc);
   15506                 :       51960 :   Bexpression* offset = gogo->backend()->conditional_expression(bfn, int_btype,
   15507                 :             :                                                                 cond, zero,
   15508                 :             :                                                                 start, loc);
   15509                 :       51960 :   Expression* valptr = array_type->get_value_pointer(gogo, this->array_);
   15510                 :       51960 :   Bexpression* val = valptr->get_backend(context);
   15511                 :       51960 :   val = gogo->backend()->pointer_offset_expression(val, offset, loc);
   15512                 :             : 
   15513                 :       51960 :   Btype* struct_btype = this->type()->get_backend(gogo);
   15514                 :       51960 :   std::vector<Bexpression*> init;
   15515                 :       51960 :   init.push_back(val);
   15516                 :       51960 :   init.push_back(result_length);
   15517                 :       51960 :   init.push_back(result_capacity);
   15518                 :             : 
   15519                 :       51960 :   return gogo->backend()->constructor_expression(struct_btype, init, loc);
   15520                 :       51960 : }
   15521                 :             : 
   15522                 :             : // Export an array index expression.
   15523                 :             : 
   15524                 :             : void
   15525                 :        3549 : Array_index_expression::do_export(Export_function_body* efb) const
   15526                 :             : {
   15527                 :        3549 :   efb->write_c_string("(");
   15528                 :        3549 :   this->array_->export_expression(efb);
   15529                 :        3549 :   efb->write_c_string(")[");
   15530                 :             : 
   15531                 :        3549 :   Type* old_context = efb->type_context();
   15532                 :        3549 :   efb->set_type_context(Type::lookup_integer_type("int"));
   15533                 :             : 
   15534                 :        3549 :   this->start_->export_expression(efb);
   15535                 :        3549 :   if (this->end_ == NULL)
   15536                 :        2760 :     go_assert(this->cap_ == NULL);
   15537                 :             :   else
   15538                 :             :     {
   15539                 :         789 :       efb->write_c_string(":");
   15540                 :         789 :       if (!this->end_->is_nil_expression())
   15541                 :         427 :         this->end_->export_expression(efb);
   15542                 :         789 :       if (this->cap_ != NULL)
   15543                 :             :         {
   15544                 :           0 :           efb->write_c_string(":");
   15545                 :           0 :           this->cap_->export_expression(efb);
   15546                 :             :         }
   15547                 :             :     }
   15548                 :             : 
   15549                 :        3549 :   efb->set_type_context(old_context);
   15550                 :             : 
   15551                 :        3549 :   efb->write_c_string("]");
   15552                 :        3549 : }
   15553                 :             : 
   15554                 :             : // Dump ast representation for an array index expression.
   15555                 :             : 
   15556                 :             : void
   15557                 :           0 : Array_index_expression::do_dump_expression(Ast_dump_context* ast_dump_context)
   15558                 :             :     const
   15559                 :             : {
   15560                 :           0 :   Index_expression::dump_index_expression(ast_dump_context, this->array_,
   15561                 :           0 :                                           this->start_, this->end_, this->cap_);
   15562                 :           0 : }
   15563                 :             : 
   15564                 :             : // Make an array index expression.  END and CAP may be NULL.
   15565                 :             : 
   15566                 :             : Expression*
   15567                 :      218688 : Expression::make_array_index(Expression* array, Expression* start,
   15568                 :             :                              Expression* end, Expression* cap,
   15569                 :             :                              Location location)
   15570                 :             : {
   15571                 :      218688 :   return new Array_index_expression(array, start, end, cap, location);
   15572                 :             : }
   15573                 :             : 
   15574                 :             : // Class String_index_expression.
   15575                 :             : 
   15576                 :             : // String index traversal.
   15577                 :             : 
   15578                 :             : int
   15579                 :      314914 : String_index_expression::do_traverse(Traverse* traverse)
   15580                 :             : {
   15581                 :      314914 :   if (Expression::traverse(&this->string_, traverse) == TRAVERSE_EXIT)
   15582                 :             :     return TRAVERSE_EXIT;
   15583                 :      314671 :   if (Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT)
   15584                 :             :     return TRAVERSE_EXIT;
   15585                 :      314622 :   if (this->end_ != NULL)
   15586                 :             :     {
   15587                 :      179740 :       if (Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT)
   15588                 :             :         return TRAVERSE_EXIT;
   15589                 :             :     }
   15590                 :             :   return TRAVERSE_CONTINUE;
   15591                 :             : }
   15592                 :             : 
   15593                 :             : Expression*
   15594                 :       21435 : String_index_expression::do_flatten(Gogo* gogo, Named_object*,
   15595                 :             :                                     Statement_inserter* inserter)
   15596                 :             : {
   15597                 :       21435 :   if (this->is_flattened_)
   15598                 :        1203 :     return this;
   15599                 :       20232 :   this->is_flattened_ = true;
   15600                 :             : 
   15601                 :       20232 :   Location loc = this->location();
   15602                 :             : 
   15603                 :       20232 :   if (this->is_error_expression())
   15604                 :           0 :     return Expression::make_error(loc);
   15605                 :             : 
   15606                 :       20232 :   Expression* string = this->string_;
   15607                 :       20232 :   Expression* start = this->start_;
   15608                 :       20232 :   Expression* end = this->end_;
   15609                 :       20232 :   if (string->is_error_expression()
   15610                 :       20232 :       || string->type()->is_error_type()
   15611                 :       20232 :       || start->is_error_expression()
   15612                 :       20229 :       || start->type()->is_error_type()
   15613                 :       40461 :       || (end != NULL
   15614                 :       22840 :           && (end->is_error_expression() || end->type()->is_error_type())))
   15615                 :             :     {
   15616                 :           3 :       go_assert(saw_errors());
   15617                 :           3 :       return Expression::make_error(loc);
   15618                 :             :     }
   15619                 :             : 
   15620                 :       20229 :   Temporary_statement* temp;
   15621                 :       20229 :   if (!string->is_multi_eval_safe())
   15622                 :             :     {
   15623                 :        3072 :       temp = Statement::make_temporary(NULL, string, loc);
   15624                 :        3072 :       inserter->insert(temp);
   15625                 :        3072 :       this->string_ = Expression::make_temporary_reference(temp, loc);
   15626                 :        3072 :       string = this->string_;
   15627                 :             :     }
   15628                 :       20229 :   if (!start->is_multi_eval_safe())
   15629                 :             :     {
   15630                 :        4291 :       temp = Statement::make_temporary(NULL, start, loc);
   15631                 :        4291 :       inserter->insert(temp);
   15632                 :        4291 :       this->start_ = Expression::make_temporary_reference(temp, loc);
   15633                 :        4291 :       start = this->start_;
   15634                 :             :     }
   15635                 :       20229 :   if (end != NULL
   15636                 :       11420 :       && !end->is_nil_expression()
   15637                 :       26163 :       && !end->is_multi_eval_safe())
   15638                 :             :     {
   15639                 :        1800 :       temp = Statement::make_temporary(NULL, end, loc);
   15640                 :        1800 :       inserter->insert(temp);
   15641                 :        1800 :       this->end_ = Expression::make_temporary_reference(temp, loc);
   15642                 :        1800 :       end = this->end_;
   15643                 :             :     }
   15644                 :             : 
   15645                 :       20229 :   Expression* len = Expression::make_string_info(string->copy(),
   15646                 :             :                                                  STRING_INFO_LENGTH, loc);
   15647                 :       20229 :   temp = Statement::make_temporary(NULL, len, loc);
   15648                 :       20229 :   temp->determine_types(gogo);
   15649                 :       20229 :   inserter->insert(temp);
   15650                 :       20229 :   len = Expression::make_temporary_reference(temp, loc);
   15651                 :             : 
   15652                 :             :   // The order of bounds checks here matches the order used by the gc
   15653                 :             :   // compiler, as tested by issue30116[u].go.
   15654                 :             : 
   15655                 :       20229 :   if (end != NULL && !end->is_nil_expression())
   15656                 :             :     {
   15657                 :        5934 :       Expression::check_bounds(gogo, end, OPERATOR_LE, len,
   15658                 :             :                                Runtime::PANIC_SLICE_ALEN,
   15659                 :             :                                Runtime::PANIC_SLICE_ALEN_U,
   15660                 :             :                                Runtime::PANIC_EXTEND_SLICE_ALEN,
   15661                 :             :                                Runtime::PANIC_EXTEND_SLICE_ALEN_U,
   15662                 :             :                                inserter, loc);
   15663                 :        5934 :       Expression::check_bounds(gogo, start, OPERATOR_LE, end,
   15664                 :             :                                Runtime::PANIC_SLICE_B,
   15665                 :             :                                Runtime::PANIC_SLICE_B_U,
   15666                 :             :                                Runtime::PANIC_EXTEND_SLICE_B,
   15667                 :             :                                Runtime::PANIC_EXTEND_SLICE_B_U,
   15668                 :             :                                inserter, loc);
   15669                 :             :     }
   15670                 :       14295 :   else if (end != NULL)
   15671                 :        5486 :     Expression::check_bounds(gogo, start, OPERATOR_LE, len,
   15672                 :             :                              Runtime::PANIC_SLICE_B,
   15673                 :             :                              Runtime::PANIC_SLICE_B_U,
   15674                 :             :                              Runtime::PANIC_EXTEND_SLICE_B,
   15675                 :             :                              Runtime::PANIC_EXTEND_SLICE_B_U,
   15676                 :             :                              inserter, loc);
   15677                 :             :   else
   15678                 :        8809 :     Expression::check_bounds(gogo, start, OPERATOR_LT, len,
   15679                 :             :                              Runtime::PANIC_INDEX,
   15680                 :             :                              Runtime::PANIC_INDEX_U,
   15681                 :             :                              Runtime::PANIC_EXTEND_INDEX,
   15682                 :             :                              Runtime::PANIC_EXTEND_INDEX_U,
   15683                 :             :                              inserter, loc);
   15684                 :             : 
   15685                 :       20229 :   return this;
   15686                 :             : }
   15687                 :             : 
   15688                 :             : // Return the type of a string index.
   15689                 :             : 
   15690                 :             : Type*
   15691                 :      256128 : String_index_expression::do_type()
   15692                 :             : {
   15693                 :      256128 :   if (this->end_ == NULL)
   15694                 :      120127 :     return Type::lookup_integer_type("byte");
   15695                 :             :   else
   15696                 :      136001 :     return this->string_->type();
   15697                 :             : }
   15698                 :             : 
   15699                 :             : // Determine the type of a string index.
   15700                 :             : 
   15701                 :             : void
   15702                 :       10575 : String_index_expression::do_determine_type(Gogo* gogo, const Type_context*)
   15703                 :             : {
   15704                 :       10575 :   this->string_->determine_type_no_context(gogo);
   15705                 :             : 
   15706                 :       10575 :   Type_context index_context(Type::lookup_integer_type("int"), false);
   15707                 :       10575 :   this->start_->determine_type(gogo, &index_context);
   15708                 :       10575 :   if (this->end_ != NULL)
   15709                 :        4547 :     this->end_->determine_type(gogo, &index_context);
   15710                 :       10575 : }
   15711                 :             : 
   15712                 :             : // Check types of a string index.
   15713                 :             : 
   15714                 :             : void
   15715                 :           0 : String_index_expression::do_check_types(Gogo*)
   15716                 :             : {
   15717                 :           0 :   if (!String_index_expression::check_indexes(this->string_, this->start_,
   15718                 :             :                                               this->end_, this->location()))
   15719                 :           0 :     this->set_is_error();
   15720                 :           0 : }
   15721                 :             : 
   15722                 :             : // A static function to check string index types.  This is also called
   15723                 :             : // by Index_expression::do_check_types.  It reports whether type
   15724                 :             : // checking succeeded.
   15725                 :             : 
   15726                 :             : bool
   15727                 :       17506 : String_index_expression::check_indexes(Expression* string, Expression* start,
   15728                 :             :                                        Expression* end, Location loc)
   15729                 :             : {
   15730                 :       17506 :   bool ret = true;
   15731                 :             : 
   15732                 :       17506 :   Numeric_constant nc;
   15733                 :       17506 :   unsigned long v;
   15734                 :       17509 :   if (start->type()->integer_type() == NULL
   15735                 :           3 :       && !start->type()->is_error()
   15736                 :           3 :       && (!start->type()->is_abstract()
   15737                 :           0 :           || !start->numeric_constant_value(&nc)
   15738                 :           0 :           || nc.to_unsigned_long(&v) == Numeric_constant::NC_UL_NOTINT))
   15739                 :             :     {
   15740                 :           3 :       go_error_at(loc, "index must be integer");
   15741                 :           3 :       ret = false;
   15742                 :             :     }
   15743                 :             : 
   15744                 :       17506 :   if (end != NULL
   15745                 :       14387 :       && end->type()->integer_type() == NULL
   15746                 :        4709 :       && !end->type()->is_error()
   15747                 :        4709 :       && !end->is_nil_expression()
   15748                 :           0 :       && !end->is_error_expression()
   15749                 :       17506 :       && (!end->type()->is_abstract()
   15750                 :           0 :           || !end->numeric_constant_value(&nc)
   15751                 :           0 :           || nc.to_unsigned_long(&v) == Numeric_constant::NC_UL_NOTINT))
   15752                 :             :     {
   15753                 :           0 :       go_error_at(loc, "slice end must be integer");
   15754                 :           0 :       ret = false;
   15755                 :             :     }
   15756                 :             : 
   15757                 :       17506 :   std::string sval;
   15758                 :       17506 :   bool sval_valid = string->string_constant_value(&sval);
   15759                 :             : 
   15760                 :       17506 :   Numeric_constant inc;
   15761                 :       17506 :   mpz_t ival;
   15762                 :       17506 :   bool ival_valid = false;
   15763                 :       17506 :   if (start->numeric_constant_value(&inc) && inc.to_int(&ival))
   15764                 :             :     {
   15765                 :        9360 :       ival_valid = true;
   15766                 :        9360 :       if (mpz_sgn(ival) < 0
   15767                 :        9360 :           || (sval_valid
   15768                 :          21 :               && (end == NULL
   15769                 :          21 :                   ? mpz_cmp_ui(ival, sval.length()) >= 0
   15770                 :         811 :                   : mpz_cmp_ui(ival, sval.length()) > 0)))
   15771                 :             :         {
   15772                 :           7 :           go_error_at(start->location(), "string index out of bounds");
   15773                 :           7 :           ret = false;
   15774                 :             :         }
   15775                 :             :     }
   15776                 :             : 
   15777                 :       17506 :   if (end != NULL && !end->is_nil_expression())
   15778                 :             :     {
   15779                 :        4969 :       Numeric_constant enc;
   15780                 :        4969 :       mpz_t eval;
   15781                 :        4969 :       if (end->numeric_constant_value(&enc) && enc.to_int(&eval))
   15782                 :             :         {
   15783                 :        1334 :           if (mpz_sgn(eval) < 0
   15784                 :        1334 :               || (sval_valid && mpz_cmp_ui(eval, sval.length()) > 0))
   15785                 :             :             {
   15786                 :           5 :               go_error_at(end->location(), "string index out of bounds");
   15787                 :           5 :               ret = false;
   15788                 :             :             }
   15789                 :        1329 :           else if (ival_valid && mpz_cmp(ival, eval) > 0)
   15790                 :             :             {
   15791                 :           2 :               go_error_at(loc, "inverted slice range");
   15792                 :           2 :               ret = false;
   15793                 :             :             }
   15794                 :        1334 :           mpz_clear(eval);
   15795                 :             :         }
   15796                 :        4969 :     }
   15797                 :       17506 :   if (ival_valid)
   15798                 :        9360 :     mpz_clear(ival);
   15799                 :             : 
   15800                 :       35012 :   return ret;
   15801                 :       17506 : }
   15802                 :             : 
   15803                 :             : // Get the backend representation for a string index.
   15804                 :             : 
   15805                 :             : Bexpression*
   15806                 :       20228 : String_index_expression::do_get_backend(Translate_context* context)
   15807                 :             : {
   15808                 :       20228 :   Location loc = this->location();
   15809                 :       20228 :   Gogo* gogo = context->gogo();
   15810                 :             : 
   15811                 :       20228 :   Type* int_type = Type::lookup_integer_type("int");
   15812                 :             : 
   15813                 :             :   // It is possible that an error occurred earlier because the start index
   15814                 :             :   // cannot be represented as an integer type.  In this case, we shouldn't
   15815                 :             :   // try casting the starting index into an integer since
   15816                 :             :   // Type_conversion_expression will fail to get the backend representation.
   15817                 :             :   // FIXME.
   15818                 :       20228 :   if (this->start_->type()->integer_type() == NULL
   15819                 :           0 :       && !Type::are_convertible(int_type, this->start_->type(), NULL))
   15820                 :             :     {
   15821                 :           0 :       go_assert(saw_errors());
   15822                 :           0 :       return context->backend()->error_expression();
   15823                 :             :     }
   15824                 :             : 
   15825                 :       20228 :   go_assert(this->string_->is_multi_eval_safe());
   15826                 :       20228 :   go_assert(this->start_->is_multi_eval_safe());
   15827                 :             : 
   15828                 :       20228 :   Expression* start = Expression::make_cast(int_type, this->start_, loc);
   15829                 :       20228 :   Bfunction* bfn = context->function()->func_value()->get_decl();
   15830                 :             : 
   15831                 :       20228 :   Expression* length =
   15832                 :       20228 :     Expression::make_string_info(this->string_, STRING_INFO_LENGTH, loc);
   15833                 :       20228 :   Expression* bytes =
   15834                 :       20228 :     Expression::make_string_info(this->string_, STRING_INFO_DATA, loc);
   15835                 :             : 
   15836                 :       20228 :   Bexpression* bstart = start->get_backend(context);
   15837                 :       20228 :   Bexpression* ptr = bytes->get_backend(context);
   15838                 :             : 
   15839                 :       20228 :   if (this->end_ == NULL)
   15840                 :             :     {
   15841                 :        8808 :       ptr = gogo->backend()->pointer_offset_expression(ptr, bstart, loc);
   15842                 :        8808 :       Btype* ubtype = Type::lookup_integer_type("uint8")->get_backend(gogo);
   15843                 :        8808 :       return gogo->backend()->indirect_expression(ubtype, ptr, false, loc);
   15844                 :             :     }
   15845                 :             : 
   15846                 :       11420 :   Expression* end = NULL;
   15847                 :       11420 :   if (this->end_->is_nil_expression())
   15848                 :             :     end = length;
   15849                 :             :   else
   15850                 :             :     {
   15851                 :        5934 :       go_assert(this->end_->is_multi_eval_safe());
   15852                 :        5934 :       end = Expression::make_cast(int_type, this->end_, loc);
   15853                 :             :     }
   15854                 :             : 
   15855                 :       11420 :   end = end->copy();
   15856                 :       11420 :   Bexpression* bend = end->get_backend(context);
   15857                 :       11420 :   Bexpression* new_length =
   15858                 :       11420 :     gogo->backend()->binary_expression(OPERATOR_MINUS, bend, bstart, loc);
   15859                 :             : 
   15860                 :             :   // If the new length is zero, don't change pointer.  Otherwise we can
   15861                 :             :   // get a pointer to the next object in memory, keeping it live
   15862                 :             :   // unnecessarily.  When the length is zero, the actual pointer
   15863                 :             :   // value doesn't matter.
   15864                 :       11420 :   Btype* int_btype = int_type->get_backend(gogo);
   15865                 :       11420 :   Bexpression* zero =
   15866                 :       11420 :     Expression::make_integer_ul(0, int_type, loc)->get_backend(context);
   15867                 :       11420 :   Bexpression* cond =
   15868                 :       11420 :     gogo->backend()->binary_expression(OPERATOR_EQEQ, new_length, zero,
   15869                 :             :                                        loc);
   15870                 :       11420 :   Bexpression* offset =
   15871                 :       11420 :     gogo->backend()->conditional_expression(bfn, int_btype, cond, zero,
   15872                 :             :                                             bstart, loc);
   15873                 :             : 
   15874                 :       11420 :   ptr = gogo->backend()->pointer_offset_expression(ptr, offset, loc);
   15875                 :             : 
   15876                 :       11420 :   Btype* str_btype = this->type()->get_backend(gogo);
   15877                 :       11420 :   std::vector<Bexpression*> init;
   15878                 :       11420 :   init.push_back(ptr);
   15879                 :       11420 :   init.push_back(new_length);
   15880                 :       11420 :   return gogo->backend()->constructor_expression(str_btype, init, loc);
   15881                 :       11420 : }
   15882                 :             : 
   15883                 :             : // Export a string index expression.
   15884                 :             : 
   15885                 :             : void
   15886                 :         944 : String_index_expression::do_export(Export_function_body* efb) const
   15887                 :             : {
   15888                 :         944 :   efb->write_c_string("(");
   15889                 :         944 :   this->string_->export_expression(efb);
   15890                 :         944 :   efb->write_c_string(")[");
   15891                 :             : 
   15892                 :         944 :   Type* old_context = efb->type_context();
   15893                 :         944 :   efb->set_type_context(Type::lookup_integer_type("int"));
   15894                 :             : 
   15895                 :         944 :   this->start_->export_expression(efb);
   15896                 :         944 :   if (this->end_ != NULL)
   15897                 :             :     {
   15898                 :         550 :       efb->write_c_string(":");
   15899                 :         550 :       if (!this->end_->is_nil_expression())
   15900                 :         340 :         this->end_->export_expression(efb);
   15901                 :             :     }
   15902                 :             : 
   15903                 :         944 :   efb->set_type_context(old_context);
   15904                 :             : 
   15905                 :         944 :   efb->write_c_string("]");
   15906                 :         944 : }
   15907                 :             : 
   15908                 :             : // Dump ast representation for a string index expression.
   15909                 :             : 
   15910                 :             : void
   15911                 :           0 : String_index_expression::do_dump_expression(Ast_dump_context* ast_dump_context)
   15912                 :             :     const
   15913                 :             : {
   15914                 :           0 :   Index_expression::dump_index_expression(ast_dump_context, this->string_,
   15915                 :           0 :                                           this->start_, this->end_, NULL);
   15916                 :           0 : }
   15917                 :             : 
   15918                 :             : // Make a string index expression.  END may be NULL.
   15919                 :             : 
   15920                 :             : Expression*
   15921                 :       20293 : Expression::make_string_index(Expression* string, Expression* start,
   15922                 :             :                               Expression* end, Location location)
   15923                 :             : {
   15924                 :       20293 :   return new String_index_expression(string, start, end, location);
   15925                 :             : }
   15926                 :             : 
   15927                 :             : // Class Map_index.
   15928                 :             : 
   15929                 :             : // Get the type of the map.
   15930                 :             : 
   15931                 :             : Map_type*
   15932                 :      107930 : Map_index_expression::get_map_type() const
   15933                 :             : {
   15934                 :      107930 :   Map_type* mt = this->map_->type()->map_type();
   15935                 :           0 :   if (mt == NULL)
   15936                 :           0 :     go_assert(saw_errors());
   15937                 :      107930 :   return mt;
   15938                 :             : }
   15939                 :             : 
   15940                 :             : // Map index traversal.
   15941                 :             : 
   15942                 :             : int
   15943                 :      115905 : Map_index_expression::do_traverse(Traverse* traverse)
   15944                 :             : {
   15945                 :      115905 :   if (Expression::traverse(&this->map_, traverse) == TRAVERSE_EXIT)
   15946                 :             :     return TRAVERSE_EXIT;
   15947                 :      105985 :   return Expression::traverse(&this->index_, traverse);
   15948                 :             : }
   15949                 :             : 
   15950                 :             : void
   15951                 :        2555 : Map_index_expression::do_determine_type(Gogo* gogo, const Type_context*)
   15952                 :             : {
   15953                 :        2555 :   this->map_->determine_type_no_context(gogo);
   15954                 :        2555 :   Map_type* mt = this->map_->type()->map_type();
   15955                 :           0 :   go_assert(mt != NULL);
   15956                 :        2555 :   Type_context index_context(mt->key_type(), false);
   15957                 :        2555 :   this->index_->determine_type(gogo, &index_context);
   15958                 :        2555 :   if (this->value_pointer_ != NULL)
   15959                 :         686 :     this->value_pointer_->determine_type_no_context(gogo);
   15960                 :        2555 : }
   15961                 :             : 
   15962                 :             : void
   15963                 :          15 : Map_index_expression::do_check_types(Gogo*)
   15964                 :             : {
   15965                 :             :   // Types should have been checked before this was created, so this
   15966                 :             :   // will probably never be called.  Check it anyhow to be sure.
   15967                 :          15 :   Map_type* mt = this->map_->type()->map_type();
   15968                 :           0 :   go_assert(mt != NULL);
   15969                 :          15 :   if (!Type::are_assignable(mt->key_type(), this->index_->type(), NULL))
   15970                 :           0 :     this->report_error(_("incompatible type for map index"));
   15971                 :          15 : }
   15972                 :             : 
   15973                 :             : // We need to pass in a pointer to the key, so flatten the index into a
   15974                 :             : // temporary variable if it isn't already.  The value pointer will be
   15975                 :             : // dereferenced and checked for nil, so flatten into a temporary to avoid
   15976                 :             : // recomputation.
   15977                 :             : 
   15978                 :             : Expression*
   15979                 :        6260 : Map_index_expression::do_flatten(Gogo* gogo, Named_object*,
   15980                 :             :                                  Statement_inserter* inserter)
   15981                 :             : {
   15982                 :        6260 :   Location loc = this->location();
   15983                 :        6260 :   Map_type* mt = this->get_map_type();
   15984                 :        6260 :   if (this->index()->is_error_expression()
   15985                 :        6260 :       || this->index()->type()->is_error_type()
   15986                 :       12520 :       || mt->is_error_type())
   15987                 :             :     {
   15988                 :           0 :       go_assert(saw_errors());
   15989                 :           0 :       return Expression::make_error(loc);
   15990                 :             :     }
   15991                 :             : 
   15992                 :             :   // Avoid copy for string([]byte) conversions used in map keys.
   15993                 :             :   // mapaccess doesn't keep the reference, so this is safe.
   15994                 :        6260 :   Type_conversion_expression* ce = this->index_->conversion_expression();
   15995                 :         209 :   if (ce != NULL && ce->type()->is_string_type()
   15996                 :          68 :       && ce->expr()->type()->is_slice_type())
   15997                 :          52 :     ce->set_no_copy(true);
   15998                 :             : 
   15999                 :        6260 :   if (!Type::are_identical(mt->key_type(), this->index_->type(),
   16000                 :             :                            Type::COMPARE_ERRORS | Type::COMPARE_TAGS,
   16001                 :             :                            NULL))
   16002                 :             :     {
   16003                 :           6 :       if (this->index_->type()->interface_type() != NULL
   16004                 :           0 :           && !this->index_->is_multi_eval_safe())
   16005                 :             :         {
   16006                 :           0 :           Temporary_statement* temp =
   16007                 :           0 :             Statement::make_temporary(NULL, this->index_, loc);
   16008                 :           0 :           inserter->insert(temp);
   16009                 :           0 :           this->index_ = Expression::make_temporary_reference(temp, loc);
   16010                 :             :         }
   16011                 :           6 :       this->index_ = Expression::convert_for_assignment(gogo, mt->key_type(),
   16012                 :             :                                                         this->index_, loc);
   16013                 :             :     }
   16014                 :             : 
   16015                 :        6260 :   if (!this->index_->is_multi_eval_safe())
   16016                 :             :     {
   16017                 :        2178 :       Temporary_statement* temp = Statement::make_temporary(NULL, this->index_,
   16018                 :             :                                                             loc);
   16019                 :        2178 :       inserter->insert(temp);
   16020                 :        2178 :       this->index_ = Expression::make_temporary_reference(temp, loc);
   16021                 :             :     }
   16022                 :             : 
   16023                 :        6260 :   if (this->value_pointer_ == NULL)
   16024                 :        5862 :     this->get_value_pointer(gogo);
   16025                 :        6260 :   if (this->value_pointer_->is_error_expression()
   16026                 :        6260 :       || this->value_pointer_->type()->is_error_type())
   16027                 :           0 :     return Expression::make_error(loc);
   16028                 :        6260 :   if (!this->value_pointer_->is_multi_eval_safe())
   16029                 :             :     {
   16030                 :        5862 :       Temporary_statement* temp =
   16031                 :        5862 :         Statement::make_temporary(NULL, this->value_pointer_, loc);
   16032                 :        5862 :       inserter->insert(temp);
   16033                 :        5862 :       this->value_pointer_ = Expression::make_temporary_reference(temp, loc);
   16034                 :             :     }
   16035                 :             : 
   16036                 :        6260 :   return this;
   16037                 :             : }
   16038                 :             : 
   16039                 :             : // Return the type of a map index.
   16040                 :             : 
   16041                 :             : Type*
   16042                 :       81619 : Map_index_expression::do_type()
   16043                 :             : {
   16044                 :       81619 :   Map_type* mt = this->get_map_type();
   16045                 :       81619 :   if (mt == NULL)
   16046                 :           0 :     return Type::make_error_type();
   16047                 :       81619 :   return mt->val_type();
   16048                 :             : }
   16049                 :             : 
   16050                 :             : // Add explicit type conversions.
   16051                 :             : 
   16052                 :             : void
   16053                 :        5862 : Map_index_expression::do_add_conversions()
   16054                 :             : {
   16055                 :        5862 :   Map_type* mt = this->get_map_type();
   16056                 :        5862 :   if (mt == NULL)
   16057                 :             :     return;
   16058                 :        5862 :   Type* lt = mt->key_type();
   16059                 :        5862 :   Type* rt = this->index_->type();
   16060                 :        5862 :   if (!Type::are_identical(lt, rt, 0, NULL)
   16061                 :        5862 :       && lt->interface_type() != NULL)
   16062                 :         123 :     this->index_ = Expression::make_cast(lt, this->index_, this->location());
   16063                 :             : }
   16064                 :             : 
   16065                 :             : // Get the backend representation for a map index.
   16066                 :             : 
   16067                 :             : Bexpression*
   16068                 :        5866 : Map_index_expression::do_get_backend(Translate_context* context)
   16069                 :             : {
   16070                 :        5866 :   Map_type* type = this->get_map_type();
   16071                 :        5866 :   if (type == NULL)
   16072                 :             :     {
   16073                 :           0 :       go_assert(saw_errors());
   16074                 :           0 :       return context->backend()->error_expression();
   16075                 :             :     }
   16076                 :             : 
   16077                 :        5866 :   go_assert(this->value_pointer_ != NULL
   16078                 :             :             && this->value_pointer_->is_multi_eval_safe());
   16079                 :             : 
   16080                 :        5866 :   Expression* val = Expression::make_dereference(this->value_pointer_,
   16081                 :             :                                                  NIL_CHECK_NOT_NEEDED,
   16082                 :             :                                                  this->location());
   16083                 :        5866 :   return val->get_backend(context);
   16084                 :             : }
   16085                 :             : 
   16086                 :             : // Get an expression for the map index.  This returns an expression
   16087                 :             : // that evaluates to a pointer to a value.  If the key is not in the
   16088                 :             : // map, the pointer will point to a zero value.
   16089                 :             : 
   16090                 :             : Expression*
   16091                 :        5862 : Map_index_expression::get_value_pointer(Gogo* gogo)
   16092                 :             : {
   16093                 :        5862 :   if (this->value_pointer_ == NULL)
   16094                 :             :     {
   16095                 :        5862 :       Map_type* type = this->get_map_type();
   16096                 :        5862 :       if (type == NULL)
   16097                 :             :         {
   16098                 :           0 :           go_assert(saw_errors());
   16099                 :           0 :           return Expression::make_error(this->location());
   16100                 :             :         }
   16101                 :             : 
   16102                 :        5862 :       Location loc = this->location();
   16103                 :        5862 :       Expression* map_ref = this->map_;
   16104                 :             : 
   16105                 :        5862 :       Expression* index_ptr = Expression::make_unary(OPERATOR_AND,
   16106                 :             :                                                      this->index_,
   16107                 :             :                                                      loc);
   16108                 :             : 
   16109                 :        5862 :       Expression* type_expr = Expression::make_type_descriptor(type, loc);
   16110                 :        5862 :       Expression* zero = type->fat_zero_value(gogo);
   16111                 :        5862 :       Expression* map_index;
   16112                 :        5862 :       if (zero == NULL)
   16113                 :             :         {
   16114                 :        5859 :           Runtime::Function code;
   16115                 :        5859 :           Expression* key;
   16116                 :        5859 :           switch (type->algorithm(gogo))
   16117                 :             :             {
   16118                 :         614 :               case Map_type::MAP_ALG_FAST32:
   16119                 :         614 :               case Map_type::MAP_ALG_FAST32PTR:
   16120                 :         614 :                 {
   16121                 :         614 :                   Type* uint32_type = Type::lookup_integer_type("uint32");
   16122                 :         614 :                   Type* uint32_ptr_type = Type::make_pointer_type(uint32_type);
   16123                 :         614 :                   key = Expression::make_unsafe_cast(uint32_ptr_type, index_ptr,
   16124                 :             :                                                      loc);
   16125                 :         614 :                   key = Expression::make_dereference(key, NIL_CHECK_NOT_NEEDED,
   16126                 :             :                                                      loc);
   16127                 :         614 :                   code = Runtime::MAPACCESS1_FAST32;
   16128                 :         614 :                   break;
   16129                 :             :                 }
   16130                 :        1169 :               case Map_type::MAP_ALG_FAST64:
   16131                 :        1169 :               case Map_type::MAP_ALG_FAST64PTR:
   16132                 :        1169 :                 {
   16133                 :        1169 :                   Type* uint64_type = Type::lookup_integer_type("uint64");
   16134                 :        1169 :                   Type* uint64_ptr_type = Type::make_pointer_type(uint64_type);
   16135                 :        1169 :                   key = Expression::make_unsafe_cast(uint64_ptr_type, index_ptr,
   16136                 :             :                                                      loc);
   16137                 :        1169 :                   key = Expression::make_dereference(key, NIL_CHECK_NOT_NEEDED,
   16138                 :             :                                                      loc);
   16139                 :        1169 :                   code = Runtime::MAPACCESS1_FAST64;
   16140                 :        1169 :                   break;
   16141                 :             :                 }
   16142                 :        2897 :               case Map_type::MAP_ALG_FASTSTR:
   16143                 :        2897 :                 key = this->index_;
   16144                 :        2897 :                 code = Runtime::MAPACCESS1_FASTSTR;
   16145                 :        2897 :                 break;
   16146                 :             :               default:
   16147                 :             :                 key = index_ptr;
   16148                 :             :                 code = Runtime::MAPACCESS1;
   16149                 :             :                 break;
   16150                 :             :             }
   16151                 :        5859 :           map_index = Runtime::make_call(gogo, code, loc, 3,
   16152                 :             :                                          type_expr, map_ref, key);
   16153                 :             :         }
   16154                 :             :       else
   16155                 :           3 :         map_index = Runtime::make_call(gogo, Runtime::MAPACCESS1_FAT, loc, 4,
   16156                 :             :                                        type_expr, map_ref, index_ptr, zero);
   16157                 :             : 
   16158                 :        5862 :       Type* val_type = type->val_type();
   16159                 :       11724 :       this->value_pointer_ =
   16160                 :        5862 :           Expression::make_unsafe_cast(Type::make_pointer_type(val_type),
   16161                 :             :                                        map_index, this->location());
   16162                 :        5862 :       this->value_pointer_->determine_type_no_context(gogo);
   16163                 :             :     }
   16164                 :             : 
   16165                 :        5862 :   return this->value_pointer_;
   16166                 :             : }
   16167                 :             : 
   16168                 :             : // Export a map index expression.
   16169                 :             : 
   16170                 :             : void
   16171                 :          96 : Map_index_expression::do_export(Export_function_body* efb) const
   16172                 :             : {
   16173                 :          96 :   efb->write_c_string("(");
   16174                 :          96 :   this->map_->export_expression(efb);
   16175                 :          96 :   efb->write_c_string(")[");
   16176                 :             : 
   16177                 :          96 :   Type* old_context = efb->type_context();
   16178                 :          96 :   efb->set_type_context(this->get_map_type()->key_type());
   16179                 :             : 
   16180                 :          96 :   this->index_->export_expression(efb);
   16181                 :             : 
   16182                 :          96 :   efb->set_type_context(old_context);
   16183                 :             : 
   16184                 :          96 :   efb->write_c_string("]");
   16185                 :          96 : }
   16186                 :             : 
   16187                 :             : // Dump ast representation for a map index expression
   16188                 :             : 
   16189                 :             : void
   16190                 :           0 : Map_index_expression::do_dump_expression(Ast_dump_context* ast_dump_context)
   16191                 :             :     const
   16192                 :             : {
   16193                 :           0 :   Index_expression::dump_index_expression(ast_dump_context, this->map_,
   16194                 :           0 :                                           this->index_, NULL, NULL);
   16195                 :           0 : }
   16196                 :             : 
   16197                 :             : // Make a map index expression.
   16198                 :             : 
   16199                 :             : Map_index_expression*
   16200                 :       14200 : Expression::make_map_index(Expression* map, Expression* index,
   16201                 :             :                            Location location)
   16202                 :             : {
   16203                 :       14200 :   return new Map_index_expression(map, index, location);
   16204                 :             : }
   16205                 :             : 
   16206                 :             : // Class Field_reference_expression.
   16207                 :             : 
   16208                 :             : // Lower a field reference expression.  There is nothing to lower, but
   16209                 :             : // this is where we generate the tracking information for fields with
   16210                 :             : // the magic go:"track" tag.
   16211                 :             : 
   16212                 :             : Expression*
   16213                 :     1328287 : Field_reference_expression::do_lower(Gogo* gogo, Named_object* function,
   16214                 :             :                                      Statement_inserter* inserter)
   16215                 :             : {
   16216                 :     1328287 :   Struct_type* struct_type = this->expr_->type()->struct_type();
   16217                 :     1328287 :   if (struct_type == NULL)
   16218                 :             :     {
   16219                 :             :       // Error will be reported elsewhere.
   16220                 :           2 :       return this;
   16221                 :             :     }
   16222                 :     1328285 :   const Struct_field* field = struct_type->field(this->field_index_);
   16223                 :     1328285 :   if (field == NULL)
   16224                 :           0 :     return this;
   16225                 :     1328285 :   if (!field->has_tag())
   16226                 :     1309329 :     return this;
   16227                 :       18956 :   if (field->tag().find("go:\"track\"") == std::string::npos)
   16228                 :       18956 :     return this;
   16229                 :             : 
   16230                 :             :   // References from functions generated by the compiler don't count.
   16231                 :           0 :   if (function != NULL && function->func_value()->is_type_specific_function())
   16232                 :           0 :     return this;
   16233                 :             : 
   16234                 :             :   // We have found a reference to a tracked field.  Build a call to
   16235                 :             :   // the runtime function __go_fieldtrack with a string that describes
   16236                 :             :   // the field.  FIXME: We should only call this once per referenced
   16237                 :             :   // field per function, not once for each reference to the field.
   16238                 :             : 
   16239                 :           0 :   if (this->called_fieldtrack_)
   16240                 :           0 :     return this;
   16241                 :           0 :   this->called_fieldtrack_ = true;
   16242                 :             : 
   16243                 :           0 :   Location loc = this->location();
   16244                 :             : 
   16245                 :           0 :   std::string s = "fieldtrack \"";
   16246                 :           0 :   Named_type* nt = this->expr_->type()->unalias()->named_type();
   16247                 :           0 :   if (nt == NULL || nt->named_object()->package() == NULL)
   16248                 :           0 :     s.append(gogo->pkgpath());
   16249                 :             :   else
   16250                 :           0 :     s.append(nt->named_object()->package()->pkgpath());
   16251                 :           0 :   s.push_back('.');
   16252                 :           0 :   if (nt != NULL)
   16253                 :           0 :     s.append(Gogo::unpack_hidden_name(nt->name()));
   16254                 :           0 :   s.push_back('.');
   16255                 :           0 :   s.append(Gogo::unpack_hidden_name(field->field_name()));
   16256                 :           0 :   s.push_back('"');
   16257                 :             : 
   16258                 :             :   // We can't use a string here, because internally a string holds a
   16259                 :             :   // pointer to the actual bytes; when the linker garbage collects the
   16260                 :             :   // string, it won't garbage collect the bytes.  So we use a
   16261                 :             :   // [...]byte.
   16262                 :             : 
   16263                 :           0 :   Expression* length_expr = Expression::make_integer_ul(s.length(), NULL, loc);
   16264                 :             : 
   16265                 :           0 :   Type* byte_type = Type::lookup_integer_type("byte");
   16266                 :           0 :   Array_type* array_type = Type::make_array_type(byte_type, length_expr);
   16267                 :           0 :   array_type->set_is_array_incomparable();
   16268                 :             : 
   16269                 :           0 :   Expression_list* bytes = new Expression_list();
   16270                 :           0 :   for (std::string::const_iterator p = s.begin(); p != s.end(); p++)
   16271                 :             :     {
   16272                 :           0 :       unsigned char c = static_cast<unsigned char>(*p);
   16273                 :           0 :       bytes->push_back(Expression::make_integer_ul(c, NULL, loc));
   16274                 :             :     }
   16275                 :             : 
   16276                 :           0 :   Expression* e = Expression::make_composite_literal(array_type, 0, false,
   16277                 :             :                                                      bytes, false, loc);
   16278                 :             : 
   16279                 :           0 :   Variable* var = new Variable(array_type, e, true, false, false, loc);
   16280                 :             : 
   16281                 :           0 :   static int count;
   16282                 :           0 :   char buf[50];
   16283                 :           0 :   snprintf(buf, sizeof buf, "fieldtrack.%d", count);
   16284                 :           0 :   ++count;
   16285                 :             : 
   16286                 :           0 :   Named_object* no = gogo->add_variable(buf, var);
   16287                 :           0 :   e = Expression::make_var_reference(no, loc);
   16288                 :           0 :   e = Expression::make_unary(OPERATOR_AND, e, loc);
   16289                 :             : 
   16290                 :           0 :   Expression* call = Runtime::make_call(gogo, Runtime::FIELDTRACK, loc, 1, e);
   16291                 :           0 :   call->determine_type_no_context(gogo);
   16292                 :           0 :   gogo->lower_expression(function, inserter, &call);
   16293                 :           0 :   inserter->insert(Statement::make_statement(call, false));
   16294                 :             : 
   16295                 :             :   // Put this function, and the global variable we just created, into
   16296                 :             :   // unique sections.  This will permit the linker to garbage collect
   16297                 :             :   // them if they are not referenced.  The effect is that the only
   16298                 :             :   // strings, indicating field references, that will wind up in the
   16299                 :             :   // executable will be those for functions that are actually needed.
   16300                 :           0 :   if (function != NULL)
   16301                 :           0 :     function->func_value()->set_in_unique_section();
   16302                 :           0 :   var->set_in_unique_section();
   16303                 :             : 
   16304                 :           0 :   return this;
   16305                 :           0 : }
   16306                 :             : 
   16307                 :             : // Return the type of a field reference.
   16308                 :             : 
   16309                 :             : Type*
   16310                 :    12699757 : Field_reference_expression::do_type()
   16311                 :             : {
   16312                 :    12699757 :   Type* type = this->expr_->type();
   16313                 :    12699757 :   if (type->is_error())
   16314                 :             :     return type;
   16315                 :    12699737 :   Struct_type* struct_type = type->struct_type();
   16316                 :           0 :   go_assert(struct_type != NULL);
   16317                 :    12699737 :   return struct_type->field(this->field_index_)->type();
   16318                 :             : }
   16319                 :             : 
   16320                 :             : // Check the types for a field reference.
   16321                 :             : 
   16322                 :             : void
   16323                 :       43948 : Field_reference_expression::do_check_types(Gogo*)
   16324                 :             : {
   16325                 :       43948 :   Type* type = this->expr_->type();
   16326                 :       43948 :   if (type->is_error())
   16327                 :             :     return;
   16328                 :       43947 :   Struct_type* struct_type = type->struct_type();
   16329                 :           0 :   go_assert(struct_type != NULL);
   16330                 :       43947 :   go_assert(struct_type->field(this->field_index_) != NULL);
   16331                 :             : }
   16332                 :             : 
   16333                 :             : // Get the backend representation for a field reference.
   16334                 :             : 
   16335                 :             : Bexpression*
   16336                 :      981722 : Field_reference_expression::do_get_backend(Translate_context* context)
   16337                 :             : {
   16338                 :      981722 :   Bexpression* bstruct = this->expr_->get_backend(context);
   16339                 :      981722 :   return context->gogo()->backend()->struct_field_expression(bstruct,
   16340                 :      981722 :                                                              this->field_index_,
   16341                 :      981722 :                                                              this->location());
   16342                 :             : }
   16343                 :             : 
   16344                 :             : // Dump ast representation for a field reference expression.
   16345                 :             : 
   16346                 :             : void
   16347                 :           0 : Field_reference_expression::do_dump_expression(
   16348                 :             :     Ast_dump_context* ast_dump_context) const
   16349                 :             : {
   16350                 :           0 :   this->expr_->dump_expression(ast_dump_context);
   16351                 :           0 :   ast_dump_context->ostream() << "." <<  this->field_index_;
   16352                 :           0 : }
   16353                 :             : 
   16354                 :             : // Make a reference to a qualified identifier in an expression.
   16355                 :             : 
   16356                 :             : Field_reference_expression*
   16357                 :      986772 : Expression::make_field_reference(Expression* expr, unsigned int field_index,
   16358                 :             :                                  Location location)
   16359                 :             : {
   16360                 :      986772 :   return new Field_reference_expression(expr, field_index, location);
   16361                 :             : }
   16362                 :             : 
   16363                 :             : // Class Interface_field_reference_expression.
   16364                 :             : 
   16365                 :             : // Return an expression for the pointer to the function to call.
   16366                 :             : 
   16367                 :             : Expression*
   16368                 :       31774 : Interface_field_reference_expression::get_function()
   16369                 :             : {
   16370                 :       31774 :   Expression* ref = this->expr_;
   16371                 :       31774 :   Location loc = this->location();
   16372                 :       31774 :   if (ref->type()->points_to() != NULL)
   16373                 :        1160 :     ref = Expression::make_dereference(ref, NIL_CHECK_DEFAULT, loc);
   16374                 :             : 
   16375                 :       31774 :   Expression* mtable =
   16376                 :       31774 :       Expression::make_interface_info(ref, INTERFACE_INFO_METHODS, loc);
   16377                 :       31774 :   Struct_type* mtable_type = mtable->type()->points_to()->struct_type();
   16378                 :             : 
   16379                 :       31774 :   std::string name = Gogo::unpack_hidden_name(this->name_);
   16380                 :       31774 :   unsigned int index;
   16381                 :       31774 :   const Struct_field* field = mtable_type->find_local_field(name, &index);
   16382                 :       31774 :   go_assert(field != NULL);
   16383                 :             : 
   16384                 :       31774 :   mtable = Expression::make_dereference(mtable, NIL_CHECK_NOT_NEEDED, loc);
   16385                 :       31774 :   return Expression::make_field_reference(mtable, index, loc);
   16386                 :       31774 : }
   16387                 :             : 
   16388                 :             : // Return an expression for the first argument to pass to the interface
   16389                 :             : // function.
   16390                 :             : 
   16391                 :             : Expression*
   16392                 :       31774 : Interface_field_reference_expression::get_underlying_object()
   16393                 :             : {
   16394                 :       31774 :   Expression* expr = this->expr_;
   16395                 :       31774 :   if (expr->type()->points_to() != NULL)
   16396                 :        1160 :     expr = Expression::make_dereference(expr, NIL_CHECK_DEFAULT,
   16397                 :             :                                         this->location());
   16398                 :       31774 :   return Expression::make_interface_info(expr, INTERFACE_INFO_OBJECT,
   16399                 :       31774 :                                          this->location());
   16400                 :             : }
   16401                 :             : 
   16402                 :             : // Traversal.
   16403                 :             : 
   16404                 :             : int
   16405                 :      495980 : Interface_field_reference_expression::do_traverse(Traverse* traverse)
   16406                 :             : {
   16407                 :      495980 :   return Expression::traverse(&this->expr_, traverse);
   16408                 :             : }
   16409                 :             : 
   16410                 :             : // Lower the expression.  If this expression is not called, we need to
   16411                 :             : // evaluate the expression twice when converting to the backend
   16412                 :             : // interface.  So introduce a temporary variable if necessary.
   16413                 :             : 
   16414                 :             : Expression*
   16415                 :       39774 : Interface_field_reference_expression::do_flatten(Gogo*, Named_object*,
   16416                 :             :                                                  Statement_inserter* inserter)
   16417                 :             : {
   16418                 :       39774 :   if (this->expr_->is_error_expression()
   16419                 :       39774 :       || this->expr_->type()->is_error_type())
   16420                 :             :     {
   16421                 :           1 :       go_assert(saw_errors());
   16422                 :           1 :       return Expression::make_error(this->location());
   16423                 :             :     }
   16424                 :             : 
   16425                 :       39773 :   if (!this->expr_->is_multi_eval_safe())
   16426                 :             :     {
   16427                 :       10225 :       Temporary_statement* temp =
   16428                 :       10225 :         Statement::make_temporary(NULL, this->expr_, this->location());
   16429                 :       10225 :       inserter->insert(temp);
   16430                 :       10225 :       this->expr_ = Expression::make_temporary_reference(temp, this->location());
   16431                 :             :     }
   16432                 :       39773 :   return this;
   16433                 :             : }
   16434                 :             : 
   16435                 :             : // Return the type of an interface field reference.
   16436                 :             : 
   16437                 :             : Type*
   16438                 :     1455651 : Interface_field_reference_expression::do_type()
   16439                 :             : {
   16440                 :     1455651 :   Type* expr_type = this->expr_->type();
   16441                 :             : 
   16442                 :     1455651 :   Type* points_to = expr_type->points_to();
   16443                 :     1455651 :   if (points_to != NULL)
   16444                 :       66180 :     expr_type = points_to;
   16445                 :             : 
   16446                 :     1455651 :   Interface_type* interface_type = expr_type->interface_type();
   16447                 :     1455651 :   if (interface_type == NULL)
   16448                 :          16 :     return Type::make_error_type();
   16449                 :             : 
   16450                 :     1455635 :   const Typed_identifier* method = interface_type->find_method(this->name_);
   16451                 :     1455635 :   if (method == NULL)
   16452                 :           0 :     return Type::make_error_type();
   16453                 :             : 
   16454                 :     1455635 :   return method->type();
   16455                 :             : }
   16456                 :             : 
   16457                 :             : // Determine types.
   16458                 :             : 
   16459                 :             : void
   16460                 :       33019 : Interface_field_reference_expression::do_determine_type(Gogo* gogo,
   16461                 :             :                                                         const Type_context*)
   16462                 :             : {
   16463                 :       33019 :   this->expr_->determine_type_no_context(gogo);
   16464                 :       33019 : }
   16465                 :             : 
   16466                 :             : // Check the types for an interface field reference.
   16467                 :             : 
   16468                 :             : void
   16469                 :        1292 : Interface_field_reference_expression::do_check_types(Gogo*)
   16470                 :             : {
   16471                 :        1292 :   Type* type = this->expr_->type();
   16472                 :             : 
   16473                 :        1292 :   Type* points_to = type->points_to();
   16474                 :        1292 :   if (points_to != NULL)
   16475                 :        1160 :     type = points_to;
   16476                 :             : 
   16477                 :        1292 :   Interface_type* interface_type = type->interface_type();
   16478                 :        1292 :   if (interface_type == NULL)
   16479                 :             :     {
   16480                 :           1 :       if (!type->is_error_type())
   16481                 :           0 :         this->report_error(_("expected interface or pointer to interface"));
   16482                 :             :     }
   16483                 :             :   else
   16484                 :             :     {
   16485                 :        1291 :       const Typed_identifier* method =
   16486                 :        1291 :         interface_type->find_method(this->name_);
   16487                 :        1291 :       if (method == NULL)
   16488                 :             :         {
   16489                 :           0 :           go_error_at(this->location(), "method %qs not in interface",
   16490                 :           0 :                       Gogo::message_name(this->name_).c_str());
   16491                 :           0 :           this->set_is_error();
   16492                 :             :         }
   16493                 :             :     }
   16494                 :        1292 : }
   16495                 :             : 
   16496                 :             : // If an interface field reference is not simply called, then it is
   16497                 :             : // represented as a closure.  The closure will hold a single variable,
   16498                 :             : // the value of the interface on which the method should be called.
   16499                 :             : // The function will be a simple thunk that pulls the value from the
   16500                 :             : // closure and calls the method with the remaining arguments.
   16501                 :             : 
   16502                 :             : // Because method values are not common, we don't build all thunks for
   16503                 :             : // all possible interface methods, but instead only build them as we
   16504                 :             : // need them.  In particular, we even build them on demand for
   16505                 :             : // interface methods defined in other packages.
   16506                 :             : 
   16507                 :             : Interface_field_reference_expression::Interface_method_thunks
   16508                 :             :   Interface_field_reference_expression::interface_method_thunks;
   16509                 :             : 
   16510                 :             : // Find or create the thunk to call method NAME on TYPE.
   16511                 :             : 
   16512                 :             : Named_object*
   16513                 :         105 : Interface_field_reference_expression::create_thunk(Gogo* gogo,
   16514                 :             :                                                    Interface_type* type,
   16515                 :             :                                                    const std::string& name)
   16516                 :             : {
   16517                 :         105 :   std::pair<Interface_type*, Method_thunks*> val(type, NULL);
   16518                 :         105 :   std::pair<Interface_method_thunks::iterator, bool> ins =
   16519                 :         105 :     Interface_field_reference_expression::interface_method_thunks.insert(val);
   16520                 :         105 :   if (ins.second)
   16521                 :             :     {
   16522                 :             :       // This is the first time we have seen this interface.
   16523                 :          52 :       ins.first->second = new Method_thunks();
   16524                 :             :     }
   16525                 :             : 
   16526                 :         114 :   for (Method_thunks::const_iterator p = ins.first->second->begin();
   16527                 :         114 :        p != ins.first->second->end();
   16528                 :           9 :        p++)
   16529                 :          53 :     if (p->first == name)
   16530                 :          44 :       return p->second;
   16531                 :             : 
   16532                 :          61 :   Location loc = type->location();
   16533                 :             : 
   16534                 :          61 :   const Typed_identifier* method_id = type->find_method(name);
   16535                 :          61 :   if (method_id == NULL)
   16536                 :           0 :     return Named_object::make_erroneous_name(gogo->thunk_name());
   16537                 :             : 
   16538                 :          61 :   Function_type* orig_fntype = method_id->type()->function_type();
   16539                 :          61 :   if (orig_fntype == NULL)
   16540                 :           0 :     return Named_object::make_erroneous_name(gogo->thunk_name());
   16541                 :             : 
   16542                 :          61 :   Struct_field_list* sfl = new Struct_field_list();
   16543                 :             :   // The type here is wrong--it should be the C function type.  But it
   16544                 :             :   // doesn't really matter.
   16545                 :          61 :   Type* vt = Type::make_pointer_type(Type::make_void_type());
   16546                 :          61 :   sfl->push_back(Struct_field(Typed_identifier("fn", vt, loc)));
   16547                 :          61 :   sfl->push_back(Struct_field(Typed_identifier("val", type, loc)));
   16548                 :          61 :   Struct_type* st = Type::make_struct_type(sfl, loc);
   16549                 :          61 :   st->set_is_struct_incomparable();
   16550                 :          61 :   Type* closure_type = Type::make_pointer_type(st);
   16551                 :             : 
   16552                 :          61 :   Function_type* new_fntype = orig_fntype->copy_with_names();
   16553                 :             : 
   16554                 :          61 :   std::string thunk_name = gogo->thunk_name();
   16555                 :          61 :   Named_object* new_no = gogo->start_function(thunk_name, new_fntype,
   16556                 :          61 :                                               false, loc);
   16557                 :             : 
   16558                 :          61 :   Variable* cvar = new Variable(closure_type, NULL, false, false, false, loc);
   16559                 :          61 :   cvar->set_is_used();
   16560                 :          61 :   cvar->set_is_closure();
   16561                 :          61 :   Named_object* cp = Named_object::make_variable("$closure" + thunk_name,
   16562                 :             :                                                  NULL, cvar);
   16563                 :          61 :   new_no->func_value()->set_closure_var(cp);
   16564                 :             : 
   16565                 :          61 :   gogo->start_block(loc);
   16566                 :             : 
   16567                 :             :   // Field 0 of the closure is the function code pointer, field 1 is
   16568                 :             :   // the value on which to invoke the method.
   16569                 :          61 :   Expression* arg = Expression::make_var_reference(cp, loc);
   16570                 :          61 :   arg = Expression::make_dereference(arg, NIL_CHECK_NOT_NEEDED, loc);
   16571                 :          61 :   arg = Expression::make_field_reference(arg, 1, loc);
   16572                 :             : 
   16573                 :          61 :   Expression *ifre = Expression::make_interface_field_reference(arg, name,
   16574                 :             :                                                                 loc);
   16575                 :             : 
   16576                 :          61 :   const Typed_identifier_list* orig_params = orig_fntype->parameters();
   16577                 :          61 :   Expression_list* args;
   16578                 :          61 :   if (orig_params == NULL || orig_params->empty())
   16579                 :             :     args = NULL;
   16580                 :             :   else
   16581                 :             :     {
   16582                 :          38 :       const Typed_identifier_list* new_params = new_fntype->parameters();
   16583                 :          38 :       args = new Expression_list();
   16584                 :          38 :       for (Typed_identifier_list::const_iterator p = new_params->begin();
   16585                 :          92 :            p != new_params->end();
   16586                 :          54 :            ++p)
   16587                 :             :         {
   16588                 :          54 :           Named_object* p_no = gogo->lookup(p->name(), NULL);
   16589                 :          54 :           go_assert(p_no != NULL
   16590                 :             :                     && p_no->is_variable()
   16591                 :             :                     && p_no->var_value()->is_parameter());
   16592                 :          54 :           args->push_back(Expression::make_var_reference(p_no, loc));
   16593                 :             :         }
   16594                 :             :     }
   16595                 :             : 
   16596                 :          61 :   Call_expression* call = Expression::make_call(ifre, args,
   16597                 :          61 :                                                 orig_fntype->is_varargs(),
   16598                 :             :                                                 loc);
   16599                 :          61 :   call->set_varargs_are_lowered();
   16600                 :             : 
   16601                 :          61 :   Statement* s = Statement::make_return_from_call(new_no, call, loc);
   16602                 :          61 :   s->determine_types(gogo);
   16603                 :          61 :   gogo->add_statement(s);
   16604                 :          61 :   Block* b = gogo->finish_block(loc);
   16605                 :          61 :   gogo->add_block(b, loc);
   16606                 :             : 
   16607                 :             :   // This is called after lowering.
   16608                 :          61 :   gogo->lower_block(new_no, b);
   16609                 :             : 
   16610                 :          61 :   gogo->finish_function(loc);
   16611                 :             : 
   16612                 :          61 :   ins.first->second->push_back(std::make_pair(name, new_no));
   16613                 :          61 :   return new_no;
   16614                 :          61 : }
   16615                 :             : 
   16616                 :             : // Lookup a thunk to call method NAME on TYPE.
   16617                 :             : 
   16618                 :             : Named_object*
   16619                 :         105 : Interface_field_reference_expression::lookup_thunk(Interface_type* type,
   16620                 :             :                                                    const std::string& name)
   16621                 :             : {
   16622                 :         105 :   Interface_method_thunks::const_iterator p =
   16623                 :         105 :     Interface_field_reference_expression::interface_method_thunks.find(type);
   16624                 :         105 :   if (p == Interface_field_reference_expression::interface_method_thunks.end())
   16625                 :             :     return NULL;
   16626                 :         114 :   for (Method_thunks::const_iterator pm = p->second->begin();
   16627                 :         114 :        pm != p->second->end();
   16628                 :           9 :        ++pm)
   16629                 :         114 :     if (pm->first == name)
   16630                 :         105 :       return pm->second;
   16631                 :             :   return NULL;
   16632                 :             : }
   16633                 :             : 
   16634                 :             : // Get the backend representation for a method value.
   16635                 :             : 
   16636                 :             : Bexpression*
   16637                 :         105 : Interface_field_reference_expression::do_get_backend(Translate_context* context)
   16638                 :             : {
   16639                 :         105 :   Interface_type* type = this->expr_->type()->interface_type();
   16640                 :         105 :   if (type == NULL)
   16641                 :             :     {
   16642                 :           0 :       go_assert(saw_errors());
   16643                 :           0 :       return context->backend()->error_expression();
   16644                 :             :     }
   16645                 :             : 
   16646                 :         105 :   Named_object* thunk =
   16647                 :         105 :     Interface_field_reference_expression::lookup_thunk(type, this->name_);
   16648                 :             : 
   16649                 :             :   // The thunk should have been created during the
   16650                 :             :   // create_function_descriptors pass.
   16651                 :         105 :   if (thunk == NULL || thunk->is_erroneous())
   16652                 :             :     {
   16653                 :           0 :       go_assert(saw_errors());
   16654                 :           0 :       return context->backend()->error_expression();
   16655                 :             :     }
   16656                 :             : 
   16657                 :             :   // FIXME: We should lower this earlier, but we can't it lower it in
   16658                 :             :   // the lowering pass because at that point we don't know whether we
   16659                 :             :   // need to create the thunk or not.  If the expression is called, we
   16660                 :             :   // don't need the thunk.
   16661                 :             : 
   16662                 :         105 :   Location loc = this->location();
   16663                 :             : 
   16664                 :         105 :   Struct_field_list* fields = new Struct_field_list();
   16665                 :         105 :   fields->push_back(Struct_field(Typed_identifier("fn",
   16666                 :         105 :                                                   thunk->func_value()->type(),
   16667                 :             :                                                   loc)));
   16668                 :         105 :   fields->push_back(Struct_field(Typed_identifier("val",
   16669                 :         105 :                                                   this->expr_->type(),
   16670                 :             :                                                   loc)));
   16671                 :         105 :   Struct_type* st = Type::make_struct_type(fields, loc);
   16672                 :         105 :   st->set_is_struct_incomparable();
   16673                 :             : 
   16674                 :         105 :   Expression_list* vals = new Expression_list();
   16675                 :         105 :   vals->push_back(Expression::make_func_code_reference(thunk, loc));
   16676                 :         105 :   vals->push_back(this->expr_);
   16677                 :             : 
   16678                 :         105 :   Expression* expr = Expression::make_struct_composite_literal(st, vals, loc);
   16679                 :         105 :   Bexpression* bclosure =
   16680                 :         105 :     Expression::make_heap_expression(expr, loc)->get_backend(context);
   16681                 :             : 
   16682                 :         105 :   Gogo* gogo = context->gogo();
   16683                 :         105 :   Btype* btype = this->type()->get_backend(gogo);
   16684                 :         105 :   bclosure = gogo->backend()->convert_expression(btype, bclosure, loc);
   16685                 :             : 
   16686                 :         105 :   Expression* nil_check =
   16687                 :         105 :       Expression::make_binary(OPERATOR_EQEQ, this->expr_,
   16688                 :             :                               Expression::make_nil(loc), loc);
   16689                 :         105 :   Bexpression* bnil_check = nil_check->get_backend(context);
   16690                 :             : 
   16691                 :         105 :   Expression* crash = Runtime::make_call(gogo, Runtime::PANIC_MEM, loc, 0);
   16692                 :         105 :   crash->determine_type_no_context(gogo);
   16693                 :         105 :   Bexpression* bcrash = crash->get_backend(context);
   16694                 :             : 
   16695                 :         105 :   Bfunction* bfn = context->function()->func_value()->get_decl();
   16696                 :         105 :   Bexpression* bcond =
   16697                 :         105 :       gogo->backend()->conditional_expression(bfn, NULL,
   16698                 :             :                                               bnil_check, bcrash, NULL, loc);
   16699                 :         105 :   Bfunction* bfunction = context->function()->func_value()->get_decl();
   16700                 :         105 :   Bstatement* cond_statement =
   16701                 :         105 :       gogo->backend()->expression_statement(bfunction, bcond);
   16702                 :         105 :   return gogo->backend()->compound_expression(cond_statement, bclosure, loc);
   16703                 :             : }
   16704                 :             : 
   16705                 :             : // Dump ast representation for an interface field reference.
   16706                 :             : 
   16707                 :             : void
   16708                 :           0 : Interface_field_reference_expression::do_dump_expression(
   16709                 :             :     Ast_dump_context* ast_dump_context) const
   16710                 :             : {
   16711                 :           0 :   this->expr_->dump_expression(ast_dump_context);
   16712                 :           0 :   ast_dump_context->ostream() << "." << this->name_;
   16713                 :           0 : }
   16714                 :             : 
   16715                 :             : // Make a reference to a field in an interface.
   16716                 :             : 
   16717                 :             : Expression*
   16718                 :       32971 : Expression::make_interface_field_reference(Expression* expr,
   16719                 :             :                                            const std::string& field,
   16720                 :             :                                            Location location)
   16721                 :             : {
   16722                 :       32971 :   return new Interface_field_reference_expression(expr, field, location);
   16723                 :             : }
   16724                 :             : 
   16725                 :             : // Class Allocation_expression.
   16726                 :             : 
   16727                 :             : int
   16728                 :      132214 : Allocation_expression::do_traverse(Traverse* traverse)
   16729                 :             : {
   16730                 :      132214 :   return Type::traverse(this->type_, traverse);
   16731                 :             : }
   16732                 :             : 
   16733                 :             : Type*
   16734                 :      150456 : Allocation_expression::do_type()
   16735                 :             : {
   16736                 :      150456 :   return Type::make_pointer_type(this->type_);
   16737                 :             : }
   16738                 :             : 
   16739                 :             : void
   16740                 :        1911 : Allocation_expression::do_check_types(Gogo*)
   16741                 :             : {
   16742                 :        1911 :   if (!this->type_->in_heap())
   16743                 :           0 :     go_error_at(this->location(), "cannot heap allocate go:notinheap type");
   16744                 :        1911 : }
   16745                 :             : 
   16746                 :             : // Make a copy of an allocation expression.
   16747                 :             : 
   16748                 :             : Expression*
   16749                 :           0 : Allocation_expression::do_copy()
   16750                 :             : {
   16751                 :           0 :   Allocation_expression* alloc =
   16752                 :           0 :     new Allocation_expression(this->type_->copy_expressions(),
   16753                 :           0 :                               this->location());
   16754                 :           0 :   if (this->allocate_on_stack_)
   16755                 :           0 :     alloc->set_allocate_on_stack();
   16756                 :           0 :   if (this->no_zero_)
   16757                 :           0 :     alloc->set_no_zero();
   16758                 :           0 :   return alloc;
   16759                 :             : }
   16760                 :             : 
   16761                 :             : // Return the backend representation for an allocation expression.
   16762                 :             : 
   16763                 :             : Bexpression*
   16764                 :      239416 : Allocation_expression::do_get_backend(Translate_context* context)
   16765                 :             : {
   16766                 :      239416 :   Gogo* gogo = context->gogo();
   16767                 :      239416 :   Location loc = this->location();
   16768                 :      239416 :   Btype* btype = this->type_->get_backend(gogo);
   16769                 :             : 
   16770                 :      239416 :   if (this->allocate_on_stack_)
   16771                 :             :     {
   16772                 :       35031 :       int64_t size;
   16773                 :       35031 :       bool ok = this->type_->backend_type_size(gogo, &size);
   16774                 :       35031 :       if (!ok)
   16775                 :             :         {
   16776                 :           0 :           go_assert(saw_errors());
   16777                 :           0 :           return gogo->backend()->error_expression();
   16778                 :             :         }
   16779                 :       35031 :       Bstatement* decl;
   16780                 :       35031 :       Named_object* fn = context->function();
   16781                 :       35031 :       go_assert(fn != NULL);
   16782                 :       35031 :       Bfunction* fndecl = fn->func_value()->get_or_make_decl(gogo, fn);
   16783                 :       35031 :       Bexpression* init = (this->no_zero_
   16784                 :       35031 :                            ? NULL
   16785                 :       21813 :                            : gogo->backend()->zero_expression(btype));
   16786                 :       35031 :       Bvariable* temp =
   16787                 :       35031 :         gogo->backend()->temporary_variable(fndecl, context->bblock(), btype,
   16788                 :             :                                             init,
   16789                 :             :                                             Backend::variable_address_is_taken,
   16790                 :             :                                             loc, &decl);
   16791                 :       35031 :       Bexpression* ret = gogo->backend()->var_expression(temp, loc);
   16792                 :       35031 :       ret = gogo->backend()->address_expression(ret, loc);
   16793                 :       35031 :       ret = gogo->backend()->compound_expression(decl, ret, loc);
   16794                 :       35031 :       return ret;
   16795                 :             :     }
   16796                 :             : 
   16797                 :      204385 :   Bexpression* space =
   16798                 :      204385 :     gogo->allocate_memory(this->type_, loc)->get_backend(context);
   16799                 :      204385 :   Btype* pbtype = gogo->backend()->pointer_type(btype);
   16800                 :      204385 :   return gogo->backend()->convert_expression(pbtype, space, loc);
   16801                 :             : }
   16802                 :             : 
   16803                 :             : // Dump ast representation for an allocation expression.
   16804                 :             : 
   16805                 :             : void
   16806                 :           0 : Allocation_expression::do_dump_expression(Ast_dump_context* ast_dump_context)
   16807                 :             :     const
   16808                 :             : {
   16809                 :           0 :   ast_dump_context->ostream() << "new(";
   16810                 :           0 :   ast_dump_context->dump_type(this->type_);
   16811                 :           0 :   ast_dump_context->ostream() << ")";
   16812                 :           0 : }
   16813                 :             : 
   16814                 :             : // Make an allocation expression.
   16815                 :             : 
   16816                 :             : Expression*
   16817                 :      241333 : Expression::make_allocation(Type* type, Location location)
   16818                 :             : {
   16819                 :      241333 :   return new Allocation_expression(type, location);
   16820                 :             : }
   16821                 :             : 
   16822                 :             : // Class Ordered_value_list.
   16823                 :             : 
   16824                 :             : int
   16825                 :     5308929 : Ordered_value_list::traverse_vals(Traverse* traverse)
   16826                 :             : {
   16827                 :     5308929 :   if (this->vals_ != NULL)
   16828                 :             :     {
   16829                 :     5125530 :       if (this->traverse_order_ == NULL)
   16830                 :             :         {
   16831                 :     4272759 :           if (this->vals_->traverse(traverse) == TRAVERSE_EXIT)
   16832                 :             :             return TRAVERSE_EXIT;
   16833                 :             :         }
   16834                 :             :       else
   16835                 :             :         {
   16836                 :     2357104 :           for (std::vector<unsigned long>::const_iterator p =
   16837                 :      852771 :                    this->traverse_order_->begin();
   16838                 :     3209875 :                p != this->traverse_order_->end();
   16839                 :     2357104 :                ++p)
   16840                 :             :             {
   16841                 :     2365003 :               if (Expression::traverse(&this->vals_->at(*p), traverse)
   16842                 :             :                   == TRAVERSE_EXIT)
   16843                 :       38154 :                 return TRAVERSE_EXIT;
   16844                 :             :             }
   16845                 :             :         }
   16846                 :             :     }
   16847                 :             :   return TRAVERSE_CONTINUE;
   16848                 :             : }
   16849                 :             : 
   16850                 :             : // Class Struct_construction_expression.
   16851                 :             : 
   16852                 :             : // Traversal.
   16853                 :             : 
   16854                 :             : int
   16855                 :     3301508 : Struct_construction_expression::do_traverse(Traverse* traverse)
   16856                 :             : {
   16857                 :     3301508 :   if (this->traverse_vals(traverse) == TRAVERSE_EXIT)
   16858                 :             :     return TRAVERSE_EXIT;
   16859                 :     3283870 :   if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
   16860                 :             :     return TRAVERSE_EXIT;
   16861                 :             :   return TRAVERSE_CONTINUE;
   16862                 :             : }
   16863                 :             : 
   16864                 :             : // Return whether this is a constant initializer.
   16865                 :             : 
   16866                 :             : bool
   16867                 :           0 : Struct_construction_expression::is_constant_struct() const
   16868                 :             : {
   16869                 :           0 :   if (this->vals() == NULL)
   16870                 :             :     return true;
   16871                 :           0 :   for (Expression_list::const_iterator pv = this->vals()->begin();
   16872                 :           0 :        pv != this->vals()->end();
   16873                 :           0 :        ++pv)
   16874                 :             :     {
   16875                 :           0 :       if (*pv != NULL
   16876                 :           0 :           && !(*pv)->is_constant()
   16877                 :           0 :           && (!(*pv)->is_composite_literal()
   16878                 :           0 :               || (*pv)->is_nonconstant_composite_literal()))
   16879                 :           0 :         return false;
   16880                 :             :     }
   16881                 :             : 
   16882                 :           0 :   const Struct_field_list* fields = this->type_->struct_type()->fields();
   16883                 :           0 :   for (Struct_field_list::const_iterator pf = fields->begin();
   16884                 :           0 :        pf != fields->end();
   16885                 :           0 :        ++pf)
   16886                 :             :     {
   16887                 :             :       // There are no constant constructors for interfaces.
   16888                 :           0 :       if (pf->type()->interface_type() != NULL)
   16889                 :           0 :         return false;
   16890                 :             :     }
   16891                 :             : 
   16892                 :             :   return true;
   16893                 :             : }
   16894                 :             : 
   16895                 :             : // Return whether this is a zero value.
   16896                 :             : 
   16897                 :             : bool
   16898                 :          36 : Struct_construction_expression::do_is_zero_value() const
   16899                 :             : {
   16900                 :          36 :   if (this->vals() == NULL)
   16901                 :             :     return true;
   16902                 :           9 :   for (Expression_list::const_iterator pv = this->vals()->begin();
   16903                 :           9 :        pv != this->vals()->end();
   16904                 :           0 :        ++pv)
   16905                 :           9 :     if (*pv != NULL && !(*pv)->is_zero_value())
   16906                 :           9 :       return false;
   16907                 :             : 
   16908                 :           0 :   const Struct_field_list* fields = this->type_->struct_type()->fields();
   16909                 :           0 :   for (Struct_field_list::const_iterator pf = fields->begin();
   16910                 :           0 :        pf != fields->end();
   16911                 :           0 :        ++pf)
   16912                 :             :     {
   16913                 :             :       // Interface conversion may cause a zero value being converted
   16914                 :             :       // to a non-zero value, like interface{}(0).  Be conservative.
   16915                 :           0 :       if (pf->type()->interface_type() != NULL)
   16916                 :           9 :         return false;
   16917                 :             :     }
   16918                 :             : 
   16919                 :             :   return true;
   16920                 :             : }
   16921                 :             : 
   16922                 :             : // Return whether this struct can be used as a constant initializer.
   16923                 :             : 
   16924                 :             : bool
   16925                 :      866679 : Struct_construction_expression::do_is_static_initializer() const
   16926                 :             : {
   16927                 :      866679 :   if (this->vals() == NULL)
   16928                 :             :     return true;
   16929                 :     4565226 :   for (Expression_list::const_iterator pv = this->vals()->begin();
   16930                 :     4565226 :        pv != this->vals()->end();
   16931                 :     3699126 :        ++pv)
   16932                 :             :     {
   16933                 :     3703428 :       if (*pv != NULL && !(*pv)->is_static_initializer())
   16934                 :        4374 :         return false;
   16935                 :             :     }
   16936                 :             : 
   16937                 :     1723596 :   const Struct_field_list* fields = this->type_->struct_type()->fields();
   16938                 :      861798 :   for (Struct_field_list::const_iterator pf = fields->begin();
   16939                 :     4556095 :        pf != fields->end();
   16940                 :     3694297 :        ++pf)
   16941                 :             :     {
   16942                 :             :       // There are no constant constructors for interfaces.
   16943                 :     7388666 :       if (pf->type()->interface_type() != NULL)
   16944                 :        4374 :         return false;
   16945                 :             :     }
   16946                 :             : 
   16947                 :             :   return true;
   16948                 :             : }
   16949                 :             : 
   16950                 :             : // Final type determination.
   16951                 :             : 
   16952                 :             : void
   16953                 :     1799406 : Struct_construction_expression::do_determine_type(Gogo* gogo,
   16954                 :             :                                                   const Type_context*)
   16955                 :             : {
   16956                 :     1799406 :   if (this->vals() == NULL)
   16957                 :             :     return;
   16958                 :     3583654 :   const Struct_field_list* fields = this->type_->struct_type()->fields();
   16959                 :     1791827 :   Expression_list::const_iterator pv = this->vals()->begin();
   16960                 :     1791827 :   for (Struct_field_list::const_iterator pf = fields->begin();
   16961                 :    11658764 :        pf != fields->end();
   16962                 :     9866937 :        ++pf, ++pv)
   16963                 :             :     {
   16964                 :     9866937 :       if (pv == this->vals()->end())
   16965                 :     1799406 :         return;
   16966                 :     9866937 :       if (*pv != NULL)
   16967                 :             :         {
   16968                 :     9819470 :           Type_context subcontext(pf->type(), false);
   16969                 :     9819470 :           (*pv)->determine_type(gogo, &subcontext);
   16970                 :             :         }
   16971                 :             :     }
   16972                 :             :   // Extra values are an error we will report elsewhere; we still want
   16973                 :             :   // to determine the type to avoid knockon errors.
   16974                 :     1830473 :   for (; pv != this->vals()->end(); ++pv)
   16975                 :       38646 :     (*pv)->determine_type_no_context(gogo);
   16976                 :             : }
   16977                 :             : 
   16978                 :             : // Check types.
   16979                 :             : 
   16980                 :             : void
   16981                 :       29829 : Struct_construction_expression::do_check_types(Gogo* gogo)
   16982                 :             : {
   16983                 :       29829 :   Struct_construction_expression::check_value_types(gogo, this->type_,
   16984                 :             :                                                     this->vals(),
   16985                 :             :                                                     this->location());
   16986                 :       29829 : }
   16987                 :             : 
   16988                 :             : // Check types.  This static function is also called by
   16989                 :             : // Composite_literal_expression::do_check_types.  Reports whether type
   16990                 :             : // checking succeeded.
   16991                 :             : 
   16992                 :             : bool
   16993                 :      203788 : Struct_construction_expression::check_value_types(Gogo* gogo,
   16994                 :             :                                                   Type* type,
   16995                 :             :                                                   Expression_list* vals,
   16996                 :             :                                                   Location loc)
   16997                 :             : {
   16998                 :      203788 :   if (vals == NULL || vals->empty())
   16999                 :             :     return true;
   17000                 :             : 
   17001                 :      194860 :   Struct_type* st = type->struct_type();
   17002                 :      194860 :   if (vals->size() > st->field_count())
   17003                 :             :     {
   17004                 :           4 :       go_error_at(loc, "too many expressions for struct");
   17005                 :           4 :       return false;
   17006                 :             :     }
   17007                 :             : 
   17008                 :      194856 :   bool imported_type =
   17009                 :      194856 :     (type->named_type() != NULL
   17010                 :      194856 :      && type->named_type()->named_object()->package() != NULL);
   17011                 :             : 
   17012                 :      194856 :   bool ret = true;
   17013                 :      194856 :   const Struct_field_list* fields = st->fields();
   17014                 :      194856 :   Expression_list::const_iterator pv = vals->begin();
   17015                 :      194856 :   int i = 0;
   17016                 :      194856 :   for (Struct_field_list::const_iterator pf = fields->begin();
   17017                 :      819930 :        pf != fields->end();
   17018                 :      625074 :        ++pf, ++pv, ++i)
   17019                 :             :     {
   17020                 :      625077 :       if (pv == vals->end())
   17021                 :             :         {
   17022                 :           3 :           go_error_at(loc, "too few expressions for struct");
   17023                 :           3 :           return false;
   17024                 :             :         }
   17025                 :             : 
   17026                 :      625074 :       if (*pv == NULL)
   17027                 :       95941 :         continue;
   17028                 :             : 
   17029                 :      529133 :       if (imported_type
   17030                 :      529133 :           && (Gogo::is_hidden_name(pf->field_name())
   17031                 :       60732 :               || pf->is_embedded_builtin(gogo)))
   17032                 :             :         {
   17033                 :           2 :           go_error_at(loc,
   17034                 :             :                       "assignment of unexported field %qs in %qs literal",
   17035                 :           4 :                       Gogo::message_name(pf->field_name()).c_str(),
   17036                 :           2 :                       type->named_type()->message_name().c_str());
   17037                 :           2 :           ret = false;
   17038                 :             :         }
   17039                 :             : 
   17040                 :      529133 :       std::string reason;
   17041                 :      529133 :       if (!Type::are_assignable(pf->type(), (*pv)->type(), &reason))
   17042                 :             :         {
   17043                 :           4 :           if (reason.empty())
   17044                 :           0 :             go_error_at((*pv)->location(),
   17045                 :             :                         "incompatible type for field %d in struct construction",
   17046                 :             :                         i + 1);
   17047                 :             :           else
   17048                 :           4 :             go_error_at((*pv)->location(),
   17049                 :             :                         ("incompatible type for field %d in "
   17050                 :             :                          "struct construction (%s)"),
   17051                 :             :                         i + 1, reason.c_str());
   17052                 :             :           ret = false;
   17053                 :             :         }
   17054                 :      529133 :     }
   17055                 :      194853 :   go_assert(pv == vals->end());
   17056                 :             :   return ret;
   17057                 :             : }
   17058                 :             : 
   17059                 :             : // Copy.
   17060                 :             : 
   17061                 :             : Expression*
   17062                 :           0 : Struct_construction_expression::do_copy()
   17063                 :             : {
   17064                 :           0 :   Struct_construction_expression* ret =
   17065                 :           0 :     new Struct_construction_expression(this->type_->copy_expressions(),
   17066                 :           0 :                                        (this->vals() == NULL
   17067                 :             :                                         ? NULL
   17068                 :           0 :                                         : this->vals()->copy()),
   17069                 :           0 :                                        this->location());
   17070                 :           0 :   if (this->traverse_order() != NULL)
   17071                 :           0 :     ret->set_traverse_order(this->traverse_order());
   17072                 :           0 :   return ret;
   17073                 :             : }
   17074                 :             : 
   17075                 :             : // Make implicit type conversions explicit.
   17076                 :             : 
   17077                 :             : void
   17078                 :      186522 : Struct_construction_expression::do_add_conversions()
   17079                 :             : {
   17080                 :      186522 :   if (this->vals() == NULL)
   17081                 :        7698 :     return;
   17082                 :             : 
   17083                 :      178824 :   Location loc = this->location();
   17084                 :      357648 :   const Struct_field_list* fields = this->type_->struct_type()->fields();
   17085                 :      178824 :   Expression_list::iterator pv = this->vals()->begin();
   17086                 :      178824 :   for (Struct_field_list::const_iterator pf = fields->begin();
   17087                 :      778681 :        pf != fields->end();
   17088                 :      599857 :        ++pf, ++pv)
   17089                 :             :     {
   17090                 :      599857 :       if (pv == this->vals()->end())
   17091                 :             :         break;
   17092                 :      599857 :       if (*pv != NULL)
   17093                 :             :         {
   17094                 :      503873 :           Type* ft = pf->type();
   17095                 :      503873 :           if (!Type::are_identical(ft, (*pv)->type(), 0, NULL)
   17096                 :      618120 :               && ft->interface_type() != NULL)
   17097                 :       15177 :            *pv = Expression::make_cast(ft, *pv, loc);
   17098                 :             :         }
   17099                 :             :     }
   17100                 :             : }
   17101                 :             : 
   17102                 :             : // Return the backend representation for constructing a struct.
   17103                 :             : 
   17104                 :             : Bexpression*
   17105                 :     1133911 : Struct_construction_expression::do_get_backend(Translate_context* context)
   17106                 :             : {
   17107                 :     1133911 :   Gogo* gogo = context->gogo();
   17108                 :             : 
   17109                 :     1133911 :   Btype* btype = this->type_->get_backend(gogo);
   17110                 :     1133911 :   if (this->vals() == NULL)
   17111                 :        7690 :     return gogo->backend()->zero_expression(btype);
   17112                 :             : 
   17113                 :     2252442 :   const Struct_field_list* fields = this->type_->struct_type()->fields();
   17114                 :     1126221 :   Expression_list::const_iterator pv = this->vals()->begin();
   17115                 :     1126221 :   std::vector<Bexpression*> init;
   17116                 :     6877847 :   for (Struct_field_list::const_iterator pf = fields->begin();
   17117                 :     6877847 :        pf != fields->end();
   17118                 :     5751626 :        ++pf)
   17119                 :             :     {
   17120                 :     5751626 :       Btype* fbtype = pf->type()->get_backend(gogo);
   17121                 :     5751626 :       if (pv == this->vals()->end())
   17122                 :           0 :         init.push_back(gogo->backend()->zero_expression(fbtype));
   17123                 :     5751626 :       else if (*pv == NULL)
   17124                 :             :         {
   17125                 :       95958 :           init.push_back(gogo->backend()->zero_expression(fbtype));
   17126                 :       95958 :           ++pv;
   17127                 :             :         }
   17128                 :             :       else
   17129                 :             :         {
   17130                 :     5655668 :           Expression* val =
   17131                 :     5655668 :               Expression::convert_for_assignment(gogo, pf->type(),
   17132                 :     5655668 :                                                  *pv, this->location());
   17133                 :     5655668 :           init.push_back(val->get_backend(context));
   17134                 :     5655668 :           ++pv;
   17135                 :             :         }
   17136                 :             :     }
   17137                 :     2252442 :   if (this->type_->struct_type()->has_padding())
   17138                 :             :     {
   17139                 :             :       // Feed an extra value if there is a padding field.
   17140                 :         250 :       Btype *fbtype = Type::lookup_integer_type("uint8")->get_backend(gogo);
   17141                 :         250 :       init.push_back(gogo->backend()->zero_expression(fbtype));
   17142                 :             :     }
   17143                 :     1126221 :   return gogo->backend()->constructor_expression(btype, init, this->location());
   17144                 :     1126221 : }
   17145                 :             : 
   17146                 :             : // Export a struct construction.
   17147                 :             : 
   17148                 :             : void
   17149                 :           0 : Struct_construction_expression::do_export(Export_function_body* efb) const
   17150                 :             : {
   17151                 :           0 :   efb->write_c_string("$convert(");
   17152                 :           0 :   efb->write_type(this->type_);
   17153                 :           0 :   for (Expression_list::const_iterator pv = this->vals()->begin();
   17154                 :           0 :        pv != this->vals()->end();
   17155                 :           0 :        ++pv)
   17156                 :             :     {
   17157                 :           0 :       efb->write_c_string(", ");
   17158                 :           0 :       if (*pv != NULL)
   17159                 :           0 :         (*pv)->export_expression(efb);
   17160                 :             :     }
   17161                 :           0 :   efb->write_c_string(")");
   17162                 :           0 : }
   17163                 :             : 
   17164                 :             : // Dump ast representation of a struct construction expression.
   17165                 :             : 
   17166                 :             : void
   17167                 :           0 : Struct_construction_expression::do_dump_expression(
   17168                 :             :     Ast_dump_context* ast_dump_context) const
   17169                 :             : {
   17170                 :           0 :   ast_dump_context->dump_type(this->type_);
   17171                 :           0 :   ast_dump_context->ostream() << "{";
   17172                 :           0 :   ast_dump_context->dump_expression_list(this->vals());
   17173                 :           0 :   ast_dump_context->ostream() << "}";
   17174                 :           0 : }
   17175                 :             : 
   17176                 :             : // Make a struct composite literal.  This used by the thunk code.
   17177                 :             : 
   17178                 :             : Expression*
   17179                 :     1369998 : Expression::make_struct_composite_literal(Type* type, Expression_list* vals,
   17180                 :             :                                           Location location)
   17181                 :             : {
   17182                 :     1369998 :   go_assert(type->struct_type() != NULL);
   17183                 :     1369998 :   return new Struct_construction_expression(type, vals, location);
   17184                 :             : }
   17185                 :             : 
   17186                 :             : // Class Array_construction_expression.
   17187                 :             : 
   17188                 :             : // Traversal.
   17189                 :             : 
   17190                 :             : int
   17191                 :     2007421 : Array_construction_expression::do_traverse(Traverse* traverse)
   17192                 :             : {
   17193                 :     2007421 :   if (this->traverse_vals(traverse) == TRAVERSE_EXIT)
   17194                 :             :     return TRAVERSE_EXIT;
   17195                 :     1986905 :   if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
   17196                 :             :     return TRAVERSE_EXIT;
   17197                 :             :   return TRAVERSE_CONTINUE;
   17198                 :             : }
   17199                 :             : 
   17200                 :             : // Return whether this is a constant initializer.
   17201                 :             : 
   17202                 :             : bool
   17203                 :           0 : Array_construction_expression::is_constant_array() const
   17204                 :             : {
   17205                 :           0 :   if (this->vals() == NULL)
   17206                 :             :     return true;
   17207                 :             : 
   17208                 :             :   // There are no constant constructors for interfaces.
   17209                 :           0 :   if (this->type_->array_type()->element_type()->interface_type() != NULL)
   17210                 :             :     return false;
   17211                 :             : 
   17212                 :           0 :   for (Expression_list::const_iterator pv = this->vals()->begin();
   17213                 :           0 :        pv != this->vals()->end();
   17214                 :           0 :        ++pv)
   17215                 :             :     {
   17216                 :           0 :       if (*pv != NULL
   17217                 :           0 :           && !(*pv)->is_constant()
   17218                 :           0 :           && (!(*pv)->is_composite_literal()
   17219                 :           0 :               || (*pv)->is_nonconstant_composite_literal()))
   17220                 :           0 :         return false;
   17221                 :             :     }
   17222                 :             :   return true;
   17223                 :             : }
   17224                 :             : 
   17225                 :             : // Return whether this is a zero value.
   17226                 :             : 
   17227                 :             : bool
   17228                 :          10 : Array_construction_expression::do_is_zero_value() const
   17229                 :             : {
   17230                 :          10 :   if (this->vals() == NULL)
   17231                 :             :     return true;
   17232                 :             : 
   17233                 :             :   // Interface conversion may cause a zero value being converted
   17234                 :             :   // to a non-zero value, like interface{}(0).  Be conservative.
   17235                 :          12 :   if (this->type_->array_type()->element_type()->interface_type() != NULL)
   17236                 :             :     return false;
   17237                 :             : 
   17238                 :           4 :   for (Expression_list::const_iterator pv = this->vals()->begin();
   17239                 :           4 :        pv != this->vals()->end();
   17240                 :           0 :        ++pv)
   17241                 :           4 :     if (*pv != NULL && !(*pv)->is_zero_value())
   17242                 :           4 :       return false;
   17243                 :             : 
   17244                 :             :   return true;
   17245                 :             : }
   17246                 :             : 
   17247                 :             : // Return whether this can be used a constant initializer.
   17248                 :             : 
   17249                 :             : bool
   17250                 :      860706 : Array_construction_expression::do_is_static_initializer() const
   17251                 :             : {
   17252                 :      860706 :   if (this->vals() == NULL)
   17253                 :             :     return true;
   17254                 :             : 
   17255                 :             :   // There are no constant constructors for interfaces.
   17256                 :     1384509 :   if (this->type_->array_type()->element_type()->interface_type() != NULL)
   17257                 :             :     return false;
   17258                 :             : 
   17259                 :     2773895 :   for (Expression_list::const_iterator pv = this->vals()->begin();
   17260                 :     2773895 :        pv != this->vals()->end();
   17261                 :     2193066 :        ++pv)
   17262                 :             :     {
   17263                 :     2236127 :       if (*pv != NULL && !(*pv)->is_static_initializer())
   17264                 :      102991 :         return false;
   17265                 :             :     }
   17266                 :             :   return true;
   17267                 :             : }
   17268                 :             : 
   17269                 :             : // Final type determination.
   17270                 :             : 
   17271                 :             : void
   17272                 :      494264 : Array_construction_expression::do_determine_type(Gogo* gogo,
   17273                 :             :                                                  const Type_context*)
   17274                 :             : {
   17275                 :      494264 :   if (this->is_error_expression())
   17276                 :             :     {
   17277                 :           0 :       go_assert(saw_errors());
   17278                 :      148651 :       return;
   17279                 :             :     }
   17280                 :             : 
   17281                 :      494264 :   if (this->vals() == NULL)
   17282                 :             :     return;
   17283                 :      345613 :   Array_type* at = this->type_->array_type();
   17284                 :      345613 :   if (at == NULL || at->is_error() || at->element_type()->is_error())
   17285                 :             :     {
   17286                 :           0 :       go_assert(saw_errors());
   17287                 :           0 :       this->set_is_error();
   17288                 :           0 :       return;
   17289                 :             :     }
   17290                 :      345613 :   Type_context subcontext(at->element_type(), false);
   17291                 :     1554847 :   for (Expression_list::const_iterator pv = this->vals()->begin();
   17292                 :     1554847 :        pv != this->vals()->end();
   17293                 :     1209234 :        ++pv)
   17294                 :             :     {
   17295                 :     1209234 :       if (*pv != NULL)
   17296                 :     1209234 :         (*pv)->determine_type(gogo, &subcontext);
   17297                 :             :     }
   17298                 :             : }
   17299                 :             : 
   17300                 :             : // Check types.
   17301                 :             : 
   17302                 :             : void
   17303                 :       64037 : Array_construction_expression::do_check_types(Gogo*)
   17304                 :             : {
   17305                 :       64037 :   if (this->is_error_expression())
   17306                 :             :     {
   17307                 :           0 :       go_assert(saw_errors());
   17308                 :             :       return;
   17309                 :             :     }
   17310                 :             : 
   17311                 :       64037 :   if (this->vals() == NULL)
   17312                 :             :     return;
   17313                 :             : 
   17314                 :       64028 :   Array_type* at = this->type_->array_type();
   17315                 :       64028 :   if (at == NULL || at->is_error() || at->element_type()->is_error())
   17316                 :             :     {
   17317                 :           0 :       go_assert(saw_errors());
   17318                 :           0 :       this->set_is_error();
   17319                 :           0 :       return;
   17320                 :             :     }
   17321                 :       64028 :   int i = 0;
   17322                 :       64028 :   Type* element_type = at->element_type();
   17323                 :       64028 :   for (Expression_list::const_iterator pv = this->vals()->begin();
   17324                 :      179313 :        pv != this->vals()->end();
   17325                 :      115285 :        ++pv, ++i)
   17326                 :             :     {
   17327                 :      115285 :       if (*pv != NULL
   17328                 :      115285 :           && !Type::are_assignable(element_type, (*pv)->type(), NULL))
   17329                 :             :         {
   17330                 :           0 :           go_error_at((*pv)->location(),
   17331                 :             :                       "incompatible type for element %d in composite literal",
   17332                 :             :                       i + 1);
   17333                 :           0 :           this->set_is_error();
   17334                 :             :         }
   17335                 :             :     }
   17336                 :             : }
   17337                 :             : 
   17338                 :             : // Make implicit type conversions explicit.
   17339                 :             : 
   17340                 :             : void
   17341                 :      117250 : Array_construction_expression::do_add_conversions()
   17342                 :             : {
   17343                 :      117250 :   if (this->is_error_expression())
   17344                 :             :     {
   17345                 :           0 :       go_assert(saw_errors());
   17346                 :       57385 :       return;
   17347                 :             :     }
   17348                 :             : 
   17349                 :      117250 :   if (this->vals() == NULL)
   17350                 :             :     return;
   17351                 :             : 
   17352                 :      226692 :   Type* et = this->type_->array_type()->element_type();
   17353                 :      170731 :   if (et->interface_type() == NULL)
   17354                 :             :     return;
   17355                 :             : 
   17356                 :       59865 :   Location loc = this->location();
   17357                 :      169313 :   for (Expression_list::iterator pv = this->vals()->begin();
   17358                 :      169313 :        pv != this->vals()->end();
   17359                 :      109448 :        ++pv)
   17360                 :      109448 :     if (!Type::are_identical(et, (*pv)->type(), 0, NULL))
   17361                 :      107390 :       *pv = Expression::make_cast(et, *pv, loc);
   17362                 :             : }
   17363                 :             : 
   17364                 :             : // Get a constructor expression for the array values.
   17365                 :             : 
   17366                 :             : Bexpression*
   17367                 :      434332 : Array_construction_expression::get_constructor(Translate_context* context,
   17368                 :             :                                                Btype* array_btype)
   17369                 :             : {
   17370                 :      868664 :   Type* element_type = this->type_->array_type()->element_type();
   17371                 :             : 
   17372                 :      434332 :   std::vector<unsigned long> indexes;
   17373                 :      434332 :   std::vector<Bexpression*> vals;
   17374                 :      434332 :   Gogo* gogo = context->gogo();
   17375                 :      434332 :   if (this->vals() != NULL)
   17376                 :             :     {
   17377                 :      362797 :       size_t i = 0;
   17378                 :      362797 :       std::vector<unsigned long>::const_iterator pi;
   17379                 :      362797 :       if (this->indexes_ != NULL)
   17380                 :         448 :         pi = this->indexes_->begin();
   17381                 :      362797 :       for (Expression_list::const_iterator pv = this->vals()->begin();
   17382                 :     2053681 :            pv != this->vals()->end();
   17383                 :     1690884 :            ++pv, ++i)
   17384                 :             :         {
   17385                 :     1690884 :           if (this->indexes_ != NULL)
   17386                 :      110395 :             go_assert(pi != this->indexes_->end());
   17387                 :             : 
   17388                 :     1690884 :           if (this->indexes_ == NULL)
   17389                 :     1580489 :             indexes.push_back(i);
   17390                 :             :           else
   17391                 :      110395 :             indexes.push_back(*pi);
   17392                 :     1690884 :           if (*pv == NULL)
   17393                 :             :             {
   17394                 :           0 :               Btype* ebtype = element_type->get_backend(gogo);
   17395                 :           0 :               Bexpression *zv = gogo->backend()->zero_expression(ebtype);
   17396                 :           0 :               vals.push_back(zv);
   17397                 :             :             }
   17398                 :             :           else
   17399                 :             :             {
   17400                 :     1690884 :               Expression* val_expr =
   17401                 :     1690884 :                   Expression::convert_for_assignment(gogo, element_type, *pv,
   17402                 :             :                                                      this->location());
   17403                 :     1690884 :               vals.push_back(val_expr->get_backend(context));
   17404                 :             :             }
   17405                 :     1690884 :           if (this->indexes_ != NULL)
   17406                 :      110395 :             ++pi;
   17407                 :             :         }
   17408                 :      362797 :       if (this->indexes_ != NULL)
   17409                 :         448 :         go_assert(pi == this->indexes_->end());
   17410                 :             :     }
   17411                 :      434332 :   return gogo->backend()->array_constructor_expression(array_btype, indexes,
   17412                 :      434332 :                                                        vals, this->location());
   17413                 :      434332 : }
   17414                 :             : 
   17415                 :             : // Export an array construction.
   17416                 :             : 
   17417                 :             : void
   17418                 :           0 : Array_construction_expression::do_export(Export_function_body* efb) const
   17419                 :             : {
   17420                 :           0 :   efb->write_c_string("$convert(");
   17421                 :           0 :   efb->write_type(this->type_);
   17422                 :           0 :   if (this->vals() != NULL)
   17423                 :             :     {
   17424                 :           0 :       std::vector<unsigned long>::const_iterator pi;
   17425                 :           0 :       if (this->indexes_ != NULL)
   17426                 :           0 :         pi = this->indexes_->begin();
   17427                 :           0 :       for (Expression_list::const_iterator pv = this->vals()->begin();
   17428                 :           0 :            pv != this->vals()->end();
   17429                 :           0 :            ++pv)
   17430                 :             :         {
   17431                 :           0 :           efb->write_c_string(", ");
   17432                 :             : 
   17433                 :           0 :           if (this->indexes_ != NULL)
   17434                 :             :             {
   17435                 :           0 :               char buf[100];
   17436                 :           0 :               snprintf(buf, sizeof buf, "%lu", *pi);
   17437                 :           0 :               efb->write_c_string(buf);
   17438                 :           0 :               efb->write_c_string(":");
   17439                 :             :             }
   17440                 :             : 
   17441                 :           0 :           if (*pv != NULL)
   17442                 :           0 :             (*pv)->export_expression(efb);
   17443                 :             : 
   17444                 :           0 :           if (this->indexes_ != NULL)
   17445                 :           0 :             ++pi;
   17446                 :             :         }
   17447                 :             :     }
   17448                 :           0 :   efb->write_c_string(")");
   17449                 :           0 : }
   17450                 :             : 
   17451                 :             : // Dump ast representation of an array construction expression.
   17452                 :             : 
   17453                 :             : void
   17454                 :           0 : Array_construction_expression::do_dump_expression(
   17455                 :             :     Ast_dump_context* ast_dump_context) const
   17456                 :             : {
   17457                 :           0 :   Expression* length = this->type_->array_type()->length();
   17458                 :             : 
   17459                 :           0 :   ast_dump_context->ostream() << "[" ;
   17460                 :           0 :   if (length != NULL)
   17461                 :             :     {
   17462                 :           0 :       ast_dump_context->dump_expression(length);
   17463                 :             :     }
   17464                 :           0 :   ast_dump_context->ostream() << "]" ;
   17465                 :           0 :   ast_dump_context->dump_type(this->type_);
   17466                 :           0 :   this->dump_slice_storage_expression(ast_dump_context);
   17467                 :           0 :   ast_dump_context->ostream() << "{" ;
   17468                 :           0 :   if (this->indexes_ == NULL)
   17469                 :           0 :     ast_dump_context->dump_expression_list(this->vals());
   17470                 :             :   else
   17471                 :             :     {
   17472                 :           0 :       Expression_list::const_iterator pv = this->vals()->begin();
   17473                 :           0 :       for (std::vector<unsigned long>::const_iterator pi =
   17474                 :           0 :              this->indexes_->begin();
   17475                 :           0 :            pi != this->indexes_->end();
   17476                 :           0 :            ++pi, ++pv)
   17477                 :             :         {
   17478                 :           0 :           if (pi != this->indexes_->begin())
   17479                 :           0 :             ast_dump_context->ostream() << ", ";
   17480                 :           0 :           ast_dump_context->ostream() << *pi << ':';
   17481                 :           0 :           ast_dump_context->dump_expression(*pv);
   17482                 :             :         }
   17483                 :             :     }
   17484                 :           0 :   ast_dump_context->ostream() << "}" ;
   17485                 :             : 
   17486                 :           0 : }
   17487                 :             : 
   17488                 :             : // Class Fixed_array_construction_expression.
   17489                 :             : 
   17490                 :      434372 : Fixed_array_construction_expression::Fixed_array_construction_expression(
   17491                 :             :     Type* type, const std::vector<unsigned long>* indexes,
   17492                 :      434372 :     Expression_list* vals, Location location)
   17493                 :             :   : Array_construction_expression(EXPRESSION_FIXED_ARRAY_CONSTRUCTION,
   17494                 :      434372 :                                   type, indexes, vals, location)
   17495                 :      868744 : { go_assert(type->array_type() != NULL && !type->is_slice_type()); }
   17496                 :             : 
   17497                 :             : 
   17498                 :             : // Copy.
   17499                 :             : 
   17500                 :             : Expression*
   17501                 :           0 : Fixed_array_construction_expression::do_copy()
   17502                 :             : {
   17503                 :           0 :   Type* t = this->type()->copy_expressions();
   17504                 :           0 :   return new Fixed_array_construction_expression(t, this->indexes(),
   17505                 :           0 :                                                  (this->vals() == NULL
   17506                 :             :                                                   ? NULL
   17507                 :           0 :                                                   : this->vals()->copy()),
   17508                 :           0 :                                                  this->location());
   17509                 :             : }
   17510                 :             : 
   17511                 :             : // Return the backend representation for constructing a fixed array.
   17512                 :             : 
   17513                 :             : Bexpression*
   17514                 :      434332 : Fixed_array_construction_expression::do_get_backend(Translate_context* context)
   17515                 :             : {
   17516                 :      434332 :   Type* type = this->type();
   17517                 :      434332 :   Btype* btype = type->get_backend(context->gogo());
   17518                 :      434332 :   return this->get_constructor(context, btype);
   17519                 :             : }
   17520                 :             : 
   17521                 :             : Expression*
   17522                 :       36250 : Expression::make_array_composite_literal(Type* type, Expression_list* vals,
   17523                 :             :                                          Location location)
   17524                 :             : {
   17525                 :       72500 :   go_assert(type->array_type() != NULL && !type->is_slice_type());
   17526                 :       36250 :   return new Fixed_array_construction_expression(type, NULL, vals, location);
   17527                 :             : }
   17528                 :             : 
   17529                 :             : // Class Slice_construction_expression.
   17530                 :             : 
   17531                 :      434071 : Slice_construction_expression::Slice_construction_expression(
   17532                 :             :   Type* type, const std::vector<unsigned long>* indexes,
   17533                 :      434071 :   Expression_list* vals, Location location)
   17534                 :             :   : Array_construction_expression(EXPRESSION_SLICE_CONSTRUCTION,
   17535                 :             :                                   type, indexes, vals, location),
   17536                 :      434071 :     valtype_(NULL), array_val_(NULL), slice_storage_(NULL),
   17537                 :      434071 :     storage_escapes_(true)
   17538                 :             : {
   17539                 :      434071 :   go_assert(type->is_slice_type());
   17540                 :             : 
   17541                 :      434071 :   unsigned long lenval;
   17542                 :      434071 :   Expression* length;
   17543                 :      434071 :   if (vals == NULL || vals->empty())
   17544                 :             :     lenval = 0;
   17545                 :             :   else
   17546                 :             :     {
   17547                 :      345094 :       if (this->indexes() == NULL)
   17548                 :      344964 :         lenval = vals->size();
   17549                 :             :       else
   17550                 :         130 :         lenval = indexes->back() + 1;
   17551                 :             :     }
   17552                 :      434071 :   Type* int_type = Type::lookup_integer_type("int");
   17553                 :      434071 :   length = Expression::make_integer_ul(lenval, int_type, location);
   17554                 :      868142 :   Type* element_type = type->array_type()->element_type();
   17555                 :      434071 :   Array_type* array_type = Type::make_array_type(element_type, length);
   17556                 :      434071 :   array_type->set_is_array_incomparable();
   17557                 :      434071 :   this->valtype_ = array_type;
   17558                 :      434071 : }
   17559                 :             : 
   17560                 :             : // Traversal.
   17561                 :             : 
   17562                 :             : int
   17563                 :     1644060 : Slice_construction_expression::do_traverse(Traverse* traverse)
   17564                 :             : {
   17565                 :     1644060 :   if (this->Array_construction_expression::do_traverse(traverse)
   17566                 :             :       == TRAVERSE_EXIT)
   17567                 :             :     return TRAVERSE_EXIT;
   17568                 :     1626367 :   if (Type::traverse(this->valtype_, traverse) == TRAVERSE_EXIT)
   17569                 :             :     return TRAVERSE_EXIT;
   17570                 :     1624835 :   if (this->array_val_ != NULL
   17571                 :     1624835 :       && Expression::traverse(&this->array_val_, traverse) == TRAVERSE_EXIT)
   17572                 :             :     return TRAVERSE_EXIT;
   17573                 :     1624835 :   if (this->slice_storage_ != NULL
   17574                 :     1624835 :       && Expression::traverse(&this->slice_storage_, traverse) == TRAVERSE_EXIT)
   17575                 :             :     return TRAVERSE_EXIT;
   17576                 :             :   return TRAVERSE_CONTINUE;
   17577                 :             : }
   17578                 :             : 
   17579                 :             : // Helper routine to create fixed array value underlying the slice literal.
   17580                 :             : // May be called during flattening, or later during do_get_backend().
   17581                 :             : 
   17582                 :             : Expression*
   17583                 :      377447 : Slice_construction_expression::create_array_val()
   17584                 :             : {
   17585                 :      377447 :   Array_type* array_type = this->type()->array_type();
   17586                 :      377447 :   if (array_type == NULL)
   17587                 :             :     {
   17588                 :           0 :       go_assert(this->type()->is_error());
   17589                 :             :       return NULL;
   17590                 :             :     }
   17591                 :             : 
   17592                 :      377447 :   Location loc = this->location();
   17593                 :      377447 :   go_assert(this->valtype_ != NULL);
   17594                 :             : 
   17595                 :      377447 :   Expression_list* vals = this->vals();
   17596                 :      377447 :   return new Fixed_array_construction_expression(
   17597                 :      377447 :       this->valtype_, this->indexes(), vals, loc);
   17598                 :             : }
   17599                 :             : 
   17600                 :             : // If we're previous established that the slice storage does not
   17601                 :             : // escape, then create a separate array temp val here for it. We
   17602                 :             : // need to do this as part of flattening so as to be able to insert
   17603                 :             : // the new temp statement.
   17604                 :             : 
   17605                 :             : Expression*
   17606                 :      113370 : Slice_construction_expression::do_flatten(Gogo*, Named_object*,
   17607                 :             :                                           Statement_inserter* inserter)
   17608                 :             : {
   17609                 :      113370 :   if (this->type()->array_type() == NULL)
   17610                 :             :     {
   17611                 :           0 :       go_assert(saw_errors());
   17612                 :           0 :       return Expression::make_error(this->location());
   17613                 :             :     }
   17614                 :             : 
   17615                 :             :   // Create a stack-allocated storage temp if storage won't escape
   17616                 :      113370 :   if (!this->storage_escapes_
   17617                 :       35047 :       && this->slice_storage_ == NULL
   17618                 :      146335 :       && this->element_count() > 0)
   17619                 :             :     {
   17620                 :       32759 :       Location loc = this->location();
   17621                 :       32759 :       this->array_val_ = this->create_array_val();
   17622                 :       32759 :       go_assert(this->array_val_ != NULL);
   17623                 :       32759 :       Temporary_statement* temp =
   17624                 :       32759 :           Statement::make_temporary(this->valtype_, this->array_val_, loc);
   17625                 :       32759 :       inserter->insert(temp);
   17626                 :       32759 :       this->slice_storage_ = Expression::make_temporary_reference(temp, loc);
   17627                 :             :     }
   17628                 :             :   return this;
   17629                 :             : }
   17630                 :             : 
   17631                 :             : // When dumping a slice construction expression that has an explicit
   17632                 :             : // storeage temp, emit the temp here (if we don't do this the storage
   17633                 :             : // temp appears unused in the AST dump).
   17634                 :             : 
   17635                 :             : void
   17636                 :           0 : Slice_construction_expression::
   17637                 :             : dump_slice_storage_expression(Ast_dump_context* ast_dump_context) const
   17638                 :             : {
   17639                 :           0 :   if (this->slice_storage_ == NULL)
   17640                 :             :     return;
   17641                 :           0 :   ast_dump_context->ostream() << "storage=" ;
   17642                 :           0 :   ast_dump_context->dump_expression(this->slice_storage_);
   17643                 :             : }
   17644                 :             : 
   17645                 :             : // Copy.
   17646                 :             : 
   17647                 :             : Expression*
   17648                 :           0 : Slice_construction_expression::do_copy()
   17649                 :             : {
   17650                 :           0 :   return new Slice_construction_expression(this->type()->copy_expressions(),
   17651                 :             :                                            this->indexes(),
   17652                 :           0 :                                            (this->vals() == NULL
   17653                 :             :                                             ? NULL
   17654                 :           0 :                                             : this->vals()->copy()),
   17655                 :           0 :                                            this->location());
   17656                 :             : }
   17657                 :             : 
   17658                 :             : // Return the backend representation for constructing a slice.
   17659                 :             : 
   17660                 :             : Bexpression*
   17661                 :      377443 : Slice_construction_expression::do_get_backend(Translate_context* context)
   17662                 :             : {
   17663                 :      377443 :   if (this->array_val_ == NULL)
   17664                 :      344688 :     this->array_val_ = this->create_array_val();
   17665                 :      377443 :   if (this->array_val_ == NULL)
   17666                 :             :     {
   17667                 :           0 :       go_assert(this->type()->is_error());
   17668                 :           0 :       return context->backend()->error_expression();
   17669                 :             :     }
   17670                 :             : 
   17671                 :      377443 :   Location loc = this->location();
   17672                 :             : 
   17673                 :      377443 :   bool is_static_initializer = this->array_val_->is_static_initializer();
   17674                 :             : 
   17675                 :             :   // We have to copy the initial values into heap memory if we are in
   17676                 :             :   // a function or if the values are not constants.
   17677                 :      377443 :   bool copy_to_heap = context->function() != NULL || !is_static_initializer;
   17678                 :             : 
   17679                 :      377443 :   Expression* space;
   17680                 :             : 
   17681                 :      377443 :   if (this->slice_storage_ != NULL)
   17682                 :             :     {
   17683                 :       32755 :       go_assert(!this->storage_escapes_);
   17684                 :       32755 :       space = Expression::make_unary(OPERATOR_AND, this->slice_storage_, loc);
   17685                 :             :     }
   17686                 :      344688 :   else if (!copy_to_heap)
   17687                 :             :     {
   17688                 :             :       // The initializer will only run once.
   17689                 :      287079 :       space = Expression::make_unary(OPERATOR_AND, this->array_val_, loc);
   17690                 :      287079 :       space->unary_expression()->set_is_slice_init();
   17691                 :             :     }
   17692                 :             :   else
   17693                 :             :     {
   17694                 :       57609 :       go_assert(this->storage_escapes_ || this->element_count() == 0);
   17695                 :       57609 :       space = Expression::make_heap_expression(this->array_val_, loc);
   17696                 :             :     }
   17697                 :      377443 :   Array_type* at = this->valtype_->array_type();
   17698                 :      377443 :   Type* et = at->element_type();
   17699                 :      377443 :   space = Expression::make_unsafe_cast(Type::make_pointer_type(et),
   17700                 :             :                                        space, loc);
   17701                 :             : 
   17702                 :             :   // Build a constructor for the slice.
   17703                 :      377443 :   Expression* len = at->length();
   17704                 :      377443 :   Expression* slice_val =
   17705                 :      377443 :     Expression::make_slice_value(this->type(), space, len, len, loc);
   17706                 :      377443 :   return slice_val->get_backend(context);
   17707                 :             : }
   17708                 :             : 
   17709                 :             : // Make a slice composite literal.  This is used by the type
   17710                 :             : // descriptor code.
   17711                 :             : 
   17712                 :             : Slice_construction_expression*
   17713                 :      402776 : Expression::make_slice_composite_literal(Type* type, Expression_list* vals,
   17714                 :             :                                          Location location)
   17715                 :             : {
   17716                 :      402776 :   go_assert(type->is_slice_type());
   17717                 :      402776 :   return new Slice_construction_expression(type, NULL, vals, location);
   17718                 :             : }
   17719                 :             : 
   17720                 :             : // Class Map_construction_expression.
   17721                 :             : 
   17722                 :             : // Traversal.
   17723                 :             : 
   17724                 :             : int
   17725                 :       53169 : Map_construction_expression::do_traverse(Traverse* traverse)
   17726                 :             : {
   17727                 :       53169 :   if (this->vals_ != NULL
   17728                 :       53169 :       && this->vals_->traverse(traverse) == TRAVERSE_EXIT)
   17729                 :             :     return TRAVERSE_EXIT;
   17730                 :       52785 :   if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
   17731                 :             :     return TRAVERSE_EXIT;
   17732                 :             :   return TRAVERSE_CONTINUE;
   17733                 :             : }
   17734                 :             : 
   17735                 :             : void
   17736                 :        2433 : Map_construction_expression::do_determine_type(Gogo* gogo, const Type_context*)
   17737                 :             : {
   17738                 :        2433 :   Map_type* mt = this->type_->map_type();
   17739                 :           0 :   go_assert(mt != NULL);
   17740                 :        2433 :   Type_context key_context(mt->key_type(), false);
   17741                 :        2433 :   Type_context val_context(mt->val_type(), false);
   17742                 :        2433 :   if (this->vals_ != NULL)
   17743                 :             :     {
   17744                 :       29823 :       for (Expression_list::const_iterator pv = this->vals_->begin();
   17745                 :       29823 :            pv != this->vals_->end();
   17746                 :       27748 :            ++pv)
   17747                 :             :         {
   17748                 :       27748 :           (*pv)->determine_type(gogo, &key_context);
   17749                 :       27748 :           ++pv;
   17750                 :       27748 :           (*pv)->determine_type(gogo, &val_context);
   17751                 :             :         }
   17752                 :             :     }
   17753                 :        2433 : }
   17754                 :             : 
   17755                 :             : void
   17756                 :           0 : Map_construction_expression::do_check_types(Gogo*)
   17757                 :             : {
   17758                 :             :   // Types should have been checked before this was created, so this
   17759                 :             :   // will probably never be called.  Check it anyhow to be sure.
   17760                 :             : 
   17761                 :           0 :   if (this->vals_ == NULL)
   17762                 :             :     return;
   17763                 :             : 
   17764                 :           0 :   Map_type* mt = this->type_->map_type();
   17765                 :           0 :   go_assert(mt != NULL);
   17766                 :           0 :   Type* key_type = mt->key_type();
   17767                 :           0 :   Type* val_type = mt->val_type();
   17768                 :           0 :   for (Expression_list::const_iterator pv = this->vals_->begin();
   17769                 :           0 :        pv != this->vals_->end();
   17770                 :           0 :        ++pv)
   17771                 :             :     {
   17772                 :           0 :       if (!Type::are_assignable(key_type, (*pv)->type(), NULL))
   17773                 :             :         {
   17774                 :           0 :           go_error_at((*pv)->location(),
   17775                 :             :                       "incompatible type for key in map composite literal");
   17776                 :           0 :           this->set_is_error();
   17777                 :             :         }
   17778                 :           0 :       ++pv;
   17779                 :           0 :       if (!Type::are_assignable(val_type, (*pv)->type(), NULL))
   17780                 :             :         {
   17781                 :           0 :           go_error_at((*pv)->location(),
   17782                 :             :                       "incompatible type for value in map composite literal");
   17783                 :           0 :           this->set_is_error();
   17784                 :             :         }
   17785                 :             :     }
   17786                 :             : }
   17787                 :             : 
   17788                 :             : // Flatten constructor initializer into a temporary variable since
   17789                 :             : // we need to take its address for __go_construct_map.
   17790                 :             : 
   17791                 :             : Expression*
   17792                 :        3534 : Map_construction_expression::do_flatten(Gogo* gogo, Named_object*,
   17793                 :             :                                         Statement_inserter* inserter)
   17794                 :             : {
   17795                 :        3534 :   if (!this->is_error_expression()
   17796                 :        3534 :       && this->vals_ != NULL
   17797                 :        2672 :       && !this->vals_->empty()
   17798                 :        6206 :       && this->constructor_temp_ == NULL)
   17799                 :             :     {
   17800                 :        2638 :       Map_type* mt = this->type_->map_type();
   17801                 :        2638 :       Type* key_type = mt->key_type();
   17802                 :        2638 :       Type* val_type = mt->val_type();
   17803                 :        2638 :       this->element_type_ = Type::make_builtin_struct_type(2,
   17804                 :             :                                                            "__key", key_type,
   17805                 :             :                                                            "__val", val_type);
   17806                 :             : 
   17807                 :        2638 :       Expression_list* value_pairs = new Expression_list();
   17808                 :        2638 :       Location loc = this->location();
   17809                 :             : 
   17810                 :        2638 :       size_t i = 0;
   17811                 :        2638 :       for (Expression_list::const_iterator pv = this->vals_->begin();
   17812                 :       32491 :            pv != this->vals_->end();
   17813                 :       29853 :            ++pv, ++i)
   17814                 :             :         {
   17815                 :       29853 :           Expression_list* key_value_pair = new Expression_list();
   17816                 :       29853 :           Expression* key = *pv;
   17817                 :       59706 :           if (key->is_error_expression() || key->type()->is_error_type())
   17818                 :             :             {
   17819                 :           0 :               go_assert(saw_errors());
   17820                 :           0 :               return Expression::make_error(loc);
   17821                 :             :             }
   17822                 :       29853 :           if (key->type()->interface_type() != NULL
   17823                 :         148 :               && !key->is_multi_eval_safe())
   17824                 :             :             {
   17825                 :          44 :               Temporary_statement* temp =
   17826                 :          44 :                 Statement::make_temporary(NULL, key, loc);
   17827                 :          44 :               inserter->insert(temp);
   17828                 :          44 :               key = Expression::make_temporary_reference(temp, loc);
   17829                 :             :             }
   17830                 :       29853 :           key = Expression::convert_for_assignment(gogo, key_type, key, loc);
   17831                 :             : 
   17832                 :       29853 :           ++pv;
   17833                 :       29853 :           Expression* val = *pv;
   17834                 :       59706 :           if (val->is_error_expression() || val->type()->is_error_type())
   17835                 :             :             {
   17836                 :           0 :               go_assert(saw_errors());
   17837                 :           0 :               return Expression::make_error(loc);
   17838                 :             :             }
   17839                 :       29853 :           if (val->type()->interface_type() != NULL
   17840                 :         501 :               && !val->is_multi_eval_safe())
   17841                 :             :             {
   17842                 :         497 :               Temporary_statement* temp =
   17843                 :         497 :                 Statement::make_temporary(NULL, val, loc);
   17844                 :         497 :               inserter->insert(temp);
   17845                 :         497 :               val = Expression::make_temporary_reference(temp, loc);
   17846                 :             :             }
   17847                 :       29853 :           val = Expression::convert_for_assignment(gogo, val_type, val, loc);
   17848                 :             : 
   17849                 :       29853 :           key_value_pair->push_back(key);
   17850                 :       29853 :           key_value_pair->push_back(val);
   17851                 :       29853 :           value_pairs->push_back(
   17852                 :       29853 :               Expression::make_struct_composite_literal(this->element_type_,
   17853                 :             :                                                         key_value_pair, loc));
   17854                 :             :         }
   17855                 :             : 
   17856                 :        2638 :       Expression* element_count = Expression::make_integer_ul(i, NULL, loc);
   17857                 :        2638 :       Array_type* ctor_type =
   17858                 :        2638 :           Type::make_array_type(this->element_type_, element_count);
   17859                 :        2638 :       ctor_type->set_is_array_incomparable();
   17860                 :        2638 :       Expression* constructor =
   17861                 :             :           new Fixed_array_construction_expression(ctor_type, NULL,
   17862                 :        2638 :                                                   value_pairs, loc);
   17863                 :             : 
   17864                 :        5276 :       this->constructor_temp_ =
   17865                 :        2638 :           Statement::make_temporary(NULL, constructor, loc);
   17866                 :        2638 :       constructor->issue_nil_check();
   17867                 :        2638 :       this->constructor_temp_->set_is_address_taken();
   17868                 :        2638 :       this->constructor_temp_->determine_types(gogo);
   17869                 :        2638 :       inserter->insert(this->constructor_temp_);
   17870                 :             :     }
   17871                 :             : 
   17872                 :        3534 :   return this;
   17873                 :             : }
   17874                 :             : 
   17875                 :             : // Copy.
   17876                 :             : 
   17877                 :             : Expression*
   17878                 :           0 : Map_construction_expression::do_copy()
   17879                 :             : {
   17880                 :           0 :   return new Map_construction_expression(this->type_->copy_expressions(),
   17881                 :           0 :                                          (this->vals_ == NULL
   17882                 :             :                                           ? NULL
   17883                 :           0 :                                           : this->vals_->copy()),
   17884                 :           0 :                                          this->location());
   17885                 :             : }
   17886                 :             : 
   17887                 :             : // Make implicit type conversions explicit.
   17888                 :             : 
   17889                 :             : void
   17890                 :        3481 : Map_construction_expression::do_add_conversions()
   17891                 :             : {
   17892                 :        3481 :   if (this->vals_ == NULL || this->vals_->empty())
   17893                 :        3336 :     return;
   17894                 :             : 
   17895                 :        2638 :   Map_type* mt = this->type_->map_type();
   17896                 :        2638 :   Type* kt = mt->key_type();
   17897                 :        2638 :   Type* vt = mt->val_type();
   17898                 :        2638 :   bool key_is_interface = (kt->interface_type() != NULL);
   17899                 :        2638 :   bool val_is_interface = (vt->interface_type() != NULL);
   17900                 :        2638 :   if (!key_is_interface && !val_is_interface)
   17901                 :             :     return;
   17902                 :             : 
   17903                 :         145 :   Location loc = this->location();
   17904                 :         790 :   for (Expression_list::iterator pv = this->vals_->begin();
   17905                 :         790 :        pv != this->vals_->end();
   17906                 :         645 :        ++pv)
   17907                 :             :     {
   17908                 :         793 :       if (key_is_interface &&
   17909                 :         148 :           !Type::are_identical(kt, (*pv)->type(), 0, NULL))
   17910                 :          44 :         *pv = Expression::make_cast(kt, *pv, loc);
   17911                 :         645 :       ++pv;
   17912                 :        1146 :       if (val_is_interface &&
   17913                 :         501 :           !Type::are_identical(vt, (*pv)->type(), 0, NULL))
   17914                 :         493 :         *pv = Expression::make_cast(vt, *pv, loc);
   17915                 :             :     }
   17916                 :             : }
   17917                 :             : 
   17918                 :             : // Return the backend representation for constructing a map.
   17919                 :             : 
   17920                 :             : Bexpression*
   17921                 :        3481 : Map_construction_expression::do_get_backend(Translate_context* context)
   17922                 :             : {
   17923                 :        3481 :   if (this->is_error_expression())
   17924                 :           0 :     return context->backend()->error_expression();
   17925                 :        3481 :   Location loc = this->location();
   17926                 :             : 
   17927                 :        3481 :   size_t i = 0;
   17928                 :        3481 :   Expression* ventries;
   17929                 :        3481 :   if (this->vals_ == NULL || this->vals_->empty())
   17930                 :         843 :     ventries = Expression::make_nil(loc);
   17931                 :             :   else
   17932                 :             :     {
   17933                 :        2638 :       go_assert(this->constructor_temp_ != NULL);
   17934                 :        2638 :       i = this->vals_->size() / 2;
   17935                 :             : 
   17936                 :        2638 :       Expression* ctor_ref =
   17937                 :        2638 :           Expression::make_temporary_reference(this->constructor_temp_, loc);
   17938                 :        2638 :       ventries = Expression::make_unary(OPERATOR_AND, ctor_ref, loc);
   17939                 :             :     }
   17940                 :             : 
   17941                 :        3481 :   Map_type* mt = this->type_->map_type();
   17942                 :        3481 :   if (this->element_type_ == NULL)
   17943                 :         843 :       this->element_type_ =
   17944                 :         843 :           Type::make_builtin_struct_type(2,
   17945                 :             :                                          "__key", mt->key_type(),
   17946                 :             :                                          "__val", mt->val_type());
   17947                 :        3481 :   Expression* descriptor = Expression::make_type_descriptor(mt, loc);
   17948                 :             : 
   17949                 :        3481 :   Type* uintptr_t = Type::lookup_integer_type("uintptr");
   17950                 :        3481 :   Expression* count = Expression::make_integer_ul(i, uintptr_t, loc);
   17951                 :             : 
   17952                 :        3481 :   Expression* entry_size =
   17953                 :        3481 :       Expression::make_type_info(this->element_type_, TYPE_INFO_SIZE);
   17954                 :             : 
   17955                 :        3481 :   unsigned int field_index;
   17956                 :        3481 :   const Struct_field* valfield =
   17957                 :        3481 :       this->element_type_->find_local_field("__val", &field_index);
   17958                 :        3481 :   Expression* val_offset =
   17959                 :        3481 :       Expression::make_struct_field_offset(this->element_type_, valfield);
   17960                 :             : 
   17961                 :        3481 :   Gogo* gogo = context->gogo();
   17962                 :        3481 :   Expression* map_ctor =
   17963                 :        3481 :     Runtime::make_call(gogo, Runtime::CONSTRUCT_MAP, loc, 5,
   17964                 :             :                        descriptor, count, entry_size, val_offset, ventries);
   17965                 :        3481 :   map_ctor->determine_type_no_context(gogo);
   17966                 :        3481 :   return map_ctor->get_backend(context);
   17967                 :             : }
   17968                 :             : 
   17969                 :             : // Export an array construction.
   17970                 :             : 
   17971                 :             : void
   17972                 :           0 : Map_construction_expression::do_export(Export_function_body* efb) const
   17973                 :             : {
   17974                 :           0 :   efb->write_c_string("$convert(");
   17975                 :           0 :   efb->write_type(this->type_);
   17976                 :           0 :   for (Expression_list::const_iterator pv = this->vals_->begin();
   17977                 :           0 :        pv != this->vals_->end();
   17978                 :           0 :        ++pv)
   17979                 :             :     {
   17980                 :           0 :       efb->write_c_string(", ");
   17981                 :           0 :       (*pv)->export_expression(efb);
   17982                 :             :     }
   17983                 :           0 :   efb->write_c_string(")");
   17984                 :           0 : }
   17985                 :             : 
   17986                 :             : // Dump ast representation for a map construction expression.
   17987                 :             : 
   17988                 :             : void
   17989                 :           0 : Map_construction_expression::do_dump_expression(
   17990                 :             :     Ast_dump_context* ast_dump_context) const
   17991                 :             : {
   17992                 :           0 :   ast_dump_context->ostream() << "{" ;
   17993                 :           0 :   ast_dump_context->dump_expression_list(this->vals_, true);
   17994                 :           0 :   ast_dump_context->ostream() << "}";
   17995                 :           0 : }
   17996                 :             : 
   17997                 :             : // A composite literal key.  This is seen during parsing, but is not
   17998                 :             : // resolved to a named_object in case this is a composite literal of
   17999                 :             : // struct type.
   18000                 :             : 
   18001                 :             : class Composite_literal_key_expression : public Parser_expression
   18002                 :             : {
   18003                 :             :  public:
   18004                 :      115521 :   Composite_literal_key_expression(const std::string& name, Location location)
   18005                 :      115521 :     : Parser_expression(EXPRESSION_COMPOSITE_LITERAL_KEY, location),
   18006                 :      115521 :       name_(name), expr_(NULL)
   18007                 :      115521 :   { }
   18008                 :             : 
   18009                 :             :   const std::string&
   18010                 :      114982 :   name() const
   18011                 :      114982 :   { return this->name_; }
   18012                 :             : 
   18013                 :             :  protected:
   18014                 :             :   Type*
   18015                 :             :   do_type();
   18016                 :             : 
   18017                 :             :   void
   18018                 :             :   do_determine_type(Gogo*, const Type_context*);
   18019                 :             : 
   18020                 :             :   Expression*
   18021                 :             :   do_lower(Gogo*, Named_object*, Statement_inserter*);
   18022                 :             : 
   18023                 :             :   Expression*
   18024                 :           0 :   do_copy()
   18025                 :             :   {
   18026                 :           0 :     return new Composite_literal_key_expression(this->name_, this->location());
   18027                 :             :   }
   18028                 :             : 
   18029                 :             :   void
   18030                 :             :   do_dump_expression(Ast_dump_context*) const;
   18031                 :             : 
   18032                 :             :  private:
   18033                 :             :   // The name.
   18034                 :             :   std::string name_;
   18035                 :             :   // After we look up the name, a corresponding expression.
   18036                 :             :   Expression* expr_;
   18037                 :             : };
   18038                 :             : 
   18039                 :             : // Determine the type of a composite literal key.  We will never get
   18040                 :             : // here for keys in composite literals of struct types, because they
   18041                 :             : // will be handled by Composite_literal_expression::do_determine_type.
   18042                 :             : // So if we get here, this must be a regular name reference after all.
   18043                 :             : 
   18044                 :             : void
   18045                 :         529 : Composite_literal_key_expression::do_determine_type(
   18046                 :             :     Gogo* gogo,
   18047                 :             :     const Type_context* context)
   18048                 :             : {
   18049                 :         529 :   if (this->expr_ != NULL)
   18050                 :             :     {
   18051                 :             :       // Already resolved.
   18052                 :             :       return;
   18053                 :             :     }
   18054                 :             : 
   18055                 :         529 :   Named_object* no = gogo->lookup(this->name_, NULL);
   18056                 :         529 :   if (no == NULL)
   18057                 :             :     {
   18058                 :             :       // Gogo::lookup doesn't look in the global namespace, and names
   18059                 :             :       // used in composite literal keys aren't seen by
   18060                 :             :       // Gogo::define_global_names, so we have to look in the global
   18061                 :             :       // namespace ourselves.
   18062                 :           9 :       no = gogo->lookup_global(Gogo::unpack_hidden_name(this->name_).c_str());
   18063                 :           9 :       if (no == NULL)
   18064                 :             :         {
   18065                 :           1 :           go_error_at(this->location(), "reference to undefined name %qs",
   18066                 :           1 :                       Gogo::message_name(this->name_).c_str());
   18067                 :           1 :           this->set_is_error();
   18068                 :           1 :           return;
   18069                 :             :         }
   18070                 :             :     }
   18071                 :             : 
   18072                 :         528 :   this->expr_ = Expression::make_unknown_reference(no, this->location());
   18073                 :         528 :   this->expr_->determine_type(gogo, context);
   18074                 :             : }
   18075                 :             : 
   18076                 :             : Type*
   18077                 :         369 : Composite_literal_key_expression::do_type()
   18078                 :             : {
   18079                 :         369 :   if (this->is_error_expression())
   18080                 :           0 :     return Type::make_error_type();
   18081                 :         369 :   go_assert(this->expr_ != NULL);
   18082                 :         369 :   return this->expr_->type();
   18083                 :             : }
   18084                 :             : 
   18085                 :             : Expression*
   18086                 :         529 : Composite_literal_key_expression::do_lower(Gogo*, Named_object*,
   18087                 :             :                                            Statement_inserter*)
   18088                 :             : {
   18089                 :         529 :   if (this->is_error_expression())
   18090                 :           1 :     return Expression::make_error(this->location());
   18091                 :         528 :   go_assert(this->expr_ != NULL);
   18092                 :             :   return this->expr_;
   18093                 :             : }
   18094                 :             : 
   18095                 :             : // Dump a composite literal key.
   18096                 :             : 
   18097                 :             : void
   18098                 :           0 : Composite_literal_key_expression::do_dump_expression(
   18099                 :             :     Ast_dump_context* ast_dump_context) const
   18100                 :             : {
   18101                 :           0 :   ast_dump_context->ostream() << "_UnknownName_(" << this->name_ << ")";
   18102                 :           0 : }
   18103                 :             : 
   18104                 :             : // Make a composite literal key.
   18105                 :             : 
   18106                 :             : Expression*
   18107                 :      115521 : Expression::make_composite_literal_key(const std::string& name,
   18108                 :             :                                        Location location)
   18109                 :             : {
   18110                 :      115521 :   return new Composite_literal_key_expression(name, location);
   18111                 :             : }
   18112                 :             : 
   18113                 :             : // Class Composite_literal_expression.
   18114                 :             : 
   18115                 :             : // Traversal.
   18116                 :             : 
   18117                 :             : int
   18118                 :     1146569 : Composite_literal_expression::do_traverse(Traverse* traverse)
   18119                 :             : {
   18120                 :     1146569 :   if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
   18121                 :             :     return TRAVERSE_EXIT;
   18122                 :             : 
   18123                 :             :   // If this is a struct composite literal with keys, then the keys
   18124                 :             :   // are field names, not expressions.  We don't want to traverse them
   18125                 :             :   // in that case.  If we do, we can give an erroneous error "variable
   18126                 :             :   // initializer refers to itself."  See bug482.go in the testsuite.
   18127                 :     1146569 :   if (this->has_keys_ && this->vals_ != NULL)
   18128                 :             :     {
   18129                 :             :       // The type may not be resolvable at this point.
   18130                 :      108128 :       Type* type = this->type_;
   18131                 :             : 
   18132                 :      158074 :       for (int depth = 0; depth < this->depth_; ++depth)
   18133                 :             :         {
   18134                 :       49951 :           type = type->deref();
   18135                 :       49951 :           if (type->array_type() != NULL)
   18136                 :       96928 :             type = type->array_type()->element_type();
   18137                 :      159689 :           else if (type->map_type() != NULL)
   18138                 :             :             {
   18139                 :        1482 :               if (this->key_path_[depth])
   18140                 :          12 :                 type = type->map_type()->key_type();
   18141                 :             :               else
   18142                 :        2952 :                 type = type->map_type()->val_type();
   18143                 :             :             }
   18144                 :             :           else
   18145                 :             :             {
   18146                 :             :               // This error will be reported during lowering.
   18147                 :             :               return TRAVERSE_CONTINUE;
   18148                 :             :             }
   18149                 :             :         }
   18150                 :      108123 :       type = type->deref();
   18151                 :             : 
   18152                 :      239712 :       while (true)
   18153                 :             :         {
   18154                 :      239712 :           if (type->classification() == Type::TYPE_NAMED)
   18155                 :       86409 :             type = type->named_type()->real_type();
   18156                 :      153303 :           else if (type->classification() == Type::TYPE_FORWARD)
   18157                 :             :             {
   18158                 :       45180 :               Type* t = type->forwarded();
   18159                 :       45180 :               if (t == type)
   18160                 :             :                 break;
   18161                 :             :               type = t;
   18162                 :             :             }
   18163                 :             :           else
   18164                 :             :             break;
   18165                 :             :         }
   18166                 :             : 
   18167                 :      108123 :       if (type->classification() == Type::TYPE_STRUCT)
   18168                 :             :         {
   18169                 :       92452 :           Expression_list::iterator p = this->vals_->begin();
   18170                 :      346938 :           while (p != this->vals_->end())
   18171                 :             :             {
   18172                 :             :               // Skip key.
   18173                 :      254486 :               ++p;
   18174                 :      254486 :               go_assert(p != this->vals_->end());
   18175                 :      254486 :               if (Expression::traverse(&*p, traverse) == TRAVERSE_EXIT)
   18176                 :             :                 return TRAVERSE_EXIT;
   18177                 :      254486 :               ++p;
   18178                 :             :             }
   18179                 :             :           return TRAVERSE_CONTINUE;
   18180                 :             :         }
   18181                 :             :     }
   18182                 :             : 
   18183                 :     1054112 :   if (this->vals_ != NULL)
   18184                 :      988367 :     return this->vals_->traverse(traverse);
   18185                 :             : 
   18186                 :             :   return TRAVERSE_CONTINUE;
   18187                 :             : }
   18188                 :             : 
   18189                 :             : void
   18190                 :      233460 : Composite_literal_expression::do_determine_type(Gogo* gogo,
   18191                 :             :                                                 const Type_context*)
   18192                 :             : {
   18193                 :             :   // Resolve the type if this composite literal is within another
   18194                 :             :   // composite literal and has omitted the type.  The DEPTH_ field
   18195                 :             :   // tells us how deeply this one is embedded.
   18196                 :      233460 :   Type* type = this->type_;
   18197                 :      412341 :   for (int depth = 0; depth < this->depth_; ++depth)
   18198                 :             :     {
   18199                 :      178885 :       type = type->deref();
   18200                 :      178885 :       if (type->array_type() != NULL)
   18201                 :      349558 :         type = type->array_type()->element_type();
   18202                 :        4106 :       else if (type->map_type() != NULL)
   18203                 :             :         {
   18204                 :        4102 :           if (this->key_path_[depth])
   18205                 :          70 :             type = type->map_type()->key_type();
   18206                 :             :           else
   18207                 :        8134 :             type = type->map_type()->val_type();
   18208                 :             :         }
   18209                 :             :       else
   18210                 :             :         {
   18211                 :           4 :           if (!type->is_error())
   18212                 :           4 :             this->report_error(_("may only omit types within composite "
   18213                 :             :                                  "literals of slice, array, or map type"));
   18214                 :             :           else
   18215                 :             :             {
   18216                 :           0 :               go_assert(saw_errors());
   18217                 :           0 :               this->set_is_error();
   18218                 :             :             }
   18219                 :           4 :           return;
   18220                 :             :         }
   18221                 :             :     }
   18222                 :             : 
   18223                 :      233456 :   this->type_ = type;
   18224                 :      233456 :   this->depth_ = 0;
   18225                 :             : 
   18226                 :      233456 :   type = type->deref();
   18227                 :             : 
   18228                 :      233456 :   if (this->vals_ == NULL || this->vals_->empty())
   18229                 :             :     {
   18230                 :             :       // No value types to determine.
   18231                 :       17677 :       if (type->array_type() != NULL
   18232                 :        4059 :           && type->array_type()->length() != NULL
   18233                 :         577 :           && type->array_type()->length()->is_nil_expression())
   18234                 :           2 :         this->resolve_array_length(type);
   18235                 :       13618 :       return;
   18236                 :             :     }
   18237                 :             : 
   18238                 :      390165 :   if (type->struct_type() != NULL && this->has_keys_)
   18239                 :             :     {
   18240                 :             :       // Rewrite the value list by removing the struct field keys.  We
   18241                 :             :       // do this now rather than during lowering because handling
   18242                 :             :       // struct keys is painful.  We don't need to do this for
   18243                 :             :       // slice/array/map literals, because it's easy to determine
   18244                 :             :       // their types with or without the keys.
   18245                 :       45900 :       if (!this->resolve_struct_keys(gogo, type))
   18246                 :             :         {
   18247                 :          19 :           this->set_is_error();
   18248                 :          38 :           delete this->vals_;
   18249                 :          19 :           this->vals_ = NULL;
   18250                 :          19 :           this->has_keys_ = false;
   18251                 :          19 :           return;
   18252                 :             :         }
   18253                 :             :     }
   18254                 :             : 
   18255                 :      219819 :   if (type->struct_type() != NULL)
   18256                 :             :     {
   18257                 :      340616 :       const Struct_field_list* fields = type->struct_type()->fields();
   18258                 :      170308 :       Expression_list::const_iterator pv = this->vals_->begin();
   18259                 :      170308 :       for (Struct_field_list::const_iterator pf = fields->begin();
   18260                 :      747385 :            pf != fields->end();
   18261                 :      577077 :            ++pf, ++pv)
   18262                 :             :         {
   18263                 :      577080 :           if (pv == this->vals_->end())
   18264                 :      233460 :             return;
   18265                 :      577077 :           if (*pv != NULL)
   18266                 :             :             {
   18267                 :      480394 :               Type_context subcontext(pf->type(), false);
   18268                 :      480394 :               (*pv)->determine_type(gogo, &subcontext);
   18269                 :             :             }
   18270                 :             :         }
   18271                 :             :       // Extra values are an error we will report in the check_types
   18272                 :             :       // pass; we still want to determine the type to avoid knockon
   18273                 :             :       // errors.
   18274                 :      170313 :       for (; pv != this->vals_->end(); ++pv)
   18275                 :           8 :         (*pv)->determine_type_no_context(gogo);
   18276                 :             :     }
   18277                 :       49511 :   else if (type->array_type() != NULL)
   18278                 :             :     {
   18279                 :       46824 :       Array_type* at = type->array_type();
   18280                 :       46824 :       Type_context intcontext(Type::lookup_integer_type("int"), false);
   18281                 :       46824 :       Type_context subcontext(at->element_type(), false);
   18282                 :      652101 :       for (Expression_list::const_iterator pv = this->vals_->begin();
   18283                 :      652101 :            pv != this->vals_->end();
   18284                 :      605277 :            ++pv)
   18285                 :             :         {
   18286                 :      605277 :           if (this->has_keys_)
   18287                 :             :             {
   18288                 :      110423 :               if (*pv != NULL)
   18289                 :      110400 :                 (*pv)->determine_type(gogo, &intcontext);
   18290                 :      110423 :               ++pv;
   18291                 :      110423 :               if (pv == this->vals_->end())
   18292                 :             :                 break;
   18293                 :             :             }
   18294                 :             : 
   18295                 :      605277 :           if (*pv != NULL)
   18296                 :      605277 :             (*pv)->determine_type(gogo, &subcontext);
   18297                 :             :         }
   18298                 :             : 
   18299                 :       46824 :       if (at->length() != NULL && at->length()->is_nil_expression())
   18300                 :         867 :         this->resolve_array_length(type);
   18301                 :             :     }
   18302                 :        2687 :   else if (type->map_type() != NULL)
   18303                 :             :     {
   18304                 :        2685 :       if (!this->has_keys_)
   18305                 :             :         {
   18306                 :           0 :           this->report_error(_("map composite literal must have keys"));
   18307                 :           0 :           return;
   18308                 :             :         }
   18309                 :             : 
   18310                 :        2685 :       Map_type* mt = type->map_type();
   18311                 :        2685 :       Type_context key_context(mt->key_type(), false);
   18312                 :        2685 :       Type_context val_context(mt->val_type(), false);
   18313                 :       32763 :       for (Expression_list::const_iterator pv = this->vals_->begin();
   18314                 :       32763 :            pv != this->vals_->end();
   18315                 :       30078 :            ++pv)
   18316                 :             :         {
   18317                 :       30078 :           if (*pv != NULL)
   18318                 :       30076 :             (*pv)->determine_type(gogo, &key_context);
   18319                 :       30078 :           ++pv;
   18320                 :       30078 :           if (*pv != NULL)
   18321                 :       30078 :             (*pv)->determine_type(gogo, &val_context);
   18322                 :             :         }
   18323                 :             :     }
   18324                 :             :   else
   18325                 :             :     {
   18326                 :             :       // We'll report this as an error in the check_types pass.
   18327                 :             :       // Determine types to avoid knockon errors.
   18328                 :           6 :       for (Expression_list::const_iterator pv = this->vals_->begin();
   18329                 :           6 :            pv != this->vals_->end();
   18330                 :           4 :            ++pv)
   18331                 :             :         {
   18332                 :           4 :           if (*pv != NULL)
   18333                 :           4 :             (*pv)->determine_type_no_context(gogo);
   18334                 :             :         }
   18335                 :             :     }
   18336                 :             : }
   18337                 :             : 
   18338                 :             : Type*
   18339                 :      257216 : Composite_literal_expression::do_type()
   18340                 :             : {
   18341                 :      257216 :   if (this->is_error_expression())
   18342                 :          38 :     return Type::make_error_type();
   18343                 :      257178 :   go_assert(this->depth_ == 0);
   18344                 :      257178 :   return this->type_;
   18345                 :             : }
   18346                 :             : 
   18347                 :             : // Resolve the field keys of a struct composite literal.
   18348                 :             : 
   18349                 :             : bool
   18350                 :       45900 : Composite_literal_expression::resolve_struct_keys(Gogo* gogo, Type* type)
   18351                 :             : {
   18352                 :       45900 :   Struct_type* st = type->struct_type();
   18353                 :       45900 :   size_t field_count = st->field_count();
   18354                 :       45900 :   std::vector<Expression*> vals(field_count);
   18355                 :       45900 :   std::vector<unsigned long>* traverse_order = new(std::vector<unsigned long>);
   18356                 :       45900 :   Expression_list::const_iterator p = this->vals_->begin();
   18357                 :       45900 :   Expression* external_expr = NULL;
   18358                 :       45900 :   const Named_object* external_no = NULL;
   18359                 :      171820 :   while (p != this->vals_->end())
   18360                 :             :     {
   18361                 :      125938 :       Expression* name_expr = *p;
   18362                 :             : 
   18363                 :      125938 :       ++p;
   18364                 :      125938 :       go_assert(p != this->vals_->end());
   18365                 :      125938 :       Expression* val = *p;
   18366                 :             : 
   18367                 :      125938 :       ++p;
   18368                 :             : 
   18369                 :      125938 :       if (name_expr == NULL)
   18370                 :             :         {
   18371                 :           1 :           go_error_at(val->location(),
   18372                 :             :                       "mixture of field and value initializers");
   18373                 :          18 :           return false;
   18374                 :             :         }
   18375                 :             : 
   18376                 :      125937 :       bool bad_key = false;
   18377                 :      125937 :       std::string name;
   18378                 :      125937 :       const Named_object* no = NULL;
   18379                 :      125937 :       switch (name_expr->classification())
   18380                 :             :         {
   18381                 :      114982 :         case EXPRESSION_COMPOSITE_LITERAL_KEY:
   18382                 :      114982 :           name =
   18383                 :      114982 :             static_cast<Composite_literal_key_expression*>(name_expr)->name();
   18384                 :      114982 :           break;
   18385                 :             : 
   18386                 :        5232 :         case EXPRESSION_UNKNOWN_REFERENCE:
   18387                 :        5232 :           name = name_expr->unknown_expression()->name();
   18388                 :        5232 :           if (type->named_type() != NULL)
   18389                 :             :             {
   18390                 :             :               // If the named object found for this field name comes from a
   18391                 :             :               // different package than the struct it is a part of, do not count
   18392                 :             :               // this incorrect lookup as a usage of the object's package.
   18393                 :        5042 :               no = name_expr->unknown_expression()->named_object();
   18394                 :        5042 :               if (no->package() != NULL
   18395                 :        5419 :                   && (no->package()
   18396                 :         377 :                       != type->named_type()->named_object()->package()))
   18397                 :          39 :                 no->package()->forget_usage(name_expr);
   18398                 :             :             }
   18399                 :             :           break;
   18400                 :             : 
   18401                 :         111 :         case EXPRESSION_CONST_REFERENCE:
   18402                 :         111 :           no = static_cast<Const_expression*>(name_expr)->named_object();
   18403                 :         111 :           break;
   18404                 :             : 
   18405                 :           1 :         case EXPRESSION_TYPE:
   18406                 :           1 :           {
   18407                 :           1 :             Type* t = name_expr->type();
   18408                 :           1 :             Named_type* nt = t->named_type();
   18409                 :           1 :             if (nt == NULL)
   18410                 :             :               bad_key = true;
   18411                 :             :             else
   18412                 :           1 :               no = nt->named_object();
   18413                 :             :           }
   18414                 :             :           break;
   18415                 :             : 
   18416                 :        5185 :         case EXPRESSION_VAR_REFERENCE:
   18417                 :        5185 :           no = name_expr->var_expression()->named_object();
   18418                 :        5185 :           break;
   18419                 :             : 
   18420                 :          91 :         case EXPRESSION_ENCLOSED_VAR_REFERENCE:
   18421                 :          91 :           no = name_expr->enclosed_var_expression()->variable();
   18422                 :          91 :           break;
   18423                 :             : 
   18424                 :         331 :         case EXPRESSION_FUNC_REFERENCE:
   18425                 :         331 :           no = name_expr->func_expression()->named_object();
   18426                 :         331 :           break;
   18427                 :             : 
   18428                 :             :         default:
   18429                 :             :           bad_key = true;
   18430                 :             :           break;
   18431                 :             :         }
   18432                 :      120740 :       if (bad_key)
   18433                 :             :         {
   18434                 :           4 :           go_error_at(name_expr->location(), "expected struct field name");
   18435                 :           4 :           return false;
   18436                 :             :         }
   18437                 :             : 
   18438                 :      125933 :       if (no != NULL)
   18439                 :             :         {
   18440                 :       10761 :           if (no->package() != NULL && external_expr == NULL)
   18441                 :             :             {
   18442                 :       10761 :               external_expr = name_expr;
   18443                 :       10761 :               external_no = no;
   18444                 :             :             }
   18445                 :             : 
   18446                 :       10761 :           name = no->name();
   18447                 :             : 
   18448                 :             :           // A predefined name won't be packed.  If it starts with a
   18449                 :             :           // lower case letter we need to check for that case, because
   18450                 :             :           // the field name will be packed.  FIXME.
   18451                 :       10761 :           if (!Gogo::is_hidden_name(name)
   18452                 :        4447 :               && name[0] >= 'a'
   18453                 :       10761 :               && name[0] <= 'z')
   18454                 :             :             {
   18455                 :           0 :               Named_object* gno = gogo->lookup_global(name.c_str());
   18456                 :           0 :               if (gno == no)
   18457                 :           0 :                 name = gogo->pack_hidden_name(name, false);
   18458                 :             :             }
   18459                 :             :         }
   18460                 :             : 
   18461                 :      125933 :       unsigned int index;
   18462                 :      125933 :       const Struct_field* sf = st->find_local_field(name, &index);
   18463                 :      125933 :       if (sf == NULL)
   18464                 :             :         {
   18465                 :          35 :           go_error_at(name_expr->location(), "unknown field %qs in %qs",
   18466                 :          12 :                       Gogo::message_name(name).c_str(),
   18467                 :          12 :                       (type->named_type() != NULL
   18468                 :          34 :                        ? type->named_type()->message_name().c_str()
   18469                 :             :                        : "unnamed struct"));
   18470                 :          12 :           return false;
   18471                 :             :         }
   18472                 :      125921 :       if (vals[index] != NULL)
   18473                 :             :         {
   18474                 :           3 :           go_error_at(name_expr->location(),
   18475                 :             :                       "duplicate value for field %qs in %qs",
   18476                 :           1 :                       Gogo::message_name(name).c_str(),
   18477                 :           1 :                       (type->named_type() != NULL
   18478                 :           3 :                        ? type->named_type()->message_name().c_str()
   18479                 :             :                        : "unnamed struct"));
   18480                 :           1 :           return false;
   18481                 :             :         }
   18482                 :             : 
   18483                 :      125920 :       vals[index] = val;
   18484                 :      125920 :       traverse_order->push_back(static_cast<unsigned long>(index));
   18485                 :      125937 :     }
   18486                 :             : 
   18487                 :       45882 :   if (!this->all_are_names_)
   18488                 :             :     {
   18489                 :             :       // This is a weird case like bug462 in the testsuite.
   18490                 :           1 :       if (external_expr == NULL)
   18491                 :           0 :         go_error_at(this->location(), "unknown field in %qs literal",
   18492                 :           0 :                     (type->named_type() != NULL
   18493                 :           0 :                      ? type->named_type()->message_name().c_str()
   18494                 :             :                      : "unnamed struct"));
   18495                 :             :       else
   18496                 :           3 :         go_error_at(external_expr->location(), "unknown field %qs in %qs",
   18497                 :           1 :                     external_no->message_name().c_str(),
   18498                 :           1 :                     (type->named_type() != NULL
   18499                 :           3 :                      ? type->named_type()->message_name().c_str()
   18500                 :             :                      : "unnamed struct"));
   18501                 :           1 :       return false;
   18502                 :             :     }
   18503                 :             : 
   18504                 :       45881 :   Expression_list* list = new Expression_list;
   18505                 :       45881 :   list->reserve(field_count);
   18506                 :      267738 :   for (size_t i = 0; i < field_count; ++i)
   18507                 :      221857 :     list->push_back(vals[i]);
   18508                 :             : 
   18509                 :       45881 :   this->vals_ = list;
   18510                 :       45881 :   this->traverse_order_ = traverse_order;
   18511                 :       45881 :   this->has_keys_ = false;
   18512                 :             : 
   18513                 :       45881 :   return true;
   18514                 :       45900 : }
   18515                 :             : 
   18516                 :             : // Handle [...]{...}
   18517                 :             : 
   18518                 :             : void
   18519                 :         869 : Composite_literal_expression::resolve_array_length(Type* type)
   18520                 :             : {
   18521                 :         869 :   size_t size;
   18522                 :         869 :   if (this->vals_ == NULL || this->vals_->empty())
   18523                 :             :     size = 0;
   18524                 :         867 :   else if (!this->has_keys_)
   18525                 :         690 :     size = this->vals_->size();
   18526                 :             :   else
   18527                 :             :     {
   18528                 :             :       unsigned long index = 0;
   18529                 :             :       size = 0;
   18530                 :        2588 :       for (Expression_list::const_iterator pv = this->vals_->begin();
   18531                 :        2765 :            pv != this->vals_->end();
   18532                 :        2588 :            pv += 2)
   18533                 :             :         {
   18534                 :        2590 :           Expression* index_expr = *pv;
   18535                 :        2590 :           if (index_expr != NULL)
   18536                 :             :             {
   18537                 :        2583 :               Numeric_constant nc;
   18538                 :        2583 :               unsigned long iv;
   18539                 :        2583 :               if (!index_expr->numeric_constant_value(&nc)
   18540                 :        2583 :                   || nc.to_unsigned_long(&iv) != Numeric_constant::NC_UL_VALID)
   18541                 :             :                 {
   18542                 :             :                   // We will report an error when lowering.
   18543                 :             :                   break;
   18544                 :             :                 }
   18545                 :        2581 :               index = iv;
   18546                 :        2583 :             }
   18547                 :             : 
   18548                 :        2588 :           if (index >= size)
   18549                 :        2553 :             size = index + 1;
   18550                 :             : 
   18551                 :        2588 :           ++index;
   18552                 :             :         }
   18553                 :             :     }
   18554                 :             : 
   18555                 :         869 :   Expression* elen = Expression::make_integer_ul(size, NULL, this->location());
   18556                 :        1738 :   this->type_ = Type::make_array_type(type->array_type()->element_type(),
   18557                 :             :                                       elen);
   18558                 :         869 : }
   18559                 :             : 
   18560                 :             : void
   18561                 :      226843 : Composite_literal_expression::do_check_types(Gogo* gogo)
   18562                 :             : {
   18563                 :      226843 :   if (this->is_error_expression() || this->type_->is_error())
   18564                 :             :     {
   18565                 :          28 :       go_assert(saw_errors());
   18566                 :             :       return;
   18567                 :             :     }
   18568                 :      226815 :   go_assert(this->depth_ == 0);
   18569                 :             : 
   18570                 :      226815 :   Type* type = this->type_->deref();
   18571                 :      226815 :   if (type->struct_type() != NULL)
   18572                 :             :     {
   18573                 :      173959 :       go_assert(!this->has_keys_);
   18574                 :      173959 :       if (!Struct_construction_expression::check_value_types(gogo, type,
   18575                 :             :                                                              this->vals_,
   18576                 :             :                                                              this->location()))
   18577                 :          12 :         this->set_is_error();
   18578                 :             :     }
   18579                 :       52856 :   else if (type->array_type() != NULL)
   18580                 :             :     {
   18581                 :       98714 :       Type* element_type = type->array_type()->element_type();
   18582                 :       49357 :       if (element_type->is_error())
   18583                 :             :         {
   18584                 :           1 :           go_assert(saw_errors());
   18585                 :             :           return;
   18586                 :             :         }
   18587                 :       49356 :       if (this->vals_ == NULL || this->vals_->empty())
   18588                 :             :         return;
   18589                 :             :       int i = 0;
   18590                 :             :       for (Expression_list::const_iterator pv = this->vals_->begin();
   18591                 :      641084 :            pv != this->vals_->end();
   18592                 :      595670 :            ++pv, ++i)
   18593                 :             :         {
   18594                 :      595670 :           if (this->has_keys_)
   18595                 :             :             {
   18596                 :             :               // We check the key type in the lowering pass.
   18597                 :      110423 :               ++pv;
   18598                 :      110423 :               if (pv == this->vals_->end())
   18599                 :             :                 break;
   18600                 :             :             }
   18601                 :      595670 :           if (*pv != NULL
   18602                 :      595670 :               && !Type::are_assignable(element_type, (*pv)->type(), NULL))
   18603                 :             :             {
   18604                 :          10 :               go_error_at((*pv)->location(),
   18605                 :             :                           ("incompatible type for element %d "
   18606                 :             :                            "in composite literal"),
   18607                 :             :                           i + 1);
   18608                 :          10 :               this->set_is_error();
   18609                 :             :             }
   18610                 :             :         }
   18611                 :             :     }
   18612                 :        3499 :   else if (type->map_type() != NULL)
   18613                 :             :     {
   18614                 :        3496 :       if (this->vals_ == NULL || this->vals_->empty())
   18615                 :             :         return;
   18616                 :        2653 :       if (!this->has_keys_)
   18617                 :             :         {
   18618                 :           0 :           this->report_error(_("map composite literal must have keys"));
   18619                 :           0 :           return;
   18620                 :             :         }
   18621                 :        2653 :       int i = 0;
   18622                 :        2653 :       Map_type* mt = type->map_type();
   18623                 :        2653 :       Type* key_type = mt->key_type();
   18624                 :        2653 :       Type* val_type = mt->val_type();
   18625                 :        2653 :       for (Expression_list::const_iterator pv = this->vals_->begin();
   18626                 :       32543 :            pv != this->vals_->end();
   18627                 :       29890 :            ++pv, ++i)
   18628                 :             :         {
   18629                 :       29890 :           if (*pv != NULL
   18630                 :       29890 :               && !Type::are_assignable(key_type, (*pv)->type(), NULL))
   18631                 :             :             {
   18632                 :           1 :               go_error_at((*pv)->location(),
   18633                 :             :                           ("incompatible type for element %d key "
   18634                 :             :                            "in map construction"),
   18635                 :             :                           i + 1);
   18636                 :           1 :               this->set_is_error();
   18637                 :             :             }
   18638                 :       29890 :           ++pv;
   18639                 :       29890 :           if (!Type::are_assignable(val_type, (*pv)->type(), NULL))
   18640                 :             :             {
   18641                 :           0 :               go_error_at((*pv)->location(),
   18642                 :             :                           ("incompatible type for element %d value "
   18643                 :             :                            "in map construction"),
   18644                 :             :                           i + 1);
   18645                 :           0 :               this->set_is_error();
   18646                 :             :             }
   18647                 :             :         }
   18648                 :             :     }
   18649                 :             :   else
   18650                 :             :     {
   18651                 :           3 :       this->report_error(_("expected struct, slice, array, or map type "
   18652                 :             :                            "for composite literal"));
   18653                 :             :     }
   18654                 :             : }
   18655                 :             : 
   18656                 :             : // Lower a generic composite literal into a specific version based on
   18657                 :             : // the type.
   18658                 :             : 
   18659                 :             : Expression*
   18660                 :      227356 : Composite_literal_expression::do_lower(Gogo* gogo, Named_object* function,
   18661                 :             :                                        Statement_inserter* inserter)
   18662                 :             : {
   18663                 :      227356 :   if (this->is_error_expression() || this->type_->is_error())
   18664                 :          50 :     return Expression::make_error(this->location());
   18665                 :      227306 :   go_assert(this->depth_ == 0);
   18666                 :             : 
   18667                 :      227306 :   Type* type = this->type_;
   18668                 :      227306 :   Type *pt = type->points_to();
   18669                 :      227306 :   bool is_pointer = false;
   18670                 :      227306 :   if (pt != NULL)
   18671                 :             :     {
   18672                 :        2001 :       is_pointer = true;
   18673                 :        2001 :       type = pt;
   18674                 :             :     }
   18675                 :             : 
   18676                 :      227306 :   Expression* ret;
   18677                 :      227306 :   if (type->is_error())
   18678                 :           0 :     return Expression::make_error(this->location());
   18679                 :      227306 :   else if (type->struct_type() != NULL)
   18680                 :      174460 :     ret = this->lower_struct(type);
   18681                 :       52846 :   else if (type->array_type() != NULL)
   18682                 :       49351 :     ret = this->lower_array(type);
   18683                 :        3495 :   else if (type->map_type() != NULL)
   18684                 :        3495 :     ret = this->lower_map(gogo, function, inserter, type);
   18685                 :             :   else
   18686                 :           0 :     go_unreachable();
   18687                 :             : 
   18688                 :      227306 :   if (is_pointer)
   18689                 :        2001 :     ret = Expression::make_heap_expression(ret, this->location());
   18690                 :             : 
   18691                 :             :   return ret;
   18692                 :             : }
   18693                 :             : 
   18694                 :             : // Lower a struct composite literal.
   18695                 :             : 
   18696                 :             : Expression*
   18697                 :      174460 : Composite_literal_expression::lower_struct(Type* type)
   18698                 :             : {
   18699                 :      174460 :   go_assert(!this->has_keys_);
   18700                 :      174460 :   Struct_construction_expression* ret =
   18701                 :      174460 :     new Struct_construction_expression(type, this->vals_, this->location());
   18702                 :      174460 :   ret->set_traverse_order(this->traverse_order_);
   18703                 :      174460 :   return ret;
   18704                 :             : }
   18705                 :             : 
   18706                 :             : // Index/value/traversal-order triple.
   18707                 :             : 
   18708                 :             : struct IVT_triple {
   18709                 :             :   unsigned long index;
   18710                 :             :   unsigned long traversal_order;
   18711                 :             :   Expression* expr;
   18712                 :        1881 :   IVT_triple(unsigned long i, unsigned long to, Expression *e)
   18713                 :        1881 :       : index(i), traversal_order(to), expr(e) { }
   18714                 :       10509 :   bool operator<(const IVT_triple& other) const
   18715                 :       10337 :   { return this->index < other.index; }
   18716                 :             : };
   18717                 :             : 
   18718                 :             : // Lower an array composite literal.
   18719                 :             : 
   18720                 :             : Expression*
   18721                 :       49351 : Composite_literal_expression::lower_array(Type* type)
   18722                 :             : {
   18723                 :       49351 :   Location location = this->location();
   18724                 :       49351 :   if (this->vals_ == NULL || !this->has_keys_)
   18725                 :       48884 :     return this->make_array(type, NULL, this->vals_);
   18726                 :             : 
   18727                 :         467 :   std::vector<unsigned long>* indexes = new std::vector<unsigned long>;
   18728                 :         467 :   indexes->reserve(this->vals_->size());
   18729                 :         467 :   bool indexes_out_of_order = false;
   18730                 :         467 :   Expression_list* vals = new Expression_list();
   18731                 :         467 :   vals->reserve(this->vals_->size());
   18732                 :         467 :   unsigned long index = 0;
   18733                 :         467 :   Expression_list::const_iterator p = this->vals_->begin();
   18734                 :      110874 :   while (p != this->vals_->end())
   18735                 :             :     {
   18736                 :      110420 :       Expression* index_expr = *p;
   18737                 :             : 
   18738                 :      110420 :       ++p;
   18739                 :      110420 :       go_assert(p != this->vals_->end());
   18740                 :      110420 :       Expression* val = *p;
   18741                 :             : 
   18742                 :      110420 :       ++p;
   18743                 :             : 
   18744                 :      110420 :       if (index_expr == NULL)
   18745                 :             :         {
   18746                 :          22 :           if (std::find(indexes->begin(), indexes->end(), index)
   18747                 :          22 :               != indexes->end())
   18748                 :             :             {
   18749                 :           0 :               go_error_at(val->location(),
   18750                 :             :                           "duplicate value for index %lu", index);
   18751                 :           0 :               return Expression::make_error(location);
   18752                 :             :             }
   18753                 :          22 :           if (!indexes->empty())
   18754                 :          16 :             indexes->push_back(index);
   18755                 :             :         }
   18756                 :             :       else
   18757                 :             :         {
   18758                 :      110398 :           if (indexes->empty() && !vals->empty())
   18759                 :             :             {
   18760                 :          10 :               for (size_t i = 0; i < vals->size(); ++i)
   18761                 :           6 :                 indexes->push_back(i);
   18762                 :             :             }
   18763                 :             : 
   18764                 :      110398 :           Numeric_constant nc;
   18765                 :      110398 :           if (!index_expr->numeric_constant_value(&nc))
   18766                 :             :             {
   18767                 :           4 :               go_error_at(index_expr->location(),
   18768                 :             :                           "index expression is not integer constant");
   18769                 :           4 :               return Expression::make_error(location);
   18770                 :             :             }
   18771                 :             : 
   18772                 :      110394 :           switch (nc.to_unsigned_long(&index))
   18773                 :             :             {
   18774                 :      110387 :             case Numeric_constant::NC_UL_VALID:
   18775                 :      110387 :               break;
   18776                 :           1 :             case Numeric_constant::NC_UL_NOTINT:
   18777                 :           1 :               go_error_at(index_expr->location(),
   18778                 :             :                           "index expression is not integer constant");
   18779                 :           1 :               return Expression::make_error(location);
   18780                 :           6 :             case Numeric_constant::NC_UL_NEGATIVE:
   18781                 :           6 :               go_error_at(index_expr->location(),
   18782                 :             :                           "index expression is negative");
   18783                 :           6 :               return Expression::make_error(location);
   18784                 :           0 :             case Numeric_constant::NC_UL_BIG:
   18785                 :           0 :               go_error_at(index_expr->location(), "index value overflow");
   18786                 :           0 :               return Expression::make_error(location);
   18787                 :           0 :             default:
   18788                 :           0 :               go_unreachable();
   18789                 :             :             }
   18790                 :             : 
   18791                 :      110387 :           Named_type* ntype = Type::lookup_integer_type("int");
   18792                 :      110387 :           Integer_type* inttype = ntype->integer_type();
   18793                 :      110387 :           if (sizeof(index) >= static_cast<size_t>(inttype->bits() / 8)
   18794                 :      110387 :               && index >> (inttype->bits() - 1) != 0)
   18795                 :             :             {
   18796                 :           0 :               go_error_at(index_expr->location(), "index value overflow");
   18797                 :           0 :               return Expression::make_error(location);
   18798                 :             :             }
   18799                 :             : 
   18800                 :      110387 :           if (std::find(indexes->begin(), indexes->end(), index)
   18801                 :      110387 :               != indexes->end())
   18802                 :             :             {
   18803                 :           2 :               go_error_at(index_expr->location(),
   18804                 :             :                           "duplicate value for index %lu",
   18805                 :             :                           index);
   18806                 :           2 :               return Expression::make_error(location);
   18807                 :             :             }
   18808                 :             : 
   18809                 :      110385 :           if (!indexes->empty() && index < indexes->back())
   18810                 :             :             indexes_out_of_order = true;
   18811                 :             : 
   18812                 :      110385 :           indexes->push_back(index);
   18813                 :      110398 :         }
   18814                 :             : 
   18815                 :      110407 :       vals->push_back(val);
   18816                 :             : 
   18817                 :      110407 :       ++index;
   18818                 :             :     }
   18819                 :             : 
   18820                 :         454 :   if (indexes->empty())
   18821                 :             :     {
   18822                 :           0 :       delete indexes;
   18823                 :           0 :       indexes = NULL;
   18824                 :             :     }
   18825                 :             : 
   18826                 :         454 :   std::vector<unsigned long>* traverse_order = NULL;
   18827                 :         454 :   if (indexes_out_of_order)
   18828                 :             :     {
   18829                 :          62 :       typedef std::vector<IVT_triple> V;
   18830                 :             : 
   18831                 :          62 :       V v;
   18832                 :          62 :       v.reserve(indexes->size());
   18833                 :          62 :       std::vector<unsigned long>::const_iterator pi = indexes->begin();
   18834                 :          62 :       unsigned long torder = 0;
   18835                 :          62 :       for (Expression_list::const_iterator pe = vals->begin();
   18836                 :        1943 :            pe != vals->end();
   18837                 :        1881 :            ++pe, ++pi, ++torder)
   18838                 :        1881 :         v.push_back(IVT_triple(*pi, torder, *pe));
   18839                 :             : 
   18840                 :          62 :       std::sort(v.begin(), v.end());
   18841                 :             : 
   18842                 :          62 :       delete indexes;
   18843                 :          62 :       delete vals;
   18844                 :             : 
   18845                 :          62 :       indexes = new std::vector<unsigned long>();
   18846                 :          62 :       indexes->reserve(v.size());
   18847                 :          62 :       vals = new Expression_list();
   18848                 :          62 :       vals->reserve(v.size());
   18849                 :          62 :       traverse_order = new std::vector<unsigned long>();
   18850                 :          62 :       traverse_order->reserve(v.size());
   18851                 :             : 
   18852                 :        1943 :       for (V::const_iterator pv = v.begin(); pv != v.end(); ++pv)
   18853                 :             :         {
   18854                 :        1881 :           indexes->push_back(pv->index);
   18855                 :        1881 :           vals->push_back(pv->expr);
   18856                 :        1881 :           traverse_order->push_back(pv->traversal_order);
   18857                 :             :         }
   18858                 :          62 :     }
   18859                 :             : 
   18860                 :         454 :   Expression* ret = this->make_array(type, indexes, vals);
   18861                 :         454 :   Array_construction_expression* ace = ret->array_literal();
   18862                 :         454 :   if (ace != NULL && traverse_order != NULL)
   18863                 :          55 :     ace->set_traverse_order(traverse_order);
   18864                 :             :   return ret;
   18865                 :             : }
   18866                 :             : 
   18867                 :             : // Actually build the array composite literal. This handles
   18868                 :             : // [...]{...}.
   18869                 :             : 
   18870                 :             : Expression*
   18871                 :       49338 : Composite_literal_expression::make_array(
   18872                 :             :     Type* type,
   18873                 :             :     const std::vector<unsigned long>* indexes,
   18874                 :             :     Expression_list* vals)
   18875                 :             : {
   18876                 :       49338 :   Location location = this->location();
   18877                 :       49338 :   Array_type* at = type->array_type();
   18878                 :             : 
   18879                 :       49338 :   if (at->length() != NULL
   18880                 :       18043 :       && !at->length()->is_error_expression()
   18881                 :       67381 :       && this->vals_ != NULL)
   18882                 :             :     {
   18883                 :       17491 :       Numeric_constant nc;
   18884                 :       17491 :       unsigned long val;
   18885                 :       17491 :       if (at->length()->numeric_constant_value(&nc)
   18886                 :       17491 :           && nc.to_unsigned_long(&val) == Numeric_constant::NC_UL_VALID)
   18887                 :             :         {
   18888                 :       17491 :           if (indexes == NULL)
   18889                 :             :             {
   18890                 :       17167 :               if (this->vals_->size() > val)
   18891                 :             :                 {
   18892                 :           1 :                   go_error_at(location,
   18893                 :             :                               "too many elements in composite literal");
   18894                 :           1 :                   return Expression::make_error(location);
   18895                 :             :                 }
   18896                 :             :             }
   18897                 :             :           else
   18898                 :             :             {
   18899                 :         324 :               unsigned long max = indexes->back();
   18900                 :         324 :               if (max >= val)
   18901                 :             :                 {
   18902                 :           5 :                   go_error_at(location,
   18903                 :             :                               ("some element keys in composite literal "
   18904                 :             :                                "are out of range"));
   18905                 :           5 :                   return Expression::make_error(location);
   18906                 :             :                 }
   18907                 :             :             }
   18908                 :             :         }
   18909                 :       17491 :     }
   18910                 :             : 
   18911                 :       49332 :   if (at->length() != NULL)
   18912                 :       18037 :     return new Fixed_array_construction_expression(type, indexes, vals,
   18913                 :       18037 :                                                    location);
   18914                 :             :   else
   18915                 :       31295 :     return new Slice_construction_expression(type, indexes, vals, location);
   18916                 :             : }
   18917                 :             : 
   18918                 :             : // Lower a map composite literal.
   18919                 :             : 
   18920                 :             : Expression*
   18921                 :        3495 : Composite_literal_expression::lower_map(Gogo* gogo, Named_object* function,
   18922                 :             :                                         Statement_inserter* inserter,
   18923                 :             :                                         Type* type)
   18924                 :             : {
   18925                 :        3495 :   Location location = this->location();
   18926                 :        3495 :   Unordered_map(unsigned int, std::vector<Expression*>) st;
   18927                 :        3495 :   Unordered_map(unsigned int, std::vector<Expression*>) nt;
   18928                 :        3495 :   bool saw_false = false;
   18929                 :        3495 :   bool saw_true = false;
   18930                 :        3495 :   if (this->vals_ != NULL)
   18931                 :             :     {
   18932                 :        2652 :       go_assert(this->has_keys_);
   18933                 :             : 
   18934                 :       32535 :       for (Expression_list::iterator p = this->vals_->begin();
   18935                 :       32535 :            p != this->vals_->end();
   18936                 :       29883 :            p += 2)
   18937                 :             :         {
   18938                 :       29885 :           if (*p == NULL)
   18939                 :             :             {
   18940                 :           1 :               ++p;
   18941                 :           1 :               go_error_at((*p)->location(),
   18942                 :             :                           ("map composite literal must "
   18943                 :             :                            "have keys for every value"));
   18944                 :           2 :               return Expression::make_error(location);
   18945                 :             :             }
   18946                 :             :           // Make sure we have lowered the key; it may not have been
   18947                 :             :           // lowered in order to handle keys for struct composite
   18948                 :             :           // literals.  Lower it now to get the right error message.
   18949                 :       29884 :           if ((*p)->unknown_expression() != NULL)
   18950                 :             :             {
   18951                 :           0 :               gogo->lower_expression(function, inserter, &*p);
   18952                 :           0 :               go_assert((*p)->is_error_expression());
   18953                 :           0 :               return Expression::make_error(location);
   18954                 :             :             }
   18955                 :             :           // Check if there are duplicate constant keys.
   18956                 :       29884 :           if (!(*p)->is_constant())
   18957                 :        1169 :             continue;
   18958                 :       28715 :           std::string sval;
   18959                 :       28715 :           Numeric_constant nval;
   18960                 :       28715 :           bool bval;
   18961                 :       28715 :           if ((*p)->string_constant_value(&sval)) // Check string keys.
   18962                 :             :             {
   18963                 :       25028 :               unsigned int h = Gogo::hash_string(sval, 0);
   18964                 :             :               // Search the index h in the hash map.
   18965                 :       25028 :               Unordered_map(unsigned int, std::vector<Expression*>)::iterator mit;
   18966                 :       25028 :               mit = st.find(h);
   18967                 :       25028 :               if (mit == st.end())
   18968                 :             :                 {
   18969                 :             :                   // No duplicate since h is a new index.
   18970                 :             :                   // Create a new vector indexed by h and add it to the hash map.
   18971                 :       25027 :                   std::vector<Expression*> l;
   18972                 :       25027 :                   l.push_back(*p);
   18973                 :       25027 :                   std::pair<unsigned int, std::vector<Expression*> > val(h, l);
   18974                 :       25027 :                   st.insert(val);
   18975                 :       25027 :                 }
   18976                 :             :               else
   18977                 :             :                 {
   18978                 :             :                   // Do further check since index h already exists.
   18979                 :           1 :                   for (std::vector<Expression*>::iterator lit =
   18980                 :           1 :                            mit->second.begin();
   18981                 :           1 :                        lit != mit->second.end();
   18982                 :           0 :                        lit++)
   18983                 :             :                     {
   18984                 :           1 :                       std::string s;
   18985                 :           1 :                       bool ok = (*lit)->string_constant_value(&s);
   18986                 :           1 :                       go_assert(ok);
   18987                 :           1 :                       if (s == sval)
   18988                 :             :                         {
   18989                 :           1 :                           go_error_at((*p)->location(), ("duplicate key "
   18990                 :             :                                       "in map literal"));
   18991                 :           1 :                           return Expression::make_error(location);
   18992                 :             :                         }
   18993                 :           1 :                     }
   18994                 :             :                   // Add this new string key to the vector indexed by h.
   18995                 :           0 :                   mit->second.push_back(*p);
   18996                 :             :                 }
   18997                 :             :             }
   18998                 :        3687 :           else if ((*p)->numeric_constant_value(&nval))
   18999                 :             :             {
   19000                 :             :               // Check numeric keys.
   19001                 :        3663 :               unsigned int h = nval.hash(0);
   19002                 :        3663 :               Unordered_map(unsigned int, std::vector<Expression*>)::iterator mit;
   19003                 :        3663 :               mit = nt.find(h);
   19004                 :        3663 :               if (mit == nt.end())
   19005                 :             :                 {
   19006                 :             :                   // No duplicate since h is a new code.
   19007                 :             :                   // Create a new vector indexed by h and add it to the hash map.
   19008                 :        3659 :                   std::vector<Expression*> l;
   19009                 :        3659 :                   l.push_back(*p);
   19010                 :        3659 :                   std::pair<unsigned int, std::vector<Expression*> > val(h, l);
   19011                 :        3659 :                   nt.insert(val);
   19012                 :        3659 :                 }
   19013                 :             :               else
   19014                 :             :                 {
   19015                 :             :                   // Do further check since h already exists.
   19016                 :           8 :                   for (std::vector<Expression*>::iterator lit =
   19017                 :           4 :                            mit->second.begin();
   19018                 :           8 :                        lit != mit->second.end();
   19019                 :           4 :                        lit++)
   19020                 :             :                     {
   19021                 :           4 :                       Numeric_constant rval;
   19022                 :           4 :                       bool ok = (*lit)->numeric_constant_value(&rval);
   19023                 :           4 :                       go_assert(ok);
   19024                 :           4 :                       if (nval.equals(rval))
   19025                 :             :                         {
   19026                 :           0 :                           go_error_at((*p)->location(),
   19027                 :             :                                       "duplicate key in map literal");
   19028                 :           0 :                           return Expression::make_error(location);
   19029                 :             :                         }
   19030                 :           4 :                     }
   19031                 :             :                   // Add this new numeric key to the vector indexed by h.
   19032                 :           4 :                   mit->second.push_back(*p);
   19033                 :             :                 }
   19034                 :             :             }
   19035                 :          24 :           else if ((*p)->boolean_constant_value(&bval))
   19036                 :             :             {
   19037                 :          17 :               if ((bval && saw_true) || (!bval && saw_false))
   19038                 :             :                 {
   19039                 :           0 :                   go_error_at((*p)->location(),
   19040                 :             :                               "duplicate key in map literal");
   19041                 :           0 :                   return Expression::make_error(location);
   19042                 :             :                 }
   19043                 :          17 :               if (bval)
   19044                 :             :                 saw_true = true;
   19045                 :             :               else
   19046                 :           7 :                 saw_false = true;
   19047                 :             :             }
   19048                 :       28715 :         }
   19049                 :             :     }
   19050                 :             : 
   19051                 :        3493 :   return new Map_construction_expression(type, this->vals_, location);
   19052                 :        3495 : }
   19053                 :             : 
   19054                 :             : // Copy.
   19055                 :             : 
   19056                 :             : Expression*
   19057                 :           3 : Composite_literal_expression::do_copy()
   19058                 :             : {
   19059                 :           3 :   Composite_literal_expression* ret =
   19060                 :           3 :     new Composite_literal_expression(this->type_->copy_expressions(),
   19061                 :           3 :                                      this->depth_, this->has_keys_,
   19062                 :           3 :                                      (this->vals_ == NULL
   19063                 :             :                                       ? NULL
   19064                 :           0 :                                       : this->vals_->copy()),
   19065                 :           3 :                                      this->all_are_names_,
   19066                 :           3 :                                      this->location());
   19067                 :           3 :   ret->key_path_ = this->key_path_;
   19068                 :           3 :   return ret;
   19069                 :             : }
   19070                 :             : 
   19071                 :             : // Dump ast representation for a composite literal expression.
   19072                 :             : 
   19073                 :             : void
   19074                 :           0 : Composite_literal_expression::do_dump_expression(
   19075                 :             :                                Ast_dump_context* ast_dump_context) const
   19076                 :             : {
   19077                 :           0 :   ast_dump_context->ostream() << "composite(";
   19078                 :           0 :   ast_dump_context->dump_type(this->type_);
   19079                 :           0 :   ast_dump_context->ostream() << ", {";
   19080                 :           0 :   ast_dump_context->dump_expression_list(this->vals_, this->has_keys_);
   19081                 :           0 :   ast_dump_context->ostream() << "})";
   19082                 :           0 : }
   19083                 :             : 
   19084                 :             : // Make a composite literal expression.
   19085                 :             : 
   19086                 :             : Expression*
   19087                 :      226854 : Expression::make_composite_literal(Type* type, int depth, bool has_keys,
   19088                 :             :                                    Expression_list* vals, bool all_are_names,
   19089                 :             :                                    Location location)
   19090                 :             : {
   19091                 :      226854 :   return new Composite_literal_expression(type, depth, has_keys, vals,
   19092                 :      226854 :                                           all_are_names, location);
   19093                 :             : }
   19094                 :             : 
   19095                 :             : // Return whether this expression is a composite literal.
   19096                 :             : 
   19097                 :             : bool
   19098                 :     4394860 : Expression::is_composite_literal() const
   19099                 :             : {
   19100                 :     4394860 :   switch (this->classification_)
   19101                 :             :     {
   19102                 :             :     case EXPRESSION_COMPOSITE_LITERAL:
   19103                 :             :     case EXPRESSION_STRUCT_CONSTRUCTION:
   19104                 :             :     case EXPRESSION_FIXED_ARRAY_CONSTRUCTION:
   19105                 :             :     case EXPRESSION_SLICE_CONSTRUCTION:
   19106                 :             :     case EXPRESSION_MAP_CONSTRUCTION:
   19107                 :             :       return true;
   19108                 :     3974648 :     default:
   19109                 :     3974648 :       return false;
   19110                 :             :     }
   19111                 :             : }
   19112                 :             : 
   19113                 :             : // Return whether this expression is a composite literal which is not
   19114                 :             : // constant.
   19115                 :             : 
   19116                 :             : bool
   19117                 :        1079 : Expression::is_nonconstant_composite_literal() const
   19118                 :             : {
   19119                 :        1079 :   switch (this->classification_)
   19120                 :             :     {
   19121                 :           0 :     case EXPRESSION_STRUCT_CONSTRUCTION:
   19122                 :           0 :       {
   19123                 :           0 :         const Struct_construction_expression *psce =
   19124                 :             :           static_cast<const Struct_construction_expression*>(this);
   19125                 :           0 :         return !psce->is_constant_struct();
   19126                 :             :       }
   19127                 :           0 :     case EXPRESSION_FIXED_ARRAY_CONSTRUCTION:
   19128                 :           0 :       {
   19129                 :           0 :         const Fixed_array_construction_expression *pace =
   19130                 :             :           static_cast<const Fixed_array_construction_expression*>(this);
   19131                 :           0 :         return !pace->is_constant_array();
   19132                 :             :       }
   19133                 :           0 :     case EXPRESSION_SLICE_CONSTRUCTION:
   19134                 :           0 :       {
   19135                 :           0 :         const Slice_construction_expression *pace =
   19136                 :             :           static_cast<const Slice_construction_expression*>(this);
   19137                 :           0 :         return !pace->is_constant_array();
   19138                 :             :       }
   19139                 :             :     case EXPRESSION_MAP_CONSTRUCTION:
   19140                 :             :       return true;
   19141                 :        1079 :     default:
   19142                 :        1079 :       return false;
   19143                 :             :     }
   19144                 :             : }
   19145                 :             : 
   19146                 :             : // Return true if this is a variable or temporary_variable.
   19147                 :             : 
   19148                 :             : bool
   19149                 :           0 : Expression::is_variable() const
   19150                 :             : {
   19151                 :           0 :   switch (this->classification_)
   19152                 :             :     {
   19153                 :             :     case EXPRESSION_VAR_REFERENCE:
   19154                 :             :     case EXPRESSION_TEMPORARY_REFERENCE:
   19155                 :             :     case EXPRESSION_SET_AND_USE_TEMPORARY:
   19156                 :             :     case EXPRESSION_ENCLOSED_VAR_REFERENCE:
   19157                 :             :       return true;
   19158                 :           0 :     default:
   19159                 :           0 :       return false;
   19160                 :             :     }
   19161                 :             : }
   19162                 :             : 
   19163                 :             : // Return true if this is a reference to a local variable.
   19164                 :             : 
   19165                 :             : bool
   19166                 :           0 : Expression::is_local_variable() const
   19167                 :             : {
   19168                 :           0 :   const Var_expression* ve = this->var_expression();
   19169                 :           0 :   if (ve == NULL)
   19170                 :             :     return false;
   19171                 :           0 :   const Named_object* no = ve->named_object();
   19172                 :           0 :   return (no->is_result_variable()
   19173                 :           0 :           || (no->is_variable() && !no->var_value()->is_global()));
   19174                 :             : }
   19175                 :             : 
   19176                 :             : // Return true if multiple evaluations are OK.
   19177                 :             : 
   19178                 :             : bool
   19179                 :     2787452 : Expression::is_multi_eval_safe()
   19180                 :             : {
   19181                 :     2787452 :   switch (this->classification_)
   19182                 :             :     {
   19183                 :     1050129 :     case EXPRESSION_VAR_REFERENCE:
   19184                 :     1050129 :       {
   19185                 :             :         // A variable is a simple reference if not stored in the heap.
   19186                 :     1050129 :         const Named_object* no = this->var_expression()->named_object();
   19187                 :     1050129 :         if (no->is_variable())
   19188                 :     2085278 :           return !no->var_value()->is_in_heap();
   19189                 :        7490 :         else if (no->is_result_variable())
   19190                 :        7490 :           return !no->result_var_value()->is_in_heap();
   19191                 :             :         else
   19192                 :           0 :           go_unreachable();
   19193                 :             :       }
   19194                 :             : 
   19195                 :             :     case EXPRESSION_TEMPORARY_REFERENCE:
   19196                 :             :       return true;
   19197                 :             : 
   19198                 :      837373 :     default:
   19199                 :      837373 :       break;
   19200                 :             :     }
   19201                 :             : 
   19202                 :      837373 :   if (!this->is_constant())
   19203                 :             :     return false;
   19204                 :             : 
   19205                 :             :   // Only numeric and boolean constants are really multi-evaluation
   19206                 :             :   // safe.  We don't want multiple copies of string constants.
   19207                 :      425224 :   Type* type = this->type();
   19208                 :      496605 :   return type->is_numeric_type() || type->is_boolean_type();
   19209                 :             : }
   19210                 :             : 
   19211                 :             : const Named_object*
   19212                 :      347385 : Expression::named_constant() const
   19213                 :             : {
   19214                 :      347385 :   if (this->classification() != EXPRESSION_CONST_REFERENCE)
   19215                 :             :     return NULL;
   19216                 :       37786 :   const Const_expression* ce = static_cast<const Const_expression*>(this);
   19217                 :       37786 :   return ce->named_object();
   19218                 :             : }
   19219                 :             : 
   19220                 :             : // Class Type_guard_expression.
   19221                 :             : 
   19222                 :             : // Traversal.
   19223                 :             : 
   19224                 :             : int
   19225                 :      247595 : Type_guard_expression::do_traverse(Traverse* traverse)
   19226                 :             : {
   19227                 :      247595 :   if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT
   19228                 :      247595 :       || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
   19229                 :        4659 :     return TRAVERSE_EXIT;
   19230                 :             :   return TRAVERSE_CONTINUE;
   19231                 :             : }
   19232                 :             : 
   19233                 :             : Expression*
   19234                 :       13894 : Type_guard_expression::do_flatten(Gogo*, Named_object*,
   19235                 :             :                                   Statement_inserter* inserter)
   19236                 :             : {
   19237                 :       13894 :   if (this->expr_->is_error_expression()
   19238                 :       13894 :       || this->expr_->type()->is_error_type())
   19239                 :             :     {
   19240                 :           0 :       go_assert(saw_errors());
   19241                 :           0 :       return Expression::make_error(this->location());
   19242                 :             :     }
   19243                 :             : 
   19244                 :       13894 :   if (!this->expr_->is_multi_eval_safe())
   19245                 :             :     {
   19246                 :        1608 :       Temporary_statement* temp = Statement::make_temporary(NULL, this->expr_,
   19247                 :             :                                                             this->location());
   19248                 :        1608 :       inserter->insert(temp);
   19249                 :        1608 :       this->expr_ =
   19250                 :        1608 :           Expression::make_temporary_reference(temp, this->location());
   19251                 :             :     }
   19252                 :       13894 :   return this;
   19253                 :             : }
   19254                 :             : 
   19255                 :             : // Check types of a type guard expression.  The expression must have
   19256                 :             : // an interface type, but the actual type conversion is checked at run
   19257                 :             : // time.
   19258                 :             : 
   19259                 :             : void
   19260                 :       13803 : Type_guard_expression::do_check_types(Gogo*)
   19261                 :             : {
   19262                 :       13803 :   Type* expr_type = this->expr_->type();
   19263                 :       13803 :   if (expr_type->interface_type() == NULL)
   19264                 :             :     {
   19265                 :           1 :       if (!expr_type->is_error() && !this->type_->is_error())
   19266                 :           1 :         this->report_error(_("type assertion only valid for interface types"));
   19267                 :           1 :       this->set_is_error();
   19268                 :             :     }
   19269                 :       13802 :   else if (this->type_->interface_type() == NULL)
   19270                 :             :     {
   19271                 :       12780 :       std::string reason;
   19272                 :       25560 :       if (!expr_type->interface_type()->implements_interface(this->type_,
   19273                 :             :                                                              &reason))
   19274                 :             :         {
   19275                 :           3 :           if (!this->type_->is_error())
   19276                 :             :             {
   19277                 :           3 :               if (reason.empty())
   19278                 :           0 :                 this->report_error(_("impossible type assertion: "
   19279                 :             :                                      "type does not implement interface"));
   19280                 :             :               else
   19281                 :           3 :                 go_error_at(this->location(),
   19282                 :             :                             ("impossible type assertion: "
   19283                 :             :                              "type does not implement interface (%s)"),
   19284                 :             :                             reason.c_str());
   19285                 :             :             }
   19286                 :           3 :           this->set_is_error();
   19287                 :             :         }
   19288                 :       12780 :     }
   19289                 :       13803 : }
   19290                 :             : 
   19291                 :             : // Copy.
   19292                 :             : 
   19293                 :             : Expression*
   19294                 :           0 : Type_guard_expression::do_copy()
   19295                 :             : {
   19296                 :           0 :   return new Type_guard_expression(this->expr_->copy(),
   19297                 :           0 :                                    this->type_->copy_expressions(),
   19298                 :           0 :                                    this->location());
   19299                 :             : }
   19300                 :             : 
   19301                 :             : // Return the backend representation for a type guard expression.
   19302                 :             : 
   19303                 :             : Bexpression*
   19304                 :       13541 : Type_guard_expression::do_get_backend(Translate_context* context)
   19305                 :             : {
   19306                 :       13541 :   Expression* conversion;
   19307                 :       13541 :   if (this->type_->interface_type() != NULL)
   19308                 :        1016 :     conversion = Expression::convert_interface_to_interface(context->gogo(),
   19309                 :             :                                                             this->type_,
   19310                 :             :                                                             this->expr_,
   19311                 :             :                                                             true,
   19312                 :             :                                                             this->location());
   19313                 :             :   else
   19314                 :       12525 :     conversion = Expression::convert_for_assignment(context->gogo(),
   19315                 :             :                                                     this->type_,
   19316                 :             :                                                     this->expr_,
   19317                 :             :                                                     this->location());
   19318                 :             : 
   19319                 :       13541 :   Gogo* gogo = context->gogo();
   19320                 :       13541 :   Btype* bt = this->type_->get_backend(gogo);
   19321                 :       13541 :   Bexpression* bexpr = conversion->get_backend(context);
   19322                 :       13541 :   return gogo->backend()->convert_expression(bt, bexpr, this->location());
   19323                 :             : }
   19324                 :             : 
   19325                 :             : // Dump ast representation for a type guard expression.
   19326                 :             : 
   19327                 :             : void
   19328                 :           0 : Type_guard_expression::do_dump_expression(Ast_dump_context* ast_dump_context)
   19329                 :             :     const
   19330                 :             : {
   19331                 :           0 :   this->expr_->dump_expression(ast_dump_context);
   19332                 :           0 :   ast_dump_context->ostream() <<  ".";
   19333                 :           0 :   ast_dump_context->dump_type(this->type_);
   19334                 :           0 : }
   19335                 :             : 
   19336                 :             : // Make a type guard expression.
   19337                 :             : 
   19338                 :             : Expression*
   19339                 :       20347 : Expression::make_type_guard(Expression* expr, Type* type,
   19340                 :             :                             Location location)
   19341                 :             : {
   19342                 :       20347 :   return new Type_guard_expression(expr, type, location);
   19343                 :             : }
   19344                 :             : 
   19345                 :             : // Class Heap_expression.
   19346                 :             : 
   19347                 :             : // Return the type of the expression stored on the heap.
   19348                 :             : 
   19349                 :             : Type*
   19350                 :      693908 : Heap_expression::do_type()
   19351                 :      693908 : { return Type::make_pointer_type(this->expr_->type()); }
   19352                 :             : 
   19353                 :             : // Return the backend representation for allocating an expression on the heap.
   19354                 :             : 
   19355                 :             : Bexpression*
   19356                 :      204189 : Heap_expression::do_get_backend(Translate_context* context)
   19357                 :             : {
   19358                 :      204189 :   Type* etype = this->expr_->type();
   19359                 :      204189 :   if (this->expr_->is_error_expression() || etype->is_error())
   19360                 :           5 :     return context->backend()->error_expression();
   19361                 :             : 
   19362                 :      204184 :   Location loc = this->location();
   19363                 :      204184 :   Gogo* gogo = context->gogo();
   19364                 :      204184 :   Btype* btype = this->type()->get_backend(gogo);
   19365                 :             : 
   19366                 :      204184 :   Expression* alloc = Expression::make_allocation(etype, loc);
   19367                 :      204184 :   if (this->allocate_on_stack_)
   19368                 :       19415 :     alloc->allocation_expression()->set_allocate_on_stack();
   19369                 :      204184 :   Bexpression* space = alloc->get_backend(context);
   19370                 :             : 
   19371                 :      204184 :   Bstatement* decl;
   19372                 :      204184 :   Named_object* fn = context->function();
   19373                 :      204184 :   go_assert(fn != NULL);
   19374                 :      204184 :   Bfunction* fndecl = fn->func_value()->get_or_make_decl(gogo, fn);
   19375                 :      204184 :   Bvariable* space_temp =
   19376                 :      204184 :     gogo->backend()->temporary_variable(fndecl, context->bblock(), btype,
   19377                 :             :                                         space,
   19378                 :             :                                         Backend::variable_address_is_taken,
   19379                 :             :                                         loc, &decl);
   19380                 :      204184 :   Btype* expr_btype = etype->get_backend(gogo);
   19381                 :             : 
   19382                 :      204184 :   Bexpression* bexpr = this->expr_->get_backend(context);
   19383                 :             : 
   19384                 :             :   // If this assignment needs a write barrier, call typedmemmove.  We
   19385                 :             :   // don't do this in the write barrier pass because in some cases
   19386                 :             :   // backend conversion can introduce new Heap_expression values.
   19387                 :      204184 :   Bstatement* assn;
   19388                 :      204184 :   if (!etype->has_pointer() || this->allocate_on_stack_)
   19389                 :             :     {
   19390                 :       74124 :       space = gogo->backend()->var_expression(space_temp, loc);
   19391                 :       74124 :       Bexpression* ref =
   19392                 :       74124 :         gogo->backend()->indirect_expression(expr_btype, space, true, loc);
   19393                 :       74124 :       assn = gogo->backend()->assignment_statement(fndecl, ref, bexpr, loc);
   19394                 :             :     }
   19395                 :             :   else
   19396                 :             :     {
   19397                 :      130060 :       Bstatement* edecl;
   19398                 :      130060 :       Bvariable* btemp =
   19399                 :      130060 :         gogo->backend()->temporary_variable(fndecl, context->bblock(),
   19400                 :             :                                             expr_btype, bexpr,
   19401                 :             :                                             Backend::variable_address_is_taken,
   19402                 :             :                                             loc, &edecl);
   19403                 :      130060 :       Bexpression* btempref = gogo->backend()->var_expression(btemp,
   19404                 :             :                                                               loc);
   19405                 :      130060 :       space = gogo->backend()->var_expression(space_temp, loc);
   19406                 :      130060 :       Type* etype_ptr = Type::make_pointer_type(etype);
   19407                 :      130060 :       Expression* elhs = Expression::make_backend(space, etype_ptr, loc);
   19408                 :      130060 :       Expression* erhs;
   19409                 :      130060 :       Expression* call;
   19410                 :      130060 :       if (etype->is_direct_iface_type())
   19411                 :             :         {
   19412                 :             :           // Single pointer.
   19413                 :        4672 :           Type* uintptr_type = Type::lookup_integer_type("uintptr");
   19414                 :        4672 :           erhs = Expression::make_backend(btempref, etype, loc);
   19415                 :        4672 :           erhs = Expression::unpack_direct_iface(erhs, loc);
   19416                 :        4672 :           erhs = Expression::make_unsafe_cast(uintptr_type, erhs, loc);
   19417                 :        4672 :           call = Runtime::make_call(gogo, Runtime::GCWRITEBARRIER, loc, 2,
   19418                 :             :                                     elhs, erhs);
   19419                 :             :         }
   19420                 :             :       else
   19421                 :             :         {
   19422                 :      125388 :           Expression* td = Expression::make_type_descriptor(etype, loc);
   19423                 :      125388 :           Bexpression* addr =
   19424                 :      125388 :             gogo->backend()->address_expression(btempref, loc);
   19425                 :      125388 :           erhs = Expression::make_backend(addr, etype_ptr, loc);
   19426                 :      125388 :           call = Runtime::make_call(gogo, Runtime::TYPEDMEMMOVE, loc, 3,
   19427                 :             :                                     td, elhs, erhs);
   19428                 :             :         }
   19429                 :      130060 :       Statement* cs = Statement::make_statement(call, false);
   19430                 :             : 
   19431                 :      130060 :       space = gogo->backend()->var_expression(space_temp, loc);
   19432                 :      130060 :       Bexpression* ref =
   19433                 :      130060 :         gogo->backend()->indirect_expression(expr_btype, space, true, loc);
   19434                 :      130060 :       Expression* eref = Expression::make_backend(ref, etype, loc);
   19435                 :      130060 :       btempref = gogo->backend()->var_expression(btemp, loc);
   19436                 :      130060 :       erhs = Expression::make_backend(btempref, etype, loc);
   19437                 :      130060 :       Statement* as = Statement::make_assignment(eref, erhs, loc);
   19438                 :             : 
   19439                 :      130060 :       as = gogo->check_write_barrier(context->block(), as, cs);
   19440                 :      130060 :       Bstatement* s = as->get_backend(context);
   19441                 :             : 
   19442                 :      130060 :       assn = gogo->backend()->compound_statement(edecl, s);
   19443                 :             :     }
   19444                 :      204184 :   decl = gogo->backend()->compound_statement(decl, assn);
   19445                 :      204184 :   space = gogo->backend()->var_expression(space_temp, loc);
   19446                 :      204184 :   return gogo->backend()->compound_expression(decl, space, loc);
   19447                 :             : }
   19448                 :             : 
   19449                 :             : // Dump ast representation for a heap expression.
   19450                 :             : 
   19451                 :             : void
   19452                 :           0 : Heap_expression::do_dump_expression(
   19453                 :             :     Ast_dump_context* ast_dump_context) const
   19454                 :             : {
   19455                 :           0 :   ast_dump_context->ostream() << "&(";
   19456                 :           0 :   ast_dump_context->dump_expression(this->expr_);
   19457                 :           0 :   ast_dump_context->ostream() << ")";
   19458                 :           0 : }
   19459                 :             : 
   19460                 :             : // Allocate an expression on the heap.
   19461                 :             : 
   19462                 :             : Expression*
   19463                 :      204263 : Expression::make_heap_expression(Expression* expr, Location location)
   19464                 :             : {
   19465                 :      204263 :   return new Heap_expression(expr, location);
   19466                 :             : }
   19467                 :             : 
   19468                 :             : // Class Receive_expression.
   19469                 :             : 
   19470                 :             : // Return the type of a receive expression.
   19471                 :             : 
   19472                 :             : Type*
   19473                 :        9704 : Receive_expression::do_type()
   19474                 :             : {
   19475                 :        9704 :   if (this->is_error_expression())
   19476                 :           2 :     return Type::make_error_type();
   19477                 :        9702 :   Channel_type* channel_type = this->channel_->type()->channel_type();
   19478                 :        9702 :   if (channel_type == NULL)
   19479                 :             :     {
   19480                 :           0 :       this->report_error(_("expected channel"));
   19481                 :           0 :       return Type::make_error_type();
   19482                 :             :     }
   19483                 :        9702 :   return channel_type->element_type();
   19484                 :             : }
   19485                 :             : 
   19486                 :             : // Check types for a receive expression.
   19487                 :             : 
   19488                 :             : void
   19489                 :        1986 : Receive_expression::do_check_types(Gogo*)
   19490                 :             : {
   19491                 :        1986 :   Type* type = this->channel_->type();
   19492                 :        1986 :   if (type->is_error())
   19493                 :             :     {
   19494                 :           0 :       go_assert(saw_errors());
   19495                 :           0 :       this->set_is_error();
   19496                 :           0 :       return;
   19497                 :             :     }
   19498                 :        1986 :   if (type->channel_type() == NULL)
   19499                 :             :     {
   19500                 :           1 :       this->report_error(_("expected channel"));
   19501                 :           1 :       return;
   19502                 :             :     }
   19503                 :        3970 :   if (!type->channel_type()->may_receive())
   19504                 :             :     {
   19505                 :           1 :       this->report_error(_("invalid receive on send-only channel"));
   19506                 :           1 :       return;
   19507                 :             :     }
   19508                 :             : }
   19509                 :             : 
   19510                 :             : // Flattening for receive expressions creates a temporary variable to store
   19511                 :             : // received data in for receives.
   19512                 :             : 
   19513                 :             : Expression*
   19514                 :        2042 : Receive_expression::do_flatten(Gogo*, Named_object*,
   19515                 :             :                                Statement_inserter* inserter)
   19516                 :             : {
   19517                 :        2042 :   Channel_type* channel_type = this->channel_->type()->channel_type();
   19518                 :        2042 :   if (channel_type == NULL)
   19519                 :             :     {
   19520                 :           1 :       go_assert(saw_errors());
   19521                 :           1 :       return this;
   19522                 :             :     }
   19523                 :        2041 :   else if (this->channel_->is_error_expression())
   19524                 :             :    {
   19525                 :           0 :      go_assert(saw_errors());
   19526                 :           0 :      return Expression::make_error(this->location());
   19527                 :             :    }
   19528                 :             : 
   19529                 :        2041 :   Type* element_type = channel_type->element_type();
   19530                 :        2041 :   if (this->temp_receiver_ == NULL)
   19531                 :             :     {
   19532                 :        2033 :       this->temp_receiver_ = Statement::make_temporary(element_type, NULL,
   19533                 :             :                                                        this->location());
   19534                 :        2033 :       this->temp_receiver_->set_is_address_taken();
   19535                 :        2033 :       inserter->insert(this->temp_receiver_);
   19536                 :             :     }
   19537                 :             : 
   19538                 :        2041 :   return this;
   19539                 :             : }
   19540                 :             : 
   19541                 :             : // Get the backend representation for a receive expression.
   19542                 :             : 
   19543                 :             : Bexpression*
   19544                 :        2021 : Receive_expression::do_get_backend(Translate_context* context)
   19545                 :             : {
   19546                 :        2021 :   Location loc = this->location();
   19547                 :             : 
   19548                 :        2021 :   Channel_type* channel_type = this->channel_->type()->channel_type();
   19549                 :        2021 :   if (channel_type == NULL)
   19550                 :             :     {
   19551                 :           0 :       go_assert(this->channel_->type()->is_error());
   19552                 :           0 :       return context->backend()->error_expression();
   19553                 :             :     }
   19554                 :             : 
   19555                 :        2021 :   Gogo* gogo = context->gogo();
   19556                 :        2021 :   Expression* recv_ref =
   19557                 :        2021 :     Expression::make_temporary_reference(this->temp_receiver_, loc);
   19558                 :        2021 :   Expression* recv_addr =
   19559                 :        2021 :     Expression::make_temporary_reference(this->temp_receiver_, loc);
   19560                 :        2021 :   recv_addr = Expression::make_unary(OPERATOR_AND, recv_addr, loc);
   19561                 :        2021 :   Expression* recv = Runtime::make_call(gogo, Runtime::CHANRECV1, loc, 2,
   19562                 :             :                                         this->channel_, recv_addr);
   19563                 :        2021 :   Expression* ret = Expression::make_compound(recv, recv_ref, loc);
   19564                 :        2021 :   ret->determine_type_no_context(gogo);
   19565                 :        2021 :   return ret->get_backend(context);
   19566                 :             : }
   19567                 :             : 
   19568                 :             : // Export a receive expression.
   19569                 :             : 
   19570                 :             : void
   19571                 :          17 : Receive_expression::do_export(Export_function_body* efb) const
   19572                 :             : {
   19573                 :          17 :   efb->write_c_string("<-");
   19574                 :          17 :   this->channel_->export_expression(efb);
   19575                 :          17 : }
   19576                 :             : 
   19577                 :             : // Dump ast representation for a receive expression.
   19578                 :             : 
   19579                 :             : void
   19580                 :           0 : Receive_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
   19581                 :             : {
   19582                 :           0 :   ast_dump_context->ostream() << " <- " ;
   19583                 :           0 :   ast_dump_context->dump_expression(channel_);
   19584                 :           0 : }
   19585                 :             : 
   19586                 :             : // Import a receive expression.
   19587                 :             : 
   19588                 :             : Expression*
   19589                 :           0 : Receive_expression::do_import(Import_expression* imp, Location loc)
   19590                 :             : {
   19591                 :           0 :   imp->require_c_string("<-");
   19592                 :           0 :   Expression* expr = Expression::import_expression(imp, loc);
   19593                 :           0 :   return Expression::make_receive(expr, loc);
   19594                 :             : }
   19595                 :             : 
   19596                 :             : // Make a receive expression.
   19597                 :             : 
   19598                 :             : Receive_expression*
   19599                 :        4139 : Expression::make_receive(Expression* channel, Location location)
   19600                 :             : {
   19601                 :        4139 :   return new Receive_expression(channel, location);
   19602                 :             : }
   19603                 :             : 
   19604                 :             : // An expression which evaluates to a pointer to the type descriptor
   19605                 :             : // of a type.
   19606                 :             : 
   19607                 :             : class Type_descriptor_expression : public Expression
   19608                 :             : {
   19609                 :             :  public:
   19610                 :     1914108 :   Type_descriptor_expression(Type* type, Location location)
   19611                 :     1914108 :     : Expression(EXPRESSION_TYPE_DESCRIPTOR, location),
   19612                 :     3828216 :       type_(type)
   19613                 :             :   { }
   19614                 :             : 
   19615                 :             :  protected:
   19616                 :             :   int
   19617                 :             :   do_traverse(Traverse*);
   19618                 :             : 
   19619                 :             :   Type*
   19620                 :     2181279 :   do_type()
   19621                 :     2181279 :   { return Type::make_type_descriptor_ptr_type(); }
   19622                 :             : 
   19623                 :             :   bool
   19624                 :     1391102 :   do_is_static_initializer() const
   19625                 :     1391102 :   { return true; }
   19626                 :             : 
   19627                 :             :   void
   19628                 :     2066676 :   do_determine_type(Gogo*, const Type_context*)
   19629                 :     2066676 :   { }
   19630                 :             : 
   19631                 :             :   Expression*
   19632                 :       12525 :   do_copy()
   19633                 :       12525 :   { return this; }
   19634                 :             : 
   19635                 :             :   Bexpression*
   19636                 :     1327231 :   do_get_backend(Translate_context* context)
   19637                 :             :   {
   19638                 :     1327231 :     return this->type_->type_descriptor_pointer(context->gogo(),
   19639                 :     1327231 :                                                 this->location());
   19640                 :             :   }
   19641                 :             : 
   19642                 :             :   void
   19643                 :             :   do_dump_expression(Ast_dump_context*) const;
   19644                 :             : 
   19645                 :             :  private:
   19646                 :             :   // The type for which this is the descriptor.
   19647                 :             :   Type* type_;
   19648                 :             : };
   19649                 :             : 
   19650                 :             : int
   19651                 :      683098 : Type_descriptor_expression::do_traverse(Traverse* traverse)
   19652                 :             : {
   19653                 :      683098 :   if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
   19654                 :           0 :     return TRAVERSE_EXIT;
   19655                 :             :   return TRAVERSE_CONTINUE;
   19656                 :             : }
   19657                 :             : 
   19658                 :             : // Dump ast representation for a type descriptor expression.
   19659                 :             : 
   19660                 :             : void
   19661                 :           0 : Type_descriptor_expression::do_dump_expression(
   19662                 :             :     Ast_dump_context* ast_dump_context) const
   19663                 :             : {
   19664                 :           0 :   ast_dump_context->dump_type(this->type_);
   19665                 :           0 : }
   19666                 :             : 
   19667                 :             : // Make a type descriptor expression.
   19668                 :             : 
   19669                 :             : Expression*
   19670                 :     1914108 : Expression::make_type_descriptor(Type* type, Location location)
   19671                 :             : {
   19672                 :     1914108 :   return new Type_descriptor_expression(type, location);
   19673                 :             : }
   19674                 :             : 
   19675                 :             : // An expression which evaluates to a pointer to the Garbage Collection symbol
   19676                 :             : // of a type.
   19677                 :             : 
   19678                 :             : class GC_symbol_expression : public Expression
   19679                 :             : {
   19680                 :             :  public:
   19681                 :      243613 :   GC_symbol_expression(Type* type)
   19682                 :      243613 :     : Expression(EXPRESSION_GC_SYMBOL, Linemap::predeclared_location()),
   19683                 :      243613 :       type_(type)
   19684                 :      243613 :   {}
   19685                 :             : 
   19686                 :             :  protected:
   19687                 :             :   Type*
   19688                 :      212579 :   do_type()
   19689                 :      212579 :   { return Type::make_pointer_type(Type::lookup_integer_type("uint8")); }
   19690                 :             : 
   19691                 :             :   bool
   19692                 :           0 :   do_is_static_initializer() const
   19693                 :           0 :   { return true; }
   19694                 :             : 
   19695                 :             :   void
   19696                 :      278006 :   do_determine_type(Gogo*, const Type_context*)
   19697                 :      278006 :   { }
   19698                 :             : 
   19699                 :             :   Expression*
   19700                 :           0 :   do_copy()
   19701                 :           0 :   { return this; }
   19702                 :             : 
   19703                 :             :   Bexpression*
   19704                 :      212579 :   do_get_backend(Translate_context* context)
   19705                 :      212579 :   { return this->type_->gc_symbol_pointer(context->gogo()); }
   19706                 :             : 
   19707                 :             :   void
   19708                 :             :   do_dump_expression(Ast_dump_context*) const;
   19709                 :             : 
   19710                 :             :  private:
   19711                 :             :   // The type which this gc symbol describes.
   19712                 :             :   Type* type_;
   19713                 :             : };
   19714                 :             : 
   19715                 :             : // Dump ast representation for a gc symbol expression.
   19716                 :             : 
   19717                 :             : void
   19718                 :           0 : GC_symbol_expression::do_dump_expression(
   19719                 :             :     Ast_dump_context* ast_dump_context) const
   19720                 :             : {
   19721                 :           0 :   ast_dump_context->ostream() << "gcdata(";
   19722                 :           0 :   ast_dump_context->dump_type(this->type_);
   19723                 :           0 :   ast_dump_context->ostream() << ")";
   19724                 :           0 : }
   19725                 :             : 
   19726                 :             : // Make a gc symbol expression.
   19727                 :             : 
   19728                 :             : Expression*
   19729                 :      243613 : Expression::make_gc_symbol(Type* type)
   19730                 :             : {
   19731                 :      243613 :   return new GC_symbol_expression(type);
   19732                 :             : }
   19733                 :             : 
   19734                 :             : // An expression that evaluates to a pointer to a symbol holding the
   19735                 :             : // ptrmask data of a type.
   19736                 :             : 
   19737                 :             : class Ptrmask_symbol_expression : public Expression
   19738                 :             : {
   19739                 :             :  public:
   19740                 :       22047 :   Ptrmask_symbol_expression(Type* type)
   19741                 :       22047 :     : Expression(EXPRESSION_PTRMASK_SYMBOL, Linemap::predeclared_location()),
   19742                 :       22047 :       type_(type)
   19743                 :       22047 :   {}
   19744                 :             : 
   19745                 :             :  protected:
   19746                 :             :   Type*
   19747                 :       44094 :   do_type()
   19748                 :       44094 :   { return Type::make_pointer_type(Type::lookup_integer_type("uint8")); }
   19749                 :             : 
   19750                 :             :   bool
   19751                 :       22047 :   do_is_static_initializer() const
   19752                 :       22047 :   { return true; }
   19753                 :             : 
   19754                 :             :   void
   19755                 :       22047 :   do_determine_type(Gogo*, const Type_context*)
   19756                 :       22047 :   { }
   19757                 :             : 
   19758                 :             :   Expression*
   19759                 :           0 :   do_copy()
   19760                 :           0 :   { return this; }
   19761                 :             : 
   19762                 :             :   Bexpression*
   19763                 :             :   do_get_backend(Translate_context*);
   19764                 :             : 
   19765                 :             :   void
   19766                 :             :   do_dump_expression(Ast_dump_context*) const;
   19767                 :             : 
   19768                 :             :  private:
   19769                 :             :   // The type that this ptrmask symbol describes.
   19770                 :             :   Type* type_;
   19771                 :             : };
   19772                 :             : 
   19773                 :             : // Return the ptrmask variable.
   19774                 :             : 
   19775                 :             : Bexpression*
   19776                 :       22047 : Ptrmask_symbol_expression::do_get_backend(Translate_context* context)
   19777                 :             : {
   19778                 :       22047 :   Gogo* gogo = context->gogo();
   19779                 :             : 
   19780                 :             :   // If this type does not need a gcprog, then we can use the standard
   19781                 :             :   // GC symbol.
   19782                 :       22047 :   int64_t ptrsize, ptrdata;
   19783                 :       22047 :   if (!this->type_->needs_gcprog(gogo, &ptrsize, &ptrdata))
   19784                 :       22034 :     return this->type_->gc_symbol_pointer(gogo);
   19785                 :             : 
   19786                 :             :   // Otherwise we have to build a ptrmask variable, and return a
   19787                 :             :   // pointer to it.
   19788                 :             : 
   19789                 :          13 :   Bvariable* bvar = this->type_->gc_ptrmask_var(gogo, ptrsize, ptrdata);
   19790                 :          13 :   Location bloc = Linemap::predeclared_location();
   19791                 :          13 :   Bexpression* bref = gogo->backend()->var_expression(bvar, bloc);
   19792                 :          13 :   Bexpression* baddr = gogo->backend()->address_expression(bref, bloc);
   19793                 :             : 
   19794                 :          13 :   Type* uint8_type = Type::lookup_integer_type("uint8");
   19795                 :          13 :   Type* pointer_uint8_type = Type::make_pointer_type(uint8_type);
   19796                 :          13 :   Btype* ubtype = pointer_uint8_type->get_backend(gogo);
   19797                 :          13 :   return gogo->backend()->convert_expression(ubtype, baddr, bloc);
   19798                 :             : }
   19799                 :             : 
   19800                 :             : // Dump AST for a ptrmask symbol expression.
   19801                 :             : 
   19802                 :             : void
   19803                 :           0 : Ptrmask_symbol_expression::do_dump_expression(
   19804                 :             :     Ast_dump_context* ast_dump_context) const
   19805                 :             : {
   19806                 :           0 :   ast_dump_context->ostream() << "ptrmask(";
   19807                 :           0 :   ast_dump_context->dump_type(this->type_);
   19808                 :           0 :   ast_dump_context->ostream() << ")";
   19809                 :           0 : }
   19810                 :             : 
   19811                 :             : // Make a ptrmask symbol expression.
   19812                 :             : 
   19813                 :             : Expression*
   19814                 :       22047 : Expression::make_ptrmask_symbol(Type* type)
   19815                 :             : {
   19816                 :       22047 :   return new Ptrmask_symbol_expression(type);
   19817                 :             : }
   19818                 :             : 
   19819                 :             : // An expression which evaluates to some characteristic of a type.
   19820                 :             : // This is only used to initialize fields of a type descriptor.  Using
   19821                 :             : // a new expression class is slightly inefficient but gives us a good
   19822                 :             : // separation between the frontend and the middle-end with regard to
   19823                 :             : // how types are laid out.
   19824                 :             : 
   19825                 :             : class Type_info_expression : public Expression
   19826                 :             : {
   19827                 :             :  public:
   19828                 :     1155254 :   Type_info_expression(Type* type, Type_info type_info)
   19829                 :     1155254 :     : Expression(EXPRESSION_TYPE_INFO, Linemap::predeclared_location()),
   19830                 :     1155254 :       type_(type), type_info_(type_info)
   19831                 :     1155254 :   { }
   19832                 :             : 
   19833                 :             :  protected:
   19834                 :             :   bool
   19835                 :       44094 :   do_is_static_initializer() const
   19836                 :       44094 :   { return true; }
   19837                 :             : 
   19838                 :             :   Type*
   19839                 :             :   do_type();
   19840                 :             : 
   19841                 :             :   void
   19842                 :     1433472 :   do_determine_type(Gogo*, const Type_context*)
   19843                 :     1433472 :   { }
   19844                 :             : 
   19845                 :             :   Expression*
   19846                 :           0 :   do_copy()
   19847                 :           0 :   { return this; }
   19848                 :             : 
   19849                 :             :   Bexpression*
   19850                 :             :   do_get_backend(Translate_context* context);
   19851                 :             : 
   19852                 :             :   void
   19853                 :             :   do_dump_expression(Ast_dump_context*) const;
   19854                 :             : 
   19855                 :             :  private:
   19856                 :             :   // The type for which we are getting information.
   19857                 :             :   Type* type_;
   19858                 :             :   // What information we want.
   19859                 :             :   Type_info type_info_;
   19860                 :             : };
   19861                 :             : 
   19862                 :             : // The type is chosen to match what the type descriptor struct
   19863                 :             : // expects.
   19864                 :             : 
   19865                 :             : Type*
   19866                 :     2101667 : Type_info_expression::do_type()
   19867                 :             : {
   19868                 :     2101667 :   switch (this->type_info_)
   19869                 :             :     {
   19870                 :     1118287 :     case TYPE_INFO_SIZE:
   19871                 :     1118287 :     case TYPE_INFO_BACKEND_PTRDATA:
   19872                 :     1118287 :     case TYPE_INFO_DESCRIPTOR_PTRDATA:
   19873                 :     1118287 :       return Type::lookup_integer_type("uintptr");
   19874                 :      983380 :     case TYPE_INFO_ALIGNMENT:
   19875                 :      983380 :     case TYPE_INFO_FIELD_ALIGNMENT:
   19876                 :      983380 :       return Type::lookup_integer_type("uint8");
   19877                 :           0 :     default:
   19878                 :           0 :       go_unreachable();
   19879                 :             :     }
   19880                 :             : }
   19881                 :             : 
   19882                 :             : // Return the backend representation for type information.
   19883                 :             : 
   19884                 :             : Bexpression*
   19885                 :     1011246 : Type_info_expression::do_get_backend(Translate_context* context)
   19886                 :             : {
   19887                 :     1011246 :   Gogo* gogo = context->gogo();
   19888                 :     1011246 :   bool ok = true;
   19889                 :     1011246 :   int64_t val;
   19890                 :     1011246 :   switch (this->type_info_)
   19891                 :             :     {
   19892                 :      284930 :     case TYPE_INFO_SIZE:
   19893                 :      284930 :       ok = this->type_->backend_type_size(gogo, &val);
   19894                 :      284930 :       break;
   19895                 :      245845 :     case TYPE_INFO_ALIGNMENT:
   19896                 :      245845 :       ok = this->type_->backend_type_align(gogo, &val);
   19897                 :      245845 :       break;
   19898                 :      245845 :     case TYPE_INFO_FIELD_ALIGNMENT:
   19899                 :      245845 :       ok = this->type_->backend_type_field_align(gogo, &val);
   19900                 :      245845 :       break;
   19901                 :       22047 :     case TYPE_INFO_BACKEND_PTRDATA:
   19902                 :       22047 :       ok = this->type_->backend_type_ptrdata(gogo, &val);
   19903                 :       22047 :       break;
   19904                 :      212579 :     case TYPE_INFO_DESCRIPTOR_PTRDATA:
   19905                 :      212579 :       ok = this->type_->descriptor_ptrdata(gogo, &val);
   19906                 :      212579 :       break;
   19907                 :           0 :     default:
   19908                 :           0 :       go_unreachable();
   19909                 :             :     }
   19910                 :     1011246 :   if (!ok)
   19911                 :             :     {
   19912                 :           3 :       go_assert(saw_errors());
   19913                 :           3 :       return gogo->backend()->error_expression();
   19914                 :             :     }
   19915                 :     1011243 :   Expression* e = Expression::make_integer_int64(val, this->type(),
   19916                 :             :                                                  this->location());
   19917                 :     1011243 :   return e->get_backend(context);
   19918                 :             : }
   19919                 :             : 
   19920                 :             : // Dump ast representation for a type info expression.
   19921                 :             : 
   19922                 :             : void
   19923                 :           0 : Type_info_expression::do_dump_expression(
   19924                 :             :     Ast_dump_context* ast_dump_context) const
   19925                 :             : {
   19926                 :           0 :   ast_dump_context->ostream() << "typeinfo(";
   19927                 :           0 :   ast_dump_context->dump_type(this->type_);
   19928                 :           0 :   ast_dump_context->ostream() << ",";
   19929                 :           0 :   ast_dump_context->ostream() <<
   19930                 :           0 :     (this->type_info_ == TYPE_INFO_ALIGNMENT ? "alignment"
   19931                 :           0 :     : this->type_info_ == TYPE_INFO_FIELD_ALIGNMENT ? "field alignment"
   19932                 :           0 :     : this->type_info_ == TYPE_INFO_SIZE ? "size"
   19933                 :           0 :     : this->type_info_ == TYPE_INFO_BACKEND_PTRDATA ? "backend_ptrdata"
   19934                 :           0 :     : this->type_info_ == TYPE_INFO_DESCRIPTOR_PTRDATA ? "descriptor_ptrdata"
   19935                 :           0 :     : "unknown");
   19936                 :           0 :   ast_dump_context->ostream() << ")";
   19937                 :           0 : }
   19938                 :             : 
   19939                 :             : // Make a type info expression.
   19940                 :             : 
   19941                 :             : Expression*
   19942                 :     1155254 : Expression::make_type_info(Type* type, Type_info type_info)
   19943                 :             : {
   19944                 :     1155254 :   return new Type_info_expression(type, type_info);
   19945                 :             : }
   19946                 :             : 
   19947                 :             : // Slice_info_expression.
   19948                 :             : 
   19949                 :             : // Return the type of the slice info.
   19950                 :             : 
   19951                 :             : Type*
   19952                 :     1101756 : Slice_info_expression::do_type()
   19953                 :             : {
   19954                 :     1101756 :   switch (this->slice_info_)
   19955                 :             :     {
   19956                 :      367207 :     case SLICE_INFO_VALUE_POINTER:
   19957                 :      734414 :       return Type::make_pointer_type(
   19958                 :      734414 :           this->slice_->type()->array_type()->element_type());
   19959                 :      734549 :     case SLICE_INFO_LENGTH:
   19960                 :      734549 :     case SLICE_INFO_CAPACITY:
   19961                 :      734549 :         return Type::lookup_integer_type("int");
   19962                 :           0 :     default:
   19963                 :           0 :       go_unreachable();
   19964                 :             :     }
   19965                 :             : }
   19966                 :             : 
   19967                 :             : // Return the backend information for slice information.
   19968                 :             : 
   19969                 :             : Bexpression*
   19970                 :      723602 : Slice_info_expression::do_get_backend(Translate_context* context)
   19971                 :             : {
   19972                 :      723602 :   Gogo* gogo = context->gogo();
   19973                 :      723602 :   Bexpression* bslice = this->slice_->get_backend(context);
   19974                 :      723602 :   switch (this->slice_info_)
   19975                 :             :     {
   19976                 :      723602 :     case SLICE_INFO_VALUE_POINTER:
   19977                 :      723602 :     case SLICE_INFO_LENGTH:
   19978                 :      723602 :     case SLICE_INFO_CAPACITY:
   19979                 :      723602 :       return gogo->backend()->struct_field_expression(bslice, this->slice_info_,
   19980                 :      723602 :                                                       this->location());
   19981                 :           0 :       break;
   19982                 :           0 :     default:
   19983                 :           0 :       go_unreachable();
   19984                 :             :     }
   19985                 :             : }
   19986                 :             : 
   19987                 :             : // Dump ast representation for a type info expression.
   19988                 :             : 
   19989                 :             : void
   19990                 :           0 : Slice_info_expression::do_dump_expression(
   19991                 :             :     Ast_dump_context* ast_dump_context) const
   19992                 :             : {
   19993                 :           0 :   ast_dump_context->ostream() << "sliceinfo(";
   19994                 :           0 :   this->slice_->dump_expression(ast_dump_context);
   19995                 :           0 :   ast_dump_context->ostream() << ",";
   19996                 :           0 :   ast_dump_context->ostream() <<
   19997                 :           0 :       (this->slice_info_ == SLICE_INFO_VALUE_POINTER ? "values"
   19998                 :           0 :     : this->slice_info_ == SLICE_INFO_LENGTH ? "length"
   19999                 :           0 :     : this->slice_info_ == SLICE_INFO_CAPACITY ? "capacity "
   20000                 :           0 :     : "unknown");
   20001                 :           0 :   ast_dump_context->ostream() << ")";
   20002                 :           0 : }
   20003                 :             : 
   20004                 :             : // Make a slice info expression.
   20005                 :             : 
   20006                 :             : Expression*
   20007                 :      692526 : Expression::make_slice_info(Expression* slice, Slice_info slice_info,
   20008                 :             :                             Location location)
   20009                 :             : {
   20010                 :      692526 :   return new Slice_info_expression(slice, slice_info, location);
   20011                 :             : }
   20012                 :             : 
   20013                 :             : // Class Slice_value_expression.
   20014                 :             : 
   20015                 :             : int
   20016                 :      126120 : Slice_value_expression::do_traverse(Traverse* traverse)
   20017                 :             : {
   20018                 :      126120 :   if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT
   20019                 :      126120 :       || Expression::traverse(&this->valmem_, traverse) == TRAVERSE_EXIT
   20020                 :      126120 :       || Expression::traverse(&this->len_, traverse) == TRAVERSE_EXIT
   20021                 :      252240 :       || Expression::traverse(&this->cap_, traverse) == TRAVERSE_EXIT)
   20022                 :           0 :     return TRAVERSE_EXIT;
   20023                 :             :   return TRAVERSE_CONTINUE;
   20024                 :             : }
   20025                 :             : 
   20026                 :             : // Determine type of a slice value.
   20027                 :             : 
   20028                 :             : void
   20029                 :       31327 : Slice_value_expression::do_determine_type(Gogo* gogo, const Type_context*)
   20030                 :             : {
   20031                 :       31327 :   this->valmem_->determine_type_no_context(gogo);
   20032                 :       31327 :   this->len_->determine_type_no_context(gogo);
   20033                 :       31327 :   this->cap_->determine_type_no_context(gogo);
   20034                 :       31327 : }
   20035                 :             : 
   20036                 :             : Expression*
   20037                 :           0 : Slice_value_expression::do_copy()
   20038                 :             : {
   20039                 :           0 :   return new Slice_value_expression(this->type_->copy_expressions(),
   20040                 :           0 :                                     this->valmem_->copy(),
   20041                 :           0 :                                     this->len_->copy(), this->cap_->copy(),
   20042                 :           0 :                                     this->location());
   20043                 :             : }
   20044                 :             : 
   20045                 :             : Bexpression*
   20046                 :      405065 : Slice_value_expression::do_get_backend(Translate_context* context)
   20047                 :             : {
   20048                 :      405065 :   std::vector<Bexpression*> vals(3);
   20049                 :      405065 :   vals[0] = this->valmem_->get_backend(context);
   20050                 :      405065 :   vals[1] = this->len_->get_backend(context);
   20051                 :      405065 :   vals[2] = this->cap_->get_backend(context);
   20052                 :             : 
   20053                 :      405065 :   Gogo* gogo = context->gogo();
   20054                 :      405065 :   Btype* btype = this->type_->get_backend(gogo);
   20055                 :      405065 :   return gogo->backend()->constructor_expression(btype, vals, this->location());
   20056                 :      405065 : }
   20057                 :             : 
   20058                 :             : void
   20059                 :           0 : Slice_value_expression::do_dump_expression(
   20060                 :             :     Ast_dump_context* ast_dump_context) const
   20061                 :             : {
   20062                 :           0 :   ast_dump_context->ostream() << "slicevalue(";
   20063                 :           0 :   ast_dump_context->ostream() << "values: ";
   20064                 :           0 :   this->valmem_->dump_expression(ast_dump_context);
   20065                 :           0 :   ast_dump_context->ostream() << ", length: ";
   20066                 :           0 :   this->len_->dump_expression(ast_dump_context);
   20067                 :           0 :   ast_dump_context->ostream() << ", capacity: ";
   20068                 :           0 :   this->cap_->dump_expression(ast_dump_context);
   20069                 :           0 :   ast_dump_context->ostream() << ")";
   20070                 :           0 : }
   20071                 :             : 
   20072                 :             : Expression*
   20073                 :      405685 : Expression::make_slice_value(Type* at, Expression* valmem, Expression* len,
   20074                 :             :                              Expression* cap, Location location)
   20075                 :             : {
   20076                 :      405685 :   go_assert(at->is_slice_type());
   20077                 :      792204 :   go_assert(valmem->is_nil_expression()
   20078                 :             :             || (at->array_type()->element_type()
   20079                 :             :                 == valmem->type()->points_to()));
   20080                 :      405685 :   return new Slice_value_expression(at, valmem, len, cap, location);
   20081                 :             : }
   20082                 :             : 
   20083                 :             : // Look through the expression of a Slice_value_expression's valmem to
   20084                 :             : // find an call to makeslice.  If found, return the call expression and
   20085                 :             : // the containing temporary statement (if any).
   20086                 :             : 
   20087                 :             : std::pair<Call_expression*, Temporary_statement*>
   20088                 :       12132 : Expression::find_makeslice_call(Expression* expr)
   20089                 :             : {
   20090                 :       12132 :   Unsafe_type_conversion_expression* utce =
   20091                 :       12159 :     expr->unsafe_conversion_expression();
   20092                 :          27 :   if (utce != NULL)
   20093                 :          27 :     expr = utce->expr();
   20094                 :             : 
   20095                 :       12132 :   Slice_value_expression* sve = expr->slice_value_expression();
   20096                 :       12132 :   if (sve == NULL)
   20097                 :        3529 :     return std::make_pair<Call_expression*, Temporary_statement*>(NULL, NULL);
   20098                 :        8603 :   expr = sve->valmem();
   20099                 :             : 
   20100                 :        8603 :   utce = expr->unsafe_conversion_expression();
   20101                 :        8594 :   if (utce != NULL)
   20102                 :        8594 :     expr = utce->expr();
   20103                 :             : 
   20104                 :        8603 :   Temporary_reference_expression* tre = expr->temporary_reference_expression();
   20105                 :        2797 :   Temporary_statement* ts = (tre != NULL ? tre->statement() : NULL);
   20106                 :        2797 :   if (ts != NULL && ts->init() != NULL && !ts->assigned()
   20107                 :        5594 :       && !ts->is_address_taken())
   20108                 :             :     expr = ts->init();
   20109                 :             : 
   20110                 :        8603 :   Call_expression* call = expr->call_expression();
   20111                 :        8603 :   if (call == NULL)
   20112                 :          27 :     return std::make_pair<Call_expression*, Temporary_statement*>(NULL, NULL);
   20113                 :             : 
   20114                 :        8576 :   Func_expression* fe = call->fn()->func_expression();
   20115                 :        8576 :   if (fe != NULL
   20116                 :        8576 :       && fe->runtime_code() == Runtime::MAKESLICE)
   20117                 :        8456 :     return std::make_pair(call, ts);
   20118                 :             : 
   20119                 :         120 :   return std::make_pair<Call_expression*, Temporary_statement*>(NULL, NULL);
   20120                 :             : }
   20121                 :             : 
   20122                 :             : // An expression that evaluates to some characteristic of a non-empty interface.
   20123                 :             : // This is used to access the method table or underlying object of an interface.
   20124                 :             : 
   20125                 :             : class Interface_info_expression : public Expression
   20126                 :             : {
   20127                 :             :  public:
   20128                 :      210141 :   Interface_info_expression(Expression* iface, Interface_info iface_info,
   20129                 :             :                             Location location)
   20130                 :      210141 :     : Expression(EXPRESSION_INTERFACE_INFO, location),
   20131                 :      420282 :       iface_(iface), iface_info_(iface_info)
   20132                 :             :   { }
   20133                 :             : 
   20134                 :             :  protected:
   20135                 :             :   Type*
   20136                 :             :   do_type();
   20137                 :             : 
   20138                 :             :   void
   20139                 :      398772 :   do_determine_type(Gogo*, const Type_context*)
   20140                 :      398772 :   { }
   20141                 :             : 
   20142                 :             :   Expression*
   20143                 :           0 :   do_copy()
   20144                 :             :   {
   20145                 :           0 :     return new Interface_info_expression(this->iface_->copy(),
   20146                 :           0 :                                          this->iface_info_, this->location());
   20147                 :             :   }
   20148                 :             : 
   20149                 :             :   Bexpression*
   20150                 :             :   do_get_backend(Translate_context* context);
   20151                 :             : 
   20152                 :             :   void
   20153                 :             :   do_dump_expression(Ast_dump_context*) const;
   20154                 :             : 
   20155                 :             :   void
   20156                 :           0 :   do_issue_nil_check()
   20157                 :           0 :   { this->iface_->issue_nil_check(); }
   20158                 :             : 
   20159                 :             :  private:
   20160                 :             :   // The interface for which we are getting information.
   20161                 :             :   Expression* iface_;
   20162                 :             :   // What information we want.
   20163                 :             :   Interface_info iface_info_;
   20164                 :             : };
   20165                 :             : 
   20166                 :             : // Return the type of the interface info.
   20167                 :             : 
   20168                 :             : Type*
   20169                 :     1108952 : Interface_info_expression::do_type()
   20170                 :             : {
   20171                 :     1108952 :   switch (this->iface_info_)
   20172                 :             :     {
   20173                 :      867135 :     case INTERFACE_INFO_METHODS:
   20174                 :      867135 :       {
   20175                 :      867135 :         typedef Unordered_map(Interface_type*, Type*) Hashtable;
   20176                 :      867135 :         static Hashtable result_types;
   20177                 :             : 
   20178                 :      867135 :         Interface_type* itype = this->iface_->type()->interface_type();
   20179                 :             : 
   20180                 :      867135 :         Hashtable::const_iterator pr = result_types.find(itype);
   20181                 :      867135 :         if (pr != result_types.end())
   20182                 :      859918 :           return pr->second;
   20183                 :             : 
   20184                 :        7217 :         Type* pdt = Type::make_type_descriptor_ptr_type();
   20185                 :        7217 :         if (itype->is_empty())
   20186                 :             :           {
   20187                 :         811 :             result_types[itype] = pdt;
   20188                 :         811 :             return pdt;
   20189                 :             :           }
   20190                 :             : 
   20191                 :        6406 :         Location loc = this->location();
   20192                 :        6406 :         Struct_field_list* sfl = new Struct_field_list();
   20193                 :        6406 :         sfl->push_back(
   20194                 :       12812 :             Struct_field(Typed_identifier("__type_descriptor", pdt, loc)));
   20195                 :             : 
   20196                 :        6406 :         for (Typed_identifier_list::const_iterator p = itype->methods()->begin();
   20197                 :       28162 :              p != itype->methods()->end();
   20198                 :       21756 :              ++p)
   20199                 :             :           {
   20200                 :       21756 :             Function_type* ft = p->type()->function_type();
   20201                 :       21756 :             go_assert(ft->receiver() == NULL);
   20202                 :             : 
   20203                 :       21756 :             const Typed_identifier_list* params = ft->parameters();
   20204                 :       21756 :             Typed_identifier_list* mparams = new Typed_identifier_list();
   20205                 :       21756 :             if (params != NULL)
   20206                 :        7984 :               mparams->reserve(params->size() + 1);
   20207                 :       21756 :             Type* vt = Type::make_pointer_type(Type::make_void_type());
   20208                 :       21756 :             mparams->push_back(Typed_identifier("", vt, ft->location()));
   20209                 :       21756 :             if (params != NULL)
   20210                 :             :               {
   20211                 :        7984 :                 for (Typed_identifier_list::const_iterator pp = params->begin();
   20212                 :       18017 :                      pp != params->end();
   20213                 :       10033 :                      ++pp)
   20214                 :       10033 :                   mparams->push_back(*pp);
   20215                 :             :               }
   20216                 :             : 
   20217                 :       21756 :             Typed_identifier_list* mresults = (ft->results() == NULL
   20218                 :       21756 :                                                ? NULL
   20219                 :       19407 :                                                : ft->results()->copy());
   20220                 :       21756 :             Backend_function_type* mft =
   20221                 :       21756 :                 Type::make_backend_function_type(NULL, mparams, mresults,
   20222                 :             :                                                  ft->location());
   20223                 :             : 
   20224                 :       21756 :             std::string fname = Gogo::unpack_hidden_name(p->name());
   20225                 :       21756 :             sfl->push_back(Struct_field(Typed_identifier(fname, mft, loc)));
   20226                 :       21756 :           }
   20227                 :             : 
   20228                 :        6406 :         Struct_type* st = Type::make_struct_type(sfl, loc);
   20229                 :        6406 :         st->set_is_struct_incomparable();
   20230                 :        6406 :         Pointer_type *pt = Type::make_pointer_type(st);
   20231                 :        6406 :         result_types[itype] = pt;
   20232                 :        6406 :         return pt;
   20233                 :             :       }
   20234                 :      241817 :     case INTERFACE_INFO_OBJECT:
   20235                 :      241817 :       return Type::make_pointer_type(Type::make_void_type());
   20236                 :           0 :     default:
   20237                 :           0 :       go_unreachable();
   20238                 :             :     }
   20239                 :             : }
   20240                 :             : 
   20241                 :             : // Return the backend representation for interface information.
   20242                 :             : 
   20243                 :             : Bexpression*
   20244                 :      281884 : Interface_info_expression::do_get_backend(Translate_context* context)
   20245                 :             : {
   20246                 :      281884 :   Gogo* gogo = context->gogo();
   20247                 :      281884 :   Bexpression* biface = this->iface_->get_backend(context);
   20248                 :      281884 :   switch (this->iface_info_)
   20249                 :             :     {
   20250                 :      281884 :     case INTERFACE_INFO_METHODS:
   20251                 :      281884 :     case INTERFACE_INFO_OBJECT:
   20252                 :      281884 :       return gogo->backend()->struct_field_expression(biface, this->iface_info_,
   20253                 :      281884 :                                                       this->location());
   20254                 :           0 :       break;
   20255                 :           0 :     default:
   20256                 :           0 :       go_unreachable();
   20257                 :             :     }
   20258                 :             : }
   20259                 :             : 
   20260                 :             : // Dump ast representation for an interface info expression.
   20261                 :             : 
   20262                 :             : void
   20263                 :           0 : Interface_info_expression::do_dump_expression(
   20264                 :             :     Ast_dump_context* ast_dump_context) const
   20265                 :             : {
   20266                 :           0 :   bool is_empty = this->iface_->type()->interface_type()->is_empty();
   20267                 :           0 :   ast_dump_context->ostream() << "interfaceinfo(";
   20268                 :           0 :   this->iface_->dump_expression(ast_dump_context);
   20269                 :           0 :   ast_dump_context->ostream() << ",";
   20270                 :           0 :   ast_dump_context->ostream() <<
   20271                 :           0 :       (this->iface_info_ == INTERFACE_INFO_METHODS && !is_empty ? "methods"
   20272                 :           0 :     : this->iface_info_ == INTERFACE_INFO_TYPE_DESCRIPTOR ? "type_descriptor"
   20273                 :           0 :     : this->iface_info_ == INTERFACE_INFO_OBJECT ? "object"
   20274                 :           0 :     : "unknown");
   20275                 :           0 :   ast_dump_context->ostream() << ")";
   20276                 :           0 : }
   20277                 :             : 
   20278                 :             : // Make an interface info expression.
   20279                 :             : 
   20280                 :             : Expression*
   20281                 :      210141 : Expression::make_interface_info(Expression* iface, Interface_info iface_info,
   20282                 :             :                                 Location location)
   20283                 :             : {
   20284                 :      210141 :   return new Interface_info_expression(iface, iface_info, location);
   20285                 :             : }
   20286                 :             : 
   20287                 :             : // An expression that represents an interface value.  The first field is either
   20288                 :             : // a type descriptor for an empty interface or a pointer to the interface method
   20289                 :             : // table for a non-empty interface.  The second field is always the object.
   20290                 :             : 
   20291                 :             : class Interface_value_expression : public Expression
   20292                 :             : {
   20293                 :             :  public:
   20294                 :      229082 :   Interface_value_expression(Type* type, Expression* first_field,
   20295                 :             :                              Expression* obj, Location location)
   20296                 :      229082 :       : Expression(EXPRESSION_INTERFACE_VALUE, location),
   20297                 :      458164 :         type_(type), first_field_(first_field), obj_(obj)
   20298                 :             :   { }
   20299                 :             : 
   20300                 :             :  protected:
   20301                 :             :   int
   20302                 :             :   do_traverse(Traverse*);
   20303                 :             : 
   20304                 :             :   Type*
   20305                 :         847 :   do_type()
   20306                 :         847 :   { return this->type_; }
   20307                 :             : 
   20308                 :             :   void
   20309                 :             :   do_determine_type(Gogo*, const Type_context*);
   20310                 :             : 
   20311                 :             :   Expression*
   20312                 :           0 :   do_copy()
   20313                 :             :   {
   20314                 :           0 :     return new Interface_value_expression(this->type_->copy_expressions(),
   20315                 :           0 :                                           this->first_field_->copy(),
   20316                 :           0 :                                           this->obj_->copy(), this->location());
   20317                 :             :   }
   20318                 :             : 
   20319                 :             :   Bexpression*
   20320                 :             :   do_get_backend(Translate_context* context);
   20321                 :             : 
   20322                 :             :   void
   20323                 :             :   do_dump_expression(Ast_dump_context*) const;
   20324                 :             : 
   20325                 :             :  private:
   20326                 :             :   // The type of the interface value.
   20327                 :             :   Type* type_;
   20328                 :             :   // The first field of the interface (either a type descriptor or a pointer
   20329                 :             :   // to the method table.
   20330                 :             :   Expression* first_field_;
   20331                 :             :   // The underlying object of the interface.
   20332                 :             :   Expression* obj_;
   20333                 :             : };
   20334                 :             : 
   20335                 :             : int
   20336                 :           0 : Interface_value_expression::do_traverse(Traverse* traverse)
   20337                 :             : {
   20338                 :           0 :   if (Expression::traverse(&this->first_field_, traverse) == TRAVERSE_EXIT
   20339                 :           0 :       || Expression::traverse(&this->obj_, traverse) == TRAVERSE_EXIT)
   20340                 :           0 :     return TRAVERSE_EXIT;
   20341                 :             :   return TRAVERSE_CONTINUE;
   20342                 :             : }
   20343                 :             : 
   20344                 :             : void
   20345                 :      281130 : Interface_value_expression::do_determine_type(Gogo* gogo, const Type_context*)
   20346                 :             : {
   20347                 :      281130 :   this->first_field_->determine_type_no_context(gogo);
   20348                 :      281130 :   this->obj_->determine_type_no_context(gogo);
   20349                 :      281130 : }
   20350                 :             : 
   20351                 :             : Bexpression*
   20352                 :      229082 : Interface_value_expression::do_get_backend(Translate_context* context)
   20353                 :             : {
   20354                 :      229082 :   std::vector<Bexpression*> vals(2);
   20355                 :      229082 :   vals[0] = this->first_field_->get_backend(context);
   20356                 :      229082 :   vals[1] = this->obj_->get_backend(context);
   20357                 :             : 
   20358                 :      229082 :   Gogo* gogo = context->gogo();
   20359                 :      229082 :   Btype* btype = this->type_->get_backend(gogo);
   20360                 :      229082 :   return gogo->backend()->constructor_expression(btype, vals, this->location());
   20361                 :      229082 : }
   20362                 :             : 
   20363                 :             : void
   20364                 :           0 : Interface_value_expression::do_dump_expression(
   20365                 :             :     Ast_dump_context* ast_dump_context) const
   20366                 :             : {
   20367                 :           0 :   ast_dump_context->ostream() << "interfacevalue(";
   20368                 :           0 :   ast_dump_context->ostream() <<
   20369                 :           0 :       (this->type_->interface_type()->is_empty()
   20370                 :             :        ? "type_descriptor: "
   20371                 :           0 :        : "methods: ");
   20372                 :           0 :   this->first_field_->dump_expression(ast_dump_context);
   20373                 :           0 :   ast_dump_context->ostream() << ", object: ";
   20374                 :           0 :   this->obj_->dump_expression(ast_dump_context);
   20375                 :           0 :   ast_dump_context->ostream() << ")";
   20376                 :           0 : }
   20377                 :             : 
   20378                 :             : Expression*
   20379                 :      229082 : Expression::make_interface_value(Type* type, Expression* first_value,
   20380                 :             :                                  Expression* object, Location location)
   20381                 :             : {
   20382                 :      229082 :   return new Interface_value_expression(type, first_value, object, location);
   20383                 :             : }
   20384                 :             : 
   20385                 :             : // An interface method table for a pair of types: an interface type and a type
   20386                 :             : // that implements that interface.
   20387                 :             : 
   20388                 :             : class Interface_mtable_expression : public Expression
   20389                 :             : {
   20390                 :             :  public:
   20391                 :       66004 :   Interface_mtable_expression(Interface_type* itype, Type* type,
   20392                 :             :                               bool is_pointer, Location location)
   20393                 :       66004 :       : Expression(EXPRESSION_INTERFACE_MTABLE, location),
   20394                 :       66004 :         itype_(itype), type_(type), is_pointer_(is_pointer),
   20395                 :      132008 :         method_table_type_(NULL), bvar_(NULL)
   20396                 :             :   { }
   20397                 :             : 
   20398                 :             :  protected:
   20399                 :             :   int
   20400                 :             :   do_traverse(Traverse*);
   20401                 :             : 
   20402                 :             :   Type*
   20403                 :             :   do_type();
   20404                 :             : 
   20405                 :             :   bool
   20406                 :           0 :   do_is_static_initializer() const
   20407                 :           0 :   { return true; }
   20408                 :             : 
   20409                 :             :   void
   20410                 :       43409 :   do_determine_type(Gogo*, const Type_context*)
   20411                 :       43409 :   { }
   20412                 :             : 
   20413                 :             :   Expression*
   20414                 :           0 :   do_copy()
   20415                 :             :   {
   20416                 :           0 :     Interface_type* itype = this->itype_->copy_expressions()->interface_type();
   20417                 :           0 :     return new Interface_mtable_expression(itype,
   20418                 :           0 :                                            this->type_->copy_expressions(),
   20419                 :           0 :                                            this->is_pointer_, this->location());
   20420                 :             :   }
   20421                 :             : 
   20422                 :             :   bool
   20423                 :           0 :   do_is_addressable() const
   20424                 :           0 :   { return true; }
   20425                 :             : 
   20426                 :             :   Bexpression*
   20427                 :             :   do_get_backend(Translate_context* context);
   20428                 :             : 
   20429                 :             :   void
   20430                 :             :   do_dump_expression(Ast_dump_context*) const;
   20431                 :             : 
   20432                 :             :  private:
   20433                 :             :   // The interface type for which the methods are defined.
   20434                 :             :   Interface_type* itype_;
   20435                 :             :   // The type to construct the interface method table for.
   20436                 :             :   Type* type_;
   20437                 :             :   // Whether this table contains the method set for the receiver type or the
   20438                 :             :   // pointer receiver type.
   20439                 :             :   bool is_pointer_;
   20440                 :             :   // The type of the method table.
   20441                 :             :   Type* method_table_type_;
   20442                 :             :   // The backend variable that refers to the interface method table.
   20443                 :             :   Bvariable* bvar_;
   20444                 :             : };
   20445                 :             : 
   20446                 :             : int
   20447                 :           0 : Interface_mtable_expression::do_traverse(Traverse* traverse)
   20448                 :             : {
   20449                 :           0 :   if (Type::traverse(this->itype_, traverse) == TRAVERSE_EXIT
   20450                 :           0 :       || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
   20451                 :           0 :     return TRAVERSE_EXIT;
   20452                 :             :   return TRAVERSE_CONTINUE;
   20453                 :             : }
   20454                 :             : 
   20455                 :             : Type*
   20456                 :      217370 : Interface_mtable_expression::do_type()
   20457                 :             : {
   20458                 :      217370 :   if (this->method_table_type_ != NULL)
   20459                 :             :     return this->method_table_type_;
   20460                 :             : 
   20461                 :       66004 :   const Typed_identifier_list* interface_methods = this->itype_->methods();
   20462                 :       66004 :   go_assert(!interface_methods->empty());
   20463                 :             : 
   20464                 :       66004 :   Struct_field_list* sfl = new Struct_field_list;
   20465                 :       66004 :   Typed_identifier tid("__type_descriptor", Type::make_type_descriptor_ptr_type(),
   20466                 :       66004 :                        this->location());
   20467                 :       66004 :   sfl->push_back(Struct_field(tid));
   20468                 :       66004 :   Type* unsafe_ptr_type = Type::make_pointer_type(Type::make_void_type());
   20469                 :       66004 :   for (Typed_identifier_list::const_iterator p = interface_methods->begin();
   20470                 :      510485 :        p != interface_methods->end();
   20471                 :      444481 :        ++p)
   20472                 :             :     {
   20473                 :             :       // We want C function pointers here, not func descriptors; model
   20474                 :             :       // using void* pointers.
   20475                 :      444481 :       Typed_identifier method(p->name(), unsafe_ptr_type, p->location());
   20476                 :      444481 :       sfl->push_back(Struct_field(method));
   20477                 :      444481 :     }
   20478                 :       66004 :   Struct_type* st = Type::make_struct_type(sfl, this->location());
   20479                 :       66004 :   st->set_is_struct_incomparable();
   20480                 :       66004 :   this->method_table_type_ = st;
   20481                 :       66004 :   return this->method_table_type_;
   20482                 :       66004 : }
   20483                 :             : 
   20484                 :             : Bexpression*
   20485                 :      182999 : Interface_mtable_expression::do_get_backend(Translate_context* context)
   20486                 :             : {
   20487                 :      182999 :   Gogo* gogo = context->gogo();
   20488                 :      182999 :   Location loc = Linemap::predeclared_location();
   20489                 :      182999 :   if (this->bvar_ != NULL)
   20490                 :      116995 :     return gogo->backend()->var_expression(this->bvar_, this->location());
   20491                 :             : 
   20492                 :       66004 :   const Typed_identifier_list* interface_methods = this->itype_->methods();
   20493                 :       66004 :   go_assert(!interface_methods->empty());
   20494                 :             : 
   20495                 :       66004 :   std::string mangled_name =
   20496                 :             :     gogo->interface_method_table_name(this->itype_, this->type_,
   20497                 :       66004 :                                       this->is_pointer_);
   20498                 :             : 
   20499                 :             :   // Set is_public if we are converting a named type to an interface
   20500                 :             :   // type that is defined in the same package as the named type, and
   20501                 :             :   // the interface has hidden methods.  In that case the interface
   20502                 :             :   // method table will be defined by the package that defines the
   20503                 :             :   // types.
   20504                 :       66004 :   bool is_public = false;
   20505                 :       66004 :   if (this->type_->named_type() != NULL
   20506                 :       66004 :       && (this->type_->named_type()->named_object()->package()
   20507                 :       65903 :           == this->itype_->package()))
   20508                 :             :     {
   20509                 :      259537 :       for (Typed_identifier_list::const_iterator p = interface_methods->begin();
   20510                 :      259537 :            p != interface_methods->end();
   20511                 :      217227 :            ++p)
   20512                 :             :         {
   20513                 :      254987 :           if (Gogo::is_hidden_name(p->name()))
   20514                 :             :             {
   20515                 :             :               is_public = true;
   20516                 :             :               break;
   20517                 :             :             }
   20518                 :             :         }
   20519                 :             :     }
   20520                 :             : 
   20521                 :       42310 :   if (is_public
   20522                 :       42310 :       && this->type_->named_type()->named_object()->package() != NULL)
   20523                 :             :     {
   20524                 :             :       // The interface conversion table is defined elsewhere.
   20525                 :       34371 :       Btype* btype = this->type()->get_backend(gogo);
   20526                 :       68742 :       this->bvar_ =
   20527                 :       68742 :           gogo->backend()->immutable_struct_reference(mangled_name, "",
   20528                 :             :                                                       btype, loc);
   20529                 :       34371 :       return gogo->backend()->var_expression(this->bvar_, this->location());
   20530                 :             :     }
   20531                 :             : 
   20532                 :             :   // The first element is the type descriptor.
   20533                 :       31633 :   Type* td_type;
   20534                 :       31633 :   if (!this->is_pointer_)
   20535                 :       13998 :     td_type = this->type_;
   20536                 :             :   else
   20537                 :       17635 :     td_type = Type::make_pointer_type(this->type_);
   20538                 :             : 
   20539                 :       31633 :   std::vector<Backend::Btyped_identifier> bstructfields;
   20540                 :             : 
   20541                 :             :   // Build an interface method table for a type: a type descriptor followed by a
   20542                 :             :   // list of function pointers, one for each interface method.  This is used for
   20543                 :             :   // interfaces.
   20544                 :       31633 :   Expression_list* svals = new Expression_list();
   20545                 :       31633 :   Expression* tdescriptor = Expression::make_type_descriptor(td_type, loc);
   20546                 :       31633 :   svals->push_back(tdescriptor);
   20547                 :             : 
   20548                 :       31633 :   Btype* tdesc_btype = tdescriptor->type()->get_backend(gogo);
   20549                 :       31633 :   Backend::Btyped_identifier btd("_type", tdesc_btype, loc);
   20550                 :       31633 :   bstructfields.push_back(btd);
   20551                 :             : 
   20552                 :       31633 :   Named_type* nt = this->type_->named_type();
   20553                 :       31633 :   Struct_type* st = this->type_->struct_type();
   20554                 :       31633 :   go_assert(nt != NULL || st != NULL);
   20555                 :             : 
   20556                 :       31633 :   for (Typed_identifier_list::const_iterator p = interface_methods->begin();
   20557                 :      217683 :        p != interface_methods->end();
   20558                 :      186050 :        ++p)
   20559                 :             :     {
   20560                 :      186050 :       bool is_ambiguous;
   20561                 :      186050 :       Method* m;
   20562                 :      186050 :       if (nt != NULL)
   20563                 :      185878 :         m = nt->method_function(p->name(), &is_ambiguous);
   20564                 :             :       else
   20565                 :         172 :         m = st->method_function(p->name(), &is_ambiguous);
   20566                 :      186050 :       go_assert(m != NULL);
   20567                 :             : 
   20568                 :             :       // See the comment in Type::method_constructor.
   20569                 :      186050 :       bool use_direct_iface_stub = false;
   20570                 :      186050 :       if (m->is_value_method()
   20571                 :       22460 :           && this->is_pointer_
   20572                 :      191919 :           && this->type_->is_direct_iface_type())
   20573                 :             :         use_direct_iface_stub = true;
   20574                 :      186050 :       if (!m->is_value_method()
   20575                 :      163590 :           && this->is_pointer_
   20576                 :      273696 :           && !this->type_->in_heap())
   20577                 :             :         use_direct_iface_stub = true;
   20578                 :      186050 :       Named_object* no = (use_direct_iface_stub
   20579                 :      186050 :                           ? m->iface_stub_object()
   20580                 :      185876 :                           : m->named_object());
   20581                 :             : 
   20582                 :      186050 :       go_assert(no->is_function() || no->is_function_declaration());
   20583                 :             : 
   20584                 :      186050 :       Function_type* fcn_type = (no->is_function()
   20585                 :      186050 :                                  ? no->func_value()->type()
   20586                 :      158248 :                                  : no->func_declaration_value()->type());
   20587                 :      186050 :       Btype* fcn_btype = fcn_type->get_backend_fntype(gogo);
   20588                 :      186050 :       Backend::Btyped_identifier bmtype(p->name(), fcn_btype, loc);
   20589                 :      186050 :       bstructfields.push_back(bmtype);
   20590                 :             : 
   20591                 :      186050 :       svals->push_back(Expression::make_func_code_reference(no, loc));
   20592                 :      186050 :     }
   20593                 :             : 
   20594                 :       31633 :   Btype *btype = gogo->backend()->struct_type(bstructfields);
   20595                 :       31633 :   std::vector<Bexpression*> ctor_bexprs;
   20596                 :       31633 :   for (Expression_list::const_iterator pe = svals->begin();
   20597                 :      249316 :        pe != svals->end();
   20598                 :      217683 :        ++pe)
   20599                 :             :     {
   20600                 :      217683 :       ctor_bexprs.push_back((*pe)->get_backend(context));
   20601                 :             :     }
   20602                 :       31633 :   Bexpression* ctor =
   20603                 :       31633 :       gogo->backend()->constructor_expression(btype, ctor_bexprs, loc);
   20604                 :             : 
   20605                 :       31633 :   unsigned int flags = 0;
   20606                 :       31633 :   if (!is_public)
   20607                 :       28244 :     flags |= Backend::variable_is_hidden;
   20608                 :       31633 :   this->bvar_ = gogo->backend()->immutable_struct(mangled_name, "", flags,
   20609                 :             :                                                   btype, loc);
   20610                 :       31633 :   gogo->backend()->immutable_struct_set_init(this->bvar_, mangled_name, flags,
   20611                 :             :                                              btype, loc, ctor);
   20612                 :       31633 :   return gogo->backend()->var_expression(this->bvar_, loc);
   20613                 :       97637 : }
   20614                 :             : 
   20615                 :             : void
   20616                 :           0 : Interface_mtable_expression::do_dump_expression(
   20617                 :             :     Ast_dump_context* ast_dump_context) const
   20618                 :             : {
   20619                 :           0 :   ast_dump_context->ostream() << "__go_"
   20620                 :           0 :                               << (this->is_pointer_ ? "pimt__" : "imt_");
   20621                 :           0 :   ast_dump_context->dump_type(this->itype_);
   20622                 :           0 :   ast_dump_context->ostream() << "__";
   20623                 :           0 :   ast_dump_context->dump_type(this->type_);
   20624                 :           0 : }
   20625                 :             : 
   20626                 :             : Expression*
   20627                 :       66004 : Expression::make_interface_mtable_ref(Interface_type* itype, Type* type,
   20628                 :             :                                       bool is_pointer, Location location)
   20629                 :             : {
   20630                 :       66004 :   return new Interface_mtable_expression(itype, type, is_pointer, location);
   20631                 :             : }
   20632                 :             : 
   20633                 :             : // An expression which evaluates to the offset of a field within a
   20634                 :             : // struct.  This, like Type_info_expression, q.v., is only used to
   20635                 :             : // initialize fields of a type descriptor.
   20636                 :             : 
   20637                 :             : class Struct_field_offset_expression : public Expression
   20638                 :             : {
   20639                 :             :  public:
   20640                 :      188405 :   Struct_field_offset_expression(Struct_type* type, const Struct_field* field)
   20641                 :      188405 :     : Expression(EXPRESSION_STRUCT_FIELD_OFFSET,
   20642                 :             :                  Linemap::predeclared_location()),
   20643                 :      188405 :       type_(type), field_(field)
   20644                 :      188405 :   { }
   20645                 :             : 
   20646                 :             :  protected:
   20647                 :             :   bool
   20648                 :      235580 :   do_is_static_initializer() const
   20649                 :      235580 :   { return true; }
   20650                 :             : 
   20651                 :             :   Type*
   20652                 :      310939 :   do_type()
   20653                 :      310939 :   { return Type::lookup_integer_type("uintptr"); }
   20654                 :             : 
   20655                 :             :   void
   20656                 :      191886 :   do_determine_type(Gogo*, const Type_context*)
   20657                 :      191886 :   { }
   20658                 :             : 
   20659                 :             :   Expression*
   20660                 :           0 :   do_copy()
   20661                 :           0 :   { return this; }
   20662                 :             : 
   20663                 :             :   Bexpression*
   20664                 :             :   do_get_backend(Translate_context* context);
   20665                 :             : 
   20666                 :             :   void
   20667                 :             :   do_dump_expression(Ast_dump_context*) const;
   20668                 :             : 
   20669                 :             :  private:
   20670                 :             :   // The type of the struct.
   20671                 :             :   Struct_type* type_;
   20672                 :             :   // The field.
   20673                 :             :   const Struct_field* field_;
   20674                 :             : };
   20675                 :             : 
   20676                 :             : // Return the backend representation for a struct field offset.
   20677                 :             : 
   20678                 :             : Bexpression*
   20679                 :      121692 : Struct_field_offset_expression::do_get_backend(Translate_context* context)
   20680                 :             : {
   20681                 :      121692 :   const Struct_field_list* fields = this->type_->fields();
   20682                 :      121692 :   Struct_field_list::const_iterator p;
   20683                 :      121692 :   unsigned i = 0;
   20684                 :      121692 :   for (p = fields->begin();
   20685                 :      558987 :        p != fields->end();
   20686                 :      437295 :        ++p, ++i)
   20687                 :      558987 :     if (&*p == this->field_)
   20688                 :             :       break;
   20689                 :      121692 :   go_assert(&*p == this->field_);
   20690                 :             : 
   20691                 :      121692 :   Gogo* gogo = context->gogo();
   20692                 :      121692 :   Btype* btype = this->type_->get_backend(gogo);
   20693                 :             : 
   20694                 :      121692 :   int64_t offset = gogo->backend()->type_field_offset(btype, i);
   20695                 :      121692 :   Type* uptr_type = Type::lookup_integer_type("uintptr");
   20696                 :      121692 :   Expression* ret =
   20697                 :      121692 :     Expression::make_integer_int64(offset, uptr_type,
   20698                 :             :                                    Linemap::predeclared_location());
   20699                 :      121692 :   return ret->get_backend(context);
   20700                 :             : }
   20701                 :             : 
   20702                 :             : // Dump ast representation for a struct field offset expression.
   20703                 :             : 
   20704                 :             : void
   20705                 :           0 : Struct_field_offset_expression::do_dump_expression(
   20706                 :             :     Ast_dump_context* ast_dump_context) const
   20707                 :             : {
   20708                 :           0 :   ast_dump_context->ostream() <<  "unsafe.Offsetof(";
   20709                 :           0 :   ast_dump_context->dump_type(this->type_);
   20710                 :           0 :   ast_dump_context->ostream() << '.';
   20711                 :           0 :   ast_dump_context->ostream() <<
   20712                 :           0 :     Gogo::message_name(this->field_->field_name());
   20713                 :           0 :   ast_dump_context->ostream() << ")";
   20714                 :           0 : }
   20715                 :             : 
   20716                 :             : // Make an expression for a struct field offset.
   20717                 :             : 
   20718                 :             : Expression*
   20719                 :      188405 : Expression::make_struct_field_offset(Struct_type* type,
   20720                 :             :                                      const Struct_field* field)
   20721                 :             : {
   20722                 :      188405 :   return new Struct_field_offset_expression(type, field);
   20723                 :             : }
   20724                 :             : 
   20725                 :             : // An expression which evaluates to the address of an unnamed label.
   20726                 :             : 
   20727                 :             : class Label_addr_expression : public Expression
   20728                 :             : {
   20729                 :             :  public:
   20730                 :        8840 :   Label_addr_expression(Label* label, Location location)
   20731                 :        8840 :     : Expression(EXPRESSION_LABEL_ADDR, location),
   20732                 :           0 :       label_(label)
   20733                 :             :   { }
   20734                 :             : 
   20735                 :             :  protected:
   20736                 :             :   Type*
   20737                 :       61880 :   do_type()
   20738                 :       61880 :   { return Type::make_pointer_type(Type::make_void_type()); }
   20739                 :             : 
   20740                 :             :   void
   20741                 :       17680 :   do_determine_type(Gogo*, const Type_context*)
   20742                 :       17680 :   { }
   20743                 :             : 
   20744                 :             :   Expression*
   20745                 :           0 :   do_copy()
   20746                 :           0 :   { return new Label_addr_expression(this->label_, this->location()); }
   20747                 :             : 
   20748                 :             :   Bexpression*
   20749                 :        8840 :   do_get_backend(Translate_context* context)
   20750                 :        8840 :   { return this->label_->get_addr(context, this->location()); }
   20751                 :             : 
   20752                 :             :   void
   20753                 :           0 :   do_dump_expression(Ast_dump_context* ast_dump_context) const
   20754                 :           0 :   { ast_dump_context->ostream() << this->label_->name(); }
   20755                 :             : 
   20756                 :             :  private:
   20757                 :             :   // The label whose address we are taking.
   20758                 :             :   Label* label_;
   20759                 :             : };
   20760                 :             : 
   20761                 :             : // Make an expression for the address of an unnamed label.
   20762                 :             : 
   20763                 :             : Expression*
   20764                 :        8840 : Expression::make_label_addr(Label* label, Location location)
   20765                 :             : {
   20766                 :        8840 :   return new Label_addr_expression(label, location);
   20767                 :             : }
   20768                 :             : 
   20769                 :             : // Class Conditional_expression.
   20770                 :             : 
   20771                 :             : // Traversal.
   20772                 :             : 
   20773                 :             : int
   20774                 :      217442 : Conditional_expression::do_traverse(Traverse* traverse)
   20775                 :             : {
   20776                 :      217442 :   if (Expression::traverse(&this->cond_, traverse) == TRAVERSE_EXIT
   20777                 :      206217 :       || Expression::traverse(&this->then_, traverse) == TRAVERSE_EXIT
   20778                 :      423659 :       || Expression::traverse(&this->else_, traverse) == TRAVERSE_EXIT)
   20779                 :       11225 :     return TRAVERSE_EXIT;
   20780                 :             :   return TRAVERSE_CONTINUE;
   20781                 :             : }
   20782                 :             : 
   20783                 :             : // Return the type of the conditional expression.
   20784                 :             : 
   20785                 :             : Type*
   20786                 :      548743 : Conditional_expression::do_type()
   20787                 :             : {
   20788                 :      548743 :   Type* result_type = Type::make_void_type();
   20789                 :      548743 :   if (Type::are_identical(this->then_->type(), this->else_->type(),
   20790                 :             :                           Type::COMPARE_ERRORS | Type::COMPARE_TAGS,
   20791                 :             :                           NULL))
   20792                 :      201258 :     result_type = this->then_->type();
   20793                 :      347485 :   else if (this->then_->is_nil_expression()
   20794                 :      347485 :            || this->else_->is_nil_expression())
   20795                 :      124904 :     result_type = (!this->then_->is_nil_expression()
   20796                 :      124904 :                    ? this->then_->type()
   20797                 :      122984 :                    : this->else_->type());
   20798                 :      548743 :   return result_type;
   20799                 :             : }
   20800                 :             : 
   20801                 :             : // Determine type for a conditional expression.
   20802                 :             : 
   20803                 :             : void
   20804                 :      439872 : Conditional_expression::do_determine_type(Gogo* gogo,
   20805                 :             :                                           const Type_context* context)
   20806                 :             : {
   20807                 :      439872 :   this->cond_->determine_type_no_context(gogo);
   20808                 :      439872 :   this->then_->determine_type(gogo, context);
   20809                 :      439872 :   this->else_->determine_type(gogo, context);
   20810                 :      439872 : }
   20811                 :             : 
   20812                 :             : // Get the backend representation of a conditional expression.
   20813                 :             : 
   20814                 :             : Bexpression*
   20815                 :      380054 : Conditional_expression::do_get_backend(Translate_context* context)
   20816                 :             : {
   20817                 :      380054 :   Gogo* gogo = context->gogo();
   20818                 :      380054 :   Btype* result_btype = this->type()->get_backend(gogo);
   20819                 :      380054 :   Bexpression* cond = this->cond_->get_backend(context);
   20820                 :      380054 :   Bexpression* then = this->then_->get_backend(context);
   20821                 :      380054 :   Bexpression* belse = this->else_->get_backend(context);
   20822                 :      380054 :   Bfunction* bfn = context->function()->func_value()->get_decl();
   20823                 :      380054 :   return gogo->backend()->conditional_expression(bfn, result_btype, cond, then,
   20824                 :      380054 :                                                  belse, this->location());
   20825                 :             : }
   20826                 :             : 
   20827                 :             : // Dump ast representation of a conditional expression.
   20828                 :             : 
   20829                 :             : void
   20830                 :           0 : Conditional_expression::do_dump_expression(
   20831                 :             :     Ast_dump_context* ast_dump_context) const
   20832                 :             : {
   20833                 :           0 :   ast_dump_context->ostream() << "(";
   20834                 :           0 :   ast_dump_context->dump_expression(this->cond_);
   20835                 :           0 :   ast_dump_context->ostream() << " ? ";
   20836                 :           0 :   ast_dump_context->dump_expression(this->then_);
   20837                 :           0 :   ast_dump_context->ostream() << " : ";
   20838                 :           0 :   ast_dump_context->dump_expression(this->else_);
   20839                 :           0 :   ast_dump_context->ostream() << ") ";
   20840                 :           0 : }
   20841                 :             : 
   20842                 :             : // Make a conditional expression.
   20843                 :             : 
   20844                 :             : Expression*
   20845                 :      381963 : Expression::make_conditional(Expression* cond, Expression* then,
   20846                 :             :                              Expression* else_expr, Location location)
   20847                 :             : {
   20848                 :      381963 :   return new Conditional_expression(cond, then, else_expr, location);
   20849                 :             : }
   20850                 :             : 
   20851                 :             : // Class Compound_expression.
   20852                 :             : 
   20853                 :             : // Traversal.
   20854                 :             : 
   20855                 :             : int
   20856                 :       14468 : Compound_expression::do_traverse(Traverse* traverse)
   20857                 :             : {
   20858                 :       14468 :   if (Expression::traverse(&this->init_, traverse) == TRAVERSE_EXIT
   20859                 :       14468 :       || Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT)
   20860                 :           0 :     return TRAVERSE_EXIT;
   20861                 :             :   return TRAVERSE_CONTINUE;
   20862                 :             : }
   20863                 :             : 
   20864                 :             : // Return the type of the compound expression.
   20865                 :             : 
   20866                 :             : Type*
   20867                 :       14131 : Compound_expression::do_type()
   20868                 :             : {
   20869                 :       14131 :   return this->expr_->type();
   20870                 :             : }
   20871                 :             : 
   20872                 :             : // Determine type for a compound expression.
   20873                 :             : 
   20874                 :             : void
   20875                 :      103569 : Compound_expression::do_determine_type(Gogo* gogo, const Type_context* context)
   20876                 :             : {
   20877                 :      103569 :   this->init_->determine_type_no_context(gogo);
   20878                 :      103569 :   this->expr_->determine_type(gogo, context);
   20879                 :      103569 : }
   20880                 :             : 
   20881                 :             : // Get the backend representation of a compound expression.
   20882                 :             : 
   20883                 :             : Bexpression*
   20884                 :      100032 : Compound_expression::do_get_backend(Translate_context* context)
   20885                 :             : {
   20886                 :      100032 :   Gogo* gogo = context->gogo();
   20887                 :      100032 :   Bexpression* binit = this->init_->get_backend(context);
   20888                 :      100032 :   Bfunction* bfunction = context->function()->func_value()->get_decl();
   20889                 :      100032 :   Bstatement* init_stmt = gogo->backend()->expression_statement(bfunction,
   20890                 :             :                                                                 binit);
   20891                 :      100032 :   Bexpression* bexpr = this->expr_->get_backend(context);
   20892                 :      100032 :   return gogo->backend()->compound_expression(init_stmt, bexpr,
   20893                 :      100032 :                                               this->location());
   20894                 :             : }
   20895                 :             : 
   20896                 :             : // Dump ast representation of a conditional expression.
   20897                 :             : 
   20898                 :             : void
   20899                 :           0 : Compound_expression::do_dump_expression(
   20900                 :             :     Ast_dump_context* ast_dump_context) const
   20901                 :             : {
   20902                 :           0 :   ast_dump_context->ostream() << "(";
   20903                 :           0 :   ast_dump_context->dump_expression(this->init_);
   20904                 :           0 :   ast_dump_context->ostream() << ",";
   20905                 :           0 :   ast_dump_context->dump_expression(this->expr_);
   20906                 :           0 :   ast_dump_context->ostream() << ") ";
   20907                 :           0 : }
   20908                 :             : 
   20909                 :             : // Make a compound expression.
   20910                 :             : 
   20911                 :             : Expression*
   20912                 :      100033 : Expression::make_compound(Expression* init, Expression* expr, Location location)
   20913                 :             : {
   20914                 :      100033 :   return new Compound_expression(init, expr, location);
   20915                 :             : }
   20916                 :             : 
   20917                 :             : // Class Backend_expression.
   20918                 :             : 
   20919                 :             : int
   20920                 :           0 : Backend_expression::do_traverse(Traverse*)
   20921                 :             : {
   20922                 :           0 :   return TRAVERSE_CONTINUE;
   20923                 :             : }
   20924                 :             : 
   20925                 :             : Expression*
   20926                 :           0 : Backend_expression::do_copy()
   20927                 :             : {
   20928                 :           0 :   return new Backend_expression(this->bexpr_, this->type_->copy_expressions(),
   20929                 :           0 :                                 this->location());
   20930                 :             : }
   20931                 :             : 
   20932                 :             : void
   20933                 :           0 : Backend_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
   20934                 :             : {
   20935                 :           0 :   ast_dump_context->ostream() << "backend_expression<";
   20936                 :           0 :   ast_dump_context->dump_type(this->type_);
   20937                 :           0 :   ast_dump_context->ostream() << ">";
   20938                 :           0 : }
   20939                 :             : 
   20940                 :             : Expression*
   20941                 :      769204 : Expression::make_backend(Bexpression* bexpr, Type* type, Location location)
   20942                 :             : {
   20943                 :      769204 :   return new Backend_expression(bexpr, type, location);
   20944                 :             : }
   20945                 :             : 
   20946                 :             : // Import an expression.  This comes at the end in order to see the
   20947                 :             : // various class definitions.
   20948                 :             : 
   20949                 :             : Expression*
   20950                 :     1115128 : Expression::import_expression(Import_expression* imp, Location loc)
   20951                 :             : {
   20952                 :     1115128 :   Expression* expr = Expression::import_expression_without_suffix(imp, loc);
   20953                 :     1137049 :   while (true)
   20954                 :             :     {
   20955                 :     1137049 :       if (imp->match_c_string("("))
   20956                 :             :         {
   20957                 :       18194 :           imp->advance(1);
   20958                 :       18194 :           Expression_list* args = new Expression_list();
   20959                 :       18194 :           bool is_varargs = false;
   20960                 :       26443 :           while (!imp->match_c_string(")"))
   20961                 :             :             {
   20962                 :       23597 :               Expression* arg = Expression::import_expression(imp, loc);
   20963                 :       23597 :               if (arg->is_error_expression())
   20964                 :           0 :                 return arg;
   20965                 :       23597 :               args->push_back(arg);
   20966                 :       23597 :               if (imp->match_c_string(")"))
   20967                 :             :                 break;
   20968                 :        8571 :               else if (imp->match_c_string("...)"))
   20969                 :             :                 {
   20970                 :         322 :                   imp->advance(3);
   20971                 :         322 :                   is_varargs = true;
   20972                 :         322 :                   break;
   20973                 :             :                 }
   20974                 :        8249 :               imp->require_c_string(", ");
   20975                 :             :             }
   20976                 :       18194 :           imp->require_c_string(")");
   20977                 :       18194 :           expr = Expression::make_call(expr, args, is_varargs, loc);
   20978                 :       18194 :           expr->call_expression()->set_varargs_are_lowered();
   20979                 :             :         }
   20980                 :     1118855 :       else if (imp->match_c_string("["))
   20981                 :             :         {
   20982                 :        3727 :           imp->advance(1);
   20983                 :        3727 :           Expression* start = Expression::import_expression(imp, loc);
   20984                 :        3727 :           Expression* end = NULL;
   20985                 :        3727 :           Expression* cap = NULL;
   20986                 :        3727 :           if (imp->match_c_string(":"))
   20987                 :             :             {
   20988                 :        2119 :               imp->advance(1);
   20989                 :        2119 :               int c = imp->peek_char();
   20990                 :        2119 :               if (c == ':' || c == ']')
   20991                 :         898 :                 end = Expression::make_nil(loc);
   20992                 :             :               else
   20993                 :        1221 :                 end = Expression::import_expression(imp, loc);
   20994                 :        2119 :               if (imp->match_c_string(":"))
   20995                 :             :                 {
   20996                 :           0 :                   imp->advance(1);
   20997                 :           0 :                   cap = Expression::import_expression(imp, loc);
   20998                 :             :                 }
   20999                 :             :             }
   21000                 :        3727 :           imp->require_c_string("]");
   21001                 :        3727 :           expr = Expression::make_index(expr, start, end, cap, loc);
   21002                 :             :         }
   21003                 :             :       else
   21004                 :             :         break;
   21005                 :             :     }
   21006                 :             : 
   21007                 :             :   return expr;
   21008                 :             : }
   21009                 :             : 
   21010                 :             : // Import an expression without considering a suffix (function
   21011                 :             : // arguments, index operations, etc.).
   21012                 :             : 
   21013                 :             : Expression*
   21014                 :     1115128 : Expression::import_expression_without_suffix(Import_expression* imp,
   21015                 :             :                                              Location loc)
   21016                 :             : {
   21017                 :     1115128 :   int c = imp->peek_char();
   21018                 :     1115128 :   if (c == '+' || c == '-' || c == '!' || c == '^' || c == '&' || c == '*')
   21019                 :        9633 :     return Unary_expression::do_import(imp, loc);
   21020                 :             :   else if (c == '(')
   21021                 :       22026 :     return Binary_expression::do_import(imp, loc);
   21022                 :     1083469 :   else if (imp->match_c_string("$true")
   21023                 :     1082099 :            || imp->match_c_string("$false")
   21024                 :     2164079 :            || (imp->version() < EXPORT_FORMAT_V3
   21025                 :           0 :                && (imp->match_c_string("true")
   21026                 :           0 :                    || imp->match_c_string("false"))))
   21027                 :        2859 :     return Boolean_expression::do_import(imp, loc);
   21028                 :     1080610 :   else if (c == '"')
   21029                 :       31858 :     return String_expression::do_import(imp, loc);
   21030                 :     1048752 :   else if (c == '-' || (c >= '0' && c <= '9'))
   21031                 :             :     {
   21032                 :             :       // This handles integers, floats and complex constants.
   21033                 :      953779 :       return Integer_expression::do_import(imp, loc);
   21034                 :             :     }
   21035                 :       94973 :   else if (imp->match_c_string("<-"))
   21036                 :           0 :     return Receive_expression::do_import(imp, loc);
   21037                 :       94973 :   else if (imp->match_c_string("$nil")
   21038                 :       94973 :            || (imp->version() < EXPORT_FORMAT_V3
   21039                 :           0 :                && imp->match_c_string("nil")))
   21040                 :         621 :     return Nil_expression::do_import(imp, loc);
   21041                 :       94352 :   else if (imp->match_c_string("$convert")
   21042                 :       94352 :            || (imp->version() < EXPORT_FORMAT_V3
   21043                 :           0 :                && imp->match_c_string("convert")))
   21044                 :       18058 :     return Type_conversion_expression::do_import(imp, loc);
   21045                 :             : 
   21046                 :       76294 :   Import_function_body* ifb = imp->ifb();
   21047                 :       76294 :   if (ifb == NULL)
   21048                 :             :     {
   21049                 :           0 :       go_error_at(imp->location(), "import error: expected expression");
   21050                 :           0 :       return Expression::make_error(loc);
   21051                 :             :     }
   21052                 :       76294 :   if (ifb->saw_error())
   21053                 :           0 :     return Expression::make_error(loc);
   21054                 :             : 
   21055                 :       76294 :   if (ifb->match_c_string("$t"))
   21056                 :        3923 :     return Temporary_reference_expression::do_import(ifb, loc);
   21057                 :             : 
   21058                 :       72371 :   return Expression::import_identifier(ifb, loc);
   21059                 :             : }
   21060                 :             : 
   21061                 :             : // Import an identifier in an expression.  This is a reference to a
   21062                 :             : // variable or function.
   21063                 :             : 
   21064                 :             : Expression*
   21065                 :       72371 : Expression::import_identifier(Import_function_body* ifb, Location loc)
   21066                 :             : {
   21067                 :       72371 :   std::string id;
   21068                 :       72371 :   Package* pkg;
   21069                 :       72371 :   bool is_exported;
   21070                 :       72371 :   if (!Import::read_qualified_identifier(ifb, &id, &pkg, &is_exported))
   21071                 :             :     {
   21072                 :           0 :       if (!ifb->saw_error())
   21073                 :           0 :         go_error_at(ifb->location(),
   21074                 :             :                     "import error for %qs: bad qualified identifier at %lu",
   21075                 :           0 :                     ifb->name().c_str(),
   21076                 :           0 :                     static_cast<unsigned long>(ifb->off()));
   21077                 :           0 :       ifb->set_saw_error();
   21078                 :           0 :       return Expression::make_error(loc);
   21079                 :             :     }
   21080                 :             : 
   21081                 :       72371 :   Named_object* no = NULL;
   21082                 :       72371 :   if (pkg == NULL && is_exported)
   21083                 :       63256 :     no = ifb->block()->bindings()->lookup(id);
   21084                 :       63256 :   if (no == NULL)
   21085                 :             :     {
   21086                 :       20060 :       const Package* ipkg = pkg;
   21087                 :       20060 :       if (ipkg == NULL)
   21088                 :       16186 :         ipkg = ifb->function()->package();
   21089                 :       20060 :       if (!is_exported)
   21090                 :        5631 :         id = '.' + ipkg->pkgpath() + '.' + id;
   21091                 :       20060 :       no = ipkg->bindings()->lookup(id);
   21092                 :             :     }
   21093                 :       20060 :   if (no == NULL)
   21094                 :        6503 :     no = ifb->gogo()->lookup_global(id.c_str());
   21095                 :             : 
   21096                 :        6503 :   if (no == NULL)
   21097                 :             :     {
   21098                 :           0 :       if (!ifb->saw_error())
   21099                 :           0 :         go_error_at(ifb->location(),
   21100                 :             :                     "import error for %qs: lookup of %qs failed",
   21101                 :           0 :                     ifb->name().c_str(), id.c_str());
   21102                 :           0 :       ifb->set_saw_error();
   21103                 :           0 :       return Expression::make_error(loc);
   21104                 :             :     }
   21105                 :             : 
   21106                 :       72371 :   if (no->is_variable() || no->is_result_variable())
   21107                 :       54239 :     return Expression::make_var_reference(no, loc);
   21108                 :       18132 :   else if (no->is_function() || no->is_function_declaration())
   21109                 :       18132 :     return Expression::make_func_reference(no, NULL, loc);
   21110                 :             :   else
   21111                 :             :     {
   21112                 :           0 :       if (!ifb->saw_error())
   21113                 :           0 :         go_error_at(ifb->location(),
   21114                 :             :                     ("import error for %qs: "
   21115                 :             :                      "unexpected type of identifier %qs (%d)"),
   21116                 :           0 :                     ifb->name().c_str(),
   21117                 :           0 :                     id.c_str(), no->classification());
   21118                 :           0 :       ifb->set_saw_error();
   21119                 :           0 :       return Expression::make_error(loc);
   21120                 :             :     }
   21121                 :       72371 : }
   21122                 :             : 
   21123                 :             : // Class Expression_list.
   21124                 :             : 
   21125                 :             : // Traverse the list.
   21126                 :             : 
   21127                 :             : int
   21128                 :    22943034 : Expression_list::traverse(Traverse* traverse)
   21129                 :             : {
   21130                 :    83402674 :   for (Expression_list::iterator p = this->begin();
   21131                 :    83402674 :        p != this->end();
   21132                 :    60459640 :        ++p)
   21133                 :             :     {
   21134                 :    60664480 :       if (*p != NULL)
   21135                 :             :         {
   21136                 :    60375477 :           if (Expression::traverse(&*p, traverse) == TRAVERSE_EXIT)
   21137                 :    22943034 :             return TRAVERSE_EXIT;
   21138                 :             :         }
   21139                 :             :     }
   21140                 :             :   return TRAVERSE_CONTINUE;
   21141                 :             : }
   21142                 :             : 
   21143                 :             : // Copy the list.
   21144                 :             : 
   21145                 :             : Expression_list*
   21146                 :          11 : Expression_list::copy()
   21147                 :             : {
   21148                 :          11 :   Expression_list* ret = new Expression_list();
   21149                 :          22 :   for (Expression_list::iterator p = this->begin();
   21150                 :          22 :        p != this->end();
   21151                 :          11 :        ++p)
   21152                 :             :     {
   21153                 :          11 :       if (*p == NULL)
   21154                 :           0 :         ret->push_back(NULL);
   21155                 :             :       else
   21156                 :          11 :         ret->push_back((*p)->copy());
   21157                 :             :     }
   21158                 :          11 :   return ret;
   21159                 :             : }
   21160                 :             : 
   21161                 :             : // Return whether an expression list has an error expression.
   21162                 :             : 
   21163                 :             : bool
   21164                 :           0 : Expression_list::contains_error() const
   21165                 :             : {
   21166                 :           0 :   for (Expression_list::const_iterator p = this->begin();
   21167                 :           0 :        p != this->end();
   21168                 :           0 :        ++p)
   21169                 :           0 :     if (*p != NULL && (*p)->is_error_expression())
   21170                 :           0 :       return true;
   21171                 :             :   return false;
   21172                 :             : }
   21173                 :             : 
   21174                 :             : // Class Numeric_constant.
   21175                 :             : 
   21176                 :             : // Destructor.
   21177                 :             : 
   21178                 :    41239302 : Numeric_constant::~Numeric_constant()
   21179                 :             : {
   21180                 :    41239302 :   this->clear();
   21181                 :    41239302 : }
   21182                 :             : 
   21183                 :             : // Copy constructor.
   21184                 :             : 
   21185                 :           0 : Numeric_constant::Numeric_constant(const Numeric_constant& a)
   21186                 :           0 :   : classification_(a.classification_), type_(a.type_)
   21187                 :             : {
   21188                 :           0 :   switch (a.classification_)
   21189                 :             :     {
   21190                 :             :     case NC_INVALID:
   21191                 :             :       break;
   21192                 :           0 :     case NC_INT:
   21193                 :           0 :     case NC_RUNE:
   21194                 :           0 :       mpz_init_set(this->u_.int_val, a.u_.int_val);
   21195                 :           0 :       break;
   21196                 :           0 :     case NC_FLOAT:
   21197                 :           0 :       mpfr_init_set(this->u_.float_val, a.u_.float_val, MPFR_RNDN);
   21198                 :           0 :       break;
   21199                 :           0 :     case NC_COMPLEX:
   21200                 :           0 :       mpc_init2(this->u_.complex_val, mpc_precision);
   21201                 :           0 :       mpc_set(this->u_.complex_val, a.u_.complex_val, MPC_RNDNN);
   21202                 :           0 :       break;
   21203                 :           0 :     default:
   21204                 :           0 :       go_unreachable();
   21205                 :             :     }
   21206                 :           0 : }
   21207                 :             : 
   21208                 :             : // Assignment operator.
   21209                 :             : 
   21210                 :             : Numeric_constant&
   21211                 :        1029 : Numeric_constant::operator=(const Numeric_constant& a)
   21212                 :             : {
   21213                 :        1029 :   this->clear();
   21214                 :        1029 :   this->classification_ = a.classification_;
   21215                 :        1029 :   this->type_ = a.type_;
   21216                 :        1029 :   switch (a.classification_)
   21217                 :             :     {
   21218                 :             :     case NC_INVALID:
   21219                 :             :       break;
   21220                 :         868 :     case NC_INT:
   21221                 :         868 :     case NC_RUNE:
   21222                 :         868 :       mpz_init_set(this->u_.int_val, a.u_.int_val);
   21223                 :         868 :       break;
   21224                 :         161 :     case NC_FLOAT:
   21225                 :         161 :       mpfr_init_set(this->u_.float_val, a.u_.float_val, MPFR_RNDN);
   21226                 :         161 :       break;
   21227                 :           0 :     case NC_COMPLEX:
   21228                 :           0 :       mpc_init2(this->u_.complex_val, mpc_precision);
   21229                 :           0 :       mpc_set(this->u_.complex_val, a.u_.complex_val, MPC_RNDNN);
   21230                 :           0 :       break;
   21231                 :           0 :     default:
   21232                 :           0 :       go_unreachable();
   21233                 :             :     }
   21234                 :        1029 :   return *this;
   21235                 :             : }
   21236                 :             : 
   21237                 :             : // Check equality with another numeric constant.
   21238                 :             : 
   21239                 :             : bool
   21240                 :          32 : Numeric_constant::equals(const Numeric_constant& a) const
   21241                 :             : {
   21242                 :          32 :   if (this->classification_ != a.classification_)
   21243                 :             :     return false;
   21244                 :             : 
   21245                 :          32 :   if (this->type_ != NULL && a.type_ != NULL
   21246                 :          64 :       && !Type::are_identical(this->type_, a.type_,
   21247                 :             :                               Type::COMPARE_ALIASES, NULL))
   21248                 :             :     return false;
   21249                 :             : 
   21250                 :          32 :   switch (a.classification_)
   21251                 :             :     {
   21252                 :             :     case NC_INVALID:
   21253                 :             :       break;
   21254                 :          30 :     case NC_INT:
   21255                 :          30 :     case NC_RUNE:
   21256                 :          30 :       return mpz_cmp(this->u_.int_val, a.u_.int_val) == 0;
   21257                 :           0 :     case NC_FLOAT:
   21258                 :           0 :       return mpfr_cmp(this->u_.float_val, a.u_.float_val) == 0;
   21259                 :           2 :     case NC_COMPLEX:
   21260                 :           2 :       return mpc_cmp(this->u_.complex_val, a.u_.complex_val) == 0;
   21261                 :           0 :     default:
   21262                 :           0 :       go_unreachable();
   21263                 :             :     }
   21264                 :             :   return false;
   21265                 :             : }
   21266                 :             : 
   21267                 :             : // Clear the contents.
   21268                 :             : 
   21269                 :             : void
   21270                 :    76773950 : Numeric_constant::clear()
   21271                 :             : {
   21272                 :    76773950 :   switch (this->classification_)
   21273                 :             :     {
   21274                 :             :     case NC_INVALID:
   21275                 :             :       break;
   21276                 :    35411442 :     case NC_INT:
   21277                 :    35411442 :     case NC_RUNE:
   21278                 :    35411442 :       mpz_clear(this->u_.int_val);
   21279                 :    35411442 :       break;
   21280                 :      112146 :     case NC_FLOAT:
   21281                 :      112146 :       mpfr_clear(this->u_.float_val);
   21282                 :      112146 :       break;
   21283                 :       10069 :     case NC_COMPLEX:
   21284                 :       10069 :       mpc_clear(this->u_.complex_val);
   21285                 :       10069 :       break;
   21286                 :           0 :     default:
   21287                 :           0 :       go_unreachable();
   21288                 :             :     }
   21289                 :    76773950 :   this->classification_ = NC_INVALID;
   21290                 :    76773950 : }
   21291                 :             : 
   21292                 :             : // Set to an unsigned long value.
   21293                 :             : 
   21294                 :             : void
   21295                 :       67214 : Numeric_constant::set_unsigned_long(Type* type, unsigned long val)
   21296                 :             : {
   21297                 :       67214 :   this->clear();
   21298                 :       67214 :   this->classification_ = NC_INT;
   21299                 :       67214 :   this->type_ = type;
   21300                 :       67214 :   mpz_init_set_ui(this->u_.int_val, val);
   21301                 :       67214 : }
   21302                 :             : 
   21303                 :             : // Set to an integer value.
   21304                 :             : 
   21305                 :             : void
   21306                 :    35250104 : Numeric_constant::set_int(Type* type, const mpz_t val)
   21307                 :             : {
   21308                 :    35250104 :   this->clear();
   21309                 :    35250104 :   this->classification_ = NC_INT;
   21310                 :    35250104 :   this->type_ = type;
   21311                 :    35250104 :   mpz_init_set(this->u_.int_val, val);
   21312                 :    35250104 : }
   21313                 :             : 
   21314                 :             : // Set to a rune value.
   21315                 :             : 
   21316                 :             : void
   21317                 :       93735 : Numeric_constant::set_rune(Type* type, const mpz_t val)
   21318                 :             : {
   21319                 :       93735 :   this->clear();
   21320                 :       93735 :   this->classification_ = NC_RUNE;
   21321                 :       93735 :   this->type_ = type;
   21322                 :       93735 :   mpz_init_set(this->u_.int_val, val);
   21323                 :       93735 : }
   21324                 :             : 
   21325                 :             : // Set to a floating point value.
   21326                 :             : 
   21327                 :             : void
   21328                 :      112486 : Numeric_constant::set_float(Type* type, const mpfr_t val)
   21329                 :             : {
   21330                 :      112486 :   this->clear();
   21331                 :      112486 :   this->classification_ = NC_FLOAT;
   21332                 :      112486 :   this->type_ = type;
   21333                 :             : 
   21334                 :             :   // Numeric constants do not have negative zero values, so remove
   21335                 :             :   // them here.  They also don't have infinity or NaN values, but we
   21336                 :             :   // should never see them here.
   21337                 :      112486 :   int bits = 0;
   21338                 :      112486 :   if (type != NULL
   21339                 :       79463 :       && type->float_type() != NULL
   21340                 :      191949 :       && !type->float_type()->is_abstract())
   21341                 :      118728 :     bits = type->float_type()->bits();
   21342                 :      112486 :   if (Numeric_constant::is_float_neg_zero(val, bits))
   21343                 :          29 :     mpfr_init_set_ui(this->u_.float_val, 0, MPFR_RNDN);
   21344                 :             :   else
   21345                 :      112457 :     mpfr_init_set(this->u_.float_val, val, MPFR_RNDN);
   21346                 :      112486 : }
   21347                 :             : 
   21348                 :             : // Set to a complex value.
   21349                 :             : 
   21350                 :             : void
   21351                 :       10080 : Numeric_constant::set_complex(Type* type, const mpc_t val)
   21352                 :             : {
   21353                 :       10080 :   this->clear();
   21354                 :       10080 :   this->classification_ = NC_COMPLEX;
   21355                 :       10080 :   this->type_ = type;
   21356                 :             : 
   21357                 :             :   // Avoid negative zero as in set_float.
   21358                 :       10080 :   int bits = 0;
   21359                 :       10080 :   if (type != NULL
   21360                 :        7143 :       && type->complex_type() != NULL
   21361                 :       17223 :       && !type->complex_type()->is_abstract())
   21362                 :       12102 :     bits = type->complex_type()->bits() / 2;
   21363                 :             : 
   21364                 :       10080 :   mpfr_t real;
   21365                 :       10080 :   mpfr_init_set(real, mpc_realref(val), MPFR_RNDN);
   21366                 :       10080 :   if (Numeric_constant::is_float_neg_zero(real, bits))
   21367                 :           3 :     mpfr_set_ui(real, 0, MPFR_RNDN);
   21368                 :             : 
   21369                 :       10080 :   mpfr_t imag;
   21370                 :       10080 :   mpfr_init_set(imag, mpc_imagref(val), MPFR_RNDN);
   21371                 :       10080 :   if (Numeric_constant::is_float_neg_zero(imag, bits))
   21372                 :           0 :     mpfr_set_ui(imag, 0, MPFR_RNDN);
   21373                 :             : 
   21374                 :       10080 :   mpc_init2(this->u_.complex_val, mpc_precision);
   21375                 :       10080 :   mpc_set_fr_fr(this->u_.complex_val, real, imag, MPC_RNDNN);
   21376                 :             : 
   21377                 :       10080 :   mpfr_clear(real);
   21378                 :       10080 :   mpfr_clear(imag);
   21379                 :       10080 : }
   21380                 :             : 
   21381                 :             : // Return whether VAL, at a precision of BITS, is a negative zero.
   21382                 :             : // BITS may be zero in which case it is ignored.
   21383                 :             : 
   21384                 :             : bool
   21385                 :      132646 : Numeric_constant::is_float_neg_zero(const mpfr_t val, int bits)
   21386                 :             : {
   21387                 :      132646 :   if (!mpfr_signbit(val))
   21388                 :             :     return false;
   21389                 :       21171 :   if (mpfr_zero_p(val))
   21390                 :             :     return true;
   21391                 :       21141 :   mpfr_exp_t min_exp;
   21392                 :       21141 :   switch (bits)
   21393                 :             :     {
   21394                 :             :     case 0:
   21395                 :             :       return false;
   21396                 :             :     case 32:
   21397                 :             :       // In a denormalized float32 the exponent is -126, and there are
   21398                 :             :       // 24 bits of which at least the last must be 1, so the smallest
   21399                 :             :       // representable non-zero exponent is -126 - (24 - 1) == -149.
   21400                 :             :       min_exp = -149;
   21401                 :             :       break;
   21402                 :       11794 :     case 64:
   21403                 :             :       // Minimum exponent is -1022, there are 53 bits.
   21404                 :       11794 :       min_exp = -1074;
   21405                 :       11794 :       break;
   21406                 :           0 :     default:
   21407                 :           0 :       go_unreachable();
   21408                 :             :     }
   21409                 :       12000 :   return mpfr_get_exp(val) < min_exp;
   21410                 :             : }
   21411                 :             : 
   21412                 :             : // Get an int value.
   21413                 :             : 
   21414                 :             : void
   21415                 :       76687 : Numeric_constant::get_int(mpz_t* val) const
   21416                 :             : {
   21417                 :       76687 :   go_assert(this->is_int());
   21418                 :       76687 :   mpz_init_set(*val, this->u_.int_val);
   21419                 :       76687 : }
   21420                 :             : 
   21421                 :             : // Get a rune value.
   21422                 :             : 
   21423                 :             : void
   21424                 :           0 : Numeric_constant::get_rune(mpz_t* val) const
   21425                 :             : {
   21426                 :           0 :   go_assert(this->is_rune());
   21427                 :           0 :   mpz_init_set(*val, this->u_.int_val);
   21428                 :           0 : }
   21429                 :             : 
   21430                 :             : // Get a floating point value.
   21431                 :             : 
   21432                 :             : void
   21433                 :        4051 : Numeric_constant::get_float(mpfr_t* val) const
   21434                 :             : {
   21435                 :        4051 :   go_assert(this->is_float());
   21436                 :        4051 :   mpfr_init_set(*val, this->u_.float_val, MPFR_RNDN);
   21437                 :        4051 : }
   21438                 :             : 
   21439                 :             : // Get a complex value.
   21440                 :             : 
   21441                 :             : void
   21442                 :          10 : Numeric_constant::get_complex(mpc_t* val) const
   21443                 :             : {
   21444                 :          10 :   go_assert(this->is_complex());
   21445                 :          10 :   mpc_init2(*val, mpc_precision);
   21446                 :          10 :   mpc_set(*val, this->u_.complex_val, MPC_RNDNN);
   21447                 :          10 : }
   21448                 :             : 
   21449                 :             : // Express value as unsigned long if possible.
   21450                 :             : 
   21451                 :             : Numeric_constant::To_unsigned_long
   21452                 :      771948 : Numeric_constant::to_unsigned_long(unsigned long* val) const
   21453                 :             : {
   21454                 :      771948 :   switch (this->classification_)
   21455                 :             :     {
   21456                 :      771651 :     case NC_INT:
   21457                 :      771651 :     case NC_RUNE:
   21458                 :      771651 :       return this->mpz_to_unsigned_long(this->u_.int_val, val);
   21459                 :         284 :     case NC_FLOAT:
   21460                 :         284 :       return this->mpfr_to_unsigned_long(this->u_.float_val, val);
   21461                 :          13 :     case NC_COMPLEX:
   21462                 :          13 :       if (!mpfr_zero_p(mpc_imagref(this->u_.complex_val)))
   21463                 :             :         return NC_UL_NOTINT;
   21464                 :          12 :       return this->mpfr_to_unsigned_long(mpc_realref(this->u_.complex_val),
   21465                 :          12 :                                          val);
   21466                 :           0 :     default:
   21467                 :           0 :       go_unreachable();
   21468                 :             :     }
   21469                 :             : }
   21470                 :             : 
   21471                 :             : // Express integer value as unsigned long if possible.
   21472                 :             : 
   21473                 :             : Numeric_constant::To_unsigned_long
   21474                 :      771945 : Numeric_constant::mpz_to_unsigned_long(const mpz_t ival,
   21475                 :             :                                        unsigned long *val) const
   21476                 :             : {
   21477                 :      771945 :   if (mpz_sgn(ival) < 0)
   21478                 :             :     return NC_UL_NEGATIVE;
   21479                 :      771518 :   unsigned long ui = mpz_get_ui(ival);
   21480                 :      771518 :   if (mpz_cmp_ui(ival, ui) != 0)
   21481                 :             :     return NC_UL_BIG;
   21482                 :      771507 :   *val = ui;
   21483                 :      771507 :   return NC_UL_VALID;
   21484                 :             : }
   21485                 :             : 
   21486                 :             : // Express floating point value as unsigned long if possible.
   21487                 :             : 
   21488                 :             : Numeric_constant::To_unsigned_long
   21489                 :         296 : Numeric_constant::mpfr_to_unsigned_long(const mpfr_t fval,
   21490                 :             :                                         unsigned long *val) const
   21491                 :             : {
   21492                 :         296 :   if (!mpfr_integer_p(fval))
   21493                 :             :     return NC_UL_NOTINT;
   21494                 :         294 :   mpz_t ival;
   21495                 :         294 :   mpz_init(ival);
   21496                 :         294 :   mpfr_get_z(ival, fval, MPFR_RNDN);
   21497                 :         294 :   To_unsigned_long ret = this->mpz_to_unsigned_long(ival, val);
   21498                 :         294 :   mpz_clear(ival);
   21499                 :         294 :   return ret;
   21500                 :             : }
   21501                 :             : 
   21502                 :             : // Express value as memory size if possible.
   21503                 :             : 
   21504                 :             : bool
   21505                 :      194227 : Numeric_constant::to_memory_size(int64_t* val) const
   21506                 :             : {
   21507                 :      194227 :   switch (this->classification_)
   21508                 :             :     {
   21509                 :      194222 :     case NC_INT:
   21510                 :      194222 :     case NC_RUNE:
   21511                 :      194222 :       return this->mpz_to_memory_size(this->u_.int_val, val);
   21512                 :           5 :     case NC_FLOAT:
   21513                 :           5 :       return this->mpfr_to_memory_size(this->u_.float_val, val);
   21514                 :           0 :     case NC_COMPLEX:
   21515                 :           0 :       if (!mpfr_zero_p(mpc_imagref(this->u_.complex_val)))
   21516                 :             :         return false;
   21517                 :           0 :       return this->mpfr_to_memory_size(mpc_realref(this->u_.complex_val), val);
   21518                 :           0 :     default:
   21519                 :           0 :       go_unreachable();
   21520                 :             :     }
   21521                 :             : }
   21522                 :             : 
   21523                 :             : // Express integer as memory size if possible.
   21524                 :             : 
   21525                 :             : bool
   21526                 :      194227 : Numeric_constant::mpz_to_memory_size(const mpz_t ival, int64_t* val) const
   21527                 :             : {
   21528                 :      194227 :   if (mpz_sgn(ival) < 0)
   21529                 :             :     return false;
   21530                 :      194227 :   if (mpz_fits_slong_p(ival))
   21531                 :             :     {
   21532                 :      194227 :       *val = static_cast<int64_t>(mpz_get_si(ival));
   21533                 :      194227 :       return true;
   21534                 :             :     }
   21535                 :             : 
   21536                 :             :   // Test >= 64, not > 64, because an int64_t can hold 63 bits of a
   21537                 :             :   // positive value.
   21538                 :           0 :   if (mpz_sizeinbase(ival, 2) >= 64)
   21539                 :             :     return false;
   21540                 :             : 
   21541                 :           0 :   mpz_t q, r;
   21542                 :           0 :   mpz_init(q);
   21543                 :           0 :   mpz_init(r);
   21544                 :           0 :   mpz_tdiv_q_2exp(q, ival, 32);
   21545                 :           0 :   mpz_tdiv_r_2exp(r, ival, 32);
   21546                 :           0 :   go_assert(mpz_fits_ulong_p(q) && mpz_fits_ulong_p(r));
   21547                 :           0 :   *val = ((static_cast<int64_t>(mpz_get_ui(q)) << 32)
   21548                 :           0 :           + static_cast<int64_t>(mpz_get_ui(r)));
   21549                 :           0 :   mpz_clear(r);
   21550                 :           0 :   mpz_clear(q);
   21551                 :           0 :   return true;
   21552                 :             : }
   21553                 :             : 
   21554                 :             : // Express floating point value as memory size if possible.
   21555                 :             : 
   21556                 :             : bool
   21557                 :           5 : Numeric_constant::mpfr_to_memory_size(const mpfr_t fval, int64_t* val) const
   21558                 :             : {
   21559                 :           5 :   if (!mpfr_integer_p(fval))
   21560                 :             :     return false;
   21561                 :           5 :   mpz_t ival;
   21562                 :           5 :   mpz_init(ival);
   21563                 :           5 :   mpfr_get_z(ival, fval, MPFR_RNDN);
   21564                 :           5 :   bool ret = this->mpz_to_memory_size(ival, val);
   21565                 :           5 :   mpz_clear(ival);
   21566                 :           5 :   return ret;
   21567                 :             : }
   21568                 :             : 
   21569                 :             : // Convert value to integer if possible.
   21570                 :             : 
   21571                 :             : bool
   21572                 :    31430481 : Numeric_constant::to_int(mpz_t* val) const
   21573                 :             : {
   21574                 :    31430481 :   switch (this->classification_)
   21575                 :             :     {
   21576                 :    31426314 :     case NC_INT:
   21577                 :    31426314 :     case NC_RUNE:
   21578                 :    31426314 :       mpz_init_set(*val, this->u_.int_val);
   21579                 :    31426314 :       return true;
   21580                 :        4128 :     case NC_FLOAT:
   21581                 :        4128 :       if (!mpfr_integer_p(this->u_.float_val))
   21582                 :             :         return false;
   21583                 :        4128 :       mpz_init(*val);
   21584                 :        4128 :       mpfr_get_z(*val, this->u_.float_val, MPFR_RNDN);
   21585                 :        4128 :       return true;
   21586                 :          39 :     case NC_COMPLEX:
   21587                 :          39 :       if (!mpfr_zero_p(mpc_imagref(this->u_.complex_val))
   21588                 :          39 :           || !mpfr_integer_p(mpc_realref(this->u_.complex_val)))
   21589                 :           0 :         return false;
   21590                 :          39 :       mpz_init(*val);
   21591                 :          39 :       mpfr_get_z(*val, mpc_realref(this->u_.complex_val), MPFR_RNDN);
   21592                 :          39 :       return true;
   21593                 :           0 :     default:
   21594                 :           0 :       go_unreachable();
   21595                 :             :     }
   21596                 :             : }
   21597                 :             : 
   21598                 :             : // Convert value to floating point if possible.
   21599                 :             : 
   21600                 :             : bool
   21601                 :       40198 : Numeric_constant::to_float(mpfr_t* val) const
   21602                 :             : {
   21603                 :       40198 :   switch (this->classification_)
   21604                 :             :     {
   21605                 :       19927 :     case NC_INT:
   21606                 :       19927 :     case NC_RUNE:
   21607                 :       19927 :       mpfr_init_set_z(*val, this->u_.int_val, MPFR_RNDN);
   21608                 :       19927 :       return true;
   21609                 :       20267 :     case NC_FLOAT:
   21610                 :       20267 :       mpfr_init_set(*val, this->u_.float_val, MPFR_RNDN);
   21611                 :       20267 :       return true;
   21612                 :           4 :     case NC_COMPLEX:
   21613                 :           4 :       if (!mpfr_zero_p(mpc_imagref(this->u_.complex_val)))
   21614                 :             :         return false;
   21615                 :           4 :       mpfr_init_set(*val, mpc_realref(this->u_.complex_val), MPFR_RNDN);
   21616                 :           4 :       return true;
   21617                 :           0 :     default:
   21618                 :           0 :       go_unreachable();
   21619                 :             :     }
   21620                 :             : }
   21621                 :             : 
   21622                 :             : // Convert value to complex.
   21623                 :             : 
   21624                 :             : bool
   21625                 :        5034 : Numeric_constant::to_complex(mpc_t* val) const
   21626                 :             : {
   21627                 :        5034 :   mpc_init2(*val, mpc_precision);
   21628                 :        5034 :   switch (this->classification_)
   21629                 :             :     {
   21630                 :         801 :     case NC_INT:
   21631                 :         801 :     case NC_RUNE:
   21632                 :         801 :       mpc_set_z(*val, this->u_.int_val, MPC_RNDNN);
   21633                 :         801 :       return true;
   21634                 :         600 :     case NC_FLOAT:
   21635                 :         600 :       mpc_set_fr(*val, this->u_.float_val, MPC_RNDNN);
   21636                 :         600 :       return true;
   21637                 :        3633 :     case NC_COMPLEX:
   21638                 :        3633 :       mpc_set(*val, this->u_.complex_val, MPC_RNDNN);
   21639                 :        3633 :       return true;
   21640                 :           0 :     default:
   21641                 :           0 :       go_unreachable();
   21642                 :             :     }
   21643                 :             : }
   21644                 :             : 
   21645                 :             : // Get the type.
   21646                 :             : 
   21647                 :             : Type*
   21648                 :     6990348 : Numeric_constant::type() const
   21649                 :             : {
   21650                 :     6990348 :   if (this->type_ != NULL)
   21651                 :             :     return this->type_;
   21652                 :       28943 :   switch (this->classification_)
   21653                 :             :     {
   21654                 :       28943 :     case NC_INT:
   21655                 :       28943 :       return Type::make_abstract_integer_type();
   21656                 :           0 :     case NC_RUNE:
   21657                 :           0 :       return Type::make_abstract_character_type();
   21658                 :           0 :     case NC_FLOAT:
   21659                 :           0 :       return Type::make_abstract_float_type();
   21660                 :           0 :     case NC_COMPLEX:
   21661                 :           0 :       return Type::make_abstract_complex_type();
   21662                 :           0 :     default:
   21663                 :           0 :       go_unreachable();
   21664                 :             :     }
   21665                 :             : }
   21666                 :             : 
   21667                 :             : // If the constant can be expressed in TYPE, then set the type of the
   21668                 :             : // constant to TYPE and return true.  Otherwise return false, and, if
   21669                 :             : // ISSUE_ERROR is true, report an appropriate error message.
   21670                 :             : 
   21671                 :             : bool
   21672                 :     4132578 : Numeric_constant::set_type(Type* type, bool issue_error, Location loc)
   21673                 :             : {
   21674                 :     4132578 :   bool ret;
   21675                 :     4132578 :   if (type == NULL || type->is_error())
   21676                 :             :     ret = true;
   21677                 :     4132578 :   else if (type->integer_type() != NULL)
   21678                 :     8130138 :     ret = this->check_int_type(type->integer_type(), issue_error, loc);
   21679                 :       67509 :   else if (type->float_type() != NULL)
   21680                 :      121476 :     ret = this->check_float_type(type->float_type(), issue_error, loc);
   21681                 :        6771 :   else if (type->complex_type() != NULL)
   21682                 :       13542 :     ret = this->check_complex_type(type->complex_type(), issue_error, loc);
   21683                 :             :   else
   21684                 :             :     {
   21685                 :           0 :       ret = false;
   21686                 :           0 :       if (issue_error)
   21687                 :           0 :         go_assert(saw_errors());
   21688                 :             :     }
   21689                 :     4132578 :   if (ret)
   21690                 :     4130716 :     this->type_ = type;
   21691                 :     4132578 :   return ret;
   21692                 :             : }
   21693                 :             : 
   21694                 :             : // Check whether the constant can be expressed in an integer type.
   21695                 :             : 
   21696                 :             : bool
   21697                 :     4065069 : Numeric_constant::check_int_type(Integer_type* type, bool issue_error,
   21698                 :             :                                  Location location)
   21699                 :             : {
   21700                 :     4065069 :   mpz_t val;
   21701                 :     4065069 :   switch (this->classification_)
   21702                 :             :     {
   21703                 :     4059577 :     case NC_INT:
   21704                 :     4059577 :     case NC_RUNE:
   21705                 :     4059577 :       mpz_init_set(val, this->u_.int_val);
   21706                 :     4059577 :       break;
   21707                 :             : 
   21708                 :        5443 :     case NC_FLOAT:
   21709                 :        5443 :       if (!mpfr_integer_p(this->u_.float_val))
   21710                 :             :         {
   21711                 :         924 :           if (issue_error)
   21712                 :             :             {
   21713                 :         489 :               go_error_at(location,
   21714                 :             :                           "floating-point constant truncated to integer");
   21715                 :         489 :               this->set_invalid();
   21716                 :             :             }
   21717                 :         924 :           return false;
   21718                 :             :         }
   21719                 :        4519 :       mpz_init(val);
   21720                 :        4519 :       mpfr_get_z(val, this->u_.float_val, MPFR_RNDN);
   21721                 :        4519 :       break;
   21722                 :             : 
   21723                 :          49 :     case NC_COMPLEX:
   21724                 :          49 :       if (!mpfr_integer_p(mpc_realref(this->u_.complex_val))
   21725                 :          49 :           || !mpfr_zero_p(mpc_imagref(this->u_.complex_val)))
   21726                 :             :         {
   21727                 :          11 :           if (issue_error)
   21728                 :             :             {
   21729                 :           8 :               go_error_at(location, "complex constant truncated to integer");
   21730                 :           8 :               this->set_invalid();
   21731                 :             :             }
   21732                 :          11 :           return false;
   21733                 :             :         }
   21734                 :          38 :       mpz_init(val);
   21735                 :          38 :       mpfr_get_z(val, mpc_realref(this->u_.complex_val), MPFR_RNDN);
   21736                 :          38 :       break;
   21737                 :             : 
   21738                 :           0 :     default:
   21739                 :           0 :       go_unreachable();
   21740                 :             :     }
   21741                 :             : 
   21742                 :     4064134 :   bool ret;
   21743                 :     4064134 :   if (type->is_abstract())
   21744                 :             :     ret = true;
   21745                 :             :   else
   21746                 :             :     {
   21747                 :     2249666 :       int bits = mpz_sizeinbase(val, 2);
   21748                 :     2249666 :       if (type->is_unsigned())
   21749                 :             :         {
   21750                 :             :           // For an unsigned type we can only accept a nonnegative
   21751                 :             :           // number, and we must be able to represents at least BITS.
   21752                 :      966576 :           ret = mpz_sgn(val) >= 0 && bits <= type->bits();
   21753                 :             :         }
   21754                 :             :       else
   21755                 :             :         {
   21756                 :             :           // For a signed type we need an extra bit to indicate the
   21757                 :             :           // sign.  We have to handle the most negative integer
   21758                 :             :           // specially.
   21759                 :     1283090 :           ret = (bits + 1 <= type->bits()
   21760                 :     1283090 :                  || (bits <= type->bits()
   21761                 :        1756 :                      && mpz_sgn(val) < 0
   21762                 :        1752 :                      && (mpz_scan1(val, 0)
   21763                 :        1752 :                          == static_cast<unsigned long>(type->bits() - 1))
   21764                 :        1752 :                      && mpz_scan0(val, type->bits()) == ULONG_MAX));
   21765                 :             :         }
   21766                 :             :     }
   21767                 :             : 
   21768                 :     4064134 :   if (!ret && issue_error)
   21769                 :             :     {
   21770                 :         484 :       go_error_at(location, "integer constant overflow");
   21771                 :         484 :       this->set_invalid();
   21772                 :             :     }
   21773                 :             : 
   21774                 :             :   return ret;
   21775                 :             : }
   21776                 :             : 
   21777                 :             : // Check whether the constant can be expressed in a floating point
   21778                 :             : // type.
   21779                 :             : 
   21780                 :             : bool
   21781                 :       60738 : Numeric_constant::check_float_type(Float_type* type, bool issue_error,
   21782                 :             :                                    Location location)
   21783                 :             : {
   21784                 :       60738 :   mpfr_t val;
   21785                 :       60738 :   switch (this->classification_)
   21786                 :             :     {
   21787                 :       22177 :     case NC_INT:
   21788                 :       22177 :     case NC_RUNE:
   21789                 :       22177 :       mpfr_init_set_z(val, this->u_.int_val, MPFR_RNDN);
   21790                 :       22177 :       break;
   21791                 :             : 
   21792                 :       38554 :     case NC_FLOAT:
   21793                 :       38554 :       mpfr_init_set(val, this->u_.float_val, MPFR_RNDN);
   21794                 :       38554 :       break;
   21795                 :             : 
   21796                 :           7 :     case NC_COMPLEX:
   21797                 :           7 :       if (!mpfr_zero_p(mpc_imagref(this->u_.complex_val)))
   21798                 :             :         {
   21799                 :           2 :           if (issue_error)
   21800                 :             :             {
   21801                 :           2 :               this->set_invalid();
   21802                 :           2 :               go_error_at(location,
   21803                 :             :                           "complex constant truncated to floating-point");
   21804                 :             :             }
   21805                 :           2 :           return false;
   21806                 :             :         }
   21807                 :           5 :       mpfr_init_set(val, mpc_realref(this->u_.complex_val), MPFR_RNDN);
   21808                 :           5 :       break;
   21809                 :             : 
   21810                 :           0 :     default:
   21811                 :           0 :       go_unreachable();
   21812                 :             :     }
   21813                 :             : 
   21814                 :       60736 :   bool ret;
   21815                 :       60736 :   if (type->is_abstract())
   21816                 :             :     ret = true;
   21817                 :       43297 :   else if (mpfr_nan_p(val) || mpfr_inf_p(val) || mpfr_zero_p(val))
   21818                 :             :     {
   21819                 :             :       // A NaN or Infinity always fits in the range of the type.
   21820                 :             :       ret = true;
   21821                 :             :     }
   21822                 :             :   else
   21823                 :             :     {
   21824                 :       39455 :       mpfr_exp_t exp = mpfr_get_exp(val);
   21825                 :       39455 :       mpfr_exp_t max_exp;
   21826                 :       39455 :       switch (type->bits())
   21827                 :             :         {
   21828                 :             :         case 32:
   21829                 :             :           max_exp = 128;
   21830                 :             :           break;
   21831                 :       30431 :         case 64:
   21832                 :       30431 :           max_exp = 1024;
   21833                 :       30431 :           break;
   21834                 :           0 :         default:
   21835                 :           0 :           go_unreachable();
   21836                 :             :         }
   21837                 :             : 
   21838                 :       39455 :       ret = exp <= max_exp;
   21839                 :             : 
   21840                 :       39455 :       if (ret)
   21841                 :             :         {
   21842                 :             :           // Round the constant to the desired type.
   21843                 :       39449 :           mpfr_t t;
   21844                 :       39449 :           mpfr_init(t);
   21845                 :       39449 :           switch (type->bits())
   21846                 :             :             {
   21847                 :        9021 :             case 32:
   21848                 :        9021 :               mpfr_set_prec(t, 24);
   21849                 :        9021 :               break;
   21850                 :       30428 :             case 64:
   21851                 :       30428 :               mpfr_set_prec(t, 53);
   21852                 :       30428 :               break;
   21853                 :           0 :             default:
   21854                 :           0 :               go_unreachable();
   21855                 :             :             }
   21856                 :       39449 :           mpfr_set(t, val, MPFR_RNDN);
   21857                 :       39449 :           mpfr_set(val, t, MPFR_RNDN);
   21858                 :       39449 :           mpfr_clear(t);
   21859                 :             : 
   21860                 :       39449 :           this->set_float(type, val);
   21861                 :             :         }
   21862                 :             :     }
   21863                 :             : 
   21864                 :       60736 :   mpfr_clear(val);
   21865                 :             : 
   21866                 :       60736 :   if (!ret && issue_error)
   21867                 :             :     {
   21868                 :           6 :       go_error_at(location, "floating-point constant overflow");
   21869                 :           6 :       this->set_invalid();
   21870                 :             :     }
   21871                 :             : 
   21872                 :             :   return ret;
   21873                 :             : }
   21874                 :             : 
   21875                 :             : // Check whether the constant can be expressed in a complex type.
   21876                 :             : 
   21877                 :             : bool
   21878                 :        6771 : Numeric_constant::check_complex_type(Complex_type* type, bool issue_error,
   21879                 :             :                                      Location location)
   21880                 :             : {
   21881                 :        6771 :   if (type->is_abstract())
   21882                 :             :     return true;
   21883                 :             : 
   21884                 :        1894 :   mpfr_exp_t max_exp;
   21885                 :        1894 :   switch (type->bits())
   21886                 :             :     {
   21887                 :             :     case 64:
   21888                 :             :       max_exp = 128;
   21889                 :             :       break;
   21890                 :        1542 :     case 128:
   21891                 :        1542 :       max_exp = 1024;
   21892                 :        1542 :       break;
   21893                 :           0 :     default:
   21894                 :           0 :       go_unreachable();
   21895                 :             :     }
   21896                 :             : 
   21897                 :        1894 :   mpc_t val;
   21898                 :        1894 :   mpc_init2(val, mpc_precision);
   21899                 :        1894 :   switch (this->classification_)
   21900                 :             :     {
   21901                 :         539 :     case NC_INT:
   21902                 :         539 :     case NC_RUNE:
   21903                 :         539 :       mpc_set_z(val, this->u_.int_val, MPC_RNDNN);
   21904                 :         539 :       break;
   21905                 :             : 
   21906                 :         100 :     case NC_FLOAT:
   21907                 :         100 :       mpc_set_fr(val, this->u_.float_val, MPC_RNDNN);
   21908                 :         100 :       break;
   21909                 :             : 
   21910                 :        1255 :     case NC_COMPLEX:
   21911                 :        1255 :       mpc_set(val, this->u_.complex_val, MPC_RNDNN);
   21912                 :        1255 :       break;
   21913                 :             : 
   21914                 :           0 :     default:
   21915                 :           0 :       go_unreachable();
   21916                 :             :     }
   21917                 :             : 
   21918                 :        1894 :   bool ret = true;
   21919                 :        1894 :   if (!mpfr_nan_p(mpc_realref(val))
   21920                 :        1894 :       && !mpfr_inf_p(mpc_realref(val))
   21921                 :        1894 :       && !mpfr_zero_p(mpc_realref(val))
   21922                 :        1212 :       && mpfr_get_exp(mpc_realref(val)) > max_exp)
   21923                 :             :     {
   21924                 :           2 :       if (issue_error)
   21925                 :             :         {
   21926                 :           2 :           go_error_at(location, "complex real part overflow");
   21927                 :           2 :           this->set_invalid();
   21928                 :             :         }
   21929                 :             :       ret = false;
   21930                 :             :     }
   21931                 :             : 
   21932                 :        1894 :   if (!mpfr_nan_p(mpc_imagref(val))
   21933                 :        1894 :       && !mpfr_inf_p(mpc_imagref(val))
   21934                 :        1894 :       && !mpfr_zero_p(mpc_imagref(val))
   21935                 :        1032 :       && mpfr_get_exp(mpc_imagref(val)) > max_exp)
   21936                 :             :     {
   21937                 :           0 :       if (issue_error)
   21938                 :             :         {
   21939                 :           0 :           go_error_at(location, "complex imaginary part overflow");
   21940                 :           0 :           this->set_invalid();
   21941                 :             :         }
   21942                 :             :       ret = false;
   21943                 :             :     }
   21944                 :             : 
   21945                 :        1894 :   if (ret)
   21946                 :             :     {
   21947                 :             :       // Round the constant to the desired type.
   21948                 :        1892 :       mpc_t t;
   21949                 :        1892 :       switch (type->bits())
   21950                 :             :         {
   21951                 :         351 :         case 64:
   21952                 :         351 :           mpc_init2(t, 24);
   21953                 :         351 :           break;
   21954                 :        1541 :         case 128:
   21955                 :        1541 :           mpc_init2(t, 53);
   21956                 :        1541 :           break;
   21957                 :           0 :         default:
   21958                 :           0 :           go_unreachable();
   21959                 :             :         }
   21960                 :        1892 :       mpc_set(t, val, MPC_RNDNN);
   21961                 :        1892 :       mpc_set(val, t, MPC_RNDNN);
   21962                 :        1892 :       mpc_clear(t);
   21963                 :             : 
   21964                 :        1892 :       this->set_complex(type, val);
   21965                 :             :     }
   21966                 :             : 
   21967                 :        1894 :   mpc_clear(val);
   21968                 :             : 
   21969                 :        1894 :   return ret;
   21970                 :             : }
   21971                 :             : 
   21972                 :             : // Return an Expression for this value.
   21973                 :             : 
   21974                 :             : Expression*
   21975                 :      235108 : Numeric_constant::expression(Location loc) const
   21976                 :             : {
   21977                 :      235108 :   switch (this->classification_)
   21978                 :             :     {
   21979                 :      218928 :     case NC_INT:
   21980                 :      218928 :       return Expression::make_integer_z(&this->u_.int_val, this->type_, loc);
   21981                 :        4382 :     case NC_RUNE:
   21982                 :        4382 :       return Expression::make_character(&this->u_.int_val, this->type_, loc);
   21983                 :        9205 :     case NC_FLOAT:
   21984                 :        9205 :       return Expression::make_float(&this->u_.float_val, this->type_, loc);
   21985                 :        2593 :     case NC_COMPLEX:
   21986                 :        2593 :       return Expression::make_complex(&this->u_.complex_val, this->type_, loc);
   21987                 :           0 :     case NC_INVALID:
   21988                 :           0 :       go_assert(saw_errors());
   21989                 :           0 :       return Expression::make_error(loc);
   21990                 :           0 :     default:
   21991                 :           0 :       go_unreachable();
   21992                 :             :     }
   21993                 :             : }
   21994                 :             : 
   21995                 :             : // Calculate a hash code with a given seed.
   21996                 :             : 
   21997                 :             : unsigned int
   21998                 :        3663 : Numeric_constant::hash(unsigned int seed) const
   21999                 :             : {
   22000                 :        3663 :   unsigned long val;
   22001                 :        3663 :   const unsigned int PRIME = 97;
   22002                 :        3663 :   long e = 0;
   22003                 :        3663 :   double f = 1.0;
   22004                 :        3663 :   mpfr_t m;
   22005                 :             : 
   22006                 :        3663 :   switch (this->classification_)
   22007                 :             :     {
   22008                 :             :     case NC_INVALID:
   22009                 :             :       return PRIME;
   22010                 :        3653 :     case NC_INT:
   22011                 :        3653 :     case NC_RUNE:
   22012                 :        3653 :       val = mpz_get_ui(this->u_.int_val);
   22013                 :             :       break;
   22014                 :           4 :     case NC_COMPLEX:
   22015                 :           4 :       mpfr_init(m);
   22016                 :           4 :       mpc_abs(m, this->u_.complex_val, MPFR_RNDN);
   22017                 :           4 :       val = mpfr_get_ui(m, MPFR_RNDN);
   22018                 :           4 :       mpfr_clear(m);
   22019                 :           4 :       break;
   22020                 :           6 :     case NC_FLOAT:
   22021                 :           6 :       f = mpfr_get_d_2exp(&e, this->u_.float_val, MPFR_RNDN) * 4294967295.0;
   22022                 :           6 :       val = static_cast<unsigned long>(e + static_cast<long>(f));
   22023                 :           6 :       break;
   22024                 :           0 :     default:
   22025                 :           0 :       go_unreachable();
   22026                 :             :     }
   22027                 :             : 
   22028                 :        3663 :   return (static_cast<unsigned int>(val) + seed) * PRIME;
   22029                 :             : }
        

Generated by: LCOV version 2.1-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.