OSDN Git Service

Make sure variable type is determined when var initialized to var.
[pf3gnuchains/gcc-fork.git] / gcc / go / gofrontend / expressions.h
1 // expressions.h -- Go frontend expression handling.     -*- 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_EXPRESSIONS_H
8 #define GO_EXPRESSIONS_H
9
10 #include <gmp.h>
11 #include <mpfr.h>
12
13 #include "operator.h"
14
15 class Gogo;
16 class Translate_context;
17 class Traverse;
18 class Type;
19 struct Type_context;
20 class Function_type;
21 class Map_type;
22 class Struct_type;
23 class Struct_field;
24 class Expression_list;
25 class Var_expression;
26 class Temporary_reference_expression;
27 class String_expression;
28 class Binary_expression;
29 class Call_expression;
30 class Func_expression;
31 class Unknown_expression;
32 class Index_expression;
33 class Map_index_expression;
34 class Bound_method_expression;
35 class Field_reference_expression;
36 class Interface_field_reference_expression;
37 class Type_guard_expression;
38 class Receive_expression;
39 class Send_expression;
40 class Named_object;
41 class Export;
42 class Import;
43 class Temporary_statement;
44 class Label;
45
46 // The base class for all expressions.
47
48 class Expression
49 {
50  public:
51   // The types of expressions.
52   enum Expression_classification
53   {
54     EXPRESSION_ERROR,
55     EXPRESSION_TYPE,
56     EXPRESSION_UNARY,
57     EXPRESSION_BINARY,
58     EXPRESSION_CONST_REFERENCE,
59     EXPRESSION_VAR_REFERENCE,
60     EXPRESSION_TEMPORARY_REFERENCE,
61     EXPRESSION_SINK,
62     EXPRESSION_FUNC_REFERENCE,
63     EXPRESSION_UNKNOWN_REFERENCE,
64     EXPRESSION_BOOLEAN,
65     EXPRESSION_STRING,
66     EXPRESSION_INTEGER,
67     EXPRESSION_FLOAT,
68     EXPRESSION_COMPLEX,
69     EXPRESSION_NIL,
70     EXPRESSION_IOTA,
71     EXPRESSION_CALL,
72     EXPRESSION_CALL_RESULT,
73     EXPRESSION_BOUND_METHOD,
74     EXPRESSION_INDEX,
75     EXPRESSION_ARRAY_INDEX,
76     EXPRESSION_STRING_INDEX,
77     EXPRESSION_MAP_INDEX,
78     EXPRESSION_SELECTOR,
79     EXPRESSION_FIELD_REFERENCE,
80     EXPRESSION_INTERFACE_FIELD_REFERENCE,
81     EXPRESSION_ALLOCATION,
82     EXPRESSION_MAKE,
83     EXPRESSION_TYPE_GUARD,
84     EXPRESSION_CONVERSION,
85     EXPRESSION_STRUCT_CONSTRUCTION,
86     EXPRESSION_FIXED_ARRAY_CONSTRUCTION,
87     EXPRESSION_OPEN_ARRAY_CONSTRUCTION,
88     EXPRESSION_MAP_CONSTRUCTION,
89     EXPRESSION_COMPOSITE_LITERAL,
90     EXPRESSION_HEAP_COMPOSITE,
91     EXPRESSION_RECEIVE,
92     EXPRESSION_SEND,
93     EXPRESSION_TYPE_DESCRIPTOR,
94     EXPRESSION_TYPE_INFO,
95     EXPRESSION_STRUCT_FIELD_OFFSET,
96     EXPRESSION_LABEL_ADDR
97   };
98
99   Expression(Expression_classification, source_location);
100
101   virtual ~Expression();
102
103   // Make an error expression.  This is used when a parse error occurs
104   // to prevent cascading errors.
105   static Expression*
106   make_error(source_location);
107
108   // Make an expression which is really a type.  This is used during
109   // parsing.
110   static Expression*
111   make_type(Type*, source_location);
112
113   // Make a unary expression.
114   static Expression*
115   make_unary(Operator, Expression*, source_location);
116
117   // Make a binary expression.
118   static Expression*
119   make_binary(Operator, Expression*, Expression*, source_location);
120
121   // Make a reference to a constant in an expression.
122   static Expression*
123   make_const_reference(Named_object*, source_location);
124
125   // Make a reference to a variable in an expression.
126   static Expression*
127   make_var_reference(Named_object*, source_location);
128
129   // Make a reference to a temporary variable.  Temporary variables
130   // are always created by a single statement, which is what we use to
131   // refer to them.
132   static Expression*
133   make_temporary_reference(Temporary_statement*, source_location);
134
135   // Make a sink expression--a reference to the blank identifier _.
136   static Expression*
137   make_sink(source_location);
138
139   // Make a reference to a function in an expression.
140   static Expression*
141   make_func_reference(Named_object*, Expression* closure, source_location);
142
143   // Make a reference to an unknown name.  In a correct program this
144   // will always be lowered to a real const/var/func reference.
145   static Expression*
146   make_unknown_reference(Named_object*, source_location);
147
148   // Make a constant bool expression.
149   static Expression*
150   make_boolean(bool val, source_location);
151
152   // Make a constant string expression.
153   static Expression*
154   make_string(const std::string&, source_location);
155
156   // Make a constant integer expression.  TYPE should be NULL for an
157   // abstract type.
158   static Expression*
159   make_integer(const mpz_t*, Type*, source_location);
160
161   // Make a constant float expression.  TYPE should be NULL for an
162   // abstract type.
163   static Expression*
164   make_float(const mpfr_t*, Type*, source_location);
165
166   // Make a constant complex expression.  TYPE should be NULL for an
167   // abstract type.
168   static Expression*
169   make_complex(const mpfr_t* real, const mpfr_t* imag, Type*, source_location);
170
171   // Make a nil expression.
172   static Expression*
173   make_nil(source_location);
174
175   // Make an iota expression.  This is used for the predeclared
176   // constant iota.
177   static Expression*
178   make_iota();
179
180   // Make a call expression.
181   static Call_expression*
182   make_call(Expression* func, Expression_list* args, bool is_varargs,
183             source_location);
184
185   // Make a reference to a specific result of a call expression which
186   // returns a tuple.
187   static Expression*
188   make_call_result(Call_expression*, unsigned int index);
189
190   // Make an expression which is a method bound to its first
191   // parameter.
192   static Bound_method_expression*
193   make_bound_method(Expression* object, Expression* method, source_location);
194
195   // Make an index or slice expression.  This is a parser expression
196   // which represents LEFT[START:END].  END may be NULL, meaning an
197   // index rather than a slice.  At parse time we may not know the
198   // type of LEFT.  After parsing this is lowered to an array index, a
199   // string index, or a map index.
200   static Expression*
201   make_index(Expression* left, Expression* start, Expression* end,
202              source_location);
203
204   // Make an array index expression.  END may be NULL, in which case
205   // this is an lvalue.
206   static Expression*
207   make_array_index(Expression* array, Expression* start, Expression* end,
208                    source_location);
209
210   // Make a string index expression.  END may be NULL.  This is never
211   // an lvalue.
212   static Expression*
213   make_string_index(Expression* string, Expression* start, Expression* end,
214                     source_location);
215
216   // Make a map index expression.  This is an lvalue.
217   static Map_index_expression*
218   make_map_index(Expression* map, Expression* val, source_location);
219
220   // Make a selector.  This is a parser expression which represents
221   // LEFT.NAME.  At parse time we may not know the type of the left
222   // hand side.
223   static Expression*
224   make_selector(Expression* left, const std::string& name, source_location);
225
226   // Make a reference to a field in a struct.
227   static Field_reference_expression*
228   make_field_reference(Expression*, unsigned int field_index, source_location);
229
230   // Make a reference to a field of an interface, with an associated
231   // object.
232   static Expression*
233   make_interface_field_reference(Expression*, const std::string&,
234                                  source_location);
235
236   // Make an allocation expression.
237   static Expression*
238   make_allocation(Type*, source_location);
239
240   // Make a call to the builtin function make.
241   static Expression*
242   make_make(Type*, Expression_list*, source_location);
243
244   // Make a type guard expression.
245   static Expression*
246   make_type_guard(Expression*, Type*, source_location);
247
248   // Make a type cast expression.
249   static Expression*
250   make_cast(Type*, Expression*, source_location);
251
252   // Make a composite literal.  The DEPTH parameter is how far down we
253   // are in a list of composite literals with omitted types.
254   static Expression*
255   make_composite_literal(Type*, int depth, bool has_keys, Expression_list*,
256                          source_location);
257
258   // Make a struct composite literal.
259   static Expression*
260   make_struct_composite_literal(Type*, Expression_list*, source_location);
261
262   // Make a slice composite literal.
263   static Expression*
264   make_slice_composite_literal(Type*, Expression_list*, source_location);
265
266   // Take a composite literal and allocate it on the heap.
267   static Expression*
268   make_heap_composite(Expression*, source_location);
269
270   // Make a receive expression.  VAL is NULL for a unary receive.
271   static Receive_expression*
272   make_receive(Expression* channel, source_location);
273
274   // Make a send expression.
275   static Send_expression*
276   make_send(Expression* channel, Expression* val, source_location);
277
278   // Make an expression which evaluates to the type descriptor of a
279   // type.
280   static Expression*
281   make_type_descriptor(Type* type, source_location);
282
283   // Make an expression which evaluates to some characteristic of a
284   // type.  These are only used for type descriptors, so there is no
285   // location parameter.
286   enum Type_info
287     {
288       // The size of a value of the type.
289       TYPE_INFO_SIZE,
290       // The required alignment of a value of the type.
291       TYPE_INFO_ALIGNMENT,
292       // The required alignment of a value of the type when used as a
293       // field in a struct.
294       TYPE_INFO_FIELD_ALIGNMENT
295     };
296
297   static Expression*
298   make_type_info(Type* type, Type_info);
299
300   // Make an expression which evaluates to the offset of a field in a
301   // struct.  This is only used for type descriptors, so there is no
302   // location parameter.
303   static Expression*
304   make_struct_field_offset(Struct_type*, const Struct_field*);
305
306   // Make an expression which evaluates to the address of an unnamed
307   // label.
308   static Expression*
309   make_label_addr(Label*, source_location);
310
311   // Return the expression classification.
312   Expression_classification
313   classification() const
314   { return this->classification_; }
315
316   // Return the location of the expression.
317   source_location
318   location() const
319   { return this->location_; }
320
321   // Return whether this is a constant expression.
322   bool
323   is_constant() const
324   { return this->do_is_constant(); }
325
326   // If this is not a constant expression with integral type, return
327   // false.  If it is one, return true, and set VAL to the value.  VAL
328   // should already be initialized.  If this returns true, it sets
329   // *PTYPE to the type of the value, or NULL for an abstract type.
330   // If IOTA_IS_CONSTANT is true, then an iota expression is assumed
331   // to have its final value.
332   bool
333   integer_constant_value(bool iota_is_constant, mpz_t val, Type** ptype) const;
334
335   // If this is not a constant expression with floating point type,
336   // return false.  If it is one, return true, and set VAL to the
337   // value.  VAL should already be initialized.  If this returns true,
338   // it sets *PTYPE to the type of the value, or NULL for an abstract
339   // type.
340   bool
341   float_constant_value(mpfr_t val, Type** ptype) const;
342
343   // If this is not a constant expression with complex type, return
344   // false.  If it is one, return true, and set REAL and IMAG to the
345   // value.  REAL and IMAG should already be initialized.  If this
346   // return strue, it sets *PTYPE to the type of the value, or NULL
347   // for an abstract type.
348   bool
349   complex_constant_value(mpfr_t real, mpfr_t imag, Type** ptype) const;
350
351   // If this is not a constant expression with string type, return
352   // false.  If it is one, return true, and set VAL to the value.
353   bool
354   string_constant_value(std::string* val) const
355   { return this->do_string_constant_value(val); }
356
357   // This is called by the parser if the value of this expression is
358   // being discarded.  This issues warnings about computed values
359   // being unused, and handles send expressions which act differently
360   // depending upon whether the value is used.
361   void
362   discarding_value()
363   { this->do_discarding_value(); }
364
365   // Return whether this is an error expression.
366   bool
367   is_error_expression() const
368   { return this->classification_ == EXPRESSION_ERROR; }
369
370   // Return whether this expression really represents a type.
371   bool
372   is_type_expression() const
373   { return this->classification_ == EXPRESSION_TYPE; }
374
375   // If this is a variable reference, return the Var_expression
376   // structure.  Otherwise, return NULL.  This is a controlled dynamic
377   // cast.
378   Var_expression*
379   var_expression()
380   { return this->convert<Var_expression, EXPRESSION_VAR_REFERENCE>(); }
381
382   const Var_expression*
383   var_expression() const
384   { return this->convert<const Var_expression, EXPRESSION_VAR_REFERENCE>(); }
385
386   // If this is a reference to a temporary variable, return the
387   // Temporary_reference_expression.  Otherwise, return NULL.
388   Temporary_reference_expression*
389   temporary_reference_expression()
390   {
391     return this->convert<Temporary_reference_expression,
392                          EXPRESSION_TEMPORARY_REFERENCE>();
393   }
394
395   // Return whether this is a sink expression.
396   bool
397   is_sink_expression() const
398   { return this->classification_ == EXPRESSION_SINK; }
399
400   // If this is a string expression, return the String_expression
401   // structure.  Otherwise, return NULL.
402   String_expression*
403   string_expression()
404   { return this->convert<String_expression, EXPRESSION_STRING>(); }
405
406   // Return whether this is the expression nil.
407   bool
408   is_nil_expression() const
409   { return this->classification_ == EXPRESSION_NIL; }
410
411   // If this is an indirection through a pointer, return the
412   // expression being pointed through.  Otherwise return this.
413   Expression*
414   deref();
415
416   // If this is a binary expression, return the Binary_expression
417   // structure.  Otherwise return NULL.
418   Binary_expression*
419   binary_expression()
420   { return this->convert<Binary_expression, EXPRESSION_BINARY>(); }
421
422   // If this is a call expression, return the Call_expression
423   // structure.  Otherwise, return NULL.  This is a controlled dynamic
424   // cast.
425   Call_expression*
426   call_expression()
427   { return this->convert<Call_expression, EXPRESSION_CALL>(); }
428
429   // If this is an expression which refers to a function, return the
430   // Func_expression structure.  Otherwise, return NULL.
431   Func_expression*
432   func_expression()
433   { return this->convert<Func_expression, EXPRESSION_FUNC_REFERENCE>(); }
434
435   const Func_expression*
436   func_expression() const
437   { return this->convert<const Func_expression, EXPRESSION_FUNC_REFERENCE>(); }
438
439   // If this is an expression which refers to an unknown name, return
440   // the Unknown_expression structure.  Otherwise, return NULL.
441   Unknown_expression*
442   unknown_expression()
443   { return this->convert<Unknown_expression, EXPRESSION_UNKNOWN_REFERENCE>(); }
444
445   const Unknown_expression*
446   unknown_expression() const
447   {
448     return this->convert<const Unknown_expression,
449                          EXPRESSION_UNKNOWN_REFERENCE>();
450   }
451
452   // If this is an index expression, return the Index_expression
453   // structure.  Otherwise, return NULL.
454   Index_expression*
455   index_expression()
456   { return this->convert<Index_expression, EXPRESSION_INDEX>(); }
457
458   // If this is an expression which refers to indexing in a map,
459   // return the Map_index_expression structure.  Otherwise, return
460   // NULL.
461   Map_index_expression*
462   map_index_expression()
463   { return this->convert<Map_index_expression, EXPRESSION_MAP_INDEX>(); }
464
465   // If this is a bound method expression, return the
466   // Bound_method_expression structure.  Otherwise, return NULL.
467   Bound_method_expression*
468   bound_method_expression()
469   { return this->convert<Bound_method_expression, EXPRESSION_BOUND_METHOD>(); }
470
471   // If this is a reference to a field in a struct, return the
472   // Field_reference_expression structure.  Otherwise, return NULL.
473   Field_reference_expression*
474   field_reference_expression()
475   {
476     return this->convert<Field_reference_expression,
477                          EXPRESSION_FIELD_REFERENCE>();
478   }
479
480   // If this is a reference to a field in an interface, return the
481   // Interface_field_reference_expression structure.  Otherwise,
482   // return NULL.
483   Interface_field_reference_expression*
484   interface_field_reference_expression()
485   {
486     return this->convert<Interface_field_reference_expression,
487                          EXPRESSION_INTERFACE_FIELD_REFERENCE>();
488   }
489
490   // If this is a type guard expression, return the
491   // Type_guard_expression structure.  Otherwise, return NULL.
492   Type_guard_expression*
493   type_guard_expression()
494   { return this->convert<Type_guard_expression, EXPRESSION_TYPE_GUARD>(); }
495
496   // If this is a receive expression, return the Receive_expression
497   // structure.  Otherwise, return NULL.
498   Receive_expression*
499   receive_expression()
500   { return this->convert<Receive_expression, EXPRESSION_RECEIVE>(); }
501
502   // Return true if this is a composite literal.
503   bool
504   is_composite_literal() const;
505
506   // Return true if this is a composite literal which is not constant.
507   bool
508   is_nonconstant_composite_literal() const;
509
510   // Return true if this is a reference to a local variable.
511   bool
512   is_local_variable() const;
513
514   // Traverse an expression.
515   static int
516   traverse(Expression**, Traverse*);
517
518   // Traverse subexpressions of this expression.
519   int
520   traverse_subexpressions(Traverse*);
521
522   // Lower an expression.  This is called immediately after parsing.
523   // IOTA_VALUE is the value that we should give to any iota
524   // expressions.  This function must resolve expressions which could
525   // not be fully parsed into their final form.  It returns the same
526   // Expression or a new one.
527   Expression*
528   lower(Gogo* gogo, Named_object* function, int iota_value)
529   { return this->do_lower(gogo, function, iota_value); }
530
531   // Determine the real type of an expression with abstract integer,
532   // floating point, or complex type.  TYPE_CONTEXT describes the
533   // expected type.
534   void
535   determine_type(const Type_context*);
536
537   // Check types in an expression.
538   void
539   check_types(Gogo* gogo)
540   { this->do_check_types(gogo); }
541
542   // Determine the type when there is no context.
543   void
544   determine_type_no_context();
545
546   // Return the current type of the expression.  This may be changed
547   // by determine_type.
548   Type*
549   type()
550   { return this->do_type(); }
551
552   // Return a copy of an expression.
553   Expression*
554   copy()
555   { return this->do_copy(); }
556
557   // Return whether the expression is addressable--something which may
558   // be used as the operand of the unary & operator.
559   bool
560   is_addressable() const
561   { return this->do_is_addressable(); }
562
563   // Note that we are taking the address of this expression.  ESCAPES
564   // is true if this address escapes the current function.
565   void
566   address_taken(bool escapes)
567   { this->do_address_taken(escapes); }
568
569   // Return whether this expression must be evaluated in order
570   // according to the order of evaluation rules.  This is basically
571   // true of all expressions with side-effects.
572   bool
573   must_eval_in_order() const
574   { return this->do_must_eval_in_order(); }
575
576   // Return the tree for this expression.
577   tree
578   get_tree(Translate_context*);
579
580   // Return a tree handling any conversions which must be done during
581   // assignment.
582   static tree
583   convert_for_assignment(Translate_context*, Type* lhs_type, Type* rhs_type,
584                          tree rhs_tree, source_location location);
585
586   // Return a tree converting a value of one interface type to another
587   // interface type.  If FOR_TYPE_GUARD is true this is for a type
588   // assertion.
589   static tree
590   convert_interface_to_interface(Translate_context*, Type* lhs_type,
591                                  Type* rhs_type, tree rhs_tree,
592                                  bool for_type_guard, source_location);
593
594   // Return a tree implementing the comparison LHS_TREE OP RHS_TREE.
595   // TYPE is the type of both sides.
596   static tree
597   comparison_tree(Translate_context*, Operator op, Type* left_type,
598                   tree left_tree, Type* right_type, tree right_tree,
599                   source_location);
600
601   // Return a tree for the multi-precision integer VAL in TYPE.
602   static tree
603   integer_constant_tree(mpz_t val, tree type);
604
605   // Return a tree for the floating point value VAL in TYPE.
606   static tree
607   float_constant_tree(mpfr_t val, tree type);
608
609   // Return a tree for the complex value REAL/IMAG in TYPE.
610   static tree
611   complex_constant_tree(mpfr_t real, mpfr_t imag, tree type);
612
613   // Export the expression.  This is only used for constants.  It will
614   // be used for things like values of named constants and sizes of
615   // arrays.
616   void
617   export_expression(Export* exp) const
618   { this->do_export(exp); }
619
620   // Import an expression.
621   static Expression*
622   import_expression(Import*);
623
624   // Return a tree which checks that VAL, of arbitrary integer type,
625   // is non-negative and is not more than the maximum value of
626   // BOUND_TYPE.  If SOFAR is not NULL, it is or'red into the result.
627   // The return value may be NULL if SOFAR is NULL.
628   static tree
629   check_bounds(tree val, tree bound_type, tree sofar, source_location);
630
631  protected:
632   // May be implemented by child class: traverse the expressions.
633   virtual int
634   do_traverse(Traverse*);
635
636   // Return a lowered expression.
637   virtual Expression*
638   do_lower(Gogo*, Named_object*, int)
639   { return this; }
640
641   // Return whether this is a constant expression.
642   virtual bool
643   do_is_constant() const
644   { return false; }
645
646   // Return whether this is a constant expression of integral type,
647   // and set VAL to the value.
648   virtual bool
649   do_integer_constant_value(bool, mpz_t, Type**) const
650   { return false; }
651
652   // Return whether this is a constant expression of floating point
653   // type, and set VAL to the value.
654   virtual bool
655   do_float_constant_value(mpfr_t, Type**) const
656   { return false; }
657
658   // Return whether this is a constant expression of complex type, and
659   // set REAL and IMAGE to the value.
660   virtual bool
661   do_complex_constant_value(mpfr_t, mpfr_t, Type**) const
662   { return false; }
663
664   // Return whether this is a constant expression of string type, and
665   // set VAL to the value.
666   virtual bool
667   do_string_constant_value(std::string*) const
668   { return false; }
669
670   // Called by the parser if the value is being discarded.
671   virtual void
672   do_discarding_value();
673
674   // Child class holds type.
675   virtual Type*
676   do_type() = 0;
677
678   // Child class implements determining type information.
679   virtual void
680   do_determine_type(const Type_context*) = 0;
681
682   // Child class implements type checking if needed.
683   virtual void
684   do_check_types(Gogo*)
685   { }
686
687   // Child class implements copying.
688   virtual Expression*
689   do_copy() = 0;
690
691   // Child class implements whether the expression is addressable.
692   virtual bool
693   do_is_addressable() const
694   { return false; }
695
696   // Child class implements taking the address of an expression.
697   virtual void
698   do_address_taken(bool)
699   { }
700
701   // Child class implements whether this expression must be evaluated
702   // in order.
703   virtual bool
704   do_must_eval_in_order() const
705   { return false; }
706
707   // Child class implements conversion to tree.
708   virtual tree
709   do_get_tree(Translate_context*) = 0;
710
711   // Child class implements export.
712   virtual void
713   do_export(Export*) const;
714
715   // For children to call to warn about an unused value.
716   void
717   warn_about_unused_value();
718
719   // For children to call when they detect that they are in error.
720   void
721   set_is_error();
722
723   // For children to call to report an error conveniently.
724   void
725   report_error(const char*);
726
727  private:
728   // Convert to the desired statement classification, or return NULL.
729   // This is a controlled dynamic cast.
730   template<typename Expression_class,
731            Expression_classification expr_classification>
732   Expression_class*
733   convert()
734   {
735     return (this->classification_ == expr_classification
736             ? static_cast<Expression_class*>(this)
737             : NULL);
738   }
739
740   template<typename Expression_class,
741            Expression_classification expr_classification>
742   const Expression_class*
743   convert() const
744   {
745     return (this->classification_ == expr_classification
746             ? static_cast<const Expression_class*>(this)
747             : NULL);
748   }
749
750   static tree
751   convert_type_to_interface(Translate_context*, Type*, Type*, tree,
752                             source_location);
753
754   static tree
755   get_interface_type_descriptor(Translate_context*, Type*, tree,
756                                 source_location);
757
758   static tree
759   convert_interface_to_type(Translate_context*, Type*, Type*, tree,
760                             source_location);
761
762   // The expression classification.
763   Expression_classification classification_;
764   // The location in the input file.
765   source_location location_;
766 };
767
768 // A list of Expressions.
769
770 class Expression_list
771 {
772  public:
773   Expression_list()
774     : entries_()
775   { }
776
777   // Return whether the list is empty.
778   bool
779   empty() const
780   { return this->entries_.empty(); }
781
782   // Return the number of entries in the list.
783   size_t
784   size() const
785   { return this->entries_.size(); }
786
787   // Add an entry to the end of the list.
788   void
789   push_back(Expression* expr)
790   { this->entries_.push_back(expr); }
791
792   void
793   append(Expression_list* add)
794   { this->entries_.insert(this->entries_.end(), add->begin(), add->end()); }
795
796   // Reserve space in the list.
797   void
798   reserve(size_t size)
799   { this->entries_.reserve(size); }
800
801   // Traverse the expressions in the list.
802   int
803   traverse(Traverse*);
804
805   // Copy the list.
806   Expression_list*
807   copy();
808
809   // Return true if the list contains an error expression.
810   bool
811   contains_error() const;
812
813   // Return the first and last elements.
814   Expression*&
815   front()
816   { return this->entries_.front(); }
817
818   Expression*
819   front() const
820   { return this->entries_.front(); }
821
822   Expression*&
823   back()
824   { return this->entries_.back(); }
825
826   Expression*
827   back() const
828   { return this->entries_.back(); }
829
830   // Iterators.
831
832   typedef std::vector<Expression*>::iterator iterator;
833   typedef std::vector<Expression*>::const_iterator const_iterator;
834
835   iterator
836   begin()
837   { return this->entries_.begin(); }
838
839   const_iterator
840   begin() const
841   { return this->entries_.begin(); }
842
843   iterator
844   end()
845   { return this->entries_.end(); }
846
847   const_iterator
848   end() const
849   { return this->entries_.end(); }
850
851   // Erase an entry.
852   void
853   erase(iterator p)
854   { this->entries_.erase(p); }
855
856  private:
857   std::vector<Expression*> entries_;
858 };
859
860 // An abstract base class for an expression which is only used by the
861 // parser, and is lowered in the lowering pass.
862
863 class Parser_expression : public Expression
864 {
865  public:
866   Parser_expression(Expression_classification classification,
867                     source_location location)
868     : Expression(classification, location)
869   { }
870
871  protected:
872   virtual Expression*
873   do_lower(Gogo*, Named_object*, int) = 0;
874
875   Type*
876   do_type();
877
878   void
879   do_determine_type(const Type_context*)
880   { gcc_unreachable(); }
881
882   void
883   do_check_types(Gogo*)
884   { gcc_unreachable(); }
885
886   tree
887   do_get_tree(Translate_context*)
888   { gcc_unreachable(); }
889 };
890
891 // An expression which is simply a variable.
892
893 class Var_expression : public Expression
894 {
895  public:
896   Var_expression(Named_object* variable, source_location location)
897     : Expression(EXPRESSION_VAR_REFERENCE, location),
898       variable_(variable)
899   { }
900
901   // Return the variable.
902   Named_object*
903   named_object() const
904   { return this->variable_; }
905
906   // Return the name of the variable.
907   const std::string&
908   name() const;
909
910  protected:
911   Expression*
912   do_lower(Gogo*, Named_object*, int);
913
914   Type*
915   do_type();
916
917   void
918   do_determine_type(const Type_context*);
919
920   Expression*
921   do_copy()
922   { return this; }
923
924   bool
925   do_is_addressable() const
926   { return true; }
927
928   void
929   do_address_taken(bool);
930
931   tree
932   do_get_tree(Translate_context*);
933
934  private:
935   // The variable we are referencing.
936   Named_object* variable_;
937 };
938
939 // A reference to a temporary variable.
940
941 class Temporary_reference_expression : public Expression
942 {
943  public:
944   Temporary_reference_expression(Temporary_statement* statement,
945                                  source_location location)
946     : Expression(EXPRESSION_TEMPORARY_REFERENCE, location),
947       statement_(statement)
948   { }
949
950  protected:
951   Type*
952   do_type();
953
954   void
955   do_determine_type(const Type_context*)
956   { }
957
958   Expression*
959   do_copy()
960   { return make_temporary_reference(this->statement_, this->location()); }
961
962   bool
963   do_is_addressable() const
964   { return true; }
965
966   void
967   do_address_taken(bool);
968
969   tree
970   do_get_tree(Translate_context*);
971
972  private:
973   // The statement where the temporary variable is defined.
974   Temporary_statement* statement_;
975 };
976
977 // A string expression.
978
979 class String_expression : public Expression
980 {
981  public:
982   String_expression(const std::string& val, source_location location)
983     : Expression(EXPRESSION_STRING, location),
984       val_(val), type_(NULL)
985   { }
986
987   const std::string&
988   val() const
989   { return this->val_; }
990
991   static Expression*
992   do_import(Import*);
993
994  protected:
995   bool
996   do_is_constant() const
997   { return true; }
998
999   bool
1000   do_string_constant_value(std::string* val) const
1001   {
1002     *val = this->val_;
1003     return true;
1004   }
1005
1006   Type*
1007   do_type();
1008
1009   void
1010   do_determine_type(const Type_context*);
1011
1012   Expression*
1013   do_copy()
1014   { return this; }
1015
1016   tree
1017   do_get_tree(Translate_context*);
1018
1019   void
1020   do_export(Export*) const;
1021
1022  private:
1023   // The string value.  This is immutable.
1024   const std::string val_;
1025   // The type as determined by context.
1026   Type* type_;
1027 };
1028
1029 // A binary expression.
1030
1031 class Binary_expression : public Expression
1032 {
1033  public:
1034   Binary_expression(Operator op, Expression* left, Expression* right,
1035                     source_location location)
1036     : Expression(EXPRESSION_BINARY, location),
1037       op_(op), left_(left), right_(right)
1038   { }
1039
1040   // Return the operator.
1041   Operator
1042   op()
1043   { return this->op_; }
1044
1045   // Return the left hand expression.
1046   Expression*
1047   left()
1048   { return this->left_; }
1049
1050   // Return the right hand expression.
1051   Expression*
1052   right()
1053   { return this->right_; }
1054
1055   // Apply binary opcode OP to LEFT_VAL and RIGHT_VAL, setting VAL.
1056   // LEFT_TYPE is the type of LEFT_VAL, RIGHT_TYPE is the type of
1057   // RIGHT_VAL; LEFT_TYPE and/or RIGHT_TYPE may be NULL.  Return true
1058   // if this could be done, false if not.
1059   static bool
1060   eval_integer(Operator op, Type* left_type, mpz_t left_val,
1061                Type* right_type, mpz_t right_val, source_location,
1062                mpz_t val);
1063
1064   // Apply binary opcode OP to LEFT_VAL and RIGHT_VAL, setting VAL.
1065   // Return true if this could be done, false if not.
1066   static bool
1067   eval_float(Operator op, Type* left_type, mpfr_t left_val,
1068              Type* right_type, mpfr_t right_val, mpfr_t val,
1069              source_location);
1070
1071   // Apply binary opcode OP to LEFT_REAL/LEFT_IMAG and
1072   // RIGHT_REAL/RIGHT_IMAG, setting REAL/IMAG.  Return true if this
1073   // could be done, false if not.
1074   static bool
1075   eval_complex(Operator op, Type* left_type, mpfr_t left_real,
1076                mpfr_t left_imag, Type* right_type, mpfr_t right_real,
1077                mpfr_t right_imag, mpfr_t real, mpfr_t imag, source_location);
1078
1079   // Compare integer constants according to OP.
1080   static bool
1081   compare_integer(Operator op, mpz_t left_val, mpz_t right_val);
1082
1083   // Compare floating point constants according to OP.
1084   static bool
1085   compare_float(Operator op, Type* type, mpfr_t left_val, mpfr_t right_val);
1086
1087   // Compare complex constants according to OP.
1088   static bool
1089   compare_complex(Operator op, Type* type, mpfr_t left_real, mpfr_t left_imag,
1090                   mpfr_t right_val, mpfr_t right_imag);
1091
1092   static Expression*
1093   do_import(Import*);
1094
1095   // Report an error if OP can not be applied to TYPE.  Return whether
1096   // it can.
1097   static bool
1098   check_operator_type(Operator op, Type* type, source_location);
1099
1100  protected:
1101   int
1102   do_traverse(Traverse* traverse);
1103
1104   Expression*
1105   do_lower(Gogo*, Named_object*, int);
1106
1107   bool
1108   do_is_constant() const
1109   { return this->left_->is_constant() && this->right_->is_constant(); }
1110
1111   bool
1112   do_integer_constant_value(bool, mpz_t val, Type**) const;
1113
1114   bool
1115   do_float_constant_value(mpfr_t val, Type**) const;
1116
1117   bool
1118   do_complex_constant_value(mpfr_t real, mpfr_t imag, Type**) const;
1119
1120   void
1121   do_discarding_value();
1122
1123   Type*
1124   do_type();
1125
1126   void
1127   do_determine_type(const Type_context*);
1128
1129   void
1130   do_check_types(Gogo*);
1131
1132   Expression*
1133   do_copy()
1134   {
1135     return Expression::make_binary(this->op_, this->left_->copy(),
1136                                    this->right_->copy(), this->location());
1137   }
1138
1139   tree
1140   do_get_tree(Translate_context*);
1141
1142   void
1143   do_export(Export*) const;
1144
1145  private:
1146   // The binary operator to apply.
1147   Operator op_;
1148   // The left hand side operand.
1149   Expression* left_;
1150   // The right hand side operand.
1151   Expression* right_;
1152 };
1153
1154 // A call expression.  The go statement needs to dig inside this.
1155
1156 class Call_expression : public Expression
1157 {
1158  public:
1159   Call_expression(Expression* fn, Expression_list* args, bool is_varargs,
1160                   source_location location)
1161     : Expression(EXPRESSION_CALL, location),
1162       fn_(fn), args_(args), type_(NULL), tree_(NULL), is_varargs_(is_varargs),
1163       varargs_are_lowered_(false), types_are_determined_(false),
1164       is_deferred_(false)
1165   { }
1166
1167   // The function to call.
1168   Expression*
1169   fn() const
1170   { return this->fn_; }
1171
1172   // The arguments.
1173   Expression_list*
1174   args()
1175   { return this->args_; }
1176
1177   const Expression_list*
1178   args() const
1179   { return this->args_; }
1180
1181   // Get the function type.
1182   Function_type*
1183   get_function_type() const;
1184
1185   // Return the number of values this call will return.
1186   size_t
1187   result_count() const;
1188
1189   // Return whether this is a call to the predeclared function
1190   // recover.
1191   bool
1192   is_recover_call() const;
1193
1194   // Set the argument for a call to recover.
1195   void
1196   set_recover_arg(Expression*);
1197
1198   // Whether the last argument is a varargs argument (f(a...)).
1199   bool
1200   is_varargs() const
1201   { return this->is_varargs_; }
1202
1203   // Whether this call is being deferred.
1204   bool
1205   is_deferred() const
1206   { return this->is_deferred_; }
1207
1208   // Note that the call is being deferred.
1209   void
1210   set_is_deferred()
1211   { this->is_deferred_ = true; }
1212
1213  protected:
1214   int
1215   do_traverse(Traverse*);
1216
1217   virtual Expression*
1218   do_lower(Gogo*, Named_object*, int);
1219
1220   void
1221   do_discarding_value()
1222   { }
1223
1224   virtual Type*
1225   do_type();
1226
1227   virtual void
1228   do_determine_type(const Type_context*);
1229
1230   virtual void
1231   do_check_types(Gogo*);
1232
1233   Expression*
1234   do_copy()
1235   {
1236     return Expression::make_call(this->fn_->copy(),
1237                                  (this->args_ == NULL
1238                                   ? NULL
1239                                   : this->args_->copy()),
1240                                  this->is_varargs_, this->location());
1241   }
1242
1243   bool
1244   do_must_eval_in_order() const;
1245
1246   virtual tree
1247   do_get_tree(Translate_context*);
1248
1249   virtual bool
1250   do_is_recover_call() const;
1251
1252   virtual void
1253   do_set_recover_arg(Expression*);
1254
1255   // Let a builtin expression change the argument list.
1256   void
1257   set_args(Expression_list* args)
1258   { this->args_ = args; }
1259
1260   // Let a builtin expression lower varargs.
1261   Expression*
1262   lower_varargs(Gogo*, Named_object* function, Type* varargs_type,
1263                 size_t param_count);
1264
1265   // Let a builtin expression check whether types have been
1266   // determined.
1267   bool
1268   determining_types();
1269
1270  private:
1271   bool
1272   check_argument_type(int, const Type*, const Type*, source_location, bool);
1273
1274   tree
1275   bound_method_function(Translate_context*, Bound_method_expression*, tree*);
1276
1277   tree
1278   interface_method_function(Translate_context*,
1279                             Interface_field_reference_expression*,
1280                             tree*);
1281
1282   // The function to call.
1283   Expression* fn_;
1284   // The arguments to pass.  This may be NULL if there are no
1285   // arguments.
1286   Expression_list* args_;
1287   // The type of the expression, to avoid recomputing it.
1288   Type* type_;
1289   // The tree for the call, used for a call which returns a tuple.
1290   tree tree_;
1291   // True if the last argument is a varargs argument (f(a...)).
1292   bool is_varargs_;
1293   // True if varargs have already been lowered.
1294   bool varargs_are_lowered_;
1295   // True if types have been determined.
1296   bool types_are_determined_;
1297   // True if the call is an argument to a defer statement.
1298   bool is_deferred_;
1299 };
1300
1301 // An expression which represents a pointer to a function.
1302
1303 class Func_expression : public Expression
1304 {
1305  public:
1306   Func_expression(Named_object* function, Expression* closure,
1307                   source_location location)
1308     : Expression(EXPRESSION_FUNC_REFERENCE, location),
1309       function_(function), closure_(closure)
1310   { }
1311
1312   // Return the object associated with the function.
1313   const Named_object*
1314   named_object() const
1315   { return this->function_; }
1316
1317   // Return the name of the function.
1318   const std::string&
1319   name() const;
1320
1321   // Return the closure for this function.  This will return NULL if
1322   // the function has no closure, which is the normal case.
1323   Expression*
1324   closure()
1325   { return this->closure_; }
1326
1327   // Return a tree for this function without evaluating the closure.
1328   tree
1329   get_tree_without_closure(Gogo*);
1330
1331  protected:
1332   int
1333   do_traverse(Traverse*);
1334
1335   Type*
1336   do_type();
1337
1338   void
1339   do_determine_type(const Type_context*)
1340   {
1341     if (this->closure_ != NULL)
1342       this->closure_->determine_type_no_context();
1343   }
1344
1345   Expression*
1346   do_copy()
1347   {
1348     return Expression::make_func_reference(this->function_,
1349                                            (this->closure_ == NULL
1350                                             ? NULL
1351                                             : this->closure_->copy()),
1352                                            this->location());
1353   }
1354
1355   tree
1356   do_get_tree(Translate_context*);
1357
1358  private:
1359   // The function itself.
1360   Named_object* function_;
1361   // A closure.  This is normally NULL.  For a nested function, it may
1362   // be a heap-allocated struct holding pointers to all the variables
1363   // referenced by this function and defined in enclosing functions.
1364   Expression* closure_;
1365 };
1366
1367 // A reference to an unknown name.
1368
1369 class Unknown_expression : public Parser_expression
1370 {
1371  public:
1372   Unknown_expression(Named_object* named_object, source_location location)
1373     : Parser_expression(EXPRESSION_UNKNOWN_REFERENCE, location),
1374       named_object_(named_object), is_composite_literal_key_(false)
1375   { }
1376
1377   // The associated named object.
1378   Named_object*
1379   named_object() const
1380   { return this->named_object_; }
1381
1382   // The name of the identifier which was unknown.
1383   const std::string&
1384   name() const;
1385
1386   // Note that this expression is being used as the key in a composite
1387   // literal, so it may be OK if it is not resolved.
1388   void
1389   set_is_composite_literal_key()
1390   { this->is_composite_literal_key_ = true; }
1391
1392   // Note that this expression should no longer be treated as a
1393   // composite literal key.
1394   void
1395   clear_is_composite_literal_key()
1396   { this->is_composite_literal_key_ = false; }
1397
1398  protected:
1399   Expression*
1400   do_lower(Gogo*, Named_object*, int);
1401
1402   Expression*
1403   do_copy()
1404   { return new Unknown_expression(this->named_object_, this->location()); }
1405
1406  private:
1407   // The unknown name.
1408   Named_object* named_object_;
1409   // True if this is the key in a composite literal.
1410   bool is_composite_literal_key_;
1411 };
1412
1413 // An index expression.  This is lowered to an array index, a string
1414 // index, or a map index.
1415
1416 class Index_expression : public Parser_expression
1417 {
1418  public:
1419   Index_expression(Expression* left, Expression* start, Expression* end,
1420                    source_location location)
1421     : Parser_expression(EXPRESSION_INDEX, location),
1422       left_(left), start_(start), end_(end), is_lvalue_(false)
1423   { }
1424
1425   // Record that this expression is an lvalue.
1426   void
1427   set_is_lvalue()
1428   { this->is_lvalue_ = true; }
1429
1430  protected:
1431   int
1432   do_traverse(Traverse*);
1433
1434   Expression*
1435   do_lower(Gogo*, Named_object*, int);
1436
1437   Expression*
1438   do_copy()
1439   {
1440     return new Index_expression(this->left_->copy(), this->start_->copy(),
1441                                 (this->end_ == NULL
1442                                  ? NULL
1443                                  : this->end_->copy()),
1444                                 this->location());
1445   }
1446
1447  private:
1448   // The expression being indexed.
1449   Expression* left_;
1450   // The first index.
1451   Expression* start_;
1452   // The second index.  This is NULL for an index, non-NULL for a
1453   // slice.
1454   Expression* end_;
1455   // Whether this is being used as an l-value.  We set this during the
1456   // parse because map index expressions need to know.
1457   bool is_lvalue_;
1458 };
1459
1460 // An index into a map.
1461
1462 class Map_index_expression : public Expression
1463 {
1464  public:
1465   Map_index_expression(Expression* map, Expression* index,
1466                        source_location location)
1467     : Expression(EXPRESSION_MAP_INDEX, location),
1468       map_(map), index_(index), is_lvalue_(false),
1469       is_in_tuple_assignment_(false)
1470   { }
1471
1472   // Return the map.
1473   Expression*
1474   map()
1475   { return this->map_; }
1476
1477   const Expression*
1478   map() const
1479   { return this->map_; }
1480
1481   // Return the index.
1482   Expression*
1483   index()
1484   { return this->index_; }
1485
1486   const Expression*
1487   index() const
1488   { return this->index_; }
1489
1490   // Get the type of the map being indexed.
1491   Map_type*
1492   get_map_type() const;
1493
1494   // Record that this map expression is an lvalue.  The difference is
1495   // that an lvalue always inserts the key.
1496   void
1497   set_is_lvalue()
1498   { this->is_lvalue_ = true; }
1499
1500   // Return whether this map expression occurs in an assignment to a
1501   // pair of values.
1502   bool
1503   is_in_tuple_assignment() const
1504   { return this->is_in_tuple_assignment_; }
1505
1506   // Record that this map expression occurs in an assignment to a pair
1507   // of values.
1508   void
1509   set_is_in_tuple_assignment()
1510   { this->is_in_tuple_assignment_ = true; }
1511
1512   // Return a tree for the map index.  This returns a tree which
1513   // evaluates to a pointer to a value in the map.  If INSERT is true,
1514   // the key will be inserted if not present, and the value pointer
1515   // will be zero initialized.  If INSERT is false, and the key is not
1516   // present in the map, the pointer will be NULL.
1517   tree
1518   get_value_pointer(Translate_context*, bool insert);
1519
1520  protected:
1521   int
1522   do_traverse(Traverse*);
1523
1524   Type*
1525   do_type();
1526
1527   void
1528   do_determine_type(const Type_context*);
1529
1530   void
1531   do_check_types(Gogo*);
1532
1533   Expression*
1534   do_copy()
1535   {
1536     return Expression::make_map_index(this->map_->copy(),
1537                                       this->index_->copy(),
1538                                       this->location());
1539   }
1540
1541   // A map index expression is an lvalue but it is not addressable.
1542
1543   tree
1544   do_get_tree(Translate_context*);
1545
1546  private:
1547   // The map we are looking into.
1548   Expression* map_;
1549   // The index.
1550   Expression* index_;
1551   // Whether this is an lvalue.
1552   bool is_lvalue_;
1553   // Whether this is in a tuple assignment to a pair of values.
1554   bool is_in_tuple_assignment_;
1555 };
1556
1557 // An expression which represents a method bound to its first
1558 // argument.
1559
1560 class Bound_method_expression : public Expression
1561 {
1562  public:
1563   Bound_method_expression(Expression* expr, Expression* method,
1564                           source_location location)
1565     : Expression(EXPRESSION_BOUND_METHOD, location),
1566       expr_(expr), expr_type_(NULL), method_(method)
1567   { }
1568
1569   // Return the object which is the first argument.
1570   Expression*
1571   first_argument()
1572   { return this->expr_; }
1573
1574   // Return the implicit type of the first argument.  This will be
1575   // non-NULL when using a method from an anonymous field without
1576   // using an explicit stub.
1577   Type*
1578   first_argument_type() const
1579   { return this->expr_type_; }
1580
1581   // Return the reference to the method function.
1582   Expression*
1583   method()
1584   { return this->method_; }
1585
1586   // Set the implicit type of the expression.
1587   void
1588   set_first_argument_type(Type* type)
1589   { this->expr_type_ = type; }
1590
1591  protected:
1592   int
1593   do_traverse(Traverse*);
1594
1595   Type*
1596   do_type();
1597
1598   void
1599   do_determine_type(const Type_context*);
1600
1601   void
1602   do_check_types(Gogo*);
1603
1604   Expression*
1605   do_copy()
1606   {
1607     return new Bound_method_expression(this->expr_->copy(),
1608                                        this->method_->copy(),
1609                                        this->location());
1610   }
1611
1612   tree
1613   do_get_tree(Translate_context*);
1614
1615  private:
1616   // The object used to find the method.  This is passed to the method
1617   // as the first argument.
1618   Expression* expr_;
1619   // The implicit type of the object to pass to the method.  This is
1620   // NULL in the normal case, non-NULL when using a method from an
1621   // anonymous field which does not require a stub.
1622   Type* expr_type_;
1623   // The method itself.  This is a Func_expression.
1624   Expression* method_;
1625 };
1626
1627 // A reference to a field in a struct.
1628
1629 class Field_reference_expression : public Expression
1630 {
1631  public:
1632   Field_reference_expression(Expression* expr, unsigned int field_index,
1633                              source_location location)
1634     : Expression(EXPRESSION_FIELD_REFERENCE, location),
1635       expr_(expr), field_index_(field_index)
1636   { }
1637
1638   // Return the struct expression.
1639   Expression*
1640   expr() const
1641   { return this->expr_; }
1642
1643   // Return the field index.
1644   unsigned int
1645   field_index() const
1646   { return this->field_index_; }
1647
1648   // Set the struct expression.  This is used when parsing.
1649   void
1650   set_struct_expression(Expression* expr)
1651   {
1652     gcc_assert(this->expr_ == NULL);
1653     this->expr_ = expr;
1654   }
1655
1656  protected:
1657   int
1658   do_traverse(Traverse* traverse)
1659   { return Expression::traverse(&this->expr_, traverse); }
1660
1661   Type*
1662   do_type();
1663
1664   void
1665   do_determine_type(const Type_context*)
1666   { this->expr_->determine_type_no_context(); }
1667
1668   void
1669   do_check_types(Gogo*);
1670
1671   Expression*
1672   do_copy()
1673   {
1674     return Expression::make_field_reference(this->expr_->copy(),
1675                                             this->field_index_,
1676                                             this->location());
1677   }
1678
1679   bool
1680   do_is_addressable() const
1681   { return this->expr_->is_addressable(); }
1682
1683   tree
1684   do_get_tree(Translate_context*);
1685
1686  private:
1687   // The expression we are looking into.  This should have a type of
1688   // struct.
1689   Expression* expr_;
1690   // The zero-based index of the field we are retrieving.
1691   unsigned int field_index_;
1692 };
1693
1694 // A reference to a field of an interface.
1695
1696 class Interface_field_reference_expression : public Expression
1697 {
1698  public:
1699   Interface_field_reference_expression(Expression* expr,
1700                                        const std::string& name,
1701                                        source_location location)
1702     : Expression(EXPRESSION_INTERFACE_FIELD_REFERENCE, location),
1703       expr_(expr), name_(name)
1704   { }
1705
1706   // Return the expression for the interface object.
1707   Expression*
1708   expr()
1709   { return this->expr_; }
1710
1711   // Return the name of the method to call.
1712   const std::string&
1713   name() const
1714   { return this->name_; }
1715
1716   // Return a tree for the pointer to the function to call, given a
1717   // tree for the expression.
1718   tree
1719   get_function_tree(Translate_context*, tree);
1720
1721   // Return a tree for the first argument to pass to the interface
1722   // function, given a tree for the expression.  This is the real
1723   // object associated with the interface object.
1724   tree
1725   get_underlying_object_tree(Translate_context*, tree);
1726
1727  protected:
1728   int
1729   do_traverse(Traverse* traverse);
1730
1731   Type*
1732   do_type();
1733
1734   void
1735   do_determine_type(const Type_context*);
1736
1737   void
1738   do_check_types(Gogo*);
1739
1740   Expression*
1741   do_copy()
1742   {
1743     return Expression::make_interface_field_reference(this->expr_->copy(),
1744                                                       this->name_,
1745                                                       this->location());
1746   }
1747
1748   tree
1749   do_get_tree(Translate_context*);
1750
1751  private:
1752   // The expression for the interface object.  This should have a type
1753   // of interface or pointer to interface.
1754   Expression* expr_;
1755   // The field we are retrieving--the name of the method.
1756   std::string name_;
1757 };
1758
1759 // A type guard expression.
1760
1761 class Type_guard_expression : public Expression
1762 {
1763  public:
1764   Type_guard_expression(Expression* expr, Type* type, source_location location)
1765     : Expression(EXPRESSION_TYPE_GUARD, location),
1766       expr_(expr), type_(type)
1767   { }
1768
1769   // Return the expression to convert.
1770   Expression*
1771   expr()
1772   { return this->expr_; }
1773
1774   // Return the type to which to convert.
1775   Type*
1776   type()
1777   { return this->type_; }
1778
1779  protected:
1780   int
1781   do_traverse(Traverse* traverse);
1782
1783   Type*
1784   do_type()
1785   { return this->type_; }
1786
1787   void
1788   do_determine_type(const Type_context*)
1789   { this->expr_->determine_type_no_context(); }
1790
1791   void
1792   do_check_types(Gogo*);
1793
1794   Expression*
1795   do_copy()
1796   {
1797     return new Type_guard_expression(this->expr_->copy(), this->type_,
1798                                      this->location());
1799   }
1800
1801   tree
1802   do_get_tree(Translate_context*);
1803
1804  private:
1805   // The expression to convert.
1806   Expression* expr_;
1807   // The type to which to convert.
1808   Type* type_;
1809 };
1810
1811 // A receive expression.
1812
1813 class Receive_expression : public Expression
1814 {
1815  public:
1816   Receive_expression(Expression* channel, source_location location)
1817     : Expression(EXPRESSION_RECEIVE, location),
1818       channel_(channel), is_value_discarded_(false), for_select_(false)
1819   { }
1820
1821   // Return the channel.
1822   Expression*
1823   channel()
1824   { return this->channel_; }
1825
1826   // Note that this is for a select statement.
1827   void
1828   set_for_select()
1829   { this->for_select_ = true; }
1830
1831  protected:
1832   int
1833   do_traverse(Traverse* traverse)
1834   { return Expression::traverse(&this->channel_, traverse); }
1835
1836   void
1837   do_discarding_value()
1838   { this->is_value_discarded_ = true; }
1839
1840   Type*
1841   do_type();
1842
1843   void
1844   do_determine_type(const Type_context*)
1845   { this->channel_->determine_type_no_context(); }
1846
1847   void
1848   do_check_types(Gogo*);
1849
1850   Expression*
1851   do_copy()
1852   {
1853     return Expression::make_receive(this->channel_->copy(), this->location());
1854   }
1855
1856   bool
1857   do_must_eval_in_order() const
1858   { return true; }
1859
1860   tree
1861   do_get_tree(Translate_context*);
1862
1863  private:
1864   // The channel from which we are receiving.
1865   Expression* channel_;
1866   // Whether the value is being discarded.
1867   bool is_value_discarded_;
1868   // Whether this is for a select statement.
1869   bool for_select_;
1870 };
1871
1872 // A send expression.
1873
1874 class Send_expression : public Expression
1875 {
1876  public:
1877   Send_expression(Expression* channel, Expression* val,
1878                   source_location location)
1879     : Expression(EXPRESSION_SEND, location),
1880       channel_(channel), val_(val), is_value_discarded_(false),
1881       for_select_(false)
1882   { }
1883
1884   // Note that this is for a select statement.
1885   void
1886   set_for_select()
1887   { this->for_select_ = true; }
1888
1889  protected:
1890   int
1891   do_traverse(Traverse* traverse);
1892
1893   void
1894   do_discarding_value()
1895   { this->is_value_discarded_ = true; }
1896
1897   Type*
1898   do_type();
1899
1900   void
1901   do_determine_type(const Type_context*);
1902
1903   void
1904   do_check_types(Gogo*);
1905
1906   Expression*
1907   do_copy()
1908   {
1909     return Expression::make_send(this->channel_->copy(), this->val_->copy(),
1910                                  this->location());
1911   }
1912
1913   bool
1914   do_must_eval_in_order() const
1915   { return true; }
1916
1917   tree
1918   do_get_tree(Translate_context*);
1919
1920  private:
1921   // The channel on which to send the value.
1922   Expression* channel_;
1923   // The value to send.
1924   Expression* val_;
1925   // Whether the value is being discarded.
1926   bool is_value_discarded_;
1927   // Whether this is for a select statement.
1928   bool for_select_;
1929 };
1930
1931 #endif // !defined(GO_EXPRESSIONS_H)