OSDN Git Service

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