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