]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/go/gofrontend/expressions.cc
Factor unrelated declarations out of tree.h.
[thirdparty/gcc.git] / gcc / go / gofrontend / expressions.cc
CommitLineData
7a938933
ILT
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
52556f04
ILT
9#include <algorithm>
10
7a938933
ILT
11#include "toplev.h"
12#include "intl.h"
13#include "tree.h"
d8a2d370
DN
14#include "stringpool.h"
15#include "stor-layout.h"
7a938933 16#include "gimple.h"
6e022c86 17#include "gimplify.h"
7a938933
ILT
18#include "tree-iterator.h"
19#include "convert.h"
20#include "real.h"
21#include "realmpfr.h"
7a938933 22
7a938933
ILT
23#include "go-c.h"
24#include "gogo.h"
25#include "types.h"
26#include "export.h"
27#include "import.h"
28#include "statements.h"
29#include "lex.h"
3b8dffe7 30#include "runtime.h"
d56e6679 31#include "backend.h"
7a938933 32#include "expressions.h"
16c57fe2 33#include "ast-dump.h"
7a938933
ILT
34
35// Class Expression.
36
37Expression::Expression(Expression_classification classification,
8afa2bfb 38 Location location)
7a938933
ILT
39 : classification_(classification), location_(location)
40{
41}
42
43Expression::~Expression()
44{
45}
46
7a938933
ILT
47// Traverse the expressions.
48
49int
50Expression::traverse(Expression** pexpr, Traverse* traverse)
51{
52 Expression* expr = *pexpr;
53 if ((traverse->traverse_mask() & Traverse::traverse_expressions) != 0)
54 {
55 int t = traverse->expression(pexpr);
56 if (t == TRAVERSE_EXIT)
57 return TRAVERSE_EXIT;
58 else if (t == TRAVERSE_SKIP_COMPONENTS)
59 return TRAVERSE_CONTINUE;
60 }
61 return expr->do_traverse(traverse);
62}
63
64// Traverse subexpressions of this expression.
65
66int
67Expression::traverse_subexpressions(Traverse* traverse)
68{
69 return this->do_traverse(traverse);
70}
71
72// Default implementation for do_traverse for child classes.
73
74int
75Expression::do_traverse(Traverse*)
76{
77 return TRAVERSE_CONTINUE;
78}
79
80// This virtual function is called by the parser if the value of this
3c365907
ILT
81// expression is being discarded. By default, we give an error.
82// Expressions with side effects override.
7a938933 83
3f7af571 84bool
7a938933
ILT
85Expression::do_discarding_value()
86{
3c365907 87 this->unused_value_error();
3f7af571 88 return false;
7a938933
ILT
89}
90
91// This virtual function is called to export expressions. This will
92// only be used by expressions which may be constant.
93
94void
95Expression::do_export(Export*) const
96{
8c0d1865 97 go_unreachable();
7a938933
ILT
98}
99
3c365907 100// Give an error saying that the value of the expression is not used.
7a938933
ILT
101
102void
3c365907 103Expression::unused_value_error()
7a938933 104{
3f7af571 105 this->report_error(_("value computed is not used"));
7a938933
ILT
106}
107
108// Note that this expression is an error. This is called by children
109// when they discover an error.
110
111void
112Expression::set_is_error()
113{
114 this->classification_ = EXPRESSION_ERROR;
115}
116
117// For children to call to report an error conveniently.
118
119void
120Expression::report_error(const char* msg)
121{
122 error_at(this->location_, "%s", msg);
123 this->set_is_error();
124}
125
126// Set types of variables and constants. This is implemented by the
127// child class.
128
129void
130Expression::determine_type(const Type_context* context)
131{
132 this->do_determine_type(context);
133}
134
135// Set types when there is no context.
136
137void
138Expression::determine_type_no_context()
139{
140 Type_context context;
141 this->do_determine_type(&context);
142}
143
144// Return a tree handling any conversions which must be done during
145// assignment.
146
147tree
148Expression::convert_for_assignment(Translate_context* context, Type* lhs_type,
149 Type* rhs_type, tree rhs_tree,
8afa2bfb 150 Location location)
7a938933 151{
02ed921a 152 if (lhs_type->is_error() || rhs_type->is_error())
7a938933
ILT
153 return error_mark_node;
154
7a938933
ILT
155 if (rhs_tree == error_mark_node || TREE_TYPE(rhs_tree) == error_mark_node)
156 return error_mark_node;
157
158 Gogo* gogo = context->gogo();
159
5b735706 160 tree lhs_type_tree = type_to_tree(lhs_type->get_backend(gogo));
7a938933
ILT
161 if (lhs_type_tree == error_mark_node)
162 return error_mark_node;
163
c4675e5e
ILT
164 if (lhs_type->forwarded() != rhs_type->forwarded()
165 && lhs_type->interface_type() != NULL)
7a938933
ILT
166 {
167 if (rhs_type->interface_type() == NULL)
168 return Expression::convert_type_to_interface(context, lhs_type,
169 rhs_type, rhs_tree,
170 location);
171 else
172 return Expression::convert_interface_to_interface(context, lhs_type,
173 rhs_type, rhs_tree,
174 false, location);
175 }
c4675e5e
ILT
176 else if (lhs_type->forwarded() != rhs_type->forwarded()
177 && rhs_type->interface_type() != NULL)
7a938933
ILT
178 return Expression::convert_interface_to_type(context, lhs_type, rhs_type,
179 rhs_tree, location);
b7190f2f 180 else if (lhs_type->is_slice_type() && rhs_type->is_nil_type())
7a938933
ILT
181 {
182 // Assigning nil to an open array.
26409c52 183 go_assert(TREE_CODE(lhs_type_tree) == RECORD_TYPE);
7a938933 184
4efdbe5a
ILT
185 vec<constructor_elt, va_gc> *init;
186 vec_alloc(init, 3);
7a938933 187
f32682ca 188 constructor_elt empty = {NULL, NULL};
4efdbe5a 189 constructor_elt* elt = init->quick_push(empty);
7a938933 190 tree field = TYPE_FIELDS(lhs_type_tree);
26409c52 191 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),
7a938933
ILT
192 "__values") == 0);
193 elt->index = field;
194 elt->value = fold_convert(TREE_TYPE(field), null_pointer_node);
195
4efdbe5a 196 elt = init->quick_push(empty);
7a938933 197 field = DECL_CHAIN(field);
26409c52 198 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),
7a938933
ILT
199 "__count") == 0);
200 elt->index = field;
201 elt->value = fold_convert(TREE_TYPE(field), integer_zero_node);
202
4efdbe5a 203 elt = init->quick_push(empty);
7a938933 204 field = DECL_CHAIN(field);
26409c52 205 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),
7a938933
ILT
206 "__capacity") == 0);
207 elt->index = field;
208 elt->value = fold_convert(TREE_TYPE(field), integer_zero_node);
209
210 tree val = build_constructor(lhs_type_tree, init);
211 TREE_CONSTANT(val) = 1;
212
213 return val;
214 }
215 else if (rhs_type->is_nil_type())
216 {
217 // The left hand side should be a pointer type at the tree
218 // level.
26409c52 219 go_assert(POINTER_TYPE_P(lhs_type_tree));
7a938933
ILT
220 return fold_convert(lhs_type_tree, null_pointer_node);
221 }
222 else if (lhs_type_tree == TREE_TYPE(rhs_tree))
223 {
224 // No conversion is needed.
225 return rhs_tree;
226 }
227 else if (POINTER_TYPE_P(lhs_type_tree)
228 || INTEGRAL_TYPE_P(lhs_type_tree)
229 || SCALAR_FLOAT_TYPE_P(lhs_type_tree)
230 || COMPLEX_FLOAT_TYPE_P(lhs_type_tree))
8afa2bfb 231 return fold_convert_loc(location.gcc_location(), lhs_type_tree, rhs_tree);
2a4368af
ILT
232 else if ((TREE_CODE(lhs_type_tree) == RECORD_TYPE
233 && TREE_CODE(TREE_TYPE(rhs_tree)) == RECORD_TYPE)
234 || (TREE_CODE(lhs_type_tree) == ARRAY_TYPE
235 && TREE_CODE(TREE_TYPE(rhs_tree)) == ARRAY_TYPE))
7a938933 236 {
68c5d97b
ILT
237 // Avoid confusion from zero sized variables which may be
238 // represented as non-zero-sized.
239 if (int_size_in_bytes(lhs_type_tree) == 0
240 || int_size_in_bytes(TREE_TYPE(rhs_tree)) == 0)
241 return rhs_tree;
242
7a938933
ILT
243 // This conversion must be permitted by Go, or we wouldn't have
244 // gotten here.
26409c52 245 go_assert(int_size_in_bytes(lhs_type_tree)
68c5d97b 246 == int_size_in_bytes(TREE_TYPE(rhs_tree)));
8afa2bfb
SD
247 return fold_build1_loc(location.gcc_location(), VIEW_CONVERT_EXPR,
248 lhs_type_tree, rhs_tree);
7a938933
ILT
249 }
250 else
251 {
26409c52 252 go_assert(useless_type_conversion_p(lhs_type_tree, TREE_TYPE(rhs_tree)));
7a938933
ILT
253 return rhs_tree;
254 }
255}
256
257// Return a tree for a conversion from a non-interface type to an
258// interface type.
259
260tree
261Expression::convert_type_to_interface(Translate_context* context,
262 Type* lhs_type, Type* rhs_type,
8afa2bfb 263 tree rhs_tree, Location location)
7a938933
ILT
264{
265 Gogo* gogo = context->gogo();
266 Interface_type* lhs_interface_type = lhs_type->interface_type();
267 bool lhs_is_empty = lhs_interface_type->is_empty();
268
269 // Since RHS_TYPE is a static type, we can create the interface
270 // method table at compile time.
271
272 // When setting an interface to nil, we just set both fields to
273 // NULL.
274 if (rhs_type->is_nil_type())
54466dde
ILT
275 {
276 Btype* lhs_btype = lhs_type->get_backend(gogo);
277 return expr_to_tree(gogo->backend()->zero_expression(lhs_btype));
278 }
7a938933
ILT
279
280 // This should have been checked already.
26409c52 281 go_assert(lhs_interface_type->implements_interface(rhs_type, NULL));
7a938933 282
5b735706 283 tree lhs_type_tree = type_to_tree(lhs_type->get_backend(gogo));
7a938933
ILT
284 if (lhs_type_tree == error_mark_node)
285 return error_mark_node;
286
287 // An interface is a tuple. If LHS_TYPE is an empty interface type,
288 // then the first field is the type descriptor for RHS_TYPE.
289 // Otherwise it is the interface method table for RHS_TYPE.
290 tree first_field_value;
291 if (lhs_is_empty)
b93e0cfd
CM
292 {
293 Bexpression* rhs_bexpr =
294 rhs_type->type_descriptor_pointer(gogo, location);
295 first_field_value = expr_to_tree(rhs_bexpr);
296 }
7a938933
ILT
297 else
298 {
299 // Build the interface method table for this interface and this
300 // object type: a list of function pointers for each interface
301 // method.
302 Named_type* rhs_named_type = rhs_type->named_type();
0efaba3c 303 Struct_type* rhs_struct_type = rhs_type->struct_type();
7a938933 304 bool is_pointer = false;
0efaba3c 305 if (rhs_named_type == NULL && rhs_struct_type == NULL)
7a938933
ILT
306 {
307 rhs_named_type = rhs_type->deref()->named_type();
0efaba3c 308 rhs_struct_type = rhs_type->deref()->struct_type();
7a938933
ILT
309 is_pointer = true;
310 }
311 tree method_table;
0efaba3c 312 if (rhs_named_type != NULL)
7a938933
ILT
313 method_table =
314 rhs_named_type->interface_method_table(gogo, lhs_interface_type,
315 is_pointer);
0efaba3c
ILT
316 else if (rhs_struct_type != NULL)
317 method_table =
318 rhs_struct_type->interface_method_table(gogo, lhs_interface_type,
319 is_pointer);
320 else
321 method_table = null_pointer_node;
8afa2bfb
SD
322 first_field_value = fold_convert_loc(location.gcc_location(),
323 const_ptr_type_node, method_table);
7a938933 324 }
6d61c698
ILT
325 if (first_field_value == error_mark_node)
326 return error_mark_node;
7a938933
ILT
327
328 // Start building a constructor for the value we will return.
329
4efdbe5a
ILT
330 vec<constructor_elt, va_gc> *init;
331 vec_alloc(init, 2);
7a938933 332
f32682ca 333 constructor_elt empty = {NULL, NULL};
4efdbe5a 334 constructor_elt* elt = init->quick_push(empty);
7a938933 335 tree field = TYPE_FIELDS(lhs_type_tree);
26409c52 336 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),
7a938933
ILT
337 (lhs_is_empty ? "__type_descriptor" : "__methods")) == 0);
338 elt->index = field;
8afa2bfb
SD
339 elt->value = fold_convert_loc(location.gcc_location(), TREE_TYPE(field),
340 first_field_value);
7a938933 341
4efdbe5a 342 elt = init->quick_push(empty);
7a938933 343 field = DECL_CHAIN(field);
26409c52 344 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__object") == 0);
7a938933
ILT
345 elt->index = field;
346
347 if (rhs_type->points_to() != NULL)
348 {
349 // We are assigning a pointer to the interface; the interface
350 // holds the pointer itself.
351 elt->value = rhs_tree;
352 return build_constructor(lhs_type_tree, init);
353 }
354
355 // We are assigning a non-pointer value to the interface; the
356 // interface gets a copy of the value in the heap.
357
358 tree object_size = TYPE_SIZE_UNIT(TREE_TYPE(rhs_tree));
359
360 tree space = gogo->allocate_memory(rhs_type, object_size, location);
8afa2bfb
SD
361 space = fold_convert_loc(location.gcc_location(),
362 build_pointer_type(TREE_TYPE(rhs_tree)), space);
7a938933
ILT
363 space = save_expr(space);
364
8afa2bfb 365 tree ref = build_fold_indirect_ref_loc(location.gcc_location(), space);
7a938933 366 TREE_THIS_NOTRAP(ref) = 1;
8afa2bfb
SD
367 tree set = fold_build2_loc(location.gcc_location(), MODIFY_EXPR,
368 void_type_node, ref, rhs_tree);
7a938933 369
8afa2bfb
SD
370 elt->value = fold_convert_loc(location.gcc_location(), TREE_TYPE(field),
371 space);
7a938933
ILT
372
373 return build2(COMPOUND_EXPR, lhs_type_tree, set,
374 build_constructor(lhs_type_tree, init));
375}
376
377// Return a tree for the type descriptor of RHS_TREE, which has
378// interface type RHS_TYPE. If RHS_TREE is nil the result will be
379// NULL.
380
381tree
382Expression::get_interface_type_descriptor(Translate_context*,
383 Type* rhs_type, tree rhs_tree,
8afa2bfb 384 Location location)
7a938933
ILT
385{
386 tree rhs_type_tree = TREE_TYPE(rhs_tree);
26409c52 387 go_assert(TREE_CODE(rhs_type_tree) == RECORD_TYPE);
7a938933
ILT
388 tree rhs_field = TYPE_FIELDS(rhs_type_tree);
389 tree v = build3(COMPONENT_REF, TREE_TYPE(rhs_field), rhs_tree, rhs_field,
390 NULL_TREE);
391 if (rhs_type->interface_type()->is_empty())
392 {
26409c52 393 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)),
7a938933
ILT
394 "__type_descriptor") == 0);
395 return v;
396 }
397
26409c52 398 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)), "__methods")
7a938933 399 == 0);
26409c52 400 go_assert(POINTER_TYPE_P(TREE_TYPE(v)));
7a938933 401 v = save_expr(v);
8afa2bfb 402 tree v1 = build_fold_indirect_ref_loc(location.gcc_location(), v);
26409c52 403 go_assert(TREE_CODE(TREE_TYPE(v1)) == RECORD_TYPE);
7a938933 404 tree f = TYPE_FIELDS(TREE_TYPE(v1));
26409c52 405 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(f)), "__type_descriptor")
7a938933
ILT
406 == 0);
407 v1 = build3(COMPONENT_REF, TREE_TYPE(f), v1, f, NULL_TREE);
408
8afa2bfb
SD
409 tree eq = fold_build2_loc(location.gcc_location(), EQ_EXPR, boolean_type_node,
410 v, fold_convert_loc(location.gcc_location(),
411 TREE_TYPE(v),
412 null_pointer_node));
413 tree n = fold_convert_loc(location.gcc_location(), TREE_TYPE(v1),
414 null_pointer_node);
415 return fold_build3_loc(location.gcc_location(), COND_EXPR, TREE_TYPE(v1),
7a938933
ILT
416 eq, n, v1);
417}
418
419// Return a tree for the conversion of an interface type to an
420// interface type.
421
422tree
423Expression::convert_interface_to_interface(Translate_context* context,
424 Type *lhs_type, Type *rhs_type,
425 tree rhs_tree, bool for_type_guard,
8afa2bfb 426 Location location)
7a938933
ILT
427{
428 Gogo* gogo = context->gogo();
429 Interface_type* lhs_interface_type = lhs_type->interface_type();
430 bool lhs_is_empty = lhs_interface_type->is_empty();
431
5b735706 432 tree lhs_type_tree = type_to_tree(lhs_type->get_backend(gogo));
7a938933
ILT
433 if (lhs_type_tree == error_mark_node)
434 return error_mark_node;
435
436 // In the general case this requires runtime examination of the type
437 // method table to match it up with the interface methods.
438
439 // FIXME: If all of the methods in the right hand side interface
440 // also appear in the left hand side interface, then we don't need
441 // to do a runtime check, although we still need to build a new
442 // method table.
443
444 // Get the type descriptor for the right hand side. This will be
445 // NULL for a nil interface.
446
447 if (!DECL_P(rhs_tree))
448 rhs_tree = save_expr(rhs_tree);
449
450 tree rhs_type_descriptor =
451 Expression::get_interface_type_descriptor(context, rhs_type, rhs_tree,
452 location);
453
454 // The result is going to be a two element constructor.
455
4efdbe5a
ILT
456 vec<constructor_elt, va_gc> *init;
457 vec_alloc (init, 2);
7a938933 458
f32682ca 459 constructor_elt empty = {NULL, NULL};
4efdbe5a 460 constructor_elt* elt = init->quick_push(empty);
7a938933
ILT
461 tree field = TYPE_FIELDS(lhs_type_tree);
462 elt->index = field;
463
464 if (for_type_guard)
465 {
466 // A type assertion fails when converting a nil interface.
b93e0cfd
CM
467 Bexpression* lhs_type_expr = lhs_type->type_descriptor_pointer(gogo,
468 location);
469 tree lhs_type_descriptor = expr_to_tree(lhs_type_expr);
7a938933
ILT
470 static tree assert_interface_decl;
471 tree call = Gogo::call_builtin(&assert_interface_decl,
472 location,
473 "__go_assert_interface",
474 2,
475 ptr_type_node,
476 TREE_TYPE(lhs_type_descriptor),
477 lhs_type_descriptor,
478 TREE_TYPE(rhs_type_descriptor),
479 rhs_type_descriptor);
faff9b04
ILT
480 if (call == error_mark_node)
481 return error_mark_node;
7a938933
ILT
482 // This will panic if the interface conversion fails.
483 TREE_NOTHROW(assert_interface_decl) = 0;
8afa2bfb
SD
484 elt->value = fold_convert_loc(location.gcc_location(), TREE_TYPE(field),
485 call);
7a938933
ILT
486 }
487 else if (lhs_is_empty)
488 {
489 // A convertion to an empty interface always succeeds, and the
490 // first field is just the type descriptor of the object.
26409c52 491 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),
7a938933 492 "__type_descriptor") == 0);
6df020c0
ILT
493 elt->value = fold_convert_loc(location.gcc_location(),
494 TREE_TYPE(field), rhs_type_descriptor);
7a938933
ILT
495 }
496 else
497 {
498 // A conversion to a non-empty interface may fail, but unlike a
499 // type assertion converting nil will always succeed.
26409c52 500 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__methods")
7a938933 501 == 0);
b93e0cfd
CM
502 Bexpression* lhs_type_expr = lhs_type->type_descriptor_pointer(gogo,
503 location);
504 tree lhs_type_descriptor = expr_to_tree(lhs_type_expr);
505
7a938933
ILT
506 static tree convert_interface_decl;
507 tree call = Gogo::call_builtin(&convert_interface_decl,
508 location,
509 "__go_convert_interface",
510 2,
511 ptr_type_node,
512 TREE_TYPE(lhs_type_descriptor),
513 lhs_type_descriptor,
514 TREE_TYPE(rhs_type_descriptor),
515 rhs_type_descriptor);
faff9b04
ILT
516 if (call == error_mark_node)
517 return error_mark_node;
7a938933
ILT
518 // This will panic if the interface conversion fails.
519 TREE_NOTHROW(convert_interface_decl) = 0;
8afa2bfb
SD
520 elt->value = fold_convert_loc(location.gcc_location(), TREE_TYPE(field),
521 call);
7a938933
ILT
522 }
523
524 // The second field is simply the object pointer.
525
4efdbe5a 526 elt = init->quick_push(empty);
7a938933 527 field = DECL_CHAIN(field);
26409c52 528 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__object") == 0);
7a938933
ILT
529 elt->index = field;
530
531 tree rhs_type_tree = TREE_TYPE(rhs_tree);
26409c52 532 go_assert(TREE_CODE(rhs_type_tree) == RECORD_TYPE);
7a938933 533 tree rhs_field = DECL_CHAIN(TYPE_FIELDS(rhs_type_tree));
26409c52 534 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)), "__object") == 0);
7a938933
ILT
535 elt->value = build3(COMPONENT_REF, TREE_TYPE(rhs_field), rhs_tree, rhs_field,
536 NULL_TREE);
537
538 return build_constructor(lhs_type_tree, init);
539}
540
541// Return a tree for the conversion of an interface type to a
542// non-interface type.
543
544tree
545Expression::convert_interface_to_type(Translate_context* context,
546 Type *lhs_type, Type* rhs_type,
8afa2bfb 547 tree rhs_tree, Location location)
7a938933
ILT
548{
549 Gogo* gogo = context->gogo();
550 tree rhs_type_tree = TREE_TYPE(rhs_tree);
551
5b735706 552 tree lhs_type_tree = type_to_tree(lhs_type->get_backend(gogo));
7a938933
ILT
553 if (lhs_type_tree == error_mark_node)
554 return error_mark_node;
555
556 // Call a function to check that the type is valid. The function
557 // will panic with an appropriate runtime type error if the type is
558 // not valid.
b93e0cfd
CM
559 Bexpression* lhs_type_expr = lhs_type->type_descriptor_pointer(gogo,
560 location);
561 tree lhs_type_descriptor = expr_to_tree(lhs_type_expr);
7a938933
ILT
562
563 if (!DECL_P(rhs_tree))
564 rhs_tree = save_expr(rhs_tree);
565
566 tree rhs_type_descriptor =
567 Expression::get_interface_type_descriptor(context, rhs_type, rhs_tree,
568 location);
569
b93e0cfd
CM
570 Bexpression* rhs_inter_expr = rhs_type->type_descriptor_pointer(gogo,
571 location);
572 tree rhs_inter_descriptor = expr_to_tree(rhs_inter_expr);
7a938933
ILT
573
574 static tree check_interface_type_decl;
575 tree call = Gogo::call_builtin(&check_interface_type_decl,
576 location,
577 "__go_check_interface_type",
578 3,
579 void_type_node,
580 TREE_TYPE(lhs_type_descriptor),
581 lhs_type_descriptor,
582 TREE_TYPE(rhs_type_descriptor),
583 rhs_type_descriptor,
584 TREE_TYPE(rhs_inter_descriptor),
585 rhs_inter_descriptor);
faff9b04
ILT
586 if (call == error_mark_node)
587 return error_mark_node;
7a938933
ILT
588 // This call will panic if the conversion is invalid.
589 TREE_NOTHROW(check_interface_type_decl) = 0;
590
591 // If the call succeeds, pull out the value.
26409c52 592 go_assert(TREE_CODE(rhs_type_tree) == RECORD_TYPE);
7a938933 593 tree rhs_field = DECL_CHAIN(TYPE_FIELDS(rhs_type_tree));
26409c52 594 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(rhs_field)), "__object") == 0);
7a938933
ILT
595 tree val = build3(COMPONENT_REF, TREE_TYPE(rhs_field), rhs_tree, rhs_field,
596 NULL_TREE);
597
598 // If the value is a pointer, then it is the value we want.
599 // Otherwise it points to the value.
600 if (lhs_type->points_to() == NULL)
601 {
8afa2bfb
SD
602 val = fold_convert_loc(location.gcc_location(),
603 build_pointer_type(lhs_type_tree), val);
604 val = build_fold_indirect_ref_loc(location.gcc_location(), val);
7a938933
ILT
605 }
606
607 return build2(COMPOUND_EXPR, lhs_type_tree, call,
8afa2bfb 608 fold_convert_loc(location.gcc_location(), lhs_type_tree, val));
7a938933
ILT
609}
610
611// Convert an expression to a tree. This is implemented by the child
612// class. Not that it is not in general safe to call this multiple
613// times for a single expression, but that we don't catch such errors.
614
615tree
616Expression::get_tree(Translate_context* context)
617{
618 // The child may have marked this expression as having an error.
619 if (this->classification_ == EXPRESSION_ERROR)
620 return error_mark_node;
621
622 return this->do_get_tree(context);
623}
624
002ee4d1
CM
625// Return a backend expression for VAL.
626Bexpression*
627Expression::backend_numeric_constant_expression(Translate_context* context,
628 Numeric_constant* val)
7a938933 629{
002ee4d1
CM
630 Gogo* gogo = context->gogo();
631 Type* type = val->type();
632 if (type == NULL)
633 return gogo->backend()->error_expression();
7a938933 634
002ee4d1
CM
635 Btype* btype = type->get_backend(gogo);
636 Bexpression* ret;
637 if (type->integer_type() != NULL)
7a938933
ILT
638 {
639 mpz_t ival;
002ee4d1
CM
640 if (!val->to_int(&ival))
641 {
642 go_assert(saw_errors());
643 return gogo->backend()->error_expression();
644 }
645 ret = gogo->backend()->integer_constant_expression(btype, ival);
7a938933 646 mpz_clear(ival);
7a938933 647 }
002ee4d1 648 else if (type->float_type() != NULL)
7a938933 649 {
002ee4d1
CM
650 mpfr_t fval;
651 if (!val->to_float(&fval))
652 {
653 go_assert(saw_errors());
654 return gogo->backend()->error_expression();
655 }
656 ret = gogo->backend()->float_constant_expression(btype, fval);
657 mpfr_clear(fval);
7a938933 658 }
002ee4d1 659 else if (type->complex_type() != NULL)
7a938933 660 {
002ee4d1
CM
661 mpfr_t real;
662 mpfr_t imag;
663 if (!val->to_complex(&real, &imag))
664 {
665 go_assert(saw_errors());
666 return gogo->backend()->error_expression();
667 }
668 ret = gogo->backend()->complex_constant_expression(btype, real, imag);
669 mpfr_clear(real);
670 mpfr_clear(imag);
7a938933
ILT
671 }
672 else
8c0d1865 673 go_unreachable();
7a938933 674
002ee4d1 675 return ret;
7a938933
ILT
676}
677
678// Return a tree which evaluates to true if VAL, of arbitrary integer
679// type, is negative or is more than the maximum value of BOUND_TYPE.
680// If SOFAR is not NULL, it is or'red into the result. The return
681// value may be NULL if SOFAR is NULL.
682
683tree
684Expression::check_bounds(tree val, tree bound_type, tree sofar,
8afa2bfb 685 Location loc)
7a938933
ILT
686{
687 tree val_type = TREE_TYPE(val);
688 tree ret = NULL_TREE;
689
690 if (!TYPE_UNSIGNED(val_type))
691 {
8afa2bfb 692 ret = fold_build2_loc(loc.gcc_location(), LT_EXPR, boolean_type_node, val,
7a938933
ILT
693 build_int_cst(val_type, 0));
694 if (ret == boolean_false_node)
695 ret = NULL_TREE;
696 }
697
24271501
ILT
698 HOST_WIDE_INT val_type_size = int_size_in_bytes(val_type);
699 HOST_WIDE_INT bound_type_size = int_size_in_bytes(bound_type);
700 go_assert(val_type_size != -1 && bound_type_size != -1);
701 if (val_type_size > bound_type_size
702 || (val_type_size == bound_type_size
703 && TYPE_UNSIGNED(val_type)
704 && !TYPE_UNSIGNED(bound_type)))
7a938933
ILT
705 {
706 tree max = TYPE_MAX_VALUE(bound_type);
8afa2bfb
SD
707 tree big = fold_build2_loc(loc.gcc_location(), GT_EXPR, boolean_type_node,
708 val, fold_convert_loc(loc.gcc_location(),
709 val_type, max));
7a938933
ILT
710 if (big == boolean_false_node)
711 ;
712 else if (ret == NULL_TREE)
713 ret = big;
714 else
8afa2bfb
SD
715 ret = fold_build2_loc(loc.gcc_location(), TRUTH_OR_EXPR,
716 boolean_type_node, ret, big);
7a938933
ILT
717 }
718
719 if (ret == NULL_TREE)
720 return sofar;
721 else if (sofar == NULL_TREE)
722 return ret;
723 else
8afa2bfb 724 return fold_build2_loc(loc.gcc_location(), TRUTH_OR_EXPR, boolean_type_node,
7a938933
ILT
725 sofar, ret);
726}
727
16c57fe2
RL
728void
729Expression::dump_expression(Ast_dump_context* ast_dump_context) const
730{
731 this->do_dump_expression(ast_dump_context);
732}
733
7a938933
ILT
734// Error expressions. This are used to avoid cascading errors.
735
736class Error_expression : public Expression
737{
738 public:
8afa2bfb 739 Error_expression(Location location)
7a938933
ILT
740 : Expression(EXPRESSION_ERROR, location)
741 { }
742
743 protected:
744 bool
745 do_is_constant() const
746 { return true; }
747
748 bool
5caf63ca 749 do_numeric_constant_value(Numeric_constant* nc) const
7a938933 750 {
5caf63ca 751 nc->set_unsigned_long(NULL, 0);
7a938933
ILT
752 return true;
753 }
754
3f7af571 755 bool
7a938933 756 do_discarding_value()
3f7af571 757 { return true; }
7a938933
ILT
758
759 Type*
760 do_type()
761 { return Type::make_error_type(); }
762
763 void
764 do_determine_type(const Type_context*)
765 { }
766
767 Expression*
768 do_copy()
769 { return this; }
770
771 bool
772 do_is_addressable() const
773 { return true; }
774
775 tree
776 do_get_tree(Translate_context*)
777 { return error_mark_node; }
16c57fe2
RL
778
779 void
780 do_dump_expression(Ast_dump_context*) const;
7a938933
ILT
781};
782
16c57fe2
RL
783// Dump the ast representation for an error expression to a dump context.
784
785void
786Error_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
787{
788 ast_dump_context->ostream() << "_Error_" ;
789}
790
7a938933 791Expression*
8afa2bfb 792Expression::make_error(Location location)
7a938933
ILT
793{
794 return new Error_expression(location);
795}
796
797// An expression which is really a type. This is used during parsing.
798// It is an error if these survive after lowering.
799
800class
801Type_expression : public Expression
802{
803 public:
8afa2bfb 804 Type_expression(Type* type, Location location)
7a938933
ILT
805 : Expression(EXPRESSION_TYPE, location),
806 type_(type)
807 { }
808
809 protected:
810 int
811 do_traverse(Traverse* traverse)
812 { return Type::traverse(this->type_, traverse); }
813
814 Type*
815 do_type()
816 { return this->type_; }
817
818 void
819 do_determine_type(const Type_context*)
820 { }
821
822 void
823 do_check_types(Gogo*)
824 { this->report_error(_("invalid use of type")); }
825
826 Expression*
827 do_copy()
828 { return this; }
829
830 tree
831 do_get_tree(Translate_context*)
8c0d1865 832 { go_unreachable(); }
7a938933 833
16c57fe2
RL
834 void do_dump_expression(Ast_dump_context*) const;
835
7a938933
ILT
836 private:
837 // The type which we are representing as an expression.
838 Type* type_;
839};
840
16c57fe2
RL
841void
842Type_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
843{
844 ast_dump_context->dump_type(this->type_);
845}
846
7a938933 847Expression*
8afa2bfb 848Expression::make_type(Type* type, Location location)
7a938933
ILT
849{
850 return new Type_expression(type, location);
851}
852
8211d03c
ILT
853// Class Parser_expression.
854
855Type*
856Parser_expression::do_type()
857{
858 // We should never really ask for the type of a Parser_expression.
859 // However, it can happen, at least when we have an invalid const
860 // whose initializer refers to the const itself. In that case we
861 // may ask for the type when lowering the const itself.
26409c52 862 go_assert(saw_errors());
8211d03c
ILT
863 return Type::make_error_type();
864}
865
7a938933
ILT
866// Class Var_expression.
867
868// Lower a variable expression. Here we just make sure that the
869// initialization expression of the variable has been lowered. This
870// ensures that we will be able to determine the type of the variable
871// if necessary.
872
873Expression*
8586635c
ILT
874Var_expression::do_lower(Gogo* gogo, Named_object* function,
875 Statement_inserter* inserter, int)
7a938933
ILT
876{
877 if (this->variable_->is_variable())
878 {
879 Variable* var = this->variable_->var_value();
880 // This is either a local variable or a global variable. A
881 // reference to a variable which is local to an enclosing
882 // function will be a reference to a field in a closure.
883 if (var->is_global())
8586635c
ILT
884 {
885 function = NULL;
886 inserter = NULL;
887 }
888 var->lower_init_expression(gogo, function, inserter);
7a938933
ILT
889 }
890 return this;
891}
892
7a938933
ILT
893// Return the type of a reference to a variable.
894
895Type*
896Var_expression::do_type()
897{
898 if (this->variable_->is_variable())
899 return this->variable_->var_value()->type();
900 else if (this->variable_->is_result_variable())
901 return this->variable_->result_var_value()->type();
902 else
8c0d1865 903 go_unreachable();
7a938933
ILT
904}
905
eba4ad89
ILT
906// Determine the type of a reference to a variable.
907
908void
909Var_expression::do_determine_type(const Type_context*)
910{
911 if (this->variable_->is_variable())
912 this->variable_->var_value()->determine_type();
913}
914
7a938933
ILT
915// Something takes the address of this variable. This means that we
916// may want to move the variable onto the heap.
917
918void
919Var_expression::do_address_taken(bool escapes)
920{
921 if (!escapes)
acf98146
ILT
922 {
923 if (this->variable_->is_variable())
924 this->variable_->var_value()->set_non_escaping_address_taken();
925 else if (this->variable_->is_result_variable())
926 this->variable_->result_var_value()->set_non_escaping_address_taken();
927 else
928 go_unreachable();
929 }
7a938933 930 else
acf98146
ILT
931 {
932 if (this->variable_->is_variable())
933 this->variable_->var_value()->set_address_taken();
934 else if (this->variable_->is_result_variable())
935 this->variable_->result_var_value()->set_address_taken();
936 else
937 go_unreachable();
938 }
7a938933
ILT
939}
940
941// Get the tree for a reference to a variable.
942
943tree
944Var_expression::do_get_tree(Translate_context* context)
945{
e09ce6c5
ILT
946 Bvariable* bvar = this->variable_->get_backend_variable(context->gogo(),
947 context->function());
e09ce6c5 948 bool is_in_heap;
d4fe7beb 949 Location loc = this->location();
e09ce6c5
ILT
950 if (this->variable_->is_variable())
951 is_in_heap = this->variable_->var_value()->is_in_heap();
952 else if (this->variable_->is_result_variable())
953 is_in_heap = this->variable_->result_var_value()->is_in_heap();
954 else
8c0d1865 955 go_unreachable();
d4fe7beb
CM
956
957 Bexpression* ret = context->backend()->var_expression(bvar, loc);
e09ce6c5 958 if (is_in_heap)
d4fe7beb
CM
959 ret = context->backend()->indirect_expression(ret, true, loc);
960 return expr_to_tree(ret);
7a938933
ILT
961}
962
16c57fe2
RL
963// Ast dump for variable expression.
964
965void
966Var_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
967{
968 ast_dump_context->ostream() << this->variable_->name() ;
969}
970
7a938933
ILT
971// Make a reference to a variable in an expression.
972
973Expression*
8afa2bfb 974Expression::make_var_reference(Named_object* var, Location location)
7a938933
ILT
975{
976 if (var->is_sink())
977 return Expression::make_sink(location);
978
979 // FIXME: Creating a new object for each reference to a variable is
980 // wasteful.
981 return new Var_expression(var, location);
982}
983
984// Class Temporary_reference_expression.
985
986// The type.
987
988Type*
989Temporary_reference_expression::do_type()
990{
991 return this->statement_->type();
992}
993
994// Called if something takes the address of this temporary variable.
995// We never have to move temporary variables to the heap, but we do
996// need to know that they must live in the stack rather than in a
997// register.
998
999void
1000Temporary_reference_expression::do_address_taken(bool)
1001{
1002 this->statement_->set_is_address_taken();
1003}
1004
1005// Get a tree referring to the variable.
1006
1007tree
9131ad67 1008Temporary_reference_expression::do_get_tree(Translate_context* context)
7a938933 1009{
c6d2bfbb 1010 Gogo* gogo = context->gogo();
9131ad67 1011 Bvariable* bvar = this->statement_->get_backend_variable(context);
c6d2bfbb 1012 Bexpression* ret = gogo->backend()->var_expression(bvar, this->location());
9131ad67 1013
c6d2bfbb 1014 // The backend can't always represent the same set of recursive types
9131ad67
ILT
1015 // that the Go frontend can. In some cases this means that a
1016 // temporary variable won't have the right backend type. Correct
1017 // that here by adding a type cast. We need to use base() to push
1018 // the circularity down one level.
c6d2bfbb 1019 Type* stype = this->statement_->type();
8586635c 1020 if (!this->is_lvalue_
c6d2bfbb
CM
1021 && stype->has_pointer()
1022 && stype->deref()->is_void_type())
9131ad67 1023 {
c6d2bfbb
CM
1024 Btype* btype = this->type()->base()->get_backend(gogo);
1025 ret = gogo->backend()->convert_expression(btype, ret, this->location());
9131ad67 1026 }
c6d2bfbb 1027 return expr_to_tree(ret);
7a938933
ILT
1028}
1029
16c57fe2
RL
1030// Ast dump for temporary reference.
1031
1032void
1033Temporary_reference_expression::do_dump_expression(
1034 Ast_dump_context* ast_dump_context) const
1035{
1036 ast_dump_context->dump_temp_variable_name(this->statement_);
1037}
1038
7a938933
ILT
1039// Make a reference to a temporary variable.
1040
8586635c 1041Temporary_reference_expression*
7a938933 1042Expression::make_temporary_reference(Temporary_statement* statement,
8afa2bfb 1043 Location location)
7a938933
ILT
1044{
1045 return new Temporary_reference_expression(statement, location);
1046}
1047
f9f96987
ILT
1048// Class Set_and_use_temporary_expression.
1049
1050// Return the type.
1051
1052Type*
1053Set_and_use_temporary_expression::do_type()
1054{
1055 return this->statement_->type();
1056}
1057
571d3f91
ILT
1058// Determine the type of the expression.
1059
1060void
1061Set_and_use_temporary_expression::do_determine_type(
1062 const Type_context* context)
1063{
1064 this->expr_->determine_type(context);
1065}
1066
f9f96987
ILT
1067// Take the address.
1068
1069void
1070Set_and_use_temporary_expression::do_address_taken(bool)
1071{
1072 this->statement_->set_is_address_taken();
1073}
1074
1075// Return the backend representation.
1076
1077tree
1078Set_and_use_temporary_expression::do_get_tree(Translate_context* context)
1079{
1080 Bvariable* bvar = this->statement_->get_backend_variable(context);
1081 tree var_tree = var_to_tree(bvar);
1082 tree expr_tree = this->expr_->get_tree(context);
1083 if (var_tree == error_mark_node || expr_tree == error_mark_node)
1084 return error_mark_node;
1085 Location loc = this->location();
1086 return build2_loc(loc.gcc_location(), COMPOUND_EXPR, TREE_TYPE(var_tree),
1087 build2_loc(loc.gcc_location(), MODIFY_EXPR, void_type_node,
1088 var_tree, expr_tree),
1089 var_tree);
1090}
1091
1092// Dump.
1093
1094void
1095Set_and_use_temporary_expression::do_dump_expression(
1096 Ast_dump_context* ast_dump_context) const
1097{
1098 ast_dump_context->ostream() << '(';
1099 ast_dump_context->dump_temp_variable_name(this->statement_);
1100 ast_dump_context->ostream() << " = ";
1101 this->expr_->dump_expression(ast_dump_context);
1102 ast_dump_context->ostream() << ')';
1103}
1104
1105// Make a set-and-use temporary.
1106
1107Set_and_use_temporary_expression*
1108Expression::make_set_and_use_temporary(Temporary_statement* statement,
1109 Expression* expr, Location location)
1110{
1111 return new Set_and_use_temporary_expression(statement, expr, location);
1112}
1113
7a938933
ILT
1114// A sink expression--a use of the blank identifier _.
1115
1116class Sink_expression : public Expression
1117{
1118 public:
8afa2bfb 1119 Sink_expression(Location location)
7a938933
ILT
1120 : Expression(EXPRESSION_SINK, location),
1121 type_(NULL), var_(NULL_TREE)
1122 { }
1123
1124 protected:
3f7af571 1125 bool
7a938933 1126 do_discarding_value()
3f7af571 1127 { return true; }
7a938933
ILT
1128
1129 Type*
1130 do_type();
1131
1132 void
1133 do_determine_type(const Type_context*);
1134
1135 Expression*
1136 do_copy()
1137 { return new Sink_expression(this->location()); }
1138
1139 tree
1140 do_get_tree(Translate_context*);
1141
16c57fe2
RL
1142 void
1143 do_dump_expression(Ast_dump_context*) const;
1144
7a938933
ILT
1145 private:
1146 // The type of this sink variable.
1147 Type* type_;
1148 // The temporary variable we generate.
1149 tree var_;
1150};
1151
1152// Return the type of a sink expression.
1153
1154Type*
1155Sink_expression::do_type()
1156{
1157 if (this->type_ == NULL)
1158 return Type::make_sink_type();
1159 return this->type_;
1160}
1161
1162// Determine the type of a sink expression.
1163
1164void
1165Sink_expression::do_determine_type(const Type_context* context)
1166{
1167 if (context->type != NULL)
1168 this->type_ = context->type;
1169}
1170
1171// Return a temporary variable for a sink expression. This will
1172// presumably be a write-only variable which the middle-end will drop.
1173
1174tree
1175Sink_expression::do_get_tree(Translate_context* context)
1176{
1177 if (this->var_ == NULL_TREE)
1178 {
26409c52 1179 go_assert(this->type_ != NULL && !this->type_->is_sink_type());
5b735706
ILT
1180 Btype* bt = this->type_->get_backend(context->gogo());
1181 this->var_ = create_tmp_var(type_to_tree(bt), "blank");
7a938933
ILT
1182 }
1183 return this->var_;
1184}
1185
16c57fe2
RL
1186// Ast dump for sink expression.
1187
1188void
1189Sink_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
1190{
1191 ast_dump_context->ostream() << "_" ;
1192}
1193
7a938933
ILT
1194// Make a sink expression.
1195
1196Expression*
8afa2bfb 1197Expression::make_sink(Location location)
7a938933
ILT
1198{
1199 return new Sink_expression(location);
1200}
1201
1202// Class Func_expression.
1203
1204// FIXME: Can a function expression appear in a constant expression?
1205// The value is unchanging. Initializing a constant to the address of
1206// a function seems like it could work, though there might be little
1207// point to it.
1208
7a938933
ILT
1209// Traversal.
1210
1211int
1212Func_expression::do_traverse(Traverse* traverse)
1213{
1214 return (this->closure_ == NULL
1215 ? TRAVERSE_CONTINUE
1216 : Expression::traverse(&this->closure_, traverse));
1217}
1218
1219// Return the type of a function expression.
1220
1221Type*
1222Func_expression::do_type()
1223{
1224 if (this->function_->is_function())
1225 return this->function_->func_value()->type();
1226 else if (this->function_->is_function_declaration())
1227 return this->function_->func_declaration_value()->type();
1228 else
8c0d1865 1229 go_unreachable();
7a938933
ILT
1230}
1231
fdbc38a6 1232// Get the tree for the code of a function expression.
7a938933 1233
b7d93b46 1234Bexpression*
fdbc38a6 1235Func_expression::get_code_pointer(Gogo* gogo, Named_object* no, Location loc)
7a938933
ILT
1236{
1237 Function_type* fntype;
fdbc38a6
ILT
1238 if (no->is_function())
1239 fntype = no->func_value()->type();
1240 else if (no->is_function_declaration())
1241 fntype = no->func_declaration_value()->type();
7a938933 1242 else
8c0d1865 1243 go_unreachable();
7a938933
ILT
1244
1245 // Builtin functions are handled specially by Call_expression. We
1246 // can't take their address.
1247 if (fntype->is_builtin())
1248 {
fdbc38a6 1249 error_at(loc,
a0236882 1250 "invalid use of special builtin function %qs; must be called",
fdbc38a6 1251 no->message_name().c_str());
b7d93b46 1252 return gogo->backend()->error_expression();
7a938933
ILT
1253 }
1254
b7d93b46 1255 Bfunction* fndecl;
7a938933 1256 if (no->is_function())
f7191ecd 1257 fndecl = no->func_value()->get_or_make_decl(gogo, no);
7a938933 1258 else if (no->is_function_declaration())
f7191ecd 1259 fndecl = no->func_declaration_value()->get_or_make_decl(gogo, no);
7a938933 1260 else
8c0d1865 1261 go_unreachable();
7a938933 1262
b7d93b46 1263 return gogo->backend()->function_code_expression(fndecl, loc);
7a938933
ILT
1264}
1265
1266// Get the tree for a function expression. This is used when we take
fdbc38a6
ILT
1267// the address of a function rather than simply calling it. A func
1268// value is represented as a pointer to a block of memory. The first
1269// word of that memory is a pointer to the function code. The
1270// remaining parts of that memory are the addresses of variables that
1271// the function closes over.
7a938933
ILT
1272
1273tree
1274Func_expression::do_get_tree(Translate_context* context)
1275{
fdbc38a6 1276 // If there is no closure, just use the function descriptor.
40bb0243 1277 if (this->closure_ == NULL)
fdbc38a6
ILT
1278 {
1279 Gogo* gogo = context->gogo();
1280 Named_object* no = this->function_;
1281 Expression* descriptor;
1282 if (no->is_function())
1283 descriptor = no->func_value()->descriptor(gogo, no);
1284 else if (no->is_function_declaration())
1285 {
1286 if (no->func_declaration_value()->type()->is_builtin())
1287 {
1288 error_at(this->location(),
1289 ("invalid use of special builtin function %qs; "
1290 "must be called"),
1291 no->message_name().c_str());
1292 return error_mark_node;
1293 }
1294 descriptor = no->func_declaration_value()->descriptor(gogo, no);
1295 }
1296 else
1297 go_unreachable();
40bb0243 1298
fdbc38a6
ILT
1299 tree dtree = descriptor->get_tree(context);
1300 if (dtree == error_mark_node)
1301 return error_mark_node;
1302 return build_fold_addr_expr_loc(this->location().gcc_location(), dtree);
1303 }
7a938933 1304
fdbc38a6 1305 go_assert(this->function_->func_value()->enclosing() != NULL);
7a938933 1306
fdbc38a6
ILT
1307 // If there is a closure, then the closure is itself the function
1308 // expression. It is a pointer to a struct whose first field points
1309 // to the function code and whose remaining fields are the addresses
1310 // of the closed-over variables.
1311 return this->closure_->get_tree(context);
7a938933
ILT
1312}
1313
16c57fe2
RL
1314// Ast dump for function.
1315
1316void
1317Func_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
1318{
706cd57f
RL
1319 ast_dump_context->ostream() << this->function_->name();
1320 if (this->closure_ != NULL)
1321 {
1322 ast_dump_context->ostream() << " {closure = ";
1323 this->closure_->dump_expression(ast_dump_context);
1324 ast_dump_context->ostream() << "}";
1325 }
16c57fe2
RL
1326}
1327
7a938933
ILT
1328// Make a reference to a function in an expression.
1329
1330Expression*
1331Expression::make_func_reference(Named_object* function, Expression* closure,
8afa2bfb 1332 Location location)
7a938933
ILT
1333{
1334 return new Func_expression(function, closure, location);
1335}
1336
ed3cd943 1337// Class Func_descriptor_expression.
fdbc38a6 1338
ed3cd943 1339// Constructor.
fdbc38a6 1340
ed3cd943
ILT
1341Func_descriptor_expression::Func_descriptor_expression(Named_object* fn)
1342 : Expression(EXPRESSION_FUNC_DESCRIPTOR, fn->location()),
05a7d566 1343 fn_(fn), dvar_(NULL)
ed3cd943
ILT
1344{
1345 go_assert(!fn->is_function() || !fn->func_value()->needs_closure());
1346}
fdbc38a6 1347
ed3cd943 1348// Traversal.
fdbc38a6 1349
ed3cd943
ILT
1350int
1351Func_descriptor_expression::do_traverse(Traverse*)
1352{
1353 return TRAVERSE_CONTINUE;
1354}
fdbc38a6
ILT
1355
1356// All function descriptors have the same type.
1357
1358Type* Func_descriptor_expression::descriptor_type;
1359
1360void
1361Func_descriptor_expression::make_func_descriptor_type()
1362{
1363 if (Func_descriptor_expression::descriptor_type != NULL)
1364 return;
1365 Type* uintptr_type = Type::lookup_integer_type("uintptr");
1366 Type* struct_type = Type::make_builtin_struct_type(1, "code", uintptr_type);
1367 Func_descriptor_expression::descriptor_type =
1368 Type::make_builtin_named_type("functionDescriptor", struct_type);
1369}
1370
1371Type*
1372Func_descriptor_expression::do_type()
1373{
1374 Func_descriptor_expression::make_func_descriptor_type();
1375 return Func_descriptor_expression::descriptor_type;
1376}
1377
1378// The tree for a function descriptor.
1379
1380tree
1381Func_descriptor_expression::do_get_tree(Translate_context* context)
1382{
1383 if (this->dvar_ != NULL)
1384 return var_to_tree(this->dvar_);
1385
1386 Gogo* gogo = context->gogo();
1387 Named_object* no = this->fn_;
1388 Location loc = no->location();
1389
1390 std::string var_name;
1391 if (no->package() == NULL)
1392 var_name = gogo->pkgpath_symbol();
1393 else
1394 var_name = no->package()->pkgpath_symbol();
1395 var_name.push_back('.');
1396 var_name.append(Gogo::unpack_hidden_name(no->name()));
1397 var_name.append("$descriptor");
1398
1399 Btype* btype = this->type()->get_backend(gogo);
1400
1401 Bvariable* bvar;
1402 if (no->package() != NULL
1403 || Linemap::is_predeclared_location(no->location()))
05a7d566
ILT
1404 bvar = context->backend()->immutable_struct_reference(var_name, btype,
1405 loc);
fdbc38a6
ILT
1406 else
1407 {
1408 Location bloc = Linemap::predeclared_location();
1409 bool is_hidden = ((no->is_function()
1410 && no->func_value()->enclosing() != NULL)
1411 || Gogo::is_thunk(no));
1412 bvar = context->backend()->immutable_struct(var_name, is_hidden, false,
1413 btype, bloc);
1414 Expression_list* vals = new Expression_list();
05a7d566 1415 vals->push_back(Expression::make_func_code_reference(this->fn_, bloc));
fdbc38a6
ILT
1416 Expression* init =
1417 Expression::make_struct_composite_literal(this->type(), vals, bloc);
1418 Translate_context bcontext(gogo, NULL, NULL, NULL);
1419 bcontext.set_is_const();
1420 Bexpression* binit = tree_to_expr(init->get_tree(&bcontext));
1421 context->backend()->immutable_struct_set_init(bvar, var_name, is_hidden,
1422 false, btype, bloc, binit);
1423 }
1424
1425 this->dvar_ = bvar;
1426 return var_to_tree(bvar);
1427}
1428
ed3cd943
ILT
1429// Print a function descriptor expression.
1430
1431void
1432Func_descriptor_expression::do_dump_expression(Ast_dump_context* context) const
1433{
1434 context->ostream() << "[descriptor " << this->fn_->name() << "]";
1435}
1436
fdbc38a6
ILT
1437// Make a function descriptor expression.
1438
ed3cd943
ILT
1439Func_descriptor_expression*
1440Expression::make_func_descriptor(Named_object* fn)
fdbc38a6 1441{
ed3cd943 1442 return new Func_descriptor_expression(fn);
fdbc38a6
ILT
1443}
1444
1445// Make the function descriptor type, so that it can be converted.
1446
1447void
1448Expression::make_func_descriptor_type()
1449{
1450 Func_descriptor_expression::make_func_descriptor_type();
1451}
1452
1453// A reference to just the code of a function.
1454
1455class Func_code_reference_expression : public Expression
1456{
1457 public:
1458 Func_code_reference_expression(Named_object* function, Location location)
1459 : Expression(EXPRESSION_FUNC_CODE_REFERENCE, location),
1460 function_(function)
1461 { }
1462
1463 protected:
1464 int
1465 do_traverse(Traverse*)
1466 { return TRAVERSE_CONTINUE; }
1467
1468 Type*
1469 do_type()
1470 { return Type::make_pointer_type(Type::make_void_type()); }
1471
1472 void
1473 do_determine_type(const Type_context*)
1474 { }
1475
1476 Expression*
1477 do_copy()
1478 {
1479 return Expression::make_func_code_reference(this->function_,
1480 this->location());
1481 }
1482
1483 tree
1484 do_get_tree(Translate_context*);
1485
1486 void
1487 do_dump_expression(Ast_dump_context* context) const
1488 { context->ostream() << "[raw " << this->function_->name() << "]" ; }
1489
1490 private:
1491 // The function.
1492 Named_object* function_;
1493};
1494
1495// Get the tree for a reference to function code.
1496
1497tree
1498Func_code_reference_expression::do_get_tree(Translate_context* context)
1499{
b7d93b46
CM
1500 Bexpression* ret =
1501 Func_expression::get_code_pointer(context->gogo(), this->function_,
1502 this->location());
1503 return expr_to_tree(ret);
fdbc38a6
ILT
1504}
1505
1506// Make a reference to the code of a function.
1507
1508Expression*
1509Expression::make_func_code_reference(Named_object* function, Location location)
1510{
1511 return new Func_code_reference_expression(function, location);
1512}
1513
7a938933
ILT
1514// Class Unknown_expression.
1515
1516// Return the name of an unknown expression.
1517
1518const std::string&
1519Unknown_expression::name() const
1520{
1521 return this->named_object_->name();
1522}
1523
1524// Lower a reference to an unknown name.
1525
1526Expression*
8586635c 1527Unknown_expression::do_lower(Gogo*, Named_object*, Statement_inserter*, int)
7a938933 1528{
8afa2bfb 1529 Location location = this->location();
7a938933 1530 Named_object* no = this->named_object_;
62d1a8f9
ILT
1531 Named_object* real;
1532 if (!no->is_unknown())
1533 real = no;
1534 else
7a938933 1535 {
62d1a8f9
ILT
1536 real = no->unknown_value()->real_named_object();
1537 if (real == NULL)
1538 {
1539 if (this->is_composite_literal_key_)
1540 return this;
d4157849
ILT
1541 if (!this->no_error_message_)
1542 error_at(location, "reference to undefined name %qs",
1543 this->named_object_->message_name().c_str());
62d1a8f9
ILT
1544 return Expression::make_error(location);
1545 }
7a938933
ILT
1546 }
1547 switch (real->classification())
1548 {
1549 case Named_object::NAMED_OBJECT_CONST:
1550 return Expression::make_const_reference(real, location);
1551 case Named_object::NAMED_OBJECT_TYPE:
1552 return Expression::make_type(real->type_value(), location);
1553 case Named_object::NAMED_OBJECT_TYPE_DECLARATION:
1554 if (this->is_composite_literal_key_)
1555 return this;
d4157849
ILT
1556 if (!this->no_error_message_)
1557 error_at(location, "reference to undefined type %qs",
1558 real->message_name().c_str());
7a938933
ILT
1559 return Expression::make_error(location);
1560 case Named_object::NAMED_OBJECT_VAR:
b1b3aec1 1561 real->var_value()->set_is_used();
7a938933
ILT
1562 return Expression::make_var_reference(real, location);
1563 case Named_object::NAMED_OBJECT_FUNC:
1564 case Named_object::NAMED_OBJECT_FUNC_DECLARATION:
1565 return Expression::make_func_reference(real, NULL, location);
1566 case Named_object::NAMED_OBJECT_PACKAGE:
1567 if (this->is_composite_literal_key_)
1568 return this;
d4157849
ILT
1569 if (!this->no_error_message_)
1570 error_at(location, "unexpected reference to package");
7a938933
ILT
1571 return Expression::make_error(location);
1572 default:
8c0d1865 1573 go_unreachable();
7a938933
ILT
1574 }
1575}
1576
16c57fe2
RL
1577// Dump the ast representation for an unknown expression to a dump context.
1578
1579void
1580Unknown_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
1581{
1582 ast_dump_context->ostream() << "_Unknown_(" << this->named_object_->name()
1583 << ")";
16c57fe2
RL
1584}
1585
7a938933
ILT
1586// Make a reference to an unknown name.
1587
d4157849 1588Unknown_expression*
8afa2bfb 1589Expression::make_unknown_reference(Named_object* no, Location location)
7a938933 1590{
7a938933
ILT
1591 return new Unknown_expression(no, location);
1592}
1593
1594// A boolean expression.
1595
1596class Boolean_expression : public Expression
1597{
1598 public:
8afa2bfb 1599 Boolean_expression(bool val, Location location)
7a938933
ILT
1600 : Expression(EXPRESSION_BOOLEAN, location),
1601 val_(val), type_(NULL)
1602 { }
1603
1604 static Expression*
1605 do_import(Import*);
1606
1607 protected:
1608 bool
1609 do_is_constant() const
1610 { return true; }
1611
1612 Type*
1613 do_type();
1614
1615 void
1616 do_determine_type(const Type_context*);
1617
1618 Expression*
1619 do_copy()
1620 { return this; }
1621
1622 tree
1623 do_get_tree(Translate_context*)
1624 { return this->val_ ? boolean_true_node : boolean_false_node; }
1625
1626 void
1627 do_export(Export* exp) const
1628 { exp->write_c_string(this->val_ ? "true" : "false"); }
1629
16c57fe2
RL
1630 void
1631 do_dump_expression(Ast_dump_context* ast_dump_context) const
1632 { ast_dump_context->ostream() << (this->val_ ? "true" : "false"); }
1633
7a938933
ILT
1634 private:
1635 // The constant.
1636 bool val_;
1637 // The type as determined by context.
1638 Type* type_;
1639};
1640
1641// Get the type.
1642
1643Type*
1644Boolean_expression::do_type()
1645{
1646 if (this->type_ == NULL)
1647 this->type_ = Type::make_boolean_type();
1648 return this->type_;
1649}
1650
1651// Set the type from the context.
1652
1653void
1654Boolean_expression::do_determine_type(const Type_context* context)
1655{
1656 if (this->type_ != NULL && !this->type_->is_abstract())
1657 ;
1658 else if (context->type != NULL && context->type->is_boolean_type())
1659 this->type_ = context->type;
1660 else if (!context->may_be_abstract)
1661 this->type_ = Type::lookup_bool_type();
1662}
1663
1664// Import a boolean constant.
1665
1666Expression*
1667Boolean_expression::do_import(Import* imp)
1668{
1669 if (imp->peek_char() == 't')
1670 {
1671 imp->require_c_string("true");
1672 return Expression::make_boolean(true, imp->location());
1673 }
1674 else
1675 {
1676 imp->require_c_string("false");
1677 return Expression::make_boolean(false, imp->location());
1678 }
1679}
1680
1681// Make a boolean expression.
1682
1683Expression*
8afa2bfb 1684Expression::make_boolean(bool val, Location location)
7a938933
ILT
1685{
1686 return new Boolean_expression(val, location);
1687}
1688
1689// Class String_expression.
1690
1691// Get the type.
1692
1693Type*
1694String_expression::do_type()
1695{
1696 if (this->type_ == NULL)
1697 this->type_ = Type::make_string_type();
1698 return this->type_;
1699}
1700
1701// Set the type from the context.
1702
1703void
1704String_expression::do_determine_type(const Type_context* context)
1705{
1706 if (this->type_ != NULL && !this->type_->is_abstract())
1707 ;
1708 else if (context->type != NULL && context->type->is_string_type())
1709 this->type_ = context->type;
1710 else if (!context->may_be_abstract)
1711 this->type_ = Type::lookup_string_type();
1712}
1713
1714// Build a string constant.
1715
1716tree
1717String_expression::do_get_tree(Translate_context* context)
1718{
1719 return context->gogo()->go_string_constant_tree(this->val_);
1720}
1721
706cd57f 1722 // Write string literal to string dump.
7a938933
ILT
1723
1724void
706cd57f
RL
1725String_expression::export_string(String_dump* exp,
1726 const String_expression* str)
7a938933
ILT
1727{
1728 std::string s;
706cd57f 1729 s.reserve(str->val_.length() * 4 + 2);
7a938933 1730 s += '"';
706cd57f
RL
1731 for (std::string::const_iterator p = str->val_.begin();
1732 p != str->val_.end();
7a938933
ILT
1733 ++p)
1734 {
1735 if (*p == '\\' || *p == '"')
1736 {
1737 s += '\\';
1738 s += *p;
1739 }
1740 else if (*p >= 0x20 && *p < 0x7f)
1741 s += *p;
1742 else if (*p == '\n')
1743 s += "\\n";
1744 else if (*p == '\t')
1745 s += "\\t";
1746 else
1747 {
1748 s += "\\x";
1749 unsigned char c = *p;
1750 unsigned int dig = c >> 4;
1751 s += dig < 10 ? '0' + dig : 'A' + dig - 10;
1752 dig = c & 0xf;
1753 s += dig < 10 ? '0' + dig : 'A' + dig - 10;
1754 }
1755 }
1756 s += '"';
1757 exp->write_string(s);
1758}
1759
706cd57f
RL
1760// Export a string expression.
1761
1762void
1763String_expression::do_export(Export* exp) const
1764{
1765 String_expression::export_string(exp, this);
1766}
1767
7a938933
ILT
1768// Import a string expression.
1769
1770Expression*
1771String_expression::do_import(Import* imp)
1772{
1773 imp->require_c_string("\"");
1774 std::string val;
1775 while (true)
1776 {
1777 int c = imp->get_char();
1778 if (c == '"' || c == -1)
1779 break;
1780 if (c != '\\')
1781 val += static_cast<char>(c);
1782 else
1783 {
1784 c = imp->get_char();
1785 if (c == '\\' || c == '"')
1786 val += static_cast<char>(c);
1787 else if (c == 'n')
1788 val += '\n';
1789 else if (c == 't')
1790 val += '\t';
1791 else if (c == 'x')
1792 {
1793 c = imp->get_char();
1794 unsigned int vh = c >= '0' && c <= '9' ? c - '0' : c - 'A' + 10;
1795 c = imp->get_char();
1796 unsigned int vl = c >= '0' && c <= '9' ? c - '0' : c - 'A' + 10;
1797 char v = (vh << 4) | vl;
1798 val += v;
1799 }
1800 else
1801 {
1802 error_at(imp->location(), "bad string constant");
1803 return Expression::make_error(imp->location());
1804 }
1805 }
1806 }
1807 return Expression::make_string(val, imp->location());
1808}
1809
16c57fe2
RL
1810// Ast dump for string expression.
1811
1812void
1813String_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
1814{
706cd57f 1815 String_expression::export_string(ast_dump_context, this);
16c57fe2
RL
1816}
1817
7a938933
ILT
1818// Make a string expression.
1819
1820Expression*
8afa2bfb 1821Expression::make_string(const std::string& val, Location location)
7a938933
ILT
1822{
1823 return new String_expression(val, location);
1824}
1825
1826// Make an integer expression.
1827
1828class Integer_expression : public Expression
1829{
1830 public:
fb3f3aa2
ILT
1831 Integer_expression(const mpz_t* val, Type* type, bool is_character_constant,
1832 Location location)
7a938933 1833 : Expression(EXPRESSION_INTEGER, location),
fb3f3aa2 1834 type_(type), is_character_constant_(is_character_constant)
7a938933
ILT
1835 { mpz_init_set(this->val_, *val); }
1836
1837 static Expression*
1838 do_import(Import*);
1839
706cd57f 1840 // Write VAL to string dump.
7a938933 1841 static void
706cd57f 1842 export_integer(String_dump* exp, const mpz_t val);
7a938933 1843
16c57fe2
RL
1844 // Write VAL to dump context.
1845 static void
1846 dump_integer(Ast_dump_context* ast_dump_context, const mpz_t val);
1847
7a938933
ILT
1848 protected:
1849 bool
1850 do_is_constant() const
1851 { return true; }
1852
1853 bool
5caf63ca 1854 do_numeric_constant_value(Numeric_constant* nc) const;
7a938933
ILT
1855
1856 Type*
1857 do_type();
1858
1859 void
1860 do_determine_type(const Type_context* context);
1861
1862 void
1863 do_check_types(Gogo*);
1864
1865 tree
1866 do_get_tree(Translate_context*);
1867
1868 Expression*
1869 do_copy()
fb3f3aa2
ILT
1870 {
1871 if (this->is_character_constant_)
1872 return Expression::make_character(&this->val_, this->type_,
1873 this->location());
1874 else
1875 return Expression::make_integer(&this->val_, this->type_,
1876 this->location());
1877 }
7a938933
ILT
1878
1879 void
1880 do_export(Export*) const;
1881
16c57fe2
RL
1882 void
1883 do_dump_expression(Ast_dump_context*) const;
1884
7a938933
ILT
1885 private:
1886 // The integer value.
1887 mpz_t val_;
1888 // The type so far.
1889 Type* type_;
fb3f3aa2
ILT
1890 // Whether this is a character constant.
1891 bool is_character_constant_;
7a938933
ILT
1892};
1893
5caf63ca
ILT
1894// Return a numeric constant for this expression. We have to mark
1895// this as a character when appropriate.
7a938933
ILT
1896
1897bool
5caf63ca 1898Integer_expression::do_numeric_constant_value(Numeric_constant* nc) const
7a938933 1899{
5caf63ca
ILT
1900 if (this->is_character_constant_)
1901 nc->set_rune(this->type_, this->val_);
1902 else
1903 nc->set_int(this->type_, this->val_);
7a938933
ILT
1904 return true;
1905}
1906
1907// Return the current type. If we haven't set the type yet, we return
1908// an abstract integer type.
1909
1910Type*
1911Integer_expression::do_type()
1912{
1913 if (this->type_ == NULL)
fb3f3aa2
ILT
1914 {
1915 if (this->is_character_constant_)
1916 this->type_ = Type::make_abstract_character_type();
1917 else
1918 this->type_ = Type::make_abstract_integer_type();
1919 }
7a938933
ILT
1920 return this->type_;
1921}
1922
1923// Set the type of the integer value. Here we may switch from an
1924// abstract type to a real type.
1925
1926void
1927Integer_expression::do_determine_type(const Type_context* context)
1928{
1929 if (this->type_ != NULL && !this->type_->is_abstract())
1930 ;
5caf63ca 1931 else if (context->type != NULL && context->type->is_numeric_type())
7a938933
ILT
1932 this->type_ = context->type;
1933 else if (!context->may_be_abstract)
fb3f3aa2
ILT
1934 {
1935 if (this->is_character_constant_)
1936 this->type_ = Type::lookup_integer_type("int32");
1937 else
1938 this->type_ = Type::lookup_integer_type("int");
1939 }
7a938933
ILT
1940}
1941
7a938933
ILT
1942// Check the type of an integer constant.
1943
1944void
1945Integer_expression::do_check_types(Gogo*)
1946{
5caf63ca
ILT
1947 Type* type = this->type_;
1948 if (type == NULL)
7a938933 1949 return;
5caf63ca
ILT
1950 Numeric_constant nc;
1951 if (this->is_character_constant_)
1952 nc.set_rune(NULL, this->val_);
1953 else
1954 nc.set_int(NULL, this->val_);
1955 if (!nc.set_type(type, true, this->location()))
7a938933
ILT
1956 this->set_is_error();
1957}
1958
1959// Get a tree for an integer constant.
1960
1961tree
1962Integer_expression::do_get_tree(Translate_context* context)
1963{
002ee4d1 1964 Type* resolved_type = NULL;
7a938933 1965 if (this->type_ != NULL && !this->type_->is_abstract())
002ee4d1 1966 resolved_type = this->type_;
7a938933
ILT
1967 else if (this->type_ != NULL && this->type_->float_type() != NULL)
1968 {
1969 // We are converting to an abstract floating point type.
002ee4d1 1970 resolved_type = Type::lookup_float_type("float64");
7a938933
ILT
1971 }
1972 else if (this->type_ != NULL && this->type_->complex_type() != NULL)
1973 {
1974 // We are converting to an abstract complex type.
002ee4d1 1975 resolved_type = Type::lookup_complex_type("complex128");
7a938933
ILT
1976 }
1977 else
1978 {
1979 // If we still have an abstract type here, then this is being
1980 // used in a constant expression which didn't get reduced for
1981 // some reason. Use a type which will fit the value. We use <,
1982 // not <=, because we need an extra bit for the sign bit.
1983 int bits = mpz_sizeinbase(this->val_, 2);
776f27a6
ILT
1984 Type* int_type = Type::lookup_integer_type("int");
1985 if (bits < int_type->integer_type()->bits())
002ee4d1 1986 resolved_type = int_type;
7a938933 1987 else if (bits < 64)
002ee4d1 1988 resolved_type = Type::lookup_integer_type("int64");
7a938933 1989 else
002ee4d1
CM
1990 {
1991 if (!saw_errors())
1992 error_at(this->location(),
1993 "unknown type for large integer constant");
1994 Bexpression* ret = context->gogo()->backend()->error_expression();
1995 return expr_to_tree(ret);
1996 }
7a938933 1997 }
002ee4d1
CM
1998 Numeric_constant nc;
1999 nc.set_int(resolved_type, this->val_);
2000 Bexpression* ret =
2001 Expression::backend_numeric_constant_expression(context, &nc);
2002 return expr_to_tree(ret);
7a938933
ILT
2003}
2004
2005// Write VAL to export data.
2006
2007void
706cd57f 2008Integer_expression::export_integer(String_dump* exp, const mpz_t val)
7a938933
ILT
2009{
2010 char* s = mpz_get_str(NULL, 10, val);
2011 exp->write_c_string(s);
2012 free(s);
2013}
2014
2015// Export an integer in a constant expression.
2016
2017void
2018Integer_expression::do_export(Export* exp) const
2019{
2020 Integer_expression::export_integer(exp, this->val_);
fb3f3aa2
ILT
2021 if (this->is_character_constant_)
2022 exp->write_c_string("'");
7a938933
ILT
2023 // A trailing space lets us reliably identify the end of the number.
2024 exp->write_c_string(" ");
2025}
2026
2027// Import an integer, floating point, or complex value. This handles
2028// all these types because they all start with digits.
2029
2030Expression*
2031Integer_expression::do_import(Import* imp)
2032{
2033 std::string num = imp->read_identifier();
2034 imp->require_c_string(" ");
2035 if (!num.empty() && num[num.length() - 1] == 'i')
2036 {
2037 mpfr_t real;
2038 size_t plus_pos = num.find('+', 1);
2039 size_t minus_pos = num.find('-', 1);
2040 size_t pos;
2041 if (plus_pos == std::string::npos)
2042 pos = minus_pos;
2043 else if (minus_pos == std::string::npos)
2044 pos = plus_pos;
2045 else
2046 {
2047 error_at(imp->location(), "bad number in import data: %qs",
2048 num.c_str());
2049 return Expression::make_error(imp->location());
2050 }
2051 if (pos == std::string::npos)
2052 mpfr_set_ui(real, 0, GMP_RNDN);
2053 else
2054 {
2055 std::string real_str = num.substr(0, pos);
2056 if (mpfr_init_set_str(real, real_str.c_str(), 10, GMP_RNDN) != 0)
2057 {
2058 error_at(imp->location(), "bad number in import data: %qs",
2059 real_str.c_str());
2060 return Expression::make_error(imp->location());
2061 }
2062 }
2063
2064 std::string imag_str;
2065 if (pos == std::string::npos)
2066 imag_str = num;
2067 else
2068 imag_str = num.substr(pos);
2069 imag_str = imag_str.substr(0, imag_str.size() - 1);
2070 mpfr_t imag;
2071 if (mpfr_init_set_str(imag, imag_str.c_str(), 10, GMP_RNDN) != 0)
2072 {
2073 error_at(imp->location(), "bad number in import data: %qs",
2074 imag_str.c_str());
2075 return Expression::make_error(imp->location());
2076 }
2077 Expression* ret = Expression::make_complex(&real, &imag, NULL,
2078 imp->location());
2079 mpfr_clear(real);
2080 mpfr_clear(imag);
2081 return ret;
2082 }
2083 else if (num.find('.') == std::string::npos
2084 && num.find('E') == std::string::npos)
2085 {
fb3f3aa2
ILT
2086 bool is_character_constant = (!num.empty()
2087 && num[num.length() - 1] == '\'');
2088 if (is_character_constant)
2089 num = num.substr(0, num.length() - 1);
7a938933
ILT
2090 mpz_t val;
2091 if (mpz_init_set_str(val, num.c_str(), 10) != 0)
2092 {
2093 error_at(imp->location(), "bad number in import data: %qs",
2094 num.c_str());
2095 return Expression::make_error(imp->location());
2096 }
fb3f3aa2
ILT
2097 Expression* ret;
2098 if (is_character_constant)
2099 ret = Expression::make_character(&val, NULL, imp->location());
2100 else
2101 ret = Expression::make_integer(&val, NULL, imp->location());
7a938933
ILT
2102 mpz_clear(val);
2103 return ret;
2104 }
2105 else
2106 {
2107 mpfr_t val;
2108 if (mpfr_init_set_str(val, num.c_str(), 10, GMP_RNDN) != 0)
2109 {
2110 error_at(imp->location(), "bad number in import data: %qs",
2111 num.c_str());
2112 return Expression::make_error(imp->location());
2113 }
2114 Expression* ret = Expression::make_float(&val, NULL, imp->location());
2115 mpfr_clear(val);
2116 return ret;
2117 }
2118}
16c57fe2
RL
2119// Ast dump for integer expression.
2120
2121void
2122Integer_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
2123{
fb3f3aa2
ILT
2124 if (this->is_character_constant_)
2125 ast_dump_context->ostream() << '\'';
706cd57f 2126 Integer_expression::export_integer(ast_dump_context, this->val_);
fb3f3aa2
ILT
2127 if (this->is_character_constant_)
2128 ast_dump_context->ostream() << '\'';
16c57fe2
RL
2129}
2130
7a938933
ILT
2131// Build a new integer value.
2132
2133Expression*
fb3f3aa2
ILT
2134Expression::make_integer(const mpz_t* val, Type* type, Location location)
2135{
2136 return new Integer_expression(val, type, false, location);
2137}
2138
2139// Build a new character constant value.
2140
2141Expression*
2142Expression::make_character(const mpz_t* val, Type* type, Location location)
7a938933 2143{
fb3f3aa2 2144 return new Integer_expression(val, type, true, location);
7a938933
ILT
2145}
2146
2147// Floats.
2148
2149class Float_expression : public Expression
2150{
2151 public:
8afa2bfb 2152 Float_expression(const mpfr_t* val, Type* type, Location location)
7a938933
ILT
2153 : Expression(EXPRESSION_FLOAT, location),
2154 type_(type)
2155 {
2156 mpfr_init_set(this->val_, *val, GMP_RNDN);
2157 }
2158
7a938933
ILT
2159 // Write VAL to export data.
2160 static void
706cd57f
RL
2161 export_float(String_dump* exp, const mpfr_t val);
2162
16c57fe2
RL
2163 // Write VAL to dump file.
2164 static void
2165 dump_float(Ast_dump_context* ast_dump_context, const mpfr_t val);
7a938933
ILT
2166
2167 protected:
2168 bool
2169 do_is_constant() const
2170 { return true; }
2171
2172 bool
5caf63ca
ILT
2173 do_numeric_constant_value(Numeric_constant* nc) const
2174 {
2175 nc->set_float(this->type_, this->val_);
2176 return true;
2177 }
7a938933
ILT
2178
2179 Type*
2180 do_type();
2181
2182 void
2183 do_determine_type(const Type_context*);
2184
2185 void
2186 do_check_types(Gogo*);
2187
2188 Expression*
2189 do_copy()
2190 { return Expression::make_float(&this->val_, this->type_,
2191 this->location()); }
2192
2193 tree
2194 do_get_tree(Translate_context*);
2195
2196 void
2197 do_export(Export*) const;
2198
16c57fe2
RL
2199 void
2200 do_dump_expression(Ast_dump_context*) const;
2201
7a938933
ILT
2202 private:
2203 // The floating point value.
2204 mpfr_t val_;
2205 // The type so far.
2206 Type* type_;
2207};
2208
7a938933
ILT
2209// Return the current type. If we haven't set the type yet, we return
2210// an abstract float type.
2211
2212Type*
2213Float_expression::do_type()
2214{
2215 if (this->type_ == NULL)
2216 this->type_ = Type::make_abstract_float_type();
2217 return this->type_;
2218}
2219
2220// Set the type of the float value. Here we may switch from an
2221// abstract type to a real type.
2222
2223void
2224Float_expression::do_determine_type(const Type_context* context)
2225{
2226 if (this->type_ != NULL && !this->type_->is_abstract())
2227 ;
2228 else if (context->type != NULL
2229 && (context->type->integer_type() != NULL
2230 || context->type->float_type() != NULL
2231 || context->type->complex_type() != NULL))
2232 this->type_ = context->type;
2233 else if (!context->may_be_abstract)
ff5f50c5 2234 this->type_ = Type::lookup_float_type("float64");
7a938933
ILT
2235}
2236
7a938933
ILT
2237// Check the type of a float value.
2238
2239void
2240Float_expression::do_check_types(Gogo*)
2241{
5caf63ca
ILT
2242 Type* type = this->type_;
2243 if (type == NULL)
7a938933 2244 return;
5caf63ca
ILT
2245 Numeric_constant nc;
2246 nc.set_float(NULL, this->val_);
2247 if (!nc.set_type(this->type_, true, this->location()))
7a938933 2248 this->set_is_error();
7a938933
ILT
2249}
2250
2251// Get a tree for a float constant.
2252
2253tree
2254Float_expression::do_get_tree(Translate_context* context)
2255{
002ee4d1 2256 Type* resolved_type;
7a938933 2257 if (this->type_ != NULL && !this->type_->is_abstract())
002ee4d1 2258 resolved_type = this->type_;
7a938933
ILT
2259 else if (this->type_ != NULL && this->type_->integer_type() != NULL)
2260 {
2261 // We have an abstract integer type. We just hope for the best.
002ee4d1
CM
2262 resolved_type = Type::lookup_integer_type("int");
2263 }
2264 else if (this->type_ != NULL && this->type_->complex_type() != NULL)
2265 {
2266 // We are converting to an abstract complex type.
2267 resolved_type = Type::lookup_complex_type("complex128");
7a938933
ILT
2268 }
2269 else
2270 {
2271 // If we still have an abstract type here, then this is being
2272 // used in a constant expression which didn't get reduced. We
2273 // just use float64 and hope for the best.
002ee4d1 2274 resolved_type = Type::lookup_float_type("float64");
7a938933 2275 }
002ee4d1
CM
2276
2277 Numeric_constant nc;
2278 nc.set_float(resolved_type, this->val_);
2279 Bexpression* ret =
2280 Expression::backend_numeric_constant_expression(context, &nc);
2281 return expr_to_tree(ret);
7a938933
ILT
2282}
2283
706cd57f 2284// Write a floating point number to a string dump.
7a938933
ILT
2285
2286void
706cd57f 2287Float_expression::export_float(String_dump *exp, const mpfr_t val)
7a938933
ILT
2288{
2289 mp_exp_t exponent;
2290 char* s = mpfr_get_str(NULL, &exponent, 10, 0, val, GMP_RNDN);
2291 if (*s == '-')
2292 exp->write_c_string("-");
2293 exp->write_c_string("0.");
2294 exp->write_c_string(*s == '-' ? s + 1 : s);
2295 mpfr_free_str(s);
2296 char buf[30];
2297 snprintf(buf, sizeof buf, "E%ld", exponent);
2298 exp->write_c_string(buf);
2299}
2300
2301// Export a floating point number in a constant expression.
2302
2303void
2304Float_expression::do_export(Export* exp) const
2305{
2306 Float_expression::export_float(exp, this->val_);
2307 // A trailing space lets us reliably identify the end of the number.
2308 exp->write_c_string(" ");
2309}
2310
16c57fe2
RL
2311// Dump a floating point number to the dump file.
2312
2313void
2314Float_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
2315{
706cd57f 2316 Float_expression::export_float(ast_dump_context, this->val_);
16c57fe2
RL
2317}
2318
7a938933
ILT
2319// Make a float expression.
2320
2321Expression*
8afa2bfb 2322Expression::make_float(const mpfr_t* val, Type* type, Location location)
7a938933
ILT
2323{
2324 return new Float_expression(val, type, location);
2325}
2326
2327// Complex numbers.
2328
2329class Complex_expression : public Expression
2330{
2331 public:
2332 Complex_expression(const mpfr_t* real, const mpfr_t* imag, Type* type,
8afa2bfb 2333 Location location)
7a938933
ILT
2334 : Expression(EXPRESSION_COMPLEX, location),
2335 type_(type)
2336 {
2337 mpfr_init_set(this->real_, *real, GMP_RNDN);
2338 mpfr_init_set(this->imag_, *imag, GMP_RNDN);
2339 }
2340
706cd57f 2341 // Write REAL/IMAG to string dump.
7a938933 2342 static void
706cd57f 2343 export_complex(String_dump* exp, const mpfr_t real, const mpfr_t val);
7a938933 2344
16c57fe2
RL
2345 // Write REAL/IMAG to dump context.
2346 static void
2347 dump_complex(Ast_dump_context* ast_dump_context,
2348 const mpfr_t real, const mpfr_t val);
2349
7a938933
ILT
2350 protected:
2351 bool
2352 do_is_constant() const
2353 { return true; }
2354
2355 bool
5caf63ca
ILT
2356 do_numeric_constant_value(Numeric_constant* nc) const
2357 {
2358 nc->set_complex(this->type_, this->real_, this->imag_);
2359 return true;
2360 }
7a938933
ILT
2361
2362 Type*
2363 do_type();
2364
2365 void
2366 do_determine_type(const Type_context*);
2367
2368 void
2369 do_check_types(Gogo*);
2370
2371 Expression*
2372 do_copy()
2373 {
2374 return Expression::make_complex(&this->real_, &this->imag_, this->type_,
2375 this->location());
2376 }
2377
2378 tree
2379 do_get_tree(Translate_context*);
2380
2381 void
2382 do_export(Export*) const;
2383
16c57fe2
RL
2384 void
2385 do_dump_expression(Ast_dump_context*) const;
2386
7a938933
ILT
2387 private:
2388 // The real part.
2389 mpfr_t real_;
2390 // The imaginary part;
2391 mpfr_t imag_;
2392 // The type if known.
2393 Type* type_;
2394};
2395
7a938933
ILT
2396// Return the current type. If we haven't set the type yet, we return
2397// an abstract complex type.
2398
2399Type*
2400Complex_expression::do_type()
2401{
2402 if (this->type_ == NULL)
2403 this->type_ = Type::make_abstract_complex_type();
2404 return this->type_;
2405}
2406
2407// Set the type of the complex value. Here we may switch from an
2408// abstract type to a real type.
2409
2410void
2411Complex_expression::do_determine_type(const Type_context* context)
2412{
2413 if (this->type_ != NULL && !this->type_->is_abstract())
2414 ;
2415 else if (context->type != NULL
2416 && context->type->complex_type() != NULL)
2417 this->type_ = context->type;
2418 else if (!context->may_be_abstract)
ff5f50c5 2419 this->type_ = Type::lookup_complex_type("complex128");
7a938933
ILT
2420}
2421
7a938933
ILT
2422// Check the type of a complex value.
2423
2424void
2425Complex_expression::do_check_types(Gogo*)
2426{
5caf63ca
ILT
2427 Type* type = this->type_;
2428 if (type == NULL)
7a938933 2429 return;
5caf63ca
ILT
2430 Numeric_constant nc;
2431 nc.set_complex(NULL, this->real_, this->imag_);
2432 if (!nc.set_type(this->type_, true, this->location()))
7a938933
ILT
2433 this->set_is_error();
2434}
2435
2436// Get a tree for a complex constant.
2437
2438tree
2439Complex_expression::do_get_tree(Translate_context* context)
2440{
002ee4d1 2441 Type* resolved_type;
7a938933 2442 if (this->type_ != NULL && !this->type_->is_abstract())
002ee4d1
CM
2443 resolved_type = this->type_;
2444 else if (this->type_ != NULL && this->type_->integer_type() != NULL)
2445 {
2446 // We are converting to an abstract integer type.
2447 resolved_type = Type::lookup_integer_type("int");
2448 }
2449 else if (this->type_ != NULL && this->type_->float_type() != NULL)
2450 {
2451 // We are converting to an abstract float type.
2452 resolved_type = Type::lookup_float_type("float64");
2453 }
7a938933
ILT
2454 else
2455 {
2456 // If we still have an abstract type here, this this is being
2457 // used in a constant expression which didn't get reduced. We
2458 // just use complex128 and hope for the best.
002ee4d1 2459 resolved_type = Type::lookup_complex_type("complex128");
7a938933 2460 }
002ee4d1
CM
2461
2462 Numeric_constant nc;
2463 nc.set_complex(resolved_type, this->real_, this->imag_);
2464 Bexpression* ret =
2465 Expression::backend_numeric_constant_expression(context, &nc);
2466 return expr_to_tree(ret);
7a938933
ILT
2467}
2468
2469// Write REAL/IMAG to export data.
2470
2471void
706cd57f 2472Complex_expression::export_complex(String_dump* exp, const mpfr_t real,
7a938933
ILT
2473 const mpfr_t imag)
2474{
2475 if (!mpfr_zero_p(real))
2476 {
2477 Float_expression::export_float(exp, real);
2478 if (mpfr_sgn(imag) > 0)
2479 exp->write_c_string("+");
2480 }
2481 Float_expression::export_float(exp, imag);
2482 exp->write_c_string("i");
2483}
2484
2485// Export a complex number in a constant expression.
2486
2487void
2488Complex_expression::do_export(Export* exp) const
2489{
2490 Complex_expression::export_complex(exp, this->real_, this->imag_);
2491 // A trailing space lets us reliably identify the end of the number.
2492 exp->write_c_string(" ");
2493}
2494
16c57fe2
RL
2495// Dump a complex expression to the dump file.
2496
2497void
2498Complex_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
2499{
706cd57f 2500 Complex_expression::export_complex(ast_dump_context,
16c57fe2
RL
2501 this->real_,
2502 this->imag_);
2503}
2504
7a938933
ILT
2505// Make a complex expression.
2506
2507Expression*
2508Expression::make_complex(const mpfr_t* real, const mpfr_t* imag, Type* type,
8afa2bfb 2509 Location location)
7a938933
ILT
2510{
2511 return new Complex_expression(real, imag, type, location);
2512}
2513
6e25f095
ILT
2514// Find a named object in an expression.
2515
2516class Find_named_object : public Traverse
2517{
2518 public:
2519 Find_named_object(Named_object* no)
2520 : Traverse(traverse_expressions),
2521 no_(no), found_(false)
2522 { }
2523
2524 // Whether we found the object.
2525 bool
2526 found() const
2527 { return this->found_; }
2528
2529 protected:
2530 int
2531 expression(Expression**);
2532
2533 private:
2534 // The object we are looking for.
2535 Named_object* no_;
2536 // Whether we found it.
2537 bool found_;
2538};
2539
7a938933
ILT
2540// A reference to a const in an expression.
2541
2542class Const_expression : public Expression
2543{
2544 public:
8afa2bfb 2545 Const_expression(Named_object* constant, Location location)
7a938933 2546 : Expression(EXPRESSION_CONST_REFERENCE, location),
f80d990e 2547 constant_(constant), type_(NULL), seen_(false)
7a938933
ILT
2548 { }
2549
6e25f095
ILT
2550 Named_object*
2551 named_object()
2552 { return this->constant_; }
2553
036c8f37
ILT
2554 // Check that the initializer does not refer to the constant itself.
2555 void
2556 check_for_init_loop();
2557
7a938933 2558 protected:
b68a9b10
ILT
2559 int
2560 do_traverse(Traverse*);
2561
7a938933 2562 Expression*
8586635c 2563 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
7a938933
ILT
2564
2565 bool
2566 do_is_constant() const
2567 { return true; }
2568
2569 bool
5caf63ca 2570 do_numeric_constant_value(Numeric_constant* nc) const;
7a938933
ILT
2571
2572 bool
2dfc736c 2573 do_string_constant_value(std::string* val) const;
7a938933
ILT
2574
2575 Type*
2576 do_type();
2577
2578 // The type of a const is set by the declaration, not the use.
2579 void
2580 do_determine_type(const Type_context*);
2581
2582 void
2583 do_check_types(Gogo*);
2584
2585 Expression*
2586 do_copy()
2587 { return this; }
2588
2589 tree
2590 do_get_tree(Translate_context* context);
2591
2592 // When exporting a reference to a const as part of a const
2593 // expression, we export the value. We ignore the fact that it has
2594 // a name.
2595 void
2596 do_export(Export* exp) const
2597 { this->constant_->const_value()->expr()->export_expression(exp); }
2598
16c57fe2
RL
2599 void
2600 do_dump_expression(Ast_dump_context*) const;
2601
7a938933
ILT
2602 private:
2603 // The constant.
2604 Named_object* constant_;
2605 // The type of this reference. This is used if the constant has an
2606 // abstract type.
2607 Type* type_;
f80d990e
ILT
2608 // Used to prevent infinite recursion when a constant incorrectly
2609 // refers to itself.
2610 mutable bool seen_;
7a938933
ILT
2611};
2612
b68a9b10
ILT
2613// Traversal.
2614
2615int
2616Const_expression::do_traverse(Traverse* traverse)
2617{
2618 if (this->type_ != NULL)
2619 return Type::traverse(this->type_, traverse);
2620 return TRAVERSE_CONTINUE;
2621}
2622
7a938933
ILT
2623// Lower a constant expression. This is where we convert the
2624// predeclared constant iota into an integer value.
2625
2626Expression*
8586635c
ILT
2627Const_expression::do_lower(Gogo* gogo, Named_object*,
2628 Statement_inserter*, int iota_value)
7a938933
ILT
2629{
2630 if (this->constant_->const_value()->expr()->classification()
2631 == EXPRESSION_IOTA)
2632 {
2633 if (iota_value == -1)
2634 {
2635 error_at(this->location(),
2636 "iota is only defined in const declarations");
2637 iota_value = 0;
2638 }
2639 mpz_t val;
2640 mpz_init_set_ui(val, static_cast<unsigned long>(iota_value));
2641 Expression* ret = Expression::make_integer(&val, NULL,
2642 this->location());
2643 mpz_clear(val);
2644 return ret;
2645 }
2646
2647 // Make sure that the constant itself has been lowered.
2648 gogo->lower_constant(this->constant_);
2649
2650 return this;
2651}
2652
5caf63ca 2653// Return a numeric constant value.
7a938933
ILT
2654
2655bool
5caf63ca 2656Const_expression::do_numeric_constant_value(Numeric_constant* nc) const
7a938933 2657{
f80d990e
ILT
2658 if (this->seen_)
2659 return false;
2660
7a938933 2661 Expression* e = this->constant_->const_value()->expr();
5caf63ca 2662
f80d990e
ILT
2663 this->seen_ = true;
2664
5caf63ca 2665 bool r = e->numeric_constant_value(nc);
7a938933 2666
f80d990e
ILT
2667 this->seen_ = false;
2668
7a938933
ILT
2669 Type* ctype;
2670 if (this->type_ != NULL)
2671 ctype = this->type_;
2672 else
2673 ctype = this->constant_->const_value()->type();
7a938933
ILT
2674 if (r && ctype != NULL)
2675 {
5caf63ca 2676 if (!nc->set_type(ctype, false, this->location()))
7a938933 2677 return false;
7a938933 2678 }
7a938933 2679
7a938933
ILT
2680 return r;
2681}
2682
2dfc736c
ILT
2683bool
2684Const_expression::do_string_constant_value(std::string* val) const
2685{
2686 if (this->seen_)
2687 return false;
2688
2689 Expression* e = this->constant_->const_value()->expr();
2690
2691 this->seen_ = true;
2692 bool ok = e->string_constant_value(val);
2693 this->seen_ = false;
2694
2695 return ok;
2696}
2697
7a938933
ILT
2698// Return the type of the const reference.
2699
2700Type*
2701Const_expression::do_type()
2702{
2703 if (this->type_ != NULL)
2704 return this->type_;
f80d990e 2705
8e6c2a27
ILT
2706 Named_constant* nc = this->constant_->const_value();
2707
2708 if (this->seen_ || nc->lowering())
f80d990e
ILT
2709 {
2710 this->report_error(_("constant refers to itself"));
2711 this->type_ = Type::make_error_type();
2712 return this->type_;
2713 }
2714
2715 this->seen_ = true;
2716
7a938933 2717 Type* ret = nc->type();
f80d990e 2718
7a938933 2719 if (ret != NULL)
f80d990e
ILT
2720 {
2721 this->seen_ = false;
2722 return ret;
2723 }
2724
7a938933
ILT
2725 // During parsing, a named constant may have a NULL type, but we
2726 // must not return a NULL type here.
f80d990e
ILT
2727 ret = nc->expr()->type();
2728
2729 this->seen_ = false;
2730
2731 return ret;
7a938933
ILT
2732}
2733
2734// Set the type of the const reference.
2735
2736void
2737Const_expression::do_determine_type(const Type_context* context)
2738{
2739 Type* ctype = this->constant_->const_value()->type();
2740 Type* cetype = (ctype != NULL
2741 ? ctype
2742 : this->constant_->const_value()->expr()->type());
2743 if (ctype != NULL && !ctype->is_abstract())
2744 ;
2745 else if (context->type != NULL
5caf63ca
ILT
2746 && context->type->is_numeric_type()
2747 && cetype->is_numeric_type())
7a938933
ILT
2748 this->type_ = context->type;
2749 else if (context->type != NULL
2750 && context->type->is_string_type()
2751 && cetype->is_string_type())
2752 this->type_ = context->type;
2753 else if (context->type != NULL
2754 && context->type->is_boolean_type()
2755 && cetype->is_boolean_type())
2756 this->type_ = context->type;
2757 else if (!context->may_be_abstract)
2758 {
2759 if (cetype->is_abstract())
2760 cetype = cetype->make_non_abstract_type();
2761 this->type_ = cetype;
2762 }
2763}
2764
036c8f37
ILT
2765// Check for a loop in which the initializer of a constant refers to
2766// the constant itself.
7a938933
ILT
2767
2768void
036c8f37 2769Const_expression::check_for_init_loop()
7a938933 2770{
02ed921a 2771 if (this->type_ != NULL && this->type_->is_error())
6e25f095
ILT
2772 return;
2773
036c8f37
ILT
2774 if (this->seen_)
2775 {
2776 this->report_error(_("constant refers to itself"));
2777 this->type_ = Type::make_error_type();
2778 return;
2779 }
2780
6e25f095
ILT
2781 Expression* init = this->constant_->const_value()->expr();
2782 Find_named_object find_named_object(this->constant_);
036c8f37
ILT
2783
2784 this->seen_ = true;
6e25f095 2785 Expression::traverse(&init, &find_named_object);
036c8f37
ILT
2786 this->seen_ = false;
2787
6e25f095
ILT
2788 if (find_named_object.found())
2789 {
02ed921a 2790 if (this->type_ == NULL || !this->type_->is_error())
036c8f37
ILT
2791 {
2792 this->report_error(_("constant refers to itself"));
2793 this->type_ = Type::make_error_type();
2794 }
6e25f095
ILT
2795 return;
2796 }
036c8f37
ILT
2797}
2798
2799// Check types of a const reference.
2800
2801void
2802Const_expression::do_check_types(Gogo*)
2803{
02ed921a 2804 if (this->type_ != NULL && this->type_->is_error())
036c8f37
ILT
2805 return;
2806
2807 this->check_for_init_loop();
6e25f095 2808
5caf63ca
ILT
2809 // Check that numeric constant fits in type.
2810 if (this->type_ != NULL && this->type_->is_numeric_type())
7a938933 2811 {
5caf63ca
ILT
2812 Numeric_constant nc;
2813 if (this->constant_->const_value()->expr()->numeric_constant_value(&nc))
7a938933 2814 {
5caf63ca
ILT
2815 if (!nc.set_type(this->type_, true, this->location()))
2816 this->set_is_error();
7a938933 2817 }
7a938933
ILT
2818 }
2819}
2820
2821// Return a tree for the const reference.
2822
2823tree
2824Const_expression::do_get_tree(Translate_context* context)
2825{
2826 Gogo* gogo = context->gogo();
2827 tree type_tree;
2828 if (this->type_ == NULL)
2829 type_tree = NULL_TREE;
2830 else
2831 {
5b735706 2832 type_tree = type_to_tree(this->type_->get_backend(gogo));
7a938933
ILT
2833 if (type_tree == error_mark_node)
2834 return error_mark_node;
2835 }
2836
2837 // If the type has been set for this expression, but the underlying
2838 // object is an abstract int or float, we try to get the abstract
2839 // value. Otherwise we may lose something in the conversion.
2840 if (this->type_ != NULL
5caf63ca 2841 && this->type_->is_numeric_type()
1ed36e90
ILT
2842 && (this->constant_->const_value()->type() == NULL
2843 || this->constant_->const_value()->type()->is_abstract()))
7a938933
ILT
2844 {
2845 Expression* expr = this->constant_->const_value()->expr();
5caf63ca
ILT
2846 Numeric_constant nc;
2847 if (expr->numeric_constant_value(&nc)
2848 && nc.set_type(this->type_, false, this->location()))
7a938933 2849 {
5caf63ca
ILT
2850 Expression* e = nc.expression(this->location());
2851 return e->get_tree(context);
7a938933 2852 }
7a938933
ILT
2853 }
2854
2855 tree const_tree = this->constant_->get_tree(gogo, context->function());
2856 if (this->type_ == NULL
2857 || const_tree == error_mark_node
2858 || TREE_TYPE(const_tree) == error_mark_node)
2859 return const_tree;
2860
2861 tree ret;
2862 if (TYPE_MAIN_VARIANT(type_tree) == TYPE_MAIN_VARIANT(TREE_TYPE(const_tree)))
2863 ret = fold_convert(type_tree, const_tree);
2864 else if (TREE_CODE(type_tree) == INTEGER_TYPE)
2865 ret = fold(convert_to_integer(type_tree, const_tree));
2866 else if (TREE_CODE(type_tree) == REAL_TYPE)
2867 ret = fold(convert_to_real(type_tree, const_tree));
2868 else if (TREE_CODE(type_tree) == COMPLEX_TYPE)
2869 ret = fold(convert_to_complex(type_tree, const_tree));
2870 else
8c0d1865 2871 go_unreachable();
7a938933
ILT
2872 return ret;
2873}
2874
16c57fe2
RL
2875// Dump ast representation for constant expression.
2876
2877void
2878Const_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
2879{
2880 ast_dump_context->ostream() << this->constant_->name();
2881}
2882
7a938933
ILT
2883// Make a reference to a constant in an expression.
2884
2885Expression*
2886Expression::make_const_reference(Named_object* constant,
8afa2bfb 2887 Location location)
7a938933
ILT
2888{
2889 return new Const_expression(constant, location);
2890}
2891
6e25f095
ILT
2892// Find a named object in an expression.
2893
2894int
2895Find_named_object::expression(Expression** pexpr)
2896{
2897 switch ((*pexpr)->classification())
2898 {
2899 case Expression::EXPRESSION_CONST_REFERENCE:
036c8f37
ILT
2900 {
2901 Const_expression* ce = static_cast<Const_expression*>(*pexpr);
2902 if (ce->named_object() == this->no_)
2903 break;
2904
2905 // We need to check a constant initializer explicitly, as
2906 // loops here will not be caught by the loop checking for
2907 // variable initializers.
2908 ce->check_for_init_loop();
2909
2910 return TRAVERSE_CONTINUE;
2911 }
2912
6e25f095
ILT
2913 case Expression::EXPRESSION_VAR_REFERENCE:
2914 if ((*pexpr)->var_expression()->named_object() == this->no_)
2915 break;
2916 return TRAVERSE_CONTINUE;
2917 case Expression::EXPRESSION_FUNC_REFERENCE:
2918 if ((*pexpr)->func_expression()->named_object() == this->no_)
2919 break;
2920 return TRAVERSE_CONTINUE;
2921 default:
2922 return TRAVERSE_CONTINUE;
2923 }
2924 this->found_ = true;
2925 return TRAVERSE_EXIT;
2926}
2927
7a938933
ILT
2928// The nil value.
2929
2930class Nil_expression : public Expression
2931{
2932 public:
8afa2bfb 2933 Nil_expression(Location location)
7a938933
ILT
2934 : Expression(EXPRESSION_NIL, location)
2935 { }
2936
2937 static Expression*
2938 do_import(Import*);
2939
2940 protected:
2941 bool
2942 do_is_constant() const
2943 { return true; }
2944
2945 Type*
2946 do_type()
2947 { return Type::make_nil_type(); }
2948
2949 void
2950 do_determine_type(const Type_context*)
2951 { }
2952
2953 Expression*
2954 do_copy()
2955 { return this; }
2956
2957 tree
2958 do_get_tree(Translate_context*)
2959 { return null_pointer_node; }
2960
2961 void
2962 do_export(Export* exp) const
2963 { exp->write_c_string("nil"); }
16c57fe2
RL
2964
2965 void
2966 do_dump_expression(Ast_dump_context* ast_dump_context) const
2967 { ast_dump_context->ostream() << "nil"; }
7a938933
ILT
2968};
2969
2970// Import a nil expression.
2971
2972Expression*
2973Nil_expression::do_import(Import* imp)
2974{
2975 imp->require_c_string("nil");
2976 return Expression::make_nil(imp->location());
2977}
2978
2979// Make a nil expression.
2980
2981Expression*
8afa2bfb 2982Expression::make_nil(Location location)
7a938933
ILT
2983{
2984 return new Nil_expression(location);
2985}
2986
2987// The value of the predeclared constant iota. This is little more
2988// than a marker. This will be lowered to an integer in
2989// Const_expression::do_lower, which is where we know the value that
2990// it should have.
2991
2992class Iota_expression : public Parser_expression
2993{
2994 public:
8afa2bfb 2995 Iota_expression(Location location)
7a938933
ILT
2996 : Parser_expression(EXPRESSION_IOTA, location)
2997 { }
2998
2999 protected:
3000 Expression*
8586635c 3001 do_lower(Gogo*, Named_object*, Statement_inserter*, int)
8c0d1865 3002 { go_unreachable(); }
7a938933
ILT
3003
3004 // There should only ever be one of these.
3005 Expression*
3006 do_copy()
8c0d1865 3007 { go_unreachable(); }
16c57fe2
RL
3008
3009 void
3010 do_dump_expression(Ast_dump_context* ast_dump_context) const
3011 { ast_dump_context->ostream() << "iota"; }
7a938933
ILT
3012};
3013
3014// Make an iota expression. This is only called for one case: the
3015// value of the predeclared constant iota.
3016
3017Expression*
3018Expression::make_iota()
3019{
8afa2bfb 3020 static Iota_expression iota_expression(Linemap::unknown_location());
7a938933
ILT
3021 return &iota_expression;
3022}
3023
3024// A type conversion expression.
3025
3026class Type_conversion_expression : public Expression
3027{
3028 public:
3029 Type_conversion_expression(Type* type, Expression* expr,
8afa2bfb 3030 Location location)
7a938933
ILT
3031 : Expression(EXPRESSION_CONVERSION, location),
3032 type_(type), expr_(expr), may_convert_function_types_(false)
3033 { }
3034
3035 // Return the type to which we are converting.
3036 Type*
3037 type() const
3038 { return this->type_; }
3039
3040 // Return the expression which we are converting.
3041 Expression*
3042 expr() const
3043 { return this->expr_; }
3044
3045 // Permit converting from one function type to another. This is
3046 // used internally for method expressions.
3047 void
3048 set_may_convert_function_types()
3049 {
3050 this->may_convert_function_types_ = true;
3051 }
3052
3053 // Import a type conversion expression.
3054 static Expression*
3055 do_import(Import*);
3056
3057 protected:
3058 int
3059 do_traverse(Traverse* traverse);
3060
3061 Expression*
8586635c 3062 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
7a938933
ILT
3063
3064 bool
e7d9342c 3065 do_is_constant() const;
7a938933
ILT
3066
3067 bool
5caf63ca 3068 do_numeric_constant_value(Numeric_constant*) const;
7a938933
ILT
3069
3070 bool
3071 do_string_constant_value(std::string*) const;
3072
3073 Type*
3074 do_type()
3075 { return this->type_; }
3076
3077 void
3078 do_determine_type(const Type_context*)
3079 {
3080 Type_context subcontext(this->type_, false);
3081 this->expr_->determine_type(&subcontext);
3082 }
3083
3084 void
3085 do_check_types(Gogo*);
3086
3087 Expression*
3088 do_copy()
3089 {
3090 return new Type_conversion_expression(this->type_, this->expr_->copy(),
3091 this->location());
3092 }
3093
3094 tree
3095 do_get_tree(Translate_context* context);
3096
3097 void
3098 do_export(Export*) const;
3099
16c57fe2
RL
3100 void
3101 do_dump_expression(Ast_dump_context*) const;
3102
7a938933
ILT
3103 private:
3104 // The type to convert to.
3105 Type* type_;
3106 // The expression to convert.
3107 Expression* expr_;
3108 // True if this is permitted to convert function types. This is
3109 // used internally for method expressions.
3110 bool may_convert_function_types_;
3111};
3112
3113// Traversal.
3114
3115int
3116Type_conversion_expression::do_traverse(Traverse* traverse)
3117{
3118 if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT
3119 || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
3120 return TRAVERSE_EXIT;
3121 return TRAVERSE_CONTINUE;
3122}
3123
3124// Convert to a constant at lowering time.
3125
3126Expression*
8586635c
ILT
3127Type_conversion_expression::do_lower(Gogo*, Named_object*,
3128 Statement_inserter*, int)
7a938933
ILT
3129{
3130 Type* type = this->type_;
3131 Expression* val = this->expr_;
8afa2bfb 3132 Location location = this->location();
7a938933 3133
5caf63ca 3134 if (type->is_numeric_type())
7a938933 3135 {
5caf63ca
ILT
3136 Numeric_constant nc;
3137 if (val->numeric_constant_value(&nc))
7a938933 3138 {
5caf63ca
ILT
3139 if (!nc.set_type(type, true, location))
3140 return Expression::make_error(location);
3141 return nc.expression(location);
7a938933 3142 }
7a938933
ILT
3143 }
3144
863ea6cf 3145 if (type->is_slice_type())
7a938933
ILT
3146 {
3147 Type* element_type = type->array_type()->element_type()->forwarded();
7b31a84d
ILT
3148 bool is_byte = (element_type->integer_type() != NULL
3149 && element_type->integer_type()->is_byte());
3150 bool is_rune = (element_type->integer_type() != NULL
3151 && element_type->integer_type()->is_rune());
3152 if (is_byte || is_rune)
7a938933
ILT
3153 {
3154 std::string s;
3155 if (val->string_constant_value(&s))
3156 {
3157 Expression_list* vals = new Expression_list();
3158 if (is_byte)
3159 {
3160 for (std::string::const_iterator p = s.begin();
3161 p != s.end();
3162 p++)
3163 {
3164 mpz_t val;
3165 mpz_init_set_ui(val, static_cast<unsigned char>(*p));
3166 Expression* v = Expression::make_integer(&val,
3167 element_type,
3168 location);
3169 vals->push_back(v);
3170 mpz_clear(val);
3171 }
3172 }
3173 else
3174 {
3175 const char *p = s.data();
3176 const char *pend = s.data() + s.length();
3177 while (p < pend)
3178 {
3179 unsigned int c;
3180 int adv = Lex::fetch_char(p, &c);
3181 if (adv == 0)
3182 {
3183 warning_at(this->location(), 0,
3184 "invalid UTF-8 encoding");
3185 adv = 1;
3186 }
3187 p += adv;
3188 mpz_t val;
3189 mpz_init_set_ui(val, c);
3190 Expression* v = Expression::make_integer(&val,
3191 element_type,
3192 location);
3193 vals->push_back(v);
3194 mpz_clear(val);
3195 }
3196 }
3197
3198 return Expression::make_slice_composite_literal(type, vals,
3199 location);
3200 }
3201 }
3202 }
3203
3204 return this;
3205}
3206
e7d9342c
ILT
3207// Return whether a type conversion is a constant.
3208
3209bool
3210Type_conversion_expression::do_is_constant() const
3211{
3212 if (!this->expr_->is_constant())
3213 return false;
3214
3215 // A conversion to a type that may not be used as a constant is not
3216 // a constant. For example, []byte(nil).
3217 Type* type = this->type_;
3218 if (type->integer_type() == NULL
3219 && type->float_type() == NULL
3220 && type->complex_type() == NULL
3221 && !type->is_boolean_type()
3222 && !type->is_string_type())
3223 return false;
3224
3225 return true;
3226}
3227
5caf63ca 3228// Return the constant numeric value if there is one.
7a938933
ILT
3229
3230bool
5caf63ca
ILT
3231Type_conversion_expression::do_numeric_constant_value(
3232 Numeric_constant* nc) const
7a938933 3233{
5caf63ca 3234 if (!this->type_->is_numeric_type())
7a938933 3235 return false;
5caf63ca 3236 if (!this->expr_->numeric_constant_value(nc))
7a938933 3237 return false;
5caf63ca 3238 return nc->set_type(this->type_, false, this->location());
7a938933
ILT
3239}
3240
3241// Return the constant string value if there is one.
3242
3243bool
3244Type_conversion_expression::do_string_constant_value(std::string* val) const
3245{
3246 if (this->type_->is_string_type()
3247 && this->expr_->type()->integer_type() != NULL)
3248 {
5caf63ca
ILT
3249 Numeric_constant nc;
3250 if (this->expr_->numeric_constant_value(&nc))
7a938933 3251 {
5caf63ca
ILT
3252 unsigned long ival;
3253 if (nc.to_unsigned_long(&ival) == Numeric_constant::NC_UL_VALID)
7a938933 3254 {
5caf63ca
ILT
3255 val->clear();
3256 Lex::append_char(ival, true, val, this->location());
7a938933
ILT
3257 return true;
3258 }
3259 }
7a938933
ILT
3260 }
3261
3262 // FIXME: Could handle conversion from const []int here.
3263
3264 return false;
3265}
3266
3267// Check that types are convertible.
3268
3269void
3270Type_conversion_expression::do_check_types(Gogo*)
3271{
3272 Type* type = this->type_;
3273 Type* expr_type = this->expr_->type();
3274 std::string reason;
3275
02ed921a 3276 if (type->is_error() || expr_type->is_error())
ecdacbb3 3277 {
ecdacbb3
ILT
3278 this->set_is_error();
3279 return;
3280 }
3281
7a938933
ILT
3282 if (this->may_convert_function_types_
3283 && type->function_type() != NULL
3284 && expr_type->function_type() != NULL)
3285 return;
3286
3287 if (Type::are_convertible(type, expr_type, &reason))
3288 return;
3289
3290 error_at(this->location(), "%s", reason.c_str());
3291 this->set_is_error();
3292}
3293
3294// Get a tree for a type conversion.
3295
3296tree
3297Type_conversion_expression::do_get_tree(Translate_context* context)
3298{
3299 Gogo* gogo = context->gogo();
5b735706 3300 tree type_tree = type_to_tree(this->type_->get_backend(gogo));
7a938933
ILT
3301 tree expr_tree = this->expr_->get_tree(context);
3302
3303 if (type_tree == error_mark_node
3304 || expr_tree == error_mark_node
3305 || TREE_TYPE(expr_tree) == error_mark_node)
3306 return error_mark_node;
3307
3308 if (TYPE_MAIN_VARIANT(type_tree) == TYPE_MAIN_VARIANT(TREE_TYPE(expr_tree)))
3309 return fold_convert(type_tree, expr_tree);
3310
3311 Type* type = this->type_;
3312 Type* expr_type = this->expr_->type();
3313 tree ret;
3314 if (type->interface_type() != NULL || expr_type->interface_type() != NULL)
3315 ret = Expression::convert_for_assignment(context, type, expr_type,
3316 expr_tree, this->location());
3317 else if (type->integer_type() != NULL)
3318 {
3319 if (expr_type->integer_type() != NULL
3320 || expr_type->float_type() != NULL
3321 || expr_type->is_unsafe_pointer_type())
3322 ret = fold(convert_to_integer(type_tree, expr_tree));
3323 else
8c0d1865 3324 go_unreachable();
7a938933
ILT
3325 }
3326 else if (type->float_type() != NULL)
3327 {
3328 if (expr_type->integer_type() != NULL
3329 || expr_type->float_type() != NULL)
3330 ret = fold(convert_to_real(type_tree, expr_tree));
3331 else
8c0d1865 3332 go_unreachable();
7a938933
ILT
3333 }
3334 else if (type->complex_type() != NULL)
3335 {
3336 if (expr_type->complex_type() != NULL)
3337 ret = fold(convert_to_complex(type_tree, expr_tree));
3338 else
8c0d1865 3339 go_unreachable();
7a938933
ILT
3340 }
3341 else if (type->is_string_type()
3342 && expr_type->integer_type() != NULL)
3343 {
776f27a6
ILT
3344 Type* int_type = Type::lookup_integer_type("int");
3345 tree int_type_tree = type_to_tree(int_type->get_backend(gogo));
3346
3347 expr_tree = fold_convert(int_type_tree, expr_tree);
9541ffee 3348 if (tree_fits_shwi_p (expr_tree))
7a938933 3349 {
9439e9a1 3350 HOST_WIDE_INT intval = tree_to_shwi (expr_tree);
7a938933
ILT
3351 std::string s;
3352 Lex::append_char(intval, true, &s, this->location());
3353 Expression* se = Expression::make_string(s, this->location());
3354 return se->get_tree(context);
3355 }
3356
7d6c5039 3357 Expression* i2s_expr =
a81eaeed
ILT
3358 Runtime::make_call(Runtime::INT_TO_STRING, this->location(), 1,
3359 this->expr_);
7d6c5039 3360 i2s_expr = Expression::make_cast(type, i2s_expr, this->location());
a81eaeed 3361 ret = i2s_expr->get_tree(context);
7a938933 3362 }
863ea6cf 3363 else if (type->is_string_type() && expr_type->is_slice_type())
7a938933 3364 {
7a938933
ILT
3365 if (!DECL_P(expr_tree))
3366 expr_tree = save_expr(expr_tree);
776f27a6
ILT
3367
3368 Type* int_type = Type::lookup_integer_type("int");
3369 tree int_type_tree = type_to_tree(int_type->get_backend(gogo));
3370
863ea6cf 3371 Array_type* a = expr_type->array_type();
7a938933 3372 Type* e = a->element_type()->forwarded();
26409c52 3373 go_assert(e->integer_type() != NULL);
7a938933
ILT
3374 tree valptr = fold_convert(const_ptr_type_node,
3375 a->value_pointer_tree(gogo, expr_tree));
3376 tree len = a->length_tree(gogo, expr_tree);
776f27a6 3377 len = fold_convert_loc(this->location().gcc_location(), int_type_tree,
8afa2bfb 3378 len);
7b31a84d 3379 if (e->integer_type()->is_byte())
7a938933
ILT
3380 {
3381 static tree byte_array_to_string_fndecl;
3382 ret = Gogo::call_builtin(&byte_array_to_string_fndecl,
3383 this->location(),
3384 "__go_byte_array_to_string",
3385 2,
3386 type_tree,
3387 const_ptr_type_node,
3388 valptr,
776f27a6 3389 int_type_tree,
7a938933
ILT
3390 len);
3391 }
3392 else
3393 {
7b31a84d 3394 go_assert(e->integer_type()->is_rune());
7a938933
ILT
3395 static tree int_array_to_string_fndecl;
3396 ret = Gogo::call_builtin(&int_array_to_string_fndecl,
3397 this->location(),
3398 "__go_int_array_to_string",
3399 2,
3400 type_tree,
3401 const_ptr_type_node,
3402 valptr,
776f27a6 3403 int_type_tree,
7a938933
ILT
3404 len);
3405 }
3406 }
b7190f2f 3407 else if (type->is_slice_type() && expr_type->is_string_type())
7a938933
ILT
3408 {
3409 Type* e = type->array_type()->element_type()->forwarded();
26409c52 3410 go_assert(e->integer_type() != NULL);
a81eaeed 3411
7d6c5039 3412 Expression* s2a_expr;
7b31a84d 3413 if (e->integer_type()->is_byte())
a81eaeed
ILT
3414 s2a_expr = Runtime::make_call(Runtime::STRING_TO_BYTE_ARRAY,
3415 this->location(), 1, this->expr_);
7a938933
ILT
3416 else
3417 {
7b31a84d 3418 go_assert(e->integer_type()->is_rune());
a81eaeed
ILT
3419 s2a_expr = Runtime::make_call(Runtime::STRING_TO_INT_ARRAY,
3420 this->location(), 1, this->expr_);
7a938933 3421 }
7d6c5039
ILT
3422 s2a_expr = Expression::make_unsafe_cast(type, s2a_expr,
3423 this->location());
a81eaeed 3424 ret = s2a_expr->get_tree(context);
7a938933
ILT
3425 }
3426 else if ((type->is_unsafe_pointer_type()
3427 && expr_type->points_to() != NULL)
3428 || (expr_type->is_unsafe_pointer_type()
3429 && type->points_to() != NULL))
3430 ret = fold_convert(type_tree, expr_tree);
3431 else if (type->is_unsafe_pointer_type()
3432 && expr_type->integer_type() != NULL)
3433 ret = convert_to_pointer(type_tree, expr_tree);
3434 else if (this->may_convert_function_types_
3435 && type->function_type() != NULL
3436 && expr_type->function_type() != NULL)
8afa2bfb
SD
3437 ret = fold_convert_loc(this->location().gcc_location(), type_tree,
3438 expr_tree);
7a938933
ILT
3439 else
3440 ret = Expression::convert_for_assignment(context, type, expr_type,
3441 expr_tree, this->location());
3442
3443 return ret;
3444}
3445
3446// Output a type conversion in a constant expression.
3447
3448void
3449Type_conversion_expression::do_export(Export* exp) const
3450{
3451 exp->write_c_string("convert(");
3452 exp->write_type(this->type_);
3453 exp->write_c_string(", ");
3454 this->expr_->export_expression(exp);
3455 exp->write_c_string(")");
3456}
3457
3458// Import a type conversion or a struct construction.
3459
3460Expression*
3461Type_conversion_expression::do_import(Import* imp)
3462{
3463 imp->require_c_string("convert(");
3464 Type* type = imp->read_type();
3465 imp->require_c_string(", ");
3466 Expression* val = Expression::import_expression(imp);
3467 imp->require_c_string(")");
3468 return Expression::make_cast(type, val, imp->location());
3469}
3470
16c57fe2
RL
3471// Dump ast representation for a type conversion expression.
3472
3473void
3474Type_conversion_expression::do_dump_expression(
3475 Ast_dump_context* ast_dump_context) const
3476{
3477 ast_dump_context->dump_type(this->type_);
3478 ast_dump_context->ostream() << "(";
3479 ast_dump_context->dump_expression(this->expr_);
3480 ast_dump_context->ostream() << ") ";
3481}
3482
7a938933
ILT
3483// Make a type cast expression.
3484
3485Expression*
8afa2bfb 3486Expression::make_cast(Type* type, Expression* val, Location location)
7a938933
ILT
3487{
3488 if (type->is_error_type() || val->is_error_expression())
3489 return Expression::make_error(location);
3490 return new Type_conversion_expression(type, val, location);
3491}
3492
b39c10b8
ILT
3493// An unsafe type conversion, used to pass values to builtin functions.
3494
3495class Unsafe_type_conversion_expression : public Expression
3496{
3497 public:
3498 Unsafe_type_conversion_expression(Type* type, Expression* expr,
8afa2bfb 3499 Location location)
b39c10b8
ILT
3500 : Expression(EXPRESSION_UNSAFE_CONVERSION, location),
3501 type_(type), expr_(expr)
3502 { }
3503
3504 protected:
3505 int
3506 do_traverse(Traverse* traverse);
3507
3508 Type*
3509 do_type()
3510 { return this->type_; }
3511
3512 void
3513 do_determine_type(const Type_context*)
3b8dffe7 3514 { this->expr_->determine_type_no_context(); }
b39c10b8
ILT
3515
3516 Expression*
3517 do_copy()
3518 {
3519 return new Unsafe_type_conversion_expression(this->type_,
3520 this->expr_->copy(),
3521 this->location());
3522 }
3523
3524 tree
3525 do_get_tree(Translate_context*);
3526
16c57fe2
RL
3527 void
3528 do_dump_expression(Ast_dump_context*) const;
3529
b39c10b8
ILT
3530 private:
3531 // The type to convert to.
3532 Type* type_;
3533 // The expression to convert.
3534 Expression* expr_;
3535};
3536
3537// Traversal.
3538
3539int
3540Unsafe_type_conversion_expression::do_traverse(Traverse* traverse)
3541{
3542 if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT
3543 || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
3544 return TRAVERSE_EXIT;
3545 return TRAVERSE_CONTINUE;
3546}
3547
3548// Convert to backend representation.
3549
3550tree
3551Unsafe_type_conversion_expression::do_get_tree(Translate_context* context)
3552{
3553 // We are only called for a limited number of cases.
3554
3555 Type* t = this->type_;
3556 Type* et = this->expr_->type();
3557
5b735706 3558 tree type_tree = type_to_tree(this->type_->get_backend(context->gogo()));
b39c10b8
ILT
3559 tree expr_tree = this->expr_->get_tree(context);
3560 if (type_tree == error_mark_node || expr_tree == error_mark_node)
3561 return error_mark_node;
3562
8afa2bfb 3563 Location loc = this->location();
b39c10b8
ILT
3564
3565 bool use_view_convert = false;
b7190f2f 3566 if (t->is_slice_type())
b39c10b8 3567 {
b7190f2f 3568 go_assert(et->is_slice_type());
b39c10b8
ILT
3569 use_view_convert = true;
3570 }
3571 else if (t->map_type() != NULL)
26409c52 3572 go_assert(et->map_type() != NULL);
b39c10b8 3573 else if (t->channel_type() != NULL)
26409c52 3574 go_assert(et->channel_type() != NULL);
1bbf7edb 3575 else if (t->points_to() != NULL)
26409c52 3576 go_assert(et->points_to() != NULL || et->is_nil_type());
b39c10b8 3577 else if (et->is_unsafe_pointer_type())
26409c52 3578 go_assert(t->points_to() != NULL);
b39c10b8
ILT
3579 else if (t->interface_type() != NULL && !t->interface_type()->is_empty())
3580 {
26409c52 3581 go_assert(et->interface_type() != NULL
b39c10b8
ILT
3582 && !et->interface_type()->is_empty());
3583 use_view_convert = true;
3584 }
3585 else if (t->interface_type() != NULL && t->interface_type()->is_empty())
3586 {
26409c52 3587 go_assert(et->interface_type() != NULL
b39c10b8
ILT
3588 && et->interface_type()->is_empty());
3589 use_view_convert = true;
3590 }
a6de01a6
ILT
3591 else if (t->integer_type() != NULL)
3592 {
26409c52 3593 go_assert(et->is_boolean_type()
a6de01a6
ILT
3594 || et->integer_type() != NULL
3595 || et->function_type() != NULL
3596 || et->points_to() != NULL
3597 || et->map_type() != NULL
3598 || et->channel_type() != NULL);
3599 return convert_to_integer(type_tree, expr_tree);
3600 }
b39c10b8 3601 else
8c0d1865 3602 go_unreachable();
b39c10b8
ILT
3603
3604 if (use_view_convert)
8afa2bfb
SD
3605 return fold_build1_loc(loc.gcc_location(), VIEW_CONVERT_EXPR, type_tree,
3606 expr_tree);
b39c10b8 3607 else
8afa2bfb 3608 return fold_convert_loc(loc.gcc_location(), type_tree, expr_tree);
b39c10b8
ILT
3609}
3610
16c57fe2
RL
3611// Dump ast representation for an unsafe type conversion expression.
3612
3613void
3614Unsafe_type_conversion_expression::do_dump_expression(
3615 Ast_dump_context* ast_dump_context) const
3616{
3617 ast_dump_context->dump_type(this->type_);
3618 ast_dump_context->ostream() << "(";
3619 ast_dump_context->dump_expression(this->expr_);
3620 ast_dump_context->ostream() << ") ";
3621}
3622
b39c10b8
ILT
3623// Make an unsafe type conversion expression.
3624
3625Expression*
3626Expression::make_unsafe_cast(Type* type, Expression* expr,
8afa2bfb 3627 Location location)
b39c10b8
ILT
3628{
3629 return new Unsafe_type_conversion_expression(type, expr, location);
3630}
3631
7a938933
ILT
3632// Unary expressions.
3633
3634class Unary_expression : public Expression
3635{
3636 public:
8afa2bfb 3637 Unary_expression(Operator op, Expression* expr, Location location)
7a938933 3638 : Expression(EXPRESSION_UNARY, location),
64c7b4c0
ILT
3639 op_(op), escapes_(true), create_temp_(false), expr_(expr),
3640 issue_nil_check_(false)
7a938933
ILT
3641 { }
3642
3643 // Return the operator.
3644 Operator
3645 op() const
3646 { return this->op_; }
3647
3648 // Return the operand.
3649 Expression*
3650 operand() const
3651 { return this->expr_; }
3652
3653 // Record that an address expression does not escape.
3654 void
3655 set_does_not_escape()
3656 {
26409c52 3657 go_assert(this->op_ == OPERATOR_AND);
7a938933
ILT
3658 this->escapes_ = false;
3659 }
3660
1bbf7edb
ILT
3661 // Record that this is an address expression which should create a
3662 // temporary variable if necessary. This is used for method calls.
3663 void
3664 set_create_temp()
3665 {
3666 go_assert(this->op_ == OPERATOR_AND);
3667 this->create_temp_ = true;
3668 }
3669
5caf63ca
ILT
3670 // Apply unary opcode OP to UNC, setting NC. Return true if this
3671 // could be done, false if not. Issue errors for overflow.
7a938933 3672 static bool
5caf63ca
ILT
3673 eval_constant(Operator op, const Numeric_constant* unc,
3674 Location, Numeric_constant* nc);
7a938933
ILT
3675
3676 static Expression*
3677 do_import(Import*);
3678
3679 protected:
3680 int
3681 do_traverse(Traverse* traverse)
3682 { return Expression::traverse(&this->expr_, traverse); }
3683
3684 Expression*
8586635c 3685 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
7a938933
ILT
3686
3687 bool
3688 do_is_constant() const;
3689
3690 bool
5caf63ca 3691 do_numeric_constant_value(Numeric_constant*) const;
7a938933
ILT
3692
3693 Type*
3694 do_type();
3695
3696 void
3697 do_determine_type(const Type_context*);
3698
3699 void
3700 do_check_types(Gogo*);
3701
3702 Expression*
3703 do_copy()
3704 {
3705 return Expression::make_unary(this->op_, this->expr_->copy(),
3706 this->location());
3707 }
3708
cbe98a41
ILT
3709 bool
3710 do_must_eval_subexpressions_in_order(int*) const
3711 { return this->op_ == OPERATOR_MULT; }
3712
7a938933
ILT
3713 bool
3714 do_is_addressable() const
3715 { return this->op_ == OPERATOR_MULT; }
3716
3717 tree
3718 do_get_tree(Translate_context*);
3719
3720 void
3721 do_export(Export*) const;
3722
16c57fe2
RL
3723 void
3724 do_dump_expression(Ast_dump_context*) const;
3725
64c7b4c0
ILT
3726 void
3727 do_issue_nil_check()
3728 { this->issue_nil_check_ = (this->op_ == OPERATOR_MULT); }
3729
7a938933
ILT
3730 private:
3731 // The unary operator to apply.
3732 Operator op_;
3733 // Normally true. False if this is an address expression which does
3734 // not escape the current function.
3735 bool escapes_;
1bbf7edb
ILT
3736 // True if this is an address expression which should create a
3737 // temporary variable if necessary.
3738 bool create_temp_;
7a938933
ILT
3739 // The operand.
3740 Expression* expr_;
64c7b4c0
ILT
3741 // Whether or not to issue a nil check for this expression if its address
3742 // is being taken.
3743 bool issue_nil_check_;
7a938933
ILT
3744};
3745
3746// If we are taking the address of a composite literal, and the
3747// contents are not constant, then we want to make a heap composite
3748// instead.
3749
3750Expression*
8586635c 3751Unary_expression::do_lower(Gogo*, Named_object*, Statement_inserter*, int)
7a938933 3752{
8afa2bfb 3753 Location loc = this->location();
7a938933
ILT
3754 Operator op = this->op_;
3755 Expression* expr = this->expr_;
3756
3757 if (op == OPERATOR_MULT && expr->is_type_expression())
3758 return Expression::make_type(Type::make_pointer_type(expr->type()), loc);
3759
3760 // *&x simplifies to x. *(*T)(unsafe.Pointer)(&x) does not require
3761 // moving x to the heap. FIXME: Is it worth doing a real escape
3762 // analysis here? This case is found in math/unsafe.go and is
3763 // therefore worth special casing.
3764 if (op == OPERATOR_MULT)
3765 {
3766 Expression* e = expr;
3767 while (e->classification() == EXPRESSION_CONVERSION)
3768 {
3769 Type_conversion_expression* te
3770 = static_cast<Type_conversion_expression*>(e);
3771 e = te->expr();
3772 }
3773
3774 if (e->classification() == EXPRESSION_UNARY)
3775 {
3776 Unary_expression* ue = static_cast<Unary_expression*>(e);
3777 if (ue->op_ == OPERATOR_AND)
3778 {
3779 if (e == expr)
3780 {
3781 // *&x == x.
3782 return ue->expr_;
3783 }
3784 ue->set_does_not_escape();
3785 }
3786 }
3787 }
3788
15ea09a0
ILT
3789 // Catching an invalid indirection of unsafe.Pointer here avoid
3790 // having to deal with TYPE_VOID in other places.
3791 if (op == OPERATOR_MULT && expr->type()->is_unsafe_pointer_type())
3792 {
3793 error_at(this->location(), "invalid indirect of %<unsafe.Pointer%>");
3794 return Expression::make_error(this->location());
3795 }
3796
c414667b 3797 if (op == OPERATOR_PLUS || op == OPERATOR_MINUS || op == OPERATOR_XOR)
7a938933 3798 {
5caf63ca
ILT
3799 Numeric_constant nc;
3800 if (expr->numeric_constant_value(&nc))
7a938933 3801 {
5caf63ca
ILT
3802 Numeric_constant result;
3803 if (Unary_expression::eval_constant(op, &nc, loc, &result))
3804 return result.expression(loc);
7a938933
ILT
3805 }
3806 }
3807
3808 return this;
3809}
3810
3811// Return whether a unary expression is a constant.
3812
3813bool
3814Unary_expression::do_is_constant() const
3815{
3816 if (this->op_ == OPERATOR_MULT)
3817 {
3818 // Indirecting through a pointer is only constant if the object
3819 // to which the expression points is constant, but we currently
3820 // have no way to determine that.
3821 return false;
3822 }
3823 else if (this->op_ == OPERATOR_AND)
3824 {
3825 // Taking the address of a variable is constant if it is a
3826 // global variable, not constant otherwise. In other cases
3827 // taking the address is probably not a constant.
3828 Var_expression* ve = this->expr_->var_expression();
3829 if (ve != NULL)
3830 {
3831 Named_object* no = ve->named_object();
3832 return no->is_variable() && no->var_value()->is_global();
3833 }
3834 return false;
3835 }
3836 else
3837 return this->expr_->is_constant();
3838}
3839
5caf63ca
ILT
3840// Apply unary opcode OP to UNC, setting NC. Return true if this
3841// could be done, false if not. Issue errors for overflow.
7a938933
ILT
3842
3843bool
5caf63ca
ILT
3844Unary_expression::eval_constant(Operator op, const Numeric_constant* unc,
3845 Location location, Numeric_constant* nc)
7a938933
ILT
3846{
3847 switch (op)
3848 {
3849 case OPERATOR_PLUS:
5caf63ca 3850 *nc = *unc;
7a938933 3851 return true;
5caf63ca 3852
7a938933 3853 case OPERATOR_MINUS:
5caf63ca
ILT
3854 if (unc->is_int() || unc->is_rune())
3855 break;
3856 else if (unc->is_float())
3857 {
3858 mpfr_t uval;
3859 unc->get_float(&uval);
3860 mpfr_t val;
3861 mpfr_init(val);
3862 mpfr_neg(val, uval, GMP_RNDN);
3863 nc->set_float(unc->type(), val);
3864 mpfr_clear(uval);
3865 mpfr_clear(val);
3866 return true;
3867 }
3868 else if (unc->is_complex())
3869 {
3870 mpfr_t ureal, uimag;
3871 unc->get_complex(&ureal, &uimag);
3872 mpfr_t real, imag;
3873 mpfr_init(real);
3874 mpfr_init(imag);
3875 mpfr_neg(real, ureal, GMP_RNDN);
3876 mpfr_neg(imag, uimag, GMP_RNDN);
3877 nc->set_complex(unc->type(), real, imag);
3878 mpfr_clear(ureal);
3879 mpfr_clear(uimag);
3880 mpfr_clear(real);
3881 mpfr_clear(imag);
3882 return true;
3883 }
7a938933 3884 else
5caf63ca 3885 go_unreachable();
7a938933 3886
5caf63ca
ILT
3887 case OPERATOR_XOR:
3888 break;
fc24bee9 3889
c414667b 3890 case OPERATOR_NOT:
7a938933
ILT
3891 case OPERATOR_AND:
3892 case OPERATOR_MULT:
3893 return false;
5caf63ca 3894
7a938933 3895 default:
8c0d1865 3896 go_unreachable();
7a938933 3897 }
7a938933 3898
5caf63ca
ILT
3899 if (!unc->is_int() && !unc->is_rune())
3900 return false;
3901
3902 mpz_t uval;
ca30ba74
ILT
3903 if (unc->is_rune())
3904 unc->get_rune(&uval);
3905 else
3906 unc->get_int(&uval);
5caf63ca
ILT
3907 mpz_t val;
3908 mpz_init(val);
7a938933 3909
7a938933
ILT
3910 switch (op)
3911 {
7a938933 3912 case OPERATOR_MINUS:
5caf63ca
ILT
3913 mpz_neg(val, uval);
3914 break;
3915
7a938933 3916 case OPERATOR_NOT:
5caf63ca
ILT
3917 mpz_set_ui(val, mpz_cmp_si(uval, 0) == 0 ? 1 : 0);
3918 break;
3919
7a938933 3920 case OPERATOR_XOR:
5caf63ca
ILT
3921 {
3922 Type* utype = unc->type();
3923 if (utype->integer_type() == NULL
3924 || utype->integer_type()->is_abstract())
3925 mpz_com(val, uval);
3926 else
3927 {
3928 // The number of HOST_WIDE_INTs that it takes to represent
3929 // UVAL.
3930 size_t count = ((mpz_sizeinbase(uval, 2)
3931 + HOST_BITS_PER_WIDE_INT
3932 - 1)
3933 / HOST_BITS_PER_WIDE_INT);
7a938933 3934
5caf63ca
ILT
3935 unsigned HOST_WIDE_INT* phwi = new unsigned HOST_WIDE_INT[count];
3936 memset(phwi, 0, count * sizeof(HOST_WIDE_INT));
3937
3938 size_t obits = utype->integer_type()->bits();
3939
3940 if (!utype->integer_type()->is_unsigned() && mpz_sgn(uval) < 0)
3941 {
3942 mpz_t adj;
3943 mpz_init_set_ui(adj, 1);
3944 mpz_mul_2exp(adj, adj, obits);
3945 mpz_add(uval, uval, adj);
3946 mpz_clear(adj);
3947 }
3948
3949 size_t ecount;
3950 mpz_export(phwi, &ecount, -1, sizeof(HOST_WIDE_INT), 0, 0, uval);
3951 go_assert(ecount <= count);
3952
3953 // Trim down to the number of words required by the type.
3954 size_t ocount = ((obits + HOST_BITS_PER_WIDE_INT - 1)
3955 / HOST_BITS_PER_WIDE_INT);
3956 go_assert(ocount <= count);
3957
3958 for (size_t i = 0; i < ocount; ++i)
3959 phwi[i] = ~phwi[i];
3960
3961 size_t clearbits = ocount * HOST_BITS_PER_WIDE_INT - obits;
3962 if (clearbits != 0)
3963 phwi[ocount - 1] &= (((unsigned HOST_WIDE_INT) (HOST_WIDE_INT) -1)
3964 >> clearbits);
3965
3966 mpz_import(val, ocount, -1, sizeof(HOST_WIDE_INT), 0, 0, phwi);
3967
3968 if (!utype->integer_type()->is_unsigned()
3969 && mpz_tstbit(val, obits - 1))
3970 {
3971 mpz_t adj;
3972 mpz_init_set_ui(adj, 1);
3973 mpz_mul_2exp(adj, adj, obits);
3974 mpz_sub(val, val, adj);
3975 mpz_clear(adj);
3976 }
3977
3978 delete[] phwi;
3979 }
3980 }
3981 break;
7a938933 3982
7a938933 3983 default:
8c0d1865 3984 go_unreachable();
7a938933 3985 }
7a938933 3986
5caf63ca
ILT
3987 if (unc->is_rune())
3988 nc->set_rune(NULL, val);
7a938933 3989 else
5caf63ca 3990 nc->set_int(NULL, val);
7a938933 3991
5caf63ca
ILT
3992 mpz_clear(uval);
3993 mpz_clear(val);
7a938933 3994
5caf63ca 3995 return nc->set_type(unc->type(), true, location);
7a938933
ILT
3996}
3997
5caf63ca 3998// Return the integral constant value of a unary expression, if it has one.
7a938933
ILT
3999
4000bool
5caf63ca 4001Unary_expression::do_numeric_constant_value(Numeric_constant* nc) const
7a938933 4002{
5caf63ca
ILT
4003 Numeric_constant unc;
4004 if (!this->expr_->numeric_constant_value(&unc))
4005 return false;
4006 return Unary_expression::eval_constant(this->op_, &unc, this->location(),
4007 nc);
7a938933
ILT
4008}
4009
4010// Return the type of a unary expression.
4011
4012Type*
4013Unary_expression::do_type()
4014{
4015 switch (this->op_)
4016 {
4017 case OPERATOR_PLUS:
4018 case OPERATOR_MINUS:
4019 case OPERATOR_NOT:
4020 case OPERATOR_XOR:
4021 return this->expr_->type();
4022
4023 case OPERATOR_AND:
4024 return Type::make_pointer_type(this->expr_->type());
4025
4026 case OPERATOR_MULT:
4027 {
4028 Type* subtype = this->expr_->type();
4029 Type* points_to = subtype->points_to();
4030 if (points_to == NULL)
4031 return Type::make_error_type();
4032 return points_to;
4033 }
4034
4035 default:
8c0d1865 4036 go_unreachable();
7a938933
ILT
4037 }
4038}
4039
4040// Determine abstract types for a unary expression.
4041
4042void
4043Unary_expression::do_determine_type(const Type_context* context)
4044{
4045 switch (this->op_)
4046 {
4047 case OPERATOR_PLUS:
4048 case OPERATOR_MINUS:
4049 case OPERATOR_NOT:
4050 case OPERATOR_XOR:
4051 this->expr_->determine_type(context);
4052 break;
4053
4054 case OPERATOR_AND:
4055 // Taking the address of something.
4056 {
4057 Type* subtype = (context->type == NULL
4058 ? NULL
4059 : context->type->points_to());
4060 Type_context subcontext(subtype, false);
4061 this->expr_->determine_type(&subcontext);
4062 }
4063 break;
4064
4065 case OPERATOR_MULT:
4066 // Indirecting through a pointer.
4067 {
4068 Type* subtype = (context->type == NULL
4069 ? NULL
4070 : Type::make_pointer_type(context->type));
4071 Type_context subcontext(subtype, false);
4072 this->expr_->determine_type(&subcontext);
4073 }
4074 break;
4075
4076 default:
8c0d1865 4077 go_unreachable();
7a938933
ILT
4078 }
4079}
4080
4081// Check types for a unary expression.
4082
4083void
4084Unary_expression::do_check_types(Gogo*)
4085{
173fb2ff 4086 Type* type = this->expr_->type();
02ed921a 4087 if (type->is_error())
173fb2ff
ILT
4088 {
4089 this->set_is_error();
4090 return;
4091 }
4092
7a938933
ILT
4093 switch (this->op_)
4094 {
4095 case OPERATOR_PLUS:
4096 case OPERATOR_MINUS:
173fb2ff
ILT
4097 if (type->integer_type() == NULL
4098 && type->float_type() == NULL
4099 && type->complex_type() == NULL)
4100 this->report_error(_("expected numeric type"));
7a938933
ILT
4101 break;
4102
4103 case OPERATOR_NOT:
c414667b
ILT
4104 if (!type->is_boolean_type())
4105 this->report_error(_("expected boolean type"));
4106 break;
4107
7a938933 4108 case OPERATOR_XOR:
173fb2ff
ILT
4109 if (type->integer_type() == NULL
4110 && !type->is_boolean_type())
4111 this->report_error(_("expected integer or boolean type"));
7a938933
ILT
4112 break;
4113
4114 case OPERATOR_AND:
4115 if (!this->expr_->is_addressable())
1bbf7edb
ILT
4116 {
4117 if (!this->create_temp_)
4118 this->report_error(_("invalid operand for unary %<&%>"));
4119 }
7a938933 4120 else
64c7b4c0
ILT
4121 {
4122 this->expr_->address_taken(this->escapes_);
4123 this->expr_->issue_nil_check();
4124 }
7a938933
ILT
4125 break;
4126
4127 case OPERATOR_MULT:
4128 // Indirecting through a pointer.
173fb2ff
ILT
4129 if (type->points_to() == NULL)
4130 this->report_error(_("expected pointer"));
7a938933
ILT
4131 break;
4132
4133 default:
8c0d1865 4134 go_unreachable();
7a938933
ILT
4135 }
4136}
4137
4138// Get a tree for a unary expression.
4139
4140tree
4141Unary_expression::do_get_tree(Translate_context* context)
4142{
776f27a6 4143 Gogo* gogo = context->gogo();
f9f96987
ILT
4144 Location loc = this->location();
4145
4146 // Taking the address of a set-and-use-temporary expression requires
4147 // setting the temporary and then taking the address.
4148 if (this->op_ == OPERATOR_AND)
4149 {
4150 Set_and_use_temporary_expression* sut =
4151 this->expr_->set_and_use_temporary_expression();
4152 if (sut != NULL)
4153 {
4154 Temporary_statement* temp = sut->temporary();
4155 Bvariable* bvar = temp->get_backend_variable(context);
4156 tree var_tree = var_to_tree(bvar);
4157 Expression* val = sut->expression();
4158 tree val_tree = val->get_tree(context);
4159 if (var_tree == error_mark_node || val_tree == error_mark_node)
4160 return error_mark_node;
4161 tree addr_tree = build_fold_addr_expr_loc(loc.gcc_location(),
4162 var_tree);
4163 return build2_loc(loc.gcc_location(), COMPOUND_EXPR,
4164 TREE_TYPE(addr_tree),
4165 build2_loc(sut->location().gcc_location(),
4166 MODIFY_EXPR, void_type_node,
4167 var_tree, val_tree),
4168 addr_tree);
4169 }
4170 }
4171
7a938933
ILT
4172 tree expr = this->expr_->get_tree(context);
4173 if (expr == error_mark_node)
4174 return error_mark_node;
4175
7a938933
ILT
4176 switch (this->op_)
4177 {
4178 case OPERATOR_PLUS:
4179 return expr;
4180
4181 case OPERATOR_MINUS:
4182 {
4183 tree type = TREE_TYPE(expr);
4184 tree compute_type = excess_precision_type(type);
4185 if (compute_type != NULL_TREE)
4186 expr = ::convert(compute_type, expr);
8afa2bfb 4187 tree ret = fold_build1_loc(loc.gcc_location(), NEGATE_EXPR,
7a938933
ILT
4188 (compute_type != NULL_TREE
4189 ? compute_type
4190 : type),
4191 expr);
4192 if (compute_type != NULL_TREE)
4193 ret = ::convert(type, ret);
4194 return ret;
4195 }
4196
4197 case OPERATOR_NOT:
4198 if (TREE_CODE(TREE_TYPE(expr)) == BOOLEAN_TYPE)
8afa2bfb
SD
4199 return fold_build1_loc(loc.gcc_location(), TRUTH_NOT_EXPR,
4200 TREE_TYPE(expr), expr);
7a938933 4201 else
8afa2bfb
SD
4202 return fold_build2_loc(loc.gcc_location(), NE_EXPR, boolean_type_node,
4203 expr, build_int_cst(TREE_TYPE(expr), 0));
7a938933
ILT
4204
4205 case OPERATOR_XOR:
8afa2bfb
SD
4206 return fold_build1_loc(loc.gcc_location(), BIT_NOT_EXPR, TREE_TYPE(expr),
4207 expr);
7a938933
ILT
4208
4209 case OPERATOR_AND:
1bbf7edb
ILT
4210 if (!this->create_temp_)
4211 {
4212 // We should not see a non-constant constructor here; cases
4213 // where we would see one should have been moved onto the
4214 // heap at parse time. Taking the address of a nonconstant
4215 // constructor will not do what the programmer expects.
4216 go_assert(TREE_CODE(expr) != CONSTRUCTOR || TREE_CONSTANT(expr));
4217 go_assert(TREE_CODE(expr) != ADDR_EXPR);
4218 }
7a938933
ILT
4219
4220 // Build a decl for a constant constructor.
4221 if (TREE_CODE(expr) == CONSTRUCTOR && TREE_CONSTANT(expr))
4222 {
8afa2bfb 4223 tree decl = build_decl(this->location().gcc_location(), VAR_DECL,
7a938933
ILT
4224 create_tmp_var_name("C"), TREE_TYPE(expr));
4225 DECL_EXTERNAL(decl) = 0;
4226 TREE_PUBLIC(decl) = 0;
4227 TREE_READONLY(decl) = 1;
4228 TREE_CONSTANT(decl) = 1;
4229 TREE_STATIC(decl) = 1;
4230 TREE_ADDRESSABLE(decl) = 1;
4231 DECL_ARTIFICIAL(decl) = 1;
4232 DECL_INITIAL(decl) = expr;
4233 rest_of_decl_compilation(decl, 1, 0);
4234 expr = decl;
4235 }
4236
1bbf7edb
ILT
4237 if (this->create_temp_
4238 && !TREE_ADDRESSABLE(TREE_TYPE(expr))
ae10b574 4239 && (TREE_CODE(expr) == CONST_DECL || !DECL_P(expr))
1bbf7edb
ILT
4240 && TREE_CODE(expr) != INDIRECT_REF
4241 && TREE_CODE(expr) != COMPONENT_REF)
4242 {
df4ddb11
ILT
4243 if (current_function_decl != NULL)
4244 {
4245 tree tmp = create_tmp_var(TREE_TYPE(expr), get_name(expr));
4246 DECL_IGNORED_P(tmp) = 1;
4247 DECL_INITIAL(tmp) = expr;
4248 TREE_ADDRESSABLE(tmp) = 1;
4249 return build2_loc(loc.gcc_location(), COMPOUND_EXPR,
4250 build_pointer_type(TREE_TYPE(expr)),
4251 build1_loc(loc.gcc_location(), DECL_EXPR,
4252 void_type_node, tmp),
4253 build_fold_addr_expr_loc(loc.gcc_location(),
4254 tmp));
4255 }
4256 else
4257 {
4258 tree tmp = build_decl(loc.gcc_location(), VAR_DECL,
4259 create_tmp_var_name("A"), TREE_TYPE(expr));
4260 DECL_EXTERNAL(tmp) = 0;
4261 TREE_PUBLIC(tmp) = 0;
4262 TREE_STATIC(tmp) = 1;
4263 DECL_ARTIFICIAL(tmp) = 1;
4264 TREE_ADDRESSABLE(tmp) = 1;
4265 tree make_tmp;
4266 if (!TREE_CONSTANT(expr))
4267 make_tmp = fold_build2_loc(loc.gcc_location(), INIT_EXPR,
4268 void_type_node, tmp, expr);
4269 else
4270 {
4271 TREE_READONLY(tmp) = 1;
4272 TREE_CONSTANT(tmp) = 1;
4273 DECL_INITIAL(tmp) = expr;
4274 make_tmp = NULL_TREE;
4275 }
4276 rest_of_decl_compilation(tmp, 1, 0);
4277 tree addr = build_fold_addr_expr_loc(loc.gcc_location(), tmp);
4278 if (make_tmp == NULL_TREE)
4279 return addr;
4280 return build2_loc(loc.gcc_location(), COMPOUND_EXPR,
4281 TREE_TYPE(addr), make_tmp, addr);
4282 }
1bbf7edb
ILT
4283 }
4284
8afa2bfb 4285 return build_fold_addr_expr_loc(loc.gcc_location(), expr);
7a938933
ILT
4286
4287 case OPERATOR_MULT:
4288 {
26409c52 4289 go_assert(POINTER_TYPE_P(TREE_TYPE(expr)));
7a938933
ILT
4290
4291 // If we are dereferencing the pointer to a large struct, we
4292 // need to check for nil. We don't bother to check for small
4293 // structs because we expect the system to crash on a nil
64c7b4c0
ILT
4294 // pointer dereference. However, if we know the address of this
4295 // expression is being taken, we must always check for nil.
bedcedc1
ILT
4296 tree target_type_tree = TREE_TYPE(TREE_TYPE(expr));
4297 if (!VOID_TYPE_P(target_type_tree))
7a938933 4298 {
bedcedc1 4299 HOST_WIDE_INT s = int_size_in_bytes(target_type_tree);
64c7b4c0 4300 if (s == -1 || s >= 4096 || this->issue_nil_check_)
bedcedc1
ILT
4301 {
4302 if (!DECL_P(expr))
4303 expr = save_expr(expr);
4304 tree compare = fold_build2_loc(loc.gcc_location(), EQ_EXPR,
4305 boolean_type_node,
4306 expr,
4307 fold_convert(TREE_TYPE(expr),
4308 null_pointer_node));
776f27a6 4309 tree crash = gogo->runtime_error(RUNTIME_ERROR_NIL_DEREFERENCE,
bedcedc1
ILT
4310 loc);
4311 expr = fold_build2_loc(loc.gcc_location(), COMPOUND_EXPR,
4312 TREE_TYPE(expr), build3(COND_EXPR,
4313 void_type_node,
4314 compare, crash,
4315 NULL_TREE),
4316 expr);
4317 }
7a938933
ILT
4318 }
4319
4320 // If the type of EXPR is a recursive pointer type, then we
4321 // need to insert a cast before indirecting.
bedcedc1 4322 if (VOID_TYPE_P(target_type_tree))
7a938933
ILT
4323 {
4324 Type* pt = this->expr_->type()->points_to();
776f27a6 4325 tree ind = type_to_tree(pt->get_backend(gogo));
8afa2bfb
SD
4326 expr = fold_convert_loc(loc.gcc_location(),
4327 build_pointer_type(ind), expr);
7a938933
ILT
4328 }
4329
8afa2bfb 4330 return build_fold_indirect_ref_loc(loc.gcc_location(), expr);
7a938933
ILT
4331 }
4332
4333 default:
8c0d1865 4334 go_unreachable();
7a938933
ILT
4335 }
4336}
4337
4338// Export a unary expression.
4339
4340void
4341Unary_expression::do_export(Export* exp) const
4342{
4343 switch (this->op_)
4344 {
4345 case OPERATOR_PLUS:
4346 exp->write_c_string("+ ");
4347 break;
4348 case OPERATOR_MINUS:
4349 exp->write_c_string("- ");
4350 break;
4351 case OPERATOR_NOT:
4352 exp->write_c_string("! ");
4353 break;
4354 case OPERATOR_XOR:
4355 exp->write_c_string("^ ");
4356 break;
4357 case OPERATOR_AND:
4358 case OPERATOR_MULT:
4359 default:
8c0d1865 4360 go_unreachable();
7a938933
ILT
4361 }
4362 this->expr_->export_expression(exp);
4363}
4364
4365// Import a unary expression.
4366
4367Expression*
4368Unary_expression::do_import(Import* imp)
4369{
4370 Operator op;
4371 switch (imp->get_char())
4372 {
4373 case '+':
4374 op = OPERATOR_PLUS;
4375 break;
4376 case '-':
4377 op = OPERATOR_MINUS;
4378 break;
4379 case '!':
4380 op = OPERATOR_NOT;
4381 break;
4382 case '^':
4383 op = OPERATOR_XOR;
4384 break;
4385 default:
8c0d1865 4386 go_unreachable();
7a938933
ILT
4387 }
4388 imp->require_c_string(" ");
4389 Expression* expr = Expression::import_expression(imp);
4390 return Expression::make_unary(op, expr, imp->location());
4391}
4392
16c57fe2
RL
4393// Dump ast representation of an unary expression.
4394
4395void
4396Unary_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
4397{
4398 ast_dump_context->dump_operator(this->op_);
4399 ast_dump_context->ostream() << "(";
4400 ast_dump_context->dump_expression(this->expr_);
4401 ast_dump_context->ostream() << ") ";
4402}
4403
7a938933
ILT
4404// Make a unary expression.
4405
4406Expression*
8afa2bfb 4407Expression::make_unary(Operator op, Expression* expr, Location location)
7a938933
ILT
4408{
4409 return new Unary_expression(op, expr, location);
4410}
4411
4412// If this is an indirection through a pointer, return the expression
4413// being pointed through. Otherwise return this.
4414
4415Expression*
4416Expression::deref()
4417{
4418 if (this->classification_ == EXPRESSION_UNARY)
4419 {
4420 Unary_expression* ue = static_cast<Unary_expression*>(this);
4421 if (ue->op() == OPERATOR_MULT)
4422 return ue->operand();
4423 }
4424 return this;
4425}
4426
4427// Class Binary_expression.
4428
4429// Traversal.
4430
4431int
4432Binary_expression::do_traverse(Traverse* traverse)
4433{
4434 int t = Expression::traverse(&this->left_, traverse);
4435 if (t == TRAVERSE_EXIT)
4436 return TRAVERSE_EXIT;
4437 return Expression::traverse(&this->right_, traverse);
4438}
4439
5caf63ca
ILT
4440// Return the type to use for a binary operation on operands of
4441// LEFT_TYPE and RIGHT_TYPE. These are the types of constants and as
4442// such may be NULL or abstract.
4443
4444bool
4445Binary_expression::operation_type(Operator op, Type* left_type,
4446 Type* right_type, Type** result_type)
4447{
4448 if (left_type != right_type
4449 && !left_type->is_abstract()
4450 && !right_type->is_abstract()
4451 && left_type->base() != right_type->base()
4452 && op != OPERATOR_LSHIFT
4453 && op != OPERATOR_RSHIFT)
4454 {
4455 // May be a type error--let it be diagnosed elsewhere.
4456 return false;
4457 }
4458
4459 if (op == OPERATOR_LSHIFT || op == OPERATOR_RSHIFT)
4460 {
4461 if (left_type->integer_type() != NULL)
4462 *result_type = left_type;
4463 else
4464 *result_type = Type::make_abstract_integer_type();
4465 }
4466 else if (!left_type->is_abstract() && left_type->named_type() != NULL)
4467 *result_type = left_type;
4468 else if (!right_type->is_abstract() && right_type->named_type() != NULL)
4469 *result_type = right_type;
4470 else if (!left_type->is_abstract())
4471 *result_type = left_type;
4472 else if (!right_type->is_abstract())
4473 *result_type = right_type;
4474 else if (left_type->complex_type() != NULL)
4475 *result_type = left_type;
4476 else if (right_type->complex_type() != NULL)
4477 *result_type = right_type;
4478 else if (left_type->float_type() != NULL)
4479 *result_type = left_type;
4480 else if (right_type->float_type() != NULL)
4481 *result_type = right_type;
4482 else if (left_type->integer_type() != NULL
4483 && left_type->integer_type()->is_rune())
4484 *result_type = left_type;
4485 else if (right_type->integer_type() != NULL
4486 && right_type->integer_type()->is_rune())
4487 *result_type = right_type;
4488 else
4489 *result_type = left_type;
4490
4491 return true;
4492}
4493
4494// Convert an integer comparison code and an operator to a boolean
4495// value.
7a938933
ILT
4496
4497bool
5caf63ca 4498Binary_expression::cmp_to_bool(Operator op, int cmp)
7a938933 4499{
7a938933
ILT
4500 switch (op)
4501 {
4502 case OPERATOR_EQEQ:
5caf63ca
ILT
4503 return cmp == 0;
4504 break;
7a938933 4505 case OPERATOR_NOTEQ:
5caf63ca
ILT
4506 return cmp != 0;
4507 break;
7a938933 4508 case OPERATOR_LT:
5caf63ca
ILT
4509 return cmp < 0;
4510 break;
7a938933 4511 case OPERATOR_LE:
5caf63ca 4512 return cmp <= 0;
7a938933 4513 case OPERATOR_GT:
5caf63ca 4514 return cmp > 0;
7a938933 4515 case OPERATOR_GE:
5caf63ca 4516 return cmp >= 0;
7a938933 4517 default:
8c0d1865 4518 go_unreachable();
7a938933
ILT
4519 }
4520}
4521
5caf63ca 4522// Compare constants according to OP.
7a938933
ILT
4523
4524bool
5caf63ca
ILT
4525Binary_expression::compare_constant(Operator op, Numeric_constant* left_nc,
4526 Numeric_constant* right_nc,
4527 Location location, bool* result)
7a938933 4528{
5caf63ca
ILT
4529 Type* left_type = left_nc->type();
4530 Type* right_type = right_nc->type();
4531
4532 Type* type;
4533 if (!Binary_expression::operation_type(op, left_type, right_type, &type))
4534 return false;
4535
4536 // When comparing an untyped operand to a typed operand, we are
4537 // effectively coercing the untyped operand to the other operand's
4538 // type, so make sure that is valid.
4539 if (!left_nc->set_type(type, true, location)
4540 || !right_nc->set_type(type, true, location))
4541 return false;
4542
4543 bool ret;
4544 int cmp;
4545 if (type->complex_type() != NULL)
4546 {
4547 if (op != OPERATOR_EQEQ && op != OPERATOR_NOTEQ)
4548 return false;
4549 ret = Binary_expression::compare_complex(left_nc, right_nc, &cmp);
4550 }
4551 else if (type->float_type() != NULL)
4552 ret = Binary_expression::compare_float(left_nc, right_nc, &cmp);
7a938933 4553 else
5caf63ca
ILT
4554 ret = Binary_expression::compare_integer(left_nc, right_nc, &cmp);
4555
4556 if (ret)
4557 *result = Binary_expression::cmp_to_bool(op, cmp);
4558
4559 return ret;
4560}
4561
4562// Compare integer constants.
4563
4564bool
4565Binary_expression::compare_integer(const Numeric_constant* left_nc,
4566 const Numeric_constant* right_nc,
4567 int* cmp)
4568{
4569 mpz_t left_val;
4570 if (!left_nc->to_int(&left_val))
4571 return false;
4572 mpz_t right_val;
4573 if (!right_nc->to_int(&right_val))
7a938933 4574 {
5caf63ca
ILT
4575 mpz_clear(left_val);
4576 return false;
7a938933 4577 }
5caf63ca
ILT
4578
4579 *cmp = mpz_cmp(left_val, right_val);
4580
4581 mpz_clear(left_val);
4582 mpz_clear(right_val);
4583
4584 return true;
4585}
4586
4587// Compare floating point constants.
4588
4589bool
4590Binary_expression::compare_float(const Numeric_constant* left_nc,
4591 const Numeric_constant* right_nc,
4592 int* cmp)
4593{
4594 mpfr_t left_val;
4595 if (!left_nc->to_float(&left_val))
4596 return false;
4597 mpfr_t right_val;
4598 if (!right_nc->to_float(&right_val))
7a938933 4599 {
5caf63ca
ILT
4600 mpfr_clear(left_val);
4601 return false;
4602 }
4603
4604 // We already coerced both operands to the same type. If that type
4605 // is not an abstract type, we need to round the values accordingly.
4606 Type* type = left_nc->type();
4607 if (!type->is_abstract() && type->float_type() != NULL)
4608 {
4609 int bits = type->float_type()->bits();
4610 mpfr_prec_round(left_val, bits, GMP_RNDN);
4611 mpfr_prec_round(right_val, bits, GMP_RNDN);
7a938933 4612 }
5caf63ca
ILT
4613
4614 *cmp = mpfr_cmp(left_val, right_val);
4615
4616 mpfr_clear(left_val);
4617 mpfr_clear(right_val);
4618
4619 return true;
7a938933
ILT
4620}
4621
5caf63ca
ILT
4622// Compare complex constants. Complex numbers may only be compared
4623// for equality.
7a938933
ILT
4624
4625bool
5caf63ca
ILT
4626Binary_expression::compare_complex(const Numeric_constant* left_nc,
4627 const Numeric_constant* right_nc,
4628 int* cmp)
7a938933 4629{
5caf63ca
ILT
4630 mpfr_t left_real, left_imag;
4631 if (!left_nc->to_complex(&left_real, &left_imag))
4632 return false;
4633 mpfr_t right_real, right_imag;
4634 if (!right_nc->to_complex(&right_real, &right_imag))
7a938933 4635 {
5caf63ca
ILT
4636 mpfr_clear(left_real);
4637 mpfr_clear(left_imag);
4638 return false;
7a938933 4639 }
5caf63ca
ILT
4640
4641 // We already coerced both operands to the same type. If that type
4642 // is not an abstract type, we need to round the values accordingly.
4643 Type* type = left_nc->type();
4644 if (!type->is_abstract() && type->complex_type() != NULL)
7a938933 4645 {
5caf63ca
ILT
4646 int bits = type->complex_type()->bits();
4647 mpfr_prec_round(left_real, bits / 2, GMP_RNDN);
4648 mpfr_prec_round(left_imag, bits / 2, GMP_RNDN);
4649 mpfr_prec_round(right_real, bits / 2, GMP_RNDN);
4650 mpfr_prec_round(right_imag, bits / 2, GMP_RNDN);
7a938933 4651 }
5caf63ca
ILT
4652
4653 *cmp = (mpfr_cmp(left_real, right_real) != 0
4654 || mpfr_cmp(left_imag, right_imag) != 0);
4655
4656 mpfr_clear(left_real);
4657 mpfr_clear(left_imag);
4658 mpfr_clear(right_real);
4659 mpfr_clear(right_imag);
4660
4661 return true;
7a938933
ILT
4662}
4663
5caf63ca
ILT
4664// Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC. Return
4665// true if this could be done, false if not. Issue errors at LOCATION
4666// as appropriate.
7a938933
ILT
4667
4668bool
5caf63ca
ILT
4669Binary_expression::eval_constant(Operator op, Numeric_constant* left_nc,
4670 Numeric_constant* right_nc,
4671 Location location, Numeric_constant* nc)
7a938933 4672{
7a938933
ILT
4673 switch (op)
4674 {
4675 case OPERATOR_OROR:
4676 case OPERATOR_ANDAND:
4677 case OPERATOR_EQEQ:
4678 case OPERATOR_NOTEQ:
4679 case OPERATOR_LT:
4680 case OPERATOR_LE:
4681 case OPERATOR_GT:
4682 case OPERATOR_GE:
ea3ef06a
ILT
4683 // These return boolean values, not numeric.
4684 return false;
5caf63ca
ILT
4685 default:
4686 break;
4687 }
4688
4689 Type* left_type = left_nc->type();
4690 Type* right_type = right_nc->type();
4691
4692 Type* type;
4693 if (!Binary_expression::operation_type(op, left_type, right_type, &type))
4694 return false;
4695
4696 bool is_shift = op == OPERATOR_LSHIFT || op == OPERATOR_RSHIFT;
4697
4698 // When combining an untyped operand with a typed operand, we are
4699 // effectively coercing the untyped operand to the other operand's
4700 // type, so make sure that is valid.
4701 if (!left_nc->set_type(type, true, location))
4702 return false;
4703 if (!is_shift && !right_nc->set_type(type, true, location))
4704 return false;
4705
4706 bool r;
4707 if (type->complex_type() != NULL)
4708 r = Binary_expression::eval_complex(op, left_nc, right_nc, location, nc);
4709 else if (type->float_type() != NULL)
4710 r = Binary_expression::eval_float(op, left_nc, right_nc, location, nc);
4711 else
4712 r = Binary_expression::eval_integer(op, left_nc, right_nc, location, nc);
4713
4714 if (r)
4715 r = nc->set_type(type, true, location);
4716
4717 return r;
4718}
4719
4720// Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC, using
4721// integer operations. Return true if this could be done, false if
4722// not.
4723
4724bool
4725Binary_expression::eval_integer(Operator op, const Numeric_constant* left_nc,
4726 const Numeric_constant* right_nc,
4727 Location location, Numeric_constant* nc)
4728{
4729 mpz_t left_val;
4730 if (!left_nc->to_int(&left_val))
4731 return false;
4732 mpz_t right_val;
4733 if (!right_nc->to_int(&right_val))
4734 {
4735 mpz_clear(left_val);
7a938933 4736 return false;
5caf63ca
ILT
4737 }
4738
4739 mpz_t val;
4740 mpz_init(val);
4741
4742 switch (op)
4743 {
7a938933
ILT
4744 case OPERATOR_PLUS:
4745 mpz_add(val, left_val, right_val);
4746 break;
4747 case OPERATOR_MINUS:
4748 mpz_sub(val, left_val, right_val);
4749 break;
4750 case OPERATOR_OR:
4751 mpz_ior(val, left_val, right_val);
4752 break;
4753 case OPERATOR_XOR:
4754 mpz_xor(val, left_val, right_val);
4755 break;
4756 case OPERATOR_MULT:
4757 mpz_mul(val, left_val, right_val);
4758 break;
4759 case OPERATOR_DIV:
4760 if (mpz_sgn(right_val) != 0)
4761 mpz_tdiv_q(val, left_val, right_val);
4762 else
4763 {
4764 error_at(location, "division by zero");
4765 mpz_set_ui(val, 0);
7a938933
ILT
4766 }
4767 break;
4768 case OPERATOR_MOD:
4769 if (mpz_sgn(right_val) != 0)
4770 mpz_tdiv_r(val, left_val, right_val);
4771 else
4772 {
4773 error_at(location, "division by zero");
4774 mpz_set_ui(val, 0);
7a938933
ILT
4775 }
4776 break;
4777 case OPERATOR_LSHIFT:
4778 {
4779 unsigned long shift = mpz_get_ui(right_val);
5caf63ca
ILT
4780 if (mpz_cmp_ui(right_val, shift) == 0 && shift <= 0x100000)
4781 mpz_mul_2exp(val, left_val, shift);
4782 else
7a938933
ILT
4783 {
4784 error_at(location, "shift count overflow");
4785 mpz_set_ui(val, 0);
7a938933 4786 }
7a938933
ILT
4787 break;
4788 }
4789 break;
4790 case OPERATOR_RSHIFT:
4791 {
4792 unsigned long shift = mpz_get_ui(right_val);
4793 if (mpz_cmp_ui(right_val, shift) != 0)
4794 {
4795 error_at(location, "shift count overflow");
4796 mpz_set_ui(val, 0);
7a938933 4797 }
7a938933 4798 else
5caf63ca
ILT
4799 {
4800 if (mpz_cmp_ui(left_val, 0) >= 0)
4801 mpz_tdiv_q_2exp(val, left_val, shift);
4802 else
4803 mpz_fdiv_q_2exp(val, left_val, shift);
4804 }
7a938933
ILT
4805 break;
4806 }
4807 break;
4808 case OPERATOR_AND:
4809 mpz_and(val, left_val, right_val);
4810 break;
4811 case OPERATOR_BITCLEAR:
4812 {
4813 mpz_t tval;
4814 mpz_init(tval);
4815 mpz_com(tval, right_val);
4816 mpz_and(val, left_val, tval);
4817 mpz_clear(tval);
4818 }
4819 break;
4820 default:
8c0d1865 4821 go_unreachable();
7a938933
ILT
4822 }
4823
5caf63ca
ILT
4824 mpz_clear(left_val);
4825 mpz_clear(right_val);
7a938933 4826
5caf63ca
ILT
4827 if (left_nc->is_rune()
4828 || (op != OPERATOR_LSHIFT
4829 && op != OPERATOR_RSHIFT
4830 && right_nc->is_rune()))
4831 nc->set_rune(NULL, val);
4832 else
4833 nc->set_int(NULL, val);
4834
4835 mpz_clear(val);
7a938933
ILT
4836
4837 return true;
4838}
4839
5caf63ca
ILT
4840// Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC, using
4841// floating point operations. Return true if this could be done,
4842// false if not.
7a938933
ILT
4843
4844bool
5caf63ca
ILT
4845Binary_expression::eval_float(Operator op, const Numeric_constant* left_nc,
4846 const Numeric_constant* right_nc,
4847 Location location, Numeric_constant* nc)
7a938933 4848{
5caf63ca
ILT
4849 mpfr_t left_val;
4850 if (!left_nc->to_float(&left_val))
4851 return false;
4852 mpfr_t right_val;
4853 if (!right_nc->to_float(&right_val))
7a938933 4854 {
5caf63ca 4855 mpfr_clear(left_val);
7a938933 4856 return false;
5caf63ca
ILT
4857 }
4858
4859 mpfr_t val;
4860 mpfr_init(val);
4861
4862 bool ret = true;
4863 switch (op)
4864 {
7a938933
ILT
4865 case OPERATOR_PLUS:
4866 mpfr_add(val, left_val, right_val, GMP_RNDN);
4867 break;
4868 case OPERATOR_MINUS:
4869 mpfr_sub(val, left_val, right_val, GMP_RNDN);
4870 break;
4871 case OPERATOR_OR:
4872 case OPERATOR_XOR:
4873 case OPERATOR_AND:
4874 case OPERATOR_BITCLEAR:
5caf63ca
ILT
4875 case OPERATOR_MOD:
4876 case OPERATOR_LSHIFT:
4877 case OPERATOR_RSHIFT:
4878 mpfr_set_ui(val, 0, GMP_RNDN);
4879 ret = false;
4880 break;
7a938933
ILT
4881 case OPERATOR_MULT:
4882 mpfr_mul(val, left_val, right_val, GMP_RNDN);
4883 break;
4884 case OPERATOR_DIV:
5caf63ca
ILT
4885 if (!mpfr_zero_p(right_val))
4886 mpfr_div(val, left_val, right_val, GMP_RNDN);
4887 else
4888 {
4889 error_at(location, "division by zero");
4890 mpfr_set_ui(val, 0, GMP_RNDN);
4891 }
7a938933 4892 break;
7a938933 4893 default:
8c0d1865 4894 go_unreachable();
7a938933
ILT
4895 }
4896
5caf63ca
ILT
4897 mpfr_clear(left_val);
4898 mpfr_clear(right_val);
7a938933 4899
5caf63ca
ILT
4900 nc->set_float(NULL, val);
4901 mpfr_clear(val);
7a938933 4902
5caf63ca 4903 return ret;
7a938933
ILT
4904}
4905
5caf63ca
ILT
4906// Apply binary opcode OP to LEFT_NC and RIGHT_NC, setting NC, using
4907// complex operations. Return true if this could be done, false if
4908// not.
7a938933
ILT
4909
4910bool
5caf63ca
ILT
4911Binary_expression::eval_complex(Operator op, const Numeric_constant* left_nc,
4912 const Numeric_constant* right_nc,
4913 Location location, Numeric_constant* nc)
7a938933 4914{
5caf63ca
ILT
4915 mpfr_t left_real, left_imag;
4916 if (!left_nc->to_complex(&left_real, &left_imag))
4917 return false;
4918 mpfr_t right_real, right_imag;
4919 if (!right_nc->to_complex(&right_real, &right_imag))
7a938933 4920 {
5caf63ca
ILT
4921 mpfr_clear(left_real);
4922 mpfr_clear(left_imag);
7a938933 4923 return false;
5caf63ca
ILT
4924 }
4925
4926 mpfr_t real, imag;
4927 mpfr_init(real);
4928 mpfr_init(imag);
4929
4930 bool ret = true;
4931 switch (op)
4932 {
7a938933
ILT
4933 case OPERATOR_PLUS:
4934 mpfr_add(real, left_real, right_real, GMP_RNDN);
4935 mpfr_add(imag, left_imag, right_imag, GMP_RNDN);
4936 break;
4937 case OPERATOR_MINUS:
4938 mpfr_sub(real, left_real, right_real, GMP_RNDN);
4939 mpfr_sub(imag, left_imag, right_imag, GMP_RNDN);
4940 break;
4941 case OPERATOR_OR:
4942 case OPERATOR_XOR:
4943 case OPERATOR_AND:
4944 case OPERATOR_BITCLEAR:
5caf63ca
ILT
4945 case OPERATOR_MOD:
4946 case OPERATOR_LSHIFT:
4947 case OPERATOR_RSHIFT:
4948 mpfr_set_ui(real, 0, GMP_RNDN);
4949 mpfr_set_ui(imag, 0, GMP_RNDN);
4950 ret = false;
4951 break;
7a938933
ILT
4952 case OPERATOR_MULT:
4953 {
4954 // You might think that multiplying two complex numbers would
4955 // be simple, and you would be right, until you start to think
4956 // about getting the right answer for infinity. If one
4957 // operand here is infinity and the other is anything other
4958 // than zero or NaN, then we are going to wind up subtracting
4959 // two infinity values. That will give us a NaN, but the
4960 // correct answer is infinity.
4961
4962 mpfr_t lrrr;
4963 mpfr_init(lrrr);
4964 mpfr_mul(lrrr, left_real, right_real, GMP_RNDN);
4965
4966 mpfr_t lrri;
4967 mpfr_init(lrri);
4968 mpfr_mul(lrri, left_real, right_imag, GMP_RNDN);
4969
4970 mpfr_t lirr;
4971 mpfr_init(lirr);
4972 mpfr_mul(lirr, left_imag, right_real, GMP_RNDN);
4973
4974 mpfr_t liri;
4975 mpfr_init(liri);
4976 mpfr_mul(liri, left_imag, right_imag, GMP_RNDN);
4977
4978 mpfr_sub(real, lrrr, liri, GMP_RNDN);
4979 mpfr_add(imag, lrri, lirr, GMP_RNDN);
4980
4981 // If we get NaN on both sides, check whether it should really
4982 // be infinity. The rule is that if either side of the
4983 // complex number is infinity, then the whole value is
4984 // infinity, even if the other side is NaN. So the only case
4985 // we have to fix is the one in which both sides are NaN.
4986 if (mpfr_nan_p(real) && mpfr_nan_p(imag)
4987 && (!mpfr_nan_p(left_real) || !mpfr_nan_p(left_imag))
4988 && (!mpfr_nan_p(right_real) || !mpfr_nan_p(right_imag)))
4989 {
4990 bool is_infinity = false;
4991
4992 mpfr_t lr;
4993 mpfr_t li;
4994 mpfr_init_set(lr, left_real, GMP_RNDN);
4995 mpfr_init_set(li, left_imag, GMP_RNDN);
4996
4997 mpfr_t rr;
4998 mpfr_t ri;
4999 mpfr_init_set(rr, right_real, GMP_RNDN);
5000 mpfr_init_set(ri, right_imag, GMP_RNDN);
5001
5002 // If the left side is infinity, then the result is
5003 // infinity.
5004 if (mpfr_inf_p(lr) || mpfr_inf_p(li))
5005 {
5006 mpfr_set_ui(lr, mpfr_inf_p(lr) ? 1 : 0, GMP_RNDN);
5007 mpfr_copysign(lr, lr, left_real, GMP_RNDN);
5008 mpfr_set_ui(li, mpfr_inf_p(li) ? 1 : 0, GMP_RNDN);
5009 mpfr_copysign(li, li, left_imag, GMP_RNDN);
5010 if (mpfr_nan_p(rr))
5011 {
5012 mpfr_set_ui(rr, 0, GMP_RNDN);
5013 mpfr_copysign(rr, rr, right_real, GMP_RNDN);
5014 }
5015 if (mpfr_nan_p(ri))
5016 {
5017 mpfr_set_ui(ri, 0, GMP_RNDN);
5018 mpfr_copysign(ri, ri, right_imag, GMP_RNDN);
5019 }
5020 is_infinity = true;
5021 }
5022
5023 // If the right side is infinity, then the result is
5024 // infinity.
5025 if (mpfr_inf_p(rr) || mpfr_inf_p(ri))
5026 {
5027 mpfr_set_ui(rr, mpfr_inf_p(rr) ? 1 : 0, GMP_RNDN);
5028 mpfr_copysign(rr, rr, right_real, GMP_RNDN);
5029 mpfr_set_ui(ri, mpfr_inf_p(ri) ? 1 : 0, GMP_RNDN);
5030 mpfr_copysign(ri, ri, right_imag, GMP_RNDN);
5031 if (mpfr_nan_p(lr))
5032 {
5033 mpfr_set_ui(lr, 0, GMP_RNDN);
5034 mpfr_copysign(lr, lr, left_real, GMP_RNDN);
5035 }
5036 if (mpfr_nan_p(li))
5037 {
5038 mpfr_set_ui(li, 0, GMP_RNDN);
5039 mpfr_copysign(li, li, left_imag, GMP_RNDN);
5040 }
5041 is_infinity = true;
5042 }
5043
5044 // If we got an overflow in the intermediate computations,
5045 // then the result is infinity.
5046 if (!is_infinity
5047 && (mpfr_inf_p(lrrr) || mpfr_inf_p(lrri)
5048 || mpfr_inf_p(lirr) || mpfr_inf_p(liri)))
5049 {
5050 if (mpfr_nan_p(lr))
5051 {
5052 mpfr_set_ui(lr, 0, GMP_RNDN);
5053 mpfr_copysign(lr, lr, left_real, GMP_RNDN);
5054 }
5055 if (mpfr_nan_p(li))
5056 {
5057 mpfr_set_ui(li, 0, GMP_RNDN);
5058 mpfr_copysign(li, li, left_imag, GMP_RNDN);
5059 }
5060 if (mpfr_nan_p(rr))
5061 {
5062 mpfr_set_ui(rr, 0, GMP_RNDN);
5063 mpfr_copysign(rr, rr, right_real, GMP_RNDN);
5064 }
5065 if (mpfr_nan_p(ri))
5066 {
5067 mpfr_set_ui(ri, 0, GMP_RNDN);
5068 mpfr_copysign(ri, ri, right_imag, GMP_RNDN);
5069 }
5070 is_infinity = true;
5071 }
5072
5073 if (is_infinity)
5074 {
5075 mpfr_mul(lrrr, lr, rr, GMP_RNDN);
5076 mpfr_mul(lrri, lr, ri, GMP_RNDN);
5077 mpfr_mul(lirr, li, rr, GMP_RNDN);
5078 mpfr_mul(liri, li, ri, GMP_RNDN);
5079 mpfr_sub(real, lrrr, liri, GMP_RNDN);
5080 mpfr_add(imag, lrri, lirr, GMP_RNDN);
5081 mpfr_set_inf(real, mpfr_sgn(real));
5082 mpfr_set_inf(imag, mpfr_sgn(imag));
5083 }
5084
5085 mpfr_clear(lr);
5086 mpfr_clear(li);
5087 mpfr_clear(rr);
5088 mpfr_clear(ri);
5089 }
5090
5091 mpfr_clear(lrrr);
5092 mpfr_clear(lrri);
5093 mpfr_clear(lirr);
5094 mpfr_clear(liri);
5095 }
5096 break;
5097 case OPERATOR_DIV:
5098 {
5099 // For complex division we want to avoid having an
5100 // intermediate overflow turn the whole result in a NaN. We
5101 // scale the values to try to avoid this.
5102
5103 if (mpfr_zero_p(right_real) && mpfr_zero_p(right_imag))
5caf63ca
ILT
5104 {
5105 error_at(location, "division by zero");
5106 mpfr_set_ui(real, 0, GMP_RNDN);
5107 mpfr_set_ui(imag, 0, GMP_RNDN);
5108 break;
5109 }
7a938933
ILT
5110
5111 mpfr_t rra;
5112 mpfr_t ria;
5113 mpfr_init(rra);
5114 mpfr_init(ria);
5115 mpfr_abs(rra, right_real, GMP_RNDN);
5116 mpfr_abs(ria, right_imag, GMP_RNDN);
5117 mpfr_t t;
5118 mpfr_init(t);
5119 mpfr_max(t, rra, ria, GMP_RNDN);
5120
5121 mpfr_t rr;
5122 mpfr_t ri;
5123 mpfr_init_set(rr, right_real, GMP_RNDN);
5124 mpfr_init_set(ri, right_imag, GMP_RNDN);
5125 long ilogbw = 0;
5126 if (!mpfr_inf_p(t) && !mpfr_nan_p(t) && !mpfr_zero_p(t))
5127 {
5128 ilogbw = mpfr_get_exp(t);
5129 mpfr_mul_2si(rr, rr, - ilogbw, GMP_RNDN);
5130 mpfr_mul_2si(ri, ri, - ilogbw, GMP_RNDN);
5131 }
5132
5133 mpfr_t denom;
5134 mpfr_init(denom);
5135 mpfr_mul(denom, rr, rr, GMP_RNDN);
5136 mpfr_mul(t, ri, ri, GMP_RNDN);
5137 mpfr_add(denom, denom, t, GMP_RNDN);
5138
5139 mpfr_mul(real, left_real, rr, GMP_RNDN);
5140 mpfr_mul(t, left_imag, ri, GMP_RNDN);
5141 mpfr_add(real, real, t, GMP_RNDN);
5142 mpfr_div(real, real, denom, GMP_RNDN);
5143 mpfr_mul_2si(real, real, - ilogbw, GMP_RNDN);
5144
5145 mpfr_mul(imag, left_imag, rr, GMP_RNDN);
5146 mpfr_mul(t, left_real, ri, GMP_RNDN);
5147 mpfr_sub(imag, imag, t, GMP_RNDN);
5148 mpfr_div(imag, imag, denom, GMP_RNDN);
5149 mpfr_mul_2si(imag, imag, - ilogbw, GMP_RNDN);
5150
5151 // If we wind up with NaN on both sides, check whether we
5152 // should really have infinity. The rule is that if either
5153 // side of the complex number is infinity, then the whole
5154 // value is infinity, even if the other side is NaN. So the
5155 // only case we have to fix is the one in which both sides are
5156 // NaN.
5157 if (mpfr_nan_p(real) && mpfr_nan_p(imag)
5158 && (!mpfr_nan_p(left_real) || !mpfr_nan_p(left_imag))
5159 && (!mpfr_nan_p(right_real) || !mpfr_nan_p(right_imag)))
5160 {
5161 if (mpfr_zero_p(denom))
5162 {
5163 mpfr_set_inf(real, mpfr_sgn(rr));
5164 mpfr_mul(real, real, left_real, GMP_RNDN);
5165 mpfr_set_inf(imag, mpfr_sgn(rr));
5166 mpfr_mul(imag, imag, left_imag, GMP_RNDN);
5167 }
5168 else if ((mpfr_inf_p(left_real) || mpfr_inf_p(left_imag))
5169 && mpfr_number_p(rr) && mpfr_number_p(ri))
5170 {
5171 mpfr_set_ui(t, mpfr_inf_p(left_real) ? 1 : 0, GMP_RNDN);
5172 mpfr_copysign(t, t, left_real, GMP_RNDN);
5173
5174 mpfr_t t2;
5175 mpfr_init_set_ui(t2, mpfr_inf_p(left_imag) ? 1 : 0, GMP_RNDN);
5176 mpfr_copysign(t2, t2, left_imag, GMP_RNDN);
5177
5178 mpfr_t t3;
5179 mpfr_init(t3);
5180 mpfr_mul(t3, t, rr, GMP_RNDN);
5181
5182 mpfr_t t4;
5183 mpfr_init(t4);
5184 mpfr_mul(t4, t2, ri, GMP_RNDN);
5185
5186 mpfr_add(t3, t3, t4, GMP_RNDN);
5187 mpfr_set_inf(real, mpfr_sgn(t3));
5188
5189 mpfr_mul(t3, t2, rr, GMP_RNDN);
5190 mpfr_mul(t4, t, ri, GMP_RNDN);
5191 mpfr_sub(t3, t3, t4, GMP_RNDN);
5192 mpfr_set_inf(imag, mpfr_sgn(t3));
5193
5194 mpfr_clear(t2);
5195 mpfr_clear(t3);
5196 mpfr_clear(t4);
5197 }
5198 else if ((mpfr_inf_p(right_real) || mpfr_inf_p(right_imag))
5199 && mpfr_number_p(left_real) && mpfr_number_p(left_imag))
5200 {
5201 mpfr_set_ui(t, mpfr_inf_p(rr) ? 1 : 0, GMP_RNDN);
5202 mpfr_copysign(t, t, rr, GMP_RNDN);
5203
5204 mpfr_t t2;
5205 mpfr_init_set_ui(t2, mpfr_inf_p(ri) ? 1 : 0, GMP_RNDN);
5206 mpfr_copysign(t2, t2, ri, GMP_RNDN);
5207
5208 mpfr_t t3;
5209 mpfr_init(t3);
5210 mpfr_mul(t3, left_real, t, GMP_RNDN);
5211
5212 mpfr_t t4;
5213 mpfr_init(t4);
5214 mpfr_mul(t4, left_imag, t2, GMP_RNDN);
5215
5216 mpfr_add(t3, t3, t4, GMP_RNDN);
5217 mpfr_set_ui(real, 0, GMP_RNDN);
5218 mpfr_mul(real, real, t3, GMP_RNDN);
5219
5220 mpfr_mul(t3, left_imag, t, GMP_RNDN);
5221 mpfr_mul(t4, left_real, t2, GMP_RNDN);
5222 mpfr_sub(t3, t3, t4, GMP_RNDN);
5223 mpfr_set_ui(imag, 0, GMP_RNDN);
5224 mpfr_mul(imag, imag, t3, GMP_RNDN);
5225
5226 mpfr_clear(t2);
5227 mpfr_clear(t3);
5228 mpfr_clear(t4);
5229 }
5230 }
5231
5232 mpfr_clear(denom);
5233 mpfr_clear(rr);
5234 mpfr_clear(ri);
5235 mpfr_clear(t);
5236 mpfr_clear(rra);
5237 mpfr_clear(ria);
5238 }
5239 break;
7a938933 5240 default:
8c0d1865 5241 go_unreachable();
7a938933
ILT
5242 }
5243
5caf63ca
ILT
5244 mpfr_clear(left_real);
5245 mpfr_clear(left_imag);
5246 mpfr_clear(right_real);
5247 mpfr_clear(right_imag);
7a938933 5248
5caf63ca
ILT
5249 nc->set_complex(NULL, real, imag);
5250 mpfr_clear(real);
5251 mpfr_clear(imag);
7a938933 5252
5caf63ca 5253 return ret;
7a938933
ILT
5254}
5255
5256// Lower a binary expression. We have to evaluate constant
5257// expressions now, in order to implement Go's unlimited precision
5258// constants.
5259
5260Expression*
f9f96987
ILT
5261Binary_expression::do_lower(Gogo* gogo, Named_object*,
5262 Statement_inserter* inserter, int)
7a938933 5263{
8afa2bfb 5264 Location location = this->location();
7a938933
ILT
5265 Operator op = this->op_;
5266 Expression* left = this->left_;
5267 Expression* right = this->right_;
5268
5269 const bool is_comparison = (op == OPERATOR_EQEQ
5270 || op == OPERATOR_NOTEQ
5271 || op == OPERATOR_LT
5272 || op == OPERATOR_LE
5273 || op == OPERATOR_GT
5274 || op == OPERATOR_GE);
5275
5caf63ca 5276 // Numeric constant expressions.
7a938933 5277 {
5caf63ca
ILT
5278 Numeric_constant left_nc;
5279 Numeric_constant right_nc;
5280 if (left->numeric_constant_value(&left_nc)
5281 && right->numeric_constant_value(&right_nc))
7a938933 5282 {
5caf63ca 5283 if (is_comparison)
7a938933 5284 {
5caf63ca
ILT
5285 bool result;
5286 if (!Binary_expression::compare_constant(op, &left_nc,
5287 &right_nc, location,
5288 &result))
5289 return this;
610d0e16 5290 return Expression::make_cast(Type::make_boolean_type(),
5caf63ca
ILT
5291 Expression::make_boolean(result,
5292 location),
5293 location);
7a938933
ILT
5294 }
5295 else
5296 {
5caf63ca
ILT
5297 Numeric_constant nc;
5298 if (!Binary_expression::eval_constant(op, &left_nc, &right_nc,
5299 location, &nc))
5300 return this;
5301 return nc.expression(location);
7a938933
ILT
5302 }
5303 }
7a938933
ILT
5304 }
5305
5306 // String constant expressions.
60a3da27 5307 if (left->type()->is_string_type() && right->type()->is_string_type())
7a938933
ILT
5308 {
5309 std::string left_string;
5310 std::string right_string;
5311 if (left->string_constant_value(&left_string)
5312 && right->string_constant_value(&right_string))
60a3da27
ILT
5313 {
5314 if (op == OPERATOR_PLUS)
5315 return Expression::make_string(left_string + right_string,
5316 location);
5317 else if (is_comparison)
5318 {
5319 int cmp = left_string.compare(right_string);
5caf63ca 5320 bool r = Binary_expression::cmp_to_bool(op, cmp);
610d0e16 5321 return Expression::make_boolean(r, location);
fe53b3dd
ILT
5322 }
5323 }
fe53b3dd
ILT
5324 }
5325
58c55a32 5326 // Lower struct, array, and some interface comparisons.
f9f96987
ILT
5327 if (op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ)
5328 {
5329 if (left->type()->struct_type() != NULL)
5330 return this->lower_struct_comparison(gogo, inserter);
5331 else if (left->type()->array_type() != NULL
5332 && !left->type()->is_slice_type())
5333 return this->lower_array_comparison(gogo, inserter);
58c55a32
ILT
5334 else if ((left->type()->interface_type() != NULL
5335 && right->type()->interface_type() == NULL)
5336 || (left->type()->interface_type() == NULL
5337 && right->type()->interface_type() != NULL))
5338 return this->lower_interface_value_comparison(gogo, inserter);
f9f96987
ILT
5339 }
5340
7a938933
ILT
5341 return this;
5342}
5343
f9f96987
ILT
5344// Lower a struct comparison.
5345
5346Expression*
5347Binary_expression::lower_struct_comparison(Gogo* gogo,
5348 Statement_inserter* inserter)
5349{
5350 Struct_type* st = this->left_->type()->struct_type();
5351 Struct_type* st2 = this->right_->type()->struct_type();
5352 if (st2 == NULL)
5353 return this;
5354 if (st != st2 && !Type::are_identical(st, st2, false, NULL))
5355 return this;
5356 if (!Type::are_compatible_for_comparison(true, this->left_->type(),
5357 this->right_->type(), NULL))
5358 return this;
5359
5360 // See if we can compare using memcmp. As a heuristic, we use
5361 // memcmp rather than field references and comparisons if there are
5362 // more than two fields.
11d8e1a4 5363 if (st->compare_is_identity(gogo) && st->total_field_count() > 2)
f9f96987
ILT
5364 return this->lower_compare_to_memcmp(gogo, inserter);
5365
5366 Location loc = this->location();
5367
5368 Expression* left = this->left_;
5369 Temporary_statement* left_temp = NULL;
5370 if (left->var_expression() == NULL
5371 && left->temporary_reference_expression() == NULL)
5372 {
5373 left_temp = Statement::make_temporary(left->type(), NULL, loc);
5374 inserter->insert(left_temp);
5375 left = Expression::make_set_and_use_temporary(left_temp, left, loc);
5376 }
5377
5378 Expression* right = this->right_;
5379 Temporary_statement* right_temp = NULL;
5380 if (right->var_expression() == NULL
5381 && right->temporary_reference_expression() == NULL)
5382 {
5383 right_temp = Statement::make_temporary(right->type(), NULL, loc);
5384 inserter->insert(right_temp);
5385 right = Expression::make_set_and_use_temporary(right_temp, right, loc);
5386 }
5387
5388 Expression* ret = Expression::make_boolean(true, loc);
5389 const Struct_field_list* fields = st->fields();
5390 unsigned int field_index = 0;
5391 for (Struct_field_list::const_iterator pf = fields->begin();
5392 pf != fields->end();
5393 ++pf, ++field_index)
5394 {
6c74ce92
ILT
5395 if (Gogo::is_sink_name(pf->field_name()))
5396 continue;
5397
f9f96987
ILT
5398 if (field_index > 0)
5399 {
5400 if (left_temp == NULL)
5401 left = left->copy();
5402 else
5403 left = Expression::make_temporary_reference(left_temp, loc);
5404 if (right_temp == NULL)
5405 right = right->copy();
5406 else
5407 right = Expression::make_temporary_reference(right_temp, loc);
5408 }
5409 Expression* f1 = Expression::make_field_reference(left, field_index,
5410 loc);
5411 Expression* f2 = Expression::make_field_reference(right, field_index,
5412 loc);
5413 Expression* cond = Expression::make_binary(OPERATOR_EQEQ, f1, f2, loc);
5414 ret = Expression::make_binary(OPERATOR_ANDAND, ret, cond, loc);
5415 }
5416
5417 if (this->op_ == OPERATOR_NOTEQ)
5418 ret = Expression::make_unary(OPERATOR_NOT, ret, loc);
5419
5420 return ret;
5421}
5422
5423// Lower an array comparison.
5424
5425Expression*
5426Binary_expression::lower_array_comparison(Gogo* gogo,
5427 Statement_inserter* inserter)
5428{
5429 Array_type* at = this->left_->type()->array_type();
5430 Array_type* at2 = this->right_->type()->array_type();
5431 if (at2 == NULL)
5432 return this;
5433 if (at != at2 && !Type::are_identical(at, at2, false, NULL))
5434 return this;
5435 if (!Type::are_compatible_for_comparison(true, this->left_->type(),
5436 this->right_->type(), NULL))
5437 return this;
5438
5439 // Call memcmp directly if possible. This may let the middle-end
5440 // optimize the call.
11d8e1a4 5441 if (at->compare_is_identity(gogo))
f9f96987
ILT
5442 return this->lower_compare_to_memcmp(gogo, inserter);
5443
5444 // Call the array comparison function.
5445 Named_object* hash_fn;
5446 Named_object* equal_fn;
5447 at->type_functions(gogo, this->left_->type()->named_type(), NULL, NULL,
5448 &hash_fn, &equal_fn);
5449
5450 Location loc = this->location();
5451
5452 Expression* func = Expression::make_func_reference(equal_fn, NULL, loc);
5453
5454 Expression_list* args = new Expression_list();
5455 args->push_back(this->operand_address(inserter, this->left_));
5456 args->push_back(this->operand_address(inserter, this->right_));
5457 args->push_back(Expression::make_type_info(at, TYPE_INFO_SIZE));
5458
5459 Expression* ret = Expression::make_call(func, args, false, loc);
5460
5461 if (this->op_ == OPERATOR_NOTEQ)
5462 ret = Expression::make_unary(OPERATOR_NOT, ret, loc);
5463
5464 return ret;
5465}
5466
58c55a32
ILT
5467// Lower an interface to value comparison.
5468
5469Expression*
5470Binary_expression::lower_interface_value_comparison(Gogo*,
5471 Statement_inserter* inserter)
5472{
5473 Type* left_type = this->left_->type();
5474 Type* right_type = this->right_->type();
5475 Interface_type* ift;
5476 if (left_type->interface_type() != NULL)
5477 {
5478 ift = left_type->interface_type();
5479 if (!ift->implements_interface(right_type, NULL))
5480 return this;
5481 }
5482 else
5483 {
5484 ift = right_type->interface_type();
5485 if (!ift->implements_interface(left_type, NULL))
5486 return this;
5487 }
5488 if (!Type::are_compatible_for_comparison(true, left_type, right_type, NULL))
5489 return this;
5490
5491 Location loc = this->location();
5492
5493 if (left_type->interface_type() == NULL
5494 && left_type->points_to() == NULL
5495 && !this->left_->is_addressable())
5496 {
5497 Temporary_statement* temp =
5498 Statement::make_temporary(left_type, NULL, loc);
5499 inserter->insert(temp);
5500 this->left_ =
5501 Expression::make_set_and_use_temporary(temp, this->left_, loc);
5502 }
5503
5504 if (right_type->interface_type() == NULL
5505 && right_type->points_to() == NULL
5506 && !this->right_->is_addressable())
5507 {
5508 Temporary_statement* temp =
5509 Statement::make_temporary(right_type, NULL, loc);
5510 inserter->insert(temp);
5511 this->right_ =
5512 Expression::make_set_and_use_temporary(temp, this->right_, loc);
5513 }
5514
5515 return this;
5516}
5517
f9f96987
ILT
5518// Lower a struct or array comparison to a call to memcmp.
5519
5520Expression*
5521Binary_expression::lower_compare_to_memcmp(Gogo*, Statement_inserter* inserter)
5522{
5523 Location loc = this->location();
5524
5525 Expression* a1 = this->operand_address(inserter, this->left_);
5526 Expression* a2 = this->operand_address(inserter, this->right_);
5527 Expression* len = Expression::make_type_info(this->left_->type(),
5528 TYPE_INFO_SIZE);
5529
5530 Expression* call = Runtime::make_call(Runtime::MEMCMP, loc, 3, a1, a2, len);
5531
5532 mpz_t zval;
5533 mpz_init_set_ui(zval, 0);
5534 Expression* zero = Expression::make_integer(&zval, NULL, loc);
5535 mpz_clear(zval);
5536
5537 return Expression::make_binary(this->op_, call, zero, loc);
5538}
5539
5540// Return the address of EXPR, cast to unsafe.Pointer.
5541
5542Expression*
5543Binary_expression::operand_address(Statement_inserter* inserter,
5544 Expression* expr)
5545{
5546 Location loc = this->location();
5547
5548 if (!expr->is_addressable())
5549 {
5550 Temporary_statement* temp = Statement::make_temporary(expr->type(), NULL,
5551 loc);
5552 inserter->insert(temp);
5553 expr = Expression::make_set_and_use_temporary(temp, expr, loc);
5554 }
5555 expr = Expression::make_unary(OPERATOR_AND, expr, loc);
5556 static_cast<Unary_expression*>(expr)->set_does_not_escape();
5557 Type* void_type = Type::make_void_type();
5558 Type* unsafe_pointer_type = Type::make_pointer_type(void_type);
5559 return Expression::make_cast(unsafe_pointer_type, expr, loc);
5560}
5561
5caf63ca 5562// Return the numeric constant value, if it has one.
7a938933
ILT
5563
5564bool
5caf63ca 5565Binary_expression::do_numeric_constant_value(Numeric_constant* nc) const
7a938933 5566{
5caf63ca
ILT
5567 Numeric_constant left_nc;
5568 if (!this->left_->numeric_constant_value(&left_nc))
5569 return false;
5570 Numeric_constant right_nc;
5571 if (!this->right_->numeric_constant_value(&right_nc))
5572 return false;
ea3ef06a 5573 return Binary_expression::eval_constant(this->op_, &left_nc, &right_nc,
5caf63ca 5574 this->location(), nc);
7a938933
ILT
5575}
5576
5577// Note that the value is being discarded.
5578
3f7af571 5579bool
7a938933
ILT
5580Binary_expression::do_discarding_value()
5581{
5582 if (this->op_ == OPERATOR_OROR || this->op_ == OPERATOR_ANDAND)
3f7af571 5583 return this->right_->discarding_value();
7a938933 5584 else
3f7af571
ILT
5585 {
5586 this->unused_value_error();
5587 return false;
5588 }
7a938933
ILT
5589}
5590
5591// Get type.
5592
5593Type*
5594Binary_expression::do_type()
5595{
ced2ec3b
ILT
5596 if (this->classification() == EXPRESSION_ERROR)
5597 return Type::make_error_type();
5598
7a938933
ILT
5599 switch (this->op_)
5600 {
7a938933
ILT
5601 case OPERATOR_EQEQ:
5602 case OPERATOR_NOTEQ:
5603 case OPERATOR_LT:
5604 case OPERATOR_LE:
5605 case OPERATOR_GT:
5606 case OPERATOR_GE:
610d0e16
ILT
5607 if (this->type_ == NULL)
5608 this->type_ = Type::make_boolean_type();
5609 return this->type_;
7a938933
ILT
5610
5611 case OPERATOR_PLUS:
5612 case OPERATOR_MINUS:
5613 case OPERATOR_OR:
5614 case OPERATOR_XOR:
5615 case OPERATOR_MULT:
5616 case OPERATOR_DIV:
5617 case OPERATOR_MOD:
5618 case OPERATOR_AND:
5619 case OPERATOR_BITCLEAR:
610d0e16
ILT
5620 case OPERATOR_OROR:
5621 case OPERATOR_ANDAND:
7a938933 5622 {
5caf63ca
ILT
5623 Type* type;
5624 if (!Binary_expression::operation_type(this->op_,
5625 this->left_->type(),
5626 this->right_->type(),
5627 &type))
5628 return Type::make_error_type();
5629 return type;
7a938933
ILT
5630 }
5631
5632 case OPERATOR_LSHIFT:
5633 case OPERATOR_RSHIFT:
5634 return this->left_->type();
5635
5636 default:
8c0d1865 5637 go_unreachable();
7a938933
ILT
5638 }
5639}
5640
5641// Set type for a binary expression.
5642
5643void
5644Binary_expression::do_determine_type(const Type_context* context)
5645{
5646 Type* tleft = this->left_->type();
5647 Type* tright = this->right_->type();
5648
5649 // Both sides should have the same type, except for the shift
5650 // operations. For a comparison, we should ignore the incoming
5651 // type.
5652
5653 bool is_shift_op = (this->op_ == OPERATOR_LSHIFT
5654 || this->op_ == OPERATOR_RSHIFT);
5655
5656 bool is_comparison = (this->op_ == OPERATOR_EQEQ
5657 || this->op_ == OPERATOR_NOTEQ
5658 || this->op_ == OPERATOR_LT
5659 || this->op_ == OPERATOR_LE
5660 || this->op_ == OPERATOR_GT
5661 || this->op_ == OPERATOR_GE);
5662
5663 Type_context subcontext(*context);
5664
5665 if (is_comparison)
5666 {
5667 // In a comparison, the context does not determine the types of
5668 // the operands.
5669 subcontext.type = NULL;
5670 }
5671
3d317d48
ILT
5672 if (this->op_ == OPERATOR_ANDAND || this->op_ == OPERATOR_OROR)
5673 {
5674 // For a logical operation, the context does not determine the
5675 // types of the operands. The operands must be some boolean
5676 // type but if the context has a boolean type they do not
5677 // inherit it. See http://golang.org/issue/3924.
5678 subcontext.type = NULL;
5679 }
5680
7a938933
ILT
5681 // Set the context for the left hand operand.
5682 if (is_shift_op)
5683 {
fe53b3dd
ILT
5684 // The right hand operand of a shift plays no role in
5685 // determining the type of the left hand operand.
7a938933
ILT
5686 }
5687 else if (!tleft->is_abstract())
5688 subcontext.type = tleft;
5689 else if (!tright->is_abstract())
5690 subcontext.type = tright;
5691 else if (subcontext.type == NULL)
5692 {
5693 if ((tleft->integer_type() != NULL && tright->integer_type() != NULL)
5694 || (tleft->float_type() != NULL && tright->float_type() != NULL)
5695 || (tleft->complex_type() != NULL && tright->complex_type() != NULL))
5696 {
5697 // Both sides have an abstract integer, abstract float, or
5698 // abstract complex type. Just let CONTEXT determine
5699 // whether they may remain abstract or not.
5700 }
5701 else if (tleft->complex_type() != NULL)
5702 subcontext.type = tleft;
5703 else if (tright->complex_type() != NULL)
5704 subcontext.type = tright;
5705 else if (tleft->float_type() != NULL)
5706 subcontext.type = tleft;
5707 else if (tright->float_type() != NULL)
5708 subcontext.type = tright;
5709 else
5710 subcontext.type = tleft;
e372156c
ILT
5711
5712 if (subcontext.type != NULL && !context->may_be_abstract)
5713 subcontext.type = subcontext.type->make_non_abstract_type();
7a938933
ILT
5714 }
5715
5716 this->left_->determine_type(&subcontext);
5717
7a938933
ILT
5718 if (is_shift_op)
5719 {
fe53b3dd
ILT
5720 // We may have inherited an unusable type for the shift operand.
5721 // Give a useful error if that happened.
5722 if (tleft->is_abstract()
5723 && subcontext.type != NULL
d720b70f 5724 && !subcontext.may_be_abstract
69d8df44 5725 && subcontext.type->interface_type() == NULL
d720b70f 5726 && subcontext.type->integer_type() == NULL)
fe53b3dd 5727 this->report_error(("invalid context-determined non-integer type "
d720b70f 5728 "for left operand of shift"));
fe53b3dd
ILT
5729
5730 // The context for the right hand operand is the same as for the
5731 // left hand operand, except for a shift operator.
7a938933
ILT
5732 subcontext.type = Type::lookup_integer_type("uint");
5733 subcontext.may_be_abstract = false;
5734 }
5735
5736 this->right_->determine_type(&subcontext);
610d0e16
ILT
5737
5738 if (is_comparison)
5739 {
5740 if (this->type_ != NULL && !this->type_->is_abstract())
5741 ;
5742 else if (context->type != NULL && context->type->is_boolean_type())
5743 this->type_ = context->type;
5744 else if (!context->may_be_abstract)
5745 this->type_ = Type::lookup_bool_type();
5746 }
7a938933
ILT
5747}
5748
5749// Report an error if the binary operator OP does not support TYPE.
1358551f
ILT
5750// OTYPE is the type of the other operand. Return whether the
5751// operation is OK. This should not be used for shift.
7a938933
ILT
5752
5753bool
1358551f 5754Binary_expression::check_operator_type(Operator op, Type* type, Type* otype,
8afa2bfb 5755 Location location)
7a938933
ILT
5756{
5757 switch (op)
5758 {
5759 case OPERATOR_OROR:
5760 case OPERATOR_ANDAND:
5761 if (!type->is_boolean_type())
5762 {
5763 error_at(location, "expected boolean type");
5764 return false;
5765 }
5766 break;
5767
5768 case OPERATOR_EQEQ:
5769 case OPERATOR_NOTEQ:
f9f96987
ILT
5770 {
5771 std::string reason;
5772 if (!Type::are_compatible_for_comparison(true, type, otype, &reason))
5773 {
5774 error_at(location, "%s", reason.c_str());
5775 return false;
5776 }
5777 }
7a938933
ILT
5778 break;
5779
5780 case OPERATOR_LT:
5781 case OPERATOR_LE:
5782 case OPERATOR_GT:
5783 case OPERATOR_GE:
f9f96987
ILT
5784 {
5785 std::string reason;
5786 if (!Type::are_compatible_for_comparison(false, type, otype, &reason))
5787 {
5788 error_at(location, "%s", reason.c_str());
5789 return false;
5790 }
5791 }
7a938933
ILT
5792 break;
5793
5794 case OPERATOR_PLUS:
5795 case OPERATOR_PLUSEQ:
5796 if (type->integer_type() == NULL
5797 && type->float_type() == NULL
5798 && type->complex_type() == NULL
5799 && !type->is_string_type())
5800 {
5801 error_at(location,
5802 "expected integer, floating, complex, or string type");
5803 return false;
5804 }
5805 break;
5806
5807 case OPERATOR_MINUS:
5808 case OPERATOR_MINUSEQ:
5809 case OPERATOR_MULT:
5810 case OPERATOR_MULTEQ:
5811 case OPERATOR_DIV:
5812 case OPERATOR_DIVEQ:
5813 if (type->integer_type() == NULL
5814 && type->float_type() == NULL
5815 && type->complex_type() == NULL)
5816 {
5817 error_at(location, "expected integer, floating, or complex type");
5818 return false;
5819 }
5820 break;
5821
5822 case OPERATOR_MOD:
5823 case OPERATOR_MODEQ:
5824 case OPERATOR_OR:
5825 case OPERATOR_OREQ:
5826 case OPERATOR_AND:
5827 case OPERATOR_ANDEQ:
5828 case OPERATOR_XOR:
5829 case OPERATOR_XOREQ:
5830 case OPERATOR_BITCLEAR:
5831 case OPERATOR_BITCLEAREQ:
5832 if (type->integer_type() == NULL)
5833 {
5834 error_at(location, "expected integer type");
5835 return false;
5836 }
5837 break;
5838
5839 default:
8c0d1865 5840 go_unreachable();
7a938933
ILT
5841 }
5842
5843 return true;
5844}
5845
5846// Check types.
5847
5848void
5849Binary_expression::do_check_types(Gogo*)
5850{
ced2ec3b
ILT
5851 if (this->classification() == EXPRESSION_ERROR)
5852 return;
5853
7a938933
ILT
5854 Type* left_type = this->left_->type();
5855 Type* right_type = this->right_->type();
02ed921a 5856 if (left_type->is_error() || right_type->is_error())
173fb2ff
ILT
5857 {
5858 this->set_is_error();
5859 return;
5860 }
7a938933
ILT
5861
5862 if (this->op_ == OPERATOR_EQEQ
5863 || this->op_ == OPERATOR_NOTEQ
5864 || this->op_ == OPERATOR_LT
5865 || this->op_ == OPERATOR_LE
5866 || this->op_ == OPERATOR_GT
5867 || this->op_ == OPERATOR_GE)
5868 {
3fa057a4
ILT
5869 if (left_type->is_nil_type() && right_type->is_nil_type())
5870 {
5871 this->report_error(_("invalid comparison of nil with nil"));
5872 return;
5873 }
7a938933
ILT
5874 if (!Type::are_assignable(left_type, right_type, NULL)
5875 && !Type::are_assignable(right_type, left_type, NULL))
5876 {
5877 this->report_error(_("incompatible types in binary expression"));
5878 return;
5879 }
5880 if (!Binary_expression::check_operator_type(this->op_, left_type,
1358551f 5881 right_type,
7a938933
ILT
5882 this->location())
5883 || !Binary_expression::check_operator_type(this->op_, right_type,
1358551f 5884 left_type,
7a938933
ILT
5885 this->location()))
5886 {
5887 this->set_is_error();
5888 return;
5889 }
5890 }
5891 else if (this->op_ != OPERATOR_LSHIFT && this->op_ != OPERATOR_RSHIFT)
5892 {
5893 if (!Type::are_compatible_for_binop(left_type, right_type))
5894 {
5895 this->report_error(_("incompatible types in binary expression"));
5896 return;
5897 }
5898 if (!Binary_expression::check_operator_type(this->op_, left_type,
1358551f 5899 right_type,
7a938933
ILT
5900 this->location()))
5901 {
5902 this->set_is_error();
5903 return;
5904 }
69908ca8
ILT
5905 if (this->op_ == OPERATOR_DIV || this->op_ == OPERATOR_MOD)
5906 {
5907 // Division by a zero integer constant is an error.
5908 Numeric_constant rconst;
5909 unsigned long rval;
5910 if (left_type->integer_type() != NULL
5911 && this->right_->numeric_constant_value(&rconst)
5912 && rconst.to_unsigned_long(&rval) == Numeric_constant::NC_UL_VALID
5913 && rval == 0)
5914 {
5915 this->report_error(_("integer division by zero"));
5916 return;
5917 }
5918 }
7a938933
ILT
5919 }
5920 else
5921 {
5922 if (left_type->integer_type() == NULL)
5923 this->report_error(_("shift of non-integer operand"));
5924
5925 if (!right_type->is_abstract()
5926 && (right_type->integer_type() == NULL
5927 || !right_type->integer_type()->is_unsigned()))
5928 this->report_error(_("shift count not unsigned integer"));
5929 else
5930 {
5caf63ca
ILT
5931 Numeric_constant nc;
5932 if (this->right_->numeric_constant_value(&nc))
7a938933 5933 {
5caf63ca
ILT
5934 mpz_t val;
5935 if (!nc.to_int(&val))
5936 this->report_error(_("shift count not unsigned integer"));
5937 else
7657ab90 5938 {
5caf63ca
ILT
5939 if (mpz_sgn(val) < 0)
5940 {
5941 this->report_error(_("negative shift count"));
5942 mpz_set_ui(val, 0);
5943 Location rloc = this->right_->location();
5944 this->right_ = Expression::make_integer(&val, right_type,
5945 rloc);
5946 }
5947 mpz_clear(val);
7657ab90 5948 }
7a938933 5949 }
7a938933
ILT
5950 }
5951 }
5952}
5953
5954// Get a tree for a binary expression.
5955
5956tree
5957Binary_expression::do_get_tree(Translate_context* context)
5958{
776f27a6
ILT
5959 Gogo* gogo = context->gogo();
5960
7a938933
ILT
5961 tree left = this->left_->get_tree(context);
5962 tree right = this->right_->get_tree(context);
5963
5964 if (left == error_mark_node || right == error_mark_node)
5965 return error_mark_node;
5966
5967 enum tree_code code;
5968 bool use_left_type = true;
5969 bool is_shift_op = false;
63d1e46d 5970 bool is_idiv_op = false;
7a938933
ILT
5971 switch (this->op_)
5972 {
5973 case OPERATOR_EQEQ:
5974 case OPERATOR_NOTEQ:
5975 case OPERATOR_LT:
5976 case OPERATOR_LE:
5977 case OPERATOR_GT:
5978 case OPERATOR_GE:
610d0e16 5979 return Expression::comparison_tree(context, this->type_, this->op_,
58c55a32 5980 this->left_, this->right_,
7a938933
ILT
5981 this->location());
5982
5983 case OPERATOR_OROR:
5984 code = TRUTH_ORIF_EXPR;
5985 use_left_type = false;
5986 break;
5987 case OPERATOR_ANDAND:
5988 code = TRUTH_ANDIF_EXPR;
5989 use_left_type = false;
5990 break;
5991 case OPERATOR_PLUS:
5992 code = PLUS_EXPR;
5993 break;
5994 case OPERATOR_MINUS:
5995 code = MINUS_EXPR;
5996 break;
5997 case OPERATOR_OR:
5998 code = BIT_IOR_EXPR;
5999 break;
6000 case OPERATOR_XOR:
6001 code = BIT_XOR_EXPR;
6002 break;
6003 case OPERATOR_MULT:
6004 code = MULT_EXPR;
6005 break;
6006 case OPERATOR_DIV:
6007 {
6008 Type *t = this->left_->type();
6009 if (t->float_type() != NULL || t->complex_type() != NULL)
6010 code = RDIV_EXPR;
6011 else
63d1e46d
ILT
6012 {
6013 code = TRUNC_DIV_EXPR;
6014 is_idiv_op = true;
6015 }
7a938933
ILT
6016 }
6017 break;
6018 case OPERATOR_MOD:
6019 code = TRUNC_MOD_EXPR;
63d1e46d 6020 is_idiv_op = true;
7a938933
ILT
6021 break;
6022 case OPERATOR_LSHIFT:
6023 code = LSHIFT_EXPR;
6024 is_shift_op = true;
6025 break;
6026 case OPERATOR_RSHIFT:
6027 code = RSHIFT_EXPR;
6028 is_shift_op = true;
6029 break;
6030 case OPERATOR_AND:
6031 code = BIT_AND_EXPR;
6032 break;
6033 case OPERATOR_BITCLEAR:
6034 right = fold_build1(BIT_NOT_EXPR, TREE_TYPE(right), right);
6035 code = BIT_AND_EXPR;
6036 break;
6037 default:
8c0d1865 6038 go_unreachable();
7a938933
ILT
6039 }
6040
63d1e46d 6041 location_t gccloc = this->location().gcc_location();
7a938933
ILT
6042 tree type = use_left_type ? TREE_TYPE(left) : TREE_TYPE(right);
6043
6044 if (this->left_->type()->is_string_type())
6045 {
26409c52 6046 go_assert(this->op_ == OPERATOR_PLUS);
5b735706 6047 Type* st = Type::make_string_type();
776f27a6 6048 tree string_type = type_to_tree(st->get_backend(gogo));
7a938933
ILT
6049 static tree string_plus_decl;
6050 return Gogo::call_builtin(&string_plus_decl,
6051 this->location(),
6052 "__go_string_plus",
6053 2,
6054 string_type,
6055 string_type,
6056 left,
6057 string_type,
6058 right);
6059 }
6060
215552ad
ILT
6061 // For complex division Go wants slightly different results than the
6062 // GCC library provides, so we have our own runtime routine.
6063 if (this->op_ == OPERATOR_DIV && this->left_->type()->complex_type() != NULL)
6064 {
6065 const char *name;
6066 tree *pdecl;
6067 Type* ctype;
6068 static tree complex64_div_decl;
6069 static tree complex128_div_decl;
6070 switch (this->left_->type()->complex_type()->bits())
6071 {
6072 case 64:
6073 name = "__go_complex64_div";
6074 pdecl = &complex64_div_decl;
6075 ctype = Type::lookup_complex_type("complex64");
6076 break;
6077 case 128:
6078 name = "__go_complex128_div";
6079 pdecl = &complex128_div_decl;
6080 ctype = Type::lookup_complex_type("complex128");
6081 break;
6082 default:
6083 go_unreachable();
6084 }
6085 Btype* cbtype = ctype->get_backend(gogo);
6086 tree ctype_tree = type_to_tree(cbtype);
6087 return Gogo::call_builtin(pdecl,
6088 this->location(),
6089 name,
6090 2,
6091 ctype_tree,
6092 ctype_tree,
6093 fold_convert_loc(gccloc, ctype_tree, left),
6094 type,
6095 fold_convert_loc(gccloc, ctype_tree, right));
6096 }
6097
7a938933
ILT
6098 tree compute_type = excess_precision_type(type);
6099 if (compute_type != NULL_TREE)
6100 {
6101 left = ::convert(compute_type, left);
6102 right = ::convert(compute_type, right);
6103 }
6104
6105 tree eval_saved = NULL_TREE;
63d1e46d
ILT
6106 if (is_shift_op
6107 || (is_idiv_op && (go_check_divide_zero || go_check_divide_overflow)))
7a938933 6108 {
7a938933 6109 // Make sure the values are evaluated.
63d1e46d 6110 if (!DECL_P(left))
54ba9323
ILT
6111 {
6112 left = save_expr(left);
6113 eval_saved = left;
6114 }
63d1e46d 6115 if (!DECL_P(right))
54ba9323
ILT
6116 {
6117 right = save_expr(right);
6118 if (eval_saved == NULL_TREE)
6119 eval_saved = right;
6120 else
63d1e46d 6121 eval_saved = fold_build2_loc(gccloc, COMPOUND_EXPR,
54ba9323
ILT
6122 void_type_node, eval_saved, right);
6123 }
7a938933
ILT
6124 }
6125
63d1e46d 6126 tree ret = fold_build2_loc(gccloc, code,
7a938933
ILT
6127 compute_type != NULL_TREE ? compute_type : type,
6128 left, right);
6129
6130 if (compute_type != NULL_TREE)
6131 ret = ::convert(type, ret);
6132
6133 // In Go, a shift larger than the size of the type is well-defined.
6134 // This is not true in GENERIC, so we need to insert a conditional.
6135 if (is_shift_op)
6136 {
26409c52
ILT
6137 go_assert(INTEGRAL_TYPE_P(TREE_TYPE(left)));
6138 go_assert(this->left_->type()->integer_type() != NULL);
7a938933
ILT
6139 int bits = TYPE_PRECISION(TREE_TYPE(left));
6140
6141 tree compare = fold_build2(LT_EXPR, boolean_type_node, right,
6142 build_int_cst_type(TREE_TYPE(right), bits));
6143
63d1e46d 6144 tree overflow_result = fold_convert_loc(gccloc, TREE_TYPE(left),
7a938933
ILT
6145 integer_zero_node);
6146 if (this->op_ == OPERATOR_RSHIFT
6147 && !this->left_->type()->integer_type()->is_unsigned())
6148 {
8afa2bfb 6149 tree neg =
63d1e46d
ILT
6150 fold_build2_loc(gccloc, LT_EXPR, boolean_type_node,
6151 left,
6152 fold_convert_loc(gccloc, TREE_TYPE(left),
8afa2bfb
SD
6153 integer_zero_node));
6154 tree neg_one =
63d1e46d
ILT
6155 fold_build2_loc(gccloc, MINUS_EXPR, TREE_TYPE(left),
6156 fold_convert_loc(gccloc, TREE_TYPE(left),
8afa2bfb 6157 integer_zero_node),
63d1e46d 6158 fold_convert_loc(gccloc, TREE_TYPE(left),
8afa2bfb
SD
6159 integer_one_node));
6160 overflow_result =
63d1e46d
ILT
6161 fold_build3_loc(gccloc, COND_EXPR, TREE_TYPE(left),
6162 neg, neg_one, overflow_result);
6163 }
6164
6165 ret = fold_build3_loc(gccloc, COND_EXPR, TREE_TYPE(left),
6166 compare, ret, overflow_result);
6167
6168 if (eval_saved != NULL_TREE)
6169 ret = fold_build2_loc(gccloc, COMPOUND_EXPR, TREE_TYPE(ret),
6170 eval_saved, ret);
6171 }
6172
6173 // Add checks for division by zero and division overflow as needed.
6174 if (is_idiv_op)
6175 {
6176 if (go_check_divide_zero)
6177 {
6178 // right == 0
6179 tree check = fold_build2_loc(gccloc, EQ_EXPR, boolean_type_node,
6180 right,
6181 fold_convert_loc(gccloc,
6182 TREE_TYPE(right),
6183 integer_zero_node));
6184
6185 // __go_runtime_error(RUNTIME_ERROR_DIVISION_BY_ZERO), 0
6186 int errcode = RUNTIME_ERROR_DIVISION_BY_ZERO;
6187 tree panic = fold_build2_loc(gccloc, COMPOUND_EXPR, TREE_TYPE(ret),
776f27a6 6188 gogo->runtime_error(errcode,
63d1e46d
ILT
6189 this->location()),
6190 fold_convert_loc(gccloc, TREE_TYPE(ret),
6191 integer_zero_node));
6192
6193 // right == 0 ? (__go_runtime_error(...), 0) : ret
6194 ret = fold_build3_loc(gccloc, COND_EXPR, TREE_TYPE(ret),
6195 check, panic, ret);
8afa2bfb
SD
6196 }
6197
63d1e46d
ILT
6198 if (go_check_divide_overflow)
6199 {
6200 // right == -1
6201 // FIXME: It would be nice to say that this test is expected
6202 // to return false.
6203 tree m1 = integer_minus_one_node;
6204 tree check = fold_build2_loc(gccloc, EQ_EXPR, boolean_type_node,
6205 right,
6206 fold_convert_loc(gccloc,
6207 TREE_TYPE(right),
6208 m1));
6209
6210 tree overflow;
6211 if (TYPE_UNSIGNED(TREE_TYPE(ret)))
6212 {
6213 // An unsigned -1 is the largest possible number, so
6214 // dividing is always 1 or 0.
6215 tree cmp = fold_build2_loc(gccloc, EQ_EXPR, boolean_type_node,
6216 left, right);
6217 if (this->op_ == OPERATOR_DIV)
6218 overflow = fold_build3_loc(gccloc, COND_EXPR, TREE_TYPE(ret),
6219 cmp,
6220 fold_convert_loc(gccloc,
6221 TREE_TYPE(ret),
6222 integer_one_node),
6223 fold_convert_loc(gccloc,
6224 TREE_TYPE(ret),
6225 integer_zero_node));
6226 else
6227 overflow = fold_build3_loc(gccloc, COND_EXPR, TREE_TYPE(ret),
6228 cmp,
6229 fold_convert_loc(gccloc,
6230 TREE_TYPE(ret),
6231 integer_zero_node),
6232 left);
6233 }
6234 else
6235 {
6236 // Computing left / -1 is the same as computing - left,
6237 // which does not overflow since Go sets -fwrapv.
6238 if (this->op_ == OPERATOR_DIV)
6239 overflow = fold_build1_loc(gccloc, NEGATE_EXPR, TREE_TYPE(left),
6240 left);
6241 else
6242 overflow = integer_zero_node;
6243 }
6244 overflow = fold_convert_loc(gccloc, TREE_TYPE(ret), overflow);
6245
6246 // right == -1 ? - left : ret
6247 ret = fold_build3_loc(gccloc, COND_EXPR, TREE_TYPE(ret),
6248 check, overflow, ret);
6249 }
7a938933 6250
54ba9323 6251 if (eval_saved != NULL_TREE)
63d1e46d
ILT
6252 ret = fold_build2_loc(gccloc, COMPOUND_EXPR, TREE_TYPE(ret),
6253 eval_saved, ret);
7a938933
ILT
6254 }
6255
6256 return ret;
6257}
6258
6259// Export a binary expression.
6260
6261void
6262Binary_expression::do_export(Export* exp) const
6263{
6264 exp->write_c_string("(");
6265 this->left_->export_expression(exp);
6266 switch (this->op_)
6267 {
6268 case OPERATOR_OROR:
6269 exp->write_c_string(" || ");
6270 break;
6271 case OPERATOR_ANDAND:
6272 exp->write_c_string(" && ");
6273 break;
6274 case OPERATOR_EQEQ:
6275 exp->write_c_string(" == ");
6276 break;
6277 case OPERATOR_NOTEQ:
6278 exp->write_c_string(" != ");
6279 break;
6280 case OPERATOR_LT:
6281 exp->write_c_string(" < ");
6282 break;
6283 case OPERATOR_LE:
6284 exp->write_c_string(" <= ");
6285 break;
6286 case OPERATOR_GT:
6287 exp->write_c_string(" > ");
6288 break;
6289 case OPERATOR_GE:
6290 exp->write_c_string(" >= ");
6291 break;
6292 case OPERATOR_PLUS:
6293 exp->write_c_string(" + ");
6294 break;
6295 case OPERATOR_MINUS:
6296 exp->write_c_string(" - ");
6297 break;
6298 case OPERATOR_OR:
6299 exp->write_c_string(" | ");
6300 break;
6301 case OPERATOR_XOR:
6302 exp->write_c_string(" ^ ");
6303 break;
6304 case OPERATOR_MULT:
6305 exp->write_c_string(" * ");
6306 break;
6307 case OPERATOR_DIV:
6308 exp->write_c_string(" / ");
6309 break;
6310 case OPERATOR_MOD:
6311 exp->write_c_string(" % ");
6312 break;
6313 case OPERATOR_LSHIFT:
6314 exp->write_c_string(" << ");
6315 break;
6316 case OPERATOR_RSHIFT:
6317 exp->write_c_string(" >> ");
6318 break;
6319 case OPERATOR_AND:
6320 exp->write_c_string(" & ");
6321 break;
6322 case OPERATOR_BITCLEAR:
6323 exp->write_c_string(" &^ ");
6324 break;
6325 default:
8c0d1865 6326 go_unreachable();
7a938933
ILT
6327 }
6328 this->right_->export_expression(exp);
6329 exp->write_c_string(")");
6330}
6331
6332// Import a binary expression.
6333
6334Expression*
6335Binary_expression::do_import(Import* imp)
6336{
6337 imp->require_c_string("(");
6338
6339 Expression* left = Expression::import_expression(imp);
6340
6341 Operator op;
6342 if (imp->match_c_string(" || "))
6343 {
6344 op = OPERATOR_OROR;
6345 imp->advance(4);
6346 }
6347 else if (imp->match_c_string(" && "))
6348 {
6349 op = OPERATOR_ANDAND;
6350 imp->advance(4);
6351 }
6352 else if (imp->match_c_string(" == "))
6353 {
6354 op = OPERATOR_EQEQ;
6355 imp->advance(4);
6356 }
6357 else if (imp->match_c_string(" != "))
6358 {
6359 op = OPERATOR_NOTEQ;
6360 imp->advance(4);
6361 }
6362 else if (imp->match_c_string(" < "))
6363 {
6364 op = OPERATOR_LT;
6365 imp->advance(3);
6366 }
6367 else if (imp->match_c_string(" <= "))
6368 {
6369 op = OPERATOR_LE;
6370 imp->advance(4);
6371 }
6372 else if (imp->match_c_string(" > "))
6373 {
6374 op = OPERATOR_GT;
6375 imp->advance(3);
6376 }
6377 else if (imp->match_c_string(" >= "))
6378 {
6379 op = OPERATOR_GE;
6380 imp->advance(4);
6381 }
6382 else if (imp->match_c_string(" + "))
6383 {
6384 op = OPERATOR_PLUS;
6385 imp->advance(3);
6386 }
6387 else if (imp->match_c_string(" - "))
6388 {
6389 op = OPERATOR_MINUS;
6390 imp->advance(3);
6391 }
6392 else if (imp->match_c_string(" | "))
6393 {
6394 op = OPERATOR_OR;
6395 imp->advance(3);
6396 }
6397 else if (imp->match_c_string(" ^ "))
6398 {
6399 op = OPERATOR_XOR;
6400 imp->advance(3);
6401 }
6402 else if (imp->match_c_string(" * "))
6403 {
6404 op = OPERATOR_MULT;
6405 imp->advance(3);
6406 }
6407 else if (imp->match_c_string(" / "))
6408 {
6409 op = OPERATOR_DIV;
6410 imp->advance(3);
6411 }
6412 else if (imp->match_c_string(" % "))
6413 {
6414 op = OPERATOR_MOD;
6415 imp->advance(3);
6416 }
6417 else if (imp->match_c_string(" << "))
6418 {
6419 op = OPERATOR_LSHIFT;
6420 imp->advance(4);
6421 }
6422 else if (imp->match_c_string(" >> "))
6423 {
6424 op = OPERATOR_RSHIFT;
6425 imp->advance(4);
6426 }
6427 else if (imp->match_c_string(" & "))
6428 {
6429 op = OPERATOR_AND;
6430 imp->advance(3);
6431 }
6432 else if (imp->match_c_string(" &^ "))
6433 {
6434 op = OPERATOR_BITCLEAR;
6435 imp->advance(4);
6436 }
6437 else
6438 {
6439 error_at(imp->location(), "unrecognized binary operator");
6440 return Expression::make_error(imp->location());
6441 }
6442
6443 Expression* right = Expression::import_expression(imp);
6444
6445 imp->require_c_string(")");
6446
6447 return Expression::make_binary(op, left, right, imp->location());
6448}
6449
16c57fe2
RL
6450// Dump ast representation of a binary expression.
6451
6452void
6453Binary_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
6454{
6455 ast_dump_context->ostream() << "(";
6456 ast_dump_context->dump_expression(this->left_);
6457 ast_dump_context->ostream() << " ";
6458 ast_dump_context->dump_operator(this->op_);
6459 ast_dump_context->ostream() << " ";
6460 ast_dump_context->dump_expression(this->right_);
6461 ast_dump_context->ostream() << ") ";
6462}
6463
7a938933
ILT
6464// Make a binary expression.
6465
6466Expression*
6467Expression::make_binary(Operator op, Expression* left, Expression* right,
8afa2bfb 6468 Location location)
7a938933
ILT
6469{
6470 return new Binary_expression(op, left, right, location);
6471}
6472
6473// Implement a comparison.
6474
6475tree
610d0e16 6476Expression::comparison_tree(Translate_context* context, Type* result_type,
58c55a32
ILT
6477 Operator op, Expression* left_expr,
6478 Expression* right_expr, Location location)
7a938933 6479{
58c55a32
ILT
6480 Type* left_type = left_expr->type();
6481 Type* right_type = right_expr->type();
6482
6483 mpz_t zval;
6484 mpz_init_set_ui(zval, 0UL);
6485 Expression* zexpr = Expression::make_integer(&zval, NULL, location);
6486 mpz_clear(zval);
776f27a6 6487
7a938933
ILT
6488 enum tree_code code;
6489 switch (op)
6490 {
6491 case OPERATOR_EQEQ:
6492 code = EQ_EXPR;
6493 break;
6494 case OPERATOR_NOTEQ:
6495 code = NE_EXPR;
6496 break;
6497 case OPERATOR_LT:
6498 code = LT_EXPR;
6499 break;
6500 case OPERATOR_LE:
6501 code = LE_EXPR;
6502 break;
6503 case OPERATOR_GT:
6504 code = GT_EXPR;
6505 break;
6506 case OPERATOR_GE:
6507 code = GE_EXPR;
6508 break;
6509 default:
8c0d1865 6510 go_unreachable();
7a938933
ILT
6511 }
6512
58c55a32
ILT
6513 // FIXME: Computing the tree here means it will be computed multiple times,
6514 // which is wasteful. This is a temporary modification until all tree code
6515 // here can be replaced with frontend expressions.
6516 tree left_tree = left_expr->get_tree(context);
6517 tree right_tree = right_expr->get_tree(context);
10a0275d 6518 if (left_type->is_string_type() && right_type->is_string_type())
7a938933 6519 {
58c55a32
ILT
6520 Expression* strcmp_call = Runtime::make_call(Runtime::STRCMP, location, 2,
6521 left_expr, right_expr);
6522 left_tree = strcmp_call->get_tree(context);
6523 right_tree = zexpr->get_tree(context);
7a938933 6524 }
10a0275d
ILT
6525 else if ((left_type->interface_type() != NULL
6526 && right_type->interface_type() == NULL
6527 && !right_type->is_nil_type())
6528 || (left_type->interface_type() == NULL
6529 && !left_type->is_nil_type()
6530 && right_type->interface_type() != NULL))
7a938933
ILT
6531 {
6532 // Comparing an interface value to a non-interface value.
6533 if (left_type->interface_type() == NULL)
6534 {
6535 std::swap(left_type, right_type);
58c55a32 6536 std::swap(left_expr, right_expr);
7a938933
ILT
6537 }
6538
6539 // The right operand is not an interface. We need to take its
6540 // address if it is not a pointer.
58c55a32 6541 Expression* pointer_arg = NULL;
7a938933 6542 if (right_type->points_to() != NULL)
58c55a32 6543 pointer_arg = right_expr;
7a938933
ILT
6544 else
6545 {
58c55a32
ILT
6546 go_assert(right_expr->is_addressable());
6547 pointer_arg = Expression::make_unary(OPERATOR_AND, right_expr,
6548 location);
7a938933 6549 }
7a938933 6550
58c55a32
ILT
6551 Expression* descriptor_expr = Expression::make_type_descriptor(right_type,
6552 location);
6553 Call_expression* iface_valcmp =
6554 Runtime::make_call((left_type->interface_type()->is_empty()
6555 ? Runtime::EMPTY_INTERFACE_VALUE_COMPARE
6556 : Runtime::INTERFACE_VALUE_COMPARE),
6557 location, 3, left_expr, descriptor_expr,
6558 pointer_arg);
6559 left_tree = iface_valcmp->get_tree(context);
6560 right_tree = zexpr->get_tree(context);
7a938933
ILT
6561 }
6562 else if (left_type->interface_type() != NULL
6563 && right_type->interface_type() != NULL)
6564 {
58c55a32 6565 Runtime::Function compare_function;
7b67393d
ILT
6566 if (left_type->interface_type()->is_empty()
6567 && right_type->interface_type()->is_empty())
58c55a32 6568 compare_function = Runtime::EMPTY_INTERFACE_COMPARE;
7b67393d
ILT
6569 else if (!left_type->interface_type()->is_empty()
6570 && !right_type->interface_type()->is_empty())
58c55a32 6571 compare_function = Runtime::INTERFACE_COMPARE;
7b67393d
ILT
6572 else
6573 {
6574 if (left_type->interface_type()->is_empty())
6575 {
26409c52 6576 go_assert(op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ);
7b67393d 6577 std::swap(left_type, right_type);
58c55a32 6578 std::swap(left_expr, right_expr);
7b67393d 6579 }
26409c52
ILT
6580 go_assert(!left_type->interface_type()->is_empty());
6581 go_assert(right_type->interface_type()->is_empty());
58c55a32 6582 compare_function = Runtime::INTERFACE_EMPTY_COMPARE;
7b67393d
ILT
6583 }
6584
58c55a32
ILT
6585 Call_expression* ifacecmp_call =
6586 Runtime::make_call(compare_function, location, 2,
6587 left_expr, right_expr);
6588
6589 left_tree = ifacecmp_call->get_tree(context);
6590 right_tree = zexpr->get_tree(context);
7a938933
ILT
6591 }
6592
6593 if (left_type->is_nil_type()
6594 && (op == OPERATOR_EQEQ || op == OPERATOR_NOTEQ))
6595 {
6596 std::swap(left_type, right_type);
6597 std::swap(left_tree, right_tree);
6598 }
6599
6600 if (right_type->is_nil_type())
6601 {
6602 if (left_type->array_type() != NULL
6603 && left_type->array_type()->length() == NULL)
6604 {
6605 Array_type* at = left_type->array_type();
6606 left_tree = at->value_pointer_tree(context->gogo(), left_tree);
6607 right_tree = fold_convert(TREE_TYPE(left_tree), null_pointer_node);
6608 }
6609 else if (left_type->interface_type() != NULL)
6610 {
6611 // An interface is nil if the first field is nil.
6612 tree left_type_tree = TREE_TYPE(left_tree);
26409c52 6613 go_assert(TREE_CODE(left_type_tree) == RECORD_TYPE);
7a938933
ILT
6614 tree field = TYPE_FIELDS(left_type_tree);
6615 left_tree = build3(COMPONENT_REF, TREE_TYPE(field), left_tree,
6616 field, NULL_TREE);
6617 right_tree = fold_convert(TREE_TYPE(left_tree), null_pointer_node);
6618 }
6619 else
6620 {
26409c52 6621 go_assert(POINTER_TYPE_P(TREE_TYPE(left_tree)));
7a938933
ILT
6622 right_tree = fold_convert(TREE_TYPE(left_tree), null_pointer_node);
6623 }
6624 }
6625
89ca9e60
ILT
6626 if (left_tree == error_mark_node || right_tree == error_mark_node)
6627 return error_mark_node;
6628
610d0e16
ILT
6629 tree result_type_tree;
6630 if (result_type == NULL)
6631 result_type_tree = boolean_type_node;
6632 else
6633 result_type_tree = type_to_tree(result_type->get_backend(context->gogo()));
6634
6635 tree ret = fold_build2(code, result_type_tree, left_tree, right_tree);
7a938933 6636 if (CAN_HAVE_LOCATION_P(ret))
8afa2bfb 6637 SET_EXPR_LOCATION(ret, location.gcc_location());
7a938933
ILT
6638 return ret;
6639}
6640
6641// Class Bound_method_expression.
6642
6643// Traversal.
6644
6645int
6646Bound_method_expression::do_traverse(Traverse* traverse)
6647{
48ab8ba6 6648 return Expression::traverse(&this->expr_, traverse);
7a938933
ILT
6649}
6650
571d3f91
ILT
6651// Lower the expression. If this is a method value rather than being
6652// called, and the method is accessed via a pointer, we may need to
6653// add nil checks. Introduce a temporary variable so that those nil
6654// checks do not cause multiple evaluation.
6655
6656Expression*
6657Bound_method_expression::do_lower(Gogo*, Named_object*,
6658 Statement_inserter* inserter, int)
6659{
6660 // For simplicity we use a temporary for every call to an embedded
6661 // method, even though some of them might be pure value methods and
6662 // not require a temporary.
6663 if (this->expr_->var_expression() == NULL
6664 && this->expr_->temporary_reference_expression() == NULL
6665 && this->expr_->set_and_use_temporary_expression() == NULL
6666 && (this->method_->field_indexes() != NULL
6667 || (this->method_->is_value_method()
6668 && this->expr_->type()->points_to() != NULL)))
6669 {
6670 Temporary_statement* temp =
6671 Statement::make_temporary(this->expr_->type(), NULL, this->location());
6672 inserter->insert(temp);
6673 this->expr_ = Expression::make_set_and_use_temporary(temp, this->expr_,
6674 this->location());
6675 }
6676 return this;
6677}
6678
7a938933 6679// Return the type of a bound method expression. The type of this
571d3f91 6680// object is simply the type of the method with no receiver.
7a938933
ILT
6681
6682Type*
6683Bound_method_expression::do_type()
6684{
571d3f91
ILT
6685 Named_object* fn = this->method_->named_object();
6686 Function_type* fntype;
6687 if (fn->is_function())
6688 fntype = fn->func_value()->type();
6689 else if (fn->is_function_declaration())
6690 fntype = fn->func_declaration_value()->type();
48ab8ba6
ILT
6691 else
6692 return Type::make_error_type();
571d3f91 6693 return fntype->copy_without_receiver();
7a938933
ILT
6694}
6695
6696// Determine the types of a method expression.
6697
6698void
6699Bound_method_expression::do_determine_type(const Type_context*)
6700{
571d3f91
ILT
6701 Named_object* fn = this->method_->named_object();
6702 Function_type* fntype;
6703 if (fn->is_function())
6704 fntype = fn->func_value()->type();
6705 else if (fn->is_function_declaration())
6706 fntype = fn->func_declaration_value()->type();
6707 else
6708 fntype = NULL;
7a938933
ILT
6709 if (fntype == NULL || !fntype->is_method())
6710 this->expr_->determine_type_no_context();
6711 else
6712 {
6713 Type_context subcontext(fntype->receiver()->type(), false);
6714 this->expr_->determine_type(&subcontext);
6715 }
6716}
6717
6718// Check the types of a method expression.
6719
6720void
6721Bound_method_expression::do_check_types(Gogo*)
6722{
571d3f91
ILT
6723 Named_object* fn = this->method_->named_object();
6724 if (!fn->is_function() && !fn->is_function_declaration())
6725 {
6726 this->report_error(_("object is not a method"));
6727 return;
6728 }
6729
6730 Function_type* fntype;
6731 if (fn->is_function())
6732 fntype = fn->func_value()->type();
6733 else if (fn->is_function_declaration())
6734 fntype = fn->func_declaration_value()->type();
7a938933 6735 else
571d3f91
ILT
6736 go_unreachable();
6737 Type* rtype = fntype->receiver()->type()->deref();
6738 Type* etype = (this->expr_type_ != NULL
6739 ? this->expr_type_
6740 : this->expr_->type());
6741 etype = etype->deref();
6742 if (!Type::are_identical(rtype, etype, true, NULL))
6743 this->report_error(_("method type does not match object type"));
6744}
6745
6746// If a bound method expression is not simply called, then it is
6747// represented as a closure. The closure will hold a single variable,
6748// the receiver to pass to the method. The function will be a simple
6749// thunk that pulls that value from the closure and calls the method
6750// with the remaining arguments.
6751//
6752// Because method values are not common, we don't build all thunks for
6753// every methods, but instead only build them as we need them. In
6754// particular, we even build them on demand for methods defined in
6755// other packages.
6756
6757Bound_method_expression::Method_value_thunks
6758 Bound_method_expression::method_value_thunks;
6759
6760// Find or create the thunk for METHOD.
6761
6762Named_object*
6763Bound_method_expression::create_thunk(Gogo* gogo, const Method* method,
6764 Named_object* fn)
6765{
6766 std::pair<Named_object*, Named_object*> val(fn, NULL);
6767 std::pair<Method_value_thunks::iterator, bool> ins =
6768 Bound_method_expression::method_value_thunks.insert(val);
6769 if (!ins.second)
6770 {
6771 // We have seen this method before.
6772 go_assert(ins.first->second != NULL);
6773 return ins.first->second;
6774 }
6775
6776 Location loc = fn->location();
6777
6778 Function_type* orig_fntype;
6779 if (fn->is_function())
6780 orig_fntype = fn->func_value()->type();
6781 else if (fn->is_function_declaration())
6782 orig_fntype = fn->func_declaration_value()->type();
6783 else
6784 orig_fntype = NULL;
6785
6786 if (orig_fntype == NULL || !orig_fntype->is_method())
7a938933 6787 {
571d3f91
ILT
6788 ins.first->second = Named_object::make_erroneous_name(Gogo::thunk_name());
6789 return ins.first->second;
7a938933 6790 }
571d3f91
ILT
6791
6792 Struct_field_list* sfl = new Struct_field_list();
05a7d566
ILT
6793 // The type here is wrong--it should be the C function type. But it
6794 // doesn't really matter.
571d3f91
ILT
6795 Type* vt = Type::make_pointer_type(Type::make_void_type());
6796 sfl->push_back(Struct_field(Typed_identifier("fn.0", vt, loc)));
6797 sfl->push_back(Struct_field(Typed_identifier("val.1",
6798 orig_fntype->receiver()->type(),
6799 loc)));
6800 Type* closure_type = Type::make_struct_type(sfl, loc);
6801 closure_type = Type::make_pointer_type(closure_type);
6802
05a7d566 6803 Function_type* new_fntype = orig_fntype->copy_with_names();
571d3f91
ILT
6804
6805 Named_object* new_no = gogo->start_function(Gogo::thunk_name(), new_fntype,
6806 false, loc);
6807
05a7d566
ILT
6808 Variable* cvar = new Variable(closure_type, NULL, false, false, false, loc);
6809 cvar->set_is_used();
6810 Named_object* cp = Named_object::make_variable("$closure", NULL, cvar);
6811 new_no->func_value()->set_closure_var(cp);
571d3f91 6812
05a7d566 6813 gogo->start_block(loc);
571d3f91
ILT
6814
6815 // Field 0 of the closure is the function code pointer, field 1 is
6816 // the value on which to invoke the method.
6817 Expression* arg = Expression::make_var_reference(cp, loc);
6818 arg = Expression::make_unary(OPERATOR_MULT, arg, loc);
6819 arg = Expression::make_field_reference(arg, 1, loc);
6820
6821 Expression* bme = Expression::make_bound_method(arg, method, fn, loc);
6822
6823 const Typed_identifier_list* orig_params = orig_fntype->parameters();
6824 Expression_list* args;
6825 if (orig_params == NULL || orig_params->empty())
6826 args = NULL;
6827 else
6828 {
6829 const Typed_identifier_list* new_params = new_fntype->parameters();
6830 args = new Expression_list();
6831 for (Typed_identifier_list::const_iterator p = new_params->begin();
05a7d566 6832 p != new_params->end();
571d3f91
ILT
6833 ++p)
6834 {
6835 Named_object* p_no = gogo->lookup(p->name(), NULL);
6836 go_assert(p_no != NULL
6837 && p_no->is_variable()
6838 && p_no->var_value()->is_parameter());
6839 args->push_back(Expression::make_var_reference(p_no, loc));
6840 }
6841 }
6842
6843 Call_expression* call = Expression::make_call(bme, args,
6844 orig_fntype->is_varargs(),
6845 loc);
6846 call->set_varargs_are_lowered();
6847
6848 Statement* s = Statement::make_return_from_call(call, loc);
6849 gogo->add_statement(s);
6850 Block* b = gogo->finish_block(loc);
6851 gogo->add_block(b, loc);
6852 gogo->lower_block(new_no, b);
6853 gogo->finish_function(loc);
6854
6855 ins.first->second = new_no;
6856 return new_no;
6857}
6858
6859// Return an expression to check *REF for nil while dereferencing
6860// according to FIELD_INDEXES. Update *REF to build up the field
6861// reference. This is a static function so that we don't have to
6862// worry about declaring Field_indexes in expressions.h.
6863
6864static Expression*
6865bme_check_nil(const Method::Field_indexes* field_indexes, Location loc,
6866 Expression** ref)
6867{
6868 if (field_indexes == NULL)
6869 return Expression::make_boolean(false, loc);
6870 Expression* cond = bme_check_nil(field_indexes->next, loc, ref);
6871 Struct_type* stype = (*ref)->type()->deref()->struct_type();
6872 go_assert(stype != NULL
6873 && field_indexes->field_index < stype->field_count());
6874 if ((*ref)->type()->struct_type() == NULL)
6875 {
6876 go_assert((*ref)->type()->points_to() != NULL);
6877 Expression* n = Expression::make_binary(OPERATOR_EQEQ, *ref,
6878 Expression::make_nil(loc),
6879 loc);
6880 cond = Expression::make_binary(OPERATOR_OROR, cond, n, loc);
6881 *ref = Expression::make_unary(OPERATOR_MULT, *ref, loc);
6882 go_assert((*ref)->type()->struct_type() == stype);
6883 }
6884 *ref = Expression::make_field_reference(*ref, field_indexes->field_index,
6885 loc);
6886 return cond;
7a938933
ILT
6887}
6888
571d3f91 6889// Get the tree for a method value.
7a938933
ILT
6890
6891tree
571d3f91 6892Bound_method_expression::do_get_tree(Translate_context* context)
7a938933 6893{
571d3f91
ILT
6894 Named_object* thunk = Bound_method_expression::create_thunk(context->gogo(),
6895 this->method_,
6896 this->function_);
6897 if (thunk->is_erroneous())
6898 {
6899 go_assert(saw_errors());
6900 return error_mark_node;
6901 }
6902
6903 // FIXME: We should lower this earlier, but we can't lower it in the
6904 // lowering pass because at that point we don't know whether we need
6905 // to create the thunk or not. If the expression is called, we
6906 // don't need the thunk.
6907
6908 Location loc = this->location();
6909
6910 // If the method expects a value, and we have a pointer, we need to
6911 // dereference the pointer.
6912
6913 Named_object* fn = this->method_->named_object();
6914 Function_type* fntype;
6915 if (fn->is_function())
6916 fntype = fn->func_value()->type();
6917 else if (fn->is_function_declaration())
6918 fntype = fn->func_declaration_value()->type();
6919 else
6920 go_unreachable();
6921
6922 Expression* val = this->expr_;
6923 if (fntype->receiver()->type()->points_to() == NULL
6924 && val->type()->points_to() != NULL)
6925 val = Expression::make_unary(OPERATOR_MULT, val, loc);
6926
6927 // Note that we are ignoring this->expr_type_ here. The thunk will
6928 // expect a closure whose second field has type this->expr_type_ (if
6929 // that is not NULL). We are going to pass it a closure whose
6930 // second field has type this->expr_->type(). Since
6931 // this->expr_type_ is only not-NULL for pointer types, we can get
6932 // away with this.
6933
6934 Struct_field_list* fields = new Struct_field_list();
6935 fields->push_back(Struct_field(Typed_identifier("fn.0",
6936 thunk->func_value()->type(),
6937 loc)));
6938 fields->push_back(Struct_field(Typed_identifier("val.1", val->type(), loc)));
6939 Struct_type* st = Type::make_struct_type(fields, loc);
6940
6941 Expression_list* vals = new Expression_list();
6942 vals->push_back(Expression::make_func_code_reference(thunk, loc));
6943 vals->push_back(val);
6944
6945 Expression* ret = Expression::make_struct_composite_literal(st, vals, loc);
6946 ret = Expression::make_heap_composite(ret, loc);
6947
6948 tree ret_tree = ret->get_tree(context);
6949
6950 Expression* nil_check = NULL;
6951
6952 // See whether the expression or any embedded pointers are nil.
6953
6954 Expression* expr = this->expr_;
6955 if (this->method_->field_indexes() != NULL)
6956 {
6957 // Note that we are evaluating this->expr_ twice, but that is OK
6958 // because in the lowering pass we forced it into a temporary
6959 // variable.
6960 Expression* ref = expr;
6961 nil_check = bme_check_nil(this->method_->field_indexes(), loc, &ref);
6962 expr = ref;
6963 }
6964
6965 if (this->method_->is_value_method() && expr->type()->points_to() != NULL)
6966 {
6967 Expression* n = Expression::make_binary(OPERATOR_EQEQ, expr,
6968 Expression::make_nil(loc),
6969 loc);
6970 if (nil_check == NULL)
6971 nil_check = n;
6972 else
6973 nil_check = Expression::make_binary(OPERATOR_OROR, nil_check, n, loc);
6974 }
6975
6976 if (nil_check != NULL)
6977 {
6978 tree nil_check_tree = nil_check->get_tree(context);
6979 tree crash =
6980 context->gogo()->runtime_error(RUNTIME_ERROR_NIL_DEREFERENCE, loc);
6981 if (ret_tree == error_mark_node
6982 || nil_check_tree == error_mark_node
6983 || crash == error_mark_node)
6984 return error_mark_node;
6985
6986 ret_tree = fold_build2_loc(loc.gcc_location(), COMPOUND_EXPR,
6987 TREE_TYPE(ret_tree),
6988 build3_loc(loc.gcc_location(), COND_EXPR,
6989 void_type_node, nil_check_tree,
6990 crash, NULL_TREE),
6991 ret_tree);
6992 }
6993
6994 return ret_tree;
7a938933
ILT
6995}
6996
16c57fe2
RL
6997// Dump ast representation of a bound method expression.
6998
6999void
7000Bound_method_expression::do_dump_expression(Ast_dump_context* ast_dump_context)
7001 const
7002{
7003 if (this->expr_type_ != NULL)
7004 ast_dump_context->ostream() << "(";
7005 ast_dump_context->dump_expression(this->expr_);
7006 if (this->expr_type_ != NULL)
7007 {
7008 ast_dump_context->ostream() << ":";
7009 ast_dump_context->dump_type(this->expr_type_);
7010 ast_dump_context->ostream() << ")";
7011 }
7012
571d3f91 7013 ast_dump_context->ostream() << "." << this->function_->name();
16c57fe2
RL
7014}
7015
7a938933
ILT
7016// Make a method expression.
7017
7018Bound_method_expression*
571d3f91
ILT
7019Expression::make_bound_method(Expression* expr, const Method* method,
7020 Named_object* function, Location location)
7a938933 7021{
571d3f91 7022 return new Bound_method_expression(expr, method, function, location);
7a938933
ILT
7023}
7024
7025// Class Builtin_call_expression. This is used for a call to a
7026// builtin function.
7027
7028class Builtin_call_expression : public Call_expression
7029{
7030 public:
7031 Builtin_call_expression(Gogo* gogo, Expression* fn, Expression_list* args,
8afa2bfb 7032 bool is_varargs, Location location);
7a938933
ILT
7033
7034 protected:
7035 // This overrides Call_expression::do_lower.
7036 Expression*
8586635c 7037 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
7a938933
ILT
7038
7039 bool
7040 do_is_constant() const;
7041
7042 bool
5caf63ca 7043 do_numeric_constant_value(Numeric_constant*) const;
7a938933 7044
3f7af571 7045 bool
3c365907
ILT
7046 do_discarding_value();
7047
7a938933
ILT
7048 Type*
7049 do_type();
7050
7051 void
7052 do_determine_type(const Type_context*);
7053
7054 void
7055 do_check_types(Gogo*);
7056
7057 Expression*
7058 do_copy()
7059 {
7060 return new Builtin_call_expression(this->gogo_, this->fn()->copy(),
7061 this->args()->copy(),
7062 this->is_varargs(),
7063 this->location());
7064 }
7065
7066 tree
7067 do_get_tree(Translate_context*);
7068
7069 void
7070 do_export(Export*) const;
7071
7072 virtual bool
7073 do_is_recover_call() const;
7074
7075 virtual void
7076 do_set_recover_arg(Expression*);
7077
7078 private:
7079 // The builtin functions.
7080 enum Builtin_function_code
7081 {
7082 BUILTIN_INVALID,
7083
7084 // Predeclared builtin functions.
7085 BUILTIN_APPEND,
7086 BUILTIN_CAP,
7087 BUILTIN_CLOSE,
ff5f50c5 7088 BUILTIN_COMPLEX,
7a938933 7089 BUILTIN_COPY,
f29ce5f5 7090 BUILTIN_DELETE,
7a938933
ILT
7091 BUILTIN_IMAG,
7092 BUILTIN_LEN,
7093 BUILTIN_MAKE,
7094 BUILTIN_NEW,
7095 BUILTIN_PANIC,
7096 BUILTIN_PRINT,
7097 BUILTIN_PRINTLN,
7098 BUILTIN_REAL,
7099 BUILTIN_RECOVER,
7100
7101 // Builtin functions from the unsafe package.
7102 BUILTIN_ALIGNOF,
7103 BUILTIN_OFFSETOF,
7104 BUILTIN_SIZEOF
7105 };
7106
7107 Expression*
7108 one_arg() const;
7109
7110 bool
7111 check_one_arg();
7112
7113 static Type*
7114 real_imag_type(Type*);
7115
7116 static Type*
ff5f50c5 7117 complex_type(Type*);
7a938933 7118
3b8dffe7
ILT
7119 Expression*
7120 lower_make();
7121
7122 bool
54d04de7 7123 check_int_value(Expression*, bool is_length);
3b8dffe7 7124
7a938933
ILT
7125 // A pointer back to the general IR structure. This avoids a global
7126 // variable, or passing it around everywhere.
7127 Gogo* gogo_;
7128 // The builtin function being called.
7129 Builtin_function_code code_;
ab9d6dcf
ILT
7130 // Used to stop endless loops when the length of an array uses len
7131 // or cap of the array itself.
7132 mutable bool seen_;
7a938933
ILT
7133};
7134
7135Builtin_call_expression::Builtin_call_expression(Gogo* gogo,
7136 Expression* fn,
7137 Expression_list* args,
7138 bool is_varargs,
8afa2bfb 7139 Location location)
7a938933 7140 : Call_expression(fn, args, is_varargs, location),
ab9d6dcf 7141 gogo_(gogo), code_(BUILTIN_INVALID), seen_(false)
7a938933
ILT
7142{
7143 Func_expression* fnexp = this->fn()->func_expression();
26409c52 7144 go_assert(fnexp != NULL);
7a938933
ILT
7145 const std::string& name(fnexp->named_object()->name());
7146 if (name == "append")
7147 this->code_ = BUILTIN_APPEND;
7148 else if (name == "cap")
7149 this->code_ = BUILTIN_CAP;
7150 else if (name == "close")
7151 this->code_ = BUILTIN_CLOSE;
ff5f50c5
ILT
7152 else if (name == "complex")
7153 this->code_ = BUILTIN_COMPLEX;
7a938933
ILT
7154 else if (name == "copy")
7155 this->code_ = BUILTIN_COPY;
f29ce5f5
ILT
7156 else if (name == "delete")
7157 this->code_ = BUILTIN_DELETE;
7a938933
ILT
7158 else if (name == "imag")
7159 this->code_ = BUILTIN_IMAG;
7160 else if (name == "len")
7161 this->code_ = BUILTIN_LEN;
7162 else if (name == "make")
7163 this->code_ = BUILTIN_MAKE;
7164 else if (name == "new")
7165 this->code_ = BUILTIN_NEW;
7166 else if (name == "panic")
7167 this->code_ = BUILTIN_PANIC;
7168 else if (name == "print")
7169 this->code_ = BUILTIN_PRINT;
7170 else if (name == "println")
7171 this->code_ = BUILTIN_PRINTLN;
7172 else if (name == "real")
7173 this->code_ = BUILTIN_REAL;
7174 else if (name == "recover")
7175 this->code_ = BUILTIN_RECOVER;
7176 else if (name == "Alignof")
7177 this->code_ = BUILTIN_ALIGNOF;
7178 else if (name == "Offsetof")
7179 this->code_ = BUILTIN_OFFSETOF;
7180 else if (name == "Sizeof")
7181 this->code_ = BUILTIN_SIZEOF;
7182 else
8c0d1865 7183 go_unreachable();
7a938933
ILT
7184}
7185
7186// Return whether this is a call to recover. This is a virtual
7187// function called from the parent class.
7188
7189bool
7190Builtin_call_expression::do_is_recover_call() const
7191{
7192 if (this->classification() == EXPRESSION_ERROR)
7193 return false;
7194 return this->code_ == BUILTIN_RECOVER;
7195}
7196
7197// Set the argument for a call to recover.
7198
7199void
7200Builtin_call_expression::do_set_recover_arg(Expression* arg)
7201{
7202 const Expression_list* args = this->args();
26409c52 7203 go_assert(args == NULL || args->empty());
7a938933
ILT
7204 Expression_list* new_args = new Expression_list();
7205 new_args->push_back(arg);
7206 this->set_args(new_args);
7207}
7208
7a938933
ILT
7209// Lower a builtin call expression. This turns new and make into
7210// specific expressions. We also convert to a constant if we can.
7211
7212Expression*
8586635c
ILT
7213Builtin_call_expression::do_lower(Gogo* gogo, Named_object* function,
7214 Statement_inserter* inserter, int)
7a938933 7215{
3b8dffe7
ILT
7216 if (this->classification() == EXPRESSION_ERROR)
7217 return this;
7218
8afa2bfb 7219 Location loc = this->location();
f29ce5f5 7220
530f4f43
ILT
7221 if (this->is_varargs() && this->code_ != BUILTIN_APPEND)
7222 {
7223 this->report_error(_("invalid use of %<...%> with builtin function"));
f29ce5f5 7224 return Expression::make_error(loc);
530f4f43
ILT
7225 }
7226
8259d364
ILT
7227 if (this->code_ == BUILTIN_OFFSETOF)
7228 {
7229 Expression* arg = this->one_arg();
d6218114
ILT
7230
7231 if (arg->bound_method_expression() != NULL
7232 || arg->interface_field_reference_expression() != NULL)
7233 {
7234 this->report_error(_("invalid use of method value as argument "
7235 "of Offsetof"));
7236 return this;
7237 }
7238
8259d364
ILT
7239 Field_reference_expression* farg = arg->field_reference_expression();
7240 while (farg != NULL)
7241 {
7242 if (!farg->implicit())
7243 break;
7244 // When the selector refers to an embedded field,
7245 // it must not be reached through pointer indirections.
7246 if (farg->expr()->deref() != farg->expr())
7247 {
d6218114
ILT
7248 this->report_error(_("argument of Offsetof implies "
7249 "indirection of an embedded field"));
8259d364
ILT
7250 return this;
7251 }
7252 // Go up until we reach the original base.
7253 farg = farg->expr()->field_reference_expression();
7254 }
7255 }
7256
f29ce5f5 7257 if (this->is_constant())
7a938933 7258 {
5caf63ca
ILT
7259 Numeric_constant nc;
7260 if (this->numeric_constant_value(&nc))
7261 return nc.expression(loc);
7a938933 7262 }
f29ce5f5
ILT
7263
7264 switch (this->code_)
7a938933 7265 {
f29ce5f5
ILT
7266 default:
7267 break;
7268
7269 case BUILTIN_NEW:
7270 {
7271 const Expression_list* args = this->args();
7272 if (args == NULL || args->size() < 1)
7273 this->report_error(_("not enough arguments"));
7274 else if (args->size() > 1)
7275 this->report_error(_("too many arguments"));
7276 else
7277 {
7278 Expression* arg = args->front();
7279 if (!arg->is_type_expression())
7280 {
7281 error_at(arg->location(), "expected type");
7282 this->set_is_error();
7283 }
7284 else
7285 return Expression::make_allocation(arg->type(), loc);
7286 }
7287 }
7288 break;
7289
7290 case BUILTIN_MAKE:
7291 return this->lower_make();
7292
7293 case BUILTIN_RECOVER:
7a938933
ILT
7294 if (function != NULL)
7295 function->func_value()->set_calls_recover();
7296 else
7297 {
7298 // Calling recover outside of a function always returns the
7299 // nil empty interface.
7c0434e5 7300 Type* eface = Type::make_empty_interface_type(loc);
f29ce5f5 7301 return Expression::make_cast(eface, Expression::make_nil(loc), loc);
7a938933 7302 }
f29ce5f5
ILT
7303 break;
7304
7305 case BUILTIN_APPEND:
7306 {
7307 // Lower the varargs.
7308 const Expression_list* args = this->args();
7309 if (args == NULL || args->empty())
7a938933 7310 return this;
f29ce5f5
ILT
7311 Type* slice_type = args->front()->type();
7312 if (!slice_type->is_slice_type())
7313 {
7314 error_at(args->front()->location(), "argument 1 must be a slice");
7315 this->set_is_error();
7316 return this;
7317 }
80678229
ILT
7318 Type* element_type = slice_type->array_type()->element_type();
7319 this->lower_varargs(gogo, function, inserter,
7320 Type::make_array_type(element_type, NULL),
7321 2);
f29ce5f5
ILT
7322 }
7323 break;
7324
7325 case BUILTIN_DELETE:
7326 {
7327 // Lower to a runtime function call.
7328 const Expression_list* args = this->args();
7329 if (args == NULL || args->size() < 2)
7330 this->report_error(_("not enough arguments"));
7331 else if (args->size() > 2)
7332 this->report_error(_("too many arguments"));
7333 else if (args->front()->type()->map_type() == NULL)
7334 this->report_error(_("argument 1 must be a map"));
7335 else
7336 {
7337 // Since this function returns no value it must appear in
7338 // a statement by itself, so we don't have to worry about
7339 // order of evaluation of values around it. Evaluate the
7340 // map first to get order of evaluation right.
7341 Map_type* mt = args->front()->type()->map_type();
7342 Temporary_statement* map_temp =
7343 Statement::make_temporary(mt, args->front(), loc);
7344 inserter->insert(map_temp);
7345
7346 Temporary_statement* key_temp =
7347 Statement::make_temporary(mt->key_type(), args->back(), loc);
7348 inserter->insert(key_temp);
7349
7350 Expression* e1 = Expression::make_temporary_reference(map_temp,
7351 loc);
7352 Expression* e2 = Expression::make_temporary_reference(key_temp,
7353 loc);
7354 e2 = Expression::make_unary(OPERATOR_AND, e2, loc);
7355 return Runtime::make_call(Runtime::MAPDELETE, this->location(),
7356 2, e1, e2);
7357 }
7358 }
7359 break;
7a938933
ILT
7360 }
7361
7362 return this;
7363}
7364
3b8dffe7
ILT
7365// Lower a make expression.
7366
7367Expression*
7368Builtin_call_expression::lower_make()
7369{
8afa2bfb 7370 Location loc = this->location();
3b8dffe7
ILT
7371
7372 const Expression_list* args = this->args();
7373 if (args == NULL || args->size() < 1)
7374 {
7375 this->report_error(_("not enough arguments"));
7376 return Expression::make_error(this->location());
7377 }
7378
7379 Expression_list::const_iterator parg = args->begin();
7380
7381 Expression* first_arg = *parg;
7382 if (!first_arg->is_type_expression())
7383 {
7384 error_at(first_arg->location(), "expected type");
7385 this->set_is_error();
7386 return Expression::make_error(this->location());
7387 }
7388 Type* type = first_arg->type();
7389
7390 bool is_slice = false;
7391 bool is_map = false;
7392 bool is_chan = false;
b7190f2f 7393 if (type->is_slice_type())
3b8dffe7
ILT
7394 is_slice = true;
7395 else if (type->map_type() != NULL)
7396 is_map = true;
7397 else if (type->channel_type() != NULL)
7398 is_chan = true;
7399 else
7400 {
7401 this->report_error(_("invalid type for make function"));
7402 return Expression::make_error(this->location());
7403 }
7404
047cff81
ILT
7405 bool have_big_args = false;
7406 Type* uintptr_type = Type::lookup_integer_type("uintptr");
7407 int uintptr_bits = uintptr_type->integer_type()->bits();
7408
69d8df44
ILT
7409 Type_context int_context(Type::lookup_integer_type("int"), false);
7410
3b8dffe7
ILT
7411 ++parg;
7412 Expression* len_arg;
7413 if (parg == args->end())
7414 {
7415 if (is_slice)
7416 {
7417 this->report_error(_("length required when allocating a slice"));
7418 return Expression::make_error(this->location());
7419 }
7420
7421 mpz_t zval;
7422 mpz_init_set_ui(zval, 0);
7423 len_arg = Expression::make_integer(&zval, NULL, loc);
7424 mpz_clear(zval);
7425 }
7426 else
7427 {
7428 len_arg = *parg;
69d8df44 7429 len_arg->determine_type(&int_context);
54d04de7
ILT
7430 if (!this->check_int_value(len_arg, true))
7431 return Expression::make_error(this->location());
047cff81
ILT
7432 if (len_arg->type()->integer_type() != NULL
7433 && len_arg->type()->integer_type()->bits() > uintptr_bits)
7434 have_big_args = true;
3b8dffe7
ILT
7435 ++parg;
7436 }
7437
7438 Expression* cap_arg = NULL;
7439 if (is_slice && parg != args->end())
7440 {
7441 cap_arg = *parg;
69d8df44 7442 cap_arg->determine_type(&int_context);
54d04de7
ILT
7443 if (!this->check_int_value(cap_arg, false))
7444 return Expression::make_error(this->location());
7445
7446 Numeric_constant nclen;
7447 Numeric_constant nccap;
7448 unsigned long vlen;
7449 unsigned long vcap;
7450 if (len_arg->numeric_constant_value(&nclen)
7451 && cap_arg->numeric_constant_value(&nccap)
7452 && nclen.to_unsigned_long(&vlen) == Numeric_constant::NC_UL_VALID
7453 && nccap.to_unsigned_long(&vcap) == Numeric_constant::NC_UL_VALID
7454 && vlen > vcap)
3b8dffe7 7455 {
54d04de7 7456 this->report_error(_("len larger than cap"));
3b8dffe7
ILT
7457 return Expression::make_error(this->location());
7458 }
54d04de7 7459
047cff81
ILT
7460 if (cap_arg->type()->integer_type() != NULL
7461 && cap_arg->type()->integer_type()->bits() > uintptr_bits)
7462 have_big_args = true;
3b8dffe7
ILT
7463 ++parg;
7464 }
7465
7466 if (parg != args->end())
7467 {
7468 this->report_error(_("too many arguments to make"));
7469 return Expression::make_error(this->location());
7470 }
7471
8afa2bfb 7472 Location type_loc = first_arg->location();
3b8dffe7
ILT
7473 Expression* type_arg;
7474 if (is_slice || is_chan)
7475 type_arg = Expression::make_type_descriptor(type, type_loc);
7476 else if (is_map)
7477 type_arg = Expression::make_map_descriptor(type->map_type(), type_loc);
7478 else
7479 go_unreachable();
7480
7481 Expression* call;
7482 if (is_slice)
7483 {
7484 if (cap_arg == NULL)
047cff81
ILT
7485 call = Runtime::make_call((have_big_args
7486 ? Runtime::MAKESLICE1BIG
7487 : Runtime::MAKESLICE1),
7488 loc, 2, type_arg, len_arg);
3b8dffe7 7489 else
047cff81
ILT
7490 call = Runtime::make_call((have_big_args
7491 ? Runtime::MAKESLICE2BIG
7492 : Runtime::MAKESLICE2),
7493 loc, 3, type_arg, len_arg, cap_arg);
3b8dffe7
ILT
7494 }
7495 else if (is_map)
047cff81
ILT
7496 call = Runtime::make_call((have_big_args
7497 ? Runtime::MAKEMAPBIG
7498 : Runtime::MAKEMAP),
7499 loc, 2, type_arg, len_arg);
3b8dffe7 7500 else if (is_chan)
047cff81
ILT
7501 call = Runtime::make_call((have_big_args
7502 ? Runtime::MAKECHANBIG
7503 : Runtime::MAKECHAN),
7504 loc, 2, type_arg, len_arg);
3b8dffe7
ILT
7505 else
7506 go_unreachable();
7507
7508 return Expression::make_unsafe_cast(type, call, loc);
7509}
7510
7511// Return whether an expression has an integer value. Report an error
7512// if not. This is used when handling calls to the predeclared make
7513// function.
7514
7515bool
54d04de7 7516Builtin_call_expression::check_int_value(Expression* e, bool is_length)
3b8dffe7 7517{
5caf63ca 7518 Numeric_constant nc;
54d04de7 7519 if (e->numeric_constant_value(&nc))
3b8dffe7 7520 {
54d04de7
ILT
7521 unsigned long v;
7522 switch (nc.to_unsigned_long(&v))
7523 {
7524 case Numeric_constant::NC_UL_VALID:
d6394e2b 7525 break;
54d04de7
ILT
7526 case Numeric_constant::NC_UL_NOTINT:
7527 error_at(e->location(), "non-integer %s argument to make",
7528 is_length ? "len" : "cap");
7529 return false;
7530 case Numeric_constant::NC_UL_NEGATIVE:
7531 error_at(e->location(), "negative %s argument to make",
7532 is_length ? "len" : "cap");
7533 return false;
7534 case Numeric_constant::NC_UL_BIG:
7535 // We don't want to give a compile-time error for a 64-bit
7536 // value on a 32-bit target.
d6394e2b 7537 break;
54d04de7 7538 }
d6394e2b
ILT
7539
7540 mpz_t val;
7541 if (!nc.to_int(&val))
7542 go_unreachable();
7543 int bits = mpz_sizeinbase(val, 2);
7544 mpz_clear(val);
7545 Type* int_type = Type::lookup_integer_type("int");
7546 if (bits >= int_type->integer_type()->bits())
7547 {
7548 error_at(e->location(), "%s argument too large for make",
7549 is_length ? "len" : "cap");
7550 return false;
7551 }
7552
7553 return true;
3b8dffe7
ILT
7554 }
7555
54d04de7
ILT
7556 if (e->type()->integer_type() != NULL)
7557 return true;
7558
7559 error_at(e->location(), "non-integer %s argument to make",
7560 is_length ? "len" : "cap");
3b8dffe7
ILT
7561 return false;
7562}
7563
7a938933
ILT
7564// Return the type of the real or imag functions, given the type of
7565// the argument. We need to map complex to float, complex64 to
7566// float32, and complex128 to float64, so it has to be done by name.
7567// This returns NULL if it can't figure out the type.
7568
7569Type*
7570Builtin_call_expression::real_imag_type(Type* arg_type)
7571{
7572 if (arg_type == NULL || arg_type->is_abstract())
7573 return NULL;
7574 Named_type* nt = arg_type->named_type();
7575 if (nt == NULL)
7576 return NULL;
7577 while (nt->real_type()->named_type() != NULL)
7578 nt = nt->real_type()->named_type();
ff5f50c5 7579 if (nt->name() == "complex64")
7a938933
ILT
7580 return Type::lookup_float_type("float32");
7581 else if (nt->name() == "complex128")
7582 return Type::lookup_float_type("float64");
7583 else
7584 return NULL;
7585}
7586
ff5f50c5 7587// Return the type of the complex function, given the type of one of the
7a938933
ILT
7588// argments. Like real_imag_type, we have to map by name.
7589
7590Type*
ff5f50c5 7591Builtin_call_expression::complex_type(Type* arg_type)
7a938933
ILT
7592{
7593 if (arg_type == NULL || arg_type->is_abstract())
7594 return NULL;
7595 Named_type* nt = arg_type->named_type();
7596 if (nt == NULL)
7597 return NULL;
7598 while (nt->real_type()->named_type() != NULL)
7599 nt = nt->real_type()->named_type();
ff5f50c5 7600 if (nt->name() == "float32")
7a938933
ILT
7601 return Type::lookup_complex_type("complex64");
7602 else if (nt->name() == "float64")
7603 return Type::lookup_complex_type("complex128");
7604 else
7605 return NULL;
7606}
7607
7608// Return a single argument, or NULL if there isn't one.
7609
7610Expression*
7611Builtin_call_expression::one_arg() const
7612{
7613 const Expression_list* args = this->args();
2e540b50 7614 if (args == NULL || args->size() != 1)
7a938933
ILT
7615 return NULL;
7616 return args->front();
7617}
7618
211993b3
ILT
7619// A traversal class which looks for a call or receive expression.
7620
7621class Find_call_expression : public Traverse
7622{
7623 public:
7624 Find_call_expression()
7625 : Traverse(traverse_expressions),
7626 found_(false)
7627 { }
7628
7629 int
7630 expression(Expression**);
7631
7632 bool
7633 found()
7634 { return this->found_; }
7635
7636 private:
7637 bool found_;
7638};
7639
7640int
7641Find_call_expression::expression(Expression** pexpr)
7642{
7643 if ((*pexpr)->call_expression() != NULL
7644 || (*pexpr)->receive_expression() != NULL)
7645 {
7646 this->found_ = true;
7647 return TRAVERSE_EXIT;
7648 }
7649 return TRAVERSE_CONTINUE;
7650}
7651
7652// Return whether this is constant: len of a string constant, or len
7653// or cap of an array, or unsafe.Sizeof, unsafe.Offsetof,
7654// unsafe.Alignof.
7a938933
ILT
7655
7656bool
7657Builtin_call_expression::do_is_constant() const
7658{
d6218114
ILT
7659 if (this->is_error_expression())
7660 return true;
7a938933
ILT
7661 switch (this->code_)
7662 {
7663 case BUILTIN_LEN:
7664 case BUILTIN_CAP:
7665 {
ab9d6dcf
ILT
7666 if (this->seen_)
7667 return false;
7668
7a938933
ILT
7669 Expression* arg = this->one_arg();
7670 if (arg == NULL)
7671 return false;
7672 Type* arg_type = arg->type();
7673
7674 if (arg_type->points_to() != NULL
7675 && arg_type->points_to()->array_type() != NULL
b7190f2f 7676 && !arg_type->points_to()->is_slice_type())
7a938933
ILT
7677 arg_type = arg_type->points_to();
7678
211993b3
ILT
7679 // The len and cap functions are only constant if there are no
7680 // function calls or channel operations in the arguments.
7681 // Otherwise we have to make the call.
7682 if (!arg->is_constant())
7683 {
7684 Find_call_expression find_call;
7685 Expression::traverse(&arg, &find_call);
7686 if (find_call.found())
7687 return false;
7688 }
7689
7a938933
ILT
7690 if (arg_type->array_type() != NULL
7691 && arg_type->array_type()->length() != NULL)
ab9d6dcf 7692 return true;
7a938933
ILT
7693
7694 if (this->code_ == BUILTIN_LEN && arg_type->is_string_type())
ab9d6dcf
ILT
7695 {
7696 this->seen_ = true;
7697 bool ret = arg->is_constant();
7698 this->seen_ = false;
7699 return ret;
7700 }
7a938933
ILT
7701 }
7702 break;
7703
7704 case BUILTIN_SIZEOF:
7705 case BUILTIN_ALIGNOF:
7706 return this->one_arg() != NULL;
7707
7708 case BUILTIN_OFFSETOF:
7709 {
7710 Expression* arg = this->one_arg();
7711 if (arg == NULL)
7712 return false;
7713 return arg->field_reference_expression() != NULL;
7714 }
7715
ff5f50c5 7716 case BUILTIN_COMPLEX:
7a938933
ILT
7717 {
7718 const Expression_list* args = this->args();
7719 if (args != NULL && args->size() == 2)
7720 return args->front()->is_constant() && args->back()->is_constant();
7721 }
7722 break;
7723
7724 case BUILTIN_REAL:
7725 case BUILTIN_IMAG:
7726 {
7727 Expression* arg = this->one_arg();
7728 return arg != NULL && arg->is_constant();
7729 }
7730
7731 default:
7732 break;
7733 }
7734
7735 return false;
7736}
7737
5caf63ca 7738// Return a numeric constant if possible.
7a938933
ILT
7739
7740bool
5caf63ca 7741Builtin_call_expression::do_numeric_constant_value(Numeric_constant* nc) const
7a938933
ILT
7742{
7743 if (this->code_ == BUILTIN_LEN
7744 || this->code_ == BUILTIN_CAP)
7745 {
7746 Expression* arg = this->one_arg();
7747 if (arg == NULL)
7748 return false;
7749 Type* arg_type = arg->type();
7750
7751 if (this->code_ == BUILTIN_LEN && arg_type->is_string_type())
7752 {
7753 std::string sval;
7754 if (arg->string_constant_value(&sval))
7755 {
5caf63ca
ILT
7756 nc->set_unsigned_long(Type::lookup_integer_type("int"),
7757 sval.length());
7a938933
ILT
7758 return true;
7759 }
7760 }
7761
7762 if (arg_type->points_to() != NULL
7763 && arg_type->points_to()->array_type() != NULL
b7190f2f 7764 && !arg_type->points_to()->is_slice_type())
7a938933
ILT
7765 arg_type = arg_type->points_to();
7766
7767 if (arg_type->array_type() != NULL
7768 && arg_type->array_type()->length() != NULL)
7769 {
ab9d6dcf
ILT
7770 if (this->seen_)
7771 return false;
7a938933 7772 Expression* e = arg_type->array_type()->length();
ab9d6dcf 7773 this->seen_ = true;
5caf63ca 7774 bool r = e->numeric_constant_value(nc);
ab9d6dcf
ILT
7775 this->seen_ = false;
7776 if (r)
7a938933 7777 {
5caf63ca
ILT
7778 if (!nc->set_type(Type::lookup_integer_type("int"), false,
7779 this->location()))
7780 r = false;
7a938933 7781 }
5caf63ca 7782 return r;
7a938933
ILT
7783 }
7784 }
7785 else if (this->code_ == BUILTIN_SIZEOF
7786 || this->code_ == BUILTIN_ALIGNOF)
7787 {
7788 Expression* arg = this->one_arg();
7789 if (arg == NULL)
7790 return false;
7791 Type* arg_type = arg->type();
02ed921a 7792 if (arg_type->is_error())
7a938933
ILT
7793 return false;
7794 if (arg_type->is_abstract())
7795 return false;
ef1ed13d
ILT
7796
7797 unsigned int ret;
7a938933
ILT
7798 if (this->code_ == BUILTIN_SIZEOF)
7799 {
ef1ed13d 7800 if (!arg_type->backend_type_size(this->gogo_, &ret))
7a938933
ILT
7801 return false;
7802 }
7803 else if (this->code_ == BUILTIN_ALIGNOF)
7804 {
3dcdeeb2 7805 if (arg->field_reference_expression() == NULL)
ef1ed13d
ILT
7806 {
7807 if (!arg_type->backend_type_align(this->gogo_, &ret))
7808 return false;
7809 }
3dcdeeb2 7810 else
7a938933
ILT
7811 {
7812 // Calling unsafe.Alignof(s.f) returns the alignment of
7813 // the type of f when it is used as a field in a struct.
ef1ed13d
ILT
7814 if (!arg_type->backend_type_field_align(this->gogo_, &ret))
7815 return false;
7a938933 7816 }
7a938933
ILT
7817 }
7818 else
8c0d1865 7819 go_unreachable();
ef1ed13d 7820
a0c8ad3b
ILT
7821 nc->set_unsigned_long(Type::lookup_integer_type("uintptr"),
7822 static_cast<unsigned long>(ret));
7a938933
ILT
7823 return true;
7824 }
7825 else if (this->code_ == BUILTIN_OFFSETOF)
7826 {
7827 Expression* arg = this->one_arg();
7828 if (arg == NULL)
7829 return false;
7830 Field_reference_expression* farg = arg->field_reference_expression();
7831 if (farg == NULL)
7832 return false;
51b08ada
ILT
7833 unsigned int total_offset = 0;
7834 while (true)
7835 {
7836 Expression* struct_expr = farg->expr();
7837 Type* st = struct_expr->type();
7838 if (st->struct_type() == NULL)
7839 return false;
7840 if (st->named_type() != NULL)
7841 st->named_type()->convert(this->gogo_);
7842 unsigned int offset;
7843 if (!st->struct_type()->backend_field_offset(this->gogo_,
7844 farg->field_index(),
7845 &offset))
7846 return false;
7847 total_offset += offset;
7848 if (farg->implicit() && struct_expr->field_reference_expression() != NULL)
7849 {
7850 // Go up until we reach the original base.
7851 farg = struct_expr->field_reference_expression();
7852 continue;
7853 }
7854 break;
7855 }
a0c8ad3b 7856 nc->set_unsigned_long(Type::lookup_integer_type("uintptr"),
51b08ada 7857 static_cast<unsigned long>(total_offset));
7a938933
ILT
7858 return true;
7859 }
5caf63ca 7860 else if (this->code_ == BUILTIN_REAL || this->code_ == BUILTIN_IMAG)
7a938933
ILT
7861 {
7862 Expression* arg = this->one_arg();
7863 if (arg == NULL)
7864 return false;
7865
5caf63ca
ILT
7866 Numeric_constant argnc;
7867 if (!arg->numeric_constant_value(&argnc))
7868 return false;
7869
7a938933
ILT
7870 mpfr_t real;
7871 mpfr_t imag;
5caf63ca
ILT
7872 if (!argnc.to_complex(&real, &imag))
7873 return false;
7a938933 7874
5caf63ca
ILT
7875 Type* type = Builtin_call_expression::real_imag_type(argnc.type());
7876 if (this->code_ == BUILTIN_REAL)
7877 nc->set_float(type, real);
7878 else
7879 nc->set_float(type, imag);
7880 return true;
7a938933 7881 }
5caf63ca 7882 else if (this->code_ == BUILTIN_COMPLEX)
7a938933
ILT
7883 {
7884 const Expression_list* args = this->args();
7885 if (args == NULL || args->size() != 2)
7886 return false;
7887
5caf63ca
ILT
7888 Numeric_constant rnc;
7889 if (!args->front()->numeric_constant_value(&rnc))
7890 return false;
7891 Numeric_constant inc;
7892 if (!args->back()->numeric_constant_value(&inc))
7893 return false;
7894
7895 if (rnc.type() != NULL
7896 && !rnc.type()->is_abstract()
7897 && inc.type() != NULL
7898 && !inc.type()->is_abstract()
7899 && !Type::are_identical(rnc.type(), inc.type(), false, NULL))
7900 return false;
7901
7a938933 7902 mpfr_t r;
5caf63ca
ILT
7903 if (!rnc.to_float(&r))
7904 return false;
7905 mpfr_t i;
7906 if (!inc.to_float(&i))
7a938933
ILT
7907 {
7908 mpfr_clear(r);
7909 return false;
7910 }
7911
5caf63ca
ILT
7912 Type* arg_type = rnc.type();
7913 if (arg_type == NULL || arg_type->is_abstract())
7914 arg_type = inc.type();
7a938933 7915
5caf63ca
ILT
7916 Type* type = Builtin_call_expression::complex_type(arg_type);
7917 nc->set_complex(type, r, i);
7a938933
ILT
7918
7919 mpfr_clear(r);
7920 mpfr_clear(i);
7921
5caf63ca 7922 return true;
7a938933
ILT
7923 }
7924
7925 return false;
7926}
7927
3c365907
ILT
7928// Give an error if we are discarding the value of an expression which
7929// should not normally be discarded. We don't give an error for
7930// discarding the value of an ordinary function call, but we do for
7931// builtin functions, purely for consistency with the gc compiler.
7932
3f7af571 7933bool
3c365907
ILT
7934Builtin_call_expression::do_discarding_value()
7935{
7936 switch (this->code_)
7937 {
7938 case BUILTIN_INVALID:
7939 default:
7940 go_unreachable();
7941
7942 case BUILTIN_APPEND:
7943 case BUILTIN_CAP:
7944 case BUILTIN_COMPLEX:
7945 case BUILTIN_IMAG:
7946 case BUILTIN_LEN:
7947 case BUILTIN_MAKE:
7948 case BUILTIN_NEW:
7949 case BUILTIN_REAL:
7950 case BUILTIN_ALIGNOF:
7951 case BUILTIN_OFFSETOF:
7952 case BUILTIN_SIZEOF:
7953 this->unused_value_error();
3f7af571 7954 return false;
3c365907
ILT
7955
7956 case BUILTIN_CLOSE:
7957 case BUILTIN_COPY:
f29ce5f5 7958 case BUILTIN_DELETE:
3c365907
ILT
7959 case BUILTIN_PANIC:
7960 case BUILTIN_PRINT:
7961 case BUILTIN_PRINTLN:
7962 case BUILTIN_RECOVER:
3f7af571 7963 return true;
3c365907
ILT
7964 }
7965}
7966
7a938933
ILT
7967// Return the type.
7968
7969Type*
7970Builtin_call_expression::do_type()
7971{
7972 switch (this->code_)
7973 {
7974 case BUILTIN_INVALID:
7975 default:
8c0d1865 7976 go_unreachable();
7a938933
ILT
7977
7978 case BUILTIN_NEW:
7979 case BUILTIN_MAKE:
7980 {
7981 const Expression_list* args = this->args();
7982 if (args == NULL || args->empty())
7983 return Type::make_error_type();
7984 return Type::make_pointer_type(args->front()->type());
7985 }
7986
7987 case BUILTIN_CAP:
7988 case BUILTIN_COPY:
7989 case BUILTIN_LEN:
a0c8ad3b
ILT
7990 return Type::lookup_integer_type("int");
7991
7a938933
ILT
7992 case BUILTIN_ALIGNOF:
7993 case BUILTIN_OFFSETOF:
7994 case BUILTIN_SIZEOF:
a0c8ad3b 7995 return Type::lookup_integer_type("uintptr");
7a938933
ILT
7996
7997 case BUILTIN_CLOSE:
f29ce5f5 7998 case BUILTIN_DELETE:
7a938933
ILT
7999 case BUILTIN_PANIC:
8000 case BUILTIN_PRINT:
8001 case BUILTIN_PRINTLN:
8002 return Type::make_void_type();
8003
7a938933 8004 case BUILTIN_RECOVER:
7c0434e5 8005 return Type::make_empty_interface_type(Linemap::predeclared_location());
7a938933
ILT
8006
8007 case BUILTIN_APPEND:
8008 {
8009 const Expression_list* args = this->args();
8010 if (args == NULL || args->empty())
8011 return Type::make_error_type();
8012 return args->front()->type();
8013 }
8014
8015 case BUILTIN_REAL:
8016 case BUILTIN_IMAG:
8017 {
8018 Expression* arg = this->one_arg();
8019 if (arg == NULL)
8020 return Type::make_error_type();
8021 Type* t = arg->type();
8022 if (t->is_abstract())
8023 t = t->make_non_abstract_type();
8024 t = Builtin_call_expression::real_imag_type(t);
8025 if (t == NULL)
8026 t = Type::make_error_type();
8027 return t;
8028 }
8029
ff5f50c5 8030 case BUILTIN_COMPLEX:
7a938933
ILT
8031 {
8032 const Expression_list* args = this->args();
8033 if (args == NULL || args->size() != 2)
8034 return Type::make_error_type();
8035 Type* t = args->front()->type();
8036 if (t->is_abstract())
8037 {
8038 t = args->back()->type();
8039 if (t->is_abstract())
8040 t = t->make_non_abstract_type();
8041 }
ff5f50c5 8042 t = Builtin_call_expression::complex_type(t);
7a938933
ILT
8043 if (t == NULL)
8044 t = Type::make_error_type();
8045 return t;
8046 }
8047 }
8048}
8049
8050// Determine the type.
8051
8052void
8053Builtin_call_expression::do_determine_type(const Type_context* context)
8054{
20532210
ILT
8055 if (!this->determining_types())
8056 return;
8057
7a938933
ILT
8058 this->fn()->determine_type_no_context();
8059
8060 const Expression_list* args = this->args();
8061
8062 bool is_print;
8063 Type* arg_type = NULL;
8064 switch (this->code_)
8065 {
8066 case BUILTIN_PRINT:
8067 case BUILTIN_PRINTLN:
8068 // Do not force a large integer constant to "int".
8069 is_print = true;
8070 break;
8071
8072 case BUILTIN_REAL:
8073 case BUILTIN_IMAG:
ff5f50c5 8074 arg_type = Builtin_call_expression::complex_type(context->type);
69d8df44
ILT
8075 if (arg_type == NULL)
8076 arg_type = Type::lookup_complex_type("complex128");
7a938933
ILT
8077 is_print = false;
8078 break;
8079
ff5f50c5 8080 case BUILTIN_COMPLEX:
7a938933 8081 {
ff5f50c5 8082 // For the complex function the type of one operand can
7a938933
ILT
8083 // determine the type of the other, as in a binary expression.
8084 arg_type = Builtin_call_expression::real_imag_type(context->type);
69d8df44
ILT
8085 if (arg_type == NULL)
8086 arg_type = Type::lookup_float_type("float64");
7a938933
ILT
8087 if (args != NULL && args->size() == 2)
8088 {
8089 Type* t1 = args->front()->type();
a7a90456 8090 Type* t2 = args->back()->type();
7a938933
ILT
8091 if (!t1->is_abstract())
8092 arg_type = t1;
8093 else if (!t2->is_abstract())
8094 arg_type = t2;
8095 }
8096 is_print = false;
8097 }
8098 break;
8099
8100 default:
8101 is_print = false;
8102 break;
8103 }
8104
8105 if (args != NULL)
8106 {
8107 for (Expression_list::const_iterator pa = args->begin();
8108 pa != args->end();
8109 ++pa)
8110 {
8111 Type_context subcontext;
8112 subcontext.type = arg_type;
8113
8114 if (is_print)
8115 {
8116 // We want to print large constants, we so can't just
8117 // use the appropriate nonabstract type. Use uint64 for
8118 // an integer if we know it is nonnegative, otherwise
8119 // use int64 for a integer, otherwise use float64 for a
8120 // float or complex128 for a complex.
8121 Type* want_type = NULL;
8122 Type* atype = (*pa)->type();
8123 if (atype->is_abstract())
8124 {
8125 if (atype->integer_type() != NULL)
8126 {
5caf63ca
ILT
8127 Numeric_constant nc;
8128 if (this->numeric_constant_value(&nc))
8129 {
8130 mpz_t val;
8131 if (nc.to_int(&val))
8132 {
8133 if (mpz_sgn(val) >= 0)
8134 want_type = Type::lookup_integer_type("uint64");
8135 mpz_clear(val);
8136 }
8137 }
8138 if (want_type == NULL)
7a938933 8139 want_type = Type::lookup_integer_type("int64");
7a938933
ILT
8140 }
8141 else if (atype->float_type() != NULL)
8142 want_type = Type::lookup_float_type("float64");
8143 else if (atype->complex_type() != NULL)
8144 want_type = Type::lookup_complex_type("complex128");
8145 else if (atype->is_abstract_string_type())
8146 want_type = Type::lookup_string_type();
8147 else if (atype->is_abstract_boolean_type())
8148 want_type = Type::lookup_bool_type();
8149 else
8c0d1865 8150 go_unreachable();
7a938933
ILT
8151 subcontext.type = want_type;
8152 }
8153 }
8154
8155 (*pa)->determine_type(&subcontext);
8156 }
8157 }
8158}
8159
8160// If there is exactly one argument, return true. Otherwise give an
8161// error message and return false.
8162
8163bool
8164Builtin_call_expression::check_one_arg()
8165{
8166 const Expression_list* args = this->args();
8167 if (args == NULL || args->size() < 1)
8168 {
8169 this->report_error(_("not enough arguments"));
8170 return false;
8171 }
8172 else if (args->size() > 1)
8173 {
8174 this->report_error(_("too many arguments"));
8175 return false;
8176 }
8177 if (args->front()->is_error_expression()
02ed921a 8178 || args->front()->type()->is_error())
7a938933
ILT
8179 {
8180 this->set_is_error();
8181 return false;
8182 }
8183 return true;
8184}
8185
8186// Check argument types for a builtin function.
8187
8188void
8189Builtin_call_expression::do_check_types(Gogo*)
8190{
552ab977
ILT
8191 if (this->is_error_expression())
8192 return;
7a938933
ILT
8193 switch (this->code_)
8194 {
8195 case BUILTIN_INVALID:
8196 case BUILTIN_NEW:
8197 case BUILTIN_MAKE:
9b83b6d2 8198 case BUILTIN_DELETE:
7a938933
ILT
8199 return;
8200
8201 case BUILTIN_LEN:
8202 case BUILTIN_CAP:
8203 {
8204 // The single argument may be either a string or an array or a
8205 // map or a channel, or a pointer to a closed array.
8206 if (this->check_one_arg())
8207 {
8208 Type* arg_type = this->one_arg()->type();
8209 if (arg_type->points_to() != NULL
8210 && arg_type->points_to()->array_type() != NULL
b7190f2f 8211 && !arg_type->points_to()->is_slice_type())
7a938933
ILT
8212 arg_type = arg_type->points_to();
8213 if (this->code_ == BUILTIN_CAP)
8214 {
02ed921a 8215 if (!arg_type->is_error()
7a938933
ILT
8216 && arg_type->array_type() == NULL
8217 && arg_type->channel_type() == NULL)
8218 this->report_error(_("argument must be array or slice "
8219 "or channel"));
8220 }
8221 else
8222 {
02ed921a 8223 if (!arg_type->is_error()
7a938933
ILT
8224 && !arg_type->is_string_type()
8225 && arg_type->array_type() == NULL
8226 && arg_type->map_type() == NULL
8227 && arg_type->channel_type() == NULL)
8228 this->report_error(_("argument must be string or "
8229 "array or slice or map or channel"));
8230 }
8231 }
8232 }
8233 break;
8234
8235 case BUILTIN_PRINT:
8236 case BUILTIN_PRINTLN:
8237 {
8238 const Expression_list* args = this->args();
8239 if (args == NULL)
8240 {
8241 if (this->code_ == BUILTIN_PRINT)
8242 warning_at(this->location(), 0,
8243 "no arguments for builtin function %<%s%>",
8244 (this->code_ == BUILTIN_PRINT
8245 ? "print"
8246 : "println"));
8247 }
8248 else
8249 {
8250 for (Expression_list::const_iterator p = args->begin();
8251 p != args->end();
8252 ++p)
8253 {
8254 Type* type = (*p)->type();
02ed921a 8255 if (type->is_error()
7a938933
ILT
8256 || type->is_string_type()
8257 || type->integer_type() != NULL
8258 || type->float_type() != NULL
8259 || type->complex_type() != NULL
8260 || type->is_boolean_type()
8261 || type->points_to() != NULL
8262 || type->interface_type() != NULL
8263 || type->channel_type() != NULL
8264 || type->map_type() != NULL
8265 || type->function_type() != NULL
b7190f2f 8266 || type->is_slice_type())
7a938933 8267 ;
d4157849
ILT
8268 else if ((*p)->is_type_expression())
8269 {
8270 // If this is a type expression it's going to give
8271 // an error anyhow, so we don't need one here.
8272 }
7a938933
ILT
8273 else
8274 this->report_error(_("unsupported argument type to "
8275 "builtin function"));
8276 }
8277 }
8278 }
8279 break;
8280
8281 case BUILTIN_CLOSE:
7a938933
ILT
8282 if (this->check_one_arg())
8283 {
8284 if (this->one_arg()->type()->channel_type() == NULL)
8285 this->report_error(_("argument must be channel"));
09367c0d
ILT
8286 else if (!this->one_arg()->type()->channel_type()->may_send())
8287 this->report_error(_("cannot close receive-only channel"));
7a938933
ILT
8288 }
8289 break;
8290
8291 case BUILTIN_PANIC:
8292 case BUILTIN_SIZEOF:
8293 case BUILTIN_ALIGNOF:
8294 this->check_one_arg();
8295 break;
8296
8297 case BUILTIN_RECOVER:
8298 if (this->args() != NULL && !this->args()->empty())
8299 this->report_error(_("too many arguments"));
8300 break;
8301
8302 case BUILTIN_OFFSETOF:
8303 if (this->check_one_arg())
8304 {
8305 Expression* arg = this->one_arg();
8306 if (arg->field_reference_expression() == NULL)
8307 this->report_error(_("argument must be a field reference"));
8308 }
8309 break;
8310
8311 case BUILTIN_COPY:
8312 {
8313 const Expression_list* args = this->args();
8314 if (args == NULL || args->size() < 2)
8315 {
8316 this->report_error(_("not enough arguments"));
8317 break;
8318 }
8319 else if (args->size() > 2)
8320 {
8321 this->report_error(_("too many arguments"));
8322 break;
8323 }
8324 Type* arg1_type = args->front()->type();
8325 Type* arg2_type = args->back()->type();
02ed921a 8326 if (arg1_type->is_error() || arg2_type->is_error())
7a938933
ILT
8327 break;
8328
8329 Type* e1;
b7190f2f 8330 if (arg1_type->is_slice_type())
7a938933
ILT
8331 e1 = arg1_type->array_type()->element_type();
8332 else
8333 {
8334 this->report_error(_("left argument must be a slice"));
8335 break;
8336 }
8337
b7190f2f 8338 if (arg2_type->is_slice_type())
7b31a84d
ILT
8339 {
8340 Type* e2 = arg2_type->array_type()->element_type();
8341 if (!Type::are_identical(e1, e2, true, NULL))
8342 this->report_error(_("element types must be the same"));
8343 }
7a938933 8344 else if (arg2_type->is_string_type())
7a938933 8345 {
7b31a84d
ILT
8346 if (e1->integer_type() == NULL || !e1->integer_type()->is_byte())
8347 this->report_error(_("first argument must be []byte"));
7a938933 8348 }
7b31a84d
ILT
8349 else
8350 this->report_error(_("second argument must be slice or string"));
7a938933
ILT
8351 }
8352 break;
8353
8354 case BUILTIN_APPEND:
8355 {
8356 const Expression_list* args = this->args();
d7ab2512 8357 if (args == NULL || args->size() < 2)
7a938933
ILT
8358 {
8359 this->report_error(_("not enough arguments"));
8360 break;
8361 }
7838059f
ILT
8362 if (args->size() > 2)
8363 {
8364 this->report_error(_("too many arguments"));
8365 break;
8366 }
9b83b6d2
ILT
8367 if (args->front()->type()->is_error()
8368 || args->back()->type()->is_error())
8369 break;
8370
8371 Array_type* at = args->front()->type()->array_type();
8372 Type* e = at->element_type();
a1ee0aaf
ILT
8373
8374 // The language permits appending a string to a []byte, as a
8375 // special case.
8376 if (args->back()->type()->is_string_type())
8377 {
7b31a84d 8378 if (e->integer_type() != NULL && e->integer_type()->is_byte())
a1ee0aaf
ILT
8379 break;
8380 }
8381
80678229
ILT
8382 // The language says that the second argument must be
8383 // assignable to a slice of the element type of the first
8384 // argument. We already know the first argument is a slice
8385 // type.
9b83b6d2 8386 Type* arg2_type = Type::make_array_type(e, NULL);
7a938933 8387 std::string reason;
80678229 8388 if (!Type::are_assignable(arg2_type, args->back()->type(), &reason))
7a938933
ILT
8389 {
8390 if (reason.empty())
80678229 8391 this->report_error(_("argument 2 has invalid type"));
7a938933
ILT
8392 else
8393 {
80678229 8394 error_at(this->location(), "argument 2 has invalid type (%s)",
7a938933
ILT
8395 reason.c_str());
8396 this->set_is_error();
8397 }
8398 }
8399 break;
8400 }
8401
8402 case BUILTIN_REAL:
8403 case BUILTIN_IMAG:
8404 if (this->check_one_arg())
8405 {
8406 if (this->one_arg()->type()->complex_type() == NULL)
8407 this->report_error(_("argument must have complex type"));
8408 }
8409 break;
8410
ff5f50c5 8411 case BUILTIN_COMPLEX:
7a938933
ILT
8412 {
8413 const Expression_list* args = this->args();
8414 if (args == NULL || args->size() < 2)
8415 this->report_error(_("not enough arguments"));
8416 else if (args->size() > 2)
8417 this->report_error(_("too many arguments"));
8418 else if (args->front()->is_error_expression()
02ed921a 8419 || args->front()->type()->is_error()
7a938933 8420 || args->back()->is_error_expression()
02ed921a 8421 || args->back()->type()->is_error())
7a938933
ILT
8422 this->set_is_error();
8423 else if (!Type::are_identical(args->front()->type(),
50ba28bb 8424 args->back()->type(), true, NULL))
ff5f50c5 8425 this->report_error(_("complex arguments must have identical types"));
7a938933 8426 else if (args->front()->type()->float_type() == NULL)
ff5f50c5 8427 this->report_error(_("complex arguments must have "
7a938933
ILT
8428 "floating-point type"));
8429 }
8430 break;
8431
8432 default:
8c0d1865 8433 go_unreachable();
7a938933
ILT
8434 }
8435}
8436
8437// Return the tree for a builtin function.
8438
8439tree
8440Builtin_call_expression::do_get_tree(Translate_context* context)
8441{
8442 Gogo* gogo = context->gogo();
8afa2bfb 8443 Location location = this->location();
7a938933
ILT
8444 switch (this->code_)
8445 {
8446 case BUILTIN_INVALID:
8447 case BUILTIN_NEW:
8448 case BUILTIN_MAKE:
8c0d1865 8449 go_unreachable();
7a938933
ILT
8450
8451 case BUILTIN_LEN:
8452 case BUILTIN_CAP:
8453 {
8454 const Expression_list* args = this->args();
26409c52 8455 go_assert(args != NULL && args->size() == 1);
7a938933
ILT
8456 Expression* arg = *args->begin();
8457 Type* arg_type = arg->type();
ab9d6dcf
ILT
8458
8459 if (this->seen_)
8460 {
26409c52 8461 go_assert(saw_errors());
ab9d6dcf
ILT
8462 return error_mark_node;
8463 }
8464 this->seen_ = true;
8465
7a938933 8466 tree arg_tree = arg->get_tree(context);
ab9d6dcf
ILT
8467
8468 this->seen_ = false;
8469
7a938933
ILT
8470 if (arg_tree == error_mark_node)
8471 return error_mark_node;
8472
8473 if (arg_type->points_to() != NULL)
8474 {
8475 arg_type = arg_type->points_to();
26409c52 8476 go_assert(arg_type->array_type() != NULL
b7190f2f 8477 && !arg_type->is_slice_type());
26409c52 8478 go_assert(POINTER_TYPE_P(TREE_TYPE(arg_tree)));
7a938933
ILT
8479 arg_tree = build_fold_indirect_ref(arg_tree);
8480 }
8481
776f27a6
ILT
8482 Type* int_type = Type::lookup_integer_type("int");
8483 tree int_type_tree = type_to_tree(int_type->get_backend(gogo));
8484
7a938933
ILT
8485 tree val_tree;
8486 if (this->code_ == BUILTIN_LEN)
8487 {
8488 if (arg_type->is_string_type())
8489 val_tree = String_type::length_tree(gogo, arg_tree);
8490 else if (arg_type->array_type() != NULL)
ab9d6dcf
ILT
8491 {
8492 if (this->seen_)
8493 {
26409c52 8494 go_assert(saw_errors());
ab9d6dcf
ILT
8495 return error_mark_node;
8496 }
8497 this->seen_ = true;
8498 val_tree = arg_type->array_type()->length_tree(gogo, arg_tree);
8499 this->seen_ = false;
8500 }
7a938933
ILT
8501 else if (arg_type->map_type() != NULL)
8502 {
5b735706 8503 tree arg_type_tree = type_to_tree(arg_type->get_backend(gogo));
7a938933
ILT
8504 static tree map_len_fndecl;
8505 val_tree = Gogo::call_builtin(&map_len_fndecl,
8506 location,
8507 "__go_map_len",
8508 1,
776f27a6 8509 int_type_tree,
5b735706 8510 arg_type_tree,
7a938933
ILT
8511 arg_tree);
8512 }
8513 else if (arg_type->channel_type() != NULL)
8514 {
5b735706 8515 tree arg_type_tree = type_to_tree(arg_type->get_backend(gogo));
7a938933
ILT
8516 static tree chan_len_fndecl;
8517 val_tree = Gogo::call_builtin(&chan_len_fndecl,
8518 location,
8519 "__go_chan_len",
8520 1,
776f27a6 8521 int_type_tree,
5b735706 8522 arg_type_tree,
7a938933
ILT
8523 arg_tree);
8524 }
8525 else
8c0d1865 8526 go_unreachable();
7a938933
ILT
8527 }
8528 else
8529 {
8530 if (arg_type->array_type() != NULL)
ab9d6dcf
ILT
8531 {
8532 if (this->seen_)
8533 {
26409c52 8534 go_assert(saw_errors());
ab9d6dcf
ILT
8535 return error_mark_node;
8536 }
8537 this->seen_ = true;
8538 val_tree = arg_type->array_type()->capacity_tree(gogo,
8539 arg_tree);
8540 this->seen_ = false;
8541 }
7a938933
ILT
8542 else if (arg_type->channel_type() != NULL)
8543 {
5b735706 8544 tree arg_type_tree = type_to_tree(arg_type->get_backend(gogo));
7a938933
ILT
8545 static tree chan_cap_fndecl;
8546 val_tree = Gogo::call_builtin(&chan_cap_fndecl,
8547 location,
8548 "__go_chan_cap",
8549 1,
776f27a6 8550 int_type_tree,
5b735706 8551 arg_type_tree,
7a938933
ILT
8552 arg_tree);
8553 }
8554 else
8c0d1865 8555 go_unreachable();
7a938933
ILT
8556 }
8557
776f27a6
ILT
8558 return fold_convert_loc(location.gcc_location(), int_type_tree,
8559 val_tree);
7a938933
ILT
8560 }
8561
8562 case BUILTIN_PRINT:
8563 case BUILTIN_PRINTLN:
8564 {
8565 const bool is_ln = this->code_ == BUILTIN_PRINTLN;
8566 tree stmt_list = NULL_TREE;
8567
8568 const Expression_list* call_args = this->args();
8569 if (call_args != NULL)
8570 {
8571 for (Expression_list::const_iterator p = call_args->begin();
8572 p != call_args->end();
8573 ++p)
8574 {
8575 if (is_ln && p != call_args->begin())
8576 {
8577 static tree print_space_fndecl;
8578 tree call = Gogo::call_builtin(&print_space_fndecl,
8579 location,
8580 "__go_print_space",
8581 0,
8582 void_type_node);
faff9b04
ILT
8583 if (call == error_mark_node)
8584 return error_mark_node;
7a938933
ILT
8585 append_to_statement_list(call, &stmt_list);
8586 }
8587
8588 Type* type = (*p)->type();
8589
8590 tree arg = (*p)->get_tree(context);
8591 if (arg == error_mark_node)
8592 return error_mark_node;
8593
8594 tree* pfndecl;
8595 const char* fnname;
8596 if (type->is_string_type())
8597 {
8598 static tree print_string_fndecl;
8599 pfndecl = &print_string_fndecl;
8600 fnname = "__go_print_string";
8601 }
8602 else if (type->integer_type() != NULL
8603 && type->integer_type()->is_unsigned())
8604 {
8605 static tree print_uint64_fndecl;
8606 pfndecl = &print_uint64_fndecl;
8607 fnname = "__go_print_uint64";
8608 Type* itype = Type::lookup_integer_type("uint64");
5b735706 8609 Btype* bitype = itype->get_backend(gogo);
8afa2bfb
SD
8610 arg = fold_convert_loc(location.gcc_location(),
8611 type_to_tree(bitype), arg);
7a938933
ILT
8612 }
8613 else if (type->integer_type() != NULL)
8614 {
8615 static tree print_int64_fndecl;
8616 pfndecl = &print_int64_fndecl;
8617 fnname = "__go_print_int64";
8618 Type* itype = Type::lookup_integer_type("int64");
5b735706 8619 Btype* bitype = itype->get_backend(gogo);
8afa2bfb
SD
8620 arg = fold_convert_loc(location.gcc_location(),
8621 type_to_tree(bitype), arg);
7a938933
ILT
8622 }
8623 else if (type->float_type() != NULL)
8624 {
8625 static tree print_double_fndecl;
8626 pfndecl = &print_double_fndecl;
8627 fnname = "__go_print_double";
8afa2bfb
SD
8628 arg = fold_convert_loc(location.gcc_location(),
8629 double_type_node, arg);
7a938933
ILT
8630 }
8631 else if (type->complex_type() != NULL)
8632 {
8633 static tree print_complex_fndecl;
8634 pfndecl = &print_complex_fndecl;
8635 fnname = "__go_print_complex";
8afa2bfb
SD
8636 arg = fold_convert_loc(location.gcc_location(),
8637 complex_double_type_node, arg);
7a938933
ILT
8638 }
8639 else if (type->is_boolean_type())
8640 {
8641 static tree print_bool_fndecl;
8642 pfndecl = &print_bool_fndecl;
8643 fnname = "__go_print_bool";
8644 }
8645 else if (type->points_to() != NULL
8646 || type->channel_type() != NULL
8647 || type->map_type() != NULL
8648 || type->function_type() != NULL)
8649 {
8650 static tree print_pointer_fndecl;
8651 pfndecl = &print_pointer_fndecl;
8652 fnname = "__go_print_pointer";
8afa2bfb
SD
8653 arg = fold_convert_loc(location.gcc_location(),
8654 ptr_type_node, arg);
7a938933
ILT
8655 }
8656 else if (type->interface_type() != NULL)
8657 {
8658 if (type->interface_type()->is_empty())
8659 {
8660 static tree print_empty_interface_fndecl;
8661 pfndecl = &print_empty_interface_fndecl;
8662 fnname = "__go_print_empty_interface";
8663 }
8664 else
8665 {
8666 static tree print_interface_fndecl;
8667 pfndecl = &print_interface_fndecl;
8668 fnname = "__go_print_interface";
8669 }
8670 }
b7190f2f 8671 else if (type->is_slice_type())
7a938933
ILT
8672 {
8673 static tree print_slice_fndecl;
8674 pfndecl = &print_slice_fndecl;
8675 fnname = "__go_print_slice";
8676 }
8677 else
9b83b6d2
ILT
8678 {
8679 go_assert(saw_errors());
8680 return error_mark_node;
8681 }
7a938933
ILT
8682
8683 tree call = Gogo::call_builtin(pfndecl,
8684 location,
8685 fnname,
8686 1,
8687 void_type_node,
8688 TREE_TYPE(arg),
8689 arg);
faff9b04
ILT
8690 if (call == error_mark_node)
8691 return error_mark_node;
8692 append_to_statement_list(call, &stmt_list);
7a938933
ILT
8693 }
8694 }
8695
8696 if (is_ln)
8697 {
8698 static tree print_nl_fndecl;
8699 tree call = Gogo::call_builtin(&print_nl_fndecl,
8700 location,
8701 "__go_print_nl",
8702 0,
8703 void_type_node);
faff9b04
ILT
8704 if (call == error_mark_node)
8705 return error_mark_node;
7a938933
ILT
8706 append_to_statement_list(call, &stmt_list);
8707 }
8708
8709 return stmt_list;
8710 }
8711
8712 case BUILTIN_PANIC:
8713 {
8714 const Expression_list* args = this->args();
26409c52 8715 go_assert(args != NULL && args->size() == 1);
7a938933
ILT
8716 Expression* arg = args->front();
8717 tree arg_tree = arg->get_tree(context);
8718 if (arg_tree == error_mark_node)
8719 return error_mark_node;
8afa2bfb 8720 Type *empty =
7c0434e5 8721 Type::make_empty_interface_type(Linemap::predeclared_location());
7a938933
ILT
8722 arg_tree = Expression::convert_for_assignment(context, empty,
8723 arg->type(),
8724 arg_tree, location);
8725 static tree panic_fndecl;
8726 tree call = Gogo::call_builtin(&panic_fndecl,
8727 location,
8728 "__go_panic",
8729 1,
8730 void_type_node,
8731 TREE_TYPE(arg_tree),
8732 arg_tree);
faff9b04
ILT
8733 if (call == error_mark_node)
8734 return error_mark_node;
7a938933
ILT
8735 // This function will throw an exception.
8736 TREE_NOTHROW(panic_fndecl) = 0;
8737 // This function will not return.
8738 TREE_THIS_VOLATILE(panic_fndecl) = 1;
8739 return call;
8740 }
8741
8742 case BUILTIN_RECOVER:
8743 {
8744 // The argument is set when building recover thunks. It's a
8745 // boolean value which is true if we can recover a value now.
8746 const Expression_list* args = this->args();
26409c52 8747 go_assert(args != NULL && args->size() == 1);
7a938933
ILT
8748 Expression* arg = args->front();
8749 tree arg_tree = arg->get_tree(context);
8750 if (arg_tree == error_mark_node)
8751 return error_mark_node;
8752
8afa2bfb 8753 Type *empty =
7c0434e5 8754 Type::make_empty_interface_type(Linemap::predeclared_location());
5b735706 8755 tree empty_tree = type_to_tree(empty->get_backend(context->gogo()));
7a938933
ILT
8756
8757 Type* nil_type = Type::make_nil_type();
8758 Expression* nil = Expression::make_nil(location);
8759 tree nil_tree = nil->get_tree(context);
8760 tree empty_nil_tree = Expression::convert_for_assignment(context,
8761 empty,
8762 nil_type,
8763 nil_tree,
8764 location);
8765
8766 // We need to handle a deferred call to recover specially,
8767 // because it changes whether it can recover a panic or not.
8768 // See test7 in test/recover1.go.
8769 tree call;
8770 if (this->is_deferred())
8771 {
8772 static tree deferred_recover_fndecl;
8773 call = Gogo::call_builtin(&deferred_recover_fndecl,
8774 location,
8775 "__go_deferred_recover",
8776 0,
8777 empty_tree);
8778 }
8779 else
8780 {
8781 static tree recover_fndecl;
8782 call = Gogo::call_builtin(&recover_fndecl,
8783 location,
8784 "__go_recover",
8785 0,
8786 empty_tree);
8787 }
faff9b04
ILT
8788 if (call == error_mark_node)
8789 return error_mark_node;
8afa2bfb
SD
8790 return fold_build3_loc(location.gcc_location(), COND_EXPR, empty_tree,
8791 arg_tree, call, empty_nil_tree);
7a938933
ILT
8792 }
8793
8794 case BUILTIN_CLOSE:
7a938933
ILT
8795 {
8796 const Expression_list* args = this->args();
26409c52 8797 go_assert(args != NULL && args->size() == 1);
7a938933
ILT
8798 Expression* arg = args->front();
8799 tree arg_tree = arg->get_tree(context);
8800 if (arg_tree == error_mark_node)
8801 return error_mark_node;
fe052134
ILT
8802 static tree close_fndecl;
8803 return Gogo::call_builtin(&close_fndecl,
8804 location,
8805 "__go_builtin_close",
8806 1,
8807 void_type_node,
8808 TREE_TYPE(arg_tree),
8809 arg_tree);
7a938933
ILT
8810 }
8811
8812 case BUILTIN_SIZEOF:
8813 case BUILTIN_OFFSETOF:
8814 case BUILTIN_ALIGNOF:
8815 {
5caf63ca
ILT
8816 Numeric_constant nc;
8817 unsigned long val;
8818 if (!this->numeric_constant_value(&nc)
8819 || nc.to_unsigned_long(&val) != Numeric_constant::NC_UL_VALID)
f315816c 8820 {
26409c52 8821 go_assert(saw_errors());
f315816c
ILT
8822 return error_mark_node;
8823 }
a0c8ad3b
ILT
8824 Type* uintptr_type = Type::lookup_integer_type("uintptr");
8825 tree type = type_to_tree(uintptr_type->get_backend(gogo));
5caf63ca 8826 return build_int_cst(type, val);
7a938933
ILT
8827 }
8828
8829 case BUILTIN_COPY:
8830 {
8831 const Expression_list* args = this->args();
26409c52 8832 go_assert(args != NULL && args->size() == 2);
7a938933
ILT
8833 Expression* arg1 = args->front();
8834 Expression* arg2 = args->back();
8835
8836 tree arg1_tree = arg1->get_tree(context);
8837 tree arg2_tree = arg2->get_tree(context);
8838 if (arg1_tree == error_mark_node || arg2_tree == error_mark_node)
8839 return error_mark_node;
8840
8841 Type* arg1_type = arg1->type();
8842 Array_type* at = arg1_type->array_type();
8843 arg1_tree = save_expr(arg1_tree);
8844 tree arg1_val = at->value_pointer_tree(gogo, arg1_tree);
8845 tree arg1_len = at->length_tree(gogo, arg1_tree);
89ca9e60
ILT
8846 if (arg1_val == error_mark_node || arg1_len == error_mark_node)
8847 return error_mark_node;
7a938933
ILT
8848
8849 Type* arg2_type = arg2->type();
8850 tree arg2_val;
8851 tree arg2_len;
b7190f2f 8852 if (arg2_type->is_slice_type())
7a938933
ILT
8853 {
8854 at = arg2_type->array_type();
8855 arg2_tree = save_expr(arg2_tree);
8856 arg2_val = at->value_pointer_tree(gogo, arg2_tree);
8857 arg2_len = at->length_tree(gogo, arg2_tree);
8858 }
8859 else
8860 {
8861 arg2_tree = save_expr(arg2_tree);
8862 arg2_val = String_type::bytes_tree(gogo, arg2_tree);
8863 arg2_len = String_type::length_tree(gogo, arg2_tree);
8864 }
89ca9e60
ILT
8865 if (arg2_val == error_mark_node || arg2_len == error_mark_node)
8866 return error_mark_node;
7a938933
ILT
8867
8868 arg1_len = save_expr(arg1_len);
8869 arg2_len = save_expr(arg2_len);
8afa2bfb
SD
8870 tree len = fold_build3_loc(location.gcc_location(), COND_EXPR,
8871 TREE_TYPE(arg1_len),
8872 fold_build2_loc(location.gcc_location(),
8873 LT_EXPR, boolean_type_node,
7a938933
ILT
8874 arg1_len, arg2_len),
8875 arg1_len, arg2_len);
8876 len = save_expr(len);
8877
8878 Type* element_type = at->element_type();
5b735706
ILT
8879 Btype* element_btype = element_type->get_backend(gogo);
8880 tree element_type_tree = type_to_tree(element_btype);
89ca9e60
ILT
8881 if (element_type_tree == error_mark_node)
8882 return error_mark_node;
7a938933 8883 tree element_size = TYPE_SIZE_UNIT(element_type_tree);
8afa2bfb
SD
8884 tree bytecount = fold_convert_loc(location.gcc_location(),
8885 TREE_TYPE(element_size), len);
8886 bytecount = fold_build2_loc(location.gcc_location(), MULT_EXPR,
7a938933
ILT
8887 TREE_TYPE(element_size),
8888 bytecount, element_size);
8afa2bfb
SD
8889 bytecount = fold_convert_loc(location.gcc_location(), size_type_node,
8890 bytecount);
7a938933 8891
8afa2bfb
SD
8892 arg1_val = fold_convert_loc(location.gcc_location(), ptr_type_node,
8893 arg1_val);
8894 arg2_val = fold_convert_loc(location.gcc_location(), ptr_type_node,
8895 arg2_val);
b5343013
ILT
8896
8897 static tree copy_fndecl;
8898 tree call = Gogo::call_builtin(&copy_fndecl,
8899 location,
8900 "__go_copy",
8901 3,
8902 void_type_node,
8903 ptr_type_node,
8904 arg1_val,
8905 ptr_type_node,
8906 arg2_val,
8907 size_type_node,
8908 bytecount);
8909 if (call == error_mark_node)
8910 return error_mark_node;
7a938933 8911
8afa2bfb
SD
8912 return fold_build2_loc(location.gcc_location(), COMPOUND_EXPR,
8913 TREE_TYPE(len), call, len);
7a938933
ILT
8914 }
8915
8916 case BUILTIN_APPEND:
8917 {
8918 const Expression_list* args = this->args();
26409c52 8919 go_assert(args != NULL && args->size() == 2);
7a938933
ILT
8920 Expression* arg1 = args->front();
8921 Expression* arg2 = args->back();
8922
8923 tree arg1_tree = arg1->get_tree(context);
8924 tree arg2_tree = arg2->get_tree(context);
8925 if (arg1_tree == error_mark_node || arg2_tree == error_mark_node)
8926 return error_mark_node;
8927
c7f15f80 8928 Array_type* at = arg1->type()->array_type();
a1ee0aaf 8929 Type* element_type = at->element_type()->forwarded();
c7f15f80 8930
a1ee0aaf
ILT
8931 tree arg2_val;
8932 tree arg2_len;
8933 tree element_size;
8934 if (arg2->type()->is_string_type()
7b31a84d
ILT
8935 && element_type->integer_type() != NULL
8936 && element_type->integer_type()->is_byte())
a1ee0aaf
ILT
8937 {
8938 arg2_tree = save_expr(arg2_tree);
8939 arg2_val = String_type::bytes_tree(gogo, arg2_tree);
8940 arg2_len = String_type::length_tree(gogo, arg2_tree);
8941 element_size = size_int(1);
8942 }
8943 else
8944 {
8945 arg2_tree = Expression::convert_for_assignment(context, at,
8946 arg2->type(),
8947 arg2_tree,
8948 location);
8949 if (arg2_tree == error_mark_node)
8950 return error_mark_node;
8951
8952 arg2_tree = save_expr(arg2_tree);
8953
8954 arg2_val = at->value_pointer_tree(gogo, arg2_tree);
8955 arg2_len = at->length_tree(gogo, arg2_tree);
8956
8957 Btype* element_btype = element_type->get_backend(gogo);
8958 tree element_type_tree = type_to_tree(element_btype);
8959 if (element_type_tree == error_mark_node)
8960 return error_mark_node;
8961 element_size = TYPE_SIZE_UNIT(element_type_tree);
8962 }
476e904f 8963
8afa2bfb
SD
8964 arg2_val = fold_convert_loc(location.gcc_location(), ptr_type_node,
8965 arg2_val);
8966 arg2_len = fold_convert_loc(location.gcc_location(), size_type_node,
8967 arg2_len);
8968 element_size = fold_convert_loc(location.gcc_location(), size_type_node,
b5343013 8969 element_size);
7a938933 8970
a1ee0aaf
ILT
8971 if (arg2_val == error_mark_node
8972 || arg2_len == error_mark_node
8973 || element_size == error_mark_node)
8974 return error_mark_node;
8975
7a938933
ILT
8976 // We rebuild the decl each time since the slice types may
8977 // change.
8978 tree append_fndecl = NULL_TREE;
8979 return Gogo::call_builtin(&append_fndecl,
8980 location,
8981 "__go_append",
b5343013 8982 4,
7a938933 8983 TREE_TYPE(arg1_tree),
7a938933
ILT
8984 TREE_TYPE(arg1_tree),
8985 arg1_tree,
b5343013
ILT
8986 ptr_type_node,
8987 arg2_val,
8988 size_type_node,
8989 arg2_len,
8990 size_type_node,
8991 element_size);
7a938933
ILT
8992 }
8993
8994 case BUILTIN_REAL:
8995 case BUILTIN_IMAG:
8996 {
8997 const Expression_list* args = this->args();
26409c52 8998 go_assert(args != NULL && args->size() == 1);
7a938933
ILT
8999 Expression* arg = args->front();
9000 tree arg_tree = arg->get_tree(context);
9001 if (arg_tree == error_mark_node)
9002 return error_mark_node;
26409c52 9003 go_assert(COMPLEX_FLOAT_TYPE_P(TREE_TYPE(arg_tree)));
7a938933 9004 if (this->code_ == BUILTIN_REAL)
8afa2bfb 9005 return fold_build1_loc(location.gcc_location(), REALPART_EXPR,
7a938933
ILT
9006 TREE_TYPE(TREE_TYPE(arg_tree)),
9007 arg_tree);
9008 else
8afa2bfb 9009 return fold_build1_loc(location.gcc_location(), IMAGPART_EXPR,
7a938933
ILT
9010 TREE_TYPE(TREE_TYPE(arg_tree)),
9011 arg_tree);
9012 }
9013
ff5f50c5 9014 case BUILTIN_COMPLEX:
7a938933
ILT
9015 {
9016 const Expression_list* args = this->args();
26409c52 9017 go_assert(args != NULL && args->size() == 2);
7a938933
ILT
9018 tree r = args->front()->get_tree(context);
9019 tree i = args->back()->get_tree(context);
9020 if (r == error_mark_node || i == error_mark_node)
9021 return error_mark_node;
26409c52 9022 go_assert(TYPE_MAIN_VARIANT(TREE_TYPE(r))
7a938933 9023 == TYPE_MAIN_VARIANT(TREE_TYPE(i)));
26409c52 9024 go_assert(SCALAR_FLOAT_TYPE_P(TREE_TYPE(r)));
8afa2bfb 9025 return fold_build2_loc(location.gcc_location(), COMPLEX_EXPR,
7a938933
ILT
9026 build_complex_type(TREE_TYPE(r)),
9027 r, i);
9028 }
9029
9030 default:
8c0d1865 9031 go_unreachable();
7a938933
ILT
9032 }
9033}
9034
9035// We have to support exporting a builtin call expression, because
9036// code can set a constant to the result of a builtin expression.
9037
9038void
9039Builtin_call_expression::do_export(Export* exp) const
9040{
5caf63ca
ILT
9041 Numeric_constant nc;
9042 if (!this->numeric_constant_value(&nc))
9043 {
9044 error_at(this->location(), "value is not constant");
9045 return;
9046 }
7a938933 9047
5caf63ca 9048 if (nc.is_int())
7a938933 9049 {
5caf63ca
ILT
9050 mpz_t val;
9051 nc.get_int(&val);
7a938933 9052 Integer_expression::export_integer(exp, val);
5caf63ca 9053 mpz_clear(val);
7a938933 9054 }
5caf63ca 9055 else if (nc.is_float())
7a938933
ILT
9056 {
9057 mpfr_t fval;
5caf63ca
ILT
9058 nc.get_float(&fval);
9059 Float_expression::export_float(exp, fval);
7a938933
ILT
9060 mpfr_clear(fval);
9061 }
5caf63ca 9062 else if (nc.is_complex())
7a938933
ILT
9063 {
9064 mpfr_t real;
9065 mpfr_t imag;
5caf63ca 9066 Complex_expression::export_complex(exp, real, imag);
7a938933
ILT
9067 mpfr_clear(real);
9068 mpfr_clear(imag);
9069 }
5caf63ca
ILT
9070 else
9071 go_unreachable();
7a938933
ILT
9072
9073 // A trailing space lets us reliably identify the end of the number.
9074 exp->write_c_string(" ");
9075}
9076
9077// Class Call_expression.
9078
fdbc38a6
ILT
9079// A Go function can be viewed in a couple of different ways. The
9080// code of a Go function becomes a backend function with parameters
9081// whose types are simply the backend representation of the Go types.
9082// If there are multiple results, they are returned as a backend
9083// struct.
9084
9085// However, when Go code refers to a function other than simply
9086// calling it, the backend type of that function is actually a struct.
9087// The first field of the struct points to the Go function code
9088// (sometimes a wrapper as described below). The remaining fields
9089// hold addresses of closed-over variables. This struct is called a
9090// closure.
9091
9092// There are a few cases to consider.
9093
9094// A direct function call of a known function in package scope. In
9095// this case there are no closed-over variables, and we know the name
9096// of the function code. We can simply produce a backend call to the
9097// function directly, and not worry about the closure.
9098
9099// A direct function call of a known function literal. In this case
9100// we know the function code and we know the closure. We generate the
9101// function code such that it expects an additional final argument of
9102// the closure type. We pass the closure as the last argument, after
9103// the other arguments.
9104
9105// An indirect function call. In this case we have a closure. We
9106// load the pointer to the function code from the first field of the
9107// closure. We pass the address of the closure as the last argument.
9108
9109// A call to a method of an interface. Type methods are always at
9110// package scope, so we call the function directly, and don't worry
9111// about the closure.
9112
9113// This means that for a function at package scope we have two cases.
9114// One is the direct call, which has no closure. The other is the
9115// indirect call, which does have a closure. We can't simply ignore
9116// the closure, even though it is the last argument, because that will
9117// fail on targets where the function pops its arguments. So when
9118// generating a closure for a package-scope function we set the
9119// function code pointer in the closure to point to a wrapper
9120// function. This wrapper function accepts a final argument that
9121// points to the closure, ignores it, and calls the real function as a
9122// direct function call. This wrapper will normally be efficient, and
9123// can often simply be a tail call to the real function.
9124
9125// We don't use GCC's static chain pointer because 1) we don't need
9126// it; 2) GCC only permits using a static chain to call a known
9127// function, so we can't use it for an indirect call anyhow. Since we
9128// can't use it for an indirect call, we may as well not worry about
9129// using it for a direct call either.
9130
9131// We pass the closure last rather than first because it means that
9132// the function wrapper we put into a closure for a package-scope
9133// function can normally just be a tail call to the real function.
9134
9135// For method expressions we generate a wrapper that loads the
9136// receiver from the closure and then calls the method. This
9137// unfortunately forces reshuffling the arguments, since there is a
9138// new first argument, but we can't avoid reshuffling either for
9139// method expressions or for indirect calls of package-scope
9140// functions, and since the latter are more common we reshuffle for
9141// method expressions.
9142
9143// Note that the Go code retains the Go types. The extra final
9144// argument only appears when we convert to the backend
9145// representation.
9146
7a938933
ILT
9147// Traversal.
9148
9149int
9150Call_expression::do_traverse(Traverse* traverse)
9151{
9152 if (Expression::traverse(&this->fn_, traverse) == TRAVERSE_EXIT)
9153 return TRAVERSE_EXIT;
9154 if (this->args_ != NULL)
9155 {
9156 if (this->args_->traverse(traverse) == TRAVERSE_EXIT)
9157 return TRAVERSE_EXIT;
9158 }
9159 return TRAVERSE_CONTINUE;
9160}
9161
9162// Lower a call statement.
9163
9164Expression*
8586635c
ILT
9165Call_expression::do_lower(Gogo* gogo, Named_object* function,
9166 Statement_inserter* inserter, int)
7a938933 9167{
8afa2bfb 9168 Location loc = this->location();
1bbf7edb 9169
8586635c 9170 // A type cast can look like a function call.
7a938933
ILT
9171 if (this->fn_->is_type_expression()
9172 && this->args_ != NULL
9173 && this->args_->size() == 1)
9174 return Expression::make_cast(this->fn_->type(), this->args_->front(),
1bbf7edb 9175 loc);
7a938933 9176
d59a4c82
ILT
9177 // Because do_type will return an error type and thus prevent future
9178 // errors, check for that case now to ensure that the error gets
9179 // reported.
7bacbe5c
ILT
9180 Function_type* fntype = this->get_function_type();
9181 if (fntype == NULL)
d59a4c82
ILT
9182 {
9183 if (!this->fn_->type()->is_error())
9184 this->report_error(_("expected function"));
9185 return Expression::make_error(loc);
9186 }
9187
7a938933
ILT
9188 // Handle an argument which is a call to a function which returns
9189 // multiple results.
9190 if (this->args_ != NULL
9191 && this->args_->size() == 1
7bacbe5c 9192 && this->args_->front()->call_expression() != NULL)
7a938933 9193 {
7a938933
ILT
9194 size_t rc = this->args_->front()->call_expression()->result_count();
9195 if (rc > 1
7bacbe5c
ILT
9196 && ((fntype->parameters() != NULL
9197 && (fntype->parameters()->size() == rc
9198 || (fntype->is_varargs()
9199 && fntype->parameters()->size() - 1 <= rc)))
9200 || fntype->is_builtin()))
7a938933
ILT
9201 {
9202 Call_expression* call = this->args_->front()->call_expression();
9203 Expression_list* args = new Expression_list;
9204 for (size_t i = 0; i < rc; ++i)
9205 args->push_back(Expression::make_call_result(call, i));
9206 // We can't return a new call expression here, because this
01bd5703
ILT
9207 // one may be referenced by Call_result expressions. We
9208 // also can't delete the old arguments, because we may still
9209 // traverse them somewhere up the call stack. FIXME.
7a938933
ILT
9210 this->args_ = args;
9211 }
9212 }
9213
7bacbe5c
ILT
9214 // Recognize a call to a builtin function.
9215 if (fntype->is_builtin())
9216 return new Builtin_call_expression(gogo, this->fn_, this->args_,
9217 this->is_varargs_, loc);
9218
8586635c
ILT
9219 // If this call returns multiple results, create a temporary
9220 // variable for each result.
9221 size_t rc = this->result_count();
9222 if (rc > 1 && this->results_ == NULL)
9223 {
9224 std::vector<Temporary_statement*>* temps =
9225 new std::vector<Temporary_statement*>;
9226 temps->reserve(rc);
7bacbe5c 9227 const Typed_identifier_list* results = fntype->results();
8586635c
ILT
9228 for (Typed_identifier_list::const_iterator p = results->begin();
9229 p != results->end();
9230 ++p)
9231 {
9232 Temporary_statement* temp = Statement::make_temporary(p->type(),
1bbf7edb 9233 NULL, loc);
8586635c
ILT
9234 inserter->insert(temp);
9235 temps->push_back(temp);
9236 }
9237 this->results_ = temps;
9238 }
9239
7a938933
ILT
9240 // Handle a call to a varargs function by packaging up the extra
9241 // parameters.
7bacbe5c 9242 if (fntype->is_varargs())
7a938933 9243 {
7a938933 9244 const Typed_identifier_list* parameters = fntype->parameters();
26409c52 9245 go_assert(parameters != NULL && !parameters->empty());
7a938933 9246 Type* varargs_type = parameters->back().type();
1bbf7edb
ILT
9247 this->lower_varargs(gogo, function, inserter, varargs_type,
9248 parameters->size());
9249 }
9250
9251 // If this is call to a method, call the method directly passing the
9252 // object as the first parameter.
9253 Bound_method_expression* bme = this->fn_->bound_method_expression();
9254 if (bme != NULL)
9255 {
571d3f91 9256 Named_object* methodfn = bme->function();
1bbf7edb
ILT
9257 Expression* first_arg = bme->first_argument();
9258
9259 // We always pass a pointer when calling a method.
9260 if (first_arg->type()->points_to() == NULL
9261 && !first_arg->type()->is_error())
9262 {
9263 first_arg = Expression::make_unary(OPERATOR_AND, first_arg, loc);
9264 // We may need to create a temporary variable so that we can
9265 // take the address. We can't do that here because it will
9266 // mess up the order of evaluation.
9267 Unary_expression* ue = static_cast<Unary_expression*>(first_arg);
9268 ue->set_create_temp();
9269 }
9270
9271 // If we are calling a method which was inherited from an
9272 // embedded struct, and the method did not get a stub, then the
9273 // first type may be wrong.
9274 Type* fatype = bme->first_argument_type();
9275 if (fatype != NULL)
9276 {
9277 if (fatype->points_to() == NULL)
9278 fatype = Type::make_pointer_type(fatype);
9279 first_arg = Expression::make_unsafe_cast(fatype, first_arg, loc);
9280 }
9281
9282 Expression_list* new_args = new Expression_list();
9283 new_args->push_back(first_arg);
9284 if (this->args_ != NULL)
9285 {
9286 for (Expression_list::const_iterator p = this->args_->begin();
9287 p != this->args_->end();
9288 ++p)
9289 new_args->push_back(*p);
9290 }
9291
9292 // We have to change in place because this structure may be
9293 // referenced by Call_result_expressions. We can't delete the
9294 // old arguments, because we may be traversing them up in some
9295 // caller. FIXME.
9296 this->args_ = new_args;
571d3f91 9297 this->fn_ = Expression::make_func_reference(methodfn, NULL,
1bbf7edb 9298 bme->location());
7a938933
ILT
9299 }
9300
9301 return this;
9302}
9303
9304// Lower a call to a varargs function. FUNCTION is the function in
9305// which the call occurs--it's not the function we are calling.
9306// VARARGS_TYPE is the type of the varargs parameter, a slice type.
9307// PARAM_COUNT is the number of parameters of the function we are
9308// calling; the last of these parameters will be the varargs
9309// parameter.
9310
1bbf7edb 9311void
7a938933 9312Call_expression::lower_varargs(Gogo* gogo, Named_object* function,
8586635c 9313 Statement_inserter* inserter,
7a938933
ILT
9314 Type* varargs_type, size_t param_count)
9315{
9316 if (this->varargs_are_lowered_)
1bbf7edb 9317 return;
7a938933 9318
8afa2bfb 9319 Location loc = this->location();
7a938933 9320
26409c52 9321 go_assert(param_count > 0);
b7190f2f 9322 go_assert(varargs_type->is_slice_type());
7a938933
ILT
9323
9324 size_t arg_count = this->args_ == NULL ? 0 : this->args_->size();
9325 if (arg_count < param_count - 1)
9326 {
9327 // Not enough arguments; will be caught in check_types.
1bbf7edb 9328 return;
7a938933
ILT
9329 }
9330
9331 Expression_list* old_args = this->args_;
9332 Expression_list* new_args = new Expression_list();
9333 bool push_empty_arg = false;
9334 if (old_args == NULL || old_args->empty())
9335 {
26409c52 9336 go_assert(param_count == 1);
7a938933
ILT
9337 push_empty_arg = true;
9338 }
9339 else
9340 {
9341 Expression_list::const_iterator pa;
9342 int i = 1;
9343 for (pa = old_args->begin(); pa != old_args->end(); ++pa, ++i)
9344 {
9345 if (static_cast<size_t>(i) == param_count)
9346 break;
9347 new_args->push_back(*pa);
9348 }
9349
9350 // We have reached the varargs parameter.
9351
9352 bool issued_error = false;
9353 if (pa == old_args->end())
9354 push_empty_arg = true;
9355 else if (pa + 1 == old_args->end() && this->is_varargs_)
9356 new_args->push_back(*pa);
9357 else if (this->is_varargs_)
9358 {
234bdd5b
ILT
9359 if ((*pa)->type()->is_slice_type())
9360 this->report_error(_("too many arguments"));
9361 else
9362 {
9363 error_at(this->location(),
9364 _("invalid use of %<...%> with non-slice"));
9365 this->set_is_error();
9366 }
1bbf7edb 9367 return;
7a938933 9368 }
7a938933
ILT
9369 else
9370 {
9371 Type* element_type = varargs_type->array_type()->element_type();
9372 Expression_list* vals = new Expression_list;
9373 for (; pa != old_args->end(); ++pa, ++i)
9374 {
9375 // Check types here so that we get a better message.
9376 Type* patype = (*pa)->type();
8afa2bfb 9377 Location paloc = (*pa)->location();
7a938933
ILT
9378 if (!this->check_argument_type(i, element_type, patype,
9379 paloc, issued_error))
9380 continue;
9381 vals->push_back(*pa);
9382 }
9383 Expression* val =
9384 Expression::make_slice_composite_literal(varargs_type, vals, loc);
1bbf7edb 9385 gogo->lower_expression(function, inserter, &val);
7a938933
ILT
9386 new_args->push_back(val);
9387 }
9388 }
9389
9390 if (push_empty_arg)
9391 new_args->push_back(Expression::make_nil(loc));
9392
9393 // We can't return a new call expression here, because this one may
2d8f63a1
ILT
9394 // be referenced by Call_result expressions. FIXME. We can't
9395 // delete OLD_ARGS because we may have both a Call_expression and a
9396 // Builtin_call_expression which refer to them. FIXME.
7a938933
ILT
9397 this->args_ = new_args;
9398 this->varargs_are_lowered_ = true;
7a938933
ILT
9399}
9400
8586635c 9401// Get the function type. This can return NULL in error cases.
7a938933
ILT
9402
9403Function_type*
9404Call_expression::get_function_type() const
9405{
9406 return this->fn_->type()->function_type();
9407}
9408
9409// Return the number of values which this call will return.
9410
9411size_t
9412Call_expression::result_count() const
9413{
9414 const Function_type* fntype = this->get_function_type();
9415 if (fntype == NULL)
9416 return 0;
9417 if (fntype->results() == NULL)
9418 return 0;
9419 return fntype->results()->size();
9420}
9421
8586635c
ILT
9422// Return the temporary which holds a result.
9423
9424Temporary_statement*
9425Call_expression::result(size_t i) const
9426{
9b83b6d2
ILT
9427 if (this->results_ == NULL || this->results_->size() <= i)
9428 {
9429 go_assert(saw_errors());
9430 return NULL;
9431 }
8586635c
ILT
9432 return (*this->results_)[i];
9433}
9434
7a938933
ILT
9435// Return whether this is a call to the predeclared function recover.
9436
9437bool
9438Call_expression::is_recover_call() const
9439{
9440 return this->do_is_recover_call();
9441}
9442
9443// Set the argument to the recover function.
9444
9445void
9446Call_expression::set_recover_arg(Expression* arg)
9447{
9448 this->do_set_recover_arg(arg);
9449}
9450
9451// Virtual functions also implemented by Builtin_call_expression.
9452
9453bool
9454Call_expression::do_is_recover_call() const
9455{
9456 return false;
9457}
9458
9459void
9460Call_expression::do_set_recover_arg(Expression*)
9461{
8c0d1865 9462 go_unreachable();
7a938933
ILT
9463}
9464
8586635c
ILT
9465// We have found an error with this call expression; return true if
9466// we should report it.
9467
9468bool
9469Call_expression::issue_error()
9470{
9471 if (this->issued_error_)
9472 return false;
9473 else
9474 {
9475 this->issued_error_ = true;
9476 return true;
9477 }
9478}
9479
7a938933
ILT
9480// Get the type.
9481
9482Type*
9483Call_expression::do_type()
9484{
9485 if (this->type_ != NULL)
9486 return this->type_;
9487
9488 Type* ret;
9489 Function_type* fntype = this->get_function_type();
9490 if (fntype == NULL)
9491 return Type::make_error_type();
9492
9493 const Typed_identifier_list* results = fntype->results();
9494 if (results == NULL)
9495 ret = Type::make_void_type();
9496 else if (results->size() == 1)
9497 ret = results->begin()->type();
9498 else
9499 ret = Type::make_call_multiple_result_type(this);
9500
9501 this->type_ = ret;
9502
9503 return this->type_;
9504}
9505
9506// Determine types for a call expression. We can use the function
9507// parameter types to set the types of the arguments.
9508
9509void
9510Call_expression::do_determine_type(const Type_context*)
9511{
20532210
ILT
9512 if (!this->determining_types())
9513 return;
9514
7a938933
ILT
9515 this->fn_->determine_type_no_context();
9516 Function_type* fntype = this->get_function_type();
9517 const Typed_identifier_list* parameters = NULL;
9518 if (fntype != NULL)
9519 parameters = fntype->parameters();
9520 if (this->args_ != NULL)
9521 {
9522 Typed_identifier_list::const_iterator pt;
9523 if (parameters != NULL)
9524 pt = parameters->begin();
1bbf7edb 9525 bool first = true;
7a938933
ILT
9526 for (Expression_list::const_iterator pa = this->args_->begin();
9527 pa != this->args_->end();
9528 ++pa)
9529 {
1bbf7edb
ILT
9530 if (first)
9531 {
9532 first = false;
9533 // If this is a method, the first argument is the
9534 // receiver.
9535 if (fntype != NULL && fntype->is_method())
9536 {
9537 Type* rtype = fntype->receiver()->type();
9538 // The receiver is always passed as a pointer.
9539 if (rtype->points_to() == NULL)
9540 rtype = Type::make_pointer_type(rtype);
9541 Type_context subcontext(rtype, false);
9542 (*pa)->determine_type(&subcontext);
9543 continue;
9544 }
9545 }
9546
7a938933
ILT
9547 if (parameters != NULL && pt != parameters->end())
9548 {
9549 Type_context subcontext(pt->type(), false);
9550 (*pa)->determine_type(&subcontext);
9551 ++pt;
9552 }
9553 else
9554 (*pa)->determine_type_no_context();
9555 }
9556 }
9557}
9558
20532210
ILT
9559// Called when determining types for a Call_expression. Return true
9560// if we should go ahead, false if they have already been determined.
9561
9562bool
9563Call_expression::determining_types()
9564{
9565 if (this->types_are_determined_)
9566 return false;
9567 else
9568 {
9569 this->types_are_determined_ = true;
9570 return true;
9571 }
9572}
9573
7a938933
ILT
9574// Check types for parameter I.
9575
9576bool
9577Call_expression::check_argument_type(int i, const Type* parameter_type,
9578 const Type* argument_type,
8afa2bfb 9579 Location argument_location,
7a938933
ILT
9580 bool issued_error)
9581{
9582 std::string reason;
d9930d55
ILT
9583 bool ok;
9584 if (this->are_hidden_fields_ok_)
9585 ok = Type::are_assignable_hidden_ok(parameter_type, argument_type,
9586 &reason);
9587 else
9588 ok = Type::are_assignable(parameter_type, argument_type, &reason);
9589 if (!ok)
7a938933
ILT
9590 {
9591 if (!issued_error)
9592 {
9593 if (reason.empty())
9594 error_at(argument_location, "argument %d has incompatible type", i);
9595 else
9596 error_at(argument_location,
9597 "argument %d has incompatible type (%s)",
9598 i, reason.c_str());
9599 }
9600 this->set_is_error();
9601 return false;
9602 }
9603 return true;
9604}
9605
9606// Check types.
9607
9608void
9609Call_expression::do_check_types(Gogo*)
9610{
234bdd5b
ILT
9611 if (this->classification() == EXPRESSION_ERROR)
9612 return;
9613
7a938933
ILT
9614 Function_type* fntype = this->get_function_type();
9615 if (fntype == NULL)
9616 {
02ed921a 9617 if (!this->fn_->type()->is_error())
7a938933
ILT
9618 this->report_error(_("expected function"));
9619 return;
9620 }
9621
1bbf7edb
ILT
9622 bool is_method = fntype->is_method();
9623 if (is_method)
7a938933 9624 {
1bbf7edb
ILT
9625 go_assert(this->args_ != NULL && !this->args_->empty());
9626 Type* rtype = fntype->receiver()->type();
9627 Expression* first_arg = this->args_->front();
9628 // The language permits copying hidden fields for a method
9629 // receiver. We dereference the values since receivers are
9630 // always passed as pointers.
9631 std::string reason;
9632 if (!Type::are_assignable_hidden_ok(rtype->deref(),
9633 first_arg->type()->deref(),
9634 &reason))
7a938933 9635 {
1bbf7edb
ILT
9636 if (reason.empty())
9637 this->report_error(_("incompatible type for receiver"));
9638 else
7a938933 9639 {
1bbf7edb
ILT
9640 error_at(this->location(),
9641 "incompatible type for receiver (%s)",
9642 reason.c_str());
9643 this->set_is_error();
7a938933
ILT
9644 }
9645 }
9646 }
9647
9648 // Note that varargs was handled by the lower_varargs() method, so
234bdd5b
ILT
9649 // we don't have to worry about it here unless something is wrong.
9650 if (this->is_varargs_ && !this->varargs_are_lowered_)
9651 {
9652 if (!fntype->is_varargs())
9653 {
9654 error_at(this->location(),
9655 _("invalid use of %<...%> calling non-variadic function"));
9656 this->set_is_error();
9657 return;
9658 }
9659 }
7a938933
ILT
9660
9661 const Typed_identifier_list* parameters = fntype->parameters();
9662 if (this->args_ == NULL)
9663 {
9664 if (parameters != NULL && !parameters->empty())
9665 this->report_error(_("not enough arguments"));
9666 }
9667 else if (parameters == NULL)
1bbf7edb
ILT
9668 {
9669 if (!is_method || this->args_->size() > 1)
9670 this->report_error(_("too many arguments"));
9671 }
7a938933
ILT
9672 else
9673 {
9674 int i = 0;
1bbf7edb
ILT
9675 Expression_list::const_iterator pa = this->args_->begin();
9676 if (is_method)
9677 ++pa;
9678 for (Typed_identifier_list::const_iterator pt = parameters->begin();
9679 pt != parameters->end();
9680 ++pt, ++pa, ++i)
7a938933 9681 {
1bbf7edb 9682 if (pa == this->args_->end())
7a938933 9683 {
1bbf7edb 9684 this->report_error(_("not enough arguments"));
7a938933
ILT
9685 return;
9686 }
9687 this->check_argument_type(i + 1, pt->type(), (*pa)->type(),
9688 (*pa)->location(), false);
9689 }
1bbf7edb
ILT
9690 if (pa != this->args_->end())
9691 this->report_error(_("too many arguments"));
7a938933
ILT
9692 }
9693}
9694
9695// Return whether we have to use a temporary variable to ensure that
9696// we evaluate this call expression in order. If the call returns no
8586635c 9697// results then it will inevitably be executed last.
7a938933
ILT
9698
9699bool
9700Call_expression::do_must_eval_in_order() const
9701{
8586635c 9702 return this->result_count() > 0;
7a938933
ILT
9703}
9704
7a938933
ILT
9705// Get the function and the first argument to use when calling an
9706// interface method.
9707
9708tree
9709Call_expression::interface_method_function(
9710 Translate_context* context,
9711 Interface_field_reference_expression* interface_method,
9712 tree* first_arg_ptr)
9713{
9714 tree expr = interface_method->expr()->get_tree(context);
9715 if (expr == error_mark_node)
9716 return error_mark_node;
9717 expr = save_expr(expr);
9718 tree first_arg = interface_method->get_underlying_object_tree(context, expr);
9719 if (first_arg == error_mark_node)
9720 return error_mark_node;
9721 *first_arg_ptr = first_arg;
9722 return interface_method->get_function_tree(context, expr);
9723}
9724
9725// Build the call expression.
9726
9727tree
9728Call_expression::do_get_tree(Translate_context* context)
9729{
9730 if (this->tree_ != NULL_TREE)
9731 return this->tree_;
9732
9733 Function_type* fntype = this->get_function_type();
9734 if (fntype == NULL)
9735 return error_mark_node;
9736
9737 if (this->fn_->is_error_expression())
9738 return error_mark_node;
9739
9740 Gogo* gogo = context->gogo();
8afa2bfb 9741 Location location = this->location();
7a938933
ILT
9742
9743 Func_expression* func = this->fn_->func_expression();
7a938933
ILT
9744 Interface_field_reference_expression* interface_method =
9745 this->fn_->interface_field_reference_expression();
9746 const bool has_closure = func != NULL && func->closure() != NULL;
1bbf7edb 9747 const bool is_interface_method = interface_method != NULL;
7a938933 9748
05a7d566 9749 bool has_closure_arg;
fdbc38a6 9750 if (has_closure)
05a7d566 9751 has_closure_arg = true;
fdbc38a6 9752 else if (func != NULL)
05a7d566 9753 has_closure_arg = false;
fdbc38a6 9754 else if (is_interface_method)
05a7d566 9755 has_closure_arg = false;
fdbc38a6 9756 else
05a7d566 9757 has_closure_arg = true;
fdbc38a6 9758
7a938933
ILT
9759 int nargs;
9760 tree* args;
9761 if (this->args_ == NULL || this->args_->empty())
9762 {
05a7d566 9763 nargs = is_interface_method ? 1 : 0;
7a938933
ILT
9764 args = nargs == 0 ? NULL : new tree[nargs];
9765 }
1bbf7edb
ILT
9766 else if (fntype->parameters() == NULL || fntype->parameters()->empty())
9767 {
9768 // Passing a receiver parameter.
9769 go_assert(!is_interface_method
9770 && fntype->is_method()
9771 && this->args_->size() == 1);
05a7d566 9772 nargs = 1;
1bbf7edb
ILT
9773 args = new tree[nargs];
9774 args[0] = this->args_->front()->get_tree(context);
9775 }
7a938933
ILT
9776 else
9777 {
9778 const Typed_identifier_list* params = fntype->parameters();
7a938933
ILT
9779
9780 nargs = this->args_->size();
1bbf7edb 9781 int i = is_interface_method ? 1 : 0;
7a938933
ILT
9782 nargs += i;
9783 args = new tree[nargs];
9784
9785 Typed_identifier_list::const_iterator pp = params->begin();
1bbf7edb
ILT
9786 Expression_list::const_iterator pe = this->args_->begin();
9787 if (!is_interface_method && fntype->is_method())
9788 {
9789 args[i] = (*pe)->get_tree(context);
9790 ++pe;
9791 ++i;
9792 }
9793 for (; pe != this->args_->end(); ++pe, ++pp, ++i)
7a938933 9794 {
26409c52 9795 go_assert(pp != params->end());
7a938933
ILT
9796 tree arg_val = (*pe)->get_tree(context);
9797 args[i] = Expression::convert_for_assignment(context,
9798 pp->type(),
9799 (*pe)->type(),
9800 arg_val,
9801 location);
9802 if (args[i] == error_mark_node)
fdbc38a6 9803 return error_mark_node;
7a938933 9804 }
26409c52 9805 go_assert(pp == params->end());
05a7d566 9806 go_assert(i == nargs);
7a938933
ILT
9807 }
9808
fdbc38a6 9809 tree fntype_tree = type_to_tree(fntype->get_backend(gogo));
f7191ecd
CM
9810 tree fnfield_type = type_to_tree(fntype->get_backend_fntype(gogo));
9811 if (fntype_tree == error_mark_node || fnfield_type == error_mark_node)
fdbc38a6
ILT
9812 return error_mark_node;
9813 go_assert(FUNCTION_POINTER_TYPE_P(fnfield_type));
9814 tree rettype = TREE_TYPE(TREE_TYPE(fnfield_type));
7a938933 9815 if (rettype == error_mark_node)
fdbc38a6 9816 return error_mark_node;
7a938933
ILT
9817
9818 tree fn;
05a7d566 9819 tree closure_tree;
fdbc38a6
ILT
9820 if (func != NULL)
9821 {
9822 Named_object* no = func->named_object();
b7d93b46 9823 fn = expr_to_tree(Func_expression::get_code_pointer(gogo, no, location));
05a7d566
ILT
9824 if (!has_closure)
9825 closure_tree = NULL_TREE;
9826 else
fdbc38a6 9827 {
05a7d566
ILT
9828 closure_tree = func->closure()->get_tree(context);
9829 if (closure_tree == error_mark_node)
9830 return error_mark_node;
fdbc38a6
ILT
9831 }
9832 }
1bbf7edb 9833 else if (!is_interface_method)
fdbc38a6 9834 {
05a7d566 9835 closure_tree = this->fn_->get_tree(context);
fdbc38a6
ILT
9836 if (closure_tree == error_mark_node)
9837 return error_mark_node;
9838 tree fnc = fold_convert_loc(location.gcc_location(), fntype_tree,
9839 closure_tree);
9840 go_assert(POINTER_TYPE_P(TREE_TYPE(fnc))
9841 && (TREE_CODE(TREE_TYPE(TREE_TYPE(fnc)))
9842 == RECORD_TYPE));
9843 tree field = TYPE_FIELDS(TREE_TYPE(TREE_TYPE(fnc)));
9844 fn = fold_build3_loc(location.gcc_location(), COMPONENT_REF,
9845 TREE_TYPE(field),
9846 build_fold_indirect_ref_loc(location.gcc_location(),
9847 fnc),
9848 field, NULL_TREE);
fdbc38a6 9849 }
7a938933 9850 else
6452b112 9851 {
fdbc38a6
ILT
9852 fn = this->interface_method_function(context, interface_method,
9853 &args[0]);
9854 if (fn == error_mark_node)
9855 return error_mark_node;
05a7d566 9856 closure_tree = NULL_TREE;
6452b112 9857 }
7a938933 9858
fdbc38a6
ILT
9859 if (fn == error_mark_node || TREE_TYPE(fn) == error_mark_node)
9860 return error_mark_node;
9861
7a938933
ILT
9862 tree fndecl = fn;
9863 if (TREE_CODE(fndecl) == ADDR_EXPR)
9864 fndecl = TREE_OPERAND(fndecl, 0);
91349ecb
ILT
9865
9866 // Add a type cast in case the type of the function is a recursive
9867 // type which refers to itself.
9868 if (!DECL_P(fndecl) || !DECL_IS_BUILTIN(fndecl))
fdbc38a6 9869 fn = fold_convert_loc(location.gcc_location(), fnfield_type, fn);
91349ecb
ILT
9870
9871 // This is to support builtin math functions when using 80387 math.
7a938933 9872 tree excess_type = NULL_TREE;
9d465faf
ILT
9873 if (optimize
9874 && TREE_CODE(fndecl) == FUNCTION_DECL
7a938933
ILT
9875 && DECL_IS_BUILTIN(fndecl)
9876 && DECL_BUILT_IN_CLASS(fndecl) == BUILT_IN_NORMAL
9877 && nargs > 0
9878 && ((SCALAR_FLOAT_TYPE_P(rettype)
9879 && SCALAR_FLOAT_TYPE_P(TREE_TYPE(args[0])))
9880 || (COMPLEX_FLOAT_TYPE_P(rettype)
9881 && COMPLEX_FLOAT_TYPE_P(TREE_TYPE(args[0])))))
9882 {
9883 excess_type = excess_precision_type(TREE_TYPE(args[0]));
9884 if (excess_type != NULL_TREE)
9885 {
9886 tree excess_fndecl = mathfn_built_in(excess_type,
9887 DECL_FUNCTION_CODE(fndecl));
9888 if (excess_fndecl == NULL_TREE)
9889 excess_type = NULL_TREE;
9890 else
9891 {
8afa2bfb
SD
9892 fn = build_fold_addr_expr_loc(location.gcc_location(),
9893 excess_fndecl);
7a938933 9894 for (int i = 0; i < nargs; ++i)
9a0e3259
ILT
9895 {
9896 if (SCALAR_FLOAT_TYPE_P(TREE_TYPE(args[i]))
9897 || COMPLEX_FLOAT_TYPE_P(TREE_TYPE(args[i])))
9898 args[i] = ::convert(excess_type, args[i]);
9899 }
7a938933
ILT
9900 }
9901 }
9902 }
9903
405c87c4
ILT
9904 if (func == NULL)
9905 fn = save_expr(fn);
9906
05a7d566
ILT
9907 if (!has_closure_arg)
9908 go_assert(closure_tree == NULL_TREE);
9909 else
9910 {
9911 // Pass the closure argument by calling the function function
9912 // __go_set_closure. In the order_evaluations pass we have
9913 // ensured that if any parameters contain call expressions, they
9914 // will have been moved out to temporary variables.
9915
9916 go_assert(closure_tree != NULL_TREE);
9917 closure_tree = fold_convert_loc(location.gcc_location(), ptr_type_node,
9918 closure_tree);
9919 static tree set_closure_fndecl;
9920 tree set_closure = Gogo::call_builtin(&set_closure_fndecl,
9921 location,
9922 "__go_set_closure",
9923 1,
9924 void_type_node,
9925 ptr_type_node,
9926 closure_tree);
9927 if (set_closure == error_mark_node)
9928 return error_mark_node;
9929 fn = build2_loc(location.gcc_location(), COMPOUND_EXPR,
9930 TREE_TYPE(fn), set_closure, fn);
9931 }
9932
7a938933
ILT
9933 tree ret = build_call_array(excess_type != NULL_TREE ? excess_type : rettype,
9934 fn, nargs, args);
9935 delete[] args;
9936
8afa2bfb 9937 SET_EXPR_LOCATION(ret, location.gcc_location());
7a938933 9938
7a938933
ILT
9939 // If this is a recursive function type which returns itself, as in
9940 // type F func() F
9941 // we have used ptr_type_node for the return type. Add a cast here
9942 // to the correct type.
9943 if (TREE_TYPE(ret) == ptr_type_node)
9944 {
5b735706 9945 tree t = type_to_tree(this->type()->base()->get_backend(gogo));
8afa2bfb 9946 ret = fold_convert_loc(location.gcc_location(), t, ret);
7a938933
ILT
9947 }
9948
9949 if (excess_type != NULL_TREE)
9950 {
9951 // Calling convert here can undo our excess precision change.
9952 // That may or may not be a bug in convert_to_real.
9953 ret = build1(NOP_EXPR, rettype, ret);
9954 }
9955
8586635c
ILT
9956 if (this->results_ != NULL)
9957 ret = this->set_results(context, ret);
7a938933
ILT
9958
9959 this->tree_ = ret;
9960
9961 return ret;
9962}
9963
8586635c
ILT
9964// Set the result variables if this call returns multiple results.
9965
9966tree
9967Call_expression::set_results(Translate_context* context, tree call_tree)
9968{
9969 tree stmt_list = NULL_TREE;
9970
9971 call_tree = save_expr(call_tree);
9972
9973 if (TREE_CODE(TREE_TYPE(call_tree)) != RECORD_TYPE)
9974 {
9975 go_assert(saw_errors());
9976 return call_tree;
9977 }
9978
8afa2bfb 9979 Location loc = this->location();
8586635c
ILT
9980 tree field = TYPE_FIELDS(TREE_TYPE(call_tree));
9981 size_t rc = this->result_count();
9982 for (size_t i = 0; i < rc; ++i, field = DECL_CHAIN(field))
9983 {
9984 go_assert(field != NULL_TREE);
9985
9986 Temporary_statement* temp = this->result(i);
9b83b6d2
ILT
9987 if (temp == NULL)
9988 {
9989 go_assert(saw_errors());
9990 return error_mark_node;
9991 }
8586635c
ILT
9992 Temporary_reference_expression* ref =
9993 Expression::make_temporary_reference(temp, loc);
9994 ref->set_is_lvalue();
9995 tree temp_tree = ref->get_tree(context);
9996 if (temp_tree == error_mark_node)
69f232c5 9997 return error_mark_node;
8586635c 9998
8afa2bfb
SD
9999 tree val_tree = build3_loc(loc.gcc_location(), COMPONENT_REF,
10000 TREE_TYPE(field), call_tree, field, NULL_TREE);
10001 tree set_tree = build2_loc(loc.gcc_location(), MODIFY_EXPR,
10002 void_type_node, temp_tree, val_tree);
8586635c
ILT
10003
10004 append_to_statement_list(set_tree, &stmt_list);
10005 }
10006 go_assert(field == NULL_TREE);
10007
10008 return save_expr(stmt_list);
10009}
10010
16c57fe2
RL
10011// Dump ast representation for a call expressin.
10012
10013void
10014Call_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
10015{
10016 this->fn_->dump_expression(ast_dump_context);
10017 ast_dump_context->ostream() << "(";
10018 if (args_ != NULL)
10019 ast_dump_context->dump_expression_list(this->args_);
10020
10021 ast_dump_context->ostream() << ") ";
10022}
10023
7a938933
ILT
10024// Make a call expression.
10025
10026Call_expression*
10027Expression::make_call(Expression* fn, Expression_list* args, bool is_varargs,
8afa2bfb 10028 Location location)
7a938933
ILT
10029{
10030 return new Call_expression(fn, args, is_varargs, location);
10031}
10032
10033// A single result from a call which returns multiple results.
10034
10035class Call_result_expression : public Expression
10036{
10037 public:
10038 Call_result_expression(Call_expression* call, unsigned int index)
10039 : Expression(EXPRESSION_CALL_RESULT, call->location()),
10040 call_(call), index_(index)
10041 { }
10042
10043 protected:
10044 int
10045 do_traverse(Traverse*);
10046
10047 Type*
10048 do_type();
10049
10050 void
10051 do_determine_type(const Type_context*);
10052
10053 void
10054 do_check_types(Gogo*);
10055
10056 Expression*
10057 do_copy()
10058 {
10059 return new Call_result_expression(this->call_->call_expression(),
10060 this->index_);
10061 }
10062
10063 bool
10064 do_must_eval_in_order() const
10065 { return true; }
10066
10067 tree
10068 do_get_tree(Translate_context*);
10069
16c57fe2
RL
10070 void
10071 do_dump_expression(Ast_dump_context*) const;
10072
7a938933
ILT
10073 private:
10074 // The underlying call expression.
10075 Expression* call_;
10076 // Which result we want.
10077 unsigned int index_;
10078};
10079
10080// Traverse a call result.
10081
10082int
10083Call_result_expression::do_traverse(Traverse* traverse)
10084{
10085 if (traverse->remember_expression(this->call_))
10086 {
10087 // We have already traversed the call expression.
10088 return TRAVERSE_CONTINUE;
10089 }
10090 return Expression::traverse(&this->call_, traverse);
10091}
10092
10093// Get the type.
10094
10095Type*
10096Call_result_expression::do_type()
10097{
7a2d845d
ILT
10098 if (this->classification() == EXPRESSION_ERROR)
10099 return Type::make_error_type();
10100
7a938933
ILT
10101 // THIS->CALL_ can be replaced with a temporary reference due to
10102 // Call_expression::do_must_eval_in_order when there is an error.
10103 Call_expression* ce = this->call_->call_expression();
10104 if (ce == NULL)
10d53f5d
ILT
10105 {
10106 this->set_is_error();
10107 return Type::make_error_type();
10108 }
7a938933
ILT
10109 Function_type* fntype = ce->get_function_type();
10110 if (fntype == NULL)
10d53f5d 10111 {
22508cae 10112 if (ce->issue_error())
02a72827
ILT
10113 {
10114 if (!ce->fn()->type()->is_error())
10115 this->report_error(_("expected function"));
10116 }
10d53f5d
ILT
10117 this->set_is_error();
10118 return Type::make_error_type();
10119 }
7a938933 10120 const Typed_identifier_list* results = fntype->results();
8586635c 10121 if (results == NULL || results->size() < 2)
428f5f5f 10122 {
8586635c
ILT
10123 if (ce->issue_error())
10124 this->report_error(_("number of results does not match "
10125 "number of values"));
428f5f5f
ILT
10126 return Type::make_error_type();
10127 }
7a938933
ILT
10128 Typed_identifier_list::const_iterator pr = results->begin();
10129 for (unsigned int i = 0; i < this->index_; ++i)
10130 {
10131 if (pr == results->end())
7a2d845d 10132 break;
7a938933
ILT
10133 ++pr;
10134 }
10135 if (pr == results->end())
7a2d845d 10136 {
8586635c
ILT
10137 if (ce->issue_error())
10138 this->report_error(_("number of results does not match "
10139 "number of values"));
7a2d845d
ILT
10140 return Type::make_error_type();
10141 }
7a938933
ILT
10142 return pr->type();
10143}
10144
7a2d845d
ILT
10145// Check the type. Just make sure that we trigger the warning in
10146// do_type.
7a938933
ILT
10147
10148void
10149Call_result_expression::do_check_types(Gogo*)
10150{
7a2d845d 10151 this->type();
7a938933
ILT
10152}
10153
10154// Determine the type. We have nothing to do here, but the 0 result
10155// needs to pass down to the caller.
10156
10157void
10158Call_result_expression::do_determine_type(const Type_context*)
10159{
20532210 10160 this->call_->determine_type_no_context();
7a938933
ILT
10161}
10162
8586635c
ILT
10163// Return the tree. We just refer to the temporary set by the call
10164// expression. We don't do this at lowering time because it makes it
10165// hard to evaluate the call at the right time.
7a938933
ILT
10166
10167tree
10168Call_result_expression::do_get_tree(Translate_context* context)
10169{
8586635c 10170 Call_expression* ce = this->call_->call_expression();
9b83b6d2
ILT
10171 if (ce == NULL)
10172 {
10173 go_assert(this->call_->is_error_expression());
10174 return error_mark_node;
10175 }
8586635c 10176 Temporary_statement* ts = ce->result(this->index_);
9b83b6d2
ILT
10177 if (ts == NULL)
10178 {
10179 go_assert(saw_errors());
10180 return error_mark_node;
10181 }
8586635c
ILT
10182 Expression* ref = Expression::make_temporary_reference(ts, this->location());
10183 return ref->get_tree(context);
7a938933
ILT
10184}
10185
16c57fe2
RL
10186// Dump ast representation for a call result expression.
10187
10188void
10189Call_result_expression::do_dump_expression(Ast_dump_context* ast_dump_context)
10190 const
10191{
10192 // FIXME: Wouldn't it be better if the call is assigned to a temporary
10193 // (struct) and the fields are referenced instead.
10194 ast_dump_context->ostream() << this->index_ << "@(";
10195 ast_dump_context->dump_expression(this->call_);
10196 ast_dump_context->ostream() << ")";
10197}
10198
7a938933
ILT
10199// Make a reference to a single result of a call which returns
10200// multiple results.
10201
10202Expression*
10203Expression::make_call_result(Call_expression* call, unsigned int index)
10204{
10205 return new Call_result_expression(call, index);
10206}
10207
10208// Class Index_expression.
10209
10210// Traversal.
10211
10212int
10213Index_expression::do_traverse(Traverse* traverse)
10214{
10215 if (Expression::traverse(&this->left_, traverse) == TRAVERSE_EXIT
10216 || Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT
10217 || (this->end_ != NULL
26b8f7eb
ILT
10218 && Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT)
10219 || (this->cap_ != NULL
10220 && Expression::traverse(&this->cap_, traverse) == TRAVERSE_EXIT))
7a938933
ILT
10221 return TRAVERSE_EXIT;
10222 return TRAVERSE_CONTINUE;
10223}
10224
10225// Lower an index expression. This converts the generic index
10226// expression into an array index, a string index, or a map index.
10227
10228Expression*
8586635c 10229Index_expression::do_lower(Gogo*, Named_object*, Statement_inserter*, int)
7a938933 10230{
8afa2bfb 10231 Location location = this->location();
7a938933
ILT
10232 Expression* left = this->left_;
10233 Expression* start = this->start_;
10234 Expression* end = this->end_;
26b8f7eb 10235 Expression* cap = this->cap_;
7a938933
ILT
10236
10237 Type* type = left->type();
02ed921a 10238 if (type->is_error())
7a938933 10239 return Expression::make_error(location);
d18c88a8
ILT
10240 else if (left->is_type_expression())
10241 {
10242 error_at(location, "attempt to index type expression");
10243 return Expression::make_error(location);
10244 }
7a938933 10245 else if (type->array_type() != NULL)
26b8f7eb 10246 return Expression::make_array_index(left, start, end, cap, location);
7a938933
ILT
10247 else if (type->points_to() != NULL
10248 && type->points_to()->array_type() != NULL
b7190f2f 10249 && !type->points_to()->is_slice_type())
7a938933
ILT
10250 {
10251 Expression* deref = Expression::make_unary(OPERATOR_MULT, left,
10252 location);
26b8f7eb 10253 return Expression::make_array_index(deref, start, end, cap, location);
7a938933
ILT
10254 }
10255 else if (type->is_string_type())
26b8f7eb
ILT
10256 {
10257 if (cap != NULL)
10258 {
10259 error_at(location, "invalid 3-index slice of string");
10260 return Expression::make_error(location);
10261 }
10262 return Expression::make_string_index(left, start, end, location);
10263 }
7a938933
ILT
10264 else if (type->map_type() != NULL)
10265 {
26b8f7eb 10266 if (end != NULL || cap != NULL)
7a938933
ILT
10267 {
10268 error_at(location, "invalid slice of map");
10269 return Expression::make_error(location);
10270 }
2d8f63a1
ILT
10271 Map_index_expression* ret = Expression::make_map_index(left, start,
10272 location);
7a938933
ILT
10273 if (this->is_lvalue_)
10274 ret->set_is_lvalue();
10275 return ret;
10276 }
10277 else
10278 {
10279 error_at(location,
10280 "attempt to index object which is not array, string, or map");
10281 return Expression::make_error(location);
10282 }
10283}
10284
26b8f7eb
ILT
10285// Write an indexed expression
10286// (expr[expr:expr:expr], expr[expr:expr] or expr[expr]) to a dump context.
16c57fe2
RL
10287
10288void
10289Index_expression::dump_index_expression(Ast_dump_context* ast_dump_context,
10290 const Expression* expr,
10291 const Expression* start,
26b8f7eb
ILT
10292 const Expression* end,
10293 const Expression* cap)
16c57fe2
RL
10294{
10295 expr->dump_expression(ast_dump_context);
10296 ast_dump_context->ostream() << "[";
10297 start->dump_expression(ast_dump_context);
10298 if (end != NULL)
10299 {
10300 ast_dump_context->ostream() << ":";
10301 end->dump_expression(ast_dump_context);
10302 }
26b8f7eb
ILT
10303 if (cap != NULL)
10304 {
10305 ast_dump_context->ostream() << ":";
10306 cap->dump_expression(ast_dump_context);
10307 }
16c57fe2
RL
10308 ast_dump_context->ostream() << "]";
10309}
10310
10311// Dump ast representation for an index expression.
10312
10313void
10314Index_expression::do_dump_expression(Ast_dump_context* ast_dump_context)
10315 const
10316{
10317 Index_expression::dump_index_expression(ast_dump_context, this->left_,
26b8f7eb 10318 this->start_, this->end_, this->cap_);
16c57fe2
RL
10319}
10320
7a938933
ILT
10321// Make an index expression.
10322
10323Expression*
10324Expression::make_index(Expression* left, Expression* start, Expression* end,
26b8f7eb 10325 Expression* cap, Location location)
7a938933 10326{
26b8f7eb 10327 return new Index_expression(left, start, end, cap, location);
7a938933
ILT
10328}
10329
10330// An array index. This is used for both indexing and slicing.
10331
10332class Array_index_expression : public Expression
10333{
10334 public:
10335 Array_index_expression(Expression* array, Expression* start,
26b8f7eb 10336 Expression* end, Expression* cap, Location location)
7a938933 10337 : Expression(EXPRESSION_ARRAY_INDEX, location),
26b8f7eb 10338 array_(array), start_(start), end_(end), cap_(cap), type_(NULL)
7a938933
ILT
10339 { }
10340
10341 protected:
10342 int
10343 do_traverse(Traverse*);
10344
10345 Type*
10346 do_type();
10347
10348 void
10349 do_determine_type(const Type_context*);
10350
10351 void
10352 do_check_types(Gogo*);
10353
10354 Expression*
10355 do_copy()
10356 {
10357 return Expression::make_array_index(this->array_->copy(),
10358 this->start_->copy(),
10359 (this->end_ == NULL
10360 ? NULL
10361 : this->end_->copy()),
26b8f7eb
ILT
10362 (this->cap_ == NULL
10363 ? NULL
10364 : this->cap_->copy()),
7a938933
ILT
10365 this->location());
10366 }
10367
cbe98a41
ILT
10368 bool
10369 do_must_eval_subexpressions_in_order(int* skip) const
10370 {
10371 *skip = 1;
10372 return true;
10373 }
10374
7a938933
ILT
10375 bool
10376 do_is_addressable() const;
10377
10378 void
10379 do_address_taken(bool escapes)
10380 { this->array_->address_taken(escapes); }
10381
64c7b4c0
ILT
10382 void
10383 do_issue_nil_check()
10384 { this->array_->issue_nil_check(); }
10385
7a938933
ILT
10386 tree
10387 do_get_tree(Translate_context*);
10388
16c57fe2
RL
10389 void
10390 do_dump_expression(Ast_dump_context*) const;
10391
7a938933
ILT
10392 private:
10393 // The array we are getting a value from.
10394 Expression* array_;
10395 // The start or only index.
10396 Expression* start_;
10397 // The end index of a slice. This may be NULL for a simple array
10398 // index, or it may be a nil expression for the length of the array.
10399 Expression* end_;
26b8f7eb
ILT
10400 // The capacity argument of a slice. This may be NULL for an array index or
10401 // slice.
10402 Expression* cap_;
7a938933
ILT
10403 // The type of the expression.
10404 Type* type_;
10405};
10406
10407// Array index traversal.
10408
10409int
10410Array_index_expression::do_traverse(Traverse* traverse)
10411{
10412 if (Expression::traverse(&this->array_, traverse) == TRAVERSE_EXIT)
10413 return TRAVERSE_EXIT;
10414 if (Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT)
10415 return TRAVERSE_EXIT;
10416 if (this->end_ != NULL)
10417 {
10418 if (Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT)
10419 return TRAVERSE_EXIT;
10420 }
26b8f7eb
ILT
10421 if (this->cap_ != NULL)
10422 {
10423 if (Expression::traverse(&this->cap_, traverse) == TRAVERSE_EXIT)
10424 return TRAVERSE_EXIT;
10425 }
7a938933
ILT
10426 return TRAVERSE_CONTINUE;
10427}
10428
10429// Return the type of an array index.
10430
10431Type*
10432Array_index_expression::do_type()
10433{
10434 if (this->type_ == NULL)
10435 {
10436 Array_type* type = this->array_->type()->array_type();
10437 if (type == NULL)
10438 this->type_ = Type::make_error_type();
10439 else if (this->end_ == NULL)
10440 this->type_ = type->element_type();
b7190f2f 10441 else if (type->is_slice_type())
7a938933
ILT
10442 {
10443 // A slice of a slice has the same type as the original
10444 // slice.
10445 this->type_ = this->array_->type()->deref();
10446 }
10447 else
10448 {
10449 // A slice of an array is a slice.
10450 this->type_ = Type::make_array_type(type->element_type(), NULL);
10451 }
10452 }
10453 return this->type_;
10454}
10455
10456// Set the type of an array index.
10457
10458void
10459Array_index_expression::do_determine_type(const Type_context*)
10460{
10461 this->array_->determine_type_no_context();
1c4a5fc8 10462 this->start_->determine_type_no_context();
7a938933 10463 if (this->end_ != NULL)
1c4a5fc8 10464 this->end_->determine_type_no_context();
26b8f7eb
ILT
10465 if (this->cap_ != NULL)
10466 this->cap_->determine_type_no_context();
7a938933
ILT
10467}
10468
10469// Check types of an array index.
10470
10471void
10472Array_index_expression::do_check_types(Gogo*)
10473{
69d8df44
ILT
10474 Numeric_constant nc;
10475 unsigned long v;
10476 if (this->start_->type()->integer_type() == NULL
10477 && !this->start_->type()->is_error()
10478 && (!this->start_->numeric_constant_value(&nc)
10479 || nc.to_unsigned_long(&v) == Numeric_constant::NC_UL_NOTINT))
7a938933
ILT
10480 this->report_error(_("index must be integer"));
10481 if (this->end_ != NULL
10482 && this->end_->type()->integer_type() == NULL
02a72827
ILT
10483 && !this->end_->type()->is_error()
10484 && !this->end_->is_nil_expression()
69d8df44
ILT
10485 && !this->end_->is_error_expression()
10486 && (!this->end_->numeric_constant_value(&nc)
10487 || nc.to_unsigned_long(&v) == Numeric_constant::NC_UL_NOTINT))
7a938933 10488 this->report_error(_("slice end must be integer"));
26b8f7eb
ILT
10489 if (this->cap_ != NULL
10490 && this->cap_->type()->integer_type() == NULL
10491 && !this->cap_->type()->is_error()
10492 && !this->cap_->is_nil_expression()
10493 && !this->cap_->is_error_expression()
10494 && (!this->cap_->numeric_constant_value(&nc)
10495 || nc.to_unsigned_long(&v) == Numeric_constant::NC_UL_NOTINT))
10496 this->report_error(_("slice capacity must be integer"));
7a938933
ILT
10497
10498 Array_type* array_type = this->array_->type()->array_type();
f39c772f
ILT
10499 if (array_type == NULL)
10500 {
26409c52 10501 go_assert(this->array_->type()->is_error());
f39c772f
ILT
10502 return;
10503 }
7a938933
ILT
10504
10505 unsigned int int_bits =
10506 Type::lookup_integer_type("int")->integer_type()->bits();
10507
5caf63ca 10508 Numeric_constant lvalnc;
7a938933 10509 mpz_t lval;
7a938933 10510 bool lval_valid = (array_type->length() != NULL
5caf63ca
ILT
10511 && array_type->length()->numeric_constant_value(&lvalnc)
10512 && lvalnc.to_int(&lval));
10513 Numeric_constant inc;
7a938933 10514 mpz_t ival;
f2b2ead4 10515 bool ival_valid = false;
5caf63ca 10516 if (this->start_->numeric_constant_value(&inc) && inc.to_int(&ival))
7a938933 10517 {
f2b2ead4 10518 ival_valid = true;
7a938933
ILT
10519 if (mpz_sgn(ival) < 0
10520 || mpz_sizeinbase(ival, 2) >= int_bits
10521 || (lval_valid
10522 && (this->end_ == NULL
10523 ? mpz_cmp(ival, lval) >= 0
10524 : mpz_cmp(ival, lval) > 0)))
10525 {
10526 error_at(this->start_->location(), "array index out of bounds");
10527 this->set_is_error();
10528 }
10529 }
10530 if (this->end_ != NULL && !this->end_->is_nil_expression())
10531 {
5caf63ca
ILT
10532 Numeric_constant enc;
10533 mpz_t eval;
26b8f7eb 10534 bool eval_valid = false;
5caf63ca 10535 if (this->end_->numeric_constant_value(&enc) && enc.to_int(&eval))
7a938933 10536 {
26b8f7eb 10537 eval_valid = true;
5caf63ca
ILT
10538 if (mpz_sgn(eval) < 0
10539 || mpz_sizeinbase(eval, 2) >= int_bits
10540 || (lval_valid && mpz_cmp(eval, lval) > 0))
7a938933
ILT
10541 {
10542 error_at(this->end_->location(), "array index out of bounds");
10543 this->set_is_error();
10544 }
f2b2ead4
ILT
10545 else if (ival_valid && mpz_cmp(ival, eval) > 0)
10546 this->report_error(_("inverted slice range"));
7a938933 10547 }
26b8f7eb
ILT
10548
10549 Numeric_constant cnc;
10550 mpz_t cval;
10551 if (this->cap_ != NULL
10552 && this->cap_->numeric_constant_value(&cnc) && cnc.to_int(&cval))
10553 {
10554 if (mpz_sgn(cval) < 0
10555 || mpz_sizeinbase(cval, 2) >= int_bits
10556 || (lval_valid && mpz_cmp(cval, lval) > 0))
10557 {
10558 error_at(this->cap_->location(), "array index out of bounds");
10559 this->set_is_error();
10560 }
10561 else if (ival_valid && mpz_cmp(ival, cval) > 0)
10562 {
10563 error_at(this->cap_->location(),
10564 "invalid slice index: capacity less than start");
10565 this->set_is_error();
10566 }
10567 else if (eval_valid && mpz_cmp(eval, cval) > 0)
10568 {
10569 error_at(this->cap_->location(),
10570 "invalid slice index: capacity less than length");
10571 this->set_is_error();
10572 }
10573 mpz_clear(cval);
10574 }
10575
10576 if (eval_valid)
10577 mpz_clear(eval);
7a938933 10578 }
f2b2ead4
ILT
10579 if (ival_valid)
10580 mpz_clear(ival);
5caf63ca
ILT
10581 if (lval_valid)
10582 mpz_clear(lval);
7a938933
ILT
10583
10584 // A slice of an array requires an addressable array. A slice of a
10585 // slice is always possible.
b7190f2f 10586 if (this->end_ != NULL && !array_type->is_slice_type())
9ebd2806
ILT
10587 {
10588 if (!this->array_->is_addressable())
5fca1e3f 10589 this->report_error(_("slice of unaddressable value"));
9ebd2806
ILT
10590 else
10591 this->array_->address_taken(true);
10592 }
7a938933
ILT
10593}
10594
10595// Return whether this expression is addressable.
10596
10597bool
10598Array_index_expression::do_is_addressable() const
10599{
10600 // A slice expression is not addressable.
10601 if (this->end_ != NULL)
10602 return false;
10603
10604 // An index into a slice is addressable.
b7190f2f 10605 if (this->array_->type()->is_slice_type())
7a938933
ILT
10606 return true;
10607
10608 // An index into an array is addressable if the array is
10609 // addressable.
10610 return this->array_->is_addressable();
10611}
10612
10613// Get a tree for an array index.
10614
10615tree
10616Array_index_expression::do_get_tree(Translate_context* context)
10617{
10618 Gogo* gogo = context->gogo();
8afa2bfb 10619 Location loc = this->location();
7a938933
ILT
10620
10621 Array_type* array_type = this->array_->type()->array_type();
91ab2208
ILT
10622 if (array_type == NULL)
10623 {
26409c52 10624 go_assert(this->array_->type()->is_error());
91ab2208
ILT
10625 return error_mark_node;
10626 }
7a938933 10627
5b735706 10628 tree type_tree = type_to_tree(array_type->get_backend(gogo));
5becc70b
ILT
10629 if (type_tree == error_mark_node)
10630 return error_mark_node;
7a938933
ILT
10631
10632 tree array_tree = this->array_->get_tree(context);
10633 if (array_tree == error_mark_node)
10634 return error_mark_node;
10635
10636 if (array_type->length() == NULL && !DECL_P(array_tree))
10637 array_tree = save_expr(array_tree);
b6422b37
ILT
10638
10639 tree length_tree = NULL_TREE;
10640 if (this->end_ == NULL || this->end_->is_nil_expression())
10641 {
10642 length_tree = array_type->length_tree(gogo, array_tree);
10643 if (length_tree == error_mark_node)
10644 return error_mark_node;
10645 length_tree = save_expr(length_tree);
10646 }
10647
10648 tree capacity_tree = NULL_TREE;
10649 if (this->end_ != NULL)
10650 {
10651 capacity_tree = array_type->capacity_tree(gogo, array_tree);
10652 if (capacity_tree == error_mark_node)
10653 return error_mark_node;
10654 capacity_tree = save_expr(capacity_tree);
10655 }
10656
26b8f7eb
ILT
10657 tree cap_arg = capacity_tree;
10658 if (this->cap_ != NULL)
10659 {
10660 cap_arg = this->cap_->get_tree(context);
10661 if (cap_arg == error_mark_node)
10662 return error_mark_node;
10663 }
10664
b6422b37
ILT
10665 tree length_type = (length_tree != NULL_TREE
10666 ? TREE_TYPE(length_tree)
26b8f7eb 10667 : TREE_TYPE(cap_arg));
7a938933
ILT
10668
10669 tree bad_index = boolean_false_node;
10670
10671 tree start_tree = this->start_->get_tree(context);
10672 if (start_tree == error_mark_node)
10673 return error_mark_node;
10674 if (!DECL_P(start_tree))
10675 start_tree = save_expr(start_tree);
10676 if (!INTEGRAL_TYPE_P(TREE_TYPE(start_tree)))
10677 start_tree = convert_to_integer(length_type, start_tree);
10678
10679 bad_index = Expression::check_bounds(start_tree, length_type, bad_index,
10680 loc);
10681
8afa2bfb
SD
10682 start_tree = fold_convert_loc(loc.gcc_location(), length_type, start_tree);
10683 bad_index = fold_build2_loc(loc.gcc_location(), TRUTH_OR_EXPR,
10684 boolean_type_node, bad_index,
10685 fold_build2_loc(loc.gcc_location(),
7a938933
ILT
10686 (this->end_ == NULL
10687 ? GE_EXPR
10688 : GT_EXPR),
10689 boolean_type_node, start_tree,
b6422b37
ILT
10690 (this->end_ == NULL
10691 ? length_tree
10692 : capacity_tree)));
7a938933
ILT
10693
10694 int code = (array_type->length() != NULL
10695 ? (this->end_ == NULL
10696 ? RUNTIME_ERROR_ARRAY_INDEX_OUT_OF_BOUNDS
10697 : RUNTIME_ERROR_ARRAY_SLICE_OUT_OF_BOUNDS)
10698 : (this->end_ == NULL
10699 ? RUNTIME_ERROR_SLICE_INDEX_OUT_OF_BOUNDS
10700 : RUNTIME_ERROR_SLICE_SLICE_OUT_OF_BOUNDS));
776f27a6 10701 tree crash = gogo->runtime_error(code, loc);
7a938933
ILT
10702
10703 if (this->end_ == NULL)
10704 {
10705 // Simple array indexing. This has to return an l-value, so
10706 // wrap the index check into START_TREE.
10707 start_tree = build2(COMPOUND_EXPR, TREE_TYPE(start_tree),
10708 build3(COND_EXPR, void_type_node,
10709 bad_index, crash, NULL_TREE),
10710 start_tree);
8afa2bfb 10711 start_tree = fold_convert_loc(loc.gcc_location(), sizetype, start_tree);
7a938933
ILT
10712
10713 if (array_type->length() != NULL)
10714 {
10715 // Fixed array.
10716 return build4(ARRAY_REF, TREE_TYPE(type_tree), array_tree,
10717 start_tree, NULL_TREE, NULL_TREE);
10718 }
10719 else
10720 {
10721 // Open array.
10722 tree values = array_type->value_pointer_tree(gogo, array_tree);
5b735706
ILT
10723 Type* element_type = array_type->element_type();
10724 Btype* belement_type = element_type->get_backend(gogo);
10725 tree element_type_tree = type_to_tree(belement_type);
5becc70b
ILT
10726 if (element_type_tree == error_mark_node)
10727 return error_mark_node;
7a938933 10728 tree element_size = TYPE_SIZE_UNIT(element_type_tree);
8afa2bfb 10729 tree offset = fold_build2_loc(loc.gcc_location(), MULT_EXPR, sizetype,
7a938933 10730 start_tree, element_size);
8afa2bfb 10731 tree ptr = fold_build2_loc(loc.gcc_location(), POINTER_PLUS_EXPR,
7a938933
ILT
10732 TREE_TYPE(values), values, offset);
10733 return build_fold_indirect_ref(ptr);
10734 }
10735 }
10736
10737 // Array slice.
10738
26b8f7eb
ILT
10739 if (this->cap_ != NULL)
10740 {
10741 if (!DECL_P(cap_arg))
10742 cap_arg = save_expr(cap_arg);
10743 if (!INTEGRAL_TYPE_P(TREE_TYPE(cap_arg)))
10744 cap_arg = convert_to_integer(length_type, cap_arg);
10745
10746 bad_index = Expression::check_bounds(cap_arg, length_type, bad_index,
10747 loc);
10748 cap_arg = fold_convert_loc(loc.gcc_location(), length_type, cap_arg);
10749
10750 tree bad_cap = fold_build2_loc(loc.gcc_location(), TRUTH_OR_EXPR,
10751 boolean_type_node,
10752 fold_build2_loc(loc.gcc_location(),
10753 LT_EXPR, boolean_type_node,
10754 cap_arg, start_tree),
10755 fold_build2_loc(loc.gcc_location(),
10756 GT_EXPR, boolean_type_node,
10757 cap_arg, capacity_tree));
10758 bad_index = fold_build2_loc(loc.gcc_location(), TRUTH_OR_EXPR,
10759 boolean_type_node, bad_index, bad_cap);
10760 }
10761
7a938933
ILT
10762 tree end_tree;
10763 if (this->end_->is_nil_expression())
10764 end_tree = length_tree;
10765 else
10766 {
10767 end_tree = this->end_->get_tree(context);
10768 if (end_tree == error_mark_node)
10769 return error_mark_node;
10770 if (!DECL_P(end_tree))
10771 end_tree = save_expr(end_tree);
10772 if (!INTEGRAL_TYPE_P(TREE_TYPE(end_tree)))
10773 end_tree = convert_to_integer(length_type, end_tree);
10774
10775 bad_index = Expression::check_bounds(end_tree, length_type, bad_index,
10776 loc);
10777
8afa2bfb 10778 end_tree = fold_convert_loc(loc.gcc_location(), length_type, end_tree);
7a938933 10779
8afa2bfb
SD
10780 tree bad_end = fold_build2_loc(loc.gcc_location(), TRUTH_OR_EXPR,
10781 boolean_type_node,
10782 fold_build2_loc(loc.gcc_location(),
10783 LT_EXPR, boolean_type_node,
7a938933 10784 end_tree, start_tree),
8afa2bfb
SD
10785 fold_build2_loc(loc.gcc_location(),
10786 GT_EXPR, boolean_type_node,
26b8f7eb 10787 end_tree, cap_arg));
8afa2bfb
SD
10788 bad_index = fold_build2_loc(loc.gcc_location(), TRUTH_OR_EXPR,
10789 boolean_type_node, bad_index, bad_end);
7a938933
ILT
10790 }
10791
26b8f7eb 10792
5b735706
ILT
10793 Type* element_type = array_type->element_type();
10794 tree element_type_tree = type_to_tree(element_type->get_backend(gogo));
5becc70b
ILT
10795 if (element_type_tree == error_mark_node)
10796 return error_mark_node;
7a938933
ILT
10797 tree element_size = TYPE_SIZE_UNIT(element_type_tree);
10798
8afa2bfb
SD
10799 tree offset = fold_build2_loc(loc.gcc_location(), MULT_EXPR, sizetype,
10800 fold_convert_loc(loc.gcc_location(), sizetype,
10801 start_tree),
7a938933
ILT
10802 element_size);
10803
10804 tree value_pointer = array_type->value_pointer_tree(gogo, array_tree);
5becc70b
ILT
10805 if (value_pointer == error_mark_node)
10806 return error_mark_node;
7a938933 10807
8afa2bfb 10808 value_pointer = fold_build2_loc(loc.gcc_location(), POINTER_PLUS_EXPR,
7a938933
ILT
10809 TREE_TYPE(value_pointer),
10810 value_pointer, offset);
10811
8afa2bfb
SD
10812 tree result_length_tree = fold_build2_loc(loc.gcc_location(), MINUS_EXPR,
10813 length_type, end_tree, start_tree);
7a938933 10814
8afa2bfb 10815 tree result_capacity_tree = fold_build2_loc(loc.gcc_location(), MINUS_EXPR,
26b8f7eb 10816 length_type, cap_arg, start_tree);
7a938933 10817
5b735706 10818 tree struct_tree = type_to_tree(this->type()->get_backend(gogo));
26409c52 10819 go_assert(TREE_CODE(struct_tree) == RECORD_TYPE);
7a938933 10820
4efdbe5a
ILT
10821 vec<constructor_elt, va_gc> *init;
10822 vec_alloc (init, 3);
7a938933 10823
f32682ca 10824 constructor_elt empty = {NULL, NULL};
4efdbe5a 10825 constructor_elt* elt = init->quick_push(empty);
7a938933 10826 tree field = TYPE_FIELDS(struct_tree);
26409c52 10827 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__values") == 0);
7a938933
ILT
10828 elt->index = field;
10829 elt->value = value_pointer;
10830
4efdbe5a 10831 elt = init->quick_push(empty);
7a938933 10832 field = DECL_CHAIN(field);
26409c52 10833 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__count") == 0);
7a938933 10834 elt->index = field;
8afa2bfb
SD
10835 elt->value = fold_convert_loc(loc.gcc_location(), TREE_TYPE(field),
10836 result_length_tree);
7a938933 10837
4efdbe5a 10838 elt = init->quick_push(empty);
7a938933 10839 field = DECL_CHAIN(field);
26409c52 10840 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__capacity") == 0);
7a938933 10841 elt->index = field;
8afa2bfb
SD
10842 elt->value = fold_convert_loc(loc.gcc_location(), TREE_TYPE(field),
10843 result_capacity_tree);
7a938933
ILT
10844
10845 tree constructor = build_constructor(struct_tree, init);
10846
10847 if (TREE_CONSTANT(value_pointer)
10848 && TREE_CONSTANT(result_length_tree)
10849 && TREE_CONSTANT(result_capacity_tree))
10850 TREE_CONSTANT(constructor) = 1;
10851
8afa2bfb
SD
10852 return fold_build2_loc(loc.gcc_location(), COMPOUND_EXPR,
10853 TREE_TYPE(constructor),
7a938933
ILT
10854 build3(COND_EXPR, void_type_node,
10855 bad_index, crash, NULL_TREE),
10856 constructor);
10857}
10858
16c57fe2
RL
10859// Dump ast representation for an array index expression.
10860
10861void
10862Array_index_expression::do_dump_expression(Ast_dump_context* ast_dump_context)
10863 const
10864{
10865 Index_expression::dump_index_expression(ast_dump_context, this->array_,
26b8f7eb 10866 this->start_, this->end_, this->cap_);
16c57fe2
RL
10867}
10868
26b8f7eb 10869// Make an array index expression. END and CAP may be NULL.
7a938933
ILT
10870
10871Expression*
10872Expression::make_array_index(Expression* array, Expression* start,
26b8f7eb
ILT
10873 Expression* end, Expression* cap,
10874 Location location)
7a938933 10875{
26b8f7eb 10876 return new Array_index_expression(array, start, end, cap, location);
7a938933
ILT
10877}
10878
10879// A string index. This is used for both indexing and slicing.
10880
10881class String_index_expression : public Expression
10882{
10883 public:
10884 String_index_expression(Expression* string, Expression* start,
8afa2bfb 10885 Expression* end, Location location)
7a938933
ILT
10886 : Expression(EXPRESSION_STRING_INDEX, location),
10887 string_(string), start_(start), end_(end)
10888 { }
10889
10890 protected:
10891 int
10892 do_traverse(Traverse*);
10893
10894 Type*
10895 do_type();
10896
10897 void
10898 do_determine_type(const Type_context*);
10899
10900 void
10901 do_check_types(Gogo*);
10902
10903 Expression*
10904 do_copy()
10905 {
10906 return Expression::make_string_index(this->string_->copy(),
10907 this->start_->copy(),
10908 (this->end_ == NULL
10909 ? NULL
10910 : this->end_->copy()),
10911 this->location());
10912 }
10913
cbe98a41
ILT
10914 bool
10915 do_must_eval_subexpressions_in_order(int* skip) const
10916 {
10917 *skip = 1;
10918 return true;
10919 }
10920
7a938933
ILT
10921 tree
10922 do_get_tree(Translate_context*);
10923
16c57fe2
RL
10924 void
10925 do_dump_expression(Ast_dump_context*) const;
10926
7a938933
ILT
10927 private:
10928 // The string we are getting a value from.
10929 Expression* string_;
10930 // The start or only index.
10931 Expression* start_;
10932 // The end index of a slice. This may be NULL for a single index,
10933 // or it may be a nil expression for the length of the string.
10934 Expression* end_;
10935};
10936
10937// String index traversal.
10938
10939int
10940String_index_expression::do_traverse(Traverse* traverse)
10941{
10942 if (Expression::traverse(&this->string_, traverse) == TRAVERSE_EXIT)
10943 return TRAVERSE_EXIT;
10944 if (Expression::traverse(&this->start_, traverse) == TRAVERSE_EXIT)
10945 return TRAVERSE_EXIT;
10946 if (this->end_ != NULL)
10947 {
10948 if (Expression::traverse(&this->end_, traverse) == TRAVERSE_EXIT)
10949 return TRAVERSE_EXIT;
10950 }
10951 return TRAVERSE_CONTINUE;
10952}
10953
10954// Return the type of a string index.
10955
10956Type*
10957String_index_expression::do_type()
10958{
10959 if (this->end_ == NULL)
10960 return Type::lookup_integer_type("uint8");
10961 else
82afdd42 10962 return this->string_->type();
7a938933
ILT
10963}
10964
10965// Determine the type of a string index.
10966
10967void
10968String_index_expression::do_determine_type(const Type_context*)
10969{
10970 this->string_->determine_type_no_context();
e811e209 10971 this->start_->determine_type_no_context();
7a938933 10972 if (this->end_ != NULL)
e811e209 10973 this->end_->determine_type_no_context();
7a938933
ILT
10974}
10975
10976// Check types of a string index.
10977
10978void
10979String_index_expression::do_check_types(Gogo*)
10980{
0213a547
ILT
10981 Numeric_constant nc;
10982 unsigned long v;
10983 if (this->start_->type()->integer_type() == NULL
10984 && !this->start_->type()->is_error()
10985 && (!this->start_->numeric_constant_value(&nc)
10986 || nc.to_unsigned_long(&v) == Numeric_constant::NC_UL_NOTINT))
7a938933
ILT
10987 this->report_error(_("index must be integer"));
10988 if (this->end_ != NULL
10989 && this->end_->type()->integer_type() == NULL
0213a547
ILT
10990 && !this->end_->type()->is_error()
10991 && !this->end_->is_nil_expression()
10992 && !this->end_->is_error_expression()
10993 && (!this->end_->numeric_constant_value(&nc)
10994 || nc.to_unsigned_long(&v) == Numeric_constant::NC_UL_NOTINT))
7a938933
ILT
10995 this->report_error(_("slice end must be integer"));
10996
10997 std::string sval;
10998 bool sval_valid = this->string_->string_constant_value(&sval);
10999
5caf63ca 11000 Numeric_constant inc;
7a938933 11001 mpz_t ival;
f2b2ead4 11002 bool ival_valid = false;
5caf63ca 11003 if (this->start_->numeric_constant_value(&inc) && inc.to_int(&ival))
7a938933 11004 {
f2b2ead4 11005 ival_valid = true;
7a938933
ILT
11006 if (mpz_sgn(ival) < 0
11007 || (sval_valid && mpz_cmp_ui(ival, sval.length()) >= 0))
11008 {
11009 error_at(this->start_->location(), "string index out of bounds");
11010 this->set_is_error();
11011 }
11012 }
11013 if (this->end_ != NULL && !this->end_->is_nil_expression())
11014 {
5caf63ca
ILT
11015 Numeric_constant enc;
11016 mpz_t eval;
11017 if (this->end_->numeric_constant_value(&enc) && enc.to_int(&eval))
7a938933 11018 {
5caf63ca
ILT
11019 if (mpz_sgn(eval) < 0
11020 || (sval_valid && mpz_cmp_ui(eval, sval.length()) > 0))
7a938933
ILT
11021 {
11022 error_at(this->end_->location(), "string index out of bounds");
11023 this->set_is_error();
11024 }
f2b2ead4
ILT
11025 else if (ival_valid && mpz_cmp(ival, eval) > 0)
11026 this->report_error(_("inverted slice range"));
5caf63ca 11027 mpz_clear(eval);
7a938933
ILT
11028 }
11029 }
f2b2ead4
ILT
11030 if (ival_valid)
11031 mpz_clear(ival);
7a938933
ILT
11032}
11033
11034// Get a tree for a string index.
11035
11036tree
11037String_index_expression::do_get_tree(Translate_context* context)
11038{
8afa2bfb 11039 Location loc = this->location();
7a938933
ILT
11040
11041 tree string_tree = this->string_->get_tree(context);
11042 if (string_tree == error_mark_node)
11043 return error_mark_node;
11044
11045 if (this->string_->type()->points_to() != NULL)
11046 string_tree = build_fold_indirect_ref(string_tree);
11047 if (!DECL_P(string_tree))
11048 string_tree = save_expr(string_tree);
11049 tree string_type = TREE_TYPE(string_tree);
11050
11051 tree length_tree = String_type::length_tree(context->gogo(), string_tree);
11052 length_tree = save_expr(length_tree);
776f27a6
ILT
11053
11054 Type* int_type = Type::lookup_integer_type("int");
11055 tree length_type = type_to_tree(int_type->get_backend(context->gogo()));
7a938933
ILT
11056
11057 tree bad_index = boolean_false_node;
11058
11059 tree start_tree = this->start_->get_tree(context);
11060 if (start_tree == error_mark_node)
11061 return error_mark_node;
11062 if (!DECL_P(start_tree))
11063 start_tree = save_expr(start_tree);
11064 if (!INTEGRAL_TYPE_P(TREE_TYPE(start_tree)))
11065 start_tree = convert_to_integer(length_type, start_tree);
11066
11067 bad_index = Expression::check_bounds(start_tree, length_type, bad_index,
11068 loc);
11069
8afa2bfb 11070 start_tree = fold_convert_loc(loc.gcc_location(), length_type, start_tree);
7a938933
ILT
11071
11072 int code = (this->end_ == NULL
11073 ? RUNTIME_ERROR_STRING_INDEX_OUT_OF_BOUNDS
11074 : RUNTIME_ERROR_STRING_SLICE_OUT_OF_BOUNDS);
776f27a6 11075 tree crash = context->gogo()->runtime_error(code, loc);
7a938933
ILT
11076
11077 if (this->end_ == NULL)
11078 {
8afa2bfb
SD
11079 bad_index = fold_build2_loc(loc.gcc_location(), TRUTH_OR_EXPR,
11080 boolean_type_node, bad_index,
11081 fold_build2_loc(loc.gcc_location(), GE_EXPR,
7a938933
ILT
11082 boolean_type_node,
11083 start_tree, length_tree));
11084
11085 tree bytes_tree = String_type::bytes_tree(context->gogo(), string_tree);
8afa2bfb
SD
11086 tree ptr = fold_build2_loc(loc.gcc_location(), POINTER_PLUS_EXPR,
11087 TREE_TYPE(bytes_tree),
7a938933 11088 bytes_tree,
8afa2bfb
SD
11089 fold_convert_loc(loc.gcc_location(), sizetype,
11090 start_tree));
11091 tree index = build_fold_indirect_ref_loc(loc.gcc_location(), ptr);
7a938933
ILT
11092
11093 return build2(COMPOUND_EXPR, TREE_TYPE(index),
11094 build3(COND_EXPR, void_type_node,
11095 bad_index, crash, NULL_TREE),
11096 index);
11097 }
11098 else
11099 {
11100 tree end_tree;
11101 if (this->end_->is_nil_expression())
11102 end_tree = build_int_cst(length_type, -1);
11103 else
11104 {
11105 end_tree = this->end_->get_tree(context);
11106 if (end_tree == error_mark_node)
11107 return error_mark_node;
11108 if (!DECL_P(end_tree))
11109 end_tree = save_expr(end_tree);
11110 if (!INTEGRAL_TYPE_P(TREE_TYPE(end_tree)))
11111 end_tree = convert_to_integer(length_type, end_tree);
11112
11113 bad_index = Expression::check_bounds(end_tree, length_type,
11114 bad_index, loc);
11115
8afa2bfb
SD
11116 end_tree = fold_convert_loc(loc.gcc_location(), length_type,
11117 end_tree);
7a938933
ILT
11118 }
11119
11120 static tree strslice_fndecl;
11121 tree ret = Gogo::call_builtin(&strslice_fndecl,
11122 loc,
11123 "__go_string_slice",
11124 3,
11125 string_type,
11126 string_type,
11127 string_tree,
11128 length_type,
11129 start_tree,
11130 length_type,
11131 end_tree);
faff9b04
ILT
11132 if (ret == error_mark_node)
11133 return error_mark_node;
7a938933
ILT
11134 // This will panic if the bounds are out of range for the
11135 // string.
11136 TREE_NOTHROW(strslice_fndecl) = 0;
11137
11138 if (bad_index == boolean_false_node)
11139 return ret;
11140 else
11141 return build2(COMPOUND_EXPR, TREE_TYPE(ret),
11142 build3(COND_EXPR, void_type_node,
11143 bad_index, crash, NULL_TREE),
11144 ret);
11145 }
11146}
11147
16c57fe2
RL
11148// Dump ast representation for a string index expression.
11149
11150void
11151String_index_expression::do_dump_expression(Ast_dump_context* ast_dump_context)
11152 const
11153{
26b8f7eb
ILT
11154 Index_expression::dump_index_expression(ast_dump_context, this->string_,
11155 this->start_, this->end_, NULL);
16c57fe2
RL
11156}
11157
7a938933
ILT
11158// Make a string index expression. END may be NULL.
11159
11160Expression*
11161Expression::make_string_index(Expression* string, Expression* start,
8afa2bfb 11162 Expression* end, Location location)
7a938933
ILT
11163{
11164 return new String_index_expression(string, start, end, location);
11165}
11166
11167// Class Map_index.
11168
11169// Get the type of the map.
11170
11171Map_type*
11172Map_index_expression::get_map_type() const
11173{
11174 Map_type* mt = this->map_->type()->deref()->map_type();
7cfc62ba 11175 if (mt == NULL)
26409c52 11176 go_assert(saw_errors());
7a938933
ILT
11177 return mt;
11178}
11179
11180// Map index traversal.
11181
11182int
11183Map_index_expression::do_traverse(Traverse* traverse)
11184{
11185 if (Expression::traverse(&this->map_, traverse) == TRAVERSE_EXIT)
11186 return TRAVERSE_EXIT;
11187 return Expression::traverse(&this->index_, traverse);
11188}
11189
11190// Return the type of a map index.
11191
11192Type*
11193Map_index_expression::do_type()
11194{
7cfc62ba
ILT
11195 Map_type* mt = this->get_map_type();
11196 if (mt == NULL)
11197 return Type::make_error_type();
11198 Type* type = mt->val_type();
7a938933
ILT
11199 // If this map index is in a tuple assignment, we actually return a
11200 // pointer to the value type. Tuple_map_assignment_statement is
11201 // responsible for handling this correctly. We need to get the type
11202 // right in case this gets assigned to a temporary variable.
11203 if (this->is_in_tuple_assignment_)
11204 type = Type::make_pointer_type(type);
11205 return type;
11206}
11207
11208// Fix the type of a map index.
11209
11210void
11211Map_index_expression::do_determine_type(const Type_context*)
11212{
11213 this->map_->determine_type_no_context();
7cfc62ba
ILT
11214 Map_type* mt = this->get_map_type();
11215 Type* key_type = mt == NULL ? NULL : mt->key_type();
11216 Type_context subcontext(key_type, false);
7a938933
ILT
11217 this->index_->determine_type(&subcontext);
11218}
11219
11220// Check types of a map index.
11221
11222void
11223Map_index_expression::do_check_types(Gogo*)
11224{
11225 std::string reason;
7cfc62ba
ILT
11226 Map_type* mt = this->get_map_type();
11227 if (mt == NULL)
11228 return;
11229 if (!Type::are_assignable(mt->key_type(), this->index_->type(), &reason))
7a938933
ILT
11230 {
11231 if (reason.empty())
11232 this->report_error(_("incompatible type for map index"));
11233 else
11234 {
11235 error_at(this->location(), "incompatible type for map index (%s)",
11236 reason.c_str());
11237 this->set_is_error();
11238 }
11239 }
11240}
11241
11242// Get a tree for a map index.
11243
11244tree
11245Map_index_expression::do_get_tree(Translate_context* context)
11246{
11247 Map_type* type = this->get_map_type();
7cfc62ba
ILT
11248 if (type == NULL)
11249 return error_mark_node;
7a938933
ILT
11250
11251 tree valptr = this->get_value_pointer(context, this->is_lvalue_);
11252 if (valptr == error_mark_node)
11253 return error_mark_node;
11254 valptr = save_expr(valptr);
11255
11256 tree val_type_tree = TREE_TYPE(TREE_TYPE(valptr));
11257
11258 if (this->is_lvalue_)
11259 return build_fold_indirect_ref(valptr);
11260 else if (this->is_in_tuple_assignment_)
11261 {
11262 // Tuple_map_assignment_statement is responsible for using this
11263 // appropriately.
11264 return valptr;
11265 }
11266 else
11267 {
54466dde
ILT
11268 Gogo* gogo = context->gogo();
11269 Btype* val_btype = type->val_type()->get_backend(gogo);
11270 Bexpression* val_zero = gogo->backend()->zero_expression(val_btype);
7a938933
ILT
11271 return fold_build3(COND_EXPR, val_type_tree,
11272 fold_build2(EQ_EXPR, boolean_type_node, valptr,
11273 fold_convert(TREE_TYPE(valptr),
11274 null_pointer_node)),
54466dde 11275 expr_to_tree(val_zero),
7a938933
ILT
11276 build_fold_indirect_ref(valptr));
11277 }
11278}
11279
11280// Get a tree for the map index. This returns a tree which evaluates
11281// to a pointer to a value. The pointer will be NULL if the key is
11282// not in the map.
11283
11284tree
11285Map_index_expression::get_value_pointer(Translate_context* context,
11286 bool insert)
11287{
11288 Map_type* type = this->get_map_type();
7cfc62ba
ILT
11289 if (type == NULL)
11290 return error_mark_node;
7a938933
ILT
11291
11292 tree map_tree = this->map_->get_tree(context);
11293 tree index_tree = this->index_->get_tree(context);
11294 index_tree = Expression::convert_for_assignment(context, type->key_type(),
11295 this->index_->type(),
11296 index_tree,
11297 this->location());
11298 if (map_tree == error_mark_node || index_tree == error_mark_node)
11299 return error_mark_node;
11300
11301 if (this->map_->type()->points_to() != NULL)
11302 map_tree = build_fold_indirect_ref(map_tree);
11303
11304 // We need to pass in a pointer to the key, so stuff it into a
11305 // variable.
76ace672
ILT
11306 tree tmp;
11307 tree make_tmp;
11308 if (current_function_decl != NULL)
11309 {
11310 tmp = create_tmp_var(TREE_TYPE(index_tree), get_name(index_tree));
11311 DECL_IGNORED_P(tmp) = 0;
11312 DECL_INITIAL(tmp) = index_tree;
11313 make_tmp = build1(DECL_EXPR, void_type_node, tmp);
11314 TREE_ADDRESSABLE(tmp) = 1;
11315 }
11316 else
11317 {
8afa2bfb
SD
11318 tmp = build_decl(this->location().gcc_location(), VAR_DECL,
11319 create_tmp_var_name("M"),
76ace672
ILT
11320 TREE_TYPE(index_tree));
11321 DECL_EXTERNAL(tmp) = 0;
11322 TREE_PUBLIC(tmp) = 0;
11323 TREE_STATIC(tmp) = 1;
11324 DECL_ARTIFICIAL(tmp) = 1;
11325 if (!TREE_CONSTANT(index_tree))
8afa2bfb
SD
11326 make_tmp = fold_build2_loc(this->location().gcc_location(),
11327 INIT_EXPR, void_type_node,
76ace672
ILT
11328 tmp, index_tree);
11329 else
11330 {
11331 TREE_READONLY(tmp) = 1;
11332 TREE_CONSTANT(tmp) = 1;
11333 DECL_INITIAL(tmp) = index_tree;
11334 make_tmp = NULL_TREE;
11335 }
11336 rest_of_decl_compilation(tmp, 1, 0);
11337 }
8afa2bfb
SD
11338 tree tmpref =
11339 fold_convert_loc(this->location().gcc_location(), const_ptr_type_node,
11340 build_fold_addr_expr_loc(this->location().gcc_location(),
11341 tmp));
7a938933
ILT
11342
11343 static tree map_index_fndecl;
11344 tree call = Gogo::call_builtin(&map_index_fndecl,
11345 this->location(),
11346 "__go_map_index",
11347 3,
11348 const_ptr_type_node,
11349 TREE_TYPE(map_tree),
11350 map_tree,
11351 const_ptr_type_node,
11352 tmpref,
11353 boolean_type_node,
11354 (insert
11355 ? boolean_true_node
11356 : boolean_false_node));
faff9b04
ILT
11357 if (call == error_mark_node)
11358 return error_mark_node;
7a938933
ILT
11359 // This can panic on a map of interface type if the interface holds
11360 // an uncomparable or unhashable type.
11361 TREE_NOTHROW(map_index_fndecl) = 0;
11362
5b735706
ILT
11363 Type* val_type = type->val_type();
11364 tree val_type_tree = type_to_tree(val_type->get_backend(context->gogo()));
7a938933
ILT
11365 if (val_type_tree == error_mark_node)
11366 return error_mark_node;
11367 tree ptr_val_type_tree = build_pointer_type(val_type_tree);
11368
8afa2bfb
SD
11369 tree ret = fold_convert_loc(this->location().gcc_location(),
11370 ptr_val_type_tree, call);
76ace672
ILT
11371 if (make_tmp != NULL_TREE)
11372 ret = build2(COMPOUND_EXPR, ptr_val_type_tree, make_tmp, ret);
11373 return ret;
7a938933
ILT
11374}
11375
16c57fe2
RL
11376// Dump ast representation for a map index expression
11377
11378void
11379Map_index_expression::do_dump_expression(Ast_dump_context* ast_dump_context)
11380 const
11381{
26b8f7eb
ILT
11382 Index_expression::dump_index_expression(ast_dump_context, this->map_,
11383 this->index_, NULL, NULL);
16c57fe2
RL
11384}
11385
7a938933
ILT
11386// Make a map index expression.
11387
11388Map_index_expression*
11389Expression::make_map_index(Expression* map, Expression* index,
8afa2bfb 11390 Location location)
7a938933
ILT
11391{
11392 return new Map_index_expression(map, index, location);
11393}
11394
11395// Class Field_reference_expression.
11396
744c3195
ILT
11397// Lower a field reference expression. There is nothing to lower, but
11398// this is where we generate the tracking information for fields with
11399// the magic go:"track" tag.
11400
11401Expression*
11402Field_reference_expression::do_lower(Gogo* gogo, Named_object* function,
11403 Statement_inserter* inserter, int)
11404{
11405 Struct_type* struct_type = this->expr_->type()->struct_type();
11406 if (struct_type == NULL)
11407 {
11408 // Error will be reported elsewhere.
11409 return this;
11410 }
11411 const Struct_field* field = struct_type->field(this->field_index_);
11412 if (field == NULL)
11413 return this;
11414 if (!field->has_tag())
11415 return this;
11416 if (field->tag().find("go:\"track\"") == std::string::npos)
11417 return this;
11418
11419 // We have found a reference to a tracked field. Build a call to
11420 // the runtime function __go_fieldtrack with a string that describes
11421 // the field. FIXME: We should only call this once per referenced
11422 // field per function, not once for each reference to the field.
11423
11424 if (this->called_fieldtrack_)
11425 return this;
11426 this->called_fieldtrack_ = true;
11427
11428 Location loc = this->location();
11429
11430 std::string s = "fieldtrack \"";
11431 Named_type* nt = this->expr_->type()->named_type();
11432 if (nt == NULL || nt->named_object()->package() == NULL)
11433 s.append(gogo->pkgpath());
11434 else
11435 s.append(nt->named_object()->package()->pkgpath());
11436 s.push_back('.');
11437 if (nt != NULL)
df9471b6 11438 s.append(Gogo::unpack_hidden_name(nt->name()));
744c3195
ILT
11439 s.push_back('.');
11440 s.append(field->field_name());
11441 s.push_back('"');
11442
11443 // We can't use a string here, because internally a string holds a
11444 // pointer to the actual bytes; when the linker garbage collects the
11445 // string, it won't garbage collect the bytes. So we use a
11446 // [...]byte.
11447
11448 mpz_t val;
11449 mpz_init_set_ui(val, s.length());
11450 Expression* length_expr = Expression::make_integer(&val, NULL, loc);
11451 mpz_clear(val);
11452
11453 Type* byte_type = gogo->lookup_global("byte")->type_value();
11454 Type* array_type = Type::make_array_type(byte_type, length_expr);
11455
11456 Expression_list* bytes = new Expression_list();
11457 for (std::string::const_iterator p = s.begin(); p != s.end(); p++)
11458 {
11459 mpz_init_set_ui(val, *p);
11460 Expression* byte = Expression::make_integer(&val, NULL, loc);
11461 mpz_clear(val);
11462 bytes->push_back(byte);
11463 }
11464
11465 Expression* e = Expression::make_composite_literal(array_type, 0, false,
8ae4c35c 11466 bytes, false, loc);
744c3195
ILT
11467
11468 Variable* var = new Variable(array_type, e, true, false, false, loc);
11469
11470 static int count;
11471 char buf[50];
11472 snprintf(buf, sizeof buf, "fieldtrack.%d", count);
11473 ++count;
11474
11475 Named_object* no = gogo->add_variable(buf, var);
11476 e = Expression::make_var_reference(no, loc);
11477 e = Expression::make_unary(OPERATOR_AND, e, loc);
11478
11479 Expression* call = Runtime::make_call(Runtime::FIELDTRACK, loc, 1, e);
11480 inserter->insert(Statement::make_statement(call, false));
11481
11482 // Put this function, and the global variable we just created, into
11483 // unique sections. This will permit the linker to garbage collect
11484 // them if they are not referenced. The effect is that the only
11485 // strings, indicating field references, that will wind up in the
11486 // executable will be those for functions that are actually needed.
9cc263b8
ILT
11487 if (function != NULL)
11488 function->func_value()->set_in_unique_section();
744c3195
ILT
11489 var->set_in_unique_section();
11490
11491 return this;
11492}
11493
7a938933
ILT
11494// Return the type of a field reference.
11495
11496Type*
11497Field_reference_expression::do_type()
11498{
0f9f95ad 11499 Type* type = this->expr_->type();
02ed921a 11500 if (type->is_error())
0f9f95ad
ILT
11501 return type;
11502 Struct_type* struct_type = type->struct_type();
26409c52 11503 go_assert(struct_type != NULL);
7a938933
ILT
11504 return struct_type->field(this->field_index_)->type();
11505}
11506
11507// Check the types for a field reference.
11508
11509void
11510Field_reference_expression::do_check_types(Gogo*)
11511{
0f9f95ad 11512 Type* type = this->expr_->type();
02ed921a 11513 if (type->is_error())
0f9f95ad
ILT
11514 return;
11515 Struct_type* struct_type = type->struct_type();
26409c52
ILT
11516 go_assert(struct_type != NULL);
11517 go_assert(struct_type->field(this->field_index_) != NULL);
7a938933
ILT
11518}
11519
11520// Get a tree for a field reference.
11521
11522tree
11523Field_reference_expression::do_get_tree(Translate_context* context)
11524{
11525 tree struct_tree = this->expr_->get_tree(context);
11526 if (struct_tree == error_mark_node
11527 || TREE_TYPE(struct_tree) == error_mark_node)
11528 return error_mark_node;
26409c52 11529 go_assert(TREE_CODE(TREE_TYPE(struct_tree)) == RECORD_TYPE);
7a938933 11530 tree field = TYPE_FIELDS(TREE_TYPE(struct_tree));
81ef977e
ILT
11531 if (field == NULL_TREE)
11532 {
11533 // This can happen for a type which refers to itself indirectly
11534 // and then turns out to be erroneous.
26409c52 11535 go_assert(saw_errors());
81ef977e
ILT
11536 return error_mark_node;
11537 }
7a938933
ILT
11538 for (unsigned int i = this->field_index_; i > 0; --i)
11539 {
11540 field = DECL_CHAIN(field);
26409c52 11541 go_assert(field != NULL_TREE);
7a938933 11542 }
e76efdbd
ILT
11543 if (TREE_TYPE(field) == error_mark_node)
11544 return error_mark_node;
7a938933
ILT
11545 return build3(COMPONENT_REF, TREE_TYPE(field), struct_tree, field,
11546 NULL_TREE);
11547}
11548
16c57fe2
RL
11549// Dump ast representation for a field reference expression.
11550
11551void
11552Field_reference_expression::do_dump_expression(
11553 Ast_dump_context* ast_dump_context) const
11554{
11555 this->expr_->dump_expression(ast_dump_context);
11556 ast_dump_context->ostream() << "." << this->field_index_;
11557}
11558
7a938933
ILT
11559// Make a reference to a qualified identifier in an expression.
11560
11561Field_reference_expression*
11562Expression::make_field_reference(Expression* expr, unsigned int field_index,
8afa2bfb 11563 Location location)
7a938933
ILT
11564{
11565 return new Field_reference_expression(expr, field_index, location);
11566}
11567
11568// Class Interface_field_reference_expression.
11569
11570// Return a tree for the pointer to the function to call.
11571
11572tree
11573Interface_field_reference_expression::get_function_tree(Translate_context*,
11574 tree expr)
11575{
11576 if (this->expr_->type()->points_to() != NULL)
11577 expr = build_fold_indirect_ref(expr);
11578
11579 tree expr_type = TREE_TYPE(expr);
26409c52 11580 go_assert(TREE_CODE(expr_type) == RECORD_TYPE);
7a938933
ILT
11581
11582 tree field = TYPE_FIELDS(expr_type);
26409c52 11583 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__methods") == 0);
7a938933
ILT
11584
11585 tree table = build3(COMPONENT_REF, TREE_TYPE(field), expr, field, NULL_TREE);
26409c52 11586 go_assert(POINTER_TYPE_P(TREE_TYPE(table)));
7a938933
ILT
11587
11588 table = build_fold_indirect_ref(table);
26409c52 11589 go_assert(TREE_CODE(TREE_TYPE(table)) == RECORD_TYPE);
7a938933
ILT
11590
11591 std::string name = Gogo::unpack_hidden_name(this->name_);
11592 for (field = DECL_CHAIN(TYPE_FIELDS(TREE_TYPE(table)));
11593 field != NULL_TREE;
11594 field = DECL_CHAIN(field))
11595 {
11596 if (name == IDENTIFIER_POINTER(DECL_NAME(field)))
11597 break;
11598 }
26409c52 11599 go_assert(field != NULL_TREE);
7a938933
ILT
11600
11601 return build3(COMPONENT_REF, TREE_TYPE(field), table, field, NULL_TREE);
11602}
11603
11604// Return a tree for the first argument to pass to the interface
11605// function.
11606
11607tree
11608Interface_field_reference_expression::get_underlying_object_tree(
11609 Translate_context*,
11610 tree expr)
11611{
11612 if (this->expr_->type()->points_to() != NULL)
11613 expr = build_fold_indirect_ref(expr);
11614
11615 tree expr_type = TREE_TYPE(expr);
26409c52 11616 go_assert(TREE_CODE(expr_type) == RECORD_TYPE);
7a938933
ILT
11617
11618 tree field = DECL_CHAIN(TYPE_FIELDS(expr_type));
26409c52 11619 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__object") == 0);
7a938933
ILT
11620
11621 return build3(COMPONENT_REF, TREE_TYPE(field), expr, field, NULL_TREE);
11622}
11623
11624// Traversal.
11625
11626int
11627Interface_field_reference_expression::do_traverse(Traverse* traverse)
11628{
11629 return Expression::traverse(&this->expr_, traverse);
11630}
11631
571d3f91
ILT
11632// Lower the expression. If this expression is not called, we need to
11633// evaluate the expression twice when converting to the backend
11634// interface. So introduce a temporary variable if necessary.
11635
11636Expression*
11637Interface_field_reference_expression::do_lower(Gogo*, Named_object*,
11638 Statement_inserter* inserter,
11639 int)
11640{
11641 if (this->expr_->var_expression() == NULL
11642 && this->expr_->temporary_reference_expression() == NULL
11643 && this->expr_->set_and_use_temporary_expression() == NULL)
11644 {
11645 Temporary_statement* temp =
11646 Statement::make_temporary(this->expr_->type(), NULL, this->location());
11647 inserter->insert(temp);
11648 this->expr_ = Expression::make_set_and_use_temporary(temp, this->expr_,
11649 this->location());
11650 }
11651 return this;
11652}
11653
7a938933
ILT
11654// Return the type of an interface field reference.
11655
11656Type*
11657Interface_field_reference_expression::do_type()
11658{
11659 Type* expr_type = this->expr_->type();
11660
11661 Type* points_to = expr_type->points_to();
11662 if (points_to != NULL)
11663 expr_type = points_to;
11664
11665 Interface_type* interface_type = expr_type->interface_type();
11666 if (interface_type == NULL)
11667 return Type::make_error_type();
11668
11669 const Typed_identifier* method = interface_type->find_method(this->name_);
11670 if (method == NULL)
11671 return Type::make_error_type();
11672
11673 return method->type();
11674}
11675
11676// Determine types.
11677
11678void
11679Interface_field_reference_expression::do_determine_type(const Type_context*)
11680{
11681 this->expr_->determine_type_no_context();
11682}
11683
11684// Check the types for an interface field reference.
11685
11686void
11687Interface_field_reference_expression::do_check_types(Gogo*)
11688{
11689 Type* type = this->expr_->type();
11690
11691 Type* points_to = type->points_to();
11692 if (points_to != NULL)
11693 type = points_to;
11694
11695 Interface_type* interface_type = type->interface_type();
11696 if (interface_type == NULL)
f7b8b261
ILT
11697 {
11698 if (!type->is_error_type())
11699 this->report_error(_("expected interface or pointer to interface"));
11700 }
7a938933
ILT
11701 else
11702 {
11703 const Typed_identifier* method =
11704 interface_type->find_method(this->name_);
11705 if (method == NULL)
11706 {
11707 error_at(this->location(), "method %qs not in interface",
11708 Gogo::message_name(this->name_).c_str());
11709 this->set_is_error();
11710 }
11711 }
11712}
11713
571d3f91
ILT
11714// If an interface field reference is not simply called, then it is
11715// represented as a closure. The closure will hold a single variable,
11716// the value of the interface on which the method should be called.
11717// The function will be a simple thunk that pulls the value from the
11718// closure and calls the method with the remaining arguments.
11719
11720// Because method values are not common, we don't build all thunks for
11721// all possible interface methods, but instead only build them as we
11722// need them. In particular, we even build them on demand for
11723// interface methods defined in other packages.
11724
11725Interface_field_reference_expression::Interface_method_thunks
11726 Interface_field_reference_expression::interface_method_thunks;
11727
11728// Find or create the thunk to call method NAME on TYPE.
11729
11730Named_object*
11731Interface_field_reference_expression::create_thunk(Gogo* gogo,
11732 Interface_type* type,
11733 const std::string& name)
11734{
11735 std::pair<Interface_type*, Method_thunks*> val(type, NULL);
11736 std::pair<Interface_method_thunks::iterator, bool> ins =
11737 Interface_field_reference_expression::interface_method_thunks.insert(val);
11738 if (ins.second)
11739 {
11740 // This is the first time we have seen this interface.
11741 ins.first->second = new Method_thunks();
11742 }
11743
11744 for (Method_thunks::const_iterator p = ins.first->second->begin();
11745 p != ins.first->second->end();
11746 p++)
11747 if (p->first == name)
11748 return p->second;
11749
11750 Location loc = type->location();
11751
11752 const Typed_identifier* method_id = type->find_method(name);
11753 if (method_id == NULL)
11754 return Named_object::make_erroneous_name(Gogo::thunk_name());
11755
11756 Function_type* orig_fntype = method_id->type()->function_type();
11757 if (orig_fntype == NULL)
11758 return Named_object::make_erroneous_name(Gogo::thunk_name());
11759
11760 Struct_field_list* sfl = new Struct_field_list();
05a7d566
ILT
11761 // The type here is wrong--it should be the C function type. But it
11762 // doesn't really matter.
571d3f91
ILT
11763 Type* vt = Type::make_pointer_type(Type::make_void_type());
11764 sfl->push_back(Struct_field(Typed_identifier("fn.0", vt, loc)));
11765 sfl->push_back(Struct_field(Typed_identifier("val.1", type, loc)));
11766 Type* closure_type = Type::make_struct_type(sfl, loc);
11767 closure_type = Type::make_pointer_type(closure_type);
11768
05a7d566 11769 Function_type* new_fntype = orig_fntype->copy_with_names();
571d3f91
ILT
11770
11771 Named_object* new_no = gogo->start_function(Gogo::thunk_name(), new_fntype,
11772 false, loc);
11773
05a7d566
ILT
11774 Variable* cvar = new Variable(closure_type, NULL, false, false, false, loc);
11775 cvar->set_is_used();
11776 Named_object* cp = Named_object::make_variable("$closure", NULL, cvar);
11777 new_no->func_value()->set_closure_var(cp);
571d3f91 11778
05a7d566 11779 gogo->start_block(loc);
571d3f91
ILT
11780
11781 // Field 0 of the closure is the function code pointer, field 1 is
11782 // the value on which to invoke the method.
11783 Expression* arg = Expression::make_var_reference(cp, loc);
11784 arg = Expression::make_unary(OPERATOR_MULT, arg, loc);
11785 arg = Expression::make_field_reference(arg, 1, loc);
11786
11787 Expression *ifre = Expression::make_interface_field_reference(arg, name,
11788 loc);
11789
11790 const Typed_identifier_list* orig_params = orig_fntype->parameters();
11791 Expression_list* args;
11792 if (orig_params == NULL || orig_params->empty())
11793 args = NULL;
11794 else
11795 {
11796 const Typed_identifier_list* new_params = new_fntype->parameters();
11797 args = new Expression_list();
11798 for (Typed_identifier_list::const_iterator p = new_params->begin();
05a7d566 11799 p != new_params->end();
571d3f91
ILT
11800 ++p)
11801 {
11802 Named_object* p_no = gogo->lookup(p->name(), NULL);
11803 go_assert(p_no != NULL
11804 && p_no->is_variable()
11805 && p_no->var_value()->is_parameter());
11806 args->push_back(Expression::make_var_reference(p_no, loc));
11807 }
11808 }
11809
11810 Call_expression* call = Expression::make_call(ifre, args,
11811 orig_fntype->is_varargs(),
11812 loc);
11813 call->set_varargs_are_lowered();
11814
11815 Statement* s = Statement::make_return_from_call(call, loc);
11816 gogo->add_statement(s);
11817 Block* b = gogo->finish_block(loc);
11818 gogo->add_block(b, loc);
11819 gogo->lower_block(new_no, b);
11820 gogo->finish_function(loc);
11821
11822 ins.first->second->push_back(std::make_pair(name, new_no));
11823 return new_no;
11824}
11825
11826// Get a tree for a method value.
7a938933
ILT
11827
11828tree
571d3f91 11829Interface_field_reference_expression::do_get_tree(Translate_context* context)
7a938933 11830{
571d3f91
ILT
11831 Interface_type* type = this->expr_->type()->interface_type();
11832 if (type == NULL)
11833 {
11834 go_assert(saw_errors());
11835 return error_mark_node;
11836 }
11837
11838 Named_object* thunk =
11839 Interface_field_reference_expression::create_thunk(context->gogo(),
11840 type, this->name_);
11841 if (thunk->is_erroneous())
11842 {
11843 go_assert(saw_errors());
11844 return error_mark_node;
11845 }
11846
11847 // FIXME: We should lower this earlier, but we can't it lower it in
11848 // the lowering pass because at that point we don't know whether we
11849 // need to create the thunk or not. If the expression is called, we
11850 // don't need the thunk.
11851
11852 Location loc = this->location();
11853
11854 Struct_field_list* fields = new Struct_field_list();
11855 fields->push_back(Struct_field(Typed_identifier("fn.0",
11856 thunk->func_value()->type(),
11857 loc)));
11858 fields->push_back(Struct_field(Typed_identifier("val.1",
11859 this->expr_->type(),
11860 loc)));
11861 Struct_type* st = Type::make_struct_type(fields, loc);
11862
11863 Expression_list* vals = new Expression_list();
11864 vals->push_back(Expression::make_func_code_reference(thunk, loc));
11865 vals->push_back(this->expr_);
11866
11867 Expression* expr = Expression::make_struct_composite_literal(st, vals, loc);
11868 expr = Expression::make_heap_composite(expr, loc);
11869
11870 tree closure_tree = expr->get_tree(context);
11871
11872 // Note that we are evaluating this->expr_ twice, but that is OK
11873 // because in the lowering pass we forced it into a temporary
11874 // variable.
571d3f91
ILT
11875 tree nil_check_tree = Expression::comparison_tree(context,
11876 Type::lookup_bool_type(),
11877 OPERATOR_EQEQ,
58c55a32
ILT
11878 this->expr_,
11879 Expression::make_nil(loc),
571d3f91
ILT
11880 loc);
11881 tree crash = context->gogo()->runtime_error(RUNTIME_ERROR_NIL_DEREFERENCE,
11882 loc);
11883 if (closure_tree == error_mark_node
11884 || nil_check_tree == error_mark_node
11885 || crash == error_mark_node)
11886 return error_mark_node;
11887 return fold_build2_loc(loc.gcc_location(), COMPOUND_EXPR,
11888 TREE_TYPE(closure_tree),
11889 build3_loc(loc.gcc_location(), COND_EXPR,
11890 void_type_node, nil_check_tree, crash,
11891 NULL_TREE),
11892 closure_tree);
7a938933
ILT
11893}
11894
16c57fe2
RL
11895// Dump ast representation for an interface field reference.
11896
11897void
11898Interface_field_reference_expression::do_dump_expression(
11899 Ast_dump_context* ast_dump_context) const
11900{
11901 this->expr_->dump_expression(ast_dump_context);
11902 ast_dump_context->ostream() << "." << this->name_;
11903}
11904
7a938933
ILT
11905// Make a reference to a field in an interface.
11906
11907Expression*
11908Expression::make_interface_field_reference(Expression* expr,
11909 const std::string& field,
8afa2bfb 11910 Location location)
7a938933
ILT
11911{
11912 return new Interface_field_reference_expression(expr, field, location);
11913}
11914
11915// A general selector. This is a Parser_expression for LEFT.NAME. It
11916// is lowered after we know the type of the left hand side.
11917
11918class Selector_expression : public Parser_expression
11919{
11920 public:
11921 Selector_expression(Expression* left, const std::string& name,
8afa2bfb 11922 Location location)
7a938933
ILT
11923 : Parser_expression(EXPRESSION_SELECTOR, location),
11924 left_(left), name_(name)
11925 { }
11926
11927 protected:
11928 int
11929 do_traverse(Traverse* traverse)
11930 { return Expression::traverse(&this->left_, traverse); }
11931
11932 Expression*
8586635c 11933 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
7a938933
ILT
11934
11935 Expression*
11936 do_copy()
11937 {
11938 return new Selector_expression(this->left_->copy(), this->name_,
11939 this->location());
11940 }
11941
16c57fe2
RL
11942 void
11943 do_dump_expression(Ast_dump_context* ast_dump_context) const;
11944
7a938933
ILT
11945 private:
11946 Expression*
11947 lower_method_expression(Gogo*);
11948
11949 // The expression on the left hand side.
11950 Expression* left_;
11951 // The name on the right hand side.
11952 std::string name_;
11953};
11954
11955// Lower a selector expression once we know the real type of the left
11956// hand side.
11957
11958Expression*
8586635c
ILT
11959Selector_expression::do_lower(Gogo* gogo, Named_object*, Statement_inserter*,
11960 int)
7a938933
ILT
11961{
11962 Expression* left = this->left_;
11963 if (left->is_type_expression())
11964 return this->lower_method_expression(gogo);
11965 return Type::bind_field_or_method(gogo, left->type(), left, this->name_,
11966 this->location());
11967}
11968
11969// Lower a method expression T.M or (*T).M. We turn this into a
11970// function literal.
11971
11972Expression*
11973Selector_expression::lower_method_expression(Gogo* gogo)
11974{
8afa2bfb 11975 Location location = this->location();
7a938933
ILT
11976 Type* type = this->left_->type();
11977 const std::string& name(this->name_);
11978
11979 bool is_pointer;
11980 if (type->points_to() == NULL)
11981 is_pointer = false;
11982 else
11983 {
11984 is_pointer = true;
11985 type = type->points_to();
11986 }
11987 Named_type* nt = type->named_type();
11988 if (nt == NULL)
11989 {
11990 error_at(location,
11991 ("method expression requires named type or "
11992 "pointer to named type"));
11993 return Expression::make_error(location);
11994 }
11995
11996 bool is_ambiguous;
11997 Method* method = nt->method_function(name, &is_ambiguous);
80607544 11998 const Typed_identifier* imethod = NULL;
e45f44f3 11999 if (method == NULL && !is_pointer)
80607544
ILT
12000 {
12001 Interface_type* it = nt->interface_type();
12002 if (it != NULL)
12003 imethod = it->find_method(name);
12004 }
12005
12006 if (method == NULL && imethod == NULL)
7a938933
ILT
12007 {
12008 if (!is_ambiguous)
e45f44f3
ILT
12009 error_at(location, "type %<%s%s%> has no method %<%s%>",
12010 is_pointer ? "*" : "",
7a938933
ILT
12011 nt->message_name().c_str(),
12012 Gogo::message_name(name).c_str());
12013 else
e45f44f3 12014 error_at(location, "method %<%s%s%> is ambiguous in type %<%s%>",
7a938933 12015 Gogo::message_name(name).c_str(),
e45f44f3 12016 is_pointer ? "*" : "",
7a938933
ILT
12017 nt->message_name().c_str());
12018 return Expression::make_error(location);
12019 }
12020
80607544 12021 if (method != NULL && !is_pointer && !method->is_value_method())
7a938933
ILT
12022 {
12023 error_at(location, "method requires pointer (use %<(*%s).%s)%>",
12024 nt->message_name().c_str(),
12025 Gogo::message_name(name).c_str());
12026 return Expression::make_error(location);
12027 }
12028
12029 // Build a new function type in which the receiver becomes the first
12030 // argument.
80607544
ILT
12031 Function_type* method_type;
12032 if (method != NULL)
12033 {
12034 method_type = method->type();
26409c52 12035 go_assert(method_type->is_method());
80607544
ILT
12036 }
12037 else
12038 {
12039 method_type = imethod->type()->function_type();
26409c52 12040 go_assert(method_type != NULL && !method_type->is_method());
80607544 12041 }
7a938933
ILT
12042
12043 const char* const receiver_name = "$this";
12044 Typed_identifier_list* parameters = new Typed_identifier_list();
12045 parameters->push_back(Typed_identifier(receiver_name, this->left_->type(),
12046 location));
12047
12048 const Typed_identifier_list* method_parameters = method_type->parameters();
12049 if (method_parameters != NULL)
12050 {
3375a6c9 12051 int i = 0;
7a938933
ILT
12052 for (Typed_identifier_list::const_iterator p = method_parameters->begin();
12053 p != method_parameters->end();
3375a6c9
ILT
12054 ++p, ++i)
12055 {
b2c4b7b9 12056 if (!p->name().empty())
3375a6c9
ILT
12057 parameters->push_back(*p);
12058 else
12059 {
12060 char buf[20];
12061 snprintf(buf, sizeof buf, "$param%d", i);
12062 parameters->push_back(Typed_identifier(buf, p->type(),
12063 p->location()));
12064 }
12065 }
7a938933
ILT
12066 }
12067
12068 const Typed_identifier_list* method_results = method_type->results();
12069 Typed_identifier_list* results;
12070 if (method_results == NULL)
12071 results = NULL;
12072 else
12073 {
12074 results = new Typed_identifier_list();
12075 for (Typed_identifier_list::const_iterator p = method_results->begin();
12076 p != method_results->end();
12077 ++p)
12078 results->push_back(*p);
12079 }
12080
12081 Function_type* fntype = Type::make_function_type(NULL, parameters, results,
12082 location);
12083 if (method_type->is_varargs())
12084 fntype->set_is_varargs();
12085
12086 // We generate methods which always takes a pointer to the receiver
12087 // as their first argument. If this is for a pointer type, we can
12088 // simply reuse the existing function. We use an internal hack to
12089 // get the right type.
fdbc38a6
ILT
12090 // FIXME: This optimization is disabled because it doesn't yet work
12091 // with function descriptors when the method expression is not
12092 // directly called.
12093 if (method != NULL && is_pointer && false)
7a938933
ILT
12094 {
12095 Named_object* mno = (method->needs_stub_method()
12096 ? method->stub_object()
12097 : method->named_object());
12098 Expression* f = Expression::make_func_reference(mno, NULL, location);
12099 f = Expression::make_cast(fntype, f, location);
12100 Type_conversion_expression* tce =
12101 static_cast<Type_conversion_expression*>(f);
12102 tce->set_may_convert_function_types();
12103 return f;
12104 }
12105
12106 Named_object* no = gogo->start_function(Gogo::thunk_name(), fntype, false,
12107 location);
12108
12109 Named_object* vno = gogo->lookup(receiver_name, NULL);
26409c52 12110 go_assert(vno != NULL);
7a938933 12111 Expression* ve = Expression::make_var_reference(vno, location);
80607544
ILT
12112 Expression* bm;
12113 if (method != NULL)
12114 bm = Type::bind_field_or_method(gogo, nt, ve, name, location);
12115 else
12116 bm = Expression::make_interface_field_reference(ve, name, location);
1e2afadb
ILT
12117
12118 // Even though we found the method above, if it has an error type we
12119 // may see an error here.
12120 if (bm->is_error_expression())
196bc407
ILT
12121 {
12122 gogo->finish_function(location);
12123 return bm;
12124 }
7a938933
ILT
12125
12126 Expression_list* args;
3375a6c9 12127 if (parameters->size() <= 1)
7a938933
ILT
12128 args = NULL;
12129 else
12130 {
12131 args = new Expression_list();
3375a6c9
ILT
12132 Typed_identifier_list::const_iterator p = parameters->begin();
12133 ++p;
12134 for (; p != parameters->end(); ++p)
7a938933
ILT
12135 {
12136 vno = gogo->lookup(p->name(), NULL);
26409c52 12137 go_assert(vno != NULL);
7a938933
ILT
12138 args->push_back(Expression::make_var_reference(vno, location));
12139 }
12140 }
12141
8586635c
ILT
12142 gogo->start_block(location);
12143
7a938933
ILT
12144 Call_expression* call = Expression::make_call(bm, args,
12145 method_type->is_varargs(),
12146 location);
12147
571d3f91 12148 Statement* s = Statement::make_return_from_call(call, location);
7a938933
ILT
12149 gogo->add_statement(s);
12150
8586635c
ILT
12151 Block* b = gogo->finish_block(location);
12152
12153 gogo->add_block(b, location);
12154
12155 // Lower the call in case there are multiple results.
12156 gogo->lower_block(no, b);
12157
7a938933
ILT
12158 gogo->finish_function(location);
12159
12160 return Expression::make_func_reference(no, NULL, location);
12161}
12162
16c57fe2
RL
12163// Dump the ast for a selector expression.
12164
12165void
12166Selector_expression::do_dump_expression(Ast_dump_context* ast_dump_context)
12167 const
12168{
12169 ast_dump_context->dump_expression(this->left_);
12170 ast_dump_context->ostream() << ".";
12171 ast_dump_context->ostream() << this->name_;
12172}
12173
7a938933
ILT
12174// Make a selector expression.
12175
12176Expression*
12177Expression::make_selector(Expression* left, const std::string& name,
8afa2bfb 12178 Location location)
7a938933
ILT
12179{
12180 return new Selector_expression(left, name, location);
12181}
12182
12183// Implement the builtin function new.
12184
12185class Allocation_expression : public Expression
12186{
12187 public:
8afa2bfb 12188 Allocation_expression(Type* type, Location location)
7a938933
ILT
12189 : Expression(EXPRESSION_ALLOCATION, location),
12190 type_(type)
12191 { }
12192
12193 protected:
12194 int
12195 do_traverse(Traverse* traverse)
12196 { return Type::traverse(this->type_, traverse); }
12197
12198 Type*
12199 do_type()
12200 { return Type::make_pointer_type(this->type_); }
12201
12202 void
12203 do_determine_type(const Type_context*)
12204 { }
12205
7a938933
ILT
12206 Expression*
12207 do_copy()
12208 { return new Allocation_expression(this->type_, this->location()); }
12209
12210 tree
12211 do_get_tree(Translate_context*);
12212
16c57fe2
RL
12213 void
12214 do_dump_expression(Ast_dump_context*) const;
12215
7a938933
ILT
12216 private:
12217 // The type we are allocating.
12218 Type* type_;
12219};
12220
7a938933
ILT
12221// Return a tree for an allocation expression.
12222
12223tree
12224Allocation_expression::do_get_tree(Translate_context* context)
12225{
5b735706 12226 tree type_tree = type_to_tree(this->type_->get_backend(context->gogo()));
67eff3d9
ILT
12227 if (type_tree == error_mark_node)
12228 return error_mark_node;
7a938933
ILT
12229 tree size_tree = TYPE_SIZE_UNIT(type_tree);
12230 tree space = context->gogo()->allocate_memory(this->type_, size_tree,
12231 this->location());
67eff3d9
ILT
12232 if (space == error_mark_node)
12233 return error_mark_node;
7a938933
ILT
12234 return fold_convert(build_pointer_type(type_tree), space);
12235}
12236
16c57fe2
RL
12237// Dump ast representation for an allocation expression.
12238
12239void
12240Allocation_expression::do_dump_expression(Ast_dump_context* ast_dump_context)
12241 const
12242{
12243 ast_dump_context->ostream() << "new(";
12244 ast_dump_context->dump_type(this->type_);
12245 ast_dump_context->ostream() << ")";
12246}
12247
7a938933
ILT
12248// Make an allocation expression.
12249
12250Expression*
8afa2bfb 12251Expression::make_allocation(Type* type, Location location)
7a938933
ILT
12252{
12253 return new Allocation_expression(type, location);
12254}
12255
7a938933
ILT
12256// Construct a struct.
12257
12258class Struct_construction_expression : public Expression
12259{
12260 public:
12261 Struct_construction_expression(Type* type, Expression_list* vals,
8afa2bfb 12262 Location location)
7a938933 12263 : Expression(EXPRESSION_STRUCT_CONSTRUCTION, location),
50f671c6 12264 type_(type), vals_(vals), traverse_order_(NULL)
7a938933
ILT
12265 { }
12266
50f671c6
ILT
12267 // Set the traversal order, used to ensure that we implement the
12268 // order of evaluation rules. Takes ownership of the argument.
12269 void
12270 set_traverse_order(std::vector<int>* traverse_order)
12271 { this->traverse_order_ = traverse_order; }
12272
7a938933
ILT
12273 // Return whether this is a constant initializer.
12274 bool
12275 is_constant_struct() const;
12276
12277 protected:
12278 int
12279 do_traverse(Traverse* traverse);
12280
12281 Type*
12282 do_type()
12283 { return this->type_; }
12284
12285 void
12286 do_determine_type(const Type_context*);
12287
12288 void
12289 do_check_types(Gogo*);
12290
12291 Expression*
12292 do_copy()
12293 {
50f671c6
ILT
12294 Struct_construction_expression* ret =
12295 new Struct_construction_expression(this->type_, this->vals_->copy(),
12296 this->location());
12297 if (this->traverse_order_ != NULL)
12298 ret->set_traverse_order(this->traverse_order_);
12299 return ret;
7a938933
ILT
12300 }
12301
7a938933
ILT
12302 tree
12303 do_get_tree(Translate_context*);
12304
12305 void
12306 do_export(Export*) const;
12307
16c57fe2
RL
12308 void
12309 do_dump_expression(Ast_dump_context*) const;
12310
7a938933
ILT
12311 private:
12312 // The type of the struct to construct.
12313 Type* type_;
12314 // The list of values, in order of the fields in the struct. A NULL
12315 // entry means that the field should be zero-initialized.
12316 Expression_list* vals_;
50f671c6
ILT
12317 // If not NULL, the order in which to traverse vals_. This is used
12318 // so that we implement the order of evaluation rules correctly.
12319 std::vector<int>* traverse_order_;
7a938933
ILT
12320};
12321
12322// Traversal.
12323
12324int
12325Struct_construction_expression::do_traverse(Traverse* traverse)
12326{
50f671c6
ILT
12327 if (this->vals_ != NULL)
12328 {
12329 if (this->traverse_order_ == NULL)
12330 {
12331 if (this->vals_->traverse(traverse) == TRAVERSE_EXIT)
12332 return TRAVERSE_EXIT;
12333 }
12334 else
12335 {
12336 for (std::vector<int>::const_iterator p =
12337 this->traverse_order_->begin();
12338 p != this->traverse_order_->end();
12339 ++p)
12340 {
12341 if (Expression::traverse(&this->vals_->at(*p), traverse)
12342 == TRAVERSE_EXIT)
12343 return TRAVERSE_EXIT;
12344 }
12345 }
12346 }
7a938933
ILT
12347 if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
12348 return TRAVERSE_EXIT;
12349 return TRAVERSE_CONTINUE;
12350}
12351
12352// Return whether this is a constant initializer.
12353
12354bool
12355Struct_construction_expression::is_constant_struct() const
12356{
12357 if (this->vals_ == NULL)
12358 return true;
12359 for (Expression_list::const_iterator pv = this->vals_->begin();
12360 pv != this->vals_->end();
12361 ++pv)
12362 {
12363 if (*pv != NULL
12364 && !(*pv)->is_constant()
12365 && (!(*pv)->is_composite_literal()
12366 || (*pv)->is_nonconstant_composite_literal()))
12367 return false;
12368 }
12369
12370 const Struct_field_list* fields = this->type_->struct_type()->fields();
12371 for (Struct_field_list::const_iterator pf = fields->begin();
12372 pf != fields->end();
12373 ++pf)
12374 {
12375 // There are no constant constructors for interfaces.
12376 if (pf->type()->interface_type() != NULL)
12377 return false;
12378 }
12379
12380 return true;
12381}
12382
12383// Final type determination.
12384
12385void
12386Struct_construction_expression::do_determine_type(const Type_context*)
12387{
12388 if (this->vals_ == NULL)
12389 return;
12390 const Struct_field_list* fields = this->type_->struct_type()->fields();
12391 Expression_list::const_iterator pv = this->vals_->begin();
12392 for (Struct_field_list::const_iterator pf = fields->begin();
12393 pf != fields->end();
12394 ++pf, ++pv)
12395 {
12396 if (pv == this->vals_->end())
12397 return;
12398 if (*pv != NULL)
12399 {
12400 Type_context subcontext(pf->type(), false);
12401 (*pv)->determine_type(&subcontext);
12402 }
12403 }
9081b67b
ILT
12404 // Extra values are an error we will report elsewhere; we still want
12405 // to determine the type to avoid knockon errors.
12406 for (; pv != this->vals_->end(); ++pv)
12407 (*pv)->determine_type_no_context();
7a938933
ILT
12408}
12409
12410// Check types.
12411
12412void
12413Struct_construction_expression::do_check_types(Gogo*)
12414{
12415 if (this->vals_ == NULL)
12416 return;
12417
12418 Struct_type* st = this->type_->struct_type();
12419 if (this->vals_->size() > st->field_count())
12420 {
12421 this->report_error(_("too many expressions for struct"));
12422 return;
12423 }
12424
12425 const Struct_field_list* fields = st->fields();
12426 Expression_list::const_iterator pv = this->vals_->begin();
12427 int i = 0;
12428 for (Struct_field_list::const_iterator pf = fields->begin();
12429 pf != fields->end();
12430 ++pf, ++pv, ++i)
12431 {
12432 if (pv == this->vals_->end())
12433 {
12434 this->report_error(_("too few expressions for struct"));
12435 break;
12436 }
12437
12438 if (*pv == NULL)
12439 continue;
12440
12441 std::string reason;
12442 if (!Type::are_assignable(pf->type(), (*pv)->type(), &reason))
12443 {
12444 if (reason.empty())
12445 error_at((*pv)->location(),
12446 "incompatible type for field %d in struct construction",
12447 i + 1);
12448 else
12449 error_at((*pv)->location(),
12450 ("incompatible type for field %d in "
12451 "struct construction (%s)"),
12452 i + 1, reason.c_str());
12453 this->set_is_error();
12454 }
12455 }
26409c52 12456 go_assert(pv == this->vals_->end());
7a938933
ILT
12457}
12458
12459// Return a tree for constructing a struct.
12460
12461tree
12462Struct_construction_expression::do_get_tree(Translate_context* context)
12463{
12464 Gogo* gogo = context->gogo();
12465
12466 if (this->vals_ == NULL)
54466dde
ILT
12467 {
12468 Btype* btype = this->type_->get_backend(gogo);
12469 return expr_to_tree(gogo->backend()->zero_expression(btype));
12470 }
7a938933 12471
5b735706 12472 tree type_tree = type_to_tree(this->type_->get_backend(gogo));
7a938933
ILT
12473 if (type_tree == error_mark_node)
12474 return error_mark_node;
26409c52 12475 go_assert(TREE_CODE(type_tree) == RECORD_TYPE);
7a938933
ILT
12476
12477 bool is_constant = true;
12478 const Struct_field_list* fields = this->type_->struct_type()->fields();
4efdbe5a
ILT
12479 vec<constructor_elt, va_gc> *elts;
12480 vec_alloc (elts, fields->size());
7a938933
ILT
12481 Struct_field_list::const_iterator pf = fields->begin();
12482 Expression_list::const_iterator pv = this->vals_->begin();
12483 for (tree field = TYPE_FIELDS(type_tree);
12484 field != NULL_TREE;
12485 field = DECL_CHAIN(field), ++pf)
12486 {
26409c52 12487 go_assert(pf != fields->end());
7a938933 12488
54466dde
ILT
12489 Btype* fbtype = pf->type()->get_backend(gogo);
12490
7a938933
ILT
12491 tree val;
12492 if (pv == this->vals_->end())
54466dde 12493 val = expr_to_tree(gogo->backend()->zero_expression(fbtype));
7a938933
ILT
12494 else if (*pv == NULL)
12495 {
54466dde 12496 val = expr_to_tree(gogo->backend()->zero_expression(fbtype));
7a938933
ILT
12497 ++pv;
12498 }
12499 else
12500 {
12501 val = Expression::convert_for_assignment(context, pf->type(),
12502 (*pv)->type(),
12503 (*pv)->get_tree(context),
12504 this->location());
12505 ++pv;
12506 }
12507
12508 if (val == error_mark_node || TREE_TYPE(val) == error_mark_node)
12509 return error_mark_node;
12510
f32682ca 12511 constructor_elt empty = {NULL, NULL};
4efdbe5a 12512 constructor_elt* elt = elts->quick_push(empty);
7a938933
ILT
12513 elt->index = field;
12514 elt->value = val;
12515 if (!TREE_CONSTANT(val))
12516 is_constant = false;
12517 }
26409c52 12518 go_assert(pf == fields->end());
7a938933
ILT
12519
12520 tree ret = build_constructor(type_tree, elts);
12521 if (is_constant)
12522 TREE_CONSTANT(ret) = 1;
12523 return ret;
12524}
12525
12526// Export a struct construction.
12527
12528void
12529Struct_construction_expression::do_export(Export* exp) const
12530{
12531 exp->write_c_string("convert(");
12532 exp->write_type(this->type_);
12533 for (Expression_list::const_iterator pv = this->vals_->begin();
12534 pv != this->vals_->end();
12535 ++pv)
12536 {
12537 exp->write_c_string(", ");
12538 if (*pv != NULL)
12539 (*pv)->export_expression(exp);
12540 }
12541 exp->write_c_string(")");
12542}
12543
16c57fe2
RL
12544// Dump ast representation of a struct construction expression.
12545
12546void
12547Struct_construction_expression::do_dump_expression(
12548 Ast_dump_context* ast_dump_context) const
12549{
16c57fe2
RL
12550 ast_dump_context->dump_type(this->type_);
12551 ast_dump_context->ostream() << "{";
12552 ast_dump_context->dump_expression_list(this->vals_);
12553 ast_dump_context->ostream() << "}";
12554}
12555
7a938933
ILT
12556// Make a struct composite literal. This used by the thunk code.
12557
12558Expression*
12559Expression::make_struct_composite_literal(Type* type, Expression_list* vals,
8afa2bfb 12560 Location location)
7a938933 12561{
26409c52 12562 go_assert(type->struct_type() != NULL);
7a938933
ILT
12563 return new Struct_construction_expression(type, vals, location);
12564}
12565
12566// Construct an array. This class is not used directly; instead we
12567// use the child classes, Fixed_array_construction_expression and
12568// Open_array_construction_expression.
12569
12570class Array_construction_expression : public Expression
12571{
12572 protected:
12573 Array_construction_expression(Expression_classification classification,
52556f04
ILT
12574 Type* type,
12575 const std::vector<unsigned long>* indexes,
12576 Expression_list* vals, Location location)
7a938933 12577 : Expression(classification, location),
52556f04
ILT
12578 type_(type), indexes_(indexes), vals_(vals)
12579 { go_assert(indexes == NULL || indexes->size() == vals->size()); }
7a938933
ILT
12580
12581 public:
12582 // Return whether this is a constant initializer.
12583 bool
12584 is_constant_array() const;
12585
12586 // Return the number of elements.
12587 size_t
12588 element_count() const
12589 { return this->vals_ == NULL ? 0 : this->vals_->size(); }
12590
12591protected:
12592 int
12593 do_traverse(Traverse* traverse);
12594
12595 Type*
12596 do_type()
12597 { return this->type_; }
12598
12599 void
12600 do_determine_type(const Type_context*);
12601
12602 void
12603 do_check_types(Gogo*);
12604
7a938933
ILT
12605 void
12606 do_export(Export*) const;
12607
52556f04
ILT
12608 // The indexes.
12609 const std::vector<unsigned long>*
12610 indexes()
12611 { return this->indexes_; }
12612
7a938933
ILT
12613 // The list of values.
12614 Expression_list*
12615 vals()
12616 { return this->vals_; }
12617
12618 // Get a constructor tree for the array values.
12619 tree
12620 get_constructor_tree(Translate_context* context, tree type_tree);
12621
16c57fe2
RL
12622 void
12623 do_dump_expression(Ast_dump_context*) const;
12624
7a938933
ILT
12625 private:
12626 // The type of the array to construct.
12627 Type* type_;
52556f04
ILT
12628 // The list of indexes into the array, one for each value. This may
12629 // be NULL, in which case the indexes start at zero and increment.
12630 const std::vector<unsigned long>* indexes_;
12631 // The list of values. This may be NULL if there are no values.
7a938933
ILT
12632 Expression_list* vals_;
12633};
12634
12635// Traversal.
12636
12637int
12638Array_construction_expression::do_traverse(Traverse* traverse)
12639{
12640 if (this->vals_ != NULL
12641 && this->vals_->traverse(traverse) == TRAVERSE_EXIT)
12642 return TRAVERSE_EXIT;
12643 if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
12644 return TRAVERSE_EXIT;
12645 return TRAVERSE_CONTINUE;
12646}
12647
12648// Return whether this is a constant initializer.
12649
12650bool
12651Array_construction_expression::is_constant_array() const
12652{
12653 if (this->vals_ == NULL)
12654 return true;
12655
12656 // There are no constant constructors for interfaces.
12657 if (this->type_->array_type()->element_type()->interface_type() != NULL)
12658 return false;
12659
12660 for (Expression_list::const_iterator pv = this->vals_->begin();
12661 pv != this->vals_->end();
12662 ++pv)
12663 {
12664 if (*pv != NULL
12665 && !(*pv)->is_constant()
12666 && (!(*pv)->is_composite_literal()
12667 || (*pv)->is_nonconstant_composite_literal()))
12668 return false;
12669 }
12670 return true;
12671}
12672
12673// Final type determination.
12674
12675void
12676Array_construction_expression::do_determine_type(const Type_context*)
12677{
12678 if (this->vals_ == NULL)
12679 return;
12680 Type_context subcontext(this->type_->array_type()->element_type(), false);
12681 for (Expression_list::const_iterator pv = this->vals_->begin();
12682 pv != this->vals_->end();
12683 ++pv)
12684 {
12685 if (*pv != NULL)
12686 (*pv)->determine_type(&subcontext);
12687 }
12688}
12689
12690// Check types.
12691
12692void
12693Array_construction_expression::do_check_types(Gogo*)
12694{
12695 if (this->vals_ == NULL)
12696 return;
12697
12698 Array_type* at = this->type_->array_type();
12699 int i = 0;
12700 Type* element_type = at->element_type();
12701 for (Expression_list::const_iterator pv = this->vals_->begin();
12702 pv != this->vals_->end();
12703 ++pv, ++i)
12704 {
12705 if (*pv != NULL
12706 && !Type::are_assignable(element_type, (*pv)->type(), NULL))
12707 {
12708 error_at((*pv)->location(),
12709 "incompatible type for element %d in composite literal",
12710 i + 1);
12711 this->set_is_error();
12712 }
12713 }
7a938933
ILT
12714}
12715
12716// Get a constructor tree for the array values.
12717
12718tree
12719Array_construction_expression::get_constructor_tree(Translate_context* context,
12720 tree type_tree)
12721{
4efdbe5a
ILT
12722 vec<constructor_elt, va_gc> *values;
12723 vec_alloc (values, (this->vals_ == NULL ? 0 : this->vals_->size()));
7a938933
ILT
12724 Type* element_type = this->type_->array_type()->element_type();
12725 bool is_constant = true;
12726 if (this->vals_ != NULL)
12727 {
12728 size_t i = 0;
52556f04
ILT
12729 std::vector<unsigned long>::const_iterator pi;
12730 if (this->indexes_ != NULL)
12731 pi = this->indexes_->begin();
7a938933
ILT
12732 for (Expression_list::const_iterator pv = this->vals_->begin();
12733 pv != this->vals_->end();
12734 ++pv, ++i)
12735 {
52556f04
ILT
12736 if (this->indexes_ != NULL)
12737 go_assert(pi != this->indexes_->end());
f32682ca 12738 constructor_elt empty = {NULL, NULL};
4efdbe5a 12739 constructor_elt* elt = values->quick_push(empty);
52556f04
ILT
12740
12741 if (this->indexes_ == NULL)
12742 elt->index = size_int(i);
12743 else
12744 elt->index = size_int(*pi);
12745
7a938933 12746 if (*pv == NULL)
54466dde
ILT
12747 {
12748 Gogo* gogo = context->gogo();
12749 Btype* ebtype = element_type->get_backend(gogo);
12750 Bexpression *zv = gogo->backend()->zero_expression(ebtype);
12751 elt->value = expr_to_tree(zv);
12752 }
7a938933
ILT
12753 else
12754 {
12755 tree value_tree = (*pv)->get_tree(context);
12756 elt->value = Expression::convert_for_assignment(context,
12757 element_type,
12758 (*pv)->type(),
12759 value_tree,
12760 this->location());
12761 }
12762 if (elt->value == error_mark_node)
12763 return error_mark_node;
12764 if (!TREE_CONSTANT(elt->value))
12765 is_constant = false;
52556f04
ILT
12766 if (this->indexes_ != NULL)
12767 ++pi;
7a938933 12768 }
52556f04
ILT
12769 if (this->indexes_ != NULL)
12770 go_assert(pi == this->indexes_->end());
7a938933
ILT
12771 }
12772
12773 tree ret = build_constructor(type_tree, values);
12774 if (is_constant)
12775 TREE_CONSTANT(ret) = 1;
12776 return ret;
12777}
12778
12779// Export an array construction.
12780
12781void
12782Array_construction_expression::do_export(Export* exp) const
12783{
12784 exp->write_c_string("convert(");
12785 exp->write_type(this->type_);
12786 if (this->vals_ != NULL)
12787 {
52556f04
ILT
12788 std::vector<unsigned long>::const_iterator pi;
12789 if (this->indexes_ != NULL)
12790 pi = this->indexes_->begin();
7a938933
ILT
12791 for (Expression_list::const_iterator pv = this->vals_->begin();
12792 pv != this->vals_->end();
12793 ++pv)
12794 {
12795 exp->write_c_string(", ");
52556f04
ILT
12796
12797 if (this->indexes_ != NULL)
12798 {
12799 char buf[100];
12800 snprintf(buf, sizeof buf, "%lu", *pi);
12801 exp->write_c_string(buf);
12802 exp->write_c_string(":");
12803 }
12804
7a938933
ILT
12805 if (*pv != NULL)
12806 (*pv)->export_expression(exp);
52556f04
ILT
12807
12808 if (this->indexes_ != NULL)
12809 ++pi;
7a938933
ILT
12810 }
12811 }
12812 exp->write_c_string(")");
12813}
12814
16c57fe2
RL
12815// Dump ast representation of an array construction expressin.
12816
12817void
12818Array_construction_expression::do_dump_expression(
12819 Ast_dump_context* ast_dump_context) const
12820{
52556f04 12821 Expression* length = this->type_->array_type()->length();
706cd57f
RL
12822
12823 ast_dump_context->ostream() << "[" ;
12824 if (length != NULL)
12825 {
12826 ast_dump_context->dump_expression(length);
12827 }
12828 ast_dump_context->ostream() << "]" ;
16c57fe2
RL
12829 ast_dump_context->dump_type(this->type_);
12830 ast_dump_context->ostream() << "{" ;
52556f04
ILT
12831 if (this->indexes_ == NULL)
12832 ast_dump_context->dump_expression_list(this->vals_);
12833 else
12834 {
12835 Expression_list::const_iterator pv = this->vals_->begin();
12836 for (std::vector<unsigned long>::const_iterator pi =
12837 this->indexes_->begin();
12838 pi != this->indexes_->end();
12839 ++pi, ++pv)
12840 {
12841 if (pi != this->indexes_->begin())
12842 ast_dump_context->ostream() << ", ";
12843 ast_dump_context->ostream() << *pi << ':';
12844 ast_dump_context->dump_expression(*pv);
12845 }
12846 }
16c57fe2
RL
12847 ast_dump_context->ostream() << "}" ;
12848
12849}
12850
7a938933
ILT
12851// Construct a fixed array.
12852
12853class Fixed_array_construction_expression :
12854 public Array_construction_expression
12855{
12856 public:
52556f04
ILT
12857 Fixed_array_construction_expression(Type* type,
12858 const std::vector<unsigned long>* indexes,
12859 Expression_list* vals, Location location)
7a938933 12860 : Array_construction_expression(EXPRESSION_FIXED_ARRAY_CONSTRUCTION,
52556f04
ILT
12861 type, indexes, vals, location)
12862 { go_assert(type->array_type() != NULL && !type->is_slice_type()); }
7a938933
ILT
12863
12864 protected:
12865 Expression*
12866 do_copy()
12867 {
12868 return new Fixed_array_construction_expression(this->type(),
52556f04 12869 this->indexes(),
7a938933
ILT
12870 (this->vals() == NULL
12871 ? NULL
12872 : this->vals()->copy()),
12873 this->location());
12874 }
12875
12876 tree
12877 do_get_tree(Translate_context*);
12878};
12879
12880// Return a tree for constructing a fixed array.
12881
12882tree
12883Fixed_array_construction_expression::do_get_tree(Translate_context* context)
12884{
5b735706
ILT
12885 Type* type = this->type();
12886 Btype* btype = type->get_backend(context->gogo());
12887 return this->get_constructor_tree(context, type_to_tree(btype));
7a938933
ILT
12888}
12889
12890// Construct an open array.
12891
12892class Open_array_construction_expression : public Array_construction_expression
12893{
12894 public:
52556f04
ILT
12895 Open_array_construction_expression(Type* type,
12896 const std::vector<unsigned long>* indexes,
12897 Expression_list* vals, Location location)
7a938933 12898 : Array_construction_expression(EXPRESSION_OPEN_ARRAY_CONSTRUCTION,
52556f04
ILT
12899 type, indexes, vals, location)
12900 { go_assert(type->is_slice_type()); }
7a938933
ILT
12901
12902 protected:
12903 // Note that taking the address of an open array literal is invalid.
12904
12905 Expression*
12906 do_copy()
12907 {
12908 return new Open_array_construction_expression(this->type(),
52556f04 12909 this->indexes(),
7a938933
ILT
12910 (this->vals() == NULL
12911 ? NULL
12912 : this->vals()->copy()),
12913 this->location());
12914 }
12915
12916 tree
12917 do_get_tree(Translate_context*);
12918};
12919
12920// Return a tree for constructing an open array.
12921
12922tree
12923Open_array_construction_expression::do_get_tree(Translate_context* context)
12924{
f39c772f
ILT
12925 Array_type* array_type = this->type()->array_type();
12926 if (array_type == NULL)
12927 {
26409c52 12928 go_assert(this->type()->is_error());
f39c772f
ILT
12929 return error_mark_node;
12930 }
12931
12932 Type* element_type = array_type->element_type();
5b735706
ILT
12933 Btype* belement_type = element_type->get_backend(context->gogo());
12934 tree element_type_tree = type_to_tree(belement_type);
6930f562
ILT
12935 if (element_type_tree == error_mark_node)
12936 return error_mark_node;
12937
7a938933
ILT
12938 tree values;
12939 tree length_tree;
12940 if (this->vals() == NULL || this->vals()->empty())
12941 {
12942 // We need to create a unique value.
12943 tree max = size_int(0);
12944 tree constructor_type = build_array_type(element_type_tree,
12945 build_index_type(max));
12946 if (constructor_type == error_mark_node)
12947 return error_mark_node;
4efdbe5a
ILT
12948 vec<constructor_elt, va_gc> *vec;
12949 vec_alloc(vec, 1);
f32682ca 12950 constructor_elt empty = {NULL, NULL};
4efdbe5a 12951 constructor_elt* elt = vec->quick_push(empty);
7a938933 12952 elt->index = size_int(0);
54466dde
ILT
12953 Gogo* gogo = context->gogo();
12954 Btype* btype = element_type->get_backend(gogo);
12955 elt->value = expr_to_tree(gogo->backend()->zero_expression(btype));
7a938933
ILT
12956 values = build_constructor(constructor_type, vec);
12957 if (TREE_CONSTANT(elt->value))
12958 TREE_CONSTANT(values) = 1;
12959 length_tree = size_int(0);
12960 }
12961 else
12962 {
52556f04
ILT
12963 unsigned long max_index;
12964 if (this->indexes() == NULL)
12965 max_index = this->vals()->size() - 1;
12966 else
87fd4bbf 12967 max_index = this->indexes()->back();
52556f04 12968 tree max_tree = size_int(max_index);
7a938933 12969 tree constructor_type = build_array_type(element_type_tree,
52556f04 12970 build_index_type(max_tree));
7a938933
ILT
12971 if (constructor_type == error_mark_node)
12972 return error_mark_node;
12973 values = this->get_constructor_tree(context, constructor_type);
52556f04 12974 length_tree = size_int(max_index + 1);
7a938933
ILT
12975 }
12976
12977 if (values == error_mark_node)
12978 return error_mark_node;
12979
12980 bool is_constant_initializer = TREE_CONSTANT(values);
01c59996
ILT
12981
12982 // We have to copy the initial values into heap memory if we are in
12983 // a function or if the values are not constants. We also have to
12984 // copy them if they may contain pointers in a non-constant context,
12985 // as otherwise the garbage collector won't see them.
12986 bool copy_to_heap = (context->function() != NULL
12987 || !is_constant_initializer
12988 || (element_type->has_pointer()
12989 && !context->is_const()));
7a938933
ILT
12990
12991 if (is_constant_initializer)
12992 {
8afa2bfb 12993 tree tmp = build_decl(this->location().gcc_location(), VAR_DECL,
7a938933
ILT
12994 create_tmp_var_name("C"), TREE_TYPE(values));
12995 DECL_EXTERNAL(tmp) = 0;
12996 TREE_PUBLIC(tmp) = 0;
12997 TREE_STATIC(tmp) = 1;
12998 DECL_ARTIFICIAL(tmp) = 1;
01c59996 12999 if (copy_to_heap)
7a938933 13000 {
01c59996
ILT
13001 // If we are not copying the value to the heap, we will only
13002 // initialize the value once, so we can use this directly
13003 // rather than copying it. In that case we can't make it
13004 // read-only, because the program is permitted to change it.
7a938933
ILT
13005 TREE_READONLY(tmp) = 1;
13006 TREE_CONSTANT(tmp) = 1;
13007 }
13008 DECL_INITIAL(tmp) = values;
13009 rest_of_decl_compilation(tmp, 1, 0);
13010 values = tmp;
13011 }
13012
13013 tree space;
13014 tree set;
01c59996 13015 if (!copy_to_heap)
7a938933 13016 {
01c59996 13017 // the initializer will only run once.
7a938933
ILT
13018 space = build_fold_addr_expr(values);
13019 set = NULL_TREE;
13020 }
13021 else
13022 {
13023 tree memsize = TYPE_SIZE_UNIT(TREE_TYPE(values));
13024 space = context->gogo()->allocate_memory(element_type, memsize,
13025 this->location());
13026 space = save_expr(space);
13027
13028 tree s = fold_convert(build_pointer_type(TREE_TYPE(values)), space);
8afa2bfb
SD
13029 tree ref = build_fold_indirect_ref_loc(this->location().gcc_location(),
13030 s);
7a938933
ILT
13031 TREE_THIS_NOTRAP(ref) = 1;
13032 set = build2(MODIFY_EXPR, void_type_node, ref, values);
13033 }
13034
13035 // Build a constructor for the open array.
13036
5b735706 13037 tree type_tree = type_to_tree(this->type()->get_backend(context->gogo()));
6930f562
ILT
13038 if (type_tree == error_mark_node)
13039 return error_mark_node;
26409c52 13040 go_assert(TREE_CODE(type_tree) == RECORD_TYPE);
7a938933 13041
4efdbe5a
ILT
13042 vec<constructor_elt, va_gc> *init;
13043 vec_alloc(init, 3);
7a938933 13044
f32682ca 13045 constructor_elt empty = {NULL, NULL};
4efdbe5a 13046 constructor_elt* elt = init->quick_push(empty);
7a938933 13047 tree field = TYPE_FIELDS(type_tree);
26409c52 13048 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__values") == 0);
7a938933
ILT
13049 elt->index = field;
13050 elt->value = fold_convert(TREE_TYPE(field), space);
13051
4efdbe5a 13052 elt = init->quick_push(empty);
7a938933 13053 field = DECL_CHAIN(field);
26409c52 13054 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "__count") == 0);
7a938933
ILT
13055 elt->index = field;
13056 elt->value = fold_convert(TREE_TYPE(field), length_tree);
13057
4efdbe5a 13058 elt = init->quick_push(empty);
7a938933 13059 field = DECL_CHAIN(field);
26409c52 13060 go_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)),"__capacity") == 0);
7a938933
ILT
13061 elt->index = field;
13062 elt->value = fold_convert(TREE_TYPE(field), length_tree);
13063
13064 tree constructor = build_constructor(type_tree, init);
6930f562
ILT
13065 if (constructor == error_mark_node)
13066 return error_mark_node;
01c59996 13067 if (!copy_to_heap)
7a938933
ILT
13068 TREE_CONSTANT(constructor) = 1;
13069
13070 if (set == NULL_TREE)
13071 return constructor;
13072 else
13073 return build2(COMPOUND_EXPR, type_tree, set, constructor);
13074}
13075
13076// Make a slice composite literal. This is used by the type
13077// descriptor code.
13078
13079Expression*
13080Expression::make_slice_composite_literal(Type* type, Expression_list* vals,
8afa2bfb 13081 Location location)
7a938933 13082{
b7190f2f 13083 go_assert(type->is_slice_type());
52556f04 13084 return new Open_array_construction_expression(type, NULL, vals, location);
7a938933
ILT
13085}
13086
13087// Construct a map.
13088
13089class Map_construction_expression : public Expression
13090{
13091 public:
13092 Map_construction_expression(Type* type, Expression_list* vals,
8afa2bfb 13093 Location location)
7a938933
ILT
13094 : Expression(EXPRESSION_MAP_CONSTRUCTION, location),
13095 type_(type), vals_(vals)
26409c52 13096 { go_assert(vals == NULL || vals->size() % 2 == 0); }
7a938933
ILT
13097
13098 protected:
13099 int
13100 do_traverse(Traverse* traverse);
13101
13102 Type*
13103 do_type()
13104 { return this->type_; }
13105
13106 void
13107 do_determine_type(const Type_context*);
13108
13109 void
13110 do_check_types(Gogo*);
13111
13112 Expression*
13113 do_copy()
13114 {
13115 return new Map_construction_expression(this->type_, this->vals_->copy(),
13116 this->location());
13117 }
13118
13119 tree
13120 do_get_tree(Translate_context*);
13121
13122 void
13123 do_export(Export*) const;
13124
16c57fe2
RL
13125 void
13126 do_dump_expression(Ast_dump_context*) const;
13127
7a938933
ILT
13128 private:
13129 // The type of the map to construct.
13130 Type* type_;
13131 // The list of values.
13132 Expression_list* vals_;
13133};
13134
13135// Traversal.
13136
13137int
13138Map_construction_expression::do_traverse(Traverse* traverse)
13139{
13140 if (this->vals_ != NULL
13141 && this->vals_->traverse(traverse) == TRAVERSE_EXIT)
13142 return TRAVERSE_EXIT;
13143 if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
13144 return TRAVERSE_EXIT;
13145 return TRAVERSE_CONTINUE;
13146}
13147
13148// Final type determination.
13149
13150void
13151Map_construction_expression::do_determine_type(const Type_context*)
13152{
13153 if (this->vals_ == NULL)
13154 return;
13155
13156 Map_type* mt = this->type_->map_type();
13157 Type_context key_context(mt->key_type(), false);
13158 Type_context val_context(mt->val_type(), false);
13159 for (Expression_list::const_iterator pv = this->vals_->begin();
13160 pv != this->vals_->end();
13161 ++pv)
13162 {
13163 (*pv)->determine_type(&key_context);
13164 ++pv;
13165 (*pv)->determine_type(&val_context);
13166 }
13167}
13168
13169// Check types.
13170
13171void
13172Map_construction_expression::do_check_types(Gogo*)
13173{
13174 if (this->vals_ == NULL)
13175 return;
13176
13177 Map_type* mt = this->type_->map_type();
13178 int i = 0;
13179 Type* key_type = mt->key_type();
13180 Type* val_type = mt->val_type();
13181 for (Expression_list::const_iterator pv = this->vals_->begin();
13182 pv != this->vals_->end();
13183 ++pv, ++i)
13184 {
13185 if (!Type::are_assignable(key_type, (*pv)->type(), NULL))
13186 {
13187 error_at((*pv)->location(),
13188 "incompatible type for element %d key in map construction",
13189 i + 1);
13190 this->set_is_error();
13191 }
13192 ++pv;
13193 if (!Type::are_assignable(val_type, (*pv)->type(), NULL))
13194 {
13195 error_at((*pv)->location(),
13196 ("incompatible type for element %d value "
13197 "in map construction"),
13198 i + 1);
13199 this->set_is_error();
13200 }
13201 }
13202}
13203
13204// Return a tree for constructing a map.
13205
13206tree
13207Map_construction_expression::do_get_tree(Translate_context* context)
13208{
13209 Gogo* gogo = context->gogo();
8afa2bfb 13210 Location loc = this->location();
7a938933
ILT
13211
13212 Map_type* mt = this->type_->map_type();
13213
13214 // Build a struct to hold the key and value.
13215 tree struct_type = make_node(RECORD_TYPE);
13216
13217 Type* key_type = mt->key_type();
13218 tree id = get_identifier("__key");
5b735706 13219 tree key_type_tree = type_to_tree(key_type->get_backend(gogo));
0cc3d14e
ILT
13220 if (key_type_tree == error_mark_node)
13221 return error_mark_node;
8afa2bfb
SD
13222 tree key_field = build_decl(loc.gcc_location(), FIELD_DECL, id,
13223 key_type_tree);
7a938933
ILT
13224 DECL_CONTEXT(key_field) = struct_type;
13225 TYPE_FIELDS(struct_type) = key_field;
13226
13227 Type* val_type = mt->val_type();
13228 id = get_identifier("__val");
5b735706 13229 tree val_type_tree = type_to_tree(val_type->get_backend(gogo));
0cc3d14e
ILT
13230 if (val_type_tree == error_mark_node)
13231 return error_mark_node;
8afa2bfb
SD
13232 tree val_field = build_decl(loc.gcc_location(), FIELD_DECL, id,
13233 val_type_tree);
7a938933
ILT
13234 DECL_CONTEXT(val_field) = struct_type;
13235 DECL_CHAIN(key_field) = val_field;
13236
13237 layout_type(struct_type);
13238
13239 bool is_constant = true;
13240 size_t i = 0;
13241 tree valaddr;
13242 tree make_tmp;
13243
13244 if (this->vals_ == NULL || this->vals_->empty())
13245 {
13246 valaddr = null_pointer_node;
13247 make_tmp = NULL_TREE;
13248 }
13249 else
13250 {
4efdbe5a
ILT
13251 vec<constructor_elt, va_gc> *values;
13252 vec_alloc(values, this->vals_->size() / 2);
7a938933
ILT
13253
13254 for (Expression_list::const_iterator pv = this->vals_->begin();
13255 pv != this->vals_->end();
13256 ++pv, ++i)
13257 {
13258 bool one_is_constant = true;
13259
4efdbe5a
ILT
13260 vec<constructor_elt, va_gc> *one;
13261 vec_alloc(one, 2);
7a938933 13262
f32682ca 13263 constructor_elt empty = {NULL, NULL};
4efdbe5a 13264 constructor_elt* elt = one->quick_push(empty);
7a938933
ILT
13265 elt->index = key_field;
13266 tree val_tree = (*pv)->get_tree(context);
13267 elt->value = Expression::convert_for_assignment(context, key_type,
13268 (*pv)->type(),
13269 val_tree, loc);
13270 if (elt->value == error_mark_node)
13271 return error_mark_node;
13272 if (!TREE_CONSTANT(elt->value))
13273 one_is_constant = false;
13274
13275 ++pv;
13276
4efdbe5a 13277 elt = one->quick_push(empty);
7a938933
ILT
13278 elt->index = val_field;
13279 val_tree = (*pv)->get_tree(context);
13280 elt->value = Expression::convert_for_assignment(context, val_type,
13281 (*pv)->type(),
13282 val_tree, loc);
13283 if (elt->value == error_mark_node)
13284 return error_mark_node;
13285 if (!TREE_CONSTANT(elt->value))
13286 one_is_constant = false;
13287
4efdbe5a 13288 elt = values->quick_push(empty);
7a938933
ILT
13289 elt->index = size_int(i);
13290 elt->value = build_constructor(struct_type, one);
13291 if (one_is_constant)
13292 TREE_CONSTANT(elt->value) = 1;
13293 else
13294 is_constant = false;
13295 }
13296
13297 tree index_type = build_index_type(size_int(i - 1));
13298 tree array_type = build_array_type(struct_type, index_type);
13299 tree init = build_constructor(array_type, values);
13300 if (is_constant)
13301 TREE_CONSTANT(init) = 1;
13302 tree tmp;
13303 if (current_function_decl != NULL)
13304 {
13305 tmp = create_tmp_var(array_type, get_name(array_type));
13306 DECL_INITIAL(tmp) = init;
8afa2bfb
SD
13307 make_tmp = fold_build1_loc(loc.gcc_location(), DECL_EXPR,
13308 void_type_node, tmp);
7a938933
ILT
13309 TREE_ADDRESSABLE(tmp) = 1;
13310 }
13311 else
13312 {
8afa2bfb
SD
13313 tmp = build_decl(loc.gcc_location(), VAR_DECL,
13314 create_tmp_var_name("M"), array_type);
7a938933
ILT
13315 DECL_EXTERNAL(tmp) = 0;
13316 TREE_PUBLIC(tmp) = 0;
13317 TREE_STATIC(tmp) = 1;
13318 DECL_ARTIFICIAL(tmp) = 1;
13319 if (!TREE_CONSTANT(init))
8afa2bfb
SD
13320 make_tmp = fold_build2_loc(loc.gcc_location(), INIT_EXPR,
13321 void_type_node, tmp, init);
7a938933
ILT
13322 else
13323 {
13324 TREE_READONLY(tmp) = 1;
13325 TREE_CONSTANT(tmp) = 1;
13326 DECL_INITIAL(tmp) = init;
13327 make_tmp = NULL_TREE;
13328 }
13329 rest_of_decl_compilation(tmp, 1, 0);
13330 }
13331
13332 valaddr = build_fold_addr_expr(tmp);
13333 }
13334
b93e0cfd
CM
13335 Bexpression* bdescriptor = mt->map_descriptor_pointer(gogo, loc);
13336 tree descriptor = expr_to_tree(bdescriptor);
7a938933 13337
5b735706 13338 tree type_tree = type_to_tree(this->type_->get_backend(gogo));
0cc3d14e
ILT
13339 if (type_tree == error_mark_node)
13340 return error_mark_node;
7a938933
ILT
13341
13342 static tree construct_map_fndecl;
13343 tree call = Gogo::call_builtin(&construct_map_fndecl,
13344 loc,
13345 "__go_construct_map",
13346 6,
13347 type_tree,
13348 TREE_TYPE(descriptor),
13349 descriptor,
13350 sizetype,
13351 size_int(i),
13352 sizetype,
13353 TYPE_SIZE_UNIT(struct_type),
13354 sizetype,
13355 byte_position(val_field),
13356 sizetype,
13357 TYPE_SIZE_UNIT(TREE_TYPE(val_field)),
13358 const_ptr_type_node,
13359 fold_convert(const_ptr_type_node, valaddr));
faff9b04
ILT
13360 if (call == error_mark_node)
13361 return error_mark_node;
7a938933
ILT
13362
13363 tree ret;
13364 if (make_tmp == NULL)
13365 ret = call;
13366 else
8afa2bfb
SD
13367 ret = fold_build2_loc(loc.gcc_location(), COMPOUND_EXPR, type_tree,
13368 make_tmp, call);
7a938933
ILT
13369 return ret;
13370}
13371
13372// Export an array construction.
13373
13374void
13375Map_construction_expression::do_export(Export* exp) const
13376{
13377 exp->write_c_string("convert(");
13378 exp->write_type(this->type_);
13379 for (Expression_list::const_iterator pv = this->vals_->begin();
13380 pv != this->vals_->end();
13381 ++pv)
13382 {
13383 exp->write_c_string(", ");
13384 (*pv)->export_expression(exp);
13385 }
13386 exp->write_c_string(")");
13387}
13388
16c57fe2
RL
13389// Dump ast representation for a map construction expression.
13390
13391void
13392Map_construction_expression::do_dump_expression(
13393 Ast_dump_context* ast_dump_context) const
13394{
16c57fe2 13395 ast_dump_context->ostream() << "{" ;
706cd57f 13396 ast_dump_context->dump_expression_list(this->vals_, true);
16c57fe2
RL
13397 ast_dump_context->ostream() << "}";
13398}
13399
7a938933
ILT
13400// A general composite literal. This is lowered to a type specific
13401// version.
13402
13403class Composite_literal_expression : public Parser_expression
13404{
13405 public:
13406 Composite_literal_expression(Type* type, int depth, bool has_keys,
8ae4c35c
ILT
13407 Expression_list* vals, bool all_are_names,
13408 Location location)
7a938933 13409 : Parser_expression(EXPRESSION_COMPOSITE_LITERAL, location),
8ae4c35c
ILT
13410 type_(type), depth_(depth), vals_(vals), has_keys_(has_keys),
13411 all_are_names_(all_are_names)
7a938933
ILT
13412 { }
13413
13414 protected:
13415 int
13416 do_traverse(Traverse* traverse);
13417
13418 Expression*
8586635c 13419 do_lower(Gogo*, Named_object*, Statement_inserter*, int);
7a938933
ILT
13420
13421 Expression*
13422 do_copy()
13423 {
13424 return new Composite_literal_expression(this->type_, this->depth_,
13425 this->has_keys_,
13426 (this->vals_ == NULL
13427 ? NULL
13428 : this->vals_->copy()),
8ae4c35c 13429 this->all_are_names_,
7a938933
ILT
13430 this->location());
13431 }
13432
16c57fe2
RL
13433 void
13434 do_dump_expression(Ast_dump_context*) const;
13435
7a938933
ILT
13436 private:
13437 Expression*
6481a43b 13438 lower_struct(Gogo*, Type*);
7a938933
ILT
13439
13440 Expression*
11d8e1a4 13441 lower_array(Type*);
7a938933
ILT
13442
13443 Expression*
52556f04 13444 make_array(Type*, const std::vector<unsigned long>*, Expression_list*);
7a938933
ILT
13445
13446 Expression*
8586635c 13447 lower_map(Gogo*, Named_object*, Statement_inserter*, Type*);
7a938933
ILT
13448
13449 // The type of the composite literal.
13450 Type* type_;
13451 // The depth within a list of composite literals within a composite
13452 // literal, when the type is omitted.
13453 int depth_;
13454 // The values to put in the composite literal.
13455 Expression_list* vals_;
13456 // If this is true, then VALS_ is a list of pairs: a key and a
13457 // value. In an array initializer, a missing key will be NULL.
13458 bool has_keys_;
8ae4c35c
ILT
13459 // If this is true, then HAS_KEYS_ is true, and every key is a
13460 // simple identifier.
13461 bool all_are_names_;
7a938933
ILT
13462};
13463
13464// Traversal.
13465
13466int
13467Composite_literal_expression::do_traverse(Traverse* traverse)
13468{
68607055 13469 if (Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
7a938933 13470 return TRAVERSE_EXIT;
68607055
ILT
13471
13472 // If this is a struct composite literal with keys, then the keys
13473 // are field names, not expressions. We don't want to traverse them
13474 // in that case. If we do, we can give an erroneous error "variable
13475 // initializer refers to itself." See bug482.go in the testsuite.
13476 if (this->has_keys_ && this->vals_ != NULL)
13477 {
13478 // The type may not be resolvable at this point.
13479 Type* type = this->type_;
13480 while (true)
13481 {
13482 if (type->classification() == Type::TYPE_NAMED)
13483 type = type->named_type()->real_type();
13484 else if (type->classification() == Type::TYPE_FORWARD)
13485 {
13486 Type* t = type->forwarded();
13487 if (t == type)
13488 break;
13489 type = t;
13490 }
13491 else
13492 break;
13493 }
13494
13495 if (type->classification() == Type::TYPE_STRUCT)
13496 {
13497 Expression_list::iterator p = this->vals_->begin();
13498 while (p != this->vals_->end())
13499 {
13500 // Skip key.
13501 ++p;
13502 go_assert(p != this->vals_->end());
13503 if (Expression::traverse(&*p, traverse) == TRAVERSE_EXIT)
13504 return TRAVERSE_EXIT;
13505 ++p;
13506 }
13507 return TRAVERSE_CONTINUE;
13508 }
13509 }
13510
13511 if (this->vals_ != NULL)
13512 return this->vals_->traverse(traverse);
13513
13514 return TRAVERSE_CONTINUE;
7a938933
ILT
13515}
13516
13517// Lower a generic composite literal into a specific version based on
13518// the type.
13519
13520Expression*
8586635c
ILT
13521Composite_literal_expression::do_lower(Gogo* gogo, Named_object* function,
13522 Statement_inserter* inserter, int)
7a938933
ILT
13523{
13524 Type* type = this->type_;
13525
13526 for (int depth = this->depth_; depth > 0; --depth)
13527 {
13528 if (type->array_type() != NULL)
13529 type = type->array_type()->element_type();
13530 else if (type->map_type() != NULL)
13531 type = type->map_type()->val_type();
13532 else
13533 {
02ed921a 13534 if (!type->is_error())
7a938933
ILT
13535 error_at(this->location(),
13536 ("may only omit types within composite literals "
13537 "of slice, array, or map type"));
13538 return Expression::make_error(this->location());
13539 }
13540 }
13541
c623f837
ILT
13542 Type *pt = type->points_to();
13543 bool is_pointer = false;
13544 if (pt != NULL)
13545 {
13546 is_pointer = true;
13547 type = pt;
13548 }
13549
13550 Expression* ret;
02ed921a 13551 if (type->is_error())
7a938933
ILT
13552 return Expression::make_error(this->location());
13553 else if (type->struct_type() != NULL)
c623f837 13554 ret = this->lower_struct(gogo, type);
7a938933 13555 else if (type->array_type() != NULL)
11d8e1a4 13556 ret = this->lower_array(type);
7a938933 13557 else if (type->map_type() != NULL)
c623f837 13558 ret = this->lower_map(gogo, function, inserter, type);
7a938933
ILT
13559 else
13560 {
13561 error_at(this->location(),
13562 ("expected struct, slice, array, or map type "
13563 "for composite literal"));
13564 return Expression::make_error(this->location());
13565 }
c623f837
ILT
13566
13567 if (is_pointer)
13568 ret = Expression::make_heap_composite(ret, this->location());
13569
13570 return ret;
7a938933
ILT
13571}
13572
13573// Lower a struct composite literal.
13574
13575Expression*
6481a43b 13576Composite_literal_expression::lower_struct(Gogo* gogo, Type* type)
7a938933 13577{
8afa2bfb 13578 Location location = this->location();
7a938933
ILT
13579 Struct_type* st = type->struct_type();
13580 if (this->vals_ == NULL || !this->has_keys_)
1fbbc69c 13581 {
c573faf5
ILT
13582 if (this->vals_ != NULL
13583 && !this->vals_->empty()
13584 && type->named_type() != NULL
13585 && type->named_type()->named_object()->package() != NULL)
13586 {
13587 for (Struct_field_list::const_iterator pf = st->fields()->begin();
13588 pf != st->fields()->end();
13589 ++pf)
1fbbc69c 13590 {
c573faf5 13591 if (Gogo::is_hidden_name(pf->field_name()))
1fbbc69c 13592 error_at(this->location(),
c573faf5
ILT
13593 "assignment of unexported field %qs in %qs literal",
13594 Gogo::message_name(pf->field_name()).c_str(),
13595 type->named_type()->message_name().c_str());
1fbbc69c
ILT
13596 }
13597 }
13598
13599 return new Struct_construction_expression(type, this->vals_, location);
13600 }
7a938933
ILT
13601
13602 size_t field_count = st->field_count();
13603 std::vector<Expression*> vals(field_count);
50f671c6 13604 std::vector<int>* traverse_order = new(std::vector<int>);
7a938933 13605 Expression_list::const_iterator p = this->vals_->begin();
8ae4c35c
ILT
13606 Expression* external_expr = NULL;
13607 const Named_object* external_no = NULL;
7a938933
ILT
13608 while (p != this->vals_->end())
13609 {
13610 Expression* name_expr = *p;
13611
13612 ++p;
26409c52 13613 go_assert(p != this->vals_->end());
7a938933
ILT
13614 Expression* val = *p;
13615
13616 ++p;
13617
13618 if (name_expr == NULL)
13619 {
13620 error_at(val->location(), "mixture of field and value initializers");
13621 return Expression::make_error(location);
13622 }
13623
13624 bool bad_key = false;
13625 std::string name;
6481a43b 13626 const Named_object* no = NULL;
7a938933
ILT
13627 switch (name_expr->classification())
13628 {
13629 case EXPRESSION_UNKNOWN_REFERENCE:
13630 name = name_expr->unknown_expression()->name();
13631 break;
13632
13633 case EXPRESSION_CONST_REFERENCE:
6481a43b 13634 no = static_cast<Const_expression*>(name_expr)->named_object();
7a938933
ILT
13635 break;
13636
13637 case EXPRESSION_TYPE:
13638 {
13639 Type* t = name_expr->type();
13640 Named_type* nt = t->named_type();
13641 if (nt == NULL)
13642 bad_key = true;
13643 else
6481a43b 13644 no = nt->named_object();
7a938933
ILT
13645 }
13646 break;
13647
13648 case EXPRESSION_VAR_REFERENCE:
6481a43b 13649 no = name_expr->var_expression()->named_object();
7a938933
ILT
13650 break;
13651
13652 case EXPRESSION_FUNC_REFERENCE:
6481a43b 13653 no = name_expr->func_expression()->named_object();
7a938933
ILT
13654 break;
13655
13656 case EXPRESSION_UNARY:
13657 // If there is a local variable around with the same name as
13658 // the field, and this occurs in the closure, then the
13659 // parser may turn the field reference into an indirection
13660 // through the closure. FIXME: This is a mess.
13661 {
13662 bad_key = true;
13663 Unary_expression* ue = static_cast<Unary_expression*>(name_expr);
13664 if (ue->op() == OPERATOR_MULT)
13665 {
13666 Field_reference_expression* fre =
13667 ue->operand()->field_reference_expression();
13668 if (fre != NULL)
13669 {
13670 Struct_type* st =
13671 fre->expr()->type()->deref()->struct_type();
13672 if (st != NULL)
13673 {
13674 const Struct_field* sf = st->field(fre->field_index());
13675 name = sf->field_name();
ffe08917
ILT
13676
13677 // See below. FIXME.
13678 if (!Gogo::is_hidden_name(name)
13679 && name[0] >= 'a'
13680 && name[0] <= 'z')
13681 {
13682 if (gogo->lookup_global(name.c_str()) != NULL)
13683 name = gogo->pack_hidden_name(name, false);
13684 }
13685
7a938933
ILT
13686 char buf[20];
13687 snprintf(buf, sizeof buf, "%u", fre->field_index());
13688 size_t buflen = strlen(buf);
13689 if (name.compare(name.length() - buflen, buflen, buf)
13690 == 0)
13691 {
13692 name = name.substr(0, name.length() - buflen);
13693 bad_key = false;
13694 }
13695 }
13696 }
13697 }
13698 }
13699 break;
13700
13701 default:
13702 bad_key = true;
13703 break;
13704 }
13705 if (bad_key)
13706 {
13707 error_at(name_expr->location(), "expected struct field name");
13708 return Expression::make_error(location);
13709 }
13710
6481a43b
ILT
13711 if (no != NULL)
13712 {
8ae4c35c
ILT
13713 if (no->package() != NULL && external_expr == NULL)
13714 {
13715 external_expr = name_expr;
13716 external_no = no;
13717 }
13718
6481a43b
ILT
13719 name = no->name();
13720
13721 // A predefined name won't be packed. If it starts with a
13722 // lower case letter we need to check for that case, because
ffe08917 13723 // the field name will be packed. FIXME.
6481a43b
ILT
13724 if (!Gogo::is_hidden_name(name)
13725 && name[0] >= 'a'
13726 && name[0] <= 'z')
13727 {
13728 Named_object* gno = gogo->lookup_global(name.c_str());
13729 if (gno == no)
13730 name = gogo->pack_hidden_name(name, false);
13731 }
13732 }
13733
7a938933
ILT
13734 unsigned int index;
13735 const Struct_field* sf = st->find_local_field(name, &index);
13736 if (sf == NULL)
13737 {
13738 error_at(name_expr->location(), "unknown field %qs in %qs",
13739 Gogo::message_name(name).c_str(),
13740 (type->named_type() != NULL
13741 ? type->named_type()->message_name().c_str()
13742 : "unnamed struct"));
13743 return Expression::make_error(location);
13744 }
13745 if (vals[index] != NULL)
13746 {
13747 error_at(name_expr->location(),
13748 "duplicate value for field %qs in %qs",
13749 Gogo::message_name(name).c_str(),
13750 (type->named_type() != NULL
13751 ? type->named_type()->message_name().c_str()
13752 : "unnamed struct"));
13753 return Expression::make_error(location);
13754 }
13755
1fbbc69c
ILT
13756 if (type->named_type() != NULL
13757 && type->named_type()->named_object()->package() != NULL
13758 && Gogo::is_hidden_name(sf->field_name()))
13759 error_at(name_expr->location(),
13760 "assignment of unexported field %qs in %qs literal",
13761 Gogo::message_name(sf->field_name()).c_str(),
13762 type->named_type()->message_name().c_str());
1fbbc69c 13763
7a938933 13764 vals[index] = val;
50f671c6 13765 traverse_order->push_back(index);
7a938933
ILT
13766 }
13767
8ae4c35c
ILT
13768 if (!this->all_are_names_)
13769 {
13770 // This is a weird case like bug462 in the testsuite.
13771 if (external_expr == NULL)
13772 error_at(this->location(), "unknown field in %qs literal",
13773 (type->named_type() != NULL
13774 ? type->named_type()->message_name().c_str()
13775 : "unnamed struct"));
13776 else
13777 error_at(external_expr->location(), "unknown field %qs in %qs",
13778 external_no->message_name().c_str(),
13779 (type->named_type() != NULL
13780 ? type->named_type()->message_name().c_str()
13781 : "unnamed struct"));
13782 return Expression::make_error(location);
13783 }
13784
7a938933
ILT
13785 Expression_list* list = new Expression_list;
13786 list->reserve(field_count);
13787 for (size_t i = 0; i < field_count; ++i)
13788 list->push_back(vals[i]);
13789
50f671c6
ILT
13790 Struct_construction_expression* ret =
13791 new Struct_construction_expression(type, list, location);
13792 ret->set_traverse_order(traverse_order);
13793 return ret;
7a938933
ILT
13794}
13795
87fd4bbf
ILT
13796// Used to sort an index/value array.
13797
13798class Index_value_compare
13799{
13800 public:
13801 bool
13802 operator()(const std::pair<unsigned long, Expression*>& a,
13803 const std::pair<unsigned long, Expression*>& b)
13804 { return a.first < b.first; }
13805};
13806
7a938933
ILT
13807// Lower an array composite literal.
13808
13809Expression*
11d8e1a4 13810Composite_literal_expression::lower_array(Type* type)
7a938933 13811{
8afa2bfb 13812 Location location = this->location();
7a938933 13813 if (this->vals_ == NULL || !this->has_keys_)
52556f04 13814 return this->make_array(type, NULL, this->vals_);
7a938933 13815
52556f04
ILT
13816 std::vector<unsigned long>* indexes = new std::vector<unsigned long>;
13817 indexes->reserve(this->vals_->size());
87fd4bbf 13818 bool indexes_out_of_order = false;
52556f04
ILT
13819 Expression_list* vals = new Expression_list();
13820 vals->reserve(this->vals_->size());
7a938933
ILT
13821 unsigned long index = 0;
13822 Expression_list::const_iterator p = this->vals_->begin();
13823 while (p != this->vals_->end())
13824 {
13825 Expression* index_expr = *p;
13826
13827 ++p;
26409c52 13828 go_assert(p != this->vals_->end());
7a938933
ILT
13829 Expression* val = *p;
13830
13831 ++p;
13832
52556f04
ILT
13833 if (index_expr == NULL)
13834 {
13835 if (!indexes->empty())
13836 indexes->push_back(index);
13837 }
13838 else
7a938933 13839 {
52556f04
ILT
13840 if (indexes->empty() && !vals->empty())
13841 {
13842 for (size_t i = 0; i < vals->size(); ++i)
13843 indexes->push_back(i);
13844 }
13845
5caf63ca
ILT
13846 Numeric_constant nc;
13847 if (!index_expr->numeric_constant_value(&nc))
7a938933 13848 {
7a938933
ILT
13849 error_at(index_expr->location(),
13850 "index expression is not integer constant");
13851 return Expression::make_error(location);
13852 }
e2e280a3 13853
5caf63ca 13854 switch (nc.to_unsigned_long(&index))
7a938933 13855 {
5caf63ca
ILT
13856 case Numeric_constant::NC_UL_VALID:
13857 break;
13858 case Numeric_constant::NC_UL_NOTINT:
13859 error_at(index_expr->location(),
13860 "index expression is not integer constant");
13861 return Expression::make_error(location);
13862 case Numeric_constant::NC_UL_NEGATIVE:
7a938933
ILT
13863 error_at(index_expr->location(), "index expression is negative");
13864 return Expression::make_error(location);
5caf63ca 13865 case Numeric_constant::NC_UL_BIG:
7a938933
ILT
13866 error_at(index_expr->location(), "index value overflow");
13867 return Expression::make_error(location);
5caf63ca
ILT
13868 default:
13869 go_unreachable();
7a938933 13870 }
e2e280a3
ILT
13871
13872 Named_type* ntype = Type::lookup_integer_type("int");
13873 Integer_type* inttype = ntype->integer_type();
5caf63ca
ILT
13874 if (sizeof(index) <= static_cast<size_t>(inttype->bits() * 8)
13875 && index >> (inttype->bits() - 1) != 0)
e2e280a3 13876 {
e2e280a3
ILT
13877 error_at(index_expr->location(), "index value overflow");
13878 return Expression::make_error(location);
13879 }
13880
52556f04
ILT
13881 if (std::find(indexes->begin(), indexes->end(), index)
13882 != indexes->end())
7a938933 13883 {
52556f04 13884 error_at(index_expr->location(), "duplicate value for index %lu",
7a938933
ILT
13885 index);
13886 return Expression::make_error(location);
13887 }
52556f04 13888
87fd4bbf
ILT
13889 if (!indexes->empty() && index < indexes->back())
13890 indexes_out_of_order = true;
13891
52556f04 13892 indexes->push_back(index);
7a938933
ILT
13893 }
13894
52556f04
ILT
13895 vals->push_back(val);
13896
7a938933
ILT
13897 ++index;
13898 }
13899
52556f04
ILT
13900 if (indexes->empty())
13901 {
13902 delete indexes;
13903 indexes = NULL;
13904 }
7a938933 13905
87fd4bbf
ILT
13906 if (indexes_out_of_order)
13907 {
13908 typedef std::vector<std::pair<unsigned long, Expression*> > V;
13909
13910 V v;
13911 v.reserve(indexes->size());
13912 std::vector<unsigned long>::const_iterator pi = indexes->begin();
13913 for (Expression_list::const_iterator pe = vals->begin();
13914 pe != vals->end();
13915 ++pe, ++pi)
13916 v.push_back(std::make_pair(*pi, *pe));
13917
13918 std::sort(v.begin(), v.end(), Index_value_compare());
13919
13920 delete indexes;
13921 delete vals;
13922 indexes = new std::vector<unsigned long>();
13923 indexes->reserve(v.size());
13924 vals = new Expression_list();
13925 vals->reserve(v.size());
13926
13927 for (V::const_iterator p = v.begin(); p != v.end(); ++p)
13928 {
13929 indexes->push_back(p->first);
13930 vals->push_back(p->second);
13931 }
13932 }
13933
52556f04 13934 return this->make_array(type, indexes, vals);
7a938933
ILT
13935}
13936
13937// Actually build the array composite literal. This handles
13938// [...]{...}.
13939
13940Expression*
52556f04
ILT
13941Composite_literal_expression::make_array(
13942 Type* type,
13943 const std::vector<unsigned long>* indexes,
13944 Expression_list* vals)
7a938933 13945{
8afa2bfb 13946 Location location = this->location();
7a938933 13947 Array_type* at = type->array_type();
52556f04 13948
7a938933
ILT
13949 if (at->length() != NULL && at->length()->is_nil_expression())
13950 {
52556f04
ILT
13951 size_t size;
13952 if (vals == NULL)
13953 size = 0;
87fd4bbf
ILT
13954 else if (indexes != NULL)
13955 size = indexes->back() + 1;
13956 else
52556f04
ILT
13957 {
13958 size = vals->size();
13959 Integer_type* it = Type::lookup_integer_type("int")->integer_type();
13960 if (sizeof(size) <= static_cast<size_t>(it->bits() * 8)
13961 && size >> (it->bits() - 1) != 0)
13962 {
13963 error_at(location, "too many elements in composite literal");
13964 return Expression::make_error(location);
13965 }
13966 }
52556f04 13967
7a938933
ILT
13968 mpz_t vlen;
13969 mpz_init_set_ui(vlen, size);
13970 Expression* elen = Expression::make_integer(&vlen, NULL, location);
13971 mpz_clear(vlen);
13972 at = Type::make_array_type(at->element_type(), elen);
13973 type = at;
13974 }
52556f04
ILT
13975 else if (at->length() != NULL
13976 && !at->length()->is_error_expression()
13977 && this->vals_ != NULL)
13978 {
13979 Numeric_constant nc;
13980 unsigned long val;
13981 if (at->length()->numeric_constant_value(&nc)
13982 && nc.to_unsigned_long(&val) == Numeric_constant::NC_UL_VALID)
13983 {
13984 if (indexes == NULL)
13985 {
13986 if (this->vals_->size() > val)
13987 {
13988 error_at(location, "too many elements in composite literal");
13989 return Expression::make_error(location);
13990 }
13991 }
13992 else
13993 {
87fd4bbf 13994 unsigned long max = indexes->back();
52556f04
ILT
13995 if (max >= val)
13996 {
13997 error_at(location,
13998 ("some element keys in composite literal "
13999 "are out of range"));
14000 return Expression::make_error(location);
14001 }
14002 }
14003 }
14004 }
14005
7a938933 14006 if (at->length() != NULL)
52556f04
ILT
14007 return new Fixed_array_construction_expression(type, indexes, vals,
14008 location);
7a938933 14009 else
52556f04
ILT
14010 return new Open_array_construction_expression(type, indexes, vals,
14011 location);
7a938933
ILT
14012}
14013
14014// Lower a map composite literal.
14015
14016Expression*
cd96b4e2 14017Composite_literal_expression::lower_map(Gogo* gogo, Named_object* function,
8586635c 14018 Statement_inserter* inserter,
cd96b4e2 14019 Type* type)
7a938933 14020{
8afa2bfb 14021 Location location = this->location();
7a938933
ILT
14022 if (this->vals_ != NULL)
14023 {
14024 if (!this->has_keys_)
14025 {
14026 error_at(location, "map composite literal must have keys");
14027 return Expression::make_error(location);
14028 }
14029
cd96b4e2 14030 for (Expression_list::iterator p = this->vals_->begin();
7a938933
ILT
14031 p != this->vals_->end();
14032 p += 2)
14033 {
14034 if (*p == NULL)
14035 {
14036 ++p;
14037 error_at((*p)->location(),
14038 "map composite literal must have keys for every value");
14039 return Expression::make_error(location);
14040 }
cd96b4e2
ILT
14041 // Make sure we have lowered the key; it may not have been
14042 // lowered in order to handle keys for struct composite
14043 // literals. Lower it now to get the right error message.
14044 if ((*p)->unknown_expression() != NULL)
14045 {
14046 (*p)->unknown_expression()->clear_is_composite_literal_key();
8586635c 14047 gogo->lower_expression(function, inserter, &*p);
26409c52 14048 go_assert((*p)->is_error_expression());
cd96b4e2
ILT
14049 return Expression::make_error(location);
14050 }
7a938933
ILT
14051 }
14052 }
14053
14054 return new Map_construction_expression(type, this->vals_, location);
14055}
14056
16c57fe2
RL
14057// Dump ast representation for a composite literal expression.
14058
14059void
14060Composite_literal_expression::do_dump_expression(
14061 Ast_dump_context* ast_dump_context) const
14062{
706cd57f 14063 ast_dump_context->ostream() << "composite(";
16c57fe2
RL
14064 ast_dump_context->dump_type(this->type_);
14065 ast_dump_context->ostream() << ", {";
706cd57f 14066 ast_dump_context->dump_expression_list(this->vals_, this->has_keys_);
16c57fe2
RL
14067 ast_dump_context->ostream() << "})";
14068}
14069
7a938933
ILT
14070// Make a composite literal expression.
14071
14072Expression*
14073Expression::make_composite_literal(Type* type, int depth, bool has_keys,
8ae4c35c 14074 Expression_list* vals, bool all_are_names,
8afa2bfb 14075 Location location)
7a938933
ILT
14076{
14077 return new Composite_literal_expression(type, depth, has_keys, vals,
8ae4c35c 14078 all_are_names, location);
7a938933
ILT
14079}
14080
14081// Return whether this expression is a composite literal.
14082
14083bool
14084Expression::is_composite_literal() const
14085{
14086 switch (this->classification_)
14087 {
14088 case EXPRESSION_COMPOSITE_LITERAL:
14089 case EXPRESSION_STRUCT_CONSTRUCTION:
14090 case EXPRESSION_FIXED_ARRAY_CONSTRUCTION:
14091 case EXPRESSION_OPEN_ARRAY_CONSTRUCTION:
14092 case EXPRESSION_MAP_CONSTRUCTION:
14093 return true;
14094 default:
14095 return false;
14096 }
14097}
14098
14099// Return whether this expression is a composite literal which is not
14100// constant.
14101
14102bool
14103Expression::is_nonconstant_composite_literal() const
14104{
14105 switch (this->classification_)
14106 {
14107 case EXPRESSION_STRUCT_CONSTRUCTION:
14108 {
14109 const Struct_construction_expression *psce =
14110 static_cast<const Struct_construction_expression*>(this);
14111 return !psce->is_constant_struct();
14112 }
14113 case EXPRESSION_FIXED_ARRAY_CONSTRUCTION:
14114 {
14115 const Fixed_array_construction_expression *pace =
14116 static_cast<const Fixed_array_construction_expression*>(this);
14117 return !pace->is_constant_array();
14118 }
14119 case EXPRESSION_OPEN_ARRAY_CONSTRUCTION:
14120 {
14121 const Open_array_construction_expression *pace =
14122 static_cast<const Open_array_construction_expression*>(this);
14123 return !pace->is_constant_array();
14124 }
14125 case EXPRESSION_MAP_CONSTRUCTION:
14126 return true;
14127 default:
14128 return false;
14129 }
14130}
14131
14132// Return true if this is a reference to a local variable.
14133
14134bool
14135Expression::is_local_variable() const
14136{
14137 const Var_expression* ve = this->var_expression();
14138 if (ve == NULL)
14139 return false;
14140 const Named_object* no = ve->named_object();
14141 return (no->is_result_variable()
14142 || (no->is_variable() && !no->var_value()->is_global()));
14143}
14144
14145// Class Type_guard_expression.
14146
14147// Traversal.
14148
14149int
14150Type_guard_expression::do_traverse(Traverse* traverse)
14151{
14152 if (Expression::traverse(&this->expr_, traverse) == TRAVERSE_EXIT
14153 || Type::traverse(this->type_, traverse) == TRAVERSE_EXIT)
14154 return TRAVERSE_EXIT;
14155 return TRAVERSE_CONTINUE;
14156}
14157
14158// Check types of a type guard expression. The expression must have
14159// an interface type, but the actual type conversion is checked at run
14160// time.
14161
14162void
14163Type_guard_expression::do_check_types(Gogo*)
14164{
7a938933 14165 Type* expr_type = this->expr_->type();
c92900d1 14166 if (expr_type->interface_type() == NULL)
05d556e8 14167 {
02ed921a 14168 if (!expr_type->is_error() && !this->type_->is_error())
05d556e8
ILT
14169 this->report_error(_("type assertion only valid for interface types"));
14170 this->set_is_error();
14171 }
7a938933
ILT
14172 else if (this->type_->interface_type() == NULL)
14173 {
14174 std::string reason;
14175 if (!expr_type->interface_type()->implements_interface(this->type_,
14176 &reason))
14177 {
02ed921a 14178 if (!this->type_->is_error())
7a938933 14179 {
05d556e8
ILT
14180 if (reason.empty())
14181 this->report_error(_("impossible type assertion: "
14182 "type does not implement interface"));
14183 else
14184 error_at(this->location(),
14185 ("impossible type assertion: "
14186 "type does not implement interface (%s)"),
14187 reason.c_str());
7a938933 14188 }
05d556e8 14189 this->set_is_error();
7a938933
ILT
14190 }
14191 }
14192}
14193
14194// Return a tree for a type guard expression.
14195
14196tree
14197Type_guard_expression::do_get_tree(Translate_context* context)
14198{
7a938933
ILT
14199 tree expr_tree = this->expr_->get_tree(context);
14200 if (expr_tree == error_mark_node)
14201 return error_mark_node;
c92900d1 14202 if (this->type_->interface_type() != NULL)
7a938933
ILT
14203 return Expression::convert_interface_to_interface(context, this->type_,
14204 this->expr_->type(),
14205 expr_tree, true,
14206 this->location());
14207 else
14208 return Expression::convert_for_assignment(context, this->type_,
14209 this->expr_->type(), expr_tree,
14210 this->location());
14211}
14212
16c57fe2
RL
14213// Dump ast representation for a type guard expression.
14214
14215void
14216Type_guard_expression::do_dump_expression(Ast_dump_context* ast_dump_context)
14217 const
14218{
14219 this->expr_->dump_expression(ast_dump_context);
14220 ast_dump_context->ostream() << ".";
14221 ast_dump_context->dump_type(this->type_);
14222}
14223
7a938933
ILT
14224// Make a type guard expression.
14225
14226Expression*
14227Expression::make_type_guard(Expression* expr, Type* type,
8afa2bfb 14228 Location location)
7a938933
ILT
14229{
14230 return new Type_guard_expression(expr, type, location);
14231}
14232
14233// Class Heap_composite_expression.
14234
14235// When you take the address of a composite literal, it is allocated
14236// on the heap. This class implements that.
14237
14238class Heap_composite_expression : public Expression
14239{
14240 public:
8afa2bfb 14241 Heap_composite_expression(Expression* expr, Location location)
7a938933
ILT
14242 : Expression(EXPRESSION_HEAP_COMPOSITE, location),
14243 expr_(expr)
14244 { }
14245
14246 protected:
14247 int
14248 do_traverse(Traverse* traverse)
14249 { return Expression::traverse(&this->expr_, traverse); }
14250
14251 Type*
14252 do_type()
14253 { return Type::make_pointer_type(this->expr_->type()); }
14254
14255 void
14256 do_determine_type(const Type_context*)
14257 { this->expr_->determine_type_no_context(); }
14258
14259 Expression*
14260 do_copy()
14261 {
14262 return Expression::make_heap_composite(this->expr_->copy(),
14263 this->location());
14264 }
14265
14266 tree
14267 do_get_tree(Translate_context*);
14268
14269 // We only export global objects, and the parser does not generate
14270 // this in global scope.
14271 void
14272 do_export(Export*) const
8c0d1865 14273 { go_unreachable(); }
7a938933 14274
16c57fe2
RL
14275 void
14276 do_dump_expression(Ast_dump_context*) const;
14277
7a938933
ILT
14278 private:
14279 // The composite literal which is being put on the heap.
14280 Expression* expr_;
14281};
14282
14283// Return a tree which allocates a composite literal on the heap.
14284
14285tree
14286Heap_composite_expression::do_get_tree(Translate_context* context)
14287{
14288 tree expr_tree = this->expr_->get_tree(context);
ce842ad6 14289 if (expr_tree == error_mark_node || TREE_TYPE(expr_tree) == error_mark_node)
7a938933
ILT
14290 return error_mark_node;
14291 tree expr_size = TYPE_SIZE_UNIT(TREE_TYPE(expr_tree));
26409c52 14292 go_assert(TREE_CODE(expr_size) == INTEGER_CST);
7a938933
ILT
14293 tree space = context->gogo()->allocate_memory(this->expr_->type(),
14294 expr_size, this->location());
14295 space = fold_convert(build_pointer_type(TREE_TYPE(expr_tree)), space);
14296 space = save_expr(space);
8afa2bfb
SD
14297 tree ref = build_fold_indirect_ref_loc(this->location().gcc_location(),
14298 space);
7a938933
ILT
14299 TREE_THIS_NOTRAP(ref) = 1;
14300 tree ret = build2(COMPOUND_EXPR, TREE_TYPE(space),
14301 build2(MODIFY_EXPR, void_type_node, ref, expr_tree),
14302 space);
8afa2bfb 14303 SET_EXPR_LOCATION(ret, this->location().gcc_location());
7a938933
ILT
14304 return ret;
14305}
14306
16c57fe2
RL
14307// Dump ast representation for a heap composite expression.
14308
14309void
14310Heap_composite_expression::do_dump_expression(
14311 Ast_dump_context* ast_dump_context) const
14312{
14313 ast_dump_context->ostream() << "&(";
14314 ast_dump_context->dump_expression(this->expr_);
14315 ast_dump_context->ostream() << ")";
14316}
14317
7a938933
ILT
14318// Allocate a composite literal on the heap.
14319
14320Expression*
8afa2bfb 14321Expression::make_heap_composite(Expression* expr, Location location)
7a938933
ILT
14322{
14323 return new Heap_composite_expression(expr, location);
14324}
14325
14326// Class Receive_expression.
14327
14328// Return the type of a receive expression.
14329
14330Type*
14331Receive_expression::do_type()
14332{
14333 Channel_type* channel_type = this->channel_->type()->channel_type();
14334 if (channel_type == NULL)
14335 return Type::make_error_type();
14336 return channel_type->element_type();
14337}
14338
14339// Check types for a receive expression.
14340
14341void
14342Receive_expression::do_check_types(Gogo*)
14343{
14344 Type* type = this->channel_->type();
02ed921a 14345 if (type->is_error())
7a938933
ILT
14346 {
14347 this->set_is_error();
14348 return;
14349 }
14350 if (type->channel_type() == NULL)
14351 {
14352 this->report_error(_("expected channel"));
14353 return;
14354 }
14355 if (!type->channel_type()->may_receive())
14356 {
14357 this->report_error(_("invalid receive on send-only channel"));
14358 return;
14359 }
14360}
14361
14362// Get a tree for a receive expression.
14363
14364tree
14365Receive_expression::do_get_tree(Translate_context* context)
14366{
3e68d6d7
ILT
14367 Location loc = this->location();
14368
7a938933 14369 Channel_type* channel_type = this->channel_->type()->channel_type();
bf6526a0
ILT
14370 if (channel_type == NULL)
14371 {
26409c52 14372 go_assert(this->channel_->type()->is_error());
bf6526a0
ILT
14373 return error_mark_node;
14374 }
3e68d6d7
ILT
14375
14376 Expression* td = Expression::make_type_descriptor(channel_type, loc);
14377 tree td_tree = td->get_tree(context);
14378
7a938933 14379 Type* element_type = channel_type->element_type();
5b735706
ILT
14380 Btype* element_type_btype = element_type->get_backend(context->gogo());
14381 tree element_type_tree = type_to_tree(element_type_btype);
7a938933
ILT
14382
14383 tree channel = this->channel_->get_tree(context);
14384 if (element_type_tree == error_mark_node || channel == error_mark_node)
14385 return error_mark_node;
14386
3e68d6d7 14387 return Gogo::receive_from_channel(element_type_tree, td_tree, channel, loc);
7a938933
ILT
14388}
14389
16c57fe2
RL
14390// Dump ast representation for a receive expression.
14391
14392void
14393Receive_expression::do_dump_expression(Ast_dump_context* ast_dump_context) const
14394{
14395 ast_dump_context->ostream() << " <- " ;
14396 ast_dump_context->dump_expression(channel_);
14397}
14398
7a938933
ILT
14399// Make a receive expression.
14400
14401Receive_expression*
8afa2bfb 14402Expression::make_receive(Expression* channel, Location location)
7a938933
ILT
14403{
14404 return new Receive_expression(channel, location);
14405}
14406
7a938933
ILT
14407// An expression which evaluates to a pointer to the type descriptor
14408// of a type.
14409
14410class Type_descriptor_expression : public Expression
14411{
14412 public:
8afa2bfb 14413 Type_descriptor_expression(Type* type, Location location)
7a938933
ILT
14414 : Expression(EXPRESSION_TYPE_DESCRIPTOR, location),
14415 type_(type)
14416 { }
14417
14418 protected:
14419 Type*
14420 do_type()
14421 { return Type::make_type_descriptor_ptr_type(); }
14422
14423 void
14424 do_determine_type(const Type_context*)
14425 { }
14426
14427 Expression*
14428 do_copy()
14429 { return this; }
14430
14431 tree
14432 do_get_tree(Translate_context* context)
70f91024 14433 {
b93e0cfd
CM
14434 Bexpression* ret = this->type_->type_descriptor_pointer(context->gogo(),
14435 this->location());
14436 return expr_to_tree(ret);
70f91024 14437 }
7a938933 14438
16c57fe2
RL
14439 void
14440 do_dump_expression(Ast_dump_context*) const;
14441
7a938933
ILT
14442 private:
14443 // The type for which this is the descriptor.
14444 Type* type_;
14445};
14446
16c57fe2
RL
14447// Dump ast representation for a type descriptor expression.
14448
14449void
14450Type_descriptor_expression::do_dump_expression(
14451 Ast_dump_context* ast_dump_context) const
14452{
14453 ast_dump_context->dump_type(this->type_);
14454}
14455
7a938933
ILT
14456// Make a type descriptor expression.
14457
14458Expression*
8afa2bfb 14459Expression::make_type_descriptor(Type* type, Location location)
7a938933
ILT
14460{
14461 return new Type_descriptor_expression(type, location);
14462}
14463
14464// An expression which evaluates to some characteristic of a type.
14465// This is only used to initialize fields of a type descriptor. Using
14466// a new expression class is slightly inefficient but gives us a good
14467// separation between the frontend and the middle-end with regard to
14468// how types are laid out.
14469
14470class Type_info_expression : public Expression
14471{
14472 public:
14473 Type_info_expression(Type* type, Type_info type_info)
8afa2bfb 14474 : Expression(EXPRESSION_TYPE_INFO, Linemap::predeclared_location()),
7a938933
ILT
14475 type_(type), type_info_(type_info)
14476 { }
14477
14478 protected:
14479 Type*
14480 do_type();
14481
14482 void
14483 do_determine_type(const Type_context*)
14484 { }
14485
14486 Expression*
14487 do_copy()
14488 { return this; }
14489
14490 tree
14491 do_get_tree(Translate_context* context);
14492
16c57fe2
RL
14493 void
14494 do_dump_expression(Ast_dump_context*) const;
14495
7a938933
ILT
14496 private:
14497 // The type for which we are getting information.
14498 Type* type_;
14499 // What information we want.
14500 Type_info type_info_;
14501};
14502
14503// The type is chosen to match what the type descriptor struct
14504// expects.
14505
14506Type*
14507Type_info_expression::do_type()
14508{
14509 switch (this->type_info_)
14510 {
14511 case TYPE_INFO_SIZE:
14512 return Type::lookup_integer_type("uintptr");
14513 case TYPE_INFO_ALIGNMENT:
14514 case TYPE_INFO_FIELD_ALIGNMENT:
14515 return Type::lookup_integer_type("uint8");
14516 default:
8c0d1865 14517 go_unreachable();
7a938933
ILT
14518 }
14519}
14520
14521// Return type information in GENERIC.
14522
14523tree
14524Type_info_expression::do_get_tree(Translate_context* context)
14525{
ef1ed13d
ILT
14526 Btype* btype = this->type_->get_backend(context->gogo());
14527 Gogo* gogo = context->gogo();
14528 size_t val;
14529 switch (this->type_info_)
7a938933 14530 {
ef1ed13d
ILT
14531 case TYPE_INFO_SIZE:
14532 val = gogo->backend()->type_size(btype);
14533 break;
14534 case TYPE_INFO_ALIGNMENT:
14535 val = gogo->backend()->type_alignment(btype);
14536 break;
14537 case TYPE_INFO_FIELD_ALIGNMENT:
14538 val = gogo->backend()->type_field_alignment(btype);
14539 break;
14540 default:
14541 go_unreachable();
7a938933 14542 }
ef1ed13d
ILT
14543 tree val_type_tree = type_to_tree(this->type()->get_backend(gogo));
14544 go_assert(val_type_tree != error_mark_node);
14545 return build_int_cstu(val_type_tree, val);
7a938933
ILT
14546}
14547
16c57fe2
RL
14548// Dump ast representation for a type info expression.
14549
14550void
14551Type_info_expression::do_dump_expression(
14552 Ast_dump_context* ast_dump_context) const
14553{
14554 ast_dump_context->ostream() << "typeinfo(";
14555 ast_dump_context->dump_type(this->type_);
14556 ast_dump_context->ostream() << ",";
14557 ast_dump_context->ostream() <<
14558 (this->type_info_ == TYPE_INFO_ALIGNMENT ? "alignment"
14559 : this->type_info_ == TYPE_INFO_FIELD_ALIGNMENT ? "field alignment"
14560 : this->type_info_ == TYPE_INFO_SIZE ? "size "
14561 : "unknown");
14562 ast_dump_context->ostream() << ")";
14563}
14564
7a938933
ILT
14565// Make a type info expression.
14566
14567Expression*
14568Expression::make_type_info(Type* type, Type_info type_info)
14569{
14570 return new Type_info_expression(type, type_info);
14571}
14572
14573// An expression which evaluates to the offset of a field within a
14574// struct. This, like Type_info_expression, q.v., is only used to
14575// initialize fields of a type descriptor.
14576
14577class Struct_field_offset_expression : public Expression
14578{
14579 public:
14580 Struct_field_offset_expression(Struct_type* type, const Struct_field* field)
8afa2bfb
SD
14581 : Expression(EXPRESSION_STRUCT_FIELD_OFFSET,
14582 Linemap::predeclared_location()),
7a938933
ILT
14583 type_(type), field_(field)
14584 { }
14585
14586 protected:
14587 Type*
14588 do_type()
14589 { return Type::lookup_integer_type("uintptr"); }
14590
14591 void
14592 do_determine_type(const Type_context*)
14593 { }
14594
14595 Expression*
14596 do_copy()
14597 { return this; }
14598
14599 tree
14600 do_get_tree(Translate_context* context);
14601
16c57fe2
RL
14602 void
14603 do_dump_expression(Ast_dump_context*) const;
14604
7a938933
ILT
14605 private:
14606 // The type of the struct.
14607 Struct_type* type_;
14608 // The field.
14609 const Struct_field* field_;
14610};
14611
14612// Return a struct field offset in GENERIC.
14613
14614tree
14615Struct_field_offset_expression::do_get_tree(Translate_context* context)
14616{
5b735706 14617 tree type_tree = type_to_tree(this->type_->get_backend(context->gogo()));
7a938933
ILT
14618 if (type_tree == error_mark_node)
14619 return error_mark_node;
14620
5b735706 14621 tree val_type_tree = type_to_tree(this->type()->get_backend(context->gogo()));
26409c52 14622 go_assert(val_type_tree != error_mark_node);
7a938933
ILT
14623
14624 const Struct_field_list* fields = this->type_->fields();
14625 tree struct_field_tree = TYPE_FIELDS(type_tree);
14626 Struct_field_list::const_iterator p;
14627 for (p = fields->begin();
14628 p != fields->end();
14629 ++p, struct_field_tree = DECL_CHAIN(struct_field_tree))
14630 {
26409c52 14631 go_assert(struct_field_tree != NULL_TREE);
7a938933
ILT
14632 if (&*p == this->field_)
14633 break;
14634 }
26409c52 14635 go_assert(&*p == this->field_);
7a938933
ILT
14636
14637 return fold_convert_loc(BUILTINS_LOCATION, val_type_tree,
14638 byte_position(struct_field_tree));
14639}
14640
16c57fe2
RL
14641// Dump ast representation for a struct field offset expression.
14642
14643void
14644Struct_field_offset_expression::do_dump_expression(
14645 Ast_dump_context* ast_dump_context) const
14646{
14647 ast_dump_context->ostream() << "unsafe.Offsetof(";
ffe08917
ILT
14648 ast_dump_context->dump_type(this->type_);
14649 ast_dump_context->ostream() << '.';
14650 ast_dump_context->ostream() <<
14651 Gogo::message_name(this->field_->field_name());
16c57fe2
RL
14652 ast_dump_context->ostream() << ")";
14653}
14654
7a938933
ILT
14655// Make an expression for a struct field offset.
14656
14657Expression*
14658Expression::make_struct_field_offset(Struct_type* type,
14659 const Struct_field* field)
14660{
14661 return new Struct_field_offset_expression(type, field);
14662}
14663
3b8dffe7
ILT
14664// An expression which evaluates to a pointer to the map descriptor of
14665// a map type.
14666
14667class Map_descriptor_expression : public Expression
14668{
14669 public:
8afa2bfb 14670 Map_descriptor_expression(Map_type* type, Location location)
3b8dffe7
ILT
14671 : Expression(EXPRESSION_MAP_DESCRIPTOR, location),
14672 type_(type)
14673 { }
14674
14675 protected:
14676 Type*
14677 do_type()
14678 { return Type::make_pointer_type(Map_type::make_map_descriptor_type()); }
14679
14680 void
14681 do_determine_type(const Type_context*)
14682 { }
14683
14684 Expression*
14685 do_copy()
14686 { return this; }
14687
14688 tree
14689 do_get_tree(Translate_context* context)
14690 {
b93e0cfd
CM
14691 Bexpression* ret = this->type_->map_descriptor_pointer(context->gogo(),
14692 this->location());
14693 return expr_to_tree(ret);
3b8dffe7
ILT
14694 }
14695
16c57fe2
RL
14696 void
14697 do_dump_expression(Ast_dump_context*) const;
14698
3b8dffe7
ILT
14699 private:
14700 // The type for which this is the descriptor.
14701 Map_type* type_;
14702};
14703
16c57fe2
RL
14704// Dump ast representation for a map descriptor expression.
14705
14706void
14707Map_descriptor_expression::do_dump_expression(
14708 Ast_dump_context* ast_dump_context) const
14709{
14710 ast_dump_context->ostream() << "map_descriptor(";
14711 ast_dump_context->dump_type(this->type_);
14712 ast_dump_context->ostream() << ")";
14713}
14714
3b8dffe7
ILT
14715// Make a map descriptor expression.
14716
14717Expression*
8afa2bfb 14718Expression::make_map_descriptor(Map_type* type, Location location)
3b8dffe7
ILT
14719{
14720 return new Map_descriptor_expression(type, location);
14721}
14722
7a938933
ILT
14723// An expression which evaluates to the address of an unnamed label.
14724
14725class Label_addr_expression : public Expression
14726{
14727 public:
8afa2bfb 14728 Label_addr_expression(Label* label, Location location)
7a938933
ILT
14729 : Expression(EXPRESSION_LABEL_ADDR, location),
14730 label_(label)
14731 { }
14732
14733 protected:
14734 Type*
14735 do_type()
14736 { return Type::make_pointer_type(Type::make_void_type()); }
14737
14738 void
14739 do_determine_type(const Type_context*)
14740 { }
14741
14742 Expression*
14743 do_copy()
14744 { return new Label_addr_expression(this->label_, this->location()); }
14745
14746 tree
d56e6679
ILT
14747 do_get_tree(Translate_context* context)
14748 {
db0adf82 14749 return expr_to_tree(this->label_->get_addr(context, this->location()));
d56e6679 14750 }
7a938933 14751
16c57fe2
RL
14752 void
14753 do_dump_expression(Ast_dump_context* ast_dump_context) const
14754 { ast_dump_context->ostream() << this->label_->name(); }
14755
7a938933
ILT
14756 private:
14757 // The label whose address we are taking.
14758 Label* label_;
14759};
14760
14761// Make an expression for the address of an unnamed label.
14762
14763Expression*
8afa2bfb 14764Expression::make_label_addr(Label* label, Location location)
7a938933
ILT
14765{
14766 return new Label_addr_expression(label, location);
14767}
14768
14769// Import an expression. This comes at the end in order to see the
14770// various class definitions.
14771
14772Expression*
14773Expression::import_expression(Import* imp)
14774{
14775 int c = imp->peek_char();
14776 if (imp->match_c_string("- ")
14777 || imp->match_c_string("! ")
14778 || imp->match_c_string("^ "))
14779 return Unary_expression::do_import(imp);
14780 else if (c == '(')
14781 return Binary_expression::do_import(imp);
14782 else if (imp->match_c_string("true")
14783 || imp->match_c_string("false"))
14784 return Boolean_expression::do_import(imp);
14785 else if (c == '"')
14786 return String_expression::do_import(imp);
14787 else if (c == '-' || (c >= '0' && c <= '9'))
14788 {
14789 // This handles integers, floats and complex constants.
14790 return Integer_expression::do_import(imp);
14791 }
14792 else if (imp->match_c_string("nil"))
14793 return Nil_expression::do_import(imp);
14794 else if (imp->match_c_string("convert"))
14795 return Type_conversion_expression::do_import(imp);
14796 else
14797 {
14798 error_at(imp->location(), "import error: expected expression");
14799 return Expression::make_error(imp->location());
14800 }
14801}
14802
14803// Class Expression_list.
14804
14805// Traverse the list.
14806
14807int
14808Expression_list::traverse(Traverse* traverse)
14809{
14810 for (Expression_list::iterator p = this->begin();
14811 p != this->end();
14812 ++p)
14813 {
14814 if (*p != NULL)
14815 {
14816 if (Expression::traverse(&*p, traverse) == TRAVERSE_EXIT)
14817 return TRAVERSE_EXIT;
14818 }
14819 }
14820 return TRAVERSE_CONTINUE;
14821}
14822
14823// Copy the list.
14824
14825Expression_list*
14826Expression_list::copy()
14827{
14828 Expression_list* ret = new Expression_list();
14829 for (Expression_list::iterator p = this->begin();
14830 p != this->end();
14831 ++p)
14832 {
14833 if (*p == NULL)
14834 ret->push_back(NULL);
14835 else
14836 ret->push_back((*p)->copy());
14837 }
14838 return ret;
14839}
14840
14841// Return whether an expression list has an error expression.
14842
14843bool
14844Expression_list::contains_error() const
14845{
14846 for (Expression_list::const_iterator p = this->begin();
14847 p != this->end();
14848 ++p)
14849 if (*p != NULL && (*p)->is_error_expression())
14850 return true;
14851 return false;
14852}
5caf63ca
ILT
14853
14854// Class Numeric_constant.
14855
14856// Destructor.
14857
14858Numeric_constant::~Numeric_constant()
14859{
14860 this->clear();
14861}
14862
14863// Copy constructor.
14864
14865Numeric_constant::Numeric_constant(const Numeric_constant& a)
14866 : classification_(a.classification_), type_(a.type_)
14867{
14868 switch (a.classification_)
14869 {
14870 case NC_INVALID:
14871 break;
14872 case NC_INT:
14873 case NC_RUNE:
14874 mpz_init_set(this->u_.int_val, a.u_.int_val);
14875 break;
14876 case NC_FLOAT:
14877 mpfr_init_set(this->u_.float_val, a.u_.float_val, GMP_RNDN);
14878 break;
14879 case NC_COMPLEX:
14880 mpfr_init_set(this->u_.complex_val.real, a.u_.complex_val.real,
14881 GMP_RNDN);
14882 mpfr_init_set(this->u_.complex_val.imag, a.u_.complex_val.imag,
14883 GMP_RNDN);
14884 break;
14885 default:
14886 go_unreachable();
14887 }
14888}
14889
14890// Assignment operator.
14891
14892Numeric_constant&
14893Numeric_constant::operator=(const Numeric_constant& a)
14894{
14895 this->clear();
14896 this->classification_ = a.classification_;
14897 this->type_ = a.type_;
14898 switch (a.classification_)
14899 {
14900 case NC_INVALID:
14901 break;
14902 case NC_INT:
14903 case NC_RUNE:
14904 mpz_init_set(this->u_.int_val, a.u_.int_val);
14905 break;
14906 case NC_FLOAT:
14907 mpfr_init_set(this->u_.float_val, a.u_.float_val, GMP_RNDN);
14908 break;
14909 case NC_COMPLEX:
14910 mpfr_init_set(this->u_.complex_val.real, a.u_.complex_val.real,
14911 GMP_RNDN);
14912 mpfr_init_set(this->u_.complex_val.imag, a.u_.complex_val.imag,
14913 GMP_RNDN);
14914 break;
14915 default:
14916 go_unreachable();
14917 }
14918 return *this;
14919}
14920
14921// Clear the contents.
14922
14923void
14924Numeric_constant::clear()
14925{
14926 switch (this->classification_)
14927 {
14928 case NC_INVALID:
14929 break;
14930 case NC_INT:
14931 case NC_RUNE:
14932 mpz_clear(this->u_.int_val);
14933 break;
14934 case NC_FLOAT:
14935 mpfr_clear(this->u_.float_val);
14936 break;
14937 case NC_COMPLEX:
14938 mpfr_clear(this->u_.complex_val.real);
14939 mpfr_clear(this->u_.complex_val.imag);
14940 break;
14941 default:
14942 go_unreachable();
14943 }
14944 this->classification_ = NC_INVALID;
14945}
14946
14947// Set to an unsigned long value.
14948
14949void
14950Numeric_constant::set_unsigned_long(Type* type, unsigned long val)
14951{
14952 this->clear();
14953 this->classification_ = NC_INT;
14954 this->type_ = type;
14955 mpz_init_set_ui(this->u_.int_val, val);
14956}
14957
14958// Set to an integer value.
14959
14960void
14961Numeric_constant::set_int(Type* type, const mpz_t val)
14962{
14963 this->clear();
14964 this->classification_ = NC_INT;
14965 this->type_ = type;
14966 mpz_init_set(this->u_.int_val, val);
14967}
14968
14969// Set to a rune value.
14970
14971void
14972Numeric_constant::set_rune(Type* type, const mpz_t val)
14973{
14974 this->clear();
14975 this->classification_ = NC_RUNE;
14976 this->type_ = type;
14977 mpz_init_set(this->u_.int_val, val);
14978}
14979
14980// Set to a floating point value.
14981
14982void
14983Numeric_constant::set_float(Type* type, const mpfr_t val)
14984{
14985 this->clear();
14986 this->classification_ = NC_FLOAT;
14987 this->type_ = type;
073d123c
ILT
14988 // Numeric constants do not have negative zero values, so remove
14989 // them here. They also don't have infinity or NaN values, but we
14990 // should never see them here.
14991 if (mpfr_zero_p(val))
14992 mpfr_init_set_ui(this->u_.float_val, 0, GMP_RNDN);
14993 else
14994 mpfr_init_set(this->u_.float_val, val, GMP_RNDN);
5caf63ca
ILT
14995}
14996
14997// Set to a complex value.
14998
14999void
15000Numeric_constant::set_complex(Type* type, const mpfr_t real, const mpfr_t imag)
15001{
15002 this->clear();
15003 this->classification_ = NC_COMPLEX;
15004 this->type_ = type;
15005 mpfr_init_set(this->u_.complex_val.real, real, GMP_RNDN);
15006 mpfr_init_set(this->u_.complex_val.imag, imag, GMP_RNDN);
15007}
15008
15009// Get an int value.
15010
15011void
15012Numeric_constant::get_int(mpz_t* val) const
15013{
15014 go_assert(this->is_int());
15015 mpz_init_set(*val, this->u_.int_val);
15016}
15017
15018// Get a rune value.
15019
15020void
15021Numeric_constant::get_rune(mpz_t* val) const
15022{
15023 go_assert(this->is_rune());
15024 mpz_init_set(*val, this->u_.int_val);
15025}
15026
15027// Get a floating point value.
15028
15029void
15030Numeric_constant::get_float(mpfr_t* val) const
15031{
15032 go_assert(this->is_float());
15033 mpfr_init_set(*val, this->u_.float_val, GMP_RNDN);
15034}
15035
15036// Get a complex value.
15037
15038void
15039Numeric_constant::get_complex(mpfr_t* real, mpfr_t* imag) const
15040{
15041 go_assert(this->is_complex());
15042 mpfr_init_set(*real, this->u_.complex_val.real, GMP_RNDN);
15043 mpfr_init_set(*imag, this->u_.complex_val.imag, GMP_RNDN);
15044}
15045
15046// Express value as unsigned long if possible.
15047
15048Numeric_constant::To_unsigned_long
15049Numeric_constant::to_unsigned_long(unsigned long* val) const
15050{
15051 switch (this->classification_)
15052 {
15053 case NC_INT:
15054 case NC_RUNE:
15055 return this->mpz_to_unsigned_long(this->u_.int_val, val);
15056 case NC_FLOAT:
15057 return this->mpfr_to_unsigned_long(this->u_.float_val, val);
15058 case NC_COMPLEX:
15059 if (!mpfr_zero_p(this->u_.complex_val.imag))
15060 return NC_UL_NOTINT;
15061 return this->mpfr_to_unsigned_long(this->u_.complex_val.real, val);
15062 default:
15063 go_unreachable();
15064 }
15065}
15066
15067// Express integer value as unsigned long if possible.
15068
15069Numeric_constant::To_unsigned_long
15070Numeric_constant::mpz_to_unsigned_long(const mpz_t ival,
15071 unsigned long *val) const
15072{
15073 if (mpz_sgn(ival) < 0)
15074 return NC_UL_NEGATIVE;
15075 unsigned long ui = mpz_get_ui(ival);
15076 if (mpz_cmp_ui(ival, ui) != 0)
15077 return NC_UL_BIG;
15078 *val = ui;
15079 return NC_UL_VALID;
15080}
15081
15082// Express floating point value as unsigned long if possible.
15083
15084Numeric_constant::To_unsigned_long
15085Numeric_constant::mpfr_to_unsigned_long(const mpfr_t fval,
15086 unsigned long *val) const
15087{
15088 if (!mpfr_integer_p(fval))
15089 return NC_UL_NOTINT;
15090 mpz_t ival;
15091 mpz_init(ival);
15092 mpfr_get_z(ival, fval, GMP_RNDN);
15093 To_unsigned_long ret = this->mpz_to_unsigned_long(ival, val);
15094 mpz_clear(ival);
15095 return ret;
15096}
15097
15098// Convert value to integer if possible.
15099
15100bool
15101Numeric_constant::to_int(mpz_t* val) const
15102{
15103 switch (this->classification_)
15104 {
15105 case NC_INT:
15106 case NC_RUNE:
15107 mpz_init_set(*val, this->u_.int_val);
15108 return true;
15109 case NC_FLOAT:
15110 if (!mpfr_integer_p(this->u_.float_val))
15111 return false;
15112 mpz_init(*val);
15113 mpfr_get_z(*val, this->u_.float_val, GMP_RNDN);
15114 return true;
15115 case NC_COMPLEX:
15116 if (!mpfr_zero_p(this->u_.complex_val.imag)
15117 || !mpfr_integer_p(this->u_.complex_val.real))
15118 return false;
15119 mpz_init(*val);
15120 mpfr_get_z(*val, this->u_.complex_val.real, GMP_RNDN);
15121 return true;
15122 default:
15123 go_unreachable();
15124 }
15125}
15126
15127// Convert value to floating point if possible.
15128
15129bool
15130Numeric_constant::to_float(mpfr_t* val) const
15131{
15132 switch (this->classification_)
15133 {
15134 case NC_INT:
15135 case NC_RUNE:
15136 mpfr_init_set_z(*val, this->u_.int_val, GMP_RNDN);
15137 return true;
15138 case NC_FLOAT:
15139 mpfr_init_set(*val, this->u_.float_val, GMP_RNDN);
15140 return true;
15141 case NC_COMPLEX:
15142 if (!mpfr_zero_p(this->u_.complex_val.imag))
15143 return false;
15144 mpfr_init_set(*val, this->u_.complex_val.real, GMP_RNDN);
15145 return true;
15146 default:
15147 go_unreachable();
15148 }
15149}
15150
15151// Convert value to complex.
15152
15153bool
15154Numeric_constant::to_complex(mpfr_t* vr, mpfr_t* vi) const
15155{
15156 switch (this->classification_)
15157 {
15158 case NC_INT:
15159 case NC_RUNE:
15160 mpfr_init_set_z(*vr, this->u_.int_val, GMP_RNDN);
15161 mpfr_init_set_ui(*vi, 0, GMP_RNDN);
15162 return true;
15163 case NC_FLOAT:
15164 mpfr_init_set(*vr, this->u_.float_val, GMP_RNDN);
15165 mpfr_init_set_ui(*vi, 0, GMP_RNDN);
15166 return true;
15167 case NC_COMPLEX:
15168 mpfr_init_set(*vr, this->u_.complex_val.real, GMP_RNDN);
15169 mpfr_init_set(*vi, this->u_.complex_val.imag, GMP_RNDN);
15170 return true;
15171 default:
15172 go_unreachable();
15173 }
15174}
15175
15176// Get the type.
15177
15178Type*
15179Numeric_constant::type() const
15180{
15181 if (this->type_ != NULL)
15182 return this->type_;
15183 switch (this->classification_)
15184 {
15185 case NC_INT:
15186 return Type::make_abstract_integer_type();
15187 case NC_RUNE:
15188 return Type::make_abstract_character_type();
15189 case NC_FLOAT:
15190 return Type::make_abstract_float_type();
15191 case NC_COMPLEX:
15192 return Type::make_abstract_complex_type();
15193 default:
15194 go_unreachable();
15195 }
15196}
15197
15198// If the constant can be expressed in TYPE, then set the type of the
15199// constant to TYPE and return true. Otherwise return false, and, if
15200// ISSUE_ERROR is true, report an appropriate error message.
15201
15202bool
15203Numeric_constant::set_type(Type* type, bool issue_error, Location loc)
15204{
15205 bool ret;
15206 if (type == NULL)
15207 ret = true;
15208 else if (type->integer_type() != NULL)
15209 ret = this->check_int_type(type->integer_type(), issue_error, loc);
15210 else if (type->float_type() != NULL)
15211 ret = this->check_float_type(type->float_type(), issue_error, loc);
15212 else if (type->complex_type() != NULL)
15213 ret = this->check_complex_type(type->complex_type(), issue_error, loc);
15214 else
15215 go_unreachable();
15216 if (ret)
15217 this->type_ = type;
15218 return ret;
15219}
15220
15221// Check whether the constant can be expressed in an integer type.
15222
15223bool
15224Numeric_constant::check_int_type(Integer_type* type, bool issue_error,
15225 Location location) const
15226{
15227 mpz_t val;
15228 switch (this->classification_)
15229 {
15230 case NC_INT:
15231 case NC_RUNE:
15232 mpz_init_set(val, this->u_.int_val);
15233 break;
15234
15235 case NC_FLOAT:
15236 if (!mpfr_integer_p(this->u_.float_val))
15237 {
15238 if (issue_error)
15239 error_at(location, "floating point constant truncated to integer");
15240 return false;
15241 }
15242 mpz_init(val);
15243 mpfr_get_z(val, this->u_.float_val, GMP_RNDN);
15244 break;
15245
15246 case NC_COMPLEX:
15247 if (!mpfr_integer_p(this->u_.complex_val.real)
15248 || !mpfr_zero_p(this->u_.complex_val.imag))
15249 {
15250 if (issue_error)
15251 error_at(location, "complex constant truncated to integer");
15252 return false;
15253 }
15254 mpz_init(val);
15255 mpfr_get_z(val, this->u_.complex_val.real, GMP_RNDN);
15256 break;
15257
15258 default:
15259 go_unreachable();
15260 }
15261
15262 bool ret;
15263 if (type->is_abstract())
15264 ret = true;
15265 else
15266 {
15267 int bits = mpz_sizeinbase(val, 2);
15268 if (type->is_unsigned())
15269 {
15270 // For an unsigned type we can only accept a nonnegative
15271 // number, and we must be able to represents at least BITS.
15272 ret = mpz_sgn(val) >= 0 && bits <= type->bits();
15273 }
15274 else
15275 {
15276 // For a signed type we need an extra bit to indicate the
15277 // sign. We have to handle the most negative integer
15278 // specially.
15279 ret = (bits + 1 <= type->bits()
15280 || (bits <= type->bits()
15281 && mpz_sgn(val) < 0
15282 && (mpz_scan1(val, 0)
15283 == static_cast<unsigned long>(type->bits() - 1))
15284 && mpz_scan0(val, type->bits()) == ULONG_MAX));
15285 }
15286 }
15287
15288 if (!ret && issue_error)
15289 error_at(location, "integer constant overflow");
15290
15291 return ret;
15292}
15293
15294// Check whether the constant can be expressed in a floating point
15295// type.
15296
15297bool
15298Numeric_constant::check_float_type(Float_type* type, bool issue_error,
405c87c4 15299 Location location)
5caf63ca
ILT
15300{
15301 mpfr_t val;
15302 switch (this->classification_)
15303 {
15304 case NC_INT:
15305 case NC_RUNE:
15306 mpfr_init_set_z(val, this->u_.int_val, GMP_RNDN);
15307 break;
15308
15309 case NC_FLOAT:
15310 mpfr_init_set(val, this->u_.float_val, GMP_RNDN);
15311 break;
15312
15313 case NC_COMPLEX:
15314 if (!mpfr_zero_p(this->u_.complex_val.imag))
15315 {
15316 if (issue_error)
15317 error_at(location, "complex constant truncated to float");
15318 return false;
15319 }
15320 mpfr_init_set(val, this->u_.complex_val.real, GMP_RNDN);
15321 break;
15322
15323 default:
15324 go_unreachable();
15325 }
15326
15327 bool ret;
15328 if (type->is_abstract())
15329 ret = true;
15330 else if (mpfr_nan_p(val) || mpfr_inf_p(val) || mpfr_zero_p(val))
15331 {
15332 // A NaN or Infinity always fits in the range of the type.
15333 ret = true;
15334 }
15335 else
15336 {
15337 mp_exp_t exp = mpfr_get_exp(val);
15338 mp_exp_t max_exp;
15339 switch (type->bits())
15340 {
15341 case 32:
15342 max_exp = 128;
15343 break;
15344 case 64:
15345 max_exp = 1024;
15346 break;
15347 default:
15348 go_unreachable();
15349 }
15350
15351 ret = exp <= max_exp;
405c87c4
ILT
15352
15353 if (ret)
15354 {
15355 // Round the constant to the desired type.
15356 mpfr_t t;
15357 mpfr_init(t);
15358 switch (type->bits())
15359 {
15360 case 32:
15361 mpfr_set_prec(t, 24);
15362 break;
15363 case 64:
15364 mpfr_set_prec(t, 53);
15365 break;
15366 default:
15367 go_unreachable();
15368 }
15369 mpfr_set(t, val, GMP_RNDN);
15370 mpfr_set(val, t, GMP_RNDN);
15371 mpfr_clear(t);
15372
15373 this->set_float(type, val);
15374 }
5caf63ca
ILT
15375 }
15376
15377 mpfr_clear(val);
15378
15379 if (!ret && issue_error)
15380 error_at(location, "floating point constant overflow");
15381
15382 return ret;
15383}
15384
15385// Check whether the constant can be expressed in a complex type.
15386
15387bool
15388Numeric_constant::check_complex_type(Complex_type* type, bool issue_error,
405c87c4 15389 Location location)
5caf63ca
ILT
15390{
15391 if (type->is_abstract())
15392 return true;
15393
15394 mp_exp_t max_exp;
15395 switch (type->bits())
15396 {
15397 case 64:
15398 max_exp = 128;
15399 break;
15400 case 128:
15401 max_exp = 1024;
15402 break;
15403 default:
15404 go_unreachable();
15405 }
15406
15407 mpfr_t real;
405c87c4 15408 mpfr_t imag;
5caf63ca
ILT
15409 switch (this->classification_)
15410 {
15411 case NC_INT:
15412 case NC_RUNE:
15413 mpfr_init_set_z(real, this->u_.int_val, GMP_RNDN);
405c87c4 15414 mpfr_init_set_ui(imag, 0, GMP_RNDN);
5caf63ca
ILT
15415 break;
15416
15417 case NC_FLOAT:
15418 mpfr_init_set(real, this->u_.float_val, GMP_RNDN);
405c87c4 15419 mpfr_init_set_ui(imag, 0, GMP_RNDN);
5caf63ca
ILT
15420 break;
15421
15422 case NC_COMPLEX:
5caf63ca 15423 mpfr_init_set(real, this->u_.complex_val.real, GMP_RNDN);
405c87c4 15424 mpfr_init_set(imag, this->u_.complex_val.imag, GMP_RNDN);
5caf63ca
ILT
15425 break;
15426
15427 default:
15428 go_unreachable();
15429 }
15430
405c87c4
ILT
15431 bool ret = true;
15432 if (!mpfr_nan_p(real)
15433 && !mpfr_inf_p(real)
15434 && !mpfr_zero_p(real)
15435 && mpfr_get_exp(real) > max_exp)
15436 {
15437 if (issue_error)
15438 error_at(location, "complex real part overflow");
15439 ret = false;
15440 }
5caf63ca 15441
405c87c4
ILT
15442 if (!mpfr_nan_p(imag)
15443 && !mpfr_inf_p(imag)
15444 && !mpfr_zero_p(imag)
15445 && mpfr_get_exp(imag) > max_exp)
15446 {
15447 if (issue_error)
15448 error_at(location, "complex imaginary part overflow");
15449 ret = false;
15450 }
5caf63ca 15451
405c87c4
ILT
15452 if (ret)
15453 {
15454 // Round the constant to the desired type.
15455 mpfr_t t;
15456 mpfr_init(t);
15457 switch (type->bits())
15458 {
15459 case 64:
15460 mpfr_set_prec(t, 24);
15461 break;
15462 case 128:
15463 mpfr_set_prec(t, 53);
15464 break;
15465 default:
15466 go_unreachable();
15467 }
15468 mpfr_set(t, real, GMP_RNDN);
15469 mpfr_set(real, t, GMP_RNDN);
15470 mpfr_set(t, imag, GMP_RNDN);
15471 mpfr_set(imag, t, GMP_RNDN);
15472 mpfr_clear(t);
15473
15474 this->set_complex(type, real, imag);
15475 }
15476
15477 mpfr_clear(real);
15478 mpfr_clear(imag);
5caf63ca
ILT
15479
15480 return ret;
15481}
15482
15483// Return an Expression for this value.
15484
15485Expression*
15486Numeric_constant::expression(Location loc) const
15487{
15488 switch (this->classification_)
15489 {
15490 case NC_INT:
15491 return Expression::make_integer(&this->u_.int_val, this->type_, loc);
15492 case NC_RUNE:
15493 return Expression::make_character(&this->u_.int_val, this->type_, loc);
15494 case NC_FLOAT:
15495 return Expression::make_float(&this->u_.float_val, this->type_, loc);
15496 case NC_COMPLEX:
15497 return Expression::make_complex(&this->u_.complex_val.real,
15498 &this->u_.complex_val.imag,
15499 this->type_, loc);
15500 default:
15501 go_unreachable();
15502 }
15503}