OSDN Git Service

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