OSDN Git Service

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