OSDN Git Service

bb4f23e5810a88d0eda127a3f6b08e344b0b8895
[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 by the parser if the value of this expression is
363   // being discarded.  This issues warnings about computed values
364   // being 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 the tree for this expression.
586   tree
587   get_tree(Translate_context*);
588
589   // Return a tree handling any conversions which must be done during
590   // assignment.
591   static tree
592   convert_for_assignment(Translate_context*, Type* lhs_type, Type* rhs_type,
593                          tree rhs_tree, source_location location);
594
595   // Return a tree converting a value of one interface type to another
596   // interface type.  If FOR_TYPE_GUARD is true this is for a type
597   // assertion.
598   static tree
599   convert_interface_to_interface(Translate_context*, Type* lhs_type,
600                                  Type* rhs_type, tree rhs_tree,
601                                  bool for_type_guard, source_location);
602
603   // Return a tree implementing the comparison LHS_TREE OP RHS_TREE.
604   // TYPE is the type of both sides.
605   static tree
606   comparison_tree(Translate_context*, Operator op, Type* left_type,
607                   tree left_tree, Type* right_type, tree right_tree,
608                   source_location);
609
610   // Return a tree for the multi-precision integer VAL in TYPE.
611   static tree
612   integer_constant_tree(mpz_t val, tree type);
613
614   // Return a tree for the floating point value VAL in TYPE.
615   static tree
616   float_constant_tree(mpfr_t val, tree type);
617
618   // Return a tree for the complex value REAL/IMAG in TYPE.
619   static tree
620   complex_constant_tree(mpfr_t real, mpfr_t imag, tree type);
621
622   // Export the expression.  This is only used for constants.  It will
623   // be used for things like values of named constants and sizes of
624   // arrays.
625   void
626   export_expression(Export* exp) const
627   { this->do_export(exp); }
628
629   // Import an expression.
630   static Expression*
631   import_expression(Import*);
632
633   // Return a tree which checks that VAL, of arbitrary integer type,
634   // is non-negative and is not more than the maximum value of
635   // BOUND_TYPE.  If SOFAR is not NULL, it is or'red into the result.
636   // The return value may be NULL if SOFAR is NULL.
637   static tree
638   check_bounds(tree val, tree bound_type, tree sofar, source_location);
639
640   // Dump an expression to a dump constext.
641   void
642   dump_expression(Ast_dump_context*) const;
643
644  protected:
645   // May be implemented by child class: traverse the expressions.
646   virtual int
647   do_traverse(Traverse*);
648
649   // Return a lowered expression.
650   virtual Expression*
651   do_lower(Gogo*, Named_object*, Statement_inserter*, int)
652   { return this; }
653
654   // Return whether this is a constant expression.
655   virtual bool
656   do_is_constant() const
657   { return false; }
658
659   // Return whether this is a constant expression of integral type,
660   // and set VAL to the value.
661   virtual bool
662   do_integer_constant_value(bool, mpz_t, Type**) const
663   { return false; }
664
665   // Return whether this is a constant expression of floating point
666   // type, and set VAL to the value.
667   virtual bool
668   do_float_constant_value(mpfr_t, Type**) const
669   { return false; }
670
671   // Return whether this is a constant expression of complex type, and
672   // set REAL and IMAGE to the value.
673   virtual bool
674   do_complex_constant_value(mpfr_t, mpfr_t, Type**) const
675   { return false; }
676
677   // Return whether this is a constant expression of string type, and
678   // set VAL to the value.
679   virtual bool
680   do_string_constant_value(std::string*) const
681   { return false; }
682
683   // Called by the parser if the value is being discarded.
684   virtual void
685   do_discarding_value();
686
687   // Child class holds type.
688   virtual Type*
689   do_type() = 0;
690
691   // Child class implements determining type information.
692   virtual void
693   do_determine_type(const Type_context*) = 0;
694
695   // Child class implements type checking if needed.
696   virtual void
697   do_check_types(Gogo*)
698   { }
699
700   // Child class implements copying.
701   virtual Expression*
702   do_copy() = 0;
703
704   // Child class implements whether the expression is addressable.
705   virtual bool
706   do_is_addressable() const
707   { return false; }
708
709   // Child class implements taking the address of an expression.
710   virtual void
711   do_address_taken(bool)
712   { }
713
714   // Child class implements whether this expression must be evaluated
715   // in order.
716   virtual bool
717   do_must_eval_in_order() const
718   { return false; }
719
720   // Child class implements conversion to tree.
721   virtual tree
722   do_get_tree(Translate_context*) = 0;
723
724   // Child class implements export.
725   virtual void
726   do_export(Export*) const;
727
728   // For children to call to warn about an unused value.
729   void
730   warn_about_unused_value();
731
732   // For children to call when they detect that they are in error.
733   void
734   set_is_error();
735
736   // For children to call to report an error conveniently.
737   void
738   report_error(const char*);
739
740   // Child class implements dumping to a dump context.
741   virtual void
742   do_dump_expression(Ast_dump_context*) const = 0;
743
744  private:
745   // Convert to the desired statement classification, or return NULL.
746   // This is a controlled dynamic cast.
747   template<typename Expression_class,
748            Expression_classification expr_classification>
749   Expression_class*
750   convert()
751   {
752     return (this->classification_ == expr_classification
753             ? static_cast<Expression_class*>(this)
754             : NULL);
755   }
756
757   template<typename Expression_class,
758            Expression_classification expr_classification>
759   const Expression_class*
760   convert() const
761   {
762     return (this->classification_ == expr_classification
763             ? static_cast<const Expression_class*>(this)
764             : NULL);
765   }
766
767   static tree
768   convert_type_to_interface(Translate_context*, Type*, Type*, tree,
769                             source_location);
770
771   static tree
772   get_interface_type_descriptor(Translate_context*, Type*, tree,
773                                 source_location);
774
775   static tree
776   convert_interface_to_type(Translate_context*, Type*, Type*, tree,
777                             source_location);
778
779   // The expression classification.
780   Expression_classification classification_;
781   // The location in the input file.
782   source_location location_;
783 };
784
785 // A list of Expressions.
786
787 class Expression_list
788 {
789  public:
790   Expression_list()
791     : entries_()
792   { }
793
794   // Return whether the list is empty.
795   bool
796   empty() const
797   { return this->entries_.empty(); }
798
799   // Return the number of entries in the list.
800   size_t
801   size() const
802   { return this->entries_.size(); }
803
804   // Add an entry to the end of the list.
805   void
806   push_back(Expression* expr)
807   { this->entries_.push_back(expr); }
808
809   void
810   append(Expression_list* add)
811   { this->entries_.insert(this->entries_.end(), add->begin(), add->end()); }
812
813   // Reserve space in the list.
814   void
815   reserve(size_t size)
816   { this->entries_.reserve(size); }
817
818   // Traverse the expressions in the list.
819   int
820   traverse(Traverse*);
821
822   // Copy the list.
823   Expression_list*
824   copy();
825
826   // Return true if the list contains an error expression.
827   bool
828   contains_error() const;
829
830   // Return the first and last elements.
831   Expression*&
832   front()
833   { return this->entries_.front(); }
834
835   Expression*
836   front() const
837   { return this->entries_.front(); }
838
839   Expression*&
840   back()
841   { return this->entries_.back(); }
842
843   Expression*
844   back() const
845   { return this->entries_.back(); }
846
847   // Iterators.
848
849   typedef std::vector<Expression*>::iterator iterator;
850   typedef std::vector<Expression*>::const_iterator const_iterator;
851
852   iterator
853   begin()
854   { return this->entries_.begin(); }
855
856   const_iterator
857   begin() const
858   { return this->entries_.begin(); }
859
860   iterator
861   end()
862   { return this->entries_.end(); }
863
864   const_iterator
865   end() const
866   { return this->entries_.end(); }
867
868   // Erase an entry.
869   void
870   erase(iterator p)
871   { this->entries_.erase(p); }
872
873  private:
874   std::vector<Expression*> entries_;
875 };
876
877 // An abstract base class for an expression which is only used by the
878 // parser, and is lowered in the lowering pass.
879
880 class Parser_expression : public Expression
881 {
882  public:
883   Parser_expression(Expression_classification classification,
884                     source_location location)
885     : Expression(classification, location)
886   { }
887
888  protected:
889   virtual Expression*
890   do_lower(Gogo*, Named_object*, Statement_inserter*, int) = 0;
891
892   Type*
893   do_type();
894
895   void
896   do_determine_type(const Type_context*)
897   { go_unreachable(); }
898
899   void
900   do_check_types(Gogo*)
901   { go_unreachable(); }
902
903   tree
904   do_get_tree(Translate_context*)
905   { go_unreachable(); }
906 };
907
908 // An expression which is simply a variable.
909
910 class Var_expression : public Expression
911 {
912  public:
913   Var_expression(Named_object* variable, source_location location)
914     : Expression(EXPRESSION_VAR_REFERENCE, location),
915       variable_(variable)
916   { }
917
918   // Return the variable.
919   Named_object*
920   named_object() const
921   { return this->variable_; }
922
923  protected:
924   Expression*
925   do_lower(Gogo*, Named_object*, Statement_inserter*, int);
926
927   Type*
928   do_type();
929
930   void
931   do_determine_type(const Type_context*);
932
933   Expression*
934   do_copy()
935   { return this; }
936
937   bool
938   do_is_addressable() const
939   { return true; }
940
941   void
942   do_address_taken(bool);
943
944   tree
945   do_get_tree(Translate_context*);
946
947   void
948   do_dump_expression(Ast_dump_context*) const;
949
950  private:
951   // The variable we are referencing.
952   Named_object* variable_;
953 };
954
955 // A reference to a temporary variable.
956
957 class Temporary_reference_expression : public Expression
958 {
959  public:
960   Temporary_reference_expression(Temporary_statement* statement,
961                                  source_location location)
962     : Expression(EXPRESSION_TEMPORARY_REFERENCE, location),
963       statement_(statement), is_lvalue_(false)
964   { }
965
966   // Indicate that this reference appears on the left hand side of an
967   // assignment statement.
968   void
969   set_is_lvalue()
970   { this->is_lvalue_ = true; }
971
972  protected:
973   Type*
974   do_type();
975
976   void
977   do_determine_type(const Type_context*)
978   { }
979
980   Expression*
981   do_copy()
982   { return make_temporary_reference(this->statement_, this->location()); }
983
984   bool
985   do_is_addressable() const
986   { return true; }
987
988   void
989   do_address_taken(bool);
990
991   tree
992   do_get_tree(Translate_context*);
993
994   void
995   do_dump_expression(Ast_dump_context*) const;
996
997  private:
998   // The statement where the temporary variable is defined.
999   Temporary_statement* statement_;
1000   // Whether this reference appears on the left hand side of an
1001   // assignment statement.
1002   bool is_lvalue_;
1003 };
1004
1005 // A string expression.
1006
1007 class String_expression : public Expression
1008 {
1009  public:
1010   String_expression(const std::string& val, source_location location)
1011     : Expression(EXPRESSION_STRING, location),
1012       val_(val), type_(NULL)
1013   { }
1014
1015   const std::string&
1016   val() const
1017   { return this->val_; }
1018
1019   static Expression*
1020   do_import(Import*);
1021
1022  protected:
1023   bool
1024   do_is_constant() const
1025   { return true; }
1026
1027   bool
1028   do_string_constant_value(std::string* val) const
1029   {
1030     *val = this->val_;
1031     return true;
1032   }
1033
1034   Type*
1035   do_type();
1036
1037   void
1038   do_determine_type(const Type_context*);
1039
1040   Expression*
1041   do_copy()
1042   { return this; }
1043
1044   tree
1045   do_get_tree(Translate_context*);
1046
1047   // Write string literal to a string dump.
1048   static void
1049   export_string(String_dump* exp, const String_expression* str);
1050
1051   void
1052   do_export(Export*) const;
1053
1054   void
1055   do_dump_expression(Ast_dump_context*) const;
1056
1057  private:
1058   // The string value.  This is immutable.
1059   const std::string val_;
1060   // The type as determined by context.
1061   Type* type_;
1062 };
1063
1064 // A binary expression.
1065
1066 class Binary_expression : public Expression
1067 {
1068  public:
1069   Binary_expression(Operator op, Expression* left, Expression* right,
1070                     source_location location)
1071     : Expression(EXPRESSION_BINARY, location),
1072       op_(op), left_(left), right_(right)
1073   { }
1074
1075   // Return the operator.
1076   Operator
1077   op()
1078   { return this->op_; }
1079
1080   // Return the left hand expression.
1081   Expression*
1082   left()
1083   { return this->left_; }
1084
1085   // Return the right hand expression.
1086   Expression*
1087   right()
1088   { return this->right_; }
1089
1090   // Apply binary opcode OP to LEFT_VAL and RIGHT_VAL, setting VAL.
1091   // LEFT_TYPE is the type of LEFT_VAL, RIGHT_TYPE is the type of
1092   // RIGHT_VAL; LEFT_TYPE and/or RIGHT_TYPE may be NULL.  Return true
1093   // if this could be done, false if not.
1094   static bool
1095   eval_integer(Operator op, Type* left_type, mpz_t left_val,
1096                Type* right_type, mpz_t right_val, source_location,
1097                mpz_t val);
1098
1099   // Apply binary opcode OP to LEFT_VAL and RIGHT_VAL, setting VAL.
1100   // Return true if this could be done, false if not.
1101   static bool
1102   eval_float(Operator op, Type* left_type, mpfr_t left_val,
1103              Type* right_type, mpfr_t right_val, mpfr_t val,
1104              source_location);
1105
1106   // Apply binary opcode OP to LEFT_REAL/LEFT_IMAG and
1107   // RIGHT_REAL/RIGHT_IMAG, setting REAL/IMAG.  Return true if this
1108   // could be done, false if not.
1109   static bool
1110   eval_complex(Operator op, Type* left_type, mpfr_t left_real,
1111                mpfr_t left_imag, Type* right_type, mpfr_t right_real,
1112                mpfr_t right_imag, mpfr_t real, mpfr_t imag, source_location);
1113
1114   // Compare integer constants according to OP.
1115   static bool
1116   compare_integer(Operator op, mpz_t left_val, mpz_t right_val);
1117
1118   // Compare floating point constants according to OP.
1119   static bool
1120   compare_float(Operator op, Type* type, mpfr_t left_val, mpfr_t right_val);
1121
1122   // Compare complex constants according to OP.
1123   static bool
1124   compare_complex(Operator op, Type* type, mpfr_t left_real, mpfr_t left_imag,
1125                   mpfr_t right_val, mpfr_t right_imag);
1126
1127   static Expression*
1128   do_import(Import*);
1129
1130   // Report an error if OP can not be applied to TYPE.  Return whether
1131   // it can.
1132   static bool
1133   check_operator_type(Operator op, Type* type, source_location);
1134
1135  protected:
1136   int
1137   do_traverse(Traverse* traverse);
1138
1139   Expression*
1140   do_lower(Gogo*, Named_object*, Statement_inserter*, int);
1141
1142   bool
1143   do_is_constant() const
1144   { return this->left_->is_constant() && this->right_->is_constant(); }
1145
1146   bool
1147   do_integer_constant_value(bool, mpz_t val, Type**) const;
1148
1149   bool
1150   do_float_constant_value(mpfr_t val, Type**) const;
1151
1152   bool
1153   do_complex_constant_value(mpfr_t real, mpfr_t imag, Type**) const;
1154
1155   void
1156   do_discarding_value();
1157
1158   Type*
1159   do_type();
1160
1161   void
1162   do_determine_type(const Type_context*);
1163
1164   void
1165   do_check_types(Gogo*);
1166
1167   Expression*
1168   do_copy()
1169   {
1170     return Expression::make_binary(this->op_, this->left_->copy(),
1171                                    this->right_->copy(), this->location());
1172   }
1173
1174   tree
1175   do_get_tree(Translate_context*);
1176
1177   void
1178   do_export(Export*) const;
1179
1180   void
1181   do_dump_expression(Ast_dump_context*) const;
1182
1183  private:
1184   // The binary operator to apply.
1185   Operator op_;
1186   // The left hand side operand.
1187   Expression* left_;
1188   // The right hand side operand.
1189   Expression* right_;
1190 };
1191
1192 // A call expression.  The go statement needs to dig inside this.
1193
1194 class Call_expression : public Expression
1195 {
1196  public:
1197   Call_expression(Expression* fn, Expression_list* args, bool is_varargs,
1198                   source_location location)
1199     : Expression(EXPRESSION_CALL, location),
1200       fn_(fn), args_(args), type_(NULL), results_(NULL), tree_(NULL),
1201       is_varargs_(is_varargs), varargs_are_lowered_(false),
1202       types_are_determined_(false), is_deferred_(false), issued_error_(false)
1203   { }
1204
1205   // The function to call.
1206   Expression*
1207   fn() const
1208   { return this->fn_; }
1209
1210   // The arguments.
1211   Expression_list*
1212   args()
1213   { return this->args_; }
1214
1215   const Expression_list*
1216   args() const
1217   { return this->args_; }
1218
1219   // Get the function type.
1220   Function_type*
1221   get_function_type() const;
1222
1223   // Return the number of values this call will return.
1224   size_t
1225   result_count() const;
1226
1227   // Return the temporary variable which holds result I.  This is only
1228   // valid after the expression has been lowered, and is only valid
1229   // for calls which return multiple results.
1230   Temporary_statement*
1231   result(size_t i) const;
1232
1233   // Return whether this is a call to the predeclared function
1234   // recover.
1235   bool
1236   is_recover_call() const;
1237
1238   // Set the argument for a call to recover.
1239   void
1240   set_recover_arg(Expression*);
1241
1242   // Whether the last argument is a varargs argument (f(a...)).
1243   bool
1244   is_varargs() const
1245   { return this->is_varargs_; }
1246
1247   // Note that varargs have already been lowered.
1248   void
1249   set_varargs_are_lowered()
1250   { this->varargs_are_lowered_ = true; }
1251
1252   // Whether this call is being deferred.
1253   bool
1254   is_deferred() const
1255   { return this->is_deferred_; }
1256
1257   // Note that the call is being deferred.
1258   void
1259   set_is_deferred()
1260   { this->is_deferred_ = true; }
1261
1262   // We have found an error with this call expression; return true if
1263   // we should report it.
1264   bool
1265   issue_error();
1266
1267  protected:
1268   int
1269   do_traverse(Traverse*);
1270
1271   virtual Expression*
1272   do_lower(Gogo*, Named_object*, Statement_inserter*, int);
1273
1274   void
1275   do_discarding_value()
1276   { }
1277
1278   virtual Type*
1279   do_type();
1280
1281   virtual void
1282   do_determine_type(const Type_context*);
1283
1284   virtual void
1285   do_check_types(Gogo*);
1286
1287   Expression*
1288   do_copy()
1289   {
1290     return Expression::make_call(this->fn_->copy(),
1291                                  (this->args_ == NULL
1292                                   ? NULL
1293                                   : this->args_->copy()),
1294                                  this->is_varargs_, this->location());
1295   }
1296
1297   bool
1298   do_must_eval_in_order() const;
1299
1300   virtual tree
1301   do_get_tree(Translate_context*);
1302
1303   virtual bool
1304   do_is_recover_call() const;
1305
1306   virtual void
1307   do_set_recover_arg(Expression*);
1308
1309   // Let a builtin expression change the argument list.
1310   void
1311   set_args(Expression_list* args)
1312   { this->args_ = args; }
1313
1314   // Let a builtin expression lower varargs.
1315   void
1316   lower_varargs(Gogo*, Named_object* function, Statement_inserter* inserter,
1317                 Type* varargs_type, size_t param_count);
1318
1319   // Let a builtin expression check whether types have been
1320   // determined.
1321   bool
1322   determining_types();
1323
1324   void
1325   do_dump_expression(Ast_dump_context*) const;
1326
1327  private:
1328   bool
1329   check_argument_type(int, const Type*, const Type*, source_location, bool);
1330
1331   tree
1332   interface_method_function(Translate_context*,
1333                             Interface_field_reference_expression*,
1334                             tree*);
1335
1336   tree
1337   set_results(Translate_context*, tree);
1338
1339   // The function to call.
1340   Expression* fn_;
1341   // The arguments to pass.  This may be NULL if there are no
1342   // arguments.
1343   Expression_list* args_;
1344   // The type of the expression, to avoid recomputing it.
1345   Type* type_;
1346   // The list of temporaries which will hold the results if the
1347   // function returns a tuple.
1348   std::vector<Temporary_statement*>* results_;
1349   // The tree for the call, used for a call which returns a tuple.
1350   tree tree_;
1351   // True if the last argument is a varargs argument (f(a...)).
1352   bool is_varargs_;
1353   // True if varargs have already been lowered.
1354   bool varargs_are_lowered_;
1355   // True if types have been determined.
1356   bool types_are_determined_;
1357   // True if the call is an argument to a defer statement.
1358   bool is_deferred_;
1359   // True if we reported an error about a mismatch between call
1360   // results and uses.  This is to avoid producing multiple errors
1361   // when there are multiple Call_result_expressions.
1362   bool issued_error_;
1363 };
1364
1365 // An expression which represents a pointer to a function.
1366
1367 class Func_expression : public Expression
1368 {
1369  public:
1370   Func_expression(Named_object* function, Expression* closure,
1371                   source_location location)
1372     : Expression(EXPRESSION_FUNC_REFERENCE, location),
1373       function_(function), closure_(closure)
1374   { }
1375
1376   // Return the object associated with the function.
1377   const Named_object*
1378   named_object() const
1379   { return this->function_; }
1380
1381   // Return the closure for this function.  This will return NULL if
1382   // the function has no closure, which is the normal case.
1383   Expression*
1384   closure()
1385   { return this->closure_; }
1386
1387   // Return a tree for this function without evaluating the closure.
1388   tree
1389   get_tree_without_closure(Gogo*);
1390
1391  protected:
1392   int
1393   do_traverse(Traverse*);
1394
1395   Type*
1396   do_type();
1397
1398   void
1399   do_determine_type(const Type_context*)
1400   {
1401     if (this->closure_ != NULL)
1402       this->closure_->determine_type_no_context();
1403   }
1404
1405   Expression*
1406   do_copy()
1407   {
1408     return Expression::make_func_reference(this->function_,
1409                                            (this->closure_ == NULL
1410                                             ? NULL
1411                                             : this->closure_->copy()),
1412                                            this->location());
1413   }
1414
1415   tree
1416   do_get_tree(Translate_context*);
1417
1418   void
1419   do_dump_expression(Ast_dump_context*) const;
1420
1421  private:
1422   // The function itself.
1423   Named_object* function_;
1424   // A closure.  This is normally NULL.  For a nested function, it may
1425   // be a heap-allocated struct holding pointers to all the variables
1426   // referenced by this function and defined in enclosing functions.
1427   Expression* closure_;
1428 };
1429
1430 // A reference to an unknown name.
1431
1432 class Unknown_expression : public Parser_expression
1433 {
1434  public:
1435   Unknown_expression(Named_object* named_object, source_location location)
1436     : Parser_expression(EXPRESSION_UNKNOWN_REFERENCE, location),
1437       named_object_(named_object), is_composite_literal_key_(false)
1438   { }
1439
1440   // The associated named object.
1441   Named_object*
1442   named_object() const
1443   { return this->named_object_; }
1444
1445   // The name of the identifier which was unknown.
1446   const std::string&
1447   name() const;
1448
1449   // Note that this expression is being used as the key in a composite
1450   // literal, so it may be OK if it is not resolved.
1451   void
1452   set_is_composite_literal_key()
1453   { this->is_composite_literal_key_ = true; }
1454
1455   // Note that this expression should no longer be treated as a
1456   // composite literal key.
1457   void
1458   clear_is_composite_literal_key()
1459   { this->is_composite_literal_key_ = false; }
1460
1461  protected:
1462   Expression*
1463   do_lower(Gogo*, Named_object*, Statement_inserter*, int);
1464
1465   Expression*
1466   do_copy()
1467   { return new Unknown_expression(this->named_object_, this->location()); }
1468
1469   void
1470   do_dump_expression(Ast_dump_context*) const;
1471   
1472  private:
1473   // The unknown name.
1474   Named_object* named_object_;
1475   // True if this is the key in a composite literal.
1476   bool is_composite_literal_key_;
1477 };
1478
1479 // An index expression.  This is lowered to an array index, a string
1480 // index, or a map index.
1481
1482 class Index_expression : public Parser_expression
1483 {
1484  public:
1485   Index_expression(Expression* left, Expression* start, Expression* end,
1486                    source_location location)
1487     : Parser_expression(EXPRESSION_INDEX, location),
1488       left_(left), start_(start), end_(end), is_lvalue_(false)
1489   { }
1490
1491   // Record that this expression is an lvalue.
1492   void
1493   set_is_lvalue()
1494   { this->is_lvalue_ = true; }
1495
1496   // Dump an index expression, i.e. an expression of the form
1497   // expr[expr] or expr[expr:expr], to a dump context.
1498   static void
1499   dump_index_expression(Ast_dump_context*, const Expression* expr, 
1500                         const Expression* start, const Expression* end);
1501
1502  protected:
1503   int
1504   do_traverse(Traverse*);
1505
1506   Expression*
1507   do_lower(Gogo*, Named_object*, Statement_inserter*, int);
1508
1509   Expression*
1510   do_copy()
1511   {
1512     return new Index_expression(this->left_->copy(), this->start_->copy(),
1513                                 (this->end_ == NULL
1514                                  ? NULL
1515                                  : this->end_->copy()),
1516                                 this->location());
1517   }
1518
1519   void
1520   do_dump_expression(Ast_dump_context*) const;
1521
1522  private:
1523   // The expression being indexed.
1524   Expression* left_;
1525   // The first index.
1526   Expression* start_;
1527   // The second index.  This is NULL for an index, non-NULL for a
1528   // slice.
1529   Expression* end_;
1530   // Whether this is being used as an l-value.  We set this during the
1531   // parse because map index expressions need to know.
1532   bool is_lvalue_;
1533 };
1534
1535 // An index into a map.
1536
1537 class Map_index_expression : public Expression
1538 {
1539  public:
1540   Map_index_expression(Expression* map, Expression* index,
1541                        source_location location)
1542     : Expression(EXPRESSION_MAP_INDEX, location),
1543       map_(map), index_(index), is_lvalue_(false),
1544       is_in_tuple_assignment_(false)
1545   { }
1546
1547   // Return the map.
1548   Expression*
1549   map()
1550   { return this->map_; }
1551
1552   const Expression*
1553   map() const
1554   { return this->map_; }
1555
1556   // Return the index.
1557   Expression*
1558   index()
1559   { return this->index_; }
1560
1561   const Expression*
1562   index() const
1563   { return this->index_; }
1564
1565   // Get the type of the map being indexed.
1566   Map_type*
1567   get_map_type() const;
1568
1569   // Record that this map expression is an lvalue.  The difference is
1570   // that an lvalue always inserts the key.
1571   void
1572   set_is_lvalue()
1573   { this->is_lvalue_ = true; }
1574
1575   // Return whether this map expression occurs in an assignment to a
1576   // pair of values.
1577   bool
1578   is_in_tuple_assignment() const
1579   { return this->is_in_tuple_assignment_; }
1580
1581   // Record that this map expression occurs in an assignment to a pair
1582   // of values.
1583   void
1584   set_is_in_tuple_assignment()
1585   { this->is_in_tuple_assignment_ = true; }
1586
1587   // Return a tree for the map index.  This returns a tree which
1588   // evaluates to a pointer to a value in the map.  If INSERT is true,
1589   // the key will be inserted if not present, and the value pointer
1590   // will be zero initialized.  If INSERT is false, and the key is not
1591   // present in the map, the pointer will be NULL.
1592   tree
1593   get_value_pointer(Translate_context*, bool insert);
1594
1595  protected:
1596   int
1597   do_traverse(Traverse*);
1598
1599   Type*
1600   do_type();
1601
1602   void
1603   do_determine_type(const Type_context*);
1604
1605   void
1606   do_check_types(Gogo*);
1607
1608   Expression*
1609   do_copy()
1610   {
1611     return Expression::make_map_index(this->map_->copy(),
1612                                       this->index_->copy(),
1613                                       this->location());
1614   }
1615
1616   // A map index expression is an lvalue but it is not addressable.
1617
1618   tree
1619   do_get_tree(Translate_context*);
1620
1621   void
1622   do_dump_expression(Ast_dump_context*) const;
1623
1624  private:
1625   // The map we are looking into.
1626   Expression* map_;
1627   // The index.
1628   Expression* index_;
1629   // Whether this is an lvalue.
1630   bool is_lvalue_;
1631   // Whether this is in a tuple assignment to a pair of values.
1632   bool is_in_tuple_assignment_;
1633 };
1634
1635 // An expression which represents a method bound to its first
1636 // argument.
1637
1638 class Bound_method_expression : public Expression
1639 {
1640  public:
1641   Bound_method_expression(Expression* expr, Named_object* method,
1642                           source_location location)
1643     : Expression(EXPRESSION_BOUND_METHOD, location),
1644       expr_(expr), expr_type_(NULL), method_(method)
1645   { }
1646
1647   // Return the object which is the first argument.
1648   Expression*
1649   first_argument()
1650   { return this->expr_; }
1651
1652   // Return the implicit type of the first argument.  This will be
1653   // non-NULL when using a method from an anonymous field without
1654   // using an explicit stub.
1655   Type*
1656   first_argument_type() const
1657   { return this->expr_type_; }
1658
1659   // Return the method function.
1660   Named_object*
1661   method()
1662   { return this->method_; }
1663
1664   // Set the implicit type of the expression.
1665   void
1666   set_first_argument_type(Type* type)
1667   { this->expr_type_ = type; }
1668
1669  protected:
1670   int
1671   do_traverse(Traverse*);
1672
1673   Type*
1674   do_type();
1675
1676   void
1677   do_determine_type(const Type_context*);
1678
1679   void
1680   do_check_types(Gogo*);
1681
1682   Expression*
1683   do_copy()
1684   {
1685     return new Bound_method_expression(this->expr_->copy(), this->method_,
1686                                        this->location());
1687   }
1688
1689   tree
1690   do_get_tree(Translate_context*);
1691
1692   void
1693   do_dump_expression(Ast_dump_context*) const;
1694
1695  private:
1696   // The object used to find the method.  This is passed to the method
1697   // as the first argument.
1698   Expression* expr_;
1699   // The implicit type of the object to pass to the method.  This is
1700   // NULL in the normal case, non-NULL when using a method from an
1701   // anonymous field which does not require a stub.
1702   Type* expr_type_;
1703   // The method itself.
1704   Named_object* method_;
1705 };
1706
1707 // A reference to a field in a struct.
1708
1709 class Field_reference_expression : public Expression
1710 {
1711  public:
1712   Field_reference_expression(Expression* expr, unsigned int field_index,
1713                              source_location location)
1714     : Expression(EXPRESSION_FIELD_REFERENCE, location),
1715       expr_(expr), field_index_(field_index)
1716   { }
1717
1718   // Return the struct expression.
1719   Expression*
1720   expr() const
1721   { return this->expr_; }
1722
1723   // Return the field index.
1724   unsigned int
1725   field_index() const
1726   { return this->field_index_; }
1727
1728   // Set the struct expression.  This is used when parsing.
1729   void
1730   set_struct_expression(Expression* expr)
1731   {
1732     go_assert(this->expr_ == NULL);
1733     this->expr_ = expr;
1734   }
1735
1736  protected:
1737   int
1738   do_traverse(Traverse* traverse)
1739   { return Expression::traverse(&this->expr_, traverse); }
1740
1741   Type*
1742   do_type();
1743
1744   void
1745   do_determine_type(const Type_context*)
1746   { this->expr_->determine_type_no_context(); }
1747
1748   void
1749   do_check_types(Gogo*);
1750
1751   Expression*
1752   do_copy()
1753   {
1754     return Expression::make_field_reference(this->expr_->copy(),
1755                                             this->field_index_,
1756                                             this->location());
1757   }
1758
1759   bool
1760   do_is_addressable() const
1761   { return this->expr_->is_addressable(); }
1762
1763   tree
1764   do_get_tree(Translate_context*);
1765
1766   void
1767   do_dump_expression(Ast_dump_context*) const;
1768
1769  private:
1770   // The expression we are looking into.  This should have a type of
1771   // struct.
1772   Expression* expr_;
1773   // The zero-based index of the field we are retrieving.
1774   unsigned int field_index_;
1775 };
1776
1777 // A reference to a field of an interface.
1778
1779 class Interface_field_reference_expression : public Expression
1780 {
1781  public:
1782   Interface_field_reference_expression(Expression* expr,
1783                                        const std::string& name,
1784                                        source_location location)
1785     : Expression(EXPRESSION_INTERFACE_FIELD_REFERENCE, location),
1786       expr_(expr), name_(name)
1787   { }
1788
1789   // Return the expression for the interface object.
1790   Expression*
1791   expr()
1792   { return this->expr_; }
1793
1794   // Return the name of the method to call.
1795   const std::string&
1796   name() const
1797   { return this->name_; }
1798
1799   // Return a tree for the pointer to the function to call, given a
1800   // tree for the expression.
1801   tree
1802   get_function_tree(Translate_context*, tree);
1803
1804   // Return a tree for the first argument to pass to the interface
1805   // function, given a tree for the expression.  This is the real
1806   // object associated with the interface object.
1807   tree
1808   get_underlying_object_tree(Translate_context*, tree);
1809
1810  protected:
1811   int
1812   do_traverse(Traverse* traverse);
1813
1814   Type*
1815   do_type();
1816
1817   void
1818   do_determine_type(const Type_context*);
1819
1820   void
1821   do_check_types(Gogo*);
1822
1823   Expression*
1824   do_copy()
1825   {
1826     return Expression::make_interface_field_reference(this->expr_->copy(),
1827                                                       this->name_,
1828                                                       this->location());
1829   }
1830
1831   tree
1832   do_get_tree(Translate_context*);
1833
1834   void
1835   do_dump_expression(Ast_dump_context*) const;
1836
1837  private:
1838   // The expression for the interface object.  This should have a type
1839   // of interface or pointer to interface.
1840   Expression* expr_;
1841   // The field we are retrieving--the name of the method.
1842   std::string name_;
1843 };
1844
1845 // A type guard expression.
1846
1847 class Type_guard_expression : public Expression
1848 {
1849  public:
1850   Type_guard_expression(Expression* expr, Type* type, source_location location)
1851     : Expression(EXPRESSION_TYPE_GUARD, location),
1852       expr_(expr), type_(type)
1853   { }
1854
1855   // Return the expression to convert.
1856   Expression*
1857   expr()
1858   { return this->expr_; }
1859
1860   // Return the type to which to convert.
1861   Type*
1862   type()
1863   { return this->type_; }
1864
1865  protected:
1866   int
1867   do_traverse(Traverse* traverse);
1868
1869   Type*
1870   do_type()
1871   { return this->type_; }
1872
1873   void
1874   do_determine_type(const Type_context*)
1875   { this->expr_->determine_type_no_context(); }
1876
1877   void
1878   do_check_types(Gogo*);
1879
1880   Expression*
1881   do_copy()
1882   {
1883     return new Type_guard_expression(this->expr_->copy(), this->type_,
1884                                      this->location());
1885   }
1886
1887   tree
1888   do_get_tree(Translate_context*);
1889
1890   void
1891   do_dump_expression(Ast_dump_context*) const;
1892
1893  private:
1894   // The expression to convert.
1895   Expression* expr_;
1896   // The type to which to convert.
1897   Type* type_;
1898 };
1899
1900 // A receive expression.
1901
1902 class Receive_expression : public Expression
1903 {
1904  public:
1905   Receive_expression(Expression* channel, source_location location)
1906     : Expression(EXPRESSION_RECEIVE, location),
1907       channel_(channel), for_select_(false)
1908   { }
1909
1910   // Return the channel.
1911   Expression*
1912   channel()
1913   { return this->channel_; }
1914
1915   // Note that this is for a select statement.
1916   void
1917   set_for_select()
1918   { this->for_select_ = true; }
1919
1920  protected:
1921   int
1922   do_traverse(Traverse* traverse)
1923   { return Expression::traverse(&this->channel_, traverse); }
1924
1925   void
1926   do_discarding_value()
1927   { }
1928
1929   Type*
1930   do_type();
1931
1932   void
1933   do_determine_type(const Type_context*)
1934   { this->channel_->determine_type_no_context(); }
1935
1936   void
1937   do_check_types(Gogo*);
1938
1939   Expression*
1940   do_copy()
1941   {
1942     return Expression::make_receive(this->channel_->copy(), this->location());
1943   }
1944
1945   bool
1946   do_must_eval_in_order() const
1947   { return true; }
1948
1949   tree
1950   do_get_tree(Translate_context*);
1951
1952   void
1953   do_dump_expression(Ast_dump_context*) const;
1954
1955  private:
1956   // The channel from which we are receiving.
1957   Expression* channel_;
1958   // Whether this is for a select statement.
1959   bool for_select_;
1960 };
1961
1962 #endif // !defined(GO_EXPRESSIONS_H)