OSDN Git Service

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