OSDN Git Service

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