OSDN Git Service

compiler: Fix parse of (<- chan <- chan <- int)(x).
[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*, 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   // Note that a new local type has been added.  Return its index.
967   unsigned int
968   new_local_type_index()
969   { return this->local_type_count_++; }
970
971   // Whether this function calls the predeclared recover function.
972   bool
973   calls_recover() const
974   { return this->calls_recover_; }
975
976   // Record that this function calls the predeclared recover function.
977   // This is set during the lowering pass.
978   void
979   set_calls_recover()
980   { this->calls_recover_ = true; }
981
982   // Whether this is a recover thunk function.
983   bool
984   is_recover_thunk() const
985   { return this->is_recover_thunk_; }
986
987   // Record that this is a thunk built for a function which calls
988   // recover.
989   void
990   set_is_recover_thunk()
991   { this->is_recover_thunk_ = true; }
992
993   // Whether this function already has a recover thunk.
994   bool
995   has_recover_thunk() const
996   { return this->has_recover_thunk_; }
997
998   // Record that this function already has a recover thunk.
999   void
1000   set_has_recover_thunk()
1001   { this->has_recover_thunk_ = true; }
1002
1003   // Swap with another function.  Used only for the thunk which calls
1004   // recover.
1005   void
1006   swap_for_recover(Function *);
1007
1008   // Traverse the tree.
1009   int
1010   traverse(Traverse*);
1011
1012   // Determine types in the function.
1013   void
1014   determine_types();
1015
1016   // Return the function's decl given an identifier.
1017   tree
1018   get_or_make_decl(Gogo*, Named_object*, tree id);
1019
1020   // Return the function's decl after it has been built.
1021   tree
1022   get_decl() const
1023   {
1024     go_assert(this->fndecl_ != NULL);
1025     return this->fndecl_;
1026   }
1027
1028   // Set the function decl to hold a tree of the function code.
1029   void
1030   build_tree(Gogo*, Named_object*);
1031
1032   // Get the value to return when not explicitly specified.  May also
1033   // add statements to execute first to STMT_LIST.
1034   tree
1035   return_value(Gogo*, Named_object*, Location, tree* stmt_list) const;
1036
1037   // Get a tree for the variable holding the defer stack.
1038   Expression*
1039   defer_stack(Location);
1040
1041   // Export the function.
1042   void
1043   export_func(Export*, const std::string& name) const;
1044
1045   // Export a function with a type.
1046   static void
1047   export_func_with_type(Export*, const std::string& name,
1048                         const Function_type*);
1049
1050   // Import a function.
1051   static void
1052   import_func(Import*, std::string* pname, Typed_identifier** receiver,
1053               Typed_identifier_list** pparameters,
1054               Typed_identifier_list** presults, bool* is_varargs);
1055
1056  private:
1057   // Type for mapping from label names to Label objects.
1058   typedef Unordered_map(std::string, Label*) Labels;
1059
1060   tree
1061   make_receiver_parm_decl(Gogo*, Named_object*, tree);
1062
1063   tree
1064   copy_parm_to_heap(Gogo*, Named_object*, tree);
1065
1066   void
1067   build_defer_wrapper(Gogo*, Named_object*, tree*, tree*);
1068
1069   typedef std::vector<std::pair<Named_object*,
1070                                 Location> > Closure_fields;
1071
1072   // The function's type.
1073   Function_type* type_;
1074   // The enclosing function.  This is NULL when there isn't one, which
1075   // is the normal case.
1076   Function* enclosing_;
1077   // The result variables, if any.
1078   Results* results_;
1079   // If there is a closure, this is the list of variables which appear
1080   // in the closure.  This is created by the parser, and then resolved
1081   // to a real type when we lower parse trees.
1082   Closure_fields closure_fields_;
1083   // The closure variable, passed as a parameter using the static
1084   // chain parameter.  Normally NULL.
1085   Named_object* closure_var_;
1086   // The outer block of statements in the function.
1087   Block* block_;
1088   // The source location of the start of the function.
1089   Location location_;
1090   // Labels defined or referenced in the function.
1091   Labels labels_;
1092   // The number of local types defined in this function.
1093   unsigned int local_type_count_;
1094   // The function decl.
1095   tree fndecl_;
1096   // The defer stack variable.  A pointer to this variable is used to
1097   // distinguish the defer stack for one function from another.  This
1098   // is NULL unless we actually need a defer stack.
1099   Temporary_statement* defer_stack_;
1100   // True if the result variables are named.
1101   bool results_are_named_;
1102   // True if this function calls the predeclared recover function.
1103   bool calls_recover_;
1104   // True if this a thunk built for a function which calls recover.
1105   bool is_recover_thunk_;
1106   // True if this function already has a recover thunk.
1107   bool has_recover_thunk_;
1108 };
1109
1110 // A snapshot of the current binding state.
1111
1112 class Bindings_snapshot
1113 {
1114  public:
1115   Bindings_snapshot(const Block*, Location);
1116
1117   // Report any errors appropriate for a goto from the current binding
1118   // state of B to this one.
1119   void
1120   check_goto_from(const Block* b, Location);
1121
1122   // Report any errors appropriate for a goto from this binding state
1123   // to the current state of B.
1124   void
1125   check_goto_to(const Block* b);
1126
1127  private:
1128   bool
1129   check_goto_block(Location, const Block*, const Block*, size_t*);
1130
1131   void
1132   check_goto_defs(Location, const Block*, size_t, size_t);
1133
1134   // The current block.
1135   const Block* block_;
1136   // The number of names currently defined in each open block.
1137   // Element 0 is this->block_, element 1 is
1138   // this->block_->enclosing(), etc.
1139   std::vector<size_t> counts_;
1140   // The location where this snapshot was taken.
1141   Location location_;
1142 };
1143
1144 // A function declaration.
1145
1146 class Function_declaration
1147 {
1148  public:
1149   Function_declaration(Function_type* fntype, Location location)
1150     : fntype_(fntype), location_(location), asm_name_(), fndecl_(NULL)
1151   { }
1152
1153   Function_type*
1154   type() const
1155   { return this->fntype_; }
1156
1157   Location
1158   location() const
1159   { return this->location_; }
1160
1161   const std::string&
1162   asm_name() const
1163   { return this->asm_name_; }
1164
1165   // Set the assembler name.
1166   void
1167   set_asm_name(const std::string& asm_name)
1168   { this->asm_name_ = asm_name; }
1169
1170   // Return a decl for the function given an identifier.
1171   tree
1172   get_or_make_decl(Gogo*, Named_object*, tree id);
1173
1174   // Export a function declaration.
1175   void
1176   export_func(Export* exp, const std::string& name) const
1177   { Function::export_func_with_type(exp, name, this->fntype_); }
1178
1179  private:
1180   // The type of the function.
1181   Function_type* fntype_;
1182   // The location of the declaration.
1183   Location location_;
1184   // The assembler name: this is the name to use in references to the
1185   // function.  This is normally empty.
1186   std::string asm_name_;
1187   // The function decl if needed.
1188   tree fndecl_;
1189 };
1190
1191 // A variable.
1192
1193 class Variable
1194 {
1195  public:
1196   Variable(Type*, Expression*, bool is_global, bool is_parameter,
1197            bool is_receiver, Location);
1198
1199   // Get the type of the variable.
1200   Type*
1201   type();
1202
1203   Type*
1204   type() const;
1205
1206   // Return whether the type is defined yet.
1207   bool
1208   has_type() const;
1209
1210   // Get the initial value.
1211   Expression*
1212   init() const
1213   { return this->init_; }
1214
1215   // Return whether there are any preinit statements.
1216   bool
1217   has_pre_init() const
1218   { return this->preinit_ != NULL; }
1219
1220   // Return the preinit statements if any.
1221   Block*
1222   preinit() const
1223   { return this->preinit_; }
1224
1225   // Return whether this is a global variable.
1226   bool
1227   is_global() const
1228   { return this->is_global_; }
1229
1230   // Return whether this is a function parameter.
1231   bool
1232   is_parameter() const
1233   { return this->is_parameter_; }
1234
1235   // Return whether this is the receiver parameter of a method.
1236   bool
1237   is_receiver() const
1238   { return this->is_receiver_; }
1239
1240   // Change this parameter to be a receiver.  This is used when
1241   // creating the thunks created for functions which call recover.
1242   void
1243   set_is_receiver()
1244   {
1245     go_assert(this->is_parameter_);
1246     this->is_receiver_ = true;
1247   }
1248
1249   // Change this parameter to not be a receiver.  This is used when
1250   // creating the thunks created for functions which call recover.
1251   void
1252   set_is_not_receiver()
1253   {
1254     go_assert(this->is_parameter_);
1255     this->is_receiver_ = false;
1256   }
1257
1258   // Return whether this is the varargs parameter of a function.
1259   bool
1260   is_varargs_parameter() const
1261   { return this->is_varargs_parameter_; }
1262
1263   // Whether this variable's address is taken.
1264   bool
1265   is_address_taken() const
1266   { return this->is_address_taken_; }
1267
1268   // Whether this variable should live in the heap.
1269   bool
1270   is_in_heap() const
1271   { return this->is_address_taken_ && !this->is_global_; }
1272
1273   // Note that something takes the address of this variable.
1274   void
1275   set_address_taken()
1276   { this->is_address_taken_ = true; }
1277
1278   // Return whether the address is taken but does not escape.
1279   bool
1280   is_non_escaping_address_taken() const
1281   { return this->is_non_escaping_address_taken_; }
1282
1283   // Note that something takes the address of this variable such that
1284   // the address does not escape the function.
1285   void
1286   set_non_escaping_address_taken()
1287   { this->is_non_escaping_address_taken_ = true; }
1288
1289   // Get the source location of the variable's declaration.
1290   Location
1291   location() const
1292   { return this->location_; }
1293
1294   // Record that this is the varargs parameter of a function.
1295   void
1296   set_is_varargs_parameter()
1297   {
1298     go_assert(this->is_parameter_);
1299     this->is_varargs_parameter_ = true;
1300   }
1301
1302   // Return whether the variable has been used.
1303   bool
1304   is_used() const
1305   { return this->is_used_; }
1306
1307   // Mark that the variable has been used.
1308   void
1309   set_is_used()
1310   { this->is_used_ = true; }
1311
1312   // Clear the initial value; used for error handling.
1313   void
1314   clear_init()
1315   { this->init_ = NULL; }
1316
1317   // Set the initial value; used for converting shortcuts.
1318   void
1319   set_init(Expression* init)
1320   { this->init_ = init; }
1321
1322   // Get the preinit block, a block of statements to be run before the
1323   // initialization expression.
1324   Block*
1325   preinit_block(Gogo*);
1326
1327   // Add a statement to be run before the initialization expression.
1328   // This is only used for global variables.
1329   void
1330   add_preinit_statement(Gogo*, Statement*);
1331
1332   // Lower the initialization expression after parsing is complete.
1333   void
1334   lower_init_expression(Gogo*, Named_object*, Statement_inserter*);
1335
1336   // A special case: the init value is used only to determine the
1337   // type.  This is used if the variable is defined using := with the
1338   // comma-ok form of a map index or a receive expression.  The init
1339   // value is actually the map index expression or receive expression.
1340   // We use this because we may not know the right type at parse time.
1341   void
1342   set_type_from_init_tuple()
1343   { this->type_from_init_tuple_ = true; }
1344
1345   // Another special case: the init value is used only to determine
1346   // the type.  This is used if the variable is defined using := with
1347   // a range clause.  The init value is the range expression.  The
1348   // type of the variable is the index type of the range expression
1349   // (i.e., the first value returned by a range).
1350   void
1351   set_type_from_range_index()
1352   { this->type_from_range_index_ = true; }
1353
1354   // Another special case: like set_type_from_range_index, but the
1355   // type is the value type of the range expression (i.e., the second
1356   // value returned by a range).
1357   void
1358   set_type_from_range_value()
1359   { this->type_from_range_value_ = true; }
1360
1361   // Another special case: the init value is used only to determine
1362   // the type.  This is used if the variable is defined using := with
1363   // a case in a select statement.  The init value is the channel.
1364   // The type of the variable is the channel's element type.
1365   void
1366   set_type_from_chan_element()
1367   { this->type_from_chan_element_ = true; }
1368
1369   // After we lower the select statement, we once again set the type
1370   // from the initialization expression.
1371   void
1372   clear_type_from_chan_element()
1373   {
1374     go_assert(this->type_from_chan_element_);
1375     this->type_from_chan_element_ = false;
1376   }
1377
1378   // Note that this variable was created for a type switch clause.
1379   void
1380   set_is_type_switch_var()
1381   { this->is_type_switch_var_ = true; }
1382
1383   // Traverse the initializer expression.
1384   int
1385   traverse_expression(Traverse*, unsigned int traverse_mask);
1386
1387   // Determine the type of the variable if necessary.
1388   void
1389   determine_type();
1390
1391   // Get the backend representation of the variable.
1392   Bvariable*
1393   get_backend_variable(Gogo*, Named_object*, const Package*,
1394                        const std::string&);
1395
1396   // Get the initial value of the variable as a tree.  This may only
1397   // be called if has_pre_init() returns false.
1398   tree
1399   get_init_tree(Gogo*, Named_object* function);
1400
1401   // Return a series of statements which sets the value of the
1402   // variable in DECL.  This should only be called is has_pre_init()
1403   // returns true.  DECL may be NULL for a sink variable.
1404   tree
1405   get_init_block(Gogo*, Named_object* function, tree decl);
1406
1407   // Export the variable.
1408   void
1409   export_var(Export*, const std::string& name) const;
1410
1411   // Import a variable.
1412   static void
1413   import_var(Import*, std::string* pname, Type** ptype);
1414
1415  private:
1416   // The type of a tuple.
1417   Type*
1418   type_from_tuple(Expression*, bool) const;
1419
1420   // The type of a range.
1421   Type*
1422   type_from_range(Expression*, bool, bool) const;
1423
1424   // The element type of a channel.
1425   Type*
1426   type_from_chan_element(Expression*, bool) const;
1427
1428   // The variable's type.  This may be NULL if the type is set from
1429   // the expression.
1430   Type* type_;
1431   // The initial value.  This may be NULL if the variable should be
1432   // initialized to the default value for the type.
1433   Expression* init_;
1434   // Statements to run before the init statement.
1435   Block* preinit_;
1436   // Location of variable definition.
1437   Location location_;
1438   // Backend representation.
1439   Bvariable* backend_;
1440   // Whether this is a global variable.
1441   bool is_global_ : 1;
1442   // Whether this is a function parameter.
1443   bool is_parameter_ : 1;
1444   // Whether this is the receiver parameter of a method.
1445   bool is_receiver_ : 1;
1446   // Whether this is the varargs parameter of a function.
1447   bool is_varargs_parameter_ : 1;
1448   // Whether this variable is ever referenced.
1449   bool is_used_ : 1;
1450   // Whether something takes the address of this variable.  For a
1451   // local variable this implies that the variable has to be on the
1452   // heap.
1453   bool is_address_taken_ : 1;
1454   // Whether something takes the address of this variable such that
1455   // the address does not escape the function.
1456   bool is_non_escaping_address_taken_ : 1;
1457   // True if we have seen this variable in a traversal.
1458   bool seen_ : 1;
1459   // True if we have lowered the initialization expression.
1460   bool init_is_lowered_ : 1;
1461   // True if init is a tuple used to set the type.
1462   bool type_from_init_tuple_ : 1;
1463   // True if init is a range clause and the type is the index type.
1464   bool type_from_range_index_ : 1;
1465   // True if init is a range clause and the type is the value type.
1466   bool type_from_range_value_ : 1;
1467   // True if init is a channel and the type is the channel's element type.
1468   bool type_from_chan_element_ : 1;
1469   // True if this is a variable created for a type switch case.
1470   bool is_type_switch_var_ : 1;
1471   // True if we have determined types.
1472   bool determined_type_ : 1;
1473 };
1474
1475 // A variable which is really the name for a function return value, or
1476 // part of one.
1477
1478 class Result_variable
1479 {
1480  public:
1481   Result_variable(Type* type, Function* function, int index,
1482                   Location location)
1483     : type_(type), function_(function), index_(index), location_(location),
1484       backend_(NULL), is_address_taken_(false),
1485       is_non_escaping_address_taken_(false)
1486   { }
1487
1488   // Get the type of the result variable.
1489   Type*
1490   type() const
1491   { return this->type_; }
1492
1493   // Get the function that this is associated with.
1494   Function*
1495   function() const
1496   { return this->function_; }
1497
1498   // Index in the list of function results.
1499   int
1500   index() const
1501   { return this->index_; }
1502
1503   // The location of the variable definition.
1504   Location
1505   location() const
1506   { return this->location_; }
1507
1508   // Whether this variable's address is taken.
1509   bool
1510   is_address_taken() const
1511   { return this->is_address_taken_; }
1512
1513   // Note that something takes the address of this variable.
1514   void
1515   set_address_taken()
1516   { this->is_address_taken_ = true; }
1517
1518   // Return whether the address is taken but does not escape.
1519   bool
1520   is_non_escaping_address_taken() const
1521   { return this->is_non_escaping_address_taken_; }
1522
1523   // Note that something takes the address of this variable such that
1524   // the address does not escape the function.
1525   void
1526   set_non_escaping_address_taken()
1527   { this->is_non_escaping_address_taken_ = true; }
1528
1529   // Whether this variable should live in the heap.
1530   bool
1531   is_in_heap() const
1532   { return this->is_address_taken_; }
1533
1534   // Set the function.  This is used when cloning functions which call
1535   // recover.
1536   void
1537   set_function(Function* function)
1538   { this->function_ = function; }
1539
1540   // Get the backend representation of the variable.
1541   Bvariable*
1542   get_backend_variable(Gogo*, Named_object*, const std::string&);
1543
1544  private:
1545   // Type of result variable.
1546   Type* type_;
1547   // Function with which this is associated.
1548   Function* function_;
1549   // Index in list of results.
1550   int index_;
1551   // Where the result variable is defined.
1552   Location location_;
1553   // Backend representation.
1554   Bvariable* backend_;
1555   // Whether something takes the address of this variable.
1556   bool is_address_taken_;
1557   // Whether something takes the address of this variable such that
1558   // the address does not escape the function.
1559   bool is_non_escaping_address_taken_;
1560 };
1561
1562 // The value we keep for a named constant.  This lets us hold a type
1563 // and an expression.
1564
1565 class Named_constant
1566 {
1567  public:
1568   Named_constant(Type* type, Expression* expr, int iota_value,
1569                  Location location)
1570     : type_(type), expr_(expr), iota_value_(iota_value), location_(location),
1571       lowering_(false)
1572   { }
1573
1574   Type*
1575   type() const
1576   { return this->type_; }
1577
1578   Expression*
1579   expr() const
1580   { return this->expr_; }
1581
1582   int
1583   iota_value() const
1584   { return this->iota_value_; }
1585
1586   Location
1587   location() const
1588   { return this->location_; }
1589
1590   // Whether we are lowering.
1591   bool
1592   lowering() const
1593   { return this->lowering_; }
1594
1595   // Set that we are lowering.
1596   void
1597   set_lowering()
1598   { this->lowering_ = true; }
1599
1600   // We are no longer lowering.
1601   void
1602   clear_lowering()
1603   { this->lowering_ = false; }
1604
1605   // Traverse the expression.
1606   int
1607   traverse_expression(Traverse*);
1608
1609   // Determine the type of the constant if necessary.
1610   void
1611   determine_type();
1612
1613   // Indicate that we found and reported an error for this constant.
1614   void
1615   set_error();
1616
1617   // Export the constant.
1618   void
1619   export_const(Export*, const std::string& name) const;
1620
1621   // Import a constant.
1622   static void
1623   import_const(Import*, std::string*, Type**, Expression**);
1624
1625  private:
1626   // The type of the constant.
1627   Type* type_;
1628   // The expression for the constant.
1629   Expression* expr_;
1630   // If the predeclared constant iota is used in EXPR_, this is the
1631   // value it will have.  We do this because at parse time we don't
1632   // know whether the name "iota" will refer to the predeclared
1633   // constant or to something else.  We put in the right value in when
1634   // we lower.
1635   int iota_value_;
1636   // The location of the definition.
1637   Location location_;
1638   // Whether we are currently lowering this constant.
1639   bool lowering_;
1640 };
1641
1642 // A type declaration.
1643
1644 class Type_declaration
1645 {
1646  public:
1647   Type_declaration(Location location)
1648     : location_(location), in_function_(NULL), in_function_index_(0),
1649       methods_(), issued_warning_(false)
1650   { }
1651
1652   // Return the location.
1653   Location
1654   location() const
1655   { return this->location_; }
1656
1657   // Return the function in which this type is declared.  This will
1658   // return NULL for a type declared in global scope.
1659   Named_object*
1660   in_function(unsigned int* pindex)
1661   {
1662     *pindex = this->in_function_index_;
1663     return this->in_function_;
1664   }
1665
1666   // Set the function in which this type is declared.
1667   void
1668   set_in_function(Named_object* f, unsigned int index)
1669   {
1670     this->in_function_ = f;
1671     this->in_function_index_ = index;
1672   }
1673
1674   // Add a method to this type.  This is used when methods are defined
1675   // before the type.
1676   Named_object*
1677   add_method(const std::string& name, Function* function);
1678
1679   // Add a method declaration to this type.
1680   Named_object*
1681   add_method_declaration(const std::string& name, Package*,
1682                          Function_type* type, Location location);
1683
1684   // Return whether any methods were defined.
1685   bool
1686   has_methods() const;
1687
1688   // Define methods when the real type is known.
1689   void
1690   define_methods(Named_type*);
1691
1692   // This is called if we are trying to use this type.  It returns
1693   // true if we should issue a warning.
1694   bool
1695   using_type();
1696
1697  private:
1698   typedef std::vector<Named_object*> Methods;
1699
1700   // The location of the type declaration.
1701   Location location_;
1702   // If this type is declared in a function, a pointer back to the
1703   // function in which it is defined.
1704   Named_object* in_function_;
1705   // The index of this type in IN_FUNCTION_.
1706   unsigned int in_function_index_;
1707   // Methods defined before the type is defined.
1708   Methods methods_;
1709   // True if we have issued a warning about a use of this type
1710   // declaration when it is undefined.
1711   bool issued_warning_;
1712 };
1713
1714 // An unknown object.  These are created by the parser for forward
1715 // references to names which have not been seen before.  In a correct
1716 // program, these will always point to a real definition by the end of
1717 // the parse.  Because they point to another Named_object, these may
1718 // only be referenced by Unknown_expression objects.
1719
1720 class Unknown_name
1721 {
1722  public:
1723   Unknown_name(Location location)
1724     : location_(location), real_named_object_(NULL)
1725   { }
1726
1727   // Return the location where this name was first seen.
1728   Location
1729   location() const
1730   { return this->location_; }
1731
1732   // Return the real named object that this points to, or NULL if it
1733   // was never resolved.
1734   Named_object*
1735   real_named_object() const
1736   { return this->real_named_object_; }
1737
1738   // Set the real named object that this points to.
1739   void
1740   set_real_named_object(Named_object* no);
1741
1742  private:
1743   // The location where this name was first seen.
1744   Location location_;
1745   // The real named object when it is known.
1746   Named_object*
1747   real_named_object_;
1748 };
1749
1750 // A named object named.  This is the result of a declaration.  We
1751 // don't use a superclass because they all have to be handled
1752 // differently.
1753
1754 class Named_object
1755 {
1756  public:
1757   enum Classification
1758   {
1759     // An uninitialized Named_object.  We should never see this.
1760     NAMED_OBJECT_UNINITIALIZED,
1761     // An erroneous name.  This indicates a parse error, to avoid
1762     // later errors about undefined references.
1763     NAMED_OBJECT_ERRONEOUS,
1764     // An unknown name.  This is used for forward references.  In a
1765     // correct program, these will all be resolved by the end of the
1766     // parse.
1767     NAMED_OBJECT_UNKNOWN,
1768     // A const.
1769     NAMED_OBJECT_CONST,
1770     // A type.
1771     NAMED_OBJECT_TYPE,
1772     // A forward type declaration.
1773     NAMED_OBJECT_TYPE_DECLARATION,
1774     // A var.
1775     NAMED_OBJECT_VAR,
1776     // A result variable in a function.
1777     NAMED_OBJECT_RESULT_VAR,
1778     // The blank identifier--the special variable named _.
1779     NAMED_OBJECT_SINK,
1780     // A func.
1781     NAMED_OBJECT_FUNC,
1782     // A forward func declaration.
1783     NAMED_OBJECT_FUNC_DECLARATION,
1784     // A package.
1785     NAMED_OBJECT_PACKAGE
1786   };
1787
1788   // Return the classification.
1789   Classification
1790   classification() const
1791   { return this->classification_; }
1792
1793   // Classifiers.
1794
1795   bool
1796   is_erroneous() const
1797   { return this->classification_ == NAMED_OBJECT_ERRONEOUS; }
1798
1799   bool
1800   is_unknown() const
1801   { return this->classification_ == NAMED_OBJECT_UNKNOWN; }
1802
1803   bool
1804   is_const() const
1805   { return this->classification_ == NAMED_OBJECT_CONST; }
1806
1807   bool
1808   is_type() const
1809   { return this->classification_ == NAMED_OBJECT_TYPE; }
1810
1811   bool
1812   is_type_declaration() const
1813   { return this->classification_ == NAMED_OBJECT_TYPE_DECLARATION; }
1814
1815   bool
1816   is_variable() const
1817   { return this->classification_ == NAMED_OBJECT_VAR; }
1818
1819   bool
1820   is_result_variable() const
1821   { return this->classification_ == NAMED_OBJECT_RESULT_VAR; }
1822
1823   bool
1824   is_sink() const
1825   { return this->classification_ == NAMED_OBJECT_SINK; }
1826
1827   bool
1828   is_function() const
1829   { return this->classification_ == NAMED_OBJECT_FUNC; }
1830
1831   bool
1832   is_function_declaration() const
1833   { return this->classification_ == NAMED_OBJECT_FUNC_DECLARATION; }
1834
1835   bool
1836   is_package() const
1837   { return this->classification_ == NAMED_OBJECT_PACKAGE; }
1838
1839   // Creators.
1840
1841   static Named_object*
1842   make_erroneous_name(const std::string& name)
1843   { return new Named_object(name, NULL, NAMED_OBJECT_ERRONEOUS); }
1844
1845   static Named_object*
1846   make_unknown_name(const std::string& name, Location);
1847
1848   static Named_object*
1849   make_constant(const Typed_identifier&, const Package*, Expression*,
1850                 int iota_value);
1851
1852   static Named_object*
1853   make_type(const std::string&, const Package*, Type*, Location);
1854
1855   static Named_object*
1856   make_type_declaration(const std::string&, const Package*, Location);
1857
1858   static Named_object*
1859   make_variable(const std::string&, const Package*, Variable*);
1860
1861   static Named_object*
1862   make_result_variable(const std::string&, Result_variable*);
1863
1864   static Named_object*
1865   make_sink();
1866
1867   static Named_object*
1868   make_function(const std::string&, const Package*, Function*);
1869
1870   static Named_object*
1871   make_function_declaration(const std::string&, const Package*, Function_type*,
1872                             Location);
1873
1874   static Named_object*
1875   make_package(const std::string& alias, Package* package);
1876
1877   // Getters.
1878
1879   Unknown_name*
1880   unknown_value()
1881   {
1882     go_assert(this->classification_ == NAMED_OBJECT_UNKNOWN);
1883     return this->u_.unknown_value;
1884   }
1885
1886   const Unknown_name*
1887   unknown_value() const
1888   {
1889     go_assert(this->classification_ == NAMED_OBJECT_UNKNOWN);
1890     return this->u_.unknown_value;
1891   }
1892
1893   Named_constant*
1894   const_value()
1895   {
1896     go_assert(this->classification_ == NAMED_OBJECT_CONST);
1897     return this->u_.const_value;
1898   }
1899
1900   const Named_constant*
1901   const_value() const
1902   {
1903     go_assert(this->classification_ == NAMED_OBJECT_CONST);
1904     return this->u_.const_value;
1905   }
1906
1907   Named_type*
1908   type_value()
1909   {
1910     go_assert(this->classification_ == NAMED_OBJECT_TYPE);
1911     return this->u_.type_value;
1912   }
1913
1914   const Named_type*
1915   type_value() const
1916   {
1917     go_assert(this->classification_ == NAMED_OBJECT_TYPE);
1918     return this->u_.type_value;
1919   }
1920
1921   Type_declaration*
1922   type_declaration_value()
1923   {
1924     go_assert(this->classification_ == NAMED_OBJECT_TYPE_DECLARATION);
1925     return this->u_.type_declaration;
1926   }
1927
1928   const Type_declaration*
1929   type_declaration_value() const
1930   {
1931     go_assert(this->classification_ == NAMED_OBJECT_TYPE_DECLARATION);
1932     return this->u_.type_declaration;
1933   }
1934
1935   Variable*
1936   var_value()
1937   {
1938     go_assert(this->classification_ == NAMED_OBJECT_VAR);
1939     return this->u_.var_value;
1940   }
1941
1942   const Variable*
1943   var_value() const
1944   {
1945     go_assert(this->classification_ == NAMED_OBJECT_VAR);
1946     return this->u_.var_value;
1947   }
1948
1949   Result_variable*
1950   result_var_value()
1951   {
1952     go_assert(this->classification_ == NAMED_OBJECT_RESULT_VAR);
1953     return this->u_.result_var_value;
1954   }
1955
1956   const Result_variable*
1957   result_var_value() const
1958   {
1959     go_assert(this->classification_ == NAMED_OBJECT_RESULT_VAR);
1960     return this->u_.result_var_value;
1961   }
1962
1963   Function*
1964   func_value()
1965   {
1966     go_assert(this->classification_ == NAMED_OBJECT_FUNC);
1967     return this->u_.func_value;
1968   }
1969
1970   const Function*
1971   func_value() const
1972   {
1973     go_assert(this->classification_ == NAMED_OBJECT_FUNC);
1974     return this->u_.func_value;
1975   }
1976
1977   Function_declaration*
1978   func_declaration_value()
1979   {
1980     go_assert(this->classification_ == NAMED_OBJECT_FUNC_DECLARATION);
1981     return this->u_.func_declaration_value;
1982   }
1983
1984   const Function_declaration*
1985   func_declaration_value() const
1986   {
1987     go_assert(this->classification_ == NAMED_OBJECT_FUNC_DECLARATION);
1988     return this->u_.func_declaration_value;
1989   }
1990
1991   Package*
1992   package_value()
1993   {
1994     go_assert(this->classification_ == NAMED_OBJECT_PACKAGE);
1995     return this->u_.package_value;
1996   }
1997
1998   const Package*
1999   package_value() const
2000   {
2001     go_assert(this->classification_ == NAMED_OBJECT_PACKAGE);
2002     return this->u_.package_value;
2003   }
2004
2005   const std::string&
2006   name() const
2007   { return this->name_; }
2008
2009   // Return the name to use in an error message.  The difference is
2010   // that if this Named_object is defined in a different package, this
2011   // will return PACKAGE.NAME.
2012   std::string
2013   message_name() const;
2014
2015   const Package*
2016   package() const
2017   { return this->package_; }
2018
2019   // Resolve an unknown value if possible.  This returns the same
2020   // Named_object or a new one.
2021   Named_object*
2022   resolve()
2023   {
2024     Named_object* ret = this;
2025     if (this->is_unknown())
2026       {
2027         Named_object* r = this->unknown_value()->real_named_object();
2028         if (r != NULL)
2029           ret = r;
2030       }
2031     return ret;
2032   }
2033
2034   const Named_object*
2035   resolve() const
2036   {
2037     const Named_object* ret = this;
2038     if (this->is_unknown())
2039       {
2040         const Named_object* r = this->unknown_value()->real_named_object();
2041         if (r != NULL)
2042           ret = r;
2043       }
2044     return ret;
2045   }
2046
2047   // The location where this object was defined or referenced.
2048   Location
2049   location() const;
2050
2051   // Convert a variable to the backend representation.
2052   Bvariable*
2053   get_backend_variable(Gogo*, Named_object* function);
2054
2055   // Return a tree for the external identifier for this object.
2056   tree
2057   get_id(Gogo*);
2058
2059   // Return a tree representing this object.
2060   tree
2061   get_tree(Gogo*, Named_object* function);
2062
2063   // Define a type declaration.
2064   void
2065   set_type_value(Named_type*);
2066
2067   // Define a function declaration.
2068   void
2069   set_function_value(Function*);
2070
2071   // Declare an unknown name as a type declaration.
2072   void
2073   declare_as_type();
2074
2075   // Export this object.
2076   void
2077   export_named_object(Export*) const;
2078
2079  private:
2080   Named_object(const std::string&, const Package*, Classification);
2081
2082   // The name of the object.
2083   std::string name_;
2084   // The package that this object is in.  This is NULL if it is in the
2085   // file we are compiling.
2086   const Package* package_;
2087   // The type of object this is.
2088   Classification classification_;
2089   // The real data.
2090   union
2091   {
2092     Unknown_name* unknown_value;
2093     Named_constant* const_value;
2094     Named_type* type_value;
2095     Type_declaration* type_declaration;
2096     Variable* var_value;
2097     Result_variable* result_var_value;
2098     Function* func_value;
2099     Function_declaration* func_declaration_value;
2100     Package* package_value;
2101   } u_;
2102   // The DECL tree for this object if we have already converted it.
2103   tree tree_;
2104 };
2105
2106 // A binding contour.  This binds names to objects.
2107
2108 class Bindings
2109 {
2110  public:
2111   // Type for mapping from names to objects.
2112   typedef Unordered_map(std::string, Named_object*) Contour;
2113
2114   Bindings(Bindings* enclosing);
2115
2116   // Add an erroneous name.
2117   Named_object*
2118   add_erroneous_name(const std::string& name)
2119   { return this->add_named_object(Named_object::make_erroneous_name(name)); }
2120
2121   // Add an unknown name.
2122   Named_object*
2123   add_unknown_name(const std::string& name, Location location)
2124   {
2125     return this->add_named_object(Named_object::make_unknown_name(name,
2126                                                                   location));
2127   }
2128
2129   // Add a constant.
2130   Named_object*
2131   add_constant(const Typed_identifier& tid, const Package* package,
2132                Expression* expr, int iota_value)
2133   {
2134     return this->add_named_object(Named_object::make_constant(tid, package,
2135                                                               expr,
2136                                                               iota_value));
2137   }
2138
2139   // Add a type.
2140   Named_object*
2141   add_type(const std::string& name, const Package* package, Type* type,
2142            Location location)
2143   {
2144     return this->add_named_object(Named_object::make_type(name, package, type,
2145                                                           location));
2146   }
2147
2148   // Add a named type.  This is used for builtin types, and to add an
2149   // imported type to the global scope.
2150   Named_object*
2151   add_named_type(Named_type* named_type);
2152
2153   // Add a type declaration.
2154   Named_object*
2155   add_type_declaration(const std::string& name, const Package* package,
2156                        Location location)
2157   {
2158     Named_object* no = Named_object::make_type_declaration(name, package,
2159                                                            location);
2160     return this->add_named_object(no);
2161   }
2162
2163   // Add a variable.
2164   Named_object*
2165   add_variable(const std::string& name, const Package* package,
2166                Variable* variable)
2167   {
2168     return this->add_named_object(Named_object::make_variable(name, package,
2169                                                               variable));
2170   }
2171
2172   // Add a result variable.
2173   Named_object*
2174   add_result_variable(const std::string& name, Result_variable* result)
2175   {
2176     return this->add_named_object(Named_object::make_result_variable(name,
2177                                                                      result));
2178   }
2179
2180   // Add a function.
2181   Named_object*
2182   add_function(const std::string& name, const Package*, Function* function);
2183
2184   // Add a function declaration.
2185   Named_object*
2186   add_function_declaration(const std::string& name, const Package* package,
2187                            Function_type* type, Location location);
2188
2189   // Add a package.  The location is the location of the import
2190   // statement.
2191   Named_object*
2192   add_package(const std::string& alias, Package* package)
2193   {
2194     Named_object* no = Named_object::make_package(alias, package);
2195     return this->add_named_object(no);
2196   }
2197
2198   // Define a type which was already declared.
2199   void
2200   define_type(Named_object*, Named_type*);
2201
2202   // Add a method to the list of objects.  This is not added to the
2203   // lookup table.
2204   void
2205   add_method(Named_object*);
2206
2207   // Add a named object to this binding.
2208   Named_object*
2209   add_named_object(Named_object* no)
2210   { return this->add_named_object_to_contour(&this->bindings_, no); }
2211
2212   // Clear all names in file scope from the bindings.
2213   void
2214   clear_file_scope();
2215
2216   // Look up a name in this binding contour and in any enclosing
2217   // binding contours.  This returns NULL if the name is not found.
2218   Named_object*
2219   lookup(const std::string&) const;
2220
2221   // Look up a name in this binding contour without looking in any
2222   // enclosing binding contours.  Returns NULL if the name is not found.
2223   Named_object*
2224   lookup_local(const std::string&) const;
2225
2226   // Remove a name.
2227   void
2228   remove_binding(Named_object*);
2229
2230   // Mark all variables as used.  This is used for some types of parse
2231   // error.
2232   void
2233   mark_locals_used();
2234
2235   // Traverse the tree.  See the Traverse class.
2236   int
2237   traverse(Traverse*, bool is_global);
2238
2239   // Iterate over definitions.  This does not include things which
2240   // were only declared.
2241
2242   typedef std::vector<Named_object*>::const_iterator
2243     const_definitions_iterator;
2244
2245   const_definitions_iterator
2246   begin_definitions() const
2247   { return this->named_objects_.begin(); }
2248
2249   const_definitions_iterator
2250   end_definitions() const
2251   { return this->named_objects_.end(); }
2252
2253   // Return the number of definitions.
2254   size_t
2255   size_definitions() const
2256   { return this->named_objects_.size(); }
2257
2258   // Return whether there are no definitions.
2259   bool
2260   empty_definitions() const
2261   { return this->named_objects_.empty(); }
2262
2263   // Iterate over declarations.  This is everything that has been
2264   // declared, which includes everything which has been defined.
2265
2266   typedef Contour::const_iterator const_declarations_iterator;
2267
2268   const_declarations_iterator
2269   begin_declarations() const
2270   { return this->bindings_.begin(); }
2271
2272   const_declarations_iterator
2273   end_declarations() const
2274   { return this->bindings_.end(); }
2275
2276   // Return the number of declarations.
2277   size_t
2278   size_declarations() const
2279   { return this->bindings_.size(); }
2280
2281   // Return whether there are no declarations.
2282   bool
2283   empty_declarations() const
2284   { return this->bindings_.empty(); }
2285
2286   // Return the first declaration.
2287   Named_object*
2288   first_declaration()
2289   { return this->bindings_.empty() ? NULL : this->bindings_.begin()->second; }
2290
2291  private:
2292   Named_object*
2293   add_named_object_to_contour(Contour*, Named_object*);
2294
2295   Named_object*
2296   new_definition(Named_object*, Named_object*);
2297
2298   // Enclosing bindings.
2299   Bindings* enclosing_;
2300   // The list of objects.
2301   std::vector<Named_object*> named_objects_;
2302   // The mapping from names to objects.
2303   Contour bindings_;
2304 };
2305
2306 // A label.
2307
2308 class Label
2309 {
2310  public:
2311   Label(const std::string& name)
2312     : name_(name), location_(Linemap::unknown_location()), snapshot_(NULL),
2313       refs_(), is_used_(false), blabel_(NULL)
2314   { }
2315
2316   // Return the label's name.
2317   const std::string&
2318   name() const
2319   { return this->name_; }
2320
2321   // Return whether the label has been defined.
2322   bool
2323   is_defined() const
2324   { return !Linemap::is_unknown_location(this->location_); }
2325
2326   // Return whether the label has been used.
2327   bool
2328   is_used() const
2329   { return this->is_used_; }
2330
2331   // Record that the label is used.
2332   void
2333   set_is_used()
2334   { this->is_used_ = true; }
2335
2336   // Return the location of the definition.
2337   Location
2338   location() const
2339   { return this->location_; }
2340
2341   // Return the bindings snapshot.
2342   Bindings_snapshot*
2343   snapshot() const
2344   { return this->snapshot_; }
2345
2346   // Add a snapshot of a goto which refers to this label.
2347   void
2348   add_snapshot_ref(Bindings_snapshot* snapshot)
2349   {
2350     go_assert(Linemap::is_unknown_location(this->location_));
2351     this->refs_.push_back(snapshot);
2352   }
2353
2354   // Return the list of snapshots of goto statements which refer to
2355   // this label.
2356   const std::vector<Bindings_snapshot*>&
2357   refs() const
2358   { return this->refs_; }
2359
2360   // Clear the references.
2361   void
2362   clear_refs();
2363
2364   // Define the label at LOCATION with the given bindings snapshot.
2365   void
2366   define(Location location, Bindings_snapshot* snapshot)
2367   {
2368     go_assert(Linemap::is_unknown_location(this->location_)
2369               && this->snapshot_ == NULL);
2370     this->location_ = location;
2371     this->snapshot_ = snapshot;
2372   }
2373
2374   // Return the backend representation for this label.
2375   Blabel*
2376   get_backend_label(Translate_context*);
2377
2378   // Return an expression for the address of this label.  This is used
2379   // to get the return address of a deferred function to see whether
2380   // the function may call recover.
2381   Bexpression*
2382   get_addr(Translate_context*, Location location);
2383
2384  private:
2385   // The name of the label.
2386   std::string name_;
2387   // The location of the definition.  This is 0 if the label has not
2388   // yet been defined.
2389   Location location_;
2390   // A snapshot of the set of bindings defined at this label, used to
2391   // issue errors about invalid goto statements.
2392   Bindings_snapshot* snapshot_;
2393   // A list of snapshots of goto statements which refer to this label.
2394   std::vector<Bindings_snapshot*> refs_;
2395   // Whether the label has been used.
2396   bool is_used_;
2397   // The backend representation.
2398   Blabel* blabel_;
2399 };
2400
2401 // An unnamed label.  These are used when lowering loops.
2402
2403 class Unnamed_label
2404 {
2405  public:
2406   Unnamed_label(Location location)
2407     : location_(location), blabel_(NULL)
2408   { }
2409
2410   // Get the location where the label is defined.
2411   Location
2412   location() const
2413   { return this->location_; }
2414
2415   // Set the location where the label is defined.
2416   void
2417   set_location(Location location)
2418   { this->location_ = location; }
2419
2420   // Return a statement which defines this label.
2421   Bstatement*
2422   get_definition(Translate_context*);
2423
2424   // Return a goto to this label from LOCATION.
2425   Bstatement*
2426   get_goto(Translate_context*, Location location);
2427
2428  private:
2429   // Return the backend representation.
2430   Blabel*
2431   get_blabel(Translate_context*);
2432
2433   // The location where the label is defined.
2434   Location location_;
2435   // The backend representation of this label.
2436   Blabel* blabel_;
2437 };
2438
2439 // An imported package.
2440
2441 class Package
2442 {
2443  public:
2444   Package(const std::string& pkgpath, Location location);
2445
2446   // Get the package path used for all symbols exported from this
2447   // package.
2448   const std::string&
2449   pkgpath() const
2450   { return this->pkgpath_; }
2451
2452   // Return the package path to use for a symbol name.
2453   const std::string&
2454   pkgpath_symbol() const
2455   { return this->pkgpath_symbol_; }
2456
2457   // Return the location of the import statement.
2458   Location
2459   location() const
2460   { return this->location_; }
2461
2462   // Return whether we know the name of this package yet.
2463   bool
2464   has_package_name() const
2465   { return !this->package_name_.empty(); }
2466
2467   // The name that this package uses in its package clause.  This may
2468   // be different from the name in the associated Named_object if the
2469   // import statement used an alias.
2470   const std::string&
2471   package_name() const
2472   {
2473     go_assert(!this->package_name_.empty());
2474     return this->package_name_;
2475   }
2476
2477   // The priority of this package.  The init function of packages with
2478   // lower priority must be run before the init function of packages
2479   // with higher priority.
2480   int
2481   priority() const
2482   { return this->priority_; }
2483
2484   // Set the priority.
2485   void
2486   set_priority(int priority);
2487
2488   // Return the bindings.
2489   Bindings*
2490   bindings()
2491   { return this->bindings_; }
2492
2493   // Whether some symbol from the package was used.
2494   bool
2495   used() const
2496   { return this->used_; }
2497
2498   // Note that some symbol from this package was used.
2499   void
2500   set_used() const
2501   { this->used_ = true; }
2502
2503   // Clear the used field for the next file.
2504   void
2505   clear_used()
2506   { this->used_ = false; }
2507
2508   // Whether this package was imported in the current file.
2509   bool
2510   is_imported() const
2511   { return this->is_imported_; }
2512
2513   // Note that this package was imported in the current file.
2514   void
2515   set_is_imported()
2516   { this->is_imported_ = true; }
2517
2518   // Clear the imported field for the next file.
2519   void
2520   clear_is_imported()
2521   { this->is_imported_ = false; }
2522
2523   // Whether this package was imported with a name of "_".
2524   bool
2525   uses_sink_alias() const
2526   { return this->uses_sink_alias_; }
2527
2528   // Note that this package was imported with a name of "_".
2529   void
2530   set_uses_sink_alias()
2531   { this->uses_sink_alias_ = true; }
2532
2533   // Clear the sink alias field for the next file.
2534   void
2535   clear_uses_sink_alias()
2536   { this->uses_sink_alias_ = false; }
2537
2538   // Look up a name in the package.  Returns NULL if the name is not
2539   // found.
2540   Named_object*
2541   lookup(const std::string& name) const
2542   { return this->bindings_->lookup(name); }
2543
2544   // Set the name of the package.
2545   void
2546   set_package_name(const std::string& name, Location);
2547
2548   // Set the location of the package.  This is used to record the most
2549   // recent import location.
2550   void
2551   set_location(Location location)
2552   { this->location_ = location; }
2553
2554   // Add a constant to the package.
2555   Named_object*
2556   add_constant(const Typed_identifier& tid, Expression* expr)
2557   { return this->bindings_->add_constant(tid, this, expr, 0); }
2558
2559   // Add a type to the package.
2560   Named_object*
2561   add_type(const std::string& name, Type* type, Location location)
2562   { return this->bindings_->add_type(name, this, type, location); }
2563
2564   // Add a type declaration to the package.
2565   Named_object*
2566   add_type_declaration(const std::string& name, Location location)
2567   { return this->bindings_->add_type_declaration(name, this, location); }
2568
2569   // Add a variable to the package.
2570   Named_object*
2571   add_variable(const std::string& name, Variable* variable)
2572   { return this->bindings_->add_variable(name, this, variable); }
2573
2574   // Add a function declaration to the package.
2575   Named_object*
2576   add_function_declaration(const std::string& name, Function_type* type,
2577                            Location loc)
2578   { return this->bindings_->add_function_declaration(name, this, type, loc); }
2579
2580   // Determine types of constants.
2581   void
2582   determine_types();
2583
2584  private:
2585   // The package path for type reflection data.
2586   std::string pkgpath_;
2587   // The package path for symbol names.
2588   std::string pkgpath_symbol_;
2589   // The name that this package uses in the package clause.  This may
2590   // be the empty string if it is not yet known.
2591   std::string package_name_;
2592   // The names in this package.
2593   Bindings* bindings_;
2594   // The priority of this package.  A package has a priority higher
2595   // than the priority of all of the packages that it imports.  This
2596   // is used to run init functions in the right order.
2597   int priority_;
2598   // The location of the import statement.
2599   Location location_;
2600   // True if some name from this package was used.  This is mutable
2601   // because we can use a package even if we have a const pointer to
2602   // it.
2603   mutable bool used_;
2604   // True if this package was imported in the current file.
2605   bool is_imported_;
2606   // True if this package was imported with a name of "_".
2607   bool uses_sink_alias_;
2608 };
2609
2610 // Return codes for the traversal functions.  This is not an enum
2611 // because we want to be able to declare traversal functions in other
2612 // header files without including this one.
2613
2614 // Continue traversal as usual.
2615 const int TRAVERSE_CONTINUE = -1;
2616
2617 // Exit traversal.
2618 const int TRAVERSE_EXIT = 0;
2619
2620 // Continue traversal, but skip components of the current object.
2621 // E.g., if this is returned by Traverse::statement, we do not
2622 // traverse the expressions in the statement even if
2623 // traverse_expressions is set in the traverse_mask.
2624 const int TRAVERSE_SKIP_COMPONENTS = 1;
2625
2626 // This class is used when traversing the parse tree.  The caller uses
2627 // a subclass which overrides functions as desired.
2628
2629 class Traverse
2630 {
2631  public:
2632   // These bitmasks say what to traverse.
2633   static const unsigned int traverse_variables =    0x1;
2634   static const unsigned int traverse_constants =    0x2;
2635   static const unsigned int traverse_functions =    0x4;
2636   static const unsigned int traverse_blocks =       0x8;
2637   static const unsigned int traverse_statements =  0x10;
2638   static const unsigned int traverse_expressions = 0x20;
2639   static const unsigned int traverse_types =       0x40;
2640
2641   Traverse(unsigned int traverse_mask)
2642     : traverse_mask_(traverse_mask), types_seen_(NULL), expressions_seen_(NULL)
2643   { }
2644
2645   virtual ~Traverse();
2646
2647   // The bitmask of what to traverse.
2648   unsigned int
2649   traverse_mask() const
2650   { return this->traverse_mask_; }
2651
2652   // Record that we are going to traverse a type.  This returns true
2653   // if the type has already been seen in this traversal.  This is
2654   // required because types, unlike expressions, can form a circular
2655   // graph.
2656   bool
2657   remember_type(const Type*);
2658
2659   // Record that we are going to see an expression.  This returns true
2660   // if the expression has already been seen in this traversal.  This
2661   // is only needed for cases where multiple expressions can point to
2662   // a single one.
2663   bool
2664   remember_expression(const Expression*);
2665
2666   // These functions return one of the TRAVERSE codes defined above.
2667
2668   // If traverse_variables is set in the mask, this is called for
2669   // every variable in the tree.
2670   virtual int
2671   variable(Named_object*);
2672
2673   // If traverse_constants is set in the mask, this is called for
2674   // every named constant in the tree.  The bool parameter is true for
2675   // a global constant.
2676   virtual int
2677   constant(Named_object*, bool);
2678
2679   // If traverse_functions is set in the mask, this is called for
2680   // every function in the tree.
2681   virtual int
2682   function(Named_object*);
2683
2684   // If traverse_blocks is set in the mask, this is called for every
2685   // block in the tree.
2686   virtual int
2687   block(Block*);
2688
2689   // If traverse_statements is set in the mask, this is called for
2690   // every statement in the tree.
2691   virtual int
2692   statement(Block*, size_t* index, Statement*);
2693
2694   // If traverse_expressions is set in the mask, this is called for
2695   // every expression in the tree.
2696   virtual int
2697   expression(Expression**);
2698
2699   // If traverse_types is set in the mask, this is called for every
2700   // type in the tree.
2701   virtual int
2702   type(Type*);
2703
2704  private:
2705   // A hash table for types we have seen during this traversal.  Note
2706   // that this uses the default hash functions for pointers rather
2707   // than Type_hash_identical and Type_identical.  This is because for
2708   // traversal we care about seeing a specific type structure.  If
2709   // there are two separate instances of identical types, we want to
2710   // traverse both.
2711   typedef Unordered_set(const Type*) Types_seen;
2712
2713   typedef Unordered_set(const Expression*) Expressions_seen;
2714
2715   // Bitmask of what sort of objects to traverse.
2716   unsigned int traverse_mask_;
2717   // Types which have been seen in this traversal.
2718   Types_seen* types_seen_;
2719   // Expressions which have been seen in this traversal.
2720   Expressions_seen* expressions_seen_;
2721 };
2722
2723 // A class which makes it easier to insert new statements before the
2724 // current statement during a traversal.
2725
2726 class Statement_inserter
2727 {
2728  public:
2729   // Empty constructor.
2730   Statement_inserter()
2731     : block_(NULL), pindex_(NULL), gogo_(NULL), var_(NULL)
2732   { }
2733
2734   // Constructor for a statement in a block.
2735   Statement_inserter(Block* block, size_t *pindex)
2736     : block_(block), pindex_(pindex), gogo_(NULL), var_(NULL)
2737   { }
2738
2739   // Constructor for a global variable.
2740   Statement_inserter(Gogo* gogo, Variable* var)
2741     : block_(NULL), pindex_(NULL), gogo_(gogo), var_(var)
2742   { go_assert(var->is_global()); }
2743
2744   // We use the default copy constructor and assignment operator.
2745
2746   // Insert S before the statement we are traversing, or before the
2747   // initialization expression of a global variable.
2748   void
2749   insert(Statement* s);
2750
2751  private:
2752   // The block that the statement is in.
2753   Block* block_;
2754   // The index of the statement that we are traversing.
2755   size_t* pindex_;
2756   // The IR, needed when looking at an initializer expression for a
2757   // global variable.
2758   Gogo* gogo_;
2759   // The global variable, when looking at an initializer expression.
2760   Variable* var_;
2761 };
2762
2763 // When translating the gogo IR into the backend data structure, this
2764 // is the context we pass down the blocks and statements.
2765
2766 class Translate_context
2767 {
2768  public:
2769   Translate_context(Gogo* gogo, Named_object* function, Block* block,
2770                     Bblock* bblock)
2771     : gogo_(gogo), backend_(gogo->backend()), function_(function),
2772       block_(block), bblock_(bblock), is_const_(false)
2773   { }
2774
2775   // Accessors.
2776
2777   Gogo*
2778   gogo()
2779   { return this->gogo_; }
2780
2781   Backend*
2782   backend()
2783   { return this->backend_; }
2784
2785   Named_object*
2786   function()
2787   { return this->function_; }
2788
2789   Block*
2790   block()
2791   { return this->block_; }
2792
2793   Bblock*
2794   bblock()
2795   { return this->bblock_; }
2796
2797   bool
2798   is_const()
2799   { return this->is_const_; }
2800
2801   // Make a constant context.
2802   void
2803   set_is_const()
2804   { this->is_const_ = true; }
2805
2806  private:
2807   // The IR for the entire compilation unit.
2808   Gogo* gogo_;
2809   // The generator for the backend data structures.
2810   Backend* backend_;
2811   // The function we are currently translating.  NULL if not in a
2812   // function, e.g., the initializer of a global variable.
2813   Named_object* function_;
2814   // The block we are currently translating.  NULL if not in a
2815   // function.
2816   Block *block_;
2817   // The backend representation of the current block.  NULL if block_
2818   // is NULL.
2819   Bblock* bblock_;
2820   // Whether this is being evaluated in a constant context.  This is
2821   // used for type descriptor initializers.
2822   bool is_const_;
2823 };
2824
2825 // Runtime error codes.  These must match the values in
2826 // libgo/runtime/go-runtime-error.c.
2827
2828 // Slice index out of bounds: negative or larger than the length of
2829 // the slice.
2830 static const int RUNTIME_ERROR_SLICE_INDEX_OUT_OF_BOUNDS = 0;
2831
2832 // Array index out of bounds.
2833 static const int RUNTIME_ERROR_ARRAY_INDEX_OUT_OF_BOUNDS = 1;
2834
2835 // String index out of bounds.
2836 static const int RUNTIME_ERROR_STRING_INDEX_OUT_OF_BOUNDS = 2;
2837
2838 // Slice slice out of bounds: negative or larger than the length of
2839 // the slice or high bound less than low bound.
2840 static const int RUNTIME_ERROR_SLICE_SLICE_OUT_OF_BOUNDS = 3;
2841
2842 // Array slice out of bounds.
2843 static const int RUNTIME_ERROR_ARRAY_SLICE_OUT_OF_BOUNDS = 4;
2844
2845 // String slice out of bounds.
2846 static const int RUNTIME_ERROR_STRING_SLICE_OUT_OF_BOUNDS = 5;
2847
2848 // Dereference of nil pointer.  This is used when there is a
2849 // dereference of a pointer to a very large struct or array, to ensure
2850 // that a gigantic array is not used a proxy to access random memory
2851 // locations.
2852 static const int RUNTIME_ERROR_NIL_DEREFERENCE = 6;
2853
2854 // Slice length or capacity out of bounds in make: negative or
2855 // overflow or length greater than capacity.
2856 static const int RUNTIME_ERROR_MAKE_SLICE_OUT_OF_BOUNDS = 7;
2857
2858 // Map capacity out of bounds in make: negative or overflow.
2859 static const int RUNTIME_ERROR_MAKE_MAP_OUT_OF_BOUNDS = 8;
2860
2861 // Channel capacity out of bounds in make: negative or overflow.
2862 static const int RUNTIME_ERROR_MAKE_CHAN_OUT_OF_BOUNDS = 9;
2863
2864 // Division by zero.
2865 static const int RUNTIME_ERROR_DIVISION_BY_ZERO = 10;
2866
2867 // This is used by some of the langhooks.
2868 extern Gogo* go_get_gogo();
2869
2870 // Whether we have seen any errors.  FIXME: Replace with a backend
2871 // interface.
2872 extern bool saw_errors();
2873
2874 #endif // !defined(GO_GOGO_H)