OSDN Git Service

a09d09a41ba64e1ba8a80fceb66820a53f74de13
[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_UNSAFE_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_TYPE_DESCRIPTOR,
93     EXPRESSION_TYPE_INFO,
94     EXPRESSION_STRUCT_FIELD_OFFSET,
95     EXPRESSION_LABEL_ADDR
96   };
97
98   Expression(Expression_classification, source_location);
99
100   virtual ~Expression();
101
102   // Make an error expression.  This is used when a parse error occurs
103   // to prevent cascading errors.
104   static Expression*
105   make_error(source_location);
106
107   // Make an expression which is really a type.  This is used during
108   // parsing.
109   static Expression*
110   make_type(Type*, source_location);
111
112   // Make a unary expression.
113   static Expression*
114   make_unary(Operator, Expression*, source_location);
115
116   // Make a binary expression.
117   static Expression*
118   make_binary(Operator, Expression*, Expression*, source_location);
119
120   // Make a reference to a constant in an expression.
121   static Expression*
122   make_const_reference(Named_object*, source_location);
123
124   // Make a reference to a variable in an expression.
125   static Expression*
126   make_var_reference(Named_object*, source_location);
127
128   // Make a reference to a temporary variable.  Temporary variables
129   // are always created by a single statement, which is what we use to
130   // refer to them.
131   static Expression*
132   make_temporary_reference(Temporary_statement*, source_location);
133
134   // Make a sink expression--a reference to the blank identifier _.
135   static Expression*
136   make_sink(source_location);
137
138   // Make a reference to a function in an expression.
139   static Expression*
140   make_func_reference(Named_object*, Expression* closure, source_location);
141
142   // Make a reference to an unknown name.  In a correct program this
143   // will always be lowered to a real const/var/func reference.
144   static Expression*
145   make_unknown_reference(Named_object*, source_location);
146
147   // Make a constant bool expression.
148   static Expression*
149   make_boolean(bool val, source_location);
150
151   // Make a constant string expression.
152   static Expression*
153   make_string(const std::string&, source_location);
154
155   // Make a constant integer expression.  TYPE should be NULL for an
156   // abstract type.
157   static Expression*
158   make_integer(const mpz_t*, Type*, source_location);
159
160   // Make a constant float expression.  TYPE should be NULL for an
161   // abstract type.
162   static Expression*
163   make_float(const mpfr_t*, Type*, source_location);
164
165   // Make a constant complex expression.  TYPE should be NULL for an
166   // abstract type.
167   static Expression*
168   make_complex(const mpfr_t* real, const mpfr_t* imag, Type*, source_location);
169
170   // Make a nil expression.
171   static Expression*
172   make_nil(source_location);
173
174   // Make an iota expression.  This is used for the predeclared
175   // constant iota.
176   static Expression*
177   make_iota();
178
179   // Make a call expression.
180   static Call_expression*
181   make_call(Expression* func, Expression_list* args, bool is_varargs,
182             source_location);
183
184   // Make a reference to a specific result of a call expression which
185   // returns a tuple.
186   static Expression*
187   make_call_result(Call_expression*, unsigned int index);
188
189   // Make an expression which is a method bound to its first
190   // parameter.
191   static Bound_method_expression*
192   make_bound_method(Expression* object, Expression* method, source_location);
193
194   // Make an index or slice expression.  This is a parser expression
195   // which represents LEFT[START:END].  END may be NULL, meaning an
196   // index rather than a slice.  At parse time we may not know the
197   // type of LEFT.  After parsing this is lowered to an array index, a
198   // string index, or a map index.
199   static Expression*
200   make_index(Expression* left, Expression* start, Expression* end,
201              source_location);
202
203   // Make an array index expression.  END may be NULL, in which case
204   // this is an lvalue.
205   static Expression*
206   make_array_index(Expression* array, Expression* start, Expression* end,
207                    source_location);
208
209   // Make a string index expression.  END may be NULL.  This is never
210   // an lvalue.
211   static Expression*
212   make_string_index(Expression* string, Expression* start, Expression* end,
213                     source_location);
214
215   // Make a map index expression.  This is an lvalue.
216   static Map_index_expression*
217   make_map_index(Expression* map, Expression* val, source_location);
218
219   // Make a selector.  This is a parser expression which represents
220   // LEFT.NAME.  At parse time we may not know the type of the left
221   // hand side.
222   static Expression*
223   make_selector(Expression* left, const std::string& name, source_location);
224
225   // Make a reference to a field in a struct.
226   static Field_reference_expression*
227   make_field_reference(Expression*, unsigned int field_index, source_location);
228
229   // Make a reference to a field of an interface, with an associated
230   // object.
231   static Expression*
232   make_interface_field_reference(Expression*, const std::string&,
233                                  source_location);
234
235   // Make an allocation expression.
236   static Expression*
237   make_allocation(Type*, source_location);
238
239   // Make a call to the builtin function make.
240   static Expression*
241   make_make(Type*, Expression_list*, source_location);
242
243   // Make a type guard expression.
244   static Expression*
245   make_type_guard(Expression*, Type*, source_location);
246
247   // Make a type cast expression.
248   static Expression*
249   make_cast(Type*, Expression*, source_location);
250
251   // Make an unsafe type cast expression.  This is only used when
252   // passing parameter to builtin functions that are part of the Go
253   // runtime.
254   static Expression*
255   make_unsafe_cast(Type*, Expression*, source_location);
256
257   // Make a composite literal.  The DEPTH parameter is how far down we
258   // are in a list of composite literals with omitted types.
259   static Expression*
260   make_composite_literal(Type*, int depth, bool has_keys, Expression_list*,
261                          source_location);
262
263   // Make a struct composite literal.
264   static Expression*
265   make_struct_composite_literal(Type*, Expression_list*, source_location);
266
267   // Make a slice composite literal.
268   static Expression*
269   make_slice_composite_literal(Type*, Expression_list*, source_location);
270
271   // Take a composite literal and allocate it on the heap.
272   static Expression*
273   make_heap_composite(Expression*, source_location);
274
275   // Make a receive expression.  VAL is NULL for a unary receive.
276   static Receive_expression*
277   make_receive(Expression* channel, source_location);
278
279   // Make an expression which evaluates to the type descriptor of a
280   // type.
281   static Expression*
282   make_type_descriptor(Type* type, source_location);
283
284   // Make an expression which evaluates to some characteristic of a
285   // type.  These are only used for type descriptors, so there is no
286   // location parameter.
287   enum Type_info
288     {
289       // The size of a value of the type.
290       TYPE_INFO_SIZE,
291       // The required alignment of a value of the type.
292       TYPE_INFO_ALIGNMENT,
293       // The required alignment of a value of the type when used as a
294       // field in a struct.
295       TYPE_INFO_FIELD_ALIGNMENT
296     };
297
298   static Expression*
299   make_type_info(Type* type, Type_info);
300
301   // Make an expression which evaluates to the offset of a field in a
302   // struct.  This is only used for type descriptors, so there is no
303   // location parameter.
304   static Expression*
305   make_struct_field_offset(Struct_type*, const Struct_field*);
306
307   // Make an expression which evaluates to the address of an unnamed
308   // label.
309   static Expression*
310   make_label_addr(Label*, source_location);
311
312   // Return the expression classification.
313   Expression_classification
314   classification() const
315   { return this->classification_; }
316
317   // Return the location of the expression.
318   source_location
319   location() const
320   { return this->location_; }
321
322   // Return whether this is a constant expression.
323   bool
324   is_constant() const
325   { return this->do_is_constant(); }
326
327   // If this is not a constant expression with integral type, return
328   // false.  If it is one, return true, and set VAL to the value.  VAL
329   // should already be initialized.  If this returns true, it sets
330   // *PTYPE to the type of the value, or NULL for an abstract type.
331   // If IOTA_IS_CONSTANT is true, then an iota expression is assumed
332   // to have its final value.
333   bool
334   integer_constant_value(bool iota_is_constant, mpz_t val, Type** ptype) const;
335
336   // If this is not a constant expression with floating point type,
337   // return false.  If it is one, return true, and set VAL to the
338   // value.  VAL should already be initialized.  If this returns true,
339   // it sets *PTYPE to the type of the value, or NULL for an abstract
340   // type.
341   bool
342   float_constant_value(mpfr_t val, Type** ptype) const;
343
344   // If this is not a constant expression with complex type, return
345   // false.  If it is one, return true, and set REAL and IMAG to the
346   // value.  REAL and IMAG should already be initialized.  If this
347   // return strue, it sets *PTYPE to the type of the value, or NULL
348   // for an abstract type.
349   bool
350   complex_constant_value(mpfr_t real, mpfr_t imag, Type** ptype) const;
351
352   // If this is not a constant expression with string type, return
353   // false.  If it is one, return true, and set VAL to the value.
354   bool
355   string_constant_value(std::string* val) const
356   { return this->do_string_constant_value(val); }
357
358   // This is called by the parser if the value of this expression is
359   // being discarded.  This issues warnings about computed values
360   // being unused.
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  protected:
907   Expression*
908   do_lower(Gogo*, Named_object*, int);
909
910   Type*
911   do_type();
912
913   void
914   do_determine_type(const Type_context*);
915
916   Expression*
917   do_copy()
918   { return this; }
919
920   bool
921   do_is_addressable() const
922   { return true; }
923
924   void
925   do_address_taken(bool);
926
927   tree
928   do_get_tree(Translate_context*);
929
930  private:
931   // The variable we are referencing.
932   Named_object* variable_;
933 };
934
935 // A reference to a temporary variable.
936
937 class Temporary_reference_expression : public Expression
938 {
939  public:
940   Temporary_reference_expression(Temporary_statement* statement,
941                                  source_location location)
942     : Expression(EXPRESSION_TEMPORARY_REFERENCE, location),
943       statement_(statement)
944   { }
945
946  protected:
947   Type*
948   do_type();
949
950   void
951   do_determine_type(const Type_context*)
952   { }
953
954   Expression*
955   do_copy()
956   { return make_temporary_reference(this->statement_, this->location()); }
957
958   bool
959   do_is_addressable() const
960   { return true; }
961
962   void
963   do_address_taken(bool);
964
965   tree
966   do_get_tree(Translate_context*);
967
968  private:
969   // The statement where the temporary variable is defined.
970   Temporary_statement* statement_;
971 };
972
973 // A string expression.
974
975 class String_expression : public Expression
976 {
977  public:
978   String_expression(const std::string& val, source_location location)
979     : Expression(EXPRESSION_STRING, location),
980       val_(val), type_(NULL)
981   { }
982
983   const std::string&
984   val() const
985   { return this->val_; }
986
987   static Expression*
988   do_import(Import*);
989
990  protected:
991   bool
992   do_is_constant() const
993   { return true; }
994
995   bool
996   do_string_constant_value(std::string* val) const
997   {
998     *val = this->val_;
999     return true;
1000   }
1001
1002   Type*
1003   do_type();
1004
1005   void
1006   do_determine_type(const Type_context*);
1007
1008   Expression*
1009   do_copy()
1010   { return this; }
1011
1012   tree
1013   do_get_tree(Translate_context*);
1014
1015   void
1016   do_export(Export*) const;
1017
1018  private:
1019   // The string value.  This is immutable.
1020   const std::string val_;
1021   // The type as determined by context.
1022   Type* type_;
1023 };
1024
1025 // A binary expression.
1026
1027 class Binary_expression : public Expression
1028 {
1029  public:
1030   Binary_expression(Operator op, Expression* left, Expression* right,
1031                     source_location location)
1032     : Expression(EXPRESSION_BINARY, location),
1033       op_(op), left_(left), right_(right)
1034   { }
1035
1036   // Return the operator.
1037   Operator
1038   op()
1039   { return this->op_; }
1040
1041   // Return the left hand expression.
1042   Expression*
1043   left()
1044   { return this->left_; }
1045
1046   // Return the right hand expression.
1047   Expression*
1048   right()
1049   { return this->right_; }
1050
1051   // Apply binary opcode OP to LEFT_VAL and RIGHT_VAL, setting VAL.
1052   // LEFT_TYPE is the type of LEFT_VAL, RIGHT_TYPE is the type of
1053   // RIGHT_VAL; LEFT_TYPE and/or RIGHT_TYPE may be NULL.  Return true
1054   // if this could be done, false if not.
1055   static bool
1056   eval_integer(Operator op, Type* left_type, mpz_t left_val,
1057                Type* right_type, mpz_t right_val, source_location,
1058                mpz_t val);
1059
1060   // Apply binary opcode OP to LEFT_VAL and RIGHT_VAL, setting VAL.
1061   // Return true if this could be done, false if not.
1062   static bool
1063   eval_float(Operator op, Type* left_type, mpfr_t left_val,
1064              Type* right_type, mpfr_t right_val, mpfr_t val,
1065              source_location);
1066
1067   // Apply binary opcode OP to LEFT_REAL/LEFT_IMAG and
1068   // RIGHT_REAL/RIGHT_IMAG, setting REAL/IMAG.  Return true if this
1069   // could be done, false if not.
1070   static bool
1071   eval_complex(Operator op, Type* left_type, mpfr_t left_real,
1072                mpfr_t left_imag, Type* right_type, mpfr_t right_real,
1073                mpfr_t right_imag, mpfr_t real, mpfr_t imag, source_location);
1074
1075   // Compare integer constants according to OP.
1076   static bool
1077   compare_integer(Operator op, mpz_t left_val, mpz_t right_val);
1078
1079   // Compare floating point constants according to OP.
1080   static bool
1081   compare_float(Operator op, Type* type, mpfr_t left_val, mpfr_t right_val);
1082
1083   // Compare complex constants according to OP.
1084   static bool
1085   compare_complex(Operator op, Type* type, mpfr_t left_real, mpfr_t left_imag,
1086                   mpfr_t right_val, mpfr_t right_imag);
1087
1088   static Expression*
1089   do_import(Import*);
1090
1091   // Report an error if OP can not be applied to TYPE.  Return whether
1092   // it can.
1093   static bool
1094   check_operator_type(Operator op, Type* type, source_location);
1095
1096  protected:
1097   int
1098   do_traverse(Traverse* traverse);
1099
1100   Expression*
1101   do_lower(Gogo*, Named_object*, int);
1102
1103   bool
1104   do_is_constant() const
1105   { return this->left_->is_constant() && this->right_->is_constant(); }
1106
1107   bool
1108   do_integer_constant_value(bool, mpz_t val, Type**) const;
1109
1110   bool
1111   do_float_constant_value(mpfr_t val, Type**) const;
1112
1113   bool
1114   do_complex_constant_value(mpfr_t real, mpfr_t imag, Type**) const;
1115
1116   void
1117   do_discarding_value();
1118
1119   Type*
1120   do_type();
1121
1122   void
1123   do_determine_type(const Type_context*);
1124
1125   void
1126   do_check_types(Gogo*);
1127
1128   Expression*
1129   do_copy()
1130   {
1131     return Expression::make_binary(this->op_, this->left_->copy(),
1132                                    this->right_->copy(), this->location());
1133   }
1134
1135   tree
1136   do_get_tree(Translate_context*);
1137
1138   void
1139   do_export(Export*) const;
1140
1141  private:
1142   // The binary operator to apply.
1143   Operator op_;
1144   // The left hand side operand.
1145   Expression* left_;
1146   // The right hand side operand.
1147   Expression* right_;
1148 };
1149
1150 // A call expression.  The go statement needs to dig inside this.
1151
1152 class Call_expression : public Expression
1153 {
1154  public:
1155   Call_expression(Expression* fn, Expression_list* args, bool is_varargs,
1156                   source_location location)
1157     : Expression(EXPRESSION_CALL, location),
1158       fn_(fn), args_(args), type_(NULL), tree_(NULL), is_varargs_(is_varargs),
1159       varargs_are_lowered_(false), types_are_determined_(false),
1160       is_deferred_(false)
1161   { }
1162
1163   // The function to call.
1164   Expression*
1165   fn() const
1166   { return this->fn_; }
1167
1168   // The arguments.
1169   Expression_list*
1170   args()
1171   { return this->args_; }
1172
1173   const Expression_list*
1174   args() const
1175   { return this->args_; }
1176
1177   // Get the function type.
1178   Function_type*
1179   get_function_type() const;
1180
1181   // Return the number of values this call will return.
1182   size_t
1183   result_count() const;
1184
1185   // Return whether this is a call to the predeclared function
1186   // recover.
1187   bool
1188   is_recover_call() const;
1189
1190   // Set the argument for a call to recover.
1191   void
1192   set_recover_arg(Expression*);
1193
1194   // Whether the last argument is a varargs argument (f(a...)).
1195   bool
1196   is_varargs() const
1197   { return this->is_varargs_; }
1198
1199   // Whether this call is being deferred.
1200   bool
1201   is_deferred() const
1202   { return this->is_deferred_; }
1203
1204   // Note that the call is being deferred.
1205   void
1206   set_is_deferred()
1207   { this->is_deferred_ = true; }
1208
1209  protected:
1210   int
1211   do_traverse(Traverse*);
1212
1213   virtual Expression*
1214   do_lower(Gogo*, Named_object*, int);
1215
1216   void
1217   do_discarding_value()
1218   { }
1219
1220   virtual Type*
1221   do_type();
1222
1223   virtual void
1224   do_determine_type(const Type_context*);
1225
1226   virtual void
1227   do_check_types(Gogo*);
1228
1229   Expression*
1230   do_copy()
1231   {
1232     return Expression::make_call(this->fn_->copy(),
1233                                  (this->args_ == NULL
1234                                   ? NULL
1235                                   : this->args_->copy()),
1236                                  this->is_varargs_, this->location());
1237   }
1238
1239   bool
1240   do_must_eval_in_order() const;
1241
1242   virtual tree
1243   do_get_tree(Translate_context*);
1244
1245   virtual bool
1246   do_is_recover_call() const;
1247
1248   virtual void
1249   do_set_recover_arg(Expression*);
1250
1251   // Let a builtin expression change the argument list.
1252   void
1253   set_args(Expression_list* args)
1254   { this->args_ = args; }
1255
1256   // Let a builtin expression lower varargs.
1257   Expression*
1258   lower_varargs(Gogo*, Named_object* function, Type* varargs_type,
1259                 size_t param_count);
1260
1261   // Let a builtin expression check whether types have been
1262   // determined.
1263   bool
1264   determining_types();
1265
1266  private:
1267   bool
1268   check_argument_type(int, const Type*, const Type*, source_location, bool);
1269
1270   tree
1271   bound_method_function(Translate_context*, Bound_method_expression*, tree*);
1272
1273   tree
1274   interface_method_function(Translate_context*,
1275                             Interface_field_reference_expression*,
1276                             tree*);
1277
1278   // The function to call.
1279   Expression* fn_;
1280   // The arguments to pass.  This may be NULL if there are no
1281   // arguments.
1282   Expression_list* args_;
1283   // The type of the expression, to avoid recomputing it.
1284   Type* type_;
1285   // The tree for the call, used for a call which returns a tuple.
1286   tree tree_;
1287   // True if the last argument is a varargs argument (f(a...)).
1288   bool is_varargs_;
1289   // True if varargs have already been lowered.
1290   bool varargs_are_lowered_;
1291   // True if types have been determined.
1292   bool types_are_determined_;
1293   // True if the call is an argument to a defer statement.
1294   bool is_deferred_;
1295 };
1296
1297 // An expression which represents a pointer to a function.
1298
1299 class Func_expression : public Expression
1300 {
1301  public:
1302   Func_expression(Named_object* function, Expression* closure,
1303                   source_location location)
1304     : Expression(EXPRESSION_FUNC_REFERENCE, location),
1305       function_(function), closure_(closure)
1306   { }
1307
1308   // Return the object associated with the function.
1309   const Named_object*
1310   named_object() const
1311   { return this->function_; }
1312
1313   // Return the closure for this function.  This will return NULL if
1314   // the function has no closure, which is the normal case.
1315   Expression*
1316   closure()
1317   { return this->closure_; }
1318
1319   // Return a tree for this function without evaluating the closure.
1320   tree
1321   get_tree_without_closure(Gogo*);
1322
1323  protected:
1324   int
1325   do_traverse(Traverse*);
1326
1327   Type*
1328   do_type();
1329
1330   void
1331   do_determine_type(const Type_context*)
1332   {
1333     if (this->closure_ != NULL)
1334       this->closure_->determine_type_no_context();
1335   }
1336
1337   Expression*
1338   do_copy()
1339   {
1340     return Expression::make_func_reference(this->function_,
1341                                            (this->closure_ == NULL
1342                                             ? NULL
1343                                             : this->closure_->copy()),
1344                                            this->location());
1345   }
1346
1347   tree
1348   do_get_tree(Translate_context*);
1349
1350  private:
1351   // The function itself.
1352   Named_object* function_;
1353   // A closure.  This is normally NULL.  For a nested function, it may
1354   // be a heap-allocated struct holding pointers to all the variables
1355   // referenced by this function and defined in enclosing functions.
1356   Expression* closure_;
1357 };
1358
1359 // A reference to an unknown name.
1360
1361 class Unknown_expression : public Parser_expression
1362 {
1363  public:
1364   Unknown_expression(Named_object* named_object, source_location location)
1365     : Parser_expression(EXPRESSION_UNKNOWN_REFERENCE, location),
1366       named_object_(named_object), is_composite_literal_key_(false)
1367   { }
1368
1369   // The associated named object.
1370   Named_object*
1371   named_object() const
1372   { return this->named_object_; }
1373
1374   // The name of the identifier which was unknown.
1375   const std::string&
1376   name() const;
1377
1378   // Note that this expression is being used as the key in a composite
1379   // literal, so it may be OK if it is not resolved.
1380   void
1381   set_is_composite_literal_key()
1382   { this->is_composite_literal_key_ = true; }
1383
1384   // Note that this expression should no longer be treated as a
1385   // composite literal key.
1386   void
1387   clear_is_composite_literal_key()
1388   { this->is_composite_literal_key_ = false; }
1389
1390  protected:
1391   Expression*
1392   do_lower(Gogo*, Named_object*, int);
1393
1394   Expression*
1395   do_copy()
1396   { return new Unknown_expression(this->named_object_, this->location()); }
1397
1398  private:
1399   // The unknown name.
1400   Named_object* named_object_;
1401   // True if this is the key in a composite literal.
1402   bool is_composite_literal_key_;
1403 };
1404
1405 // An index expression.  This is lowered to an array index, a string
1406 // index, or a map index.
1407
1408 class Index_expression : public Parser_expression
1409 {
1410  public:
1411   Index_expression(Expression* left, Expression* start, Expression* end,
1412                    source_location location)
1413     : Parser_expression(EXPRESSION_INDEX, location),
1414       left_(left), start_(start), end_(end), is_lvalue_(false)
1415   { }
1416
1417   // Record that this expression is an lvalue.
1418   void
1419   set_is_lvalue()
1420   { this->is_lvalue_ = true; }
1421
1422  protected:
1423   int
1424   do_traverse(Traverse*);
1425
1426   Expression*
1427   do_lower(Gogo*, Named_object*, int);
1428
1429   Expression*
1430   do_copy()
1431   {
1432     return new Index_expression(this->left_->copy(), this->start_->copy(),
1433                                 (this->end_ == NULL
1434                                  ? NULL
1435                                  : this->end_->copy()),
1436                                 this->location());
1437   }
1438
1439  private:
1440   // The expression being indexed.
1441   Expression* left_;
1442   // The first index.
1443   Expression* start_;
1444   // The second index.  This is NULL for an index, non-NULL for a
1445   // slice.
1446   Expression* end_;
1447   // Whether this is being used as an l-value.  We set this during the
1448   // parse because map index expressions need to know.
1449   bool is_lvalue_;
1450 };
1451
1452 // An index into a map.
1453
1454 class Map_index_expression : public Expression
1455 {
1456  public:
1457   Map_index_expression(Expression* map, Expression* index,
1458                        source_location location)
1459     : Expression(EXPRESSION_MAP_INDEX, location),
1460       map_(map), index_(index), is_lvalue_(false),
1461       is_in_tuple_assignment_(false)
1462   { }
1463
1464   // Return the map.
1465   Expression*
1466   map()
1467   { return this->map_; }
1468
1469   const Expression*
1470   map() const
1471   { return this->map_; }
1472
1473   // Return the index.
1474   Expression*
1475   index()
1476   { return this->index_; }
1477
1478   const Expression*
1479   index() const
1480   { return this->index_; }
1481
1482   // Get the type of the map being indexed.
1483   Map_type*
1484   get_map_type() const;
1485
1486   // Record that this map expression is an lvalue.  The difference is
1487   // that an lvalue always inserts the key.
1488   void
1489   set_is_lvalue()
1490   { this->is_lvalue_ = true; }
1491
1492   // Return whether this map expression occurs in an assignment to a
1493   // pair of values.
1494   bool
1495   is_in_tuple_assignment() const
1496   { return this->is_in_tuple_assignment_; }
1497
1498   // Record that this map expression occurs in an assignment to a pair
1499   // of values.
1500   void
1501   set_is_in_tuple_assignment()
1502   { this->is_in_tuple_assignment_ = true; }
1503
1504   // Return a tree for the map index.  This returns a tree which
1505   // evaluates to a pointer to a value in the map.  If INSERT is true,
1506   // the key will be inserted if not present, and the value pointer
1507   // will be zero initialized.  If INSERT is false, and the key is not
1508   // present in the map, the pointer will be NULL.
1509   tree
1510   get_value_pointer(Translate_context*, bool insert);
1511
1512  protected:
1513   int
1514   do_traverse(Traverse*);
1515
1516   Type*
1517   do_type();
1518
1519   void
1520   do_determine_type(const Type_context*);
1521
1522   void
1523   do_check_types(Gogo*);
1524
1525   Expression*
1526   do_copy()
1527   {
1528     return Expression::make_map_index(this->map_->copy(),
1529                                       this->index_->copy(),
1530                                       this->location());
1531   }
1532
1533   // A map index expression is an lvalue but it is not addressable.
1534
1535   tree
1536   do_get_tree(Translate_context*);
1537
1538  private:
1539   // The map we are looking into.
1540   Expression* map_;
1541   // The index.
1542   Expression* index_;
1543   // Whether this is an lvalue.
1544   bool is_lvalue_;
1545   // Whether this is in a tuple assignment to a pair of values.
1546   bool is_in_tuple_assignment_;
1547 };
1548
1549 // An expression which represents a method bound to its first
1550 // argument.
1551
1552 class Bound_method_expression : public Expression
1553 {
1554  public:
1555   Bound_method_expression(Expression* expr, Expression* method,
1556                           source_location location)
1557     : Expression(EXPRESSION_BOUND_METHOD, location),
1558       expr_(expr), expr_type_(NULL), method_(method)
1559   { }
1560
1561   // Return the object which is the first argument.
1562   Expression*
1563   first_argument()
1564   { return this->expr_; }
1565
1566   // Return the implicit type of the first argument.  This will be
1567   // non-NULL when using a method from an anonymous field without
1568   // using an explicit stub.
1569   Type*
1570   first_argument_type() const
1571   { return this->expr_type_; }
1572
1573   // Return the reference to the method function.
1574   Expression*
1575   method()
1576   { return this->method_; }
1577
1578   // Set the implicit type of the expression.
1579   void
1580   set_first_argument_type(Type* type)
1581   { this->expr_type_ = type; }
1582
1583  protected:
1584   int
1585   do_traverse(Traverse*);
1586
1587   Type*
1588   do_type();
1589
1590   void
1591   do_determine_type(const Type_context*);
1592
1593   void
1594   do_check_types(Gogo*);
1595
1596   Expression*
1597   do_copy()
1598   {
1599     return new Bound_method_expression(this->expr_->copy(),
1600                                        this->method_->copy(),
1601                                        this->location());
1602   }
1603
1604   tree
1605   do_get_tree(Translate_context*);
1606
1607  private:
1608   // The object used to find the method.  This is passed to the method
1609   // as the first argument.
1610   Expression* expr_;
1611   // The implicit type of the object to pass to the method.  This is
1612   // NULL in the normal case, non-NULL when using a method from an
1613   // anonymous field which does not require a stub.
1614   Type* expr_type_;
1615   // The method itself.  This is a Func_expression.
1616   Expression* method_;
1617 };
1618
1619 // A reference to a field in a struct.
1620
1621 class Field_reference_expression : public Expression
1622 {
1623  public:
1624   Field_reference_expression(Expression* expr, unsigned int field_index,
1625                              source_location location)
1626     : Expression(EXPRESSION_FIELD_REFERENCE, location),
1627       expr_(expr), field_index_(field_index)
1628   { }
1629
1630   // Return the struct expression.
1631   Expression*
1632   expr() const
1633   { return this->expr_; }
1634
1635   // Return the field index.
1636   unsigned int
1637   field_index() const
1638   { return this->field_index_; }
1639
1640   // Set the struct expression.  This is used when parsing.
1641   void
1642   set_struct_expression(Expression* expr)
1643   {
1644     go_assert(this->expr_ == NULL);
1645     this->expr_ = expr;
1646   }
1647
1648  protected:
1649   int
1650   do_traverse(Traverse* traverse)
1651   { return Expression::traverse(&this->expr_, traverse); }
1652
1653   Type*
1654   do_type();
1655
1656   void
1657   do_determine_type(const Type_context*)
1658   { this->expr_->determine_type_no_context(); }
1659
1660   void
1661   do_check_types(Gogo*);
1662
1663   Expression*
1664   do_copy()
1665   {
1666     return Expression::make_field_reference(this->expr_->copy(),
1667                                             this->field_index_,
1668                                             this->location());
1669   }
1670
1671   bool
1672   do_is_addressable() const
1673   { return this->expr_->is_addressable(); }
1674
1675   tree
1676   do_get_tree(Translate_context*);
1677
1678  private:
1679   // The expression we are looking into.  This should have a type of
1680   // struct.
1681   Expression* expr_;
1682   // The zero-based index of the field we are retrieving.
1683   unsigned int field_index_;
1684 };
1685
1686 // A reference to a field of an interface.
1687
1688 class Interface_field_reference_expression : public Expression
1689 {
1690  public:
1691   Interface_field_reference_expression(Expression* expr,
1692                                        const std::string& name,
1693                                        source_location location)
1694     : Expression(EXPRESSION_INTERFACE_FIELD_REFERENCE, location),
1695       expr_(expr), name_(name)
1696   { }
1697
1698   // Return the expression for the interface object.
1699   Expression*
1700   expr()
1701   { return this->expr_; }
1702
1703   // Return the name of the method to call.
1704   const std::string&
1705   name() const
1706   { return this->name_; }
1707
1708   // Return a tree for the pointer to the function to call, given a
1709   // tree for the expression.
1710   tree
1711   get_function_tree(Translate_context*, tree);
1712
1713   // Return a tree for the first argument to pass to the interface
1714   // function, given a tree for the expression.  This is the real
1715   // object associated with the interface object.
1716   tree
1717   get_underlying_object_tree(Translate_context*, tree);
1718
1719  protected:
1720   int
1721   do_traverse(Traverse* traverse);
1722
1723   Type*
1724   do_type();
1725
1726   void
1727   do_determine_type(const Type_context*);
1728
1729   void
1730   do_check_types(Gogo*);
1731
1732   Expression*
1733   do_copy()
1734   {
1735     return Expression::make_interface_field_reference(this->expr_->copy(),
1736                                                       this->name_,
1737                                                       this->location());
1738   }
1739
1740   tree
1741   do_get_tree(Translate_context*);
1742
1743  private:
1744   // The expression for the interface object.  This should have a type
1745   // of interface or pointer to interface.
1746   Expression* expr_;
1747   // The field we are retrieving--the name of the method.
1748   std::string name_;
1749 };
1750
1751 // A type guard expression.
1752
1753 class Type_guard_expression : public Expression
1754 {
1755  public:
1756   Type_guard_expression(Expression* expr, Type* type, source_location location)
1757     : Expression(EXPRESSION_TYPE_GUARD, location),
1758       expr_(expr), type_(type)
1759   { }
1760
1761   // Return the expression to convert.
1762   Expression*
1763   expr()
1764   { return this->expr_; }
1765
1766   // Return the type to which to convert.
1767   Type*
1768   type()
1769   { return this->type_; }
1770
1771  protected:
1772   int
1773   do_traverse(Traverse* traverse);
1774
1775   Type*
1776   do_type()
1777   { return this->type_; }
1778
1779   void
1780   do_determine_type(const Type_context*)
1781   { this->expr_->determine_type_no_context(); }
1782
1783   void
1784   do_check_types(Gogo*);
1785
1786   Expression*
1787   do_copy()
1788   {
1789     return new Type_guard_expression(this->expr_->copy(), this->type_,
1790                                      this->location());
1791   }
1792
1793   tree
1794   do_get_tree(Translate_context*);
1795
1796  private:
1797   // The expression to convert.
1798   Expression* expr_;
1799   // The type to which to convert.
1800   Type* type_;
1801 };
1802
1803 // A receive expression.
1804
1805 class Receive_expression : public Expression
1806 {
1807  public:
1808   Receive_expression(Expression* channel, source_location location)
1809     : Expression(EXPRESSION_RECEIVE, location),
1810       channel_(channel), for_select_(false)
1811   { }
1812
1813   // Return the channel.
1814   Expression*
1815   channel()
1816   { return this->channel_; }
1817
1818   // Note that this is for a select statement.
1819   void
1820   set_for_select()
1821   { this->for_select_ = true; }
1822
1823  protected:
1824   int
1825   do_traverse(Traverse* traverse)
1826   { return Expression::traverse(&this->channel_, traverse); }
1827
1828   void
1829   do_discarding_value()
1830   { }
1831
1832   Type*
1833   do_type();
1834
1835   void
1836   do_determine_type(const Type_context*)
1837   { this->channel_->determine_type_no_context(); }
1838
1839   void
1840   do_check_types(Gogo*);
1841
1842   Expression*
1843   do_copy()
1844   {
1845     return Expression::make_receive(this->channel_->copy(), this->location());
1846   }
1847
1848   bool
1849   do_must_eval_in_order() const
1850   { return true; }
1851
1852   tree
1853   do_get_tree(Translate_context*);
1854
1855  private:
1856   // The channel from which we are receiving.
1857   Expression* channel_;
1858   // Whether this is for a select statement.
1859   bool for_select_;
1860 };
1861
1862 #endif // !defined(GO_EXPRESSIONS_H)