OSDN Git Service

Use backend interface for type descriptors.
[pf3gnuchains/gcc-fork.git] / gcc / go / gofrontend / gogo.h
1 // gogo.h -- Go frontend parsed representation.     -*- C++ -*-
2
3 // Copyright 2009 The Go Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
6
7 #ifndef GO_GOGO_H
8 #define GO_GOGO_H
9
10 class Traverse;
11 class Type;
12 class Type_hash_identical;
13 class Type_equal;
14 class Type_identical;
15 class Typed_identifier;
16 class Typed_identifier_list;
17 class Function_type;
18 class Expression;
19 class Statement;
20 class Temporary_statement;
21 class Block;
22 class Function;
23 class Bindings;
24 class Package;
25 class Variable;
26 class Pointer_type;
27 class Struct_type;
28 class Struct_field;
29 class Struct_field_list;
30 class Array_type;
31 class Map_type;
32 class Channel_type;
33 class Interface_type;
34 class Named_type;
35 class Forward_declaration_type;
36 class Method;
37 class Methods;
38 class Named_object;
39 class Label;
40 class Translate_context;
41 class Backend;
42 class Export;
43 class Import;
44 class Bexpression;
45 class Bstatement;
46 class Bblock;
47 class Bvariable;
48 class Blabel;
49
50 // This file declares the basic classes used to hold the internal
51 // representation of Go which is built by the parser.
52
53 // An initialization function for an imported package.  This is a
54 // magic function which initializes variables and runs the "init"
55 // function.
56
57 class Import_init
58 {
59  public:
60   Import_init(const std::string& package_name, const std::string& init_name,
61               int priority)
62     : package_name_(package_name), init_name_(init_name), priority_(priority)
63   { }
64
65   // The name of the package being imported.
66   const std::string&
67   package_name() const
68   { return this->package_name_; }
69
70   // The name of the package's init function.
71   const std::string&
72   init_name() const
73   { return this->init_name_; }
74
75   // The priority of the initialization function.  Functions with a
76   // lower priority number must be run first.
77   int
78   priority() const
79   { return this->priority_; }
80
81  private:
82   // The name of the package being imported.
83   std::string package_name_;
84   // The name of the package's init function.
85   std::string init_name_;
86   // The priority.
87   int priority_;
88 };
89
90 // For sorting purposes.
91
92 inline bool
93 operator<(const Import_init& i1, const Import_init& i2)
94 {
95   if (i1.priority() < i2.priority())
96     return true;
97   if (i1.priority() > i2.priority())
98     return false;
99   if (i1.package_name() != i2.package_name())
100     return i1.package_name() < i2.package_name();
101   return i1.init_name() < i2.init_name();
102 }
103
104 // The holder for the internal representation of the entire
105 // compilation unit.
106
107 class Gogo
108 {
109  public:
110   // Create the IR, passing in the sizes of the types "int" and
111   // "uintptr" in bits.
112   Gogo(Backend* backend, int int_type_size, int pointer_size);
113
114   // Get the backend generator.
115   Backend*
116   backend()
117   { return this->backend_; }
118
119   // Get the package name.
120   const std::string&
121   package_name() const;
122
123   // Set the package name.
124   void
125   set_package_name(const std::string&, source_location);
126
127   // Return whether this is the "main" package.
128   bool
129   is_main_package() const;
130
131   // If necessary, adjust the name to use for a hidden symbol.  We add
132   // a prefix of the package name, so that hidden symbols in different
133   // packages do not collide.
134   std::string
135   pack_hidden_name(const std::string& name, bool is_exported) const
136   {
137     return (is_exported
138             ? name
139             : ('.' + this->unique_prefix()
140                + '.' + this->package_name()
141                + '.' + name));
142   }
143
144   // Unpack a name which may have been hidden.  Returns the
145   // user-visible name of the object.
146   static std::string
147   unpack_hidden_name(const std::string& name)
148   { return name[0] != '.' ? name : name.substr(name.rfind('.') + 1); }
149
150   // Return whether a possibly packed name is hidden.
151   static bool
152   is_hidden_name(const std::string& name)
153   { return name[0] == '.'; }
154
155   // Return the package prefix of a hidden name.
156   static std::string
157   hidden_name_prefix(const std::string& name)
158   {
159     go_assert(Gogo::is_hidden_name(name));
160     return name.substr(1, name.rfind('.') - 1);
161   }
162
163   // Given a name which may or may not have been hidden, return the
164   // name to use in an error message.
165   static std::string
166   message_name(const std::string& name);
167
168   // Return whether a name is the blank identifier _.
169   static bool
170   is_sink_name(const std::string& name)
171   {
172     return (name[0] == '.'
173             && name[name.length() - 1] == '_'
174             && name[name.length() - 2] == '.');
175   }
176
177   // Return the unique prefix to use for all exported symbols.
178   const std::string&
179   unique_prefix() const;
180
181   // Set the unique prefix.
182   void
183   set_unique_prefix(const std::string&);
184
185   // Return the priority to use for the package we are compiling.
186   // This is two more than the largest priority of any package we
187   // import.
188   int
189   package_priority() const;
190
191   // Import a package.  FILENAME is the file name argument, LOCAL_NAME
192   // is the local name to give to the package.  If LOCAL_NAME is empty
193   // the declarations are added to the global scope.
194   void
195   import_package(const std::string& filename, const std::string& local_name,
196                  bool is_local_name_exported, source_location);
197
198   // Whether we are the global binding level.
199   bool
200   in_global_scope() const;
201
202   // Look up a name in the current binding contours.
203   Named_object*
204   lookup(const std::string&, Named_object** pfunction) const;
205
206   // Look up a name in the current block.
207   Named_object*
208   lookup_in_block(const std::string&) const;
209
210   // Look up a name in the global namespace--the universal scope.
211   Named_object*
212   lookup_global(const char*) const;
213
214   // Add a new imported package.  REAL_NAME is the real name of the
215   // package.  ALIAS is the alias of the package; this may be the same
216   // as REAL_NAME.  This sets *PADD_TO_GLOBALS if symbols added to
217   // this package should be added to the global namespace; this is
218   // true if the alias is ".".  LOCATION is the location of the import
219   // statement.  This returns the new package, or NULL on error.
220   Package*
221   add_imported_package(const std::string& real_name, const std::string& alias,
222                        bool is_alias_exported,
223                        const std::string& unique_prefix,
224                        source_location location,
225                        bool* padd_to_globals);
226
227   // Register a package.  This package may or may not be imported.
228   // This returns the Package structure for the package, creating if
229   // it necessary.
230   Package*
231   register_package(const std::string& name, const std::string& unique_prefix,
232                    source_location);
233
234   // Start compiling a function.  ADD_METHOD_TO_TYPE is true if a
235   // method function should be added to the type of its receiver.
236   Named_object*
237   start_function(const std::string& name, Function_type* type,
238                  bool add_method_to_type, source_location);
239
240   // Finish compiling a function.
241   void
242   finish_function(source_location);
243
244   // Return the current function.
245   Named_object*
246   current_function() const;
247
248   // Start a new block.  This is not initially associated with a
249   // function.
250   void
251   start_block(source_location);
252
253   // Finish the current block and return it.
254   Block*
255   finish_block(source_location);
256
257   // Declare an unknown name.  This is used while parsing.  The name
258   // must be resolved by the end of the parse.  Unknown names are
259   // always added at the package level.
260   Named_object*
261   add_unknown_name(const std::string& name, source_location);
262
263   // Declare a function.
264   Named_object*
265   declare_function(const std::string&, Function_type*, source_location);
266
267   // Add a label.
268   Label*
269   add_label_definition(const std::string&, source_location);
270
271   // Add a label reference.
272   Label*
273   add_label_reference(const std::string&);
274
275   // Add a statement to the current block.
276   void
277   add_statement(Statement*);
278
279   // Add a block to the current block.
280   void
281   add_block(Block*, source_location);
282
283   // Add a constant.
284   Named_object*
285   add_constant(const Typed_identifier&, Expression*, int iota_value);
286
287   // Add a type.
288   void
289   add_type(const std::string&, Type*, source_location);
290
291   // Add a named type.  This is used for builtin types, and to add an
292   // imported type to the global scope.
293   void
294   add_named_type(Named_type*);
295
296   // Declare a type.
297   Named_object*
298   declare_type(const std::string&, source_location);
299
300   // Declare a type at the package level.  This is used when the
301   // parser sees an unknown name where a type name is required.
302   Named_object*
303   declare_package_type(const std::string&, source_location);
304
305   // Define a type which was already declared.
306   void
307   define_type(Named_object*, Named_type*);
308
309   // Add a variable.
310   Named_object*
311   add_variable(const std::string&, Variable*);
312
313   // Add a sink--a reference to the blank identifier _.
314   Named_object*
315   add_sink();
316
317   // Add a named object to the current namespace.  This is used for
318   // import . "package".
319   void
320   add_named_object(Named_object*);
321
322   // Return a name to use for a thunk function.  A thunk function is
323   // one we create during the compilation, for a go statement or a
324   // defer statement or a method expression.
325   static std::string
326   thunk_name();
327
328   // Return whether an object is a thunk.
329   static bool
330   is_thunk(const Named_object*);
331
332   // Note that we've seen an interface type.  This is used to build
333   // all required interface method tables.
334   void
335   record_interface_type(Interface_type*);
336
337   // Note that we need an initialization function.
338   void
339   set_need_init_fn()
340   { this->need_init_fn_ = true; }
341
342   // Clear out all names in file scope.  This is called when we start
343   // parsing a new file.
344   void
345   clear_file_scope();
346
347   // Traverse the tree.  See the Traverse class.
348   void
349   traverse(Traverse*);
350
351   // Define the predeclared global names.
352   void
353   define_global_names();
354
355   // Verify and complete all types.
356   void
357   verify_types();
358
359   // Lower the parse tree.
360   void
361   lower_parse_tree();
362
363   // Lower all the statements in a block.
364   void
365   lower_block(Named_object* function, Block*);
366
367   // Lower an expression.
368   void
369   lower_expression(Named_object* function, Expression**);
370
371   // Lower a constant.
372   void
373   lower_constant(Named_object*);
374
375   // Finalize the method lists and build stub methods for named types.
376   void
377   finalize_methods();
378
379   // Work out the types to use for unspecified variables and
380   // constants.
381   void
382   determine_types();
383
384   // Type check the program.
385   void
386   check_types();
387
388   // Check the types in a single block.  This is used for complicated
389   // go statements.
390   void
391   check_types_in_block(Block*);
392
393   // Check for return statements.
394   void
395   check_return_statements();
396
397   // Do all exports.
398   void
399   do_exports();
400
401   // Add an import control function for an imported package to the
402   // list.
403   void
404   add_import_init_fn(const std::string& package_name,
405                      const std::string& init_name, int prio);
406
407   // Turn short-cut operators (&&, ||) into explicit if statements.
408   void
409   remove_shortcuts();
410
411   // Use temporary variables to force order of evaluation.
412   void
413   order_evaluations();
414
415   // Build thunks for functions which call recover.
416   void
417   build_recover_thunks();
418
419   // Simplify statements which might use thunks: go and defer
420   // statements.
421   void
422   simplify_thunk_statements();
423
424   // Convert named types to the backend representation.
425   void
426   convert_named_types();
427
428   // Convert named types in a list of bindings.
429   void
430   convert_named_types_in_bindings(Bindings*);
431
432   // True if named types have been converted to the backend
433   // representation.
434   bool
435   named_types_are_converted() const
436   { return this->named_types_are_converted_; }
437
438   // Write out the global values.
439   void
440   write_globals();
441
442   // Create trees for implicit builtin functions.
443   void
444   define_builtin_function_trees();
445
446   // Build a call to a builtin function.  PDECL should point to a NULL
447   // initialized static pointer which will hold the fndecl.  NAME is
448   // the name of the function.  NARGS is the number of arguments.
449   // RETTYPE is the return type.  It is followed by NARGS pairs of
450   // type and argument (both trees).
451   static tree
452   call_builtin(tree* pdecl, source_location, const char* name, int nargs,
453                tree rettype, ...);
454
455   // Build a call to the runtime error function.
456   static tree
457   runtime_error(int code, source_location);
458
459   // Build a builtin struct with a list of fields.
460   static tree
461   builtin_struct(tree* ptype, const char* struct_name, tree struct_type,
462                  int nfields, ...);
463
464   // Mark a function declaration as a builtin library function.
465   static void
466   mark_fndecl_as_builtin_library(tree fndecl);
467
468   // Build a constructor for a slice.  SLICE_TYPE_TREE is the type of
469   // the slice.  VALUES points to the values.  COUNT is the size,
470   // CAPACITY is the capacity.  If CAPACITY is NULL, it is set to
471   // COUNT.
472   static tree
473   slice_constructor(tree slice_type_tree, tree values, tree count,
474                     tree capacity);
475
476   // Build a map descriptor.
477   tree
478   map_descriptor(Map_type*);
479
480   // Return a tree for the type of a map descriptor.  This is struct
481   // __go_map_descriptor in libgo/runtime/map.h.  This is the same for
482   // all map types.
483   tree
484   map_descriptor_type();
485
486   // Build required interface method tables.
487   void
488   build_interface_method_tables();
489
490   // Build an interface method table for a type: a list of function
491   // pointers, one for each interface method.  This returns a decl.
492   tree
493   interface_method_table_for_type(const Interface_type*, Named_type*,
494                                   bool is_pointer);
495
496   // Return a tree which allocate SIZE bytes to hold values of type
497   // TYPE.
498   tree
499   allocate_memory(Type *type, tree size, source_location);
500
501   // Return a type to use for pointer to const char.
502   static tree
503   const_char_pointer_type_tree();
504
505   // Build a string constant with the right type.
506   static tree
507   string_constant_tree(const std::string&);
508
509   // Build a Go string constant.  This returns a pointer to the
510   // constant.
511   tree
512   go_string_constant_tree(const std::string&);
513
514   // Receive a value from a channel.
515   static tree
516   receive_from_channel(tree type_tree, tree channel, bool for_select,
517                        source_location);
518
519   // Return a tree for receiving an integer on a channel.
520   static tree
521   receive_as_64bit_integer(tree type, tree channel, bool blocking,
522                            bool for_select);
523
524
525   // Make a trampoline which calls FNADDR passing CLOSURE.
526   tree
527   make_trampoline(tree fnaddr, tree closure, source_location);
528
529  private:
530   // During parsing, we keep a stack of functions.  Each function on
531   // the stack is one that we are currently parsing.  For each
532   // function, we keep track of the current stack of blocks.
533   struct Open_function
534   {
535     // The function.
536     Named_object* function;
537     // The stack of active blocks in the function.
538     std::vector<Block*> blocks;
539   };
540
541   // The stack of functions.
542   typedef std::vector<Open_function> Open_functions;
543
544   // Set up the built-in unsafe package.
545   void
546   import_unsafe(const std::string&, bool is_exported, source_location);
547
548   // Add a new imported package.
549   Named_object*
550   add_package(const std::string& real_name, const std::string& alias,
551               const std::string& unique_prefix, source_location location);
552
553   // Return the current binding contour.
554   Bindings*
555   current_bindings();
556
557   const Bindings*
558   current_bindings() const;
559
560   // Return the current block.
561   Block*
562   current_block();
563
564   // Get the name of the magic initialization function.
565   const std::string&
566   get_init_fn_name();
567
568   // Get the decl for the magic initialization function.
569   tree
570   initialization_function_decl();
571
572   // Write the magic initialization function.
573   void
574   write_initialization_function(tree fndecl, tree init_stmt_list);
575
576   // Initialize imported packages.
577   void
578   init_imports(tree*);
579
580   // Register variables with the garbage collector.
581   void
582   register_gc_vars(const std::vector<Named_object*>&, tree*);
583
584   // Build a pointer to a Go string constant.  This returns a pointer
585   // to the pointer.
586   tree
587   ptr_go_string_constant_tree(const std::string&);
588
589   // Return the type of a trampoline.
590   static tree
591   trampoline_type_tree();
592
593   // Type used to map import names to packages.
594   typedef std::map<std::string, Package*> Imports;
595
596   // Type used to map package names to packages.
597   typedef std::map<std::string, Package*> Packages;
598
599   // Type used to map special names in the sys package.
600   typedef std::map<std::string, std::string> Sys_names;
601
602   // Hash table mapping map types to map descriptor decls.
603   typedef Unordered_map_hash(const Map_type*, tree, Type_hash_identical,
604                              Type_identical) Map_descriptors;
605
606   // The backend generator.
607   Backend* backend_;
608   // The package we are compiling.
609   Package* package_;
610   // The list of currently open functions during parsing.
611   Open_functions functions_;
612   // The global binding contour.  This includes the builtin functions
613   // and the package we are compiling.
614   Bindings* globals_;
615   // Mapping from import file names to packages.
616   Imports imports_;
617   // Whether the magic unsafe package was imported.
618   bool imported_unsafe_;
619   // Mapping from package names we have seen to packages.  This does
620   // not include the package we are compiling.
621   Packages packages_;
622   // Mapping from map types to map descriptors.
623   Map_descriptors* map_descriptors_;
624   // The functions named "init", if there are any.
625   std::vector<Named_object*> init_functions_;
626   // Whether we need a magic initialization function.
627   bool need_init_fn_;
628   // The name of the magic initialization function.
629   std::string init_fn_name_;
630   // A list of import control variables for packages that we import.
631   std::set<Import_init> imported_init_fns_;
632   // The unique prefix used for all global symbols.
633   std::string unique_prefix_;
634   // Whether an explicit unique prefix was set by -fgo-prefix.
635   bool unique_prefix_specified_;
636   // A list of interface types defined while parsing.
637   std::vector<Interface_type*> interface_types_;
638   // Whether named types have been converted.
639   bool named_types_are_converted_;
640 };
641
642 // A block of statements.
643
644 class Block
645 {
646  public:
647   Block(Block* enclosing, source_location);
648
649   // Return the enclosing block.
650   const Block*
651   enclosing() const
652   { return this->enclosing_; }
653
654   // Return the bindings of the block.
655   Bindings*
656   bindings()
657   { return this->bindings_; }
658
659   const Bindings*
660   bindings() const
661   { return this->bindings_; }
662
663   // Look at the block's statements.
664   const std::vector<Statement*>*
665   statements() const
666   { return &this->statements_; }
667
668   // Return the start location.  This is normally the location of the
669   // left curly brace which starts the block.
670   source_location
671   start_location() const
672   { return this->start_location_; }
673
674   // Return the end location.  This is normally the location of the
675   // right curly brace which ends the block.
676   source_location
677   end_location() const
678   { return this->end_location_; }
679
680   // Add a statement to the block.
681   void
682   add_statement(Statement*);
683
684   // Add a statement to the front of the block.
685   void
686   add_statement_at_front(Statement*);
687
688   // Replace a statement in a block.
689   void
690   replace_statement(size_t index, Statement*);
691
692   // Add a Statement before statement number INDEX.
693   void
694   insert_statement_before(size_t index, Statement*);
695
696   // Add a Statement after statement number INDEX.
697   void
698   insert_statement_after(size_t index, Statement*);
699
700   // Set the end location of the block.
701   void
702   set_end_location(source_location location)
703   { this->end_location_ = location; }
704
705   // Traverse the tree.
706   int
707   traverse(Traverse*);
708
709   // Set final types for unspecified variables and constants.
710   void
711   determine_types();
712
713   // Return true if execution of this block may fall through to the
714   // next block.
715   bool
716   may_fall_through() const;
717
718   // Convert the block to the backend representation.
719   Bblock*
720   get_backend(Translate_context*);
721
722   // Iterate over statements.
723
724   typedef std::vector<Statement*>::iterator iterator;
725
726   iterator
727   begin()
728   { return this->statements_.begin(); }
729
730   iterator
731   end()
732   { return this->statements_.end(); }
733
734  private:
735   // Enclosing block.
736   Block* enclosing_;
737   // Statements in the block.
738   std::vector<Statement*> statements_;
739   // Binding contour.
740   Bindings* bindings_;
741   // Location of start of block.
742   source_location start_location_;
743   // Location of end of block.
744   source_location end_location_;
745 };
746
747 // A function.
748
749 class Function
750 {
751  public:
752   Function(Function_type* type, Function*, Block*, source_location);
753
754   // Return the function's type.
755   Function_type*
756   type() const
757   { return this->type_; }
758
759   // Return the enclosing function if there is one.
760   Function*
761   enclosing()
762   { return this->enclosing_; }
763
764   // Set the enclosing function.  This is used when building thunks
765   // for functions which call recover.
766   void
767   set_enclosing(Function* enclosing)
768   {
769     go_assert(this->enclosing_ == NULL);
770     this->enclosing_ = enclosing;
771   }
772
773   // The result variables.
774   typedef std::vector<Named_object*> Results;
775
776   // Create the result variables in the outer block.
777   void
778   create_result_variables(Gogo*);
779
780   // Update the named result variables when cloning a function which
781   // calls recover.
782   void
783   update_result_variables();
784
785   // Return the result variables.
786   Results*
787   result_variables()
788   { return this->results_; }
789
790   // Whether the result variables have names.
791   bool
792   results_are_named() const
793   { return this->results_are_named_; }
794
795   // Add a new field to the closure variable.
796   void
797   add_closure_field(Named_object* var, source_location loc)
798   { this->closure_fields_.push_back(std::make_pair(var, loc)); }
799
800   // Whether this function needs a closure.
801   bool
802   needs_closure() const
803   { return !this->closure_fields_.empty(); }
804
805   // Return the closure variable, creating it if necessary.  This is
806   // passed to the function as a static chain parameter.
807   Named_object*
808   closure_var();
809
810   // Set the closure variable.  This is used when building thunks for
811   // functions which call recover.
812   void
813   set_closure_var(Named_object* v)
814   {
815     go_assert(this->closure_var_ == NULL);
816     this->closure_var_ = v;
817   }
818
819   // Return the variable for a reference to field INDEX in the closure
820   // variable.
821   Named_object*
822   enclosing_var(unsigned int index)
823   {
824     go_assert(index < this->closure_fields_.size());
825     return closure_fields_[index].first;
826   }
827
828   // Set the type of the closure variable if there is one.
829   void
830   set_closure_type();
831
832   // Get the block of statements associated with the function.
833   Block*
834   block() const
835   { return this->block_; }
836
837   // Get the location of the start of the function.
838   source_location
839   location() const
840   { return this->location_; }
841
842   // Return whether this function is actually a method.
843   bool
844   is_method() const;
845
846   // Add a label definition to the function.
847   Label*
848   add_label_definition(const std::string& label_name, source_location);
849
850   // Add a label reference to a function.
851   Label*
852   add_label_reference(const std::string& label_name);
853
854   // Warn about labels that are defined but not used.
855   void
856   check_labels() const;
857
858   // Whether this function calls the predeclared recover function.
859   bool
860   calls_recover() const
861   { return this->calls_recover_; }
862
863   // Record that this function calls the predeclared recover function.
864   // This is set during the lowering pass.
865   void
866   set_calls_recover()
867   { this->calls_recover_ = true; }
868
869   // Whether this is a recover thunk function.
870   bool
871   is_recover_thunk() const
872   { return this->is_recover_thunk_; }
873
874   // Record that this is a thunk built for a function which calls
875   // recover.
876   void
877   set_is_recover_thunk()
878   { this->is_recover_thunk_ = true; }
879
880   // Whether this function already has a recover thunk.
881   bool
882   has_recover_thunk() const
883   { return this->has_recover_thunk_; }
884
885   // Record that this function already has a recover thunk.
886   void
887   set_has_recover_thunk()
888   { this->has_recover_thunk_ = true; }
889
890   // Swap with another function.  Used only for the thunk which calls
891   // recover.
892   void
893   swap_for_recover(Function *);
894
895   // Traverse the tree.
896   int
897   traverse(Traverse*);
898
899   // Determine types in the function.
900   void
901   determine_types();
902
903   // Return the function's decl given an identifier.
904   tree
905   get_or_make_decl(Gogo*, Named_object*, tree id);
906
907   // Return the function's decl after it has been built.
908   tree
909   get_decl() const
910   {
911     go_assert(this->fndecl_ != NULL);
912     return this->fndecl_;
913   }
914
915   // Set the function decl to hold a tree of the function code.
916   void
917   build_tree(Gogo*, Named_object*);
918
919   // Get the value to return when not explicitly specified.  May also
920   // add statements to execute first to STMT_LIST.
921   tree
922   return_value(Gogo*, Named_object*, source_location, tree* stmt_list) const;
923
924   // Get a tree for the variable holding the defer stack.
925   Expression*
926   defer_stack(source_location);
927
928   // Export the function.
929   void
930   export_func(Export*, const std::string& name) const;
931
932   // Export a function with a type.
933   static void
934   export_func_with_type(Export*, const std::string& name,
935                         const Function_type*);
936
937   // Import a function.
938   static void
939   import_func(Import*, std::string* pname, Typed_identifier** receiver,
940               Typed_identifier_list** pparameters,
941               Typed_identifier_list** presults, bool* is_varargs);
942
943  private:
944   // Type for mapping from label names to Label objects.
945   typedef Unordered_map(std::string, Label*) Labels;
946
947   tree
948   make_receiver_parm_decl(Gogo*, Named_object*, tree);
949
950   tree
951   copy_parm_to_heap(Gogo*, Named_object*, tree);
952
953   void
954   build_defer_wrapper(Gogo*, Named_object*, tree*, tree*);
955
956   typedef std::vector<std::pair<Named_object*,
957                                 source_location> > Closure_fields;
958
959   // The function's type.
960   Function_type* type_;
961   // The enclosing function.  This is NULL when there isn't one, which
962   // is the normal case.
963   Function* enclosing_;
964   // The result variables, if any.
965   Results* results_;
966   // If there is a closure, this is the list of variables which appear
967   // in the closure.  This is created by the parser, and then resolved
968   // to a real type when we lower parse trees.
969   Closure_fields closure_fields_;
970   // The closure variable, passed as a parameter using the static
971   // chain parameter.  Normally NULL.
972   Named_object* closure_var_;
973   // The outer block of statements in the function.
974   Block* block_;
975   // The source location of the start of the function.
976   source_location location_;
977   // Labels defined or referenced in the function.
978   Labels labels_;
979   // The function decl.
980   tree fndecl_;
981   // The defer stack variable.  A pointer to this variable is used to
982   // distinguish the defer stack for one function from another.  This
983   // is NULL unless we actually need a defer stack.
984   Temporary_statement* defer_stack_;
985   // True if the result variables are named.
986   bool results_are_named_;
987   // True if this function calls the predeclared recover function.
988   bool calls_recover_;
989   // True if this a thunk built for a function which calls recover.
990   bool is_recover_thunk_;
991   // True if this function already has a recover thunk.
992   bool has_recover_thunk_;
993 };
994
995 // A function declaration.
996
997 class Function_declaration
998 {
999  public:
1000   Function_declaration(Function_type* fntype, source_location location)
1001     : fntype_(fntype), location_(location), asm_name_(), fndecl_(NULL)
1002   { }
1003
1004   Function_type*
1005   type() const
1006   { return this->fntype_; }
1007
1008   source_location
1009   location() const
1010   { return this->location_; }
1011
1012   const std::string&
1013   asm_name() const
1014   { return this->asm_name_; }
1015
1016   // Set the assembler name.
1017   void
1018   set_asm_name(const std::string& asm_name)
1019   { this->asm_name_ = asm_name; }
1020
1021   // Return a decl for the function given an identifier.
1022   tree
1023   get_or_make_decl(Gogo*, Named_object*, tree id);
1024
1025   // Export a function declaration.
1026   void
1027   export_func(Export* exp, const std::string& name) const
1028   { Function::export_func_with_type(exp, name, this->fntype_); }
1029
1030  private:
1031   // The type of the function.
1032   Function_type* fntype_;
1033   // The location of the declaration.
1034   source_location location_;
1035   // The assembler name: this is the name to use in references to the
1036   // function.  This is normally empty.
1037   std::string asm_name_;
1038   // The function decl if needed.
1039   tree fndecl_;
1040 };
1041
1042 // A variable.
1043
1044 class Variable
1045 {
1046  public:
1047   Variable(Type*, Expression*, bool is_global, bool is_parameter,
1048            bool is_receiver, source_location);
1049
1050   // Get the type of the variable.
1051   Type*
1052   type();
1053
1054   Type*
1055   type() const;
1056
1057   // Return whether the type is defined yet.
1058   bool
1059   has_type() const
1060   { return this->type_ != NULL; }
1061
1062   // Get the initial value.
1063   Expression*
1064   init() const
1065   { return this->init_; }
1066
1067   // Return whether there are any preinit statements.
1068   bool
1069   has_pre_init() const
1070   { return this->preinit_ != NULL; }
1071
1072   // Return the preinit statements if any.
1073   Block*
1074   preinit() const
1075   { return this->preinit_; }
1076
1077   // Return whether this is a global variable.
1078   bool
1079   is_global() const
1080   { return this->is_global_; }
1081
1082   // Return whether this is a function parameter.
1083   bool
1084   is_parameter() const
1085   { return this->is_parameter_; }
1086
1087   // Return whether this is the receiver parameter of a method.
1088   bool
1089   is_receiver() const
1090   { return this->is_receiver_; }
1091
1092   // Change this parameter to be a receiver.  This is used when
1093   // creating the thunks created for functions which call recover.
1094   void
1095   set_is_receiver()
1096   {
1097     go_assert(this->is_parameter_);
1098     this->is_receiver_ = true;
1099   }
1100
1101   // Change this parameter to not be a receiver.  This is used when
1102   // creating the thunks created for functions which call recover.
1103   void
1104   set_is_not_receiver()
1105   {
1106     go_assert(this->is_parameter_);
1107     this->is_receiver_ = false;
1108   }
1109
1110   // Return whether this is the varargs parameter of a function.
1111   bool
1112   is_varargs_parameter() const
1113   { return this->is_varargs_parameter_; }
1114
1115   // Whether this variable's address is taken.
1116   bool
1117   is_address_taken() const
1118   { return this->is_address_taken_; }
1119
1120   // Whether this variable should live in the heap.
1121   bool
1122   is_in_heap() const
1123   { return this->is_address_taken_ && !this->is_global_; }
1124
1125   // Note that something takes the address of this variable.
1126   void
1127   set_address_taken()
1128   { this->is_address_taken_ = true; }
1129
1130   // Return whether the address is taken but does not escape.
1131   bool
1132   is_non_escaping_address_taken() const
1133   { return this->is_non_escaping_address_taken_; }
1134
1135   // Note that something takes the address of this variable such that
1136   // the address does not escape the function.
1137   void
1138   set_non_escaping_address_taken()
1139   { this->is_non_escaping_address_taken_ = true; }
1140
1141   // Get the source location of the variable's declaration.
1142   source_location
1143   location() const
1144   { return this->location_; }
1145
1146   // Record that this is the varargs parameter of a function.
1147   void
1148   set_is_varargs_parameter()
1149   {
1150     go_assert(this->is_parameter_);
1151     this->is_varargs_parameter_ = true;
1152   }
1153
1154   // Clear the initial value; used for error handling.
1155   void
1156   clear_init()
1157   { this->init_ = NULL; }
1158
1159   // Set the initial value; used for converting shortcuts.
1160   void
1161   set_init(Expression* init)
1162   { this->init_ = init; }
1163
1164   // Get the preinit block, a block of statements to be run before the
1165   // initialization expression.
1166   Block*
1167   preinit_block(Gogo*);
1168
1169   // Add a statement to be run before the initialization expression.
1170   // This is only used for global variables.
1171   void
1172   add_preinit_statement(Gogo*, Statement*);
1173
1174   // Lower the initialization expression after parsing is complete.
1175   void
1176   lower_init_expression(Gogo*, Named_object*);
1177
1178   // A special case: the init value is used only to determine the
1179   // type.  This is used if the variable is defined using := with the
1180   // comma-ok form of a map index or a receive expression.  The init
1181   // value is actually the map index expression or receive expression.
1182   // We use this because we may not know the right type at parse time.
1183   void
1184   set_type_from_init_tuple()
1185   { this->type_from_init_tuple_ = true; }
1186
1187   // Another special case: the init value is used only to determine
1188   // the type.  This is used if the variable is defined using := with
1189   // a range clause.  The init value is the range expression.  The
1190   // type of the variable is the index type of the range expression
1191   // (i.e., the first value returned by a range).
1192   void
1193   set_type_from_range_index()
1194   { this->type_from_range_index_ = true; }
1195
1196   // Another special case: like set_type_from_range_index, but the
1197   // type is the value type of the range expression (i.e., the second
1198   // value returned by a range).
1199   void
1200   set_type_from_range_value()
1201   { this->type_from_range_value_ = true; }
1202
1203   // Another special case: the init value is used only to determine
1204   // the type.  This is used if the variable is defined using := with
1205   // a case in a select statement.  The init value is the channel.
1206   // The type of the variable is the channel's element type.
1207   void
1208   set_type_from_chan_element()
1209   { this->type_from_chan_element_ = true; }
1210
1211   // After we lower the select statement, we once again set the type
1212   // from the initialization expression.
1213   void
1214   clear_type_from_chan_element()
1215   {
1216     go_assert(this->type_from_chan_element_);
1217     this->type_from_chan_element_ = false;
1218   }
1219
1220   // Note that this variable was created for a type switch clause.
1221   void
1222   set_is_type_switch_var()
1223   { this->is_type_switch_var_ = true; }
1224
1225   // Traverse the initializer expression.
1226   int
1227   traverse_expression(Traverse*);
1228
1229   // Determine the type of the variable if necessary.
1230   void
1231   determine_type();
1232
1233   // Get the backend representation of the variable.
1234   Bvariable*
1235   get_backend_variable(Gogo*, Named_object*, const Package*,
1236                        const std::string&);
1237
1238   // Get the initial value of the variable as a tree.  This may only
1239   // be called if has_pre_init() returns false.
1240   tree
1241   get_init_tree(Gogo*, Named_object* function);
1242
1243   // Return a series of statements which sets the value of the
1244   // variable in DECL.  This should only be called is has_pre_init()
1245   // returns true.  DECL may be NULL for a sink variable.
1246   tree
1247   get_init_block(Gogo*, Named_object* function, tree decl);
1248
1249   // Export the variable.
1250   void
1251   export_var(Export*, const std::string& name) const;
1252
1253   // Import a variable.
1254   static void
1255   import_var(Import*, std::string* pname, Type** ptype);
1256
1257  private:
1258   // The type of a tuple.
1259   Type*
1260   type_from_tuple(Expression*, bool) const;
1261
1262   // The type of a range.
1263   Type*
1264   type_from_range(Expression*, bool, bool) const;
1265
1266   // The element type of a channel.
1267   Type*
1268   type_from_chan_element(Expression*, bool) const;
1269
1270   // The variable's type.  This may be NULL if the type is set from
1271   // the expression.
1272   Type* type_;
1273   // The initial value.  This may be NULL if the variable should be
1274   // initialized to the default value for the type.
1275   Expression* init_;
1276   // Statements to run before the init statement.
1277   Block* preinit_;
1278   // Location of variable definition.
1279   source_location location_;
1280   // Backend representation.
1281   Bvariable* backend_;
1282   // Whether this is a global variable.
1283   bool is_global_ : 1;
1284   // Whether this is a function parameter.
1285   bool is_parameter_ : 1;
1286   // Whether this is the receiver parameter of a method.
1287   bool is_receiver_ : 1;
1288   // Whether this is the varargs parameter of a function.
1289   bool is_varargs_parameter_ : 1;
1290   // Whether something takes the address of this variable.  For a
1291   // local variable this implies that the variable has to be on the
1292   // heap.
1293   bool is_address_taken_ : 1;
1294   // Whether something takes the address of this variable such that
1295   // the address does not escape the function.
1296   bool is_non_escaping_address_taken_ : 1;
1297   // True if we have seen this variable in a traversal.
1298   bool seen_ : 1;
1299   // True if we have lowered the initialization expression.
1300   bool init_is_lowered_ : 1;
1301   // True if init is a tuple used to set the type.
1302   bool type_from_init_tuple_ : 1;
1303   // True if init is a range clause and the type is the index type.
1304   bool type_from_range_index_ : 1;
1305   // True if init is a range clause and the type is the value type.
1306   bool type_from_range_value_ : 1;
1307   // True if init is a channel and the type is the channel's element type.
1308   bool type_from_chan_element_ : 1;
1309   // True if this is a variable created for a type switch case.
1310   bool is_type_switch_var_ : 1;
1311   // True if we have determined types.
1312   bool determined_type_ : 1;
1313 };
1314
1315 // A variable which is really the name for a function return value, or
1316 // part of one.
1317
1318 class Result_variable
1319 {
1320  public:
1321   Result_variable(Type* type, Function* function, int index,
1322                   source_location location)
1323     : type_(type), function_(function), index_(index), location_(location),
1324       backend_(NULL), is_address_taken_(false),
1325       is_non_escaping_address_taken_(false)
1326   { }
1327
1328   // Get the type of the result variable.
1329   Type*
1330   type() const
1331   { return this->type_; }
1332
1333   // Get the function that this is associated with.
1334   Function*
1335   function() const
1336   { return this->function_; }
1337
1338   // Index in the list of function results.
1339   int
1340   index() const
1341   { return this->index_; }
1342
1343   // The location of the variable definition.
1344   source_location
1345   location() const
1346   { return this->location_; }
1347
1348   // Whether this variable's address is taken.
1349   bool
1350   is_address_taken() const
1351   { return this->is_address_taken_; }
1352
1353   // Note that something takes the address of this variable.
1354   void
1355   set_address_taken()
1356   { this->is_address_taken_ = true; }
1357
1358   // Return whether the address is taken but does not escape.
1359   bool
1360   is_non_escaping_address_taken() const
1361   { return this->is_non_escaping_address_taken_; }
1362
1363   // Note that something takes the address of this variable such that
1364   // the address does not escape the function.
1365   void
1366   set_non_escaping_address_taken()
1367   { this->is_non_escaping_address_taken_ = true; }
1368
1369   // Whether this variable should live in the heap.
1370   bool
1371   is_in_heap() const
1372   { return this->is_address_taken_; }
1373
1374   // Set the function.  This is used when cloning functions which call
1375   // recover.
1376   void
1377   set_function(Function* function)
1378   { this->function_ = function; }
1379
1380   // Get the backend representation of the variable.
1381   Bvariable*
1382   get_backend_variable(Gogo*, Named_object*, const std::string&);
1383
1384  private:
1385   // Type of result variable.
1386   Type* type_;
1387   // Function with which this is associated.
1388   Function* function_;
1389   // Index in list of results.
1390   int index_;
1391   // Where the result variable is defined.
1392   source_location location_;
1393   // Backend representation.
1394   Bvariable* backend_;
1395   // Whether something takes the address of this variable.
1396   bool is_address_taken_;
1397   // Whether something takes the address of this variable such that
1398   // the address does not escape the function.
1399   bool is_non_escaping_address_taken_;
1400 };
1401
1402 // The value we keep for a named constant.  This lets us hold a type
1403 // and an expression.
1404
1405 class Named_constant
1406 {
1407  public:
1408   Named_constant(Type* type, Expression* expr, int iota_value,
1409                  source_location location)
1410     : type_(type), expr_(expr), iota_value_(iota_value), location_(location),
1411       lowering_(false)
1412   { }
1413
1414   Type*
1415   type() const
1416   { return this->type_; }
1417
1418   Expression*
1419   expr() const
1420   { return this->expr_; }
1421
1422   int
1423   iota_value() const
1424   { return this->iota_value_; }
1425
1426   source_location
1427   location() const
1428   { return this->location_; }
1429
1430   // Whether we are lowering.
1431   bool
1432   lowering() const
1433   { return this->lowering_; }
1434
1435   // Set that we are lowering.
1436   void
1437   set_lowering()
1438   { this->lowering_ = true; }
1439
1440   // We are no longer lowering.
1441   void
1442   clear_lowering()
1443   { this->lowering_ = false; }
1444
1445   // Traverse the expression.
1446   int
1447   traverse_expression(Traverse*);
1448
1449   // Determine the type of the constant if necessary.
1450   void
1451   determine_type();
1452
1453   // Indicate that we found and reported an error for this constant.
1454   void
1455   set_error();
1456
1457   // Export the constant.
1458   void
1459   export_const(Export*, const std::string& name) const;
1460
1461   // Import a constant.
1462   static void
1463   import_const(Import*, std::string*, Type**, Expression**);
1464
1465  private:
1466   // The type of the constant.
1467   Type* type_;
1468   // The expression for the constant.
1469   Expression* expr_;
1470   // If the predeclared constant iota is used in EXPR_, this is the
1471   // value it will have.  We do this because at parse time we don't
1472   // know whether the name "iota" will refer to the predeclared
1473   // constant or to something else.  We put in the right value in when
1474   // we lower.
1475   int iota_value_;
1476   // The location of the definition.
1477   source_location location_;
1478   // Whether we are currently lowering this constant.
1479   bool lowering_;
1480 };
1481
1482 // A type declaration.
1483
1484 class Type_declaration
1485 {
1486  public:
1487   Type_declaration(source_location location)
1488     : location_(location), in_function_(NULL), methods_(),
1489       issued_warning_(false)
1490   { }
1491
1492   // Return the location.
1493   source_location
1494   location() const
1495   { return this->location_; }
1496
1497   // Return the function in which this type is declared.  This will
1498   // return NULL for a type declared in global scope.
1499   Named_object*
1500   in_function()
1501   { return this->in_function_; }
1502
1503   // Set the function in which this type is declared.
1504   void
1505   set_in_function(Named_object* f)
1506   { this->in_function_ = f; }
1507
1508   // Add a method to this type.  This is used when methods are defined
1509   // before the type.
1510   Named_object*
1511   add_method(const std::string& name, Function* function);
1512
1513   // Add a method declaration to this type.
1514   Named_object*
1515   add_method_declaration(const std::string& name, Function_type* type,
1516                          source_location location);
1517
1518   // Return whether any methods were defined.
1519   bool
1520   has_methods() const;
1521
1522   // Define methods when the real type is known.
1523   void
1524   define_methods(Named_type*);
1525
1526   // This is called if we are trying to use this type.  It returns
1527   // true if we should issue a warning.
1528   bool
1529   using_type();
1530
1531  private:
1532   typedef std::vector<Named_object*> Methods;
1533
1534   // The location of the type declaration.
1535   source_location location_;
1536   // If this type is declared in a function, a pointer back to the
1537   // function in which it is defined.
1538   Named_object* in_function_;
1539   // Methods defined before the type is defined.
1540   Methods methods_;
1541   // True if we have issued a warning about a use of this type
1542   // declaration when it is undefined.
1543   bool issued_warning_;
1544 };
1545
1546 // An unknown object.  These are created by the parser for forward
1547 // references to names which have not been seen before.  In a correct
1548 // program, these will always point to a real definition by the end of
1549 // the parse.  Because they point to another Named_object, these may
1550 // only be referenced by Unknown_expression objects.
1551
1552 class Unknown_name
1553 {
1554  public:
1555   Unknown_name(source_location location)
1556     : location_(location), real_named_object_(NULL)
1557   { }
1558
1559   // Return the location where this name was first seen.
1560   source_location
1561   location() const
1562   { return this->location_; }
1563
1564   // Return the real named object that this points to, or NULL if it
1565   // was never resolved.
1566   Named_object*
1567   real_named_object() const
1568   { return this->real_named_object_; }
1569
1570   // Set the real named object that this points to.
1571   void
1572   set_real_named_object(Named_object* no);
1573
1574  private:
1575   // The location where this name was first seen.
1576   source_location location_;
1577   // The real named object when it is known.
1578   Named_object*
1579   real_named_object_;
1580 };
1581
1582 // A named object named.  This is the result of a declaration.  We
1583 // don't use a superclass because they all have to be handled
1584 // differently.
1585
1586 class Named_object
1587 {
1588  public:
1589   enum Classification
1590   {
1591     // An uninitialized Named_object.  We should never see this.
1592     NAMED_OBJECT_UNINITIALIZED,
1593     // An unknown name.  This is used for forward references.  In a
1594     // correct program, these will all be resolved by the end of the
1595     // parse.
1596     NAMED_OBJECT_UNKNOWN,
1597     // A const.
1598     NAMED_OBJECT_CONST,
1599     // A type.
1600     NAMED_OBJECT_TYPE,
1601     // A forward type declaration.
1602     NAMED_OBJECT_TYPE_DECLARATION,
1603     // A var.
1604     NAMED_OBJECT_VAR,
1605     // A result variable in a function.
1606     NAMED_OBJECT_RESULT_VAR,
1607     // The blank identifier--the special variable named _.
1608     NAMED_OBJECT_SINK,
1609     // A func.
1610     NAMED_OBJECT_FUNC,
1611     // A forward func declaration.
1612     NAMED_OBJECT_FUNC_DECLARATION,
1613     // A package.
1614     NAMED_OBJECT_PACKAGE
1615   };
1616
1617   // Return the classification.
1618   Classification
1619   classification() const
1620   { return this->classification_; }
1621
1622   // Classifiers.
1623
1624   bool
1625   is_unknown() const
1626   { return this->classification_ == NAMED_OBJECT_UNKNOWN; }
1627
1628   bool
1629   is_const() const
1630   { return this->classification_ == NAMED_OBJECT_CONST; }
1631
1632   bool
1633   is_type() const
1634   { return this->classification_ == NAMED_OBJECT_TYPE; }
1635
1636   bool
1637   is_type_declaration() const
1638   { return this->classification_ == NAMED_OBJECT_TYPE_DECLARATION; }
1639
1640   bool
1641   is_variable() const
1642   { return this->classification_ == NAMED_OBJECT_VAR; }
1643
1644   bool
1645   is_result_variable() const
1646   { return this->classification_ == NAMED_OBJECT_RESULT_VAR; }
1647
1648   bool
1649   is_sink() const
1650   { return this->classification_ == NAMED_OBJECT_SINK; }
1651
1652   bool
1653   is_function() const
1654   { return this->classification_ == NAMED_OBJECT_FUNC; }
1655
1656   bool
1657   is_function_declaration() const
1658   { return this->classification_ == NAMED_OBJECT_FUNC_DECLARATION; }
1659
1660   bool
1661   is_package() const
1662   { return this->classification_ == NAMED_OBJECT_PACKAGE; }
1663
1664   // Creators.
1665
1666   static Named_object*
1667   make_unknown_name(const std::string& name, source_location);
1668
1669   static Named_object*
1670   make_constant(const Typed_identifier&, const Package*, Expression*,
1671                 int iota_value);
1672
1673   static Named_object*
1674   make_type(const std::string&, const Package*, Type*, source_location);
1675
1676   static Named_object*
1677   make_type_declaration(const std::string&, const Package*, source_location);
1678
1679   static Named_object*
1680   make_variable(const std::string&, const Package*, Variable*);
1681
1682   static Named_object*
1683   make_result_variable(const std::string&, Result_variable*);
1684
1685   static Named_object*
1686   make_sink();
1687
1688   static Named_object*
1689   make_function(const std::string&, const Package*, Function*);
1690
1691   static Named_object*
1692   make_function_declaration(const std::string&, const Package*, Function_type*,
1693                             source_location);
1694
1695   static Named_object*
1696   make_package(const std::string& alias, Package* package);
1697
1698   // Getters.
1699
1700   Unknown_name*
1701   unknown_value()
1702   {
1703     go_assert(this->classification_ == NAMED_OBJECT_UNKNOWN);
1704     return this->u_.unknown_value;
1705   }
1706
1707   const Unknown_name*
1708   unknown_value() const
1709   {
1710     go_assert(this->classification_ == NAMED_OBJECT_UNKNOWN);
1711     return this->u_.unknown_value;
1712   }
1713
1714   Named_constant*
1715   const_value()
1716   {
1717     go_assert(this->classification_ == NAMED_OBJECT_CONST);
1718     return this->u_.const_value;
1719   }
1720
1721   const Named_constant*
1722   const_value() const
1723   {
1724     go_assert(this->classification_ == NAMED_OBJECT_CONST);
1725     return this->u_.const_value;
1726   }
1727
1728   Named_type*
1729   type_value()
1730   {
1731     go_assert(this->classification_ == NAMED_OBJECT_TYPE);
1732     return this->u_.type_value;
1733   }
1734
1735   const Named_type*
1736   type_value() const
1737   {
1738     go_assert(this->classification_ == NAMED_OBJECT_TYPE);
1739     return this->u_.type_value;
1740   }
1741
1742   Type_declaration*
1743   type_declaration_value()
1744   {
1745     go_assert(this->classification_ == NAMED_OBJECT_TYPE_DECLARATION);
1746     return this->u_.type_declaration;
1747   }
1748
1749   const Type_declaration*
1750   type_declaration_value() const
1751   {
1752     go_assert(this->classification_ == NAMED_OBJECT_TYPE_DECLARATION);
1753     return this->u_.type_declaration;
1754   }
1755
1756   Variable*
1757   var_value()
1758   {
1759     go_assert(this->classification_ == NAMED_OBJECT_VAR);
1760     return this->u_.var_value;
1761   }
1762
1763   const Variable*
1764   var_value() const
1765   {
1766     go_assert(this->classification_ == NAMED_OBJECT_VAR);
1767     return this->u_.var_value;
1768   }
1769
1770   Result_variable*
1771   result_var_value()
1772   {
1773     go_assert(this->classification_ == NAMED_OBJECT_RESULT_VAR);
1774     return this->u_.result_var_value;
1775   }
1776
1777   const Result_variable*
1778   result_var_value() const
1779   {
1780     go_assert(this->classification_ == NAMED_OBJECT_RESULT_VAR);
1781     return this->u_.result_var_value;
1782   }
1783
1784   Function*
1785   func_value()
1786   {
1787     go_assert(this->classification_ == NAMED_OBJECT_FUNC);
1788     return this->u_.func_value;
1789   }
1790
1791   const Function*
1792   func_value() const
1793   {
1794     go_assert(this->classification_ == NAMED_OBJECT_FUNC);
1795     return this->u_.func_value;
1796   }
1797
1798   Function_declaration*
1799   func_declaration_value()
1800   {
1801     go_assert(this->classification_ == NAMED_OBJECT_FUNC_DECLARATION);
1802     return this->u_.func_declaration_value;
1803   }
1804
1805   const Function_declaration*
1806   func_declaration_value() const
1807   {
1808     go_assert(this->classification_ == NAMED_OBJECT_FUNC_DECLARATION);
1809     return this->u_.func_declaration_value;
1810   }
1811
1812   Package*
1813   package_value()
1814   {
1815     go_assert(this->classification_ == NAMED_OBJECT_PACKAGE);
1816     return this->u_.package_value;
1817   }
1818
1819   const Package*
1820   package_value() const
1821   {
1822     go_assert(this->classification_ == NAMED_OBJECT_PACKAGE);
1823     return this->u_.package_value;
1824   }
1825
1826   const std::string&
1827   name() const
1828   { return this->name_; }
1829
1830   // Return the name to use in an error message.  The difference is
1831   // that if this Named_object is defined in a different package, this
1832   // will return PACKAGE.NAME.
1833   std::string
1834   message_name() const;
1835
1836   const Package*
1837   package() const
1838   { return this->package_; }
1839
1840   // Resolve an unknown value if possible.  This returns the same
1841   // Named_object or a new one.
1842   Named_object*
1843   resolve()
1844   {
1845     Named_object* ret = this;
1846     if (this->is_unknown())
1847       {
1848         Named_object* r = this->unknown_value()->real_named_object();
1849         if (r != NULL)
1850           ret = r;
1851       }
1852     return ret;
1853   }
1854
1855   const Named_object*
1856   resolve() const
1857   {
1858     const Named_object* ret = this;
1859     if (this->is_unknown())
1860       {
1861         const Named_object* r = this->unknown_value()->real_named_object();
1862         if (r != NULL)
1863           ret = r;
1864       }
1865     return ret;
1866   }
1867
1868   // The location where this object was defined or referenced.
1869   source_location
1870   location() const;
1871
1872   // Convert a variable to the backend representation.
1873   Bvariable*
1874   get_backend_variable(Gogo*, Named_object* function);
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     go_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     go_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                     Bblock* bblock)
2490     : gogo_(gogo), backend_(gogo->backend()), function_(function),
2491       block_(block), bblock_(bblock), 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   Bblock*
2513   bblock()
2514   { return this->bblock_; }
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.  NULL if not in a
2531   // function, e.g., the initializer of a global variable.
2532   Named_object* function_;
2533   // The block we are currently translating.  NULL if not in a
2534   // function.
2535   Block *block_;
2536   // The backend representation of the current block.  NULL if block_
2537   // is NULL.
2538   Bblock* bblock_;
2539   // Whether this is being evaluated in a constant context.  This is
2540   // used for type descriptor initializers.
2541   bool is_const_;
2542 };
2543
2544 // Runtime error codes.  These must match the values in
2545 // libgo/runtime/go-runtime-error.c.
2546
2547 // Slice index out of bounds: negative or larger than the length of
2548 // the slice.
2549 static const int RUNTIME_ERROR_SLICE_INDEX_OUT_OF_BOUNDS = 0;
2550
2551 // Array index out of bounds.
2552 static const int RUNTIME_ERROR_ARRAY_INDEX_OUT_OF_BOUNDS = 1;
2553
2554 // String index out of bounds.
2555 static const int RUNTIME_ERROR_STRING_INDEX_OUT_OF_BOUNDS = 2;
2556
2557 // Slice slice out of bounds: negative or larger than the length of
2558 // the slice or high bound less than low bound.
2559 static const int RUNTIME_ERROR_SLICE_SLICE_OUT_OF_BOUNDS = 3;
2560
2561 // Array slice out of bounds.
2562 static const int RUNTIME_ERROR_ARRAY_SLICE_OUT_OF_BOUNDS = 4;
2563
2564 // String slice out of bounds.
2565 static const int RUNTIME_ERROR_STRING_SLICE_OUT_OF_BOUNDS = 5;
2566
2567 // Dereference of nil pointer.  This is used when there is a
2568 // dereference of a pointer to a very large struct or array, to ensure
2569 // that a gigantic array is not used a proxy to access random memory
2570 // locations.
2571 static const int RUNTIME_ERROR_NIL_DEREFERENCE = 6;
2572
2573 // Slice length or capacity out of bounds in make: negative or
2574 // overflow or length greater than capacity.
2575 static const int RUNTIME_ERROR_MAKE_SLICE_OUT_OF_BOUNDS = 7;
2576
2577 // Map capacity out of bounds in make: negative or overflow.
2578 static const int RUNTIME_ERROR_MAKE_MAP_OUT_OF_BOUNDS = 8;
2579
2580 // Channel capacity out of bounds in make: negative or overflow.
2581 static const int RUNTIME_ERROR_MAKE_CHAN_OUT_OF_BOUNDS = 9;
2582
2583 // This is used by some of the langhooks.
2584 extern Gogo* go_get_gogo();
2585
2586 // Whether we have seen any errors.  FIXME: Replace with a backend
2587 // interface.
2588 extern bool saw_errors();
2589
2590 #endif // !defined(GO_GOGO_H)