OSDN Git Service

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