OSDN Git Service

Revert delta 190174
[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 Integer_type;
22 class Float_type;
23 class Complex_type;
24 class Function_type;
25 class Map_type;
26 class Struct_type;
27 class Struct_field;
28 class Expression_list;
29 class Var_expression;
30 class Temporary_reference_expression;
31 class Set_and_use_temporary_expression;
32 class String_expression;
33 class Binary_expression;
34 class Call_expression;
35 class Func_expression;
36 class Unknown_expression;
37 class Index_expression;
38 class Map_index_expression;
39 class Bound_method_expression;
40 class Field_reference_expression;
41 class Interface_field_reference_expression;
42 class Type_guard_expression;
43 class Receive_expression;
44 class Numeric_constant;
45 class Named_object;
46 class Export;
47 class Import;
48 class Temporary_statement;
49 class Label;
50 class Ast_dump_context;
51 class String_dump;
52
53 // The base class for all expressions.
54
55 class Expression
56 {
57  public:
58   // The types of expressions.
59   enum Expression_classification
60   {
61     EXPRESSION_ERROR,
62     EXPRESSION_TYPE,
63     EXPRESSION_UNARY,
64     EXPRESSION_BINARY,
65     EXPRESSION_CONST_REFERENCE,
66     EXPRESSION_VAR_REFERENCE,
67     EXPRESSION_TEMPORARY_REFERENCE,
68     EXPRESSION_SET_AND_USE_TEMPORARY,
69     EXPRESSION_SINK,
70     EXPRESSION_FUNC_REFERENCE,
71     EXPRESSION_UNKNOWN_REFERENCE,
72     EXPRESSION_BOOLEAN,
73     EXPRESSION_STRING,
74     EXPRESSION_INTEGER,
75     EXPRESSION_FLOAT,
76     EXPRESSION_COMPLEX,
77     EXPRESSION_NIL,
78     EXPRESSION_IOTA,
79     EXPRESSION_CALL,
80     EXPRESSION_CALL_RESULT,
81     EXPRESSION_BOUND_METHOD,
82     EXPRESSION_INDEX,
83     EXPRESSION_ARRAY_INDEX,
84     EXPRESSION_STRING_INDEX,
85     EXPRESSION_MAP_INDEX,
86     EXPRESSION_SELECTOR,
87     EXPRESSION_FIELD_REFERENCE,
88     EXPRESSION_INTERFACE_FIELD_REFERENCE,
89     EXPRESSION_ALLOCATION,
90     EXPRESSION_TYPE_GUARD,
91     EXPRESSION_CONVERSION,
92     EXPRESSION_UNSAFE_CONVERSION,
93     EXPRESSION_STRUCT_CONSTRUCTION,
94     EXPRESSION_FIXED_ARRAY_CONSTRUCTION,
95     EXPRESSION_OPEN_ARRAY_CONSTRUCTION,
96     EXPRESSION_MAP_CONSTRUCTION,
97     EXPRESSION_COMPOSITE_LITERAL,
98     EXPRESSION_HEAP_COMPOSITE,
99     EXPRESSION_RECEIVE,
100     EXPRESSION_TYPE_DESCRIPTOR,
101     EXPRESSION_TYPE_INFO,
102     EXPRESSION_STRUCT_FIELD_OFFSET,
103     EXPRESSION_MAP_DESCRIPTOR,
104     EXPRESSION_LABEL_ADDR
105   };
106
107   Expression(Expression_classification, Location);
108
109   virtual ~Expression();
110
111   // Make an error expression.  This is used when a parse error occurs
112   // to prevent cascading errors.
113   static Expression*
114   make_error(Location);
115
116   // Make an expression which is really a type.  This is used during
117   // parsing.
118   static Expression*
119   make_type(Type*, Location);
120
121   // Make a unary expression.
122   static Expression*
123   make_unary(Operator, Expression*, Location);
124
125   // Make a binary expression.
126   static Expression*
127   make_binary(Operator, Expression*, Expression*, Location);
128
129   // Make a reference to a constant in an expression.
130   static Expression*
131   make_const_reference(Named_object*, Location);
132
133   // Make a reference to a variable in an expression.
134   static Expression*
135   make_var_reference(Named_object*, Location);
136
137   // Make a reference to a temporary variable.  Temporary variables
138   // are always created by a single statement, which is what we use to
139   // refer to them.
140   static Temporary_reference_expression*
141   make_temporary_reference(Temporary_statement*, Location);
142
143   // Make an expressions which sets a temporary variable and then
144   // evaluates to a reference to that temporary variable.  This is
145   // used to set a temporary variable while retaining the order of
146   // evaluation.
147   static Set_and_use_temporary_expression*
148   make_set_and_use_temporary(Temporary_statement*, Expression*, Location);
149
150   // Make a sink expression--a reference to the blank identifier _.
151   static Expression*
152   make_sink(Location);
153
154   // Make a reference to a function in an expression.
155   static Expression*
156   make_func_reference(Named_object*, Expression* closure, Location);
157
158   // Make a reference to an unknown name.  In a correct program this
159   // will always be lowered to a real const/var/func reference.
160   static Unknown_expression*
161   make_unknown_reference(Named_object*, Location);
162
163   // Make a constant bool expression.
164   static Expression*
165   make_boolean(bool val, Location);
166
167   // Make a constant string expression.
168   static Expression*
169   make_string(const std::string&, Location);
170
171   // Make a character constant expression.  TYPE should be NULL for an
172   // abstract type.
173   static Expression*
174   make_character(const mpz_t*, Type*, Location);
175
176   // Make a constant integer expression.  TYPE should be NULL for an
177   // abstract type.
178   static Expression*
179   make_integer(const mpz_t*, Type*, Location);
180
181   // Make a constant float expression.  TYPE should be NULL for an
182   // abstract type.
183   static Expression*
184   make_float(const mpfr_t*, Type*, Location);
185
186   // Make a constant complex expression.  TYPE should be NULL for an
187   // abstract type.
188   static Expression*
189   make_complex(const mpfr_t* real, const mpfr_t* imag, Type*, Location);
190
191   // Make a nil expression.
192   static Expression*
193   make_nil(Location);
194
195   // Make an iota expression.  This is used for the predeclared
196   // constant iota.
197   static Expression*
198   make_iota();
199
200   // Make a call expression.
201   static Call_expression*
202   make_call(Expression* func, Expression_list* args, bool is_varargs,
203             Location);
204
205   // Make a reference to a specific result of a call expression which
206   // returns a tuple.
207   static Expression*
208   make_call_result(Call_expression*, unsigned int index);
209
210   // Make an expression which is a method bound to its first
211   // parameter.
212   static Bound_method_expression*
213   make_bound_method(Expression* object, Named_object* method, Location);
214
215   // Make an index or slice expression.  This is a parser expression
216   // which represents LEFT[START:END].  END may be NULL, meaning an
217   // index rather than a slice.  At parse time we may not know the
218   // type of LEFT.  After parsing this is lowered to an array index, a
219   // string index, or a map index.
220   static Expression*
221   make_index(Expression* left, Expression* start, Expression* end,
222              Location);
223
224   // Make an array index expression.  END may be NULL, in which case
225   // this is an lvalue.
226   static Expression*
227   make_array_index(Expression* array, Expression* start, Expression* end,
228                    Location);
229
230   // Make a string index expression.  END may be NULL.  This is never
231   // an lvalue.
232   static Expression*
233   make_string_index(Expression* string, Expression* start, Expression* end,
234                     Location);
235
236   // Make a map index expression.  This is an lvalue.
237   static Map_index_expression*
238   make_map_index(Expression* map, Expression* val, Location);
239
240   // Make a selector.  This is a parser expression which represents
241   // LEFT.NAME.  At parse time we may not know the type of the left
242   // hand side.
243   static Expression*
244   make_selector(Expression* left, const std::string& name, Location);
245
246   // Make a reference to a field in a struct.
247   static Field_reference_expression*
248   make_field_reference(Expression*, unsigned int field_index, Location);
249
250   // Make a reference to a field of an interface, with an associated
251   // object.
252   static Expression*
253   make_interface_field_reference(Expression*, const std::string&,
254                                  Location);
255
256   // Make an allocation expression.
257   static Expression*
258   make_allocation(Type*, Location);
259
260   // Make a type guard expression.
261   static Expression*
262   make_type_guard(Expression*, Type*, Location);
263
264   // Make a type cast expression.
265   static Expression*
266   make_cast(Type*, Expression*, Location);
267
268   // Make an unsafe type cast expression.  This is only used when
269   // passing parameter to builtin functions that are part of the Go
270   // runtime.
271   static Expression*
272   make_unsafe_cast(Type*, Expression*, Location);
273
274   // Make a composite literal.  The DEPTH parameter is how far down we
275   // are in a list of composite literals with omitted types.
276   static Expression*
277   make_composite_literal(Type*, int depth, bool has_keys, Expression_list*,
278                          Location);
279
280   // Make a struct composite literal.
281   static Expression*
282   make_struct_composite_literal(Type*, Expression_list*, Location);
283
284   // Make a slice composite literal.
285   static Expression*
286   make_slice_composite_literal(Type*, Expression_list*, Location);
287
288   // Take a composite literal and allocate it on the heap.
289   static Expression*
290   make_heap_composite(Expression*, Location);
291
292   // Make a receive expression.  VAL is NULL for a unary receive.
293   static Receive_expression*
294   make_receive(Expression* channel, Location);
295
296   // Make an expression which evaluates to the address of the type
297   // descriptor for TYPE.
298   static Expression*
299   make_type_descriptor(Type* type, Location);
300
301   // Make an expression which evaluates to some characteristic of a
302   // type.  These are only used for type descriptors, so there is no
303   // location parameter.
304   enum Type_info
305     {
306       // The size of a value of the type.
307       TYPE_INFO_SIZE,
308       // The required alignment of a value of the type.
309       TYPE_INFO_ALIGNMENT,
310       // The required alignment of a value of the type when used as a
311       // field in a struct.
312       TYPE_INFO_FIELD_ALIGNMENT
313     };
314
315   static Expression*
316   make_type_info(Type* type, Type_info);
317
318   // Make an expression which evaluates to the offset of a field in a
319   // struct.  This is only used for type descriptors, so there is no
320   // location parameter.
321   static Expression*
322   make_struct_field_offset(Struct_type*, const Struct_field*);
323
324   // Make an expression which evaluates to the address of the map
325   // descriptor for TYPE.
326   static Expression*
327   make_map_descriptor(Map_type* type, Location);
328
329   // Make an expression which evaluates to the address of an unnamed
330   // label.
331   static Expression*
332   make_label_addr(Label*, Location);
333
334   // Return the expression classification.
335   Expression_classification
336   classification() const
337   { return this->classification_; }
338
339   // Return the location of the expression.
340   Location
341   location() const
342   { return this->location_; }
343
344   // Return whether this is a constant expression.
345   bool
346   is_constant() const
347   { return this->do_is_constant(); }
348
349   // If this is not a numeric constant, return false.  If it is one,
350   // return true, and set VAL to hold the value.
351   bool
352   numeric_constant_value(Numeric_constant* val) const
353   { return this->do_numeric_constant_value(val); }
354
355   // If this is not a constant expression with string type, return
356   // false.  If it is one, return true, and set VAL to the value.
357   bool
358   string_constant_value(std::string* val) const
359   { return this->do_string_constant_value(val); }
360
361   // This is called if the value of this expression is being
362   // discarded.  This issues warnings about computed values being
363   // unused.
364   void
365   discarding_value()
366   { 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*, Operator op, Type* left_type,
627                   tree left_tree, Type* right_type, tree right_tree,
628                   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 void
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   // Indicate that this reference appears on the left hand side of an
987   // assignment statement.
988   void
989   set_is_lvalue()
990   { this->is_lvalue_ = true; }
991
992  protected:
993   Type*
994   do_type();
995
996   void
997   do_determine_type(const Type_context*)
998   { }
999
1000   Expression*
1001   do_copy()
1002   { return make_temporary_reference(this->statement_, this->location()); }
1003
1004   bool
1005   do_is_addressable() const
1006   { return true; }
1007
1008   void
1009   do_address_taken(bool);
1010
1011   tree
1012   do_get_tree(Translate_context*);
1013
1014   void
1015   do_dump_expression(Ast_dump_context*) const;
1016
1017  private:
1018   // The statement where the temporary variable is defined.
1019   Temporary_statement* statement_;
1020   // Whether this reference appears on the left hand side of an
1021   // assignment statement.
1022   bool is_lvalue_;
1023 };
1024
1025 // Set and use a temporary variable.
1026
1027 class Set_and_use_temporary_expression : public Expression
1028 {
1029  public:
1030   Set_and_use_temporary_expression(Temporary_statement* statement,
1031                                    Expression* expr, Location location)
1032     : Expression(EXPRESSION_SET_AND_USE_TEMPORARY, location),
1033       statement_(statement), expr_(expr)
1034   { }
1035
1036   // Return the temporary.
1037   Temporary_statement*
1038   temporary() const
1039   { return this->statement_; }
1040
1041   // Return the expression.
1042   Expression*
1043   expression() const
1044   { return this->expr_; }
1045
1046  protected:
1047   int
1048   do_traverse(Traverse* traverse)
1049   { return Expression::traverse(&this->expr_, traverse); }
1050
1051   Type*
1052   do_type();
1053
1054   void
1055   do_determine_type(const Type_context*)
1056   { }
1057
1058   Expression*
1059   do_copy()
1060   {
1061     return make_set_and_use_temporary(this->statement_, this->expr_,
1062                                       this->location());
1063   }
1064
1065   bool
1066   do_is_addressable() const
1067   { return true; }
1068
1069   void
1070   do_address_taken(bool);
1071
1072   tree
1073   do_get_tree(Translate_context*);
1074
1075   void
1076   do_dump_expression(Ast_dump_context*) const;
1077
1078  private:
1079   // The statement where the temporary variable is defined.
1080   Temporary_statement* statement_;
1081   // The expression to assign to the temporary.
1082   Expression* expr_;
1083 };
1084
1085 // A string expression.
1086
1087 class String_expression : public Expression
1088 {
1089  public:
1090   String_expression(const std::string& val, Location location)
1091     : Expression(EXPRESSION_STRING, location),
1092       val_(val), type_(NULL)
1093   { }
1094
1095   const std::string&
1096   val() const
1097   { return this->val_; }
1098
1099   static Expression*
1100   do_import(Import*);
1101
1102  protected:
1103   bool
1104   do_is_constant() const
1105   { return true; }
1106
1107   bool
1108   do_string_constant_value(std::string* val) const
1109   {
1110     *val = this->val_;
1111     return true;
1112   }
1113
1114   Type*
1115   do_type();
1116
1117   void
1118   do_determine_type(const Type_context*);
1119
1120   Expression*
1121   do_copy()
1122   { return this; }
1123
1124   tree
1125   do_get_tree(Translate_context*);
1126
1127   // Write string literal to a string dump.
1128   static void
1129   export_string(String_dump* exp, const String_expression* str);
1130
1131   void
1132   do_export(Export*) const;
1133
1134   void
1135   do_dump_expression(Ast_dump_context*) const;
1136
1137  private:
1138   // The string value.  This is immutable.
1139   const std::string val_;
1140   // The type as determined by context.
1141   Type* type_;
1142 };
1143
1144 // A binary expression.
1145
1146 class Binary_expression : public Expression
1147 {
1148  public:
1149   Binary_expression(Operator op, Expression* left, Expression* right,
1150                     Location location)
1151     : Expression(EXPRESSION_BINARY, location),
1152       op_(op), left_(left), right_(right)
1153   { }
1154
1155   // Return the operator.
1156   Operator
1157   op()
1158   { return this->op_; }
1159
1160   // Return the left hand expression.
1161   Expression*
1162   left()
1163   { return this->left_; }
1164
1165   // Return the right hand expression.
1166   Expression*
1167   right()
1168   { return this->right_; }
1169
1170   // Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC.
1171   // Return true if this could be done, false if not.  Issue errors at
1172   // LOCATION as appropriate.
1173   static bool
1174   eval_constant(Operator op, Numeric_constant* left_nc,
1175                 Numeric_constant* right_nc, Location location,
1176                 Numeric_constant* nc);
1177
1178   // Compare constants LEFT_NC and RIGHT_NC according to OP, setting
1179   // *RESULT.  Return true if this could be done, false if not.  Issue
1180   // errors at LOCATION as appropriate.
1181   static bool
1182   compare_constant(Operator op, Numeric_constant* left_nc,
1183                    Numeric_constant* right_nc, Location location,
1184                    bool* result);
1185
1186   static Expression*
1187   do_import(Import*);
1188
1189   // Report an error if OP can not be applied to TYPE.  Return whether
1190   // it can.  OTYPE is the type of the other operand.
1191   static bool
1192   check_operator_type(Operator op, Type* type, Type* otype, Location);
1193
1194  protected:
1195   int
1196   do_traverse(Traverse* traverse);
1197
1198   Expression*
1199   do_lower(Gogo*, Named_object*, Statement_inserter*, int);
1200
1201   bool
1202   do_is_constant() const
1203   { return this->left_->is_constant() && this->right_->is_constant(); }
1204
1205   bool
1206   do_numeric_constant_value(Numeric_constant*) const;
1207
1208   void
1209   do_discarding_value();
1210
1211   Type*
1212   do_type();
1213
1214   void
1215   do_determine_type(const Type_context*);
1216
1217   void
1218   do_check_types(Gogo*);
1219
1220   Expression*
1221   do_copy()
1222   {
1223     return Expression::make_binary(this->op_, this->left_->copy(),
1224                                    this->right_->copy(), this->location());
1225   }
1226
1227   tree
1228   do_get_tree(Translate_context*);
1229
1230   void
1231   do_export(Export*) const;
1232
1233   void
1234   do_dump_expression(Ast_dump_context*) const;
1235
1236  private:
1237   static bool
1238   operation_type(Operator op, Type* left_type, Type* right_type,
1239                  Type** result_type);
1240
1241   static bool
1242   cmp_to_bool(Operator op, int cmp);
1243
1244   static bool
1245   eval_integer(Operator op, const Numeric_constant*, const Numeric_constant*,
1246                Location, Numeric_constant*);
1247
1248   static bool
1249   eval_float(Operator op, const Numeric_constant*, const Numeric_constant*,
1250              Location, Numeric_constant*);
1251
1252   static bool
1253   eval_complex(Operator op, const Numeric_constant*, const Numeric_constant*,
1254                Location, Numeric_constant*);
1255
1256   static bool
1257   compare_integer(const Numeric_constant*, const Numeric_constant*, int*);
1258
1259   static bool
1260   compare_float(const Numeric_constant*, const Numeric_constant *, int*);
1261
1262   static bool
1263   compare_complex(const Numeric_constant*, const Numeric_constant*, int*);
1264
1265   Expression*
1266   lower_struct_comparison(Gogo*, Statement_inserter*);
1267
1268   Expression*
1269   lower_array_comparison(Gogo*, Statement_inserter*);
1270
1271   Expression*
1272   lower_compare_to_memcmp(Gogo*, Statement_inserter*);
1273
1274   Expression*
1275   operand_address(Statement_inserter*, Expression*);
1276
1277   // The binary operator to apply.
1278   Operator op_;
1279   // The left hand side operand.
1280   Expression* left_;
1281   // The right hand side operand.
1282   Expression* right_;
1283 };
1284
1285 // A call expression.  The go statement needs to dig inside this.
1286
1287 class Call_expression : public Expression
1288 {
1289  public:
1290   Call_expression(Expression* fn, Expression_list* args, bool is_varargs,
1291                   Location location)
1292     : Expression(EXPRESSION_CALL, location),
1293       fn_(fn), args_(args), type_(NULL), results_(NULL), tree_(NULL),
1294       is_varargs_(is_varargs), are_hidden_fields_ok_(false),
1295       varargs_are_lowered_(false), types_are_determined_(false),
1296       is_deferred_(false), issued_error_(false)
1297   { }
1298
1299   // The function to call.
1300   Expression*
1301   fn() const
1302   { return this->fn_; }
1303
1304   // The arguments.
1305   Expression_list*
1306   args()
1307   { return this->args_; }
1308
1309   const Expression_list*
1310   args() const
1311   { return this->args_; }
1312
1313   // Get the function type.
1314   Function_type*
1315   get_function_type() const;
1316
1317   // Return the number of values this call will return.
1318   size_t
1319   result_count() const;
1320
1321   // Return the temporary variable which holds result I.  This is only
1322   // valid after the expression has been lowered, and is only valid
1323   // for calls which return multiple results.
1324   Temporary_statement*
1325   result(size_t i) const;
1326
1327   // Return whether this is a call to the predeclared function
1328   // recover.
1329   bool
1330   is_recover_call() const;
1331
1332   // Set the argument for a call to recover.
1333   void
1334   set_recover_arg(Expression*);
1335
1336   // Whether the last argument is a varargs argument (f(a...)).
1337   bool
1338   is_varargs() const
1339   { return this->is_varargs_; }
1340
1341   // Note that varargs have already been lowered.
1342   void
1343   set_varargs_are_lowered()
1344   { this->varargs_are_lowered_ = true; }
1345
1346   // Note that it is OK for this call to set hidden fields when
1347   // passing arguments.
1348   void
1349   set_hidden_fields_are_ok()
1350   { this->are_hidden_fields_ok_ = true; }
1351
1352   // Whether this call is being deferred.
1353   bool
1354   is_deferred() const
1355   { return this->is_deferred_; }
1356
1357   // Note that the call is being deferred.
1358   void
1359   set_is_deferred()
1360   { this->is_deferred_ = true; }
1361
1362   // We have found an error with this call expression; return true if
1363   // we should report it.
1364   bool
1365   issue_error();
1366
1367  protected:
1368   int
1369   do_traverse(Traverse*);
1370
1371   virtual Expression*
1372   do_lower(Gogo*, Named_object*, Statement_inserter*, int);
1373
1374   void
1375   do_discarding_value()
1376   { }
1377
1378   virtual Type*
1379   do_type();
1380
1381   virtual void
1382   do_determine_type(const Type_context*);
1383
1384   virtual void
1385   do_check_types(Gogo*);
1386
1387   Expression*
1388   do_copy()
1389   {
1390     return Expression::make_call(this->fn_->copy(),
1391                                  (this->args_ == NULL
1392                                   ? NULL
1393                                   : this->args_->copy()),
1394                                  this->is_varargs_, this->location());
1395   }
1396
1397   bool
1398   do_must_eval_in_order() const;
1399
1400   virtual tree
1401   do_get_tree(Translate_context*);
1402
1403   virtual bool
1404   do_is_recover_call() const;
1405
1406   virtual void
1407   do_set_recover_arg(Expression*);
1408
1409   // Let a builtin expression change the argument list.
1410   void
1411   set_args(Expression_list* args)
1412   { this->args_ = args; }
1413
1414   // Let a builtin expression lower varargs.
1415   void
1416   lower_varargs(Gogo*, Named_object* function, Statement_inserter* inserter,
1417                 Type* varargs_type, size_t param_count);
1418
1419   // Let a builtin expression check whether types have been
1420   // determined.
1421   bool
1422   determining_types();
1423
1424   void
1425   do_dump_expression(Ast_dump_context*) const;
1426
1427  private:
1428   bool
1429   check_argument_type(int, const Type*, const Type*, Location, bool);
1430
1431   tree
1432   interface_method_function(Translate_context*,
1433                             Interface_field_reference_expression*,
1434                             tree*);
1435
1436   tree
1437   set_results(Translate_context*, tree);
1438
1439   // The function to call.
1440   Expression* fn_;
1441   // The arguments to pass.  This may be NULL if there are no
1442   // arguments.
1443   Expression_list* args_;
1444   // The type of the expression, to avoid recomputing it.
1445   Type* type_;
1446   // The list of temporaries which will hold the results if the
1447   // function returns a tuple.
1448   std::vector<Temporary_statement*>* results_;
1449   // The tree for the call, used for a call which returns a tuple.
1450   tree tree_;
1451   // True if the last argument is a varargs argument (f(a...)).
1452   bool is_varargs_;
1453   // True if this statement may pass hidden fields in the arguments.
1454   // This is used for generated method stubs.
1455   bool are_hidden_fields_ok_;
1456   // True if varargs have already been lowered.
1457   bool varargs_are_lowered_;
1458   // True if types have been determined.
1459   bool types_are_determined_;
1460   // True if the call is an argument to a defer statement.
1461   bool is_deferred_;
1462   // True if we reported an error about a mismatch between call
1463   // results and uses.  This is to avoid producing multiple errors
1464   // when there are multiple Call_result_expressions.
1465   bool issued_error_;
1466 };
1467
1468 // An expression which represents a pointer to a function.
1469
1470 class Func_expression : public Expression
1471 {
1472  public:
1473   Func_expression(Named_object* function, Expression* closure,
1474                   Location location)
1475     : Expression(EXPRESSION_FUNC_REFERENCE, location),
1476       function_(function), closure_(closure)
1477   { }
1478
1479   // Return the object associated with the function.
1480   const Named_object*
1481   named_object() const
1482   { return this->function_; }
1483
1484   // Return the closure for this function.  This will return NULL if
1485   // the function has no closure, which is the normal case.
1486   Expression*
1487   closure()
1488   { return this->closure_; }
1489
1490   // Return a tree for this function without evaluating the closure.
1491   tree
1492   get_tree_without_closure(Gogo*);
1493
1494  protected:
1495   int
1496   do_traverse(Traverse*);
1497
1498   Type*
1499   do_type();
1500
1501   void
1502   do_determine_type(const Type_context*)
1503   {
1504     if (this->closure_ != NULL)
1505       this->closure_->determine_type_no_context();
1506   }
1507
1508   Expression*
1509   do_copy()
1510   {
1511     return Expression::make_func_reference(this->function_,
1512                                            (this->closure_ == NULL
1513                                             ? NULL
1514                                             : this->closure_->copy()),
1515                                            this->location());
1516   }
1517
1518   tree
1519   do_get_tree(Translate_context*);
1520
1521   void
1522   do_dump_expression(Ast_dump_context*) const;
1523
1524  private:
1525   // The function itself.
1526   Named_object* function_;
1527   // A closure.  This is normally NULL.  For a nested function, it may
1528   // be a heap-allocated struct holding pointers to all the variables
1529   // referenced by this function and defined in enclosing functions.
1530   Expression* closure_;
1531 };
1532
1533 // A reference to an unknown name.
1534
1535 class Unknown_expression : public Parser_expression
1536 {
1537  public:
1538   Unknown_expression(Named_object* named_object, Location location)
1539     : Parser_expression(EXPRESSION_UNKNOWN_REFERENCE, location),
1540       named_object_(named_object), no_error_message_(false),
1541       is_composite_literal_key_(false)
1542   { }
1543
1544   // The associated named object.
1545   Named_object*
1546   named_object() const
1547   { return this->named_object_; }
1548
1549   // The name of the identifier which was unknown.
1550   const std::string&
1551   name() const;
1552
1553   // Call this to indicate that we should not give an error if this
1554   // name is never defined.  This is used to avoid knock-on errors
1555   // during an erroneous parse.
1556   void
1557   set_no_error_message()
1558   { this->no_error_message_ = true; }
1559
1560   // Note that this expression is being used as the key in a composite
1561   // literal, so it may be OK if it is not resolved.
1562   void
1563   set_is_composite_literal_key()
1564   { this->is_composite_literal_key_ = true; }
1565
1566   // Note that this expression should no longer be treated as a
1567   // composite literal key.
1568   void
1569   clear_is_composite_literal_key()
1570   { this->is_composite_literal_key_ = false; }
1571
1572  protected:
1573   Expression*
1574   do_lower(Gogo*, Named_object*, Statement_inserter*, int);
1575
1576   Expression*
1577   do_copy()
1578   { return new Unknown_expression(this->named_object_, this->location()); }
1579
1580   void
1581   do_dump_expression(Ast_dump_context*) const;
1582   
1583  private:
1584   // The unknown name.
1585   Named_object* named_object_;
1586   // True if we should not give errors if this is undefined.  This is
1587   // used if there was a parse failure.
1588   bool no_error_message_;
1589   // True if this is the key in a composite literal.
1590   bool is_composite_literal_key_;
1591 };
1592
1593 // An index expression.  This is lowered to an array index, a string
1594 // index, or a map index.
1595
1596 class Index_expression : public Parser_expression
1597 {
1598  public:
1599   Index_expression(Expression* left, Expression* start, Expression* end,
1600                    Location location)
1601     : Parser_expression(EXPRESSION_INDEX, location),
1602       left_(left), start_(start), end_(end), is_lvalue_(false)
1603   { }
1604
1605   // Record that this expression is an lvalue.
1606   void
1607   set_is_lvalue()
1608   { this->is_lvalue_ = true; }
1609
1610   // Dump an index expression, i.e. an expression of the form
1611   // expr[expr] or expr[expr:expr], to a dump context.
1612   static void
1613   dump_index_expression(Ast_dump_context*, const Expression* expr, 
1614                         const Expression* start, const Expression* end);
1615
1616  protected:
1617   int
1618   do_traverse(Traverse*);
1619
1620   Expression*
1621   do_lower(Gogo*, Named_object*, Statement_inserter*, int);
1622
1623   Expression*
1624   do_copy()
1625   {
1626     return new Index_expression(this->left_->copy(), this->start_->copy(),
1627                                 (this->end_ == NULL
1628                                  ? NULL
1629                                  : this->end_->copy()),
1630                                 this->location());
1631   }
1632
1633   bool
1634   do_must_eval_subexpressions_in_order(int* skip) const
1635   {
1636     *skip = 1;
1637     return true;
1638   }
1639
1640   void
1641   do_dump_expression(Ast_dump_context*) const;
1642
1643  private:
1644   // The expression being indexed.
1645   Expression* left_;
1646   // The first index.
1647   Expression* start_;
1648   // The second index.  This is NULL for an index, non-NULL for a
1649   // slice.
1650   Expression* end_;
1651   // Whether this is being used as an l-value.  We set this during the
1652   // parse because map index expressions need to know.
1653   bool is_lvalue_;
1654 };
1655
1656 // An index into a map.
1657
1658 class Map_index_expression : public Expression
1659 {
1660  public:
1661   Map_index_expression(Expression* map, Expression* index,
1662                        Location location)
1663     : Expression(EXPRESSION_MAP_INDEX, location),
1664       map_(map), index_(index), is_lvalue_(false),
1665       is_in_tuple_assignment_(false)
1666   { }
1667
1668   // Return the map.
1669   Expression*
1670   map()
1671   { return this->map_; }
1672
1673   const Expression*
1674   map() const
1675   { return this->map_; }
1676
1677   // Return the index.
1678   Expression*
1679   index()
1680   { return this->index_; }
1681
1682   const Expression*
1683   index() const
1684   { return this->index_; }
1685
1686   // Get the type of the map being indexed.
1687   Map_type*
1688   get_map_type() const;
1689
1690   // Record that this map expression is an lvalue.  The difference is
1691   // that an lvalue always inserts the key.
1692   void
1693   set_is_lvalue()
1694   { this->is_lvalue_ = true; }
1695
1696   // Return whether this map expression occurs in an assignment to a
1697   // pair of values.
1698   bool
1699   is_in_tuple_assignment() const
1700   { return this->is_in_tuple_assignment_; }
1701
1702   // Record that this map expression occurs in an assignment to a pair
1703   // of values.
1704   void
1705   set_is_in_tuple_assignment()
1706   { this->is_in_tuple_assignment_ = true; }
1707
1708   // Return a tree for the map index.  This returns a tree which
1709   // evaluates to a pointer to a value in the map.  If INSERT is true,
1710   // the key will be inserted if not present, and the value pointer
1711   // will be zero initialized.  If INSERT is false, and the key is not
1712   // present in the map, the pointer will be NULL.
1713   tree
1714   get_value_pointer(Translate_context*, bool insert);
1715
1716  protected:
1717   int
1718   do_traverse(Traverse*);
1719
1720   Type*
1721   do_type();
1722
1723   void
1724   do_determine_type(const Type_context*);
1725
1726   void
1727   do_check_types(Gogo*);
1728
1729   Expression*
1730   do_copy()
1731   {
1732     return Expression::make_map_index(this->map_->copy(),
1733                                       this->index_->copy(),
1734                                       this->location());
1735   }
1736
1737   bool
1738   do_must_eval_subexpressions_in_order(int* skip) const
1739   {
1740     *skip = 1;
1741     return true;
1742   }
1743
1744   // A map index expression is an lvalue but it is not addressable.
1745
1746   tree
1747   do_get_tree(Translate_context*);
1748
1749   void
1750   do_dump_expression(Ast_dump_context*) const;
1751
1752  private:
1753   // The map we are looking into.
1754   Expression* map_;
1755   // The index.
1756   Expression* index_;
1757   // Whether this is an lvalue.
1758   bool is_lvalue_;
1759   // Whether this is in a tuple assignment to a pair of values.
1760   bool is_in_tuple_assignment_;
1761 };
1762
1763 // An expression which represents a method bound to its first
1764 // argument.
1765
1766 class Bound_method_expression : public Expression
1767 {
1768  public:
1769   Bound_method_expression(Expression* expr, Named_object* method,
1770                           Location location)
1771     : Expression(EXPRESSION_BOUND_METHOD, location),
1772       expr_(expr), expr_type_(NULL), method_(method)
1773   { }
1774
1775   // Return the object which is the first argument.
1776   Expression*
1777   first_argument()
1778   { return this->expr_; }
1779
1780   // Return the implicit type of the first argument.  This will be
1781   // non-NULL when using a method from an anonymous field without
1782   // using an explicit stub.
1783   Type*
1784   first_argument_type() const
1785   { return this->expr_type_; }
1786
1787   // Return the method function.
1788   Named_object*
1789   method()
1790   { return this->method_; }
1791
1792   // Set the implicit type of the expression.
1793   void
1794   set_first_argument_type(Type* type)
1795   { this->expr_type_ = type; }
1796
1797  protected:
1798   int
1799   do_traverse(Traverse*);
1800
1801   Type*
1802   do_type();
1803
1804   void
1805   do_determine_type(const Type_context*);
1806
1807   void
1808   do_check_types(Gogo*);
1809
1810   Expression*
1811   do_copy()
1812   {
1813     return new Bound_method_expression(this->expr_->copy(), this->method_,
1814                                        this->location());
1815   }
1816
1817   tree
1818   do_get_tree(Translate_context*);
1819
1820   void
1821   do_dump_expression(Ast_dump_context*) const;
1822
1823  private:
1824   // The object used to find the method.  This is passed to the method
1825   // as the first argument.
1826   Expression* expr_;
1827   // The implicit type of the object to pass to the method.  This is
1828   // NULL in the normal case, non-NULL when using a method from an
1829   // anonymous field which does not require a stub.
1830   Type* expr_type_;
1831   // The method itself.
1832   Named_object* method_;
1833 };
1834
1835 // A reference to a field in a struct.
1836
1837 class Field_reference_expression : public Expression
1838 {
1839  public:
1840   Field_reference_expression(Expression* expr, unsigned int field_index,
1841                              Location location)
1842     : Expression(EXPRESSION_FIELD_REFERENCE, location),
1843       expr_(expr), field_index_(field_index)
1844   { }
1845
1846   // Return the struct expression.
1847   Expression*
1848   expr() const
1849   { return this->expr_; }
1850
1851   // Return the field index.
1852   unsigned int
1853   field_index() const
1854   { return this->field_index_; }
1855
1856   // Set the struct expression.  This is used when parsing.
1857   void
1858   set_struct_expression(Expression* expr)
1859   {
1860     go_assert(this->expr_ == NULL);
1861     this->expr_ = expr;
1862   }
1863
1864  protected:
1865   int
1866   do_traverse(Traverse* traverse)
1867   { return Expression::traverse(&this->expr_, traverse); }
1868
1869   Type*
1870   do_type();
1871
1872   void
1873   do_determine_type(const Type_context*)
1874   { this->expr_->determine_type_no_context(); }
1875
1876   void
1877   do_check_types(Gogo*);
1878
1879   Expression*
1880   do_copy()
1881   {
1882     return Expression::make_field_reference(this->expr_->copy(),
1883                                             this->field_index_,
1884                                             this->location());
1885   }
1886
1887   bool
1888   do_is_addressable() const
1889   { return this->expr_->is_addressable(); }
1890
1891   void
1892   do_address_taken(bool escapes)
1893   { this->expr_->address_taken(escapes); }
1894
1895   tree
1896   do_get_tree(Translate_context*);
1897
1898   void
1899   do_dump_expression(Ast_dump_context*) const;
1900
1901  private:
1902   // The expression we are looking into.  This should have a type of
1903   // struct.
1904   Expression* expr_;
1905   // The zero-based index of the field we are retrieving.
1906   unsigned int field_index_;
1907 };
1908
1909 // A reference to a field of an interface.
1910
1911 class Interface_field_reference_expression : public Expression
1912 {
1913  public:
1914   Interface_field_reference_expression(Expression* expr,
1915                                        const std::string& name,
1916                                        Location location)
1917     : Expression(EXPRESSION_INTERFACE_FIELD_REFERENCE, location),
1918       expr_(expr), name_(name)
1919   { }
1920
1921   // Return the expression for the interface object.
1922   Expression*
1923   expr()
1924   { return this->expr_; }
1925
1926   // Return the name of the method to call.
1927   const std::string&
1928   name() const
1929   { return this->name_; }
1930
1931   // Return a tree for the pointer to the function to call, given a
1932   // tree for the expression.
1933   tree
1934   get_function_tree(Translate_context*, tree);
1935
1936   // Return a tree for the first argument to pass to the interface
1937   // function, given a tree for the expression.  This is the real
1938   // object associated with the interface object.
1939   tree
1940   get_underlying_object_tree(Translate_context*, tree);
1941
1942  protected:
1943   int
1944   do_traverse(Traverse* traverse);
1945
1946   Type*
1947   do_type();
1948
1949   void
1950   do_determine_type(const Type_context*);
1951
1952   void
1953   do_check_types(Gogo*);
1954
1955   Expression*
1956   do_copy()
1957   {
1958     return Expression::make_interface_field_reference(this->expr_->copy(),
1959                                                       this->name_,
1960                                                       this->location());
1961   }
1962
1963   tree
1964   do_get_tree(Translate_context*);
1965
1966   void
1967   do_dump_expression(Ast_dump_context*) const;
1968
1969  private:
1970   // The expression for the interface object.  This should have a type
1971   // of interface or pointer to interface.
1972   Expression* expr_;
1973   // The field we are retrieving--the name of the method.
1974   std::string name_;
1975 };
1976
1977 // A type guard expression.
1978
1979 class Type_guard_expression : public Expression
1980 {
1981  public:
1982   Type_guard_expression(Expression* expr, Type* type, Location location)
1983     : Expression(EXPRESSION_TYPE_GUARD, location),
1984       expr_(expr), type_(type)
1985   { }
1986
1987   // Return the expression to convert.
1988   Expression*
1989   expr()
1990   { return this->expr_; }
1991
1992   // Return the type to which to convert.
1993   Type*
1994   type()
1995   { return this->type_; }
1996
1997  protected:
1998   int
1999   do_traverse(Traverse* traverse);
2000
2001   Type*
2002   do_type()
2003   { return this->type_; }
2004
2005   void
2006   do_determine_type(const Type_context*)
2007   { this->expr_->determine_type_no_context(); }
2008
2009   void
2010   do_check_types(Gogo*);
2011
2012   Expression*
2013   do_copy()
2014   {
2015     return new Type_guard_expression(this->expr_->copy(), this->type_,
2016                                      this->location());
2017   }
2018
2019   tree
2020   do_get_tree(Translate_context*);
2021
2022   void
2023   do_dump_expression(Ast_dump_context*) const;
2024
2025  private:
2026   // The expression to convert.
2027   Expression* expr_;
2028   // The type to which to convert.
2029   Type* type_;
2030 };
2031
2032 // A receive expression.
2033
2034 class Receive_expression : public Expression
2035 {
2036  public:
2037   Receive_expression(Expression* channel, Location location)
2038     : Expression(EXPRESSION_RECEIVE, location),
2039       channel_(channel)
2040   { }
2041
2042   // Return the channel.
2043   Expression*
2044   channel()
2045   { return this->channel_; }
2046
2047  protected:
2048   int
2049   do_traverse(Traverse* traverse)
2050   { return Expression::traverse(&this->channel_, traverse); }
2051
2052   void
2053   do_discarding_value()
2054   { }
2055
2056   Type*
2057   do_type();
2058
2059   void
2060   do_determine_type(const Type_context*)
2061   { this->channel_->determine_type_no_context(); }
2062
2063   void
2064   do_check_types(Gogo*);
2065
2066   Expression*
2067   do_copy()
2068   {
2069     return Expression::make_receive(this->channel_->copy(), this->location());
2070   }
2071
2072   bool
2073   do_must_eval_in_order() const
2074   { return true; }
2075
2076   tree
2077   do_get_tree(Translate_context*);
2078
2079   void
2080   do_dump_expression(Ast_dump_context*) const;
2081
2082  private:
2083   // The channel from which we are receiving.
2084   Expression* channel_;
2085 };
2086
2087 // A numeric constant.  This is used both for untyped constants and
2088 // for constants that have a type.
2089
2090 class Numeric_constant
2091 {
2092  public:
2093   Numeric_constant()
2094     : classification_(NC_INVALID), type_(NULL)
2095   { }
2096
2097   ~Numeric_constant();
2098
2099   Numeric_constant(const Numeric_constant&);
2100
2101   Numeric_constant& operator=(const Numeric_constant&);
2102
2103   // Set to an unsigned long value.
2104   void
2105   set_unsigned_long(Type*, unsigned long);
2106
2107   // Set to an integer value.
2108   void
2109   set_int(Type*, const mpz_t);
2110
2111   // Set to a rune value.
2112   void
2113   set_rune(Type*, const mpz_t);
2114
2115   // Set to a floating point value.
2116   void
2117   set_float(Type*, const mpfr_t);
2118
2119   // Set to a complex value.
2120   void
2121   set_complex(Type*, const mpfr_t, const mpfr_t);
2122
2123   // Classifiers.
2124   bool
2125   is_int() const
2126   { return this->classification_ == Numeric_constant::NC_INT; }
2127
2128   bool
2129   is_rune() const
2130   { return this->classification_ == Numeric_constant::NC_RUNE; }
2131
2132   bool
2133   is_float() const
2134   { return this->classification_ == Numeric_constant::NC_FLOAT; }
2135
2136   bool
2137   is_complex() const
2138   { return this->classification_ == Numeric_constant::NC_COMPLEX; }
2139
2140   // Value retrievers.  These will initialize the values as well as
2141   // set them.  GET_INT is only valid if IS_INT returns true, and
2142   // likewise respectively.
2143   void
2144   get_int(mpz_t*) const;
2145
2146   void
2147   get_rune(mpz_t*) const;
2148
2149   void
2150   get_float(mpfr_t*) const;
2151
2152   void
2153   get_complex(mpfr_t*, mpfr_t*) const;
2154
2155   // Codes returned by to_unsigned_long.
2156   enum To_unsigned_long
2157   {
2158     // Value is integer and fits in unsigned long.
2159     NC_UL_VALID,
2160     // Value is not integer.
2161     NC_UL_NOTINT,
2162     // Value is integer but is negative.
2163     NC_UL_NEGATIVE,
2164     // Value is non-negative integer but does not fit in unsigned
2165     // long.
2166     NC_UL_BIG
2167   };
2168
2169   // If the value can be expressed as an integer that fits in an
2170   // unsigned long, set *VAL and return NC_UL_VALID.  Otherwise return
2171   // one of the other To_unsigned_long codes.
2172   To_unsigned_long
2173   to_unsigned_long(unsigned long* val) const;
2174
2175   // If the value can be expressed as an int, return true and
2176   // initialize and set VAL.  This will return false for a value with
2177   // an explicit float or complex type, even if the value is integral.
2178   bool
2179   to_int(mpz_t* val) const;
2180
2181   // If the value can be expressed as a float, return true and
2182   // initialize and set VAL.
2183   bool
2184   to_float(mpfr_t* val) const;
2185
2186   // If the value can be expressed as a complex, return true and
2187   // initialize and set VR and VI.
2188   bool
2189   to_complex(mpfr_t* vr, mpfr_t* vi) const;
2190
2191   // Get the type.
2192   Type*
2193   type() const;
2194
2195   // If the constant can be expressed in TYPE, then set the type of
2196   // the constant to TYPE and return true.  Otherwise return false,
2197   // and, if ISSUE_ERROR is true, issue an error message.  LOCATION is
2198   // the location to use for the error.
2199   bool
2200   set_type(Type* type, bool issue_error, Location location);
2201
2202   // Return an Expression for this value.
2203   Expression*
2204   expression(Location) const;
2205
2206  private:
2207   void
2208   clear();
2209
2210   To_unsigned_long
2211   mpz_to_unsigned_long(const mpz_t ival, unsigned long *val) const;
2212
2213   To_unsigned_long
2214   mpfr_to_unsigned_long(const mpfr_t fval, unsigned long *val) const;
2215
2216   bool
2217   check_int_type(Integer_type*, bool, Location) const;
2218
2219   bool
2220   check_float_type(Float_type*, bool, Location) const;
2221
2222   bool
2223   check_complex_type(Complex_type*, bool, Location) const;
2224
2225   // The kinds of constants.
2226   enum Classification
2227   {
2228     NC_INVALID,
2229     NC_RUNE,
2230     NC_INT,
2231     NC_FLOAT,
2232     NC_COMPLEX
2233   };
2234
2235   // The kind of constant.
2236   Classification classification_;
2237   // The value.
2238   union
2239   {
2240     // If NC_INT or NC_RUNE.
2241     mpz_t int_val;
2242     // If NC_FLOAT.
2243     mpfr_t float_val;
2244     // If NC_COMPLEX.
2245     struct
2246     {
2247       mpfr_t real;
2248       mpfr_t imag;
2249     } complex_val;
2250   } u_;
2251   // The type if there is one.  This will be NULL for an untyped
2252   // constant.
2253   Type* type_;
2254 };
2255
2256 #endif // !defined(GO_EXPRESSIONS_H)