OSDN Git Service

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