OSDN Git Service

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