OSDN Git Service

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