compiler: include global variable preinit blocks in ast dumps

Dump out the blocks corresponding to variable pre-inits when
-fgo-dump-ast is in effect. Each preinit block is prefixed with a
comment indicating the variable it is initializing.

Change-Id: Id507920c566fab28ee07bc55b89d5c4cb788441d
Reviewed-on: https://go-review.googlesource.com/118636
Reviewed-by: Ian Lance Taylor <iant@golang.org>
diff --git a/go/ast-dump.cc b/go/ast-dump.cc
index 94bf5ef..1fbc890 100644
--- a/go/ast-dump.cc
+++ b/go/ast-dump.cc
@@ -29,7 +29,7 @@
 {
  public:
   Ast_dump_traverse_blocks_and_functions(Ast_dump_context* ast_dump_context)
-      : Traverse(traverse_blocks | traverse_functions),
+      : Traverse(traverse_blocks | traverse_functions | traverse_variables),
       ast_dump_context_(ast_dump_context)
   { }
 
@@ -40,6 +40,9 @@
   int
   function(Named_object*);
 
+  int
+  variable(Named_object*);
+
  private:
   Ast_dump_context* ast_dump_context_;
 };
@@ -150,6 +153,27 @@
   return TRAVERSE_CONTINUE;
 }
 
+// Dump variable preinits
+
+int
+Ast_dump_traverse_blocks_and_functions::variable(Named_object* no)
+{
+  if (!no->is_variable())
+    return TRAVERSE_CONTINUE;
+
+  Variable* var = no->var_value();
+  if (var->has_pre_init())
+    {
+      this->ast_dump_context_->ostream() << "// preinit block for var "
+                                         << no->message_name() << "\n";
+      var->preinit()->traverse(this);
+    }
+
+  return TRAVERSE_CONTINUE;
+}
+
+
+
 // Class Ast_dump_context.
 
 Ast_dump_context::Ast_dump_context(std::ostream* out /* = NULL */,