OSDN Git Service

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