]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
compiler: Don't let dot-import names match names from previous files.
authorIan Lance Taylor <ian@gcc.gnu.org>
Sun, 20 Jul 2014 15:13:50 +0000 (15:13 +0000)
committerIan Lance Taylor <ian@gcc.gnu.org>
Sun, 20 Jul 2014 15:13:50 +0000 (15:13 +0000)
The test case for this will be bug488.go in the main
repository: https://codereview.appspot.com/118000043 .

From-SVN: r212871

gcc/go/gofrontend/gogo.cc
gcc/go/gofrontend/gogo.h
gcc/go/gofrontend/import.cc
gcc/go/gofrontend/unsafe.cc

index b9c8fa95e02c3bc7a88dfda155c90ae7919aa026..623befd1027960c52031dd67ed4e9b1fbfa90663 100644 (file)
@@ -473,7 +473,7 @@ Gogo::import_package(const std::string& filename,
                 bindings->begin_declarations();
               p != bindings->end_declarations();
               ++p)
-           this->add_named_object(p->second);
+           this->add_dot_import_object(p->second);
        }
       else if (ln == "_")
        package->set_uses_sink_alias();
@@ -1968,11 +1968,32 @@ Gogo::add_sink()
   return Named_object::make_sink();
 }
 
-// Add a named object.
+// Add a named object for a dot import.
 
 void
-Gogo::add_named_object(Named_object* no)
-{
+Gogo::add_dot_import_object(Named_object* no)
+{
+  // If the name already exists, then it was defined in some file seen
+  // earlier.  If the earlier name is just a declaration, don't add
+  // this name, because that will cause the previous declaration to
+  // merge to this imported name, which should not happen.  Just add
+  // this name to the list of file block names to get appropriate
+  // errors if we see a later definition.
+  Named_object* e = this->package_->bindings()->lookup(no->name());
+  if (e != NULL && e->package() == NULL)
+    {
+      if (e->is_unknown())
+       e = e->resolve();
+      if (e->package() == NULL
+         && (e->is_type_declaration()
+             || e->is_function_declaration()
+             || e->is_unknown()))
+       {
+         this->add_file_block_name(no->name(), no->location());
+         return;
+       }
+    }
+
   this->current_bindings()->add_named_object(no);
 }
 
index 700b09207a17a9e21e487ef01574160a535f312c..01390ac28d20beef2c8136feb5ecc59e314b9897 100644 (file)
@@ -397,7 +397,7 @@ class Gogo
   // Add a named object to the current namespace.  This is used for
   // import . "package".
   void
-  add_named_object(Named_object*);
+  add_dot_import_object(Named_object*);
 
   // Add an identifier to the list of names seen in the file block.
   void
index 4913100b5fddd17ba0db2a83bfe2cb1c2f52b0ac..c83ebe29853e7663163d41f7b4602514768e0ea1 100644 (file)
@@ -431,7 +431,7 @@ Import::import_const()
   Typed_identifier tid(name, type, this->location_);
   Named_object* no = this->package_->add_constant(tid, expr);
   if (this->add_to_globals_)
-    this->gogo_->add_named_object(no);
+    this->gogo_->add_dot_import_object(no);
 }
 
 // Import a type.
@@ -464,7 +464,7 @@ Import::import_var()
   Named_object* no;
   no = this->package_->add_variable(name, var);
   if (this->add_to_globals_)
-    this->gogo_->add_named_object(no);
+    this->gogo_->add_dot_import_object(no);
 }
 
 // Import a function into PACKAGE.  PACKAGE is normally
@@ -518,7 +518,7 @@ Import::import_func(Package* package)
     {
       no = package->add_function_declaration(name, fntype, loc);
       if (this->add_to_globals_)
-       this->gogo_->add_named_object(no);
+       this->gogo_->add_dot_import_object(no);
     }
   return no;
 }
index e7c61f02303569418819b4ce1743359b2aab3273..9b5ec44ad09f510c74322522d200f46a61bd031f 100644 (file)
@@ -66,7 +66,7 @@ Gogo::import_unsafe(const std::string& local_name, bool is_local_name_exported,
   fntype->set_is_builtin();
   no = bindings->add_function_declaration("Sizeof", package, fntype, bloc);
   if (add_to_globals)
-    this->add_named_object(no);
+    this->add_dot_import_object(no);
 
   // Offsetof.
   results = new Typed_identifier_list;
@@ -76,7 +76,7 @@ Gogo::import_unsafe(const std::string& local_name, bool is_local_name_exported,
   fntype->set_is_builtin();
   no = bindings->add_function_declaration("Offsetof", package, fntype, bloc);
   if (add_to_globals)
-    this->add_named_object(no);
+    this->add_dot_import_object(no);
 
   // Alignof.
   results = new Typed_identifier_list;
@@ -86,7 +86,7 @@ Gogo::import_unsafe(const std::string& local_name, bool is_local_name_exported,
   fntype->set_is_builtin();
   no = bindings->add_function_declaration("Alignof", package, fntype, bloc);
   if (add_to_globals)
-    this->add_named_object(no);
+    this->add_dot_import_object(no);
 
   if (!this->imported_unsafe_)
     {