OSDN Git Service

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