OSDN Git Service

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