compiler: traverse expressions when exporting constants

When exporting a constant A that is expressed through a constant
B from another package, it is necessary to traverse an expression
representing the constant A to generate a sequence of type casts
from the constant B. Current implementation doesn't collect types
of constants contained in such expressions. This change fetches
these types.

Fixes golang/go#51291

Change-Id: I0f0922a4fa3dd82c18d8cfce7eb83ed98a642505
Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/405976
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Than McIntosh <thanm@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
diff --git a/go/export.cc b/go/export.cc
index 70d3f70..a30b11a 100644
--- a/go/export.cc
+++ b/go/export.cc
@@ -124,6 +124,11 @@
   void
   prepare_types(const std::vector<Named_object*>& sorted_exports);
 
+  // Third entry point (called after the method above), to find
+  // all types in expressions referenced by exports.
+  void
+  prepare_expressions(const std::vector<Named_object*>& sorted_exports);
+
  protected:
   // Override of parent class method.
   int
@@ -281,6 +286,28 @@
   return TRAVERSE_CONTINUE;
 }
 
+// Collect up the set of types mentioned in expressions of things we're exporting,
+// and collect all the packages encountered during type traversal, to make sure
+// we can declare things referered to indirectly (for example, in the body of an
+// exported inline function from another package).
+
+void
+Collect_export_references::prepare_expressions(const std::vector<Named_object*>& sorted_exports)
+{
+  for (std::vector<Named_object*>::const_iterator p = sorted_exports.begin();
+       p != sorted_exports.end();
+       ++p)
+    {
+      Named_object* no = *p;
+      if (no->classification() == Named_object::NAMED_OBJECT_CONST)
+        {
+          Expression* e = no->const_value()->expr();
+          if (e != NULL)
+            Expression::traverse(&e, this);
+        }
+    }
+}
+
 // Collect up the set of types mentioned in things we're exporting, and collect
 // all the packages encountered during type traversal, to make sure we can
 // declare things referered to indirectly (for example, in the body of an
@@ -891,6 +918,7 @@
   // Collect up the set of types mentioned in things we're exporting,
   // and any packages that may be referred to indirectly.
   collect.prepare_types(sorted_exports);
+  collect.prepare_expressions(sorted_exports);
 
   // Assign indexes to all exported types and types referenced by
   // things we're exporting.  Return value is index of first non-exported