OSDN Git Service

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