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