OSDN Git Service

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