OSDN Git Service

compiler: Fix order of initialization bug with global var a, b = f().
[pf3gnuchains/gcc-fork.git] / gcc / go / gofrontend / gogo.h
1 // gogo.h -- Go frontend parsed representation.     -*- C++ -*-
2
3 // Copyright 2009 The Go Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
6
7 #ifndef GO_GOGO_H
8 #define GO_GOGO_H
9
10 #include "go-linemap.h"
11
12 class Traverse;
13 class Statement_inserter;
14 class Type;
15 class Type_hash_identical;
16 class Type_equal;
17 class Type_identical;
18 class Typed_identifier;
19 class Typed_identifier_list;
20 class Function_type;
21 class Expression;
22 class Statement;
23 class Temporary_statement;
24 class Block;
25 class Function;
26 class Bindings;
27 class Bindings_snapshot;
28 class Package;
29 class Variable;
30 class Pointer_type;
31 class Struct_type;
32 class Struct_field;
33 class Struct_field_list;
34 class Array_type;
35 class Map_type;
36 class Channel_type;
37 class Interface_type;
38 class Named_type;
39 class Forward_declaration_type;
40 class Method;
41 class Methods;
42 class Named_object;
43 class Label;
44 class Translate_context;
45 class Backend;
46 class Export;
47 class Import;
48 class Bexpression;
49 class Bstatement;
50 class Bblock;
51 class Bvariable;
52 class Blabel;
53
54 // This file declares the basic classes used to hold the internal
55 // representation of Go which is built by the parser.
56
57 // An initialization function for an imported package.  This is a
58 // magic function which initializes variables and runs the "init"
59 // function.
60
61 class Import_init
62 {
63  public:
64   Import_init(const std::string& package_name, const std::string& init_name,
65               int priority)
66     : package_name_(package_name), init_name_(init_name), priority_(priority)
67   { }
68
69   // The name of the package being imported.
70   const std::string&
71   package_name() const
72   { return this->package_name_; }
73
74   // The name of the package's init function.
75   const std::string&
76   init_name() const
77   { return this->init_name_; }
78
79   // The priority of the initialization function.  Functions with a
80   // lower priority number must be run first.
81   int
82   priority() const
83   { return this->priority_; }
84
85  private:
86   // The name of the package being imported.
87   std::string package_name_;
88   // The name of the package's init function.
89   std::string init_name_;
90   // The priority.
91   int priority_;
92 };
93
94 // For sorting purposes.
95
96 inline bool
97 operator<(const Import_init& i1, const Import_init& i2)
98 {
99   if (i1.priority() < i2.priority())
100     return true;
101   if (i1.priority() > i2.priority())
102     return false;
103   if (i1.package_name() != i2.package_name())
104     return i1.package_name() < i2.package_name();
105   return i1.init_name() < i2.init_name();
106 }
107
108 // The holder for the internal representation of the entire
109 // compilation unit.
110
111 class Gogo
112 {
113  public:
114   // Create the IR, passing in the sizes of the types "int" and
115   // "uintptr" in bits.
116   Gogo(Backend* backend, Linemap *linemap, int int_type_size, int pointer_size);
117
118   // Get the backend generator.
119   Backend*
120   backend()
121   { return this->backend_; }
122
123   // Get the Location generator.
124   Linemap*
125   linemap()
126   { return this->linemap_; }
127
128   // Get the package name.
129   const std::string&
130   package_name() const;
131
132   // Set the package name.
133   void
134   set_package_name(const std::string&, Location);
135
136   // Return whether this is the "main" package.
137   bool
138   is_main_package() const;
139
140   // If necessary, adjust the name to use for a hidden symbol.  We add
141   // a prefix of the package name, so that hidden symbols in different
142   // packages do not collide.
143   std::string
144   pack_hidden_name(const std::string& name, bool is_exported) const
145   {
146     return (is_exported
147             ? name
148             : ('.' + this->unique_prefix()
149                + '.' + this->package_name()
150                + '.' + name));
151   }
152
153   // Unpack a name which may have been hidden.  Returns the
154   // user-visible name of the object.
155   static std::string
156   unpack_hidden_name(const std::string& name)
157   { return name[0] != '.' ? name : name.substr(name.rfind('.') + 1); }
158
159   // Return whether a possibly packed name is hidden.
160   static bool
161   is_hidden_name(const std::string& name)
162   { return name[0] == '.'; }
163
164   // Return the package prefix of a hidden name.
165   static std::string
166   hidden_name_prefix(const std::string& name)
167   {
168     go_assert(Gogo::is_hidden_name(name));
169     return name.substr(1, name.rfind('.') - 1);
170   }
171
172   // Given a name which may or may not have been hidden, return the
173   // name to use in an error message.
174   static std::string
175   message_name(const std::string& name);
176
177   // Return whether a name is the blank identifier _.
178   static bool
179   is_sink_name(const std::string& name)
180   {
181     return (name[0] == '.'
182             && name[name.length() - 1] == '_'
183             && name[name.length() - 2] == '.');
184   }
185
186   // Return the unique prefix to use for all exported symbols.
187   const std::string&
188   unique_prefix() const;
189
190   // Set the unique prefix.
191   void
192   set_unique_prefix(const std::string&);
193
194   // Return the priority to use for the package we are compiling.
195   // This is two more than the largest priority of any package we
196   // import.
197   int
198   package_priority() const;
199
200   // Import a package.  FILENAME is the file name argument, LOCAL_NAME
201   // is the local name to give to the package.  If LOCAL_NAME is empty
202   // the declarations are added to the global scope.
203   void
204   import_package(const std::string& filename, const std::string& local_name,
205                  bool is_local_name_exported, Location);
206
207   // Whether we are the global binding level.
208   bool
209   in_global_scope() const;
210
211   // Look up a name in the current binding contours.
212   Named_object*
213   lookup(const std::string&, Named_object** pfunction) const;
214
215   // Look up a name in the current block.
216   Named_object*
217   lookup_in_block(const std::string&) const;
218
219   // Look up a name in the global namespace--the universal scope.
220   Named_object*
221   lookup_global(const char*) const;
222
223   // Add a new imported package.  REAL_NAME is the real name of the
224   // package.  ALIAS is the alias of the package; this may be the same
225   // as REAL_NAME.  This sets *PADD_TO_GLOBALS if symbols added to
226   // this package should be added to the global namespace; this is
227   // true if the alias is ".".  LOCATION is the location of the import
228   // statement.  This returns the new package, or NULL on error.
229   Package*
230   add_imported_package(const std::string& real_name, const std::string& alias,
231                        bool is_alias_exported,
232                        const std::string& unique_prefix,
233                        Location location,
234                        bool* padd_to_globals);
235
236   // Register a package.  This package may or may not be imported.
237   // This returns the Package structure for the package, creating if
238   // it necessary.
239   Package*
240   register_package(const std::string& name, const std::string& unique_prefix,
241                    Location);
242
243   // Start compiling a function.  ADD_METHOD_TO_TYPE is true if a
244   // method function should be added to the type of its receiver.
245   Named_object*
246   start_function(const std::string& name, Function_type* type,
247                  bool add_method_to_type, Location);
248
249   // Finish compiling a function.
250   void
251   finish_function(Location);
252
253   // Return the current function.
254   Named_object*
255   current_function() const;
256
257   // Return the current block.
258   Block*
259   current_block();
260
261   // Start a new block.  This is not initially associated with a
262   // function.
263   void
264   start_block(Location);
265
266   // Finish the current block and return it.
267   Block*
268   finish_block(Location);
269
270   // Declare an erroneous name.  This is used to avoid knock-on errors
271   // after a parsing error.
272   Named_object*
273   add_erroneous_name(const std::string& name);
274
275   // Declare an unknown name.  This is used while parsing.  The name
276   // must be resolved by the end of the parse.  Unknown names are
277   // always added at the package level.
278   Named_object*
279   add_unknown_name(const std::string& name, Location);
280
281   // Declare a function.
282   Named_object*
283   declare_function(const std::string&, Function_type*, Location);
284
285   // Declare a function at the package level.  This is used for
286   // functions generated for a type.
287   Named_object*
288   declare_package_function(const std::string&, Function_type*, Location);
289
290   // Add a label.
291   Label*
292   add_label_definition(const std::string&, Location);
293
294   // Add a label reference.  ISSUE_GOTO_ERRORS is true if we should
295   // report errors for a goto from the current location to the label
296   // location.
297   Label*
298   add_label_reference(const std::string&, Location,
299                       bool issue_goto_errors);
300
301   // Return a snapshot of the current binding state.
302   Bindings_snapshot*
303   bindings_snapshot(Location);
304
305   // Add a statement to the current block.
306   void
307   add_statement(Statement*);
308
309   // Add a block to the current block.
310   void
311   add_block(Block*, Location);
312
313   // Add a constant.
314   Named_object*
315   add_constant(const Typed_identifier&, Expression*, int iota_value);
316
317   // Add a type.
318   void
319   add_type(const std::string&, Type*, Location);
320
321   // Add a named type.  This is used for builtin types, and to add an
322   // imported type to the global scope.
323   void
324   add_named_type(Named_type*);
325
326   // Declare a type.
327   Named_object*
328   declare_type(const std::string&, Location);
329
330   // Declare a type at the package level.  This is used when the
331   // parser sees an unknown name where a type name is required.
332   Named_object*
333   declare_package_type(const std::string&, Location);
334
335   // Define a type which was already declared.
336   void
337   define_type(Named_object*, Named_type*);
338
339   // Add a variable.
340   Named_object*
341   add_variable(const std::string&, Variable*);
342
343   // Add a sink--a reference to the blank identifier _.
344   Named_object*
345   add_sink();
346
347   // Add a type which needs to be verified.  This is used for sink
348   // types, just to give appropriate error messages.
349   void
350   add_type_to_verify(Type* type);
351
352   // Add a named object to the current namespace.  This is used for
353   // import . "package".
354   void
355   add_named_object(Named_object*);
356
357   // Mark all local variables in current bindings as used.  This is
358   // used when there is a parse error to avoid useless errors.
359   void
360   mark_locals_used();
361
362   // Return a name to use for a thunk function.  A thunk function is
363   // one we create during the compilation, for a go statement or a
364   // defer statement or a method expression.
365   static std::string
366   thunk_name();
367
368   // Return whether an object is a thunk.
369   static bool
370   is_thunk(const Named_object*);
371
372   // Note that we've seen an interface type.  This is used to build
373   // all required interface method tables.
374   void
375   record_interface_type(Interface_type*);
376
377   // Note that we need an initialization function.
378   void
379   set_need_init_fn()
380   { this->need_init_fn_ = true; }
381
382   // Clear out all names in file scope.  This is called when we start
383   // parsing a new file.
384   void
385   clear_file_scope();
386
387   // Record that VAR1 must be initialized after VAR2.  This is used
388   // when VAR2 does not appear in VAR1's INIT or PREINIT.
389   void
390   record_var_depends_on(Variable* var1, Named_object* var2)
391   {
392     go_assert(this->var_deps_.find(var1) == this->var_deps_.end());
393     this->var_deps_[var1] = var2;
394   }
395
396   // Return the variable that VAR depends on, or NULL if none.
397   Named_object*
398   var_depends_on(Variable* var) const
399   {
400     Var_deps::const_iterator p = this->var_deps_.find(var);
401     return p != this->var_deps_.end() ? p->second : NULL;
402   }
403
404   // Queue up a type-specific function to be written out.  This is
405   // used when a type-specific function is needed when not at the top
406   // level.
407   void
408   queue_specific_type_function(Type* type, Named_type* name,
409                                const std::string& hash_name,
410                                Function_type* hash_fntype,
411                                const std::string& equal_name,
412                                Function_type* equal_fntype);
413
414   // Write out queued specific type functions.
415   void
416   write_specific_type_functions();
417
418   // Whether we are done writing out specific type functions.
419   bool
420   specific_type_functions_are_written() const
421   { return this->specific_type_functions_are_written_; }
422
423   // Traverse the tree.  See the Traverse class.
424   void
425   traverse(Traverse*);
426
427   // Define the predeclared global names.
428   void
429   define_global_names();
430
431   // Verify and complete all types.
432   void
433   verify_types();
434
435   // Lower the parse tree.
436   void
437   lower_parse_tree();
438
439   // Lower all the statements in a block.
440   void
441   lower_block(Named_object* function, Block*);
442
443   // Lower an expression.
444   void
445   lower_expression(Named_object* function, Statement_inserter*, Expression**);
446
447   // Lower a constant.
448   void
449   lower_constant(Named_object*);
450
451   // Finalize the method lists and build stub methods for named types.
452   void
453   finalize_methods();
454
455   // Work out the types to use for unspecified variables and
456   // constants.
457   void
458   determine_types();
459
460   // Type check the program.
461   void
462   check_types();
463
464   // Check the types in a single block.  This is used for complicated
465   // go statements.
466   void
467   check_types_in_block(Block*);
468
469   // Check for return statements.
470   void
471   check_return_statements();
472
473   // Do all exports.
474   void
475   do_exports();
476
477   // Add an import control function for an imported package to the
478   // list.
479   void
480   add_import_init_fn(const std::string& package_name,
481                      const std::string& init_name, int prio);
482
483   // Turn short-cut operators (&&, ||) into explicit if statements.
484   void
485   remove_shortcuts();
486
487   // Use temporary variables to force order of evaluation.
488   void
489   order_evaluations();
490
491   // Build thunks for functions which call recover.
492   void
493   build_recover_thunks();
494
495   // Simplify statements which might use thunks: go and defer
496   // statements.
497   void
498   simplify_thunk_statements();
499
500   // Dump AST if -fgo-dump-ast is set 
501   void
502   dump_ast(const char* basename);
503
504   // Convert named types to the backend representation.
505   void
506   convert_named_types();
507
508   // Convert named types in a list of bindings.
509   void
510   convert_named_types_in_bindings(Bindings*);
511
512   // True if named types have been converted to the backend
513   // representation.
514   bool
515   named_types_are_converted() const
516   { return this->named_types_are_converted_; }
517
518   // Write out the global values.
519   void
520   write_globals();
521
522   // Create trees for implicit builtin functions.
523   void
524   define_builtin_function_trees();
525
526   // Build a call to a builtin function.  PDECL should point to a NULL
527   // initialized static pointer which will hold the fndecl.  NAME is
528   // the name of the function.  NARGS is the number of arguments.
529   // RETTYPE is the return type.  It is followed by NARGS pairs of
530   // type and argument (both trees).
531   static tree
532   call_builtin(tree* pdecl, Location, const char* name, int nargs,
533                tree rettype, ...);
534
535   // Build a call to the runtime error function.
536   static tree
537   runtime_error(int code, Location);
538
539   // Build a builtin struct with a list of fields.
540   static tree
541   builtin_struct(tree* ptype, const char* struct_name, tree struct_type,
542                  int nfields, ...);
543
544   // Mark a function declaration as a builtin library function.
545   static void
546   mark_fndecl_as_builtin_library(tree fndecl);
547
548   // Build a constructor for a slice.  SLICE_TYPE_TREE is the type of
549   // the slice.  VALUES points to the values.  COUNT is the size,
550   // CAPACITY is the capacity.  If CAPACITY is NULL, it is set to
551   // COUNT.
552   static tree
553   slice_constructor(tree slice_type_tree, tree values, tree count,
554                     tree capacity);
555
556   // Build required interface method tables.
557   void
558   build_interface_method_tables();
559
560   // Build an interface method table for a type: a list of function
561   // pointers, one for each interface method.  This returns a decl.
562   tree
563   interface_method_table_for_type(const Interface_type*, Named_type*,
564                                   bool is_pointer);
565
566   // Return a tree which allocate SIZE bytes to hold values of type
567   // TYPE.
568   tree
569   allocate_memory(Type *type, tree size, Location);
570
571   // Return a type to use for pointer to const char.
572   static tree
573   const_char_pointer_type_tree();
574
575   // Build a string constant with the right type.
576   static tree
577   string_constant_tree(const std::string&);
578
579   // Build a Go string constant.  This returns a pointer to the
580   // constant.
581   tree
582   go_string_constant_tree(const std::string&);
583
584   // Receive a value from a channel.
585   static tree
586   receive_from_channel(tree type_tree, tree type_descriptor_tree, tree channel,
587                        Location);
588
589   // Make a trampoline which calls FNADDR passing CLOSURE.
590   tree
591   make_trampoline(tree fnaddr, tree closure, Location);
592
593  private:
594   // During parsing, we keep a stack of functions.  Each function on
595   // the stack is one that we are currently parsing.  For each
596   // function, we keep track of the current stack of blocks.
597   struct Open_function
598   {
599     // The function.
600     Named_object* function;
601     // The stack of active blocks in the function.
602     std::vector<Block*> blocks;
603   };
604
605   // The stack of functions.
606   typedef std::vector<Open_function> Open_functions;
607
608   // Set up the built-in unsafe package.
609   void
610   import_unsafe(const std::string&, bool is_exported, Location);
611
612   // Add a new imported package.
613   Named_object*
614   add_package(const std::string& real_name, const std::string& alias,
615               const std::string& unique_prefix, Location location);
616
617   // Return the current binding contour.
618   Bindings*
619   current_bindings();
620
621   const Bindings*
622   current_bindings() const;
623
624   // Get the name of the magic initialization function.
625   const std::string&
626   get_init_fn_name();
627
628   // Get the decl for the magic initialization function.
629   tree
630   initialization_function_decl();
631
632   // Write the magic initialization function.
633   void
634   write_initialization_function(tree fndecl, tree init_stmt_list);
635
636   // Initialize imported packages.
637   void
638   init_imports(tree*);
639
640   // Register variables with the garbage collector.
641   void
642   register_gc_vars(const std::vector<Named_object*>&, tree*);
643
644   // Build a pointer to a Go string constant.  This returns a pointer
645   // to the pointer.
646   tree
647   ptr_go_string_constant_tree(const std::string&);
648
649   // Return the type of a trampoline.
650   static tree
651   trampoline_type_tree();
652
653   // Type used to map import names to packages.
654   typedef std::map<std::string, Package*> Imports;
655
656   // Type used to map package names to packages.
657   typedef std::map<std::string, Package*> Packages;
658
659   // Type used to map variables to the function calls that set them.
660   // This is used for initialization dependency analysis.
661   typedef std::map<Variable*, Named_object*> Var_deps;
662
663   // Type used to queue writing a type specific function.
664   struct Specific_type_function
665   {
666     Type* type;
667     Named_type* name;
668     std::string hash_name;
669     Function_type* hash_fntype;
670     std::string equal_name;
671     Function_type* equal_fntype;
672
673     Specific_type_function(Type* atype, Named_type* aname,
674                            const std::string& ahash_name,
675                            Function_type* ahash_fntype,
676                            const std::string& aequal_name,
677                            Function_type* aequal_fntype)
678       : type(atype), name(aname), hash_name(ahash_name),
679         hash_fntype(ahash_fntype), equal_name(aequal_name),
680         equal_fntype(aequal_fntype)
681     { }
682   };
683
684   // The backend generator.
685   Backend* backend_;
686   // The object used to keep track of file names and line numbers.
687   Linemap* linemap_;
688   // The package we are compiling.
689   Package* package_;
690   // The list of currently open functions during parsing.
691   Open_functions functions_;
692   // The global binding contour.  This includes the builtin functions
693   // and the package we are compiling.
694   Bindings* globals_;
695   // Mapping from import file names to packages.
696   Imports imports_;
697   // Whether the magic unsafe package was imported.
698   bool imported_unsafe_;
699   // Mapping from package names we have seen to packages.  This does
700   // not include the package we are compiling.
701   Packages packages_;
702   // The functions named "init", if there are any.
703   std::vector<Named_object*> init_functions_;
704   // A mapping from variables to the function calls that initialize
705   // them, if it is not stored in the variable's init or preinit.
706   // This is used for dependency analysis.
707   Var_deps var_deps_;
708   // Whether we need a magic initialization function.
709   bool need_init_fn_;
710   // The name of the magic initialization function.
711   std::string init_fn_name_;
712   // A list of import control variables for packages that we import.
713   std::set<Import_init> imported_init_fns_;
714   // The unique prefix used for all global symbols.
715   std::string unique_prefix_;
716   // Whether an explicit unique prefix was set by -fgo-prefix.
717   bool unique_prefix_specified_;
718   // A list of types to verify.
719   std::vector<Type*> verify_types_;
720   // A list of interface types defined while parsing.
721   std::vector<Interface_type*> interface_types_;
722   // Type specific functions to write out.
723   std::vector<Specific_type_function*> specific_type_functions_;
724   // Whether we are done writing out specific type functions.
725   bool specific_type_functions_are_written_;
726   // Whether named types have been converted.
727   bool named_types_are_converted_;
728 };
729
730 // A block of statements.
731
732 class Block
733 {
734  public:
735   Block(Block* enclosing, Location);
736
737   // Return the enclosing block.
738   const Block*
739   enclosing() const
740   { return this->enclosing_; }
741
742   // Return the bindings of the block.
743   Bindings*
744   bindings()
745   { return this->bindings_; }
746
747   const Bindings*
748   bindings() const
749   { return this->bindings_; }
750
751   // Look at the block's statements.
752   const std::vector<Statement*>*
753   statements() const
754   { return &this->statements_; }
755
756   // Return the start location.  This is normally the location of the
757   // left curly brace which starts the block.
758   Location
759   start_location() const
760   { return this->start_location_; }
761
762   // Return the end location.  This is normally the location of the
763   // right curly brace which ends the block.
764   Location
765   end_location() const
766   { return this->end_location_; }
767
768   // Add a statement to the block.
769   void
770   add_statement(Statement*);
771
772   // Add a statement to the front of the block.
773   void
774   add_statement_at_front(Statement*);
775
776   // Replace a statement in a block.
777   void
778   replace_statement(size_t index, Statement*);
779
780   // Add a Statement before statement number INDEX.
781   void
782   insert_statement_before(size_t index, Statement*);
783
784   // Add a Statement after statement number INDEX.
785   void
786   insert_statement_after(size_t index, Statement*);
787
788   // Set the end location of the block.
789   void
790   set_end_location(Location location)
791   { this->end_location_ = location; }
792
793   // Traverse the tree.
794   int
795   traverse(Traverse*);
796
797   // Set final types for unspecified variables and constants.
798   void
799   determine_types();
800
801   // Return true if execution of this block may fall through to the
802   // next block.
803   bool
804   may_fall_through() const;
805
806   // Convert the block to the backend representation.
807   Bblock*
808   get_backend(Translate_context*);
809
810   // Iterate over statements.
811
812   typedef std::vector<Statement*>::iterator iterator;
813
814   iterator
815   begin()
816   { return this->statements_.begin(); }
817
818   iterator
819   end()
820   { return this->statements_.end(); }
821
822  private:
823   // Enclosing block.
824   Block* enclosing_;
825   // Statements in the block.
826   std::vector<Statement*> statements_;
827   // Binding contour.
828   Bindings* bindings_;
829   // Location of start of block.
830   Location start_location_;
831   // Location of end of block.
832   Location end_location_;
833 };
834
835 // A function.
836
837 class Function
838 {
839  public:
840   Function(Function_type* type, Function*, Block*, Location);
841
842   // Return the function's type.
843   Function_type*
844   type() const
845   { return this->type_; }
846
847   // Return the enclosing function if there is one.
848   Function*
849   enclosing()
850   { return this->enclosing_; }
851
852   // Set the enclosing function.  This is used when building thunks
853   // for functions which call recover.
854   void
855   set_enclosing(Function* enclosing)
856   {
857     go_assert(this->enclosing_ == NULL);
858     this->enclosing_ = enclosing;
859   }
860
861   // The result variables.
862   typedef std::vector<Named_object*> Results;
863
864   // Create the result variables in the outer block.
865   void
866   create_result_variables(Gogo*);
867
868   // Update the named result variables when cloning a function which
869   // calls recover.
870   void
871   update_result_variables();
872
873   // Return the result variables.
874   Results*
875   result_variables()
876   { return this->results_; }
877
878   // Whether the result variables have names.
879   bool
880   results_are_named() const
881   { return this->results_are_named_; }
882
883   // Add a new field to the closure variable.
884   void
885   add_closure_field(Named_object* var, Location loc)
886   { this->closure_fields_.push_back(std::make_pair(var, loc)); }
887
888   // Whether this function needs a closure.
889   bool
890   needs_closure() const
891   { return !this->closure_fields_.empty(); }
892
893   // Return the closure variable, creating it if necessary.  This is
894   // passed to the function as a static chain parameter.
895   Named_object*
896   closure_var();
897
898   // Set the closure variable.  This is used when building thunks for
899   // functions which call recover.
900   void
901   set_closure_var(Named_object* v)
902   {
903     go_assert(this->closure_var_ == NULL);
904     this->closure_var_ = v;
905   }
906
907   // Return the variable for a reference to field INDEX in the closure
908   // variable.
909   Named_object*
910   enclosing_var(unsigned int index)
911   {
912     go_assert(index < this->closure_fields_.size());
913     return closure_fields_[index].first;
914   }
915
916   // Set the type of the closure variable if there is one.
917   void
918   set_closure_type();
919
920   // Get the block of statements associated with the function.
921   Block*
922   block() const
923   { return this->block_; }
924
925   // Get the location of the start of the function.
926   Location
927   location() const
928   { return this->location_; }
929
930   // Return whether this function is actually a method.
931   bool
932   is_method() const;
933
934   // Add a label definition to the function.
935   Label*
936   add_label_definition(Gogo*, const std::string& label_name, Location);
937
938   // Add a label reference to a function.  ISSUE_GOTO_ERRORS is true
939   // if we should report errors for a goto from the current location
940   // to the label location.
941   Label*
942   add_label_reference(Gogo*, const std::string& label_name,
943                       Location, bool issue_goto_errors);
944
945   // Warn about labels that are defined but not used.
946   void
947   check_labels() const;
948
949   // Whether this function calls the predeclared recover function.
950   bool
951   calls_recover() const
952   { return this->calls_recover_; }
953
954   // Record that this function calls the predeclared recover function.
955   // This is set during the lowering pass.
956   void
957   set_calls_recover()
958   { this->calls_recover_ = true; }
959
960   // Whether this is a recover thunk function.
961   bool
962   is_recover_thunk() const
963   { return this->is_recover_thunk_; }
964
965   // Record that this is a thunk built for a function which calls
966   // recover.
967   void
968   set_is_recover_thunk()
969   { this->is_recover_thunk_ = true; }
970
971   // Whether this function already has a recover thunk.
972   bool
973   has_recover_thunk() const
974   { return this->has_recover_thunk_; }
975
976   // Record that this function already has a recover thunk.
977   void
978   set_has_recover_thunk()
979   { this->has_recover_thunk_ = true; }
980
981   // Swap with another function.  Used only for the thunk which calls
982   // recover.
983   void
984   swap_for_recover(Function *);
985
986   // Traverse the tree.
987   int
988   traverse(Traverse*);
989
990   // Determine types in the function.
991   void
992   determine_types();
993
994   // Return the function's decl given an identifier.
995   tree
996   get_or_make_decl(Gogo*, Named_object*, tree id);
997
998   // Return the function's decl after it has been built.
999   tree
1000   get_decl() const
1001   {
1002     go_assert(this->fndecl_ != NULL);
1003     return this->fndecl_;
1004   }
1005
1006   // Set the function decl to hold a tree of the function code.
1007   void
1008   build_tree(Gogo*, Named_object*);
1009
1010   // Get the value to return when not explicitly specified.  May also
1011   // add statements to execute first to STMT_LIST.
1012   tree
1013   return_value(Gogo*, Named_object*, Location, tree* stmt_list) const;
1014
1015   // Get a tree for the variable holding the defer stack.
1016   Expression*
1017   defer_stack(Location);
1018
1019   // Export the function.
1020   void
1021   export_func(Export*, const std::string& name) const;
1022
1023   // Export a function with a type.
1024   static void
1025   export_func_with_type(Export*, const std::string& name,
1026                         const Function_type*);
1027
1028   // Import a function.
1029   static void
1030   import_func(Import*, std::string* pname, Typed_identifier** receiver,
1031               Typed_identifier_list** pparameters,
1032               Typed_identifier_list** presults, bool* is_varargs);
1033
1034  private:
1035   // Type for mapping from label names to Label objects.
1036   typedef Unordered_map(std::string, Label*) Labels;
1037
1038   tree
1039   make_receiver_parm_decl(Gogo*, Named_object*, tree);
1040
1041   tree
1042   copy_parm_to_heap(Gogo*, Named_object*, tree);
1043
1044   void
1045   build_defer_wrapper(Gogo*, Named_object*, tree*, tree*);
1046
1047   typedef std::vector<std::pair<Named_object*,
1048                                 Location> > Closure_fields;
1049
1050   // The function's type.
1051   Function_type* type_;
1052   // The enclosing function.  This is NULL when there isn't one, which
1053   // is the normal case.
1054   Function* enclosing_;
1055   // The result variables, if any.
1056   Results* results_;
1057   // If there is a closure, this is the list of variables which appear
1058   // in the closure.  This is created by the parser, and then resolved
1059   // to a real type when we lower parse trees.
1060   Closure_fields closure_fields_;
1061   // The closure variable, passed as a parameter using the static
1062   // chain parameter.  Normally NULL.
1063   Named_object* closure_var_;
1064   // The outer block of statements in the function.
1065   Block* block_;
1066   // The source location of the start of the function.
1067   Location location_;
1068   // Labels defined or referenced in the function.
1069   Labels labels_;
1070   // The function decl.
1071   tree fndecl_;
1072   // The defer stack variable.  A pointer to this variable is used to
1073   // distinguish the defer stack for one function from another.  This
1074   // is NULL unless we actually need a defer stack.
1075   Temporary_statement* defer_stack_;
1076   // True if the result variables are named.
1077   bool results_are_named_;
1078   // True if this function calls the predeclared recover function.
1079   bool calls_recover_;
1080   // True if this a thunk built for a function which calls recover.
1081   bool is_recover_thunk_;
1082   // True if this function already has a recover thunk.
1083   bool has_recover_thunk_;
1084 };
1085
1086 // A snapshot of the current binding state.
1087
1088 class Bindings_snapshot
1089 {
1090  public:
1091   Bindings_snapshot(const Block*, Location);
1092
1093   // Report any errors appropriate for a goto from the current binding
1094   // state of B to this one.
1095   void
1096   check_goto_from(const Block* b, Location);
1097
1098   // Report any errors appropriate for a goto from this binding state
1099   // to the current state of B.
1100   void
1101   check_goto_to(const Block* b);
1102
1103  private:
1104   bool
1105   check_goto_block(Location, const Block*, const Block*, size_t*);
1106
1107   void
1108   check_goto_defs(Location, const Block*, size_t, size_t);
1109
1110   // The current block.
1111   const Block* block_;
1112   // The number of names currently defined in each open block.
1113   // Element 0 is this->block_, element 1 is
1114   // this->block_->enclosing(), etc.
1115   std::vector<size_t> counts_;
1116   // The location where this snapshot was taken.
1117   Location location_;
1118 };
1119
1120 // A function declaration.
1121
1122 class Function_declaration
1123 {
1124  public:
1125   Function_declaration(Function_type* fntype, Location location)
1126     : fntype_(fntype), location_(location), asm_name_(), fndecl_(NULL)
1127   { }
1128
1129   Function_type*
1130   type() const
1131   { return this->fntype_; }
1132
1133   Location
1134   location() const
1135   { return this->location_; }
1136
1137   const std::string&
1138   asm_name() const
1139   { return this->asm_name_; }
1140
1141   // Set the assembler name.
1142   void
1143   set_asm_name(const std::string& asm_name)
1144   { this->asm_name_ = asm_name; }
1145
1146   // Return a decl for the function given an identifier.
1147   tree
1148   get_or_make_decl(Gogo*, Named_object*, tree id);
1149
1150   // Export a function declaration.
1151   void
1152   export_func(Export* exp, const std::string& name) const
1153   { Function::export_func_with_type(exp, name, this->fntype_); }
1154
1155  private:
1156   // The type of the function.
1157   Function_type* fntype_;
1158   // The location of the declaration.
1159   Location location_;
1160   // The assembler name: this is the name to use in references to the
1161   // function.  This is normally empty.
1162   std::string asm_name_;
1163   // The function decl if needed.
1164   tree fndecl_;
1165 };
1166
1167 // A variable.
1168
1169 class Variable
1170 {
1171  public:
1172   Variable(Type*, Expression*, bool is_global, bool is_parameter,
1173            bool is_receiver, Location);
1174
1175   // Get the type of the variable.
1176   Type*
1177   type();
1178
1179   Type*
1180   type() const;
1181
1182   // Return whether the type is defined yet.
1183   bool
1184   has_type() const;
1185
1186   // Get the initial value.
1187   Expression*
1188   init() const
1189   { return this->init_; }
1190
1191   // Return whether there are any preinit statements.
1192   bool
1193   has_pre_init() const
1194   { return this->preinit_ != NULL; }
1195
1196   // Return the preinit statements if any.
1197   Block*
1198   preinit() const
1199   { return this->preinit_; }
1200
1201   // Return whether this is a global variable.
1202   bool
1203   is_global() const
1204   { return this->is_global_; }
1205
1206   // Return whether this is a function parameter.
1207   bool
1208   is_parameter() const
1209   { return this->is_parameter_; }
1210
1211   // Return whether this is the receiver parameter of a method.
1212   bool
1213   is_receiver() const
1214   { return this->is_receiver_; }
1215
1216   // Change this parameter to be a receiver.  This is used when
1217   // creating the thunks created for functions which call recover.
1218   void
1219   set_is_receiver()
1220   {
1221     go_assert(this->is_parameter_);
1222     this->is_receiver_ = true;
1223   }
1224
1225   // Change this parameter to not be a receiver.  This is used when
1226   // creating the thunks created for functions which call recover.
1227   void
1228   set_is_not_receiver()
1229   {
1230     go_assert(this->is_parameter_);
1231     this->is_receiver_ = false;
1232   }
1233
1234   // Return whether this is the varargs parameter of a function.
1235   bool
1236   is_varargs_parameter() const
1237   { return this->is_varargs_parameter_; }
1238
1239   // Whether this variable's address is taken.
1240   bool
1241   is_address_taken() const
1242   { return this->is_address_taken_; }
1243
1244   // Whether this variable should live in the heap.
1245   bool
1246   is_in_heap() const
1247   { return this->is_address_taken_ && !this->is_global_; }
1248
1249   // Note that something takes the address of this variable.
1250   void
1251   set_address_taken()
1252   { this->is_address_taken_ = true; }
1253
1254   // Return whether the address is taken but does not escape.
1255   bool
1256   is_non_escaping_address_taken() const
1257   { return this->is_non_escaping_address_taken_; }
1258
1259   // Note that something takes the address of this variable such that
1260   // the address does not escape the function.
1261   void
1262   set_non_escaping_address_taken()
1263   { this->is_non_escaping_address_taken_ = true; }
1264
1265   // Get the source location of the variable's declaration.
1266   Location
1267   location() const
1268   { return this->location_; }
1269
1270   // Record that this is the varargs parameter of a function.
1271   void
1272   set_is_varargs_parameter()
1273   {
1274     go_assert(this->is_parameter_);
1275     this->is_varargs_parameter_ = true;
1276   }
1277
1278   // Return whether the variable has been used.
1279   bool
1280   is_used() const
1281   { return this->is_used_; }
1282
1283   // Mark that the variable has been used.
1284   void
1285   set_is_used()
1286   { this->is_used_ = true; }
1287
1288   // Clear the initial value; used for error handling.
1289   void
1290   clear_init()
1291   { this->init_ = NULL; }
1292
1293   // Set the initial value; used for converting shortcuts.
1294   void
1295   set_init(Expression* init)
1296   { this->init_ = init; }
1297
1298   // Get the preinit block, a block of statements to be run before the
1299   // initialization expression.
1300   Block*
1301   preinit_block(Gogo*);
1302
1303   // Add a statement to be run before the initialization expression.
1304   // This is only used for global variables.
1305   void
1306   add_preinit_statement(Gogo*, Statement*);
1307
1308   // Lower the initialization expression after parsing is complete.
1309   void
1310   lower_init_expression(Gogo*, Named_object*, Statement_inserter*);
1311
1312   // A special case: the init value is used only to determine the
1313   // type.  This is used if the variable is defined using := with the
1314   // comma-ok form of a map index or a receive expression.  The init
1315   // value is actually the map index expression or receive expression.
1316   // We use this because we may not know the right type at parse time.
1317   void
1318   set_type_from_init_tuple()
1319   { this->type_from_init_tuple_ = true; }
1320
1321   // Another special case: the init value is used only to determine
1322   // the type.  This is used if the variable is defined using := with
1323   // a range clause.  The init value is the range expression.  The
1324   // type of the variable is the index type of the range expression
1325   // (i.e., the first value returned by a range).
1326   void
1327   set_type_from_range_index()
1328   { this->type_from_range_index_ = true; }
1329
1330   // Another special case: like set_type_from_range_index, but the
1331   // type is the value type of the range expression (i.e., the second
1332   // value returned by a range).
1333   void
1334   set_type_from_range_value()
1335   { this->type_from_range_value_ = true; }
1336
1337   // Another special case: the init value is used only to determine
1338   // the type.  This is used if the variable is defined using := with
1339   // a case in a select statement.  The init value is the channel.
1340   // The type of the variable is the channel's element type.
1341   void
1342   set_type_from_chan_element()
1343   { this->type_from_chan_element_ = true; }
1344
1345   // After we lower the select statement, we once again set the type
1346   // from the initialization expression.
1347   void
1348   clear_type_from_chan_element()
1349   {
1350     go_assert(this->type_from_chan_element_);
1351     this->type_from_chan_element_ = false;
1352   }
1353
1354   // Note that this variable was created for a type switch clause.
1355   void
1356   set_is_type_switch_var()
1357   { this->is_type_switch_var_ = true; }
1358
1359   // Traverse the initializer expression.
1360   int
1361   traverse_expression(Traverse*, unsigned int traverse_mask);
1362
1363   // Determine the type of the variable if necessary.
1364   void
1365   determine_type();
1366
1367   // Get the backend representation of the variable.
1368   Bvariable*
1369   get_backend_variable(Gogo*, Named_object*, const Package*,
1370                        const std::string&);
1371
1372   // Get the initial value of the variable as a tree.  This may only
1373   // be called if has_pre_init() returns false.
1374   tree
1375   get_init_tree(Gogo*, Named_object* function);
1376
1377   // Return a series of statements which sets the value of the
1378   // variable in DECL.  This should only be called is has_pre_init()
1379   // returns true.  DECL may be NULL for a sink variable.
1380   tree
1381   get_init_block(Gogo*, Named_object* function, tree decl);
1382
1383   // Export the variable.
1384   void
1385   export_var(Export*, const std::string& name) const;
1386
1387   // Import a variable.
1388   static void
1389   import_var(Import*, std::string* pname, Type** ptype);
1390
1391  private:
1392   // The type of a tuple.
1393   Type*
1394   type_from_tuple(Expression*, bool) const;
1395
1396   // The type of a range.
1397   Type*
1398   type_from_range(Expression*, bool, bool) const;
1399
1400   // The element type of a channel.
1401   Type*
1402   type_from_chan_element(Expression*, bool) const;
1403
1404   // The variable's type.  This may be NULL if the type is set from
1405   // the expression.
1406   Type* type_;
1407   // The initial value.  This may be NULL if the variable should be
1408   // initialized to the default value for the type.
1409   Expression* init_;
1410   // Statements to run before the init statement.
1411   Block* preinit_;
1412   // Location of variable definition.
1413   Location location_;
1414   // Backend representation.
1415   Bvariable* backend_;
1416   // Whether this is a global variable.
1417   bool is_global_ : 1;
1418   // Whether this is a function parameter.
1419   bool is_parameter_ : 1;
1420   // Whether this is the receiver parameter of a method.
1421   bool is_receiver_ : 1;
1422   // Whether this is the varargs parameter of a function.
1423   bool is_varargs_parameter_ : 1;
1424   // Whether this variable is ever referenced.
1425   bool is_used_ : 1;
1426   // Whether something takes the address of this variable.  For a
1427   // local variable this implies that the variable has to be on the
1428   // heap.
1429   bool is_address_taken_ : 1;
1430   // Whether something takes the address of this variable such that
1431   // the address does not escape the function.
1432   bool is_non_escaping_address_taken_ : 1;
1433   // True if we have seen this variable in a traversal.
1434   bool seen_ : 1;
1435   // True if we have lowered the initialization expression.
1436   bool init_is_lowered_ : 1;
1437   // True if init is a tuple used to set the type.
1438   bool type_from_init_tuple_ : 1;
1439   // True if init is a range clause and the type is the index type.
1440   bool type_from_range_index_ : 1;
1441   // True if init is a range clause and the type is the value type.
1442   bool type_from_range_value_ : 1;
1443   // True if init is a channel and the type is the channel's element type.
1444   bool type_from_chan_element_ : 1;
1445   // True if this is a variable created for a type switch case.
1446   bool is_type_switch_var_ : 1;
1447   // True if we have determined types.
1448   bool determined_type_ : 1;
1449 };
1450
1451 // A variable which is really the name for a function return value, or
1452 // part of one.
1453
1454 class Result_variable
1455 {
1456  public:
1457   Result_variable(Type* type, Function* function, int index,
1458                   Location location)
1459     : type_(type), function_(function), index_(index), location_(location),
1460       backend_(NULL), is_address_taken_(false),
1461       is_non_escaping_address_taken_(false)
1462   { }
1463
1464   // Get the type of the result variable.
1465   Type*
1466   type() const
1467   { return this->type_; }
1468
1469   // Get the function that this is associated with.
1470   Function*
1471   function() const
1472   { return this->function_; }
1473
1474   // Index in the list of function results.
1475   int
1476   index() const
1477   { return this->index_; }
1478
1479   // The location of the variable definition.
1480   Location
1481   location() const
1482   { return this->location_; }
1483
1484   // Whether this variable's address is taken.
1485   bool
1486   is_address_taken() const
1487   { return this->is_address_taken_; }
1488
1489   // Note that something takes the address of this variable.
1490   void
1491   set_address_taken()
1492   { this->is_address_taken_ = true; }
1493
1494   // Return whether the address is taken but does not escape.
1495   bool
1496   is_non_escaping_address_taken() const
1497   { return this->is_non_escaping_address_taken_; }
1498
1499   // Note that something takes the address of this variable such that
1500   // the address does not escape the function.
1501   void
1502   set_non_escaping_address_taken()
1503   { this->is_non_escaping_address_taken_ = true; }
1504
1505   // Whether this variable should live in the heap.
1506   bool
1507   is_in_heap() const
1508   { return this->is_address_taken_; }
1509
1510   // Set the function.  This is used when cloning functions which call
1511   // recover.
1512   void
1513   set_function(Function* function)
1514   { this->function_ = function; }
1515
1516   // Get the backend representation of the variable.
1517   Bvariable*
1518   get_backend_variable(Gogo*, Named_object*, const std::string&);
1519
1520  private:
1521   // Type of result variable.
1522   Type* type_;
1523   // Function with which this is associated.
1524   Function* function_;
1525   // Index in list of results.
1526   int index_;
1527   // Where the result variable is defined.
1528   Location location_;
1529   // Backend representation.
1530   Bvariable* backend_;
1531   // Whether something takes the address of this variable.
1532   bool is_address_taken_;
1533   // Whether something takes the address of this variable such that
1534   // the address does not escape the function.
1535   bool is_non_escaping_address_taken_;
1536 };
1537
1538 // The value we keep for a named constant.  This lets us hold a type
1539 // and an expression.
1540
1541 class Named_constant
1542 {
1543  public:
1544   Named_constant(Type* type, Expression* expr, int iota_value,
1545                  Location location)
1546     : type_(type), expr_(expr), iota_value_(iota_value), location_(location),
1547       lowering_(false)
1548   { }
1549
1550   Type*
1551   type() const
1552   { return this->type_; }
1553
1554   Expression*
1555   expr() const
1556   { return this->expr_; }
1557
1558   int
1559   iota_value() const
1560   { return this->iota_value_; }
1561
1562   Location
1563   location() const
1564   { return this->location_; }
1565
1566   // Whether we are lowering.
1567   bool
1568   lowering() const
1569   { return this->lowering_; }
1570
1571   // Set that we are lowering.
1572   void
1573   set_lowering()
1574   { this->lowering_ = true; }
1575
1576   // We are no longer lowering.
1577   void
1578   clear_lowering()
1579   { this->lowering_ = false; }
1580
1581   // Traverse the expression.
1582   int
1583   traverse_expression(Traverse*);
1584
1585   // Determine the type of the constant if necessary.
1586   void
1587   determine_type();
1588
1589   // Indicate that we found and reported an error for this constant.
1590   void
1591   set_error();
1592
1593   // Export the constant.
1594   void
1595   export_const(Export*, const std::string& name) const;
1596
1597   // Import a constant.
1598   static void
1599   import_const(Import*, std::string*, Type**, Expression**);
1600
1601  private:
1602   // The type of the constant.
1603   Type* type_;
1604   // The expression for the constant.
1605   Expression* expr_;
1606   // If the predeclared constant iota is used in EXPR_, this is the
1607   // value it will have.  We do this because at parse time we don't
1608   // know whether the name "iota" will refer to the predeclared
1609   // constant or to something else.  We put in the right value in when
1610   // we lower.
1611   int iota_value_;
1612   // The location of the definition.
1613   Location location_;
1614   // Whether we are currently lowering this constant.
1615   bool lowering_;
1616 };
1617
1618 // A type declaration.
1619
1620 class Type_declaration
1621 {
1622  public:
1623   Type_declaration(Location location)
1624     : location_(location), in_function_(NULL), methods_(),
1625       issued_warning_(false)
1626   { }
1627
1628   // Return the location.
1629   Location
1630   location() const
1631   { return this->location_; }
1632
1633   // Return the function in which this type is declared.  This will
1634   // return NULL for a type declared in global scope.
1635   Named_object*
1636   in_function()
1637   { return this->in_function_; }
1638
1639   // Set the function in which this type is declared.
1640   void
1641   set_in_function(Named_object* f)
1642   { this->in_function_ = f; }
1643
1644   // Add a method to this type.  This is used when methods are defined
1645   // before the type.
1646   Named_object*
1647   add_method(const std::string& name, Function* function);
1648
1649   // Add a method declaration to this type.
1650   Named_object*
1651   add_method_declaration(const std::string& name, Package*,
1652                          Function_type* type, Location location);
1653
1654   // Return whether any methods were defined.
1655   bool
1656   has_methods() const;
1657
1658   // Define methods when the real type is known.
1659   void
1660   define_methods(Named_type*);
1661
1662   // This is called if we are trying to use this type.  It returns
1663   // true if we should issue a warning.
1664   bool
1665   using_type();
1666
1667  private:
1668   typedef std::vector<Named_object*> Methods;
1669
1670   // The location of the type declaration.
1671   Location location_;
1672   // If this type is declared in a function, a pointer back to the
1673   // function in which it is defined.
1674   Named_object* in_function_;
1675   // Methods defined before the type is defined.
1676   Methods methods_;
1677   // True if we have issued a warning about a use of this type
1678   // declaration when it is undefined.
1679   bool issued_warning_;
1680 };
1681
1682 // An unknown object.  These are created by the parser for forward
1683 // references to names which have not been seen before.  In a correct
1684 // program, these will always point to a real definition by the end of
1685 // the parse.  Because they point to another Named_object, these may
1686 // only be referenced by Unknown_expression objects.
1687
1688 class Unknown_name
1689 {
1690  public:
1691   Unknown_name(Location location)
1692     : location_(location), real_named_object_(NULL)
1693   { }
1694
1695   // Return the location where this name was first seen.
1696   Location
1697   location() const
1698   { return this->location_; }
1699
1700   // Return the real named object that this points to, or NULL if it
1701   // was never resolved.
1702   Named_object*
1703   real_named_object() const
1704   { return this->real_named_object_; }
1705
1706   // Set the real named object that this points to.
1707   void
1708   set_real_named_object(Named_object* no);
1709
1710  private:
1711   // The location where this name was first seen.
1712   Location location_;
1713   // The real named object when it is known.
1714   Named_object*
1715   real_named_object_;
1716 };
1717
1718 // A named object named.  This is the result of a declaration.  We
1719 // don't use a superclass because they all have to be handled
1720 // differently.
1721
1722 class Named_object
1723 {
1724  public:
1725   enum Classification
1726   {
1727     // An uninitialized Named_object.  We should never see this.
1728     NAMED_OBJECT_UNINITIALIZED,
1729     // An erroneous name.  This indicates a parse error, to avoid
1730     // later errors about undefined references.
1731     NAMED_OBJECT_ERRONEOUS,
1732     // An unknown name.  This is used for forward references.  In a
1733     // correct program, these will all be resolved by the end of the
1734     // parse.
1735     NAMED_OBJECT_UNKNOWN,
1736     // A const.
1737     NAMED_OBJECT_CONST,
1738     // A type.
1739     NAMED_OBJECT_TYPE,
1740     // A forward type declaration.
1741     NAMED_OBJECT_TYPE_DECLARATION,
1742     // A var.
1743     NAMED_OBJECT_VAR,
1744     // A result variable in a function.
1745     NAMED_OBJECT_RESULT_VAR,
1746     // The blank identifier--the special variable named _.
1747     NAMED_OBJECT_SINK,
1748     // A func.
1749     NAMED_OBJECT_FUNC,
1750     // A forward func declaration.
1751     NAMED_OBJECT_FUNC_DECLARATION,
1752     // A package.
1753     NAMED_OBJECT_PACKAGE
1754   };
1755
1756   // Return the classification.
1757   Classification
1758   classification() const
1759   { return this->classification_; }
1760
1761   // Classifiers.
1762
1763   bool
1764   is_erroneous() const
1765   { return this->classification_ == NAMED_OBJECT_ERRONEOUS; }
1766
1767   bool
1768   is_unknown() const
1769   { return this->classification_ == NAMED_OBJECT_UNKNOWN; }
1770
1771   bool
1772   is_const() const
1773   { return this->classification_ == NAMED_OBJECT_CONST; }
1774
1775   bool
1776   is_type() const
1777   { return this->classification_ == NAMED_OBJECT_TYPE; }
1778
1779   bool
1780   is_type_declaration() const
1781   { return this->classification_ == NAMED_OBJECT_TYPE_DECLARATION; }
1782
1783   bool
1784   is_variable() const
1785   { return this->classification_ == NAMED_OBJECT_VAR; }
1786
1787   bool
1788   is_result_variable() const
1789   { return this->classification_ == NAMED_OBJECT_RESULT_VAR; }
1790
1791   bool
1792   is_sink() const
1793   { return this->classification_ == NAMED_OBJECT_SINK; }
1794
1795   bool
1796   is_function() const
1797   { return this->classification_ == NAMED_OBJECT_FUNC; }
1798
1799   bool
1800   is_function_declaration() const
1801   { return this->classification_ == NAMED_OBJECT_FUNC_DECLARATION; }
1802
1803   bool
1804   is_package() const
1805   { return this->classification_ == NAMED_OBJECT_PACKAGE; }
1806
1807   // Creators.
1808
1809   static Named_object*
1810   make_erroneous_name(const std::string& name)
1811   { return new Named_object(name, NULL, NAMED_OBJECT_ERRONEOUS); }
1812
1813   static Named_object*
1814   make_unknown_name(const std::string& name, Location);
1815
1816   static Named_object*
1817   make_constant(const Typed_identifier&, const Package*, Expression*,
1818                 int iota_value);
1819
1820   static Named_object*
1821   make_type(const std::string&, const Package*, Type*, Location);
1822
1823   static Named_object*
1824   make_type_declaration(const std::string&, const Package*, Location);
1825
1826   static Named_object*
1827   make_variable(const std::string&, const Package*, Variable*);
1828
1829   static Named_object*
1830   make_result_variable(const std::string&, Result_variable*);
1831
1832   static Named_object*
1833   make_sink();
1834
1835   static Named_object*
1836   make_function(const std::string&, const Package*, Function*);
1837
1838   static Named_object*
1839   make_function_declaration(const std::string&, const Package*, Function_type*,
1840                             Location);
1841
1842   static Named_object*
1843   make_package(const std::string& alias, Package* package);
1844
1845   // Getters.
1846
1847   Unknown_name*
1848   unknown_value()
1849   {
1850     go_assert(this->classification_ == NAMED_OBJECT_UNKNOWN);
1851     return this->u_.unknown_value;
1852   }
1853
1854   const Unknown_name*
1855   unknown_value() const
1856   {
1857     go_assert(this->classification_ == NAMED_OBJECT_UNKNOWN);
1858     return this->u_.unknown_value;
1859   }
1860
1861   Named_constant*
1862   const_value()
1863   {
1864     go_assert(this->classification_ == NAMED_OBJECT_CONST);
1865     return this->u_.const_value;
1866   }
1867
1868   const Named_constant*
1869   const_value() const
1870   {
1871     go_assert(this->classification_ == NAMED_OBJECT_CONST);
1872     return this->u_.const_value;
1873   }
1874
1875   Named_type*
1876   type_value()
1877   {
1878     go_assert(this->classification_ == NAMED_OBJECT_TYPE);
1879     return this->u_.type_value;
1880   }
1881
1882   const Named_type*
1883   type_value() const
1884   {
1885     go_assert(this->classification_ == NAMED_OBJECT_TYPE);
1886     return this->u_.type_value;
1887   }
1888
1889   Type_declaration*
1890   type_declaration_value()
1891   {
1892     go_assert(this->classification_ == NAMED_OBJECT_TYPE_DECLARATION);
1893     return this->u_.type_declaration;
1894   }
1895
1896   const Type_declaration*
1897   type_declaration_value() const
1898   {
1899     go_assert(this->classification_ == NAMED_OBJECT_TYPE_DECLARATION);
1900     return this->u_.type_declaration;
1901   }
1902
1903   Variable*
1904   var_value()
1905   {
1906     go_assert(this->classification_ == NAMED_OBJECT_VAR);
1907     return this->u_.var_value;
1908   }
1909
1910   const Variable*
1911   var_value() const
1912   {
1913     go_assert(this->classification_ == NAMED_OBJECT_VAR);
1914     return this->u_.var_value;
1915   }
1916
1917   Result_variable*
1918   result_var_value()
1919   {
1920     go_assert(this->classification_ == NAMED_OBJECT_RESULT_VAR);
1921     return this->u_.result_var_value;
1922   }
1923
1924   const Result_variable*
1925   result_var_value() const
1926   {
1927     go_assert(this->classification_ == NAMED_OBJECT_RESULT_VAR);
1928     return this->u_.result_var_value;
1929   }
1930
1931   Function*
1932   func_value()
1933   {
1934     go_assert(this->classification_ == NAMED_OBJECT_FUNC);
1935     return this->u_.func_value;
1936   }
1937
1938   const Function*
1939   func_value() const
1940   {
1941     go_assert(this->classification_ == NAMED_OBJECT_FUNC);
1942     return this->u_.func_value;
1943   }
1944
1945   Function_declaration*
1946   func_declaration_value()
1947   {
1948     go_assert(this->classification_ == NAMED_OBJECT_FUNC_DECLARATION);
1949     return this->u_.func_declaration_value;
1950   }
1951
1952   const Function_declaration*
1953   func_declaration_value() const
1954   {
1955     go_assert(this->classification_ == NAMED_OBJECT_FUNC_DECLARATION);
1956     return this->u_.func_declaration_value;
1957   }
1958
1959   Package*
1960   package_value()
1961   {
1962     go_assert(this->classification_ == NAMED_OBJECT_PACKAGE);
1963     return this->u_.package_value;
1964   }
1965
1966   const Package*
1967   package_value() const
1968   {
1969     go_assert(this->classification_ == NAMED_OBJECT_PACKAGE);
1970     return this->u_.package_value;
1971   }
1972
1973   const std::string&
1974   name() const
1975   { return this->name_; }
1976
1977   // Return the name to use in an error message.  The difference is
1978   // that if this Named_object is defined in a different package, this
1979   // will return PACKAGE.NAME.
1980   std::string
1981   message_name() const;
1982
1983   const Package*
1984   package() const
1985   { return this->package_; }
1986
1987   // Resolve an unknown value if possible.  This returns the same
1988   // Named_object or a new one.
1989   Named_object*
1990   resolve()
1991   {
1992     Named_object* ret = this;
1993     if (this->is_unknown())
1994       {
1995         Named_object* r = this->unknown_value()->real_named_object();
1996         if (r != NULL)
1997           ret = r;
1998       }
1999     return ret;
2000   }
2001
2002   const Named_object*
2003   resolve() const
2004   {
2005     const Named_object* ret = this;
2006     if (this->is_unknown())
2007       {
2008         const Named_object* r = this->unknown_value()->real_named_object();
2009         if (r != NULL)
2010           ret = r;
2011       }
2012     return ret;
2013   }
2014
2015   // The location where this object was defined or referenced.
2016   Location
2017   location() const;
2018
2019   // Convert a variable to the backend representation.
2020   Bvariable*
2021   get_backend_variable(Gogo*, Named_object* function);
2022
2023   // Return a tree for the external identifier for this object.
2024   tree
2025   get_id(Gogo*);
2026
2027   // Return a tree representing this object.
2028   tree
2029   get_tree(Gogo*, Named_object* function);
2030
2031   // Define a type declaration.
2032   void
2033   set_type_value(Named_type*);
2034
2035   // Define a function declaration.
2036   void
2037   set_function_value(Function*);
2038
2039   // Declare an unknown name as a type declaration.
2040   void
2041   declare_as_type();
2042
2043   // Export this object.
2044   void
2045   export_named_object(Export*) const;
2046
2047  private:
2048   Named_object(const std::string&, const Package*, Classification);
2049
2050   // The name of the object.
2051   std::string name_;
2052   // The package that this object is in.  This is NULL if it is in the
2053   // file we are compiling.
2054   const Package* package_;
2055   // The type of object this is.
2056   Classification classification_;
2057   // The real data.
2058   union
2059   {
2060     Unknown_name* unknown_value;
2061     Named_constant* const_value;
2062     Named_type* type_value;
2063     Type_declaration* type_declaration;
2064     Variable* var_value;
2065     Result_variable* result_var_value;
2066     Function* func_value;
2067     Function_declaration* func_declaration_value;
2068     Package* package_value;
2069   } u_;
2070   // The DECL tree for this object if we have already converted it.
2071   tree tree_;
2072 };
2073
2074 // A binding contour.  This binds names to objects.
2075
2076 class Bindings
2077 {
2078  public:
2079   // Type for mapping from names to objects.
2080   typedef Unordered_map(std::string, Named_object*) Contour;
2081
2082   Bindings(Bindings* enclosing);
2083
2084   // Add an erroneous name.
2085   Named_object*
2086   add_erroneous_name(const std::string& name)
2087   { return this->add_named_object(Named_object::make_erroneous_name(name)); }
2088
2089   // Add an unknown name.
2090   Named_object*
2091   add_unknown_name(const std::string& name, Location location)
2092   {
2093     return this->add_named_object(Named_object::make_unknown_name(name,
2094                                                                   location));
2095   }
2096
2097   // Add a constant.
2098   Named_object*
2099   add_constant(const Typed_identifier& tid, const Package* package,
2100                Expression* expr, int iota_value)
2101   {
2102     return this->add_named_object(Named_object::make_constant(tid, package,
2103                                                               expr,
2104                                                               iota_value));
2105   }
2106
2107   // Add a type.
2108   Named_object*
2109   add_type(const std::string& name, const Package* package, Type* type,
2110            Location location)
2111   {
2112     return this->add_named_object(Named_object::make_type(name, package, type,
2113                                                           location));
2114   }
2115
2116   // Add a named type.  This is used for builtin types, and to add an
2117   // imported type to the global scope.
2118   Named_object*
2119   add_named_type(Named_type* named_type);
2120
2121   // Add a type declaration.
2122   Named_object*
2123   add_type_declaration(const std::string& name, const Package* package,
2124                        Location location)
2125   {
2126     Named_object* no = Named_object::make_type_declaration(name, package,
2127                                                            location);
2128     return this->add_named_object(no);
2129   }
2130
2131   // Add a variable.
2132   Named_object*
2133   add_variable(const std::string& name, const Package* package,
2134                Variable* variable)
2135   {
2136     return this->add_named_object(Named_object::make_variable(name, package,
2137                                                               variable));
2138   }
2139
2140   // Add a result variable.
2141   Named_object*
2142   add_result_variable(const std::string& name, Result_variable* result)
2143   {
2144     return this->add_named_object(Named_object::make_result_variable(name,
2145                                                                      result));
2146   }
2147
2148   // Add a function.
2149   Named_object*
2150   add_function(const std::string& name, const Package*, Function* function);
2151
2152   // Add a function declaration.
2153   Named_object*
2154   add_function_declaration(const std::string& name, const Package* package,
2155                            Function_type* type, Location location);
2156
2157   // Add a package.  The location is the location of the import
2158   // statement.
2159   Named_object*
2160   add_package(const std::string& alias, Package* package)
2161   {
2162     Named_object* no = Named_object::make_package(alias, package);
2163     return this->add_named_object(no);
2164   }
2165
2166   // Define a type which was already declared.
2167   void
2168   define_type(Named_object*, Named_type*);
2169
2170   // Add a method to the list of objects.  This is not added to the
2171   // lookup table.
2172   void
2173   add_method(Named_object*);
2174
2175   // Add a named object to this binding.
2176   Named_object*
2177   add_named_object(Named_object* no)
2178   { return this->add_named_object_to_contour(&this->bindings_, no); }
2179
2180   // Clear all names in file scope from the bindings.
2181   void
2182   clear_file_scope();
2183
2184   // Look up a name in this binding contour and in any enclosing
2185   // binding contours.  This returns NULL if the name is not found.
2186   Named_object*
2187   lookup(const std::string&) const;
2188
2189   // Look up a name in this binding contour without looking in any
2190   // enclosing binding contours.  Returns NULL if the name is not found.
2191   Named_object*
2192   lookup_local(const std::string&) const;
2193
2194   // Remove a name.
2195   void
2196   remove_binding(Named_object*);
2197
2198   // Mark all variables as used.  This is used for some types of parse
2199   // error.
2200   void
2201   mark_locals_used();
2202
2203   // Traverse the tree.  See the Traverse class.
2204   int
2205   traverse(Traverse*, bool is_global);
2206
2207   // Iterate over definitions.  This does not include things which
2208   // were only declared.
2209
2210   typedef std::vector<Named_object*>::const_iterator
2211     const_definitions_iterator;
2212
2213   const_definitions_iterator
2214   begin_definitions() const
2215   { return this->named_objects_.begin(); }
2216
2217   const_definitions_iterator
2218   end_definitions() const
2219   { return this->named_objects_.end(); }
2220
2221   // Return the number of definitions.
2222   size_t
2223   size_definitions() const
2224   { return this->named_objects_.size(); }
2225
2226   // Return whether there are no definitions.
2227   bool
2228   empty_definitions() const
2229   { return this->named_objects_.empty(); }
2230
2231   // Iterate over declarations.  This is everything that has been
2232   // declared, which includes everything which has been defined.
2233
2234   typedef Contour::const_iterator const_declarations_iterator;
2235
2236   const_declarations_iterator
2237   begin_declarations() const
2238   { return this->bindings_.begin(); }
2239
2240   const_declarations_iterator
2241   end_declarations() const
2242   { return this->bindings_.end(); }
2243
2244   // Return the number of declarations.
2245   size_t
2246   size_declarations() const
2247   { return this->bindings_.size(); }
2248
2249   // Return whether there are no declarations.
2250   bool
2251   empty_declarations() const
2252   { return this->bindings_.empty(); }
2253
2254   // Return the first declaration.
2255   Named_object*
2256   first_declaration()
2257   { return this->bindings_.empty() ? NULL : this->bindings_.begin()->second; }
2258
2259  private:
2260   Named_object*
2261   add_named_object_to_contour(Contour*, Named_object*);
2262
2263   Named_object*
2264   new_definition(Named_object*, Named_object*);
2265
2266   // Enclosing bindings.
2267   Bindings* enclosing_;
2268   // The list of objects.
2269   std::vector<Named_object*> named_objects_;
2270   // The mapping from names to objects.
2271   Contour bindings_;
2272 };
2273
2274 // A label.
2275
2276 class Label
2277 {
2278  public:
2279   Label(const std::string& name)
2280     : name_(name), location_(Linemap::unknown_location()), snapshot_(NULL),
2281       refs_(), is_used_(false), blabel_(NULL)
2282   { }
2283
2284   // Return the label's name.
2285   const std::string&
2286   name() const
2287   { return this->name_; }
2288
2289   // Return whether the label has been defined.
2290   bool
2291   is_defined() const
2292   { return !Linemap::is_unknown_location(this->location_); }
2293
2294   // Return whether the label has been used.
2295   bool
2296   is_used() const
2297   { return this->is_used_; }
2298
2299   // Record that the label is used.
2300   void
2301   set_is_used()
2302   { this->is_used_ = true; }
2303
2304   // Return the location of the definition.
2305   Location
2306   location() const
2307   { return this->location_; }
2308
2309   // Return the bindings snapshot.
2310   Bindings_snapshot*
2311   snapshot() const
2312   { return this->snapshot_; }
2313
2314   // Add a snapshot of a goto which refers to this label.
2315   void
2316   add_snapshot_ref(Bindings_snapshot* snapshot)
2317   {
2318     go_assert(Linemap::is_unknown_location(this->location_));
2319     this->refs_.push_back(snapshot);
2320   }
2321
2322   // Return the list of snapshots of goto statements which refer to
2323   // this label.
2324   const std::vector<Bindings_snapshot*>&
2325   refs() const
2326   { return this->refs_; }
2327
2328   // Clear the references.
2329   void
2330   clear_refs();
2331
2332   // Define the label at LOCATION with the given bindings snapshot.
2333   void
2334   define(Location location, Bindings_snapshot* snapshot)
2335   {
2336     go_assert(Linemap::is_unknown_location(this->location_)
2337               && this->snapshot_ == NULL);
2338     this->location_ = location;
2339     this->snapshot_ = snapshot;
2340   }
2341
2342   // Return the backend representation for this label.
2343   Blabel*
2344   get_backend_label(Translate_context*);
2345
2346   // Return an expression for the address of this label.  This is used
2347   // to get the return address of a deferred function to see whether
2348   // the function may call recover.
2349   Bexpression*
2350   get_addr(Translate_context*, Location location);
2351
2352  private:
2353   // The name of the label.
2354   std::string name_;
2355   // The location of the definition.  This is 0 if the label has not
2356   // yet been defined.
2357   Location location_;
2358   // A snapshot of the set of bindings defined at this label, used to
2359   // issue errors about invalid goto statements.
2360   Bindings_snapshot* snapshot_;
2361   // A list of snapshots of goto statements which refer to this label.
2362   std::vector<Bindings_snapshot*> refs_;
2363   // Whether the label has been used.
2364   bool is_used_;
2365   // The backend representation.
2366   Blabel* blabel_;
2367 };
2368
2369 // An unnamed label.  These are used when lowering loops.
2370
2371 class Unnamed_label
2372 {
2373  public:
2374   Unnamed_label(Location location)
2375     : location_(location), blabel_(NULL)
2376   { }
2377
2378   // Get the location where the label is defined.
2379   Location
2380   location() const
2381   { return this->location_; }
2382
2383   // Set the location where the label is defined.
2384   void
2385   set_location(Location location)
2386   { this->location_ = location; }
2387
2388   // Return a statement which defines this label.
2389   Bstatement*
2390   get_definition(Translate_context*);
2391
2392   // Return a goto to this label from LOCATION.
2393   Bstatement*
2394   get_goto(Translate_context*, Location location);
2395
2396  private:
2397   // Return the backend representation.
2398   Blabel*
2399   get_blabel(Translate_context*);
2400
2401   // The location where the label is defined.
2402   Location location_;
2403   // The backend representation of this label.
2404   Blabel* blabel_;
2405 };
2406
2407 // An imported package.
2408
2409 class Package
2410 {
2411  public:
2412   Package(const std::string& name, const std::string& unique_prefix,
2413           Location location);
2414
2415   // The real name of this package.  This may be different from the
2416   // name in the associated Named_object if the import statement used
2417   // an alias.
2418   const std::string&
2419   name() const
2420   { return this->name_; }
2421
2422   // Return the location of the import statement.
2423   Location
2424   location() const
2425   { return this->location_; }
2426
2427   // Get the unique prefix used for all symbols exported from this
2428   // package.
2429   const std::string&
2430   unique_prefix() const
2431   {
2432     go_assert(!this->unique_prefix_.empty());
2433     return this->unique_prefix_;
2434   }
2435
2436   // The priority of this package.  The init function of packages with
2437   // lower priority must be run before the init function of packages
2438   // with higher priority.
2439   int
2440   priority() const
2441   { return this->priority_; }
2442
2443   // Set the priority.
2444   void
2445   set_priority(int priority);
2446
2447   // Return the bindings.
2448   Bindings*
2449   bindings()
2450   { return this->bindings_; }
2451
2452   // Whether some symbol from the package was used.
2453   bool
2454   used() const
2455   { return this->used_; }
2456
2457   // Note that some symbol from this package was used.
2458   void
2459   set_used() const
2460   { this->used_ = true; }
2461
2462   // Clear the used field for the next file.
2463   void
2464   clear_used()
2465   { this->used_ = false; }
2466
2467   // Whether this package was imported in the current file.
2468   bool
2469   is_imported() const
2470   { return this->is_imported_; }
2471
2472   // Note that this package was imported in the current file.
2473   void
2474   set_is_imported()
2475   { this->is_imported_ = true; }
2476
2477   // Clear the imported field for the next file.
2478   void
2479   clear_is_imported()
2480   { this->is_imported_ = false; }
2481
2482   // Whether this package was imported with a name of "_".
2483   bool
2484   uses_sink_alias() const
2485   { return this->uses_sink_alias_; }
2486
2487   // Note that this package was imported with a name of "_".
2488   void
2489   set_uses_sink_alias()
2490   { this->uses_sink_alias_ = true; }
2491
2492   // Clear the sink alias field for the next file.
2493   void
2494   clear_uses_sink_alias()
2495   { this->uses_sink_alias_ = false; }
2496
2497   // Look up a name in the package.  Returns NULL if the name is not
2498   // found.
2499   Named_object*
2500   lookup(const std::string& name) const
2501   { return this->bindings_->lookup(name); }
2502
2503   // Set the location of the package.  This is used if it is seen in a
2504   // different import before it is really imported.
2505   void
2506   set_location(Location location)
2507   { this->location_ = location; }
2508
2509   // Add a constant to the package.
2510   Named_object*
2511   add_constant(const Typed_identifier& tid, Expression* expr)
2512   { return this->bindings_->add_constant(tid, this, expr, 0); }
2513
2514   // Add a type to the package.
2515   Named_object*
2516   add_type(const std::string& name, Type* type, Location location)
2517   { return this->bindings_->add_type(name, this, type, location); }
2518
2519   // Add a type declaration to the package.
2520   Named_object*
2521   add_type_declaration(const std::string& name, Location location)
2522   { return this->bindings_->add_type_declaration(name, this, location); }
2523
2524   // Add a variable to the package.
2525   Named_object*
2526   add_variable(const std::string& name, Variable* variable)
2527   { return this->bindings_->add_variable(name, this, variable); }
2528
2529   // Add a function declaration to the package.
2530   Named_object*
2531   add_function_declaration(const std::string& name, Function_type* type,
2532                            Location loc)
2533   { return this->bindings_->add_function_declaration(name, this, type, loc); }
2534
2535   // Determine types of constants.
2536   void
2537   determine_types();
2538
2539  private:
2540   // The real name of this package.
2541   std::string name_;
2542   // The unique prefix for all exported global symbols.
2543   std::string unique_prefix_;
2544   // The names in this package.
2545   Bindings* bindings_;
2546   // The priority of this package.  A package has a priority higher
2547   // than the priority of all of the packages that it imports.  This
2548   // is used to run init functions in the right order.
2549   int priority_;
2550   // The location of the import statement.
2551   Location location_;
2552   // True if some name from this package was used.  This is mutable
2553   // because we can use a package even if we have a const pointer to
2554   // it.
2555   mutable bool used_;
2556   // True if this package was imported in the current file.
2557   bool is_imported_;
2558   // True if this package was imported with a name of "_".
2559   bool uses_sink_alias_;
2560 };
2561
2562 // Return codes for the traversal functions.  This is not an enum
2563 // because we want to be able to declare traversal functions in other
2564 // header files without including this one.
2565
2566 // Continue traversal as usual.
2567 const int TRAVERSE_CONTINUE = -1;
2568
2569 // Exit traversal.
2570 const int TRAVERSE_EXIT = 0;
2571
2572 // Continue traversal, but skip components of the current object.
2573 // E.g., if this is returned by Traverse::statement, we do not
2574 // traverse the expressions in the statement even if
2575 // traverse_expressions is set in the traverse_mask.
2576 const int TRAVERSE_SKIP_COMPONENTS = 1;
2577
2578 // This class is used when traversing the parse tree.  The caller uses
2579 // a subclass which overrides functions as desired.
2580
2581 class Traverse
2582 {
2583  public:
2584   // These bitmasks say what to traverse.
2585   static const unsigned int traverse_variables =    0x1;
2586   static const unsigned int traverse_constants =    0x2;
2587   static const unsigned int traverse_functions =    0x4;
2588   static const unsigned int traverse_blocks =       0x8;
2589   static const unsigned int traverse_statements =  0x10;
2590   static const unsigned int traverse_expressions = 0x20;
2591   static const unsigned int traverse_types =       0x40;
2592
2593   Traverse(unsigned int traverse_mask)
2594     : traverse_mask_(traverse_mask), types_seen_(NULL), expressions_seen_(NULL)
2595   { }
2596
2597   virtual ~Traverse();
2598
2599   // The bitmask of what to traverse.
2600   unsigned int
2601   traverse_mask() const
2602   { return this->traverse_mask_; }
2603
2604   // Record that we are going to traverse a type.  This returns true
2605   // if the type has already been seen in this traversal.  This is
2606   // required because types, unlike expressions, can form a circular
2607   // graph.
2608   bool
2609   remember_type(const Type*);
2610
2611   // Record that we are going to see an expression.  This returns true
2612   // if the expression has already been seen in this traversal.  This
2613   // is only needed for cases where multiple expressions can point to
2614   // a single one.
2615   bool
2616   remember_expression(const Expression*);
2617
2618   // These functions return one of the TRAVERSE codes defined above.
2619
2620   // If traverse_variables is set in the mask, this is called for
2621   // every variable in the tree.
2622   virtual int
2623   variable(Named_object*);
2624
2625   // If traverse_constants is set in the mask, this is called for
2626   // every named constant in the tree.  The bool parameter is true for
2627   // a global constant.
2628   virtual int
2629   constant(Named_object*, bool);
2630
2631   // If traverse_functions is set in the mask, this is called for
2632   // every function in the tree.
2633   virtual int
2634   function(Named_object*);
2635
2636   // If traverse_blocks is set in the mask, this is called for every
2637   // block in the tree.
2638   virtual int
2639   block(Block*);
2640
2641   // If traverse_statements is set in the mask, this is called for
2642   // every statement in the tree.
2643   virtual int
2644   statement(Block*, size_t* index, Statement*);
2645
2646   // If traverse_expressions is set in the mask, this is called for
2647   // every expression in the tree.
2648   virtual int
2649   expression(Expression**);
2650
2651   // If traverse_types is set in the mask, this is called for every
2652   // type in the tree.
2653   virtual int
2654   type(Type*);
2655
2656  private:
2657   // A hash table for types we have seen during this traversal.  Note
2658   // that this uses the default hash functions for pointers rather
2659   // than Type_hash_identical and Type_identical.  This is because for
2660   // traversal we care about seeing a specific type structure.  If
2661   // there are two separate instances of identical types, we want to
2662   // traverse both.
2663   typedef Unordered_set(const Type*) Types_seen;
2664
2665   typedef Unordered_set(const Expression*) Expressions_seen;
2666
2667   // Bitmask of what sort of objects to traverse.
2668   unsigned int traverse_mask_;
2669   // Types which have been seen in this traversal.
2670   Types_seen* types_seen_;
2671   // Expressions which have been seen in this traversal.
2672   Expressions_seen* expressions_seen_;
2673 };
2674
2675 // A class which makes it easier to insert new statements before the
2676 // current statement during a traversal.
2677
2678 class Statement_inserter
2679 {
2680  public:
2681   // Empty constructor.
2682   Statement_inserter()
2683     : block_(NULL), pindex_(NULL), gogo_(NULL), var_(NULL)
2684   { }
2685
2686   // Constructor for a statement in a block.
2687   Statement_inserter(Block* block, size_t *pindex)
2688     : block_(block), pindex_(pindex), gogo_(NULL), var_(NULL)
2689   { }
2690
2691   // Constructor for a global variable.
2692   Statement_inserter(Gogo* gogo, Variable* var)
2693     : block_(NULL), pindex_(NULL), gogo_(gogo), var_(var)
2694   { go_assert(var->is_global()); }
2695
2696   // We use the default copy constructor and assignment operator.
2697
2698   // Insert S before the statement we are traversing, or before the
2699   // initialization expression of a global variable.
2700   void
2701   insert(Statement* s);
2702
2703  private:
2704   // The block that the statement is in.
2705   Block* block_;
2706   // The index of the statement that we are traversing.
2707   size_t* pindex_;
2708   // The IR, needed when looking at an initializer expression for a
2709   // global variable.
2710   Gogo* gogo_;
2711   // The global variable, when looking at an initializer expression.
2712   Variable* var_;
2713 };
2714
2715 // When translating the gogo IR into the backend data structure, this
2716 // is the context we pass down the blocks and statements.
2717
2718 class Translate_context
2719 {
2720  public:
2721   Translate_context(Gogo* gogo, Named_object* function, Block* block,
2722                     Bblock* bblock)
2723     : gogo_(gogo), backend_(gogo->backend()), function_(function),
2724       block_(block), bblock_(bblock), is_const_(false)
2725   { }
2726
2727   // Accessors.
2728
2729   Gogo*
2730   gogo()
2731   { return this->gogo_; }
2732
2733   Backend*
2734   backend()
2735   { return this->backend_; }
2736
2737   Named_object*
2738   function()
2739   { return this->function_; }
2740
2741   Block*
2742   block()
2743   { return this->block_; }
2744
2745   Bblock*
2746   bblock()
2747   { return this->bblock_; }
2748
2749   bool
2750   is_const()
2751   { return this->is_const_; }
2752
2753   // Make a constant context.
2754   void
2755   set_is_const()
2756   { this->is_const_ = true; }
2757
2758  private:
2759   // The IR for the entire compilation unit.
2760   Gogo* gogo_;
2761   // The generator for the backend data structures.
2762   Backend* backend_;
2763   // The function we are currently translating.  NULL if not in a
2764   // function, e.g., the initializer of a global variable.
2765   Named_object* function_;
2766   // The block we are currently translating.  NULL if not in a
2767   // function.
2768   Block *block_;
2769   // The backend representation of the current block.  NULL if block_
2770   // is NULL.
2771   Bblock* bblock_;
2772   // Whether this is being evaluated in a constant context.  This is
2773   // used for type descriptor initializers.
2774   bool is_const_;
2775 };
2776
2777 // Runtime error codes.  These must match the values in
2778 // libgo/runtime/go-runtime-error.c.
2779
2780 // Slice index out of bounds: negative or larger than the length of
2781 // the slice.
2782 static const int RUNTIME_ERROR_SLICE_INDEX_OUT_OF_BOUNDS = 0;
2783
2784 // Array index out of bounds.
2785 static const int RUNTIME_ERROR_ARRAY_INDEX_OUT_OF_BOUNDS = 1;
2786
2787 // String index out of bounds.
2788 static const int RUNTIME_ERROR_STRING_INDEX_OUT_OF_BOUNDS = 2;
2789
2790 // Slice slice out of bounds: negative or larger than the length of
2791 // the slice or high bound less than low bound.
2792 static const int RUNTIME_ERROR_SLICE_SLICE_OUT_OF_BOUNDS = 3;
2793
2794 // Array slice out of bounds.
2795 static const int RUNTIME_ERROR_ARRAY_SLICE_OUT_OF_BOUNDS = 4;
2796
2797 // String slice out of bounds.
2798 static const int RUNTIME_ERROR_STRING_SLICE_OUT_OF_BOUNDS = 5;
2799
2800 // Dereference of nil pointer.  This is used when there is a
2801 // dereference of a pointer to a very large struct or array, to ensure
2802 // that a gigantic array is not used a proxy to access random memory
2803 // locations.
2804 static const int RUNTIME_ERROR_NIL_DEREFERENCE = 6;
2805
2806 // Slice length or capacity out of bounds in make: negative or
2807 // overflow or length greater than capacity.
2808 static const int RUNTIME_ERROR_MAKE_SLICE_OUT_OF_BOUNDS = 7;
2809
2810 // Map capacity out of bounds in make: negative or overflow.
2811 static const int RUNTIME_ERROR_MAKE_MAP_OUT_OF_BOUNDS = 8;
2812
2813 // Channel capacity out of bounds in make: negative or overflow.
2814 static const int RUNTIME_ERROR_MAKE_CHAN_OUT_OF_BOUNDS = 9;
2815
2816 // Division by zero.
2817 static const int RUNTIME_ERROR_DIVISION_BY_ZERO = 10;
2818
2819 // This is used by some of the langhooks.
2820 extern Gogo* go_get_gogo();
2821
2822 // Whether we have seen any errors.  FIXME: Replace with a backend
2823 // interface.
2824 extern bool saw_errors();
2825
2826 #endif // !defined(GO_GOGO_H)