OSDN Git Service

Fix typo in error message.
[pf3gnuchains/gcc-fork.git] / gcc / go / gofrontend / parse.cc
index b2a1715..b91bfbd 100644 (file)
@@ -35,7 +35,7 @@ Parse::Enclosing_var_comparison::operator()(const Enclosing_var& v1,
   // If we get here it means that a single nested function refers to
   // two different variables defined in enclosing functions, and both
   // variables have the same name.  I think this is impossible.
-  gcc_unreachable();
+  go_unreachable();
 }
 
 // Class Parse.
@@ -335,10 +335,17 @@ Parse::type_name(bool issue_error)
   bool ok = true;
   if (named_object == NULL)
     {
-      if (package != NULL)
-       ok = false;
-      else
+      if (package == NULL)
        named_object = this->gogo_->add_unknown_name(name, location);
+      else
+       {
+         const std::string& packname(package->package_value()->name());
+         error_at(location, "reference to undefined identifier %<%s.%s%>",
+                  Gogo::message_name(packname).c_str(),
+                  Gogo::message_name(name).c_str());
+         issue_error = false;
+         ok = false;
+       }
     }
   else if (named_object->is_type())
     {
@@ -362,7 +369,7 @@ Parse::type_name(bool issue_error)
   else if (named_object->is_unknown() || named_object->is_type_declaration())
     return Type::make_forward_declaration(named_object);
   else
-    gcc_unreachable();
+    go_unreachable();
 }
 
 // ArrayType = "[" [ ArrayLength ] "]" ElementType .
@@ -677,6 +684,32 @@ Parse::channel_type()
   return Type::make_channel_type(send, receive, element_type);
 }
 
+// Give an error for a duplicate parameter or receiver name.
+
+void
+Parse::check_signature_names(const Typed_identifier_list* params,
+                            Parse::Names* names)
+{
+  for (Typed_identifier_list::const_iterator p = params->begin();
+       p != params->end();
+       ++p)
+    {
+      if (p->name().empty() || Gogo::is_sink_name(p->name()))
+       continue;
+      std::pair<std::string, const Typed_identifier*> val =
+       std::make_pair(p->name(), &*p);
+      std::pair<Parse::Names::iterator, bool> ins = names->insert(val);
+      if (!ins.second)
+       {
+         error_at(p->location(), "redefinition of %qs",
+                  Gogo::message_name(p->name()).c_str());
+         inform(ins.first->second->location(),
+                "previous definition of %qs was here",
+                Gogo::message_name(p->name()).c_str());
+       }
+    }
+}
+
 // Signature      = Parameters [ Result ] .
 
 // RECEIVER is the receiver if there is one, or NULL.  LOCATION is the
@@ -691,18 +724,24 @@ Parse::signature(Typed_identifier* receiver, source_location location)
   Typed_identifier_list* params;
   bool params_ok = this->parameters(&params, &is_varargs);
 
-  Typed_identifier_list* result = NULL;
+  Typed_identifier_list* results = NULL;
   if (this->peek_token()->is_op(OPERATOR_LPAREN)
       || this->type_may_start_here())
     {
-      if (!this->result(&result))
+      if (!this->result(&results))
        return NULL;
     }
 
   if (!params_ok)
     return NULL;
 
-  Function_type* ret = Type::make_function_type(receiver, params, result,
+  Parse::Names names;
+  if (params != NULL)
+    this->check_signature_names(params, &names);
+  if (results != NULL)
+    this->check_signature_names(results, &names);
+
+  Function_type* ret = Type::make_function_type(receiver, params, results,
                                                location);
   if (is_varargs)
     ret->set_is_varargs();
@@ -1839,7 +1878,7 @@ Parse::init_var(const Typed_identifier& tid, Type* type, Expression* init,
       if (!type_from_init && init != NULL)
        {
          if (!this->gogo_->in_global_scope())
-           this->gogo_->add_statement(Statement::make_statement(init));
+           this->gogo_->add_statement(Statement::make_statement(init, true));
          else
            return this->create_dummy_global(type, init, location);
        }
@@ -2323,10 +2362,10 @@ Parse::operand(bool may_be_sink)
          case Named_object::NAMED_OBJECT_UNKNOWN:
            return Expression::make_unknown_reference(named_object, location);
          default:
-           gcc_unreachable();
+           go_unreachable();
          }
       }
-      gcc_unreachable();
+      go_unreachable();
 
     case Token::TOKEN_STRING:
       ret = Expression::make_string(token->string_value(), token->location());
@@ -2445,7 +2484,9 @@ Parse::enclosing_var_reference(Named_object* in_function, Named_object* var,
 // LiteralValue  = "{" [ ElementList [ "," ] ] "}" .
 // ElementList   = Element { "," Element } .
 // Element       = [ Key ":" ] Value .
-// Key           = Expression .
+// Key           = FieldName | ElementIndex .
+// FieldName     = identifier .
+// ElementIndex  = Expression .
 // Value         = Expression | LiteralValue .
 
 // We have already seen the type if there is one, and we are now
@@ -2478,7 +2519,33 @@ Parse::composite_lit(Type* type, int depth, source_location location)
 
       const Token* token = this->peek_token();
 
-      if (!token->is_op(OPERATOR_LCURLY))
+      if (token->is_identifier())
+       {
+         std::string identifier = token->identifier();
+         bool is_exported = token->is_identifier_exported();
+         source_location location = token->location();
+
+         if (this->advance_token()->is_op(OPERATOR_COLON))
+           {
+             // This may be a field name.  We don't know for sure--it
+             // could also be an expression for an array index.  We
+             // don't want to parse it as an expression because may
+             // trigger various errors, e.g., if this identifier
+             // happens to be the name of a package.
+             Gogo* gogo = this->gogo_;
+             val = this->id_to_expression(gogo->pack_hidden_name(identifier,
+                                                                 is_exported),
+                                          location);
+           }
+         else
+           {
+             this->unget_token(Token::make_identifier_token(identifier,
+                                                            is_exported,
+                                                            location));
+             val = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
+           }
+       }
+      else if (!token->is_op(OPERATOR_LCURLY))
        val = this->expression(PRECEDENCE_NORMAL, false, true, NULL);
       else
        {
@@ -2733,8 +2800,21 @@ Parse::primary_expr(bool may_be_sink, bool may_be_composite_lit,
          else
            this->advance_token();
          if (expr->is_error_expression())
-           return expr;
-         ret = Expression::make_cast(ret->type(), expr, loc);
+           ret = expr;
+         else
+           {
+             Type* t = ret->type();
+             if (t->classification() == Type::TYPE_ARRAY
+                 && t->array_type()->length() != NULL
+                 && t->array_type()->length()->is_nil_expression())
+               {
+                 error_at(ret->location(),
+                          "invalid use of %<...%> in type conversion");
+                 ret = Expression::make_error(loc);
+               }
+             else
+               ret = Expression::make_cast(t, expr, loc);
+           }
        }
     }
 
@@ -2922,6 +3002,12 @@ Parse::id_to_expression(const std::string& name, source_location location)
       return Expression::make_func_reference(named_object, NULL, location);
     case Named_object::NAMED_OBJECT_UNKNOWN:
       return Expression::make_unknown_reference(named_object, location);
+    case Named_object::NAMED_OBJECT_PACKAGE:
+    case Named_object::NAMED_OBJECT_TYPE:
+    case Named_object::NAMED_OBJECT_TYPE_DECLARATION:
+      // These cases can arise for a field name in a composite
+      // literal.
+      return Expression::make_unknown_reference(named_object, location);
     default:
       error_at(this->location(), "unexpected type of identifier");
       return Expression::make_error(location);
@@ -3068,7 +3154,7 @@ Parse::expression_may_start_here()
     case Token::TOKEN_IMAGINARY:
       return true;
     default:
-      gcc_unreachable();
+      go_unreachable();
     }
 }
 
@@ -3459,8 +3545,7 @@ Parse::statement_list_may_start_here()
 void
 Parse::expression_stat(Expression* exp)
 {
-  exp->discarding_value();
-  this->gogo_->add_statement(Statement::make_statement(exp));
+  this->gogo_->add_statement(Statement::make_statement(exp, false));
 }
 
 // SendStmt = Channel "&lt;-" Expression .
@@ -3493,7 +3578,7 @@ Parse::inc_dec_stat(Expression* exp)
   else if (token->is_op(OPERATOR_MINUSMINUS))
     this->gogo_->add_statement(Statement::make_dec_statement(exp));
   else
-    gcc_unreachable();
+    go_unreachable();
   this->advance_token();
 }
 
@@ -3735,7 +3820,8 @@ Parse::return_stat()
   this->gogo_->add_statement(Statement::make_return_statement(vals, location));
 }
 
-// IfStmt    = "if" [ SimpleStmt ";" ] Expression Block [ "else" Statement ] .
+// IfStmt = "if" [ SimpleStmt ";" ] Expression Block
+//          [ "else" ( IfStmt | Block ) ] .
 
 void
 Parse::if_stat()
@@ -3805,10 +3891,17 @@ Parse::if_stat()
   Block* else_block = NULL;
   if (this->peek_token()->is_keyword(KEYWORD_ELSE))
     {
-      this->advance_token();
-      // We create a block to gather the statement.
       this->gogo_->start_block(this->location());
-      this->statement(NULL);
+      const Token* token = this->advance_token();
+      if (token->is_keyword(KEYWORD_IF))
+       this->if_stat();
+      else if (token->is_op(OPERATOR_LCURLY))
+       this->block();
+      else
+       {
+         error_at(this->location(), "expected %<if%> or %<{%>");
+         this->statement(NULL);
+       }
       else_block = this->gogo_->finish_block(this->location());
     }
 
@@ -4836,7 +4929,7 @@ Parse::break_stat()
        {
          // If there is a label with this name, mark it as used to
          // avoid a useless error about an unused label.
-         this->gogo_->add_label_reference(token->identifier());
+         this->gogo_->add_label_reference(token->identifier(), 0, false);
 
          error_at(token->location(), "invalid break label %qs",
                   Gogo::message_name(token->identifier()).c_str());
@@ -4858,7 +4951,7 @@ Parse::break_stat()
   else if (enclosing->classification() == Statement::STATEMENT_SELECT)
     label = enclosing->select_statement()->break_label();
   else
-    gcc_unreachable();
+    go_unreachable();
 
   this->gogo_->add_statement(Statement::make_break_statement(label,
                                                             location));
@@ -4891,7 +4984,7 @@ Parse::continue_stat()
        {
          // If there is a label with this name, mark it as used to
          // avoid a useless error about an unused label.
-         this->gogo_->add_label_reference(token->identifier());
+         this->gogo_->add_label_reference(token->identifier(), 0, false);
 
          error_at(token->location(), "invalid continue label %qs",
                   Gogo::message_name(token->identifier()).c_str());
@@ -4907,7 +5000,7 @@ Parse::continue_stat()
   else if (enclosing->classification() == Statement::STATEMENT_FOR_RANGE)
     label = enclosing->for_range_statement()->continue_label();
   else
-    gcc_unreachable();
+    go_unreachable();
 
   this->gogo_->add_statement(Statement::make_continue_statement(label,
                                                                location));
@@ -4925,7 +5018,8 @@ Parse::goto_stat()
     error_at(this->location(), "expected label for goto");
   else
     {
-      Label* label = this->gogo_->add_label_reference(token->identifier());
+      Label* label = this->gogo_->add_label_reference(token->identifier(),
+                                                     location, true);
       Statement* s = Statement::make_goto_statement(label, location);
       this->gogo_->add_statement(s);
       this->advance_token();