OSDN Git Service

compiler: Permit omitting &T in composite literal.
[pf3gnuchains/gcc-fork.git] / gcc / go / gofrontend / expressions.cc
index a80c823..3c75f8a 100644 (file)
@@ -3322,7 +3322,7 @@ Type_conversion_expression::do_lower(Gogo*, Named_object*,
       mpfr_clear(imag);
     }
 
-  if (type->is_slice_type() && type->named_type() == NULL)
+  if (type->is_slice_type())
     {
       Type* element_type = type->array_type()->element_type()->forwarded();
       bool is_byte = element_type == Type::lookup_integer_type("uint8");
@@ -3621,20 +3621,11 @@ Type_conversion_expression::do_get_tree(Translate_context* context)
                               integer_type_node,
                               fold_convert(integer_type_node, expr_tree));
     }
-  else if (type->is_string_type()
-          && (expr_type->array_type() != NULL
-              || (expr_type->points_to() != NULL
-                  && expr_type->points_to()->array_type() != NULL)))
+  else if (type->is_string_type() && expr_type->is_slice_type())
     {
-      Type* t = expr_type;
-      if (t->points_to() != NULL)
-       {
-         t = t->points_to();
-         expr_tree = build_fold_indirect_ref(expr_tree);
-       }
       if (!DECL_P(expr_tree))
        expr_tree = save_expr(expr_tree);
-      Array_type* a = t->array_type();
+      Array_type* a = expr_type->array_type();
       Type* e = a->element_type()->forwarded();
       go_assert(e->integer_type() != NULL);
       tree valptr = fold_convert(const_ptr_type_node,
@@ -3678,7 +3669,7 @@ Type_conversion_expression::do_get_tree(Translate_context* context)
       if (e->integer_type()->is_unsigned()
          && e->integer_type()->bits() == 8)
        {
-         static tree string_to_byte_array_fndecl;
+         tree string_to_byte_array_fndecl = NULL_TREE;
          ret = Gogo::call_builtin(&string_to_byte_array_fndecl,
                                   this->location(),
                                   "__go_string_to_byte_array",
@@ -3690,7 +3681,7 @@ Type_conversion_expression::do_get_tree(Translate_context* context)
       else
        {
          go_assert(e == Type::lookup_integer_type("int"));
-         static tree string_to_int_array_fndecl;
+         tree string_to_int_array_fndecl = NULL_TREE;
          ret = Gogo::call_builtin(&string_to_int_array_fndecl,
                                   this->location(),
                                   "__go_string_to_int_array",
@@ -12792,14 +12783,23 @@ Composite_literal_expression::do_lower(Gogo* gogo, Named_object* function,
        }
     }
 
+  Type *pt = type->points_to();
+  bool is_pointer = false;
+  if (pt != NULL)
+    {
+      is_pointer = true;
+      type = pt;
+    }
+
+  Expression* ret;
   if (type->is_error())
     return Expression::make_error(this->location());
   else if (type->struct_type() != NULL)
-    return this->lower_struct(gogo, type);
+    ret = this->lower_struct(gogo, type);
   else if (type->array_type() != NULL)
-    return this->lower_array(type);
+    ret = this->lower_array(type);
   else if (type->map_type() != NULL)
-    return this->lower_map(gogo, function, inserter, type);
+    ret = this->lower_map(gogo, function, inserter, type);
   else
     {
       error_at(this->location(),
@@ -12807,6 +12807,11 @@ Composite_literal_expression::do_lower(Gogo* gogo, Named_object* function,
                "for composite literal"));
       return Expression::make_error(this->location());
     }
+
+  if (is_pointer)
+    ret = Expression::make_heap_composite(ret, this->location());
+
+  return ret;
 }
 
 // Lower a struct composite literal.
@@ -12817,7 +12822,26 @@ Composite_literal_expression::lower_struct(Gogo* gogo, Type* type)
   Location location = this->location();
   Struct_type* st = type->struct_type();
   if (this->vals_ == NULL || !this->has_keys_)
-    return new Struct_construction_expression(type, this->vals_, location);
+    {
+      if (this->vals_ != NULL
+         && !this->vals_->empty()
+         && type->named_type() != NULL
+         && type->named_type()->named_object()->package() != NULL)
+       {
+         for (Struct_field_list::const_iterator pf = st->fields()->begin();
+              pf != st->fields()->end();
+              ++pf)
+           {
+             if (Gogo::is_hidden_name(pf->field_name()))
+               error_at(this->location(),
+                        "assignment of unexported field %qs in %qs literal",
+                        Gogo::message_name(pf->field_name()).c_str(),
+                        type->named_type()->message_name().c_str());
+           }
+       }
+
+      return new Struct_construction_expression(type, this->vals_, location);
+    }
 
   size_t field_count = st->field_count();
   std::vector<Expression*> vals(field_count);
@@ -12964,6 +12988,14 @@ Composite_literal_expression::lower_struct(Gogo* gogo, Type* type)
          return Expression::make_error(location);
        }
 
+      if (type->named_type() != NULL
+         && type->named_type()->named_object()->package() != NULL
+         && Gogo::is_hidden_name(sf->field_name()))
+       error_at(name_expr->location(),
+                "assignment of unexported field %qs in %qs literal",
+                Gogo::message_name(sf->field_name()).c_str(),
+                type->named_type()->message_name().c_str());
+
       vals[index] = val;
     }