OSDN Git Service

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