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_equal;
16 class Typed_identifier;
17 class Typed_identifier_list;
18 class Function_type;
19 class Expression;
20 class Expression_list;
21 class Statement;
22 class Temporary_statement;
23 class Block;
24 class Function;
25 class Bindings;
26 class Bindings_snapshot;
27 class Package;
28 class Variable;
29 class Pointer_type;
30 class Struct_type;
31 class Struct_field;
32 class Struct_field_list;
33 class Array_type;
34 class Map_type;
35 class Channel_type;
36 class Interface_type;
37 class Named_type;
38 class Forward_declaration_type;
39 class Named_object;
40 class Label;
41 class Translate_context;
42 class Backend;
43 class Export;
44 class Export_function_body;
45 class Import;
46 class Import_function_body;
47 class Bexpression;
48 class Btype;
49 class Bstatement;
50 class Bblock;
51 class Bvariable;
52 class Blabel;
53 class Bfunction;
54 class Escape_context;
55 class Node;
56 
57 // This file declares the basic classes used to hold the internal
58 // representation of Go which is built by the parser.
59 
60 // An initialization function for an imported package.  This is a
61 // magic function which initializes variables and runs the "init"
62 // function.
63 
64 class Import_init
65 {
66  public:
Import_init(const std::string & package_name,const std::string & init_name,int priority)67   Import_init(const std::string& package_name, const std::string& init_name,
68 	      int priority)
69     : package_name_(package_name), init_name_(init_name), priority_(priority)
70   { }
71 
72   // The name of the package being imported.
73   const std::string&
package_name()74   package_name() const
75   { return this->package_name_; }
76 
77   // The name of the package's init function.
78   const std::string&
init_name()79   init_name() const
80   { return this->init_name_; }
81 
82   // Older V1 export data uses a priority scheme to order
83   // initialization functions; functions with a lower priority number
84   // must be run first. This value will be set to -1 for current
85   // generation objects, and will take on a non-negative value only
86   // when importing a V1-vintage object.
87   int
priority()88   priority() const
89   { return this->priority_; }
90 
91   // Reset priority.
92   void
set_priority(int new_priority)93   set_priority(int new_priority)
94   { this->priority_ = new_priority; }
95 
96   // Record the fact that some other init fcn must be run before this init fcn.
97   void
record_precursor_fcn(std::string init_fcn_name)98   record_precursor_fcn(std::string init_fcn_name)
99   { this->precursor_functions_.insert(init_fcn_name); }
100 
101   // Return the list of precursor fcns for this fcn (must be run before it).
102   const std::set<std::string>&
precursors()103   precursors() const
104   { return this->precursor_functions_; }
105 
106  private:
107   // The name of the package being imported.
108   std::string package_name_;
109   // The name of the package's init function.
110   std::string init_name_;
111   // Names of init functions that must be run before this fcn.
112   std::set<std::string> precursor_functions_;
113   // Priority for this function. See note above on obsolescence.
114   int priority_;
115 };
116 
117 // For sorting purposes.
118 
119 struct Import_init_lt {
operatorImport_init_lt120   bool operator()(const Import_init* i1, const Import_init* i2) const
121   {
122     return i1->init_name() < i2->init_name();
123   }
124 };
125 
126 // Set of import init objects.
127 class Import_init_set : public std::set<Import_init*, Import_init_lt> {
128 };
129 
130 inline bool
priority_compare(const Import_init * i1,const Import_init * i2)131 priority_compare(const Import_init* i1, const Import_init* i2)
132 {
133   if (i1->priority() < i2->priority())
134     return true;
135   if (i1->priority() > i2->priority())
136     return false;
137   if (i1->package_name() != i2->package_name())
138     return i1->package_name() < i2->package_name();
139   return i1->init_name() < i2->init_name();
140 }
141 
142 // The holder for the internal representation of the entire
143 // compilation unit.
144 
145 class Gogo
146 {
147  public:
148   // Create the IR, passing in the sizes of the types "int" and
149   // "uintptr" in bits.
150   Gogo(Backend* backend, Linemap *linemap, int int_type_size, int pointer_size);
151 
152   // Get the backend generator.
153   Backend*
backend()154   backend()
155   { return this->backend_; }
156 
157   // Get the Location generator.
158   Linemap*
linemap()159   linemap()
160   { return this->linemap_; }
161 
162   // Get the package name.
163   const std::string&
164   package_name() const;
165 
166   // Set the package name.
167   void
168   set_package_name(const std::string&, Location);
169 
170   // Return whether this is the "main" package.
171   bool
172   is_main_package() const;
173 
174   // If necessary, adjust the name to use for a hidden symbol.  We add
175   // the package name, so that hidden symbols in different packages do
176   // not collide.
177   std::string
pack_hidden_name(const std::string & name,bool is_exported)178   pack_hidden_name(const std::string& name, bool is_exported) const
179   {
180     return (is_exported
181 	    ? name
182 	    : '.' + this->pkgpath() + '.' + name);
183   }
184 
185   // Unpack a name which may have been hidden.  Returns the
186   // user-visible name of the object.
187   static std::string
unpack_hidden_name(const std::string & name)188   unpack_hidden_name(const std::string& name)
189   { return name[0] != '.' ? name : name.substr(name.rfind('.') + 1); }
190 
191   // Return whether a possibly packed name is hidden.
192   static bool
is_hidden_name(const std::string & name)193   is_hidden_name(const std::string& name)
194   { return name[0] == '.'; }
195 
196   // Return the package path of a hidden name.
197   static std::string
hidden_name_pkgpath(const std::string & name)198   hidden_name_pkgpath(const std::string& name)
199   {
200     go_assert(Gogo::is_hidden_name(name));
201     return name.substr(1, name.rfind('.') - 1);
202   }
203 
204   // Given a name which may or may not have been hidden, append the
205   // appropriate version of the name to the result string.
206   static void
207   append_possibly_hidden_name(std::string *result, const std::string& name);
208 
209   // Given a name which may or may not have been hidden, return the
210   // name to use in an error message.
211   static std::string
212   message_name(const std::string& name);
213 
214   // Return whether a name is the blank identifier _.
215   static bool
is_sink_name(const std::string & name)216   is_sink_name(const std::string& name)
217   {
218     return (name[0] == '.'
219 	    && name[name.length() - 1] == '_'
220 	    && name[name.length() - 2] == '.');
221   }
222 
223   // Helper used when adding parameters (including receiver param) to the
224   // bindings of a function. If the specified parameter name is empty or
225   // corresponds to the sink name, param name is replaced with a new unique
226   // name. PNAME is the address of a string containing the parameter variable
227   // name to be checked/updated; TAG is a descriptive tag to be used in
228   // manufacturing the new unique name, and COUNT is the address of a counter
229   // holding the number of params renamed so far with the tag in question.
230   static void
231   rename_if_empty(std::string* pname, const char* tag, unsigned* count);
232 
233   // Convert a pkgpath into a string suitable for a symbol
234   static std::string
235   pkgpath_for_symbol(const std::string& pkgpath);
236 
237   // Compute a hash code for a string, given a seed.
238   static unsigned int
239   hash_string(const std::string&, unsigned int);
240 
241   // Return the package path to use for reflect.Type.PkgPath.
242   const std::string&
243   pkgpath() const;
244 
245   // Return the package path to use for a symbol name.
246   const std::string&
247   pkgpath_symbol() const;
248 
249   // Set the package path from a command line option.
250   void
251   set_pkgpath(const std::string&);
252 
253   // Set the prefix from a command line option.
254   void
255   set_prefix(const std::string&);
256 
257   // Return whether pkgpath was set from a command line option.
258   bool
pkgpath_from_option()259   pkgpath_from_option() const
260   { return this->pkgpath_from_option_; }
261 
262   // Return the relative import path as set from the command line.
263   // Returns an empty string if it was not set.
264   const std::string&
relative_import_path()265   relative_import_path() const
266   { return this->relative_import_path_; }
267 
268   // Set the relative import path from a command line option.
269   void
set_relative_import_path(const std::string & s)270   set_relative_import_path(const std::string& s)
271   { this->relative_import_path_ = s; }
272 
273   // Set the C header file to write.  This is used for the runtime
274   // package.
275   void
set_c_header(const std::string & s)276   set_c_header(const std::string& s)
277   { this->c_header_ = s; }
278 
279   // Return whether to check for division by zero in binary operations.
280   bool
check_divide_by_zero()281   check_divide_by_zero() const
282   { return this->check_divide_by_zero_; }
283 
284   // Set the option to check division by zero from a command line option.
285   void
set_check_divide_by_zero(bool b)286   set_check_divide_by_zero(bool b)
287   { this->check_divide_by_zero_ = b; }
288 
289   // Return whether to check for division overflow in binary operations.
290   bool
check_divide_overflow()291   check_divide_overflow() const
292   { return this->check_divide_overflow_; }
293 
294   // Set the option to check division overflow from a command line option.
295   void
set_check_divide_overflow(bool b)296   set_check_divide_overflow(bool b)
297   { this->check_divide_overflow_ = b; }
298 
299   // Return whether we are compiling the runtime package.
300   bool
compiling_runtime()301   compiling_runtime() const
302   { return this->compiling_runtime_; }
303 
304   // Set whether we are compiling the runtime package.
305   void
set_compiling_runtime(bool b)306   set_compiling_runtime(bool b)
307   { this->compiling_runtime_ = b; }
308 
309   // Return the level of escape analysis debug information to emit.
310   int
debug_escape_level()311   debug_escape_level() const
312   { return this->debug_escape_level_; }
313 
314   // Set the level of escape analysis debugging from a command line option.
315   void
set_debug_escape_level(int level)316   set_debug_escape_level(int level)
317   { this->debug_escape_level_ = level; }
318 
319   // Return the hash for debug escape analysis.
320   std::string
debug_escape_hash()321   debug_escape_hash() const
322   { return this->debug_escape_hash_; }
323 
324   // Set the hash value for debug escape analysis.
325   void
set_debug_escape_hash(const std::string & s)326   set_debug_escape_hash(const std::string& s)
327   { this->debug_escape_hash_ = s; }
328 
329   // Return the size threshold used to determine whether to issue
330   // a nil-check for a given pointer dereference. A threshold of -1
331   // implies that all potentially faulting dereference ops should
332   // be nil-checked. A positive threshold of N implies that a deref
333   // of *P where P has size less than N doesn't need a nil check.
334   int64_t
nil_check_size_threshold()335   nil_check_size_threshold() const
336   { return this->nil_check_size_threshold_; }
337 
338   // Set the nil-check size threshold, as described above.
339   void
set_nil_check_size_threshold(int64_t bytes)340   set_nil_check_size_threshold(int64_t bytes)
341   { this->nil_check_size_threshold_ = bytes; }
342 
343   // Import a package.  FILENAME is the file name argument, LOCAL_NAME
344   // is the local name to give to the package.  If LOCAL_NAME is empty
345   // the declarations are added to the global scope.
346   void
347   import_package(const std::string& filename, const std::string& local_name,
348 		 bool is_local_name_exported, bool must_exist, Location);
349 
350   // Whether we are the global binding level.
351   bool
352   in_global_scope() const;
353 
354   // Look up a name in the current binding contours.
355   Named_object*
356   lookup(const std::string&, Named_object** pfunction) const;
357 
358   // Look up a name in the current block.
359   Named_object*
360   lookup_in_block(const std::string&) const;
361 
362   // Look up a name in the global namespace--the universal scope.
363   Named_object*
364   lookup_global(const char*) const;
365 
366   // Add a new imported package.  REAL_NAME is the real name of the
367   // package.  ALIAS is the alias of the package; this may be the same
368   // as REAL_NAME.  This sets *PADD_TO_GLOBALS if symbols added to
369   // this package should be added to the global namespace; this is
370   // true if the alias is ".".  LOCATION is the location of the import
371   // statement.  This returns the new package, or NULL on error.
372   Package*
373   add_imported_package(const std::string& real_name, const std::string& alias,
374 		       bool is_alias_exported,
375 		       const std::string& pkgpath,
376 		       const std::string& pkgpath_symbol,
377 		       Location location,
378 		       bool* padd_to_globals);
379 
380   // Register a package.  This package may or may not be imported.
381   // This returns the Package structure for the package, creating if
382   // it necessary.
383   Package*
384   register_package(const std::string& pkgpath,
385 		   const std::string& pkgpath_symbol, Location);
386 
387   // Look up a package by pkgpath, and return its pkgpath_symbol.
388   std::string
389   pkgpath_symbol_for_package(const std::string&);
390 
391   // Start compiling a function.  ADD_METHOD_TO_TYPE is true if a
392   // method function should be added to the type of its receiver.
393   Named_object*
394   start_function(const std::string& name, Function_type* type,
395 		 bool add_method_to_type, Location);
396 
397   // Finish compiling a function.
398   void
399   finish_function(Location);
400 
401   // Return the current function.
402   Named_object*
403   current_function() const;
404 
405   // Return the current block.
406   Block*
407   current_block();
408 
409   // Start a new block.  This is not initially associated with a
410   // function.
411   void
412   start_block(Location);
413 
414   // Finish the current block and return it.
415   Block*
416   finish_block(Location);
417 
418   // Declare an erroneous name.  This is used to avoid knock-on errors
419   // after a parsing error.
420   Named_object*
421   add_erroneous_name(const std::string& name);
422 
423   // Declare an unknown name.  This is used while parsing.  The name
424   // must be resolved by the end of the parse.  Unknown names are
425   // always added at the package level.
426   Named_object*
427   add_unknown_name(const std::string& name, Location);
428 
429   // Declare a function.
430   Named_object*
431   declare_function(const std::string&, Function_type*, Location);
432 
433   // Declare a function at the package level.  This is used for
434   // functions generated for a type.
435   Named_object*
436   declare_package_function(const std::string&, Function_type*, Location);
437 
438   // Add a function declaration to the list of functions we may want
439   // to inline.
440   void
441   add_imported_inlinable_function(Named_object*);
442 
443   // Add a function to the list of functions that we do want to
444   // inline.
445   void
add_imported_inline_function(Named_object * no)446   add_imported_inline_function(Named_object* no)
447   { this->imported_inline_functions_.push_back(no); }
448 
449   // Add a label.
450   Label*
451   add_label_definition(const std::string&, Location);
452 
453   // Add a label reference.  ISSUE_GOTO_ERRORS is true if we should
454   // report errors for a goto from the current location to the label
455   // location.
456   Label*
457   add_label_reference(const std::string&, Location,
458 		      bool issue_goto_errors);
459 
460   // An analysis set is a list of functions paired with a boolean that indicates
461   // whether the list of functions are recursive.
462   typedef std::pair<std::vector<Named_object*>, bool> Analysis_set;
463 
464   // Add a GROUP of possibly RECURSIVE functions to the Analysis_set for this
465   // package.
466   void
add_analysis_set(const std::vector<Named_object * > & group,bool recursive)467   add_analysis_set(const std::vector<Named_object*>& group, bool recursive)
468   { this->analysis_sets_.push_back(std::make_pair(group, recursive)); }
469 
470   // Return a snapshot of the current binding state.
471   Bindings_snapshot*
472   bindings_snapshot(Location);
473 
474   // Add a statement to the current block.
475   void
476   add_statement(Statement*);
477 
478   // Add a block to the current block.
479   void
480   add_block(Block*, Location);
481 
482   // Add a constant.
483   Named_object*
484   add_constant(const Typed_identifier&, Expression*, int iota_value);
485 
486   // Add a type.
487   void
488   add_type(const std::string&, Type*, Location);
489 
490   // Add a named type.  This is used for builtin types, and to add an
491   // imported type to the global scope.
492   void
493   add_named_type(Named_type*);
494 
495   // Declare a type.
496   Named_object*
497   declare_type(const std::string&, Location);
498 
499   // Declare a type at the package level.  This is used when the
500   // parser sees an unknown name where a type name is required.
501   Named_object*
502   declare_package_type(const std::string&, Location);
503 
504   // Define a type which was already declared.
505   void
506   define_type(Named_object*, Named_type*);
507 
508   // Add a variable.
509   Named_object*
510   add_variable(const std::string&, Variable*);
511 
512   // Add a sink--a reference to the blank identifier _.
513   Named_object*
514   add_sink();
515 
516   // Add a type which needs to be verified.  This is used for sink
517   // types, just to give appropriate error messages.
518   void
519   add_type_to_verify(Type* type);
520 
521   // Add a named object to the current namespace.  This is used for
522   // import . "package".
523   void
524   add_dot_import_object(Named_object*);
525 
526   // Add an identifier to the list of names seen in the file block.
527   void
add_file_block_name(const std::string & name,Location location)528   add_file_block_name(const std::string& name, Location location)
529   { this->file_block_names_[name] = location; }
530 
531   // Add a linkname, from the go:linkname compiler directive.  This
532   // changes the externally visible name of go_name to be ext_name.
533   void
534   add_linkname(const std::string& go_name, bool is_exported,
535 	       const std::string& ext_name, Location location);
536 
537   // Mark all local variables in current bindings as used.  This is
538   // used when there is a parse error to avoid useless errors.
539   void
540   mark_locals_used();
541 
542   // Note that we've seen an interface type.  This is used to build
543   // all required interface method tables.
544   void
545   record_interface_type(Interface_type*);
546 
547   // Note that we need an initialization function.
548   void
set_need_init_fn()549   set_need_init_fn()
550   { this->need_init_fn_ = true; }
551 
552   // Return whether the current file imported the unsafe package.
553   bool
current_file_imported_unsafe()554   current_file_imported_unsafe() const
555   { return this->current_file_imported_unsafe_; }
556 
557   // Clear out all names in file scope.  This is called when we start
558   // parsing a new file.
559   void
560   clear_file_scope();
561 
562   // Record that VAR1 must be initialized after VAR2.  This is used
563   // when VAR2 does not appear in VAR1's INIT or PREINIT.
564   void
record_var_depends_on(Variable * var1,Named_object * var2)565   record_var_depends_on(Variable* var1, Named_object* var2)
566   {
567     go_assert(this->var_deps_.find(var1) == this->var_deps_.end());
568     this->var_deps_[var1] = var2;
569   }
570 
571   // Return the variable that VAR depends on, or NULL if none.
572   Named_object*
var_depends_on(Variable * var)573   var_depends_on(Variable* var) const
574   {
575     Var_deps::const_iterator p = this->var_deps_.find(var);
576     return p != this->var_deps_.end() ? p->second : NULL;
577   }
578 
579   // Queue up a type-specific function to be written out.  This is
580   // used when a type-specific function is needed when not at the top
581   // level.
582   void
583   queue_specific_type_function(Type* type, Named_type* name, int64_t size,
584 			       const std::string& hash_name,
585 			       Function_type* hash_fntype,
586 			       const std::string& equal_name,
587 			       Function_type* equal_fntype);
588 
589   // Write out queued specific type functions.
590   void
591   write_specific_type_functions();
592 
593   // Whether we are done writing out specific type functions.
594   bool
specific_type_functions_are_written()595   specific_type_functions_are_written() const
596   { return this->specific_type_functions_are_written_; }
597 
598   // Add a pointer that needs to be added to the list of objects
599   // traversed by the garbage collector.  This should be an expression
600   // of pointer type that points to static storage.  It's not
601   // necessary to add global variables to this list, just global
602   // variable initializers that would otherwise not be seen.
603   void
add_gc_root(Expression * expr)604   add_gc_root(Expression* expr)
605   {
606     this->set_need_init_fn();
607     this->gc_roots_.push_back(expr);
608   }
609 
610   // Traverse the tree.  See the Traverse class.
611   void
612   traverse(Traverse*);
613 
614   // Define the predeclared global names.
615   void
616   define_global_names();
617 
618   // Verify and complete all types.
619   void
620   verify_types();
621 
622   // Lower the parse tree.
623   void
624   lower_parse_tree();
625 
626   // Lower all the statements in a block.
627   void
628   lower_block(Named_object* function, Block*);
629 
630   // Lower an expression.
631   void
632   lower_expression(Named_object* function, Statement_inserter*, Expression**);
633 
634   // Lower a constant.
635   void
636   lower_constant(Named_object*);
637 
638   // Flatten all the statements in a block.
639   void
640   flatten_block(Named_object* function, Block*);
641 
642   // Flatten an expression.
643   void
644   flatten_expression(Named_object* function, Statement_inserter*, Expression**);
645 
646   // Create all necessary function descriptors.
647   void
648   create_function_descriptors();
649 
650   // Finalize the method lists and build stub methods for named types.
651   void
652   finalize_methods();
653 
654   // Finalize the method list for one type.
655   void
656   finalize_methods_for_type(Type*);
657 
658   // Work out the types to use for unspecified variables and
659   // constants.
660   void
661   determine_types();
662 
663   // Type check the program.
664   void
665   check_types();
666 
667   // Check the types in a single block.  This is used for complicated
668   // go statements.
669   void
670   check_types_in_block(Block*);
671 
672   // Check for return statements.
673   void
674   check_return_statements();
675 
676   // Analyze the program flow for escape information.
677   void
678   analyze_escape();
679 
680   // Discover the groups of possibly recursive functions in this package.
681   void
682   discover_analysis_sets();
683 
684   // Build a connectivity graph between the objects in each analyzed function.
685   void
686   assign_connectivity(Escape_context*, Named_object*);
687 
688   // Traverse the objects in the connecitivty graph from the sink, adjusting the
689   // escape levels of each object.
690   void
691   propagate_escape(Escape_context*, Node*);
692 
693   // Add notes about the escape level of a function's input and output
694   // parameters for exporting and importing top level functions.
695   void
696   tag_function(Escape_context*, Named_object*);
697 
698   // Reclaim memory of escape analysis Nodes.
699   void
700   reclaim_escape_nodes();
701 
702   // Do all exports.
703   void
704   do_exports();
705 
706   // Add an import control function for an imported package to the
707   // list.
708   void
709   add_import_init_fn(const std::string& package_name,
710 		     const std::string& init_name, int prio);
711 
712   // Return the Import_init for a given init name.
713   Import_init*
714   lookup_init(const std::string& init_name);
715 
716   // Turn short-cut operators (&&, ||) into explicit if statements.
717   void
718   remove_shortcuts();
719 
720   // Use temporary variables to force order of evaluation.
721   void
722   order_evaluations();
723 
724   // Add write barriers as needed.
725   void
726   add_write_barriers();
727 
728   // Return whether an assignment that sets LHS to RHS needs a write
729   // barrier.
730   bool
731   assign_needs_write_barrier(Expression* lhs);
732 
733   // Return an assignment that sets LHS to RHS using a write barrier.
734   // This returns an if statement that checks whether write barriers
735   // are enabled.  If not, it does LHS = RHS, otherwise it calls the
736   // appropriate write barrier function.
737   Statement*
738   assign_with_write_barrier(Function*, Block*, Statement_inserter*,
739 			    Expression* lhs, Expression* rhs, Location);
740 
741   // Flatten parse tree.
742   void
743   flatten();
744 
745   // Build thunks for functions which call recover.
746   void
747   build_recover_thunks();
748 
749   // Return a declaration for __builtin_return_address or
750   // __builtin_dwarf_cfa.
751   static Named_object*
752   declare_builtin_rf_address(const char* name, bool hasarg);
753 
754   // Simplify statements which might use thunks: go and defer
755   // statements.
756   void
757   simplify_thunk_statements();
758 
759   // Dump AST if -fgo-dump-ast is set.
760   void
761   dump_ast(const char* basename);
762 
763   // Dump Call Graph if -fgo-dump-calls is set.
764   void
765   dump_call_graph(const char* basename);
766 
767   // Dump Connection Graphs if -fgo-dump-connections is set.
768   void
769   dump_connection_graphs(const char* basename);
770 
771   // Convert named types to the backend representation.
772   void
773   convert_named_types();
774 
775   // Convert named types in a list of bindings.
776   void
777   convert_named_types_in_bindings(Bindings*);
778 
779   // True if named types have been converted to the backend
780   // representation.
781   bool
named_types_are_converted()782   named_types_are_converted() const
783   { return this->named_types_are_converted_; }
784 
785   // Give an error if the initialization of VAR depends on itself.
786   void
787   check_self_dep(Named_object*);
788 
789   // Write out the global values.
790   void
791   write_globals();
792 
793   // Build a call to the runtime error function.
794   Expression*
795   runtime_error(int code, Location);
796 
797   // Build required interface method tables.
798   void
799   build_interface_method_tables();
800 
801   // Return an expression which allocates memory to hold values of type TYPE.
802   Expression*
803   allocate_memory(Type *type, Location);
804 
805   // Return the assembler name to use for an exported function, a
806   // method, or a function/method declaration.
807   std::string
808   function_asm_name(const std::string& go_name, const Package*,
809 		    const Type* receiver);
810 
811   // Return the name to use for a function descriptor.
812   std::string
813   function_descriptor_name(Named_object*);
814 
815   // Return the name to use for a generated stub method.
816   std::string
817   stub_method_name(const Package*, const std::string& method_name);
818 
819   // Return the names of the hash and equality functions for TYPE.
820   void
821   specific_type_function_names(const Type*, const Named_type*,
822 			       std::string* hash_name,
823 			       std::string* equal_name);
824 
825   // Return the assembler name to use for a global variable.
826   std::string
827   global_var_asm_name(const std::string& go_name, const Package*);
828 
829   // Return a name to use for an error case.  This should only be used
830   // after reporting an error, and is used to avoid useless knockon
831   // errors.
832   static std::string
833   erroneous_name();
834 
835   // Return whether the name indicates an error.
836   static bool
837   is_erroneous_name(const std::string&);
838 
839   // Return a name to use for a thunk function.  A thunk function is
840   // one we create during the compilation, for a go statement or a
841   // defer statement or a method expression.
842   std::string
843   thunk_name();
844 
845   // Return whether an object is a thunk.
846   static bool
847   is_thunk(const Named_object*);
848 
849   // Return the name to use for an init function.
850   std::string
851   init_function_name();
852 
853   // Return the name to use for a nested function.
854   std::string
855   nested_function_name(Named_object* enclosing);
856 
857   // Return the name to use for a sink funciton.
858   std::string
859   sink_function_name();
860 
861   // Return the name to use for an (erroneous) redefined function.
862   std::string
863   redefined_function_name();
864 
865   // Return the name for use for a recover thunk.
866   std::string
867   recover_thunk_name(const std::string& name, const Type* rtype);
868 
869   // Return the name to use for the GC root variable.
870   std::string
871   gc_root_name();
872 
873   // Return the name to use for a composite literal or string
874   // initializer.
875   std::string
876   initializer_name();
877 
878   // Return the name of the variable used to represent the zero value
879   // of a map.
880   std::string
881   map_zero_value_name();
882 
883   // Get the name of the magic initialization function.
884   const std::string&
885   get_init_fn_name();
886 
887   // Return the name for a type descriptor symbol.
888   std::string
889   type_descriptor_name(Type*, Named_type*);
890 
891   // Return the assembler name for the GC symbol for a type.
892   std::string
893   gc_symbol_name(Type*);
894 
895   // Return the assembler name for a ptrmask variable.
896   std::string
897   ptrmask_symbol_name(const std::string& ptrmask_sym_name);
898 
899   // Return the name to use for an interface method table.
900   std::string
901   interface_method_table_name(Interface_type*, Type*, bool is_pointer);
902 
903   // Return whether NAME is a special name that can not be passed to
904   // unpack_hidden_name.  This is needed because various special names
905   // use "..SUFFIX", but unpack_hidden_name just looks for '.'.
906   static bool
907   is_special_name(const std::string& name);
908 
909  private:
910   // During parsing, we keep a stack of functions.  Each function on
911   // the stack is one that we are currently parsing.  For each
912   // function, we keep track of the current stack of blocks.
913   struct Open_function
914   {
915     // The function.
916     Named_object* function;
917     // The stack of active blocks in the function.
918     std::vector<Block*> blocks;
919   };
920 
921   // The stack of functions.
922   typedef std::vector<Open_function> Open_functions;
923 
924   // Set up the built-in unsafe package.
925   void
926   import_unsafe(const std::string&, bool is_exported, Location);
927 
928   // Return the current binding contour.
929   Bindings*
930   current_bindings();
931 
932   const Bindings*
933   current_bindings() const;
934 
935   void
936   write_c_header();
937 
938   // Get the decl for the magic initialization function.
939   Named_object*
940   initialization_function_decl();
941 
942   // Create the magic initialization function.
943   Named_object*
944   create_initialization_function(Named_object* fndecl, Bstatement* code_stmt);
945 
946   // Initialize imported packages. BFUNCTION is the function
947   // into which the package init calls will be placed.
948   void
949   init_imports(std::vector<Bstatement*>&, Bfunction* bfunction);
950 
951   // Register variables with the garbage collector.
952   void
953   register_gc_vars(const std::vector<Named_object*>&,
954                    std::vector<Bstatement*>&,
955                    Bfunction* init_bfunction);
956 
957   void
958   propagate_writebarrierrec();
959 
960   Named_object*
961   write_barrier_variable();
962 
963   Statement*
964   check_write_barrier(Block*, Statement*, Statement*);
965 
966   // Type used to map import names to packages.
967   typedef std::map<std::string, Package*> Imports;
968 
969   // Type used to map package names to packages.
970   typedef std::map<std::string, Package*> Packages;
971 
972   // Type used to map variables to the function calls that set them.
973   // This is used for initialization dependency analysis.
974   typedef std::map<Variable*, Named_object*> Var_deps;
975 
976   // Type used to map identifiers in the file block to the location
977   // where they were defined.
978   typedef Unordered_map(std::string, Location) File_block_names;
979 
980   // Type used to queue writing a type specific function.
981   struct Specific_type_function
982   {
983     Type* type;
984     Named_type* name;
985     int64_t size;
986     std::string hash_name;
987     Function_type* hash_fntype;
988     std::string equal_name;
989     Function_type* equal_fntype;
990 
Specific_type_functionSpecific_type_function991     Specific_type_function(Type* atype, Named_type* aname, int64_t asize,
992 			   const std::string& ahash_name,
993 			   Function_type* ahash_fntype,
994 			   const std::string& aequal_name,
995 			   Function_type* aequal_fntype)
996       : type(atype), name(aname), size(asize), hash_name(ahash_name),
997 	hash_fntype(ahash_fntype), equal_name(aequal_name),
998 	equal_fntype(aequal_fntype)
999     { }
1000   };
1001 
1002   // Recompute init priorities.
1003   void
1004   recompute_init_priorities();
1005 
1006   // Recursive helper used by the routine above.
1007   void
1008   update_init_priority(Import_init* ii,
1009                        std::set<const Import_init *>* visited);
1010 
1011   // The backend generator.
1012   Backend* backend_;
1013   // The object used to keep track of file names and line numbers.
1014   Linemap* linemap_;
1015   // The package we are compiling.
1016   Package* package_;
1017   // The list of currently open functions during parsing.
1018   Open_functions functions_;
1019   // The global binding contour.  This includes the builtin functions
1020   // and the package we are compiling.
1021   Bindings* globals_;
1022   // The list of names we have seen in the file block.
1023   File_block_names file_block_names_;
1024   // Mapping from import file names to packages.
1025   Imports imports_;
1026   // Whether the magic unsafe package was imported.
1027   bool imported_unsafe_;
1028   // Whether the magic unsafe package was imported by the current file.
1029   bool current_file_imported_unsafe_;
1030   // Mapping from package names we have seen to packages.  This does
1031   // not include the package we are compiling.
1032   Packages packages_;
1033   // The functions named "init", if there are any.
1034   std::vector<Named_object*> init_functions_;
1035   // A mapping from variables to the function calls that initialize
1036   // them, if it is not stored in the variable's init or preinit.
1037   // This is used for dependency analysis.
1038   Var_deps var_deps_;
1039   // Whether we need a magic initialization function.
1040   bool need_init_fn_;
1041   // The name of the magic initialization function.
1042   std::string init_fn_name_;
1043   // A list of import control variables for packages that we import.
1044   Import_init_set imported_init_fns_;
1045   // The package path used for reflection data.
1046   std::string pkgpath_;
1047   // The package path to use for a symbol name.
1048   std::string pkgpath_symbol_;
1049   // The prefix to use for symbols, from the -fgo-prefix option.
1050   std::string prefix_;
1051   // Whether pkgpath_ has been set.
1052   bool pkgpath_set_;
1053   // Whether an explicit package path was set by -fgo-pkgpath.
1054   bool pkgpath_from_option_;
1055   // Whether an explicit prefix was set by -fgo-prefix.
1056   bool prefix_from_option_;
1057   // The relative import path, from the -fgo-relative-import-path
1058   // option.
1059   std::string relative_import_path_;
1060   // The C header file to write, from the -fgo-c-header option.
1061   std::string c_header_;
1062   // Whether or not to check for division by zero, from the
1063   // -fgo-check-divide-zero option.
1064   bool check_divide_by_zero_;
1065   // Whether or not to check for division overflow, from the
1066   // -fgo-check-divide-overflow option.
1067   bool check_divide_overflow_;
1068   // Whether we are compiling the runtime package, from the
1069   // -fgo-compiling-runtime option.
1070   bool compiling_runtime_;
1071   // The level of escape analysis debug information to emit, from the
1072   // -fgo-debug-escape option.
1073   int debug_escape_level_;
1074   // A hash value for debug escape analysis, from the
1075   // -fgo-debug-escape-hash option. The analysis is run only on
1076   // functions with names that hash to the matching value.
1077   std::string debug_escape_hash_;
1078   // Nil-check size threshhold.
1079   int64_t nil_check_size_threshold_;
1080   // A list of types to verify.
1081   std::vector<Type*> verify_types_;
1082   // A list of interface types defined while parsing.
1083   std::vector<Interface_type*> interface_types_;
1084   // Type specific functions to write out.
1085   std::vector<Specific_type_function*> specific_type_functions_;
1086   // Whether we are done writing out specific type functions.
1087   bool specific_type_functions_are_written_;
1088   // Whether named types have been converted.
1089   bool named_types_are_converted_;
1090   // A list containing groups of possibly mutually recursive functions to be
1091   // considered during escape analysis.
1092   std::vector<Analysis_set> analysis_sets_;
1093   // A list of objects to add to the GC roots.
1094   std::vector<Expression*> gc_roots_;
1095   // A list of function declarations with imported bodies that we may
1096   // want to inline.
1097   std::vector<Named_object*> imported_inlinable_functions_;
1098   // A list of functions that we want to inline.  These will be sent
1099   // to the backend.
1100   std::vector<Named_object*> imported_inline_functions_;
1101 };
1102 
1103 // A block of statements.
1104 
1105 class Block
1106 {
1107  public:
1108   Block(Block* enclosing, Location);
1109 
1110   // Return the enclosing block.
1111   const Block*
enclosing()1112   enclosing() const
1113   { return this->enclosing_; }
1114 
1115   // Return the bindings of the block.
1116   Bindings*
bindings()1117   bindings()
1118   { return this->bindings_; }
1119 
1120   const Bindings*
bindings()1121   bindings() const
1122   { return this->bindings_; }
1123 
1124   // Look at the block's statements.
1125   const std::vector<Statement*>*
statements()1126   statements() const
1127   { return &this->statements_; }
1128 
1129   // Return the start location.  This is normally the location of the
1130   // left curly brace which starts the block.
1131   Location
start_location()1132   start_location() const
1133   { return this->start_location_; }
1134 
1135   // Return the end location.  This is normally the location of the
1136   // right curly brace which ends the block.
1137   Location
end_location()1138   end_location() const
1139   { return this->end_location_; }
1140 
1141   // Add a statement to the block.
1142   void
1143   add_statement(Statement*);
1144 
1145   // Add a statement to the front of the block.
1146   void
1147   add_statement_at_front(Statement*);
1148 
1149   // Replace a statement in a block.
1150   void
1151   replace_statement(size_t index, Statement*);
1152 
1153   // Add a Statement before statement number INDEX.
1154   void
1155   insert_statement_before(size_t index, Statement*);
1156 
1157   // Add a Statement after statement number INDEX.
1158   void
1159   insert_statement_after(size_t index, Statement*);
1160 
1161   // Set the end location of the block.
1162   void
set_end_location(Location location)1163   set_end_location(Location location)
1164   { this->end_location_ = location; }
1165 
1166   // Traverse the tree.
1167   int
1168   traverse(Traverse*);
1169 
1170   // Set final types for unspecified variables and constants.
1171   void
1172   determine_types();
1173 
1174   // Return true if execution of this block may fall through to the
1175   // next block.
1176   bool
1177   may_fall_through() const;
1178 
1179   // Write the export data for the block's statements to the string.
1180   void
1181   export_block(Export_function_body*);
1182 
1183   // Turn exported block data into a block.
1184   static bool
1185   import_block(Block*, Import_function_body*, Location);
1186 
1187   // Convert the block to the backend representation.
1188   Bblock*
1189   get_backend(Translate_context*);
1190 
1191   // Iterate over statements.
1192 
1193   typedef std::vector<Statement*>::iterator iterator;
1194 
1195   iterator
begin()1196   begin()
1197   { return this->statements_.begin(); }
1198 
1199   iterator
end()1200   end()
1201   { return this->statements_.end(); }
1202 
1203  private:
1204   // Enclosing block.
1205   Block* enclosing_;
1206   // Statements in the block.
1207   std::vector<Statement*> statements_;
1208   // Binding contour.
1209   Bindings* bindings_;
1210   // Location of start of block.
1211   Location start_location_;
1212   // Location of end of block.
1213   Location end_location_;
1214 };
1215 
1216 // A function.
1217 
1218 class Function
1219 {
1220  public:
1221   Function(Function_type* type, Named_object*, Block*, Location);
1222 
1223   // Return the function's type.
1224   Function_type*
type()1225   type() const
1226   { return this->type_; }
1227 
1228   // Return the enclosing function if there is one.
1229   Named_object*
enclosing()1230   enclosing() const
1231   { return this->enclosing_; }
1232 
1233   // Set the enclosing function.  This is used when building thunks
1234   // for functions which call recover.
1235   void
set_enclosing(Named_object * enclosing)1236   set_enclosing(Named_object* enclosing)
1237   {
1238     go_assert(this->enclosing_ == NULL);
1239     this->enclosing_ = enclosing;
1240   }
1241 
1242   // The result variables.
1243   typedef std::vector<Named_object*> Results;
1244 
1245   // Create the result variables in the outer block.
1246   void
1247   create_result_variables(Gogo*);
1248 
1249   // Update the named result variables when cloning a function which
1250   // calls recover.
1251   void
1252   update_result_variables();
1253 
1254   // Return the result variables.
1255   Results*
result_variables()1256   result_variables()
1257   { return this->results_; }
1258 
1259   bool
is_sink()1260   is_sink() const
1261   { return this->is_sink_; }
1262 
1263   void
set_is_sink()1264   set_is_sink()
1265   { this->is_sink_ = true; }
1266 
1267   // Whether the result variables have names.
1268   bool
results_are_named()1269   results_are_named() const
1270   { return this->results_are_named_; }
1271 
1272   // Return the assembler name.
1273   const std::string&
asm_name()1274   asm_name() const
1275   { return this->asm_name_; }
1276 
1277   // Set the assembler name.
1278   void
set_asm_name(const std::string & asm_name)1279   set_asm_name(const std::string& asm_name)
1280   { this->asm_name_ = asm_name; }
1281 
1282   // Return the pragmas for this function.
1283   unsigned int
pragmas()1284   pragmas() const
1285   { return this->pragmas_; }
1286 
1287   // Set the pragmas for this function.
1288   void
set_pragmas(unsigned int pragmas)1289   set_pragmas(unsigned int pragmas)
1290   {
1291     this->pragmas_ = pragmas;
1292   }
1293 
1294   // Return the index to use for a nested function.
1295   unsigned int
next_nested_function_index()1296   next_nested_function_index()
1297   {
1298     ++this->nested_functions_;
1299     return this->nested_functions_;
1300   }
1301 
1302   // Whether this method should not be included in the type
1303   // descriptor.
1304   bool
1305   nointerface() const;
1306 
1307   // Record that this method should not be included in the type
1308   // descriptor.
1309   void
1310   set_nointerface();
1311 
1312   // Record that this function is a stub method created for an unnamed
1313   // type.
1314   void
set_is_unnamed_type_stub_method()1315   set_is_unnamed_type_stub_method()
1316   {
1317     go_assert(this->is_method());
1318     this->is_unnamed_type_stub_method_ = true;
1319   }
1320 
1321   // Return the amount of enclosed variables in this closure.
1322   size_t
closure_field_count()1323   closure_field_count() const
1324   { return this->closure_fields_.size(); }
1325 
1326   // Add a new field to the closure variable.
1327   void
add_closure_field(Named_object * var,Location loc)1328   add_closure_field(Named_object* var, Location loc)
1329   { this->closure_fields_.push_back(std::make_pair(var, loc)); }
1330 
1331   // Whether this function needs a closure.
1332   bool
needs_closure()1333   needs_closure() const
1334   { return !this->closure_fields_.empty(); }
1335 
1336   // Return the closure variable, creating it if necessary.  This is
1337   // passed to the function as a static chain parameter.
1338   Named_object*
1339   closure_var();
1340 
1341   // Set the closure variable.  This is used when building thunks for
1342   // functions which call recover.
1343   void
set_closure_var(Named_object * v)1344   set_closure_var(Named_object* v)
1345   {
1346     go_assert(this->closure_var_ == NULL);
1347     this->closure_var_ = v;
1348   }
1349 
1350   // Return the variable for a reference to field INDEX in the closure
1351   // variable.
1352   Named_object*
enclosing_var(unsigned int index)1353   enclosing_var(unsigned int index)
1354   {
1355     go_assert(index < this->closure_fields_.size());
1356     return closure_fields_[index].first;
1357   }
1358 
1359   // Set the type of the closure variable if there is one.
1360   void
1361   set_closure_type();
1362 
1363   // Get the block of statements associated with the function.
1364   Block*
block()1365   block() const
1366   { return this->block_; }
1367 
1368   // Get the location of the start of the function.
1369   Location
location()1370   location() const
1371   { return this->location_; }
1372 
1373   // Return whether this function is actually a method.
1374   bool
1375   is_method() const;
1376 
1377   // Add a label definition to the function.
1378   Label*
1379   add_label_definition(Gogo*, const std::string& label_name, Location);
1380 
1381   // Add a label reference to a function.  ISSUE_GOTO_ERRORS is true
1382   // if we should report errors for a goto from the current location
1383   // to the label location.
1384   Label*
1385   add_label_reference(Gogo*, const std::string& label_name,
1386 		      Location, bool issue_goto_errors);
1387 
1388   // Warn about labels that are defined but not used.
1389   void
1390   check_labels() const;
1391 
1392   // Note that a new local type has been added.  Return its index.
1393   unsigned int
new_local_type_index()1394   new_local_type_index()
1395   { return this->local_type_count_++; }
1396 
1397   // Whether this function calls the predeclared recover function.
1398   bool
calls_recover()1399   calls_recover() const
1400   { return this->calls_recover_; }
1401 
1402   // Record that this function calls the predeclared recover function.
1403   // This is set during the lowering pass.
1404   void
set_calls_recover()1405   set_calls_recover()
1406   { this->calls_recover_ = true; }
1407 
1408   // Whether this is a recover thunk function.
1409   bool
is_recover_thunk()1410   is_recover_thunk() const
1411   { return this->is_recover_thunk_; }
1412 
1413   // Record that this is a thunk built for a function which calls
1414   // recover.
1415   void
set_is_recover_thunk()1416   set_is_recover_thunk()
1417   { this->is_recover_thunk_ = true; }
1418 
1419   // Whether this function already has a recover thunk.
1420   bool
has_recover_thunk()1421   has_recover_thunk() const
1422   { return this->has_recover_thunk_; }
1423 
1424   // Record that this function already has a recover thunk.
1425   void
set_has_recover_thunk()1426   set_has_recover_thunk()
1427   { this->has_recover_thunk_ = true; }
1428 
1429   // Record that this function is a thunk created for a defer
1430   // statement that calls the __go_set_defer_retaddr runtime function.
1431   void
set_calls_defer_retaddr()1432   set_calls_defer_retaddr()
1433   { this->calls_defer_retaddr_ = true; }
1434 
1435   // Whether this is a type hash or equality function created by the
1436   // compiler.
1437   bool
is_type_specific_function()1438   is_type_specific_function()
1439   { return this->is_type_specific_function_; }
1440 
1441   // Record that this function is a type hash or equality function
1442   // created by the compiler.
1443   void
set_is_type_specific_function()1444   set_is_type_specific_function()
1445   { this->is_type_specific_function_ = true; }
1446 
1447   // Mark the function as going into a unique section.
1448   void
set_in_unique_section()1449   set_in_unique_section()
1450   { this->in_unique_section_ = true; }
1451 
1452   // Return whether this function should be exported for inlining.
1453   bool
export_for_inlining()1454   export_for_inlining() const
1455   { return this->export_for_inlining_; }
1456 
1457   // Mark the function to be exported for inlining.
1458   void
set_export_for_inlining()1459   set_export_for_inlining()
1460   { this->export_for_inlining_ = true; }
1461 
1462   // Return whether this function is inline only.
1463   bool
is_inline_only()1464   is_inline_only() const
1465   { return this->is_inline_only_; }
1466 
1467   // Mark the function as inline only: the body should not be emitted
1468   // if it is not inlined.
1469   void
set_is_inline_only()1470   set_is_inline_only()
1471   { this->is_inline_only_ = true; }
1472 
1473   // Swap with another function.  Used only for the thunk which calls
1474   // recover.
1475   void
1476   swap_for_recover(Function *);
1477 
1478   // Traverse the tree.
1479   int
1480   traverse(Traverse*);
1481 
1482   // Determine types in the function.
1483   void
1484   determine_types();
1485 
1486   // Return an expression for the function descriptor, given the named
1487   // object for this function.  This may only be called for functions
1488   // without a closure.  This will be an immutable struct with one
1489   // field that points to the function's code.
1490   Expression*
1491   descriptor(Gogo*, Named_object*);
1492 
1493   // Set the descriptor for this function.  This is used when a
1494   // function declaration is followed by a function definition.
1495   void
set_descriptor(Expression * descriptor)1496   set_descriptor(Expression* descriptor)
1497   {
1498     go_assert(this->descriptor_ == NULL);
1499     this->descriptor_ = descriptor;
1500   }
1501 
1502   // Return the backend representation.
1503   Bfunction*
1504   get_or_make_decl(Gogo*, Named_object*);
1505 
1506   // Return the function's decl after it has been built.
1507   Bfunction*
1508   get_decl() const;
1509 
1510   // Set the function decl to hold a backend representation of the function
1511   // code.
1512   void
1513   build(Gogo*, Named_object*);
1514 
1515   // Get the statement that assigns values to this function's result struct.
1516   Bstatement*
1517   return_value(Gogo*, Named_object*, Location) const;
1518 
1519   // Get an expression for the variable holding the defer stack.
1520   Expression*
1521   defer_stack(Location);
1522 
1523   // Export the function.
1524   void
1525   export_func(Export*, const std::string& name) const;
1526 
1527   // Export a function with a type.
1528   static void
1529   export_func_with_type(Export*, const std::string& name,
1530 			const Function_type*, Results*, bool nointerface,
1531 			Block* block, Location);
1532 
1533   // Import a function.
1534   static void
1535   import_func(Import*, std::string* pname, Typed_identifier** receiver,
1536 	      Typed_identifier_list** pparameters,
1537 	      Typed_identifier_list** presults, bool* is_varargs,
1538 	      bool* nointerface, std::string* body);
1539 
1540  private:
1541   // Type for mapping from label names to Label objects.
1542   typedef Unordered_map(std::string, Label*) Labels;
1543 
1544   void
1545   build_defer_wrapper(Gogo*, Named_object*, Bstatement**, Bstatement**);
1546 
1547   typedef std::vector<std::pair<Named_object*,
1548 				Location> > Closure_fields;
1549 
1550   // The function's type.
1551   Function_type* type_;
1552   // The enclosing function.  This is NULL when there isn't one, which
1553   // is the normal case.
1554   Named_object* enclosing_;
1555   // The result variables, if any.
1556   Results* results_;
1557   // If there is a closure, this is the list of variables which appear
1558   // in the closure.  This is created by the parser, and then resolved
1559   // to a real type when we lower parse trees.
1560   Closure_fields closure_fields_;
1561   // The closure variable, passed as a parameter using the static
1562   // chain parameter.  Normally NULL.
1563   Named_object* closure_var_;
1564   // The outer block of statements in the function.
1565   Block* block_;
1566   // The source location of the start of the function.
1567   Location location_;
1568   // Labels defined or referenced in the function.
1569   Labels labels_;
1570   // The number of local types defined in this function.
1571   unsigned int local_type_count_;
1572   // The assembler name: this is the name that will be put in the object file.
1573   // Set by the go:linkname compiler directive.  This is normally empty.
1574   std::string asm_name_;
1575   // The function descriptor, if any.
1576   Expression* descriptor_;
1577   // The function decl.
1578   Bfunction* fndecl_;
1579   // The defer stack variable.  A pointer to this variable is used to
1580   // distinguish the defer stack for one function from another.  This
1581   // is NULL unless we actually need a defer stack.
1582   Temporary_statement* defer_stack_;
1583   // Pragmas for this function.  This is a set of GOPRAGMA bits.
1584   unsigned int pragmas_;
1585   // Number of nested functions defined within this function.
1586   unsigned int nested_functions_;
1587   // True if this function is sink-named.  No code is generated.
1588   bool is_sink_ : 1;
1589   // True if the result variables are named.
1590   bool results_are_named_ : 1;
1591   // True if this function is a stub method created for an unnamed
1592   // type.
1593   bool is_unnamed_type_stub_method_ : 1;
1594   // True if this function calls the predeclared recover function.
1595   bool calls_recover_ : 1;
1596   // True if this a thunk built for a function which calls recover.
1597   bool is_recover_thunk_ : 1;
1598   // True if this function already has a recover thunk.
1599   bool has_recover_thunk_ : 1;
1600   // True if this is a thunk built for a defer statement that calls
1601   // the __go_set_defer_retaddr runtime function.
1602   bool calls_defer_retaddr_ : 1;
1603   // True if this is a function built by the compiler to as a hash or
1604   // equality function for some type.
1605   bool is_type_specific_function_ : 1;
1606   // True if this function should be put in a unique section.  This is
1607   // turned on for field tracking.
1608   bool in_unique_section_ : 1;
1609   // True if we should export the body of this function for
1610   // cross-package inlining.
1611   bool export_for_inlining_ : 1;
1612   // True if this function is inline only: if it should not be emitted
1613   // if it is not inlined.
1614   bool is_inline_only_ : 1;
1615 };
1616 
1617 // A snapshot of the current binding state.
1618 
1619 class Bindings_snapshot
1620 {
1621  public:
1622   Bindings_snapshot(const Block*, Location);
1623 
1624   // Report any errors appropriate for a goto from the current binding
1625   // state of B to this one.
1626   void
1627   check_goto_from(const Block* b, Location);
1628 
1629   // Report any errors appropriate for a goto from this binding state
1630   // to the current state of B.
1631   void
1632   check_goto_to(const Block* b);
1633 
1634  private:
1635   bool
1636   check_goto_block(Location, const Block*, const Block*, size_t*);
1637 
1638   void
1639   check_goto_defs(Location, const Block*, size_t, size_t);
1640 
1641   // The current block.
1642   const Block* block_;
1643   // The number of names currently defined in each open block.
1644   // Element 0 is this->block_, element 1 is
1645   // this->block_->enclosing(), etc.
1646   std::vector<size_t> counts_;
1647   // The location where this snapshot was taken.
1648   Location location_;
1649 };
1650 
1651 // A function declaration.
1652 
1653 class Function_declaration
1654 {
1655  public:
Function_declaration(Function_type * fntype,Location location)1656   Function_declaration(Function_type* fntype, Location location)
1657     : fntype_(fntype), location_(location), asm_name_(), descriptor_(NULL),
1658       fndecl_(NULL), pragmas_(0), imported_body_(),
1659       is_on_inlinable_list_(false)
1660   { }
1661 
1662   Function_type*
type()1663   type() const
1664   { return this->fntype_; }
1665 
1666   Location
location()1667   location() const
1668   { return this->location_; }
1669 
1670   // Return whether this function declaration is a method.
1671   bool
1672   is_method() const;
1673 
1674   const std::string&
asm_name()1675   asm_name() const
1676   { return this->asm_name_; }
1677 
1678   // Set the assembler name.
1679   void
set_asm_name(const std::string & asm_name)1680   set_asm_name(const std::string& asm_name)
1681   { this->asm_name_ = asm_name; }
1682 
1683   // Return the pragmas for this function.
1684   unsigned int
pragmas()1685   pragmas() const
1686   { return this->pragmas_; }
1687 
1688   // Set the pragmas for this function.
1689   void
set_pragmas(unsigned int pragmas)1690   set_pragmas(unsigned int pragmas)
1691   {
1692     this->pragmas_ = pragmas;
1693   }
1694 
1695   // Whether this method should not be included in the type
1696   // descriptor.
1697   bool
1698   nointerface() const;
1699 
1700   // Record that this method should not be included in the type
1701   // descriptor.
1702   void
1703   set_nointerface();
1704 
1705   // Whether we have an imported function body.
1706   bool
has_imported_body()1707   has_imported_body() const
1708   { return !this->imported_body_.empty(); }
1709 
1710   // Record the imported body of this function.
1711   void
set_imported_body(Import * imp,const std::string & imported_body)1712   set_imported_body(Import* imp, const std::string& imported_body)
1713   {
1714     this->imp_ = imp;
1715     this->imported_body_ = imported_body;
1716   }
1717 
1718   // Whether this declaration is on the list of inlinable functions.
1719   bool
is_on_inlinable_list()1720   is_on_inlinable_list() const
1721   { return this->is_on_inlinable_list_; }
1722 
1723   // Set that this function is on the list of inlinable functions.
1724   void
set_is_on_inlinable_list()1725   set_is_on_inlinable_list()
1726   { this->is_on_inlinable_list_ = true; }
1727 
1728   // Import the function body, creating a function.
1729   void
1730   import_function_body(Gogo*, Named_object*);
1731 
1732   // Return an expression for the function descriptor, given the named
1733   // object for this function.  This may only be called for functions
1734   // without a closure.  This will be an immutable struct with one
1735   // field that points to the function's code.
1736   Expression*
1737   descriptor(Gogo*, Named_object*);
1738 
1739   // Return true if we have created a descriptor for this declaration.
1740   bool
has_descriptor()1741   has_descriptor() const
1742   { return this->descriptor_ != NULL; }
1743 
1744   // Return a backend representation.
1745   Bfunction*
1746   get_or_make_decl(Gogo*, Named_object*);
1747 
1748   // If there is a descriptor, build it into the backend
1749   // representation.
1750   void
1751   build_backend_descriptor(Gogo*);
1752 
1753   // Export a function declaration.
1754   void
export_func(Export * exp,const std::string & name)1755   export_func(Export* exp, const std::string& name) const
1756   {
1757     Function::export_func_with_type(exp, name, this->fntype_, NULL,
1758 				    this->is_method() && this->nointerface(),
1759 				    NULL, this->location_);
1760   }
1761 
1762   // Check that the types used in this declaration's signature are defined.
1763   void
1764   check_types() const;
1765 
1766  private:
1767   // The type of the function.
1768   Function_type* fntype_;
1769   // The location of the declaration.
1770   Location location_;
1771   // The assembler name: this is the name to use in references to the
1772   // function.  This is normally empty.
1773   std::string asm_name_;
1774   // The function descriptor, if any.
1775   Expression* descriptor_;
1776   // The function decl if needed.
1777   Bfunction* fndecl_;
1778   // Pragmas for this function.  This is a set of GOPRAGMA bits.
1779   unsigned int pragmas_;
1780   // Importer for function body if imported from a different package.
1781   Import* imp_;
1782   // Export data for function body if imported from a different package.
1783   std::string imported_body_;
1784   // Whether this declaration is already on the list of inlinable functions.
1785   bool is_on_inlinable_list_;
1786 };
1787 
1788 // A variable.
1789 
1790 class Variable
1791 {
1792  public:
1793   Variable(Type*, Expression*, bool is_global, bool is_parameter,
1794 	   bool is_receiver, Location);
1795 
1796   // Get the type of the variable.
1797   Type*
1798   type();
1799 
1800   Type*
1801   type() const;
1802 
1803   // Return whether the type is defined yet.
1804   bool
1805   has_type() const;
1806 
1807   // Get the initial value.
1808   Expression*
init()1809   init() const
1810   { return this->init_; }
1811 
1812   // Return whether there are any preinit statements.
1813   bool
has_pre_init()1814   has_pre_init() const
1815   { return this->preinit_ != NULL; }
1816 
1817   // Return the preinit statements if any.
1818   Block*
preinit()1819   preinit() const
1820   { return this->preinit_; }
1821 
1822   // Return whether this is a global variable.
1823   bool
is_global()1824   is_global() const
1825   { return this->is_global_; }
1826 
1827   // Return whether this is a function parameter.
1828   bool
is_parameter()1829   is_parameter() const
1830   { return this->is_parameter_; }
1831 
1832   // Return whether this is a closure (static chain) parameter.
1833   bool
is_closure()1834   is_closure() const
1835   { return this->is_closure_; }
1836 
1837   // Change this parameter to be a closure.
1838   void
set_is_closure()1839   set_is_closure()
1840   {
1841     this->is_closure_ = true;
1842   }
1843 
1844   // Return whether this is the receiver parameter of a method.
1845   bool
is_receiver()1846   is_receiver() const
1847   { return this->is_receiver_; }
1848 
1849   // Change this parameter to be a receiver.  This is used when
1850   // creating the thunks created for functions which call recover.
1851   void
set_is_receiver()1852   set_is_receiver()
1853   {
1854     go_assert(this->is_parameter_);
1855     this->is_receiver_ = true;
1856   }
1857 
1858   // Change this parameter to not be a receiver.  This is used when
1859   // creating the thunks created for functions which call recover.
1860   void
set_is_not_receiver()1861   set_is_not_receiver()
1862   {
1863     go_assert(this->is_parameter_);
1864     this->is_receiver_ = false;
1865   }
1866 
1867   // Return whether this is the varargs parameter of a function.
1868   bool
is_varargs_parameter()1869   is_varargs_parameter() const
1870   { return this->is_varargs_parameter_; }
1871 
1872   // Whether this variable's address is taken.
1873   bool
is_address_taken()1874   is_address_taken() const
1875   { return this->is_address_taken_; }
1876 
1877   // Whether this variable should live in the heap.
1878   bool
is_in_heap()1879   is_in_heap() const
1880   { return this->is_address_taken_ && !this->is_global_; }
1881 
1882   // Note that something takes the address of this variable.
1883   void
set_address_taken()1884   set_address_taken()
1885   { this->is_address_taken_ = true; }
1886 
1887   // Return whether the address is taken but does not escape.
1888   bool
is_non_escaping_address_taken()1889   is_non_escaping_address_taken() const
1890   { return this->is_non_escaping_address_taken_; }
1891 
1892   // Note that something takes the address of this variable such that
1893   // the address does not escape the function.
1894   void
set_non_escaping_address_taken()1895   set_non_escaping_address_taken()
1896   { this->is_non_escaping_address_taken_ = true; }
1897 
1898   // Get the source location of the variable's declaration.
1899   Location
location()1900   location() const
1901   { return this->location_; }
1902 
1903   // Record that this is the varargs parameter of a function.
1904   void
set_is_varargs_parameter()1905   set_is_varargs_parameter()
1906   {
1907     go_assert(this->is_parameter_);
1908     this->is_varargs_parameter_ = true;
1909   }
1910 
1911   // Return whether the variable has been used.
1912   bool
is_used()1913   is_used() const
1914   { return this->is_used_; }
1915 
1916   // Mark that the variable has been used.
1917   void
set_is_used()1918   set_is_used()
1919   { this->is_used_ = true; }
1920 
1921   // Clear the initial value; used for error handling and write barriers.
1922   void
clear_init()1923   clear_init()
1924   { this->init_ = NULL; }
1925 
1926   // Set the initial value; used for converting shortcuts.
1927   void
set_init(Expression * init)1928   set_init(Expression* init)
1929   { this->init_ = init; }
1930 
1931   // Get the preinit block, a block of statements to be run before the
1932   // initialization expression.
1933   Block*
1934   preinit_block(Gogo*);
1935 
1936   // Add a statement to be run before the initialization expression.
1937   // This is only used for global variables.
1938   void
1939   add_preinit_statement(Gogo*, Statement*);
1940 
1941   // Lower the initialization expression after parsing is complete.
1942   void
1943   lower_init_expression(Gogo*, Named_object*, Statement_inserter*);
1944 
1945   // Flatten the initialization expression after ordering evaluations.
1946   void
1947   flatten_init_expression(Gogo*, Named_object*, Statement_inserter*);
1948 
1949   // A special case: the init value is used only to determine the
1950   // type.  This is used if the variable is defined using := with the
1951   // comma-ok form of a map index or a receive expression.  The init
1952   // value is actually the map index expression or receive expression.
1953   // We use this because we may not know the right type at parse time.
1954   void
set_type_from_init_tuple()1955   set_type_from_init_tuple()
1956   { this->type_from_init_tuple_ = true; }
1957 
1958   // Another special case: the init value is used only to determine
1959   // the type.  This is used if the variable is defined using := with
1960   // a range clause.  The init value is the range expression.  The
1961   // type of the variable is the index type of the range expression
1962   // (i.e., the first value returned by a range).
1963   void
set_type_from_range_index()1964   set_type_from_range_index()
1965   { this->type_from_range_index_ = true; }
1966 
1967   // Another special case: like set_type_from_range_index, but the
1968   // type is the value type of the range expression (i.e., the second
1969   // value returned by a range).
1970   void
set_type_from_range_value()1971   set_type_from_range_value()
1972   { this->type_from_range_value_ = true; }
1973 
1974   // Another special case: the init value is used only to determine
1975   // the type.  This is used if the variable is defined using := with
1976   // a case in a select statement.  The init value is the channel.
1977   // The type of the variable is the channel's element type.
1978   void
set_type_from_chan_element()1979   set_type_from_chan_element()
1980   { this->type_from_chan_element_ = true; }
1981 
1982   // After we lower the select statement, we once again set the type
1983   // from the initialization expression.
1984   void
clear_type_from_chan_element()1985   clear_type_from_chan_element()
1986   {
1987     go_assert(this->type_from_chan_element_);
1988     this->type_from_chan_element_ = false;
1989   }
1990 
1991   // TRUE if this variable was created for a type switch clause.
1992   bool
is_type_switch_var()1993   is_type_switch_var() const
1994   { return this->is_type_switch_var_; }
1995 
1996   // Note that this variable was created for a type switch clause.
1997   void
set_is_type_switch_var()1998   set_is_type_switch_var()
1999   { this->is_type_switch_var_ = true; }
2000 
2001   // Mark the variable as going into a unique section.
2002   void
set_in_unique_section()2003   set_in_unique_section()
2004   {
2005     go_assert(this->is_global_);
2006     this->in_unique_section_ = true;
2007   }
2008 
2009   // Return the top-level declaration for this variable.
2010   Statement*
toplevel_decl()2011   toplevel_decl()
2012   { return this->toplevel_decl_; }
2013 
2014   // Set the top-level declaration for this variable. Only used for local
2015   // variables
2016   void
set_toplevel_decl(Statement * s)2017   set_toplevel_decl(Statement* s)
2018   {
2019     go_assert(!this->is_global_ && !this->is_parameter_ && !this->is_receiver_);
2020     this->toplevel_decl_ = s;
2021   }
2022 
2023   // Traverse the initializer expression.
2024   int
2025   traverse_expression(Traverse*, unsigned int traverse_mask);
2026 
2027   // Determine the type of the variable if necessary.
2028   void
2029   determine_type();
2030 
2031   // Get the backend representation of the variable.
2032   Bvariable*
2033   get_backend_variable(Gogo*, Named_object*, const Package*,
2034 		       const std::string&);
2035 
2036   // Get the initial value of the variable.  This may only
2037   // be called if has_pre_init() returns false.
2038   Bexpression*
2039   get_init(Gogo*, Named_object* function);
2040 
2041   // Return a series of statements which sets the value of the
2042   // variable in DECL.  This should only be called is has_pre_init()
2043   // returns true.  DECL may be NULL for a sink variable.
2044   Bstatement*
2045   get_init_block(Gogo*, Named_object* function, Bvariable* decl);
2046 
2047   // Export the variable.
2048   void
2049   export_var(Export*, const std::string& name) const;
2050 
2051   // Import a variable.
2052   static void
2053   import_var(Import*, std::string* pname, Type** ptype);
2054 
2055  private:
2056   // The type of a tuple.
2057   Type*
2058   type_from_tuple(Expression*, bool) const;
2059 
2060   // The type of a range.
2061   Type*
2062   type_from_range(Expression*, bool, bool) const;
2063 
2064   // The element type of a channel.
2065   Type*
2066   type_from_chan_element(Expression*, bool) const;
2067 
2068   // The variable's type.  This may be NULL if the type is set from
2069   // the expression.
2070   Type* type_;
2071   // The initial value.  This may be NULL if the variable should be
2072   // initialized to the default value for the type.
2073   Expression* init_;
2074   // Statements to run before the init statement.
2075   Block* preinit_;
2076   // Location of variable definition.
2077   Location location_;
2078   // Backend representation.
2079   Bvariable* backend_;
2080   // Whether this is a global variable.
2081   bool is_global_ : 1;
2082   // Whether this is a function parameter.
2083   bool is_parameter_ : 1;
2084   // Whether this is a closure parameter.
2085   bool is_closure_ : 1;
2086   // Whether this is the receiver parameter of a method.
2087   bool is_receiver_ : 1;
2088   // Whether this is the varargs parameter of a function.
2089   bool is_varargs_parameter_ : 1;
2090   // Whether this variable is ever referenced.
2091   bool is_used_ : 1;
2092   // Whether something takes the address of this variable.  For a
2093   // local variable this implies that the variable has to be on the
2094   // heap if it escapes from its function.
2095   bool is_address_taken_ : 1;
2096   // Whether something takes the address of this variable such that
2097   // the address does not escape the function.
2098   bool is_non_escaping_address_taken_ : 1;
2099   // True if we have seen this variable in a traversal.
2100   bool seen_ : 1;
2101   // True if we have lowered the initialization expression.
2102   bool init_is_lowered_ : 1;
2103   // True if we have flattened the initialization expression.
2104   bool init_is_flattened_ : 1;
2105   // True if init is a tuple used to set the type.
2106   bool type_from_init_tuple_ : 1;
2107   // True if init is a range clause and the type is the index type.
2108   bool type_from_range_index_ : 1;
2109   // True if init is a range clause and the type is the value type.
2110   bool type_from_range_value_ : 1;
2111   // True if init is a channel and the type is the channel's element type.
2112   bool type_from_chan_element_ : 1;
2113   // True if this is a variable created for a type switch case.
2114   bool is_type_switch_var_ : 1;
2115   // True if we have determined types.
2116   bool determined_type_ : 1;
2117   // True if this variable should be put in a unique section.  This is
2118   // used for field tracking.
2119   bool in_unique_section_ : 1;
2120   // The top-level declaration for this variable. Only used for local
2121   // variables. Must be a Temporary_statement if not NULL.
2122   Statement* toplevel_decl_;
2123 };
2124 
2125 // A variable which is really the name for a function return value, or
2126 // part of one.
2127 
2128 class Result_variable
2129 {
2130  public:
Result_variable(Type * type,Function * function,int index,Location location)2131   Result_variable(Type* type, Function* function, int index,
2132 		  Location location)
2133     : type_(type), function_(function), index_(index), location_(location),
2134       backend_(NULL), is_address_taken_(false),
2135       is_non_escaping_address_taken_(false)
2136   { }
2137 
2138   // Get the type of the result variable.
2139   Type*
type()2140   type() const
2141   { return this->type_; }
2142 
2143   // Get the function that this is associated with.
2144   Function*
function()2145   function() const
2146   { return this->function_; }
2147 
2148   // Index in the list of function results.
2149   int
index()2150   index() const
2151   { return this->index_; }
2152 
2153   // The location of the variable definition.
2154   Location
location()2155   location() const
2156   { return this->location_; }
2157 
2158   // Whether this variable's address is taken.
2159   bool
is_address_taken()2160   is_address_taken() const
2161   { return this->is_address_taken_; }
2162 
2163   // Note that something takes the address of this variable.
2164   void
set_address_taken()2165   set_address_taken()
2166   { this->is_address_taken_ = true; }
2167 
2168   // Return whether the address is taken but does not escape.
2169   bool
is_non_escaping_address_taken()2170   is_non_escaping_address_taken() const
2171   { return this->is_non_escaping_address_taken_; }
2172 
2173   // Note that something takes the address of this variable such that
2174   // the address does not escape the function.
2175   void
set_non_escaping_address_taken()2176   set_non_escaping_address_taken()
2177   { this->is_non_escaping_address_taken_ = true; }
2178 
2179   // Whether this variable should live in the heap.
2180   bool
is_in_heap()2181   is_in_heap() const
2182   { return this->is_address_taken_; }
2183 
2184   // Set the function.  This is used when cloning functions which call
2185   // recover.
2186   void
set_function(Function * function)2187   set_function(Function* function)
2188   { this->function_ = function; }
2189 
2190   // Get the backend representation of the variable.
2191   Bvariable*
2192   get_backend_variable(Gogo*, Named_object*, const std::string&);
2193 
2194  private:
2195   // Type of result variable.
2196   Type* type_;
2197   // Function with which this is associated.
2198   Function* function_;
2199   // Index in list of results.
2200   int index_;
2201   // Where the result variable is defined.
2202   Location location_;
2203   // Backend representation.
2204   Bvariable* backend_;
2205   // Whether something takes the address of this variable.
2206   bool is_address_taken_;
2207   // Whether something takes the address of this variable such that
2208   // the address does not escape the function.
2209   bool is_non_escaping_address_taken_;
2210 };
2211 
2212 // The value we keep for a named constant.  This lets us hold a type
2213 // and an expression.
2214 
2215 class Named_constant
2216 {
2217  public:
Named_constant(Type * type,Expression * expr,int iota_value,Location location)2218   Named_constant(Type* type, Expression* expr, int iota_value,
2219 		 Location location)
2220     : type_(type), expr_(expr), iota_value_(iota_value), location_(location),
2221       lowering_(false), is_sink_(false), bconst_(NULL)
2222   { }
2223 
2224   Type*
type()2225   type() const
2226   { return this->type_; }
2227 
2228   void
2229   set_type(Type* t);
2230 
2231   Expression*
expr()2232   expr() const
2233   { return this->expr_; }
2234 
2235   int
iota_value()2236   iota_value() const
2237   { return this->iota_value_; }
2238 
2239   Location
location()2240   location() const
2241   { return this->location_; }
2242 
2243   // Whether we are lowering.
2244   bool
lowering()2245   lowering() const
2246   { return this->lowering_; }
2247 
2248   // Set that we are lowering.
2249   void
set_lowering()2250   set_lowering()
2251   { this->lowering_ = true; }
2252 
2253   // We are no longer lowering.
2254   void
clear_lowering()2255   clear_lowering()
2256   { this->lowering_ = false; }
2257 
2258   bool
is_sink()2259   is_sink() const
2260   { return this->is_sink_; }
2261 
2262   void
set_is_sink()2263   set_is_sink()
2264   { this->is_sink_ = true; }
2265 
2266   // Traverse the expression.
2267   int
2268   traverse_expression(Traverse*);
2269 
2270   // Determine the type of the constant if necessary.
2271   void
2272   determine_type();
2273 
2274   // Indicate that we found and reported an error for this constant.
2275   void
2276   set_error();
2277 
2278   // Export the constant.
2279   void
2280   export_const(Export*, const std::string& name) const;
2281 
2282   // Import a constant.
2283   static void
2284   import_const(Import*, std::string*, Type**, Expression**);
2285 
2286   // Get the backend representation of the constant value.
2287   Bexpression*
2288   get_backend(Gogo*, Named_object*);
2289 
2290  private:
2291   // The type of the constant.
2292   Type* type_;
2293   // The expression for the constant.
2294   Expression* expr_;
2295   // If the predeclared constant iota is used in EXPR_, this is the
2296   // value it will have.  We do this because at parse time we don't
2297   // know whether the name "iota" will refer to the predeclared
2298   // constant or to something else.  We put in the right value in when
2299   // we lower.
2300   int iota_value_;
2301   // The location of the definition.
2302   Location location_;
2303   // Whether we are currently lowering this constant.
2304   bool lowering_;
2305   // Whether this constant is blank named and needs only type checking.
2306   bool is_sink_;
2307   // The backend representation of the constant value.
2308   Bexpression* bconst_;
2309 };
2310 
2311 // A type declaration.
2312 
2313 class Type_declaration
2314 {
2315  public:
Type_declaration(Location location)2316   Type_declaration(Location location)
2317     : location_(location), in_function_(NULL), in_function_index_(0),
2318       methods_(), issued_warning_(false)
2319   { }
2320 
2321   // Return the location.
2322   Location
location()2323   location() const
2324   { return this->location_; }
2325 
2326   // Return the function in which this type is declared.  This will
2327   // return NULL for a type declared in global scope.
2328   Named_object*
in_function(unsigned int * pindex)2329   in_function(unsigned int* pindex)
2330   {
2331     *pindex = this->in_function_index_;
2332     return this->in_function_;
2333   }
2334 
2335   // Set the function in which this type is declared.
2336   void
set_in_function(Named_object * f,unsigned int index)2337   set_in_function(Named_object* f, unsigned int index)
2338   {
2339     this->in_function_ = f;
2340     this->in_function_index_ = index;
2341   }
2342 
2343   // Add a method to this type.  This is used when methods are defined
2344   // before the type.
2345   Named_object*
2346   add_method(const std::string& name, Function* function);
2347 
2348   // Add a method declaration to this type.
2349   Named_object*
2350   add_method_declaration(const std::string& name, Package*,
2351 			 Function_type* type, Location location);
2352 
2353   // Add an already created object as a method.
2354   void
add_existing_method(Named_object * no)2355   add_existing_method(Named_object* no)
2356   { this->methods_.push_back(no); }
2357 
2358   // Return whether any methods were defined.
2359   bool
2360   has_methods() const;
2361 
2362   // Return the methods.
2363   const std::vector<Named_object*>*
methods()2364   methods() const
2365   { return &this->methods_; }
2366 
2367   // Define methods when the real type is known.
2368   void
2369   define_methods(Named_type*);
2370 
2371   // This is called if we are trying to use this type.  It returns
2372   // true if we should issue a warning.
2373   bool
2374   using_type();
2375 
2376  private:
2377   // The location of the type declaration.
2378   Location location_;
2379   // If this type is declared in a function, a pointer back to the
2380   // function in which it is defined.
2381   Named_object* in_function_;
2382   // The index of this type in IN_FUNCTION_.
2383   unsigned int in_function_index_;
2384   // Methods defined before the type is defined.
2385   std::vector<Named_object*> methods_;
2386   // True if we have issued a warning about a use of this type
2387   // declaration when it is undefined.
2388   bool issued_warning_;
2389 };
2390 
2391 // An unknown object.  These are created by the parser for forward
2392 // references to names which have not been seen before.  In a correct
2393 // program, these will always point to a real definition by the end of
2394 // the parse.  Because they point to another Named_object, these may
2395 // only be referenced by Unknown_expression objects.
2396 
2397 class Unknown_name
2398 {
2399  public:
Unknown_name(Location location)2400   Unknown_name(Location location)
2401     : location_(location), real_named_object_(NULL)
2402   { }
2403 
2404   // Return the location where this name was first seen.
2405   Location
location()2406   location() const
2407   { return this->location_; }
2408 
2409   // Return the real named object that this points to, or NULL if it
2410   // was never resolved.
2411   Named_object*
real_named_object()2412   real_named_object() const
2413   { return this->real_named_object_; }
2414 
2415   // Set the real named object that this points to.
2416   void
2417   set_real_named_object(Named_object* no);
2418 
2419  private:
2420   // The location where this name was first seen.
2421   Location location_;
2422   // The real named object when it is known.
2423   Named_object*
2424   real_named_object_;
2425 };
2426 
2427 // A named object named.  This is the result of a declaration.  We
2428 // don't use a superclass because they all have to be handled
2429 // differently.
2430 
2431 class Named_object
2432 {
2433  public:
2434   enum Classification
2435   {
2436     // An uninitialized Named_object.  We should never see this.
2437     NAMED_OBJECT_UNINITIALIZED,
2438     // An erroneous name.  This indicates a parse error, to avoid
2439     // later errors about undefined references.
2440     NAMED_OBJECT_ERRONEOUS,
2441     // An unknown name.  This is used for forward references.  In a
2442     // correct program, these will all be resolved by the end of the
2443     // parse.
2444     NAMED_OBJECT_UNKNOWN,
2445     // A const.
2446     NAMED_OBJECT_CONST,
2447     // A type.
2448     NAMED_OBJECT_TYPE,
2449     // A forward type declaration.
2450     NAMED_OBJECT_TYPE_DECLARATION,
2451     // A var.
2452     NAMED_OBJECT_VAR,
2453     // A result variable in a function.
2454     NAMED_OBJECT_RESULT_VAR,
2455     // The blank identifier--the special variable named _.
2456     NAMED_OBJECT_SINK,
2457     // A func.
2458     NAMED_OBJECT_FUNC,
2459     // A forward func declaration.
2460     NAMED_OBJECT_FUNC_DECLARATION,
2461     // A package.
2462     NAMED_OBJECT_PACKAGE
2463   };
2464 
2465   // Return the classification.
2466   Classification
classification()2467   classification() const
2468   { return this->classification_; }
2469 
2470   // Classifiers.
2471 
2472   bool
is_erroneous()2473   is_erroneous() const
2474   { return this->classification_ == NAMED_OBJECT_ERRONEOUS; }
2475 
2476   bool
is_unknown()2477   is_unknown() const
2478   { return this->classification_ == NAMED_OBJECT_UNKNOWN; }
2479 
2480   bool
is_const()2481   is_const() const
2482   { return this->classification_ == NAMED_OBJECT_CONST; }
2483 
2484   bool
is_type()2485   is_type() const
2486   { return this->classification_ == NAMED_OBJECT_TYPE; }
2487 
2488   bool
is_type_declaration()2489   is_type_declaration() const
2490   { return this->classification_ == NAMED_OBJECT_TYPE_DECLARATION; }
2491 
2492   bool
is_variable()2493   is_variable() const
2494   { return this->classification_ == NAMED_OBJECT_VAR; }
2495 
2496   bool
is_result_variable()2497   is_result_variable() const
2498   { return this->classification_ == NAMED_OBJECT_RESULT_VAR; }
2499 
2500   bool
is_sink()2501   is_sink() const
2502   { return this->classification_ == NAMED_OBJECT_SINK; }
2503 
2504   bool
is_function()2505   is_function() const
2506   { return this->classification_ == NAMED_OBJECT_FUNC; }
2507 
2508   bool
is_function_declaration()2509   is_function_declaration() const
2510   { return this->classification_ == NAMED_OBJECT_FUNC_DECLARATION; }
2511 
2512   bool
is_package()2513   is_package() const
2514   { return this->classification_ == NAMED_OBJECT_PACKAGE; }
2515 
2516   // Creators.
2517 
2518   static Named_object*
make_erroneous_name(const std::string & name)2519   make_erroneous_name(const std::string& name)
2520   { return new Named_object(name, NULL, NAMED_OBJECT_ERRONEOUS); }
2521 
2522   static Named_object*
2523   make_unknown_name(const std::string& name, Location);
2524 
2525   static Named_object*
2526   make_constant(const Typed_identifier&, const Package*, Expression*,
2527 		int iota_value);
2528 
2529   static Named_object*
2530   make_type(const std::string&, const Package*, Type*, Location);
2531 
2532   static Named_object*
2533   make_type_declaration(const std::string&, const Package*, Location);
2534 
2535   static Named_object*
2536   make_variable(const std::string&, const Package*, Variable*);
2537 
2538   static Named_object*
2539   make_result_variable(const std::string&, Result_variable*);
2540 
2541   static Named_object*
2542   make_sink();
2543 
2544   static Named_object*
2545   make_function(const std::string&, const Package*, Function*);
2546 
2547   static Named_object*
2548   make_function_declaration(const std::string&, const Package*, Function_type*,
2549 			    Location);
2550 
2551   static Named_object*
2552   make_package(const std::string& alias, Package* package);
2553 
2554   // Getters.
2555 
2556   Unknown_name*
unknown_value()2557   unknown_value()
2558   {
2559     go_assert(this->classification_ == NAMED_OBJECT_UNKNOWN);
2560     return this->u_.unknown_value;
2561   }
2562 
2563   const Unknown_name*
unknown_value()2564   unknown_value() const
2565   {
2566     go_assert(this->classification_ == NAMED_OBJECT_UNKNOWN);
2567     return this->u_.unknown_value;
2568   }
2569 
2570   Named_constant*
const_value()2571   const_value()
2572   {
2573     go_assert(this->classification_ == NAMED_OBJECT_CONST);
2574     return this->u_.const_value;
2575   }
2576 
2577   const Named_constant*
const_value()2578   const_value() const
2579   {
2580     go_assert(this->classification_ == NAMED_OBJECT_CONST);
2581     return this->u_.const_value;
2582   }
2583 
2584   Named_type*
type_value()2585   type_value()
2586   {
2587     go_assert(this->classification_ == NAMED_OBJECT_TYPE);
2588     return this->u_.type_value;
2589   }
2590 
2591   const Named_type*
type_value()2592   type_value() const
2593   {
2594     go_assert(this->classification_ == NAMED_OBJECT_TYPE);
2595     return this->u_.type_value;
2596   }
2597 
2598   Type_declaration*
type_declaration_value()2599   type_declaration_value()
2600   {
2601     go_assert(this->classification_ == NAMED_OBJECT_TYPE_DECLARATION);
2602     return this->u_.type_declaration;
2603   }
2604 
2605   const Type_declaration*
type_declaration_value()2606   type_declaration_value() const
2607   {
2608     go_assert(this->classification_ == NAMED_OBJECT_TYPE_DECLARATION);
2609     return this->u_.type_declaration;
2610   }
2611 
2612   Variable*
var_value()2613   var_value()
2614   {
2615     go_assert(this->classification_ == NAMED_OBJECT_VAR);
2616     return this->u_.var_value;
2617   }
2618 
2619   const Variable*
var_value()2620   var_value() const
2621   {
2622     go_assert(this->classification_ == NAMED_OBJECT_VAR);
2623     return this->u_.var_value;
2624   }
2625 
2626   Result_variable*
result_var_value()2627   result_var_value()
2628   {
2629     go_assert(this->classification_ == NAMED_OBJECT_RESULT_VAR);
2630     return this->u_.result_var_value;
2631   }
2632 
2633   const Result_variable*
result_var_value()2634   result_var_value() const
2635   {
2636     go_assert(this->classification_ == NAMED_OBJECT_RESULT_VAR);
2637     return this->u_.result_var_value;
2638   }
2639 
2640   Function*
func_value()2641   func_value()
2642   {
2643     go_assert(this->classification_ == NAMED_OBJECT_FUNC);
2644     return this->u_.func_value;
2645   }
2646 
2647   const Function*
func_value()2648   func_value() const
2649   {
2650     go_assert(this->classification_ == NAMED_OBJECT_FUNC);
2651     return this->u_.func_value;
2652   }
2653 
2654   Function_declaration*
func_declaration_value()2655   func_declaration_value()
2656   {
2657     go_assert(this->classification_ == NAMED_OBJECT_FUNC_DECLARATION);
2658     return this->u_.func_declaration_value;
2659   }
2660 
2661   const Function_declaration*
func_declaration_value()2662   func_declaration_value() const
2663   {
2664     go_assert(this->classification_ == NAMED_OBJECT_FUNC_DECLARATION);
2665     return this->u_.func_declaration_value;
2666   }
2667 
2668   Package*
package_value()2669   package_value()
2670   {
2671     go_assert(this->classification_ == NAMED_OBJECT_PACKAGE);
2672     return this->u_.package_value;
2673   }
2674 
2675   const Package*
package_value()2676   package_value() const
2677   {
2678     go_assert(this->classification_ == NAMED_OBJECT_PACKAGE);
2679     return this->u_.package_value;
2680   }
2681 
2682   const std::string&
name()2683   name() const
2684   { return this->name_; }
2685 
2686   // Return the name to use in an error message.  The difference is
2687   // that if this Named_object is defined in a different package, this
2688   // will return PACKAGE.NAME.
2689   std::string
2690   message_name() const;
2691 
2692   const Package*
package()2693   package() const
2694   { return this->package_; }
2695 
2696   // Resolve an unknown value if possible.  This returns the same
2697   // Named_object or a new one.
2698   Named_object*
resolve()2699   resolve()
2700   {
2701     Named_object* ret = this;
2702     if (this->is_unknown())
2703       {
2704 	Named_object* r = this->unknown_value()->real_named_object();
2705 	if (r != NULL)
2706 	  ret = r;
2707       }
2708     return ret;
2709   }
2710 
2711   const Named_object*
resolve()2712   resolve() const
2713   {
2714     const Named_object* ret = this;
2715     if (this->is_unknown())
2716       {
2717 	const Named_object* r = this->unknown_value()->real_named_object();
2718 	if (r != NULL)
2719 	  ret = r;
2720       }
2721     return ret;
2722   }
2723 
2724   // The location where this object was defined or referenced.
2725   Location
2726   location() const;
2727 
2728   // Convert a variable to the backend representation.
2729   Bvariable*
2730   get_backend_variable(Gogo*, Named_object* function);
2731 
2732   // Return the external identifier for this object.
2733   std::string
2734   get_id(Gogo*);
2735 
2736   // Get the backend representation of this object.
2737   void
2738   get_backend(Gogo*, std::vector<Bexpression*>&, std::vector<Btype*>&,
2739               std::vector<Bfunction*>&);
2740 
2741   // Define a type declaration.
2742   void
2743   set_type_value(Named_type*);
2744 
2745   // Define a function declaration.
2746   void
2747   set_function_value(Function*);
2748 
2749   // Declare an unknown name as a type declaration.
2750   void
2751   declare_as_type();
2752 
2753   // Export this object.
2754   void
2755   export_named_object(Export*) const;
2756 
2757   // Mark this named object as an invalid redefinition of another object.
2758   void
set_is_redefinition()2759   set_is_redefinition()
2760   { this->is_redefinition_ = true; }
2761 
2762   // Return whether or not this object is a invalid redefinition of another
2763   // object.
2764   bool
is_redefinition()2765   is_redefinition() const
2766   { return this->is_redefinition_; }
2767 
2768  private:
2769   Named_object(const std::string&, const Package*, Classification);
2770 
2771   // The name of the object.
2772   std::string name_;
2773   // The package that this object is in.  This is NULL if it is in the
2774   // file we are compiling.
2775   const Package* package_;
2776   // The type of object this is.
2777   Classification classification_;
2778   // The real data.
2779   union
2780   {
2781     Unknown_name* unknown_value;
2782     Named_constant* const_value;
2783     Named_type* type_value;
2784     Type_declaration* type_declaration;
2785     Variable* var_value;
2786     Result_variable* result_var_value;
2787     Function* func_value;
2788     Function_declaration* func_declaration_value;
2789     Package* package_value;
2790   } u_;
2791   // True if this object is an invalid redefinition of another object.
2792   bool is_redefinition_;
2793 };
2794 
2795 // A binding contour.  This binds names to objects.
2796 
2797 class Bindings
2798 {
2799  public:
2800   // Type for mapping from names to objects.
2801   typedef Unordered_map(std::string, Named_object*) Contour;
2802 
2803   Bindings(Bindings* enclosing);
2804 
2805   // Add an erroneous name.
2806   Named_object*
add_erroneous_name(const std::string & name)2807   add_erroneous_name(const std::string& name)
2808   { return this->add_named_object(Named_object::make_erroneous_name(name)); }
2809 
2810   // Add an unknown name.
2811   Named_object*
add_unknown_name(const std::string & name,Location location)2812   add_unknown_name(const std::string& name, Location location)
2813   {
2814     return this->add_named_object(Named_object::make_unknown_name(name,
2815 								  location));
2816   }
2817 
2818   // Add a constant.
2819   Named_object*
add_constant(const Typed_identifier & tid,const Package * package,Expression * expr,int iota_value)2820   add_constant(const Typed_identifier& tid, const Package* package,
2821 	       Expression* expr, int iota_value)
2822   {
2823     return this->add_named_object(Named_object::make_constant(tid, package,
2824 							      expr,
2825 							      iota_value));
2826   }
2827 
2828   // Add a type.
2829   Named_object*
add_type(const std::string & name,const Package * package,Type * type,Location location)2830   add_type(const std::string& name, const Package* package, Type* type,
2831 	   Location location)
2832   {
2833     return this->add_named_object(Named_object::make_type(name, package, type,
2834 							  location));
2835   }
2836 
2837   // Add a named type.  This is used for builtin types, and to add an
2838   // imported type to the global scope.
2839   Named_object*
2840   add_named_type(Named_type* named_type);
2841 
2842   // Add a type declaration.
2843   Named_object*
add_type_declaration(const std::string & name,const Package * package,Location location)2844   add_type_declaration(const std::string& name, const Package* package,
2845 		       Location location)
2846   {
2847     Named_object* no = Named_object::make_type_declaration(name, package,
2848 							   location);
2849     return this->add_named_object(no);
2850   }
2851 
2852   // Add a variable.
2853   Named_object*
add_variable(const std::string & name,const Package * package,Variable * variable)2854   add_variable(const std::string& name, const Package* package,
2855 	       Variable* variable)
2856   {
2857     return this->add_named_object(Named_object::make_variable(name, package,
2858 							      variable));
2859   }
2860 
2861   // Add a result variable.
2862   Named_object*
add_result_variable(const std::string & name,Result_variable * result)2863   add_result_variable(const std::string& name, Result_variable* result)
2864   {
2865     return this->add_named_object(Named_object::make_result_variable(name,
2866 								     result));
2867   }
2868 
2869   // Add a function.
2870   Named_object*
2871   add_function(const std::string& name, const Package*, Function* function);
2872 
2873   // Add a function declaration.
2874   Named_object*
2875   add_function_declaration(const std::string& name, const Package* package,
2876 			   Function_type* type, Location location);
2877 
2878   // Add a package.  The location is the location of the import
2879   // statement.
2880   Named_object*
add_package(const std::string & alias,Package * package)2881   add_package(const std::string& alias, Package* package)
2882   {
2883     Named_object* no = Named_object::make_package(alias, package);
2884     return this->add_named_object(no);
2885   }
2886 
2887   // Define a type which was already declared.
2888   void
2889   define_type(Named_object*, Named_type*);
2890 
2891   // Add a method to the list of objects.  This is not added to the
2892   // lookup table.
2893   void
2894   add_method(Named_object*);
2895 
2896   // Add a named object to this binding.
2897   Named_object*
add_named_object(Named_object * no)2898   add_named_object(Named_object* no)
2899   { return this->add_named_object_to_contour(&this->bindings_, no); }
2900 
2901   // Clear all names in file scope from the bindings.
2902   void
2903   clear_file_scope(Gogo*);
2904 
2905   // Look up a name in this binding contour and in any enclosing
2906   // binding contours.  This returns NULL if the name is not found.
2907   Named_object*
2908   lookup(const std::string&) const;
2909 
2910   // Look up a name in this binding contour without looking in any
2911   // enclosing binding contours.  Returns NULL if the name is not found.
2912   Named_object*
2913   lookup_local(const std::string&) const;
2914 
2915   // Remove a name.
2916   void
2917   remove_binding(Named_object*);
2918 
2919   // Mark all variables as used.  This is used for some types of parse
2920   // error.
2921   void
2922   mark_locals_used();
2923 
2924   // Traverse the tree.  See the Traverse class.
2925   int
2926   traverse(Traverse*, bool is_global);
2927 
2928   // Iterate over definitions.  This does not include things which
2929   // were only declared.
2930 
2931   typedef std::vector<Named_object*>::const_iterator
2932     const_definitions_iterator;
2933 
2934   const_definitions_iterator
begin_definitions()2935   begin_definitions() const
2936   { return this->named_objects_.begin(); }
2937 
2938   const_definitions_iterator
end_definitions()2939   end_definitions() const
2940   { return this->named_objects_.end(); }
2941 
2942   // Return the number of definitions.
2943   size_t
size_definitions()2944   size_definitions() const
2945   { return this->named_objects_.size(); }
2946 
2947   // Return whether there are no definitions.
2948   bool
empty_definitions()2949   empty_definitions() const
2950   { return this->named_objects_.empty(); }
2951 
2952   // Iterate over declarations.  This is everything that has been
2953   // declared, which includes everything which has been defined.
2954 
2955   typedef Contour::const_iterator const_declarations_iterator;
2956 
2957   const_declarations_iterator
begin_declarations()2958   begin_declarations() const
2959   { return this->bindings_.begin(); }
2960 
2961   const_declarations_iterator
end_declarations()2962   end_declarations() const
2963   { return this->bindings_.end(); }
2964 
2965   // Return the number of declarations.
2966   size_t
size_declarations()2967   size_declarations() const
2968   { return this->bindings_.size(); }
2969 
2970   // Return whether there are no declarations.
2971   bool
empty_declarations()2972   empty_declarations() const
2973   { return this->bindings_.empty(); }
2974 
2975   // Return the first declaration.
2976   Named_object*
first_declaration()2977   first_declaration()
2978   { return this->bindings_.empty() ? NULL : this->bindings_.begin()->second; }
2979 
2980  private:
2981   Named_object*
2982   add_named_object_to_contour(Contour*, Named_object*);
2983 
2984   Named_object*
2985   new_definition(Named_object*, Named_object*);
2986 
2987   // Enclosing bindings.
2988   Bindings* enclosing_;
2989   // The list of objects.
2990   std::vector<Named_object*> named_objects_;
2991   // The mapping from names to objects.
2992   Contour bindings_;
2993 };
2994 
2995 // A label.
2996 
2997 class Label
2998 {
2999  public:
Label(const std::string & name)3000   Label(const std::string& name)
3001     : name_(name), location_(Linemap::unknown_location()), snapshot_(NULL),
3002       refs_(), is_used_(false), blabel_(NULL), depth_(DEPTH_UNKNOWN)
3003   { }
3004 
3005   // Return the label's name.
3006   const std::string&
name()3007   name() const
3008   { return this->name_; }
3009 
3010   // Return whether the label has been defined.
3011   bool
is_defined()3012   is_defined() const
3013   { return !Linemap::is_unknown_location(this->location_); }
3014 
3015   // Return whether the label has been used.
3016   bool
is_used()3017   is_used() const
3018   { return this->is_used_; }
3019 
3020   // Record that the label is used.
3021   void
set_is_used()3022   set_is_used()
3023   { this->is_used_ = true; }
3024 
3025   // Return whether this label is looping.
3026   bool
looping()3027   looping() const
3028   { return this->depth_ == DEPTH_LOOPING; }
3029 
3030   // Set this label as looping.
3031   void
set_looping()3032   set_looping()
3033   { this->depth_ = DEPTH_LOOPING; }
3034 
3035   // Return whether this label is nonlooping.
3036   bool
nonlooping()3037   nonlooping() const
3038   { return this->depth_ == DEPTH_NONLOOPING; }
3039 
3040   // Set this label as nonlooping.
3041   void
set_nonlooping()3042   set_nonlooping()
3043   { this->depth_ = DEPTH_NONLOOPING; }
3044 
3045   // Return the location of the definition.
3046   Location
location()3047   location() const
3048   { return this->location_; }
3049 
3050   // Return the bindings snapshot.
3051   Bindings_snapshot*
snapshot()3052   snapshot() const
3053   { return this->snapshot_; }
3054 
3055   // Add a snapshot of a goto which refers to this label.
3056   void
add_snapshot_ref(Bindings_snapshot * snapshot)3057   add_snapshot_ref(Bindings_snapshot* snapshot)
3058   {
3059     go_assert(Linemap::is_unknown_location(this->location_));
3060     this->refs_.push_back(snapshot);
3061   }
3062 
3063   // Return the list of snapshots of goto statements which refer to
3064   // this label.
3065   const std::vector<Bindings_snapshot*>&
refs()3066   refs() const
3067   { return this->refs_; }
3068 
3069   // Clear the references.
3070   void
3071   clear_refs();
3072 
3073   // Define the label at LOCATION with the given bindings snapshot.
3074   void
define(Location location,Bindings_snapshot * snapshot)3075   define(Location location, Bindings_snapshot* snapshot)
3076   {
3077     if (this->is_dummy_label())
3078       return;
3079     go_assert(Linemap::is_unknown_location(this->location_)
3080               && this->snapshot_ == NULL);
3081     this->location_ = location;
3082     this->snapshot_ = snapshot;
3083   }
3084 
3085   // Return the backend representation for this label.
3086   Blabel*
3087   get_backend_label(Translate_context*);
3088 
3089   // Return an expression for the address of this label.  This is used
3090   // to get the return address of a deferred function to see whether
3091   // the function may call recover.
3092   Bexpression*
3093   get_addr(Translate_context*, Location location);
3094 
3095   // Return a dummy label, representing any instance of the blank label.
3096   static Label*
3097   create_dummy_label();
3098 
3099   // Return TRUE if this is a dummy label.
3100   bool
is_dummy_label()3101   is_dummy_label() const
3102   { return this->name_ == "_"; }
3103 
3104   // A classification of a label's looping depth.
3105   enum Loop_depth
3106   {
3107     DEPTH_UNKNOWN,
3108     // A label never jumped to.
3109     DEPTH_NONLOOPING,
3110     // A label jumped to.
3111     DEPTH_LOOPING
3112   };
3113 
3114  private:
3115   // The name of the label.
3116   std::string name_;
3117   // The location of the definition.  This is 0 if the label has not
3118   // yet been defined.
3119   Location location_;
3120   // A snapshot of the set of bindings defined at this label, used to
3121   // issue errors about invalid goto statements.
3122   Bindings_snapshot* snapshot_;
3123   // A list of snapshots of goto statements which refer to this label.
3124   std::vector<Bindings_snapshot*> refs_;
3125   // Whether the label has been used.
3126   bool is_used_;
3127   // The backend representation.
3128   Blabel* blabel_;
3129   // The looping depth of this label, for escape analysis.
3130   Loop_depth depth_;
3131 };
3132 
3133 // An unnamed label.  These are used when lowering loops.
3134 
3135 class Unnamed_label
3136 {
3137  public:
Unnamed_label(Location location)3138   Unnamed_label(Location location)
3139     : location_(location), derived_from_(NULL), blabel_(NULL)
3140   { }
3141 
3142   // Get the location where the label is defined.
3143   Location
location()3144   location() const
3145   { return this->location_; }
3146 
3147   // Set the location where the label is defined.
3148   void
set_location(Location location)3149   set_location(Location location)
3150   { this->location_ = location; }
3151 
3152   // Get the top level statement this unnamed label is derived from.
3153   Statement*
derived_from()3154   derived_from() const
3155   { return this->derived_from_; }
3156 
3157   // Set the top level statement this unnamed label is derived from.
3158   void
set_derived_from(Statement * s)3159   set_derived_from(Statement* s)
3160   { this->derived_from_ = s; }
3161 
3162   // Return a statement which defines this label.
3163   Bstatement*
3164   get_definition(Translate_context*);
3165 
3166   // Return a goto to this label from LOCATION.
3167   Bstatement*
3168   get_goto(Translate_context*, Location location);
3169 
3170  private:
3171   // Return the backend representation.
3172   Blabel*
3173   get_blabel(Translate_context*);
3174 
3175   // The location where the label is defined.
3176   Location location_;
3177   // The top-level statement this unnamed label was derived/lowered from.
3178   // This is NULL is this label is not the top-level of a lowered statement.
3179   Statement* derived_from_;
3180   // The backend representation of this label.
3181   Blabel* blabel_;
3182 };
3183 
3184 // An alias for an imported package.
3185 
3186 class Package_alias
3187 {
3188  public:
Package_alias(Location location)3189   Package_alias(Location location)
3190       : location_(location), used_(0)
3191   { }
3192 
3193   // The location of the import statement.
3194   Location
location()3195   location()
3196   { return this->location_; }
3197 
3198   // How many symbols from the package were used under this alias.
3199   size_t
used()3200   used() const
3201   { return this->used_; }
3202 
3203   // Note that some symbol was used under this alias.
3204   void
note_usage()3205   note_usage()
3206   { this->used_++; }
3207 
3208  private:
3209   // The location of the import statement.
3210   Location location_;
3211   // The amount of times some name from this package was used under this alias.
3212   size_t used_;
3213 };
3214 
3215 // An imported package.
3216 
3217 class Package
3218 {
3219  public:
3220   Package(const std::string& pkgpath, const std::string& pkgpath_symbol,
3221 	  Location location);
3222 
3223   // Get the package path used for all symbols exported from this
3224   // package.
3225   const std::string&
pkgpath()3226   pkgpath() const
3227   { return this->pkgpath_; }
3228 
3229   // Return the package path to use for a symbol name.
3230   std::string
3231   pkgpath_symbol() const;
3232 
3233   // Set the package path symbol.
3234   void
3235   set_pkgpath_symbol(const std::string&);
3236 
3237   // Return the location of the most recent import statement.
3238   Location
location()3239   location() const
3240   { return this->location_; }
3241 
3242   // Return whether we know the name of this package yet.
3243   bool
has_package_name()3244   has_package_name() const
3245   { return !this->package_name_.empty(); }
3246 
3247   // The name that this package uses in its package clause.  This may
3248   // be different from the name in the associated Named_object if the
3249   // import statement used an alias.
3250   const std::string&
package_name()3251   package_name() const
3252   {
3253     go_assert(!this->package_name_.empty());
3254     return this->package_name_;
3255   }
3256 
3257   // Return the bindings.
3258   Bindings*
bindings()3259   bindings() const
3260   { return this->bindings_; }
3261 
3262   // Type used to map import names to package aliases.
3263   typedef std::map<std::string, Package_alias*> Aliases;
3264 
3265   // Return the set of package aliases.
3266   const Aliases&
aliases()3267   aliases() const
3268   { return this->aliases_; }
3269 
3270   // Note that some symbol from this package was used and qualified by ALIAS.
3271   // For dot imports, the ALIAS should be ".PACKAGE_NAME".
3272   void
3273   note_usage(const std::string& alias) const;
3274 
3275   // Note that USAGE might be a fake usage of this package.
3276   void
note_fake_usage(Expression * usage)3277   note_fake_usage(Expression* usage) const
3278   { this->fake_uses_.insert(usage); }
3279 
3280   // Forget a given USAGE of this package.
3281   void
3282   forget_usage(Expression* usage) const;
3283 
3284   // Clear the used field for the next file.
3285   void
3286   clear_used();
3287 
3288   // Look up a name in the package.  Returns NULL if the name is not
3289   // found.
3290   Named_object*
lookup(const std::string & name)3291   lookup(const std::string& name) const
3292   { return this->bindings_->lookup(name); }
3293 
3294   // Set the name of the package.
3295   void
3296   set_package_name(const std::string& name, Location);
3297 
3298   // Set the location of the package.  This is used to record the most
3299   // recent import location.
3300   void
set_location(Location location)3301   set_location(Location location)
3302   { this->location_ = location; }
3303 
3304   // Add a package name as an ALIAS for this package.
3305   Package_alias*
3306   add_alias(const std::string& alias, Location);
3307 
3308   // Add a constant to the package.
3309   Named_object*
add_constant(const Typed_identifier & tid,Expression * expr)3310   add_constant(const Typed_identifier& tid, Expression* expr)
3311   { return this->bindings_->add_constant(tid, this, expr, 0); }
3312 
3313   // Add a type to the package.
3314   Named_object*
add_type(const std::string & name,Type * type,Location location)3315   add_type(const std::string& name, Type* type, Location location)
3316   { return this->bindings_->add_type(name, this, type, location); }
3317 
3318   // Add a type declaration to the package.
3319   Named_object*
add_type_declaration(const std::string & name,Location location)3320   add_type_declaration(const std::string& name, Location location)
3321   { return this->bindings_->add_type_declaration(name, this, location); }
3322 
3323   // Add a variable to the package.
3324   Named_object*
add_variable(const std::string & name,Variable * variable)3325   add_variable(const std::string& name, Variable* variable)
3326   { return this->bindings_->add_variable(name, this, variable); }
3327 
3328   // Add a function declaration to the package.
3329   Named_object*
add_function_declaration(const std::string & name,Function_type * type,Location loc)3330   add_function_declaration(const std::string& name, Function_type* type,
3331 			   Location loc)
3332   { return this->bindings_->add_function_declaration(name, this, type, loc); }
3333 
3334   // Determine types of constants.
3335   void
3336   determine_types();
3337 
3338  private:
3339   // The package path for type reflection data.
3340   std::string pkgpath_;
3341   // The package path for symbol names.
3342   std::string pkgpath_symbol_;
3343   // The name that this package uses in the package clause.  This may
3344   // be the empty string if it is not yet known.
3345   std::string package_name_;
3346   // The names in this package.
3347   Bindings* bindings_;
3348   // The location of the most recent import statement.
3349   Location location_;
3350   // The set of aliases associated with this package.
3351   Aliases aliases_;
3352   // A set of possibly fake uses of this package. This is mutable because we
3353   // can track fake uses of a package even if we have a const pointer to it.
3354   mutable std::set<Expression*> fake_uses_;
3355 };
3356 
3357 // Return codes for the traversal functions.  This is not an enum
3358 // because we want to be able to declare traversal functions in other
3359 // header files without including this one.
3360 
3361 // Continue traversal as usual.
3362 const int TRAVERSE_CONTINUE = -1;
3363 
3364 // Exit traversal.
3365 const int TRAVERSE_EXIT = 0;
3366 
3367 // Continue traversal, but skip components of the current object.
3368 // E.g., if this is returned by Traverse::statement, we do not
3369 // traverse the expressions in the statement even if
3370 // traverse_expressions is set in the traverse_mask.
3371 const int TRAVERSE_SKIP_COMPONENTS = 1;
3372 
3373 // This class is used when traversing the parse tree.  The caller uses
3374 // a subclass which overrides functions as desired.
3375 
3376 class Traverse
3377 {
3378  public:
3379   // These bitmasks say what to traverse.
3380   static const unsigned int traverse_variables =          0x1;
3381   static const unsigned int traverse_constants =          0x2;
3382   static const unsigned int traverse_functions =          0x4;
3383   static const unsigned int traverse_blocks =             0x8;
3384   static const unsigned int traverse_statements =        0x10;
3385   static const unsigned int traverse_expressions =       0x20;
3386   static const unsigned int traverse_types =             0x40;
3387   static const unsigned int traverse_func_declarations = 0x80;
3388 
Traverse(unsigned int traverse_mask)3389   Traverse(unsigned int traverse_mask)
3390     : traverse_mask_(traverse_mask), types_seen_(NULL), expressions_seen_(NULL)
3391   { }
3392 
3393   virtual ~Traverse();
3394 
3395   // The bitmask of what to traverse.
3396   unsigned int
traverse_mask()3397   traverse_mask() const
3398   { return this->traverse_mask_; }
3399 
3400   // Record that we are going to traverse a type.  This returns true
3401   // if the type has already been seen in this traversal.  This is
3402   // required because types, unlike expressions, can form a circular
3403   // graph.
3404   bool
3405   remember_type(const Type*);
3406 
3407   // Record that we are going to see an expression.  This returns true
3408   // if the expression has already been seen in this traversal.  This
3409   // is only needed for cases where multiple expressions can point to
3410   // a single one.
3411   bool
3412   remember_expression(const Expression*);
3413 
3414   // These functions return one of the TRAVERSE codes defined above.
3415 
3416   // If traverse_variables is set in the mask, this is called for
3417   // every variable in the tree.
3418   virtual int
3419   variable(Named_object*);
3420 
3421   // If traverse_constants is set in the mask, this is called for
3422   // every named constant in the tree.  The bool parameter is true for
3423   // a global constant.
3424   virtual int
3425   constant(Named_object*, bool);
3426 
3427   // If traverse_functions is set in the mask, this is called for
3428   // every function in the tree.
3429   virtual int
3430   function(Named_object*);
3431 
3432   // If traverse_blocks is set in the mask, this is called for every
3433   // block in the tree.
3434   virtual int
3435   block(Block*);
3436 
3437   // If traverse_statements is set in the mask, this is called for
3438   // every statement in the tree.
3439   virtual int
3440   statement(Block*, size_t* index, Statement*);
3441 
3442   // If traverse_expressions is set in the mask, this is called for
3443   // every expression in the tree.
3444   virtual int
3445   expression(Expression**);
3446 
3447   // If traverse_types is set in the mask, this is called for every
3448   // type in the tree.
3449   virtual int
3450   type(Type*);
3451 
3452   // If traverse_func_declarations is set in the mask, this is called
3453   // for every function declarations in the tree.
3454   virtual int
3455   function_declaration(Named_object*);
3456 
3457  private:
3458   // A hash table for types we have seen during this traversal.  Note
3459   // that this uses the default hash functions for pointers rather
3460   // than Type_hash_identical and Type_identical.  This is because for
3461   // traversal we care about seeing a specific type structure.  If
3462   // there are two separate instances of identical types, we want to
3463   // traverse both.
3464   typedef Unordered_set(const Type*) Types_seen;
3465 
3466   typedef Unordered_set(const Expression*) Expressions_seen;
3467 
3468   // Bitmask of what sort of objects to traverse.
3469   unsigned int traverse_mask_;
3470   // Types which have been seen in this traversal.
3471   Types_seen* types_seen_;
3472   // Expressions which have been seen in this traversal.
3473   Expressions_seen* expressions_seen_;
3474 };
3475 
3476 // A class which makes it easier to insert new statements before the
3477 // current statement during a traversal.
3478 
3479 class Statement_inserter
3480 {
3481  public:
3482   typedef Unordered_set(Statement*) Statements;
3483 
3484   // Empty constructor.
Statement_inserter()3485   Statement_inserter()
3486       : block_(NULL), pindex_(NULL), gogo_(NULL), var_(NULL),
3487         statements_added_(NULL)
3488   { }
3489 
3490   // Constructor for a statement in a block.
3491   Statement_inserter(Block* block, size_t *pindex, Statements *added = NULL)
block_(block)3492       : block_(block), pindex_(pindex), gogo_(NULL), var_(NULL),
3493         statements_added_(added)
3494   { }
3495 
3496   // Constructor for a global variable.
3497   Statement_inserter(Gogo* gogo, Variable* var, Statements *added = NULL)
block_(NULL)3498       : block_(NULL), pindex_(NULL), gogo_(gogo), var_(var),
3499         statements_added_(added)
3500   { go_assert(var->is_global()); }
3501 
3502   // We use the default copy constructor and assignment operator.
3503 
3504   // Insert S before the statement we are traversing, or before the
3505   // initialization expression of a global variable.
3506   void
3507   insert(Statement* s);
3508 
3509  private:
3510   // The block that the statement is in.
3511   Block* block_;
3512   // The index of the statement that we are traversing.
3513   size_t* pindex_;
3514   // The IR, needed when looking at an initializer expression for a
3515   // global variable.
3516   Gogo* gogo_;
3517   // The global variable, when looking at an initializer expression.
3518   Variable* var_;
3519   // If non-null, a set to record new statements inserted (non-owned).
3520   Statements* statements_added_;
3521 };
3522 
3523 // When translating the gogo IR into the backend data structure, this
3524 // is the context we pass down the blocks and statements.
3525 
3526 class Translate_context
3527 {
3528  public:
Translate_context(Gogo * gogo,Named_object * function,Block * block,Bblock * bblock)3529   Translate_context(Gogo* gogo, Named_object* function, Block* block,
3530 		    Bblock* bblock)
3531     : gogo_(gogo), backend_(gogo->backend()), function_(function),
3532       block_(block), bblock_(bblock), is_const_(false)
3533   { }
3534 
3535   // Accessors.
3536 
3537   Gogo*
gogo()3538   gogo()
3539   { return this->gogo_; }
3540 
3541   Backend*
backend()3542   backend()
3543   { return this->backend_; }
3544 
3545   Named_object*
function()3546   function()
3547   { return this->function_; }
3548 
3549   Block*
block()3550   block()
3551   { return this->block_; }
3552 
3553   Bblock*
bblock()3554   bblock()
3555   { return this->bblock_; }
3556 
3557   bool
is_const()3558   is_const()
3559   { return this->is_const_; }
3560 
3561   // Make a constant context.
3562   void
set_is_const()3563   set_is_const()
3564   { this->is_const_ = true; }
3565 
3566  private:
3567   // The IR for the entire compilation unit.
3568   Gogo* gogo_;
3569   // The generator for the backend data structures.
3570   Backend* backend_;
3571   // The function we are currently translating.  NULL if not in a
3572   // function, e.g., the initializer of a global variable.
3573   Named_object* function_;
3574   // The block we are currently translating.  NULL if not in a
3575   // function.
3576   Block *block_;
3577   // The backend representation of the current block.  NULL if block_
3578   // is NULL.
3579   Bblock* bblock_;
3580   // Whether this is being evaluated in a constant context.  This is
3581   // used for type descriptor initializers.
3582   bool is_const_;
3583 };
3584 
3585 // Runtime error codes.  These must match the values in
3586 // libgo/runtime/go-runtime-error.c.
3587 
3588 // Slice index out of bounds: negative or larger than the length of
3589 // the slice.
3590 static const int RUNTIME_ERROR_SLICE_INDEX_OUT_OF_BOUNDS = 0;
3591 
3592 // Array index out of bounds.
3593 static const int RUNTIME_ERROR_ARRAY_INDEX_OUT_OF_BOUNDS = 1;
3594 
3595 // String index out of bounds.
3596 static const int RUNTIME_ERROR_STRING_INDEX_OUT_OF_BOUNDS = 2;
3597 
3598 // Slice slice out of bounds: negative or larger than the length of
3599 // the slice or high bound less than low bound.
3600 static const int RUNTIME_ERROR_SLICE_SLICE_OUT_OF_BOUNDS = 3;
3601 
3602 // Array slice out of bounds.
3603 static const int RUNTIME_ERROR_ARRAY_SLICE_OUT_OF_BOUNDS = 4;
3604 
3605 // String slice out of bounds.
3606 static const int RUNTIME_ERROR_STRING_SLICE_OUT_OF_BOUNDS = 5;
3607 
3608 // Dereference of nil pointer.  This is used when there is a
3609 // dereference of a pointer to a very large struct or array, to ensure
3610 // that a gigantic array is not used a proxy to access random memory
3611 // locations.
3612 static const int RUNTIME_ERROR_NIL_DEREFERENCE = 6;
3613 
3614 // Slice length or capacity out of bounds in make: negative or
3615 // overflow or length greater than capacity.
3616 static const int RUNTIME_ERROR_MAKE_SLICE_OUT_OF_BOUNDS = 7;
3617 
3618 // Map capacity out of bounds in make: negative or overflow.
3619 static const int RUNTIME_ERROR_MAKE_MAP_OUT_OF_BOUNDS = 8;
3620 
3621 // Channel capacity out of bounds in make: negative or overflow.
3622 static const int RUNTIME_ERROR_MAKE_CHAN_OUT_OF_BOUNDS = 9;
3623 
3624 // Division by zero.
3625 static const int RUNTIME_ERROR_DIVISION_BY_ZERO = 10;
3626 
3627 // Go statement with nil function.
3628 static const int RUNTIME_ERROR_GO_NIL = 11;
3629 
3630 // This is used by some of the langhooks.
3631 extern Gogo* go_get_gogo();
3632 
3633 // Whether we have seen any errors.  FIXME: Replace with a backend
3634 // interface.
3635 extern bool saw_errors();
3636 
3637 #endif // !defined(GO_GOGO_H)
3638