OSDN Git Service

ab6f4feeaf3da45cddecad3aed17c2e72c90c016
[pf3gnuchains/gcc-fork.git] / gcc / go / gofrontend / expressions.cc
1 // expressions.cc -- Go frontend expression handling.
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 #include "go-system.h"
8
9 #include <gmp.h>
10
11 #ifndef ENABLE_BUILD_WITH_CXX
12 extern "C"
13 {
14 #endif
15
16 #include "toplev.h"
17 #include "intl.h"
18 #include "tree.h"
19 #include "gimple.h"
20 #include "tree-iterator.h"
21 #include "convert.h"
22 #include "real.h"
23 #include "realmpfr.h"
24
25 #ifndef ENABLE_BUILD_WITH_CXX
26 }
27 #endif
28
29 #include "go-c.h"
30 #include "gogo.h"
31 #include "types.h"
32 #include "export.h"
33 #include "import.h"
34 #include "statements.h"
35 #include "lex.h"
36 #include "runtime.h"
37 #include "backend.h"
38 #include "expressions.h"
39 #include "ast-dump.h"
40
41 // Class Expression.
42
43 Expression::Expression(Expression_classification classification,
44                        Location location)
45   : classification_(classification), location_(location)
46 {
47 }
48
49 Expression::~Expression()
50 {
51 }
52
53 // If this expression has a constant integer value, return it.
54
55 bool
56 Expression::integer_constant_value(bool iota_is_constant, mpz_t val,
57                                    Type** ptype) const
58 {
59   *ptype = NULL;
60   return this->do_integer_constant_value(iota_is_constant, val, ptype);
61 }
62
63 // If this expression has a constant floating point value, return it.
64
65 bool
66 Expression::float_constant_value(mpfr_t val, Type** ptype) const
67 {
68   *ptype = NULL;
69   if (this->do_float_constant_value(val, ptype))
70     return true;
71   mpz_t ival;
72   mpz_init(ival);
73   Type* t;
74   bool ret;
75   if (!this->do_integer_constant_value(false, ival, &t))
76     ret = false;
77   else
78     {
79       mpfr_set_z(val, ival, GMP_RNDN);
80       ret = true;
81     }
82   mpz_clear(ival);
83   return ret;
84 }
85
86 // If this expression has a constant complex value, return it.
87
88 bool
89 Expression::complex_constant_value(mpfr_t real, mpfr_t imag,
90                                    Type** ptype) const
91 {
92   *ptype = NULL;
93   if (this->do_complex_constant_value(real, imag, ptype))
94     return true;
95   Type *t;
96   if (this->float_constant_value(real, &t))
97     {
98       mpfr_set_ui(imag, 0, GMP_RNDN);
99       return true;
100     }
101   return false;
102 }
103
104 // Traverse the expressions.
105
106 int
107 Expression::traverse(Expression** pexpr, Traverse* traverse)
108 {
109   Expression* expr = *pexpr;
110   if ((traverse->traverse_mask() & Traverse::traverse_expressions) != 0)
111     {
112       int t = traverse->expression(pexpr);
113       if (t == TRAVERSE_EXIT)
114         return TRAVERSE_EXIT;
115       else if (t == TRAVERSE_SKIP_COMPONENTS)
116         return TRAVERSE_CONTINUE;
117     }
118   return expr->do_traverse(traverse);
119 }
120
121 // Traverse subexpressions of this expression.
122
123 int
124 Expression::traverse_subexpressions(Traverse* traverse)
125 {
126   return this->do_traverse(traverse);
127 }
128
129 // Default implementation for do_traverse for child classes.
130
131 int
132 Expression::do_traverse(Traverse*)
133 {
134   return TRAVERSE_CONTINUE;
135 }
136
137 // This virtual function is called by the parser if the value of this
138 // expression is being discarded.  By default, we give an error.
139 // Expressions with side effects override.
140
141 void
142 Expression::do_discarding_value()
143 {
144   this->unused_value_error();
145 }
146
147 // This virtual function is called to export expressions.  This will
148 // only be used by expressions which may be constant.
149
150 void
151 Expression::do_export(Export*) const
152 {
153   go_unreachable();
154 }
155
156 // Give an error saying that the value of the expression is not used.
157
158 void
159 Expression::unused_value_error()
160 {
161   error_at(this->location(), "value computed is not used");
162 }
163
164 // Note that this expression is an error.  This is called by children
165 // when they discover an error.
166
167 void
168 Expression::set_is_error()
169 {
170   this->classification_ = EXPRESSION_ERROR;
171 }
172
173 // For children to call to report an error conveniently.
174
175 void
176 Expression::report_error(const char* msg)
177 {
178   error_at(this->location_, "%s", msg);
179   this->set_is_error();
180 }
181
182 // Set types of variables and constants.  This is implemented by the
183 // child class.
184
185 void
186 Expression::determine_type(const Type_context* context)
187 {
188   this->do_determine_type(context);
189 }
190
191 // Set types when there is no context.
192
193 void
194 Expression::determine_type_no_context()
195 {
196   Type_context context;
197   this->do_determine_type(&context);
198 }
199
200 // Return a tree handling any conversions which must be done during
201 // assignment.
202
203 tree
204 Expression::convert_for_assignment(Translate_context* context, Type* lhs_type,
205                                    Type* rhs_type, tree rhs_tree,
206                                    Location location)
207 {
208   if (lhs_type == rhs_type)
209     return rhs_tree;
210
211   if (lhs_type->is_error() || rhs_type->is_error())
212     return error_mark_node;
213
214   if (rhs_tree == error_mark_node || TREE_TYPE(rhs_tree) == error_mark_node)
215     return error_mark_node;
216
217   Gogo* gogo = context->gogo();
218
219   tree lhs_type_tree = type_to_tree(lhs_type->get_backend(gogo));
220   if (lhs_type_tree == error_mark_node)
221     return error_mark_node;
222
223   if (lhs_type->interface_type() != NULL)
224     {
225       if (rhs_type->interface_type() == NULL)
226         return Expression::convert_type_to_interface(context, lhs_type,
227                                                      rhs_type, rhs_tree,
228                                                      location);
229       else
230         return Expression::convert_interface_to_interface(context, lhs_type,
231                                                           rhs_type, rhs_tree,
232                                                           false, location);
233     }
234   else if (rhs_type->interface_type() != NULL)
235     return Expression::convert_interface_to_type(context, lhs_type, rhs_type,
236                                                  rhs_tree, location);
237   else if (lhs_type->is_slice_type() && rhs_type->is_nil_type())
238     {
239       // Assigning nil to an open array.
240       go_assert(TREE_CODE(lhs_type_tree) == RECORD_TYPE);
241
242       VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 3);
243
244       constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
245       tree field = TYPE_FIELDS(lhs_type_tree);
246       go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),
247                         "__values") == 0);
248       elt->index = field;
249       elt->value = fold_convert(TREE_TYPE(field), null_pointer_node);
250
251       elt = VEC_quick_push(constructor_elt, init, NULL);
252       field = DECL_CHAIN(field);
253       go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),
254                         "__count") == 0);
255       elt->index = field;
256       elt->value = fold_convert(TREE_TYPE(field), integer_zero_node);
257
258       elt = VEC_quick_push(constructor_elt, init, NULL);
259       field = DECL_CHAIN(field);
260       go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),
261                         "__capacity") == 0);
262       elt->index = field;
263       elt->value = fold_convert(TREE_TYPE(field), integer_zero_node);
264
265       tree val = build_constructor(lhs_type_tree, init);
266       TREE_CONSTANT(val) = 1;
267
268       return val;
269     }
270   else if (rhs_type->is_nil_type())
271     {
272       // The left hand side should be a pointer type at the tree
273       // level.
274       go_assert(POINTER_TYPE_P(lhs_type_tree));
275       return fold_convert(lhs_type_tree, null_pointer_node);
276     }
277   else if (lhs_type_tree == TREE_TYPE(rhs_tree))
278     {
279       // No conversion is needed.
280       return rhs_tree;
281     }
282   else if (POINTER_TYPE_P(lhs_type_tree)
283            || INTEGRAL_TYPE_P(lhs_type_tree)
284            || SCALAR_FLOAT_TYPE_P(lhs_type_tree)
285            || COMPLEX_FLOAT_TYPE_P(lhs_type_tree))
286     return fold_convert_loc(location.gcc_location(), lhs_type_tree, rhs_tree);
287   else if (TREE_CODE(lhs_type_tree) == RECORD_TYPE
288            && TREE_CODE(TREE_TYPE(rhs_tree)) == RECORD_TYPE)
289     {
290       // This conversion must be permitted by Go, or we wouldn't have
291       // gotten here.
292       go_assert(int_size_in_bytes(lhs_type_tree)
293                  == int_size_in_bytes(TREE_TYPE(rhs_tree)));
294       return fold_build1_loc(location.gcc_location(), VIEW_CONVERT_EXPR,
295                              lhs_type_tree, rhs_tree);
296     }
297   else
298     {
299       go_assert(useless_type_conversion_p(lhs_type_tree, TREE_TYPE(rhs_tree)));
300       return rhs_tree;
301     }
302 }
303
304 // Return a tree for a conversion from a non-interface type to an
305 // interface type.
306
307 tree
308 Expression::convert_type_to_interface(Translate_context* context,
309                                       Type* lhs_type, Type* rhs_type,
310                                       tree rhs_tree, Location location)
311 {
312   Gogo* gogo = context->gogo();
313   Interface_type* lhs_interface_type = lhs_type->interface_type();
314   bool lhs_is_empty = lhs_interface_type->is_empty();
315
316   // Since RHS_TYPE is a static type, we can create the interface
317   // method table at compile time.
318
319   // When setting an interface to nil, we just set both fields to
320   // NULL.
321   if (rhs_type->is_nil_type())
322     {
323       Btype* lhs_btype = lhs_type->get_backend(gogo);
324       return expr_to_tree(gogo->backend()->zero_expression(lhs_btype));
325     }
326
327   // This should have been checked already.
328   go_assert(lhs_interface_type->implements_interface(rhs_type, NULL));
329
330   tree lhs_type_tree = type_to_tree(lhs_type->get_backend(gogo));
331   if (lhs_type_tree == error_mark_node)
332     return error_mark_node;
333
334   // An interface is a tuple.  If LHS_TYPE is an empty interface type,
335   // then the first field is the type descriptor for RHS_TYPE.
336   // Otherwise it is the interface method table for RHS_TYPE.
337   tree first_field_value;
338   if (lhs_is_empty)
339     first_field_value = rhs_type->type_descriptor_pointer(gogo, location);
340   else
341     {
342       // Build the interface method table for this interface and this
343       // object type: a list of function pointers for each interface
344       // method.
345       Named_type* rhs_named_type = rhs_type->named_type();
346       bool is_pointer = false;
347       if (rhs_named_type == NULL)
348         {
349           rhs_named_type = rhs_type->deref()->named_type();
350           is_pointer = true;
351         }
352       tree method_table;
353       if (rhs_named_type == NULL)
354         method_table = null_pointer_node;
355       else
356         method_table =
357           rhs_named_type->interface_method_table(gogo, lhs_interface_type,
358                                                  is_pointer);
359       first_field_value = fold_convert_loc(location.gcc_location(),
360                                            const_ptr_type_node, method_table);
361     }
362   if (first_field_value == error_mark_node)
363     return error_mark_node;
364
365   // Start building a constructor for the value we will return.
366
367   VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 2);
368
369   constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
370   tree field = TYPE_FIELDS(lhs_type_tree);
371   go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),
372                     (lhs_is_empty ? "__type_descriptor" : "__methods")) == 0);
373   elt->index = field;
374   elt->value = fold_convert_loc(location.gcc_location(), TREE_TYPE(field),
375                                 first_field_value);
376
377   elt = VEC_quick_push(constructor_elt, init, NULL);
378   field = DECL_CHAIN(field);
379   go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__object") == 0);
380   elt->index = field;
381
382   if (rhs_type->points_to() != NULL)
383     {
384       //  We are assigning a pointer to the interface; the interface
385       // holds the pointer itself.
386       elt->value = rhs_tree;
387       return build_constructor(lhs_type_tree, init);
388     }
389
390   // We are assigning a non-pointer value to the interface; the
391   // interface gets a copy of the value in the heap.
392
393   tree object_size = TYPE_SIZE_UNIT(TREE_TYPE(rhs_tree));
394
395   tree space = gogo->allocate_memory(rhs_type, object_size, location);
396   space = fold_convert_loc(location.gcc_location(),
397                            build_pointer_type(TREE_TYPE(rhs_tree)), space);
398   space = save_expr(space);
399
400   tree ref = build_fold_indirect_ref_loc(location.gcc_location(), space);
401   TREE_THIS_NOTRAP(ref) = 1;
402   tree set = fold_build2_loc(location.gcc_location(), MODIFY_EXPR,
403                              void_type_node, ref, rhs_tree);
404
405   elt->value = fold_convert_loc(location.gcc_location(), TREE_TYPE(field),
406                                 space);
407
408   return build2(COMPOUND_EXPR, lhs_type_tree, set,
409                 build_constructor(lhs_type_tree, init));
410 }
411
412 // Return a tree for the type descriptor of RHS_TREE, which has
413 // interface type RHS_TYPE.  If RHS_TREE is nil the result will be
414 // NULL.
415
416 tree
417 Expression::get_interface_type_descriptor(Translate_context*,
418                                           Type* rhs_type, tree rhs_tree,
419                                           Location location)
420 {
421   tree rhs_type_tree = TREE_TYPE(rhs_tree);
422   go_assert(TREE_CODE(rhs_type_tree) == RECORD_TYPE);
423   tree rhs_field = TYPE_FIELDS(rhs_type_tree);
424   tree v = build3(COMPONENT_REF, TREE_TYPE(rhs_field), rhs_tree, rhs_field,
425                   NULL_TREE);
426   if (rhs_type->interface_type()->is_empty())
427     {
428       go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)),
429                         "__type_descriptor") == 0);
430       return v;
431     }
432
433   go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)), "__methods")
434              == 0);
435   go_assert(POINTER_TYPE_P(TREE_TYPE(v)));
436   v = save_expr(v);
437   tree v1 = build_fold_indirect_ref_loc(location.gcc_location(), v);
438   go_assert(TREE_CODE(TREE_TYPE(v1)) == RECORD_TYPE);
439   tree f = TYPE_FIELDS(TREE_TYPE(v1));
440   go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(f)), "__type_descriptor")
441              == 0);
442   v1 = build3(COMPONENT_REF, TREE_TYPE(f), v1, f, NULL_TREE);
443
444   tree eq = fold_build2_loc(location.gcc_location(), EQ_EXPR, boolean_type_node,
445                             v, fold_convert_loc(location.gcc_location(),
446                                                 TREE_TYPE(v),
447                                                 null_pointer_node));
448   tree n = fold_convert_loc(location.gcc_location(), TREE_TYPE(v1),
449                             null_pointer_node);
450   return fold_build3_loc(location.gcc_location(), COND_EXPR, TREE_TYPE(v1),
451                          eq, n, v1);
452 }
453
454 // Return a tree for the conversion of an interface type to an
455 // interface type.
456
457 tree
458 Expression::convert_interface_to_interface(Translate_context* context,
459                                            Type *lhs_type, Type *rhs_type,
460                                            tree rhs_tree, bool for_type_guard,
461                                            Location location)
462 {
463   Gogo* gogo = context->gogo();
464   Interface_type* lhs_interface_type = lhs_type->interface_type();
465   bool lhs_is_empty = lhs_interface_type->is_empty();
466
467   tree lhs_type_tree = type_to_tree(lhs_type->get_backend(gogo));
468   if (lhs_type_tree == error_mark_node)
469     return error_mark_node;
470
471   // In the general case this requires runtime examination of the type
472   // method table to match it up with the interface methods.
473
474   // FIXME: If all of the methods in the right hand side interface
475   // also appear in the left hand side interface, then we don't need
476   // to do a runtime check, although we still need to build a new
477   // method table.
478
479   // Get the type descriptor for the right hand side.  This will be
480   // NULL for a nil interface.
481
482   if (!DECL_P(rhs_tree))
483     rhs_tree = save_expr(rhs_tree);
484
485   tree rhs_type_descriptor =
486     Expression::get_interface_type_descriptor(context, rhs_type, rhs_tree,
487                                               location);
488
489   // The result is going to be a two element constructor.
490
491   VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 2);
492
493   constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
494   tree field = TYPE_FIELDS(lhs_type_tree);
495   elt->index = field;
496
497   if (for_type_guard)
498     {
499       // A type assertion fails when converting a nil interface.
500       tree lhs_type_descriptor = lhs_type->type_descriptor_pointer(gogo,
501                                                                    location);
502       static tree assert_interface_decl;
503       tree call = Gogo::call_builtin(&assert_interface_decl,
504                                      location,
505                                      "__go_assert_interface",
506                                      2,
507                                      ptr_type_node,
508                                      TREE_TYPE(lhs_type_descriptor),
509                                      lhs_type_descriptor,
510                                      TREE_TYPE(rhs_type_descriptor),
511                                      rhs_type_descriptor);
512       if (call == error_mark_node)
513         return error_mark_node;
514       // This will panic if the interface conversion fails.
515       TREE_NOTHROW(assert_interface_decl) = 0;
516       elt->value = fold_convert_loc(location.gcc_location(), TREE_TYPE(field),
517                                     call);
518     }
519   else if (lhs_is_empty)
520     {
521       // A convertion to an empty interface always succeeds, and the
522       // first field is just the type descriptor of the object.
523       go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),
524                         "__type_descriptor") == 0);
525       go_assert(TREE_TYPE(field) == TREE_TYPE(rhs_type_descriptor));
526       elt->value = rhs_type_descriptor;
527     }
528   else
529     {
530       // A conversion to a non-empty interface may fail, but unlike a
531       // type assertion converting nil will always succeed.
532       go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__methods")
533                  == 0);
534       tree lhs_type_descriptor = lhs_type->type_descriptor_pointer(gogo,
535                                                                    location);
536       static tree convert_interface_decl;
537       tree call = Gogo::call_builtin(&convert_interface_decl,
538                                      location,
539                                      "__go_convert_interface",
540                                      2,
541                                      ptr_type_node,
542                                      TREE_TYPE(lhs_type_descriptor),
543                                      lhs_type_descriptor,
544                                      TREE_TYPE(rhs_type_descriptor),
545                                      rhs_type_descriptor);
546       if (call == error_mark_node)
547         return error_mark_node;
548       // This will panic if the interface conversion fails.
549       TREE_NOTHROW(convert_interface_decl) = 0;
550       elt->value = fold_convert_loc(location.gcc_location(), TREE_TYPE(field),
551                                     call);
552     }
553
554   // The second field is simply the object pointer.
555
556   elt = VEC_quick_push(constructor_elt, init, NULL);
557   field = DECL_CHAIN(field);
558   go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__object") == 0);
559   elt->index = field;
560
561   tree rhs_type_tree = TREE_TYPE(rhs_tree);
562   go_assert(TREE_CODE(rhs_type_tree) == RECORD_TYPE);
563   tree rhs_field = DECL_CHAIN(TYPE_FIELDS(rhs_type_tree));
564   go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)), "__object") == 0);
565   elt->value = build3(COMPONENT_REF, TREE_TYPE(rhs_field), rhs_tree, rhs_field,
566                       NULL_TREE);
567
568   return build_constructor(lhs_type_tree, init);
569 }
570
571 // Return a tree for the conversion of an interface type to a
572 // non-interface type.
573
574 tree
575 Expression::convert_interface_to_type(Translate_context* context,
576                                       Type *lhs_type, Type* rhs_type,
577                                       tree rhs_tree, Location location)
578 {
579   Gogo* gogo = context->gogo();
580   tree rhs_type_tree = TREE_TYPE(rhs_tree);
581
582   tree lhs_type_tree = type_to_tree(lhs_type->get_backend(gogo));
583   if (lhs_type_tree == error_mark_node)
584     return error_mark_node;
585
586   // Call a function to check that the type is valid.  The function
587   // will panic with an appropriate runtime type error if the type is
588   // not valid.
589
590   tree lhs_type_descriptor = lhs_type->type_descriptor_pointer(gogo, location);
591
592   if (!DECL_P(rhs_tree))
593     rhs_tree = save_expr(rhs_tree);
594
595   tree rhs_type_descriptor =
596     Expression::get_interface_type_descriptor(context, rhs_type, rhs_tree,
597                                               location);
598
599   tree rhs_inter_descriptor = rhs_type->type_descriptor_pointer(gogo,
600                                                                 location);
601
602   static tree check_interface_type_decl;
603   tree call = Gogo::call_builtin(&check_interface_type_decl,
604                                  location,
605                                  "__go_check_interface_type",
606                                  3,
607                                  void_type_node,
608                                  TREE_TYPE(lhs_type_descriptor),
609                                  lhs_type_descriptor,
610                                  TREE_TYPE(rhs_type_descriptor),
611                                  rhs_type_descriptor,
612                                  TREE_TYPE(rhs_inter_descriptor),
613                                  rhs_inter_descriptor);
614   if (call == error_mark_node)
615     return error_mark_node;
616   // This call will panic if the conversion is invalid.
617   TREE_NOTHROW(check_interface_type_decl) = 0;
618
619   // If the call succeeds, pull out the value.
620   go_assert(TREE_CODE(rhs_type_tree) == RECORD_TYPE);
621   tree rhs_field = DECL_CHAIN(TYPE_FIELDS(rhs_type_tree));
622   go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)), "__object") == 0);
623   tree val = build3(COMPONENT_REF, TREE_TYPE(rhs_field), rhs_tree, rhs_field,
624                     NULL_TREE);
625
626   // If the value is a pointer, then it is the value we want.
627   // Otherwise it points to the value.
628   if (lhs_type->points_to() == NULL)
629     {
630       val = fold_convert_loc(location.gcc_location(),
631                              build_pointer_type(lhs_type_tree), val);
632       val = build_fold_indirect_ref_loc(location.gcc_location(), val);
633     }
634
635   return build2(COMPOUND_EXPR, lhs_type_tree, call,
636                 fold_convert_loc(location.gcc_location(), lhs_type_tree, val));
637 }
638
639 // Convert an expression to a tree.  This is implemented by the child
640 // class.  Not that it is not in general safe to call this multiple
641 // times for a single expression, but that we don't catch such errors.
642
643 tree
644 Expression::get_tree(Translate_context* context)
645 {
646   // The child may have marked this expression as having an error.
647   if (this->classification_ == EXPRESSION_ERROR)
648     return error_mark_node;
649
650   return this->do_get_tree(context);
651 }
652
653 // Return a tree for VAL in TYPE.
654
655 tree
656 Expression::integer_constant_tree(mpz_t val, tree type)
657 {
658   if (type == error_mark_node)
659     return error_mark_node;
660   else if (TREE_CODE(type) == INTEGER_TYPE)
661     return double_int_to_tree(type,
662                               mpz_get_double_int(type, val, true));
663   else if (TREE_CODE(type) == REAL_TYPE)
664     {
665       mpfr_t fval;
666       mpfr_init_set_z(fval, val, GMP_RNDN);
667       tree ret = Expression::float_constant_tree(fval, type);
668       mpfr_clear(fval);
669       return ret;
670     }
671   else if (TREE_CODE(type) == COMPLEX_TYPE)
672     {
673       mpfr_t fval;
674       mpfr_init_set_z(fval, val, GMP_RNDN);
675       tree real = Expression::float_constant_tree(fval, TREE_TYPE(type));
676       mpfr_clear(fval);
677       tree imag = build_real_from_int_cst(TREE_TYPE(type),
678                                           integer_zero_node);
679       return build_complex(type, real, imag);
680     }
681   else
682     go_unreachable();
683 }
684
685 // Return a tree for VAL in TYPE.
686
687 tree
688 Expression::float_constant_tree(mpfr_t val, tree type)
689 {
690   if (type == error_mark_node)
691     return error_mark_node;
692   else if (TREE_CODE(type) == INTEGER_TYPE)
693     {
694       mpz_t ival;
695       mpz_init(ival);
696       mpfr_get_z(ival, val, GMP_RNDN);
697       tree ret = Expression::integer_constant_tree(ival, type);
698       mpz_clear(ival);
699       return ret;
700     }
701   else if (TREE_CODE(type) == REAL_TYPE)
702     {
703       REAL_VALUE_TYPE r1;
704       real_from_mpfr(&r1, val, type, GMP_RNDN);
705       REAL_VALUE_TYPE r2;
706       real_convert(&r2, TYPE_MODE(type), &r1);
707       return build_real(type, r2);
708     }
709   else if (TREE_CODE(type) == COMPLEX_TYPE)
710     {
711       REAL_VALUE_TYPE r1;
712       real_from_mpfr(&r1, val, TREE_TYPE(type), GMP_RNDN);
713       REAL_VALUE_TYPE r2;
714       real_convert(&r2, TYPE_MODE(TREE_TYPE(type)), &r1);
715       tree imag = build_real_from_int_cst(TREE_TYPE(type),
716                                           integer_zero_node);
717       return build_complex(type, build_real(TREE_TYPE(type), r2), imag);
718     }
719   else
720     go_unreachable();
721 }
722
723 // Return a tree for REAL/IMAG in TYPE.
724
725 tree
726 Expression::complex_constant_tree(mpfr_t real, mpfr_t imag, tree type)
727 {
728   if (type == error_mark_node)
729     return error_mark_node;
730   else if (TREE_CODE(type) == INTEGER_TYPE || TREE_CODE(type) == REAL_TYPE)
731     return Expression::float_constant_tree(real, type);
732   else if (TREE_CODE(type) == COMPLEX_TYPE)
733     {
734       REAL_VALUE_TYPE r1;
735       real_from_mpfr(&r1, real, TREE_TYPE(type), GMP_RNDN);
736       REAL_VALUE_TYPE r2;
737       real_convert(&r2, TYPE_MODE(TREE_TYPE(type)), &r1);
738
739       REAL_VALUE_TYPE r3;
740       real_from_mpfr(&r3, imag, TREE_TYPE(type), GMP_RNDN);
741       REAL_VALUE_TYPE r4;
742       real_convert(&r4, TYPE_MODE(TREE_TYPE(type)), &r3);
743
744       return build_complex(type, build_real(TREE_TYPE(type), r2),
745                            build_real(TREE_TYPE(type), r4));
746     }
747   else
748     go_unreachable();
749 }
750
751 // Return a tree which evaluates to true if VAL, of arbitrary integer
752 // type, is negative or is more than the maximum value of BOUND_TYPE.
753 // If SOFAR is not NULL, it is or'red into the result.  The return
754 // value may be NULL if SOFAR is NULL.
755
756 tree
757 Expression::check_bounds(tree val, tree bound_type, tree sofar,
758                          Location loc)
759 {
760   tree val_type = TREE_TYPE(val);
761   tree ret = NULL_TREE;
762
763   if (!TYPE_UNSIGNED(val_type))
764     {
765       ret = fold_build2_loc(loc.gcc_location(), LT_EXPR, boolean_type_node, val,
766                             build_int_cst(val_type, 0));
767       if (ret == boolean_false_node)
768         ret = NULL_TREE;
769     }
770
771   HOST_WIDE_INT val_type_size = int_size_in_bytes(val_type);
772   HOST_WIDE_INT bound_type_size = int_size_in_bytes(bound_type);
773   go_assert(val_type_size != -1 && bound_type_size != -1);
774   if (val_type_size > bound_type_size
775       || (val_type_size == bound_type_size
776           && TYPE_UNSIGNED(val_type)
777           && !TYPE_UNSIGNED(bound_type)))
778     {
779       tree max = TYPE_MAX_VALUE(bound_type);
780       tree big = fold_build2_loc(loc.gcc_location(), GT_EXPR, boolean_type_node,
781                                  val, fold_convert_loc(loc.gcc_location(),
782                                                        val_type, max));
783       if (big == boolean_false_node)
784         ;
785       else if (ret == NULL_TREE)
786         ret = big;
787       else
788         ret = fold_build2_loc(loc.gcc_location(), TRUTH_OR_EXPR,
789                               boolean_type_node, ret, big);
790     }
791
792   if (ret == NULL_TREE)
793     return sofar;
794   else if (sofar == NULL_TREE)
795     return ret;
796   else
797     return fold_build2_loc(loc.gcc_location(), TRUTH_OR_EXPR, boolean_type_node,
798                            sofar, ret);
799 }
800
801 void
802 Expression::dump_expression(Ast_dump_context* ast_dump_context) const
803 {
804   this->do_dump_expression(ast_dump_context);
805 }
806
807 // Error expressions.  This are used to avoid cascading errors.
808
809 class Error_expression : public Expression
810 {
811  public:
812   Error_expression(Location location)
813     : Expression(EXPRESSION_ERROR, location)
814   { }
815
816  protected:
817   bool
818   do_is_constant() const
819   { return true; }
820
821   bool
822   do_integer_constant_value(bool, mpz_t val, Type**) const
823   {
824     mpz_set_ui(val, 0);
825     return true;
826   }
827
828   bool
829   do_float_constant_value(mpfr_t val, Type**) const
830   {
831     mpfr_set_ui(val, 0, GMP_RNDN);
832     return true;
833   }
834
835   bool
836   do_complex_constant_value(mpfr_t real, mpfr_t imag, Type**) const
837   {
838     mpfr_set_ui(real, 0, GMP_RNDN);
839     mpfr_set_ui(imag, 0, GMP_RNDN);
840     return true;
841   }
842
843   void
844   do_discarding_value()
845   { }
846
847   Type*
848   do_type()
849   { return Type::make_error_type(); }
850
851   void
852   do_determine_type(const Type_context*)
853   { }
854
855   Expression*
856   do_copy()
857   { return this; }
858
859   bool
860   do_is_addressable() const
861   { return true; }
862
863   tree
864   do_get_tree(Translate_context*)
865   { return error_mark_node; }
866
867   void
868   do_dump_expression(Ast_dump_context*) const;
869 };
870
871 // Dump the ast representation for an error expression to a dump context.
872
873 void
874 Error_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
875 {
876   ast_dump_context->ostream() << "_Error_" ;
877 }
878
879 Expression*
880 Expression::make_error(Location location)
881 {
882   return new Error_expression(location);
883 }
884
885 // An expression which is really a type.  This is used during parsing.
886 // It is an error if these survive after lowering.
887
888 class
889 Type_expression : public Expression
890 {
891  public:
892   Type_expression(Type* type, Location location)
893     : Expression(EXPRESSION_TYPE, location),
894       type_(type)
895   { }
896
897  protected:
898   int
899   do_traverse(Traverse* traverse)
900   { return Type::traverse(this->type_, traverse); }
901
902   Type*
903   do_type()
904   { return this->type_; }
905
906   void
907   do_determine_type(const Type_context*)
908   { }
909
910   void
911   do_check_types(Gogo*)
912   { this->report_error(_("invalid use of type")); }
913
914   Expression*
915   do_copy()
916   { return this; }
917
918   tree
919   do_get_tree(Translate_context*)
920   { go_unreachable(); }
921
922   void do_dump_expression(Ast_dump_context*) const;
923  
924  private:
925   // The type which we are representing as an expression.
926   Type* type_;
927 };
928
929 void
930 Type_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
931 {
932   ast_dump_context->dump_type(this->type_);
933 }
934
935 Expression*
936 Expression::make_type(Type* type, Location location)
937 {
938   return new Type_expression(type, location);
939 }
940
941 // Class Parser_expression.
942
943 Type*
944 Parser_expression::do_type()
945 {
946   // We should never really ask for the type of a Parser_expression.
947   // However, it can happen, at least when we have an invalid const
948   // whose initializer refers to the const itself.  In that case we
949   // may ask for the type when lowering the const itself.
950   go_assert(saw_errors());
951   return Type::make_error_type();
952 }
953
954 // Class Var_expression.
955
956 // Lower a variable expression.  Here we just make sure that the
957 // initialization expression of the variable has been lowered.  This
958 // ensures that we will be able to determine the type of the variable
959 // if necessary.
960
961 Expression*
962 Var_expression::do_lower(Gogo* gogo, Named_object* function,
963                          Statement_inserter* inserter, int)
964 {
965   if (this->variable_->is_variable())
966     {
967       Variable* var = this->variable_->var_value();
968       // This is either a local variable or a global variable.  A
969       // reference to a variable which is local to an enclosing
970       // function will be a reference to a field in a closure.
971       if (var->is_global())
972         {
973           function = NULL;
974           inserter = NULL;
975         }
976       var->lower_init_expression(gogo, function, inserter);
977     }
978   return this;
979 }
980
981 // Return the type of a reference to a variable.
982
983 Type*
984 Var_expression::do_type()
985 {
986   if (this->variable_->is_variable())
987     return this->variable_->var_value()->type();
988   else if (this->variable_->is_result_variable())
989     return this->variable_->result_var_value()->type();
990   else
991     go_unreachable();
992 }
993
994 // Determine the type of a reference to a variable.
995
996 void
997 Var_expression::do_determine_type(const Type_context*)
998 {
999   if (this->variable_->is_variable())
1000     this->variable_->var_value()->determine_type();
1001 }
1002
1003 // Something takes the address of this variable.  This means that we
1004 // may want to move the variable onto the heap.
1005
1006 void
1007 Var_expression::do_address_taken(bool escapes)
1008 {
1009   if (!escapes)
1010     {
1011       if (this->variable_->is_variable())
1012         this->variable_->var_value()->set_non_escaping_address_taken();
1013       else if (this->variable_->is_result_variable())
1014         this->variable_->result_var_value()->set_non_escaping_address_taken();
1015       else
1016         go_unreachable();
1017     }
1018   else
1019     {
1020       if (this->variable_->is_variable())
1021         this->variable_->var_value()->set_address_taken();
1022       else if (this->variable_->is_result_variable())
1023         this->variable_->result_var_value()->set_address_taken();
1024       else
1025         go_unreachable();
1026     }
1027 }
1028
1029 // Get the tree for a reference to a variable.
1030
1031 tree
1032 Var_expression::do_get_tree(Translate_context* context)
1033 {
1034   Bvariable* bvar = this->variable_->get_backend_variable(context->gogo(),
1035                                                           context->function());
1036   tree ret = var_to_tree(bvar);
1037   if (ret == error_mark_node)
1038     return error_mark_node;
1039   bool is_in_heap;
1040   if (this->variable_->is_variable())
1041     is_in_heap = this->variable_->var_value()->is_in_heap();
1042   else if (this->variable_->is_result_variable())
1043     is_in_heap = this->variable_->result_var_value()->is_in_heap();
1044   else
1045     go_unreachable();
1046   if (is_in_heap)
1047     {
1048       ret = build_fold_indirect_ref_loc(this->location().gcc_location(), ret);
1049       TREE_THIS_NOTRAP(ret) = 1;
1050     }
1051   return ret;
1052 }
1053
1054 // Ast dump for variable expression.
1055
1056 void
1057 Var_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
1058 {
1059   ast_dump_context->ostream() << this->variable_->name() ;
1060 }
1061
1062 // Make a reference to a variable in an expression.
1063
1064 Expression*
1065 Expression::make_var_reference(Named_object* var, Location location)
1066 {
1067   if (var->is_sink())
1068     return Expression::make_sink(location);
1069
1070   // FIXME: Creating a new object for each reference to a variable is
1071   // wasteful.
1072   return new Var_expression(var, location);
1073 }
1074
1075 // Class Temporary_reference_expression.
1076
1077 // The type.
1078
1079 Type*
1080 Temporary_reference_expression::do_type()
1081 {
1082   return this->statement_->type();
1083 }
1084
1085 // Called if something takes the address of this temporary variable.
1086 // We never have to move temporary variables to the heap, but we do
1087 // need to know that they must live in the stack rather than in a
1088 // register.
1089
1090 void
1091 Temporary_reference_expression::do_address_taken(bool)
1092 {
1093   this->statement_->set_is_address_taken();
1094 }
1095
1096 // Get a tree referring to the variable.
1097
1098 tree
1099 Temporary_reference_expression::do_get_tree(Translate_context* context)
1100 {
1101   Bvariable* bvar = this->statement_->get_backend_variable(context);
1102
1103   // The gcc backend can't represent the same set of recursive types
1104   // that the Go frontend can.  In some cases this means that a
1105   // temporary variable won't have the right backend type.  Correct
1106   // that here by adding a type cast.  We need to use base() to push
1107   // the circularity down one level.
1108   tree ret = var_to_tree(bvar);
1109   if (!this->is_lvalue_
1110       && POINTER_TYPE_P(TREE_TYPE(ret))
1111       && VOID_TYPE_P(TREE_TYPE(TREE_TYPE(ret))))
1112     {
1113       Btype* type_btype = this->type()->base()->get_backend(context->gogo());
1114       tree type_tree = type_to_tree(type_btype);
1115       ret = fold_convert_loc(this->location().gcc_location(), type_tree, ret);
1116     }
1117   return ret;
1118 }
1119
1120 // Ast dump for temporary reference.
1121
1122 void
1123 Temporary_reference_expression::do_dump_expression(
1124                                 Ast_dump_context* ast_dump_context) const
1125 {
1126   ast_dump_context->dump_temp_variable_name(this->statement_);
1127 }
1128
1129 // Make a reference to a temporary variable.
1130
1131 Temporary_reference_expression*
1132 Expression::make_temporary_reference(Temporary_statement* statement,
1133                                      Location location)
1134 {
1135   return new Temporary_reference_expression(statement, location);
1136 }
1137
1138 // A sink expression--a use of the blank identifier _.
1139
1140 class Sink_expression : public Expression
1141 {
1142  public:
1143   Sink_expression(Location location)
1144     : Expression(EXPRESSION_SINK, location),
1145       type_(NULL), var_(NULL_TREE)
1146   { }
1147
1148  protected:
1149   void
1150   do_discarding_value()
1151   { }
1152
1153   Type*
1154   do_type();
1155
1156   void
1157   do_determine_type(const Type_context*);
1158
1159   Expression*
1160   do_copy()
1161   { return new Sink_expression(this->location()); }
1162
1163   tree
1164   do_get_tree(Translate_context*);
1165
1166   void
1167   do_dump_expression(Ast_dump_context*) const;
1168
1169  private:
1170   // The type of this sink variable.
1171   Type* type_;
1172   // The temporary variable we generate.
1173   tree var_;
1174 };
1175
1176 // Return the type of a sink expression.
1177
1178 Type*
1179 Sink_expression::do_type()
1180 {
1181   if (this->type_ == NULL)
1182     return Type::make_sink_type();
1183   return this->type_;
1184 }
1185
1186 // Determine the type of a sink expression.
1187
1188 void
1189 Sink_expression::do_determine_type(const Type_context* context)
1190 {
1191   if (context->type != NULL)
1192     this->type_ = context->type;
1193 }
1194
1195 // Return a temporary variable for a sink expression.  This will
1196 // presumably be a write-only variable which the middle-end will drop.
1197
1198 tree
1199 Sink_expression::do_get_tree(Translate_context* context)
1200 {
1201   if (this->var_ == NULL_TREE)
1202     {
1203       go_assert(this->type_ != NULL && !this->type_->is_sink_type());
1204       Btype* bt = this->type_->get_backend(context->gogo());
1205       this->var_ = create_tmp_var(type_to_tree(bt), "blank");
1206     }
1207   return this->var_;
1208 }
1209
1210 // Ast dump for sink expression.
1211
1212 void
1213 Sink_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
1214 {
1215   ast_dump_context->ostream() << "_" ;
1216 }
1217
1218 // Make a sink expression.
1219
1220 Expression*
1221 Expression::make_sink(Location location)
1222 {
1223   return new Sink_expression(location);
1224 }
1225
1226 // Class Func_expression.
1227
1228 // FIXME: Can a function expression appear in a constant expression?
1229 // The value is unchanging.  Initializing a constant to the address of
1230 // a function seems like it could work, though there might be little
1231 // point to it.
1232
1233 // Traversal.
1234
1235 int
1236 Func_expression::do_traverse(Traverse* traverse)
1237 {
1238   return (this->closure_ == NULL
1239           ? TRAVERSE_CONTINUE
1240           : Expression::traverse(&this->closure_, traverse));
1241 }
1242
1243 // Return the type of a function expression.
1244
1245 Type*
1246 Func_expression::do_type()
1247 {
1248   if (this->function_->is_function())
1249     return this->function_->func_value()->type();
1250   else if (this->function_->is_function_declaration())
1251     return this->function_->func_declaration_value()->type();
1252   else
1253     go_unreachable();
1254 }
1255
1256 // Get the tree for a function expression without evaluating the
1257 // closure.
1258
1259 tree
1260 Func_expression::get_tree_without_closure(Gogo* gogo)
1261 {
1262   Function_type* fntype;
1263   if (this->function_->is_function())
1264     fntype = this->function_->func_value()->type();
1265   else if (this->function_->is_function_declaration())
1266     fntype = this->function_->func_declaration_value()->type();
1267   else
1268     go_unreachable();
1269
1270   // Builtin functions are handled specially by Call_expression.  We
1271   // can't take their address.
1272   if (fntype->is_builtin())
1273     {
1274       error_at(this->location(), "invalid use of special builtin function %qs",
1275                this->function_->name().c_str());
1276       return error_mark_node;
1277     }
1278
1279   Named_object* no = this->function_;
1280
1281   tree id = no->get_id(gogo);
1282   if (id == error_mark_node)
1283     return error_mark_node;
1284
1285   tree fndecl;
1286   if (no->is_function())
1287     fndecl = no->func_value()->get_or_make_decl(gogo, no, id);
1288   else if (no->is_function_declaration())
1289     fndecl = no->func_declaration_value()->get_or_make_decl(gogo, no, id);
1290   else
1291     go_unreachable();
1292
1293   if (fndecl == error_mark_node)
1294     return error_mark_node;
1295
1296   return build_fold_addr_expr_loc(this->location().gcc_location(), fndecl);
1297 }
1298
1299 // Get the tree for a function expression.  This is used when we take
1300 // the address of a function rather than simply calling it.  If the
1301 // function has a closure, we must use a trampoline.
1302
1303 tree
1304 Func_expression::do_get_tree(Translate_context* context)
1305 {
1306   Gogo* gogo = context->gogo();
1307
1308   tree fnaddr = this->get_tree_without_closure(gogo);
1309   if (fnaddr == error_mark_node)
1310     return error_mark_node;
1311
1312   go_assert(TREE_CODE(fnaddr) == ADDR_EXPR
1313              && TREE_CODE(TREE_OPERAND(fnaddr, 0)) == FUNCTION_DECL);
1314   TREE_ADDRESSABLE(TREE_OPERAND(fnaddr, 0)) = 1;
1315
1316   // For a normal non-nested function call, that is all we have to do.
1317   if (!this->function_->is_function()
1318       || this->function_->func_value()->enclosing() == NULL)
1319     {
1320       go_assert(this->closure_ == NULL);
1321       return fnaddr;
1322     }
1323
1324   // For a nested function call, we have to always allocate a
1325   // trampoline.  If we don't always allocate, then closures will not
1326   // be reliably distinct.
1327   Expression* closure = this->closure_;
1328   tree closure_tree;
1329   if (closure == NULL)
1330     closure_tree = null_pointer_node;
1331   else
1332     {
1333       // Get the value of the closure.  This will be a pointer to
1334       // space allocated on the heap.
1335       closure_tree = closure->get_tree(context);
1336       if (closure_tree == error_mark_node)
1337         return error_mark_node;
1338       go_assert(POINTER_TYPE_P(TREE_TYPE(closure_tree)));
1339     }
1340
1341   // Now we need to build some code on the heap.  This code will load
1342   // the static chain pointer with the closure and then jump to the
1343   // body of the function.  The normal gcc approach is to build the
1344   // code on the stack.  Unfortunately we can not do that, as Go
1345   // permits us to return the function pointer.
1346
1347   return gogo->make_trampoline(fnaddr, closure_tree, this->location());
1348 }
1349
1350 // Ast dump for function.
1351
1352 void
1353 Func_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
1354 {
1355   ast_dump_context->ostream() << this->function_->name();
1356   if (this->closure_ != NULL)
1357     {
1358       ast_dump_context->ostream() << " {closure =  ";
1359       this->closure_->dump_expression(ast_dump_context);
1360       ast_dump_context->ostream() << "}";
1361     }
1362 }
1363
1364 // Make a reference to a function in an expression.
1365
1366 Expression*
1367 Expression::make_func_reference(Named_object* function, Expression* closure,
1368                                 Location location)
1369 {
1370   return new Func_expression(function, closure, location);
1371 }
1372
1373 // Class Unknown_expression.
1374
1375 // Return the name of an unknown expression.
1376
1377 const std::string&
1378 Unknown_expression::name() const
1379 {
1380   return this->named_object_->name();
1381 }
1382
1383 // Lower a reference to an unknown name.
1384
1385 Expression*
1386 Unknown_expression::do_lower(Gogo*, Named_object*, Statement_inserter*, int)
1387 {
1388   Location location = this->location();
1389   Named_object* no = this->named_object_;
1390   Named_object* real;
1391   if (!no->is_unknown())
1392     real = no;
1393   else
1394     {
1395       real = no->unknown_value()->real_named_object();
1396       if (real == NULL)
1397         {
1398           if (this->is_composite_literal_key_)
1399             return this;
1400           error_at(location, "reference to undefined name %qs",
1401                    this->named_object_->message_name().c_str());
1402           return Expression::make_error(location);
1403         }
1404     }
1405   switch (real->classification())
1406     {
1407     case Named_object::NAMED_OBJECT_CONST:
1408       return Expression::make_const_reference(real, location);
1409     case Named_object::NAMED_OBJECT_TYPE:
1410       return Expression::make_type(real->type_value(), location);
1411     case Named_object::NAMED_OBJECT_TYPE_DECLARATION:
1412       if (this->is_composite_literal_key_)
1413         return this;
1414       error_at(location, "reference to undefined type %qs",
1415                real->message_name().c_str());
1416       return Expression::make_error(location);
1417     case Named_object::NAMED_OBJECT_VAR:
1418       return Expression::make_var_reference(real, location);
1419     case Named_object::NAMED_OBJECT_FUNC:
1420     case Named_object::NAMED_OBJECT_FUNC_DECLARATION:
1421       return Expression::make_func_reference(real, NULL, location);
1422     case Named_object::NAMED_OBJECT_PACKAGE:
1423       if (this->is_composite_literal_key_)
1424         return this;
1425       error_at(location, "unexpected reference to package");
1426       return Expression::make_error(location);
1427     default:
1428       go_unreachable();
1429     }
1430 }
1431
1432 // Dump the ast representation for an unknown expression to a dump context.
1433
1434 void
1435 Unknown_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
1436 {
1437   ast_dump_context->ostream() << "_Unknown_(" << this->named_object_->name()
1438                               << ")";
1439 }
1440
1441 // Make a reference to an unknown name.
1442
1443 Expression*
1444 Expression::make_unknown_reference(Named_object* no, Location location)
1445 {
1446   return new Unknown_expression(no, location);
1447 }
1448
1449 // A boolean expression.
1450
1451 class Boolean_expression : public Expression
1452 {
1453  public:
1454   Boolean_expression(bool val, Location location)
1455     : Expression(EXPRESSION_BOOLEAN, location),
1456       val_(val), type_(NULL)
1457   { }
1458
1459   static Expression*
1460   do_import(Import*);
1461
1462  protected:
1463   bool
1464   do_is_constant() const
1465   { return true; }
1466
1467   Type*
1468   do_type();
1469
1470   void
1471   do_determine_type(const Type_context*);
1472
1473   Expression*
1474   do_copy()
1475   { return this; }
1476
1477   tree
1478   do_get_tree(Translate_context*)
1479   { return this->val_ ? boolean_true_node : boolean_false_node; }
1480
1481   void
1482   do_export(Export* exp) const
1483   { exp->write_c_string(this->val_ ? "true" : "false"); }
1484
1485   void
1486   do_dump_expression(Ast_dump_context* ast_dump_context) const
1487   { ast_dump_context->ostream() << (this->val_ ? "true" : "false"); }
1488   
1489  private:
1490   // The constant.
1491   bool val_;
1492   // The type as determined by context.
1493   Type* type_;
1494 };
1495
1496 // Get the type.
1497
1498 Type*
1499 Boolean_expression::do_type()
1500 {
1501   if (this->type_ == NULL)
1502     this->type_ = Type::make_boolean_type();
1503   return this->type_;
1504 }
1505
1506 // Set the type from the context.
1507
1508 void
1509 Boolean_expression::do_determine_type(const Type_context* context)
1510 {
1511   if (this->type_ != NULL && !this->type_->is_abstract())
1512     ;
1513   else if (context->type != NULL && context->type->is_boolean_type())
1514     this->type_ = context->type;
1515   else if (!context->may_be_abstract)
1516     this->type_ = Type::lookup_bool_type();
1517 }
1518
1519 // Import a boolean constant.
1520
1521 Expression*
1522 Boolean_expression::do_import(Import* imp)
1523 {
1524   if (imp->peek_char() == 't')
1525     {
1526       imp->require_c_string("true");
1527       return Expression::make_boolean(true, imp->location());
1528     }
1529   else
1530     {
1531       imp->require_c_string("false");
1532       return Expression::make_boolean(false, imp->location());
1533     }
1534 }
1535
1536 // Make a boolean expression.
1537
1538 Expression*
1539 Expression::make_boolean(bool val, Location location)
1540 {
1541   return new Boolean_expression(val, location);
1542 }
1543
1544 // Class String_expression.
1545
1546 // Get the type.
1547
1548 Type*
1549 String_expression::do_type()
1550 {
1551   if (this->type_ == NULL)
1552     this->type_ = Type::make_string_type();
1553   return this->type_;
1554 }
1555
1556 // Set the type from the context.
1557
1558 void
1559 String_expression::do_determine_type(const Type_context* context)
1560 {
1561   if (this->type_ != NULL && !this->type_->is_abstract())
1562     ;
1563   else if (context->type != NULL && context->type->is_string_type())
1564     this->type_ = context->type;
1565   else if (!context->may_be_abstract)
1566     this->type_ = Type::lookup_string_type();
1567 }
1568
1569 // Build a string constant.
1570
1571 tree
1572 String_expression::do_get_tree(Translate_context* context)
1573 {
1574   return context->gogo()->go_string_constant_tree(this->val_);
1575 }
1576
1577  // Write string literal to string dump.
1578
1579 void
1580 String_expression::export_string(String_dump* exp,
1581                                  const String_expression* str)
1582 {
1583   std::string s;
1584   s.reserve(str->val_.length() * 4 + 2);
1585   s += '"';
1586   for (std::string::const_iterator p = str->val_.begin();
1587        p != str->val_.end();
1588        ++p)
1589     {
1590       if (*p == '\\' || *p == '"')
1591         {
1592           s += '\\';
1593           s += *p;
1594         }
1595       else if (*p >= 0x20 && *p < 0x7f)
1596         s += *p;
1597       else if (*p == '\n')
1598         s += "\\n";
1599       else if (*p == '\t')
1600         s += "\\t";
1601       else
1602         {
1603           s += "\\x";
1604           unsigned char c = *p;
1605           unsigned int dig = c >> 4;
1606           s += dig < 10 ? '0' + dig : 'A' + dig - 10;
1607           dig = c & 0xf;
1608           s += dig < 10 ? '0' + dig : 'A' + dig - 10;
1609         }
1610     }
1611   s += '"';
1612   exp->write_string(s);
1613 }
1614
1615 // Export a string expression.
1616
1617 void
1618 String_expression::do_export(Export* exp) const
1619 {
1620   String_expression::export_string(exp, this);
1621 }
1622
1623 // Import a string expression.
1624
1625 Expression*
1626 String_expression::do_import(Import* imp)
1627 {
1628   imp->require_c_string("\"");
1629   std::string val;
1630   while (true)
1631     {
1632       int c = imp->get_char();
1633       if (c == '"' || c == -1)
1634         break;
1635       if (c != '\\')
1636         val += static_cast<char>(c);
1637       else
1638         {
1639           c = imp->get_char();
1640           if (c == '\\' || c == '"')
1641             val += static_cast<char>(c);
1642           else if (c == 'n')
1643             val += '\n';
1644           else if (c == 't')
1645             val += '\t';
1646           else if (c == 'x')
1647             {
1648               c = imp->get_char();
1649               unsigned int vh = c >= '0' && c <= '9' ? c - '0' : c - 'A' + 10;
1650               c = imp->get_char();
1651               unsigned int vl = c >= '0' && c <= '9' ? c - '0' : c - 'A' + 10;
1652               char v = (vh << 4) | vl;
1653               val += v;
1654             }
1655           else
1656             {
1657               error_at(imp->location(), "bad string constant");
1658               return Expression::make_error(imp->location());
1659             }
1660         }
1661     }
1662   return Expression::make_string(val, imp->location());
1663 }
1664
1665 // Ast dump for string expression.
1666
1667 void
1668 String_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
1669 {
1670   String_expression::export_string(ast_dump_context, this);
1671 }
1672
1673 // Make a string expression.
1674
1675 Expression*
1676 Expression::make_string(const std::string& val, Location location)
1677 {
1678   return new String_expression(val, location);
1679 }
1680
1681 // Make an integer expression.
1682
1683 class Integer_expression : public Expression
1684 {
1685  public:
1686   Integer_expression(const mpz_t* val, Type* type, Location location)
1687     : Expression(EXPRESSION_INTEGER, location),
1688       type_(type)
1689   { mpz_init_set(this->val_, *val); }
1690
1691   static Expression*
1692   do_import(Import*);
1693
1694   // Return whether VAL fits in the type.
1695   static bool
1696   check_constant(mpz_t val, Type*, Location);
1697
1698   // Write VAL to string dump.
1699   static void
1700   export_integer(String_dump* exp, const mpz_t val);
1701
1702   // Write VAL to dump context.
1703   static void
1704   dump_integer(Ast_dump_context* ast_dump_context, const mpz_t val);
1705
1706  protected:
1707   bool
1708   do_is_constant() const
1709   { return true; }
1710
1711   bool
1712   do_integer_constant_value(bool, mpz_t val, Type** ptype) const;
1713
1714   Type*
1715   do_type();
1716
1717   void
1718   do_determine_type(const Type_context* context);
1719
1720   void
1721   do_check_types(Gogo*);
1722
1723   tree
1724   do_get_tree(Translate_context*);
1725
1726   Expression*
1727   do_copy()
1728   { return Expression::make_integer(&this->val_, this->type_,
1729                                     this->location()); }
1730
1731   void
1732   do_export(Export*) const;
1733
1734   void
1735   do_dump_expression(Ast_dump_context*) const;
1736
1737  private:
1738   // The integer value.
1739   mpz_t val_;
1740   // The type so far.
1741   Type* type_;
1742 };
1743
1744 // Return an integer constant value.
1745
1746 bool
1747 Integer_expression::do_integer_constant_value(bool, mpz_t val,
1748                                               Type** ptype) const
1749 {
1750   if (this->type_ != NULL)
1751     *ptype = this->type_;
1752   mpz_set(val, this->val_);
1753   return true;
1754 }
1755
1756 // Return the current type.  If we haven't set the type yet, we return
1757 // an abstract integer type.
1758
1759 Type*
1760 Integer_expression::do_type()
1761 {
1762   if (this->type_ == NULL)
1763     this->type_ = Type::make_abstract_integer_type();
1764   return this->type_;
1765 }
1766
1767 // Set the type of the integer value.  Here we may switch from an
1768 // abstract type to a real type.
1769
1770 void
1771 Integer_expression::do_determine_type(const Type_context* context)
1772 {
1773   if (this->type_ != NULL && !this->type_->is_abstract())
1774     ;
1775   else if (context->type != NULL
1776            && (context->type->integer_type() != NULL
1777                || context->type->float_type() != NULL
1778                || context->type->complex_type() != NULL))
1779     this->type_ = context->type;
1780   else if (!context->may_be_abstract)
1781     this->type_ = Type::lookup_integer_type("int");
1782 }
1783
1784 // Return true if the integer VAL fits in the range of the type TYPE.
1785 // Otherwise give an error and return false.  TYPE may be NULL.
1786
1787 bool
1788 Integer_expression::check_constant(mpz_t val, Type* type,
1789                                    Location location)
1790 {
1791   if (type == NULL)
1792     return true;
1793   Integer_type* itype = type->integer_type();
1794   if (itype == NULL || itype->is_abstract())
1795     return true;
1796
1797   int bits = mpz_sizeinbase(val, 2);
1798
1799   if (itype->is_unsigned())
1800     {
1801       // For an unsigned type we can only accept a nonnegative number,
1802       // and we must be able to represent at least BITS.
1803       if (mpz_sgn(val) >= 0
1804           && bits <= itype->bits())
1805         return true;
1806     }
1807   else
1808     {
1809       // For a signed type we need an extra bit to indicate the sign.
1810       // We have to handle the most negative integer specially.
1811       if (bits + 1 <= itype->bits()
1812           || (bits <= itype->bits()
1813               && mpz_sgn(val) < 0
1814               && (mpz_scan1(val, 0)
1815                   == static_cast<unsigned long>(itype->bits() - 1))
1816               && mpz_scan0(val, itype->bits()) == ULONG_MAX))
1817         return true;
1818     }
1819
1820   error_at(location, "integer constant overflow");
1821   return false;
1822 }
1823
1824 // Check the type of an integer constant.
1825
1826 void
1827 Integer_expression::do_check_types(Gogo*)
1828 {
1829   if (this->type_ == NULL)
1830     return;
1831   if (!Integer_expression::check_constant(this->val_, this->type_,
1832                                           this->location()))
1833     this->set_is_error();
1834 }
1835
1836 // Get a tree for an integer constant.
1837
1838 tree
1839 Integer_expression::do_get_tree(Translate_context* context)
1840 {
1841   Gogo* gogo = context->gogo();
1842   tree type;
1843   if (this->type_ != NULL && !this->type_->is_abstract())
1844     type = type_to_tree(this->type_->get_backend(gogo));
1845   else if (this->type_ != NULL && this->type_->float_type() != NULL)
1846     {
1847       // We are converting to an abstract floating point type.
1848       Type* ftype = Type::lookup_float_type("float64");
1849       type = type_to_tree(ftype->get_backend(gogo));
1850     }
1851   else if (this->type_ != NULL && this->type_->complex_type() != NULL)
1852     {
1853       // We are converting to an abstract complex type.
1854       Type* ctype = Type::lookup_complex_type("complex128");
1855       type = type_to_tree(ctype->get_backend(gogo));
1856     }
1857   else
1858     {
1859       // If we still have an abstract type here, then this is being
1860       // used in a constant expression which didn't get reduced for
1861       // some reason.  Use a type which will fit the value.  We use <,
1862       // not <=, because we need an extra bit for the sign bit.
1863       int bits = mpz_sizeinbase(this->val_, 2);
1864       if (bits < INT_TYPE_SIZE)
1865         {
1866           Type* t = Type::lookup_integer_type("int");
1867           type = type_to_tree(t->get_backend(gogo));
1868         }
1869       else if (bits < 64)
1870         {
1871           Type* t = Type::lookup_integer_type("int64");
1872           type = type_to_tree(t->get_backend(gogo));
1873         }
1874       else
1875         type = long_long_integer_type_node;
1876     }
1877   return Expression::integer_constant_tree(this->val_, type);
1878 }
1879
1880 // Write VAL to export data.
1881
1882 void
1883 Integer_expression::export_integer(String_dump* exp, const mpz_t val)
1884 {
1885   char* s = mpz_get_str(NULL, 10, val);
1886   exp->write_c_string(s);
1887   free(s);
1888 }
1889
1890 // Export an integer in a constant expression.
1891
1892 void
1893 Integer_expression::do_export(Export* exp) const
1894 {
1895   Integer_expression::export_integer(exp, this->val_);
1896   // A trailing space lets us reliably identify the end of the number.
1897   exp->write_c_string(" ");
1898 }
1899
1900 // Import an integer, floating point, or complex value.  This handles
1901 // all these types because they all start with digits.
1902
1903 Expression*
1904 Integer_expression::do_import(Import* imp)
1905 {
1906   std::string num = imp->read_identifier();
1907   imp->require_c_string(" ");
1908   if (!num.empty() && num[num.length() - 1] == 'i')
1909     {
1910       mpfr_t real;
1911       size_t plus_pos = num.find('+', 1);
1912       size_t minus_pos = num.find('-', 1);
1913       size_t pos;
1914       if (plus_pos == std::string::npos)
1915         pos = minus_pos;
1916       else if (minus_pos == std::string::npos)
1917         pos = plus_pos;
1918       else
1919         {
1920           error_at(imp->location(), "bad number in import data: %qs",
1921                    num.c_str());
1922           return Expression::make_error(imp->location());
1923         }
1924       if (pos == std::string::npos)
1925         mpfr_set_ui(real, 0, GMP_RNDN);
1926       else
1927         {
1928           std::string real_str = num.substr(0, pos);
1929           if (mpfr_init_set_str(real, real_str.c_str(), 10, GMP_RNDN) != 0)
1930             {
1931               error_at(imp->location(), "bad number in import data: %qs",
1932                        real_str.c_str());
1933               return Expression::make_error(imp->location());
1934             }
1935         }
1936
1937       std::string imag_str;
1938       if (pos == std::string::npos)
1939         imag_str = num;
1940       else
1941         imag_str = num.substr(pos);
1942       imag_str = imag_str.substr(0, imag_str.size() - 1);
1943       mpfr_t imag;
1944       if (mpfr_init_set_str(imag, imag_str.c_str(), 10, GMP_RNDN) != 0)
1945         {
1946           error_at(imp->location(), "bad number in import data: %qs",
1947                    imag_str.c_str());
1948           return Expression::make_error(imp->location());
1949         }
1950       Expression* ret = Expression::make_complex(&real, &imag, NULL,
1951                                                  imp->location());
1952       mpfr_clear(real);
1953       mpfr_clear(imag);
1954       return ret;
1955     }
1956   else if (num.find('.') == std::string::npos
1957            && num.find('E') == std::string::npos)
1958     {
1959       mpz_t val;
1960       if (mpz_init_set_str(val, num.c_str(), 10) != 0)
1961         {
1962           error_at(imp->location(), "bad number in import data: %qs",
1963                    num.c_str());
1964           return Expression::make_error(imp->location());
1965         }
1966       Expression* ret = Expression::make_integer(&val, NULL, imp->location());
1967       mpz_clear(val);
1968       return ret;
1969     }
1970   else
1971     {
1972       mpfr_t val;
1973       if (mpfr_init_set_str(val, num.c_str(), 10, GMP_RNDN) != 0)
1974         {
1975           error_at(imp->location(), "bad number in import data: %qs",
1976                    num.c_str());
1977           return Expression::make_error(imp->location());
1978         }
1979       Expression* ret = Expression::make_float(&val, NULL, imp->location());
1980       mpfr_clear(val);
1981       return ret;
1982     }
1983 }
1984 // Ast dump for integer expression.
1985
1986 void
1987 Integer_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
1988 {
1989   Integer_expression::export_integer(ast_dump_context, this->val_);
1990 }
1991
1992 // Build a new integer value.
1993
1994 Expression*
1995 Expression::make_integer(const mpz_t* val, Type* type,
1996                          Location location)
1997 {
1998   return new Integer_expression(val, type, location);
1999 }
2000
2001 // Floats.
2002
2003 class Float_expression : public Expression
2004 {
2005  public:
2006   Float_expression(const mpfr_t* val, Type* type, Location location)
2007     : Expression(EXPRESSION_FLOAT, location),
2008       type_(type)
2009   {
2010     mpfr_init_set(this->val_, *val, GMP_RNDN);
2011   }
2012
2013   // Constrain VAL to fit into TYPE.
2014   static void
2015   constrain_float(mpfr_t val, Type* type);
2016
2017   // Return whether VAL fits in the type.
2018   static bool
2019   check_constant(mpfr_t val, Type*, Location);
2020
2021   // Write VAL to export data.
2022   static void
2023   export_float(String_dump* exp, const mpfr_t val);
2024
2025   // Write VAL to dump file.
2026   static void
2027   dump_float(Ast_dump_context* ast_dump_context, const mpfr_t val);
2028
2029  protected:
2030   bool
2031   do_is_constant() const
2032   { return true; }
2033
2034   bool
2035   do_float_constant_value(mpfr_t val, Type**) const;
2036
2037   Type*
2038   do_type();
2039
2040   void
2041   do_determine_type(const Type_context*);
2042
2043   void
2044   do_check_types(Gogo*);
2045
2046   Expression*
2047   do_copy()
2048   { return Expression::make_float(&this->val_, this->type_,
2049                                   this->location()); }
2050
2051   tree
2052   do_get_tree(Translate_context*);
2053
2054   void
2055   do_export(Export*) const;
2056
2057   void
2058   do_dump_expression(Ast_dump_context*) const;
2059
2060  private:
2061   // The floating point value.
2062   mpfr_t val_;
2063   // The type so far.
2064   Type* type_;
2065 };
2066
2067 // Constrain VAL to fit into TYPE.
2068
2069 void
2070 Float_expression::constrain_float(mpfr_t val, Type* type)
2071 {
2072   Float_type* ftype = type->float_type();
2073   if (ftype != NULL && !ftype->is_abstract())
2074     mpfr_prec_round(val, ftype->bits(), GMP_RNDN);
2075 }
2076
2077 // Return a floating point constant value.
2078
2079 bool
2080 Float_expression::do_float_constant_value(mpfr_t val, Type** ptype) const
2081 {
2082   if (this->type_ != NULL)
2083     *ptype = this->type_;
2084   mpfr_set(val, this->val_, GMP_RNDN);
2085   return true;
2086 }
2087
2088 // Return the current type.  If we haven't set the type yet, we return
2089 // an abstract float type.
2090
2091 Type*
2092 Float_expression::do_type()
2093 {
2094   if (this->type_ == NULL)
2095     this->type_ = Type::make_abstract_float_type();
2096   return this->type_;
2097 }
2098
2099 // Set the type of the float value.  Here we may switch from an
2100 // abstract type to a real type.
2101
2102 void
2103 Float_expression::do_determine_type(const Type_context* context)
2104 {
2105   if (this->type_ != NULL && !this->type_->is_abstract())
2106     ;
2107   else if (context->type != NULL
2108            && (context->type->integer_type() != NULL
2109                || context->type->float_type() != NULL
2110                || context->type->complex_type() != NULL))
2111     this->type_ = context->type;
2112   else if (!context->may_be_abstract)
2113     this->type_ = Type::lookup_float_type("float64");
2114 }
2115
2116 // Return true if the floating point value VAL fits in the range of
2117 // the type TYPE.  Otherwise give an error and return false.  TYPE may
2118 // be NULL.
2119
2120 bool
2121 Float_expression::check_constant(mpfr_t val, Type* type,
2122                                  Location location)
2123 {
2124   if (type == NULL)
2125     return true;
2126   Float_type* ftype = type->float_type();
2127   if (ftype == NULL || ftype->is_abstract())
2128     return true;
2129
2130   // A NaN or Infinity always fits in the range of the type.
2131   if (mpfr_nan_p(val) || mpfr_inf_p(val) || mpfr_zero_p(val))
2132     return true;
2133
2134   mp_exp_t exp = mpfr_get_exp(val);
2135   mp_exp_t max_exp;
2136   switch (ftype->bits())
2137     {
2138     case 32:
2139       max_exp = 128;
2140       break;
2141     case 64:
2142       max_exp = 1024;
2143       break;
2144     default:
2145       go_unreachable();
2146     }
2147   if (exp > max_exp)
2148     {
2149       error_at(location, "floating point constant overflow");
2150       return false;
2151     }
2152   return true;
2153 }
2154
2155 // Check the type of a float value.
2156
2157 void
2158 Float_expression::do_check_types(Gogo*)
2159 {
2160   if (this->type_ == NULL)
2161     return;
2162
2163   if (!Float_expression::check_constant(this->val_, this->type_,
2164                                         this->location()))
2165     this->set_is_error();
2166
2167   Integer_type* integer_type = this->type_->integer_type();
2168   if (integer_type != NULL)
2169     {
2170       if (!mpfr_integer_p(this->val_))
2171         this->report_error(_("floating point constant truncated to integer"));
2172       else
2173         {
2174           go_assert(!integer_type->is_abstract());
2175           mpz_t ival;
2176           mpz_init(ival);
2177           mpfr_get_z(ival, this->val_, GMP_RNDN);
2178           Integer_expression::check_constant(ival, integer_type,
2179                                              this->location());
2180           mpz_clear(ival);
2181         }
2182     }
2183 }
2184
2185 // Get a tree for a float constant.
2186
2187 tree
2188 Float_expression::do_get_tree(Translate_context* context)
2189 {
2190   Gogo* gogo = context->gogo();
2191   tree type;
2192   if (this->type_ != NULL && !this->type_->is_abstract())
2193     type = type_to_tree(this->type_->get_backend(gogo));
2194   else if (this->type_ != NULL && this->type_->integer_type() != NULL)
2195     {
2196       // We have an abstract integer type.  We just hope for the best.
2197       type = type_to_tree(Type::lookup_integer_type("int")->get_backend(gogo));
2198     }
2199   else
2200     {
2201       // If we still have an abstract type here, then this is being
2202       // used in a constant expression which didn't get reduced.  We
2203       // just use float64 and hope for the best.
2204       Type* ft = Type::lookup_float_type("float64");
2205       type = type_to_tree(ft->get_backend(gogo));
2206     }
2207   return Expression::float_constant_tree(this->val_, type);
2208 }
2209
2210 // Write a floating point number to a string dump.
2211
2212 void
2213 Float_expression::export_float(String_dump *exp, const mpfr_t val)
2214 {
2215   mp_exp_t exponent;
2216   char* s = mpfr_get_str(NULL, &exponent, 10, 0, val, GMP_RNDN);
2217   if (*s == '-')
2218     exp->write_c_string("-");
2219   exp->write_c_string("0.");
2220   exp->write_c_string(*s == '-' ? s + 1 : s);
2221   mpfr_free_str(s);
2222   char buf[30];
2223   snprintf(buf, sizeof buf, "E%ld", exponent);
2224   exp->write_c_string(buf);
2225 }
2226
2227 // Export a floating point number in a constant expression.
2228
2229 void
2230 Float_expression::do_export(Export* exp) const
2231 {
2232   Float_expression::export_float(exp, this->val_);
2233   // A trailing space lets us reliably identify the end of the number.
2234   exp->write_c_string(" ");
2235 }
2236
2237 // Dump a floating point number to the dump file.
2238
2239 void
2240 Float_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
2241 {
2242   Float_expression::export_float(ast_dump_context, this->val_);
2243 }
2244
2245 // Make a float expression.
2246
2247 Expression*
2248 Expression::make_float(const mpfr_t* val, Type* type, Location location)
2249 {
2250   return new Float_expression(val, type, location);
2251 }
2252
2253 // Complex numbers.
2254
2255 class Complex_expression : public Expression
2256 {
2257  public:
2258   Complex_expression(const mpfr_t* real, const mpfr_t* imag, Type* type,
2259                      Location location)
2260     : Expression(EXPRESSION_COMPLEX, location),
2261       type_(type)
2262   {
2263     mpfr_init_set(this->real_, *real, GMP_RNDN);
2264     mpfr_init_set(this->imag_, *imag, GMP_RNDN);
2265   }
2266
2267   // Constrain REAL/IMAG to fit into TYPE.
2268   static void
2269   constrain_complex(mpfr_t real, mpfr_t imag, Type* type);
2270
2271   // Return whether REAL/IMAG fits in the type.
2272   static bool
2273   check_constant(mpfr_t real, mpfr_t imag, Type*, Location);
2274
2275   // Write REAL/IMAG to string dump.
2276   static void
2277   export_complex(String_dump* exp, const mpfr_t real, const mpfr_t val);
2278
2279   // Write REAL/IMAG to dump context.
2280   static void
2281   dump_complex(Ast_dump_context* ast_dump_context, 
2282                const mpfr_t real, const mpfr_t val);
2283   
2284  protected:
2285   bool
2286   do_is_constant() const
2287   { return true; }
2288
2289   bool
2290   do_complex_constant_value(mpfr_t real, mpfr_t imag, Type**) const;
2291
2292   Type*
2293   do_type();
2294
2295   void
2296   do_determine_type(const Type_context*);
2297
2298   void
2299   do_check_types(Gogo*);
2300
2301   Expression*
2302   do_copy()
2303   {
2304     return Expression::make_complex(&this->real_, &this->imag_, this->type_,
2305                                     this->location());
2306   }
2307
2308   tree
2309   do_get_tree(Translate_context*);
2310
2311   void
2312   do_export(Export*) const;
2313
2314   void
2315   do_dump_expression(Ast_dump_context*) const;
2316   
2317  private:
2318   // The real part.
2319   mpfr_t real_;
2320   // The imaginary part;
2321   mpfr_t imag_;
2322   // The type if known.
2323   Type* type_;
2324 };
2325
2326 // Constrain REAL/IMAG to fit into TYPE.
2327
2328 void
2329 Complex_expression::constrain_complex(mpfr_t real, mpfr_t imag, Type* type)
2330 {
2331   Complex_type* ctype = type->complex_type();
2332   if (ctype != NULL && !ctype->is_abstract())
2333     {
2334       mpfr_prec_round(real, ctype->bits() / 2, GMP_RNDN);
2335       mpfr_prec_round(imag, ctype->bits() / 2, GMP_RNDN);
2336     }
2337 }
2338
2339 // Return a complex constant value.
2340
2341 bool
2342 Complex_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag,
2343                                               Type** ptype) const
2344 {
2345   if (this->type_ != NULL)
2346     *ptype = this->type_;
2347   mpfr_set(real, this->real_, GMP_RNDN);
2348   mpfr_set(imag, this->imag_, GMP_RNDN);
2349   return true;
2350 }
2351
2352 // Return the current type.  If we haven't set the type yet, we return
2353 // an abstract complex type.
2354
2355 Type*
2356 Complex_expression::do_type()
2357 {
2358   if (this->type_ == NULL)
2359     this->type_ = Type::make_abstract_complex_type();
2360   return this->type_;
2361 }
2362
2363 // Set the type of the complex value.  Here we may switch from an
2364 // abstract type to a real type.
2365
2366 void
2367 Complex_expression::do_determine_type(const Type_context* context)
2368 {
2369   if (this->type_ != NULL && !this->type_->is_abstract())
2370     ;
2371   else if (context->type != NULL
2372            && context->type->complex_type() != NULL)
2373     this->type_ = context->type;
2374   else if (!context->may_be_abstract)
2375     this->type_ = Type::lookup_complex_type("complex128");
2376 }
2377
2378 // Return true if the complex value REAL/IMAG fits in the range of the
2379 // type TYPE.  Otherwise give an error and return false.  TYPE may be
2380 // NULL.
2381
2382 bool
2383 Complex_expression::check_constant(mpfr_t real, mpfr_t imag, Type* type,
2384                                    Location location)
2385 {
2386   if (type == NULL)
2387     return true;
2388   Complex_type* ctype = type->complex_type();
2389   if (ctype == NULL || ctype->is_abstract())
2390     return true;
2391
2392   mp_exp_t max_exp;
2393   switch (ctype->bits())
2394     {
2395     case 64:
2396       max_exp = 128;
2397       break;
2398     case 128:
2399       max_exp = 1024;
2400       break;
2401     default:
2402       go_unreachable();
2403     }
2404
2405   // A NaN or Infinity always fits in the range of the type.
2406   if (!mpfr_nan_p(real) && !mpfr_inf_p(real) && !mpfr_zero_p(real))
2407     {
2408       if (mpfr_get_exp(real) > max_exp)
2409         {
2410           error_at(location, "complex real part constant overflow");
2411           return false;
2412         }
2413     }
2414
2415   if (!mpfr_nan_p(imag) && !mpfr_inf_p(imag) && !mpfr_zero_p(imag))
2416     {
2417       if (mpfr_get_exp(imag) > max_exp)
2418         {
2419           error_at(location, "complex imaginary part constant overflow");
2420           return false;
2421         }
2422     }
2423
2424   return true;
2425 }
2426
2427 // Check the type of a complex value.
2428
2429 void
2430 Complex_expression::do_check_types(Gogo*)
2431 {
2432   if (this->type_ == NULL)
2433     return;
2434
2435   if (!Complex_expression::check_constant(this->real_, this->imag_,
2436                                           this->type_, this->location()))
2437     this->set_is_error();
2438 }
2439
2440 // Get a tree for a complex constant.
2441
2442 tree
2443 Complex_expression::do_get_tree(Translate_context* context)
2444 {
2445   Gogo* gogo = context->gogo();
2446   tree type;
2447   if (this->type_ != NULL && !this->type_->is_abstract())
2448     type = type_to_tree(this->type_->get_backend(gogo));
2449   else
2450     {
2451       // If we still have an abstract type here, this this is being
2452       // used in a constant expression which didn't get reduced.  We
2453       // just use complex128 and hope for the best.
2454       Type* ct = Type::lookup_complex_type("complex128");
2455       type = type_to_tree(ct->get_backend(gogo));
2456     }
2457   return Expression::complex_constant_tree(this->real_, this->imag_, type);
2458 }
2459
2460 // Write REAL/IMAG to export data.
2461
2462 void
2463 Complex_expression::export_complex(String_dump* exp, const mpfr_t real,
2464                                    const mpfr_t imag)
2465 {
2466   if (!mpfr_zero_p(real))
2467     {
2468       Float_expression::export_float(exp, real);
2469       if (mpfr_sgn(imag) > 0)
2470         exp->write_c_string("+");
2471     }
2472   Float_expression::export_float(exp, imag);
2473   exp->write_c_string("i");
2474 }
2475
2476 // Export a complex number in a constant expression.
2477
2478 void
2479 Complex_expression::do_export(Export* exp) const
2480 {
2481   Complex_expression::export_complex(exp, this->real_, this->imag_);
2482   // A trailing space lets us reliably identify the end of the number.
2483   exp->write_c_string(" ");
2484 }
2485
2486 // Dump a complex expression to the dump file.
2487
2488 void
2489 Complex_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
2490 {
2491   Complex_expression::export_complex(ast_dump_context,
2492                                       this->real_,
2493                                       this->imag_);
2494 }
2495
2496 // Make a complex expression.
2497
2498 Expression*
2499 Expression::make_complex(const mpfr_t* real, const mpfr_t* imag, Type* type,
2500                          Location location)
2501 {
2502   return new Complex_expression(real, imag, type, location);
2503 }
2504
2505 // Find a named object in an expression.
2506
2507 class Find_named_object : public Traverse
2508 {
2509  public:
2510   Find_named_object(Named_object* no)
2511     : Traverse(traverse_expressions),
2512       no_(no), found_(false)
2513   { }
2514
2515   // Whether we found the object.
2516   bool
2517   found() const
2518   { return this->found_; }
2519
2520  protected:
2521   int
2522   expression(Expression**);
2523
2524  private:
2525   // The object we are looking for.
2526   Named_object* no_;
2527   // Whether we found it.
2528   bool found_;
2529 };
2530
2531 // A reference to a const in an expression.
2532
2533 class Const_expression : public Expression
2534 {
2535  public:
2536   Const_expression(Named_object* constant, Location location)
2537     : Expression(EXPRESSION_CONST_REFERENCE, location),
2538       constant_(constant), type_(NULL), seen_(false)
2539   { }
2540
2541   Named_object*
2542   named_object()
2543   { return this->constant_; }
2544
2545   // Check that the initializer does not refer to the constant itself.
2546   void
2547   check_for_init_loop();
2548
2549  protected:
2550   int
2551   do_traverse(Traverse*);
2552
2553   Expression*
2554   do_lower(Gogo*, Named_object*, Statement_inserter*, int);
2555
2556   bool
2557   do_is_constant() const
2558   { return true; }
2559
2560   bool
2561   do_integer_constant_value(bool, mpz_t val, Type**) const;
2562
2563   bool
2564   do_float_constant_value(mpfr_t val, Type**) const;
2565
2566   bool
2567   do_complex_constant_value(mpfr_t real, mpfr_t imag, Type**) const;
2568
2569   bool
2570   do_string_constant_value(std::string* val) const
2571   { return this->constant_->const_value()->expr()->string_constant_value(val); }
2572
2573   Type*
2574   do_type();
2575
2576   // The type of a const is set by the declaration, not the use.
2577   void
2578   do_determine_type(const Type_context*);
2579
2580   void
2581   do_check_types(Gogo*);
2582
2583   Expression*
2584   do_copy()
2585   { return this; }
2586
2587   tree
2588   do_get_tree(Translate_context* context);
2589
2590   // When exporting a reference to a const as part of a const
2591   // expression, we export the value.  We ignore the fact that it has
2592   // a name.
2593   void
2594   do_export(Export* exp) const
2595   { this->constant_->const_value()->expr()->export_expression(exp); }
2596
2597   void
2598   do_dump_expression(Ast_dump_context*) const;
2599
2600  private:
2601   // The constant.
2602   Named_object* constant_;
2603   // The type of this reference.  This is used if the constant has an
2604   // abstract type.
2605   Type* type_;
2606   // Used to prevent infinite recursion when a constant incorrectly
2607   // refers to itself.
2608   mutable bool seen_;
2609 };
2610
2611 // Traversal.
2612
2613 int
2614 Const_expression::do_traverse(Traverse* traverse)
2615 {
2616   if (this->type_ != NULL)
2617     return Type::traverse(this->type_, traverse);
2618   return TRAVERSE_CONTINUE;
2619 }
2620
2621 // Lower a constant expression.  This is where we convert the
2622 // predeclared constant iota into an integer value.
2623
2624 Expression*
2625 Const_expression::do_lower(Gogo* gogo, Named_object*,
2626                            Statement_inserter*, int iota_value)
2627 {
2628   if (this->constant_->const_value()->expr()->classification()
2629       == EXPRESSION_IOTA)
2630     {
2631       if (iota_value == -1)
2632         {
2633           error_at(this->location(),
2634                    "iota is only defined in const declarations");
2635           iota_value = 0;
2636         }
2637       mpz_t val;
2638       mpz_init_set_ui(val, static_cast<unsigned long>(iota_value));
2639       Expression* ret = Expression::make_integer(&val, NULL,
2640                                                  this->location());
2641       mpz_clear(val);
2642       return ret;
2643     }
2644
2645   // Make sure that the constant itself has been lowered.
2646   gogo->lower_constant(this->constant_);
2647
2648   return this;
2649 }
2650
2651 // Return an integer constant value.
2652
2653 bool
2654 Const_expression::do_integer_constant_value(bool iota_is_constant, mpz_t val,
2655                                             Type** ptype) const
2656 {
2657   if (this->seen_)
2658     return false;
2659
2660   Type* ctype;
2661   if (this->type_ != NULL)
2662     ctype = this->type_;
2663   else
2664     ctype = this->constant_->const_value()->type();
2665   if (ctype != NULL && ctype->integer_type() == NULL)
2666     return false;
2667
2668   Expression* e = this->constant_->const_value()->expr();
2669
2670   this->seen_ = true;
2671
2672   Type* t;
2673   bool r = e->integer_constant_value(iota_is_constant, val, &t);
2674
2675   this->seen_ = false;
2676
2677   if (r
2678       && ctype != NULL
2679       && !Integer_expression::check_constant(val, ctype, this->location()))
2680     return false;
2681
2682   *ptype = ctype != NULL ? ctype : t;
2683   return r;
2684 }
2685
2686 // Return a floating point constant value.
2687
2688 bool
2689 Const_expression::do_float_constant_value(mpfr_t val, Type** ptype) const
2690 {
2691   if (this->seen_)
2692     return false;
2693
2694   Type* ctype;
2695   if (this->type_ != NULL)
2696     ctype = this->type_;
2697   else
2698     ctype = this->constant_->const_value()->type();
2699   if (ctype != NULL && ctype->float_type() == NULL)
2700     return false;
2701
2702   this->seen_ = true;
2703
2704   Type* t;
2705   bool r = this->constant_->const_value()->expr()->float_constant_value(val,
2706                                                                         &t);
2707
2708   this->seen_ = false;
2709
2710   if (r && ctype != NULL)
2711     {
2712       if (!Float_expression::check_constant(val, ctype, this->location()))
2713         return false;
2714       Float_expression::constrain_float(val, ctype);
2715     }
2716   *ptype = ctype != NULL ? ctype : t;
2717   return r;
2718 }
2719
2720 // Return a complex constant value.
2721
2722 bool
2723 Const_expression::do_complex_constant_value(mpfr_t real, mpfr_t imag,
2724                                             Type **ptype) const
2725 {
2726   if (this->seen_)
2727     return false;
2728
2729   Type* ctype;
2730   if (this->type_ != NULL)
2731     ctype = this->type_;
2732   else
2733     ctype = this->constant_->const_value()->type();
2734   if (ctype != NULL && ctype->complex_type() == NULL)
2735     return false;
2736
2737   this->seen_ = true;
2738
2739   Type *t;
2740   bool r = this->constant_->const_value()->expr()->complex_constant_value(real,
2741                                                                           imag,
2742                                                                           &t);
2743
2744   this->seen_ = false;
2745
2746   if (r && ctype != NULL)
2747     {
2748       if (!Complex_expression::check_constant(real, imag, ctype,
2749                                               this->location()))
2750         return false;
2751       Complex_expression::constrain_complex(real, imag, ctype);
2752     }
2753   *ptype = ctype != NULL ? ctype : t;
2754   return r;
2755 }
2756
2757 // Return the type of the const reference.
2758
2759 Type*
2760 Const_expression::do_type()
2761 {
2762   if (this->type_ != NULL)
2763     return this->type_;
2764
2765   Named_constant* nc = this->constant_->const_value();
2766
2767   if (this->seen_ || nc->lowering())
2768     {
2769       this->report_error(_("constant refers to itself"));
2770       this->type_ = Type::make_error_type();
2771       return this->type_;
2772     }
2773
2774   this->seen_ = true;
2775
2776   Type* ret = nc->type();
2777
2778   if (ret != NULL)
2779     {
2780       this->seen_ = false;
2781       return ret;
2782     }
2783
2784   // During parsing, a named constant may have a NULL type, but we
2785   // must not return a NULL type here.
2786   ret = nc->expr()->type();
2787
2788   this->seen_ = false;
2789
2790   return ret;
2791 }
2792
2793 // Set the type of the const reference.
2794
2795 void
2796 Const_expression::do_determine_type(const Type_context* context)
2797 {
2798   Type* ctype = this->constant_->const_value()->type();
2799   Type* cetype = (ctype != NULL
2800                   ? ctype
2801                   : this->constant_->const_value()->expr()->type());
2802   if (ctype != NULL && !ctype->is_abstract())
2803     ;
2804   else if (context->type != NULL
2805            && (context->type->integer_type() != NULL
2806                || context->type->float_type() != NULL
2807                || context->type->complex_type() != NULL)
2808            && (cetype->integer_type() != NULL
2809                || cetype->float_type() != NULL
2810                || cetype->complex_type() != NULL))
2811     this->type_ = context->type;
2812   else if (context->type != NULL
2813            && context->type->is_string_type()
2814            && cetype->is_string_type())
2815     this->type_ = context->type;
2816   else if (context->type != NULL
2817            && context->type->is_boolean_type()
2818            && cetype->is_boolean_type())
2819     this->type_ = context->type;
2820   else if (!context->may_be_abstract)
2821     {
2822       if (cetype->is_abstract())
2823         cetype = cetype->make_non_abstract_type();
2824       this->type_ = cetype;
2825     }
2826 }
2827
2828 // Check for a loop in which the initializer of a constant refers to
2829 // the constant itself.
2830
2831 void
2832 Const_expression::check_for_init_loop()
2833 {
2834   if (this->type_ != NULL && this->type_->is_error())
2835     return;
2836
2837   if (this->seen_)
2838     {
2839       this->report_error(_("constant refers to itself"));
2840       this->type_ = Type::make_error_type();
2841       return;
2842     }
2843
2844   Expression* init = this->constant_->const_value()->expr();
2845   Find_named_object find_named_object(this->constant_);
2846
2847   this->seen_ = true;
2848   Expression::traverse(&init, &find_named_object);
2849   this->seen_ = false;
2850
2851   if (find_named_object.found())
2852     {
2853       if (this->type_ == NULL || !this->type_->is_error())
2854         {
2855           this->report_error(_("constant refers to itself"));
2856           this->type_ = Type::make_error_type();
2857         }
2858       return;
2859     }
2860 }
2861
2862 // Check types of a const reference.
2863
2864 void
2865 Const_expression::do_check_types(Gogo*)
2866 {
2867   if (this->type_ != NULL && this->type_->is_error())
2868     return;
2869
2870   this->check_for_init_loop();
2871
2872   if (this->type_ == NULL || this->type_->is_abstract())
2873     return;
2874
2875   // Check for integer overflow.
2876   if (this->type_->integer_type() != NULL)
2877     {
2878       mpz_t ival;
2879       mpz_init(ival);
2880       Type* dummy;
2881       if (!this->integer_constant_value(true, ival, &dummy))
2882         {
2883           mpfr_t fval;
2884           mpfr_init(fval);
2885           Expression* cexpr = this->constant_->const_value()->expr();
2886           if (cexpr->float_constant_value(fval, &dummy))
2887             {
2888               if (!mpfr_integer_p(fval))
2889                 this->report_error(_("floating point constant "
2890                                      "truncated to integer"));
2891               else
2892                 {
2893                   mpfr_get_z(ival, fval, GMP_RNDN);
2894                   Integer_expression::check_constant(ival, this->type_,
2895                                                      this->location());
2896                 }
2897             }
2898           mpfr_clear(fval);
2899         }
2900       mpz_clear(ival);
2901     }
2902 }
2903
2904 // Return a tree for the const reference.
2905
2906 tree
2907 Const_expression::do_get_tree(Translate_context* context)
2908 {
2909   Gogo* gogo = context->gogo();
2910   tree type_tree;
2911   if (this->type_ == NULL)
2912     type_tree = NULL_TREE;
2913   else
2914     {
2915       type_tree = type_to_tree(this->type_->get_backend(gogo));
2916       if (type_tree == error_mark_node)
2917         return error_mark_node;
2918     }
2919
2920   // If the type has been set for this expression, but the underlying
2921   // object is an abstract int or float, we try to get the abstract
2922   // value.  Otherwise we may lose something in the conversion.
2923   if (this->type_ != NULL
2924       && (this->constant_->const_value()->type() == NULL
2925           || this->constant_->const_value()->type()->is_abstract()))
2926     {
2927       Expression* expr = this->constant_->const_value()->expr();
2928       mpz_t ival;
2929       mpz_init(ival);
2930       Type* t;
2931       if (expr->integer_constant_value(true, ival, &t))
2932         {
2933           tree ret = Expression::integer_constant_tree(ival, type_tree);
2934           mpz_clear(ival);
2935           return ret;
2936         }
2937       mpz_clear(ival);
2938
2939       mpfr_t fval;
2940       mpfr_init(fval);
2941       if (expr->float_constant_value(fval, &t))
2942         {
2943           tree ret = Expression::float_constant_tree(fval, type_tree);
2944           mpfr_clear(fval);
2945           return ret;
2946         }
2947
2948       mpfr_t imag;
2949       mpfr_init(imag);
2950       if (expr->complex_constant_value(fval, imag, &t))
2951         {
2952           tree ret = Expression::complex_constant_tree(fval, imag, type_tree);
2953           mpfr_clear(fval);
2954           mpfr_clear(imag);
2955           return ret;
2956         }
2957       mpfr_clear(imag);
2958       mpfr_clear(fval);
2959     }
2960
2961   tree const_tree = this->constant_->get_tree(gogo, context->function());
2962   if (this->type_ == NULL
2963       || const_tree == error_mark_node
2964       || TREE_TYPE(const_tree) == error_mark_node)
2965     return const_tree;
2966
2967   tree ret;
2968   if (TYPE_MAIN_VARIANT(type_tree) == TYPE_MAIN_VARIANT(TREE_TYPE(const_tree)))
2969     ret = fold_convert(type_tree, const_tree);
2970   else if (TREE_CODE(type_tree) == INTEGER_TYPE)
2971     ret = fold(convert_to_integer(type_tree, const_tree));
2972   else if (TREE_CODE(type_tree) == REAL_TYPE)
2973     ret = fold(convert_to_real(type_tree, const_tree));
2974   else if (TREE_CODE(type_tree) == COMPLEX_TYPE)
2975     ret = fold(convert_to_complex(type_tree, const_tree));
2976   else
2977     go_unreachable();
2978   return ret;
2979 }
2980
2981 // Dump ast representation for constant expression.
2982
2983 void
2984 Const_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
2985 {
2986   ast_dump_context->ostream() << this->constant_->name();
2987 }
2988
2989 // Make a reference to a constant in an expression.
2990
2991 Expression*
2992 Expression::make_const_reference(Named_object* constant,
2993                                  Location location)
2994 {
2995   return new Const_expression(constant, location);
2996 }
2997
2998 // Find a named object in an expression.
2999
3000 int
3001 Find_named_object::expression(Expression** pexpr)
3002 {
3003   switch ((*pexpr)->classification())
3004     {
3005     case Expression::EXPRESSION_CONST_REFERENCE:
3006       {
3007         Const_expression* ce = static_cast<Const_expression*>(*pexpr);
3008         if (ce->named_object() == this->no_)
3009           break;
3010
3011         // We need to check a constant initializer explicitly, as
3012         // loops here will not be caught by the loop checking for
3013         // variable initializers.
3014         ce->check_for_init_loop();
3015
3016         return TRAVERSE_CONTINUE;
3017       }
3018
3019     case Expression::EXPRESSION_VAR_REFERENCE:
3020       if ((*pexpr)->var_expression()->named_object() == this->no_)
3021         break;
3022       return TRAVERSE_CONTINUE;
3023     case Expression::EXPRESSION_FUNC_REFERENCE:
3024       if ((*pexpr)->func_expression()->named_object() == this->no_)
3025         break;
3026       return TRAVERSE_CONTINUE;
3027     default:
3028       return TRAVERSE_CONTINUE;
3029     }
3030   this->found_ = true;
3031   return TRAVERSE_EXIT;
3032 }
3033
3034 // The nil value.
3035
3036 class Nil_expression : public Expression
3037 {
3038  public:
3039   Nil_expression(Location location)
3040     : Expression(EXPRESSION_NIL, location)
3041   { }
3042
3043   static Expression*
3044   do_import(Import*);
3045
3046  protected:
3047   bool
3048   do_is_constant() const
3049   { return true; }
3050
3051   Type*
3052   do_type()
3053   { return Type::make_nil_type(); }
3054
3055   void
3056   do_determine_type(const Type_context*)
3057   { }
3058
3059   Expression*
3060   do_copy()
3061   { return this; }
3062
3063   tree
3064   do_get_tree(Translate_context*)
3065   { return null_pointer_node; }
3066
3067   void
3068   do_export(Export* exp) const
3069   { exp->write_c_string("nil"); }
3070
3071   void
3072   do_dump_expression(Ast_dump_context* ast_dump_context) const
3073   { ast_dump_context->ostream() << "nil"; }
3074 };
3075
3076 // Import a nil expression.
3077
3078 Expression*
3079 Nil_expression::do_import(Import* imp)
3080 {
3081   imp->require_c_string("nil");
3082   return Expression::make_nil(imp->location());
3083 }
3084
3085 // Make a nil expression.
3086
3087 Expression*
3088 Expression::make_nil(Location location)
3089 {
3090   return new Nil_expression(location);
3091 }
3092
3093 // The value of the predeclared constant iota.  This is little more
3094 // than a marker.  This will be lowered to an integer in
3095 // Const_expression::do_lower, which is where we know the value that
3096 // it should have.
3097
3098 class Iota_expression : public Parser_expression
3099 {
3100  public:
3101   Iota_expression(Location location)
3102     : Parser_expression(EXPRESSION_IOTA, location)
3103   { }
3104
3105  protected:
3106   Expression*
3107   do_lower(Gogo*, Named_object*, Statement_inserter*, int)
3108   { go_unreachable(); }
3109
3110   // There should only ever be one of these.
3111   Expression*
3112   do_copy()
3113   { go_unreachable(); }
3114   
3115   void
3116   do_dump_expression(Ast_dump_context* ast_dump_context) const
3117   { ast_dump_context->ostream() << "iota"; } 
3118 };
3119
3120 // Make an iota expression.  This is only called for one case: the
3121 // value of the predeclared constant iota.
3122
3123 Expression*
3124 Expression::make_iota()
3125 {
3126   static Iota_expression iota_expression(Linemap::unknown_location());
3127   return &iota_expression;
3128 }
3129
3130 // A type conversion expression.
3131
3132 class Type_conversion_expression : public Expression
3133 {
3134  public:
3135   Type_conversion_expression(Type* type, Expression* expr,
3136                              Location location)
3137     : Expression(EXPRESSION_CONVERSION, location),
3138       type_(type), expr_(expr), may_convert_function_types_(false)
3139   { }
3140
3141   // Return the type to which we are converting.
3142   Type*
3143   type() const
3144   { return this->type_; }
3145
3146   // Return the expression which we are converting.
3147   Expression*
3148   expr() const
3149   { return this->expr_; }
3150
3151   // Permit converting from one function type to another.  This is
3152   // used internally for method expressions.
3153   void
3154   set_may_convert_function_types()
3155   {
3156     this->may_convert_function_types_ = true;
3157   }
3158
3159   // Import a type conversion expression.
3160   static Expression*
3161   do_import(Import*);
3162
3163  protected:
3164   int
3165   do_traverse(Traverse* traverse);
3166
3167   Expression*
3168   do_lower(Gogo*, Named_object*, Statement_inserter*, int);
3169
3170   bool
3171   do_is_constant() const
3172   { return this->expr_->is_constant(); }
3173
3174   bool
3175   do_integer_constant_value(bool, mpz_t, Type**) const;
3176
3177   bool
3178   do_float_constant_value(mpfr_t, Type**) const;
3179
3180   bool
3181   do_complex_constant_value(mpfr_t, mpfr_t, Type**) const;
3182
3183   bool
3184   do_string_constant_value(std::string*) const;
3185
3186   Type*
3187   do_type()
3188   { return this->type_; }
3189
3190   void
3191   do_determine_type(const Type_context*)
3192   {
3193     Type_context subcontext(this->type_, false);
3194     this->expr_->determine_type(&subcontext);
3195   }
3196
3197   void
3198   do_check_types(Gogo*);
3199
3200   Expression*
3201   do_copy()
3202   {
3203     return new Type_conversion_expression(this->type_, this->expr_->copy(),
3204                                           this->location());
3205   }
3206
3207   tree
3208   do_get_tree(Translate_context* context);
3209
3210   void
3211   do_export(Export*) const;
3212
3213   void
3214   do_dump_expression(Ast_dump_context*) const;
3215
3216  private:
3217   // The type to convert to.
3218   Type* type_;
3219   // The expression to convert.
3220   Expression* expr_;
3221   // True if this is permitted to convert function types.  This is
3222   // used internally for method expressions.
3223   bool may_convert_function_types_;
3224 };
3225
3226 // Traversal.
3227
3228 int
3229 Type_conversion_expression::do_traverse(Traverse* traverse)
3230 {
3231   if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT
3232       || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
3233     return TRAVERSE_EXIT;
3234   return TRAVERSE_CONTINUE;
3235 }
3236
3237 // Convert to a constant at lowering time.
3238
3239 Expression*
3240 Type_conversion_expression::do_lower(Gogo*, Named_object*,
3241                                      Statement_inserter*, int)
3242 {
3243   Type* type = this->type_;
3244   Expression* val = this->expr_;
3245   Location location = this->location();
3246
3247   if (type->integer_type() != NULL)
3248     {
3249       mpz_t ival;
3250       mpz_init(ival);
3251       Type* dummy;
3252       if (val->integer_constant_value(false, ival, &dummy))
3253         {
3254           if (!Integer_expression::check_constant(ival, type, location))
3255             mpz_set_ui(ival, 0);
3256           Expression* ret = Expression::make_integer(&ival, type, location);
3257           mpz_clear(ival);
3258           return ret;
3259         }
3260
3261       mpfr_t fval;
3262       mpfr_init(fval);
3263       if (val->float_constant_value(fval, &dummy))
3264         {
3265           if (!mpfr_integer_p(fval))
3266             {
3267               error_at(location,
3268                        "floating point constant truncated to integer");
3269               return Expression::make_error(location);
3270             }
3271           mpfr_get_z(ival, fval, GMP_RNDN);
3272           if (!Integer_expression::check_constant(ival, type, location))
3273             mpz_set_ui(ival, 0);
3274           Expression* ret = Expression::make_integer(&ival, type, location);
3275           mpfr_clear(fval);
3276           mpz_clear(ival);
3277           return ret;
3278         }
3279       mpfr_clear(fval);
3280       mpz_clear(ival);
3281     }
3282
3283   if (type->float_type() != NULL)
3284     {
3285       mpfr_t fval;
3286       mpfr_init(fval);
3287       Type* dummy;
3288       if (val->float_constant_value(fval, &dummy))
3289         {
3290           if (!Float_expression::check_constant(fval, type, location))
3291             mpfr_set_ui(fval, 0, GMP_RNDN);
3292           Float_expression::constrain_float(fval, type);
3293           Expression *ret = Expression::make_float(&fval, type, location);
3294           mpfr_clear(fval);
3295           return ret;
3296         }
3297       mpfr_clear(fval);
3298     }
3299
3300   if (type->complex_type() != NULL)
3301     {
3302       mpfr_t real;
3303       mpfr_t imag;
3304       mpfr_init(real);
3305       mpfr_init(imag);
3306       Type* dummy;
3307       if (val->complex_constant_value(real, imag, &dummy))
3308         {
3309           if (!Complex_expression::check_constant(real, imag, type, location))
3310             {
3311               mpfr_set_ui(real, 0, GMP_RNDN);
3312               mpfr_set_ui(imag, 0, GMP_RNDN);
3313             }
3314           Complex_expression::constrain_complex(real, imag, type);
3315           Expression* ret = Expression::make_complex(&real, &imag, type,
3316                                                      location);
3317           mpfr_clear(real);
3318           mpfr_clear(imag);
3319           return ret;
3320         }
3321       mpfr_clear(real);
3322       mpfr_clear(imag);
3323     }
3324
3325   if (type->is_slice_type())
3326     {
3327       Type* element_type = type->array_type()->element_type()->forwarded();
3328       bool is_byte = element_type == Type::lookup_integer_type("uint8");
3329       bool is_int = element_type == Type::lookup_integer_type("int");
3330       if (is_byte || is_int)
3331         {
3332           std::string s;
3333           if (val->string_constant_value(&s))
3334             {
3335               Expression_list* vals = new Expression_list();
3336               if (is_byte)
3337                 {
3338                   for (std::string::const_iterator p = s.begin();
3339                        p != s.end();
3340                        p++)
3341                     {
3342                       mpz_t val;
3343                       mpz_init_set_ui(val, static_cast<unsigned char>(*p));
3344                       Expression* v = Expression::make_integer(&val,
3345                                                                element_type,
3346                                                                location);
3347                       vals->push_back(v);
3348                       mpz_clear(val);
3349                     }
3350                 }
3351               else
3352                 {
3353                   const char *p = s.data();
3354                   const char *pend = s.data() + s.length();
3355                   while (p < pend)
3356                     {
3357                       unsigned int c;
3358                       int adv = Lex::fetch_char(p, &c);
3359                       if (adv == 0)
3360                         {
3361                           warning_at(this->location(), 0,
3362                                      "invalid UTF-8 encoding");
3363                           adv = 1;
3364                         }
3365                       p += adv;
3366                       mpz_t val;
3367                       mpz_init_set_ui(val, c);
3368                       Expression* v = Expression::make_integer(&val,
3369                                                                element_type,
3370                                                                location);
3371                       vals->push_back(v);
3372                       mpz_clear(val);
3373                     }
3374                 }
3375
3376               return Expression::make_slice_composite_literal(type, vals,
3377                                                               location);
3378             }
3379         }
3380     }
3381
3382   return this;
3383 }
3384
3385 // Return the constant integer value if there is one.
3386
3387 bool
3388 Type_conversion_expression::do_integer_constant_value(bool iota_is_constant,
3389                                                       mpz_t val,
3390                                                       Type** ptype) const
3391 {
3392   if (this->type_->integer_type() == NULL)
3393     return false;
3394
3395   mpz_t ival;
3396   mpz_init(ival);
3397   Type* dummy;
3398   if (this->expr_->integer_constant_value(iota_is_constant, ival, &dummy))
3399     {
3400       if (!Integer_expression::check_constant(ival, this->type_,
3401                                               this->location()))
3402         {
3403           mpz_clear(ival);
3404           return false;
3405         }
3406       mpz_set(val, ival);
3407       mpz_clear(ival);
3408       *ptype = this->type_;
3409       return true;
3410     }
3411   mpz_clear(ival);
3412
3413   mpfr_t fval;
3414   mpfr_init(fval);
3415   if (this->expr_->float_constant_value(fval, &dummy))
3416     {
3417       mpfr_get_z(val, fval, GMP_RNDN);
3418       mpfr_clear(fval);
3419       if (!Integer_expression::check_constant(val, this->type_,
3420                                               this->location()))
3421         return false;
3422       *ptype = this->type_;
3423       return true;
3424     }
3425   mpfr_clear(fval);
3426
3427   return false;
3428 }
3429
3430 // Return the constant floating point value if there is one.
3431
3432 bool
3433 Type_conversion_expression::do_float_constant_value(mpfr_t val,
3434                                                     Type** ptype) const
3435 {
3436   if (this->type_->float_type() == NULL)
3437     return false;
3438
3439   mpfr_t fval;
3440   mpfr_init(fval);
3441   Type* dummy;
3442   if (this->expr_->float_constant_value(fval, &dummy))
3443     {
3444       if (!Float_expression::check_constant(fval, this->type_,
3445                                             this->location()))
3446         {
3447           mpfr_clear(fval);
3448           return false;
3449         }
3450       mpfr_set(val, fval, GMP_RNDN);
3451       mpfr_clear(fval);
3452       Float_expression::constrain_float(val, this->type_);
3453       *ptype = this->type_;
3454       return true;
3455     }
3456   mpfr_clear(fval);
3457
3458   return false;
3459 }
3460
3461 // Return the constant complex value if there is one.
3462
3463 bool
3464 Type_conversion_expression::do_complex_constant_value(mpfr_t real,
3465                                                       mpfr_t imag,
3466                                                       Type **ptype) const
3467 {
3468   if (this->type_->complex_type() == NULL)
3469     return false;
3470
3471   mpfr_t rval;
3472   mpfr_t ival;
3473   mpfr_init(rval);
3474   mpfr_init(ival);
3475   Type* dummy;
3476   if (this->expr_->complex_constant_value(rval, ival, &dummy))
3477     {
3478       if (!Complex_expression::check_constant(rval, ival, this->type_,
3479                                               this->location()))
3480         {
3481           mpfr_clear(rval);
3482           mpfr_clear(ival);
3483           return false;
3484         }
3485       mpfr_set(real, rval, GMP_RNDN);
3486       mpfr_set(imag, ival, GMP_RNDN);
3487       mpfr_clear(rval);
3488       mpfr_clear(ival);
3489       Complex_expression::constrain_complex(real, imag, this->type_);
3490       *ptype = this->type_;
3491       return true;
3492     }
3493   mpfr_clear(rval);
3494   mpfr_clear(ival);
3495
3496   return false;  
3497 }
3498
3499 // Return the constant string value if there is one.
3500
3501 bool
3502 Type_conversion_expression::do_string_constant_value(std::string* val) const
3503 {
3504   if (this->type_->is_string_type()
3505       && this->expr_->type()->integer_type() != NULL)
3506     {
3507       mpz_t ival;
3508       mpz_init(ival);
3509       Type* dummy;
3510       if (this->expr_->integer_constant_value(false, ival, &dummy))
3511         {
3512           unsigned long ulval = mpz_get_ui(ival);
3513           if (mpz_cmp_ui(ival, ulval) == 0)
3514             {
3515               Lex::append_char(ulval, true, val, this->location());
3516               mpz_clear(ival);
3517               return true;
3518             }
3519         }
3520       mpz_clear(ival);
3521     }
3522
3523   // FIXME: Could handle conversion from const []int here.
3524
3525   return false;
3526 }
3527
3528 // Check that types are convertible.
3529
3530 void
3531 Type_conversion_expression::do_check_types(Gogo*)
3532 {
3533   Type* type = this->type_;
3534   Type* expr_type = this->expr_->type();
3535   std::string reason;
3536
3537   if (type->is_error() || expr_type->is_error())
3538     {
3539       this->set_is_error();
3540       return;
3541     }
3542
3543   if (this->may_convert_function_types_
3544       && type->function_type() != NULL
3545       && expr_type->function_type() != NULL)
3546     return;
3547
3548   if (Type::are_convertible(type, expr_type, &reason))
3549     return;
3550
3551   error_at(this->location(), "%s", reason.c_str());
3552   this->set_is_error();
3553 }
3554
3555 // Get a tree for a type conversion.
3556
3557 tree
3558 Type_conversion_expression::do_get_tree(Translate_context* context)
3559 {
3560   Gogo* gogo = context->gogo();
3561   tree type_tree = type_to_tree(this->type_->get_backend(gogo));
3562   tree expr_tree = this->expr_->get_tree(context);
3563
3564   if (type_tree == error_mark_node
3565       || expr_tree == error_mark_node
3566       || TREE_TYPE(expr_tree) == error_mark_node)
3567     return error_mark_node;
3568
3569   if (TYPE_MAIN_VARIANT(type_tree) == TYPE_MAIN_VARIANT(TREE_TYPE(expr_tree)))
3570     return fold_convert(type_tree, expr_tree);
3571
3572   Type* type = this->type_;
3573   Type* expr_type = this->expr_->type();
3574   tree ret;
3575   if (type->interface_type() != NULL || expr_type->interface_type() != NULL)
3576     ret = Expression::convert_for_assignment(context, type, expr_type,
3577                                              expr_tree, this->location());
3578   else if (type->integer_type() != NULL)
3579     {
3580       if (expr_type->integer_type() != NULL
3581           || expr_type->float_type() != NULL
3582           || expr_type->is_unsafe_pointer_type())
3583         ret = fold(convert_to_integer(type_tree, expr_tree));
3584       else
3585         go_unreachable();
3586     }
3587   else if (type->float_type() != NULL)
3588     {
3589       if (expr_type->integer_type() != NULL
3590           || expr_type->float_type() != NULL)
3591         ret = fold(convert_to_real(type_tree, expr_tree));
3592       else
3593         go_unreachable();
3594     }
3595   else if (type->complex_type() != NULL)
3596     {
3597       if (expr_type->complex_type() != NULL)
3598         ret = fold(convert_to_complex(type_tree, expr_tree));
3599       else
3600         go_unreachable();
3601     }
3602   else if (type->is_string_type()
3603            && expr_type->integer_type() != NULL)
3604     {
3605       expr_tree = fold_convert(integer_type_node, expr_tree);
3606       if (host_integerp(expr_tree, 0))
3607         {
3608           HOST_WIDE_INT intval = tree_low_cst(expr_tree, 0);
3609           std::string s;
3610           Lex::append_char(intval, true, &s, this->location());
3611           Expression* se = Expression::make_string(s, this->location());
3612           return se->get_tree(context);
3613         }
3614
3615       static tree int_to_string_fndecl;
3616       ret = Gogo::call_builtin(&int_to_string_fndecl,
3617                                this->location(),
3618                                "__go_int_to_string",
3619                                1,
3620                                type_tree,
3621                                integer_type_node,
3622                                fold_convert(integer_type_node, expr_tree));
3623     }
3624   else if (type->is_string_type() && expr_type->is_slice_type())
3625     {
3626       if (!DECL_P(expr_tree))
3627         expr_tree = save_expr(expr_tree);
3628       Array_type* a = expr_type->array_type();
3629       Type* e = a->element_type()->forwarded();
3630       go_assert(e->integer_type() != NULL);
3631       tree valptr = fold_convert(const_ptr_type_node,
3632                                  a->value_pointer_tree(gogo, expr_tree));
3633       tree len = a->length_tree(gogo, expr_tree);
3634       len = fold_convert_loc(this->location().gcc_location(), integer_type_node,
3635                              len);
3636       if (e->integer_type()->is_unsigned()
3637           && e->integer_type()->bits() == 8)
3638         {
3639           static tree byte_array_to_string_fndecl;
3640           ret = Gogo::call_builtin(&byte_array_to_string_fndecl,
3641                                    this->location(),
3642                                    "__go_byte_array_to_string",
3643                                    2,
3644                                    type_tree,
3645                                    const_ptr_type_node,
3646                                    valptr,
3647                                    integer_type_node,
3648                                    len);
3649         }
3650       else
3651         {
3652           go_assert(e == Type::lookup_integer_type("int"));
3653           static tree int_array_to_string_fndecl;
3654           ret = Gogo::call_builtin(&int_array_to_string_fndecl,
3655                                    this->location(),
3656                                    "__go_int_array_to_string",
3657                                    2,
3658                                    type_tree,
3659                                    const_ptr_type_node,
3660                                    valptr,
3661                                    integer_type_node,
3662                                    len);
3663         }
3664     }
3665   else if (type->is_slice_type() && expr_type->is_string_type())
3666     {
3667       Type* e = type->array_type()->element_type()->forwarded();
3668       go_assert(e->integer_type() != NULL);
3669       if (e->integer_type()->is_unsigned()
3670           && e->integer_type()->bits() == 8)
3671         {
3672           static tree string_to_byte_array_fndecl;
3673           ret = Gogo::call_builtin(&string_to_byte_array_fndecl,
3674                                    this->location(),
3675                                    "__go_string_to_byte_array",
3676                                    1,
3677                                    type_tree,
3678                                    TREE_TYPE(expr_tree),
3679                                    expr_tree);
3680         }
3681       else
3682         {
3683           go_assert(e == Type::lookup_integer_type("int"));
3684           static tree string_to_int_array_fndecl;
3685           ret = Gogo::call_builtin(&string_to_int_array_fndecl,
3686                                    this->location(),
3687                                    "__go_string_to_int_array",
3688                                    1,
3689                                    type_tree,
3690                                    TREE_TYPE(expr_tree),
3691                                    expr_tree);
3692         }
3693     }
3694   else if ((type->is_unsafe_pointer_type()
3695             && expr_type->points_to() != NULL)
3696            || (expr_type->is_unsafe_pointer_type()
3697                && type->points_to() != NULL))
3698     ret = fold_convert(type_tree, expr_tree);
3699   else if (type->is_unsafe_pointer_type()
3700            && expr_type->integer_type() != NULL)
3701     ret = convert_to_pointer(type_tree, expr_tree);
3702   else if (this->may_convert_function_types_
3703            && type->function_type() != NULL
3704            && expr_type->function_type() != NULL)
3705     ret = fold_convert_loc(this->location().gcc_location(), type_tree,
3706                            expr_tree);
3707   else
3708     ret = Expression::convert_for_assignment(context, type, expr_type,
3709                                              expr_tree, this->location());
3710
3711   return ret;
3712 }
3713
3714 // Output a type conversion in a constant expression.
3715
3716 void
3717 Type_conversion_expression::do_export(Export* exp) const
3718 {
3719   exp->write_c_string("convert(");
3720   exp->write_type(this->type_);
3721   exp->write_c_string(", ");
3722   this->expr_->export_expression(exp);
3723   exp->write_c_string(")");
3724 }
3725
3726 // Import a type conversion or a struct construction.
3727
3728 Expression*
3729 Type_conversion_expression::do_import(Import* imp)
3730 {
3731   imp->require_c_string("convert(");
3732   Type* type = imp->read_type();
3733   imp->require_c_string(", ");
3734   Expression* val = Expression::import_expression(imp);
3735   imp->require_c_string(")");
3736   return Expression::make_cast(type, val, imp->location());
3737 }
3738
3739 // Dump ast representation for a type conversion expression.
3740
3741 void
3742 Type_conversion_expression::do_dump_expression(
3743     Ast_dump_context* ast_dump_context) const
3744 {
3745   ast_dump_context->dump_type(this->type_);
3746   ast_dump_context->ostream() << "(";
3747   ast_dump_context->dump_expression(this->expr_);
3748   ast_dump_context->ostream() << ") ";
3749 }
3750
3751 // Make a type cast expression.
3752
3753 Expression*
3754 Expression::make_cast(Type* type, Expression* val, Location location)
3755 {
3756   if (type->is_error_type() || val->is_error_expression())
3757     return Expression::make_error(location);
3758   return new Type_conversion_expression(type, val, location);
3759 }
3760
3761 // An unsafe type conversion, used to pass values to builtin functions.
3762
3763 class Unsafe_type_conversion_expression : public Expression
3764 {
3765  public:
3766   Unsafe_type_conversion_expression(Type* type, Expression* expr,
3767                                     Location location)
3768     : Expression(EXPRESSION_UNSAFE_CONVERSION, location),
3769       type_(type), expr_(expr)
3770   { }
3771
3772  protected:
3773   int
3774   do_traverse(Traverse* traverse);
3775
3776   Type*
3777   do_type()
3778   { return this->type_; }
3779
3780   void
3781   do_determine_type(const Type_context*)
3782   { this->expr_->determine_type_no_context(); }
3783
3784   Expression*
3785   do_copy()
3786   {
3787     return new Unsafe_type_conversion_expression(this->type_,
3788                                                  this->expr_->copy(),
3789                                                  this->location());
3790   }
3791
3792   tree
3793   do_get_tree(Translate_context*);
3794
3795   void
3796   do_dump_expression(Ast_dump_context*) const;
3797
3798  private:
3799   // The type to convert to.
3800   Type* type_;
3801   // The expression to convert.
3802   Expression* expr_;
3803 };
3804
3805 // Traversal.
3806
3807 int
3808 Unsafe_type_conversion_expression::do_traverse(Traverse* traverse)
3809 {
3810   if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT
3811       || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
3812     return TRAVERSE_EXIT;
3813   return TRAVERSE_CONTINUE;
3814 }
3815
3816 // Convert to backend representation.
3817
3818 tree
3819 Unsafe_type_conversion_expression::do_get_tree(Translate_context* context)
3820 {
3821   // We are only called for a limited number of cases.
3822
3823   Type* t = this->type_;
3824   Type* et = this->expr_->type();
3825
3826   tree type_tree = type_to_tree(this->type_->get_backend(context->gogo()));
3827   tree expr_tree = this->expr_->get_tree(context);
3828   if (type_tree == error_mark_node || expr_tree == error_mark_node)
3829     return error_mark_node;
3830
3831   Location loc = this->location();
3832
3833   bool use_view_convert = false;
3834   if (t->is_slice_type())
3835     {
3836       go_assert(et->is_slice_type());
3837       use_view_convert = true;
3838     }
3839   else if (t->map_type() != NULL)
3840     go_assert(et->map_type() != NULL);
3841   else if (t->channel_type() != NULL)
3842     go_assert(et->channel_type() != NULL);
3843   else if (t->points_to() != NULL && t->points_to()->channel_type() != NULL)
3844     go_assert((et->points_to() != NULL
3845                 && et->points_to()->channel_type() != NULL)
3846                || et->is_nil_type());
3847   else if (t->points_to() != NULL)
3848     go_assert(et->points_to() != NULL || et->is_nil_type());
3849   else if (et->is_unsafe_pointer_type())
3850     go_assert(t->points_to() != NULL);
3851   else if (t->interface_type() != NULL && !t->interface_type()->is_empty())
3852     {
3853       go_assert(et->interface_type() != NULL
3854                  && !et->interface_type()->is_empty());
3855       use_view_convert = true;
3856     }
3857   else if (t->interface_type() != NULL && t->interface_type()->is_empty())
3858     {
3859       go_assert(et->interface_type() != NULL
3860                  && et->interface_type()->is_empty());
3861       use_view_convert = true;
3862     }
3863   else if (t->integer_type() != NULL)
3864     {
3865       go_assert(et->is_boolean_type()
3866                  || et->integer_type() != NULL
3867                  || et->function_type() != NULL
3868                  || et->points_to() != NULL
3869                  || et->map_type() != NULL
3870                  || et->channel_type() != NULL);
3871       return convert_to_integer(type_tree, expr_tree);
3872     }
3873   else
3874     go_unreachable();
3875
3876   if (use_view_convert)
3877     return fold_build1_loc(loc.gcc_location(), VIEW_CONVERT_EXPR, type_tree,
3878                            expr_tree);
3879   else
3880     return fold_convert_loc(loc.gcc_location(), type_tree, expr_tree);
3881 }
3882
3883 // Dump ast representation for an unsafe type conversion expression.
3884
3885 void
3886 Unsafe_type_conversion_expression::do_dump_expression(
3887     Ast_dump_context* ast_dump_context) const
3888 {
3889   ast_dump_context->dump_type(this->type_);
3890   ast_dump_context->ostream() << "(";
3891   ast_dump_context->dump_expression(this->expr_);
3892   ast_dump_context->ostream() << ") ";
3893 }
3894
3895 // Make an unsafe type conversion expression.
3896
3897 Expression*
3898 Expression::make_unsafe_cast(Type* type, Expression* expr,
3899                              Location location)
3900 {
3901   return new Unsafe_type_conversion_expression(type, expr, location);
3902 }
3903
3904 // Unary expressions.
3905
3906 class Unary_expression : public Expression
3907 {
3908  public:
3909   Unary_expression(Operator op, Expression* expr, Location location)
3910     : Expression(EXPRESSION_UNARY, location),
3911       op_(op), escapes_(true), create_temp_(false), expr_(expr)
3912   { }
3913
3914   // Return the operator.
3915   Operator
3916   op() const
3917   { return this->op_; }
3918
3919   // Return the operand.
3920   Expression*
3921   operand() const
3922   { return this->expr_; }
3923
3924   // Record that an address expression does not escape.
3925   void
3926   set_does_not_escape()
3927   {
3928     go_assert(this->op_ == OPERATOR_AND);
3929     this->escapes_ = false;
3930   }
3931
3932   // Record that this is an address expression which should create a
3933   // temporary variable if necessary.  This is used for method calls.
3934   void
3935   set_create_temp()
3936   {
3937     go_assert(this->op_ == OPERATOR_AND);
3938     this->create_temp_ = true;
3939   }
3940
3941   // Apply unary opcode OP to UVAL, setting VAL.  Return true if this
3942   // could be done, false if not.
3943   static bool
3944   eval_integer(Operator op, Type* utype, mpz_t uval, mpz_t val,
3945                Location);
3946
3947   // Apply unary opcode OP to UVAL, setting VAL.  Return true if this
3948   // could be done, false if not.
3949   static bool
3950   eval_float(Operator op, mpfr_t uval, mpfr_t val);
3951
3952   // Apply unary opcode OP to UREAL/UIMAG, setting REAL/IMAG.  Return
3953   // true if this could be done, false if not.
3954   static bool
3955   eval_complex(Operator op, mpfr_t ureal, mpfr_t uimag, mpfr_t real,
3956                mpfr_t imag);
3957
3958   static Expression*
3959   do_import(Import*);
3960
3961  protected:
3962   int
3963   do_traverse(Traverse* traverse)
3964   { return Expression::traverse(&this->expr_, traverse); }
3965
3966   Expression*
3967   do_lower(Gogo*, Named_object*, Statement_inserter*, int);
3968
3969   bool
3970   do_is_constant() const;
3971
3972   bool
3973   do_integer_constant_value(bool, mpz_t, Type**) const;
3974
3975   bool
3976   do_float_constant_value(mpfr_t, Type**) const;
3977
3978   bool
3979   do_complex_constant_value(mpfr_t, mpfr_t, Type**) const;
3980
3981   Type*
3982   do_type();
3983
3984   void
3985   do_determine_type(const Type_context*);
3986
3987   void
3988   do_check_types(Gogo*);
3989
3990   Expression*
3991   do_copy()
3992   {
3993     return Expression::make_unary(this->op_, this->expr_->copy(),
3994                                   this->location());
3995   }
3996
3997   bool
3998   do_must_eval_subexpressions_in_order(int*) const
3999   { return this->op_ == OPERATOR_MULT; }
4000
4001   bool
4002   do_is_addressable() const
4003   { return this->op_ == OPERATOR_MULT; }
4004
4005   tree
4006   do_get_tree(Translate_context*);
4007
4008   void
4009   do_export(Export*) const;
4010
4011   void
4012   do_dump_expression(Ast_dump_context*) const;
4013
4014  private:
4015   // The unary operator to apply.
4016   Operator op_;
4017   // Normally true.  False if this is an address expression which does
4018   // not escape the current function.
4019   bool escapes_;
4020   // True if this is an address expression which should create a
4021   // temporary variable if necessary.
4022   bool create_temp_;
4023   // The operand.
4024   Expression* expr_;
4025 };
4026
4027 // If we are taking the address of a composite literal, and the
4028 // contents are not constant, then we want to make a heap composite
4029 // instead.
4030
4031 Expression*
4032 Unary_expression::do_lower(Gogo*, Named_object*, Statement_inserter*, int)
4033 {
4034   Location loc = this->location();
4035   Operator op = this->op_;
4036   Expression* expr = this->expr_;
4037
4038   if (op == OPERATOR_MULT && expr->is_type_expression())
4039     return Expression::make_type(Type::make_pointer_type(expr->type()), loc);
4040
4041   // *&x simplifies to x.  *(*T)(unsafe.Pointer)(&x) does not require
4042   // moving x to the heap.  FIXME: Is it worth doing a real escape
4043   // analysis here?  This case is found in math/unsafe.go and is
4044   // therefore worth special casing.
4045   if (op == OPERATOR_MULT)
4046     {
4047       Expression* e = expr;
4048       while (e->classification() == EXPRESSION_CONVERSION)
4049         {
4050           Type_conversion_expression* te
4051             = static_cast<Type_conversion_expression*>(e);
4052           e = te->expr();
4053         }
4054
4055       if (e->classification() == EXPRESSION_UNARY)
4056         {
4057           Unary_expression* ue = static_cast<Unary_expression*>(e);
4058           if (ue->op_ == OPERATOR_AND)
4059             {
4060               if (e == expr)
4061                 {
4062                   // *&x == x.
4063                   return ue->expr_;
4064                 }
4065               ue->set_does_not_escape();
4066             }
4067         }
4068     }
4069
4070   // Catching an invalid indirection of unsafe.Pointer here avoid
4071   // having to deal with TYPE_VOID in other places.
4072   if (op == OPERATOR_MULT && expr->type()->is_unsafe_pointer_type())
4073     {
4074       error_at(this->location(), "invalid indirect of %<unsafe.Pointer%>");
4075       return Expression::make_error(this->location());
4076     }
4077
4078   if (op == OPERATOR_PLUS || op == OPERATOR_MINUS
4079       || op == OPERATOR_NOT || op == OPERATOR_XOR)
4080     {
4081       Expression* ret = NULL;
4082
4083       mpz_t eval;
4084       mpz_init(eval);
4085       Type* etype;
4086       if (expr->integer_constant_value(false, eval, &etype))
4087         {
4088           mpz_t val;
4089           mpz_init(val);
4090           if (Unary_expression::eval_integer(op, etype, eval, val, loc))
4091             ret = Expression::make_integer(&val, etype, loc);
4092           mpz_clear(val);
4093         }
4094       mpz_clear(eval);
4095       if (ret != NULL)
4096         return ret;
4097
4098       if (op == OPERATOR_PLUS || op == OPERATOR_MINUS)
4099         {
4100           mpfr_t fval;
4101           mpfr_init(fval);
4102           Type* ftype;
4103           if (expr->float_constant_value(fval, &ftype))
4104             {
4105               mpfr_t val;
4106               mpfr_init(val);
4107               if (Unary_expression::eval_float(op, fval, val))
4108                 ret = Expression::make_float(&val, ftype, loc);
4109               mpfr_clear(val);
4110             }
4111           if (ret != NULL)
4112             {
4113               mpfr_clear(fval);
4114               return ret;
4115             }
4116
4117           mpfr_t ival;
4118           mpfr_init(ival);
4119           if (expr->complex_constant_value(fval, ival, &ftype))
4120             {
4121               mpfr_t real;
4122               mpfr_t imag;
4123               mpfr_init(real);
4124               mpfr_init(imag);