OSDN Git Service

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