OSDN Git Service

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