From: Ian Lance Taylor Date: Mon, 7 Mar 2011 21:22:35 +0000 (+0000) Subject: Handle predeclared names used as fields in struct composite literals. X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=6481a43b012d6ab105b8fbc13a4673041ee20778;p=gcc.git Handle predeclared names used as fields in struct composite literals. From-SVN: r170754 --- diff --git a/gcc/go/gofrontend/expressions.cc b/gcc/go/gofrontend/expressions.cc index 075ba6468fe..8660c755abb 100644 --- a/gcc/go/gofrontend/expressions.cc +++ b/gcc/go/gofrontend/expressions.cc @@ -936,14 +936,6 @@ Var_expression::do_lower(Gogo* gogo, Named_object* function, int) return this; } -// Return the name of the variable. - -const std::string& -Var_expression::name() const -{ - return this->variable_->name(); -} - // Return the type of a reference to a variable. Type* @@ -1125,14 +1117,6 @@ Expression::make_sink(source_location location) // a function seems like it could work, though there might be little // point to it. -// Return the name of the function. - -const std::string& -Func_expression::name() const -{ - return this->function_->name(); -} - // Traversal. int @@ -2359,10 +2343,6 @@ class Const_expression : public Expression named_object() { return this->constant_; } - const std::string& - name() const - { return this->constant_->name(); } - // Check that the initializer does not refer to the constant itself. void check_for_init_loop(); @@ -11645,7 +11625,7 @@ class Composite_literal_expression : public Parser_expression private: Expression* - lower_struct(Type*); + lower_struct(Gogo*, Type*); Expression* lower_array(Type*); @@ -11706,7 +11686,7 @@ Composite_literal_expression::do_lower(Gogo* gogo, Named_object* function, int) if (type->is_error_type()) return Expression::make_error(this->location()); else if (type->struct_type() != NULL) - return this->lower_struct(type); + return this->lower_struct(gogo, type); else if (type->array_type() != NULL) return this->lower_array(type); else if (type->map_type() != NULL) @@ -11723,7 +11703,7 @@ Composite_literal_expression::do_lower(Gogo* gogo, Named_object* function, int) // Lower a struct composite literal. Expression* -Composite_literal_expression::lower_struct(Type* type) +Composite_literal_expression::lower_struct(Gogo* gogo, Type* type) { source_location location = this->location(); Struct_type* st = type->struct_type(); @@ -11751,6 +11731,7 @@ Composite_literal_expression::lower_struct(Type* type) bool bad_key = false; std::string name; + const Named_object* no = NULL; switch (name_expr->classification()) { case EXPRESSION_UNKNOWN_REFERENCE: @@ -11758,7 +11739,7 @@ Composite_literal_expression::lower_struct(Type* type) break; case EXPRESSION_CONST_REFERENCE: - name = static_cast(name_expr)->name(); + no = static_cast(name_expr)->named_object(); break; case EXPRESSION_TYPE: @@ -11768,16 +11749,16 @@ Composite_literal_expression::lower_struct(Type* type) if (nt == NULL) bad_key = true; else - name = nt->name(); + no = nt->named_object(); } break; case EXPRESSION_VAR_REFERENCE: - name = name_expr->var_expression()->name(); + no = name_expr->var_expression()->named_object(); break; case EXPRESSION_FUNC_REFERENCE: - name = name_expr->func_expression()->name(); + no = name_expr->func_expression()->named_object(); break; case EXPRESSION_UNARY: @@ -11825,6 +11806,23 @@ Composite_literal_expression::lower_struct(Type* type) return Expression::make_error(location); } + if (no != NULL) + { + name = no->name(); + + // A predefined name won't be packed. If it starts with a + // lower case letter we need to check for that case, because + // the field name will be packed. + if (!Gogo::is_hidden_name(name) + && name[0] >= 'a' + && name[0] <= 'z') + { + Named_object* gno = gogo->lookup_global(name.c_str()); + if (gno == no) + name = gogo->pack_hidden_name(name, false); + } + } + unsigned int index; const Struct_field* sf = st->find_local_field(name, &index); if (sf == NULL) diff --git a/gcc/go/gofrontend/expressions.h b/gcc/go/gofrontend/expressions.h index 20386469f73..1dc408d557f 100644 --- a/gcc/go/gofrontend/expressions.h +++ b/gcc/go/gofrontend/expressions.h @@ -903,10 +903,6 @@ class Var_expression : public Expression named_object() const { return this->variable_; } - // Return the name of the variable. - const std::string& - name() const; - protected: Expression* do_lower(Gogo*, Named_object*, int); @@ -1314,10 +1310,6 @@ class Func_expression : public Expression named_object() const { return this->function_; } - // Return the name of the function. - const std::string& - name() const; - // Return the closure for this function. This will return NULL if // the function has no closure, which is the normal case. Expression*