OSDN Git Service

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